home · contact · privacy
TCE: Add terrain movement cost.
[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
111
112 def make_map():
113     from server.make_map import new_pos, is_neighbor
114     from server.utils import rand
115     world_db["MAP"] = bytearray(b'X' * (world_db["MAP_LENGTH"] ** 2))
116     length = world_db["MAP_LENGTH"]
117     add_half_width = (not (length % 2)) * int(length / 2)
118     world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord("#")
119     while (1):
120         y, x, pos = new_pos()
121         if "X" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "#"):
122             if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
123                 break
124             world_db["MAP"][pos] = ord("#")
125     n_trees = int((length ** 2) / 16)
126     i_trees = 0
127     while (i_trees <= n_trees):
128         single_allowed = rand.next() % 32
129         y, x, pos = new_pos()
130         if "#" == chr(world_db["MAP"][pos]) \
131                 and ((not single_allowed) or is_neighbor((y, x), "_")):
132             world_db["MAP"][pos] = ord("_")
133             i_trees += 1
134
135
136 def calc_effort(ta, t):
137     from server.utils import mv_yx_in_dir_legal
138     if ta["TA_NAME"] == "move":
139         move_result = mv_yx_in_dir_legal(chr(t["T_ARGUMENT"]),
140                                          t["T_POSY"], t["T_POSX"])
141         if 1 == move_result[0]:
142             pos = (move_result[1] * world_db["MAP_LENGTH"]) + move_result[2]
143             terrain = chr(world_db["MAP"][pos])
144             if terrain == ".":
145                 return 2
146             elif terrain == ":":
147                 return 4
148     return 1
149 world_db["calc_effort"] = calc_effort
150
151
152 def turn_over():
153     from server.ai import ai
154     from server.config.actions import action_db
155     from server.update_map_memory import update_map_memory
156     from server.io import try_worldstate_update
157     from server.config.io import io_db
158     from server.utils import rand
159     while world_db["Things"][0]["T_LIFEPOINTS"]:
160         for tid in [tid for tid in world_db["Things"]]:
161             if not tid in world_db["Things"]:
162                 continue
163             Thing = world_db["Things"][tid]
164             if Thing["T_LIFEPOINTS"]:
165                 if not Thing["T_COMMAND"]:
166                     update_map_memory(Thing)
167                     if 0 == tid:
168                         return
169                     ai(Thing)
170                 if Thing["T_LIFEPOINTS"]:
171                     Thing["T_PROGRESS"] += 1
172                     taid = [a for a in world_db["ThingActions"]
173                               if a == Thing["T_COMMAND"]][0]
174                     ThingAction = world_db["ThingActions"][taid]
175                     effort = world_db["calc_effort"](ThingAction, Thing)
176                     if Thing["T_PROGRESS"] >= effort:
177                         action = action_db["actor_" + ThingAction["TA_NAME"]]
178                         action(Thing)
179                         Thing["T_COMMAND"] = 0
180                         Thing["T_PROGRESS"] = 0
181                     if Thing["T_STOMACH"] > 16:
182                         if 0 == (rand.next() % (33 - Thing["T_STOMACH"])):
183                             action_db["actor_drop"](Thing)
184         world_db["TURN"] += 1
185         io_db["worldstate_updateable"] = True
186         try_worldstate_update()
187 world_db["turn_over"] = turn_over
188
189
190 def command_ai():
191     """Call ai() on player Thing, then turn_over()."""
192     from server.ai import ai
193     if world_db["WORLD_ACTIVE"]:
194         ai(world_db["Things"][0])
195         world_db["turn_over"]()
196
197
198 def set_command(action):
199     """Set player's T_COMMAND, then call turn_over()."""
200     tid = [x for x in world_db["ThingActions"]
201            if world_db["ThingActions"][x]["TA_NAME"] == action][0]
202     world_db["Things"][0]["T_COMMAND"] = tid
203     world_db["turn_over"]()
204 world_db["set_command"] = set_command
205
206
207 def play_wait():
208     """Try "wait" as player's T_COMMAND."""
209     if world_db["WORLD_ACTIVE"]:
210         world_db["set_command"]("wait")
211
212
213 from server.config.io import io_db
214 io_db["worldstate_write_order"] += [["T_STOMACH", "player_int"]]
215 import server.config.world_data
216 server.config.world_data.symbols_hide = "%#X"
217 server.config.world_data.symbols_passable = "_.:"
218 server.config.world_data.thing_defaults["T_STOMACH"] = 0
219 import server.config.make_world_helpers
220 server.config.make_world_helpers.make_map = make_map
221 from server.config.commands import commands_db
222 commands_db["THINGS_HERE"] = (2, True, lambda x, y: None)
223 commands_db["ai"] = (0, False, command_ai)
224 commands_db["move"] = (1, False, play_move)
225 commands_db["wait"] = (0, False, play_wait)
226 commands_db["drop"] = (0, False, play_drop)
227 commands_db["use"] = (1, False, lambda x: None)
228 commands_db["pickup"] = (0, False, lambda: None)
229 commands_db["T_STOMACH"] = (1, False, setter("Thing", "T_STOMACH", 0, 255))
230 from server.actions import actor_wait
231 import server.config.actions
232 server.config.actions.action_db = {
233     "actor_wait": actor_wait,
234     "actor_move": actor_move,
235     "actor_drop": actor_drop
236 }
237
238 strong_write(io_db["file_out"], "PLUGIN TheCrawlingEater\n")