update bot logic

This commit is contained in:
2024-10-24 18:06:10 +08:00
parent 8fe13a42ca
commit df436e7af0
3 changed files with 284 additions and 23 deletions
+124 -22
View File
@@ -214,37 +214,139 @@ func _on_message_input_text_submitted(new_text):
$MessageInput.text = ""
$MessageInput.release_focus()
# ---------
# Bot Logic
# ---------
func evaluate_bot_move(bot: Node) -> Dictionary:
var best_move = {
"action": "none",
"position": bot.current_position,
"value": -1
}
# If no action performed yet, prioritize grab/put
if not bot.has_performed_action:
# Check current position for grabbing
var current_cell = Vector3i(bot.current_position.x, 1, bot.current_position.y)
var current_item = bot.enhanced_gridmap.get_cell_item(current_cell)
# Evaluate grabbing current item if it matches goals
if current_item != -1 and current_item in bot.goals and bot.playerboard.find(-1) != -1:
return {
"action": "grab",
"position": bot.current_position,
"value": 10
}
# Evaluate putting item from playerboard
for goal in bot.goals:
var item_index = bot.playerboard.find(goal)
if item_index != -1 and current_item == -1:
return {
"action": "put",
"position": bot.current_position,
"value": 15
}
else:
# Bot has performed action, look for movement
var neighbors = bot.enhanced_gridmap.get_neighbors(bot.current_position, 1)
for neighbor in neighbors:
if not neighbor.is_walkable:
continue
if not bot.is_within_movement_range(neighbor.position):
continue
var cell = Vector3i(neighbor.position.x, 1, neighbor.position.y)
var item = bot.enhanced_gridmap.get_cell_item(cell)
# Evaluate position for next turn
if item in bot.goals or item == -1:
var value = 8
if value > best_move.value:
best_move = {
"action": "move",
"position": neighbor.position,
"value": value
}
return best_move
#func move_bot(bot_id):
#if multiplayer.is_server():
#if moving_bots.get(bot_id, false):
#return # Skip if bot is already moving
#
#var bot = get_node(str(bot_id))
#if bot and (turn_based_mode or not bot.is_player_moving):
#moving_bots[bot_id] = true # Mark bot as moving
#var target_position = bot.find_random_valid_position_in_range() # Use the new function
#if target_position != bot.current_position:
#var path = bot.enhanced_gridmap.find_path(Vector2(bot.current_position), Vector2(target_position))
#
#if path.size() > 1:
#path.pop_front()
## Trim path to respect movement range
#var trimmed_path = path.slice(0, bot.movement_range)
#bot.move_bot_along_path(trimmed_path, bot_id)
#else:
#print("No valid path found for bot")
#moving_bots[bot_id] = false
#if turn_based_mode:
#end_current_turn()
#else:
#print("No new valid position found for bot")
#moving_bots[bot_id] = false
#if turn_based_mode:
#end_current_turn()
#else:
#print("Bot not found or is moving")
#if turn_based_mode:
#end_current_turn()
func move_bot(bot_id):
if multiplayer.is_server():
if moving_bots.get(bot_id, false):
return # Skip if bot is already moving
return
var bot = get_node(str(bot_id))
if bot and (turn_based_mode or not bot.is_player_moving):
moving_bots[bot_id] = true # Mark bot as moving
var target_position = bot.find_random_valid_position_in_range() # Use the new function
if target_position != bot.current_position:
var path = bot.enhanced_gridmap.find_path(Vector2(bot.current_position), Vector2(target_position))
if path.size() > 1:
path.pop_front()
# Trim path to respect movement range
var trimmed_path = path.slice(0, bot.movement_range)
bot.move_bot_along_path(trimmed_path, bot_id)
else:
print("No valid path found for bot")
moving_bots[bot_id] = true
var best_move = evaluate_bot_move(bot)
match best_move.action:
"grab":
if bot.grab_item():
bot.has_performed_action = true
moving_bots[bot_id] = false
# Don't end turn, allow movement after grab
move_bot(bot_id) # Try to move after grabbing
"put":
if bot.put_item():
bot.has_performed_action = true
moving_bots[bot_id] = false
# Don't end turn, allow movement after put
move_bot(bot_id) # Try to move after putting
"move":
if bot.has_performed_action:
var path = bot.enhanced_gridmap.find_path(Vector2(bot.current_position), Vector2(best_move.position))
if path.size() > 1:
path.pop_front()
var trimmed_path = path.slice(0, bot.movement_range)
bot.rotate_towards_target(best_move.position)
bot.move_bot_along_path(trimmed_path, bot_id)
else:
moving_bots[bot_id] = false
if turn_based_mode:
end_current_turn()
else:
moving_bots[bot_id] = false
if turn_based_mode:
end_current_turn()
_:
moving_bots[bot_id] = false
if turn_based_mode:
end_current_turn()
else:
print("No new valid position found for bot")
moving_bots[bot_id] = false
if turn_based_mode:
end_current_turn()
else:
print("Bot not found or is moving")
if turn_based_mode:
end_current_turn()
# Called when a bot finishes moving
func bot_movement_completed(bot_id):