-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentities.py
284 lines (230 loc) · 9.15 KB
/
entities.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
from globals import *
from pathfinding import *
import random
# Base entity class
class Entity:
def __init__(self, _type, pos, entity_id=None, count=None):
assert _type in ENTITY_NAMES, "Error: ${0} not found in lookup table.".format(_type)
self.entity_id = entity_id
self._type = _type
self.pos = pos
self.count = count
self.sprite = LOOKUP_STATS['sprite'][_type]
def getTransmissable(self):
return {
'type': self._type,
'pos': self.pos,
'count': self.count,
'sprite': self.sprite,
}
# Effects are special entities
class Effect(Entity):
def __init__(self, _type, pos, entity_id=None, count=None):
super().__init__(_type, pos, entity_id)
# TBD - HANDLE THIS
self.timer = LOOKUP_STATS['effects'][_type]['timer']
# self.entity_id = entity_id
# self._type = _type
# self.pos = pos
# self.count = count
# self.sprite = LOOKUP_STATS['sprite'][_type]
def getTransmissable(self):
return {
'type': self._type,
'pos': self.pos,
'count': self.count,
'sprite': self.sprite,
'timer': self.timer,
}
# Entity that can move around the screen
class MoveableEntity(Entity):
def __init__(self, _type, pos, entity_id=None, player_class=None):
super().__init__(_type, pos, entity_id)
# particulars
# self._type = _type
# self.pos = pos
self.last_pos = pos # used to store "prior" position when going up and down stairs
# self.entity_id = entity_id # only used for logged in players
# stats
self.hp = LOOKUP_STATS['maxHP'][_type]
self.maxHP = LOOKUP_STATS['maxHP'][_type]
self.active = True
self.inventory = {}
self.effect_timeout = 0
self.atk = 1
self.ac = 1
# interaction - ['msg', ticksRemaining]
self.chatlog = []
# update health
def updateHealth(self, amt):
# print(amt,self.hp)
self.hp += amt
# constrain - still valid
if self.hp > self.maxHP:
self.hp = self.maxHP
return True
# dead - return invalid
if self.hp <= 0:
return False
return True
# chat related - only 1 can be active per entity
def addChat(self, msg):
self.chatlog = [msg, MSG_TIME]
def hasChat(self):
if len(self.chatlog) > 0:
return True
return False
# generic update function per entity
def update(self):
# next time entity can cast effect
if self.effect_timeout > 0:
self.effect_timeout -= 1
self.updateChats()
def updateChats(self):
if self.hasChat():
self.chatlog[1] -= 1
if self.chatlog[1] <= 0:
self.chatlog = []
def getTransmissable(self):
retval = {
'type': self._type,
'pos': self.pos,
'hp': self.hp,
'maxHP': self.maxHP,
'active': self.active,
'inventory': self.inventory,
'sprite': self.sprite,
'chatlog' : self.chatlog,
'effect_timeout': self.effect_timeout,
}
return retval
class Player(MoveableEntity):
def __init__(self, _type, pos, entity_id=None, player_class=None, game=None):
super().__init__(_type, pos, entity_id)
# class info
assert player_class is not None, "Error: missing reference to player class"
self.player_class = player_class
self.player_stats = {
'level': 0,
'woodcutting': 0,
'strength': 0,
'defense': 0,
'speed': 0,
'magic': 0,
'mining': 0,
'firemaking': 0,
'intelligence': 0,
}
# reference to game map info
assert game is not None, "Error: missing reference to game object"
self.game = game
def update(self):
super().update()
def getTransmissable(self):
retval = {
'type': self._type,
'pos': self.pos,
'hp': self.hp,
'maxHP': self.maxHP,
'active': self.active,
'inventory': self.inventory,
'sprite': self.sprite,
'chatlog' : self.chatlog,
'effect_timeout': self.effect_timeout,
'stats': self.player_stats,
}
return retval
class Monster(MoveableEntity):
def __init__(self, _type, pos, entity_id=None, game=None):
super().__init__(_type, pos, entity_id)
# reference to game map info
assert game is not None, "Error: missing reference to game object"
self.game = game
# targeting info
self.target = None
# pathfinding
self.pathfinding_map = GridWithWeights(game.gameMap[pos['level']], game.NUM_COLS, game.NUM_ROWS)
def setTarget(self, other_id):
self.target = other_id
# returns ID of target
def getTarget(self):
return self.target
def removeTarget(self):
self.target = None
def update(self, players_by_level):
super().update() # update generic entity things
# TBD (follower):
# 1 if no target, set target
# 2 if target, follow
# 3 if target out of range, forget
# add a check to see what type of e this is as well
# make follow dist a function of the lookup table!!
tgt = self.getTarget()
if tgt is not None and tgt in self.game.players: # we have a target
# same level test / meditating test
if self.game.players[tgt].pos['level'] != self.pos['level'] or not self.game.players[tgt].active:
self.removeTarget()
else:
# if in range, follow
player = self.game.players[tgt]
d = abs(self.pos['c'] - player.pos['c']) + abs(self.pos['r'] - player.pos['r'])
if d > self.game.CAM_NUM_COLS: # forget!
self.removeTarget()
else: # follow!
enemy_grid_id = (self.pos['c'], self.pos['r'])
player_grid_id = (player.pos['c'], player.pos['r'])
came_from, cost_so_far = a_star_search(self.pathfinding_map, enemy_grid_id, player_grid_id)
path = reconstruct_path(came_from, enemy_grid_id, player_grid_id)
# grab the 2nd option as the first is the entity itself
if len(path) > 1:
next_c = path[1][0]
next_r = path[1][1]
# distance to player is the last entry
last_id = path[-1]
# walkable AND the distance is farther away
if last_id in cost_so_far and cost_so_far[last_id] > 1 and self.game.isWalkable(next_c, next_r, player.pos['level']):
self.pos['r'] = next_r
self.pos['c'] = next_c
else: # no target
min_dist, min_id = self.game.CAM_NUM_COLS, "none"
# for pk, pv in players_by_level[self.pos['level']].items():
for pk, pv in players_by_level.items():
if pv.active:
d = abs(self.pos['c'] - pv.pos['c']) + abs(self.pos['r'] - pv.pos['r'])
if d < min_dist:
min_dist = d
min_id = pv.entity_id
# we found a target
if min_id in self.game.players:
self.setTarget(min_id)
# add random flavor text if following
if random.random() > 0.5 and not self.hasChat():
self.addChat(random.choice(ENEMY_FLAVOR_PHRASES_RANDOM))
else:
new_dir = random.choice(ENEMY_DIRS)
new_pos = {'r': self.pos['r'] + new_dir['r'], 'c': self.pos['c'] + new_dir['c']}
if self.game.isWalkable(new_pos['c'], new_pos['r'], self.pos['level']):
self.pos['r'] = new_pos['r']
self.pos['c'] = new_pos['c']
# A blob that just replicates and doesn't move
class SlimeMold(Monster):
def __init__(self, _type, pos, entity_id=None, game=None):
super().__init__(_type, pos, entity_id, game)
def update(self, players_by_level):
if random.random() > 0.90: # replicate
neighbors = [{'level': self.pos['level'], 'c': d['c']+self.pos['c'], 'r': d['r']+self.pos['r']} for d in ENEMY_DIRS]
n = random.choice(neighbors)
#if self.game.isWalkable(n['c'], n['r'], n['level']):
if self.game.isEmpty(n['c'], n['r'], n['level']):
e = self.game.addEnemy("slimeMold", n)
if e is not None:
self.game.enemies[n['level']].append(e)
class NPC(MoveableEntity):
def __init__(self, _type, pos, wander_radius, entity_id=None, game=None):
super().__init__(_type, pos, entity_id, game)
self.wander_radius = wander_radius
# wander within a predefined radius
def update(self):
# move entity
if random.random() > 0.75:
pass