-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayerSave.nim
More file actions
855 lines (736 loc) · 24.7 KB
/
Copy pathplayerSave.nim
File metadata and controls
855 lines (736 loc) · 24.7 KB
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
import
os,
strutils,
sequtils,
tables,
json,
strformat
import
formats,
jsontools
# --- Types ---
type
PlayerSave* = object
slot*: int = 0
data*: seq[JsonNode]
SetterProc* = proc(ps: var PlayerSave, value: JsonNode)
const StorageSlot* = {
"head": (index: 3, x: 0, y: 0),
"body": (index: 3, x: 0, y: 1),
"legs": (index: 3, x: 0, y: 2),
"accessory1": (index: 3, x: 1, y: 0),
"accessory2": (index: 3, x: 1, y: 1),
"accessory3": (index: 3, x: 1, y: 2),
"accessory4": (index: 3, x: 2, y: 0),
"accessory5": (index: 3, x: 2, y: 1),
"accessory6": (index: 3, x: 2, y: 2),
"ammo1": (index: 3, x: 3, y: 0),
"ammo2": (index: 3, x: 3, y: 1),
"ammo3": (index: 3, x: 3, y: 2),
"vanityhead": (index: 3, x: 4, y: 0),
"vanitybody": (index: 3, x: 4, y: 1),
"vanitylegs": (index: 3, x: 4, y: 2),
"coin1": (index: 3, x: 5, y: 0),
"coin2": (index: 3, x: 5, y: 1),
"coin3": (index: 3, x: 5, y: 2),
"coin4": (index: 3, x: 6, y: 0),
"pet": (index: 3, x: 6, y: 2),
"hook": (index: 3, x: 7, y: 0),
"mount": (index: 3, x: 7, y: 1),
"astralbox1": (index: 4, x: 0, y: 0),
"astralbox2": (index: 4, x: 1, y: 0),
"astralbox3": (index: 4, x: 2, y: 0),
"astralbox4": (index: 4, x: 3, y: 0),
"astralbox5": (index: 4, x: 4, y: 0),
"astralbox6": (index: 4, x: 0, y: 1),
"astralbox7": (index: 4, x: 1, y: 1),
"astralbox8": (index: 4, x: 2, y: 1),
"astralbox9": (index: 4, x: 3, y: 1),
"astralbox10": (index: 4, x: 4, y: 1),
"astralbox11": (index: 4, x: 0, y: 2),
"astralbox12": (index: 4, x: 1, y: 2),
"astralbox13": (index: 4, x: 2, y: 2),
"astralbox14": (index: 4, x: 3, y: 2),
"astralbox15": (index: 4, x: 4, y: 2),
"astralbox16": (index: 4, x: 0, y: 3),
"astralbox17": (index: 4, x: 1, y: 3),
"astralbox18": (index: 4, x: 2, y: 3),
"astralbox19": (index: 4, x: 3, y: 3),
"astralbox20": (index: 4, x: 4, y: 3),
"astralbox21": (index: 4, x: 0, y: 4),
"astralbox22": (index: 4, x: 1, y: 4),
"astralbox23": (index: 4, x: 2, y: 4),
"astralbox24": (index: 4, x: 3, y: 4),
"astralbox25": (index: 4, x: 4, y: 4),
"astralbox26": (index: 4, x: 0, y: 5),
"astralbox27": (index: 4, x: 1, y: 5),
"astralbox28": (index: 4, x: 2, y: 5),
"astralbox29": (index: 4, x: 3, y: 5),
"astralbox30": (index: 4, x: 4, y: 5),
"hotbar1": (index: 5, x: 0, y: 0),
"hotbar2": (index: 5, x: 1, y: 0),
"hotbar3": (index: 5, x: 2, y: 0),
"hotbar4": (index: 5, x: 3, y: 0),
"hotbar5": (index: 5, x: 4, y: 0),
"hotbar6": (index: 5, x: 5, y: 0),
"hotbar7": (index: 5, x: 6, y: 0),
"hotbar8": (index: 5, x: 7, y: 0),
"hotbar9": (index: 5, x: 8, y: 0),
"hotbar10": (index: 5, x: 9, y: 0),
"inventory1": (index: 5, x: 0, y: 1),
"inventory2": (index: 5, x: 1, y: 1),
"inventory3": (index: 5, x: 2, y: 1),
"inventory4": (index: 5, x: 3, y: 1),
"inventory5": (index: 5, x: 4, y: 1),
"inventory6": (index: 5, x: 5, y: 1),
"inventory7": (index: 5, x: 6, y: 1),
"inventory8": (index: 5, x: 7, y: 1),
"inventory9": (index: 5, x: 8, y: 1),
"inventory10": (index: 5, x: 9, y: 1),
"inventory11": (index: 5, x: 10, y: 1),
"inventory12": (index: 5, x: 0, y: 2),
"inventory13": (index: 5, x: 1, y: 2),
"inventory14": (index: 5, x: 2, y: 2),
"inventory15": (index: 5, x: 3, y: 2),
"inventory16": (index: 5, x: 4, y: 2),
"inventory17": (index: 5, x: 5, y: 2),
"inventory18": (index: 5, x: 6, y: 2),
"inventory19": (index: 5, x: 7, y: 2),
"inventory20": (index: 5, x: 8, y: 2),
"inventory21": (index: 5, x: 9, y: 2),
"inventory22": (index: 5, x: 10, y: 2),
"inventory23": (index: 5, x: 0, y: 3),
"inventory24": (index: 5, x: 1, y: 3),
"inventory25": (index: 5, x: 2, y: 3),
"inventory26": (index: 5, x: 3, y: 3),
"inventory27": (index: 5, x: 4, y: 3),
"inventory28": (index: 5, x: 5, y: 3),
"inventory29": (index: 5, x: 6, y: 3),
"inventory30": (index: 5, x: 7, y: 3),
"inventory31": (index: 5, x: 8, y: 3),
"inventory32": (index: 5, x: 9, y: 3),
"inventory33": (index: 5, x: 10, y: 3),
"inventory34": (index: 5, x: 0, y: 4),
"inventory35": (index: 5, x: 1, y: 4),
"inventory36": (index: 5, x: 2, y: 4),
"inventory37": (index: 5, x: 3, y: 4),
"inventory38": (index: 5, x: 4, y: 4),
"inventory39": (index: 5, x: 5, y: 4),
"inventory40": (index: 5, x: 6, y: 4),
"inventory41": (index: 5, x: 7, y: 4),
"inventory42": (index: 5, x: 8, y: 4),
"inventory43": (index: 5, x: 9, y: 4),
"inventory44": (index: 5, x: 10, y: 4),
}.toTable
let Affixes* = {
1: "Useless",
2: "Deteriorated",
3: "Old",
4: "Slow",
5: "Fast",
6: "Powerful",
7: "Overpowered",
8: "Legendary",
9: "Lucky",
10: "Critical",
11: "Beast",
12: "Compressed",
13: "Brutal",
14: "Bloody",
15: "Deadly",
16: "Swift",
17: "Rapid",
18: "Hurricane",
19: "Smart",
20: "Blessed",
21: "Spiritual",
22: "Mystic",
23: "Arcane",
24: "Ethereal",
25: "Enlightened",
26: "Mystical",
27: "Enduring",
28: "Energetic",
29: "Vigorous",
30: "Vital",
31: "Fierce",
32: "Destructive",
33: "Savage",
34: "Merciless",
35: "Sturdy",
36: "Guarded",
37: "Unbreakable",
38: "Fortified",
39: "Lightning",
40: "Frenzied",
41: "Turbo",
42: "Blazing",
# 0: "Solid",
# 0: "Hard",
# 0: "Heavy",
}.toTable
# --- Setters table ---
var setters*: Table[string, SetterProc] = initTable[string, SetterProc]()
# --- Utility to set a field inside data ---
proc setField*(ps: var PlayerSave, idx: int, field: string, value: JsonNode) =
if idx >= ps.data.len:
ps.data.setLen(idx + 1)
ps.data[idx] = %*{}
if ps.data[idx].kind != JObject:
ps.data[idx] = %*{}
ps.data[idx][field] = value
# --- Register normal field setter ---
setters["maxHp"] = proc(ps: var PlayerSave, value: JsonNode) =
setField(ps, 2, "hpMax", value)
setters["maxMp"] = proc(ps: var PlayerSave, value: JsonNode) =
setField(ps, 2, "mpMax", value)
# --- Register cheats setter with arbitrary side effects ---
setters["cheats"] = proc(ps: var PlayerSave, value: JsonNode) =
if value.getBool:
setField(ps, 2, "hpMax", %1000)
setField(ps, 2, "hpCurrent", %1000)
# could also update undo data, flags, etc.
else:
setField(ps, 2, "hpMax", %500)
setField(ps, 2, "hpCurrent", %500)
# --- Operators ---
proc `[]=`*[T](ps: var PlayerSave, key: string, value: T) =
# Generic setter: converts any Nim type to JsonNode
let jvalue = % value
if setters.hasKey(key):
setters[key](ps, jvalue)
else:
for i in 0..<ps.data.len:
if ps.data[i].kind == JObject and ps.data[i].hasKey(key):
setField(ps, i, key, jvalue)
return
proc `[]`*(ps: PlayerSave, key: string): JsonNode =
case key
of "version":
return ps.data[0]
of "maxHp":
if ps.data.len > 2 and ps.data[2].hasKey("hpMax"):
return ps.data[2]["hpMax"]
else:
return % 0
else:
for i in 0..<ps.data.len:
if ps.data[i].kind == JObject and ps.data[i].hasKey(key):
return ps.data[i][key]
return % 0
# if ps.data.len > 0 and ps.data[2].hasKey(key):
# return ps.data[2][key]
# else:
# return % 0
let npcShopIds* = @[
0, # The Guide
1, # The Blacksmith
3, # The Merchant
5, # The Bard
6, # The Witch
8, # The Miner
9, # The Farmer
10, # The Carpenter
11, # The Skeleton
12, # The Chef
13, # The Fisherman
14, # The Summoner
15, # The Electrician
18, # The Stylist
21, # The Cartographer
26, # The Nurse
28, # The Robot
29, # The Penguin
30, # The Enchantress
]
let shopOrder* = %*
[
# The Guide
1708, # Portable Guide
222, # Guide Suit
223, # Guide Pants
# The Blacksmith
39, # Copper Ore
37, # Iron Ore
38, # Gold Ore
62, # Spider Ore
301, # Serpentinite Ore
650, # Bloody Ore
344, # Spectrite Ore
385, # Coralite Ore
545, # Solarite Ore
546, # Lunarite Ore
1499, # Molten Ore
1701, # Blacksmith Blessing
226, # Blacksmith Shirt
227, # Blacksmith Apron
# The Merchant
80, # Flippers
879, # Poison Arrow
1145, # Bouncing Arrow
961, # Beetle Arrow
398, # Thunder Arrow
397, # Coral Arrow
159, # Ghost Arrow
1725, # Pocket Merchant
229, # Merchant Blouse
230, # Merchant Skirt
# The Bard
1726, # Bard’s Guitar
1727, # Drum
1728, # Harp
238, # Bard Clothes
239, # Bard Pants
# The Witch
1711, # Mushroom Grower
247, # Witch Coat
248, # Witch Skirt
# The Miner
306, # Cocobomb
388, # Pufferfish Bomb
1144, # Bomb Arrow
288, # Miner Suit
289, # Miner Pants
# The Farmer
1712, # Farmer Visor
492, # Farmer Suit
493, # Farmer Pants
# The Carpenter
1713, # Astral Box
526, # Carpenter Suit
527, # Carpenter Pants
#11 The Skeleton
1762, # Box of Frights
#12 The Chef
1707, # Spicer
735, # Chef Suit
736, # Chef Pants
#13 The Fisherman
1724, # Tracker Bait
738, # Fisherman Suit
739, # Fisherman Pants
#14 The Summoner
291, # Slime Voodoo Doll
685, # Cursed Doll
849, # Forbidden Scroll
963, # Mineral Spider Egg
972, # Love Letter
973, # Krill Snack
974, # Frog Toy
975, # Pocket Thumper
976, # Bloody Crown
977, # Spoiled Alioli
978, # Beetle Whistle
1239, # Fae Gem
1299, # Goo-Stained Needles
1300, # Crab Juice
1642, # Magma Worm Larva
1644, # Security Control Console
1921, # Eclipse Phoenix Egg
1710, # Truth Potion
795, # Summoner Suit
796, # Summoner Pants
#15 The Electrician
1736, # Flamethrower Trap
1737, # Poison Trap
858, # Anemone Statue
827, # Angry Cactus Statue
859, # Anglerfish Statue
1256, # Anubis Statue
1258, # Ancient Mummy Statue
1612, # Ash Elemental Statue
833, # Archer Skeleton Statue
1982, # Army Merman Statue
1874, # Biting Tome Statue
1316, # Blood Slime Statue
1255, # Blood Thing Statue
828, # Crocodile Statue
829, # Crotalus Statue
1926, # Dandelion Statue
718, # Fluffy Snow Statue
838, # Ghoul Statue
1294, # Gooblin Statue
1254, # Golem Statue
1295, # Goop Hog Statue
834, # General Skeleton Statue
1627, # Guardian Robot Statue
839, # Hooded Cultist Statue
1259, # Hypnotoad Statue
1296, # Human Berserker Statue
860, # Jellyfish Statue
1980, # Kitsune Statue
1260, # Lamia Statue
721, # Leech Statue
717, # Light Muncher Statue
1873, # Living Enigma Statue
1869, # Living Secret Statue
1614, # Magma Slime Statue
830, # Maneater Statue
891, # Merman Statue
722, # Mongos Statue
831, # Monkey Statue
719, # Mosquito Statue
715, # Mummy Statue
835, # Necromancer Statue
861, # Octopus Statue
714, # Orc Statue
832, # Phoneutria Statue
723, # Phongos Statue
1317, # Pirate Statue
1265, # Pumpkin Statue
862, # Pufferfish Statue
1257, # Pyramid Beetle Statue
1613, # Salamander Statue
1266, # Scarecrow Statue
1979, # Scorpion Statue
1977, # Shooting Flower Statue
1269, # Siren Statue
716, # Skeleton Statue
725, # Slime Statue
1978, # Spider Mini Statue
1267, # Spookie Ghost Statue
1927, # Spriggan Statue
863, # Squidcannon Statue
724, # Swamp Thing Statue
1261, # Swamp Slime Statue
864, # Swordfish Statue
1695, # Thug Penguin Statue
720, # Toad Statue
1263, # Tricolor Mage Statue
1264, # Tricolor Slime Statue
892, # Tribe Statue
943, # Turtle General Statue
1268, # Turtle Statue
836, # Undertaker Statue
893, # Venomous Plant Statue
837, # Vampire Statue
1981, # Wanderer Statue
1866, # Wild Dryad Statue
1938, # Wild Dryad Dog Statue
1252, # Wolf Statue
1253, # Yeti Statue
#18 The Stylist
1714, # Tailor Needle
846, # Stylist Suit
847, # Stylist Pants
#21 The Cartographer
1851, # Pumpkin Map
854, # Jungle Map
855, # Ghostlands Map
856, # Coraline Fields Map
1853, # Tricolor Map
1852, # Goblin Map
1636, # Volcano Map
1856, # Day Map
1857, # Night Map
#26 The Nurse
281, # High HP Potion
282, # High MP Potion
658, # Healing Staff
1709, # Emergency Potion
1380, # Nurse Suit
1381, # Nurse Pants
#28 The Robot
1738, # Teleport Platform
#29 The Penguin
1704, # Portable Safe
#30 The Enchantress
1976, # Rune Shard Magnet
1785, # Enchantress Suit
1786, # Enchantress Skirt
]
proc version*(save: PlayerSave): int =
save.data[0].getInt
proc sortShopItems*(node: var JsonNode) =
for npcId in npcShopIds:
var j = %* []
let key = $npcId
for item in shopOrder:
if % item.getInt.float in node["npcs"][key]["shopExtraItems"]:
j.add(% item.getInt.float)
for item in node["npcs"][key]["shopExtraItems"]:
if item notin j:
j.add(item)
node["npcs"][key]["shopExtraItems"] = j
proc extractPlayer*(saveFile: string) =
# createFolders("data" / "player")
var index = 0
var version = 0
var useVersion = 0
echo fmt"reading {saveFile}"
for line in lines(saveFile):
var str:string
var ext:string
if index == 0:
version = line.parseInt
useVersion = version
# fall back to the closest version below the given
while not saveFormat.hasKey(useVersion):
dec useVersion
if useVersion <= 0:
quit fmt"Could not process save version {version}."
if saveFormat[useVersion][index] == "json":
str = prettyJson(line)
ext = "json"
elif saveFormat[useVersion][index] == "jstring":
str = unstringifyJsonString(line)
ext = "json"
elif saveFormat[useVersion][index] == "number":
str = line
ext = "txt"
let filename = fmt"data/player/output.{index+1}.{ext}" / ""
writeFile(filename, str)
echo fmt"wrote {filename}"
inc index
proc buildPlayer*(saveFile: string) =
let version = readFile("data/player/output.1.txt").parseInt
var useVersion = version
# fall back to the closest version below the given
while not saveFormat.hasKey(useVersion):
dec useVersion
if useVersion <= 0:
quit fmt"Could not process save version {version}."
var index = 0
var lines: seq[string] = @[]
for entry in saveFormat[useVersion]:
if saveFormat[useVersion][index] == "json":
lines.add minifyJson(readFile(fmt"data/player/output.{index+1}.json"))
elif saveFormat[useVersion][index] == "jstring":
lines.add stringifyJsonString(readFile(fmt"data/player/output.{index+1}.json"))
elif saveFormat[useVersion][index] == "number":
lines.add readFile(fmt"data/player/output.{index+1}.txt")
inc index
lines.add ""
writeFile(saveFile, lines.join("\r\n"))
echo fmt"wrote {saveFile}"
proc getItemInSlot*(p: PlayerSave, slot: string): JsonNode =
let slot = StorageSlot[slot]
var node = p.data[slot.index]["items"]
if node.kind == JArray:
for item in node.elems:
if item[1].getFloat == slot.x.float and item[2].getFloat == slot.y.float:
return item
return newJNull()
proc updatePlayerVisuals*(p: var PlayerSave, slot: string, index: int) =
let node = p.getItemInSlot(slot)
if node.kind == JNull:
p.data[index] = % -1
else:
p.data[index] = % node[0].getFloat.int
proc updatePlayerVisuals*(p: var PlayerSave) =
p.updatePlayerVisuals("head", 6)
p.updatePlayerVisuals("body", 7)
p.updatePlayerVisuals("legs", 8)
p.updatePlayerVisuals("vanityhead", 9)
p.updatePlayerVisuals("vanitybody", 10)
p.updatePlayerVisuals("vanitylegs", 11)
proc loadPlayer*(saveFile: string): PlayerSave =
var index = 0
var version = 0
var useVersion = 0
for line in lines(saveFile):
var str:string
var ext:string
if index == 0:
version = line.parseInt
useVersion = version
# fall back to the closest version below the given
while not saveFormat.hasKey(useVersion):
dec useVersion
if useVersion <= 0:
quit fmt"Could not process save version {version}."
if saveFormat[useVersion][index] == "json":
str = prettyJson(line)
ext = "json"
elif saveFormat[useVersion][index] == "jstring":
str = unstringifyJsonString(line)
ext = "json"
elif saveFormat[useVersion][index] == "number":
str = line
ext = "txt"
result.data.add(parseJson(str))
inc index
proc save*(ps: var PlayerSave, saveFile: string) =
let version = ps.version
var useVersion = version
var lines: seq[string] = @[]
# fall back to the closest version below the given
while not saveFormat.hasKey(useVersion):
dec useVersion
if useVersion <= 0:
quit fmt"Could not process save version {version}."
ps.updatePlayerVisuals
for i in 0..<ps.data.len:
if saveFormat[useVersion][i] == "json":
lines.add minifyJson($ps.data[i])
elif saveFormat[useVersion][i] == "jstring":
lines.add stringifyJsonString($ps.data[i])
elif saveFormat[useVersion][i] == "number":
lines.add $ps.data[i].getInt
lines.add ""
writeFile(saveFile, lines.join("\r\n"))
echo fmt"wrote {saveFile}"
proc save*(ps: var PlayerSave) =
let filename = fmt"savegame0{ps.slot}.player.new" / ""
ps.save(filename)
proc equipItem*(p: PlayerSave, id: int, slot: string, amount: int = 1) =
let slot = StorageSlot[slot]
var node = p.data[slot.index]["items"]
# remove any item in that slot
if node.kind == JArray:
node.elems = node.elems.filterIt(
it.kind == JArray and (
not (it[1].getFloat == slot.x.float and it[2].getFloat == slot.y.float)
)
)
node.add(%*[id.float,slot.x.float,slot.y.float,amount.float,0.0,nil,nil])
proc stripFromItems(node: var JsonNode) =
if node.kind == JArray:
node.elems = node.elems.filterIt(
it.kind == JArray and (
it.elems.len == 0 or
it[0].kind != JString or
not it[0].getStr.contains("@")
)
)
proc stripMods*(ps: PlayerSave) =
# remove modded recipes
var arr = ps.data[2]["knownRecipes"]
arr.elems = arr.elems.filterIt(not it.getStr.contains("@"))
# remove modded ingredients
arr = ps.data[2]["knownIngredients"]
arr.elems = arr.elems.filterIt(not it.getStr.contains("@"))
# these blocks are here to capture the JsonNode for a mutable
# copy/reference to a variable then discard it when done.
# remove modded items from equipment
block:
var node = ps.data[3]["items"]
node.stripFromItems
# remove modded items from astral box
block:
var node = ps.data[4]["items"]
node.stripFromItems
# remove modded items from inventory
block:
var node = ps.data[5]["items"]
node.stripFromItems
# remove modded items from transmutation cube
block:
var node = ps.data[14]["items"]
node.stripFromItems
# remove modded items from vault
block:
var node = ps.data[17]["items"]
node.stripFromItems
# remove modded items from farmer storage
block:
var node = ps.data[16] # make a mutable copy/reference
for item in node:
if item.hasKey("container"):
# Parse the JSON string stored in "container"
var containerNode = parseJson(item["container"].getStr.unstringifyJsonString)
# Access the "items" array inside the container
var itemsNode = containerNode["items"]
itemsNode.stripFromItems
# Store back as a JString (escaped json)
item["container"] = % $containerNode
# remove modded items from shops
block:
for npcID in 0..100:
let npcKey = $npcID
if ps.data[12]["npcs"].hasKey(npcKey):
var shop = ps.data[12]["npcs"][npcKey]["shopExtraItems"]
# Filter out any strings containing "@"
shop.elems = shop.elems.filterIt(
it.kind != JString or not it.getStr.contains("@")
)
# for i in 6..11:
# if ps.data[i].kind == JString and ps.data[i].contains("@"):
# var node = ps.data[i]
# node = % 0
proc sortShopItems*(save: var PlayerSave) =
for npcId in npcShopIds:
var j = %* []
let key = $npcId
for item in shopOrder:
if % item.getInt.float in save.data[12]["npcs"][key]["shopExtraItems"]:
j.add(% item.getInt.float)
for item in save.data[12]["npcs"][key]["shopExtraItems"]:
if item notin j:
j.add(item)
save.data[12]["npcs"][key]["shopExtraItems"] = j
proc getFreeInventorySlot*(ps: PlayerSave): tuple[x, y: int] =
let node = ps.data[5]["items"]
for y in 0..4:
for x in 0..10:
var found = false
for item in node:
if item[1].getFloat == x.float and item[2].getFloat == y.float:
found = true
break
if not found:
return (x,y)
return (-1, -1)
proc inventoryIsFull*(ps: PlayerSave): bool =
ps.getFreeInventorySlot[0] == -1
var upgrades = %* {
"upgrades": %* {
"lifeFlower": %* {
"level": % 0.0,
"ID": % 1.0
},
"manaFlower": %* {
"level": % 0.0,
"ID": % 2.0
},
"defenseFlower": %* {
"level": % 0.0,
"ID": % 3.0
},
"dashUpgrade01": %* {
"level": % false,
"ID": % 4.0
},
"dashUpgrade02": %* {
"level": % false,
"ID": % 5.0
},
"speedUpgrade01": %* {
"level": % false,
"ID": % 6.0
},
}
}
for i in 0..19:
upgrades["upgrades"][fmt"dictionary{i:02}"] = %* {
"level": % false,
"ID": % float(7+i)
}
proc setUpgradeDefaultKey(p: PlayerSave, key: string): bool =
if not p.data[12]["upgrades"].hasKey(key):
if upgrades["upgrades"].hasKey(key):
p.data[12]["upgrades"][key] = upgrades["upgrades"][key].copy()
else:
return false
return true
proc setUpgrade*(p: PlayerSave, key: string, value: bool = true) =
if not p.setUpgradeDefaultKey(key):
echo fmt"Could not set upgrade {key}"
return
if key in ["dashUpgrade01", "dashUpgrade02", "speedUpgrade01", "speedUpgrade02"]:
p.data[12][key] = % value
p.data[12]["upgrades"][key]["level"] = % value
proc setUpgrade*(p: PlayerSave, key: string, value: int = 1) =
if not p.setUpgradeDefaultKey(key):
echo fmt"Could not set upgrade {key}"
return
var v = value
if key == "lifeFlower":
# recalculate life
v = min(v, 18)
p.data[2]["hpMax"] = % float(40 + 20 * v)
if key == "manaFlower":
# recalculate mana
v = min(v, 9)
p.data[2]["mpMax"] = % float(20 + 20 * v)
p.data[12]["upgrades"][key]["level"] = % v.float