home · contact · privacy
Server, plugin: Abort command_worldactive checks as early as possible.
[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 def make_world(seed):
7     """(Re-)build game world, i.e. map, things, to a new turn 1 from seed.
8
9     Seed rand with seed. Do more only with a "wait" ThingAction and
10     world["PLAYER_TYPE"] matching ThingType of TT_START_NUMBER > 0. Then,
11     world_db["Things"] emptied, call make_map() and set
12     world_db["WORLD_ACTIVE"], world_db["TURN"] to 1. Build new Things
13     according to ThingTypes' TT_START_NUMBERS, with Thing of ID 0 to ThingType
14     of ID = world["PLAYER_TYPE"]. Place Things randomly, and actors not on each
15     other. Init player's memory map. Write "NEW_WORLD" line to out file.
16     """
17     from server.config.world_data import world_db, symbols_passable
18     from server.config.misc import make_map_func
19     from server.config.io import io_db
20     from server.utils import rand, libpr, id_setter
21     from server.new_thing import new_Thing
22     from server.io import strong_write
23     from server.update_map_memory import update_map_memory
24
25     def free_pos():
26         i = 0
27         while 1:
28             err = "Space to put thing on too hard to find. Map too small?"
29             while 1:
30                 y = rand.next() % world_db["MAP_LENGTH"]
31                 x = rand.next() % world_db["MAP_LENGTH"]
32                 if chr(world_db["MAP"][y * world_db["MAP_LENGTH"] + x]) in \
33                     symbols_passable:
34                     break
35                 i += 1
36                 if i == 65535:
37                     raise SystemExit(err)
38             # Replica of C code, wrongly ignores animatedness of new Thing.
39             pos_clear = (0 == len([id for id in world_db["Things"]
40                                    if world_db["Things"][id]["T_LIFEPOINTS"]
41                                    if world_db["Things"][id]["T_POSY"] == y
42                                    if world_db["Things"][id]["T_POSX"] == x]))
43             if pos_clear:
44                 break
45         return (y, x)
46
47     rand.seed = seed 
48     if world_db["MAP_LENGTH"] < 1:
49         print("Ignoring: No map length >= 1 defined.")
50         return
51     libpr.set_maplength(world_db["MAP_LENGTH"])
52     player_will_be_generated = False
53     playertype = world_db["PLAYER_TYPE"]
54     for ThingType in world_db["ThingTypes"]:
55         if playertype == ThingType:
56             if 0 < world_db["ThingTypes"][ThingType]["TT_START_NUMBER"]:
57                 player_will_be_generated = True
58             break
59     if not player_will_be_generated:
60         print("Ignoring: No player type with start number >0 defined.")
61         return
62     wait_action = False
63     for ThingAction in world_db["ThingActions"]:
64         if "wait" == world_db["ThingActions"][ThingAction]["TA_NAME"]:
65             wait_action = True
66     if not wait_action:
67         print("Ignoring beyond SEED_MAP: " +
68               "No thing action with name 'wait' defined.")
69         return
70     world_db["Things"] = {}
71     make_map_func()
72     world_db["WORLD_ACTIVE"] = 1
73     world_db["TURN"] = 1
74     for i in range(world_db["ThingTypes"][playertype]["TT_START_NUMBER"]):
75         id = id_setter(-1, "Things")
76         world_db["Things"][id] = new_Thing(playertype, free_pos())
77     if not world_db["Things"][0]["fovmap"]:
78         empty_fovmap = bytearray(b" " * world_db["MAP_LENGTH"] ** 2)
79         world_db["Things"][0]["fovmap"] = empty_fovmap
80     update_map_memory(world_db["Things"][0])
81     for type in world_db["ThingTypes"]:
82         for i in range(world_db["ThingTypes"][type]["TT_START_NUMBER"]):
83             if type != playertype:
84                 id = id_setter(-1, "Things")
85                 world_db["Things"][id] = new_Thing(type, free_pos())
86     strong_write(io_db["file_out"], "NEW_WORLD\n")