-
Notifications
You must be signed in to change notification settings - Fork 0
/
character.go
457 lines (390 loc) · 11.4 KB
/
character.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package main
import (
"fmt"
"math/rand"
"time"
vars "github.com/blaine-t-bush/wh3e-char-gen/vars"
)
func GenerateCharacter() Character {
// Seed the randomizer.
rand.Seed(time.Now().UnixNano())
// Randomly select a class.
class := GenerateClass()
// Start at level 1.
level := 1
levelInfo := class.levels[level]
// Randomly generate attributes.
attributes := GenerateAttributeScores()
// Generate hit points.
hitPoints := GenerateHitPoints(levelInfo.hitDie, levelInfo.bonusHitPoints, level, class.name, attributes["con"].Score, 0)
// Determine total group count.
bonusGroupCount := 0
for _, val := range attributes {
if val.Score <= 5 {
bonusGroupCount++
}
}
remainingGroupCount := levelInfo.groupCount + bonusGroupCount
// Determine species.
species := GenerateSpecies()
if species != "Human" {
remainingGroupCount--
}
// Determine vocation.
vocation := ""
if remainingGroupCount > 0 {
vocation = GenerateVocation()
remainingGroupCount--
}
// Determine affiliations.
var affiliations []string
for {
if remainingGroupCount <= 0 {
break
}
affiliations = append(affiliations, GenerateAffiliation(species, affiliations))
remainingGroupCount--
}
// Assign groups to attributes.
var groups []Group
if class.name != "Deft" {
groups = append(groups, Group{
name: vocation,
category: "vocation",
attribute: SelectGroupAttribute(groups, make([]string, 0)),
})
}
if species != "Human" {
firstSpeciesAttr := SelectGroupAttribute(groups, make([]string, 0))
groups = append(groups, Group{
name: species,
category: "species",
attribute: firstSpeciesAttr,
})
groups = append(groups, Group{
name: species,
category: "species",
attribute: SelectGroupAttribute(groups, []string{firstSpeciesAttr}),
})
}
for _, affiliation := range affiliations {
groups = append(groups, Group{
name: affiliation,
category: "affiliation",
attribute: SelectGroupAttribute(groups, make([]string, 0)),
})
}
// Format groups for printing.
groupsByAttribute := map[string][]string{
"Strength": {},
"Dexterity": {},
"Constitution": {},
"Intelligence": {},
"Wisdom": {},
"Charisma": {},
}
for _, group := range groups {
groupsByAttribute[group.attribute] = append(groupsByAttribute[group.attribute], group.name)
}
attributeGroupStrings := map[string]string{
"Strength": "",
"Dexterity": "",
"Constitution": "",
"Intelligence": "",
"Wisdom": "",
"Charisma": "",
}
for attr, groupNames := range groupsByAttribute {
if len(groupNames) > 0 {
for index, groupName := range groupNames {
if index == 0 {
attributeGroupStrings[attr] += groupName
} else {
attributeGroupStrings[attr] += fmt.Sprintf(", %s", groupName)
}
}
}
}
name := GenerateName(species)
languages := GenerateLanguages(attributes["int"].Score)
coins := GenerateCoins()
formattedAttributes := map[string]Attribute{
"str": {
Score: attributes["str"].Score,
Groups: make([]string, 2),
},
"dex": {
Score: attributes["dex"].Score,
Groups: make([]string, 2),
},
"con": {
Score: attributes["con"].Score,
Groups: make([]string, 2),
},
"int": {
Score: attributes["int"].Score,
Groups: make([]string, 2),
},
"wis": {
Score: attributes["wis"].Score,
Groups: make([]string, 2),
},
"cha": {
Score: attributes["cha"].Score,
Groups: make([]string, 2),
},
}
character := Character{
Name: name,
Level: level,
Class: class.name,
Species: species,
Vocation: vocation,
HitPoints: hitPoints,
AttackValue: levelInfo.attackValue,
SavingThrow: levelInfo.savingThrow,
Attributes: formattedAttributes,
Languages: languages,
Coins: coins,
}
return character
// fmt.Printf("%s, level %d %s %s %s\n", name, level, class.name, species, vocation)
// fmt.Println("# Statistics")
// fmt.Printf(" - HP %2s\n", strconv.Itoa(hitPoints))
// fmt.Printf(" - AV %2s\n", strconv.Itoa(levelInfo.attackValue))
// fmt.Printf(" - ST %2s\n", strconv.Itoa(levelInfo.savingThrow))
// fmt.Println("# Attributes")
// fmt.Printf(" - STR %2s %s\n", strconv.Itoa(attributes["str"].score), attributeGroupStrings["Strength"])
// fmt.Printf(" - DEX %2s %s\n", strconv.Itoa(attributes["dex"].score), attributeGroupStrings["Dexterity"])
// fmt.Printf(" - CON %2s %s\n", strconv.Itoa(attributes["con"].score), attributeGroupStrings["Constitution"])
// fmt.Printf(" - INT %2s %s\n", strconv.Itoa(attributes["int"].score), attributeGroupStrings["Intelligence"])
// fmt.Printf(" - WIS %2s %s\n", strconv.Itoa(attributes["wis"].score), attributeGroupStrings["Wisdom"])
// fmt.Printf(" - CHA %2s %s\n", strconv.Itoa(attributes["cha"].score), attributeGroupStrings["Charisma"])
// fmt.Println("# Languages")
// for _, language := range languages {
// fmt.Printf(" - %s\n", language)
// }
// if class.name == "Deft" {
// fmt.Println("# Attunements")
// attunements := GenerateAttunements()
// for index, attunement := range attunements {
// if index == 0 {
// fmt.Printf(" - %s*\n", attunement)
// } else {
// fmt.Printf(" - %s\n", attunement)
// }
// }
// } else if class.name == "Strong" {
// fmt.Println("# Abilities")
// abilities := GenerateAbilities()
// for _, ability := range abilities {
// fmt.Printf(" - %s\n", ability)
// }
// } else if class.name == "Wise" {
// fmt.Println("# Miracles")
// miracles := GenerateMiracles(attributes["wis"].score)
// for index, miracle := range miracles {
// if index == 0 {
// fmt.Printf(" - %s*\n", miracle)
// } else {
// fmt.Printf(" - %s\n", miracle)
// }
// }
// }
// fmt.Println("# Inventory")
// fmt.Printf(" - %3s coins\n", strconv.Itoa(coins))
}
func GenerateName(species string) string {
name := ""
if species == "Dwarf" {
name += GetRandomElement(vars.NamesDwarfAdjective) + " " + GetRandomElement(vars.NamesDwarfNoun)
} else {
prefixChance := 0.5
rollPrefix := rand.Float64()
// Randomly choose either prefix or suffix
if rollPrefix <= prefixChance {
name += GetRandomElement(vars.NamesHumanPrefix) + " " + GetRandomElement(vars.NamesHumanPrimary)
} else {
name += GetRandomElement(vars.NamesHumanPrimary) + " " + GetRandomElement(vars.NamesHumanSuffix)
}
}
return name
}
func GenerateClass() Class {
classNames := [3]string{
"deft",
"strong",
"wise",
}
return Classes[classNames[rand.Intn(len(classNames))]]
}
func GenerateSpecies() string {
// Normalize probabilities.
var totalProbability float64 = 0
for _, species := range Speciess {
totalProbability += species.probability
}
// Randomly select a species.
var cumulativeProbability float64 = 0
var name string
roll := rand.Float64()
for _, species := range Speciess {
cumulativeProbability += species.probability / totalProbability
if roll <= cumulativeProbability {
name = species.name
break
}
}
return name
}
// Randomly generate six attribute scores with 3d6.
func GenerateAttributeScores() map[string]Attribute {
attributes := Attributes
strength := attributes["str"]
strength.Score = D(3, 6)
attributes["str"] = strength
dexterity := attributes["dex"]
dexterity.Score = D(3, 6)
attributes["dex"] = dexterity
constitution := attributes["con"]
constitution.Score = D(3, 6)
attributes["con"] = constitution
intelligence := attributes["int"]
intelligence.Score = D(3, 6)
attributes["int"] = intelligence
wisdom := attributes["wis"]
wisdom.Score = D(3, 6)
attributes["wis"] = wisdom
charisma := attributes["cha"]
charisma.Score = D(3, 6)
attributes["cha"] = charisma
return attributes
}
// Randomly generate hitpoints. The hitpoints are generated by rolling a number
// of d6 equal to hitDice, and adding bonusHitPoints. The minimum returned value
// is equal to previousHitPoints+1. At level 1, hit points are maximized.
func GenerateHitPoints(hitDice int, bonusHitPoints int, level int, className string, conScore int, previousHitPoints int) int {
if className == "Strong" && conScore >= 16 {
bonusHitPoints += 2
} else if className == "Strong" && conScore >= 13 {
bonusHitPoints += 1
}
if level == 1 {
return 6*hitDice + bonusHitPoints
} else {
newHitPoints := D(hitDice, 6) + bonusHitPoints
if newHitPoints <= previousHitPoints {
return previousHitPoints + 1
} else {
return newHitPoints
}
}
}
func GenerateVocation() string {
return GetRandomElement(vars.Vocations)
}
func GenerateAffiliation(species string, existingAffiliations []string) string {
var affiliations []string
if species == "Human" {
affiliations = append(vars.Affiliations, vars.AffiliationsHuman...)
} else if species == "Dwarf" {
affiliations = append(vars.Affiliations, vars.AffiliationsDwarf...)
}
return GetUniqueElement(affiliations, existingAffiliations)
}
func GenerateAbility(existingAbilities []string) string {
return GetUniqueElement(vars.Abilities, existingAbilities)
}
func GenerateAbilities() []string {
var abilities []string
abilityCount := 1
for i := 0; i < abilityCount; i++ {
abilities = append(abilities, GenerateAbility(abilities))
}
return abilities
}
func GenerateAttunement(existingAttunements []string) string {
return GetUniqueElement(vars.Attunements, existingAttunements)
}
func GenerateAttunements() []string {
var attunements []string
attunementCount := 2
for i := 0; i < attunementCount; i++ {
attunements = append(attunements, GenerateAttunement(attunements))
}
return attunements
}
func GenerateMiracle(existingMiracles []string) string {
return GetUniqueElement(vars.Miracles, existingMiracles)
}
func GenerateMiracles(wisdomScore int) []string {
var miracles []string
var miracleCount int
if wisdomScore >= 16 {
miracleCount = 4
} else if wisdomScore >= 13 {
miracleCount = 3
} else {
miracleCount = 2
}
for i := 0; i < miracleCount; i++ {
miracles = append(miracles, GenerateMiracle(miracles))
}
return miracles
}
func SelectGroupAttribute(groups []Group, excludes []string) string {
// Determine which attributes have fewer than two groups already.
groupAttributeCounts := map[string]int{
"Strength": 0,
"Dexterity": 0,
"Constitution": 0,
"Intelligence": 0,
"Wisdom": 0,
"Charisma": 0,
}
for _, group := range groups {
groupAttributeCounts[group.attribute]++
}
var availableAttributes []string
for attribute, count := range groupAttributeCounts {
if count < 2 {
availableAttributes = append(availableAttributes, attribute)
}
}
// Exclude any attributes if that argument has been passed.
if len(excludes) > 0 {
var modifiedAvailableAttributes []string
var include bool
for _, attr := range availableAttributes {
include = true
for _, exclude := range excludes {
if attr == exclude {
include = false
break
}
}
if include {
modifiedAvailableAttributes = append(modifiedAvailableAttributes, attr)
}
}
availableAttributes = modifiedAvailableAttributes
}
// Randomly select one of those attributes.
return availableAttributes[rand.Intn(len(availableAttributes)-1)]
}
func GenerateLanguages(intScore int) []string {
languageCount := 1
if intScore >= 16 {
languageCount += 2
} else if intScore >= 13 {
languageCount += 1
}
languages := []string{"Low Imperial"} // Everyone knows the common tongue.
for i := 1; i < languageCount; i++ {
languages = append(languages, GetUniqueElement(vars.Languages, languages))
}
return languages
}
func GenerateCoins() int {
return D(3, 6) * 10
}