-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtreePlacement.py
More file actions
212 lines (177 loc) · 7.4 KB
/
treePlacement.py
File metadata and controls
212 lines (177 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import math
import random
import os
import common
import cityPlanning
from logger import Logger
import sys
import numpy
from pymclevel import MCSchematic
from pymclevel.box import Vector
name = 'treePlacement'
logger = Logger(name)
# add gates in mapArr - needs to be 13 blocks away from edge
def reassignGate(mapArr):
for z in range(len(mapArr)):
for x in range(len(mapArr[z])):
if z < 13 or x < 13 or z > len(mapArr)-14 or x > len(mapArr[z])-14:
mapArr[z][x] = 1
return mapArr
# find all the possible tree positions
def findPosition(level, box):
try:
trees = []
start = [box.minx, box.minz]
# loops through the z-axis within the box selection and dividing it into 11 sections
for z in range(int(math.ceil((box.maxz-(box.minz))/11))):
tempStart = [start[0]-z, start[1]+(11*z)]
if z == 1:
bottomStart = trees[len(trees)-1]
# loops through the x-axis within the box selection and dividing it into 11 sections
for x in range(int(math.ceil((box.maxx-(box.minx-z))/11))):
position = [tempStart[0]+(11*x), tempStart[1]+x]
# checks if the position is within the box x-axis selection and box z-axis selection
if box.minx < position[0] < box.maxx and box.minz < position[1] < box.maxz:
trees.append(position)
else:
if position[0] < box.maxx and position[1] < box.maxz:
continue
else:
break
for z in range(int(math.ceil((box.maxz-(box.minz))/11))):
tempStart = [bottomStart[0]+z, bottomStart[1]-(11*z)]
if z == 0:
pass
for x in range(int(math.ceil((box.maxx-(box.minx-z))/11))):
position = [tempStart[0]-(11*x), tempStart[1]-x]
if box.minx < position[0] < box.maxx and box.minz < position[1] < box.maxz:
trees.append(position)
if position in trees:
trees.pop()
break
else:
if position[0] > box.minx and position[1] > box.minz:
continue
else:
break
for t in trees:
t[0] -= box.minx
t[1] -= box.minz
return trees
except Exception as e:
logger.error(e)
# checks the buildableAreaArray with the trees array
def compareTreePosition(pos, mapArr, afterHM):
try:
# checks the adjacent area around the tree position
adj = [(0, 0), (0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]
for newPos in adj:
z = pos[0] + newPos[0]
x = pos[1] + newPos[1]
if z < len(mapArr[0]) and x < len(mapArr) and z > 0 and x > 0:
# checks if it is water or lava
if mapArr[x][z] != 0 or afterHM[z][x] == -1 or afterHM[z][x] == -2:
return False
return True
except Exception as e:
logger.error(e)
# checks adjacent area of the 3x3 tree pos
def treePositions(trees, mapArr, afterHM):
try:
for idx, pos in reversed(list(enumerate(trees))):
adj = [(0, 0), (0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]
randPlot = random.randint(0, len(adj)-1)
z = pos[0] + adj[randPlot][0]
x = pos[1] + adj[randPlot][1]
stop = False
while compareTreePosition([z, x], mapArr, afterHM) == False:
adj.pop(randPlot)
if len(adj) == 0:
trees.pop(idx)
stop = True
break
randPlot = random.randint(0, len(adj)-1)
z = pos[0] + adj[randPlot][0]
x = pos[1] + adj[randPlot][1]
if stop is not True:
trees[idx] = [z, x]
return trees
except Exception as e:
logger.error(e)
# generates the trees in the postions
def generateTrees(heightMap, trees, level, box):
#loads the schematics
tree_type = '1'
filename = os.path.join(os.path.dirname(__file__), 'schematics', 'tree', 'tree_{}.schematic'.format(tree_type))
tree_1 = MCSchematic(shape=(7,10,8), filename=filename)
tree_type = '2'
filename = os.path.join(os.path.dirname(__file__), 'schematics', 'tree', 'tree_{}.schematic'.format(tree_type))
tree_2 = MCSchematic(shape=(5,6,5), filename=filename)
tree_type = '3'
filename = os.path.join(os.path.dirname(__file__), 'schematics', 'tree', 'tree_{}.schematic'.format(tree_type))
tree_3 = MCSchematic(shape=(7,10,7), filename=filename)
tree_type = '3'
filename = os.path.join(os.path.dirname(__file__), 'schematics', 'tree', 'tree_{}.schematic'.format(tree_type))
tree_4 = MCSchematic(shape=(6,6,6), filename=filename)
tree_type = '5'
filename = os.path.join(os.path.dirname(__file__), 'schematics', 'tree', 'tree_{}.schematic'.format(tree_type))
tree_5 = MCSchematic(shape=(8,9,8), filename=filename)
tree_type = '6'
filename = os.path.join(os.path.dirname(__file__), 'schematics', 'tree', 'tree_{}.schematic'.format(tree_type))
tree_6 = MCSchematic(shape=(8,10,8), filename=filename)
#loops through the available tree plots
for pos in trees:
x = pos[0] + box.minx
y = heightMap[pos[0]][pos[1]]
z = pos[1] + box.minz
random_tree = random.randint(1, 6)
if random_tree == 1:
level.copyBlocksFrom(tree_1, tree_1.bounds, Vector(x-3, y, z-4))
elif random_tree == 2:
level.copyBlocksFrom(tree_2, tree_2.bounds, Vector(x-2, y, z-2))
elif random_tree == 3:
level.copyBlocksFrom(tree_3, tree_3.bounds, Vector(x-3, y, z-3))
elif random_tree == 4:
level.copyBlocksFrom(tree_4, tree_4.bounds, Vector(x-2, y, z-3))
elif random_tree == 5:
level.copyBlocksFrom(tree_5, tree_5.bounds, Vector(x-4, y, z-3))
else:
level.copyBlocksFrom(tree_6, tree_6.bounds, Vector(x-4, y, z-3))
# creates tree map
def createTreeMap(box, trees):
try:
# create map of zeros
treeMap = []
for z in range(box.length):
row = []
for x in range(box.width):
row.append(0)
treeMap.append(row)
print(len(treeMap))
# loops through trees and adjacent blocks
for validTree in trees:
adj = [(0, 0), (0, -1), (0, 1), (-1, 0), (1, 0)]
for treeArea in adj:
z = validTree[0] + treeArea[0]
x = validTree[1] + treeArea[1]
if treeArea == (0, 0):
treeMap[x][z] = 2
else:
treeMap[x][z] = 1
return treeMap
except Exception as e:
logger.error(e)
# acts as controller
def treePlacement(level, box, mapArr, afterHM):
try:
logger.info('Generating trees...')
paddedMapArr = mapArr.copy()
trees = findPosition(level, box)
paddedMapArr = reassignGate(paddedMapArr)
# updates trees array
trees = treePositions(trees, paddedMapArr, afterHM)
generateTrees(afterHM, trees, level, box)
treeMap = createTreeMap(box, trees)
return treeMap
except Exception as e:
logger.error(e)