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.
6 from server.config.world_data import world_db
7 from server.utils import rand
10 def is_neighbor(coordinates, type):
13 length = world_db["MAP_LENGTH"]
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])) \
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)])) \
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)])):
34 length = world_db["MAP_LENGTH"]
35 y = rand.next() % length
36 x = rand.next() % length
37 return y, x, (y * length) + x
40 """(Re-)make island map.
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).
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(".")
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):
57 world_db["MAP"][pos] = ord(".")
58 n_trees = int((length ** 2) / 16)
60 while (i_trees <= n_trees):
61 single_allowed = rand.next() % 32
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")