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.
6 from server.config.world_data import world_db
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.")
16 elif world_db["Things"][0]["T_BLADDER"] >= 32:
17 log("You're too FULL to drink more.")
19 world_db["set_command"]("drink")
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:
28 world_db["wetmap"][pos] -= 1
29 if world_db["wetmap"][pos] == ord("0"):
30 world_db["MAP"][pos] = ord("0")
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.")
38 world_db["set_command"]("pee")
42 if t["T_BLADDER"] < 1:
44 if t == world_db["Things"][0]:
45 log("You LOSE fluid.")
46 if not world_db["catch_air"](t):
49 world_db["wetmap"][t["pos"]] += 1
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.")
57 world_db["set_command"]("drop")
63 if t == world_db["Things"][0]:
64 log("You DROP waste.")
65 if not world_db["catch_air"](t):
67 world_db["MAP"][t["pos"]] += 1
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.")
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.")
89 world_db["Things"][0]["T_ARGUMENT"] = d
90 world_db["set_command"]("move")
92 if chr(world_db["MAP"][pos]) in symbols_passable:
93 world_db["Things"][0]["T_ARGUMENT"] = d
94 world_db["set_command"]("move")
96 log("You CAN'T eat your way through there.")
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
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]]
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 + ".")
120 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
121 log(hitter_name +" BUMPS into you.")
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]
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]
132 if t["T_BOWEL"] >= 32 or chr(world_db["MAP"][pos]) == "5":
135 if chr(world_db["MAP"][pos]) == "3" and 0 == int(rand.next() % 2):
138 elif chr(world_db["MAP"][pos]) == "4" and 0 == int(rand.next() % 5):
143 world_db["MAP"][pos] = ord("0")
144 if t["T_BOWEL"] > 32:
150 if (world_db["wetmap"][t["pos"]] - ord("0")) \
151 + (world_db["MAP"][t["pos"]] - ord("0")) > 4:
152 t["T_LIFEPOINTS"] = 0
153 if t == world_db["Things"][0]:
154 t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
155 log("You SUFFOCATE.")
158 world_db["catch_air"] = catch_air
162 from server.make_map import new_pos, is_neighbor
163 from server.utils import rand
164 world_db["MAP"] = bytearray(b'5' * (world_db["MAP_LENGTH"] ** 2))
165 length = world_db["MAP_LENGTH"]
166 add_half_width = (not (length % 2)) * int(length / 2)
167 world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord("4")
169 y, x, pos = new_pos()
170 if "5" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "4"):
171 if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
173 world_db["MAP"][pos] = ord("4")
174 n_ground = int((length ** 2) / 16)
176 while (i_ground <= n_ground):
177 single_allowed = rand.next() % 32
178 y, x, pos = new_pos()
179 if "4" == chr(world_db["MAP"][pos]) \
180 and ((not single_allowed) or is_neighbor((y, x), "0")):
181 world_db["MAP"][pos] = ord("0")
183 n_water = int((length ** 2) / 64)
185 while (i_water <= n_water):
186 y, x, pos = new_pos()
187 if ord("0") == world_db["MAP"][pos] and \
188 ord("0") == world_db["wetmap"][pos]:
189 world_db["wetmap"][pos] = ord("3")
193 def calc_effort(ta, t):
194 from server.utils import mv_yx_in_dir_legal
195 if ta["TA_NAME"] == "move":
196 move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
197 t["T_POSY"], t["T_POSX"])
198 if 1 == move_result[0]:
199 pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
200 terrain = chr(world_db["MAP"][pos])
206 world_db["calc_effort"] = calc_effort
210 from server.ai import ai
211 from server.config.actions import action_db
212 from server.update_map_memory import update_map_memory
213 from server.io import try_worldstate_update
214 from server.config.io import io_db
215 from server.utils import rand
216 while world_db["Things"][0]["T_LIFEPOINTS"]:
217 for tid in [tid for tid in world_db["Things"]]:
218 if not tid in world_db["Things"]:
220 Thing = world_db["Things"][tid]
221 if Thing["T_LIFEPOINTS"]:
222 if not Thing["T_COMMAND"]:
223 update_map_memory(Thing)
227 if Thing["T_LIFEPOINTS"]:
228 Thing["T_PROGRESS"] += 1
229 taid = [a for a in world_db["ThingActions"]
230 if a == Thing["T_COMMAND"]][0]
231 ThingAction = world_db["ThingActions"][taid]
232 effort = world_db["calc_effort"](ThingAction, Thing)
233 if Thing["T_PROGRESS"] >= effort:
234 action = action_db["actor_" + ThingAction["TA_NAME"]]
236 Thing["T_COMMAND"] = 0
237 Thing["T_PROGRESS"] = 0
238 if Thing["T_BOWEL"] > 16:
239 if 0 == (rand.next() % (33 - Thing["T_BOWEL"])):
240 action_db["actor_drop"](Thing)
241 if Thing["T_BLADDER"] > 16:
242 if 0 == (rand.next() % (33 - Thing["T_BLADDER"])):
243 action_db["actor_pee"](Thing)
245 positions_to_wet = []
246 for i in range(world_db["MAP_LENGTH"] ** 2):
247 if world_db["MAP"][i] == ord("0") \
248 and world_db["wetmap"][i] < ord("5"):
249 positions_to_wet += [i]
250 i_positions_to_wet = len(positions_to_wet)
251 for pos in range(world_db["MAP_LENGTH"] ** 2):
252 if 0 == rand.next() % 5 \
253 and ((world_db["wetmap"][pos] > ord("0")
254 and not world_db["MAP"][pos] == ord("0"))
255 or world_db["wetmap"][pos] > ord("1")):
256 world_db["wetmap"][pos] -= 1
258 i_positions_to_wet -= 1
259 if i_positions_to_wet == 0:
263 select = rand.next() % len(positions_to_wet)
264 pos = positions_to_wet[select]
265 world_db["wetmap"][pos] += 1
266 positions_to_wet.remove(pos)
268 world_db["TURN"] += 1
269 io_db["worldstate_updateable"] = True
270 try_worldstate_update()
271 world_db["turn_over"] = turn_over
275 """Call ai() on player Thing, then turn_over()."""
276 from server.ai import ai
277 if world_db["WORLD_ACTIVE"]:
278 ai(world_db["Things"][0])
279 world_db["turn_over"]()
282 def set_command(action):
283 """Set player's T_COMMAND, then call turn_over()."""
284 tid = [x for x in world_db["ThingActions"]
285 if world_db["ThingActions"][x]["TA_NAME"] == action][0]
286 world_db["Things"][0]["T_COMMAND"] = tid
287 world_db["turn_over"]()
288 world_db["set_command"] = set_command
292 """Try "wait" as player's T_COMMAND."""
293 if world_db["WORLD_ACTIVE"]:
294 world_db["set_command"]("wait")
298 length = world_db["MAP_LENGTH"]
300 for i in range(length):
301 line = world_db["wetmap"][i * length:(i * length) + length].decode()
302 string = string + "WETMAP" + " " + str(i) + " " + line + "\n"
306 def wetmapset(str_int, mapline):
307 def valid_map_line(str_int, mapline):
308 from server.utils import integer_test
309 val = integer_test(str_int, 0, 255)
311 if val >= world_db["MAP_LENGTH"]:
312 print("Illegal value for map line number.")
313 elif len(mapline) != world_db["MAP_LENGTH"]:
314 print("Map line length is unequal map width.")
318 val = valid_map_line(str_int, mapline)
320 length = world_db["MAP_LENGTH"]
321 if not world_db["wetmap"]:
322 m = bytearray(b' ' * (length ** 2))
324 m = world_db["wetmap"]
325 m[val * length:(val * length) + length] = mapline.encode()
326 if not world_db["wetmap"]:
327 world_db["wetmap"] = m
330 from server.worldstate_write_helpers import write_map
331 length = world_db["MAP_LENGTH"]
332 visible_wetmap = bytearray(b' ' * (length ** 2))
333 for i in range(length ** 2):
334 if world_db["Things"][0]["fovmap"][i] == ord('v'):
335 visible_wetmap[i] = world_db["wetmap"][i]
336 return write_map(visible_wetmap, world_db["MAP_LENGTH"])
339 from server.config.io import io_db
340 io_db["worldstate_write_order"] += [["T_BOWEL", "player_int"]]
341 io_db["worldstate_write_order"] += [["T_BLADDER", "player_int"]]
342 io_db["worldstate_write_order"] += [[write_wetmap, "func"]]
343 import server.config.world_data
344 server.config.world_data.symbols_hide = "345"
345 server.config.world_data.symbols_passable = "012"
346 server.config.world_data.thing_defaults["T_BOWEL"] = 0
347 server.config.world_data.thing_defaults["T_BLADDER"] = 0
348 world_db["wetmap"] = bytearray(b"0" * world_db["MAP_LENGTH"] ** 2)
349 io_db["hook_save"] = save_wetmap
350 import server.config.make_world_helpers
351 server.config.make_world_helpers.make_map = make_map
352 from server.config.commands import commands_db
353 commands_db["THINGS_HERE"] = (2, True, lambda x, y: None)
354 commands_db["ai"] = (0, False, command_ai)
355 commands_db["move"] = (1, False, play_move)
356 commands_db["wait"] = (0, False, play_wait)
357 commands_db["drop"] = (0, False, play_drop)
358 commands_db["drink"] = (0, False, play_drink)
359 commands_db["pee"] = (0, False, play_pee)
360 commands_db["use"] = (1, False, lambda x: None)
361 commands_db["pickup"] = (0, False, lambda: None)
362 commands_db["T_BOWEL"] = (1, False, setter("Thing", "T_BOWEL", 0, 255))
363 commands_db["T_BLADDER"] = (1, False, setter("Thing", "T_BLADDER", 0, 255))
364 commands_db["WETMAP"] = (2, False, wetmapset)
365 from server.actions import actor_wait
366 import server.config.actions
367 server.config.actions.action_db = {
368 "actor_wait": actor_wait,
369 "actor_move": actor_move,
370 "actor_drop": actor_drop,
371 "actor_drink": actor_drink,
372 "actor_pee": actor_pee,
375 strong_write(io_db["file_out"], "PLUGIN TheCrawlingEater\n")