-
Notifications
You must be signed in to change notification settings - Fork 0
/
makecharacter.py
280 lines (227 loc) · 8.88 KB
/
makecharacter.py
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
import django, os, yaml, random, queue, time
from django.db.models import Sum
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MakeRPG.settings")
django.setup()
from CharacterCreator.models import *
def random_name(firstnames,lastnames):
return ' '.join([ firstnames[random.randint(0, len(firstnames))] , lastnames[random.randint(0, len(lastnames))] ])
def get_history_starts(yaml_file):
# needs error handling
with open(yaml_file, 'r') as yamlfile:
tree = yaml.load(yamlfile)
if 'START' in tree and 'NPC' in tree:
history_tree = tree
else:
print(yaml_file + ' is missing either START or NPC keywords.')
return
return (history_tree['START'], history_tree['NPC'][0])
def roll_stats(c):
sp = c.stat_points
# initialize all character statistics
for stat in Statistic.objects.all().exclude(name='SPECIAL'):
cstat = CharacterStatistic()
cstat.character = c
cstat.statistic = stat
cstat.current = stat.minimum
sp -= stat.minimum
cstat.save()
# roll all character statistics until their sum is stat points
cstats = CharacterStatistic.objects.filter(character=c)
while sp > 0:
cstat = random.choice(cstats)
if cstat.current < cstat.statistic.maximum:
cstat.current += 1
sp -= 1
cstat.save()
def roll_skills(c):
rp = c.role_points
op = c.other_points
# initialize skills
if c.role:
special_skills = c.role.special_skills.all()
common_skills = c.role.common_skills.all()
role_skills = (special_skills | common_skills).distinct()
special_stat = Statistic.objects.get(name='SPECIAL')
all_special_skills = Skill.objects.filter(statistic=special_stat)
other_skills = Skill.objects.all().difference(all_special_skills, role_skills)
else:
special_skills = []
common_skills = []
role_skills = []
other_skills = Skill.objects.all()
for skill in role_skills:
cskill = CharacterSkill()
cskill.character = c
cskill.skill = skill
cskill.current = skill.minimum
rp -= skill.minimum
cskill.save()
role_cskills = CharacterSkill.objects.filter(character=c)
while rp > 0:
cskill = random.choice(role_cskills)
if cskill.current < cskill.skill.maximum:
cskill.current += 1
rp -= 1
cskill.save()
for skill in other_skills:
cskill = CharacterSkill()
cskill.character = c
cskill.skill = skill
cskill.current = skill.minimum
op -= skill.minimum
cskill.save()
other_cskills = CharacterSkill.objects.filter(character=c).difference(role_cskills)
while op > 0:
skill = random.choice(other_skills)
cskill = CharacterSkill.objects.get(character=c, skill=skill)
if cskill.current < cskill.skill.maximum:
cskill.current += 1
op -= 1
cskill.save()
if __name__ == '__main__':
# character count to make per run
character_count = 5
# mean (a.k.a. average) point values
mean_stat_points = 40
mean_role_points = 0
mean_other_points = 20
# percentage of mean variance
mean_percentage = 20/100
history_yaml = 'Examples/classless_cyberpunk_test/classless_cyberpunk_test_history.yaml'
history_start, npc_start = get_history_starts(history_yaml)
# names lists
with open('MakeRPG/firstnames.txt', 'r') as firsts:
firstnames = [name.strip('\n') for name in firsts.readlines()]
with open('MakeRPG/lastnames.txt', 'r') as lasts:
lastnames = [name.strip('\n') for name in lasts.readlines()]
tstart = time.time()
npc_count = 0
for _ in range(character_count):
char_tstart = time.time()
# set points based on a normal distribution with a center mean and 20% mean variance
# and don't let any points go below zero
if mean_stat_points > 0:
sp = round(random.normalvariate(mean_stat_points, mean_stat_points*mean_percentage))
if sp < 0:
sp = 0
else:
sp = 0
if mean_role_points > 0:
rp = round(random.normalvariate(mean_role_points, mean_role_points*mean_percentage))
if rp < 0:
rp = 0
else:
rp = 0
if mean_other_points > 0:
op = round(random.normalvariate(mean_other_points, mean_other_points*mean_percentage))
if op < 0:
op = 0
else:
op = 0
c = Character()
c.name = random_name(firstnames, lastnames)
c.stat_points = sp
c.role_points = rp
c.other_points = op
all_roles = Role.objects.all()
if len(all_roles) > 0:
c.role = random.choice(all_roles)
c.save()
roll_stats(c)
roll_skills(c)
# roll character history
current = Event.objects.get(name=history_start)
next_q = queue.LifoQueue()
reroll_q = queue.LifoQueue()
npc_q = queue.Queue()
next_q.put(current)
while not next_q.empty():
current = next_q.get()
event_rolls = EventRoll.objects.filter(mainevent=current)
er = random.choice(event_rolls)
cer = CharacterEventRoll()
cer.character = c
cer.eventroll = er
cer.save()
if current.nextevent:
next_q.put(current.nextevent)
if er.npc:
for i in range(er.rerollcount):
npc_q.put(er.npc)
if er.rollevent and er.rerollcount > 1:
for i in range(er.rerollcount):
reroll_q.put(er.rollevent)
elif er.rollevent:
next_q.put(er.rollevent)
while not reroll_q.empty():
current = reroll_q.get()
event_rolls = EventRoll.objects.filter(mainevent=current)
er = random.choice(event_rolls)
cer = CharacterEventRoll()
cer.character = c
cer.eventroll = er
cer.save()
if er.npc:
for i in range(er.rerollcount):
npc_q.put(er.npc)
if er.rollevent:
reroll_q.put(er.rollevent)
elif current.nextevent:
reroll_q.put(current.nextevent)
while not npc_q.empty():
npc_tstart = time.time()
npc_count += 1
# set points based on a normal distribution with a center mean and 20% mean variance
# and don't let any points go below zero
if mean_stat_points > 0:
sp = round(random.normalvariate(mean_stat_points, mean_stat_points*mean_percentage))
if sp < 0:
sp = 0
else:
sp = 0
if mean_role_points > 0:
rp = round(random.normalvariate(mean_role_points, mean_role_points*mean_percentage))
if rp < 0:
rp = 0
else:
rp = 0
if mean_other_points > 0:
op = round(random.normalvariate(mean_other_points, mean_other_points*mean_percentage))
if op < 0:
op = 0
else:
op = 0
npc = Character()
npc.name = '[NPC ' + npc_q.get() + '] ' + random_name(firstnames, lastnames)
npc.stat_points = sp
npc.role_points = rp
npc.other_points = op
if len(all_roles) > 0:
npc.role = random.choice(all_roles)
npc.save()
roll_stats(npc)
roll_skills(npc)
npc_event = Event.objects.get(name=npc_start)
while True:
npc_event_rolls = EventRoll.objects.filter(mainevent=npc_event)
npc_event_roll = random.choice(npc_event_rolls)
npc_er = NPCEventRoll()
npc_er.npc = npc
npc_er.character = c
npc_er.eventroll = npc_event_roll
npc_er.save()
try:
npc_event = NPCEvent.objects.get(current=npc_event).next
except:
break
npc_tstop = time.time()
print('%s made in %0.1f seconds' % (npc, npc_tstop-npc_tstart))
char_tstop = time.time()
print('%s made in %0.1f seconds' % (c, char_tstop-char_tstart))
tstop = time.time()
elapsed_time = tstop - tstart
avg_character_time = elapsed_time / character_count
avg_character_npc_time = elapsed_time / (character_count + npc_count)
print('Created %d character(s) and %d NPC(s) in %0.2f minutes' % (character_count, npc_count, elapsed_time/60))
print('Average full character with NPC(s) time: %0.1f seconds' % (avg_character_time))
print('Average time per character or NPC: %0.1f seconds' % (avg_character_npc_time))