-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
118 lines (93 loc) · 3.98 KB
/
agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Python Modules imports
import logging
# Lux API imports
from lux.game import Game
# My imports
from Cluster.clusterController import ClusterController
from Resources.resourceService import get_resources, get_minable_resource_cells
from Missions.Mission import Mission
from Missions.constants import BUILD_TILE, GUARD_CLUSTER, EXPLORE
from helperFunctions.helper_functions import (
negotiate_actions,
update_game_stats,
get_city_actions,
)
from Map.mapService import get_occupied_positions
logging.basicConfig(filename="Game.log", level=logging.INFO, force=True)
def agent(observation, configuration):
global game_state, game_stats
global cluster_controller
### Do not edit ###
if observation["step"] == 0:
game_state = Game()
game_state._initialize(observation["updates"])
game_state._update(observation["updates"][2:])
game_state.id = observation.player
width, height = game_state.map.width, game_state.map.height
cluster_controller = ClusterController(width, height, game_state)
cluster_controller.getClustersRolling(width, height, game_state)
else:
game_state._update(observation["updates"])
actions = []
game_stats = update_game_stats(observation["step"])
my_id = observation.player
opponent_id = (observation.player + 1) % 2
### AI Code goes down here! ###
player = game_state.players[observation.player]
opponent = game_state.players[(observation.player + 1) % 2]
# Get resources and minable resources
resource_cells = get_resources(game_state)
minable_resources = get_minable_resource_cells(player, resource_cells)
# Update Clusters
cluster_controller.update_clusters(game_state, player)
cluster_controller.update_missions(game_state, player)
# Units without home cluster
units_wo_clusters = cluster_controller.get_units_without_clusters(player)
# Assign Missions to units without homes
for unit in units_wo_clusters:
assigned_cluster = cluster_controller.assign_worker(
unit, game_state, player, my_id, opponent
)
if assigned_cluster is not None:
assigned_cluster.add_unit(unit.id)
current_mission = Mission(responsible_unit=unit.id, mission_type=EXPLORE)
assigned_cluster.missions.append(current_mission)
# After assigning missions to units without homes, update the list
units_wo_clusters = cluster_controller.get_units_without_clusters(player)
# Now, all units have missions assigned to them
# Instruct Clusters to assign targets to missions
for cluster in cluster_controller.clusterDict.values():
cluster.assign_targets_to_missions(
game_state, player, opponent, BUILD_TILE, observation["step"]
)
cluster.assign_targets_to_missions(
game_state, player, opponent, GUARD_CLUSTER, observation["step"]
)
cluster.handle_explore_missions(game_stats, minable_resources, player)
cluster.assign_targets_to_missions(
game_state, player, opponent, EXPLORE, observation["step"]
)
occupied_positions = get_occupied_positions(player, opponent, cluster_controller)
for cluster in cluster_controller.clusterDict.values():
if len(cluster.missions) == 0:
continue
actions.extend(cluster.get_build_actions(game_stats, player))
# Getting the required movements for all clusters
required_moves = list()
for cluster in cluster_controller.clusterDict.values():
moves = cluster.get_required_moves(player)
required_moves.extend(moves)
# Add the valid actions (those who have occupied positions are not valid)
actions.extend(negotiate_actions(occupied_positions, required_moves))
actions.extend(
get_city_actions(
game_state,
game_stats,
player,
cluster_controller.clusterDict,
my_id,
opponent,
opponent_id,
)
)
return actions