-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker_logic.py
More file actions
237 lines (197 loc) · 6.03 KB
/
worker_logic.py
File metadata and controls
237 lines (197 loc) · 6.03 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from typing import Dict, List, Tuple
from lux.game_map import Position, Cell
from lux import annotate
from harvest_logic import get_action
from pathfinding import get_lowest_neighbor_weight, action_to_avoidance_map, get_neighbor_cells
from utils import log, get_most_abundant_resource, get_resource_from_cargo
import pathfinding
import rules
worker_rules = None
cart_rules = None
class UnitAction:
def __init__(
self,
worker_id,
target_pos,
target_action,
new_pos,
command,
neighbor_cell_values : List[Tuple[Cell, float]] = None,
path = None
):
self.worker_id = worker_id
self.target_pos = target_pos
self.target_action = target_action
self.new_pos = new_pos
self.command = command
self.neighbor_cell_values = neighbor_cell_values
self.path = path
def __eq__(self, o):
return (
self.worker_id == o.worker_id and
self.target_pos == o.target_pos and
self.new_pos == o.new_pos and
self.target_action == o.target_action and
self.command == o.command
)
def same_pos(self, o):
return self.target_pos == o.target_pos
def annotate(self, actions):
x = self.target_pos.x
y = self.target_pos.y
actions.append(annotate.text(x, y, self.target_action, 64))
if self.neighbor_cell_values is not None:
for cell, value in self.neighbor_cell_values:
actions.append(
annotate.text(
cell.pos.x, cell.pos.y,
round(value,2), fontsize=64
)
)
if self.new_pos is not None:
actions.append(
annotate.x(self.new_pos.x, self.new_pos.y)
)
if self.target_pos is not None:
actions.append(
annotate.circle(
self.target_pos.x, self.target_pos.y
)
)
if self.path is None:
return
for i, cell in enumerate(self.path):
if i == 0:
continue
# Previous cell
pcell = self.path[i-1]
actions.append(annotate.line(
cell.pos.x, cell.pos.y,
pcell.pos.x, pcell.pos.y
))
def units_work(player, opponent, game_state, configuration):
global worker_rules
global cart_rules
hparams = configuration['hparams']
# Shitty code beware
if worker_rules is None:
worker_rules = rules.generate_rule_array(hparams.worker_rule_weights)
if cart_rules is None:
cart_rules = rules.generate_rule_array(hparams.cart_rule_weights)
actions = []
unit_action_map = {}
for unit in player.units:
if unit.can_act():
if unit.is_worker():
unit_rules = worker_rules
elif unit.is_cart():
unit_rules = cart_rules
action = get_unit_action(
unit,
player,
opponent,
game_state,
unit_action_map,
unit_rules,
hparams
)
action.annotate(actions)
if action.command is not None:
actions.append(action.command)
# Add action to dictionary
unit_action_map[unit.id] = action
return actions
def get_unit_action(
worker,
player,
opponent,
game_state,
worker_actions,
rules,
hparams
):
target_pos, action = get_action(
worker,
player,
opponent,
game_state,
worker_actions,
rules,
hparams
)
command = None
new_pos = worker.pos
cell_weights = None
if (
worker.pos.distance_to(target_pos) > action_to_minimum_dist(action)
):
# Shitty pathfinding.
cell_weights = get_neighbor_cells(
game_state.map,
player,
opponent,
worker.pos,
target_pos,
action_to_avoidance_map(action)
)
lowest_neighbor_weight = min(
cell_weights, key=lambda n: n[1]
)
#lowest_neighbor_weight = get_lowest_neighbor_weight(
# game_state.map,
# player,
# opponent,
# worker.pos,
# target_pos,
# action_to_avoidance_map(action)
#)
#lowest_neighbor_weight = get_lowest_neighbor_weight(
# game_state.map,
# player,
# opponent,
# worker.pos,
# target_pos,
# action_to_avoidance_map(action)
#)
new_pos = lowest_neighbor_weight[0].pos
direction = worker.pos.direction_to(new_pos)
command = worker.move(direction)
elif action == 'build':
log('Worker is attempting to build.')
command = worker.build_city()
elif action == 'pillage':
log('Worker is attempting to pillage.')
command = worker.pillage()
elif 'transfer' in action:
action, transfer_id = action.split(';')
resource_type = get_most_abundant_resource(worker.cargo)
amount = get_resource_from_cargo(worker.cargo, resource_type)
command = worker.transfer(transfer_id, resource_type, amount)
return UnitAction(
worker.id,
target_pos,
action,
new_pos,
command,
cell_weights
# path
)
map_action_to_minimum_dist = {
'move': 0,
'build': 0,
'pillage': 0,
'transfer': 1
}
def action_to_minimum_dist(action):
action = action.split(';')[0]
return map_action_to_minimum_dist[action]
map_role_to_rules = {
'builder': worker_rules,
'collector': worker_rules,
'cart': cart_rules
}
map_worker_to_role = {}
def role_to_rules(role : str):
return map_role_to_rules[role]
def get_percentage_of_role():
pass