home · contact · privacy
TCE: Make sure stomach is never fuller than 32.
[plomrogue] / server / make_world.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, symbols_passable
7 from server.config.make_world_helpers import make_map, world_makable, pos_test
8 from server.config.io import io_db
9 from server.utils import rand, libpr, id_setter
10 from server.new_thing import new_Thing
11 from server.io import strong_write
12 from server.update_map_memory import update_map_memory
13
14
15 def make_world(seed):
16     """(Re-)build game world, i.e. map, things, to a new turn 1 from seed.
17
18     Seed rand with seed. Do more only with a "wait" ThingAction and
19     world["PLAYER_TYPE"] matching ThingType of TT_START_NUMBER > 0. Then,
20     world_db["Things"] emptied, call make_map() and set
21     world_db["WORLD_ACTIVE"], world_db["TURN"] to 1. Build new Things
22     according to ThingTypes' TT_START_NUMBERS, with Thing of ID 0 to ThingType
23     of ID = world["PLAYER_TYPE"]. Place Things randomly, and actors not on each
24     other. Init player's memory map. Write "NEW_WORLD" line to out file.
25     """
26     def free_pos(type):
27         i = 0
28         while 1:
29             err = "Space to put thing on too hard to find. Map too small?"
30             while 1:
31                 y = rand.next() % world_db["MAP_LENGTH"]
32                 x = rand.next() % world_db["MAP_LENGTH"]
33                 if chr(world_db["MAP"][y * world_db["MAP_LENGTH"] + x]) in \
34                     symbols_passable and pos_test(type, y, x):
35                     break
36                 i += 1
37                 if i == 65535:
38                     raise SystemExit(err)
39             # Replica of C code, wrongly ignores animatedness of new Thing.
40             pos_clear = (0 == len([id for id in world_db["Things"]
41                                    if world_db["Things"][id]["T_LIFEPOINTS"]
42                                    if world_db["Things"][id]["T_POSY"] == y
43                                    if world_db["Things"][id]["T_POSX"] == x]))
44             if pos_clear:
45                 break
46         return (y, x)
47
48     playertype = world_makable()
49     if playertype < 0:
50         return
51     rand.seed = seed
52     libpr.set_maplength(world_db["MAP_LENGTH"])
53     world_db["Things"] = {}
54     make_map()
55     world_db["WORLD_ACTIVE"] = 1
56     world_db["TURN"] = 1
57     for i in range(world_db["ThingTypes"][playertype]["TT_START_NUMBER"]):
58         id = id_setter(-1, "Things")
59         world_db["Things"][id] = new_Thing(playertype, free_pos(playertype))
60     if not world_db["Things"][0]["fovmap"]:
61         empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
62         world_db["Things"][0]["fovmap"] = empty_fovmap
63     update_map_memory(world_db["Things"][0])
64     for type in world_db["ThingTypes"]:
65         for i in range(world_db["ThingTypes"][type]["TT_START_NUMBER"]):
66             if type != playertype:
67                 id = id_setter(-1, "Things")
68                 world_db["Things"][id] = new_Thing(type, free_pos(type))
69     strong_write(io_db["file_out"], "NEW_WORLD\n")