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