home · contact · privacy
3bafef91fd3739618f94e60fc9dabf8aa0d1a599
[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_BLADDER"] >= 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_BLADDER"] < 32:
26         log("You DRINK.")
27         t["T_BLADDER"] += 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_BOWEL"] >= 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"]("move")
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_move(t):
100     from server.build_fov_map import build_fov_map
101     from server.utils import mv_yx_in_dir_legal, rand
102     from server.config.world_data import directions_db, symbols_passable
103     passable = False
104     move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
105                                      t["T_POSY"], t["T_POSX"])
106     if 1 == move_result[0]:
107         pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
108         hitted = [tid for tid in world_db["Things"]
109                   if world_db["Things"][tid] != t
110                   if world_db["Things"][tid]["T_LIFEPOINTS"]
111                   if world_db["Things"][tid]["T_POSY"] == move_result[1]
112                   if world_db["Things"][tid]["T_POSX"] == move_result[2]]
113         if len(hitted):
114             hit_id = hitted[0]
115             hitted_tid = world_db["Things"][hit_id]["T_TYPE"]
116             if t == world_db["Things"][0]:
117                 hitted_name = world_db["ThingTypes"][hitted_tid]["TT_NAME"]
118                 log("You BUMP into " + hitted_name + ".")
119             elif 0 == hit_id:
120                 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
121                 log(hitter_name +" BUMPS into you.")
122             return
123         passable = chr(world_db["MAP"][pos]) in symbols_passable
124     direction = [direction for direction in directions_db
125                  if directions_db[direction] == chr(t["T_ARGUMENT"])][0]
126     if passable:
127         t["T_POSY"] = move_result[1]
128         t["T_POSX"] = move_result[2]
129         t["pos"] = move_result[1] * world_db["MAP_LENGTH"] + move_result[2]
130         build_fov_map(t)
131     else:
132         height = world_db["MAP"][pos] - ord("0")
133         if t["T_BOWEL"] >= 32 or height == 5:
134             return
135         eaten = False
136         if height == 3 and 0 == int(rand.next() % 2):
137             t["T_BOWEL"] += height
138             eaten = True
139         elif height == 4 and 0 == int(rand.next() % 5):
140             t["T_BOWEL"] += height
141             eaten = True
142         log("You EAT.")
143         if eaten:
144             world_db["MAP"][pos] = ord("0")
145             if t["T_BOWEL"] > 32:
146                 t["T_BOWEL"] = 32
147
148
149 def test_hole(t):
150     if world_db["MAP"][t["pos"]] == ord("-"):
151         world_db["die"](t, "You FALL in a hole, and die.")
152         return False
153     return True
154 world_db["test_hole"] = test_hole
155
156
157 def test_air(t):
158     if (world_db["wetmap"][t["pos"]] - ord("0")) \
159             + (world_db["MAP"][t["pos"]] - ord("0")) > 5:
160         world_db["die"](t, "You SUFFOCATE")
161         return False
162     return True
163 world_db["test_air"] = test_air
164
165
166 def die(t, message):
167     t["T_LIFEPOINTS"] = 0
168     if t == world_db["Things"][0]:
169         t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
170         log(message)
171 world_db["die"] = die
172
173
174 def make_map():
175     from server.make_map import new_pos, is_neighbor
176     from server.utils import rand
177     world_db["MAP"] = bytearray(b'5' * (world_db["MAP_LENGTH"] ** 2))
178     length = world_db["MAP_LENGTH"]
179     add_half_width = (not (length % 2)) * int(length / 2)
180     world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord("4")
181     while (1):
182         y, x, pos = new_pos()
183         if "5" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "4"):
184             if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
185                 break
186             world_db["MAP"][pos] = ord("4")
187     n_ground = int((length ** 2) / 16)
188     i_ground = 0
189     while (i_ground <= n_ground):
190         single_allowed = rand.next() % 32
191         y, x, pos = new_pos()
192         if "4" == chr(world_db["MAP"][pos]) \
193                 and ((not single_allowed) or is_neighbor((y, x), "0")):
194             world_db["MAP"][pos] = ord("0")
195             i_ground += 1
196     n_water = int((length ** 2) / 16)
197     i_water = 0
198     while (i_water <= n_water):
199         y, x, pos = new_pos()
200         if ord("0") == world_db["MAP"][pos] and \
201                 ord("0") == world_db["wetmap"][pos]:
202             world_db["wetmap"][pos] = ord("3")
203             i_water += 1
204
205
206 def calc_effort(ta, t):
207     from server.utils import mv_yx_in_dir_legal
208     if ta["TA_NAME"] == "move":
209         move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
210                                          t["T_POSY"], t["T_POSX"])
211         if 1 == move_result[0]:
212             pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
213             narrowness = world_db["MAP"][pos] - ord("0")
214             return 2 ** narrowness
215     return 1
216 world_db["calc_effort"] = calc_effort
217
218
219 def turn_over():
220     from server.ai import ai
221     from server.config.actions import action_db
222     from server.update_map_memory import update_map_memory
223     from server.io import try_worldstate_update
224     from server.config.io import io_db
225     from server.utils import rand
226     while world_db["Things"][0]["T_LIFEPOINTS"]:
227         for tid in [tid for tid in world_db["Things"]]:
228             if not tid in world_db["Things"]:
229                 continue
230             t = world_db["Things"][tid]
231             if t["T_LIFEPOINTS"]:
232                 if not (world_db["test_air"](t) and world_db["test_hole"](t)):
233                     continue
234                 if not t["T_COMMAND"]:
235                     update_map_memory(t)
236                     if 0 == tid:
237                         return
238                     ai(t)
239                 if t["T_LIFEPOINTS"]:
240                     t["T_PROGRESS"] += 1
241                     taid = [a for a in world_db["ThingActions"]
242                               if a == t["T_COMMAND"]][0]
243                     ThingAction = world_db["ThingActions"][taid]
244                     effort = world_db["calc_effort"](ThingAction, t)
245                     if t["T_PROGRESS"] >= effort:
246                         action = action_db["actor_" + ThingAction["TA_NAME"]]
247                         action(t)
248                         t["T_COMMAND"] = 0
249                         t["T_PROGRESS"] = 0
250                     if t["T_BOWEL"] > 16:
251                         if 0 == (rand.next() % (33 - t["T_BOWEL"])):
252                             action_db["actor_drop"](t)
253                     if t["T_BLADDER"] > 16:
254                         if 0 == (rand.next() % (33 - t["T_BLADDER"])):
255                             action_db["actor_pee"](t)
256         water = 0
257         positions_to_wet = []
258         for pos in range(world_db["MAP_LENGTH"] ** 2):
259             if world_db["MAP"][pos] == ord("0") \
260                     and world_db["wetmap"][pos] < ord("5"):
261                 positions_to_wet += [pos]
262         i_positions_to_wet = len(positions_to_wet)
263         for pos in range(world_db["MAP_LENGTH"] ** 2):
264             wetness = world_db["wetmap"][pos] - ord("0")
265             height = world_db["MAP"][pos] - ord("0")
266             if height == 0 and wetness > 0 \
267                     and 0 == rand.next() % ((2 ** 12) / (2 ** wetness)):
268                 world_db["MAP"][pos] = ord("-")
269                 if pos in positions_to_wet:
270                     positions_to_wet.remove(pos)
271                     i_positions_to_wet -= 1
272             if ((wetness > 0 and height != 0) or wetness > 1) \
273                 and 0 == rand.next() % 5:
274                 world_db["wetmap"][pos] -= 1
275                 water += 1
276                 i_positions_to_wet -= 1
277             if i_positions_to_wet == 0:
278                 break
279         if water > 0:
280             while water > 0:
281                 select = rand.next() % len(positions_to_wet)
282                 pos = positions_to_wet[select]
283                 world_db["wetmap"][pos] += 1
284                 positions_to_wet.remove(pos)
285                 water -= 1
286         world_db["TURN"] += 1
287         io_db["worldstate_updateable"] = True
288         try_worldstate_update()
289 world_db["turn_over"] = turn_over
290
291
292 def command_ai():
293     """Call ai() on player Thing, then turn_over()."""
294     from server.ai import ai
295     if world_db["WORLD_ACTIVE"]:
296         ai(world_db["Things"][0])
297         world_db["turn_over"]()
298
299
300 def set_command(action):
301     """Set player's T_COMMAND, then call turn_over()."""
302     tid = [x for x in world_db["ThingActions"]
303            if world_db["ThingActions"][x]["TA_NAME"] == action][0]
304     world_db["Things"][0]["T_COMMAND"] = tid
305     world_db["turn_over"]()
306 world_db["set_command"] = set_command
307
308
309 def play_wait():
310     """Try "wait" as player's T_COMMAND."""
311     if world_db["WORLD_ACTIVE"]:
312         world_db["set_command"]("wait")
313
314
315 def save_wetmap():
316     length = world_db["MAP_LENGTH"]
317     string = ""
318     for i in range(length):
319         line = world_db["wetmap"][i * length:(i * length) + length].decode()
320         string = string + "WETMAP" + " "  + str(i) + " " + line + "\n"
321     return string
322
323
324 def wetmapset(str_int, mapline):
325     def valid_map_line(str_int, mapline):
326         from server.utils import integer_test
327         val = integer_test(str_int, 0, 255)
328         if None != val:
329             if val >= world_db["MAP_LENGTH"]:
330                 print("Illegal value for map line number.")
331             elif len(mapline) != world_db["MAP_LENGTH"]:
332                 print("Map line length is unequal map width.")
333             else:
334                 return val
335         return None
336     val = valid_map_line(str_int, mapline)
337     if None != val:
338         length = world_db["MAP_LENGTH"]
339         if not world_db["wetmap"]:
340             m = bytearray(b' ' * (length ** 2))
341         else:
342             m = world_db["wetmap"]
343         m[val * length:(val * length) + length] = mapline.encode()
344         if not world_db["wetmap"]:
345             world_db["wetmap"] = m
346
347 def write_wetmap():
348     from server.worldstate_write_helpers import write_map
349     length = world_db["MAP_LENGTH"]
350     visible_wetmap = bytearray(b' ' * (length ** 2))
351     for i in range(length ** 2):
352         if world_db["Things"][0]["fovmap"][i] == ord('v'):
353             visible_wetmap[i] = world_db["wetmap"][i]
354     return write_map(visible_wetmap, world_db["MAP_LENGTH"])
355
356
357 from server.config.io import io_db
358 io_db["worldstate_write_order"] += [["T_BOWEL", "player_int"]]
359 io_db["worldstate_write_order"] += [["T_BLADDER", "player_int"]]
360 io_db["worldstate_write_order"] += [[write_wetmap, "func"]]
361 import server.config.world_data
362 server.config.world_data.symbols_hide = "345"
363 server.config.world_data.symbols_passable = "012-"
364 server.config.world_data.thing_defaults["T_BOWEL"] = 0
365 server.config.world_data.thing_defaults["T_BLADDER"] = 0
366 world_db["wetmap"] = bytearray(b"0" * world_db["MAP_LENGTH"] ** 2)
367 io_db["hook_save"] = save_wetmap
368 import server.config.make_world_helpers
369 server.config.make_world_helpers.make_map = make_map
370 from server.config.commands import commands_db
371 commands_db["THINGS_HERE"] = (2, True, lambda x, y: None)
372 commands_db["ai"] = (0, False, command_ai)
373 commands_db["move"] = (1, False, play_move)
374 commands_db["wait"] = (0, False, play_wait)
375 commands_db["drop"] = (0, False, play_drop)
376 commands_db["drink"] = (0, False, play_drink)
377 commands_db["pee"] = (0, False, play_pee)
378 commands_db["use"] = (1, False, lambda x: None)
379 commands_db["pickup"] = (0, False, lambda: None)
380 commands_db["T_BOWEL"] = (1, False, setter("Thing", "T_BOWEL", 0, 255))
381 commands_db["T_BLADDER"] = (1, False, setter("Thing", "T_BLADDER", 0, 255))
382 commands_db["WETMAP"] = (2, False, wetmapset)
383 from server.actions import actor_wait
384 import server.config.actions
385 server.config.actions.action_db = {
386     "actor_wait": actor_wait,
387     "actor_move": actor_move,
388     "actor_drop": actor_drop,
389     "actor_drink": actor_drink,
390     "actor_pee": actor_pee,
391 }
392
393 strong_write(io_db["file_out"], "PLUGIN TheCrawlingEater\n")