home · contact · privacy
7f8ca0be400d1725e72177b8887b1eb6726d74be
[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         t["T_STOMACH"] += 1
130         log("You EAT.")
131         eaten = (height == 3 and 0 == int(rand.next() % 2)) or \
132                 (height == 4 and 0 == int(rand.next() % 5))
133         if eaten:
134             world_db["MAP"][pos] = ord("0")
135             if t["T_STOMACH"] > 32:
136                 t["T_STOMACH"] = 32
137
138
139 def actor_move(t):
140     from server.build_fov_map import build_fov_map
141     from server.utils import mv_yx_in_dir_legal, rand
142     from server.config.world_data import symbols_passable
143     passable = False
144     move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
145                                      t["T_POSY"], t["T_POSX"])
146     if 1 == move_result[0]:
147         pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
148         #hitted = [tid for tid in world_db["Things"]
149         #          if world_db["Things"][tid] != t
150         #          if world_db["Things"][tid]["T_LIFEPOINTS"]
151         #          if world_db["Things"][tid]["T_POSY"] == move_result[1]
152         #          if world_db["Things"][tid]["T_POSX"] == move_result[2]]
153         #if len(hitted):
154         #    hit_id = hitted[0]
155         #    hitted_tid = world_db["Things"][hit_id]["T_TYPE"]
156         #    if t == world_db["Things"][0]:
157         #        hitted_name = world_db["ThingTypes"][hitted_tid]["TT_NAME"]
158         #        log("You BUMP into " + hitted_name + ".")
159         #    elif 0 == hit_id:
160         #        hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
161         #        log(hitter_name +" BUMPS into you.")
162         #    return
163         passable = chr(world_db["MAP"][pos]) in symbols_passable
164     if passable:
165         t["T_POSY"] = move_result[1]
166         t["T_POSX"] = move_result[2]
167         t["pos"] = move_result[1] * world_db["MAP_LENGTH"] + move_result[2]
168         build_fov_map(t)
169     else:
170         log("You try to MOVE there, but fail.")
171
172
173 def test_hole(t):
174     if world_db["MAP"][t["pos"]] == ord("-"):
175         world_db["die"](t, "You FALL in a hole, and die.")
176         return False
177     return True
178 world_db["test_hole"] = test_hole
179
180
181 def test_air(t):
182     if world_db["terrain_fullness"](t["pos"]) > 5:
183         world_db["die"](t, "You SUFFOCATE")
184         return False
185     return True
186 world_db["test_air"] = test_air
187
188
189 def die(t, message):
190     t["T_LIFEPOINTS"] = 0
191     if t == world_db["Things"][0]:
192         t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
193         t["T_MEMMAP"][t["pos"]] = ord("@")
194         log(message)
195 world_db["die"] = die
196
197
198 def make_map():
199     from server.make_map import new_pos, is_neighbor
200     from server.utils import rand
201     world_db["MAP"] = bytearray(b'5' * (world_db["MAP_LENGTH"] ** 2))
202     length = world_db["MAP_LENGTH"]
203     add_half_width = (not (length % 2)) * int(length / 2)
204     world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord("4")
205     while (1):
206         y, x, pos = new_pos()
207         if "5" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "4"):
208             if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
209                 break
210             world_db["MAP"][pos] = ord("4")
211     n_ground = int((length ** 2) / 16)
212     i_ground = 0
213     while (i_ground <= n_ground):
214         single_allowed = rand.next() % 32
215         y, x, pos = new_pos()
216         if "4" == chr(world_db["MAP"][pos]) \
217                 and ((not single_allowed) or is_neighbor((y, x), "0")):
218             world_db["MAP"][pos] = ord("0")
219             i_ground += 1
220     n_water = int((length ** 2) / 64)
221     i_water = 0
222     while (i_water <= n_water):
223         y, x, pos = new_pos()
224         if ord("0") == world_db["MAP"][pos] and \
225                 ord("0") == world_db["wetmap"][pos]:
226             world_db["wetmap"][pos] = ord("3")
227             i_water += 1
228
229
230 def calc_effort(ta, t):
231     from server.utils import mv_yx_in_dir_legal
232     if ta["TA_NAME"] == "move":
233         move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
234                                          t["T_POSY"], t["T_POSX"])
235         if 1 == move_result[0]:
236             pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
237             narrowness = world_db["MAP"][pos] - ord("0")
238             return 2 ** narrowness
239     return 1
240 world_db["calc_effort"] = calc_effort
241
242
243 def turn_over():
244     from server.ai import ai
245     from server.config.actions import action_db
246     from server.update_map_memory import update_map_memory
247     from server.io import try_worldstate_update
248     from server.config.io import io_db
249     from server.utils import rand
250     while world_db["Things"][0]["T_LIFEPOINTS"]:
251         for tid in [tid for tid in world_db["Things"]]:
252             if not tid in world_db["Things"]:
253                 continue
254             t = world_db["Things"][tid]
255             if t["T_LIFEPOINTS"]:
256                 if not (world_db["test_air"](t) and world_db["test_hole"](t)):
257                     continue
258                 if not t["T_COMMAND"]:
259                     update_map_memory(t)
260                     if 0 == tid:
261                         return
262                     world_db["ai"](t)
263                 if t["T_LIFEPOINTS"]:
264                     t["T_PROGRESS"] += 1
265                     taid = [a for a in world_db["ThingActions"]
266                               if a == t["T_COMMAND"]][0]
267                     ThingAction = world_db["ThingActions"][taid]
268                     effort = world_db["calc_effort"](ThingAction, t)
269                     if t["T_PROGRESS"] >= effort:
270                         action = action_db["actor_" + ThingAction["TA_NAME"]]
271                         action(t)
272                         t["T_COMMAND"] = 0
273                         t["T_PROGRESS"] = 0
274                     if t["T_BOWEL"] > 16:
275                         if 0 == (rand.next() % (33 - t["T_BOWEL"])):
276                             action_db["actor_drop"](t)
277                     if t["T_BLADDER"] > 16:
278                         if 0 == (rand.next() % (33 - t["T_BLADDER"])):
279                             action_db["actor_pee"](t)
280                     if 0 == world_db["TURN"] % 5:
281                         t["T_STOMACH"] -= 1
282                         t["T_BOWEL"] += 1
283                         t["T_KIDNEY"] -= 1
284                         t["T_BLADDER"] += 1
285                     if t["T_STOMACH"] <= 0:
286                         world_db["die"](t, "You DIE of hunger.")
287                     elif t["T_KIDNEY"] <= 0:
288                         world_db["die"](t, "You DIE of dehydration.")
289         water = 0
290         positions_to_wet = []
291         for pos in range(world_db["MAP_LENGTH"] ** 2):
292             if world_db["MAP"][pos] == ord("0") \
293                     and world_db["wetmap"][pos] < ord("5"):
294                 positions_to_wet += [pos]
295         i_positions_to_wet = len(positions_to_wet)
296         for pos in range(world_db["MAP_LENGTH"] ** 2):
297             wetness = world_db["wetmap"][pos] - ord("0")
298             height = world_db["MAP"][pos] - ord("0")
299             if height == 0 and wetness > 0 \
300                     and 0 == rand.next() % ((2 ** 13) / (2 ** wetness)):
301                 world_db["MAP"][pos] = ord("-")
302                 if pos in positions_to_wet:
303                     positions_to_wet.remove(pos)
304                     i_positions_to_wet -= 1
305             if ((wetness > 0 and height != 0) or wetness > 1) \
306                 and 0 == rand.next() % 5:
307                 world_db["wetmap"][pos] -= 1
308                 water += 1
309                 i_positions_to_wet -= 1
310             if i_positions_to_wet == 0:
311                 break
312         if water > 0:
313             while water > 0:
314                 select = rand.next() % len(positions_to_wet)
315                 pos = positions_to_wet[select]
316                 world_db["wetmap"][pos] += 1
317                 positions_to_wet.remove(pos)
318                 water -= 1
319         world_db["TURN"] += 1
320         io_db["worldstate_updateable"] = True
321         try_worldstate_update()
322 world_db["turn_over"] = turn_over
323
324
325 def command_ai():
326     """Call ai() on player Thing, then turn_over()."""
327     from server.ai import ai
328     if world_db["WORLD_ACTIVE"]:
329         ai(world_db["Things"][0])
330         world_db["turn_over"]()
331
332
333 def set_command(action):
334     """Set player's T_COMMAND, then call turn_over()."""
335     tid = [x for x in world_db["ThingActions"]
336            if world_db["ThingActions"][x]["TA_NAME"] == action][0]
337     world_db["Things"][0]["T_COMMAND"] = tid
338     world_db["turn_over"]()
339 world_db["set_command"] = set_command
340
341
342 def play_wait():
343     """Try "wait" as player's T_COMMAND."""
344     if world_db["WORLD_ACTIVE"]:
345         world_db["set_command"]("wait")
346
347
348 def save_wetmap():
349     length = world_db["MAP_LENGTH"]
350     string = ""
351     for i in range(length):
352         line = world_db["wetmap"][i * length:(i * length) + length].decode()
353         string = string + "WETMAP" + " "  + str(i) + " " + line + "\n"
354     return string
355
356
357 def wetmapset(str_int, mapline):
358     def valid_map_line(str_int, mapline):
359         from server.utils import integer_test
360         val = integer_test(str_int, 0, 255)
361         if None != val:
362             if val >= world_db["MAP_LENGTH"]:
363                 print("Illegal value for map line number.")
364             elif len(mapline) != world_db["MAP_LENGTH"]:
365                 print("Map line length is unequal map width.")
366             else:
367                 return val
368         return None
369     val = valid_map_line(str_int, mapline)
370     if None != val:
371         length = world_db["MAP_LENGTH"]
372         if not world_db["wetmap"]:
373             m = bytearray(b' ' * (length ** 2))
374         else:
375             m = world_db["wetmap"]
376         m[val * length:(val * length) + length] = mapline.encode()
377         if not world_db["wetmap"]:
378             world_db["wetmap"] = m
379
380 def write_wetmap():
381     from server.worldstate_write_helpers import write_map
382     length = world_db["MAP_LENGTH"]
383     visible_wetmap = bytearray(b' ' * (length ** 2))
384     for i in range(length ** 2):
385         if world_db["Things"][0]["fovmap"][i] == ord('v'):
386             visible_wetmap[i] = world_db["wetmap"][i]
387     return write_map(visible_wetmap, world_db["MAP_LENGTH"])
388
389
390 def command_ai():
391     if world_db["WORLD_ACTIVE"]:
392         world_db["ai"](world_db["Things"][0])
393         world_db["turn_over"]()
394
395
396 def get_dir_to_target(t, target):
397
398     from server.utils import rand, libpr, c_pointer_to_bytearray
399     from server.config.world_data import symbols_passable
400
401     def zero_score_map_where_char_on_memdepthmap(c):
402         map = c_pointer_to_bytearray(t["T_MEMDEPTHMAP"])
403         if libpr.zero_score_map_where_char_on_memdepthmap(c, map):
404             raise RuntimeError("No score map allocated for "
405                                "zero_score_map_where_char_on_memdepthmap().")
406
407     def set_map_score(pos, score):
408         test = libpr.set_map_score(pos, score)
409         if test:
410             raise RuntimeError("No score map allocated for set_map_score().")
411
412     def set_movement_cost_map():
413         memmap = c_pointer_to_bytearray(t["T_MEMMAP"])
414         if libpr.TCE_set_movement_cost_map(memmap):
415             raise RuntimeError("No movement cost map allocated for "
416                                "set_movement_cost_map().")
417
418     def seeing_thing():
419         def exists(gen):
420             try:
421                 next(gen)
422             except StopIteration:
423                 return False
424             return True
425         mapsize = world_db["MAP_LENGTH"] ** 2
426         if target == "food" and t["T_MEMMAP"]:
427             return exists(pos for pos in range(mapsize)
428                            if ord("2") < t["T_MEMMAP"][pos] < ord("5"))
429         elif target == "fluid_certain" and t["fovmap"]:
430             return exists(pos for pos in range(mapsize)
431                            if t["fovmap"] == ord("v")
432                            if world_db["MAP"][pos] == ord("0")
433                            if world_db["wetmap"][pos] > ord("0"))
434         elif target == "fluid_potential" and t["T_MEMMAP"] and t["fovmap"]:
435             return exists(pos for pos in range(mapsize)
436                            if t["T_MEMMAP"][pos] == ord("0")
437                            if t["fovmap"] != ord("v"))
438         elif target == "space" and t["T_MEMMAP"] and t["fovmap"]:
439             return exists(pos for pos in range(mapsize)
440                           if ord("0") <= t["T_MEMMAP"][pos] <= ord("2")
441                           if (t["fovmap"] != ord("v")
442                               or world_db["terrain_fullness"](pos) < 5))
443         return False
444
445     def init_score_map():
446         test = libpr.init_score_map()
447         set_movement_cost_map()
448         mapsize = world_db["MAP_LENGTH"] ** 2
449         if test:
450             raise RuntimeError("Malloc error in init_score_map().")
451         if target == "food" and t["T_MEMMAP"]:
452             [set_map_score(pos, 0) for pos in range(mapsize)
453              if ord("2") < t["T_MEMMAP"][pos] < ord("5")]
454         elif target == "fluid_certain" and t["fovmap"]:
455             [set_map_score(pos, 0) for pos in range(mapsize)
456              if t["fovmap"] == ord("v")
457              if world_db["MAP"][pos] == ord("0")
458              if world_db["wetmap"][pos] > ord("0")]
459         elif target == "fluid_potential" and t["T_MEMMAP"] and t["fovmap"]:
460             [set_map_score(pos, 0) for pos in range(mapsize)
461              if t["T_MEMMAP"][pos] == ord("0")
462              if t["fovmap"] != ord("v")]
463         elif target == "space" and t["T_MEMMAP"] and t["fovmap"]:
464             [set_map_score(pos, 0) for pos in range(mapsize)
465              if ord("0") <= t["T_MEMMAP"][pos] <= ord("2")
466              if (t["fovmap"] != ord("v")
467                  or world_db["terrain_fullness"](pos) < 5)]
468         elif target == "search":
469             zero_score_map_where_char_on_memdepthmap(mem_depth_c[0])
470
471     def rand_target_dir(neighbors, cmp, dirs):
472         candidates = []
473         n_candidates = 0
474         for i in range(len(dirs)):
475             if cmp == neighbors[i]:
476                 candidates.append(dirs[i])
477                 n_candidates += 1
478         return candidates[rand.next() % n_candidates] if n_candidates else 0
479
480     def get_neighbor_scores(dirs, eye_pos):
481         scores = []
482         if libpr.ready_neighbor_scores(eye_pos):
483             raise RuntimeError("No score map allocated for " +
484                                "ready_neighbor_scores.()")
485         for i in range(len(dirs)):
486             scores.append(libpr.get_neighbor_score(i))
487         return scores
488
489     def get_dir_from_neighbors():
490         import math
491         dir_to_target = False
492         dirs = "edcxsw"
493         eye_pos = t["pos"]
494         neighbors = get_neighbor_scores(dirs, eye_pos)
495         minmax_start = 65535 - 1
496         minmax_neighbor = minmax_start
497         for i in range(len(dirs)):
498             if minmax_neighbor > neighbors[i]:
499                 minmax_neighbor = neighbors[i]
500         if minmax_neighbor != minmax_start:
501             dir_to_target = rand_target_dir(neighbors, minmax_neighbor, dirs)
502         return dir_to_target, minmax_neighbor
503
504     dir_to_target = False
505     mem_depth_c = b' '
506     run_i = 9 + 1 if "search" == target else 1
507     minmax_neighbor = 0
508     while run_i and not dir_to_target and \
509             ("search" == target or seeing_thing()):
510         run_i -= 1
511         init_score_map()
512         mem_depth_c = b'9' if b' ' == mem_depth_c \
513             else bytes([mem_depth_c[0] - 1])
514         if libpr.TCE_dijkstra_map_with_movement_cost():
515             raise RuntimeError("No score map allocated for dijkstra_map().")
516         dir_to_target, minmax_neighbor = get_dir_from_neighbors()
517         libpr.free_score_map()
518         if dir_to_target and str == type(dir_to_target):
519             action = "move"
520             from server.utils import mv_yx_in_dir_legal
521             move_result = mv_yx_in_dir_legal(dir_to_target, t["T_POSY"],
522                                                             t["T_POSX"])
523             if 1 != move_result[0]:
524                 return False, 0
525             pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
526             if world_db["MAP"][pos] > ord("2"):
527                 action = "eat"
528             t["T_COMMAND"] = [taid for taid in world_db["ThingActions"]
529                               if world_db["ThingActions"][taid]["TA_NAME"]
530                               == action][0]
531             t["T_ARGUMENT"] = ord(dir_to_target)
532     return dir_to_target, minmax_neighbor
533 world_db["get_dir_to_target"] = get_dir_to_target
534
535
536 def terrain_fullness(pos):
537     return (world_db["MAP"][pos] - ord("0")) + \
538         (world_db["wetmap"][pos] - ord("0"))
539 world_db["terrain_fullness"] = terrain_fullness
540
541
542 def ai(t):
543
544     if t["T_LIFEPOINTS"] == 0:
545         return
546
547     def standing_on_fluid(t):
548         if world_db["MAP"][t["pos"]] == ord("0") and \
549             world_db["wetmap"][t["pos"]] > ord("0"):
550                 return True
551         else:
552             return False
553
554     def thing_action_id(name):
555         return [taid for taid in world_db["ThingActions"]
556                 if world_db["ThingActions"][taid]
557                 ["TA_NAME"] == name][0]
558
559     t["T_COMMAND"] = thing_action_id("wait")
560     needs = {
561         "safe_pee": (world_db["terrain_fullness"](t["pos"]) * t["T_BLADDER"]) / 4,
562         "safe_drop": (world_db["terrain_fullness"](t["pos"]) * t["T_BOWEL"]) / 4,
563         "food": 33 - t["T_STOMACH"],
564         "fluid_certain": 33 - t["T_KIDNEY"],
565         "fluid_potential": 32 - t["T_KIDNEY"],
566         "search": 1,
567     }
568     from operator import itemgetter
569     needs = sorted(needs.items(), key=itemgetter(1,0))
570     needs.reverse()
571     for need in needs:
572         if need[1] > 0:
573             if need[0] in {"fluid_certain", "fluid_potential"}:
574                 if standing_on_fluid(t):
575                     t["T_COMMAND"] = thing_action_id("drink")
576                     return
577                 elif t["T_BLADDER"] > 0 and \
578                          world_db["MAP"][t["pos"]] == ord("0"):
579                     t["T_COMMAND"] = thing_action_id("pee")
580                     return
581             elif need[0] in {"safe_pee", "safe_drop"}:
582                 action_name = need[0][len("safe_"):]
583                 if world_db["terrain_fullness"](t["pos"]) < 4:
584                     t["T_COMMAND"] = thing_action_id(action_name)
585                     return
586                 else:
587                     test = world_db["get_dir_to_target"](t, "space")
588                     if test[0]:
589                         if (not test[1] < 5) and \
590                                 world_db["terrain_fullness"](t["pos"]) < 5:
591                             t["T_COMMAND"] = thing_action_id(action_name)
592                         return
593                     if t["T_STOMACH"] < 32 and \
594                             world_db["get_dir_to_target"](t, "food")[0]:
595                         return
596                 continue
597             if world_db["get_dir_to_target"](t, need[0])[0]:
598                 return
599             elif t["T_STOMACH"] < 32 and \
600                     need[0] in {"fluid_certain", "fluid_potential"} and \
601                     world_db["get_dir_to_target"](t, "food")[0]:
602                 return
603 world_db["ai"] = ai
604
605
606 from server.config.io import io_db
607 io_db["worldstate_write_order"] += [["T_STOMACH", "player_int"]]
608 io_db["worldstate_write_order"] += [["T_KIDNEY", "player_int"]]
609 io_db["worldstate_write_order"] += [["T_BOWEL", "player_int"]]
610 io_db["worldstate_write_order"] += [["T_BLADDER", "player_int"]]
611 io_db["worldstate_write_order"] += [[write_wetmap, "func"]]
612 import server.config.world_data
613 server.config.world_data.symbols_hide = "345"
614 server.config.world_data.symbols_passable = "012-"
615 server.config.world_data.thing_defaults["T_STOMACH"] = 16
616 server.config.world_data.thing_defaults["T_BOWEL"] = 0
617 server.config.world_data.thing_defaults["T_KIDNEY"] = 16
618 server.config.world_data.thing_defaults["T_BLADDER"] = 0
619 world_db["wetmap"] = bytearray(b"0" * world_db["MAP_LENGTH"] ** 2)
620 io_db["hook_save"] = save_wetmap
621 import server.config.make_world_helpers
622 server.config.make_world_helpers.make_map = make_map
623 from server.config.commands import commands_db
624 commands_db["THINGS_HERE"] = (2, True, lambda x, y: None)
625 commands_db["ai"] = (0, False, command_ai)
626 commands_db["move"] = (1, False, play_move)
627 commands_db["eat"] = (1, False, play_move)
628 commands_db["wait"] = (0, False, play_wait)
629 commands_db["drop"] = (0, False, play_drop)
630 commands_db["drink"] = (0, False, play_drink)
631 commands_db["pee"] = (0, False, play_pee)
632 commands_db["use"] = (1, False, lambda x: None)
633 commands_db["pickup"] = (0, False, lambda: None)
634 commands_db["T_STOMACH"] = (1, False, setter("Thing", "T_STOMACH", 0, 255))
635 commands_db["T_KIDNEY"] = (1, False, setter("Thing", "T_KIDNEY", 0, 255))
636 commands_db["T_BOWEL"] = (1, False, setter("Thing", "T_BOWEL", 0, 255))
637 commands_db["T_BLADDER"] = (1, False, setter("Thing", "T_BLADDER", 0, 255))
638 commands_db["WETMAP"] = (2, False, wetmapset)
639 from server.actions import actor_wait
640 import server.config.actions
641 server.config.actions.action_db = {
642     "actor_wait": actor_wait,
643     "actor_move": actor_move,
644     "actor_drop": actor_drop,
645     "actor_drink": actor_drink,
646     "actor_pee": actor_pee,
647     "actor_eat": actor_eat,
648 }
649
650 strong_write(io_db["file_out"], "PLUGIN TheCrawlingEater\n")