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