-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcivReligions.go
281 lines (256 loc) · 9.61 KB
/
civReligions.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
package genworldvoronoi
import (
"fmt"
"log"
"math/rand"
"sort"
"github.com/Flokey82/go_gens/genlanguage"
"github.com/Flokey82/go_gens/genreligion"
"github.com/Flokey82/go_gens/genstory"
)
// GetReligion returns the religion of the given region (if any).
func (m *Civ) GetReligion(r int) *Religion {
if m.RegionToReligion[r] <= 0 {
return nil
}
for _, c := range m.Religions {
if c.ID == m.RegionToReligion[r] {
return c
}
}
return nil
}
// Religion represents a religion in the world.
//
// TODO: Ensure we can infer symbolisms from events and other things.
//
// For example, if they worship the 99 beer bottles on the wall, we should
// be able to infer that they highly value beer and the number 99, as well
// as walls. They might be fearful of the number 100, and might have a
// taboo against the number 1.
// They might look kindly on people who can drink 99 beers in a row.
//
// Another example: If they worship the sun, we should be able to infer
// that they highly value the sun, and that they might be fearful of the
// moon. They might have a celebration during the summer solstice and consider
// a total eclipse of the sun to be a bad omen and a moon eclipse to be a good
// omen.
//
// # DEITIES AND SYMBOLS
//
// Folk religions that are purely based on the culture might warship
// nature itself, such as the sun, summer, the rain, a particular animal,
// or a particular plant. They might worship one or multiple deities that
// represent nature, like the sun god, the rain god, the god of the forest.
//
// Organized religions might either worship one or multiple gods, or a single
// person that is considered to be a god (or chosen).
//
// # GRAPH
//
// All these themes and connections could be represented as a graph, which
// would allow us to infer the relationships between deities and symbols and
// if mundane events hold any significance for a particular religion.
type Religion struct {
ID int // The region where the religion was founded
Name string // The name of the religion
NameGen *genstory.Generated // The name generation information
Culture *Culture // The culture that the religion is based on
Parent *Religion // The parent religion (if any)
*genreligion.Classification // The religion classification
Deity *genreligion.Deity // The deity of the religion (if any)
Expansion string // How the religion wants to expand
Expansionism float64 // How much the religion wants to expand
Founded int64 // Year when the religion was founded
}
func (r *Religion) String() string {
if r.Deity == nil {
return fmt.Sprintf("%s (%s, %s, %s)", r.Name, r.Group, r.Expansion, r.Form)
}
return fmt.Sprintf("%s (%s, %s, %s)\n=%s", r.Name, r.Group, r.Expansion, r.Form, r.Deity.FullName())
}
func (r *Religion) compare(other *Religion) float64 {
if r == other {
return 1.0
}
if r == nil || other == nil {
return -1.0
}
// Sum up the expansionism of both religions.
// This will increase negative effects if the religions are very different.
expSum := 1.0 + r.Expansionism + other.Expansionism
var value float64
// If the relations are closely related, we give a bonus.
if r == other.Parent || r.Parent == other || r.Parent == other.Parent {
value += 0.1
} else {
value -= 0.1 * expSum
}
// If they are of different types, we give a penalty.
if r.Group != other.Group {
value -= 0.1 * expSum
}
if r.Form != other.Form {
value -= 0.5 * expSum
}
if r.Type != other.Type {
value -= 0.1 * expSum
}
return value
}
// genFolkReligion generates a folk religion for the given culture.
func (m *Civ) genFolkReligion(c *Culture) *Religion {
// TODO:
// - Set foundation year.
// - Maybe randomize origin region within the culture's regions.
return m.placeReligionAt(c.ID, -1, genreligion.GroupFolk, c, c.Language, nil)
}
// genOrganizedReligion generates an organized religion for the given city.
func (m *Civ) genOrganizedReligion(c *City) *Religion {
var parent *Religion
if rID := m.RegionToReligion[c.ID]; rID >= 0 {
for _, r := range m.Religions {
if r.ID == rID {
parent = r
break
}
}
}
return m.placeReligionAt(c.ID, -1, genreligion.GroupOrganized, c.Culture, c.Language, parent)
}
// PlaceNFolkReligions generates folk religions.
func (m *Civ) PlaceNFolkReligions(n int) []*Religion {
var religions []*Religion
cultures := make([]*Culture, len(m.Cultures))
copy(cultures, m.Cultures)
sort.Slice(cultures, func(i, j int) bool {
return cultures[i].Spirituality > cultures[j].Spirituality
})
if len(cultures) > n {
cultures = cultures[:n]
}
for _, c := range cultures {
religions = append(religions, m.genFolkReligion(c))
}
m.ExpandReligions()
return religions
}
// PlaceNOrganizedReligions generates organized religions.
func (m *Civ) PlaceNOrganizedReligions(n int) []*Religion {
var religions []*Religion
cities := make([]*City, len(m.Cities))
copy(cities, m.Cities)
sort.Slice(cities, func(i, j int) bool {
return cities[i].Score > cities[j].Score
})
if len(cities) > n {
cities = cities[:n]
}
for _, c := range cities {
religions = append(religions, m.genOrganizedReligion(c))
}
m.ExpandReligions()
return religions
}
// placeReligionAt places a religion of the given group at the given region.
// This code is based on:
// https://github.com/Azgaar/Fantasy-Map-Generator/blob/master/modules/religions-generator.js
func (m *Civ) placeReligionAt(r int, founded int64, group string, culture *Culture, lang *genlanguage.Language, parent *Religion) *Religion {
// If founded is -1, we take the current year.
if founded == -1 {
founded = m.History.GetYear()
}
relg := &Religion{
ID: r,
Culture: culture,
Founded: founded,
Parent: parent,
}
rlgGen := genreligion.NewGenerator(int64(r), lang)
if parent != nil {
// Inherit some characteristics from parent.
// TODO: If parent is not nil, maybe swich form to cult or heresy?
relg.Classification = rlgGen.NewClassificationWithForm(group, parent.Form)
} else {
relg.Classification = rlgGen.NewClassification(group)
}
// If appropriate, add a deity to the religion.
if relg.HasDeity() {
var err error
if parent != nil && parent.HasDeity() {
// If we have a parent religion with a deity, we use the same approach
// to generate the deity, otherwise the generator will pick a random approach.
// TODO: Use antonyms to deity of parent.
relg.Deity, err = rlgGen.GetDeityWithApproach(parent.Deity.Meaning.Template)
} else {
relg.Deity, err = rlgGen.GetDeity()
}
// TODO: Error handling.
if err != nil {
log.Printf("error generating deity: %v", err)
}
}
// Select name, expansion, and expansionism.
if group == genreligion.GroupOrganized {
nameGen, expansion := m.getOrganizedReligionName(rlgGen, culture, lang, relg.Deity, relg.Classification, r)
relg.Name = nameGen.Text
relg.NameGen = nameGen
relg.Expansion = expansion
relg.Expansionism = culture.Expansionism*rand.Float64()*1.5 + 0.5 // TODO: Move this to religion generator.
// This would look up geographically close religions and make this one a cult or heresy.
// if (!cells.burg[center] && cells.c[center].some(c => cells.burg[c])) {
// center = cells.c[center].find(c => cells.burg[c]);
// }
// const [x, y] = cells.p[center];
// const s = spacing * gauss(1, 0.3, 0.2, 2, 2); // randomize to make the placement not uniform
// if (religionsTree.find(x, y, s) !== undefined) continue; // to close to existing religion
// add "Old" to name of the folk religion on this culture
// isFolkBased := expansion == "culture" || P(0.5)
// folk := isFolkBased && religions.find(r => r.culture === culture && r.type === "Folk");
// if (folk && expansion === "culture" && folk.name.slice(0, 3) !== "Old") folk.name = "Old " + folk.name;
// const origins = folk ? [folk.i] : getReligionsInRadius({x, y, r: 150 / count, max: 2});
// const expansionism = rand(3, 8);
} else if group == genreligion.GroupFolk {
nameGen, expansion := m.getFolkReligionName(rlgGen, culture, lang, relg.Deity, relg.Classification, r)
relg.Name = nameGen.Text
relg.NameGen = nameGen
relg.Expansion = expansion
relg.Expansionism = culture.Expansionism * rand.Float64() * 1.5 // TODO: Move this to religion generator.
}
// If there is a parent religion, add an event noting that this branch
// has split off from the parent.
// TODO: Pick random person from the local population to be the founder.
if parent != nil {
m.History.AddEvent("Religion", fmt.Sprintf("%s split from %s", relg.Name, parent.Name),
ObjectReference{
Type: ObjectTypeReligion,
ID: relg.ID,
})
} else {
m.History.AddEvent("Religion", fmt.Sprintf("%s was founded", relg.Name),
ObjectReference{
Type: ObjectTypeReligion,
ID: relg.ID,
})
}
m.Religions = append(m.Religions, relg)
return relg
}
func (m *Civ) ExpandReligions() {
// The religious centers will be the seed points for the expansion.
var seeds []int
originToReligion := make(map[int]*Religion)
for _, r := range m.Religions {
seeds = append(seeds, r.ID)
originToReligion[r.ID] = r
}
territoryWeightFunc := m.getTerritoryWeightFunc()
m.RegionToReligion = m.regPlaceNTerritoriesCustom(m.RegionToReligion, seeds, func(o, u, v int) float64 {
r := originToReligion[o]
if r.Expansion == ReligionExpCulture && m.RegionToCulture[v] != r.Culture.ID ||
r.Expansion == ReligionExpState && m.RegionToCityState[v] != m.RegionToCityState[o] {
return -1
}
return territoryWeightFunc(o, u, v) / r.Expansionism
})
}