-
Notifications
You must be signed in to change notification settings - Fork 0
/
createCivilCentres.js
237 lines (205 loc) · 7.2 KB
/
createCivilCentres.js
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
const fs = require('fs')
const getModData = require("./helpers/getModData.js")
var pretty = require('pretty')
const path = require('path')
const buildObjectFromPath = require('./helpers/buildObjectFromPath.js')
const interpolate = require('./helpers/interpolate.js')
const changeCase = require('change-case')
const merge = require('deepmerge')
const X2JS = require('./x2js.js')
const x2js = new X2JS()
const truePhase = true
var config = {}
var modify = {}
function buildUnitTree(structureTree){
const buildTree = {}
delete structureTree.classes
const keys = Object.keys(structureTree)
for (let i = 0 ; i < keys.length; i++){
let phase = keys[i]
let availableStructures = structureTree[phase]
for (let j = 0; j < availableStructures.length; j++){
const structureName = availableStructures[j]
const structure = config.build_trees.structures[structureName]
buildTree[structureName]=structure
}
}
return buildTree
}
function buildStructureTree(tree){
const trees = []
if (typeof tree === 'string'){
tree = config.build_trees[tree]
}
if (typeof tree === 'object'){
if (tree.classes){
for (let i = 0 ; i < tree.classes.length; i++ ){
basetree = buildStructureTree(tree.classes[i])
tree = merge(tree, basetree)
}
}
}
return tree
}
async function createCivilCentres(){
config = await buildObjectFromPath(path.join(process.cwd(),'./config'))
modify = config.modify
var templates = []
config.active_mod = config.ars_bellica
const target_civs = [
"athen",
"brit",
"gaul",
"han"
]
const civs = config.civilizations
const keys = Object.keys(civs)
for ( let i = 0; i < keys.length; i ++ ){
const civ = civs[keys[i]]
if( target_civs.includes(civ.code) ){
const structureTree = buildStructureTree(civ.build_tree)
civ.build_tree = buildUnitTree(structureTree)
const tmp = await buildObjectFromPath(path.join(process.cwd(),'./civil_centre.yml'))
config.civ = civ
ccTemplate = interpolate(tmp.civil_centre, config)
ccTemplate.Entity.Identity.Classes = '\n\t\t'+ccTemplate.Entity.Identity.Classes.join('\n\t\t')+'\n'
ccTemplate.Entity.ProductionQueue.Technologies.__text = '\n\t\t'+ccTemplate.Entity.ProductionQueue.Technologies.__text.replace(/,/g,'\n\t\t')+'\n'
ccTemplate.Entity.ProductionQueue.Entities.__text = '\n\t\t'+ccTemplate.Entity.ProductionQueue.Entities.__text.toLowerCase().replace(/,/g,'\n\t\t')+'\n'
const phase_names = config.active_mod.phases.names
if( config.active_mod.phases.type === 'phase' ){
for ( let i = 0; i < phase_names.length; i++ ){
var template = ccTemplate
if( i+1 < phase_names.length){
promotion = {
RequiredXp: Math.round(12000 * (i*2+1)),
Entity: `structures/${civ.code}_civil_centre_${phase_names[i+1]}`
}
} else {
promotion = { "_disable": "" }
}
if( i > 0 ){
template = {
Entity: {
Identity: {
Classes: {
_datatype: "tokens",
__text: phase_names[i] + "Centre"
},
VisibleClasses: {
_datatype: "tokens",
__text: phase_names[i] + "Centre"
}
},
TerritoryInfluence: {
Radius: {
_op: "add",
__text: 30
},
Weight: {
_op: "add",
__text: 30
}
}
}
}
template.fileName = civ.code + '_civil_centre_' + phase_names[i]
} else {
template.fileName = civ.code + '_civil_centre'
}
if(i===1){
template.Entity._parent = `structures/${civ.code}_civil_centre`
}
if(i>1){
template.Entity._parent = `structures/${civ.code}_civil_centre_${phase_names[i-1]}`
}
template.Entity.Auras = {
_datatype: "tokens",
__text: "\n\t\tstructures/civil_centre_" + phase_names[i] + "_cs\n\t\tstructures/civil_centre_" + phase_names[i] + "_structure\n\t\t"
}
template.Entity.Identity.GenericName = changeCase.capitalCase(phase_names[i]) + " Centre",
template.Entity.Promotion = promotion
templates.push(template)
}
} else {
var template = ccTemplate
template.Entity.Identity.GenericName = "Civil Centre"
templates.push(template)
}
for( let j = 0 ; j < templates.length; j++ ){
var fileName = templates[j].fileName
delete templates[j].fileName
var xml = pretty(x2js.json2xml_str( templates[j] ))
xml = '<?xml version="1.0" encoding="utf-8"?>\n'+xml
fs.writeFileSync(path.join(process.cwd(),'dist/ars_bellica/simulation/templates/structures/',fileName +'.xml'),xml)
}
}
}
//Write Auras
const auras = config.auras
const auraKeys = Object.keys(auras)
for(let i = 0; i < auraKeys.length; i++){
const key = auraKeys[i]
const aura = auras[key]
const fileName = key
if(typeof aura === 'object'){
json = JSON.stringify(aura, null, 2)
fs.writeFileSync(path.join(process.cwd(), 'dist/ars_bellica/simulation/data/auras/structures/',fileName+'.json'),json)
}
}
const baseMods = await getModData()
var filtered = []
modify = modify.modify
for ( let i = 0; i < modify.length; i++ ){
if(modify[i].name){
const tmp = baseMods.filter(nameMatcher)
filtered = filtered.concat(tmp)
}
if(modify[i].class){
const tmp = baseMods.filter(classMatcher)
filtered = filtered.concat(tmp)
}
if(modify[i].merge){
for (let j = 0; j< tmp.length; j++){
const entity = filtered[j].data.Entity
const merge_data = modify[i].merge
filtered[j].data.Entity = merge(entity, merge_data)
}
}
}
for( let m = 0 ; m < filtered.length; m++ ){
const data = filtered[m].data
var xml = pretty(x2js.json2xml_str( data ))
xml = '<?xml version="1.0" encoding="utf-8"?>\n'+xml
// console.log(xml)
const writePath = path.join(__dirname, 'dist/ars_bellica/', filtered[m].modRelativePath)
fs.writeFileSync(writePath,xml)
}
}
createCivilCentres()
function nameFilter(el){
return el.modRelativePath.match(modify[i].name)
}
function filterEntities(el){
var classes = el.classes
var visibleClasses
try{
visibleClasses = el.data.Entity.Identity.VisibleClasses['#text'].split(' ')
} catch {}
if (visibleClasses) {
classes = classes.concat(visibleClasses)
}
const requiredClasses = modify[i].class.split(' ')
for (let k=0; k<requiredClasses.length; k++){
classes = classes.map((el)=>{
return changeCase.pascalCase(el)
})
requiredClases= requiredClasses.map((el)=>{
return changeCase.pascalCase(el)
})
const hasClass = classes.includes(requiredClasses[k])
if(!hasClass){
return false
}
}
return true
}