-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcivTraderoutes.go
288 lines (246 loc) · 8.13 KB
/
civTraderoutes.go
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package genworldvoronoi
import (
"log"
"math"
"sort"
"github.com/Flokey82/genworldvoronoi/various"
goastar "github.com/beefsack/go-astar"
)
func (m *Civ) GetTradeRoutes() ([][]int, [][]int) {
// TODO: Allow persistent trading routes, so we can run multiple times without
// destroying existing routes.
// Major cities will produce major trading routes that ensure that trade will be
// as efficiently as possible. Minor cities will produce minor trading routes
// that will connect them to the major trading routes.
// Currently we only connect cities / settlement by proximity, which is not
// how it works in reality. Of course along major trade routes, settlements
// will experience growth through trade passing through, which is something
// to consider later.
log.Println("Generating trade routes...")
nodeCache := make(map[int]*TradeTile)
steepness := m.GetSteepness()
cities := m.Cities
isCity := make(map[int]bool)
for _, c := range cities {
isCity[c.ID] = true
}
_, maxElevation := minMax(m.Elevation)
// linking will store which cities are linked through a trade route crossing
// the given region.
linking := make([][]int, m.SphereMesh.NumRegions)
// visited will store which city pairs have already been visited.
visited := make(map[[2]int]bool)
// visitedPathSeg will store how often path segments (neighboring regions connected by a trade route)
// have been used
visitedPathSeg := make(map[[2]int]int)
// wasVisited returns how often the path segment between the two given regions has been used.
var wasVisited func(i, j int) int
wasVisited = func(i, j int) int {
return visitedPathSeg[getSegment(i, j)]
}
// getTile returns the TradeTile for the given index from the node cache.
var getTile func(i int) *TradeTile
getTile = func(i int) *TradeTile {
// Make sure we re-use pre-existing nodes.
n, ok := nodeCache[i]
if ok {
return n
}
// If we have no cached node for this index,
// create a new one.
n = &TradeTile{
steepness: steepness,
r: m,
index: i,
getTile: getTile,
isCity: isCity,
wasVisited: wasVisited,
maxElevation: maxElevation,
}
nodeCache[i] = n
return n
}
// Paths contains a list of all trade routes represented through
// a list of connected regions.
//
// Note that we still double up if two trade routes happen to
// share a common section leading up to a city.
var paths [][]int
// TODO: Pair up by import/export of goods and taxes to the capital.
sortCityIdx := make([]int, len(cities))
for i := range sortCityIdx {
sortCityIdx[i] = i
}
connectNClosest := 5
for i, startC := range cities {
start := startC.ID
// Sort by distance to start as we try to connect the closest towns first.
// NOTE: Wouldn't it make sense to connect the largest cities first?
sort.Slice(sortCityIdx, func(j, k int) bool {
return m.GetDistance(start, cities[sortCityIdx[j]].ID) < m.GetDistance(start, cities[sortCityIdx[k]].ID)
})
var connections int
for _, j := range sortCityIdx {
if connections >= connectNClosest {
break
}
// We don't want to link a city to itself and we try to avoid double
// links (a->b and b->a) as well as we try to only connect towns within
// the same territory.
if i == j {
continue
}
end := cities[j].ID
curEdge := getSegment(start, end)
if visited[curEdge] ||
m.RegionToEmpire[start] != m.RegionToEmpire[end] ||
m.Landmasses[start] != m.Landmasses[end] { // || math.Abs(float64(i-j)) > float64(5)
continue
}
connections++
// Make sure we note that we have visited this city pair.
visited[curEdge] = true
// Attempt to find a path between the two cities.
path, _, found := goastar.Path(getTile(start), getTile(end))
if !found {
continue
}
var newPath []int
for idx, n := range path {
// Mark the node as used.
nti := n.(*TradeTile)
nti.SetUsed()
nIdx := nti.index
if idx > 0 {
visitedPathSeg[getSegment(newPath[idx-1], nIdx)]++
}
// Check if the cities are already in our list for
// the given region (aka "node index").
if !isInIntList(linking[nIdx], start) {
linking[nIdx] = append(linking[nIdx], start)
}
if !isInIntList(linking[nIdx], end) {
linking[nIdx] = append(linking[nIdx], end)
}
// Append the region to the path.
newPath = append(newPath, nIdx)
}
paths = append(paths, newPath)
}
log.Println("Done connecting city", i, "of", len(cities))
}
log.Println("Done generating trade routes.")
return paths, linking
}
type TradeTile struct {
r *Civ // Reference to the Civ object
getTile func(i int) *TradeTile // Fetch tiles from cache
index int // region index
used int // number of times this node was used for a trade route
steepness []float64 // cached steepness of all regiones
wasVisited func(i, j int) int // quick lookup if a segment was already visited
isCity map[int]bool // quick lookup if an index is a city
maxElevation float64
}
func (n *TradeTile) SetUsed() {
n.used++
}
// PathNeighbors returns the direct neighboring nodes of this node which
// can be pathed to.
func (n *TradeTile) PathNeighbors() []goastar.Pather {
nbs := make([]goastar.Pather, 0, 6)
for _, i := range n.r.GetRegNeighbors(n.index) {
nbs = append(nbs, n.getTile(i))
}
return nbs
}
// PathNeighborCost calculates the exact movement cost to neighbor nodes.
func (n *TradeTile) PathNeighborCost(to goastar.Pather) float64 {
tot := to.(*TradeTile)
// Discourage underwater paths.
if n.r.Elevation[n.index] <= 0 || n.r.Elevation[tot.index] <= 0 {
return math.Inf(1)
}
// TODO: Fix this... this is highly inefficient.
nIdx := tot.index
// Altitude changes come with a cost (downhill is cheaper than uphill)
cost := 1.0 + (n.r.Elevation[nIdx]-n.r.Elevation[n.index])/n.maxElevation
// The steeper the terrain, the more expensive.
cost *= 1.0 + n.steepness[nIdx]*n.steepness[nIdx]
// Highly incentivize re-using used segments
if nvis := n.wasVisited(n.index, nIdx); nvis > 0 {
cost /= 8.0 * float64(nvis) * float64(nvis)
} else {
cost *= 8.0
}
// Heavily incentivize re-using existing roads.
if nUsed := tot.used; nUsed > 0 {
cost /= 8.0 * float64(nUsed) * float64(nUsed)
} else {
cost *= 8.0
}
// Bonus if the neighbor is a city.
if n.isCity[nIdx] {
cost /= 4.0
}
// Bonus if along coast.
for _, nbnb := range n.r.GetRegNeighbors(nIdx) {
if n.r.Elevation[nbnb] <= 0 {
cost /= 2.0
break
}
}
if n.r.IsRegRiver(n.index) && n.r.IsRegRiver(nIdx) {
cost *= 0.8 // Bonus if along rivers.
} else if n.r.IsRegRiver(n.index) != n.r.IsRegRiver(nIdx) {
cost *= 1.4 // Cost of crossing rivers.
}
// Penalty for crossing into a new territory
if n.r.RegionToEmpire[n.index] != n.r.RegionToEmpire[nIdx] {
cost *= 2.0
}
return cost
}
// PathEstimatedCost is a heuristic method for estimating movement costs
// between non-adjacent nodes.
func (n *TradeTile) PathEstimatedCost(to goastar.Pather) float64 {
return n.r.GetDistance(n.index, to.(*TradeTile).index)
}
func getSegment(a, b int) [2]int {
if a < b {
return [2]int{a, b}
}
return [2]int{b, a}
}
func (m *Civ) getTradeRoutesInLatLonBB(minLat, minLon, maxLat, maxLon float64) [][]int {
// Convert the trade route paths to segments.
tr := m.TradeRoutes
var links [][2]int
seen := make(map[[2]int]bool)
for _, path := range tr {
for i := 0; i < len(path)-1; i++ {
seg := getSegment(path[i], path[i+1])
if seen[seg] {
continue
}
seen[seg] = true
links = append(links, seg)
}
}
filter := false
// Find the segments that are within the bounding box.
var filtered [][2]int
for _, link := range links {
if filter {
lat1, lon1 := m.LatLon[link[0]][0], m.LatLon[link[0]][1]
lat2, lon2 := m.LatLon[link[1]][0], m.LatLon[link[1]][1]
// If both points are outside the bounding box, skip the segment.
if (lat1 < minLat || lat1 > maxLat || lon1 < minLon || lon1 > maxLon) &&
(lat2 < minLat || lat2 > maxLat || lon2 < minLon || lon2 > maxLon) {
continue
}
}
filtered = append(filtered, link)
}
return various.MergeIndexSegments(filtered)
}