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, directions_db
8 from server.utils import mv_yx_in_dir_legal, rand, id_setter
9 from server.config.io import io_db
11 def pos_test(type, y, x):
12 pos = y * world_db["MAP_LENGTH"] + x;
13 plant = world_db["ThingTypes"][type]["TT_PROLIFERATE"]
14 return (not plant or ":" == chr(world_db["MAP"][pos]))
17 from server.world_makable import world_makable
18 playertype = world_makable()
19 for name in world_db["specials"]:
20 if world_db[name] not in world_db["ThingTypes"]:
21 print("Ignoring: No valid " + name + " set.")
26 from server.make_map import make_map, is_neighbor, new_pos
29 length = world_db["MAP_LENGTH"]
30 n_colons = int((length ** 2) / 16)
32 while (i_colons <= n_colons):
33 single_allowed = rand.next() % 256
35 if ("." == chr(world_db["MAP"][pos])
36 and ((not single_allowed) or is_neighbor((y, x), ":"))):
37 world_db["MAP"][pos] = ord(":")
40 while not altar_placed:
42 if (("." == chr(world_db["MAP"][pos]
43 or ":" == chr(world_db["MAP"][pos]))
44 and not is_neighbor((y, x), "X"))):
45 world_db["MAP"][pos] = ord("_")
46 world_db["altar"] = (y, x)
49 def thingprol_field_spreadable(c, t):
50 return ":" == c or (world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]
53 def thingprol_test(t):
54 tt = world_db["ThingTypes"][t["T_TYPE"]]
55 return (tt["TT_LIFEPOINTS"] == 0 or \
56 t["T_LIFEPOINTS"] >= 0.9 * tt["TT_LIFEPOINTS"])
58 def thingprol_post_create(t):
59 tt = world_db["ThingTypes"][t["T_TYPE"]]
60 if (world_db["FAVOR_STAGE"] > 0 and t["T_TYPE"] == world_db["PLANT_0"]):
61 world_db["GOD_FAVOR"] += 5
62 elif t["T_TYPE"] == world_db["PLANT_1"]:
63 world_db["GOD_FAVOR"] += 25
64 elif world_db["FAVOR_STAGE"] >= 4 and t["T_TYPE"] == world_db["ANIMAL_1"]:
65 log("The Island God SMILES upon a new-born bear baby.")
66 world_db["GOD_FAVOR"] += 750
68 def ai_hook_pickup_test(t):
69 return len(t["T_CARRIES"]) < \
70 world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]
73 used_slots = len(t["T_CARRIES"])
74 if used_slots < world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]:
75 from server.actions import actor_pickup
76 t_picked = actor_pickup(t)
78 ty = world_db["ThingTypes"][t_picked["T_TYPE"]]
79 if t != world_db["Things"][0] and t_picked["T_PLAYERDROP"] \
80 and ty["TT_TOOL"] == "food":
81 score = int(ty["TT_TOOLPOWER"] / 32)
82 world_db["GOD_FAVOR"] += score
83 t_picked["T_PLAYERDROP"] = 0
84 elif t == world_db["Things"][0]:
85 log("CAN'T pick up object: No storage room to carry more.")
87 def actor_pickup_test_hook(t, tid):
88 tt = world_db["ThingTypes"][tid]
89 return not (t != world_db["Things"][0] and (tt["TT_TOOL"] != "food" or
90 (tt["TT_TOOLPOWER"] <=
91 world_db["ThingTypes"][t["T_TYPE"]]["eat_vs_hunger_threshold"])))
94 from server.actions import actor_drop
95 dropped = actor_drop(t)
97 dropped["T_PLAYERDROP"] = 1
99 def actor_use_attempts_hook(t, ty):
100 if ty == world_db["SLIPPERS"]:
101 if t == world_db["Things"][0]:
102 log("You use the " + world_db["ThingTypes"][ty]["TT_NAME"] + ". " \
103 "It glows in wondrous colors, and emits a sound as if from a d"
104 "ying cat. The Island God laughs.")
105 t["T_LIFEPOINTS"] = 1
106 from server.config.misc import decrement_lifepoints
107 decrement_lifepoints(t)
108 elif (world_db["ThingTypes"][ty]["TT_TOOL"] == "carpentry"):
109 pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
110 if (world_db["MAP"][pos] == ord("X")
111 or world_db["MAP"][pos] == ord("|")):
113 for t_id in [t_id for t_id in world_db["Things"]
114 if not world_db["Things"][t_id] == t
115 if not world_db["Things"][t_id]["carried"]
116 if world_db["Things"][t_id]["T_POSY"] == t["T_POSY"]
117 if world_db["Things"][t_id]["T_POSX"] == t["T_POSX"]]:
120 for t_id in t["T_CARRIES"]:
121 type_material = world_db["Things"][t_id]["T_TYPE"]
122 if (world_db["ThingTypes"][type_material]["TT_TOOL"] == "wood"):
126 t["T_CARRIES"].remove(wood_id)
127 del world_db["Things"][wood_id]
128 world_db["MAP"][pos] = ord("|")
129 log("With your " + world_db["ThingTypes"][ty]["TT_NAME"] + " you" \
130 " build a WOODEN BARRIER from your "
131 + world_db["ThingTypes"][type_material]["TT_NAME"] + ".")
132 elif world_db["ThingTypes"][ty]["TT_TOOL"] == "fertilizer":
133 pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
134 if world_db["MAP"][pos] == ord("."):
135 log("You create SOIL.")
136 world_db["MAP"][pos] = ord(":")
140 def altar_msg_wait(limit):
141 log("The Island God will talk again when it favors you to >=" +
142 str(limit) + " points.")
144 altar_msg_0 = "The Island God speaks to you: \"I don't trust you. You in" \
145 "trude on the island's affairs. I think you're a nuisance at best, and a" \
146 " danger to my children at worst. I will give you a chance to lighten my" \
147 " mood, however: For a while now, I've been trying to spread the plant " \
148 + world_db["ThingTypes"][world_db["PLANT_0"]]["TT_NAME"] + " (\"" + \
149 world_db["ThingTypes"][world_db["PLANT_0"]]["TT_SYMBOL"] + "\"). I have " \
150 "not been very successful so far. Maybe you can make yourself useful the" \
151 "re. I will count each further " + \
152 world_db["ThingTypes"][world_db["PLANT_0"]]["TT_NAME"] + " that grows to" \
155 altar_msg_1 = "The Island God speaks to you: \"You could have done worse" \
156 " so far. Maybe you are not the worst to happen to this island since the" \
157 " metal birds threw the great lightning ball. Maybe you can help me spre" \
158 "ad another plant. It multiplies faster,and it is highly nutritious: " + \
159 world_db["ThingTypes"][world_db["PLANT_1"]]["TT_NAME"] + " (\"" + \
160 world_db["ThingTypes"][world_db["PLANT_1"]]["TT_SYMBOL"] + "\"). It is n" \
161 "ew. I give you the only example. Be very careful with it! I also give y" \
162 "ou another tool that may be helpful.\""
164 altar_msg_2 = "The Island God speaks to you: \"I am greatly disappointed" \
165 " that you lost all " + \
166 world_db["ThingTypes"][world_db["PLANT_1"]]["TT_NAME"] + " this island h" \
167 "ad. Here is another one. It cost me great work. Be more careful this ti" \
168 "me when planting it.\""
170 altar_msg_3 = "The Island God speaks to you: \"The " + \
171 world_db["ThingTypes"][world_db["ANIMAL_0"]]["TT_NAME"] + " has lately b" \
172 "ecome a pest. These creatures do not please me as much as they used to " \
173 "do. Exterminate them all. I will count each kill to your favor. To help" \
174 " you with the hunting, I grant you the empathy and knowledge to read an" \
177 altar_msg_4 = "You will now see animals' health bars, and activities (\"" \
178 "m\": moving (maybe for an attack), \"u\": eating, \"p\": picking someth" \
179 "ing up; no letter: waiting)."
181 altar_msg_5 = "The Island God speaks to you: \"You know what animal I fi" \
182 "nd the cutest? The " + \
183 world_db["ThingTypes"][world_db["ANIMAL_1"]]["TT_NAME"] + "! I think wha" \
184 "t this islands clearly needs more of is " + \
185 world_db["ThingTypes"][world_db["ANIMAL_1"]]["TT_NAME"] + "s. Why don't " \
186 "you help? Support them. Make sure they are well, and they will multiply" \
187 " faster. From now on, I will count each new-born " + \
188 world_db["ThingTypes"][world_db["ANIMAL_1"]]["TT_NAME"] + \
189 " (not spawned by me due to undo an extinction event) greatly to your fa" \
190 "vor. To help you with the feeding, here is something to make the ground" \
191 " bear more consumables."
193 altar_msg_6 = "The Island God speaks to you: \"You have proven yourself " \
194 "worthy of my respect. You were a good citizen to the island, and someti" \
195 "mes a better steward to its inhabitants than me. The island shall miss " \
196 "you when you leave. But you have earned the right to do so. Take this " \
197 + world_db["ThingTypes"][world_db["SLIPPERS"]]["TT_NAME"] + " and USE i" \
198 "t when you please. It will take you to where you came from. (But do fe" \
199 "el free to stay here as long as you like.)\""
202 from server.new_thing import new_Thing
203 if world_db["FAVOR_STAGE"] > 9000:
204 log("You step on a soul-less slab of stone.")
206 log("YOU ENTER SACRED GROUND.")
207 if world_db["FAVOR_STAGE"] == 0:
208 world_db["FAVOR_STAGE"] = 1
210 elif world_db["FAVOR_STAGE"] == 1 and world_db["GOD_FAVOR"] < 100:
212 elif world_db["FAVOR_STAGE"] == 1 and world_db["GOD_FAVOR"] >= 100:
213 world_db["FAVOR_STAGE"] = 2
215 id = id_setter(-1, "Things")
216 world_db["Things"][id] = new_Thing(world_db["PLANT_1"],
218 id = id_setter(-1, "Things")
219 world_db["Things"][id] = new_Thing(world_db["TOOL_0"],
221 elif world_db["FAVOR_STAGE"] == 2 and \
222 0 == len([id for id in world_db["Things"]
223 if world_db["Things"][id]["T_TYPE"]
224 == world_db["PLANT_1"]]):
226 id = id_setter(-1, "Things")
227 world_db["Things"][id] = new_Thing(world_db["PLANT_1"],
229 world_db["GOD_FAVOR"] -= 250
230 elif world_db["FAVOR_STAGE"] == 2 and world_db["GOD_FAVOR"] < 500:
232 elif world_db["FAVOR_STAGE"] == 2 and world_db["GOD_FAVOR"] >= 500:
233 world_db["FAVOR_STAGE"] = 3
236 world_db["EMPATHY"] = 1
237 elif world_db["FAVOR_STAGE"] == 3 and world_db["GOD_FAVOR"] < 5000:
239 elif world_db["FAVOR_STAGE"] == 3 and world_db["GOD_FAVOR"] >= 5000:
240 world_db["FAVOR_STAGE"] = 4
242 id = id_setter(-1, "Things")
243 world_db["Things"][id] = new_Thing(world_db["TOOL_1"],
245 elif world_db["GOD_FAVOR"] < 20000:
246 altar_msg_wait(20000)
247 elif world_db["GOD_FAVOR"] > 20000:
248 world_db["FAVOR_STAGE"] = 9001
250 id = id_setter(-1, "Things")
251 world_db["Things"][id] = new_Thing(world_db["SLIPPERS"],
254 from server.actions import actor_move
259 if world_db["FAVOR_STAGE"] >= 3 and \
260 test[1] == world_db["ANIMAL_0"]:
261 world_db["GOD_FAVOR"] += 125
262 elif t == world_db["Things"][0]:
263 world_db["GOD_FAVOR"] -= 2 * test[1]
264 elif t == world_db["Things"][0]:
265 world_db["GOD_FAVOR"] -= 1
267 if (t == world_db["Things"][0] and
268 test[1] == world_db["altar"][0] and
269 test[2] == world_db["altar"][1]):
272 def actor_move_attempts_hook(t, move_result, pos):
273 if (ord("X") == world_db["MAP"][pos] or ord("|") == world_db["MAP"][pos]):
274 for tid in t["T_CARRIES"]:
275 ty = world_db["Things"][tid]["T_TYPE"]
276 if world_db["ThingTypes"][ty]["TT_TOOL"] == "axe":
277 axe_name = world_db["ThingTypes"][ty]["TT_NAME"]
278 if t == world_db["Things"][0]:
279 log("With your " + axe_name + ", you chop!")
280 if ord("X") == world_db["MAP"][pos]:
281 world_db["GOD_FAVOR"] -= 1
282 chop_power = world_db["ThingTypes"][ty]["TT_TOOLPOWER"]
283 case_X = world_db["MAP"][pos] == ord("X")
284 if (chop_power > 0 and
285 ((case_X and 0 == int(rand.next() / chop_power))
287 0 == int(rand.next() / (3 * chop_power))))):
288 if t == world_db["Things"][0]:
289 log("You chop it DOWN.")
290 if ord("X") == world_db["MAP"][pos]:
291 world_db["GOD_FAVOR"] -= 10
292 world_db["MAP"][pos] = ord(".")
293 i = 3 if case_X else 1
294 from server.new_thing import new_Thing
296 tid = id_setter(-1, "Things")
297 world_db["Things"][tid] = new_Thing(world_db["LUMBER"],
298 (move_result[1], move_result[2]))
303 def decrement_lifepoints(t):
304 from server.decrement_lifepoints import decrement_lifepoints
305 live_tid = t["T_TYPE"]
306 test = decrement_lifepoints(t)
307 if test > 0 and t != world_db["Things"][0]:
308 n_species = len([tid for tid in world_db["Things"]
309 if world_db["Things"][tid]["T_TYPE"] == live_tid])
311 from server.new_thing import new_Thing
312 if world_db["FAVOR_STAGE"] >= 3 and \
313 live_tid == world_db["ANIMAL_0"]:
314 world_db["GOD_FAVOR"] += 3000
315 log("CONGRATULATIONS! The "
316 + world_db["ThingTypes"][live_tid]["TT_NAME"]
317 + " species has died out. The Island God is pleased.")
319 tid = id_setter(-1, "Things")
320 world_db["Things"][tid] = new_Thing(live_tid,
322 log("The " + world_db["ThingTypes"][live_tid]["TT_NAME"] + " s"
323 "pecies has temporarily died out. One new-born is spawned "
327 def command_worldactive_test_hook():
328 pos = world_db["MAP"].find(b'_')
330 y = int(pos / world_db["MAP_LENGTH"])
331 x = pos % world_db["MAP_LENGTH"]
332 world_db["altar"] = (y, x)
334 print("Ignored: No altar defined for world to activate.")
336 for name in world_db["specials"]:
337 if world_db[name] not in world_db["ThingTypes"]:
338 print("Ignored: Not all specials set for world to activate.")
342 def play_move_attempt_hook(t, direction, pos):
343 if (ord("X") == world_db["MAP"][pos] or ord("|") == world_db["MAP"][pos]):
345 for tid in t["T_CARRIES"]:
346 ty = world_db["Things"][tid]["T_TYPE"]
347 if world_db["ThingTypes"][ty]["TT_TOOL"] == "axe":
348 world_db["Things"][0]["T_ARGUMENT"] = direction
353 def play_use_attempt_hook(t, tt):
354 pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
355 if tt["TT_TOOL"] == "axe":
356 log("To use this item for chopping, MOVE towards a tree while carrying"
357 " it in your inventory.")
359 elif tt["TT_TOOL"] == "carpentry":
360 if (world_db["MAP"][pos] == ord("X")
361 or world_db["MAP"][pos] == ord("|")):
362 log("CAN'T build when standing on barrier.")
364 for tid in [tid for tid in world_db["Things"]
365 if not world_db["Things"][tid] == t
366 if not world_db["Things"][tid]["carried"]
367 if world_db["Things"][tid]["T_POSY"] == t["T_POSY"]
368 if world_db["Things"][tid]["T_POSX"] == t["T_POSX"]]:
369 log("CAN'T build when standing objects.")
372 for tid in t["T_CARRIES"]:
373 type_material = world_db["Things"][tid]["T_TYPE"]
374 if world_db["ThingTypes"][type_material]["TT_TOOL"] == "wood":
378 log("You CAN'T use a " + tt["TT_NAME"]
379 + " without some wood in your inventory.")
382 elif tt["TT_TOOL"] == "fertilizer":
383 if not world_db["MAP"][pos] == ord("."):
384 log("Can only make soil out of NON-SOIL earth.")
387 elif tt["TT_TOOL"] == "wood":
388 log("To use wood, you NEED a carpentry tool.")
390 elif tt == world_db["ThingTypes"][world_db["SLIPPERS"]]:
393 def play_pickup_attempt_hook(t):
394 if len(t["T_CARRIES"]) >= world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]:
395 log("CAN'T pick up: No storage room to carry anything more.")
399 def specialtypesetter(name):
401 val = integer_test(str_int, 0)
404 if world_db["WORLD_ACTIVE"] \
405 and world_db[name] not in world_db["ThingTypes"]:
406 world_db["WORLD_ACTIVE"] = 0
407 print(name + " fits no known ThingType, deactivating world.")
410 def write_metamap_A():
411 from server.worldstate_write_helpers import write_map
413 length = world_db["MAP_LENGTH"]
414 metamapA = bytearray(b'0' * (length ** 2))
415 for tid in [tid for tid in world_db["Things"]
416 if not world_db["Things"][tid]["carried"]
417 if world_db["Things"][tid]["T_LIFEPOINTS"]
418 if world_db["Things"][0]["fovmap"][
419 world_db["Things"][tid]["pos"]] == ord_v]:
420 pos = (world_db["Things"][tid]["pos"])
421 if tid == 0 or world_db["EMPATHY"]:
422 ttid = world_db["Things"][tid]["T_TYPE"]
423 max_hp = world_db["ThingTypes"][ttid]["TT_LIFEPOINTS"]
424 third_of_hp = max_hp / 3
425 hp = world_db["Things"][tid]["T_LIFEPOINTS"]
427 if hp > 2 * third_of_hp:
429 elif hp > third_of_hp:
431 metamapA[pos] = ord('a') + add
433 metamapA[pos] = ord('X')
434 for mt in world_db["Things"][0]["T_MEMTHING"]:
435 pos = mt[1] * length + mt[2]
436 if metamapA[pos] < ord('2'):
438 return write_map(metamapA, length)
440 def write_metamap_B():
441 from server.worldstate_write_helpers import write_map
443 length = world_db["MAP_LENGTH"]
444 metamapB = bytearray(b' ' * (length ** 2))
445 for tid in [tid for tid in world_db["Things"]
446 if not world_db["Things"][tid]["carried"]
447 if world_db["Things"][tid]["T_LIFEPOINTS"]
448 if world_db["Things"][0]["fovmap"][
449 world_db["Things"][tid]["pos"]] == ord_v]:
450 pos = (world_db["Things"][tid]["pos"])
451 if tid == 0 or world_db["EMPATHY"]:
452 action = world_db["Things"][tid]["T_COMMAND"]
454 name = world_db["ThingActions"][action]["TA_NAME"]
457 metamapB[pos] = ord(name[0])
458 return write_map(metamapB, length)
460 def calc_effort(thing_action, thing):
461 from math import sqrt
462 effort = thing_action["TA_EFFORT"]
463 if thing_action["TA_NAME"] == "move":
464 typ = thing["T_TYPE"]
465 max_hp = (world_db["ThingTypes"][typ]["TT_LIFEPOINTS"])
466 effort = int(effort / sqrt(max_hp))
467 effort = 1 if effort == 0 else effort
470 strong_write(io_db["file_out"], "PLUGIN PleaseTheIslandGod\n")
472 def set_zero_if_undefined(key):
473 if not key in world_db:
476 set_zero_if_undefined("GOD_FAVOR")
477 set_zero_if_undefined("FAVOR_STAGE")
478 set_zero_if_undefined("EMPATHY")
479 world_db["specials"] = ["SLIPPERS", "PLANT_0", "PLANT_1", "TOOL_0", "TOOL_1",
480 "LUMBER", "ANIMAL_0", "ANIMAL_1"]
481 for key in world_db["specials"]:
482 set_zero_if_undefined(key)
484 world_db["terrain_names"][":"] = "SOIL"
485 world_db["terrain_names"]["|"] = "WALL"
486 world_db["terrain_names"]["_"] = "ALTAR"
487 io_db["worldstate_write_order"] += [["GOD_FAVOR", "world_int"]]
488 io_db["worldstate_write_order"] += [[write_metamap_A, "func"]]
489 io_db["worldstate_write_order"] += [[write_metamap_B, "func"]]
491 import server.config.world_data
492 server.config.world_data.symbols_passable += ":_"
493 server.config.world_data.symbols_hide += "|"
494 server.config.world_data.thingprol_field_spreadable = thingprol_field_spreadable
495 server.config.world_data.thingprol_test_hook = thingprol_test
496 server.config.world_data.thingprol_post_create_hook = thingprol_post_create
498 from server.config.world_data import thing_defaults, thingtype_defaults
499 thing_defaults["T_PLAYERDROP"] = 0
500 thingtype_defaults["TT_STORAGE"] = 0
502 import server.config.actions
503 server.config.actions.action_db["actor_move"] = actor_move
504 server.config.actions.action_db["actor_pickup"] = actor_pickup
505 server.config.actions.action_db["actor_drop"] = actor_drop
506 server.config.actions.actor_pickup_test_hook = actor_pickup_test_hook
507 server.config.actions.actor_use_attempts_hook = actor_use_attempts_hook
508 server.config.actions.actor_move_attempts_hook = actor_move_attempts_hook
510 from server.config.commands import commands_db
511 commands_db["GOD_FAVOR"] = (1, False, setter(None, "GOD_FAVOR", -32768, 32767))
512 commands_db["TT_STORAGE"] = (1, False, setter("ThingType", "TT_STORAGE", 0, 255))
513 commands_db["T_PLAYERDROP"] = (1, False, setter("Thing", "T_PLAYERDROP", 0, 1))
514 commands_db["FAVOR_STAGE"] = (1, False, setter(None, "FAVOR_STAGE", 0, 255))
515 commands_db["SLIPPERS"] = (1, False, specialtypesetter("SLIPPERS"))
516 commands_db["TOOL_0"] = (1, False, specialtypesetter("TOOL_0"))
517 commands_db["TOOL_1"] = (1, False, specialtypesetter("TOOL_1"))
518 commands_db["ANIMAL_0"] = (1, False, specialtypesetter("ANIMAL_0"))
519 commands_db["ANIMAL_1"] = (1, False, specialtypesetter("ANIMAL_1"))
520 commands_db["PLANT_0"] = (1, False, specialtypesetter("PLANT_0"))
521 commands_db["PLANT_1"] = (1, False, specialtypesetter("PLANT_1"))
522 commands_db["LUMBER"] = (1, False, specialtypesetter("LUMBER"))
523 commands_db["EMPATHY"] = (1, False, setter(None, "EMPATHY", 0, 1))
524 import server.config.commands
525 server.config.commands.command_worldactive_test_hook = \
526 command_worldactive_test_hook
527 server.config.commands.play_move_attempt_hook = play_move_attempt_hook
528 server.config.commands.play_use_attempt_hook = play_use_attempt_hook
529 server.config.commands.play_pickup_attempt_hook = play_pickup_attempt_hook
531 import server.config.misc
532 server.config.misc.make_map = make_map
533 server.config.misc.decrement_lifepoints = decrement_lifepoints
534 server.config.misc.calc_effort = calc_effort
536 import server.config.make_world_helpers
537 server.config.make_world_helpers.pos_test = pos_test
538 server.config.make_world_helpers.world_makable = world_makable
539 server.config.make_world_helpers.make_map = make_map
541 import server.config.ai
542 server.config.ai.ai_hook_pickup_test = ai_hook_pickup_test