fix bot issues : Prevent bot creation when enable_bots is false

This commit is contained in:
2025-04-22 10:34:08 +08:00
parent d397ef0931
commit f8521a78ff
2 changed files with 68 additions and 27 deletions
+13 -12
View File
@@ -385,10 +385,10 @@ func _on_host_pressed():
# Sync goals to all clients after host is set up # Sync goals to all clients after host is set up
rpc("sync_preset_goals", preset_goals) rpc("sync_preset_goals", preset_goals)
# Only add bots if enable_bots is true # Only add bots if enable_bots is true and we need them
if enable_bots: if enable_bots:
# Add bots with their own goals var needed_bots = max_players - 1 # -1 for the host
for i in range(2, max_players + 1): for i in range(2, needed_bots + 2): # +2 because we start from ID 2
add_bot(i) add_bot(i)
start_game() start_game()
@@ -644,7 +644,9 @@ func _on_peer_disconnected(peer_id):
if multiplayer.is_server(): if multiplayer.is_server():
connected_peer_ids.erase(peer_id) connected_peer_ids.erase(peer_id)
players.erase(peer_id) players.erase(peer_id)
add_bot(get_next_available_bot_id()) # Only add replacement bot if bots are enabled
if enable_bots:
add_bot(get_next_available_bot_id())
@rpc("any_peer", "call_local") @rpc("any_peer", "call_local")
func add_player_character(peer_id): func add_player_character(peer_id):
@@ -735,6 +737,13 @@ func add_bot(bot_id):
@rpc("call_local") @rpc("call_local")
func create_bot(bot_id): func create_bot(bot_id):
# First check if bots are enabled
if not enable_bots:
print("Attempted to create bot while bots are disabled")
return
# Ensure we're not duplicating bots # Ensure we're not duplicating bots
if has_node(str(bot_id)): if has_node(str(bot_id)):
push_error("Bot already exists: " + str(bot_id)) push_error("Bot already exists: " + str(bot_id))
@@ -755,14 +764,6 @@ func create_bot(bot_id):
bot_character.add_to_group("Players", true) bot_character.add_to_group("Players", true)
bot_character.add_to_group("Bots", true) bot_character.add_to_group("Bots", true)
if not enable_bots:
bot_character.set_process(false)
bot_character.set_physics_process(false)
# Disable Beehave tree if it exists
var behavior_tree = bot_character.get_node_or_null("BehaviorTree")
if behavior_tree:
behavior_tree.enabled = false
if multiplayer.is_server(): if multiplayer.is_server():
bots.append(bot_id) bots.append(bot_id)
players.append(bot_id) players.append(bot_id)
+54 -14
View File
@@ -184,6 +184,8 @@ func _ready():
if is_multiplayer_authority(): if is_multiplayer_authority():
rpc("sync_position", current_position) rpc("sync_position", current_position)
# Add function to check if position is at finish line # Add function to check if position is at finish line
func is_at_finish_line() -> bool: func is_at_finish_line() -> bool:
return current_position in finish_locations return current_position in finish_locations
@@ -247,12 +249,6 @@ func finish_race():
if is_multiplayer_authority(): if is_multiplayer_authority():
rpc("display_message", message) rpc("display_message", message)
# Notify game completion
var main = get_tree().get_root().get_node_or_null("Main")
if main and is_multiplayer_authority():
main.player_finished_race(self)
# Add function to check 3x3 pattern matching anywhere in 5x5 playerboard # Add function to check 3x3 pattern matching anywhere in 5x5 playerboard
func check_pattern_match() -> bool: func check_pattern_match() -> bool:
# Early return if playerboard or goals are not properly sized # Early return if playerboard or goals are not properly sized
@@ -319,7 +315,7 @@ func start_new_lap():
goals = second_lap_goals.duplicate() goals = second_lap_goals.duplicate()
# Reset playerboard # Reset playerboard
playerboard.fill(-1) #playerboard.fill(-1)
# Reset can_finish flag # Reset can_finish flag
can_finish = false can_finish = false
@@ -446,10 +442,14 @@ func ping_existence():
# They can check if they have this node # They can check if they have this node
pass pass
func _physics_process(_delta): func _physics_process(delta):
if is_multiplayer_authority(): if is_multiplayer_authority():
rpc("remote_set_position", global_position) rpc("remote_set_position", global_position)
# Add continuous finish line check
if current_position in finish_locations and can_finish and not is_player_moving:
start_new_lap()
func _unhandled_input(event): func _unhandled_input(event):
# Early return if not authorized human player # Early return if not authorized human player
if not is_multiplayer_authority() or is_bot or is_in_group("Bots"): if not is_multiplayer_authority() or is_bot or is_in_group("Bots"):
@@ -686,6 +686,13 @@ func move_player_to_clicked_position(grid_position: Vector2i):
if not is_multiplayer_authority() or is_player_moving or action_points <= 0: if not is_multiplayer_authority() or is_player_moving or action_points <= 0:
return return
# Check if trying to move to finish line
if grid_position in finish_locations:
if not can_finish:
can_finish = check_pattern_match()
if not can_finish:
return # Cannot move to finish line if pattern doesn't match
var is_valid_finish = false var is_valid_finish = false
# Make scenario for match checking laps, for handle lap count # Make scenario for match checking laps, for handle lap count
if current_lap == 0: # first lap if current_lap == 0: # first lap
@@ -693,15 +700,11 @@ func move_player_to_clicked_position(grid_position: Vector2i):
else: # second lap else: # second lap
is_valid_finish = grid_position in spawn_locations is_valid_finish = grid_position in spawn_locations
if is_valid_finish:
if not can_finish:
can_finish = check_pattern_match()
if not can_finish:
return
if not is_within_movement_range(grid_position): if not is_within_movement_range(grid_position):
return return
var main = get_tree().get_root().get_node_or_null("Main") var main = get_tree().get_root().get_node_or_null("Main")
if not main or main.current_action_state != main.ActionState.MOVING or not grid_position in highlighted_cells: if not main or main.current_action_state != main.ActionState.MOVING or not grid_position in highlighted_cells:
return return
@@ -751,6 +754,10 @@ func start_movement_along_path(path: Array, clear_visual: bool = true):
current_position = Vector2i(path[-1].x, path[-1].y) current_position = Vector2i(path[-1].x, path[-1].y)
is_player_moving = false is_player_moving = false
# Check if we've reached the finish line
if current_position in finish_locations and can_finish:
finish_race()
var main = get_tree().get_root().get_node_or_null("Main") var main = get_tree().get_root().get_node_or_null("Main")
# Only clear visuals if this is a human player # Only clear visuals if this is a human player
@@ -773,6 +780,39 @@ func start_movement_along_path(path: Array, clear_visual: bool = true):
_after_action_completed() _after_action_completed()
) )
#func trigger_finish_line():
#if not is_multiplayer_authority():
#return
#
#if current_lap == 0: # First lap
#lap1_finishers += 1
#race_position = lap1_finishers
#
## Display first lap completion message
#var message = "Finish 1st lap on " + get_ordinal_string(race_position)
#rpc("display_message", message)
#print("DEBUG: Triggered first lap finish. Position: ", race_position)
#
## Start second lap
#current_lap += 1
#rpc("start_new_lap")
#
#elif current_lap == 1: # Second lap
#lap2_finishers += 1
#race_position = lap2_finishers
#
## Display second lap completion message
#var message = "Finish 2nd lap on " + get_ordinal_string(race_position)
#rpc("display_message", message)
#print("DEBUG: Triggered second lap finish. Position: ", race_position)
#
##func debug_finish_state():
##print("DEBUG: Current Position: ", current_position)
##print("DEBUG: Can Finish: ", can_finish)
##print("DEBUG: Current Lap: ", current_lap)
##print("DEBUG: Pattern Match: ", check_pattern_match())
##print("DEBUG: Is at finish: ", current_position in finish_locations)
func update_player_position(grid_position: Vector2i): func update_player_position(grid_position: Vector2i):
position = grid_to_world(grid_position) position = grid_to_world(grid_position)