-
Notifications
You must be signed in to change notification settings - Fork 0
/
hydraulicMapGenerator.py
253 lines (186 loc) · 8.44 KB
/
hydraulicMapGenerator.py
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
from util import *
from loader import *
import randomUtil as r
class HydraulicNode(MapGraphNode):
def __init__(self,pos,power,score, direction = None, isSource=False):
super().__init__(pos)
self.power = power
self.basePower = power
self.stagnant = False
self.streamInputs = []
self.streamOutputs = []
self.direction = direction
self.score = 0
self.length = 0
self.isSource = isSource
def getStreamOutputs(self):
return self.streamOutputs
def addStreamOutput(self,out,end=False):
self.streamOutputs.append(out)
if not end:
out.addStreamInput(self,True)
def getStreamInputs(self):
return self.streamInputs
def getParents(self):
ret = self.streamInputs[:]
for stream in self.streamInputs:
ret += stream.getParents()
return ret
def addStreamInput(self,inp,end=False):
if self.isSource:
self.isSource = False
self.addPower(-self.basePower)
self.addPower(inp.getPower())
if inp.length+1>self.length:
self.length = inp.length+1
self.streamInputs.append(inp)
if not end:
inp.addStreamOutput(self,True)
def getPower(self):
return self.power
def addPower(self,change):
self.power += change
for out in self.getStreamOutputs():
out.addPower(change)
def getDirection(self):
return self.direction
def setDirection(self,direction):
self.direction = direction
def generateMapGraphData(self,hydraulicObjects):
if self.stagnant:
if self.power<=3:
self.setObject(hydraulicObjects["Pond"].instantiate())
else:
self.setObject(hydraulicObjects["Lake"].instantiate())
else:
if self.power<=3:
self.setObject(hydraulicObjects["Stream"].instantiate())
else:
self.setObject(hydraulicObjects["River"].instantiate())
self.getObject().score = self.power
for node in self.getStreamOutputs():
node.addConnection(self)
def evaluateRiverTarget(fineBiome):
score = 0
for tag in fineBiome.getCollectiveTags():
if tag.getName()=="mountainous":
score += tag.getWeight()*1.5
if tag.getName()=="hilly":
score += tag.getWeight()
if tag.getName()=="water":
score -= tag.getWeight()
if tag.getName()=="flat":
score -= tag.getWeight()*0.5
return score
def generateHydraulicMap(mapSize, fineBiomes, sourceCellSize, sourceChance, iterationCount, smallStepLength, largeStepLength, mergeDistance):
fineBiomeNodes = fineBiomes.getNodes()
hydroGraph = MapGraph(0,mapSize)
streamsInProcess = []
#generate stream sources
usedFineBiomes = []
for x in range(sourceCellSize.a//2, mapSize.a, sourceCellSize.a):
for y in range(sourceCellSize.b//2, mapSize.b, sourceCellSize.b):
if r.random()<sourceChance:
source = fineBiomes.getClosest(Vector2(x,y))
if source in usedFineBiomes:
continue
tags = source.getCollectiveTags()
acceptable = False
for tag in tags:
if tag.getName()=="mountainous" and tag.getWeight()>=1:
acceptable = True
break
if tag.getName()=="hilly" and tag.getWeight()>=0.5:
acceptable = True
break
if tag.getName()=="forested" and tag.getWeight()>=1:
acceptable = True
break
if not acceptable:
continue
usedFineBiomes.append(source)
streamsInProcess.append(HydraulicNode(source.getPos(),1, evaluateRiverTarget(source), isSource=True))
#iterate stream extension
for i in range(iterationCount):
newStreams = []
for stream in streamsInProcess:
#try twice, once with large step, once with small step
stepLength = smallStepLength
for j in range(2):
#ignore streams outside map
posDiff = mapSize - stream.getPos()
if posDiff.a<0 or posDiff.b<0 or posDiff.a>mapSize.a or posDiff.b>mapSize.b:
break
#assign direction if not present
if not stream.getDirection():
angles = []
angle = 0
while angle<1:
angles.append(angle)
angle+=0.1
r.shuffle(angles)
minScore = 1000
for angle in angles:
direction = Vector2.UNIT.rotate(angle).floatMultiply(stepLength)
pos = direction + stream.pos
score = evaluateRiverTarget(fineBiomes.getClosest(pos))
if score<stream.score and score<minScore :
stream.setDirection(direction)
minScore = score
#generate possible next positions
possiblePos = []
angleStep = 0.02
angleRange = 0.2
angle = -angleRange
while angle <=angleRange:
possAngle = angle + r.random()*angleStep
possPos = Vector2.UNIT.rotate(possAngle).floatMultiply(stepLength) + stream.getPos()
possiblePos.append(possPos)
angle += angleStep
streamParents = stream.getParents()
riverScore = {}
for pos in possiblePos:
closest = fineBiomes.getClosest(pos)
riverScore[pos] = evaluateRiverTarget(closest)*(0.8+r.random()*0.4)
if closest in usedFineBiomes:
riverScore[pos] = 100
#get highest scoring positions
targetPos = max( possiblePos,key= lambda pos: stream.score-riverScore[pos] )
#merge streams if close enough
closestStream = hydroGraph.getClosest(targetPos)
if closestStream:
closestDist = abs(closestStream.getPos() - targetPos)
for nstream in newStreams:
if abs(nstream.getPos() - targetPos) < closestDist:
closestStream = nstream
closestDist = abs(nstream.getPos() - targetPos)
if (not closestStream in streamParents) and closestStream and (
abs(closestStream.getPos() - targetPos) < mergeDistance if not closestStream.stagnant else mergeDistance * max(1,min(1.7,1.7*closestStream.getPower()/5))):
closestStream.addStreamInput(stream)
break
#maybe set stream as stagnant if difference isn't large enough
## if (stream.score-riverScore[pos])<r.random()*0.2-0.3 and (stream.length-2)%5==0 and r.random()<0.7:
## stream.stagnant = True
if (stream.score-riverScore[pos])<r.random()*0.2-0.4 and stream.length>r.randint(3,8):
if j==0:
stepLength = largeStepLength
continue
else:
stream.stagnant = True
break
#create next node
newStream = HydraulicNode(targetPos, 0.05, riverScore[pos], targetPos-stream.getPos() )
stream.addStreamOutput(newStream)
newStreams.append(newStream)
usedFineBiomes.append(fineBiomes.getClosest(targetPos))
break
for stream in streamsInProcess:
hydroGraph.addNode(stream)
streamsInProcess = newStreams
#generate map graph data
hydraulicObjects = {}
for hydroObj in GetLoadedObjectClass("hydraulics"):
hydraulicObjects[hydroObj.getName()] = hydroObj
for node in hydroGraph.getNodes():
node.generateMapGraphData(hydraulicObjects)
return hydroGraph