home · contact · privacy
d28e235458ea0439a321f51745239f72117d35e5
[plomrogue] / plugins / server / PleaseTheIslandGod.py
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.
4
5
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
10
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]))
15
16 def world_makable():
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.")
22             return -1
23     return playertype
24
25 def make_map():
26     from server.make_map import make_map, is_neighbor, new_pos
27     global rand
28     make_map()
29     length = world_db["MAP_LENGTH"]
30     n_colons = int((length ** 2) / 16)
31     i_colons = 0
32     while (i_colons <= n_colons):
33         single_allowed = rand.next() % 256
34         y, x, pos = new_pos()
35         if ("." == chr(world_db["MAP"][pos])
36           and ((not single_allowed) or is_neighbor((y, x), ":"))):
37             world_db["MAP"][pos] = ord(":")
38             i_colons += 1
39     altar_placed = False
40     while not altar_placed:
41         y, x, pos = new_pos()
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)
47             altar_placed = True
48
49 def field_spreadable(c, t):
50     return ":" == c or (world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]
51                         and "." == c)
52
53 def thingprol_plugin_conditions(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"])
57
58 def thingprol_plugin_post_create_hook(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
67
68 def ai_hook_pickup_test(t):
69     return len(t["T_CARRIES"]) < \
70         world_db["ThingTypes"][t["T_TYPE"]]["TT_STORAGE"]
71
72 def actor_pickup(t):
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)
77         if t_picked != None:
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.")
86
87 def actor_pickup_test_hook(t, tid):
88     from server.ai import eat_vs_hunger_threshold
89     tt = world_db["ThingTypes"][tid]
90     return not (t != world_db["Things"][0] and (tt["TT_TOOL"] != "food" or
91             (tt["TT_TOOLPOWER"] <= eat_vs_hunger_threshold(t["T_TYPE"]))))
92
93 def actor_drop(t):
94     """Make t rop Thing from inventory to ground indexed by T_ARGUMENT."""
95     if len(t["T_CARRIES"]):
96         id = t["T_CARRIES"][t["T_ARGUMENT"]]
97         t["T_CARRIES"].remove(id)
98         world_db["Things"][id]["carried"] = False
99         if t == world_db["Things"][0]:
100             log("You DROP an object.")
101             world_db["Things"][id]["T_PLAYERDROP"] = 1
102
103 def actor_use(t):
104     if len(t["T_CARRIES"]):
105         id = t["T_CARRIES"][t["T_ARGUMENT"]]
106         type = world_db["Things"][id]["T_TYPE"]
107         if type == world_db["SLIPPERS"]:
108             if t == world_db["Things"][0]:
109                 log("You use the " + world_db["ThingTypes"][type]["TT_NAME"] +
110                     ". It glows in wondrous colors, and emits a sound as if fr"
111                     "om a dying cat. The Island God laughs.\n")
112             t["T_LIFEPOINTS"] = 1
113             from server.config.misc import decrement_lifepoints_func
114             decrement_lifepoints_func(t)
115         elif (world_db["ThingTypes"][type]["TT_TOOL"] == "carpentry"):
116             pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
117             if (world_db["MAP"][pos] == ord("X")
118                 or world_db["MAP"][pos] == ord("|")):
119                 return
120             for id in [id for id in world_db["Things"]
121                        if not world_db["Things"][id] == t
122                        if not world_db["Things"][id]["carried"]
123                        if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
124                        if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]:
125                  return
126             wood_id = None
127             for id in t["T_CARRIES"]:
128                 type_material = world_db["Things"][id]["T_TYPE"]
129                 if (world_db["ThingTypes"][type_material]["TT_TOOL"]
130                     == "wood"):
131                     wood_id = id
132                     break
133             if wood_id != None:
134                 t["T_CARRIES"].remove(wood_id)
135                 del world_db["Things"][wood_id]
136                 world_db["MAP"][pos] = ord("|")
137                 log("With your " + world_db["ThingTypes"][type]["TT_NAME"]
138                     + " you build a WOODEN BARRIER from your "
139                     + world_db["ThingTypes"][type_material]["TT_NAME"] + ".")
140         elif world_db["ThingTypes"][type]["TT_TOOL"] == "fertilizer":
141             pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
142             if world_db["MAP"][pos] == ord("."):
143                 log("You create SOIL.")
144                 world_db["MAP"][pos] = ord(":")
145         elif world_db["ThingTypes"][type]["TT_TOOL"] == "food":
146             t["T_CARRIES"].remove(id)
147             del world_db["Things"][id]
148             t["T_SATIATION"] += world_db["ThingTypes"][type]["TT_TOOLPOWER"]
149             if t == world_db["Things"][0]:
150                 log("You CONSUME this thing.")
151         elif t == world_db["Things"][0]:
152             log("You try to use this object, but FAIL.")
153
154 def decrement_lifepoints(t):
155     t["T_LIFEPOINTS"] -= 1
156     live_type = t["T_TYPE"]
157     _id = [_id for _id in world_db["Things"] if world_db["Things"][_id] == t][0]
158     if 0 == t["T_LIFEPOINTS"]:
159         for id in t["T_CARRIES"]:
160             t["T_CARRIES"].remove(id)
161             world_db["Things"][id]["T_POSY"] = t["T_POSY"]
162             world_db["Things"][id]["T_POSX"] = t["T_POSX"]
163             world_db["Things"][id]["carried"] = False
164         t["T_TYPE"] = world_db["ThingTypes"][t["T_TYPE"]]["TT_CORPSE_ID"]
165         if world_db["Things"][0] == t:
166             t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
167             log("You die.")
168             log("See README on how to start over.")
169         else:
170             t["fovmap"] = False
171             t["T_MEMMAP"] = False
172             t["T_MEMDEPTHMAP"] = False
173             t["T_MEMTHING"] = []
174             n_species = len([id for id in world_db["Things"]
175                              if world_db["Things"][id]["T_TYPE"] == live_type])
176             if 0 == n_species:
177                 from server.new_thing import new_Thing
178                 if world_db["FAVOR_STAGE"] >= 3 and \
179                     live_type == world_db["ANIMAL_0"]:
180                     world_db["GOD_FAVOR"] += 3000
181                     log("CONGRATULATIONS! The "
182                         + world_db["ThingTypes"][live_type]["TT_NAME"]
183                         + " species has died out. The Island God is pleased.")
184                 else:
185                     id = id_setter(-1, "Things")
186                     world_db["Things"][id] = new_Thing(live_type,
187                                                        world_db["altar"])
188                     log("The "
189                         + world_db["ThingTypes"][live_type]["TT_NAME"]
190                         + " species has temporarily died out. "
191                         + "One new-born is spawned at the altar.")
192         return world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]
193     return 0
194
195 def actor_move(t):
196
197     def altar_msg_wait(limit):
198             log("The Island God will talk again when it favors you to >=" +
199                 str(limit) + " points.")
200
201     altar_msg_0 = "The Island God speaks to you: \"I don't trust you. You in" \
202     "trude on the island's affairs. I think you're a nuisance at best, and a" \
203     " danger to my children at worst. I will give you a chance to lighten my" \
204     " mood, however: For a while now, I've been trying to spread the plant " \
205     + world_db["ThingTypes"][world_db["PLANT_0"]]["TT_NAME"] + " (\"" + \
206     world_db["ThingTypes"][world_db["PLANT_0"]]["TT_SYMBOL"] + "\"). I have " \
207     "not been very successful so far. Maybe you can make yourself useful the" \
208     "re. I will count each further " + \
209     world_db["ThingTypes"][world_db["PLANT_0"]]["TT_NAME"] + " that grows to" \
210     " your favor.\""
211
212     altar_msg_1 = "The Island God speaks to you: \"You could have done worse" \
213     " so far. Maybe you are not the worst to happen to this island since the" \
214     " metal birds threw the great lightning ball. Maybe you can help me spre" \
215     "ad another plant. It multiplies faster,and it is highly nutritious: " + \
216     world_db["ThingTypes"][world_db["PLANT_1"]]["TT_NAME"] + " (\"" + \
217     world_db["ThingTypes"][world_db["PLANT_1"]]["TT_SYMBOL"] + "\"). It is n" \
218     "ew. I give you the only example. Be very careful with it! I also give y" \
219     "ou another tool that may be helpful.\""
220
221     altar_msg_2 = "The Island God speaks to you: \"I am greatly disappointed" \
222     " that you lost all " + \
223     world_db["ThingTypes"][world_db["PLANT_1"]]["TT_NAME"] + " this island h" \
224     "ad. Here is another one. It cost me great work. Be more careful this ti" \
225     "me when planting it.\""
226
227     altar_msg_3 = "The Island God speaks to you: \"The " + \
228     world_db["ThingTypes"][world_db["ANIMAL_0"]]["TT_NAME"] + " has lately b" \
229     "ecome a pest. These creatures do not please me as much as they used to " \
230     "do. Exterminate them all. I will count each kill to your favor. To help" \
231     " you with the hunting, I grant you the empathy and knowledge to read an" \
232     "imals.\""
233
234     altar_msg_4 = "You will now see animals' health bars, and activities (\"" \
235     "m\": moving (maybe for an attack), \"u\": eating, \"p\": picking someth" \
236     "ing up; no letter: waiting)."
237
238     altar_msg_5 = "The Island God speaks to you: \"You know what animal I fi" \
239     "nd the cutest? The " + \
240     world_db["ThingTypes"][world_db["ANIMAL_1"]]["TT_NAME"] + "! I think wha" \
241     "t this islands clearly needs more of is " + \
242     world_db["ThingTypes"][world_db["ANIMAL_1"]]["TT_NAME"] + "s. Why don't " \
243     "you help? Support them. Make sure they are well, and they will multiply" \
244     " faster. From now on, I will count each new-born " + \
245     world_db["ThingTypes"][world_db["ANIMAL_1"]]["TT_NAME"] + \
246     " (not spawned by me due to undo an extinction event) greatly to your fa" \
247     "vor. To help you with the feeding, here is something to make the ground" \
248     " bear more consumables."
249
250     altar_msg_6 = "The Island God speaks to you: \"You have proven yourself " \
251     "worthy of my respect. You were a good citizen to the island, and someti" \
252     "mes a better steward to its inhabitants than me. The island shall miss " \
253     "you when you leave. But you have earned the right to do so. Take this" + \
254     world_db["ThingTypes"][world_db["SLIPPERS"]]["TT_NAME"] + " and USE it w" \
255     "hen you please. It will take you to where you came from. (But do feel f" \
256     "ree to stay here as long as you like.)\""
257
258     def enter_altar():
259         from server.new_thing import new_Thing
260         if world_db["FAVOR_STAGE"] > 9000:
261            log("You step on a soul-less slab of stone.")
262            return
263         log("YOU ENTER SACRED GROUND.")
264         if world_db["FAVOR_STAGE"] == 0:
265             world_db["FAVOR_STAGE"] = 1
266             log(altar_msg_0)
267         elif world_db["FAVOR_STAGE"] == 1 and world_db["GOD_FAVOR"] < 100:
268             altar_msg_wait(100)
269         elif world_db["FAVOR_STAGE"] == 1 and world_db["GOD_FAVOR"] >= 100:
270             world_db["FAVOR_STAGE"] = 2
271             log(altar_msg_2)
272             id = id_setter(-1, "Things")
273             world_db["Things"][id] = new_Thing(world_db["PLANT_1"],
274                                                world_db["altar"])
275             id = id_setter(-1, "Things")
276             world_db["Things"][id] = new_Thing(world_db["TOOL_0"],
277                                                world_db["altar"])
278         elif world_db["FAVOR_STAGE"] == 2 and \
279             0 == len([id for id in world_db["Things"]
280                       if world_db["Things"][id]["T_TYPE"]
281                          == world_db["PLANT_1"]]):
282             log(altar_msg_2)
283             id = id_setter(-1, "Things")
284             world_db["Things"][id] = new_Thing(world_db["PLANT_1"],
285                                                world_db["altar"])
286             world_db["GOD_FAVOR"] -= 250
287         elif world_db["FAVOR_STAGE"] == 2 and world_db["GOD_FAVOR"] < 500:
288             altar_msg_wait(500)
289         elif world_db["FAVOR_STAGE"] == 2 and world_db["GOD_FAVOR"] >= 500:
290             world_db["FAVOR_STAGE"] = 3
291             log(altar_msg_3)
292             log(altar_msg_4)
293             world_db["EMPATHY"] = 1
294         elif world_db["FAVOR_STAGE"] == 3 and world_db["GOD_FAVOR"] < 5000:
295             altar_msg_wait(5000)
296         elif world_db["FAVOR_STAGE"] == 3 and world_db["GOD_FAVOR"] >= 5000:
297             world_db["FAVOR_STAGE"] = 4
298             log(altar_msg_5)
299             id = id_setter(-1, "Things")
300             world_db["Things"][id] = new_Thing(world_db["TOOL_1"],
301                                                world_db["altar"])
302         elif world_db["GOD_FAVOR"] < 20000:
303             altar_msg_wait(20000)
304         elif world_db["GOD_FAVOR"] > 20000:
305             world_db["FAVOR_STAGE"] = 9001
306             log(altar_msg_6)
307             id = id_setter(-1, "Things")
308             world_db["Things"][id] = new_Thing(world_db["SLIPPERS"],
309                                                world_db["altar"])
310
311     from server.config.world_data import symbols_passable
312     from server.build_fov_map import build_fov_map
313     from server.config.misc import decrement_lifepoints_func
314     from server.new_thing import new_Thing
315     global mv_yx_in_dir_legal
316     passable = False
317     move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
318                                      t["T_POSY"], t["T_POSX"])
319     if 1 == move_result[0]:
320         pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
321         hitted = [id for id in world_db["Things"]
322                   if world_db["Things"][id] != t
323                   if world_db["Things"][id]["T_LIFEPOINTS"]
324                   if world_db["Things"][id]["T_POSY"] == move_result[1]
325                   if world_db["Things"][id]["T_POSX"] == move_result[2]]
326         if len(hitted):
327             hit_id = hitted[0]
328             hitted_type = world_db["Things"][hit_id]["T_TYPE"]
329             if t == world_db["Things"][0]:
330                 hitted_name = world_db["ThingTypes"][hitted_type]["TT_NAME"]
331                 log("You WOUND " + hitted_name + ".")
332                 world_db["GOD_FAVOR"] -= 1
333             elif 0 == hit_id:
334                 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
335                 log(hitter_name +" WOUNDS you.")
336             test = decrement_lifepoints_func(world_db["Things"][hit_id])
337             if test and world_db["FAVOR_STAGE"] >= 3 and \
338                hitted_type == world_db["ANIMAL_0"]:
339                 world_db["GOD_FAVOR"] += 125
340             elif test and t == world_db["Things"][0]:
341                 world_db["GOD_FAVOR"] -= 2 * test
342             return
343         if (ord("X") == world_db["MAP"][pos]
344             or ord("|") == world_db["MAP"][pos]):
345             for id in t["T_CARRIES"]:
346                 type = world_db["Things"][id]["T_TYPE"]
347                 if world_db["ThingTypes"][type]["TT_TOOL"] == "axe":
348                     axe_name = world_db["ThingTypes"][type]["TT_NAME"]
349                     if t == world_db["Things"][0]:
350                         log("With your " + axe_name + ", you chop!")
351                         if ord("X") == world_db["MAP"][pos]:
352                             world_db["GOD_FAVOR"] -= 1
353                     chop_power = world_db["ThingTypes"][type]["TT_TOOLPOWER"]
354
355                     case_X = world_db["MAP"][pos] == ord("X")
356                     if (chop_power > 0
357                         and ((case_X and
358                               0 == int(rand.next() / chop_power))
359                         or (not case_X and
360                                  0 == int(rand.next() / (3 * chop_power))))):
361                         if t == world_db["Things"][0]:
362                             log("You chop it DOWN.")
363                             if ord("X") == world_db["MAP"][pos]:
364                                 world_db["GOD_FAVOR"] -= 10
365                         world_db["MAP"][pos] = ord(".")
366                         i = 3 if case_X else 1
367                         for i in range(i):
368                             id = id_setter(-1, "Things")
369                             world_db["Things"][id] = \
370                               new_Thing(world_db["LUMBER"],
371                                         (move_result[1], move_result[2]))
372                         build_fov_map(t)
373                     return
374         passable = chr(world_db["MAP"][pos]) in symbols_passable
375     global directions_db
376     dir = [dir for dir in directions_db
377            if directions_db[dir] == chr(t["T_ARGUMENT"])][0]
378     if passable:
379         t["T_POSY"] = move_result[1]
380         t["T_POSX"] = move_result[2]
381         for id in t["T_CARRIES"]:
382             world_db["Things"][id]["T_POSY"] = move_result[1]
383             world_db["Things"][id]["T_POSX"] = move_result[2]
384         build_fov_map(t)
385         if t == world_db["Things"][0]:
386             log("You MOVE " + dir + ".")
387             if (move_result[1] == world_db["altar"][0] and
388                 move_result[2] == world_db["altar"][1]):
389                 enter_altar()
390
391 def command_ttid(id_string):
392     id = id_setter(id_string, "ThingTypes", command_ttid)
393     if None != id:
394         world_db["ThingTypes"][id] = {
395             "TT_NAME": "(none)",
396             "TT_TOOLPOWER": 0,
397             "TT_LIFEPOINTS": 0,
398             "TT_PROLIFERATE": 0,
399             "TT_START_NUMBER": 0,
400             "TT_STORAGE": 0,
401             "TT_SYMBOL": "?",
402             "TT_CORPSE_ID": id,
403             "TT_TOOL": ""
404         }
405
406 def command_worldactive(worldactive_string):
407     val = integer_test(worldactive_string, 0, 1)
408     if None != val:
409         if 0 != world_db["WORLD_ACTIVE"]:
410             if 0 == val:
411                 set_world_inactive()
412             else:
413                 print("World already active.")
414         elif 0 == world_db["WORLD_ACTIVE"]:
415             for ThingAction in world_db["ThingActions"]:
416                 if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
417                     break
418             else:
419                 print("Ignored: No wait action defined for world to activate.")
420                 return
421             for Thing in world_db["Things"]:
422                 if 0 == Thing:
423                     break
424             else:
425                 print("Ignored: No player defined for world to activate.")
426                 return
427             if world_db["MAP"]:
428                 pos = world_db["MAP"].find(b'_')
429                 if pos > 0:
430                     y = int(pos / world_db["MAP_LENGTH"])
431                     x = pos % world_db["MAP_LENGTH"]
432                     world_db["altar"] = (y, x)
433                 else:
434                     print("Ignored: No altar defined for world to activate.")
435                     return
436             else:
437                 print("Ignored: No map defined for world to activate.")
438                 return
439             for name in world_db["specials"]:
440                 if world_db[name] not in world_db["ThingTypes"]:
441                     print("Ignored: Not all specials set for world to "
442                           "activate.")
443                     return
444             for id in world_db["Things"]:
445                 if world_db["Things"][id]["T_LIFEPOINTS"]:
446                     build_fov_map(world_db["Things"][id])
447                     if 0 == id:
448                         update_map_memory(world_db["Things"][id], False)
449             if not world_db["Things"][0]["T_LIFEPOINTS"]:
450                 empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
451                 world_db["Things"][0]["fovmap"] = empty_fovmap
452             world_db["WORLD_ACTIVE"] = 1
453
454 def play_move(str_arg):
455     if action_exists("move"):
456         from server.config.world_data import directions_db, symbols_passable
457         t = world_db["Things"][0]
458         if not str_arg in directions_db:
459             print("Illegal move direction string.")
460             return
461         dir = ord(directions_db[str_arg])
462         global mv_yx_in_dir_legal
463         move_result = mv_yx_in_dir_legal(chr(dir), t["T_POSY"], t["T_POSX"])
464         if 1 == move_result[0]:
465             pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
466             if ord("~") == world_db["MAP"][pos]:
467                 log("You can't SWIM.")
468                 return
469             if (ord("X") == world_db["MAP"][pos]
470                 or ord("|") == world_db["MAP"][pos]):
471                 carries_axe = False
472                 for id in t["T_CARRIES"]:
473                     type = world_db["Things"][id]["T_TYPE"]
474                     if world_db["ThingTypes"][type]["TT_TOOL"] == "axe":
475                         world_db["Things"][0]["T_ARGUMENT"] = dir
476                         set_command("move")
477                         return
478             if chr(world_db["MAP"][pos]) in symbols_passable:
479                 world_db["Things"][0]["T_ARGUMENT"] = dir
480                 set_command("move")
481                 return
482         log("You CAN'T move there.")
483
484 def play_use(str_arg):
485     if action_exists("use"):
486         t = world_db["Things"][0]
487         if 0 == len(t["T_CARRIES"]):
488             log("You have NOTHING to use in your inventory.")
489         else:
490             val = integer_test(str_arg, 0, 255)
491             if None != val and val < len(t["T_CARRIES"]):
492                 id = t["T_CARRIES"][val]
493                 type = world_db["Things"][id]["T_TYPE"]
494                 if (world_db["ThingTypes"][type]["TT_TOOL"] == "axe"
495                       and t == world_db["Things"][0]):
496                     log("To use this item for chopping, move towards a tree "
497                         "while carrying it in your inventory.")
498                     return
499                 elif (world_db["ThingTypes"][type]["TT_TOOL"] == "carpentry"):
500                     pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
501                     if (world_db["MAP"][pos] == ord("X")
502                         or world_db["MAP"][pos] == ord("|")):
503                         log("CAN'T build when standing on barrier.")
504                         return
505                     for id in [id for id in world_db["Things"]
506                                if not world_db["Things"][id] == t
507                                if not world_db["Things"][id]["carried"]
508                                if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
509                                if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]:
510                          log("CAN'T build when standing objects.")
511                          return
512                     wood_id = None
513                     for id in t["T_CARRIES"]:
514                         type_material = world_db["Things"][id]["T_TYPE"]
515                         if (world_db["ThingTypes"][type_material]["TT_TOOL"]
516                             == "wood"):
517                             wood_id = id
518                             break
519                     if wood_id == None:
520                         log("You CAN'T use a "
521                             + world_db["ThingTypes"][type]["TT_NAME"]
522                             + " without some wood in your inventory.")
523                         return
524                 elif world_db["ThingTypes"][type]["TT_TOOL"] == "fertilizer":
525                     pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
526                     if not world_db["MAP"][pos] == ord("."):
527                         log("Can only make soil out of NON-SOIL earth.")
528                         return
529                 elif world_db["ThingTypes"][type]["TT_TOOL"] == "wood":
530                         log("To use wood, you NEED a carpentry tool.")
531                         return
532                 elif type != world_db["SLIPPERS"] and not \
533                         world_db["ThingTypes"][type]["TT_TOOL"] == "food":
534                     log("You CAN'T consume this thing.")
535                     return
536                 world_db["Things"][0]["T_ARGUMENT"] = val
537                 set_command("use")
538             else:
539                 print("Illegal inventory index.")
540
541 def specialtypesetter(name):
542     def helper(str_int):
543         val = integer_test(str_int, 0)
544         if None != val:
545             world_db[name] = val
546             if world_db["WORLD_ACTIVE"] \
547                and world_db[name] not in world_db["ThingTypes"]:
548                 world_db["WORLD_ACTIVE"] = 0
549                 print(name + " fits no known ThingType, deactivating world.")
550     return helper
551
552 def write_metamap_A():
553     from server.worldstate_write_helpers import write_map
554     ord_v = ord("v")
555     length = world_db["MAP_LENGTH"]
556     metamapA = bytearray(b'0' * (length ** 2))
557     for id in [id for id in world_db["Things"]
558                   if not world_db["Things"][id]["carried"]
559                   if world_db["Things"][id]["T_LIFEPOINTS"]
560                   if world_db["Things"][0]["fovmap"][
561                        world_db["Things"][id]["T_POSY"] * length
562                        + world_db["Things"][id]["T_POSX"]] == ord_v]:
563         pos = (world_db["Things"][id]["T_POSY"] * length
564               + world_db["Things"][id]["T_POSX"])
565         if id == 0 or world_db["EMPATHY"]:
566             type = world_db["Things"][id]["T_TYPE"]
567             max_hp = world_db["ThingTypes"][type]["TT_LIFEPOINTS"]
568             third_of_hp = max_hp / 3
569             hp = world_db["Things"][id]["T_LIFEPOINTS"]
570             add = 0
571             if hp > 2 * third_of_hp:
572                  add = 2
573             elif hp > third_of_hp:
574                 add = 1
575             metamapA[pos] = ord('a') + add
576         else:
577             metamapA[pos] = ord('X')
578     for mt in world_db["Things"][0]["T_MEMTHING"]:
579         pos = mt[1] * length + mt[2]
580         if metamapA[pos] < ord('2'):
581             metamapA[pos] += 1
582     return write_map(metamapA, length)
583
584 def write_metamap_B():
585     from server.worldstate_write_helpers import write_map
586     ord_v = ord("v")
587     length = world_db["MAP_LENGTH"]
588     metamapB = bytearray(b' ' * (length ** 2))
589     for id in [id for id in world_db["Things"]
590                   if not world_db["Things"][id]["carried"]
591                   if world_db["Things"][id]["T_LIFEPOINTS"]
592                   if world_db["Things"][0]["fovmap"][
593                        world_db["Things"][id]["T_POSY"] * length
594                        + world_db["Things"][id]["T_POSX"]] == ord_v]:
595         pos = (world_db["Things"][id]["T_POSY"] * length
596               + world_db["Things"][id]["T_POSX"])
597         if id == 0 or world_db["EMPATHY"]:
598             action = world_db["Things"][id]["T_COMMAND"]
599             if 0 != action:
600                 name = world_db["ThingActions"][action]["TA_NAME"]
601             else:
602                 name = " "
603             metamapB[pos] = ord(name[0])
604     return write_map(metamapB, length)
605
606 def calc_effort(thing_action, thing):
607     from math import sqrt
608     effort = thing_action["TA_EFFORT"]
609     if thing_action["TA_NAME"] == "move":
610         typ = thing["T_TYPE"]
611         max_hp = (world_db["ThingTypes"][typ]["TT_LIFEPOINTS"])
612         effort = int(effort / sqrt(max_hp))
613         effort = 1 if effort == 0 else effort
614     return effort
615
616 def play_pickup():
617     """Try "pickup" as player's T_COMMAND"."""
618     if action_exists("pickup"):
619         t = world_db["Things"][0]
620         ids = [id for id in world_db["Things"] if id
621                if not world_db["Things"][id]["carried"]
622                if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
623                if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]
624         if not len(ids):
625             log("NOTHING to pick up.")
626         elif len(t["T_CARRIES"]) >= world_db["ThingTypes"][t["T_TYPE"]] \
627                 ["TT_STORAGE"]:
628             log("CAN'T pick up: No storage room to carry anything more.")
629         else:
630             set_command("pickup")
631
632 strong_write(io_db["file_out"], "PLUGIN PleaseTheIslandGod\n")
633
634 def set_zero_if_undefined(key):
635     if not key in world_db:
636         world_db[key] = 0
637
638 set_zero_if_undefined("GOD_FAVOR")
639 set_zero_if_undefined("FAVOR_STAGE")
640 set_zero_if_undefined("EMPATHY")
641 world_db["specials"] = ["SLIPPERS", "PLANT_0", "PLANT_1", "TOOL_0", "TOOL_1",
642     "LUMBER", "ANIMAL_0", "ANIMAL_1"]
643 for key in world_db["specials"]:
644     set_zero_if_undefined(key)
645
646 world_db["terrain_names"][":"] = "SOIL"
647 world_db["terrain_names"]["|"] = "WALL"
648 world_db["terrain_names"]["_"] = "ALTAR"
649 io_db["worldstate_write_order"] += [["GOD_FAVOR", "world_int"]]
650 io_db["worldstate_write_order"] += [[write_metamap_A, "func"]]
651 io_db["worldstate_write_order"] += [[write_metamap_B, "func"]]
652
653 import server.config.world_data
654 server.config.world_data.symbols_passable += ":_"
655
656 from server.config.world_data import thing_defaults
657 thing_defaults["T_PLAYERDROP"] = 0
658
659 import server.config.actions
660 server.config.actions.action_db["actor_move"] = actor_move
661 server.config.actions.action_db["actor_pickup"] = actor_pickup
662 server.config.actions.action_db["actor_drop"] = actor_drop
663 server.config.actions.action_db["actor_use"] = actor_use
664 server.config.actions.actor_pickup_test_hook = actor_pickup_test_hook
665
666 from server.config.commands import commands_db
667 commands_db["TT_ID"] = (1, False, command_ttid)
668 commands_db["GOD_FAVOR"] = (1, False, setter(None, "GOD_FAVOR", -32768, 32767))
669 commands_db["TT_STORAGE"] = (1, False, setter("ThingType", "TT_STORAGE", 0, 255))
670 commands_db["T_PLAYERDROP"] = (1, False, setter("Thing", "T_PLAYERDROP", 0, 1))
671 commands_db["WORLD_ACTIVE"] = (1, False, command_worldactive)
672 commands_db["FAVOR_STAGE"] = (1, False, setter(None, "FAVOR_STAGE", 0, 1))
673 commands_db["SLIPPERS"] = (1, False, specialtypesetter("SLIPPERS"))
674 commands_db["TOOL_0"] = (1, False, specialtypesetter("TOOL_0"))
675 commands_db["TOOL_1"] = (1, False, specialtypesetter("TOOL_1"))
676 commands_db["ANIMAL_0"] = (1, False, specialtypesetter("ANIMAL_0"))
677 commands_db["ANIMAL_1"] = (1, False, specialtypesetter("ANIMAL_1"))
678 commands_db["PLANT_0"] = (1, False, specialtypesetter("PLANT_0"))
679 commands_db["PLANT_1"] = (1, False, specialtypesetter("PLANT_1"))
680 commands_db["LUMBER"] = (1, False, specialtypesetter("LUMBER"))
681 commands_db["EMPATHY"] = (1, False, setter(None, "EMPATHY", 0, 1))
682 commands_db["use"] = (1, False, play_use)
683 commands_db["move"] = (1, False, play_move)
684 commands_db["pickup"] = (0, False, play_pickup)
685
686 import server.config.misc
687 server.config.misc.make_map_func = make_map
688 server.config.misc.decrement_lifepoints_func = decrement_lifepoints
689 server.config.misc.calc_effort_func = calc_effort
690
691 import server.config.make_world_helpers
692 server.config.make_world_helpers.pos_test_func = pos_test
693 server.config.make_world_helpers.world_makable_func = world_makable
694 server.config.make_world_helpers.make_map_func = make_map
695
696 import server.config.thingproliferation
697 server.config.thingproliferation.field_spreadable = field_spreadable
698 server.config.thingproliferation.thingprol_plugin_conditions = \
699     thingprol_plugin_conditions
700 server.config.thingproliferation.thingprol_plugin_post_create_hook = \
701     thingprol_plugin_post_create_hook
702
703 import server.config.ai
704 server.config.ai.ai_hook_pickup = ai_hook_pickup_test