From f8521a78ff3100142819145dd472d08ff9877aec Mon Sep 17 00:00:00 2001 From: adtpdn Date: Tue, 22 Apr 2025 10:34:08 +0800 Subject: [PATCH] fix bot issues : Prevent bot creation when enable_bots is false --- scenes/main.gd | 25 ++++++++--------- scenes/player.gd | 70 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 68 insertions(+), 27 deletions(-) diff --git a/scenes/main.gd b/scenes/main.gd index 793c760..27f2f19 100644 --- a/scenes/main.gd +++ b/scenes/main.gd @@ -385,10 +385,10 @@ func _on_host_pressed(): # Sync goals to all clients after host is set up 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: - # Add bots with their own goals - for i in range(2, max_players + 1): + var needed_bots = max_players - 1 # -1 for the host + for i in range(2, needed_bots + 2): # +2 because we start from ID 2 add_bot(i) start_game() @@ -644,7 +644,9 @@ func _on_peer_disconnected(peer_id): if multiplayer.is_server(): connected_peer_ids.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") func add_player_character(peer_id): @@ -735,6 +737,13 @@ func add_bot(bot_id): @rpc("call_local") 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 if has_node(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("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(): bots.append(bot_id) players.append(bot_id) diff --git a/scenes/player.gd b/scenes/player.gd index ba35d19..45f5eb0 100644 --- a/scenes/player.gd +++ b/scenes/player.gd @@ -184,6 +184,8 @@ func _ready(): if is_multiplayer_authority(): rpc("sync_position", current_position) + + # Add function to check if position is at finish line func is_at_finish_line() -> bool: return current_position in finish_locations @@ -246,12 +248,6 @@ func finish_race(): var message = "Finish 2nd lap on " + get_ordinal_string(race_position) if is_multiplayer_authority(): 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 func check_pattern_match() -> bool: @@ -319,7 +315,7 @@ func start_new_lap(): goals = second_lap_goals.duplicate() # Reset playerboard - playerboard.fill(-1) + #playerboard.fill(-1) # Reset can_finish flag can_finish = false @@ -446,9 +442,13 @@ func ping_existence(): # They can check if they have this node pass -func _physics_process(_delta): +func _physics_process(delta): if is_multiplayer_authority(): 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): # Early return if not authorized human player @@ -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: 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 # Make scenario for match checking laps, for handle lap count if current_lap == 0: # first lap @@ -693,15 +700,11 @@ func move_player_to_clicked_position(grid_position: Vector2i): else: # second lap 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): return + + 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: return @@ -732,7 +735,7 @@ func move_player_to_clicked_position(grid_position: Vector2i): # Clear highlights after moving if not (is_bot or is_in_group("Bots")): clear_highlights() - + # Handle finish line crossing if is_valid_finish and can_finish: rpc("finish_race") @@ -751,6 +754,10 @@ func start_movement_along_path(path: Array, clear_visual: bool = true): current_position = Vector2i(path[-1].x, path[-1].y) 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") # 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() ) +#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): position = grid_to_world(grid_position)