extends ConditionLeaf

func is_goals_achieved(actor) -> bool:
	# ... same is_goals_achieved function as in can_grab.gd ...
	return false

func tick(actor: Node, blackboard: Blackboard) -> int:
	# First check if goals are achieved
	if is_goals_achieved(actor):
		blackboard.set_value("current_action", "idle")
		return FAILURE

	# Find an item in playerboard that matches goals
	var put_slot = -1
	for i in range(actor.playerboard.size()):
		if actor.playerboard[i] in actor.goals:
			put_slot = i
			break
			
	if put_slot == -1:
		return FAILURE
		
	# Find empty adjacent cell
	var current_cell = Vector3i(actor.current_position.x, 1, actor.current_position.y)
	if actor.enhanced_gridmap.get_cell_item(current_cell) == -1:
		blackboard.set_value("put_position", actor.current_position)
		blackboard.set_value("put_slot", put_slot)
		return SUCCESS
		
	var neighbors = actor.enhanced_gridmap.get_neighbors(actor.current_position, 0)
	for neighbor in neighbors:
		if not neighbor.is_walkable:
			continue
		var cell = Vector3i(neighbor.position.x, 1, neighbor.position.y)
		if actor.enhanced_gridmap.get_cell_item(cell) == -1:
			blackboard.set_value("put_position", neighbor.position)
			blackboard.set_value("put_slot", put_slot)
			return SUCCESS
			
	return FAILURE
