-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcivCityState.go
175 lines (151 loc) · 4.5 KB
/
civCityState.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
package genworldvoronoi
import (
"fmt"
"log"
"github.com/Flokey82/genworldvoronoi/geo"
)
func (m *Civ) GetCityState(id int) *CityState {
if m.RegionToCityState[id] < 0 {
return nil
}
for _, cs := range m.CityStates {
if cs.ID == m.RegionToCityState[id] {
return cs
}
}
return nil
}
func (m *Civ) PlaceNCityStates(n int) {
m.ResetRand()
for i, c := range m.Cities {
if i >= n {
break
}
m.PlaceCityStateAt(c.ID, c)
log.Printf("CityState %d: %s", i, c.Name)
}
m.ExpandCityStates()
}
// CityState represents a territory governed by a single city.
type CityState struct {
ID int // Region where the city state originates
Capital *City // Capital city
Culture *Culture // Culture of the city state
Cities []*City // Cities within the city state
Founded int64 // Year when the city state was founded
// TODO: DO NOT CACHE THIS!
Regions []int
*geo.Stats
}
func (c *CityState) compare(other *CityState) float64 {
if c == other {
return 1.0
}
if c == nil || other == nil {
return -1.0
}
capitalValue := c.Capital.compare(other.Capital)
cultureValue := c.Culture.compare(other.Culture)
return (capitalValue + cultureValue) / 2
}
func (c *CityState) String() string {
return fmt.Sprintf("CityState %s: %d cities, %d regions", c.Capital.Name, len(c.Cities), len(c.Regions))
}
func (c *CityState) Log() {
log.Printf("The city state of %s: %d cities, %d regions", c.Capital.Name, len(c.Cities), len(c.Regions))
c.Stats.Log()
}
func (m *Civ) PlaceCityStateAt(r int, c *City) *CityState {
cs := &CityState{
ID: r,
Capital: c,
Culture: m.GetCulture(r),
Founded: c.Founded, // TODO: Use current year.
Cities: []*City{c}, // TODO: ??? Remove this?
Regions: []int{r}, // TODO: ??? Remove this?
Stats: m.GetStats([]int{r}), // TODO: ??? Remove this?
}
// If there is no known culture, generate a new one.
if c.Culture == nil {
c.Culture = m.PlaceCultureAt(r, true, nil) // TODO: Grow this culture.
}
m.CityStates = append(m.CityStates, cs)
// TODO: Name? Language?
return cs
}
// ExpandCityStates expands the city states on the map based on their culture,
// terrain preference, and other factors.
func (m *Civ) ExpandCityStates() {
// Territories are based on cities acting as their capital.
// Since the algorithm places the cities with the highes scores
// first, we use the top 'n' cities as the capitals for the
// territories.
var seedCities []int
for _, c := range m.CityStates {
seedCities = append(seedCities, c.ID)
}
m.expandCityStates(false, seedCities)
}
func (m *Civ) expandCityStates(aggressive bool, seedCities []int) {
weight := m.getTerritoryWeightFunc()
biomeWeight := m.getTerritoryBiomeWeightFunc()
cultureWeight := m.getTerritoryCultureWeightFunc()
placeFunc := m.regPlaceNTerritoriesCustom
if aggressive {
placeFunc = m.expandTerritoriesAggressive
}
m.RegionToCityState = placeFunc(m.RegionToCityState, seedCities, func(o, u, v int) float64 {
// TODO: Make sure we take in account expansionism, wealth, score, and culture.
w := weight(o, u, v)
if w < 0 {
return -1
}
b := biomeWeight(o, u, v)
if b < 0 {
return -1
}
c := cultureWeight(o, u, v)
if c < 0 {
return -1
}
return (w + b + c) / 3
})
// Before relaxing the territories, we'd need to ensure that we only
// relax without changing the borders of the empire...
// So we'd only re-assign IDs that belong to the same territory.
// m.rRelaxTerritories(m.r_city, 5)
// Update the city states with the new regions.
for _, c := range m.CityStates {
// Loop through all cities and gather all that
// are within the current city state.
c.Cities = c.Cities[:0]
for _, ct := range m.Cities {
if m.RegionToCityState[ct.ID] == c.ID {
c.Cities = append(c.Cities, ct)
}
}
// Collect all regions that are part of the
// current territory.
c.Regions = c.Regions[:0]
for r, terr := range m.RegionToCityState {
if terr == c.ID {
c.Regions = append(c.Regions, r)
}
}
c.Stats = m.GetStats(c.Regions)
c.Log()
}
}
func (m *Civ) GetCityStates() []*CityState {
// TODO: Deduplicate with GetEmpires.
return m.CityStates
}
// getCityStateNeighbors returns all city states that are neighbors of the
// given city state.
func (m *Civ) getCityStateNeighbors(c *CityState) []int {
return m.getTerritoryNeighbors(c.ID, m.RegionToCityState)
}
// getCityStateEmpire returns the empire that the given city state belongs to (if any).
func (m *Civ) getCityStateEmpire(c *CityState) int {
return m.RegionToEmpire[c.ID]
}