Skip to content

Commit

Permalink
Fix AI, lampe-games#92
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Dec 16, 2023
1 parent 2addbe3 commit 2d426fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
4 changes: 0 additions & 4 deletions source/match/Match.gd
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ var visible_players = null:
func _enter_tree():
assert(settings != null, "match cannot start without settings, see examples in tests/manual/")
assert(map != null, "match cannot start without map, see examples in tests/manual/")
# TODO: remove - it's a temporary hack
$Players.get_children().filter(func(node): return node is Player).map(
func(player): player.add_to_group("players")
)


func _ready():
Expand Down
29 changes: 16 additions & 13 deletions source/match/players/simple-clairvoyant-ai/SimpleClairvoyantAI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var _resource_requests = {
}
var _call_to_perform_during_process = null

@onready var player = self
@onready var _match = find_parent("Match")

@onready var _economy_controller = find_child("EconomyController")
@onready var _defense_controller = find_child("DefenseController")
Expand All @@ -30,24 +30,27 @@ var _call_to_perform_during_process = null


func _ready():
if player == null:
await find_parent("Match").ready
assert(player != null, "player cannot be null at this point")
player.changed.connect(_on_player_resource_changed)
# wait for match to be ready
if not _match.is_node_ready():
await _match.ready
# wait additional frame to make sure other players are in place
await get_tree().physics_frame

changed.connect(_on_player_data_changed)
_economy_controller.resources_required.connect(
_on_resource_request.bind(_economy_controller, ResourceRequestPriority.HIGH)
)
_economy_controller.setup(player)
_economy_controller.setup(self)
_defense_controller.resources_required.connect(
_on_resource_request.bind(_defense_controller, ResourceRequestPriority.MEDIUM)
)
_defense_controller.setup(player)
_defense_controller.setup(self)
_offense_controller.resources_required.connect(
_on_resource_request.bind(_offense_controller, ResourceRequestPriority.LOW)
)
_offense_controller.setup(player)
_intelligence_controller.setup(player)
_construction_works_controller.setup(player)
_offense_controller.setup(self)
_intelligence_controller.setup(self)
_construction_works_controller.setup(self)


func _process(_delta):
Expand Down Expand Up @@ -78,7 +81,7 @@ func _try_fulfilling_resource_requests_according_to_priorities():
]:
while (
not _resource_requests[priority].is_empty()
and player.has_resources(_resource_requests[priority].front()["resources"])
and has_resources(_resource_requests[priority].front()["resources"])
):
var resource_request = _resource_requests[priority].pop_front()
_provision(
Expand All @@ -88,12 +91,12 @@ func _try_fulfilling_resource_requests_according_to_priorities():
)
if (
not _resource_requests[priority].is_empty()
and not player.has_resources(_resource_requests[priority].front()["resources"])
and not has_resources(_resource_requests[priority].front()["resources"])
):
break


func _on_player_resource_changed():
func _on_player_data_changed():
_try_fulfilling_resource_requests_according_to_priorities_next_frame()


Expand Down

0 comments on commit 2d426fb

Please sign in to comment.