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.
7 """(Re-)make island map.
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).
15 from server.config.world_data import world_db
16 from server.utils import rand
18 def is_neighbor(coordinates, type):
21 length = world_db["MAP_LENGTH"]
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])) \
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)])) \
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)])):
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(".")
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):
52 world_db["MAP"][pos] = ord(".")
53 n_trees = int((length ** 2) / 16)
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")
64 # This all-too-precise replica of the original C code misses iter_limit().