home · contact · privacy
Server: Force control for proper map length before world generation.
[plomrogue] / server / world.py
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
5
6
7 def thingproliferation(t, prol_map):
8     """To chance of 1/TT_PROLIFERATE, create t offspring in open neighbor cell.
9
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
12     selected randomly.
13     """
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)):
18         candidates = []
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"]
23                                                       + mv_result[2]]:
24                 candidates.append((mv_result[1], mv_result[2]))
25         if len(candidates):
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
30
31
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."""
34
35     def age_some_memdepthmap_on_nonfov_cells():
36         # OUTSOURCED FOR PERFORMANCE REASONS TO libplomrogue.so:
37         # ord_v = ord("v")
38         # ord_0 = ord("0")
39         # ord_9 = ord("9")
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)
50
51     if not t["T_MEMMAP"]:
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))
55     ord_v = ord("v")
56     ord_0 = ord("0")
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]
61     if age_map:
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"])
65                                                + mt[2]]]
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))
74
75
76 def build_fov_map(t):
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().")
83
84
85 def new_Thing(type, pos=(0, 0)):
86     """Return Thing of type T_TYPE, with fovmap if alive and world active."""
87     thing = {
88         "T_LIFEPOINTS": world_db["ThingTypes"][type]["TT_LIFEPOINTS"],
89         "T_ARGUMENT": 0,
90         "T_PROGRESS": 0,
91         "T_SATIATION": 0,
92         "T_COMMAND": 0,
93         "T_TYPE": type,
94         "T_POSY": pos[0],
95         "T_POSX": pos[1],
96         "T_CARRIES": [],
97         "carried": False,
98         "T_MEMTHING": [],
99         "T_MEMMAP": False,
100         "T_MEMDEPTHMAP": False,
101         "fovmap": False
102     }
103     if world_db["WORLD_ACTIVE"] and thing["T_LIFEPOINTS"]:
104         build_fov_map(thing)
105     return thing
106
107
108 def decrement_lifepoints(t):
109     """Decrement t's lifepoints by 1, and if to zero, corpse it.
110
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.
114     """
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))
125             log("You die.")
126             log("See README on how to start over.")
127         else:
128             t["fovmap"] = False
129             t["T_MEMMAP"] = False
130             t["T_MEMDEPTHMAP"] = False
131             t["T_MEMTHING"] = []
132
133
134 def try_healing(t):
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]:
145                 log("You HEAL.")
146
147
148 def hunger_per_turn(type_id):
149     """The amount of satiation score lost per turn for things of given type."""
150     import math
151     return int(math.sqrt(world_db["ThingTypes"][type_id]["TT_LIFEPOINTS"]))
152
153
154 def hunger(t):
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.")
162             else:
163                 log("You SUFFER from over-eating.")
164         decrement_lifepoints(t)
165
166
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
172
173
174 def make_map():
175     """(Re-)make island map.
176
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).
182     """
183
184     def is_neighbor(coordinates, type):
185         y = coordinates[0]
186         x = coordinates[1]
187         length = world_db["MAP_LENGTH"]
188         ind = y % 2
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])) \
194            or (x < (length - 1)
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)])) \
200            or (x > 0
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)])):
204             return True
205         return False
206
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(".")
211     while (1):
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):
217                 break
218             world_db["MAP"][pos] = ord(".")
219     n_trees = int((length ** 2) / 16)
220     i_trees = 0
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")
229             i_trees += 1
230     # This all-too-precise replica of the original C code misses iter_limit().
231
232
233 def make_world(seed):
234     """(Re-)build game world, i.e. map, things, to a new turn 1 from seed.
235
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.
243     """
244
245     def free_pos():
246         i = 0
247         while 1:
248             err = "Space to put thing on too hard to find. Map too small?"
249             while 1:
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]):
253                     break
254                 i += 1
255                 if i == 65535:
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]))
262             if pos_clear:
263                 break
264         return (y, x)
265
266     rand.seed = seed 
267     if world_db["MAP_LENGTH"] < 1:
268         print("Ignoring: No map length >= 1 defined.")
269         return
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
277             break
278     if not player_will_be_generated:
279         print("Ignoring: No player type with start number >0 defined.")
280         return
281     wait_action = False
282     for ThingAction in world_db["ThingActions"]:
283         if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
284             wait_action = True
285     if not wait_action:
286         print("Ignoring beyond SEED_MAP: " +
287               "No thing action with name 'wait' defined.")
288         return
289     world_db["Things"] = {}
290     make_map()
291     world_db["WORLD_ACTIVE"] = 1
292     world_db["TURN"] = 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")
308
309
310 def turn_over():
311     """Run game world and its inhabitants until new player input expected."""
312     from server.config.actions import action_db
313     from server.ai import ai
314     id = 0
315     whilebreaker = False
316     while world_db["Things"][0]["T_LIFEPOINTS"]:
317         proliferable_map = world_db["MAP"][:]
318         for id in [id for id in world_db["Things"]
319                    if not world_db["Things"][id]["carried"]]:
320             y = world_db["Things"][id]["T_POSY"]
321             x = world_db["Things"][id]["T_POSX"]
322             proliferable_map[y * world_db["MAP_LENGTH"] + x] = ord('X')
323         for id in [id for id in world_db["Things"]]:  # Only what's from start!
324             if not id in world_db["Things"] or \
325                world_db["Things"][id]["carried"]:   # May have been consumed or
326                 continue                            # picked up during turn …
327             Thing = world_db["Things"][id]
328             if Thing["T_LIFEPOINTS"]:
329                 if not Thing["T_COMMAND"]:
330                     update_map_memory(Thing)
331                     if 0 == id:
332                         whilebreaker = True
333                         break
334                     ai(Thing)
335                 try_healing(Thing)
336                 hunger(Thing)
337                 if Thing["T_LIFEPOINTS"]:
338                     Thing["T_PROGRESS"] += 1
339                     taid = [a for a in world_db["ThingActions"]
340                               if a == Thing["T_COMMAND"]][0]
341                     ThingAction = world_db["ThingActions"][taid]
342                     if Thing["T_PROGRESS"] == ThingAction["TA_EFFORT"]:
343                         action = action_db["actor_" + ThingAction["TA_NAME"]]
344                         action(Thing)
345                         #eval("actor_" + ThingAction["TA_NAME"])(Thing)
346                         Thing["T_COMMAND"] = 0
347                         Thing["T_PROGRESS"] = 0
348             thingproliferation(Thing, proliferable_map)
349         if whilebreaker:
350             break
351         world_db["TURN"] += 1