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