-
Notifications
You must be signed in to change notification settings - Fork 1
/
comp.py
477 lines (389 loc) · 14.7 KB
/
comp.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import action
CTYPES_LIST = ["FixedGun", "Engine", "Radar", "CnC", "Radio", "Arm"]
COMP_ATTRS_BY_CTYPE = {
"FixedGun": [
("reload_ticks", 1),
("ammunition", 0),
("min_damage", 0),
("max_damage", 0),
("range", 0),
],
"Engine": [("min_speed", 0.0), ("max_speed", 0.0), ("max_turnrate", 0.0)],
"Radar": [
("range", 0),
("level", 0),
("visarc", 0),
("offset_angle", 0),
("resolution", 0),
],
"CnC": [("max_cmds_per_tick", 0)],
"Radio": [
("max_range", 0),
],
"Arm": [("max_bulk", 0), ("max_weight", 0)],
}
MIN_NONZERO_SPEED = 0.001
class Comp:
def __init__(self, template):
"""Initializes component data
Sets data to input
Sets command, update, and view_keys functions based on component type
"""
self.data = template
self.ctype = template["ctype"]
self.command = self.no_command
self.update = self.no_update
self.view_keys = []
self.parent = None
self.set_view_keys_basic()
ctype = self.data["ctype"]
if ctype == "FixedGun":
self.command = self.fixed_gun_command
self.update = self.fixed_gun_update
self.set_view_keys_fixed_gun()
self.data["reload_ticks_remaining"] = 0
self.data["reloading"] = False
elif ctype == "Engine":
self.command = self.engine_command
self.update = self.engine_update
self.set_view_keys_engine()
self.data["cur_speed"] = 0.0
self.data["cur_turnrate"] = 0.0
elif ctype == "Radar":
self.command = self.radar_command
self.update = self.radar_update
self.set_view_keys_radar()
self.data["active"] = False
elif ctype == "CnC":
self.command = self.cnc_command
self.update = self.cnc_update
self.set_view_keys_cnc()
elif ctype == "Radio":
self.command = self.radar_command
self.update = self.radio_update
self.set_view_keys_radio()
self.data["message"] = None
elif ctype == "Arm":
self.command = self.arm_command
self.update = self.arm_update
self.set_view_keys_arm()
self.data["item"] = None
def get_data(self, key):
"""Gets data"""
if key in self.data:
return self.data[key]
else:
return None
def set_data(self, key, value):
"""Sets data"""
self.data[key] = value
def get_ctype(self):
return self.ctype
def get_parent(self):
return self.parent
def set_parent(self, parent):
self.parent = parent
###########################################################################
# Self View dispatch
def set_view_keys_basic(self):
"""Sets view keys to basic/default configuration"""
self.view_keys += ["id", "name", "ctype", "slot_id"]
def set_view_keys_fixed_gun(self):
"""Sets view keys to fixed gun configuration"""
self.view_keys += [
"reload_ticks",
"reload_ticks_remaining",
"reloading",
"ammunition",
"min_damage",
"max_damage",
"range",
]
def set_view_keys_engine(self):
"""Sets view keys to engine configuration"""
self.view_keys += [
"min_speed",
"max_speed",
"cur_speed",
"max_turnrate",
"cur_turnrate",
]
def set_view_keys_radar(self):
"""Sets view keys to radar configuration"""
self.view_keys += [
"active",
"range",
"level",
"visarc",
"offset_angle",
"resolution",
]
def set_view_keys_cnc(self):
"""Sets view keys to cnc configuration"""
self.view_keys += ["max_cmds_per_tick"]
def set_view_keys_radio(self):
"""Sets view keys to radio configuration"""
self.view_keys += ["range"]
def set_view_keys_arm(self):
"""Sets view keys to arm configuration"""
self.view_keys += ["max_weight", "max_bulk", "item"]
def get_self_view(self):
"""Gets self view"""
view = {}
for key in self.view_keys:
view[key] = self.get_data(key)
return view
##########################################################################
# UPDATE METHODS
##########################################################################
###################################
# Default
def no_command(self, cmd):
"""Default command (does nothing)
Intended for use in comps that never produce actions
"""
return []
def no_update(self):
"""Default update (does nothing)
Intended for use in comps that never produce actions
"""
return []
###################################
# Fixed Gun
def fixed_gun_command(self, cmd):
"""Update fixed gune data based on command
Options: FIRE, RELOAD
"""
actions = []
if "command" in cmd:
if cmd["command"] == "FIRE":
if self.is_loaded():
# Reset the reload ticks
self.set_reload_ticks_to_full()
a = action.Action()
a.set_type("HIGHSPEED_PROJECTILE")
a.add_data("slot_id", self.get_data("slot_id"))
a.add_data("compname", self.get_data("name"))
a.add_data(
"direction",
self.get_data("parent").get_data("facing")
)
a.add_data("min_damage", self.get_data("min_damage"))
a.add_data("max_damage", self.get_data("max_damage"))
a.add_data("range", self.get_data("range"))
actions.append(a)
elif cmd["command"] == "RELOAD":
# We can reload if we aren't reloading,
# we are not currently loaded
# and we have ammo left.
if (
not self.data["reloading"]
and not self.is_loaded()
and self.get_data("ammunition") > 0
):
self.data["reloading"] = True
# See if we were reloading
self.update_reloading()
return actions
def fixed_gun_update(self):
"""Update fixed gun data based on if it's reloading"""
# See if we were reloading
self.update_reloading()
return []
###################################
# Engine
def engine_command(self, cmd):
"""Update engine data based on command
Options: SET_SPEED, SET_TURNRATE
"""
actions = []
if "command" in cmd:
if cmd["command"] == "SET_SPEED":
if "speed" in cmd:
new_speed = cmd["speed"]
if new_speed < self.get_data("min_speed"):
self.data["cur_speed"] = self.get_data("min_speed")
elif new_speed > self.get_data("max_speed"):
self.data["cur_speed"] = self.get_data("max_speed")
elif -MIN_NONZERO_SPEED < new_speed < MIN_NONZERO_SPEED:
self.data["cur_speed"] = 0
else:
self.data["cur_speed"] = new_speed
elif cmd["command"] == "SET_TURNRATE":
if "turnrate" in cmd:
new_turnrate = cmd["turnrate"]
if abs(new_turnrate) <= self.get_data("max_turnrate"):
self.data["cur_turnrate"] = new_turnrate
else:
self.data["cur_turnrate"] = \
self.get_data("max_turnrate")
return actions
def engine_update(self):
"""Update engine data based on if it's moving and/or turning"""
actions = []
if self.is_moving():
a = action.Action()
a.set_type("MOVE")
a.add_data("slot_id", self.get_data("slot_id"))
a.add_data("speed", self.get_data("cur_speed"))
actions.append(a)
if self.is_turning():
a = action.Action()
a.set_type("TURN")
a.add_data("slot_id", self.get_data("slot_id"))
a.add_data("turnrate", self.get_data("cur_turnrate"))
actions.append(a)
return actions
###################################
# Radar
def radar_command(self, cmd):
"""Update radar data based on command
Options: ACTIVATE_RADAR, DEACTIVATE_RADAR
"""
if "command" in cmd:
if cmd["command"] == "ACTIVATE_RADAR":
self.make_active()
elif cmd["command"] == "DEACTIVATE_RADAR":
self.make_inactive()
return []
def radar_update(self):
"""Update radar data based on if it's activated"""
actions = []
if self.is_active():
a = action.Action()
a.set_type("TRANSMIT_RADAR")
a.add_data("slot_id", self.get_data("slot_id"))
a.add_data("compname", self.get_data("name"))
a.add_data("ctype", self.get_data("ctype"))
a.add_data("range", self.get_data("range"))
a.add_data("offset_angle", self.get_data("offset_angle"))
a.add_data("level", self.get_data("level"))
a.add_data("visarc", self.get_data("visarc"))
a.add_data("offset_angle", self.get_data("offset_angle"))
a.add_data("resolution", self.get_data("resolution"))
actions.append(a)
return actions
##################################
# CnC
def cnc_command(self, cmd):
"""Update CnC data based on command (does nothing)"""
return []
def cnc_update(self):
"""Update CnC data (does nothing)"""
return []
###################################
# Radio
def radio_command(self, cmd):
"""Update radio data based on command
Options: BROADCAST, SET_RANGE
"""
if "command" in cmd:
if cmd["command"] == "BROADCAST" and "message" in cmd:
self.set_data("message", cmd["message"])
elif cmd["command"] == "SET_RANGE" and "range" in cmd:
new_range = cmd["range"]
if 0 <= new_range <= self.get_data("max_range"):
self.set_data("cur_range", new_range)
return []
def radio_update(self):
"""Update radio data based on if there is a message"""
actions = []
if self.get_data("message") is not None:
a = action.Action()
a.set_type("BROADCAST")
a.add_data("slot_id", self.get_data("slot_id"))
a.add_data("message", self.get_data("message"))
a.add_data("range", self.get_data("cur_range"))
actions.append(a)
return actions
###################################
# Arm Update
def arm_command(self, cmd):
"""Update arm data based on command
Options: TAKE_ITEM, DROP_ITEM
"""
actions = []
if "command" in cmd:
if cmd["command"] == "TAKE_ITEM":
a = action.Action()
a.set_type("TAKE_ITEM")
a.add_data("slot_id", self.get_data("slot_id"))
if "item_name" in cmd:
a.add_data("item_name", cmd["item_name"])
else:
a.add_data("item_name", None)
if "item_index" in cmd:
a.add_data("item_index", cmd["item_index"])
else:
a.add_data("item_index", None)
if "item_uuid" in cmd:
a.add_data("item_uuid", cmd["item_uuid"])
else:
a.add_data("item_uuid", None)
if "location" in cmd:
a.add_data("location", cmd["location"])
else:
a.add_data("location", None)
actions.append(a)
elif cmd["command"] == "DROP_ITEM":
a = action.Action()
a.set_type("DROP_ITEM")
a.add_data("slot_id", self.get_data("slot_id"))
a.add_data("location", cmd["location"])
actions.append(a)
return actions
def arm_update(self):
"Update Arm data (does nothing)"
return []
###########################################################################
# WEAPON RELATED FUNCTIONS
def set_reload_ticks_to_full(self):
"""Set reload ticks to full"""
self.data["reload_ticks_remaining"] = self.data["reload_ticks"]
def is_loaded(self):
"""Deteremines if weapon loaded"""
return self.data["reload_ticks_remaining"] == 0
def update_reloading(self):
"""Decrement reloading ticks"""
if self.data["reloading"]:
if self.data["reload_ticks_remaining"] > 0:
self.data["reload_ticks_remaining"] -= 1
if self.data["reload_ticks_remaining"] == 0:
self.data["reloading"] = False
else:
self.data["reloading"] = False
###########################################################################
# ENGINE RELATED FUNCTIONS
def is_moving(self):
"""Determines if engine is moving"""
return self.data["cur_speed"] != 0.0
def is_turning(self):
"""Determines if engine is turning"""
return self.data["cur_turnrate"] != 0.0
###########################################################################
# RADAR RELATED FUCNTIONS
def is_transmitting(self):
"""Determines if radar is transmitting"""
return self.get_data("active")
###########################################################################
# ARM RELATED FUNCTIONS
def is_holding_item(self):
"""Determines if arm is holding an item"""
return self.get_data("item") is not None
def can_take_item(self, weight, bulk):
"""Determines if arm can take item"""
return (
self.get_data("max_weight") >= weight
and self.get_data("max_bulk") >= bulk
)
###########################################################################
#
def is_active(self):
"""Determines if component is active"""
return self.get_data("active")
def make_active(self):
"""Makes component active"""
self.set_data("active", True)
def make_inactive(self):
"""Makes component inactive"""
self.set_data("active", False)