This repository has been archived by the owner on Aug 30, 2018. It is now read-only.
/
name.go
171 lines (152 loc) · 3.86 KB
/
name.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
package hero
import (
"math/rand"
)
type HeroName struct {
FirstT NameSubtype
First uint64
Nickname string
Last1T NameSubtype
Last1 uint64
Last2T NameSubtype
Last2 uint64
Last3T NameSubtype
Last3 uint64
PrefixT NameSubtype
Prefix uint64
SuffixT NameSubtype
Suffix uint64
}
func (n *HeroName) serialize() map[string]interface{} {
return map[string]interface{}{
"v": uint(1),
"ft": uint64(n.FirstT),
"f": n.First,
"n": n.Nickname,
"l1t": uint64(n.Last1T),
"l1": n.Last1,
"l2t": uint64(n.Last2T),
"l2": n.Last2,
"l3t": uint64(n.Last3T),
"l3": n.Last3,
"pt": uint64(n.PrefixT),
"p": n.Prefix,
"st": uint64(n.SuffixT),
"s": n.Suffix,
}
}
func (n *HeroName) unserialize(data map[string]interface{}) {
version := data["v"].(uint)
switch version {
case 1:
n.Prefix = data["p"].(uint64)
n.PrefixT = NameSubtype(data["pt"].(uint64))
n.Suffix = data["s"].(uint64)
n.SuffixT = NameSubtype(data["st"].(uint64))
fallthrough
case 0:
n.First = data["f"].(uint64)
n.FirstT = NameSubtype(data["ft"].(uint64))
n.Nickname = data["n"].(string)
n.Last1 = data["l1"].(uint64)
n.Last1T = NameSubtype(data["l1t"].(uint64))
n.Last2 = data["l2"].(uint64)
n.Last2T = NameSubtype(data["l2t"].(uint64))
n.Last3 = data["l3"].(uint64)
n.Last3T = NameSubtype(data["l3t"].(uint64))
}
}
// Name converts a numeric HeroName into a human-readable string. If Name would
// return an empty string, it instead returns the string "unknown".
func (n *HeroName) Name() string {
if n == nil {
return "unknown"
}
var buf []byte
if name := names[n.FirstT][n.First]; name != "" {
buf = append(append(buf, ' '), name...)
}
if n.Nickname != "" {
buf = append(append(append(buf, " “"...), n.Nickname...), "”"...)
}
if name := names[n.Last1T][n.Last1]; name != "" {
buf = append(append(append(append(buf, ' '), name...), names[n.Last2T][n.Last2]...), names[n.Last3T][n.Last3]...)
}
if len(buf) <= 1 {
return "unknown"
}
return names[n.PrefixT][n.Prefix] + string(buf[1:]) + names[n.SuffixT][n.Suffix]
}
// GenerateHumanName randomly generates a name suitable for a human. The first
// name is chosen from a gender-specific list. There is a 3% chance of no
// surname. For the other 97% of names, 25% have a surname beginning with
// a male first name and ending with "son". An additional 5% have a surname
// of a single English word, such as "smith" or "cook". The remaining 70% have
// a surname generated from two syllables.
func GenerateHumanName(r *rand.Rand, gender Gender) HeroName {
var n HeroName
switch gender {
case GenderMale:
n.FirstT = NameMaleHuman
case GenderFemale:
n.FirstT = NameFemaleHuman
}
n.First = uint64(r.Intn(len(names[n.FirstT])))
if r.Intn(100) < 3 {
return n
}
switch c := r.Intn(100); {
case c < 25:
n.Last1T = NameMaleHuman
for {
n.Last1 = uint64(r.Intn(len(names[n.Last1T])))
name := names[n.Last1T][n.Last1]
if len(name) >= 2 && name[len(name)-2:] == "ss" {
continue
}
if len(name) >= 3 && name[len(name)-3:] == "son" {
continue
}
break
}
n.Last2T = NameUtil
n.Last2 = nameutilSon
case c < 30:
switch r.Intn(4) {
case 0, 1:
n.Last1T = NameSurnameConsonant
case 2:
n.Last2T = NameUtil
n.Last2 = nameutilR
fallthrough
case 3:
n.Last1T = NameSurnameVowel
}
n.Last1 = uint64(r.Intn(len(names[n.Last1T])))
default:
switch r.Intn(3) {
case 0:
n.Last1T = NameSurnameConsonant
switch r.Intn(3) {
case 0, 1:
n.Last2T = NameEndConsonant
case 2:
n.Last2T = NameEndVowel
}
case 1:
n.Last1T = NameSurnameVowel
n.Last2T = NameEndConsonant
case 2:
n.Last1T = NameFrontSyllableConsonant
switch r.Intn(3) {
case 0, 1:
n.Last2T = NameEndConsonant
case 2:
n.Last2T = NameEndVowel
}
}
n.Last1 = uint64(r.Intn(len(names[n.Last1T])))
n.Last2 = uint64(r.Intn(len(names[n.Last2T])))
}
return n
}