-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoA-map-imager.py
992 lines (885 loc) · 42.3 KB
/
MoA-map-imager.py
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
#! /usr/bin/python
# python3
import os
import sys
from threading import Thread
#import console
import math
import time
import re
import json
try:
from PIL import Image
except:
try:
from pillow import Image
except:
try:
import Image
except:
print("Failed:")
print("from PIL import Image")
print("from pillow import Image")
print("import Image")
timeKeyboardInterrupt = None
SETTINGS_FILE = "MoA-map-imager.conf"
settings = json.loads("{}")
mapFile = "map.dat"
itemsFile = "item.dat"
charsFile = "char.dat"
tileLength = None
itemLength = None
charLength = None
mapWidth = 1024
mapHeight = 1024
mapHexData = bytearray()
itemsHexData = bytearray()
tilesList = {}
itemsList = {}
surface = None
surfaceAltered = False
# Colour for missing image files
missingColour = (255, 0, 255)
imageColourOverrides = { "1090": (224, 130, 29), # experience pole
"1091": (224, 130, 29), # experience pole
"914": (175, 151, 12), # shrine
"16900": (206, 180, 132), # lab 9 white candlestick
"16510": (168, 84, 45), # dark metal fire-bowl
"1750": (42, 42, 40) } # tombstone
imageColourIgnore = [ "16972" ] # soulstones
imageColourCache = {}
# Drawing
layers = { "background" : True,
"foreground" : True,
"items" : True,
"itemsCarried": False,
"characters" : False }
def displayImage(image):
image.show()
def catchDoubleKeyboardInterrupt():
global timeKeyboardInterrupt
if timeKeyboardInterrupt:
if time.time() - timeKeyboardInterrupt < 2:
sys.exit()
timeKeyboardInterrupt = time.time()
def loadSettings():
global settings
global mapFile
global itemsFile
global charsFile
global mapWidth
global mapHeight
global tileLength
global itemLength
global charLength
global missingColour
global imageColourOverrides
global imageColourIgnore
global layers
if os.path.exists(os.getcwd() + '/' + SETTINGS_FILE):
if os.path.isfile(os.getcwd() + '/' + SETTINGS_FILE) and os.access(os.getcwd() + '/' + SETTINGS_FILE, os.X_OK):
try:
with open(os.getcwd() + '/' + SETTINGS_FILE, "r") as file:
settings = json.load(file)
mapFile = settings["map_file"]
itemsFile = settings["items_file"]
charsFile = settings["chars_file"]
mapWidth = settings["map_width"]
mapHeight = settings["map_height"]
tileLength = settings["tile_length"]
itemLength = settings["item_length"]
charLength = settings["char_length"]
missingColour = tuple(settings["missing_colour"])
for key, value in settings["image_colour_override"].items():
imageColourOverrides[key] = tuple(value)
imageColourIgnore = settings["image_ignore"]
layers = settings["layer_visibility"]
except:
print("Unable to load settings")
def saveSettings():
global settings
settings["map_file"] = mapFile
settings["items_file"] = itemsFile
settings["chars_file"] = charsFile
settings["map_width"] = mapWidth
settings["map_height"] = mapHeight
settings["tile_length"] = tileLength
settings["item_length"] = itemLength
settings["char_length"] = charLength
settings["missing_colour"] = missingColour
settings["image_colour_override"] = imageColourOverrides
settings["image_ignore"] = imageColourIgnore
settings["layer_visibility"] = layers
try:
with open(os.getcwd() + '/' + SETTINGS_FILE, "w+") as file:
json.dump(settings, file)
except:
print("Unable to save settings")
def loadMap():
global mapHexData
print("Loading map file ", end='')
sys.stdout.flush()
with open(os.getcwd() + '/' + mapFile, "rb") as file:
mapHexData = file.read().hex().lower()
sys.stdout.flush()
print("[100%]")
def loadItems():
global itemsHexData
print("Loading items file ", end='')
sys.stdout.flush()
with open(os.getcwd() + '/' + itemsFile, "rb") as file:
itemsHexData = file.read().hex().lower()
sys.stdout.flush()
print("[100%]")
def calibrateLengths():
global tileLength
global itemLength
global charLength
# Assumes first two tiles are the same)
portion = ""
for h in mapHexData[8:]:
portion += str(h)
if portion[-8:] == mapHexData[:8]:
tileLength = len(portion)
break
# No calibration yet
itemLength = 1268
charLength = 99999
try:
if tileLength > 0 and itemLength > 0 and charLength > 0:
return
except:
print("Unable to determine data length")
sys.exit()
def readMap():
global tilesList
tileCounter = 0
hexCounter = 0
print("Reading map file [0%]\r", end='')
sys.stdout.flush()
for h in mapHexData:
try:
tilesList[tileCounter] = tilesList[tileCounter] + str(h)
except:
tilesList[tileCounter] = ""
tilesList[tileCounter] = tilesList[tileCounter] + str(h)
hexCounter += 1
if hexCounter == tileLength:
tileCounter += 1
hexCounter = 0
if tileCounter % mapWidth == 0:
print("Reading map file [{0}%]\r".format(int(tileCounter / (mapWidth * mapHeight) * 100)), end='')
sys.stdout.flush()
print("Reading map file [100%]")
def readItems():
global itemsList
itemCounter = 0
hexCounter = 0
print("Reading items file [0%]\r", end='')
sys.stdout.flush()
itemsFileSize = os.stat(os.getcwd() + '/' + itemsFile).st_size
itemsTotalEstimate = int(math.ceil(itemsFileSize / 634 / 100) * 100)
if itemsHexData[:2] == "00":
offset = 2
else:
offset = 0
for h in itemsHexData[offset:]:
try:
itemsList[itemCounter] = itemsList[itemCounter] + str(h)
except:
itemsList[itemCounter] = ""
itemsList[itemCounter] = itemsList[itemCounter] + str(h)
hexCounter += 1
if hexCounter == itemLength:
itemCounter += 1
hexCounter = 0
if itemCounter % (itemsTotalEstimate / 100) == 0:
percentage = int(itemCounter / itemsTotalEstimate * 100)
if percentage > 100:
percentage = 100
print("Reading items file [{0}%]\r".format(percentage), end='')
sys.stdout.flush()
print("Reading items file [100%]")
def drawMap():
global surface
try:
surface = Image.new('RGB', (mapWidth, mapHeight), 0x000000)
except:
print("Pillow not working as expected, likely not running Python 3.6+ / Pillow 8.0+")
sys.exit()
print("Drawing map [0%]\r", end='')
sys.stdout.flush()
x = 0
y = 0
for key, value in tilesList.items():
sprite = value[:8]
background = sprite[:4]
sprite = sprite[4:]
# Background
if layers["background"] is True:
colour = getImageColour(background)
if colour:
surface.putpixel((y, x % mapWidth), colour)
else:
surface.putpixel((y, x % mapWidth), missingColour)
# Foreground
if layers["foreground"] is True:
colour = getImageColour(sprite)
if colour:
surface.putpixel((y, x % mapWidth), colour)
else:
if sprite != "0000":
print("Image file not found ({0})".format(str(int(imageNumber[2:] + imageNumber[:2], 16)).zfill(5)))
x += 1
if x % mapWidth == 0:
y += 1
if layers["items"] is True:
# Items are being drawn
tilesPercentage = int(y / mapWidth * 100 * 0.66)
else:
# Items are not being drawn
tilesPercentage = int(y / mapWidth * 100)
pass
if tilesPercentage > 100:
tilesPercentage = 100
print("Drawing map [{0}%]\r".format(tilesPercentage), end='')
sys.stdout.flush()
# Items
if layers["items"] is True:
itemCounter = 0
for key, value in itemsList.items():
try:
x = int(value[1062:1064] + value[1060:1062], 16)
y = int(value[1058:1060] + value[1056:1058], 16)
item = value[1072:1076]
colour = getImageColour(item)
if colour:
surface.putpixel((x, y % mapWidth), colour)
except Exception as exception:
print(exception)
pass
if itemCounter % (int(len(itemsList) / 100)) == 0:
itemsPercentage = tilesPercentage + int(itemCounter / len(itemsList) * 100 * 0.34)
if itemsPercentage > 100:
itemsPercentage = 100
print("Drawing map [{0}%]\r".format(itemsPercentage), end='')
sys.stdout.flush()
itemCounter += 1
print("Drawing map [100%]")
def getImageColour(hex):
imageNumber = str(int(hex[2:] + hex[:2], 16)).zfill(5)
# Ignore blank
if imageNumber == "00000":
return None
# Check ignores
if imageNumber.lstrip("0") in imageColourIgnore or imageNumber in imageColourIgnore:
return None
# Check overrides
for key, value in imageColourOverrides.items():
if key == imageNumber.lstrip("0") or key == imageNumber:
return value
# Check cache
for key, value in imageColourCache.items():
if key == imageNumber:
return value
try:
image = Image.open(os.getcwd() + "/png/" + imageNumber + ".png")
except:
try:
image = Image.open(os.getcwd() + "/bmp/" + imageNumber + ".bmp")
except:
return None
pixels = list(image.getdata())
averageRed = 0
averageGreen = 0
averageBlue = 0
pixelCount = 0
# Check for transparency
if image.mode == "RGBA":
imageDimensions = image.size[0] * image.size[1]
for channels in pixels:
if channels[3] != 0: # transparency
averageRed += channels[0]
averageGreen += channels[1]
averageBlue += channels[2]
pixelCount += 1
percentageTransparent = 100 / (imageDimensions / pixelCount)
if percentageTransparent < 10:
# May still be looking for a colour mask
image.convert("RGB")
# Determine average colour, excluding masks
if image.mode == "RGB":
for channels in pixels:
#255, 255, 255 # white mask
if channels != (255, 0, 255) and channels != (254, 0, 254): # pink mask
averageRed += channels[0]
averageGreen += channels[1]
averageBlue += channels[2]
pixelCount += 1
#127, 127, 127 # grey mask
#96, 96, 96 # grey mask
#0, 0, 0 # black mask
imageColour = (int(averageRed / pixelCount), int(averageGreen / pixelCount), int(averageBlue / pixelCount))
imageColourCache[imageNumber] = imageColour
return imageColour
def getLocation(x, y):
if x < 0 or x > mapWidth or y < 0 or y > mapHeight:
print("Coordinates outside map bounds")
return False
location = ""
for h in mapHexData[(y + x * mapWidth) * tileLength:(y + x * mapWidth) * tileLength + tileLength]:
location += str(h)
try:
background = int(location[2:4] + location[:2], 16)
except:
return False
if background == 0:
background = "none"
foreground = int(location[6:8] + location[4:6], 16)
if foreground == 0:
foreground = "none"
print("{0},{1}: {2}, {3}".format(x, y, background, foreground), end='')
foundValidName = False
for key, value in itemsList.items():
try:
itemX = int(value[1062:1064] + value[1060:1062], 16)
itemY = int(value[1058:1060] + value[1056:1058], 16)
if itemX == x and itemY == y:
name = value[:40].rstrip('0')
if int(value[1066:1068] + value[1064:1066], 16) == 0:
# Item not being carried (may be the wrong value)
if int(value[1074:1076] + value[1072:1074], 16) != 0:# and int(value[1076:1078], 16) == 0:
# Exclude 'items' that represent an active spell effect (icon above player model up top of GUI)
nameLength = 80
name = value[:nameLength].rstrip('0')
while foundValidName is False:
# Stops an odd issue with Titanium Two-Handed's name (and maybe others) throwing an exception
try:
print(", {0} ({1})".format(int(value[1074:1076] + value[1072:1074], 16), bytes.fromhex(name).decode('utf-8')))
foundValidName = True
except:
nameLength -= 2
if nameLength > 1:
name = value[:nameLength].rstrip('0')
else:
name = "unavailable"
except:
pass
if foundValidName is False:
print(", none")
return True
print("\nMercenaries of Astonia map imager v2.1")
print("by Makadon\n")
loadSettings()
if len(sys.argv) == 1:
loadMap()
calibrateLengths()
saveSettings()
loadItems()
readMap()
readItems()
drawMap()
print("Type \"help\" for commands")
command = ""
while True:
try:
command = input("> ").lower().strip(' ')
except:
print("")
catchDoubleKeyboardInterrupt()
continue
if command == "exit" or command == "quit" or command == "e" or command == "x" or command == "q":
sys.exit()
commandRegex = re.search("^([?\/(\-]?\??[a-zA-Z0-9-]*)?( *(,|\.| ) *([a-zA-Z0-9.\"-]*\+?\)?))?( *(,|\.| ) *([a-zA-Z0-9-]*))?( *(,|\.| ) *([a-zA-Z0-9]*))?( *(,|\.| ) *([a-zA-Z0-9]*))?", command, re.IGNORECASE)
try:
for i in range(14):
commandRegex[i].lower().strip(' \'`"!@#$^()[]{}<>;:|~=')
except:
pass
if commandRegex[1] == "?" or commandRegex[1] == "/?" or commandRegex[1] == "h" or commandRegex[1] == "help" or commandRegex[1] == "-h" or commandRegex[1] == "--h" or commandRegex[1] == "-help" or commandRegex[1] == "--help" or commandRegex[1] == "commands":
print("Usage:")
#print("begin number --- set starting point for map saving and displaying")
#print("end number --- set stopping point for map saving and displaying")
print("toggle bg/fg/items/carried/chars --- toggle layer")
print("display map/number --- display map or image")
print("save [filename.dat] --- save map")
print("location x, y --- list location info, e.g. location x1-x2, y, location x, -y2")
print("search word --- search item names, one word")
#print("mask r, g, b --- add, remove colour mask")
print("ignore number --- ignore image number")
print("override number r, g, b --- override image colour")
#print("missing r, g, b --- set missing image colour")
print("map filename --- set map file")
print("items filename --- set items file")
#print("chars filename --- set characters file")
#print("width number --- define map width")
#print("height number --- define map height")
#print("tilelength number --- define number of hex values between tiles")
#print("itemlength number --- define number of hex values between items")
#print("charlength number --- define number of hex values between characters")
#print("copyminimum --- create copies of data files with only necessary information")
print("defaults --- reset settings")
print("")
print("Omit parameters to see current")
print("")
elif commandRegex[1] == "save":
# Save map
if surfaceAltered is True:
drawMap()
surfaceAltered = False
if commandRegex[4]:
if commandRegex[4][-4:] == ".png" or commandRegex[2][-4:] == ".bmp":
surface.save(commandRegex[4])
else:
print("Filetype must be \"png\" or \"bmp\"")
else:
surface.save("map.png")
elif commandRegex[1] == "display" or commandRegex[1] == "d":
# Open image
if commandRegex[4]:
if surfaceAltered is True:
drawMap()
surfaceAltered = False
if commandRegex[4] == "map":
try:
if threadSurface.is_alive() is True:
continue
except:
pass
threadSurface=Thread(target=displayImage,args=(surface,))
threadSurface.start()
else:
imageNumberString = re.search("^\d{1,5}$", commandRegex[4].lstrip('0'))
if imageNumberString:
imageNumber = int(imageNumberString[0])
if imageNumber > 0 and imageNumber < 65536:
try:
image = Image.open(os.getcwd() + "/png/" + imageNumberString[0].zfill(5) + ".png")
except:
try:
image = Image.open(os.getcwd() + "/bmp/" + imageNumberString[0].zfill(5) + ".bmp")
except:
print("Image file not found ({0})".format(imageNumberString[0].zfill(5)))
if image:
try:
if threadImage.is_alive() is True:
continue
except:
pass
threadImage=Thread(target=displayImage,args=(image,))
threadImage.start()
#if os.name != "nt": # may be useful for linux still
# pid=os.fork()
# if pid:
# image.show()
# else:
# pass
continue
print("Image number must be between 1-65535")
else:
print("Usage: display map/number")
elif commandRegex[1] == "defaults":
# Reset everything to defaults, confirm command
mapFile = "map.dat"
itemsFile = "item.dat"
charsFile = "char.dat"
calibrateLengths()
layers["background"] = True
layers["foreground"] = True
layers["items"] = True
layers["itemsCarried"] = False
layers["characters"] = False
imageColourOverrides = {}
imageColourIgnore = [ "16972" ]
missingColour = (255, 0, 255)
surfaceAltered = True
saveSettings()
elif commandRegex[1] == "map":
# Change map file
if commandRegex[4]:
if commandRegex[4][-4:] == ".dat":
mapFile = commandRegex[4]
loadMap()
readMap()
surfaceAltered = True
saveSettings()
continue
print("Current: {0}".format(mapFile))
elif commandRegex[1] == "items" or commandRegex[1] == "item":
# Change items file
if commandRegex[4]:
if commandRegex[4][-4:] == ".dat":
itemsFile = commandRegex[4]
loadItems()
readItems()
surfaceAltered = True
saveSettings()
continue
print("Current: {0}".format(itemsFile))
elif commandRegex[1] == "chars" or commandRegex[1] == "character" or commandRegex[1] == "characters":
pass
elif commandRegex[1] == "copyminimum" or commandRegex[1] == "copyminimums" or commandRegex[1] == "copymin" or commandRegex[1] == "copymins":
# Create copies of data files with all unneeded information blanked
pass
elif commandRegex[1] == "width":
pass
elif commandRegex[1] == "height":
pass
elif commandRegex[1] == "tilelength" or commandRegex[1] == "tilelen":
if commandRegex[4]:
tileLength = commandRegex[4]
loadMap()
readMap()
surfaceAltered = True
saveSettings()
else:
print("Current: {0}".format(str(tileLength)))
elif commandRegex[1] == "itemlength" or commandRegex[1] == "itemlen":
if commandRegex[4]:
itemLength = commandRegex[4]
loadItems()
readItems()
surfaceAltered = True
saveSettings()
else:
print("Current: {0}".format(str(itemLength)))
elif commandRegex[1] == "charlength" or commandRegex[1] == "charlen" or commandRegex[1] == "characterlength" or commandRegex[1] == "characterlen":
pass
elif commandRegex[1] == "begin":
# Start point for map drawing
pass
elif commandRegex[1] == "end":
# Stop point for map drawing
pass
elif commandRegex[1] == "toggle" or commandRegex[1] == "t":
# Toggle layer
if commandRegex[4]:
if commandRegex[4] == "background" or commandRegex[4] == "bg" or commandRegex[4] == "back":
layers["background"] = not layers["background"]
elif commandRegex[4] == "foreground" or commandRegex[4] == "fg" or commandRegex[4] == "fore":
layers["foreground"] = not layers["foreground"]
elif commandRegex[4] == "items" or commandRegex[4] == "item":
layers["items"] = not layers["items"]
elif commandRegex[4] == "carried" or commandRegex[4] == "itemscarried" or commandRegex[4] == "itemsc" or commandRegex[4] == "itemc":
#layers["itemsCarried"] = not layers["itemsCarried"]
pass
elif commandRegex[4] == "characters" or commandRegex[4] == "character" or commandRegex[4] == "chars" or commandRegex[4] == "char":
#layers["characters"] = not layers["characters"]
pass
else:
print("No such layer")
continue
surfaceAltered = True
saveSettings()
for key, value in layers.items():
print("{0}: {1}".format(key, str(value).lower()))
elif commandRegex[1] == "show":
# Show layer
if commandRegex[4]:
if commandRegex[4] == "background" or commandRegex[4] == "bg" or commandRegex[4] == "back":
if layers["background"] is False:
layers["background"] = True
elif commandRegex[4] == "foreground" or commandRegex[4] == "fg" or commandRegex[4] == "fore":
if layers["foreground"] is False:
layers["foreground"] = True
elif commandRegex[4] == "items" or commandRegex[4] == "item":
if layers["items"] is False:
layers["items"] = True
elif commandRegex[4] == "carried" or commandRegex[4] == "itemscarried" or commandRegex[4] == "itemsc" or commandRegex[4] == "itemc":
#if layers["itemsCarried"] is False:
# layers["itemsCarried"] = True
pass
elif commandRegex[4] == "characters" or commandRegex[4] == "character" or commandRegex[4] == "chars" or commandRegex[4] == "char":
#if layers["characters"] is False:
# layers["characters"] = True
pass
else:
print("No such layer")
continue
surfaceAltered = True
saveSettings()
else:
for key, value in layers.items():
print("{0}: {1}".format(key, str(value).lower()))
elif commandRegex[1] == "hide":
# Hide layer
if commandRegex[4]:
if commandRegex[4] == "background" or commandRegex[4] == "bg" or commandRegex[4] == "back":
if layers["background"] is True:
layers["background"] = False
elif commandRegex[4] == "foreground" or commandRegex[4] == "fg" or commandRegex[4] == "fore":
if layers["foreground"] is True:
layers["foreground"] = False
elif commandRegex[4] == "items" or commandRegex[4] == "item":
if layers["items"] is True:
layers["items"] = False
elif commandRegex[4] == "carried" or commandRegex[4] == "itemscarried" or commandRegex[4] == "itemsc" or commandRegex[4] == "itemc":
#if layers["itemsCarried"] is True:
# layers["itemsCarried"] = False
pass
elif commandRegex[4] == "characters" or commandRegex[4] == "character" or commandRegex[4] == "chars" or commandRegex[4] == "char":
#if layers["characters"] is True:
# layers["characters"] = False
pass
else:
print("No such layer")
continue
surfaceAltered = True
saveSettings()
else:
for key, value in layers.items():
print("{0}: {1}".format(key, str(value).lower()))
elif commandRegex[1] == "mask" or commandRegex[1] == "masks":
# List masks
pass
elif commandRegex[1] == "ignore" or commandRegex[1] == "i":
# Ignore image number
if commandRegex[4]:
imageNumberString = re.search("^\d{1,5}$", commandRegex[4].lstrip('0'))
if imageNumberString:
imageNumber = int(imageNumberString[0])
if imageNumber > 0 and imageNumber < 65536:
if not imageNumberString[0].zfill(5) in imageColourIgnore or not imageNumberString[0] in imageColourIgnore:
imageColourIgnore.append(imageNumberString[0])
surfaceAltered = True
saveSettings()
else:
index = len(imageColourIgnore) - 1
for item in reversed(imageColourIgnore):
if item == imageNumberString[0]:
del imageColourIgnore[index]
index -= 1
surfaceAltered = True
saveSettings()
print("{0} no longer ignored".format(imageNumberString[0]))
continue
print("Image number must be between 1-65535")
continue
sortedIgnores = []
for item in imageColourIgnore:
sortedIgnores.append(item.zfill(5))
for item in sorted(sortedIgnores):
print(item.lstrip('0'))
elif commandRegex[1] == "override" or commandRegex[1] == "overide" or commandRegex[1] == "o":
# Override image number's colour
if commandRegex[4]:
imageNumberString = re.search("^\d{1,5}$", commandRegex[4].lstrip('0'))
if imageNumberString:
imageNumber = int(imageNumberString[0])
if imageNumber > 0 and imageNumber < 65536:
if commandRegex[7] and commandRegex[10] and commandRegex[13]:
RGBoffset = 7
RGB = {}
for index in range(3):
RGBNumberString = re.search("^\d{1,3}$", commandRegex[RGBoffset])[0]
if RGBNumberString:
if RGBNumberString != "0":
RGBNumberString = RGBNumberString.lstrip('0')
RGBNumber = int(RGBNumberString)
if RGBNumber > -1 and RGBNumber < 256:
RGB[str(RGBoffset)] = RGBNumber
else:
print("Parameters must be between 0-255")
continue
else:
print("Usage: override [image number] [red, green, blue]")
continue
RGBoffset += 3
imageColourOverrides[imageNumberString[0]] = (RGB["7"], RGB["10"], RGB["13"])
surfaceAltered = True
saveSettings()
else:
if imageNumberString[0].zfill(5) in imageColourOverrides:
del imageColourOverrides[imageNumberString[0].zfill(5)]
elif imageNumberString[0] in imageColourOverrides:
del imageColourOverrides[imageNumberString[0]]
else:
print("Image number has no override")
continue
surfaceAltered = True
saveSettings()
print("{0} override removed".format(imageNumberString[0]))
else:
print("Image number must be between 1-65535")
else:
sortedOverrides = {}
for key, value in imageColourOverrides.items():
sortedOverrides[key.zfill(5)] = value
for key, value in sorted(sortedOverrides.items()):
print("{0}: {1},{2},{3}".format(key.lstrip('0'), value[0], value[1], value[2]))
elif commandRegex[1] == "missing" or commandRegex[1] == "missingcolour" or commandRegex[1] == "missingcolor":
# Set missing image file colour
pass
elif commandRegex[1] == "search" or commandRegex[1] == "s" or commandRegex[1] == "find" or commandRegex[1] == "f":
# Search for items
if commandRegex[4]:
parameter = commandRegex[4].strip('"\'`').lower()
if len(parameter) < 3:
print("Word must be at least three characters long")
continue
itemCounter = 0
itemNames = {}
itemDescriptions = {}
itemNamesFound = []
results = {}
print("name (image number): description")
for key, value in itemsList.items():
itemNames[key] = bytes.fromhex(value[:80]).decode('utf-8').rstrip('\x00')
itemDescriptions[key] = bytes.fromhex(value[160:560]).decode('utf-8').rstrip('\x00')
itemDescription = itemDescriptions[key]
imageColour = int(value[1074:1076] + value[1072:1074], 16)
itemNamesWords = itemNames[key].lower().split()
itemDescriptionsWords = itemDescriptions[key].lower().split()
# Truncate item description
length = 80 - (len(itemNames[key]) + len(str(imageColour)) + 5)
if length < 0:
length = 0
if len(itemDescriptions[key]) > length:
itemDescription = ' '.join(itemDescription[:length+1].split(' ')[0:-1]) + "..."
if len(itemDescriptions[key]) > 3:
if itemDescription[-4:] == "....":
itemDescription = itemDescription[:-1]
elif itemDescription[-4:] == ",...":
itemDescription = itemDescription[:-4]
itemDescription += "..."
if itemDescription == "...":
itemDescription = ""
if parameter in itemNamesWords or parameter in itemDescriptionsWords:
if itemNames[key] not in itemNamesFound:
itemNamesFound.append(itemNames[key])
if imageColour in results:
results[imageColour] = (*results[imageColour], itemNames[key], itemDescription)
else:
results[imageColour] = (itemNames[key], itemDescription)
else:
for word in itemNamesWords:
if parameter in word.lower():
if itemNames[key] not in itemNamesFound:
itemNamesFound.append(itemNames[key])
results[imageColour] = (itemNames[key], itemDescription)
for word in itemDescriptionsWords:
if parameter in word.lower():
if itemNames[key] not in itemNamesFound:
itemNamesFound.append(itemNames[key])
results[imageColour] = (itemNames[key], itemDescription)
for key, value in sorted(results.items()):
try:
index = 0
while True:
print("{0} ({1}): {2}".format(value[index], key, value[index + 1]))
index += 2
except:
pass
else:
print("Usage: search word")
elif commandRegex[1] and commandRegex[4]:
# List location info
if commandRegex[1] and commandRegex[4]:
commandCoordinateX = re.search("^(\d{1,4})? *-? *(\d{1,4})?-?$", commandRegex[1])
commandCoordinateY = re.search("^(\d{1,4})? *-? *(\d{1,4})?-?$", commandRegex[4])
if commandRegex[4] and commandRegex[7]:
commandCoordinateXAlt = re.search("^(\d{1,4})? *-? *(\d{1,4})?-?$", commandRegex[4])
commandCoordinateYAlt = re.search("^(\d{1,4})? *-? *(\d{1,4})?-?$", commandRegex[7])
if (commandRegex[1] and commandCoordinateX and commandCoordinateY) or ((commandRegex[1] == "location" or commandRegex[1] == "loc" or commandRegex[1] == "locate" or commandRegex[1] == "tile") and commandRegex[7] and commandCoordinateXAlt and commandCoordinateYAlt):
if commandRegex[1] == "location" or commandRegex[1] == "loc" or commandRegex[1] == "locate" or commandRegex[1] == "l" or commandRegex[1] == "tile":
commandOffset = 3
else:
commandOffset = 0
x1 = 0
x2 = mapWidth
y1 = 0
y2 = mapHeight
commandCoordinateXRange = re.search("^(\d{1,4})? *- *(\d{1,4})?$", commandRegex[1 + commandOffset])
commandCoordinateYRange = re.search("^(\d{1,4})? *- *(\d{1,4})?$", commandRegex[4 + commandOffset])
if commandCoordinateXRange:
if commandCoordinateXRange[1]:
x1 = commandCoordinateXRange[1]
if commandCoordinateXRange[2]:
x2 = commandCoordinateXRange[2]
if commandCoordinateYRange:
if commandCoordinateYRange[1]:
y1 = commandCoordinateYRange[1]
if commandCoordinateYRange[2]:
y2 = commandCoordinateYRange[2]
if int(x2) < int(x1):
temp = x1
x1 = x2
x2 = temp
if int(y2) < int(y1):
temp = y1
y1 = y2
y2 = temp
if commandCoordinateXRange:
if commandCoordinateYRange:
print("Range only on one axis at a time")
else:
if commandOffset == 0:
y1 = commandCoordinateY[1]
else:
y1 = commandCoordinateYAlt[1]
print("x,y: background, foreground, item")
for x in range(int(x1), int(x2) + 1):
if getLocation(x, int(y1)) is False:
print("Invalid map data")
break
else:
if commandCoordinateYRange:
if commandOffset == 0:
x1 = commandCoordinateX[1]
else:
x1 = commandCoordinateXAlt[1]
print("x,y: background, foreground, item")
for y in range(int(y1), int(y2) + 1):
if getLocation(int(x1), y) is False:
print("Invalid map data")
break
else:
if commandOffset == 0:
x1 = commandCoordinateX[1]
else:
x1 = commandCoordinateXAlt[1]
if commandOffset == 0:
y1 = commandCoordinateY[1]
else:
y1 = commandCoordinateYAlt[1]
print("x,y: background, foreground, item")
if getLocation(int(x1), int(y1)) is False:
print("Invalid map data")
else:
print("Usage: location x, y")
else:
print("Unknown command")
elif len(sys.argv) > 1:
try:
gettingLocation = False
for index, arg in enumerate(sys.argv):
if index == 0:
continue
elif sys.argv[index] == "-m" or sys.argv[index] == "-map":
if sys.argv[index + 1][-4:] == ".dat":
mapFile = sys.argv[index + 1]
else:
raise
elif sys.argv[index] == "-i" or sys.argv[index] == "-items" or sys.argv[index] == "-item":
if sys.argv[index + 1][-4:] == ".dat":
itemsFile = sys.argv[index + 1]
else:
raise
elif sys.argv[index] == "-l" or sys.argv[index] == "-location" or sys.argv[index] == "-loc":
gettingLocation = True
coords = re.search("(-?\d{1,4})(,|.)(-?\d{1,4})", sys.argv[index + 1])
loadMap()
calibrateLengths()
loadItems()
readMap()
readItems()
if gettingLocation is False:
drawMap()
surface.save("map.png")
else:
print("x,y: background, foreground, item")
getLocation(int(coords[1]), int(coords[3]))
except Exception as exception:
print("Usage:")
print("python MoA-map-imager.py [-map map.dat] [-items item.dat] [-location x,y]")
sys.exit()