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