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