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