home · contact · privacy
a06f07cc64da2f9ba63ba30b24675f6b955aaeb8
[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                 log("You EAT.")
57                 world_db["Things"][0]["T_ARGUMENT"] = d
58                 world_db["set_command"]("move")
59                 return
60             if chr(world_db["MAP"][pos]) in symbols_passable:
61                 world_db["Things"][0]["T_ARGUMENT"] = d
62                 world_db["set_command"]("move")
63                 return
64         log("You CAN'T eat your way through there.")
65
66
67 def actor_move(t):
68     from server.build_fov_map import build_fov_map
69     from server.utils import mv_yx_in_dir_legal, rand
70     from server.config.world_data import directions_db, symbols_passable
71     passable = False
72     move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
73                                      t["T_POSY"], t["T_POSX"])
74     if 1 == move_result[0]:
75         pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
76         hitted = [tid for tid in world_db["Things"]
77                   if world_db["Things"][tid] != t
78                   if world_db["Things"][tid]["T_LIFEPOINTS"]
79                   if world_db["Things"][tid]["T_POSY"] == move_result[1]
80                   if world_db["Things"][tid]["T_POSX"] == move_result[2]]
81         if len(hitted):
82             hit_id = hitted[0]
83             hitted_tid = world_db["Things"][hit_id]["T_TYPE"]
84             if t == world_db["Things"][0]:
85                 hitted_name = world_db["ThingTypes"][hitted_tid]["TT_NAME"]
86                 log("You BUMP into " + hitted_name + ".")
87             elif 0 == hit_id:
88                 hitter_name = world_db["ThingTypes"][t["T_TYPE"]]["TT_NAME"]
89                 log(hitter_name +" BUMPS into you.")
90             return
91         passable = chr(world_db["MAP"][pos]) in symbols_passable
92     direction = [direction for direction in directions_db
93                  if directions_db[direction] == chr(t["T_ARGUMENT"])][0]
94     if passable:
95         t["T_POSY"] = move_result[1]
96         t["T_POSX"] = move_result[2]
97         t["pos"] = move_result[1] * world_db["MAP_LENGTH"] + move_result[2]
98         build_fov_map(t)
99     else:
100         if ord("%") == world_db["MAP"][pos] and 0 == int(rand.next() % 2):
101             world_db["MAP"][pos] = ord("_")
102             t["T_STOMACH"] += 1
103         if ord("#") == world_db["MAP"][pos] and 0 == int(rand.next() % 5):
104             world_db["MAP"][pos] = ord("_")
105             t["T_STOMACH"] += 2
106
107
108 def make_map():
109     from server.make_map import new_pos, is_neighbor
110     from server.utils import rand
111     world_db["MAP"] = bytearray(b'X' * (world_db["MAP_LENGTH"] ** 2))
112     length = world_db["MAP_LENGTH"]
113     add_half_width = (not (length % 2)) * int(length / 2)
114     world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord("#")
115     while (1):
116         y, x, pos = new_pos()
117         if "X" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "#"):
118             if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
119                 break
120             world_db["MAP"][pos] = ord("#")
121     n_trees = int((length ** 2) / 16)
122     i_trees = 0
123     while (i_trees <= n_trees):
124         single_allowed = rand.next() % 32
125         y, x, pos = new_pos()
126         if "#" == chr(world_db["MAP"][pos]) \
127                 and ((not single_allowed) or is_neighbor((y, x), "_")):
128             world_db["MAP"][pos] = ord("_")
129             i_trees += 1
130
131
132 def turn_over():
133     from server.ai import ai
134     from server.config.actions import action_db
135     from server.config.misc import calc_effort
136     from server.update_map_memory import update_map_memory
137     from server.io import try_worldstate_update
138     from server.config.io import io_db
139     while world_db["Things"][0]["T_LIFEPOINTS"]:
140         for tid in [tid for tid in world_db["Things"]]:
141             if not tid in world_db["Things"]:
142                 continue
143             Thing = world_db["Things"][tid]
144             if Thing["T_LIFEPOINTS"]:
145                 if not Thing["T_COMMAND"]:
146                     update_map_memory(Thing)
147                     if 0 == tid:
148                         return
149                     ai(Thing)
150                 if Thing["T_LIFEPOINTS"]:
151                     Thing["T_PROGRESS"] += 1
152                     taid = [a for a in world_db["ThingActions"]
153                               if a == Thing["T_COMMAND"]][0]
154                     ThingAction = world_db["ThingActions"][taid]
155                     effort = calc_effort(ThingAction, Thing)
156                     if Thing["T_PROGRESS"] == effort:
157                         action = action_db["actor_" + ThingAction["TA_NAME"]]
158                         action(Thing)
159                         Thing["T_COMMAND"] = 0
160                         Thing["T_PROGRESS"] = 0
161         world_db["TURN"] += 1
162         io_db["worldstate_updateable"] = True
163         try_worldstate_update()
164 world_db["turn_over"] = turn_over
165
166
167 def command_ai():
168     """Call ai() on player Thing, then turn_over()."""
169     from server.ai import ai
170     if world_db["WORLD_ACTIVE"]:
171         ai(world_db["Things"][0])
172         world_db["turn_over"]()
173
174
175 def set_command(action):
176     """Set player's T_COMMAND, then call turn_over()."""
177     tid = [x for x in world_db["ThingActions"]
178            if world_db["ThingActions"][x]["TA_NAME"] == action][0]
179     world_db["Things"][0]["T_COMMAND"] = tid
180     world_db["turn_over"]()
181 world_db["set_command"] = set_command
182
183
184 def play_wait():
185     """Try "wait" as player's T_COMMAND."""
186     if world_db["WORLD_ACTIVE"]:
187         world_db["set_command"]("wait")
188
189
190 from server.config.io import io_db
191 io_db["worldstate_write_order"] += [["T_STOMACH", "player_int"]]
192 import server.config.world_data
193 server.config.world_data.symbols_hide = "%#X"
194 server.config.world_data.symbols_passable = "_.:"
195 server.config.world_data.thing_defaults["T_STOMACH"] = 0
196 import server.config.make_world_helpers
197 server.config.make_world_helpers.make_map = make_map
198 from server.config.commands import commands_db
199 commands_db["THINGS_HERE"] = (2, True, lambda x, y: None)
200 commands_db["ai"] = (0, False, command_ai)
201 commands_db["move"] = (1, False, play_move)
202 commands_db["wait"] = (0, False, play_wait)
203 commands_db["drop"] = (0, False, play_drop)
204 commands_db["use"] = (1, False, lambda x: None)
205 commands_db["pickup"] = (0, False, lambda: None)
206 commands_db["T_STOMACH"] = (1, False, setter("Thing", "T_STOMACH", 0, 255))
207 from server.actions import actor_wait
208 import server.config.actions
209 server.config.actions.action_db = {
210     "actor_wait": actor_wait,
211     "actor_move": actor_move,
212     "actor_drop": actor_drop
213 }
214
215 strong_write(io_db["file_out"], "PLUGIN TheCrawlingEater\n")