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