update bot experimental
This commit is contained in:
+88
-44
@@ -36,7 +36,6 @@ var rotation_speed: float = 10.0
|
||||
rpc("display_message", "It's your turn!")
|
||||
|
||||
@export var has_moved_this_turn = false
|
||||
@onready var main_scene = get_tree().current_scene
|
||||
|
||||
var highlighted_cells = []
|
||||
|
||||
@@ -44,40 +43,53 @@ func _ready():
|
||||
name = str(get_multiplayer_authority())
|
||||
$Name.text = str(name)
|
||||
|
||||
enhanced_gridmap = get_node(enhanced_gridmap_path)
|
||||
|
||||
if main_scene:
|
||||
enhanced_gridmap = main_scene.get_node("EnhancedGridMap")
|
||||
else:
|
||||
# More robust way to get the main scene
|
||||
var main_scene = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main_scene:
|
||||
push_error("Main scene not found")
|
||||
|
||||
if not enhanced_gridmap:
|
||||
push_error("EnhancedGridMap node not found")
|
||||
return
|
||||
|
||||
enhanced_gridmap.initialize_astar()
|
||||
enhanced_gridmap.set_diagonal_movement(use_diagonal_movement)
|
||||
# Ensure proper initialization order
|
||||
enhanced_gridmap = get_node(enhanced_gridmap_path)
|
||||
if main_scene:
|
||||
enhanced_gridmap = main_scene.get_node("EnhancedGridMap")
|
||||
|
||||
current_position = find_valid_starting_position()
|
||||
update_player_position(current_position)
|
||||
# Initialize behavior tree for bots
|
||||
var behavior_tree = $BehaviorTree
|
||||
|
||||
# Set bot flag if in Bots group
|
||||
if is_in_group("Bots"):
|
||||
is_bot = true
|
||||
|
||||
if is_in_group("Bots") and behavior_tree:
|
||||
behavior_tree.enabled = true
|
||||
behavior_tree.actor = self
|
||||
|
||||
# Only process input if not a bot and is authority
|
||||
set_process_unhandled_input(not is_bot and is_multiplayer_authority())
|
||||
rpc("sync_bot_status", true)
|
||||
|
||||
# Rest of initialization
|
||||
if enhanced_gridmap:
|
||||
enhanced_gridmap.initialize_astar()
|
||||
enhanced_gridmap.set_diagonal_movement(use_diagonal_movement)
|
||||
|
||||
current_position = find_valid_starting_position()
|
||||
update_player_position(current_position)
|
||||
|
||||
set_process_unhandled_input(not is_in_group("Bots") and is_multiplayer_authority())
|
||||
|
||||
append_random_goals()
|
||||
playerboard.resize(25)
|
||||
playerboard.fill(-1)
|
||||
|
||||
# Enable behavior tree if bot
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func sync_bot_status(is_bot_status: bool):
|
||||
is_bot = is_bot_status
|
||||
if is_bot:
|
||||
var behavior_tree = $BehaviorTree
|
||||
add_to_group("Bots", true) # Persistent group addition
|
||||
var behavior_tree = get_node_or_null("BehaviorTree")
|
||||
if behavior_tree:
|
||||
behavior_tree.enabled = true
|
||||
behavior_tree.actor = self
|
||||
if not is_multiplayer_authority():
|
||||
behavior_tree.set_physics_process(false)
|
||||
behavior_tree.set_process(false)
|
||||
|
||||
func _physics_process(_delta):
|
||||
if is_multiplayer_authority():
|
||||
@@ -87,7 +99,11 @@ func _unhandled_input(event):
|
||||
#if is_in_group("Bots"):
|
||||
#return
|
||||
|
||||
var main = get_node("/root/Main/")
|
||||
# Use get_node_or_null for safer node access
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main:
|
||||
return
|
||||
|
||||
if not is_multiplayer_authority() or (main.turn_based_mode and (not is_my_turn or is_player_moving)):
|
||||
return
|
||||
|
||||
@@ -102,7 +118,8 @@ func _unhandled_input(event):
|
||||
|
||||
func _on_slot_gui_input(event, slot_index, slot_ui) -> int:
|
||||
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
|
||||
if main.current_action_state == main.ActionState.ARRANGING:
|
||||
if selected_playerboard_slot == -1:
|
||||
select_playerboard_slot(slot_index)
|
||||
@@ -120,8 +137,9 @@ func _on_slot_gui_input(event, slot_index, slot_ui) -> int:
|
||||
return -1
|
||||
|
||||
func handle_grid_click(grid_position: Vector2i):
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main:
|
||||
push_error("Main node not found")
|
||||
return
|
||||
|
||||
match main.current_action_state:
|
||||
@@ -210,7 +228,7 @@ func move_player_to_clicked_position(grid_position: Vector2i):
|
||||
if not is_multiplayer_authority() or is_player_moving or action_points <= 0:
|
||||
return
|
||||
|
||||
var main = get_node("/root/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:
|
||||
return
|
||||
|
||||
@@ -257,7 +275,7 @@ func start_movement_along_path(path: Array):
|
||||
enhanced_gridmap.clear_path_visualization()
|
||||
has_moved_this_turn = path.size() <= movement_range
|
||||
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
main.set_action_state(main.ActionState.NONE)
|
||||
|
||||
if main.turn_based_mode:
|
||||
@@ -367,7 +385,7 @@ func grab_item(grid_position: Vector2i = current_position) -> bool:
|
||||
_after_action_completed()
|
||||
return true
|
||||
else:
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if main:
|
||||
selected_gridmap_position = grid_position
|
||||
clear_highlights()
|
||||
@@ -414,14 +432,14 @@ func put_item(grid_position: Vector2i = current_position) -> bool:
|
||||
clear_playerboard_highlights()
|
||||
selected_playerboard_slot = -1
|
||||
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if main:
|
||||
main.set_action_state(main.ActionState.NONE)
|
||||
_after_action_completed()
|
||||
return true
|
||||
|
||||
func handle_put_action():
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main or action_points < 1:
|
||||
return
|
||||
|
||||
@@ -437,7 +455,7 @@ func handle_put_action():
|
||||
highlighted_cells.append(i)
|
||||
|
||||
func handle_playerboard_slot_selected(slot_index: int):
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main:
|
||||
return
|
||||
|
||||
@@ -467,7 +485,7 @@ func handle_playerboard_slot_selected(slot_index: int):
|
||||
|
||||
# We also need to add handle_put_slot_selected:
|
||||
func handle_put_slot_selected(slot_index: int):
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main or main.current_action_state != main.ActionState.PUTTING:
|
||||
return
|
||||
|
||||
@@ -483,7 +501,7 @@ func handle_put_slot_selected(slot_index: int):
|
||||
#var selected_item = playerboard[slot_index]
|
||||
#var adjacent_slots = get_adjacent_playerboard_slots(slot_index)
|
||||
#
|
||||
#var main = get_node("/root/Main")
|
||||
#var main = get_tree().get_root().get_node_or_null("Main")
|
||||
#if not main or not main.playerboard_ui:
|
||||
#return
|
||||
#
|
||||
@@ -528,7 +546,7 @@ func arrange_playerboard_item(slot_index: int):
|
||||
var selected_item = playerboard[slot_index]
|
||||
var adjacent_slots = get_adjacent_playerboard_slots(slot_index)
|
||||
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main or not main.playerboard_ui:
|
||||
return
|
||||
|
||||
@@ -558,7 +576,7 @@ func _on_slot_clicked(event: InputEvent, slot_index: int):
|
||||
if not event is InputEventMouseButton or not event.pressed or event.button_index != MOUSE_BUTTON_LEFT:
|
||||
return
|
||||
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main or main.current_action_state != main.ActionState.ARRANGING:
|
||||
return
|
||||
|
||||
@@ -760,7 +778,7 @@ func highlight_occupied_playerboard_slots():
|
||||
#if is_in_group("Bots"):
|
||||
#return
|
||||
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main or not main.playerboard_ui:
|
||||
return
|
||||
|
||||
@@ -791,7 +809,7 @@ func clear_highlights():
|
||||
|
||||
highlighted_cells.clear()
|
||||
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if main and main.playerboard_ui:
|
||||
for i in range(main.playerboard_ui.get_child_count()):
|
||||
var slot = main.playerboard_ui.get_child(i)
|
||||
@@ -799,7 +817,7 @@ func clear_highlights():
|
||||
child.hide()
|
||||
|
||||
func clear_playerboard_highlights():
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if main and main.playerboard_ui:
|
||||
for i in range(main.playerboard_ui.get_child_count()):
|
||||
var slot = main.playerboard_ui.get_child(i)
|
||||
@@ -853,7 +871,7 @@ func can_move_to_target_playerboard_slot() -> bool:
|
||||
return adjacent_slots.has(targeted_playerboard_slot)
|
||||
|
||||
func _update_playerboard_slot_visual(slot_index: int):
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main or not main.playerboard_ui:
|
||||
return
|
||||
|
||||
@@ -867,7 +885,7 @@ func _update_playerboard_slot_visual(slot_index: int):
|
||||
slot.get_child(2).visible = selected_playerboard_slot != -1 and get_adjacent_playerboard_slots(selected_playerboard_slot).has(slot_index)
|
||||
|
||||
func _highlight_adjacent_playerboard_slots():
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main or not main.playerboard_ui:
|
||||
return
|
||||
|
||||
@@ -902,14 +920,34 @@ func sync_playerboard(new_playerboard: Array):
|
||||
playerboard = new_playerboard
|
||||
_after_action_completed()
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func sync_behavior_tree(enabled: bool):
|
||||
var behavior_tree = $BehaviorTree
|
||||
if behavior_tree:
|
||||
behavior_tree.enabled = enabled
|
||||
behavior_tree.actor = self
|
||||
#behavior_tree.blackboard = blackboard
|
||||
|
||||
## Ensure blackboard has required values
|
||||
#blackboard.set_value("action_points", action_points)
|
||||
#blackboard.set_value("goals", goals)
|
||||
#blackboard.set_value("playerboard", playerboard)
|
||||
|
||||
func _after_action_completed():
|
||||
if multiplayer.get_unique_id() == get_multiplayer_authority():
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if main:
|
||||
if not main.turn_based_mode and action_points <= 0:
|
||||
action_points = 2
|
||||
# Add this condition for bots
|
||||
if not main.turn_based_mode and (action_points <= 0 or is_bot):
|
||||
action_points = 20 # For bots in non-turn-based mode, this will keep refreshing
|
||||
has_performed_action = false
|
||||
has_moved_this_turn = false
|
||||
|
||||
# Update blackboard after action points change
|
||||
#var blackboard = $Blackboard
|
||||
#if blackboard:
|
||||
#blackboard.set_value("action_points", action_points)
|
||||
|
||||
main.update_button_states()
|
||||
main.update_playerboard_ui()
|
||||
|
||||
@@ -917,11 +955,17 @@ func consume_action_points(points: int):
|
||||
if not is_instance_valid(self) or not is_multiplayer_authority():
|
||||
return
|
||||
|
||||
action_points -= points
|
||||
var main = get_node("/root/Main")
|
||||
var main = get_tree().get_root().get_node_or_null("Main")
|
||||
if not main:
|
||||
return
|
||||
|
||||
# Don't consume points for bots in non-turn-based mode
|
||||
if is_bot and not main.turn_based_mode:
|
||||
_after_action_completed()
|
||||
return
|
||||
|
||||
action_points -= points
|
||||
|
||||
if action_points <= 0:
|
||||
if main.turn_based_mode:
|
||||
main.request_end_turn()
|
||||
|
||||
Reference in New Issue
Block a user