home · contact · privacy
b1dc407898d86bb25092012585a9a023b156b6fe
[plomrogue] / plugins / server / TheCrawlingEater.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.config.world_data import world_db
7
8
9 def play_drink():
10     if action_exists("drink") and world_db["WORLD_ACTIVE"]:
11         pos = world_db["Things"][0]["pos"]
12         if not (chr(world_db["MAP"][pos]) == "0"
13                 and world_db["wetmap"][pos] > ord("0")):
14             log("NOTHING to drink here.")
15             return
16         elif world_db["Things"][0]["T_KIDNEY"] >= 32:
17             log("You're too FULL to drink more.")
18             return
19         world_db["set_command"]("drink")
20
21
22 def actor_drink(t):
23     pos = world_db["Things"][0]["pos"]
24     if chr(world_db["MAP"][pos]) == "0" and \
25                 world_db["wetmap"][pos] > ord("0") and t["T_KIDNEY"] < 32:
26         log("You DRINK.")
27         t["T_KIDNEY"] += 1
28         world_db["wetmap"][pos] -= 1
29         if world_db["wetmap"][pos] == ord("0"):
30             world_db["MAP"][pos] = ord("0")
31
32
33 def play_pee():
34     if action_exists("pee") and world_db["WORLD_ACTIVE"]:
35         if world_db["Things"][0]["T_BLADDER"] < 1:
36             log("Nothing to drop from empty bladder.")
37             return
38         world_db["set_command"]("pee")
39
40
41 def actor_pee(t):
42     if t["T_BLADDER"] < 1:
43         return
44     if t == world_db["Things"][0]:
45         log("You LOSE fluid.")
46     if not world_db["test_air"](t):
47         return
48     t["T_BLADDER"] -= 1
49     world_db["wetmap"][t["pos"]] += 1
50
51
52 def play_drop():
53     if action_exists("drop") and world_db["WORLD_ACTIVE"]:
54         if world_db["Things"][0]["T_BOWEL"] < 1:
55             log("Nothing to drop from empty bowel.")
56             return
57         world_db["set_command"]("drop")
58
59
60 def actor_drop(t):
61     if t["T_BOWEL"] < 1:
62         return
63     if t == world_db["Things"][0]:
64         log("You DROP waste.")
65     if not world_db["test_air"](t):
66         return
67     world_db["MAP"][t["pos"]] += 1
68     t["T_BOWEL"] -= 1
69
70
71 def play_move(str_arg):
72     """Try "move" as player's T_COMMAND, str_arg as T_ARGUMENT / direction."""
73     if action_exists("move") and world_db["WORLD_ACTIVE"]:
74         from server.config.world_data import directions_db, symbols_passable
75         t = world_db["Things"][0]
76         if not str_arg in directions_db:
77             print("Illegal move direction string.")
78             return
79         d = ord(directions_db[str_arg])
80         from server.utils import mv_yx_in_dir_legal
81         move_result = mv_yx_in_dir_legal(chr(d), t["T_POSY"], t["T_POSX"])
82         if 1 == move_result[0]:
83             pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
84             if chr(world_db["MAP"][pos]) in "34":
85                 if t["T_STOMACH"] >= 32:
86                     if t == world_db["Things"][0]:
87                         log("You're too FULL to eat.")
88                     return
89                 world_db["Things"][0]["T_ARGUMENT"] = d
90                 world_db["set_command"]("eat")
91                 return
92             if chr(world_db["MAP"][pos]) in symbols_passable:
93                 world_db["Things"][0]["T_ARGUMENT"] = d
94                 world_db["set_command"]("move")
95                 return
96         log("You CAN'T eat your way through there.")
97
98
99 def actor_eat(t):
100     from server.utils import mv_yx_in_dir_legal, rand
101     from server.config.world_data import symbols_passable
102     passable = False
103     move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
104                                      t["T_POSY"], t["T_POSX"])
105     if 1 == move_result[0]:
106         pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
107         #hitted = [tid for tid in world_db["Things"]
108         #          if world_db["Things"][tid] != t
109         #          if world_db["Things"][tid]["T_LIFEPOINTS"]
110         #          if world_db["Things"][tid]["T_POSY"] == move_result[1]
111         #          if world_db["Things"][tid]["T_POSX"] == move_result[2]]
112         #if len(hitted):
113         #    hit_id = hitted[0]
114         #    hitted_tid = world_db["Things"][hit_id]["T_TYPE"]
115         #    if t == world_db["Things"][0]:
116         #        hitted_name = world_db["ThingTypes"][hitted_tid]["TT_NAME"]
117         #        log("You BUMP into " + hitted_name + ".")
118         #    elif 0 == hit_id:
119         #        hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
120         #        log(hitter_name +" BUMPS into you.")
121         #    return
122         passable = chr(world_db["MAP"][pos]) in symbols_passable
123     if passable:
124         log("You try to EAT, but fail.")
125     else:
126         height = world_db["MAP"][pos] - ord("0")
127         if t["T_STOMACH"] >= 32 or height == 5:
128             return
129         eaten = False
130         if height == 3 and 0 == int(rand.next() % 2):
131             t["T_STOMACH"] += height
132             eaten = True
133         elif height == 4 and 0 == int(rand.next() % 5):
134             t["T_STOMACH"] += height
135             eaten = True
136         log("You EAT.")
137         if eaten:
138             world_db["MAP"][pos] = ord("0")
139             if t["T_STOMACH"] > 32:
140                 t["T_STOMACH"] = 32
141
142
143 def actor_move(t):
144     from server.build_fov_map import build_fov_map
145     from server.utils import mv_yx_in_dir_legal, rand
146     from server.config.world_data import symbols_passable
147     passable = False
148     move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
149                                      t["T_POSY"], t["T_POSX"])
150     if 1 == move_result[0]:
151         pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
152         #hitted = [tid for tid in world_db["Things"]
153         #          if world_db["Things"][tid] != t
154         #          if world_db["Things"][tid]["T_LIFEPOINTS"]
155         #          if world_db["Things"][tid]["T_POSY"] == move_result[1]
156         #          if world_db["Things"][tid]["T_POSX"] == move_result[2]]
157         #if len(hitted):
158         #    hit_id = hitted[0]
159         #    hitted_tid = world_db["Things"][hit_id]["T_TYPE"]
160         #    if t == world_db["Things"][0]:
161         #        hitted_name = world_db["ThingTypes"][hitted_tid]["TT_NAME"]
162         #        log("You BUMP into " + hitted_name + ".")
163         #    elif 0 == hit_id:
164         #        hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
165         #        log(hitter_name +" BUMPS into you.")
166         #    return
167         passable = chr(world_db["MAP"][pos]) in symbols_passable
168     if passable:
169         t["T_POSY"] = move_result[1]
170         t["T_POSX"] = move_result[2]
171         t["pos"] = move_result[1] * world_db["MAP_LENGTH"] + move_result[2]
172         build_fov_map(t)
173     else:
174         log("You try to MOVE there, but fail.")
175
176
177 def test_hole(t):
178     if world_db["MAP"][t["pos"]] == ord("-"):
179         world_db["die"](t, "You FALL in a hole, and die.")
180         return False
181     return True
182 world_db["test_hole"] = test_hole
183
184
185 def test_air(t):
186     if (world_db["wetmap"][t["pos"]] - ord("0")) \
187             + (world_db["MAP"][t["pos"]] - ord("0")) > 5:
188         world_db["die"](t, "You SUFFOCATE")
189         return False
190     return True
191 world_db["test_air"] = test_air
192
193
194 def die(t, message):
195     t["T_LIFEPOINTS"] = 0
196     if t == world_db["Things"][0]:
197         t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
198         log(message)
199 world_db["die"] = die
200
201
202 def make_map():
203     from server.make_map import new_pos, is_neighbor
204     from server.utils import rand
205     world_db["MAP"] = bytearray(b'5' * (world_db["MAP_LENGTH"] ** 2))
206     length = world_db["MAP_LENGTH"]
207     add_half_width = (not (length % 2)) * int(length / 2)
208     world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord("4")
209     while (1):
210         y, x, pos = new_pos()
211         if "5" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "4"):
212             if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
213                 break
214             world_db["MAP"][pos] = ord("4")
215     n_ground = int((length ** 2) / 16)
216     i_ground = 0
217     while (i_ground <= n_ground):
218         single_allowed = rand.next() % 32
219         y, x, pos = new_pos()
220         if "4" == chr(world_db["MAP"][pos]) \
221                 and ((not single_allowed) or is_neighbor((y, x), "0")):
222             world_db["MAP"][pos] = ord("0")
223             i_ground += 1
224     n_water = int((length ** 2) / 64)
225     i_water = 0
226     while (i_water <= n_water):
227         y, x, pos = new_pos()
228         if ord("0") == world_db["MAP"][pos] and \
229                 ord("0") == world_db["wetmap"][pos]:
230             world_db["wetmap"][pos] = ord("3")
231             i_water += 1
232
233
234 def calc_effort(ta, t):
235     from server.utils import mv_yx_in_dir_legal
236     if ta["TA_NAME"] == "move":
237         move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
238                                          t["T_POSY"], t["T_POSX"])
239         if 1 == move_result[0]:
240             pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
241             narrowness = world_db["MAP"][pos] - ord("0")
242             return 2 ** narrowness
243     return 1
244 world_db["calc_effort"] = calc_effort
245
246
247 def turn_over():
248     from server.ai import ai
249     from server.config.actions import action_db
250     from server.update_map_memory import update_map_memory
251     from server.io import try_worldstate_update
252     from server.config.io import io_db
253     from server.utils import rand
254     while world_db["Things"][0]["T_LIFEPOINTS"]:
255         for tid in [tid for tid in world_db["Things"]]:
256             if not tid in world_db["Things"]:
257                 continue
258             t = world_db["Things"][tid]
259             if t["T_LIFEPOINTS"]:
260                 if not (world_db["test_air"](t) and world_db["test_hole"](t)):
261                     continue
262                 if not t["T_COMMAND"]:
263                     update_map_memory(t)
264                     if 0 == tid:
265                         return
266                     ai(t)
267                 if t["T_LIFEPOINTS"]:
268                     t["T_PROGRESS"] += 1
269                     taid = [a for a in world_db["ThingActions"]
270                               if a == t["T_COMMAND"]][0]
271                     ThingAction = world_db["ThingActions"][taid]
272                     effort = world_db["calc_effort"](ThingAction, t)
273                     if t["T_PROGRESS"] >= effort:
274                         action = action_db["actor_" + ThingAction["TA_NAME"]]
275                         action(t)
276                         t["T_COMMAND"] = 0
277                         t["T_PROGRESS"] = 0
278                     if t["T_BOWEL"] > 16:
279                         if 0 == (rand.next() % (33 - t["T_BOWEL"])):
280                             action_db["actor_drop"](t)
281                     if t["T_BLADDER"] > 16:
282                         if 0 == (rand.next() % (33 - t["T_BLADDER"])):
283                             action_db["actor_pee"](t)
284                     t["T_STOMACH"] -= 1
285                     t["T_BOWEL"] += 1
286                     t["T_KIDNEY"] -= 1
287                     t["T_BLADDER"] += 1
288                     if t["T_STOMACH"] == 0:
289                         world_db["die"](t, "You DIE of hunger.")
290                     elif t["T_KIDNEY"] == 0:
291                         world_db["die"](t, "You DIE of dehydration.")
292         water = 0
293         positions_to_wet = []
294         for pos in range(world_db["MAP_LENGTH"] ** 2):
295             if world_db["MAP"][pos] == ord("0") \
296                     and world_db["wetmap"][pos] < ord("5"):
297                 positions_to_wet += [pos]
298         i_positions_to_wet = len(positions_to_wet)
299         for pos in range(world_db["MAP_LENGTH"] ** 2):
300             wetness = world_db["wetmap"][pos] - ord("0")
301             height = world_db["MAP"][pos] - ord("0")
302             if height == 0 and wetness > 0 \
303                     and 0 == rand.next() % ((2 ** 13) / (2 ** wetness)):
304                 world_db["MAP"][pos] = ord("-")
305                 if pos in positions_to_wet:
306                     positions_to_wet.remove(pos)
307                     i_positions_to_wet -= 1
308             if ((wetness > 0 and height != 0) or wetness > 1) \
309                 and 0 == rand.next() % 5:
310                 world_db["wetmap"][pos] -= 1
311                 water += 1
312                 i_positions_to_wet -= 1
313             if i_positions_to_wet == 0:
314                 break
315         if water > 0:
316             while water > 0:
317                 select = rand.next() % len(positions_to_wet)
318                 pos = positions_to_wet[select]
319                 world_db["wetmap"][pos] += 1
320                 positions_to_wet.remove(pos)
321                 water -= 1
322         world_db["TURN"] += 1
323         io_db["worldstate_updateable"] = True
324         try_worldstate_update()
325 world_db["turn_over"] = turn_over
326
327
328 def command_ai():
329     """Call ai() on player Thing, then turn_over()."""
330     from server.ai import ai
331     if world_db["WORLD_ACTIVE"]:
332         ai(world_db["Things"][0])
333         world_db["turn_over"]()
334
335
336 def set_command(action):
337     """Set player's T_COMMAND, then call turn_over()."""
338     tid = [x for x in world_db["ThingActions"]
339            if world_db["ThingActions"][x]["TA_NAME"] == action][0]
340     world_db["Things"][0]["T_COMMAND"] = tid
341     world_db["turn_over"]()
342 world_db["set_command"] = set_command
343
344
345 def play_wait():
346     """Try "wait" as player's T_COMMAND."""
347     if world_db["WORLD_ACTIVE"]:
348         world_db["set_command"]("wait")
349
350
351 def save_wetmap():
352     length = world_db["MAP_LENGTH"]
353     string = ""
354     for i in range(length):
355         line = world_db["wetmap"][i * length:(i * length) + length].decode()
356         string = string + "WETMAP" + " "  + str(i) + " " + line + "\n"
357     return string
358
359
360 def wetmapset(str_int, mapline):
361     def valid_map_line(str_int, mapline):
362         from server.utils import integer_test
363         val = integer_test(str_int, 0, 255)
364         if None != val:
365             if val >= world_db["MAP_LENGTH"]:
366                 print("Illegal value for map line number.")
367             elif len(mapline) != world_db["MAP_LENGTH"]:
368                 print("Map line length is unequal map width.")
369             else:
370                 return val
371         return None
372     val = valid_map_line(str_int, mapline)
373     if None != val:
374         length = world_db["MAP_LENGTH"]
375         if not world_db["wetmap"]:
376             m = bytearray(b' ' * (length ** 2))
377         else:
378             m = world_db["wetmap"]
379         m[val * length:(val * length) + length] = mapline.encode()
380         if not world_db["wetmap"]:
381             world_db["wetmap"] = m
382
383 def write_wetmap():
384     from server.worldstate_write_helpers import write_map
385     length = world_db["MAP_LENGTH"]
386     visible_wetmap = bytearray(b' ' * (length ** 2))
387     for i in range(length ** 2):
388         if world_db["Things"][0]["fovmap"][i] == ord('v'):
389             visible_wetmap[i] = world_db["wetmap"][i]
390     return write_map(visible_wetmap, world_db["MAP_LENGTH"])
391
392
393 from server.config.io import io_db
394 io_db["worldstate_write_order"] += [["T_STOMACH", "player_int"]]
395 io_db["worldstate_write_order"] += [["T_KIDNEY", "player_int"]]
396 io_db["worldstate_write_order"] += [["T_BOWEL", "player_int"]]
397 io_db["worldstate_write_order"] += [["T_BLADDER", "player_int"]]
398 io_db["worldstate_write_order"] += [[write_wetmap, "func"]]
399 import server.config.world_data
400 server.config.world_data.symbols_hide = "345"
401 server.config.world_data.symbols_passable = "012-"
402 server.config.world_data.thing_defaults["T_STOMACH"] = 16
403 server.config.world_data.thing_defaults["T_BOWEL"] = 0
404 server.config.world_data.thing_defaults["T_KIDNEY"] = 16
405 server.config.world_data.thing_defaults["T_BLADDER"] = 0
406 world_db["wetmap"] = bytearray(b"0" * world_db["MAP_LENGTH"] ** 2)
407 io_db["hook_save"] = save_wetmap
408 import server.config.make_world_helpers
409 server.config.make_world_helpers.make_map = make_map
410 from server.config.commands import commands_db
411 commands_db["THINGS_HERE"] = (2, True, lambda x, y: None)
412 commands_db["ai"] = (0, False, command_ai)
413 commands_db["move"] = (1, False, play_move)
414 commands_db["eat"] = (1, False, play_move)
415 commands_db["wait"] = (0, False, play_wait)
416 commands_db["drop"] = (0, False, play_drop)
417 commands_db["drink"] = (0, False, play_drink)
418 commands_db["pee"] = (0, False, play_pee)
419 commands_db["use"] = (1, False, lambda x: None)
420 commands_db["pickup"] = (0, False, lambda: None)
421 commands_db["T_STOMACH"] = (1, False, setter("Thing", "T_BOWEL", 0, 255))
422 commands_db["T_KIDNEY"] = (1, False, setter("Thing", "T_BLADDER", 0, 255))
423 commands_db["T_BOWEL"] = (1, False, setter("Thing", "T_BOWEL", 0, 255))
424 commands_db["T_BLADDER"] = (1, False, setter("Thing", "T_BLADDER", 0, 255))
425 commands_db["WETMAP"] = (2, False, wetmapset)
426 from server.actions import actor_wait
427 import server.config.actions
428 server.config.actions.action_db = {
429     "actor_wait": actor_wait,
430     "actor_move": actor_move,
431     "actor_drop": actor_drop,
432     "actor_drink": actor_drink,
433     "actor_pee": actor_pee,
434     "actor_eat": actor_eat,
435 }
436
437 strong_write(io_db["file_out"], "PLUGIN TheCrawlingEater\n")