home · contact · privacy
Server, plugin: Refactor actor_move plugin hooking.
[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     from server.actions import actor_drop
95     dropped = actor_drop(t)
96     if dropped != None:
97         dropped["T_PLAYERDROP"] = 1
98
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.\n")
105         t["T_LIFEPOINTS"] = 1
106         from server.config.misc import decrement_lifepoints_func
107         decrement_lifepoints_func(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("|")):
112             return
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"]]:
118             return
119         wood_id = None
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"):
123                 wood_id = t_id
124                 break
125         if wood_id != None:
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(":")
137
138 def actor_move(t):
139
140     def altar_msg_wait(limit):
141             log("The Island God will talk again when it favors you to >=" +
142                 str(limit) + " points.")
143
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" \
153     " your favor.\""
154
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.\""
163
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.\""
169
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" \
175     "imals.\""
176
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)."
180
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."
192
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 it w" \
198     "hen you please. It will take you to where you came from. (But do feel f" \
199     "ree to stay here as long as you like.)\""
200
201     def enter_altar():
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.")
205            return
206         log("YOU ENTER SACRED GROUND.")
207         if world_db["FAVOR_STAGE"] == 0:
208             world_db["FAVOR_STAGE"] = 1
209             log(altar_msg_0)
210         elif world_db["FAVOR_STAGE"] == 1 and world_db["GOD_FAVOR"] < 100:
211             altar_msg_wait(100)
212         elif world_db["FAVOR_STAGE"] == 1 and world_db["GOD_FAVOR"] >= 100:
213             world_db["FAVOR_STAGE"] = 2
214             log(altar_msg_2)
215             id = id_setter(-1, "Things")
216             world_db["Things"][id] = new_Thing(world_db["PLANT_1"],
217                                                world_db["altar"])
218             id = id_setter(-1, "Things")
219             world_db["Things"][id] = new_Thing(world_db["TOOL_0"],
220                                                world_db["altar"])
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"]]):
225             log(altar_msg_2)
226             id = id_setter(-1, "Things")
227             world_db["Things"][id] = new_Thing(world_db["PLANT_1"],
228                                                world_db["altar"])
229             world_db["GOD_FAVOR"] -= 250
230         elif world_db["FAVOR_STAGE"] == 2 and world_db["GOD_FAVOR"] < 500:
231             altar_msg_wait(500)
232         elif world_db["FAVOR_STAGE"] == 2 and world_db["GOD_FAVOR"] >= 500:
233             world_db["FAVOR_STAGE"] = 3
234             log(altar_msg_3)
235             log(altar_msg_4)
236             world_db["EMPATHY"] = 1
237         elif world_db["FAVOR_STAGE"] == 3 and world_db["GOD_FAVOR"] < 5000:
238             altar_msg_wait(5000)
239         elif world_db["FAVOR_STAGE"] == 3 and world_db["GOD_FAVOR"] >= 5000:
240             world_db["FAVOR_STAGE"] = 4
241             log(altar_msg_5)
242             id = id_setter(-1, "Things")
243             world_db["Things"][id] = new_Thing(world_db["TOOL_1"],
244                                                world_db["altar"])
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
249             log(altar_msg_6)
250             id = id_setter(-1, "Things")
251             world_db["Things"][id] = new_Thing(world_db["SLIPPERS"],
252                                                world_db["altar"])
253
254     from server.actions import actor_move
255     test = actor_move(t)
256     if 2 == len(test):
257         if test[0] > 0:
258             if world_db["FAVOR_STAGE"] >= 3 and \
259                     test[1] == world_db["ANIMAL_0"]:
260                 world_db["GOD_FAVOR"] += 125
261             elif t == world_db["Things"][0]:
262                 world_db["GOD_FAVOR"] -= 2 * test[1]
263         elif t == world_db["Things"][0]:
264             world_db["GOD_FAVOR"] -= 1
265     elif 3 == len(test):
266         if (t == world_db["Things"][0] and
267                 test[1] == world_db["altar"][0] and
268                 test[2] == world_db["altar"][1]):
269             enter_altar()
270
271 def actor_move_attempts_hook(t, move_result, pos):
272     if (ord("X") == world_db["MAP"][pos] or ord("|") == world_db["MAP"][pos]):
273         for tid in t["T_CARRIES"]:
274             ty = world_db["Things"][tid]["T_TYPE"]
275             if world_db["ThingTypes"][ty]["TT_TOOL"] == "axe":
276                 axe_name = world_db["ThingTypes"][ty]["TT_NAME"]
277                 if t == world_db["Things"][0]:
278                     log("With your " + axe_name + ", you chop!")
279                     if ord("X") == world_db["MAP"][pos]:
280                         world_db["GOD_FAVOR"] -= 1
281                 chop_power = world_db["ThingTypes"][ty]["TT_TOOLPOWER"]
282                 case_X = world_db["MAP"][pos] == ord("X")
283                 if (chop_power > 0 and
284                     ((case_X and 0 == int(rand.next() / chop_power))
285                      or (not case_X and
286                          0 == int(rand.next() / (3 * chop_power))))):
287                     if t == world_db["Things"][0]:
288                         log("You chop it DOWN.")
289                         if ord("X") == world_db["MAP"][pos]:
290                             world_db["GOD_FAVOR"] -= 10
291                     world_db["MAP"][pos] = ord(".")
292                     i = 3 if case_X else 1
293                     for i in range(i):
294                         tid = id_setter(-1, "Things")
295                         world_db["Things"][tid] = \
296                           new_Thing(world_db["LUMBER"],
297                                     (move_result[1], move_result[2]))
298                     build_fov_map(t)
299                 return True
300     return False
301
302 def decrement_lifepoints(t):
303     t["T_LIFEPOINTS"] -= 1
304     live_tid = t["T_TYPE"]
305     _id = [_id for _id in world_db["Things"] if world_db["Things"][_id] == t][0]
306     if 0 == t["T_LIFEPOINTS"]:
307         for id in t["T_CARRIES"]:
308             t["T_CARRIES"].remove(id)
309             world_db["Things"][id]["T_POSY"] = t["T_POSY"]
310             world_db["Things"][id]["T_POSX"] = t["T_POSX"]
311             world_db["Things"][id]["carried"] = False
312         t["T_TYPE"] = world_db["ThingTypes"][t["T_TYPE"]]["TT_CORPSE_ID"]
313         if world_db["Things"][0] == t:
314             t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
315             log("You die.")
316             log("See README on how to start over.")
317         else:
318             t["fovmap"] = False
319             t["T_MEMMAP"] = False
320             t["T_MEMDEPTHMAP"] = False
321             t["T_MEMTHING"] = []
322             n_species = len([id for id in world_db["Things"]
323                              if world_db["Things"][id]["T_TYPE"] == live_tid])
324             if 0 == n_species:
325                 from server.new_thing import new_Thing
326                 if world_db["FAVOR_STAGE"] >= 3 and \
327                     live_tid == world_db["ANIMAL_0"]:
328                     world_db["GOD_FAVOR"] += 3000
329                     log("CONGRATULATIONS! The "
330                         + world_db["ThingTypes"][live_tid]["TT_NAME"]
331                         + " species has died out. The Island God is pleased.")
332                 else:
333                     id = id_setter(-1, "Things")
334                     world_db["Things"][id] = new_Thing(live_tid,
335                                                        world_db["altar"])
336                     log("The "
337                         + world_db["ThingTypes"][live_tid]["TT_NAME"]
338                         + " species has temporarily died out. "
339                         + "One new-born is spawned at the altar.")
340         return world_db["ThingTypes"][live_tid]["TT_LIFEPOINTS"]
341     return 0
342
343 def command_ttid(id_string):
344     id = id_setter(id_string, "ThingTypes", command_ttid)
345     if None != id:
346         world_db["ThingTypes"][id] = {
347             "TT_NAME": "(none)",
348             "TT_TOOLPOWER": 0,
349             "TT_LIFEPOINTS": 0,
350             "TT_PROLIFERATE": 0,
351             "TT_START_NUMBER": 0,
352             "TT_STORAGE": 0,
353             "TT_SYMBOL": "?",
354             "TT_CORPSE_ID": id,
355             "TT_TOOL": ""
356         }
357
358 def command_worldactive(worldactive_string):
359     val = integer_test(worldactive_string, 0, 1)
360     if None != val:
361         if 0 != world_db["WORLD_ACTIVE"]:
362             if 0 == val:
363                 set_world_inactive()
364             else:
365                 print("World already active.")
366         elif 0 == world_db["WORLD_ACTIVE"]:
367             for ThingAction in world_db["ThingActions"]:
368                 if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
369                     break
370             else:
371                 print("Ignored: No wait action defined for world to activate.")
372                 return
373             for Thing in world_db["Things"]:
374                 if 0 == Thing:
375                     break
376             else:
377                 print("Ignored: No player defined for world to activate.")
378                 return
379             if world_db["MAP"]:
380                 pos = world_db["MAP"].find(b'_')
381                 if pos > 0:
382                     y = int(pos / world_db["MAP_LENGTH"])
383                     x = pos % world_db["MAP_LENGTH"]
384                     world_db["altar"] = (y, x)
385                 else:
386                     print("Ignored: No altar defined for world to activate.")
387                     return
388             else:
389                 print("Ignored: No map defined for world to activate.")
390                 return
391             for name in world_db["specials"]:
392                 if world_db[name] not in world_db["ThingTypes"]:
393                     print("Ignored: Not all specials set for world to "
394                           "activate.")
395                     return
396             for id in world_db["Things"]:
397                 if world_db["Things"][id]["T_LIFEPOINTS"]:
398                     build_fov_map(world_db["Things"][id])
399                     if 0 == id:
400                         update_map_memory(world_db["Things"][id], False)
401             if not world_db["Things"][0]["T_LIFEPOINTS"]:
402                 empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
403                 world_db["Things"][0]["fovmap"] = empty_fovmap
404             world_db["WORLD_ACTIVE"] = 1
405
406 def play_move(str_arg):
407     if action_exists("move"):
408         from server.config.world_data import directions_db, symbols_passable
409         t = world_db["Things"][0]
410         if not str_arg in directions_db:
411             print("Illegal move direction string.")
412             return
413         dir = ord(directions_db[str_arg])
414         global mv_yx_in_dir_legal
415         move_result = mv_yx_in_dir_legal(chr(dir), t["T_POSY"], t["T_POSX"])
416         if 1 == move_result[0]:
417             pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
418             if ord("~") == world_db["MAP"][pos]:
419                 log("You can't SWIM.")
420                 return
421             if (ord("X") == world_db["MAP"][pos]
422                 or ord("|") == world_db["MAP"][pos]):
423                 carries_axe = False
424                 for id in t["T_CARRIES"]:
425                     type = world_db["Things"][id]["T_TYPE"]
426                     if world_db["ThingTypes"][type]["TT_TOOL"] == "axe":
427                         world_db["Things"][0]["T_ARGUMENT"] = dir
428                         set_command("move")
429                         return
430             if chr(world_db["MAP"][pos]) in symbols_passable:
431                 world_db["Things"][0]["T_ARGUMENT"] = dir
432                 set_command("move")
433                 return
434         log("You CAN'T move there.")
435
436 def play_use(str_arg):
437     if action_exists("use"):
438         t = world_db["Things"][0]
439         if 0 == len(t["T_CARRIES"]):
440             log("You have NOTHING to use in your inventory.")
441         else:
442             val = integer_test(str_arg, 0, 255)
443             if None != val and val < len(t["T_CARRIES"]):
444                 id = t["T_CARRIES"][val]
445                 type = world_db["Things"][id]["T_TYPE"]
446                 if (world_db["ThingTypes"][type]["TT_TOOL"] == "axe"
447                       and t == world_db["Things"][0]):
448                     log("To use this item for chopping, move towards a tree "
449                         "while carrying it in your inventory.")
450                     return
451                 elif (world_db["ThingTypes"][type]["TT_TOOL"] == "carpentry"):
452                     pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
453                     if (world_db["MAP"][pos] == ord("X")
454                         or world_db["MAP"][pos] == ord("|")):
455                         log("CAN'T build when standing on barrier.")
456                         return
457                     for id in [id for id in world_db["Things"]
458                                if not world_db["Things"][id] == t
459                                if not world_db["Things"][id]["carried"]
460                                if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
461                                if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]:
462                          log("CAN'T build when standing objects.")
463                          return
464                     wood_id = None
465                     for id in t["T_CARRIES"]:
466                         type_material = world_db["Things"][id]["T_TYPE"]
467                         if (world_db["ThingTypes"][type_material]["TT_TOOL"]
468                             == "wood"):
469                             wood_id = id
470                             break
471                     if wood_id == None:
472                         log("You CAN'T use a "
473                             + world_db["ThingTypes"][type]["TT_NAME"]
474                             + " without some wood in your inventory.")
475                         return
476                 elif world_db["ThingTypes"][type]["TT_TOOL"] == "fertilizer":
477                     pos = t["T_POSY"] * world_db["MAP_LENGTH"] + t["T_POSX"]
478                     if not world_db["MAP"][pos] == ord("."):
479                         log("Can only make soil out of NON-SOIL earth.")
480                         return
481                 elif world_db["ThingTypes"][type]["TT_TOOL"] == "wood":
482                         log("To use wood, you NEED a carpentry tool.")
483                         return
484                 elif type != world_db["SLIPPERS"] and not \
485                         world_db["ThingTypes"][type]["TT_TOOL"] == "food":
486                     log("You CAN'T consume this thing.")
487                     return
488                 world_db["Things"][0]["T_ARGUMENT"] = val
489                 set_command("use")
490             else:
491                 print("Illegal inventory index.")
492
493 def specialtypesetter(name):
494     def helper(str_int):
495         val = integer_test(str_int, 0)
496         if None != val:
497             world_db[name] = val
498             if world_db["WORLD_ACTIVE"] \
499                and world_db[name] not in world_db["ThingTypes"]:
500                 world_db["WORLD_ACTIVE"] = 0
501                 print(name + " fits no known ThingType, deactivating world.")
502     return helper
503
504 def write_metamap_A():
505     from server.worldstate_write_helpers import write_map
506     ord_v = ord("v")
507     length = world_db["MAP_LENGTH"]
508     metamapA = bytearray(b'0' * (length ** 2))
509     for id in [id for id in world_db["Things"]
510                   if not world_db["Things"][id]["carried"]
511                   if world_db["Things"][id]["T_LIFEPOINTS"]
512                   if world_db["Things"][0]["fovmap"][
513                        world_db["Things"][id]["T_POSY"] * length
514                        + world_db["Things"][id]["T_POSX"]] == ord_v]:
515         pos = (world_db["Things"][id]["T_POSY"] * length
516               + world_db["Things"][id]["T_POSX"])
517         if id == 0 or world_db["EMPATHY"]:
518             type = world_db["Things"][id]["T_TYPE"]
519             max_hp = world_db["ThingTypes"][type]["TT_LIFEPOINTS"]
520             third_of_hp = max_hp / 3
521             hp = world_db["Things"][id]["T_LIFEPOINTS"]
522             add = 0
523             if hp > 2 * third_of_hp:
524                  add = 2
525             elif hp > third_of_hp:
526                 add = 1
527             metamapA[pos] = ord('a') + add
528         else:
529             metamapA[pos] = ord('X')
530     for mt in world_db["Things"][0]["T_MEMTHING"]:
531         pos = mt[1] * length + mt[2]
532         if metamapA[pos] < ord('2'):
533             metamapA[pos] += 1
534     return write_map(metamapA, length)
535
536 def write_metamap_B():
537     from server.worldstate_write_helpers import write_map
538     ord_v = ord("v")
539     length = world_db["MAP_LENGTH"]
540     metamapB = bytearray(b' ' * (length ** 2))
541     for id in [id for id in world_db["Things"]
542                   if not world_db["Things"][id]["carried"]
543                   if world_db["Things"][id]["T_LIFEPOINTS"]
544                   if world_db["Things"][0]["fovmap"][
545                        world_db["Things"][id]["T_POSY"] * length
546                        + world_db["Things"][id]["T_POSX"]] == ord_v]:
547         pos = (world_db["Things"][id]["T_POSY"] * length
548               + world_db["Things"][id]["T_POSX"])
549         if id == 0 or world_db["EMPATHY"]:
550             action = world_db["Things"][id]["T_COMMAND"]
551             if 0 != action:
552                 name = world_db["ThingActions"][action]["TA_NAME"]
553             else:
554                 name = " "
555             metamapB[pos] = ord(name[0])
556     return write_map(metamapB, length)
557
558 def calc_effort(thing_action, thing):
559     from math import sqrt
560     effort = thing_action["TA_EFFORT"]
561     if thing_action["TA_NAME"] == "move":
562         typ = thing["T_TYPE"]
563         max_hp = (world_db["ThingTypes"][typ]["TT_LIFEPOINTS"])
564         effort = int(effort / sqrt(max_hp))
565         effort = 1 if effort == 0 else effort
566     return effort
567
568 def play_pickup():
569     """Try "pickup" as player's T_COMMAND"."""
570     if action_exists("pickup"):
571         t = world_db["Things"][0]
572         ids = [id for id in world_db["Things"] if id
573                if not world_db["Things"][id]["carried"]
574                if world_db["Things"][id]["T_POSY"] == t["T_POSY"]
575                if world_db["Things"][id]["T_POSX"] == t["T_POSX"]]
576         if not len(ids):
577             log("NOTHING to pick up.")
578         elif len(t["T_CARRIES"]) >= world_db["ThingTypes"][t["T_TYPE"]] \
579                 ["TT_STORAGE"]:
580             log("CAN'T pick up: No storage room to carry anything more.")
581         else:
582             set_command("pickup")
583
584 strong_write(io_db["file_out"], "PLUGIN PleaseTheIslandGod\n")
585
586 def set_zero_if_undefined(key):
587     if not key in world_db:
588         world_db[key] = 0
589
590 set_zero_if_undefined("GOD_FAVOR")
591 set_zero_if_undefined("FAVOR_STAGE")
592 set_zero_if_undefined("EMPATHY")
593 world_db["specials"] = ["SLIPPERS", "PLANT_0", "PLANT_1", "TOOL_0", "TOOL_1",
594     "LUMBER", "ANIMAL_0", "ANIMAL_1"]
595 for key in world_db["specials"]:
596     set_zero_if_undefined(key)
597
598 world_db["terrain_names"][":"] = "SOIL"
599 world_db["terrain_names"]["|"] = "WALL"
600 world_db["terrain_names"]["_"] = "ALTAR"
601 io_db["worldstate_write_order"] += [["GOD_FAVOR", "world_int"]]
602 io_db["worldstate_write_order"] += [[write_metamap_A, "func"]]
603 io_db["worldstate_write_order"] += [[write_metamap_B, "func"]]
604
605 import server.config.world_data
606 server.config.world_data.symbols_passable += ":_"
607
608 from server.config.world_data import thing_defaults
609 thing_defaults["T_PLAYERDROP"] = 0
610
611 import server.config.actions
612 server.config.actions.action_db["actor_move"] = actor_move
613 server.config.actions.action_db["actor_pickup"] = actor_pickup
614 server.config.actions.action_db["actor_drop"] = actor_drop
615 server.config.actions.actor_pickup_test_hook = actor_pickup_test_hook
616 server.config.actions.actor_use_attempts_hook = actor_use_attempts_hook
617 server.config.actions.actor_move_attempts_hook = actor_move_attempts_hook
618
619 from server.config.commands import commands_db
620 commands_db["TT_ID"] = (1, False, command_ttid)
621 commands_db["GOD_FAVOR"] = (1, False, setter(None, "GOD_FAVOR", -32768, 32767))
622 commands_db["TT_STORAGE"] = (1, False, setter("ThingType", "TT_STORAGE", 0, 255))
623 commands_db["T_PLAYERDROP"] = (1, False, setter("Thing", "T_PLAYERDROP", 0, 1))
624 commands_db["WORLD_ACTIVE"] = (1, False, command_worldactive)
625 commands_db["FAVOR_STAGE"] = (1, False, setter(None, "FAVOR_STAGE", 0, 1))
626 commands_db["SLIPPERS"] = (1, False, specialtypesetter("SLIPPERS"))
627 commands_db["TOOL_0"] = (1, False, specialtypesetter("TOOL_0"))
628 commands_db["TOOL_1"] = (1, False, specialtypesetter("TOOL_1"))
629 commands_db["ANIMAL_0"] = (1, False, specialtypesetter("ANIMAL_0"))
630 commands_db["ANIMAL_1"] = (1, False, specialtypesetter("ANIMAL_1"))
631 commands_db["PLANT_0"] = (1, False, specialtypesetter("PLANT_0"))
632 commands_db["PLANT_1"] = (1, False, specialtypesetter("PLANT_1"))
633 commands_db["LUMBER"] = (1, False, specialtypesetter("LUMBER"))
634 commands_db["EMPATHY"] = (1, False, setter(None, "EMPATHY", 0, 1))
635 commands_db["use"] = (1, False, play_use)
636 commands_db["move"] = (1, False, play_move)
637 commands_db["pickup"] = (0, False, play_pickup)
638
639 import server.config.misc
640 server.config.misc.make_map_func = make_map
641 server.config.misc.decrement_lifepoints_func = decrement_lifepoints
642 server.config.misc.calc_effort_func = calc_effort
643
644 import server.config.make_world_helpers
645 server.config.make_world_helpers.pos_test_func = pos_test
646 server.config.make_world_helpers.world_makable_func = world_makable
647 server.config.make_world_helpers.make_map_func = make_map
648
649 import server.config.thingproliferation
650 server.config.thingproliferation.field_spreadable = field_spreadable
651 server.config.thingproliferation.thingprol_plugin_conditions = \
652     thingprol_plugin_conditions
653 server.config.thingproliferation.thingprol_plugin_post_create_hook = \
654     thingprol_plugin_post_create_hook
655
656 import server.config.ai
657 server.config.ai.ai_hook_pickup = ai_hook_pickup_test