-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
453 lines (386 loc) · 12.7 KB
/
index.ts
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
import fs from 'fs'
type Factions = {
byName: Record<string, Record<string, string> | undefined>
byCulture: Record<string, Record<string, string>[] | undefined>
}
const missingSkills = new Set()
main()
async function main() {
const f = factions('C:/Games/Rome - Total War/data/descr_sm_factions.txt')
const ud = unitsWithEverything(
'C:/Games/Rome - Total War/data/text/export_units.txt',
'C:/Games/Rome - Total War/data/export_descr_unit.txt',
'C:/Games/Rome - Total War/data/text/shared.txt',
f
)
//
fs.writeFileSync('./book/src/units.json', JSON.stringify(ud, null, 2), {
encoding: 'utf-8'
})
fs.writeFileSync('./book/src/factions.json', JSON.stringify(f, null, 2), {
encoding: 'utf-8'
})
console.log('end')
}
function factions(path: string): Factions {
const factionsText = fs.readFileSync(path, {
encoding: 'utf-8'
})
const factions: Record<string, Record<string, string>> = {}
let currentFactionName = ''
let currentFaction: Record<string, string> = {}
for (const line of factionsText.split('\n')) {
if (line.startsWith(';')) continue
const lineData = line.split('\t').filter((e) => e !== '')
if (lineData.length === 0) {
continue
}
const key = lineData[0].trim()
if (key == '') {
continue
}
const value = lineData.slice(1).join(' ').trim()
if (key === 'faction') {
if (currentFactionName === '') {
// initialize first faction
currentFactionName = value
currentFaction = { faction: value }
continue
}
// finalize previous faction
factions[currentFactionName] = currentFaction
// start new
currentFactionName = value
currentFaction = { faction: value }
continue
}
currentFaction[key] = value
}
// finalize last faction
factions[currentFactionName] = currentFaction
return {
byName: factions,
byCulture: groupBy(Object.values(factions), (f) => {
return f.culture
})
}
}
function unitsWithEverything(
unitsPath: string,
unitDescrPath: string,
skillsPath: string,
f: Factions
) {
const uMap = units(unitsPath)
const udMap = unitDescrs(unitDescrPath)
for (const [key, value] of uMap) {
const unit = key.replace(/_descr(_short)?$/, '')
const unitProps = udMap.get(unit)
if (unitProps === undefined) {
throw new Error(
`not found unit descr for: '${key}' (searched for '${unit}')`
)
}
if (key.endsWith('_descr_short')) {
unitProps.set('text_descr_short', value)
} else if (key.endsWith('_descr')) {
unitProps.set(
'text_descr',
value
.split('\\n')
.filter((s) => !s.includes('code fix') && !s.includes('not required'))
)
} else {
unitProps.set('text', value)
}
}
const s = skills(skillsPath)
return groupUnits(
Array.from(udMap.values())
.map(Object.fromEntries)
.filter(filterUnit)
.map((u) => patchUnit(u, s, f))
)
}
function hashStrArr(arr: string[]): string {
return arr.join('|')
}
function hashUnit(u: Record<string, string | string[] | undefined>): string {
const statsHashes = [
hashStrArr(u['stat_pri'] as string[]),
hashStrArr(u['stat_pri_armour'] as string[]),
hashStrArr(u['stat_sec'] as string[]),
hashStrArr(u['stat_sec_armour'] as string[]),
hashStrArr(u['stat_ground'] as string[])
]
return `${u['text'] as string}_${statsHashes.join('_')}`
}
function groupUnits(units: Record<string, string | string[] | undefined>[]) {
const groups = groupBy(units, hashUnit)
const resultUnits: Record<string, string | string[] | undefined>[] = []
for (const key in groups) {
const units = groups[key]
const firstUnit = units[0]
if (units.length > 1) {
const firstUnitOwners = firstUnit['ownership'] as string[]
const firstUnitImgs = firstUnit['img'] as string[]
for (const otherUnit of units.slice(1)) {
for (const owner of otherUnit['ownership'] as string[]) {
firstUnitOwners.push(owner)
}
for (const img of otherUnit['img'] as string[]) {
firstUnitImgs.push(img)
}
}
if (firstUnitImgs.length > 10) {
console.log(
`Too many images (${firstUnitImgs.length}): ${firstUnit['text']}`
)
}
}
resultUnits.push(firstUnit)
}
return resultUnits
}
function filterUnit(
unit: Record<string, string | string[] | undefined>
): boolean {
if (unit['category'] === 'non_combatant') {
return false
}
return true
}
function patchUnit(
unit: Record<string, string | string[] | undefined>,
skills: Record<string, string>,
factions: Factions
): Record<string, string | string[] | undefined> {
const name = unit['dictionary']
if (!name || typeof name !== 'string') {
throw new Error('no name')
}
// TODO: ownership
if (typeof unit['ownership'] !== 'string') {
throw new Error('no ownership')
}
const ownershipWithCultures = unit['ownership']
.split(',')
.map((o) => o.trim())
const ownership: string[] = []
for (const factionOrCulture of ownershipWithCultures) {
const cultureFactions = factions.byCulture[factionOrCulture]
if (cultureFactions) {
ownership.push(...cultureFactions.map((f) => f.faction.trim()))
} else {
ownership.push(factionOrCulture.trim())
}
}
unit['ownership'] = ownership
// img
const images: string[] = []
for (const owner of ownership) {
let prefix = owner.toUpperCase()
const unitName = name.toUpperCase()
if (unitName.startsWith('MERC')) {
prefix = 'MERC'
}
const imgPath = `units/${prefix}___${unitName}_INFO.png`
let invalidImg = false
try {
if (!fs.statSync(`./book/public/${imgPath}`).isFile()) {
throw new Error('not a file')
}
} catch (e: unknown) {
if (ownership.length > 1) {
// console.log(`skipping img of ${imgPath}`)
invalidImg = true
} else {
invalidImg = true
console.log(`(!) skipping unit ${imgPath}`)
}
// console.log((e as Error).message)
}
if (!invalidImg && !images.includes(imgPath)) {
images.push(imgPath)
}
}
unit['img'] = images
if (images.length > 6) {
console.log(`Too many images (${images.length}): ${name}`)
}
// attributes
const attrs = unit['attributes']
if (!attrs || typeof attrs !== 'string') {
throw new Error('no attrs')
}
const attrsArr = attrs.split(',').map((a) => a.trim())
if (!attrsArr || attrsArr.length == 0) {
throw new Error(`no attributes for '${name}'`)
}
const updatedSkills: Record<string, string> = {
...skills,
frighten_foot: 'Пугают пехоту',
frighten_mounted: 'Пугают кавалерию',
general_unit: 'Могут охранять генерала',
can_run_amok: 'Входят в ярость, когда всадник теряет контроль',
command: 'Несут легендарного орла, дающего бонус ближайшим подразделениям',
mercenary_unit: 'Наемники'
}
const resultAttrs: string[] = []
if (!attrsArr.includes('sea_faring')) {
resultAttrs.push('Не могут путешествовать на судне')
}
if (
[
'hide_forest',
'hide_improved_forest',
'hide_long_grass',
'hide_anywhere'
].findIndex((e) => attrsArr.includes(e)) === -1
) {
resultAttrs.push('Не могут спрятаться')
}
for (const a of attrsArr) {
if (!updatedSkills[a]) {
if (!missingSkills.has(a)) {
// console.log(`no skill translation for '${a}'`)
missingSkills.add(a)
}
continue
}
resultAttrs.push(updatedSkills[a])
}
unit['attributes'] = resultAttrs
// stat_pri
const statPri = unit['stat_pri']
if (!statPri || typeof statPri !== 'string') {
throw new Error('no stat_pri')
}
const statPriArr = statPri
.split(',')
.slice(0)
.map((a) => a.trim())
unit['stat_pri'] = statPriArr
// stat_sec
const statSec = unit['stat_sec']
if (!statSec || typeof statSec !== 'string') {
throw new Error('no stat_sec')
}
const statSecArr = statSec
.split(',')
.slice(0)
.map((a) => a.trim())
unit['stat_sec'] = statSecArr
// stat_pri_armour
const statPriArmour = unit['stat_pri_armour']
if (!statPriArmour || typeof statPriArmour !== 'string') {
throw new Error('no stat_pri_armour')
}
const statPriArmourArr = statPriArmour
.split(',')
.slice(0)
.map((a) => a.trim())
unit['stat_pri_armour'] = statPriArmourArr
// stat_sec_armour
const statSecArmour = unit['stat_sec_armour']
if (!statSecArmour || typeof statSecArmour !== 'string') {
throw new Error('no stat_sec_armour')
}
const statSecArmourArr = statSecArmour
.split(',')
.slice(0)
.map((a) => a.trim())
unit['stat_sec_armour'] = statSecArmourArr
// stat_ground
const statGround = unit['stat_ground']
if (!statGround || typeof statGround !== 'string') {
throw new Error('no stat_ground')
}
const statGroundArr = statGround
.split(',')
.slice(0)
.map((a) => a.trim())
unit['stat_ground'] = statGroundArr
// synonyms
unit['attributes'] = unit['attributes'].map((a) => {
if (a == 'Отличная выносливость') return 'Выносливые'
if (a == 'Очень хорошая выносливость') return 'Очень выносливые'
if (a == 'Могут строиться в кантабрийский круг')
return 'Умеют строиться кантабрийским кругом'
return a
})
unit['text_descr'] = (unit['text_descr'] as string[]).map((a) => {
a = a.trim()
if (a == 'Могут выйти из-под контроля')
return 'Входят в ярость, когда всадник теряет контроль'
if (a == 'Выносливые\\Могут выйти из-под контроля')
return 'Входят в ярость, когда всадник теряет контроль'
if (a == 'Выносливые\\Прекрасно прячутся в лесах')
return 'Прекрасно прячутся в лесах'
if (a == 'Умеют образовывать кантабрийский круг')
return 'Умеют строиться кантабрийским кругом'
if (a == 'Способность прятаться в высокой траве')
return 'Могут прятаться в высокой траве'
if (a == 'Внушают ужас пехоте') return 'Пугают пехоту'
return a
})
unit['text_descr'] = (unit['text_descr'] as string[]).filter((d) => d)
// duplicates in text_descr
unit['text_descr'] = (unit['text_descr'] as string[]).filter(
(l) => !(unit['attributes'] as string[]).includes(l)
)
return unit
}
function skills(path: string) {
const skillsText = fs.readFileSync(path, {
encoding: 'utf-16le'
})
let skills: Record<string, string> = {}
for (const m of skillsText.matchAll(/^{UA_([^}]+)}\s*(.+)$/gm)) {
skills[m[1].toLowerCase()] = m[2]
}
return skills
}
function units(path: string) {
const unitsText = fs.readFileSync(path, {
encoding: 'utf-16le'
})
let units = new Map<string, string>()
for (const m of unitsText.matchAll(/{([a-zA-Z_-]+)}(\s|\r\n)(.+)/gm)) {
units.set(m[1], m[3])
}
return units
}
function unitDescrs(path: string) {
const unitDescrs = new Map<string, Map<string, string | string[]>>()
let currentUnit: Map<string, string | string[]> | null = null
const descrUnitText = fs.readFileSync(path, {
encoding: 'utf-8'
})
for (let [_, name, value] of descrUnitText.matchAll(
/^(\w+)\s+([^;\n]+)(;|$)/gm
)) {
value = value.trim()
if (name == 'dictionary') {
if (!unitDescrs.has(value)) {
unitDescrs.set(value, new Map())
}
currentUnit = unitDescrs.get(value)!
}
currentUnit?.set(name, value)
}
return unitDescrs
}
function groupBy<T>(
array: T[],
keyFunction: (item: T) => string | number
): Record<string, T[]> {
return array.reduce((result, item) => {
const key = keyFunction(item)
if (!result[key]) {
result[key] = []
}
result[key].push(item)
return result
}, {} as Record<string, T[]>)
}