1 # This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
2 # or any later version. For details on its copyright, license, and warranties,
3 # see the file NOTICE in the root directory of the PlomRogue source package.
6 from server.io import log, strong_write
7 from server.config.world_data import world_db, symbols_passable, directions_db
8 from server.utils import mv_yx_in_dir_legal, rand, id_setter
9 from server.config.io import io_db
10 from server.new_thing import new_Thing
13 from server.update_map_memory import update_map_memory
14 from server.config.misc import make_map_func
15 from server.utils import libpr
17 def free_pos(plant=False):
20 err = "Space to put thing on too hard to find. Map too small?"
22 y = rand.next() % world_db["MAP_LENGTH"]
23 x = rand.next() % world_db["MAP_LENGTH"]
24 pos = y * world_db["MAP_LENGTH"] + x;
26 and "." == chr(world_db["MAP"][pos])) \
27 or ":" == chr(world_db["MAP"][pos]):
32 pos_clear = (0 == len([id for id in world_db["Things"]
33 if world_db["Things"][id]["T_LIFEPOINTS"]
34 if world_db["Things"][id]["T_POSY"] == y
35 if world_db["Things"][id]["T_POSX"] == x]))
41 if world_db["MAP_LENGTH"] < 1:
42 print("Ignoring: No map length >= 1 defined.")
44 libpr.set_maplength(world_db["MAP_LENGTH"])
45 player_will_be_generated = False
46 playertype = world_db["PLAYER_TYPE"]
47 for ThingType in world_db["ThingTypes"]:
48 if playertype == ThingType:
49 if 0 < world_db["ThingTypes"][ThingType]["TT_START_NUMBER"]:
50 player_will_be_generated = True
52 if not player_will_be_generated:
53 print("Ignoring: No player type with start number >0 defined.")
56 for ThingAction in world_db["ThingActions"]:
57 if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
60 print("Ignoring beyond SEED_MAP: " +
61 "No thing action with name 'wait' defined.")
63 for name in world_db["specials"]:
64 if world_db[name] not in world_db["ThingTypes"]:
65 print("Ignoring: No valid " + name + " set.")
67 world_db["Things"] = {}
69 world_db["WORLD_ACTIVE"] = 1
71 for i in range(world_db["ThingTypes"][playertype]["TT_START_NUMBER"]):
72 id = id_setter(-1, "Things")
73 world_db["Things"][id] = new_Thing(playertype, free_pos())
74 if not world_db["Things"][0]["fovmap"]:
75 empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
76 world_db["Things"][0]["fovmap"] = empty_fovmap
77 update_map_memory(world_db["Things"][0])
78 for type in world_db["ThingTypes"]:
79 for i in range(world_db["ThingTypes"][type]["TT_START_NUMBER"]):
80 if type != playertype:
81 id = id_setter(-1, "Things")
82 plantness = world_db["ThingTypes"][type]["TT_PROLIFERATE"]
83 world_db["Things"][id] = new_Thing(type, free_pos(plantness))
84 strong_write(io_db["file_out"], "NEW_WORLD\n")
86 def thingproliferation(t, prol_map):
87 from server.new_thing import new_Thing
88 global directions_db, mv_yx_in_dir_legal
89 prolscore = world_db["ThingTypes"][t["T_TYPE"]]["TT_PROLIFERATE"]
91 (world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"] == 0 or
92 t["T_LIFEPOINTS"] >= 0.9 *
93 world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]) \
95 (1 == prolscore or 1 == (rand.next() % prolscore)):
97 for dir in [directions_db[key] for key in directions_db]:
98 mv_result = mv_yx_in_dir_legal(dir, t["T_POSY"], t["T_POSX"])
99 pos = mv_result[1] * world_db["MAP_LENGTH"] + mv_result[2]
100 if mv_result[0] and \
101 (ord(":") == prol_map[pos]
102 or (world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]
103 and ord(".") == prol_map[pos])):
104 candidates.append((mv_result[1], mv_result[2]))
106 i = rand.next() % len(candidates)
107 id = id_setter(-1, "Things")
108 newT = new_Thing(t["T_TYPE"], (candidates[i][0], candidates[i][1]))
109 world_db["Things"][id] = newT
110 if (world_db["FAVOR_STAGE"] > 0
111 and t["T_TYPE"] == world_db["PLANT_0"]):
112 world_db["GOD_FAVOR"] += 5
113 elif t["T_TYPE"] == world_db["PLANT_1"]:
114 world_db["GOD_FAVOR"] += 25
115 elif world_db["FAVOR_STAGE"] >= 4 and \
116 t["T_TYPE"] == world_db["ANIMAL_1"]:
117 log("The Island God SMILES upon a new-born bear baby.")
118 world_db["GOD_FAVOR"] += 750
123 def is_neighbor(coordinates, type):
126 length = world_db["MAP_LENGTH"]
128 diag_west = x + (ind > 0)
129 diag_east = x + (ind < (length - 1))
130 pos = (y * length) + x
131 if (y > 0 and diag_east
132 and type == chr(world_db["MAP"][pos - length + ind])) \
134 and type == chr(world_db["MAP"][pos + 1])) \
135 or (y < (length - 1) and diag_east
136 and type == chr(world_db["MAP"][pos + length + ind])) \
137 or (y > 0 and diag_west
138 and type == chr(world_db["MAP"][pos - length - (not ind)])) \
140 and type == chr(world_db["MAP"][pos - 1])) \
141 or (y < (length - 1) and diag_west
142 and type == chr(world_db["MAP"][pos + length - (not ind)])):
146 world_db["MAP"] = bytearray(b'~' * (world_db["MAP_LENGTH"] ** 2))
147 length = world_db["MAP_LENGTH"]
148 add_half_width = (not (length % 2)) * int(length / 2)
149 world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord(".")
151 y = rand.next() % length
152 x = rand.next() % length
153 pos = (y * length) + x
154 if "~" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "."):
155 if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
157 world_db["MAP"][pos] = ord(".")
158 n_trees = int((length ** 2) / 16)
160 while (i_trees <= n_trees):
161 single_allowed = rand.next() % 32
162 y = rand.next() % length
163 x = rand.next() % length
164 pos = (y * length) + x
165 if "." == chr(world_db["MAP"][pos]) \
166 and ((not single_allowed) or is_neighbor((y, x), "X")):
167 world_db["MAP"][pos] = ord("X")
169 n_colons = int((length ** 2) / 16)
171 while (i_colons <= n_colons):
172 single_allowed = rand.next() % 256
173 y = rand.next() % length
174 x = rand.next() % length
175 pos = (y * length) + x
176 if ("." == chr(world_db["MAP"][pos])
177 and ((not single_allowed) or is_neighbor((y, x), ":"))):
178 world_db["MAP"][pos] = ord(":")
181 while not altar_placed:
182 y = rand.next() % length
183 x = rand.next() % length
184 pos = (y * length) + x
185 if (("." == chr(world_db["MAP"][pos]
186 or ":" == chr(world_db["MAP"][pos]))
187 and not is_neighbor((y, x), "X"))):
188 world_db["MAP"][pos] = ord("_")
189 world_db["altar"] = (y, x)
193 from server.ai import get_dir_to_target, get_inventory_slot_to_consume, \
195 t["T_COMMAND"] = [id for id in world_db["ThingActions"]
196 if world_db["ThingActions"][id]["TA_NAME"] == "wait"][0]
197 eating = len([id for id in world_db["ThingActions"]
198 if world_db["ThingActions"][id]["TA_NAME"] == "use"]) > 0
199 picking = len([id for id in world_db["ThingActions"]
200 if world_db["ThingActions"][id]["TA_NAME"] == "pickup"]) > 0
201 if eating and picking:
202 if get_dir_to_target(t, "f"):
204 sel = get_inventory_slot_to_consume(t)
206 t["T_COMMAND"] = [id for id in world_db["ThingActions"]
207 if world_db["ThingActions"][id]["TA_NAME"]
209 t["T_ARGUMENT"] = sel
210 elif standing_on_food(t) and (len(t["T_CARRIES"]) <
211 world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]):
212 t["T_COMMAND"] = [id for id in world_db["ThingActions"]
213 if world_db["ThingActions"][id]["TA_NAME"]
216 going_to_known_food_spot = get_dir_to_target(t, "c")
217 if not going_to_known_food_spot:
218 aiming_for_walking_food = get_dir_to_target(t, "a")
219 if not aiming_for_walking_food:
220 get_dir_to_target(t, "s")
223 from server.ai import eat_vs_hunger_threshold
224 used_slots = len(t["T_CARRIES"])
225 if used_slots < world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]:
226 ids = [id for id in world_db["Things"] if world_db["Things"][id] != t
227 if not world_db["Things"][id]["carried"]
228 if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
229 if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]
232 eat_cost = eat_vs_hunger_threshold(t["T_TYPE"])
234 tid = world_db["Things"][iid]["T_TYPE"]
235 if lowest_tid == -1 or tid < lowest_tid:
236 if (t != world_db["Things"][0] and
237 (world_db["ThingTypes"][tid]["TT_TOOL"] != "food"
238 or (world_db["ThingTypes"][tid]["TT_TOOLPOWER"]
243 world_db["Things"][id]["carried"] = True
244 ty = world_db["Things"][id]["T_TYPE"]
245 if (t != world_db["Things"][0]
246 and world_db["Things"][id]["T_PLAYERDROP"]
247 and world_db["ThingTypes"][ty]["TT_TOOL"] == "food"):
248 score = int(world_db["ThingTypes"][ty]["TT_TOOLPOWER"] / 32)
249 world_db["GOD_FAVOR"] += score
250 world_db["Things"][id]["T_PLAYERDROP"] = 0
251 t["T_CARRIES"].append(id)
252 if t == world_db["Things"][0]:
253 log("You PICK UP an object.")
254 elif t == world_db["Things"][0]:
255 log("CAN'T pick up object: No storage room to carry more.")
259 """Make t rop Thing from inventory to ground indexed by T_ARGUMENT."""
260 if len(t["T_CARRIES"]):
261 id = t["T_CARRIES"][t["T_ARGUMENT"]]
262 t["T_CARRIES"].remove(id)
263 world_db["Things"][id]["carried"] = False
264 if t == world_db["Things"][0]:
265 log("You DROP an object.")
266 world_db["Things"][id]["T_PLAYERDROP"] = 1
270 if len(t["T_CARRIES"]):
271 id = t["T_CARRIES"][t["T_ARGUMENT"]]
272 type = world_db["Things"][id]["T_TYPE"]
273 if type == world_db["SLIPPERS"]:
274 if t == world_db["Things"][0]:
275 log("You use the " + world_db["ThingTypes"][type]["TT_NAME"]
276 + ". It glows in wondrous colors, and emits a sound as if "
277 + "from a dying cat. The Island God laughs.\n")
278 t["T_LIFEPOINTS"] = 1
279 from server.config.misc import decrement_lifepoints_func
280 decrement_lifepoints_func(t)
281 elif (world_db["ThingTypes"][type]["TT_TOOL"] == "carpentry"):
282 pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
283 if (world_db["MAP"][pos] == ord("X")
284 or world_db["MAP"][pos] == ord("|")):
286 for id in [id for id in world_db["Things"]
287 if not world_db["Things"][id] == t
288 if not world_db["Things"][id]["carried"]
289 if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
290 if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]:
293 for id in t["T_CARRIES"]:
294 type_material = world_db["Things"][id]["T_TYPE"]
295 if (world_db["ThingTypes"][type_material]["TT_TOOL"]
300 t["T_CARRIES"].remove(wood_id)
301 del world_db["Things"][wood_id]
302 world_db["MAP"][pos] = ord("|")
303 log("With your " + world_db["ThingTypes"][type]["TT_NAME"]
304 + " you build a WOODEN BARRIER from your "
305 + world_db["ThingTypes"][type_material]["TT_NAME"] + ".")
306 elif world_db["ThingTypes"][type]["TT_TOOL"] == "fertilizer":
307 pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
308 if world_db["MAP"][pos] == ord("."):
309 log("You create SOIL.")
310 world_db["MAP"][pos] = ord(":")
311 elif world_db["ThingTypes"][type]["TT_TOOL"] == "food":
312 t["T_CARRIES"].remove(id)
313 del world_db["Things"][id]
314 t["T_SATIATION"] += world_db["ThingTypes"][type]["TT_TOOLPOWER"]
315 if t == world_db["Things"][0]:
316 log("You CONSUME this thing.")
317 elif t == world_db["Things"][0]:
318 log("You try to use this object, but FAIL.")
320 def decrement_lifepoints(t):
321 t["T_LIFEPOINTS"] -= 1
322 live_type = t["T_TYPE"]
323 _id = [_id for _id in world_db["Things"] if world_db["Things"][_id] == t][0]
324 if 0 == t["T_LIFEPOINTS"]:
325 for id in t["T_CARRIES"]:
326 t["T_CARRIES"].remove(id)
327 world_db["Things"][id]["T_POSY"] = t["T_POSY"]
328 world_db["Things"][id]["T_POSX"] = t["T_POSX"]
329 world_db["Things"][id]["carried"] = False
330 t["T_TYPE"] = world_db["ThingTypes"][t["T_TYPE"]]["TT_CORPSE_ID"]
331 if world_db["Things"][0] == t:
332 t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
334 log("See README on how to start over.")
337 t["T_MEMMAP"] = False
338 t["T_MEMDEPTHMAP"] = False
340 n_species = len([id for id in world_db["Things"]
341 if world_db["Things"][id]["T_TYPE"] == live_type])
343 from server.new_thing import new_Thing
344 if world_db["FAVOR_STAGE"] >= 3 and \
345 live_type == world_db["ANIMAL_0"]:
346 world_db["GOD_FAVOR"] += 3000
347 log("CONGRATULATIONS! The "
348 + world_db["ThingTypes"][live_type]["TT_NAME"]
349 + " species has died out. The Island God is pleased.")
351 id = id_setter(-1, "Things")
352 world_db["Things"][id] = new_Thing(live_type,
355 + world_db["ThingTypes"][live_type]["TT_NAME"]
356 + " species has temporarily died out. "
357 + "One new-born is spawned at the altar.")
358 return world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]
364 from server.new_thing import new_Thing
365 if world_db["FAVOR_STAGE"] > 9000:
366 log("You step on a soul-less slab of stone.")
368 log("YOU ENTER SACRED GROUND.")
369 if world_db["FAVOR_STAGE"] == 0:
370 world_db["FAVOR_STAGE"] = 1
371 log("The Island God speaks to you: \"I don't trust you. You intrud"
372 + "e on the island's affairs. I think you're a nuisance at be"
373 + "st, and a danger to my children at worst. I will give you "
374 + "a chance to lighten my mood, however: For a while now, I'v"
375 + "e been trying to spread the plant "
376 + world_db["ThingTypes"][world_db["PLANT_0"]]["TT_NAME"]
378 + world_db["ThingTypes"][world_db["PLANT_0"]]["TT_SYMBOL"]
379 + "\"). I have not been very successful so far. Maybe you can"
380 + " make yourself useful there. I will count each further "
381 + world_db["ThingTypes"][world_db["PLANT_0"]]["TT_NAME"]
382 + " that grows to your favor.\"")
383 elif world_db["FAVOR_STAGE"] == 1 and world_db["GOD_FAVOR"] < 100:
384 log("The Island God will talk again when it favors you to >=100 "
386 elif world_db["FAVOR_STAGE"] == 1 and world_db["GOD_FAVOR"] >= 100:
387 world_db["FAVOR_STAGE"] = 2
388 log("The Island God speaks to you: \"You could have done worse so "
389 + "far. Maybe you are not the worst to happen to this island "
390 + "since the metal birds threw the great lightning ball. Maybe"
391 + " you can help me spread another plant. It multiplies faster"
392 + ",and it is highly nutritious: "
393 + world_db["ThingTypes"][world_db["PLANT_1"]]["TT_NAME"]
395 + world_db["ThingTypes"][world_db["PLANT_1"]]["TT_SYMBOL"]
396 + "\"). It is new. I give you the only example. Be very carefu"
397 + "l with it! I also give you another tool that may be helpful"
399 id = id_setter(-1, "Things")
400 world_db["Things"][id] = new_Thing(world_db["PLANT_1"],
402 id = id_setter(-1, "Things")
403 world_db["Things"][id] = new_Thing(world_db["TOOL_0"],
405 elif world_db["FAVOR_STAGE"] == 2 and \
406 0 == len([id for id in world_db["Things"]
407 if world_db["Things"][id]["T_TYPE"]
408 == world_db["PLANT_1"]]):
409 log("The Island God speaks to you: \"I am greatly disappointed tha"
411 + world_db["ThingTypes"][world_db["PLANT_1"]]["TT_NAME"]
412 + " this island had. Here is another one. It cost me great wor"
413 + "k. Be more careful this time when planting it.\"")
414 id = id_setter(-1, "Things")
415 world_db["Things"][id] = new_Thing(world_db["PLANT_1"],
417 world_db["GOD_FAVOR"] -= 250
418 elif world_db["FAVOR_STAGE"] == 2 and world_db["GOD_FAVOR"] < 500:
419 log("The Island God will talk again when it favors you to >=500 "
421 elif world_db["FAVOR_STAGE"] == 2 and world_db["GOD_FAVOR"] >= 500:
422 world_db["FAVOR_STAGE"] = 3
423 log("The Island God speaks to you: \"The "
424 + world_db["ThingTypes"][world_db["ANIMAL_0"]]["TT_NAME"]
425 + " has lately become a pest. These creatures do not please me"
426 + " as much as they used to do. Exterminate them all. I will c"
427 + "ount each kill to your favor. To help you with the hunting,"
428 + " I grant you the empathy and knowledge to read animals.\"")
429 log("You will now see animals' health bars, and activities (\"m\": "
430 + "moving (maybe for an attack), \"u\": eating, \"p\": picking"
431 + " something up; no letter: waiting).")
432 world_db["EMPATHY"] = 1
433 elif world_db["FAVOR_STAGE"] == 3 and world_db["GOD_FAVOR"] < 5000:
434 log("The Island God will talk again when it favors you to >=5000 "
436 elif world_db["FAVOR_STAGE"] == 3 and world_db["GOD_FAVOR"] >= 5000:
437 world_db["FAVOR_STAGE"] = 4
438 log("The Island God speaks to you: \"You know what animal I find "
440 + world_db["ThingTypes"][world_db["ANIMAL_1"]]["TT_NAME"]
441 + "! I think what this islands clearly needs more of is "
442 + world_db["ThingTypes"][world_db["ANIMAL_1"]]["TT_NAME"]
443 + "s. Why don't you help? Support them. Make sure they are "
444 + "well, and they will multiply faster. From now on, I will "
445 + "count each new-born "
446 + world_db["ThingTypes"][world_db["ANIMAL_1"]]["TT_NAME"]
447 + " (not spawned by me due to undo an extinction event) "
448 + "greatly to your favor. To help you with the feeding, here "
449 + "is something to make the ground bear more consumables.")
450 id = id_setter(-1, "Things")
451 world_db["Things"][id] = new_Thing(world_db["TOOL_1"],
453 elif world_db["GOD_FAVOR"] < 20000:
454 log("The Island God will talk again when it favors you to >=20000 "
456 elif world_db["GOD_FAVOR"] > 20000:
457 world_db["FAVOR_STAGE"] = 9001
458 log("The Island God speaks to you: \"You have proven yourself wort"
459 + "hy of my respect. You were a good citizen to the island, a"
460 + "nd sometimes a better steward to its inhabitants than me. "
461 + "The island shall miss you when you leave. But you have ear"
462 + "ned the right to do so. Take this "
463 + world_db["ThingTypes"][world_db["SLIPPERS"]]["TT_NAME"]
464 + " and USE it when you please. It will take you to where you"
465 + " came from. (But do feel free to stay here as long as you "
467 id = id_setter(-1, "Things")
468 world_db["Things"][id] = new_Thing(world_db["SLIPPERS"],
471 from server.config.world_data import symbols_passable
472 from server.build_fov_map import build_fov_map
473 from server.config.misc import decrement_lifepoints_func
474 from server.new_thing import new_Thing
476 move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
477 t["T_POSY"], t["T_POSX"])
478 if 1 == move_result[0]:
479 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
480 hitted = [id for id in world_db["Things"]
481 if world_db["Things"][id] != t
482 if world_db["Things"][id]["T_LIFEPOINTS"]
483 if world_db["Things"][id]["T_POSY"] == move_result[1]
484 if world_db["Things"][id]["T_POSX"] == move_result[2]]
487 hitted_type = world_db["Things"][hit_id]["T_TYPE"]
488 if t == world_db["Things"][0]:
489 hitted_name = world_db["ThingTypes"][hitted_type]["TT_NAME"]
490 log("You WOUND " + hitted_name + ".")
491 world_db["GOD_FAVOR"] -= 1
493 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
494 log(hitter_name +" WOUNDS you.")
495 test = decrement_lifepoints_func(world_db["Things"][hit_id])
496 if test and world_db["FAVOR_STAGE"] >= 3 and \
497 hitted_type == world_db["ANIMAL_0"]:
498 world_db["GOD_FAVOR"] += 125
499 elif test and t == world_db["Things"][0]:
500 world_db["GOD_FAVOR"] -= 2 * test
502 if (ord("X") == world_db["MAP"][pos]
503 or ord("|") == world_db["MAP"][pos]):
505 for id in t["T_CARRIES"]:
506 type = world_db["Things"][id]["T_TYPE"]
507 if world_db["ThingTypes"][type]["TT_TOOL"] == "axe":
511 axe_name = world_db["ThingTypes"][type]["TT_NAME"]
512 if t == world_db["Things"][0]:
513 log("With your " + axe_name + ", you chop!")
514 if ord("X") == world_db["MAP"][pos]:
515 world_db["GOD_FAVOR"] -= 1
516 chop_power = world_db["ThingTypes"][type]["TT_TOOLPOWER"]
518 case_X = world_db["MAP"][pos] == ord("X")
521 0 == int(rand.next() / chop_power))
523 0 == int(rand.next() / (3 * chop_power))))):
524 if t == world_db["Things"][0]:
525 log("You chop it DOWN.")
526 if ord("X") == world_db["MAP"][pos]:
527 world_db["GOD_FAVOR"] -= 10
528 world_db["MAP"][pos] = ord(".")
529 i = 3 if case_X else 1
531 id = id_setter(-1, "Things")
532 world_db["Things"][id] = \
533 new_Thing(world_db["LUMBER"],
534 (move_result[1], move_result[2]))
537 passable = chr(world_db["MAP"][pos]) in symbols_passable
538 dir = [dir for dir in directions_db
539 if directions_db[dir] == chr(t["T_ARGUMENT"])][0]
541 t["T_POSY"] = move_result[1]
542 t["T_POSX"] = move_result[2]
543 for id in t["T_CARRIES"]:
544 world_db["Things"][id]["T_POSY"] = move_result[1]
545 world_db["Things"][id]["T_POSX"] = move_result[2]
547 if t == world_db["Things"][0]:
548 log("You MOVE " + dir + ".")
549 if (move_result[1] == world_db["altar"][0] and
550 move_result[2] == world_db["altar"][1]):
553 def command_ttid(id_string):
554 id = id_setter(id_string, "ThingTypes", command_ttid)
556 world_db["ThingTypes"][id] = {
561 "TT_START_NUMBER": 0,
568 def command_worldactive(worldactive_string):
569 val = integer_test(worldactive_string, 0, 1)
571 if 0 != world_db["WORLD_ACTIVE"]:
575 print("World already active.")
576 elif 0 == world_db["WORLD_ACTIVE"]:
578 for ThingAction in world_db["ThingActions"]:
579 if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
582 player_exists = False
583 for Thing in world_db["Things"]:
589 pos = world_db["MAP"].find(b'_')
591 y = int(pos / world_db["MAP_LENGTH"])
592 x = pos % world_db["MAP_LENGTH"]
593 world_db["altar"] = (y, x)
596 for name in world_db["specials"]:
597 if world_db[name] not in world_db["ThingTypes"]:
599 if altar_found and wait_exists and player_exists and \
600 world_db["MAP"] and specials_set:
601 for id in world_db["Things"]:
602 if world_db["Things"][id]["T_LIFEPOINTS"]:
603 build_fov_map(world_db["Things"][id])
605 update_map_memory(world_db["Things"][id], False)
606 if not world_db["Things"][0]["T_LIFEPOINTS"]:
607 empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
608 world_db["Things"][0]["fovmap"] = empty_fovmap
609 world_db["WORLD_ACTIVE"] = 1
611 print("Ignoring: Not all conditions for world activation met.")
613 def play_move(str_arg):
614 if action_exists("move"):
615 from server.config.world_data import directions_db, symbols_passable
616 t = world_db["Things"][0]
617 if not str_arg in directions_db:
618 print("Illegal move direction string.")
620 dir = ord(directions_db[str_arg])
621 from server.utils import mv_yx_in_dir_legal
622 move_result = mv_yx_in_dir_legal(chr(dir), t["T_POSY"], t["T_POSX"])
623 if 1 == move_result[0]:
624 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
625 if ord("~") == world_db["MAP"][pos]:
626 log("You can't SWIM.")
628 if (ord("X") == world_db["MAP"][pos]
629 or ord("|") == world_db["MAP"][pos]):
631 for id in t["T_CARRIES"]:
632 type = world_db["Things"][id]["T_TYPE"]
633 if world_db["ThingTypes"][type]["TT_TOOL"] == "axe":
634 world_db["Things"][0]["T_ARGUMENT"] = dir
637 if chr(world_db["MAP"][pos]) in symbols_passable:
638 world_db["Things"][0]["T_ARGUMENT"] = dir
641 log("You CAN'T move there.")
643 def play_use(str_arg):
644 if action_exists("use"):
645 t = world_db["Things"][0]
646 if 0 == len(t["T_CARRIES"]):
647 log("You have NOTHING to use in your inventory.")
649 val = integer_test(str_arg, 0, 255)
650 if None != val and val < len(t["T_CARRIES"]):
651 id = t["T_CARRIES"][val]
652 type = world_db["Things"][id]["T_TYPE"]
653 if (world_db["ThingTypes"][type]["TT_TOOL"] == "axe"
654 and t == world_db["Things"][0]):
655 log("To use this item for chopping, move towards a tree "
656 + "while carrying it in your inventory.")
658 elif (world_db["ThingTypes"][type]["TT_TOOL"] == "carpentry"):
659 pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
660 if (world_db["MAP"][pos] == ord("X")
661 or world_db["MAP"][pos] == ord("|")):
662 log("CAN'T build when standing on barrier.")
664 for id in [id for id in world_db["Things"]
665 if not world_db["Things"][id] == t
666 if not world_db["Things"][id]["carried"]
667 if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
668 if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]:
669 log("CAN'T build when standing objects.")
672 for id in t["T_CARRIES"]:
673 type_material = world_db["Things"][id]["T_TYPE"]
674 if (world_db["ThingTypes"][type_material]["TT_TOOL"]
679 log("You CAN'T use a "
680 + world_db["ThingTypes"][type]["TT_NAME"]
681 + " without some wood in your inventory.")
683 elif world_db["ThingTypes"][type]["TT_TOOL"] == "fertilizer":
684 pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
685 if not world_db["MAP"][pos] == ord("."):
686 log("Can only make soil out of NON-SOIL earth.")
688 elif world_db["ThingTypes"][type]["TT_TOOL"] == "wood":
689 log("To use wood, you NEED a carpentry tool.")
691 elif type != world_db["SLIPPERS"] and not \
692 world_db["ThingTypes"][type]["TT_TOOL"] == "food":
693 log("You CAN'T consume this thing.")
695 world_db["Things"][0]["T_ARGUMENT"] = val
698 print("Illegal inventory index.")
700 def specialtypesetter(name):
702 val = integer_test(str_int, 0)
705 if world_db["WORLD_ACTIVE"] \
706 and world_db[name] not in world_db["ThingTypes"]:
707 world_db["WORLD_ACTIVE"] = 0
708 print(name + " fits no known ThingType, deactivating world.")
711 def write_metamap_A():
712 from server.worldstate_write_helpers import write_map
714 length = world_db["MAP_LENGTH"]
715 metamapA = bytearray(b'0' * (length ** 2))
716 for id in [id for id in world_db["Things"]
717 if not world_db["Things"][id]["carried"]
718 if world_db["Things"][id]["T_LIFEPOINTS"]
719 if world_db["Things"][0]["fovmap"][
720 world_db["Things"][id]["T_POSY"] * length
721 + world_db["Things"][id]["T_POSX"]] == ord_v]:
722 pos = (world_db["Things"][id]["T_POSY"] * length
723 + world_db["Things"][id]["T_POSX"])
724 if id == 0 or world_db["EMPATHY"]:
725 type = world_db["Things"][id]["T_TYPE"]
726 max_hp = world_db["ThingTypes"][type]["TT_LIFEPOINTS"]
727 third_of_hp = max_hp / 3
728 hp = world_db["Things"][id]["T_LIFEPOINTS"]
730 if hp > 2 * third_of_hp:
732 elif hp > third_of_hp:
734 metamapA[pos] = ord('a') + add
736 metamapA[pos] = ord('X')
737 for mt in world_db["Things"][0]["T_MEMTHING"]:
738 pos = mt[1] * length + mt[2]
739 if metamapA[pos] < ord('2'):
741 return write_map(metamapA, length)
743 def write_metamap_B():
744 from server.worldstate_write_helpers import write_map
746 length = world_db["MAP_LENGTH"]
747 metamapB = bytearray(b' ' * (length ** 2))
748 for id in [id for id in world_db["Things"]
749 if not world_db["Things"][id]["carried"]
750 if world_db["Things"][id]["T_LIFEPOINTS"]
751 if world_db["Things"][0]["fovmap"][
752 world_db["Things"][id]["T_POSY"] * length
753 + world_db["Things"][id]["T_POSX"]] == ord_v]:
754 pos = (world_db["Things"][id]["T_POSY"] * length
755 + world_db["Things"][id]["T_POSX"])
756 if id == 0 or world_db["EMPATHY"]:
757 action = world_db["Things"][id]["T_COMMAND"]
759 name = world_db["ThingActions"][action]["TA_NAME"]
762 metamapB[pos] = ord(name[0])
763 return write_map(metamapB, length)
765 def calc_effort(thing_action, thing):
766 from math import sqrt
767 effort = thing_action["TA_EFFORT"]
768 if thing_action["TA_NAME"] == "move":
769 typ = thing["T_TYPE"]
770 max_hp = (world_db["ThingTypes"][typ]["TT_LIFEPOINTS"])
771 effort = int(effort / sqrt(max_hp))
772 effort = 1 if effort == 0 else effort
776 """Try "pickup" as player's T_COMMAND"."""
777 if action_exists("pickup"):
778 t = world_db["Things"][0]
779 ids = [id for id in world_db["Things"] if id
780 if not world_db["Things"][id]["carried"]
781 if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
782 if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]
784 log("NOTHING to pick up.")
785 elif len(t["T_CARRIES"]) >= world_db["ThingTypes"][t["T_TYPE"]] \
787 log("CAN'T pick up: No storage room to carry anything more.")
789 set_command("pickup")
791 strong_write(io_db["file_out"], "PLUGIN PleaseTheIslandGod\n")
793 if not "GOD_FAVOR" in world_db:
794 world_db["GOD_FAVOR"] = 0
795 if not "FAVOR_STAGE" in world_db:
796 world_db["FAVOR_STAGE"] = 0
797 if not "SLIPPERS" in world_db:
798 world_db["SLIPPERS"] = 0
799 if not "PLANT_0" in world_db:
800 world_db["PLANT_0"] = 0
801 if not "PLANT_1" in world_db:
802 world_db["PLANT_1"] = 0
803 if not "ANIMAL_0" in world_db:
804 world_db["ANIMAL_0"] = 0
805 if not "ANIMAL_1" in world_db:
806 world_db["ANIMAL_1"] = 0
807 if not "TOOL_0" in world_db:
808 world_db["TOOL_0"] = 0
809 if not "TOOL_1" in world_db:
810 world_db["TOOL_1"] = 0
811 if not "LUMBER" in world_db:
812 world_db["LUMBER"] = 0
813 if not "EMPATHY" in world_db:
814 world_db["EMPATHY"] = 0
815 world_db["terrain_names"][":"] = "SOIL"
816 world_db["terrain_names"]["|"] = "WALL"
817 world_db["terrain_names"]["_"] = "ALTAR"
818 world_db["specials"] = ["SLIPPERS", "PLANT_0", "PLANT_1", "TOOL_0", "TOOL_1",
819 "LUMBER", "ANIMAL_0, ANIMAL_1"]
820 io_db["worldstate_write_order"] += [["GOD_FAVOR", "world_int"]]
821 io_db["worldstate_write_order"] += [[write_metamap_A, "func"]]
822 io_db["worldstate_write_order"] += [[write_metamap_B, "func"]]
824 import server.config.world_data
825 server.config.world_data.symbols_passable += ":_"
827 from server.config.world_data import thing_defaults
828 thing_defaults["T_PLAYERDROP"] = 0
830 import server.config.actions
831 server.config.actions.action_db["actor_move"] = actor_move
832 server.config.actions.action_db["actor_pickup"] = actor_pickup
833 server.config.actions.action_db["actor_drop"] = actor_drop
834 server.config.actions.action_db["actor_use"] = actor_use
835 server.config.actions.ai_func = ai
837 from server.config.commands import commands_db
838 commands_db["TT_ID"] = (1, False, command_ttid)
839 commands_db["GOD_FAVOR"] = (1, False, setter(None, "GOD_FAVOR", -32768, 32767))
840 commands_db["TT_STORAGE"] = (1, False, setter("ThingType", "TT_STORAGE", 0, 255))
841 commands_db["T_PLAYERDROP"] = (1, False, setter("Thing", "T_PLAYERDROP", 0, 1))
842 commands_db["WORLD_ACTIVE"] = (1, False, command_worldactive)
843 commands_db["FAVOR_STAGE"] = (1, False, setter(None, "FAVOR_STAGE", 0, 1))
844 commands_db["SLIPPERS"] = (1, False, specialtypesetter("SLIPPERS"))
845 commands_db["TOOL_0"] = (1, False, specialtypesetter("TOOL_0"))
846 commands_db["TOOL_1"] = (1, False, specialtypesetter("TOOL_1"))
847 commands_db["ANIMAL_0"] = (1, False, specialtypesetter("ANIMAL_0"))
848 commands_db["ANIMAL_1"] = (1, False, specialtypesetter("ANIMAL_1"))
849 commands_db["PLANT_0"] = (1, False, specialtypesetter("PLANT_0"))
850 commands_db["PLANT_1"] = (1, False, specialtypesetter("PLANT_1"))
851 commands_db["LUMBER"] = (1, False, specialtypesetter("LUMBER"))
852 commands_db["EMPATHY"] = (1, False, setter(None, "EMPATHY", 0, 1))
853 commands_db["use"] = (1, False, play_use)
854 commands_db["move"] = (1, False, play_move)
855 commands_db["pickup"] = (0, False, play_pickup)
857 import server.config.misc
858 server.config.misc.make_map_func = make_map
859 server.config.misc.thingproliferation_func = thingproliferation
860 server.config.misc.make_world = make_world
861 server.config.misc.decrement_lifepoints_func = decrement_lifepoints
862 server.config.misc.calc_effort_func = calc_effort