-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormation.py
99 lines (93 loc) · 3.25 KB
/
Formation.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
from model.Move import Move
from model.ActionType import ActionType
from model.World import World
from model.Game import Game
from model.Player import Player
from model.Unit import Unit
from Analyze import WorldState, Vehicles
from Utils import Area, get_center
class Formation:
def __init__(self, groupnum: int):
self.group = groupnum
self.delayed = None
self.decision_list = []
self.last_direction = None
def select(self, m: Move):
m.action = ActionType.CLEAR_AND_SELECT
m.group = self.group
#print("selected group ", m.group)
def position(self, vehicles: Vehicles):
return get_center(vehicles.resolve(self.units(vehicles)))
def area(self, vehicles: Vehicles):
return Area.from_units(vehicles.resolve(self.units(vehicles)))
def units(self, vehicles: Vehicles):
return vehicles.by_group[self.group]
def __str__(self):
return "Formation of group " + str(self.group)
def tick(self, ws: WorldState, w: World, p: Player, g: Game, m: Move):
resetflag = False
if self.delayed:
m.action = self.delayed.action
m.x = self.delayed.x
m.y = self.delayed.y
m.vehicle_id = self.delayed.vehicle_id
m.angle = self.delayed.angle
m.factor = self.delayed.factor
m.max_speed = self.delayed.max_speed
self.delayed = None
if m.action == ActionType.MOVE:
self.last_direction = Unit(None, m.x, m.y)
else:
self.last_direction = None
return
for behavior in self.decision_list:
if resetflag:
behavior.reset()
elif behavior.on_tick(ws, w, p, g):
allunits = self.units(ws.vehicles)
allunitslen = len(allunits)
selected_from_all = len(allunits & ws.vehicles.selected)
#print("selected:", selected_from_all, "\nall:", allunitslen)
is_selected = selected_from_all == allunitslen
if is_selected:
behavior.act(ws, w, p, g, m)
else:
self.delayed = Move()
behavior.act(ws, w, p, g, self.delayed)
if self.delayed.action and self.delayed.action != ActionType.NONE:
self.select(m)
else:
self.delayed = None
# if m.action and m.action != ActionType.NONE:
## if m.action == ActionType.MOVE:
## self.last_direction = Unit(None, m.x, m.y)
## else:
## self.last_direction = None
# print("Taken action " + str(m.action) + " by " + str(behavior) + " in " + str(self))
# else:
# print("Group ", self.group, " skipped action via ", behavior)
resetflag = True
from Behaviors import NuclearAlert, Chase, KeepTogether, Nuke, Repair
class AerialFormation(Formation):
def __init__(self, groupnum):
Formation.__init__(self, groupnum)
print("Made aerial formation for group ", groupnum)
self.ground = False
self.decision_list = [
Nuke(self),
NuclearAlert(self),
Repair(self),
KeepTogether(self),
Chase(self)
]
class GroundFormation(Formation):
def __init__(self, groupnum: int):
print("Made ground formation for group ", groupnum)
Formation.__init__(self, groupnum)
self.ground = True
self.decision_list = [
Nuke(self),
NuclearAlert(self),
KeepTogether(self),
Chase(self)
]