home · contact · privacy
Plugin: Add endgame.
[plomrogue] / server / make_map.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_map():
7     """(Re-)make island map.
8
9     Let "~" represent water, "." land, "X" trees: Build island shape randomly,
10     start with one land cell in the middle, then go into cycle of repeatedly
11     selecting a random sea cell and transforming it into land if it is neighbor
12     to land. The cycle ends when a land cell is due to be created at the map's
13     border. Then put some trees on the map (TODO: more precise algorithm desc).
14     """
15     from server.config.world_data import world_db
16     from server.utils import rand
17
18     def is_neighbor(coordinates, type):
19         y = coordinates[0]
20         x = coordinates[1]
21         length = world_db["MAP_LENGTH"]
22         ind = y % 2
23         diag_west = x + (ind > 0)
24         diag_east = x + (ind < (length - 1))
25         pos = (y * length) + x
26         if (y > 0 and diag_east
27             and type == chr(world_db["MAP"][pos - length + ind])) \
28            or (x < (length - 1)
29                and type == chr(world_db["MAP"][pos + 1])) \
30            or (y < (length - 1) and diag_east
31                and type == chr(world_db["MAP"][pos + length + ind])) \
32            or (y > 0 and diag_west
33                and type == chr(world_db["MAP"][pos - length - (not ind)])) \
34            or (x > 0
35                and type == chr(world_db["MAP"][pos - 1])) \
36            or (y < (length - 1) and diag_west
37                and type == chr(world_db["MAP"][pos + length - (not ind)])):
38             return True
39         return False
40
41     world_db["MAP"] = bytearray(b'~' * (world_db["MAP_LENGTH"] ** 2))
42     length = world_db["MAP_LENGTH"]
43     add_half_width = (not (length % 2)) * int(length / 2)
44     world_db["MAP"][int((length ** 2) / 2) + add_half_width] = ord(".")
45     while (1):
46         y = rand.next() % length
47         x = rand.next() % length
48         pos = (y * length) + x
49         if "~" == chr(world_db["MAP"][pos]) and is_neighbor((y, x), "."):
50             if y == 0 or y == (length - 1) or x == 0 or x == (length - 1):
51                 break
52             world_db["MAP"][pos] = ord(".")
53     n_trees = int((length ** 2) / 16)
54     i_trees = 0
55     while (i_trees <= n_trees):
56         single_allowed = rand.next() % 32
57         y = rand.next() % length
58         x = rand.next() % length
59         pos = (y * length) + x
60         if "." == chr(world_db["MAP"][pos]) \
61                 and ((not single_allowed) or is_neighbor((y, x), "X")):
62             world_db["MAP"][pos] = ord("X")
63             i_trees += 1
64     # This all-too-precise replica of the original C code misses iter_limit().