/
material.go
687 lines (610 loc) · 15.3 KB
/
material.go
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
package df2014
import (
"reflect"
"strconv"
)
type Material struct {
Type MaterialType
Index int32 `df2014_version_min:"1205"`
Index23a int16 `df2014_version_max:"1169"`
}
func (m Material) prettyPrint(w *WorldDat, buf, indent []byte, outerTag reflect.StructTag) []byte {
convert := MaterialType.Convert
if w.Header.Version < 1205 {
convert = MaterialType.Convert23a
m.Index = int32(m.Index23a)
}
return prettyPrint(w, reflect.ValueOf(convert(m.Type, m.Index)), buf, indent, outerTag)
}
type MaterialList struct {
Type []MaterialType
Index []int32 `df2014_assert_same_length_as:"Type" df2014_version_min:"1205"`
Index23a []int16 `df2014_assert_same_length_as:"Type" df2014_version_max:"1169"`
}
func (ml MaterialList) prettyPrint(w *WorldDat, buf, indent []byte, outerTag reflect.StructTag) []byte {
convert := MaterialType.Convert
if w.Header.Version < 1205 {
convert = MaterialType.Convert23a
ml.Index = make([]int32, len(ml.Index23a))
for i, v := range ml.Index23a {
ml.Index[i] = int32(v)
}
}
if len(ml.Type) != len(ml.Index) {
return prettyPrint(w, reflect.ValueOf(struct {
Type []MaterialType
Index []int32
}{ml.Type, ml.Index}), buf, indent, "")
}
buf = append(buf, "(len = "...)
buf = strconv.AppendInt(buf, int64(len(ml.Type)), 10)
buf = append(buf, ") {"...)
indent = append(indent, '\t')
for i, mt := range ml.Type {
m := convert(mt, ml.Index[i])
buf = append(buf, indent...)
buf = strconv.AppendInt(buf, int64(i), 10)
buf = append(buf, ": "...)
buf = prettyPrint(w, reflect.ValueOf(m), buf, indent, "")
}
buf = append(buf, indent[:len(indent)-1]...)
buf = append(buf, '}')
return buf
}
type MaterialType uint16
func (mt MaterialType) Convert(index int32) interface{} {
switch {
case mt == 0:
return MaterialInorganic{uint16(mt), InorganicIndex(index)}
case index < 0:
return MaterialBuiltin{BuiltinIndex(mt), index}
case 19 <= mt && mt < 219:
return MaterialCreature{uint16(mt - 19), CreatureIndex(index)}
case 219 <= mt && mt < 419:
return MaterialFigure{uint16(mt - 219), index}
case 419 <= mt && mt < 619:
return MaterialPlant{uint16(mt - 419), PlantIndex(index)}
default:
return MaterialBuiltin{BuiltinIndex(mt), index}
}
}
func (mt MaterialType) Convert23a(index int32) interface{} {
switch mt {
case 0: // WOOD
return MaterialWood23a{MaterialType23a(mt), TreeIndex(index)}
case 1: // STONE_GRAY
return MaterialStone23a{MaterialType23a(mt), InorganicIndex(index)}
case 2: // STONE_LIGHT
return MaterialStone23a{MaterialType23a(mt), InorganicIndex(index)}
case 3: // STONE_DARK
return MaterialStone23a{MaterialType23a(mt), InorganicIndex(index)}
case 4: // GOLD
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 5: // IRON
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 6: // SILVER
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 7: // COPPER
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 8: // GEM_ORNAMENTAL
return MaterialGem23a{MaterialType23a(mt), GemIndex(index)}
case 9: // GEM_SEMI
return MaterialGem23a{MaterialType23a(mt), GemIndex(index)}
case 10: // GEM_PRECIOUS
return MaterialGem23a{MaterialType23a(mt), GemIndex(index)}
case 11: // GEM_RARE
return MaterialGem23a{MaterialType23a(mt), GemIndex(index)}
case 12: // BONE
return MaterialCreature23a{MaterialType23a(mt), CreatureIndex(index)}
case 13: // IVORY
return MaterialCreature23a{MaterialType23a(mt), CreatureIndex(index)}
case 14: // JADE
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 15: // HORN
return MaterialCreature23a{MaterialType23a(mt), CreatureIndex(index)}
case 16: // AMBER
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 17: // CORAL
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 18: // PEARL
return MaterialCreature23a{MaterialType23a(mt), CreatureIndex(index)}
case 19: // SHELL
return MaterialCreature23a{MaterialType23a(mt), CreatureIndex(index)}
case 20: // LEATHER
return MaterialCreature23a{MaterialType23a(mt), CreatureIndex(index)}
case 21: // ADAMANTINE
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 22: // SILK
return MaterialCreature23a{MaterialType23a(mt), CreatureIndex(index)}
case 23: // PLANT
return MaterialPlant23a{MaterialType23a(mt), PlantIndex(index)}
case 24: // GLASS_GREEN
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 25: // GLASS_CLEAR
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 26: // GLASS_CRYSTAL
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 27: // SAND
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 28: // WATER
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 29: // ZINC
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 30: // TIN
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 31: // COAL
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 32: // BRONZE
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 33: // BRASS
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 34: // STEEL
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 35: // PIGIRON
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 36: // PLATINUM
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 37: // ELECTRUM
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 38: // POTASH
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 39: // ASH
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 40: // PEARLASH
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 41: // LYE
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 42: // RENDERED_FAT
return MaterialCreature23a{MaterialType23a(mt), CreatureIndex(index)}
case 43: // SOAP_ANIMAL
return MaterialCreature23a{MaterialType23a(mt), CreatureIndex(index)}
case 44: // FAT
return MaterialCreature23a{MaterialType23a(mt), CreatureIndex(index)}
case 45: // MUD
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 46: // VOMIT
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 47: // BLOOD
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
case 48: // SLIME
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
default:
return MaterialBuiltin23a{MaterialType23a(mt), int16(index)}
}
}
type MaterialType23a int16
var materialTypes23a = [...]string{
0: "WOOD",
1: "STONE_GRAY",
2: "STONE_LIGHT",
3: "STONE_DARK",
4: "GOLD",
5: "IRON",
6: "SILVER",
7: "COPPER",
8: "GEM_ORNAMENTAL",
9: "GEM_SEMI",
10: "GEM_PRECIOUS",
11: "GEM_RARE",
12: "BONE",
13: "IVORY",
14: "JADE",
15: "HORN",
16: "AMBER",
17: "CORAL",
18: "PEARL",
19: "SHELL",
20: "LEATHER",
21: "ADAMANTINE",
22: "SILK",
23: "PLANT",
24: "GLASS_GREEN",
25: "GLASS_CLEAR",
26: "GLASS_CRYSTAL",
27: "SAND",
28: "WATER",
29: "ZINC",
30: "TIN",
31: "COAL",
32: "BRONZE",
33: "BRASS",
34: "STEEL",
35: "PIGIRON",
36: "PLATINUM",
37: "ELECTRUM",
38: "POTASH",
39: "ASH",
40: "PEARLASH",
41: "LYE",
42: "RENDERED_FAT",
43: "SOAP_ANIMAL",
44: "FAT",
45: "MUD",
46: "VOMIT",
47: "BLOOD",
48: "SLIME",
}
func (i MaterialType23a) prettyPrint(w *WorldDat, buf, indent []byte, outerTag reflect.StructTag) []byte {
return prettyPrintIndex(int64(i), uint64(i), materialTypes23a[:], buf)
}
type MaterialBuiltin23a struct {
Type MaterialType23a
Index int16
}
type MaterialWood23a struct {
Type MaterialType23a
Index TreeIndex
}
type MaterialStone23a struct {
Type MaterialType23a
Index InorganicIndex
}
type MaterialGem23a struct {
Type MaterialType23a
Index GemIndex
}
type MaterialCreature23a struct {
Type MaterialType23a
Index CreatureIndex
}
type MaterialPlant23a struct {
Type MaterialType23a
Index PlantIndex
}
type BuiltinIndex uint16
// keep in sync with builtin_mats in https://github.com/DFHack/df-structures/blob/master/df.materials.xml
var builtinMaterials = [...]string{
"INORGANIC",
"AMBER",
"CORAL",
"GLASS_GREEN",
"GLASS_CLEAR",
"GLASS_CRYSTAL",
"WATER",
"COAL",
"POTASH",
"ASH",
"PEARLASH",
"LYE",
"MUD",
"VOMIT",
"SALT",
"FILTH_B",
"FILTH_Y",
"UNKNOWN_SUBSTANCE",
"GRIME",
}
func (i BuiltinIndex) prettyPrint(w *WorldDat, buf, indent []byte, outerTag reflect.StructTag) []byte {
return prettyPrintIndex(int64(i), uint64(i), builtinMaterials[:], buf)
}
type MaterialBuiltin struct {
T BuiltinIndex
I int32
}
func (m MaterialBuiltin) prettyPrint(w *WorldDat, buf, indent []byte, outerTag reflect.StructTag) []byte {
buf = append(buf, '{')
indent = append(indent, '\t')
buf = append(buf, indent...)
buf = append(buf, "T: "...)
buf = prettyPrint(w, reflect.ValueOf(m.T), buf, indent, "")
if int(m.T) < len(builtinMaterials) {
buf = append(buf, " ("...)
buf = append(buf, builtinMaterials[m.T]...)
buf = append(buf, ')')
}
if m.I >= 0 {
buf = append(buf, indent...)
buf = append(buf, "I: "...)
buf = prettyPrint(w, reflect.ValueOf(m.I), buf, indent, "")
}
buf = append(buf, indent[:len(indent)-1]...)
buf = append(buf, '}')
return buf
}
type MaterialInorganic struct {
T uint16
I InorganicIndex
}
type MaterialCreature struct {
T uint16
I CreatureIndex
}
func (m MaterialCreature) prettyPrint(w *WorldDat, buf, indent []byte, outerTag reflect.StructTag) []byte {
buf = append(buf, '{')
indent = append(indent, '\t')
buf = append(buf, indent...)
buf = append(buf, "T: "...)
buf = prettyPrint(w, reflect.ValueOf(m.T), buf, indent, "")
// TODO: human-readable types
buf = append(buf, indent...)
buf = append(buf, "I: "...)
buf = prettyPrint(w, reflect.ValueOf(m.I), buf, indent, "")
buf = append(buf, indent[:len(indent)-1]...)
buf = append(buf, '}')
return buf
}
type MaterialFigure struct {
T uint16
I int32
}
func (m MaterialFigure) prettyPrint(w *WorldDat, buf, indent []byte, outerTag reflect.StructTag) []byte {
buf = append(buf, '{')
indent = append(indent, '\t')
buf = append(buf, indent...)
buf = append(buf, "T: "...)
buf = prettyPrint(w, reflect.ValueOf(m.T), buf, indent, "")
// TODO: human-readable types
buf = append(buf, indent...)
buf = append(buf, "I: "...)
buf = prettyPrint(w, reflect.ValueOf(m.I), buf, indent, "")
// TODO: human-readable indices
buf = append(buf, indent[:len(indent)-1]...)
buf = append(buf, '}')
return buf
}
type MaterialPlant struct {
T uint16
I PlantIndex
}
func (m MaterialPlant) prettyPrint(w *WorldDat, buf, indent []byte, outerTag reflect.StructTag) []byte {
buf = append(buf, '{')
indent = append(indent, '\t')
buf = append(buf, indent...)
buf = append(buf, "T: "...)
buf = prettyPrint(w, reflect.ValueOf(m.T), buf, indent, "")
// TODO: human-readable types
buf = append(buf, indent...)
buf = append(buf, "I: "...)
buf = prettyPrint(w, reflect.ValueOf(m.I), buf, indent, "")
buf = append(buf, indent[:len(indent)-1]...)
buf = append(buf, '}')
return buf
}
type ItemMaterialList struct {
Type []ItemType
Sub []int16 `df2014_assert_same_length_as:"Type"`
Material []MaterialType `df2014_assert_same_length_as:"Type"`
Index []int32 `df2014_assert_same_length_as:"Type"`
}
func (iml ItemMaterialList) prettyPrint(w *WorldDat, buf, indent []byte, outerTag reflect.StructTag) []byte {
if len(iml.Type) != len(iml.Sub) || len(iml.Sub) != len(iml.Material) || len(iml.Material) != len(iml.Index) {
return prettyPrint(w, reflect.ValueOf(struct {
Type []ItemType
Sub []int16
Material []MaterialType
Index []int32
}{iml.Type, iml.Sub, iml.Material, iml.Index}), buf, indent, outerTag)
}
buf = append(buf, "(len = "...)
buf = strconv.AppendInt(buf, int64(len(iml.Type)), 10)
buf = append(buf, ") {"...)
indent = append(indent, '\t')
for i, t := range iml.Type {
m := iml.Material[i].Convert(iml.Index[i])
buf = append(buf, indent...)
buf = strconv.AppendInt(buf, int64(i), 10)
buf = append(buf, ": "...)
buf = prettyPrint(w, reflect.ValueOf(t), buf, indent, "")
buf = append(buf, ": "...)
buf = prettyPrint(w, reflect.ValueOf(iml.Sub[i]), buf, indent, "")
buf = append(buf, ": "...)
buf = prettyPrint(w, reflect.ValueOf(m), buf, indent, "")
}
buf = append(buf, indent[:len(indent)-1]...)
buf = append(buf, '}')
return buf
}
type ItemType uint16
const (
ITBar ItemType = iota
ITSmallGem
ITBlock
ITRoughGem
ITBoulder
ITLog
ITDoor
ITFloodgate
ITBed
ITChair
ITChain
ITFlask
ITGoblet
ITInstrument
ITToy
ITWindow
ITCage
ITBarrel
ITBucket
ITAnimalTrap
ITTable
ITCoffin
ITStatue
ITCorpse
ITWeapon
ITArmor
ITShoes
ITShield
ITHelm
ITGloves
ITBox
ITBin
ITArmorStand
ITWeaponRack
ITCabinet
ITFigurine
ITAmulet
ITScepter
ITAmmo
ITCrown
ITRing
ITEarring
ITBracelet
ITLargeGem
ITAnvil
ITCorpsePiece
ITRemains
ITMeat
ITFish
ITFishRaw
ITVermin
ITPet
ITSeeds
ITPlant
ITLeather
ITPlantGrowth
ITThread
ITCloth
ITTotem
ITPants
ITBackpack
ITQuiver
ITCatapultParts
ITBallistaParts
ITSiegeAmmo
ITBallistaArrowHead
ITMechanism
ITTrapComponent
ITDrink
ITPowder
ITCheese
ITFood
ITLiquid
ITCoin
ITGlob
ITSmallRock
ITPipeSection
ITHatchCover
ITGrate
ITQuern
ITMillstone
ITSplint
ITCrutch
ITTractionBench
ITOrthopedicCast
ITTool
ITSlab
ITEgg
ITBook
)
var itemTypes = [...]string{
"bar",
"small gem",
"block",
"rough gem",
"boulder",
"log",
"door",
"floodgate",
"bed",
"chair",
"chain",
"flask",
"goblet",
"instrument",
"toy",
"window",
"cage",
"barrel",
"bucket",
"animal trap",
"table",
"coffin",
"statue",
"corpse",
"weapon",
"armor",
"shoes",
"shield",
"helm",
"gloves",
"box",
"bin",
"armor stand",
"weapon rack",
"cabinet",
"figurine",
"amulet",
"scepter",
"ammo",
"crown",
"ring",
"earring",
"bracelet",
"large gem",
"anvil",
"corpse piece",
"remains",
"meat",
"fish",
"fish raw",
"vermin",
"pet",
"seeds",
"plant",
"leather",
"plant growth",
"thread",
"cloth",
"totem",
"pants",
"backpack",
"quiver",
"catapult parts",
"ballista parts",
"siege ammo",
"ballista arrow head",
"mechanism",
"trap component",
"drink",
"powder",
"cheese",
"food",
"liquid",
"coin",
"glob",
"small rock",
"pipe section",
"hatch cover",
"grate",
"quern",
"millstone",
"splint",
"crutch",
"traction bench",
"orthopedic cast",
"tool",
"slab",
"egg",
"book",
}
func (i ItemType) prettyPrint(w *WorldDat, buf, indent []byte, outerTag reflect.StructTag) []byte {
return prettyPrintIndex(int64(i), uint64(i), itemTypes[:], buf)
}
type PlantGrowthList struct {
Plant []PlantIndex
Growth []int16 `df2014_assert_same_length_as:"Plant"`
}
func (pgl PlantGrowthList) prettyPrint(w *WorldDat, buf, indent []byte, outerTag reflect.StructTag) []byte {
if len(pgl.Plant) != len(pgl.Growth) {
return prettyPrint(w, reflect.ValueOf(struct {
Plant []PlantIndex
Growth []int16
}{pgl.Plant, pgl.Growth}), buf, indent, outerTag)
}
buf = append(buf, "(len = "...)
buf = strconv.AppendInt(buf, int64(len(pgl.Plant)), 10)
buf = append(buf, ") {"...)
indent = append(indent, '\t')
for i, p := range pgl.Plant {
g := pgl.Growth[i]
buf = append(buf, indent...)
buf = strconv.AppendInt(buf, int64(i), 10)
buf = append(buf, ": "...)
buf = prettyPrint(w, reflect.ValueOf(p), buf, indent, "")
buf = append(buf, ": "...)
buf = prettyPrint(w, reflect.ValueOf(g), buf, indent, "")
}
buf = append(buf, indent[:len(indent)-1]...)
buf = append(buf, '}')
return buf
}