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