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.
6 from server.config.world_data import world_db
7 from server.io import log
8 from server.utils import rand, libpr
9 from server.utils import id_setter
12 def update_map_memory(t, age_map=True):
13 """Update t's T_MEMMAP with what's in its FOV now,age its T_MEMMEPTHMAP."""
14 from server.utils import c_pointer_to_bytearray
16 def age_some_memdepthmap_on_nonfov_cells():
17 # OUTSOURCED FOR PERFORMANCE REASONS TO libplomrogue.so:
21 # for pos in [pos for pos in range(world_db["MAP_LENGTH"] ** 2)
22 # if not ord_v == t["fovmap"][pos]
23 # if ord_0 <= t["T_MEMDEPTHMAP"][pos]
24 # if ord_9 > t["T_MEMDEPTHMAP"][pos]
25 # if not rand.next() % (2 **
26 # (t["T_MEMDEPTHMAP"][pos] - 48))]:
27 # t["T_MEMDEPTHMAP"][pos] += 1
28 memdepthmap = c_pointer_to_bytearray(t["T_MEMDEPTHMAP"])
29 fovmap = c_pointer_to_bytearray(t["fovmap"])
30 libpr.age_some_memdepthmap_on_nonfov_cells(memdepthmap, fovmap)
33 t["T_MEMMAP"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
34 if not t["T_MEMDEPTHMAP"]:
35 t["T_MEMDEPTHMAP"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
38 for pos in [pos for pos in range(world_db["MAP_LENGTH"] ** 2)
39 if ord_v == t["fovmap"][pos]]:
40 t["T_MEMDEPTHMAP"][pos] = ord_0
41 t["T_MEMMAP"][pos] = world_db["MAP"][pos]
43 age_some_memdepthmap_on_nonfov_cells()
44 t["T_MEMTHING"] = [mt for mt in t["T_MEMTHING"]
45 if ord_v != t["fovmap"][(mt[1] * world_db["MAP_LENGTH"])
47 for id in [id for id in world_db["Things"]
48 if not world_db["Things"][id]["carried"]]:
49 type = world_db["Things"][id]["T_TYPE"]
50 if not world_db["ThingTypes"][type]["TT_LIFEPOINTS"]:
51 y = world_db["Things"][id]["T_POSY"]
52 x = world_db["Things"][id]["T_POSX"]
53 if ord_v == t["fovmap"][(y * world_db["MAP_LENGTH"]) + x]:
54 t["T_MEMTHING"].append((type, y, x))
57 def decrement_lifepoints(t):
58 """Decrement t's lifepoints by 1, and if to zero, corpse it.
60 If t is the player avatar, only blank its fovmap, so that the client may
61 still display memory data. On non-player things, erase fovmap and memory.
62 Dying actors drop all their things.
64 t["T_LIFEPOINTS"] -= 1
65 if 0 == t["T_LIFEPOINTS"]:
66 for id in t["T_CARRIES"]:
67 t["T_CARRIES"].remove(id)
68 world_db["Things"][id]["T_POSY"] = t["T_POSY"]
69 world_db["Things"][id]["T_POSX"] = t["T_POSX"]
70 world_db["Things"][id]["carried"] = False
71 t["T_TYPE"] = world_db["ThingTypes"][t["T_TYPE"]]["TT_CORPSE_ID"]
72 if world_db["Things"][0] == t:
73 t["fovmap"] = bytearray(b' ' * (world_db["MAP_LENGTH"] ** 2))
75 log("See README on how to start over.")
79 t["T_MEMDEPTHMAP"] = False
84 """If t's HP < max, increment them if well-nourished, maybe waiting."""
85 if t["T_LIFEPOINTS"] < \
86 world_db["ThingTypes"][t["T_TYPE"]]["TT_LIFEPOINTS"]:
87 wait_id = [id for id in world_db["ThingActions"]
88 if world_db["ThingActions"][id]["TA_NAME"] == "wait"][0]
89 wait_divider = 8 if t["T_COMMAND"] == wait_id else 1
90 testval = int(abs(t["T_SATIATION"]) / wait_divider)
91 if (testval <= 1 or 1 == (rand.next() % testval)):
92 t["T_LIFEPOINTS"] += 1
93 if t == world_db["Things"][0]:
97 def hunger_per_turn(type_id):
98 """The amount of satiation score lost per turn for things of given type."""
100 return int(math.sqrt(world_db["ThingTypes"][type_id]["TT_LIFEPOINTS"]))
104 """Decrement t's satiation,dependent on it trigger lifepoint dec chance."""
105 if t["T_SATIATION"] > -32768:
106 t["T_SATIATION"] -= hunger_per_turn(t["T_TYPE"])
107 if 0 != t["T_SATIATION"] and 0 == int(rand.next() / abs(t["T_SATIATION"])):
108 if t == world_db["Things"][0]:
109 if t["T_SATIATION"] < 0:
110 log("You SUFFER from hunger.")
112 log("You SUFFER from over-eating.")
113 decrement_lifepoints(t)
116 def set_world_inactive():
117 """Set world_db["WORLD_ACTIVE"] to 0 and remove worldstate file."""
118 from server.io import safely_remove_worldstate_file
119 safely_remove_worldstate_file()
120 world_db["WORLD_ACTIVE"] = 0
123 def make_world(seed):
124 """(Re-)build game world, i.e. map, things, to a new turn 1 from seed.
126 Seed rand with seed. Do more only with a "wait" ThingAction and
127 world["PLAYER_TYPE"] matching ThingType of TT_START_NUMBER > 0. Then,
128 world_db["Things"] emptied, call make_map() and set
129 world_db["WORLD_ACTIVE"], world_db["TURN"] to 1. Build new Things
130 according to ThingTypes' TT_START_NUMBERS, with Thing of ID 0 to ThingType
131 of ID = world["PLAYER_TYPE"]. Place Things randomly, and actors not on each
132 other. Init player's memory map. Write "NEW_WORLD" line to out file.
134 from server.config.world_data import symbols_passable
135 from server.config.misc import make_map_func
140 err = "Space to put thing on too hard to find. Map too small?"
142 y = rand.next() % world_db["MAP_LENGTH"]
143 x = rand.next() % world_db["MAP_LENGTH"]
144 if chr(world_db["MAP"][y * world_db["MAP_LENGTH"] + x]) in \
149 raise SystemExit(err)
150 # Replica of C code, wrongly ignores animatedness of new Thing.
151 pos_clear = (0 == len([id for id in world_db["Things"]
152 if world_db["Things"][id]["T_LIFEPOINTS"]
153 if world_db["Things"][id]["T_POSY"] == y
154 if world_db["Things"][id]["T_POSX"] == x]))
160 if world_db["MAP_LENGTH"] < 1:
161 print("Ignoring: No map length >= 1 defined.")
163 libpr.set_maplength(world_db["MAP_LENGTH"])
164 player_will_be_generated = False
165 playertype = world_db["PLAYER_TYPE"]
166 for ThingType in world_db["ThingTypes"]:
167 if playertype == ThingType:
168 if 0 < world_db["ThingTypes"][ThingType]["TT_START_NUMBER"]:
169 player_will_be_generated = True
171 if not player_will_be_generated:
172 print("Ignoring: No player type with start number >0 defined.")
175 for ThingAction in world_db["ThingActions"]:
176 if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
179 print("Ignoring beyond SEED_MAP: " +
180 "No thing action with name 'wait' defined.")
182 world_db["Things"] = {}
184 world_db["WORLD_ACTIVE"] = 1
186 from server.new_thing import new_Thing
187 for i in range(world_db["ThingTypes"][playertype]["TT_START_NUMBER"]):
188 id = id_setter(-1, "Things")
189 world_db["Things"][id] = new_Thing(playertype, free_pos())
190 if not world_db["Things"][0]["fovmap"]:
191 empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
192 world_db["Things"][0]["fovmap"] = empty_fovmap
193 update_map_memory(world_db["Things"][0])
194 for type in world_db["ThingTypes"]:
195 for i in range(world_db["ThingTypes"][type]["TT_START_NUMBER"]):
196 if type != playertype:
197 id = id_setter(-1, "Things")
198 world_db["Things"][id] = new_Thing(type, free_pos())
199 from server.config.io import io_db
200 from server.io import strong_write
201 strong_write(io_db["file_out"], "NEW_WORLD\n")
205 """Run game world and its inhabitants until new player input expected."""
206 from server.config.actions import action_db, ai_func
207 from server.config.misc import thingproliferation_func
210 while world_db["Things"][0]["T_LIFEPOINTS"]:
211 proliferable_map = world_db["MAP"][:]
212 for id in [id for id in world_db["Things"]
213 if not world_db["Things"][id]["carried"]]:
214 y = world_db["Things"][id]["T_POSY"]
215 x = world_db["Things"][id]["T_POSX"]
216 proliferable_map[y * world_db["MAP_LENGTH"] + x] = ord('X')
217 for id in [id for id in world_db["Things"]]: # Only what's from start!
218 if not id in world_db["Things"] or \
219 world_db["Things"][id]["carried"]: # May have been consumed or
220 continue # picked up during turn …
221 Thing = world_db["Things"][id]
222 if Thing["T_LIFEPOINTS"]:
223 if not Thing["T_COMMAND"]:
224 update_map_memory(Thing)
231 if Thing["T_LIFEPOINTS"]:
232 Thing["T_PROGRESS"] += 1
233 taid = [a for a in world_db["ThingActions"]
234 if a == Thing["T_COMMAND"]][0]
235 ThingAction = world_db["ThingActions"][taid]
236 if Thing["T_PROGRESS"] == ThingAction["TA_EFFORT"]:
237 action = action_db["actor_" + ThingAction["TA_NAME"]]
239 Thing["T_COMMAND"] = 0
240 Thing["T_PROGRESS"] = 0
241 thingproliferation_func(Thing, proliferable_map)
244 world_db["TURN"] += 1