-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriter.kt
466 lines (410 loc) · 15.9 KB
/
Writer.kt
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
454
455
456
457
458
459
460
461
462
463
464
465
466
package external
import com.soywiz.kmem.ByteArrayBuilder
import com.soywiz.korio.file.std.resourcesVfs
import com.soywiz.korio.serialization.xml.Xml
import com.soywiz.korio.serialization.xml.children
import com.soywiz.korio.serialization.xml.readXml
import components.Surface
import steps.map.`object`.BuildingsManager
import util.Constants
import util.Constants.config
import util.Constants.mapEditorVersion
import util.Constants.matrixMap
import util.Constants.rnd
import util.Constants.zones
import com.soywiz.klock.DateTime
import com.soywiz.klock.TimeFormat
import com.soywiz.korio.file.std.rootLocalVfs
import components.CellType
import components.Fraction
import components.Resource
import steps.Guards
import steps.map.`object`.Castle
import steps.obstacle.ObstacleMapManager
import kotlin.math.pow
/**
* Class for exporting to .hmm
*/
class Writer(
private val obstacleMapManager: ObstacleMapManager,
private val buildingsManager: BuildingsManager,
private val guards: Guards
) {
private var file = rootLocalVfs[config.exportPath]
init {
file = rootLocalVfs[config.exportPath]
}
private val byteArrayBuilder = ByteArrayBuilder()
fun getPath() = file.path
companion object {
const val KEY = 1982026360
const val VER = 25
const val LNG_MASK = 1
const val I_NODE_SIZE = 2
}
suspend fun writeHeader() {
writeNBytes(KEY, 4) // header
writeNBytes(VER, 4) // version
writeSize()
writeNBytes(LNG_MASK, 4) // map language mask
writeNBytes(2, 4) // cnt
writeString("Map Description")
writeNBytes(2, 2) // tet
writeString("Default description")
// do not need tet here
writeString("Map name")
writeNBytes(1, 2) // tet
writeString(Constants.template.name)
// empty strings
writeNBytes(0, 4) // empty map version in ver>=0x15 (19)
writeNBytes(0, 4) // empty author name
// no time events
writeNBytes(0, 2)
// ultimate artifact position (x,y), each 2 bytes
writePoint(0, 0)
writeNBytes(0, 4) // something else with artifact (m_radUltimateArt)
// players
writePlayers()
// heroes count
writeNBytes(0, 2)
// map objects count
writeResources()
//writeNBytes(0, 2)
// guards count
writeGuards()
// events count
writeNBytes(0, 2)
// visitables count
writeVisitables()
// ownerables count
writeMines()
// castles
writeCastles()
// map dump
writeSurface()
// write decorations
DecorationsBuilder(obstacleMapManager).placeObstacles(this)
// paths count
writeRoads()
changeFile()
file.write(byteArrayBuilder.data)
}
private suspend fun changeFile() {
file = rootLocalVfs[config.exportPath.substring(0, config.exportPath.length - 4)
+ DateTime.now().time.format(TimeFormat.FORMAT_TIME).replace(':', '_')
+ ".hmm"]
if (file.exists()) {
println("exists")
file = rootLocalVfs[file.path.substring(0, file.path.length - 4) + "(1).hmm"]
}
}
private fun writeGuards() {
val guards = guards.guards.filter { it.level >= 0 }
writeNBytes(guards.size, 2)
for (guard in guards) {
writeNBytes(3840 + guard.level, 2)
writeNBytes(config.guardCount[guard.level].value, 4)
// disp
writeNBytes(1, 1)
// notGrow
writeNBytes(0, 1)
writePoint(guard.position)
if (mapEditorVersion > 0x18)
writeString("")
}
}
private fun writeSize() {
writeNBytes(
when (config.mapSize) {
32 -> 0
64 -> 1
128 -> 2
256 -> 3
else -> throw Exception("unsupported export map size")
}, 1
) // size
}
private fun writeRoads() {
writeNBytes(matrixMap.matrix.count { it.cellType == CellType.ROAD }, 4)
for (x in 0 until matrixMap.matrix.width)
for (y in 0 until matrixMap.matrix.height) {
if (matrixMap.matrix[x, y].cellType == CellType.ROAD) {
writePoint(x, y)
writeString(
when (matrixMap.matrix[x, y].zone.type) {
Surface.LAVA -> "stone_road"
Surface.SNOW -> "snow_road"
else -> "dirt_road"
}
)
}
}
}
private fun writeVisitables() {
writeNBytes(zones.sumOf { it.teleports.size }, 2)
for (z in zones) {
for (teleport in z.teleports) {
writeString(teleport.toString())
writePoint(teleport.position)
writeArmy()
writeString("")
}
}
}
private fun writePlayers() {
val numPlayers = zones.maxOf { z -> z.buildings.maxOf { it.playerColor } } + 1
writeNBytes(numPlayers, 2)
for (playerIndex in 1..numPlayers) {
// id
writeNBytes(playerIndex, 1)
// player type mask
var pCastle: Castle? = null
zones.forEach { zone ->
val castle = zone.castles.find { it.playerColor == playerIndex - 1 }
if (castle != null) pCastle = castle
}
if (pCastle != null) {
writeNBytes(pCastle!!.fraction.ordinal, 1)
// has main castle
writeNBytes(1, 1)
writePoint(pCastle!!.position)
// create hero in castle
if (config.spawnHeroAtCastle.value)
writeNBytes(1, 1)
else
writeNBytes(0, 1)
} else {
writeNBytes(Fraction.getRandom().ordinal, 1)
writeNBytes(0, 1)
}
}
}
private suspend fun writeResources() {
val artifacts = resourcesVfs["artifacts.xml"].readXml().children("Item").toMutableList()
writeNBytes(guards.treasures.size, 2)
for (treasure in guards.treasures) {
/**
* type 0 - res
* 1 - mana crystal
* 2 - campfire
* 3 - chest
* 4 - artifact
*/
writeNBytes(treasure.type.ordinal, 1)
writePoint(treasure.position)
writeArmy()
writeString("")
when (treasure.type.ordinal) {
0 -> {
writeNBytes(Resource.getRandomExcept(listOf()).ordinal, 1) // resource type
writeNBytes(0, 4) // amount, 0 for random
}
1 -> writeNBytes(0, 4) // amount
4 -> {
writeString(artifacts.random(rnd).attributes["id"]!!) // artifact name
}
else -> {
}
}
}
}
private fun writeCastles() {
writeNBytes(buildingsManager.castles.size, 2)
for (castle in buildingsManager.castles) {
// id
writeString(castle.orientation)
//type
writeNBytes(castle.fraction.ordinal, 1)
// owner
writeNBytes(castle.playerColor, 1)
// position
writePoint(castle.position)
// creatures
writeArmy()
// write text
writeString("")
//constructions count
writeNBytes(0, 2)
}
}
private fun writeMines() {
writeNBytes(buildingsManager.mines.size, 2)
for (mine in buildingsManager.mines) {
writeString(mine.toString())
writeNBytes(mine.playerColor, 1)
writePoint(mine.position)
writeArmy()
}
}
private fun writeArmy() {
for (i in 0..6) {
writeNBytes(-1, 2)
writeNBytes(0, 4)
}
}
private fun writeDecoration(position: Pair<Int, Int>, decoName: String) =
writeDecoration(position.first, position.second, decoName)
private fun writeDecoration(x: Int, y: Int, decoName: String) {
writePoint(x, y)
writeString(decoName)
}
private fun writePoint(point: Pair<Int, Int>) = writePoint(point.first, point.second)
private fun writePoint(x: Int, y: Int) {
writeNBytes(x, 2)
writeNBytes(y, 2)
}
private fun writeNBytes(data: Int, n: Int) {
val buffer = ByteArray(n)
for (i in 0 until n) buffer[i] = (data shr (i * 8)).toByte()
byteArrayBuilder.append(buffer)
}
private fun writeSurface() {
val size = matrixMap.matrix.width
val len = ((size + 1).toDouble().pow(2) * I_NODE_SIZE).toInt()
// transpose matrix - this way it resembles the view in "visualizeMatrix()" in KorGE
for (y in 0 until size) {
for (x in 0 until size)
writeNBytes(matrixMap.matrix[x, y].zone.type.ordinal, 2)
writeNBytes(matrixMap.matrix[matrixMap.matrix.width - 1, y].zone.type.ordinal, 2)
}
for (i in 0..matrixMap.matrix.width)
writeNBytes(matrixMap.matrix[matrixMap.matrix.width - 1, matrixMap.matrix.width - 1].zone.type.ordinal, 2)
//writeNBytes(0, len)
}
private fun writeString(str: String) {
val arr = str.toCharArray()
writeNBytes(arr.size, 4)
for (i in 0..arr.lastIndex)
writeNBytes(arr[i].code, 2)
}
class DecorationsBuilder(obstacleMapManager: ObstacleMapManager) {
private val obstacleFolders = listOf("mountains", "trees", "decals")
private lateinit var groups: MutableMap<Int, Map<Surface, List<Xml>>>
private val obstacle3Squares = createNSquares(obstacleMapManager, 3)
private val obstacle2Squares = createNSquares(obstacleMapManager, 2)
private var amount3 =
((obstacle3Squares.lastIndex / 4)..(obstacle3Squares.lastIndex * 4 / 5)).random(rnd)
private var amount2 =
((obstacle2Squares.lastIndex / 4)..(obstacle2Squares.lastIndex * 4 / 5)).random(rnd)
private val amount1 = getAmountOfObstacles(obstacleMapManager)
private lateinit var map: Map<Surface, List<Xml>>
private fun createNSquares(oMM: ObstacleMapManager, n: Int) = oMM.findAllNSquares(n)
private fun getAmountOfObstacles(oMM: ObstacleMapManager) = oMM.calculateAllObstacleCells()
private suspend fun parseGroups(): Map<Int, List<Xml>> {
val children = resourcesVfs["hmm.xml"].readXml()["Avatars"].children("Item")
val groups = children.filter {
it.children("Sprite")
.all { i -> obstacleFolders.contains(i.attribute("id").toString().lowercase().split(".")[0]) }
}.groupBy { it.children("PassModifier").count() }
return groups
}
suspend fun placeObstacles(writer: Writer) {
groups = mutableMapOf()
val placedMatrix =
Array(matrixMap.matrix.width)
{ BooleanArray(matrixMap.matrix.width) { false } }
//writeUnknown(groups)
for (i in parseGroups()) {
if (i.value[0].children("PassModifier").count() != 0)
groups[i.key] = groupBySurface(i.value)
}
writer.writeNBytes(amount3 + amount2 + amount1, 4)
for (i in obstacle3Squares.shuffled(rnd).take(amount3)) {
val decoration = groups[9]!![i.first]?.random(rnd)
if (decoration != null) {
writer.writeDecoration(
i.second.position,
decoration.attribute("id")!!
)
for (x in -2..0)
for (y in -2..0)
placedMatrix[x + i.second.position.first][y + i.second.position.second] = true
} else
writer.writeDecoration(0, 0, "flowers_8")
}
for (i in obstacle2Squares.shuffled(rnd).take(amount2)) {
val decoration = groups[4]!![i.first]?.random(rnd)
if (decoration != null) {
writer.writeDecoration(
i.second.position,
decoration.attribute("id")!!
)
for (x in -1..0)
for (y in -1..0)
placedMatrix[x + i.second.position.first][y + i.second.position.second] = true
} else
writer.writeDecoration(0, 0, "flowers_8")
}
var amountLeft = amount1
for (x in placedMatrix.indices)
for (y in placedMatrix.indices) {
val cell = matrixMap.matrix[x, y]
if (Constants.OBSTACLES.contains(cell.cellType) && !placedMatrix[x][y]) {
writer.writeDecoration(
cell.position,
groups[1]!![cell.zone.type]!!.random(rnd).attribute("id")!!
)
amountLeft--
}
}
for (i in 0 until amountLeft)
writer.writeDecoration(1, 0, "flowers_8")
}
suspend fun placeAll(writer: Writer) {
val groups = parseGroups()
val allObjects = groups.values.flatten()
writer.writeNBytes(allObjects.size, 4)
var x = 0
var y = 0
for (part in allObjects) {
writer.writeDecoration(x, y, part.attribute("id")!!)
x += 3
if (x > 64) {
y += 3
x = 0
}
}
}
private suspend fun writeUnknown(groups: Map<Int, List<Xml>>) {
for (g in groups) {
map = groupBySurface(g.value)
resourcesVfs[g.key.toString() + ".xml"].writeString(
"<Group id=\"${g.key}\">" + map[Surface.RANDOM]!!.toString().filter { it != ',' } + "</Group>")
}
}
/**
* Method to group list of decorations of same size to surfaces
* @return Map<surfaceName:list of decorations>
*/
private suspend fun groupBySurface(decorations: List<Xml>): Map<Surface, List<Xml>> {
// name of surface - list of decorations
val res = mutableMapOf<String, MutableList<Xml>>()
// get combat decorations and if decoName == combatDecos[i].name add
val combatObstacles = resourcesVfs["hmm.xml"].readXml()["CombatObstacles"].children("Item")
val customItems =
resourcesVfs["${decorations[0].children("PassModifier").count()}.xml"]
.readXml().children("Item")
for (item in decorations) {
val same = combatObstacles.find {
item.children("Sprite").all { i -> i.attribute("id") == it.attribute("sprite") }
}
if (same != null)
for (surf in same.children("Surf"))
res[surf.attribute("id")]?.add(item) ?: run {
res[surf.attribute(("id"))!!] = mutableListOf(item)
}
else {
val custom = customItems.find { item.attribute("id") == it.attribute("id") }
if (custom != null)
res[custom.child("Surf")!!.attribute("id")]?.add(item) ?: run {
res[custom.child("Surf")!!.attribute("id")!!] = mutableListOf(item)
}
//else res["UNKNOWN"]?.add(item) ?: run {
// res["UNKNOWN"] = mutableListOf(item)
//}
}
}
return res.mapKeys { Surface.valueOf(it.key.split("_").last()) }
}
}
}