-
Notifications
You must be signed in to change notification settings - Fork 1
/
familiar.py
290 lines (256 loc) · 9.99 KB
/
familiar.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
__filename__ = "familiar.py"
__author__ = "Bob Mottram"
__credits__ = ["Bob Mottram"]
__license__ = "AGPL3+"
__version__ = "1.0.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "NPCs"
from functions import random_desc
from functions import deepcopy
# from copy import deepcopy
# Movement modes for familiars
FAMILIAR_MODES = ("follow", "scout", "hide", "see")
def get_familiar_modes():
return FAMILIAR_MODES
def get_familiar_name(players: {}, id, npcs: {}) -> str:
"""Returns the name of the familiar of the given player
"""
plyr = players[id]
if plyr['familiar'] != -1:
for _, npc1 in list(npcs.items()):
if npc1['familiarOf'] == plyr['name']:
return npc1['name']
return ''
def familiar_recall(mud, players: {}, id, npcs: {}, npcs_db: {}):
"""Move any familiar to the player's location
"""
plyr = players[id]
# remove any existing familiars
removals = []
for nid, npc1 in npcs.items():
if npc1['familiarOf'] == plyr['name']:
removals.append(nid)
for index in removals:
del npcs[index]
# By default player has no familiar
plyr['familiar'] = -1
# Find familiar and set its room to that of the player
for nid, npc1 in npcs_db.items():
if npc1['familiarOf'] != plyr['name']:
continue
plyr['familiar'] = int(nid)
if not npcs.get(nid):
npcs[nid] = deepcopy(npcs_db[nid])
npcs[nid]['room'] = plyr['room']
mud.send_message(id, "Your familiar is recalled.\n\n")
break
def familiar_default_mode(nid: int, npcs: {}, npcs_db: {}):
npcs[nid]['familiarMode'] = "follow"
npcs_db[nid]['familiarMode'] = "follow"
npcs[nid]['moveType'] = ""
npcs_db[nid]['moveType'] = ""
npcs[nid]['path'] = []
npcs_db[nid]['path'] = []
def familiar_sight(mud, nid: int, npcs: {}, npcs_db: {}, rooms: {},
players: {}, id,
items: {}, items_db: {}):
"""familiar reports what it sees
"""
start_room_id = npcs[nid]['room']
room_exits = rooms[start_room_id]['exits']
mud.send_message(id, "Your familiar says:\n")
if len(room_exits) == 0:
mud.send_message(id, "There are no exits.")
else:
if len(room_exits) > 1:
mud.send_message(id, "There are " + str(len(room_exits)) +
" exits.")
else:
exit_description = random_desc("a single|one")
mud.send_message(id, "There is " + exit_description + " exit.")
creatures_count = 0
creatures_friendly = 0
creatures_races = []
for _, plyr in players.items():
if plyr['name'] is None:
continue
if plyr['room'] != npcs[nid]['room']:
continue
creatures_count = creatures_count+1
if plyr['race'] not in creatures_races:
creatures_races.append(plyr['race'])
for name, value in plyr['affinity'].items():
if npcs[nid]['familiarOf'] == name:
if value >= 0:
creatures_friendly = creatures_friendly + 1
for n_co, npc1 in npcs.items():
if n_co == nid:
continue
if npc1['room'] != npcs[nid]['room']:
continue
creatures_count = creatures_count+1
if npc1.get('race'):
if npc1['race'] not in creatures_races:
creatures_races.append(npc1['race'])
if npc1.get('affinity'):
for name, value in npc1['affinity'].items():
if npcs[nid]['familiarOf'] == name:
if value >= 0:
creatures_friendly = creatures_friendly + 1
if creatures_count > 0:
creature_str = random_desc("creature|being|entity")
creatures_msg = 'I see '
if creatures_count > 1:
creatures_msg = creatures_msg + str(creatures_count) + ' ' + \
creature_str + 's'
else:
creatures_msg = creatures_msg + 'one ' + creature_str
creatures_msg = creatures_msg + ' here.'
friendly_word = \
random_desc("friendly|nice|pleasing|not threatening")
if creatures_friendly > 0:
if creatures_friendly > 1:
creatures_msg = creatures_msg + ' ' + \
str(creatures_friendly) + ' are ' + friendly_word + '.'
else:
if creatures_friendly == creatures_count:
creatures_msg = \
creatures_msg + ' They are ' + friendly_word + '.'
else:
creatures_msg = \
creatures_msg + ' One is ' + friendly_word + '.'
creatures_msg = creatures_msg + '\n'
if len(creatures_races) > 0:
creatures_msg = creatures_msg + 'They are '
if len(creatures_races) == 1:
creatures_msg = \
creatures_msg + '<f220>' + creatures_races[0] + 's<r>.'
else:
if len(creatures_races) == 2:
creatures_msg = \
creatures_msg + '<f220>' + creatures_races[0] + \
's<r> and <f220>' + creatures_races[1] + 's<r>.'
else:
ctr = 0
for rac in creatures_races:
if ctr == 0:
creatures_msg = \
creatures_msg + '<f220>' + rac + 's<r>'
if ctr > 0:
if ctr < len(creatures_races) - 1:
creatures_msg = \
creatures_msg + ', <f220>' + rac + 's<r>'
else:
creatures_msg = \
creatures_msg + ' and <f220>' + \
rac + 's<r>.'
ctr = ctr + 1
mud.send_message(id, creatures_msg)
items_in_room = 0
weapons_in_room = 0
armor_in_room = 0
edible_in_room = 0
for _, itemobj in items.items():
if itemobj['room'] != npcs[nid]['room']:
continue
if not itemobj.get('weight'):
continue
if itemobj['weight'] > 0:
items_in_room += 1
if (itemobj['mod_str'] > 0 and
(itemobj['clo_lhand'] > 0 or
itemobj['clo_rhand'] > 0)):
weapons_in_room += 1
if itemobj['mod_endu'] > 0 and \
itemobj['clo_chest'] > 0:
armor_in_room += 1
if itemobj['edible'] != 0:
edible_in_room += 1
if armor_in_room > 0 and weapons_in_room > 0:
mud.send_message(id, 'There are some weapons and armor here.')
else:
if armor_in_room > 0:
mud.send_message(id, 'There is some armor here.')
else:
if weapons_in_room > 0:
mud.send_message(id, 'There are some weapons here.')
else:
mud.send_message(id, 'There are some items here.')
if edible_in_room:
mud.send_message(id, 'There are some edibles here.')
mud.send_message(id, '\n\n')
def familiar_hide(nid: int, npcs: {}, npcs_db: {}):
"""Causes a familiar to hide
"""
npcs[nid]['familiarMode'] = "hide"
npcs_db[nid]['familiarMode'] = "hide"
def familiar_is_hidden(players: {}, id, npcs: {}) -> bool:
"""Returns true if the familiar of the player is hidden
TODO: currently unused
"""
plyr = players[id]
if plyr['familiar'] != -1:
for _, npc1 in npcs.items():
if npc1['familiarOf'] == plyr['name']:
if npc1['familiarMode'] == 'hide':
return True
return False
def _familiar_scout_any_direction(familiar_size: int, start_room_id: int,
room_exits: {}, rooms: {}) -> []:
"""Scout in any direction
"""
new_path = [start_room_id]
for _, rmid in room_exits.items():
if rooms[rmid]['maxPlayerSize'] > -1:
if familiar_size > rooms[rmid]['maxPlayerSize']:
continue
new_path.append(rmid)
new_path.append(start_room_id)
if len(new_path) == 1:
new_path.clear()
return new_path
def _familiar_scout_in_direction(mud, players: {}, id, start_room_id: int,
room_exits: {},
direction, rooms: {}) -> []:
"""Scout in the given direction
"""
new_path = []
if room_exits.get(direction):
if rooms[room_exits[direction]]['maxPlayerSize'] > -1:
if players[id]['siz'] <= \
rooms[room_exits[direction]]['maxPlayerSize']:
new_path = [start_room_id, room_exits[direction]]
else:
mud.send_message(id, "It's too small to enter!\n\n")
else:
new_path = [start_room_id, room_exits[direction]]
else:
mud.send_message(id, "I can't go that way!\n\n")
return new_path
def familiar_scout(mud, players: {}, id, nid: int, npcs: {},
npcs_db: {}, rooms: {}, direction):
"""familiar begins scouting the surrounding rooms
"""
npc1 = npcs[nid]
start_room_id = npc1['room']
room_exits = rooms[start_room_id]['exits']
new_path = []
if direction == 'any' or direction == 'all' or len(direction) == 0:
new_path = \
_familiar_scout_any_direction(npc1['siz'], start_room_id,
room_exits, rooms)
else:
new_path = \
_familiar_scout_in_direction(mud, players, id, start_room_id,
room_exits, direction, rooms)
if len(new_path) > 0:
npc1['familiarMode'] = "scout"
npc1['moveType'] = "patrol"
npc1['path'] = deepcopy(new_path)
npcs_db[nid]['familiarMode'] = "scout"
npcs_db[nid]['moveType'] = "patrol"
npcs_db[nid]['path'] = deepcopy(new_path)
else:
familiar_default_mode(nid, npcs, npcs_db)