forked from pythonwars/PythonWars-1996
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
376 lines (315 loc) · 14.9 KB
/
db.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
# PythonWars copyright © 2020, 2021 by Paul Penner. All rights reserved.
# In order to use this codebase you must comply with all licenses.
#
# Original Diku Mud copyright © 1990, 1991 by Sebastian Hammer,
# Michael Seifert, Hans Henrik Stærfeldt, Tom Madsen, and Katja Nyboe.
#
# Merc Diku Mud improvements copyright © 1992, 1993 by Michael
# Chastain, Michael Quan, and Mitchell Tse.
#
# ROM 2.4 is copyright 1993-1998 Russ Taylor. ROM has been brought to
# you by the ROM consortium: Russ Taylor (rtaylor@hypercube.org),
# Gabrielle Taylor (gtaylor@hypercube.org), and Brian Moore (zump@rom.org).
#
# Ported to Python by Davion of MudBytes.net using Miniboa
# (https://code.google.com/p/miniboa/).
#
# In order to use any part of this Merc Diku Mud, you must comply with
# both the original Diku license in 'license.doc' as well the Merc
# license in 'license.txt'. In particular, you may not remove either of
# these copyright notices.
#
# Much time and thought has gone into this software, and you are
# benefiting. We hope that you share your changes too. What goes
# around, comes around.
import sys
import comm
import const
import data_loader
import database.read.read_tables as read
import game_utils
import handler_game
import handler_item
import instance
import merc
import object_creator
import settings
import state_checks
import update
def boot_db():
import handler_npc
import handler_room
import world_classes
init_time()
init_instance()
read.read_tables()
data_loader.load_areas()
world_classes.Note.load()
area_update()
object_creator.setup_exits()
update.instance_number_save()
results = (
"------------------------[ Templates ]------------------------",
" Areas : {:6,} NPCs : {:6,} Items : {:6,}".format(world_classes.Area.template_count,
handler_npc.Npc.template_count,
handler_item.Items.template_count),
" Rooms : {:6,} Shops : {:6,}".format(handler_room.Room.template_count, len(instance.shop_templates)),
" Total : {:6,}".format(world_classes.Area.template_count + handler_npc.Npc.template_count +
handler_item.Items.template_count + handler_room.Room.template_count +
len(instance.shop_templates)),
"------------------------[ Instances ]------------------------",
" Areas : {:6,} NPCs : {:6,} Items : {:6,}".format(world_classes.Area.instance_count,
handler_npc.Npc.instance_count,
handler_item.Items.instance_count),
" Rooms : {:6,} Resets : {:6,} Helps : {:6,}".format(handler_room.Room.instance_count,
world_classes.Reset.load_count,
len(instance.help_list)),
" Socials : {:6,} Notes : {:6,}".format(len(const.social_table), len(instance.note_list)),
" Total : {:6,}".format(world_classes.Area.instance_count + handler_npc.Npc.instance_count +
handler_item.Items.instance_count + handler_room.Room.instance_count +
world_classes.Reset.load_count + len(instance.note_list) + len(instance.help_list) +
len(const.social_table)),
"------------------------[ Snap Shot ]------------------------"
)
spaces = "\n" + " " * 51
comm.notify(spaces.join(results), merc.CONSOLE_BOOT)
def init_instance():
# First lets add the bad terms we dont want to pass during instancing, while copying attributes
instance.not_to_instance.append("instance_id")
instance.not_to_instance.append("act")
try:
with open(settings.INSTANCE_FILE, "x+") as fp:
fp.write("0")
except FileExistsError:
with open(settings.INSTANCE_FILE, "r") as fp:
dummy, instance.max_instance_id = game_utils.read_int(fp.read())
if instance.max_instance_id == 0 or not instance.max_instance_id:
comm.notify("First run, or problem with the instance, setting to 6000", merc.CONSOLE_WARNING)
instance.max_instance_id = 6000
else:
comm.notify("Global Instance Tracker, instances thus far: {}".format(instance.max_instance_id), merc.CONSOLE_INFO)
def fix_exits():
for k, r in instance.room_templates.items():
for e in r.template_exit[:]:
if e and type(e.template_to_room) == int:
if e.template_to_room not in instance.room_templates:
comm.notify("fix_exits: Failed to find to_room for {}: {}".format(r.template_vnum, e.template_to_room), merc.CONSOLE_ERROR)
e.template_to_room = None
r.template_exit.remove(e)
else:
e.template_to_room = instance.room_templates[e.template_to_room]
# Repopulate areas periodically.
def area_update():
for area_id, area in instance.areas.items():
area.age += 1
if area.age < 3:
continue
# Check for PC's.
if area.player_count > 0 and area.age == 15 - 1:
for ch in list(instance.players.values()):
if ch.is_awake() and ch.in_room and ch.in_room.area == area:
ch.send("You hear an agonised scream in the distance.\n")
# Check age and reset.
# Note: Mud School resets every 3 minutes (not 15).
if not area.empty and (area.player_count == 0 or area.age >= 15):
reset_area(area)
area.age = game_utils.number_range(0, 3)
school_instance_id = instance.instances_by_room[merc.ROOM_VNUM_SCHOOL][0]
school_instance = instance.rooms[school_instance_id]
if school_instance and area_id == school_instance.area:
area.age = 15 - 3
elif area.player_count == 0:
area.empty = True
def m_reset(preset, last, level, npc):
if preset.arg1 not in instance.npc_templates.keys():
comm.notify("m_reset: 'M': bad vnum {}".format(preset.arg1), merc.CONSOLE_WARNING)
return last, level, npc
else:
npc_template = instance.npc_templates[preset.arg1]
if preset.arg3 not in instance.room_templates.keys():
comm.notify("reset_area: 'R': bad vnum {}".format(preset.arg3), merc.CONSOLE_WARNING)
return last, level, npc
else:
room_instance_id = instance.instances_by_room[preset.arg3][0]
room_instance = instance.global_instances[room_instance_id]
level = state_checks.urange(0, npc_template.level - 2, merc.LEVEL_HERO)
if npc_template.count >= preset.arg2:
last = False
return last, level, npc
npc = object_creator.create_mobile(npc_template)
# Check for pet shop.
if room_instance.vnum - 1 in instance.room_templates.keys():
prev_room_instance_id = instance.instances_by_room[room_instance.vnum - 1][0]
prev_room_instance = instance.global_instances[prev_room_instance_id]
if prev_room_instance.room_flags.is_set(merc.ROOM_PET_SHOP):
npc.act.set_bit(merc.ACT_PET)
if room_instance.is_dark():
npc.affected_by.set_bit(merc.AFF_INFRARED)
# set area
npc.area = room_instance.area
room_instance.put(npc)
level = state_checks.urange(0, npc.level - 2, merc.LEVEL_HERO)
last = True
return last, level, npc
def o_reset(parea, preset, last, level, npc):
if preset.arg1 not in instance.item_templates.keys():
comm.notify("o_reset: 'O': bad vnum {}".format(preset.arg1), merc.CONSOLE_WARNING)
return last, level, npc
else:
item_template = instance.item_templates[preset.arg1]
if preset.arg3 not in instance.room_templates.keys():
comm.notify("o_reset: 'R': bad vnum {}".format(preset.arg3), merc.CONSOLE_WARNING)
return last, level, npc
else:
room_instance_id = instance.instances_by_room[preset.arg3][0]
room_instance = instance.global_instances[room_instance_id]
if parea.player_count > 0 or handler_item.count_obj_list(item_template, room_instance.items) > 0:
last = False
return last, level, npc
item = object_creator.create_item(item_template, game_utils.number_range(1, 50))
item.cost = 0
room_instance.put(item)
last = True
return last, level, npc
def p_reset(parea, preset, last, level, npc):
if preset.arg1 not in instance.item_templates.keys():
comm.notify("p_reset: 'P': bad vnum {}".format(preset.arg1), merc.CONSOLE_WARNING)
return last, level, npc
else:
item_template = instance.item_templates[preset.arg1]
if preset.arg3 not in instance.item_templates.keys():
comm.notify("p_reset: 'P': bad vnum {}".format(preset.arg3), merc.CONSOLE_WARNING)
return last, level, npc
else:
item_to_template = instance.item_templates[preset.arg3]
item_to = None
item_to_list = instance.instances_by_item.get(item_to_template.vnum, None)
if item_to_list:
item_to = instance.global_instances[item_to_list[0]]
if parea.player_count > 0 or not item_to or not item_to.in_room or handler_item.count_obj_list(item_template, item_to.inventory) > 0:
last = False
return last, level, npc
item = object_creator.create_item(item_template, game_utils.number_range(1, 50))
item_to.put(item)
last = True
return last, level, npc
def g_e_reset(preset, last, level, npc):
if preset.arg1 not in instance.item_templates.keys():
comm.notify("g_e_reset: 'E' or 'G': bad vnum {}".format(preset.arg1), merc.CONSOLE_WARNING)
return last, level, npc
else:
item_template = instance.item_templates[preset.arg1]
if not last:
return last, level, npc
if not npc:
comm.notify("g_e_reset: 'E' or 'G': None mob for vnum {}".format(preset.arg1), merc.CONSOLE_WARNING)
last = False
return last, level, npc
if npc.pshop:
if item_template.item_type in [merc.ITEM_PILL, merc.ITEM_POTION]:
olevel = game_utils.number_range(0, 10)
elif item_template.item_type == merc.ITEM_SCROLL:
olevel = game_utils.number_range(5, 15)
elif item_template.item_type == merc.ITEM_WAND:
olevel = game_utils.number_range(10, 20)
elif item_template.item_type == merc.ITEM_STAFF:
olevel = game_utils.number_range(15, 25)
elif item_template.item_type in [merc.ITEM_ARMOR, merc.ITEM_WEAPON]:
olevel = game_utils.number_range(5, 15)
else:
olevel = 0
item = object_creator.create_item(item_template, olevel)
item.flags.shop_inventory = True
else:
item = object_creator.create_item(item_template, game_utils.number_range(1, 50))
npc.put(item)
if preset.command == "E":
npc.equip(item, True)
last = True
return last, level, npc
def d_reset(preset, last, level, npc):
if preset.arg1 not in instance.room_templates.keys():
comm.notify("d_reset: 'D': bad vnum {}".format(preset.arg1), merc.CONSOLE_WARNING)
return last, level, npc
else:
room_instance_id = instance.instances_by_room[preset.arg1][0]
room_instance = instance.global_instances[room_instance_id]
pexit = room_instance.exit[preset.arg2]
if not pexit:
return last, level, npc
if preset.arg3 == 0:
pexit.exit_info.rem_bit(merc.EX_CLOSED)
pexit.exit_info.rem_bit(merc.EX_LOCKED)
elif preset.arg3 == 1:
pexit.exit_info.set_bit(merc.EX_CLOSED)
pexit.exit_info.rem_bit(merc.EX_LOCKED)
elif preset.arg3 == 2:
pexit.exit_info.set_bit(merc.EX_CLOSED)
pexit.exit_info.set_bit(merc.EX_LOCKED)
last = True
return last, level, npc
def r_reset(preset, last, level, npc):
if preset.arg1 not in instance.room_templates.keys():
comm.notify("r_reset: 'R': bad vnum {}".format(preset.arg1), merc.CONSOLE_WARNING)
return last, level, npc
else:
room_instance_id = instance.instances_by_room[preset.arg1][0]
room_instance = instance.global_instances[room_instance_id]
for d0 in merc.irange(preset.arg2 - 1):
d1 = game_utils.number_range(d0, preset.arg2 - 1)
pexit = room_instance.exit[d0]
room_instance.exit[d0] = room_instance.exit[d1]
room_instance.exit[d1] = pexit
return last, level, npc
def reset_area(parea):
npc = None
last = True
level = 0
for preset in parea.reset_list[:]:
if preset.command == "M":
last, level, npc = m_reset(preset, last, level, npc)
elif preset.command == "O":
last, level, npc = o_reset(parea, preset, last, level, npc)
elif preset.command == "P":
last, level, npc = p_reset(parea, preset, last, level, npc)
elif preset.command in ["G", "E"]:
last, level, npc = g_e_reset(preset, last, level, npc)
elif preset.command == "D":
last, level, npc = d_reset(preset, last, level, npc)
elif preset.command == "R":
last, level, npc = r_reset(preset, last, level, npc)
else:
comm.notify("reset_area: bad command ({})".format(preset.command), merc.CONSOLE_CRITICAL)
sys.exit(1)
def init_time():
lhour = int(merc.current_time - 650336715) // (merc.PULSE_TICK // merc.PULSE_PER_SECOND)
handler_game.time_info.hour = lhour % 24
lday = lhour // 24
handler_game.time_info.day = int(lday % 35)
lmonth = lday // 35
handler_game.time_info.month = lmonth % 17
handler_game.time_info.year = lmonth // 17
if handler_game.time_info.hour < 5:
handler_game.weather_info.sunlight = merc.SUN_DARK
elif handler_game.time_info.hour < 6:
handler_game.weather_info.sunlight = merc.SUN_RISE
elif handler_game.time_info.hour < 19:
handler_game.weather_info.sunlight = merc.SUN_LIGHT
elif handler_game.time_info.hour < 20:
handler_game.weather_info.sunlight = merc.SUN_SET
else:
handler_game.weather_info.sunlight = merc.SUN_DARK
handler_game.weather_info.change = 0
handler_game.weather_info.mmhg = 960
if 7 <= handler_game.time_info.month <= 12:
handler_game.weather_info.mmhg += game_utils.number_range(1, 50)
else:
handler_game.weather_info.mmhg += game_utils.number_range(1, 80)
if handler_game.weather_info.mmhg <= 980:
handler_game.weather_info.sky = merc.SKY_LIGHTNING
elif handler_game.weather_info.mmhg <= 1000:
handler_game.weather_info.sky = merc.SKY_RAINING
elif handler_game.weather_info.mmhg <= 1020:
handler_game.weather_info.sky = merc.SKY_CLOUDY
else:
handler_game.weather_info.sky = merc.SKY_CLOUDLESS