1 from server.config.world_data import world_db
2 from server.io import log
3 from server.utils import rand, libpr, c_pointer_to_bytearray
4 from server.utils import id_setter
7 def thingproliferation(t, prol_map):
8 """To chance of 1/TT_PROLIFERATE, create t offspring in open neighbor cell.
10 Naturally only works with TT_PROLIFERATE > 0. The neighbor cell must be be
11 marked '.' in prol_map. If there are several map cell candidates, one is
14 from server.config.world_data import directions_db
15 from server.utils import mv_yx_in_dir_legal
16 prolscore = world_db["ThingTypes"][t["T_TYPE"]]["TT_PROLIFERATE"]
17 if prolscore and (1 == prolscore or 1 == (rand.next() % prolscore)):
19 for dir in [directions_db[key] for key in sorted(directions_db.keys())]:
20 mv_result = mv_yx_in_dir_legal(dir, t["T_POSY"], t["T_POSX"])
21 if mv_result[0] and ord('.') == prol_map[mv_result[1]
22 * world_db["MAP_LENGTH"]
24 candidates.append((mv_result[1], mv_result[2]))
26 i = rand.next() % len(candidates)
27 id = id_setter(-1, "Things")
28 newT = new_Thing(t["T_TYPE"], (candidates[i][0], candidates[i][1]))
29 world_db["Things"][id] = newT
32 def update_map_memory(t, age_map=True):
33 """Update t's T_MEMMAP with what's in its FOV now,age its T_MEMMEPTHMAP."""
35 def age_some_memdepthmap_on_nonfov_cells():
36 # OUTSOURCED FOR PERFORMANCE REASONS TO libplomrogue.so:
40 # for pos in [pos for pos in range(world_db["MAP_LENGTH"] ** 2)
41 # if not ord_v == t["fovmap"][pos]
42 # if ord_0 <= t["T_MEMDEPTHMAP"][pos]
43 # if ord_9 > t["T_MEMDEPTHMAP"][pos]
44 # if not rand.next() % (2 **
45 # (t["T_MEMDEPTHMAP"][pos] - 48))]:
46 # t["T_MEMDEPTHMAP"][pos] += 1
47 memdepthmap = c_pointer_to_bytearray(t["T_MEMDEPTHMAP"])
48 fovmap = c_pointer_to_bytearray(t["fovmap"])
49 libpr.age_some_memdepthmap_on_nonfov_cells(memdepthmap, fovmap)
52 t["T_MEMMAP"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
53 if not t["T_MEMDEPTHMAP"]:
54 t["T_MEMDEPTHMAP"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
57 for pos in [pos for pos in range(world_db["MAP_LENGTH"] ** 2)
58 if ord_v == t["fovmap"][pos]]:
59 t["T_MEMDEPTHMAP"][pos] = ord_0
60 t["T_MEMMAP"][pos] = world_db["MAP"][pos]
62 age_some_memdepthmap_on_nonfov_cells()
63 t["T_MEMTHING"] = [mt for mt in t["T_MEMTHING"]
64 if ord_v != t["fovmap"][(mt[1] * world_db["MAP_LENGTH"])
66 for id in [id for id in world_db["Things"]
67 if not world_db["Things"][id]["carried"]]:
68 type = world_db["Things"][id]["T_TYPE"]
69 if not world_db["ThingTypes"][type]["TT_LIFEPOINTS"]:
70 y = world_db["Things"][id]["T_POSY"]
71 x = world_db["Things"][id]["T_POSX"]
72 if ord_v == t["fovmap"][(y * world_db["MAP_LENGTH"]) + x]:
73 t["T_MEMTHING"].append((type, y, x))
77 """Build Thing's FOV map."""
78 t["fovmap"] = bytearray(b'v' * (world_db["MAP_LENGTH"] ** 2))
79 fovmap = c_pointer_to_bytearray(t["fovmap"])
80 map = c_pointer_to_bytearray(world_db["MAP"])
81 if libpr.build_fov_map(t["T_POSY"], t["T_POSX"], fovmap, map):
82 raise RuntimeError("Malloc error in build_fov_Map().")
85 def new_Thing(type, pos=(0, 0)):
86 """Return Thing of type T_TYPE, with fovmap if alive and world active."""
88 "T_LIFEPOINTS": world_db["ThingTypes"][type]["TT_LIFEPOINTS"],
100 "T_MEMDEPTHMAP": False,
103 if world_db["WORLD_ACTIVE"] and thing["T_LIFEPOINTS"]:
108 def decrement_lifepoints(t):
109 """Decrement t's lifepoints by 1, and if to zero, corpse it.
111 If t is the player avatar, only blank its fovmap, so that the client may
112 still display memory data. On non-player things, erase fovmap and memory.
113 Dying actors drop all their things.
115 t["T_LIFEPOINTS"] -= 1
116 if 0 == t["T_LIFEPOINTS"]:
117 for id in t["T_CARRIES"]:
118 t["T_CARRIES"].remove(id)
119 world_db["Things"][id]["T_POSY"] = t["T_POSY"]
120 world_db["Things"][id]["T_POSX"] = t["T_POSX"]
121 world_db["Things"][id]["carried"] = False
122 t["T_TYPE"] = world_db["ThingTypes"][t["T_TYPE"]]["TT_CORPSE_ID"]
123 if world_db["Things"][0] == t:
124 t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
126 log("See README on how to start over.")
129 t["T_MEMMAP"] = False
130 t["T_MEMDEPTHMAP"] = False
135 """If t's HP < max, increment them if well-nourished, maybe waiting."""
136 if t["T_LIFEPOINTS"] < \
137 world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]:
138 wait_id = [id for id in world_db["ThingActions"]
139 if world_db["ThingActions"][id]["TA_NAME"] == "wait"][0]
140 wait_divider = 8 if t["T_COMMAND"] == wait_id else 1
141 testval = int(abs(t["T_SATIATION"]) / wait_divider)
142 if (testval <= 1 or 1 == (rand.next() % testval)):
143 t["T_LIFEPOINTS"] += 1
144 if t == world_db["Things"][0]:
148 def hunger_per_turn(type_id):
149 """The amount of satiation score lost per turn for things of given type."""
151 return int(math.sqrt(world_db["ThingTypes"][type_id]["TT_LIFEPOINTS"]))
155 """Decrement t's satiation,dependent on it trigger lifepoint dec chance."""
156 if t["T_SATIATION"] > -32768:
157 t["T_SATIATION"] -= hunger_per_turn(t["T_TYPE"])
158 if 0 != t["T_SATIATION"] and 0 == int(rand.next() / abs(t["T_SATIATION"])):
159 if t == world_db["Things"][0]:
160 if t["T_SATIATION"] < 0:
161 log("You SUFFER from hunger.")
163 log("You SUFFER from over-eating.")
164 decrement_lifepoints(t)
167 def set_world_inactive():
168 """Set world_db["WORLD_ACTIVE"] to 0 and remove worldstate file."""
169 from server.io import safely_remove_worldstate_file
170 safely_remove_worldstate_file()
171 world_db["WORLD_ACTIVE"] = 0
175 """(Re-)make island map.
177 Let "~" represent water, "." land, "X" trees: Build island shape randomly,
178 start with one land cell in the middle, then go into cycle of repeatedly
179 selecting a random sea cell and transforming it into land if it is neighbor
180 to land. The cycle ends when a land cell is due to be created at the map's
181 border. Then put some trees on the map (TODO: more precise algorithm desc).
184 def is_neighbor(coordinates, type):
187 length = world_db["MAP_LENGTH"]
189 diag_west = x + (ind > 0)
190 diag_east = x + (ind < (length - 1))
191 pos = (y * length) + x
192 if (y > 0 and diag_east
193 and type == chr(world_db["MAP"][pos - length + ind])) \
195 and type == chr(world_db["MAP"][pos + 1])) \
196 or (y < (length - 1) and diag_east
197 and type == chr(world_db["MAP"][pos + length + ind])) \
198 or (y > 0 and diag_west
199 and type == chr(world_db["MAP"][pos - length - (not ind)])) \
201 and type == chr(world_db["MAP"][pos - 1])) \
202 or (y < (length - 1) and diag_west
203 and type == chr(world_db["MAP"][pos + length - (not ind)])):
207 world_db["MAP"] = bytearray(b'~' * (world_db["MAP_LENGTH"] ** 2))
208 length = world_db["MAP_LENGTH"]
209 add_half_width = (not (length % 2)) * int(length / 2)
210 world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord(".")
212 y = rand.next() % length
213 x = rand.next() % length
214 pos = (y * length) + x
215 if "~" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "."):
216 if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
218 world_db["MAP"][pos] = ord(".")
219 n_trees = int((length ** 2) / 16)
221 while (i_trees <= n_trees):
222 single_allowed = rand.next() % 32
223 y = rand.next() % length
224 x = rand.next() % length
225 pos = (y * length) + x
226 if "." == chr(world_db["MAP"][pos]) \
227 and ((not single_allowed) or is_neighbor((y, x), "X")):
228 world_db["MAP"][pos] = ord("X")
230 # This all-too-precise replica of the original C code misses iter_limit().
233 def make_world(seed):
234 """(Re-)build game world, i.e. map, things, to a new turn 1 from seed.
236 Seed rand with seed. Do more only with a "wait" ThingAction and
237 world["PLAYER_TYPE"] matching ThingType of TT_START_NUMBER > 0. Then,
238 world_db["Things"] emptied, call make_map() and set
239 world_db["WORLD_ACTIVE"], world_db["TURN"] to 1. Build new Things
240 according to ThingTypes' TT_START_NUMBERS, with Thing of ID 0 to ThingType
241 of ID = world["PLAYER_TYPE"]. Place Things randomly, and actors not on each
242 other. Init player's memory map. Write "NEW_WORLD" line to out file.
248 err = "Space to put thing on too hard to find. Map too small?"
250 y = rand.next() % world_db["MAP_LENGTH"]
251 x = rand.next() % world_db["MAP_LENGTH"]
252 if "." == chr(world_db["MAP"][y * world_db["MAP_LENGTH"] + x]):
256 raise SystemExit(err)
257 # Replica of C code, wrongly ignores animatedness of new Thing.
258 pos_clear = (0 == len([id for id in world_db["Things"]
259 if world_db["Things"][id]["T_LIFEPOINTS"]
260 if world_db["Things"][id]["T_POSY"] == y
261 if world_db["Things"][id]["T_POSX"] == x]))
267 if world_db["MAP_LENGTH"] < 1:
268 print("Ignoring: No map length >= 1 defined.")
270 libpr.set_maplength(world_db["MAP_LENGTH"])
271 player_will_be_generated = False
272 playertype = world_db["PLAYER_TYPE"]
273 for ThingType in world_db["ThingTypes"]:
274 if playertype == ThingType:
275 if 0 < world_db["ThingTypes"][ThingType]["TT_START_NUMBER"]:
276 player_will_be_generated = True
278 if not player_will_be_generated:
279 print("Ignoring: No player type with start number >0 defined.")
282 for ThingAction in world_db["ThingActions"]:
283 if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
286 print("Ignoring beyond SEED_MAP: " +
287 "No thing action with name 'wait' defined.")
289 world_db["Things"] = {}
291 world_db["WORLD_ACTIVE"] = 1
293 for i in range(world_db["ThingTypes"][playertype]["TT_START_NUMBER"]):
294 id = id_setter(-1, "Things")
295 world_db["Things"][id] = new_Thing(playertype, free_pos())
296 if not world_db["Things"][0]["fovmap"]:
297 empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
298 world_db["Things"][0]["fovmap"] = empty_fovmap
299 update_map_memory(world_db["Things"][0])
300 for type in world_db["ThingTypes"]:
301 for i in range(world_db["ThingTypes"][type]["TT_START_NUMBER"]):
302 if type != playertype:
303 id = id_setter(-1, "Things")
304 world_db["Things"][id] = new_Thing(type, free_pos())
305 from server.config.io import io_db
306 from server.io import strong_write
307 strong_write(io_db["file_out"], "NEW_WORLD\n")
311 """Run game world and its inhabitants until new player input expected."""
312 from server.config.actions import action_db, ai_func
315 while world_db["Things"][0]["T_LIFEPOINTS"]:
316 proliferable_map = world_db["MAP"][:]
317 for id in [id for id in world_db["Things"]
318 if not world_db["Things"][id]["carried"]]:
319 y = world_db["Things"][id]["T_POSY"]
320 x = world_db["Things"][id]["T_POSX"]
321 proliferable_map[y * world_db["MAP_LENGTH"] + x] = ord('X')
322 for id in [id for id in world_db["Things"]]: # Only what's from start!
323 if not id in world_db["Things"] or \
324 world_db["Things"][id]["carried"]: # May have been consumed or
325 continue # picked up during turn …
326 Thing = world_db["Things"][id]
327 if Thing["T_LIFEPOINTS"]:
328 if not Thing["T_COMMAND"]:
329 update_map_memory(Thing)
336 if Thing["T_LIFEPOINTS"]:
337 Thing["T_PROGRESS"] += 1
338 taid = [a for a in world_db["ThingActions"]
339 if a == Thing["T_COMMAND"]][0]
340 ThingAction = world_db["ThingActions"][taid]
341 if Thing["T_PROGRESS"] == ThingAction["TA_EFFORT"]:
342 action = action_db["actor_" + ThingAction["TA_NAME"]]
344 Thing["T_COMMAND"] = 0
345 Thing["T_PROGRESS"] = 0
346 thingproliferation(Thing, proliferable_map)
349 world_db["TURN"] += 1