-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.py
250 lines (211 loc) · 7.98 KB
/
entity.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
import libtcodpy as libtcod
import cmath as math
NEUTRAL_SIDE = 555
class Entity(object):
def __init__(self, battleground, side=NEUTRAL_SIDE, x=-1, y=-1, char=' ', color=libtcod.white):
self.bg = battleground
self.x = x
self.y = y
self.side = side
self.char = char
self.original_char = char
self.color = color
self.original_color = color
self.bg.tiles[(x, y)].entity = self
self.default_next_action = 5
self.next_action = self.default_next_action
self.pushed = False
self.alive = True
self.statuses = []
self.path = []
self.attack_effect = None
self.attack_type = "physical"
self.kills = 0
self.owner = None
def can_be_attacked(self):
return False
def can_be_pushed(self, dx, dy):
next_tile = self.bg.tiles[(self.x+dx, self.y+dy)]
return next_tile.is_passable(self) and (next_tile.entity is None or next_tile.entity.can_be_pushed(dx, dy))
def can_move(self, dx, dy):
next_tile = self.bg.tiles[(self.x+dx, self.y+dy)]
if not next_tile.is_passable(self): return False
if next_tile.entity is None: return True
if not next_tile.entity.is_ally(self): return False
return next_tile.entity.can_be_pushed(dx, dy)
def change_battleground(self, bg, x, y):
self.bg.tiles[(self.x, self.y)].entity = None
self.bg = bg
(self.x, self.y) = (x, y)
self.bg.tiles[(self.x, self.y)].entity = self
def clone(self, x, y):
if self.bg.is_inside(x, y) and self.bg.tiles[(x, y)].entity is None and self.bg.tiles[(x, y)].is_passable(self):
return self.__class__(self.bg, self.side, x, y, self.char, self.original_color)
return None
def die(self):
self.bg.tiles[(self.x, self.y)].entity = None
self.alive = False
def get_char(self, x, y):
return self.char
def get_passable_neighbours(self):
neighbours = [(self.x+i, self.y+j) for i in range(-1,2) for j in range(-1,2)]
return filter(lambda t: self.bg.tiles[t].passable and t != (self.x, self.y), neighbours)
def get_pushed(self, dx, dy):
self.pushed = False
self.move(dx, dy)
self.pushed = True
def is_ally(self, entity):
return self.side == entity.side
def move(self, dx, dy):
if self.pushed:
self.pushed = False
return False
(dx,dy) = (int(dx), int(dy))
next_tile = self.bg.tiles[(self.x+dx, self.y+dy)]
if self.can_move(dx, dy):
if next_tile.entity is not None:
next_tile.entity.get_pushed(dx, dy)
self.bg.tiles[(self.x, self.y)].entity = None
next_tile.entity = self
self.x += dx
self.y += dy
return True
return False
def move_path(self):
if self.path:
next_tile = self.path.pop(0)
if self.move(next_tile.x - self.x, next_tile.y - self.y):
return True
else:
self.path.insert(0, next_tile)
return False
def register_kill(self, killed):
self.kills += 1
if self.owner:
self.owner.kills += 1
def reset_action(self):
self.next_action = self.default_next_action
def teleport(self, x, y):
if self.bg.tiles[(x, y)].entity is None and self.bg.tiles[(x, y)].is_passable(self):
self.bg.tiles[(x, y)].entity = self
self.bg.tiles[(self.x, self.y)].entity = None
(self.x, self.y) = (x, y)
return True
return False
def update(self):
for s in self.statuses:
s.update()
class BigEntity(Entity):
def __init__(self, battleground, side, x, y, chars=["a", "b", "c", "d"], colors=[libtcod.white]*4):
super(BigEntity, self).__init__(battleground, side, x, y, chars[0], colors[0])
self.chars = chars
self.colors = colors
self.length = int(math.sqrt(len(self.chars)).real)
self.update_body()
def can_be_pushed(self, dx, dy):
return False
def can_move(self, dx, dy):
for (x,y) in [(self.x+dx+x,self.y+dy+y) for x in range (0, self.length) for y in range (0, self.length)]:
next_tile = self.bg.tiles[(x, y)]
if not next_tile.is_passable(self): return False
if next_tile.entity is None: continue
if not next_tile.entity.is_ally(self): return False
if next_tile.entity is self: continue
if not next_tile.entity.can_be_pushed(dx, dy): return False
return True
def clear_body(self):
for i in range(self.length):
for j in range(self.length):
self.bg.tiles[(self.x+i, self.y+j)].entity = None
def die(self):
self.clear_body()
self.alive = False
def get_char(self, dx, dy):
self.color = self.colors[self.length*dx+dy]
return self.chars[self.length*dx+dy]
def move(self, dx, dy):
if self.pushed:
self.pushed = False
return
next_tile = self.bg.tiles[(self.x+dx, self.y+dy)]
if self.can_move(dx, dy):
if next_tile.entity is not None and next_tile.entity is not self:
next_tile.entity.get_pushed(dx, dy)
self.clear_body()
next_tile.entity = self
self.x += dx
self.y += dy
self.update_body()
def update_body(self):
for i in range(self.length):
for j in range(self.length):
self.bg.tiles[(self.x+i, self.y+j)].entity = self
class Fortress(BigEntity):
def __init__(self, battleground, side=NEUTRAL_SIDE, x=-1, y=-1, chars=[':']*4, colors=[libtcod.white]*4, requisition_production=1):
super(Fortress, self).__init__(battleground, side, x, y, chars, colors)
self.capacity = len(chars)
self.connected_fortresses = []
self.guests = []
self.name = "Fortress"
self.requisition_production = requisition_production
def can_be_attacked(self):
return True
def can_host(self, entity):
return self.side == entity.side or self.side == NEUTRAL_SIDE
def can_move(self, dx, dy):
return False
def get_connections(self):
# Gather all tiles inside and surrounding the fortress
starting_tiles = [(self.x+i, self.y+j) for i in range(-1,3) for j in range(-1,3)]
# Remove those inside it
checked = [(self.x+i, self.y+j) for i in range(0,2) for j in range(0,2)]
starting_tiles = filter(lambda t: self.bg.tiles[t].passable and t not in checked, starting_tiles)
# Try every reachable tile from the fortress and save the connections
for starting in starting_tiles:
tiles = [starting]
while tiles:
(x, y) = tiles.pop()
checked.append((x, y))
for t in [(x+i, y+j) for i in range(-1,2) for j in range(-1,2)]:
entity = self.bg.tiles[t].entity
if entity in self.bg.fortresses and entity is not self and entity not in self.connected_fortresses:
self.connected_fortresses.append((entity, starting))
if self.bg.tiles[t].passable and t not in checked:
tiles.append(t)
def host(self, entity):
if not self.can_host(entity) or len(self.guests) >= self.capacity: return
if not self.guests:
self.side = entity.side
self.bg.tiles[(entity.x, entity.y)].entity = None
(entity.x, entity.y) = (self.x, self.y)
self.bg.generals.remove(entity)
self.chars[len(self.guests)] = entity.char
self.colors[len(self.guests)] = entity.color
self.guests.append(entity)
self.update_body()
def refresh_chars(self):
self.chars = [':']*len(self.chars)
self.colors = [libtcod.white]*len(self.colors)
for i in range(len(self.guests)):
self.chars[i] = self.guests[i].char
self.colors[i] = self.guests[i].color
def unhost(self, entity):
self.guests.remove(entity)
self.bg.generals.append(entity)
self.refresh_chars()
if not self.guests:
self.side = NEUTRAL_SIDE
class Mine(Entity):
def __init__(self, battleground, x=-1, y=-1, power=50):
super(Mine, self).__init__(battleground, NEUTRAL_SIDE, x, y, 'X', libtcod.red)
self.power = power
def can_be_attacked(self):
return True
def clone(self, x, y):
if self.bg.is_inside(x, y) and self.bg.tiles[(x, y)].entity is None and self.bg.tiles[(x, y)].is_passable(self):
return self.__class__(self.bg, x, y, self.power)
return None
def get_attacked(self, attacker):
if attacker.can_be_attacked():
attacker.get_attacked(self)
self.die()