-
Notifications
You must be signed in to change notification settings - Fork 0
/
character.go
427 lines (390 loc) · 11.6 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
// Package sotdlgen implements a character generator for the SotDL RPG.
package sotdlgen
import (
"encoding/json"
"fmt"
"math"
"sort"
"strconv"
logging "github.com/op/go-logging"
)
// Declare logger.
var (
log = logging.MustGetLogger("sotdl")
logLevels = map[string]logging.Level{
"INFO": logging.INFO,
"ERROR": logging.ERROR,
"WARNING": logging.WARNING,
}
)
// Declare various character data lists.
var (
ancestries = []string{
"Human", "Dwarf", "Goblin", "Orc", "Changeling", "Clockwork",
}
novicePaths = []string{
"Priest", "Magician", "Warrior", "Rogue",
}
expertPaths = []string{
"Artificer", "Assassin", "Berserker", "Cleric", "Druid", "Fighter",
"Oracle", "Paladin", "Ranger", "Scout", "Sorcerer", "Spellbinder", "Thief",
"Warlock", "Witch", "Wizard",
}
masterPaths = []string{
"Abjurer", "Acrobat", "Aeromancer", "Apocalyptist", "Arcanist", "Astromancer",
"Avenger", "Bard", "Beastmaster", "Blade", "Brute", "Cavalier", "Champion",
"Chaplain", "Chronomancer", "Conjurer", "Conqueror", "Death Dealer", "Defender",
"Dervish", "Destroyer", "Diplomat", "Diviner", "Dreadnaught", "Duelist",
"Enchantment", "Engineer", "Executioner", "Exorcist", "Explorer", "Geomancer",
"Gladiator", "Gunslinger", "Healer", "Hexer", "Hydromancer", "Illusionist",
"Infiltrator", "Inquisitor", "Jack-of-all-Trades", "Mage Knight", "Magus",
"Marauder", "Miracle Worker", "Myrmidon", "Necromancer", "Poisoner", "Pyromancer",
"Runesmith", "Savant", "Sentinel", "Shapeshifter", "Sharpshooter", "Stormbringer",
"Technomancer", "Templar", "Tenebrist", "Thaumaturge", "Theurge", "Transmuter",
"Traveler", "Weapon Master", "Woodwose", "Zealot",
}
genders = []string{"Male", "Female"}
languages = []string{
"Common Tongue", "Dark Speech", "Dwarfish", "Elvish", "High Archaic", "Trollish",
"Secret Language",
}
)
// Character represents the primary features of the character.
type Character struct {
Name string `json:"name"`
Gender string `json:"gender"`
Ancestry string `json:"ancestry"`
LangAndProf []string `json:"languages_and_professions"`
NovicePath string `json:"novice_path"`
ExpertPath string `json:"expert_path"`
MasterPath string `json:"master_path"`
Talents []string `json:"talents"`
Level int `json:"level"`
Attributes Attributes `json:"attributes"`
Seed string `json:"seed"`
//Background string `json:"background"`
//Description string `json:"description"`
//Magic []Spell `json:"magic"`
//Weapons []Weapon `json:"weapons"`
//Armor []Armor `json:"armor"`
//Equipment []string `json:"equipment"`
}
// Attributes represents character statistics.
type Attributes struct {
Strength int `json:"strength"`
Agility int `json:"agility"`
Intellect int `json:"intellect"`
Will int `json:"will"`
Speed int `json:"speed"`
Power int `json:"power"`
Health int `json:"health"`
Size string `json:"size"`
Insanity int `json:"insanity"`
Corruption int `json:"corruption"`
Defense int `json:"defense"`
Perception int `json:"perception"`
HealingRate int `json:"healing_rate"`
healthMod int
defenseMod int
perceptionMod int
healingRateMultiplier float64
}
// Spell represents properties of a given spell.
type Spell struct {
// TODO: Implement spells.
Name string `json:"name"`
Type string `json:"type"`
Rank int `json:"rank"`
Target string `json:"target"`
Area string `json:"area"`
Duration string `json:"duration"`
Triggered bool `json:"triggered"`
Sacrifice bool `json:"sacrifice"`
Permanence bool `json:"permanence"`
AttackRoll20 string `json:"attack_20+"`
Description string `json:"description"`
}
// Weapon represents properties of a given weapon.
type Weapon struct {
// TODO: Implement weapons.
Name string `json:"name"`
Type string `json:"type"`
Hands string `json:"hands"`
Cumbersome bool `json:"cumbersome"`
Finesse bool `json:"finesse"`
DefenseBonus int `json:"defense_bonus"`
Misfire bool `json:"misfire"`
Range string `json:"range"`
Reach int `json:"reach"`
Reload bool `json:"reload"`
Size int `json:"size"`
Uses string `json:"uses"`
Thrown bool `json:"thrown"`
Damage Die `json:"damage"`
}
// Armor represents properties of a given suit of armor.
type Armor struct {
// TODO: Implement armor.
Name string `json:"name"`
Type string `json:"type"`
DefenseBonus int `json:"defense"`
}
// Randomly increments n character attributes by 1.
func (c *Character) incrRandomAttrs(n int) {
for i := 0; i < n; i++ {
switch randomInt(0, 4) {
case 0:
c.Attributes.Strength++
case 1:
c.Attributes.Agility++
case 2:
c.Attributes.Intellect++
case 3:
c.Attributes.Will++
}
}
}
// Sets the random seed from a hex hash string.
func (c *Character) setCharSeed(charSeed string) {
var err error
c.Seed, err = setSeed(charSeed)
if err != nil {
log.Error("Failed to set character hash:", err)
}
}
// Pick a random level in [0..10] if non supplied.
func (c *Character) setLevel(level string) {
if level != "" {
c.Level, _ = strconv.Atoi(level)
} else {
c.Level = randomInt(0, 10)
}
}
func (c *Character) calcHealingRate() {
hr := float64(c.Attributes.Health) * c.Attributes.healingRateMultiplier
c.Attributes.HealingRate = int(math.Floor(hr))
}
func (c *Character) increaseAttributes(i int, path string) {
if i == 0 && path == "Human" {
c.incrRandomAttrs(1)
}
switch i {
case 1, 3:
c.incrRandomAttrs(2)
case 7:
c.incrRandomAttrs(3)
}
}
func (c *Character) calcDerived() {
c.Attributes.Perception = c.Attributes.Intellect + c.Attributes.perceptionMod
c.Attributes.Defense = c.Attributes.Agility + c.Attributes.defenseMod
c.Attributes.Health = c.Attributes.Strength + c.Attributes.healthMod
}
//
func (c *Character) setPath(path string) {
// Set the path.
if c.Ancestry == "" {
if path == "" {
path = randomChoice(ancestries)
}
c.Ancestry = path
} else if c.NovicePath == "" {
if path == "" {
path = randomChoice(novicePaths)
}
c.NovicePath = path
} else if c.ExpertPath == "" {
if path == "" {
path = randomChoice(expertPaths)
}
c.ExpertPath = path
} else if c.MasterPath == "" {
if path == "" {
path = randomChoice(masterPaths)
}
c.MasterPath = path
} else {
return
}
// Add attributes, etc.
var keys []int
for k := range db.Paths[path] {
keys = append(keys, k)
}
sort.Ints(keys)
for _, i := range keys {
lvl := db.Paths[path][i]
if i <= c.Level {
// Attributes
c.Attributes.Strength += lvl.Strength
c.Attributes.Agility += lvl.Agility
c.Attributes.Intellect += lvl.Intellect
c.Attributes.Will += lvl.Will
// Characteristics
c.Attributes.perceptionMod += lvl.PerceptionMod
c.Attributes.defenseMod += lvl.DefenseMod
c.Attributes.healthMod += lvl.HealthMod
c.Attributes.Speed += lvl.Speed
c.Attributes.Power += lvl.Power
if lvl.Size != "" {
c.Attributes.Size = lvl.Size
}
c.Attributes.Insanity += lvl.Insanity
c.Attributes.Corruption += lvl.Corruption
if lvl.HealingRate != 0.0 {
c.Attributes.healingRateMultiplier = lvl.HealingRate
}
// Talents
c.Talents = append(c.Talents, lvl.Talents...)
// Languages and Professions
if i == 0 {
c.LangAndProf = append(c.LangAndProf,
"Two professions of your choice; you may trade one for a language.")
}
c.LangAndProf = append(c.LangAndProf, lvl.LangAndProf...)
}
// Attribute increases
c.increaseAttributes(i, path)
}
// Recalc
c.calcDerived()
c.calcHealingRate()
}
// Randomly sample from name db.
func (c *Character) setName(name string) {
if name != "" {
c.Name = name
} else {
firstNames := []string{}
surnames := []string{}
ethnicities := []string{}
for _, nl := range db.Names {
if nl.Ancestry == c.Ancestry && !arrayContains(ethnicities, nl.Ethnicity) {
ethnicities = append(ethnicities, nl.Ethnicity)
}
}
ethnicity := ""
if len(ethnicities) > 0 {
ethnicity = randomChoice(ethnicities)
} else {
ethnicity = db.Names[randomInt(0, len(db.Names))].Ethnicity
}
for _, nl := range db.Names {
if nl.Ethnicity == ethnicity {
switch nl.Type {
case c.Gender:
firstNames = append(firstNames, nl.Names...)
case "Surname":
surnames = append(surnames, nl.Names...)
}
}
}
firstName := ""
surname := ""
if len(firstNames) > 0 {
firstName = randomChoice(firstNames)
}
if len(surnames) > 0 {
surname = randomChoice(surnames)
}
c.Name = trim(firstName + " " + surname)
}
}
// Randomly sample from gender list.
func (c *Character) setGender(gender string) {
if gender != "" {
c.Gender = gender
} else {
c.Gender = randomChoice(genders)
}
}
// TODO: Additional character data functions.
func (c *Character) setMagic() {}
func (c *Character) setWeapons() {}
func (c *Character) setArmor() {}
func (c *Character) setEquipment() {}
func (c *Character) setDescription(description string) {}
func (c *Character) setBackground(background string) {}
func (c *Character) setProfessions(professions string) {}
func (c *Character) setLanguages(languages string) {}
// Print writes tab-delimited character details to STDOUT.
func (c Character) Print() {
fmt.Println("Name\t" + c.Name)
fmt.Println("Gender\t" + c.Gender)
fmt.Println("Level\t", c.Level)
fmt.Println("Character Seed\t", c.Seed)
fmt.Println()
}
// ToJSON writes JSON character details to STDOUT.
func (c Character) ToJSON(pretty bool) string {
var j []byte
if pretty {
j, _ = json.MarshalIndent(c, " ", " ")
} else {
j, _ = json.Marshal(c)
}
fmt.Println(string(j))
//err := ioutil.WriteFile(fn, j, 0644)
//if err != nil {
// panic(err)
//}
return string(j)
}
// Opts contains user input optionsr; used in CLI implementations.
type Opts struct {
Age string
Ancestry string `docopt:"--ancestry"`
Background string
Description string
ExpertPath string `docopt:"--expert-path"`
Gender string `docopt:"--gender"`
Languages string
Level string `docopt:"--level"`
LogLevel string `docopt:"--log-level"`
MasterPath string `docopt:"--master-path"`
Name string `docopt:"--name"`
NovicePath string `docopt:"--novice-path"`
Professions string
Seed string `docopt:"--seed"`
DataFile string `docopt:"--data-file"`
}
// NewCharacter generates a SotDL character given a set of user options.
func NewCharacter(opts Opts) (c Character, err error) {
logging.SetLevel(logLevels[opts.LogLevel], "")
// Load the character db if empty.
if len(db.Paths) == 0 {
log.Info("Loading Character DB.")
db, err = NewCharDB(opts.DataFile, false)
if err != nil {
return c, err
}
}
// Initialize character and set random seed from hash
c.setCharSeed(opts.Seed)
// Generate base characteristics
log.Info("Generating attributes and characteristics.")
c.setPath(opts.Ancestry)
c.setGender(opts.Gender)
c.setName(opts.Name)
c.setLevel(opts.Level)
if c.Level > 0 {
c.setPath(opts.NovicePath)
}
if c.Level > 2 {
c.setPath(opts.ExpertPath)
}
if c.Level > 6 {
c.setPath(opts.MasterPath)
}
// Generate stuff
//c.setMagic()
//c.setWeapons()
//c.setArmor()
//c.setEquipment()
// Generate fluff
//c.setDescription(opts.Description)
//c.setBackground(opts.Background)
//c.setProfessions(opts.Professions)
//c.setLanguages(opts.Languages)
return c, nil
}