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