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