-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom4_indies.py
206 lines (161 loc) · 7.62 KB
/
random4_indies.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
import json
import random
terrainlists = lambda : {"plains": [], "farm": [], "forest": [], "mountain": [], "waste": [], "swamp": [], "cave": [], "water": [], "deepwater": [], "coast": []}
class Random4Indies:
def __init__(self, rarechance, strength):
self.rarechance = rarechance
self.strength = strength // 100
self.poptypes = terrainlists()
self.rares = terrainlists()
self.throne_guards = {key: terrainlists() for key in xrange(1, 4)}
self.allpop = []
self.index = 25
random.seed()
def readIndies(self, sourcefile):
with open(sourcefile, 'r') as infile:
data = json.load(infile)
if data.get('poptypes') is not None:
for entry in data['poptypes']:
entry['index'] = str(self.index)
for terrain in entry['terrain']:
if self.poptypes.get(terrain) is None:
print "Error: found unknown terrain " + terrain + " in " + sourcefile
else:
self.poptypes[terrain].append(entry)
self.allpop.append(entry)
self.index += 1
if data.get('rares') is not None:
for entry in data['rares']:
for terrain in entry['terrain']:
if self.rares.get(terrain) is None:
print "Error: found unknown terrain " + terrain + " in " + sourcefile
else:
self.rares[terrain].append(entry)
if data.get('throne_guards') is not None:
for level in data['throne_guards']:
for entry in data['throne_guards'][level]:
for terrain in entry['terrain']:
if self.throne_guards[int(level)].get(terrain) is None:
print "Error: found unknown terrain " + terrain + " in " + sourcefile
else:
self.throne_guards[int(level)][terrain].append(entry)
def getPoptypeForTerrain(self, terrains):
choices = []
for terrain in terrains:
if self.poptypes[terrain] is not None:
choices = choices + self.poptypes[terrain]
result = None
while True:
poptype = random.choice(choices)
if poptype.get('rare') is not None:
if random.random() < poptype['rare']:
result = poptype
break
else:
continue
else:
result = poptype
break
return result
def getRaresForTerrain(self, terrains):
choices = []
for terrain in terrains:
if self.rares[terrain] is not None:
choices = choices + self.rares[terrain]
result = None
while True:
rare = random.choice(choices)
if rare.get('rare') is not None:
if random.random() < rare['rare']:
result = rare
break
else:
continue
else:
result = rare
break
return result
def getThroneGuardsForTerrain(self, terrains, throne_level, throne_tags):
choices = [choice for terrain in terrains for choice in self.throne_guards[throne_level][terrain]]
filtered = [f for tag in throne_tags for f in choices if f.get('tags') is not None and tag in f['tags']]
if len(filtered) != 0:
choices = filtered
result = None
while True:
guards = random.choice(choices)
if guards.get('rare') is not None:
if random.random() < guards['rare']:
result = guards
break
else:
continue
else:
result = guards
break
return result
def getUnitsForEntry(self, entry):
stringList = []
for commander in entry['commander']:
for i in xrange(1 if commander.get('count') is None else commander['count']):
# generate one each time due to possible random attributes
stringList += self.getCommanderString(commander)
for unit in entry['unit']:
count = max(int(random.randrange(int(unit['count']*0.7), int(unit['count']*1.3)) * self.strength), 1)
stringList += '#units ' + str(count) + ' ' + self.getString(unit['type']) + '\n'
return ''.join(stringList)
def getIndiesFor(self, terrains, throne_level, throne_tags):
poptype = self.getPoptypeForTerrain(terrains)
stringList = []
stringList += '#poptype ' + poptype['index'] + '\n'
stringList += self.getUnitsForEntry(poptype)
if random.random() * 100 < self.rarechance:
rare = self.getRaresForTerrain(terrains)
stringList += self.getUnitsForEntry(rare)
if throne_level > 0:
throneguards = self.getThroneGuardsForTerrain(terrains, throne_level, throne_tags)
stringList += self.getUnitsForEntry(throneguards)
return ''.join(stringList)
def getCommanderString(self, commander):
stringList = []
stringList += '#commander ' + self.getString(commander['type']) + '\n'
if commander.get('items') is not None:
stringList += '#randomequip ' + str(commander['items']) + '\n'
if commander.get('name') is not None:
stringList += '#comname "' + random.choice(commander['name']) + '"\n'
if commander.get('xp') is not None:
stringList += '#xp ' + str(commander['xp']) + '\n'
if commander.get('magic') is not None:
for path in commander['magic']:
stringList += '#mag_' + path + ' ' + str(commander['magic'][path]) + '\n'
return ''.join(stringList)
def writeModFile(self, modfile):
with open(modfile + '.dm', 'w') as ofile:
self.writeHeader(ofile)
self.writePoptype(ofile)
def writeHeader(self, ofile, modfile):
ofile.write('#modname "Random indies for ' + modfile + '"\n')
ofile.write('#description "Random indies for ' + modfile + '"\n')
ofile.write('#version 100\n')
ofile.write('#domversion 350\n')
def writePoptype(self, ofile):
for poptype in self.allpop:
ofile.write('\n')
ofile.write('#selectpoptype ' + str(poptype['index']) + '\n')
ofile.write('#clearrec\n')
ofile.write('#cleardef\n')
ofile.write('#defcom1 ' + self.getString(poptype['pd_commander']) + '\n')
prefix = ['', 'b', 'c']
for pd in xrange(min(len(poptype['pd']), 3)):
ofile.write('#defunit1' + prefix[pd] + ' ' + self.getString(poptype['pd'][pd]['type']) + '\n')
ofile.write('#defmult1' + prefix[pd] + ' ' + str(poptype['pd'][pd]['count']) + '\n')
for reccom in poptype['recruitable_commanders']:
ofile.write('#addreccom ' + self.getString(reccom) + '\n')
for recunit in poptype['recruitable_units']:
ofile.write('#addrecunit ' + self.getString(recunit) + '\n')
ofile.write('#end\n')
def getString(self, item):
if isinstance(item, basestring):
return '"' + item + '"'
return str(item)
if __name__ == "__main__":
print "This file is not intended to be run as stand alone!"