forked from poiuyqwert/PyMS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyMAP.pyw
More file actions
1794 lines (1628 loc) · 60.3 KB
/
PyMAP.pyw
File metadata and controls
1794 lines (1628 loc) · 60.3 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
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
993
994
995
996
997
998
999
1000
from Libs.utils import *
from Libs.setutils import *
from Libs.trace import setup_trace
from Libs.SFmpq import *
from Libs import TBL, AIBIN, DAT, Tilesets, GRP, PAL, PCX
from Libs.CHK import *
from Tkinter import *
from tkMessageBox import *
import tkFileDialog,tkColorChooser
from Libs.SpecialLists import TreeList
from PIL import Image as PILImage
from PIL import ImageDraw as PILDraw
from PIL import ImageTk
from thread import start_new_thread
from math import ceil
import optparse, os, webbrowser, sys, time, random
VERSION = (0,1)
LONG_VERSION = 'v%s.%s-DEV' % VERSION
FRAME_DELAY = 42
def z_position(y, elevation, img_offset=0):
if elevation < 2:
return elevation
return ((int(y / 32.0) + 1) * (256*256)) + (128 + elevation * 256) + img_offset
def z_position_unit(unit, unitsdat):
elevation = unitsdat.get_value(unit.unit_id, 'ElevationLevel')
return z_position(unit.position[1], elevation)
def compare_z(unit1, unit2, unitsdat, reverse=False):
zorder1 = z_position_unit(unit1, unitsdat)
zorder2 = z_position_unit(unit2, unitsdat)
if zorder1 < zorder2:
return 1 if reverse else -1
if zorder1 > zorder2:
return -1 if reverse else 1
return 0
class BWImage:
GRP_CACHE = {}
FRAME_CACHE = {}
@staticmethod
def purge_frames(grpFile=None, player_color_id=None, flipHor=None, draw_function_id=None, draw_remapping=None, index=None):
keys = [grpFile, player_color_id, flipHor, draw_function_id, draw_remapping, index]
while keys[-1] == None:
del keys[-1]
def do_purge(cache, keys):
if key[0] == None:
for k in cache.keys():
do_purge(cache[k], keys[1:])
elif len(keys) == 1:
del cache[key[0]]
else:
do_purge(cache[k], keys[1:])
if not keys:
BWImage.FRAME_CACHE = {}
else:
do_purge(BWImage.FRAME_CACHE, keys)
def __init__(self, ui, ref, image_id, pos, parent=None, grpPath='', elevation=4, pos_offset=(0,0), elevation_offset=0, unit_ref=None):
self.ui = ui
self.ref = ref
self.unit_ref = unit_ref
self.image_id = image_id
self.parent = parent
self.children = []
self.iscript_id = self.ui.imagesdat.get_value(self.image_id,'IscriptID')
self.header_jump(0)
self.iscript_wait = 0
self.iscript_turns = self.ui.imagesdat.get_value(self.image_id,'GfxTurns')
self.iscript_direction = 0
if self.iscript_turns:
self.iscript_direction = int(random.uniform(0,16))
self.draw_function = GRP.rle_normal
self.draw_info = None
self.draw_function_id = self.ui.imagesdat.get_value(self.image_id, 'DrawFunction')
self.draw_remapping = self.ui.imagesdat.get_value(self.image_id, 'Remapping')
self.iscript_opcodes = {
0: self.op_playfram,
1: self.op_playframtile,
# sethorpos
3: self.op_setvertpos,
# setpos
5: self.op_wait,
6: self.op_waitrand,
7: self.op_goto,
8: self.op_imgol,
9: self.op_imgul,
# imgolorig
# switchul
# __0c
# imgoluselo
# imguluselo
# sprol
# highsprol
17: self.op_lowsprul,
# uflunstable
# spruluselo
# sprul
# sproluselo
22: self.op_end,
# setflipstate
# playsnd
# playsndrand
# playsndbtwn
# domissiledmg
# attackmelee
29: self.op_followmaingraphic,
30: self.op_randcondjmp,
31: self.op_turnccwise,
32: self.op_turncwise,
# turnlcwise
34: self.op_turnrand,
# setspawnframe
# sigorder
# attackwith
# attack
# castspell
# useweapon
# move
# gotorepeatattk
# engframe
# engset
# __2d
# nobrkcodestart
# nobrkcodeend
# ignorerest
# attkshiftproj
# tmprmgraphicstart
# tmprmgraphicend
52: self.op_setfldirect,
# call
# return
# setflspeed
# creategasoverlays
# pwrupcondjmp
# trgtrangecondjmp
# trgtarccondjmp
# curdirectcondjmp
# imgulnextid
# __3e
# liftoffcondjmp
# warpoverlay
# orderdone
# grdsprol
# __43
# dogrddamage
}
self.position = list(pos)
self.position_offset = list(pos_offset)
self.frame = 0
self.grpFile = None
self.grp = None
self.player_color_id = None
self.elevation = elevation
self.elevation_offset = elevation_offset
self.pal = 'Terrain'
string_id = self.ui.imagesdat.get_value(self.image_id,'GRPFile')
if string_id:
self.grpFile = grpPath + self.ui.imagestbl.strings[string_id-1][:-1]
if self.draw_function_id == 9 and 0 < self.draw_remapping < 4:
self.pal = ['o','b','g'][self.draw_remapping-1] + 'fire'
elif self.draw_function_id == 10:
self.draw_function = GRP.rle_shadow
elif self.draw_function_id == 13:
self.draw_function = GRP.rle_outline
self.draw_info = GRP.OUTLINE_ALLY
print (1, self.parent)
print (2, self.parent.unit_ref)
if self.parent != None and self.parent.unit_ref != None:
units = self.ui.chk.get_section(CHKSectionUNIT.NAME)
unit = units.get_unit(self.parent.unit_ref)
if unit:
print (3,unit.owner)
ownr = self.ui.chk.get_section(CHKSectionOWNR.NAME)
print (4,ownr.owners[unit.owner])
owner = ownr.owners[unit.owner]
if owner == CHKSectionOWNR.HUMAN:
self.draw_info = GRP.OUTLINE_SELF
elif owner == CHKSectionOWNR.COMPUTER:
self.draw_info = GRP.OUTLINE_ENEMY
elif self.unit_ref != None:
units = self.ui.chk.get_section(CHKSectionUNIT.NAME)
unit = units.get_unit(self.unit_ref)
if unit:
self.player_color_id = self.ui.chk.player_color(unit.owner)
if self.player_color_id != None:
start_index = self.player_color_id * 8
if start_index+7 < len(self.ui.tunitpcx.image[0]):
self.draw_info = []
for n in range(start_index,start_index+8):
i = self.ui.tunitpcx.image[0][n]
self.draw_info.append(self.ui.palettes['Units'][i])
def z_position(self):
return z_position(self.position[1], self.elevation, self.elevation_offset)
def render_position(self):
return (self.position[0] + self.position_offset[0], self.position[1] + self.position_offset[1])
def get_grp(self):
if self.grp == None:
if self.grpFile in BWImage.GRP_CACHE:
self.grp = BWImage.GRP_CACHE[self.grpFile]
else:
grp_file = self.ui.mpqhandler.get_file('MPQ:' + self.grpFile)
try:
grp = GRP.CacheGRP()
grp.load_file(grp_file)
except PyMSError, e:
return None
self.grp = grp
BWImage.GRP_CACHE[self.grpFile] = grp
return self.grp
def current_frame(self):
frame = None
if self.grpFile:
flipHor = False
index = self.frame
if self.iscript_turns:
flipHor = (self.iscript_direction > 16)
if flipHor:
index += 33 - self.iscript_direction
else:
index += self.iscript_direction
if self.grpFile in BWImage.FRAME_CACHE \
and self.player_color_id in BWImage.FRAME_CACHE[self.grpFile] \
and flipHor in BWImage.FRAME_CACHE[self.grpFile][self.player_color_id] \
and self.draw_function_id in BWImage.FRAME_CACHE[self.grpFile][self.player_color_id][flipHor] \
and self.draw_remapping in BWImage.FRAME_CACHE[self.grpFile][self.player_color_id][flipHor][self.draw_function_id]:
frame = BWImage.FRAME_CACHE[self.grpFile][self.player_color_id][flipHor][self.draw_function_id][self.draw_remapping].get(index)
if frame == None:
grp = self.get_grp()
if grp:
frame = GRP.frame_to_photo(self.ui.palettes[self.pal], grp, index, True, False, True, 0, flipHor, self.draw_function, self.draw_info)
if not self.grpFile in BWImage.FRAME_CACHE:
BWImage.FRAME_CACHE[self.grpFile] = {}
if not self.player_color_id in BWImage.FRAME_CACHE[self.grpFile]:
BWImage.FRAME_CACHE[self.grpFile][self.player_color_id] = {}
if not flipHor in BWImage.FRAME_CACHE[self.grpFile][self.player_color_id]:
BWImage.FRAME_CACHE[self.grpFile][self.player_color_id][flipHor] = {}
if not self.draw_function_id in BWImage.FRAME_CACHE[self.grpFile][self.player_color_id][flipHor]:
BWImage.FRAME_CACHE[self.grpFile][self.player_color_id][flipHor][self.draw_function_id] = {}
if not self.draw_remapping in BWImage.FRAME_CACHE[self.grpFile][self.player_color_id][flipHor][self.draw_function_id]:
BWImage.FRAME_CACHE[self.grpFile][self.player_color_id][flipHor][self.draw_function_id][self.draw_remapping] = {}
BWImage.FRAME_CACHE[self.grpFile][self.player_color_id][flipHor][self.draw_function_id][self.draw_remapping][index] = frame
return frame
def die(self):
if not self.header_jump(1):
self.op_end()
def header_jump(self, header):
headers = self.ui.iscriptbin.headers[self.iscript_id][2]
if header < len(headers):
iscript_offset = headers[header]
if iscript_offset != None:
self.iscript_wait = 0
self.op_goto(iscript_offset)
for child in self.children:
child.header_jump(header)
return True
# elif header == 1:
# self.op_end()
return False
def op_playfram(self, frame):
self.frame = frame
return True
def op_playframtile(self, frame):
era = self.ui.chk.get_section(CHKSectionERA.NAME)
frame += era.tileset
if frame < self.get_grp().frames:
self.frame = frame
return True
def op_setvertpos(self, y_offset):
self.position_offset[1] = y_offset
return True
def op_wait(self, ticks):
self.iscript_wait += FRAME_DELAY * ticks
return True
def op_waitrand(self, min_ticks, max_ticks):
ticks = random.randint(min_ticks, max_ticks)
return self.op_wait(ticks)
def op_goto(self, offset):
self.iscript_cmd = self.ui.iscriptbin.code.index(offset)
return False
def op_imgol(self, image_id, offset_x,offset_y):
ref = self.ref + '-child%d' % len(self.children)
image = BWImage(self.ui, ref, image_id, (self.position[0], self.position[1]), self, 'unit\\', self.elevation, (offset_x,offset_y), 1)
self.children.append(image)
self.ui.maplayer_images.add_image(image)
return True
def op_imgul(self, image_id, offset_x,offset_y):
ref = self.ref + '-child%d' % len(self.children)
image = BWImage(self.ui, ref, image_id, (self.position[0], self.position[1]), self, 'unit\\', self.elevation, (offset_x,offset_y), -1)
self.children.append(image)
self.ui.maplayer_images.add_image(image)
return True
def op_lowsprul(self, sprite_id, offset_x,offset_y):
ref = 'sprite%d' % sprite_id
image_id = self.ui.spritesdat.get_value(sprite_id,'ImageFile')
image = BWImage(self.ui, ref, image_id, (self.position[0], self.position[1]), None, 'unit\\', 1, (offset_x,offset_y))
self.ui.maplayer_images.add_image(image)
return True
def op_end(self):
self.iscript_wait = sys.maxint
self.ui.maplayer_images.remove_image(self)
for child in self.children:
child.parent = None
# self.ui.maplayer_images.remove_image(child)
return False
def op_followmaingraphic(self):
if self.parent:
self.frame = self.parent.frame
self.iscript_direction = self.parent.iscript_direction
else:
self.op_end()
return True
def op_randcondjmp(self, chance, offset):
rand = random.randint(0,255)
if rand <= chance:
return self.op_goto(offset)
return True
def op_turnccwise(self, delta):
return self.op_setfldirect(self.iscript_direction - delta)
def op_turncwise(self, delta):
return self.op_setfldirect(self.iscript_direction + delta)
def op_turnrand(self, delta):
rand = random.randint(0,65535)
if rand & 3 == 1:
return self.op_turnccwise(delta)
return self.op_turncwise(delta)
def op_setfldirect(self, direction):
while direction < 0:
direction += 34
self.iscript_direction = direction % 34
return True
def tick(self, dt):
if self.iscript_wait > 0:
self.iscript_wait -= dt
if self.iscript_wait < 0:
self.iscript_wait = 0
while self.iscript_wait == 0:
cmd = self.ui.iscriptbin.code.getitem(self.iscript_cmd)
opcode = self.iscript_opcodes.get(cmd[0])
inc = True
if opcode:
inc = opcode(*cmd[1:])
else:
print (self.iscript_id, cmd)
if inc:
self.iscript_cmd += 1
class ActionUpdateLocation(ActionUpdateValues):
def __init__(self, ui, location_id, location, attrs):
ActionUpdateValues.__init__(self, location, attrs)
self.ui = ui
self.location_id = location_id
def get_obj(self):
locations = self.ui.chk.get_section(CHKSectionMRGN.NAME)
return locations.locations[self.location_id]
def update_display(self, info):
if self.ui.current_editlayer == self.ui.editlayer_locations:
self.ui.current_editlayer.update_location(self.location_id)
if 'name' in self.start_values or 'name' in self.end_values:
self.ui.listlayer_locations.clear_list()
self.ui.listlayer_locations.populate_list()
class ActionUpdateString(Action):
def __init__(self, ui, string_id, start_text):
self.ui = ui
self.string_id = string_id
self.start_text = start_text
self.end_text = None
def update_end_text(self):
strings = self.ui.chk.get_section(CHKSectionSTR.NAME)
string = strings.get_string(self.string_id)
if string:
self.end_text = string.text
def undo(self):
strings = self.ui.chk.get_section(CHKSectionSTR.NAME)
if self.end_text != None and self.start_text == None:
strings.delete_string(self.string_id)
else:
strings.set_string(self.string_id, self.start_text)
def redo(self):
strings = self.ui.chk.get_section(CHKSectionSTR.NAME)
if self.start_text != None and self.end_text == None:
strings.delete_string(self.string_id)
else:
strings.set_string(self.string_id, self.end_text)
def resize_event(location, mouseX,mouseY):
event = []
x1,y1,x2,y2 = location.normalized_coords()
if x1-5 <= mouseX <= x2+5 and y1-5 <= mouseY <= y2+5:
event.append(EditLayer.EDIT_MOVE)
dist_left = abs(location.start[0] - mouseX)
dist_right = abs(location.end[0] - mouseX)
if dist_left < dist_right and dist_left <= 5:
event[0] = EditLayer.EDIT_RESIZE_LEFT
elif dist_right < dist_left and dist_right <= 5:
event[0] = EditLayer.EDIT_RESIZE_RIGHT
dist_top = abs(location.start[1] - mouseY)
dist_bot = abs(location.end[1] - mouseY)
if dist_top < dist_bot and dist_top <= 5:
event.append(EditLayer.EDIT_RESIZE_TOP)
elif dist_bot < dist_top and dist_bot <= 5:
event.append(EditLayer.EDIT_RESIZE_BOTTOM)
return event
class EditLayer:
INACTIVE = 0
ACTIVE = 1
MOUSE_LEFT = 1
MOUSE_MIDDLE = 2
MOUSE_RIGHT = 3
MODIFIER_SHIFT = (1 << 0)
MODIFIER_CTRL = (1 << 1)
MOUSE_DOWN = (1 << 2)
MOUSE_MOVE = (1 << 3)
MOUSE_UP = (1 << 4)
MOUSE_DOUBLE = (1 << 5)
EDIT_MOVE = 0
EDIT_RESIZE_LEFT = 1
EDIT_RESIZE_TOP = 2
EDIT_RESIZE_RIGHT = 3
EDIT_RESIZE_BOTTOM = 4
def __init__(self, ui, name):
self.ui = ui
self.name = name
self.list_group = None
self.mode = EditLayer.INACTIVE
def list_select(self, info):
pass
def set_mode(self, mode, x1,y1, x2,y2):
isNew = mode != self.mode
self.mode = mode
return isNew
def update_display(self, x1,y1, x2,y2, mouseX,mouseY):
pass
def mouse_event(self, button, button_event, x1,y1, x2,y2, mouseX,mouseY):
pass
class EditLayerTerrain(EditLayer):
def __init__(self, ui):
EditLayer.__init__(self, ui, "Terrain")
def set_mode(self, mode, x1,y1, x2,y2):
if EditLayer.set_mode(self, mode, x1,y1, x2,y2):
tag = 'tile_border'
if self.mode == EditLayer.INACTIVE:
self.ui.mapCanvas.delete(tag)
else:
self.ui.mapCanvas.create_rectangle(0,0,0,0, outline='#FF0000', tags=tag)
def update_display(self, x1,y1, x2,y2, mouseX,mouseY):
if self.mode == EditLayer.ACTIVE:
tag = 'tile_border'
x = nearest_multiple(x1+mouseX,32,math.floor)
y = nearest_multiple(y1+mouseY,32,math.floor)
self.ui.mapCanvas.coords(tag, x,y, x+32,y+32)
self.ui.mapCanvas.tag_raise(tag)
class EditLayerDoodads(EditLayer):
def __init__(self, ui):
EditLayer.__init__(self, ui, "Doodads")
class EditLayerFogOfWar(EditLayer):
def __init__(self, ui):
EditLayer.__init__(self, ui, "Fog of War")
class EditLayerLocations(EditLayer):
def __init__(self, ui):
EditLayer.__init__(self, ui, "Locations")
self.snap_to_grid = True
self.always_display = self.ui.settings.get('always_show_locations', False)
self.show_anywhere = False
self.locations = {}
self.zOrder = None
self.old_cursor = None
self.current_event = []
self.mouse_offset = [0,0]
self.resize_location = None
self.action = None
def raise_location(self, location_id):
self.ui.mapCanvas.tag_raise('location%d' % location_id)
self.zOrder.remove(location_id)
self.zOrder.insert(0, location_id)
def list_select(self, location_id):
self.raise_location(location_id)
dims = self.ui.chk.get_section(CHKSectionDIM.NAME)
map_width = dims.width * 32
map_height = dims.height * 32
view_width = self.ui.mapCanvas.winfo_width()
view_height = self.ui.mapCanvas.winfo_height()
locations = self.ui.chk.get_section(CHKSectionMRGN.NAME)
location = locations.locations[location_id]
x1,y1,x2,y2 = location.normalized_coords()
loc_width = x2-x1
loc_height = y2-y1
x = x1
y = y1
if loc_width < view_width:
x -= (view_width - loc_width) / 2.0
if loc_height < view_height:
y -= (view_height - loc_height) / 2.0
self.ui.mapCanvas.xview_moveto(x / float(map_width))
self.ui.mapCanvas.yview_moveto(y / float(map_height))
def set_mode(self, mode, x1,y1, x2,y2):
if EditLayer.set_mode(self, mode, x1,y1, x2,y2):
if self.mode == EditLayer.ACTIVE:
self.old_cursor = self.ui.mapCanvas.cget('cursor')
# dims = self.chk.get_section(CHKSectionDIM.NAME)
locations = self.ui.chk.get_section(CHKSectionMRGN.NAME)
if not self.zOrder:
self.zOrder = list(range(len(locations.locations)))
for l in self.zOrder:
if l == 63 and not self.show_anywhere:
continue
self.update_location(l)
else:
self.ui.mapCanvas.config(cursor=self.old_cursor)
for tag in self.locations.keys():
self.ui.mapCanvas.delete(tag)
self.ui.mapCanvas.delete(tag + '-name')
self.locations = {}
def update_display(self, x1,y1, x2,y2, mouseX,mouseY):
if self.mode == EditLayer.ACTIVE:
x = x1+mouseX
y = y1+mouseY
locations = self.ui.chk.get_section(CHKSectionMRGN.NAME)
cursor = [self.old_cursor]
found = None
for l in self.zOrder:
if l == 63 and not self.show_anywhere:
continue
location = locations.locations[l]
if location.in_use():
x1,y1,x2,y2 = location.normalized_coords()
event = resize_event(location,x,y)
if event:
if event[0] == EditLayer.EDIT_MOVE:
cursor.extend(['crosshair','fleur','size'])
elif event[0] == EditLayer.EDIT_RESIZE_LEFT:
cursor.extend(['left_side','size_we','resizeleft','resizeleftright'])
elif event[0] == EditLayer.EDIT_RESIZE_RIGHT:
cursor.extend(['right_side','size_we','resizeright','resizeleftright'])
if len(event) == 2:
if event[1] == EditLayer.EDIT_RESIZE_TOP:
cursor.extend(['top_side','size_ns','resizeup','resizeupdown'])
elif event[1] == EditLayer.EDIT_RESIZE_BOTTOM:
cursor.extend(['bottom_side','size_ns','resizedown','resizeupdown'])
if event[0] == EditLayer.EDIT_RESIZE_LEFT and event[1] == EditLayer.EDIT_RESIZE_TOP:
cursor.extend(['top_left_corner','size_nw_se','resizetopleft'])
elif event[0] == EditLayer.EDIT_RESIZE_RIGHT and event[1] == EditLayer.EDIT_RESIZE_TOP:
cursor.extend(['top_right_corner','size_ne_sw','resizetopright'])
elif event[0] == EditLayer.EDIT_RESIZE_LEFT and event[1] == EditLayer.EDIT_RESIZE_BOTTOM:
cursor.extend(['bottom_left_corner','size_ne_sw','resizebottomleft'])
elif event[0] == EditLayer.EDIT_RESIZE_RIGHT and event[1] == EditLayer.EDIT_RESIZE_BOTTOM:
cursor.extend(['bottom_right_corner','size_nw_se','resizebottomright'])
found = location
break
if found:
strings = self.ui.chk.get_section(CHKSectionSTR.NAME)
name = strings.get_text(location.name-1, '')
self.ui.edit_status.set('Edit Location: ' + TBL.decompile_string(name))
else:
self.ui.edit_status.set('Create new Location')
apply_cursor(self.ui.mapCanvas, cursor)
def update_location(self, location_id):
locations = self.ui.chk.get_section(CHKSectionMRGN.NAME)
location = locations.locations[location_id]
tag = 'location%d' % location_id
tag_name = tag + '-name'
loc_rect = self.ui.mapCanvas.find_withtag(tag)
loc_name = self.ui.mapCanvas.find_withtag(tag_name)
if location.in_use():
strings = self.ui.chk.get_section(CHKSectionSTR.NAME)
name = strings.get_text(location.name-1, '')
name = TBL.decompile_string(name)
x1,y1,x2,y2 = location.normalized_coords()
if loc_rect:
self.ui.mapCanvas.coords(loc_rect, x1,y1,x2,y2)
else:
self.locations[tag] = self.ui.mapCanvas.create_rectangle(x1,y1, x2,y2, width=2, outline='#0080ff', stipple='gray75', tags=tag) #, fill='#5555FF'
if loc_name:
self.ui.mapCanvas.coords(loc_name, x1+2,y1+2)
self.ui.mapCanvas.itemconfig(loc_name, text=name, width=x2-x1-4)
else:
self.ui.mapCanvas.create_text(x1+2,y1+2, anchor=NW, text=name, font=('courier', -10, 'normal'), fill='#00FF00', width=x2-x1-4, tags=tag_name)
else:
if loc_rect:
self.ui.mapCanvas.delete(loc_rect)
if loc_name:
self.ui.mapCanvas.delete(loc_name)
def mouse_event(self, button, button_event, x1,y1, x2,y2, mouseX,mouseY):
if self.mode == EditLayer.ACTIVE:
x = x1+mouseX
y = y1+mouseY
locations = self.ui.chk.get_section(CHKSectionMRGN.NAME)
if button_event & EditLayer.MOUSE_DOWN or button_event & EditLayer.MOUSE_DOUBLE:
self.current_event = []
unused = None
for l in self.zOrder:
if l == 63 and not self.show_anywhere:
continue
location = locations.locations[l]
if location.in_use():
x1,y1,x2,y2 = location.normalized_coords()
if button_event & EditLayer.MOUSE_DOWN:
event = resize_event(location,x,y)
if event:
self.current_event = event
if self.current_event[0] == EditLayer.EDIT_MOVE:
self.mouse_offset = [x1 - x,y1 - y]
self.resize_location = l
self.ui.action_manager.start_group()
self.action = ActionUpdateLocation(self.ui, l, location, ('start','end'))
self.ui.action_manager.add_action(self.action)
break
elif x1 <= x <= x2 and y1 <= y <= y2:
print 'Edit Location'
return
elif unused == None:
unused = l
if not self.current_event and unused != None and button_event & EditLayer.MOUSE_DOWN:
location = locations.locations[unused]
self.ui.action_manager.start_group()
strings = self.ui.chk.get_section(CHKSectionSTR.NAME)
name = 'Location %d' % (unused+1)
string = strings.lookup_string(name)
if not string:
string = strings.add_string(name)
action = ActionUpdateString(self.ui, string.string_id, None)
action.update_end_text()
self.ui.action_manager.add_action(action)
self.action = ActionUpdateLocation(self.ui, unused, location, ('name','elevation','start','end'))
self.ui.action_manager.add_action(self.action)
location.name = string.string_id+1
location.start[0] = nearest_multiple(x,32,math.floor)
location.start[1] = nearest_multiple(y,32,math.floor)
location.end[0] = location.start[0] + 32
location.end[1] = location.start[1] + 32
location.elevation = CHKLocation.ALL_ELEVATIONS
self.update_location(unused)
self.ui.listlayer_locations.clear_list()
self.ui.listlayer_locations.populate_list()
self.current_event = [EditLayer.EDIT_RESIZE_RIGHT,EditLayer.EDIT_RESIZE_BOTTOM]
self.mouse_offset = [0,0]
self.resize_location = unused
if self.current_event and self.resize_location != None:
self.raise_location(self.resize_location)
location = locations.locations[self.resize_location]
if self.current_event[0] == EditLayer.EDIT_MOVE:
x1,y1,x2,y2 = location.normalized_coords()
dx = (x + self.mouse_offset[0]) - x1
dy = (y + self.mouse_offset[1]) - y1
location.start[0] += dx
location.start[1] += dy
location.end[0] += dx
location.end[1] += dy
else:
if self.current_event[0] == EditLayer.EDIT_RESIZE_LEFT:
location.start[0] = x
elif self.current_event[0] == EditLayer.EDIT_RESIZE_RIGHT:
location.end[0] = x
if self.current_event[1] == EditLayer.EDIT_RESIZE_TOP:
location.start[1] = y
elif self.current_event[1] == EditLayer.EDIT_RESIZE_BOTTOM:
location.end[1] = y
if self.snap_to_grid:
location.start[0] = nearest_multiple(location.start[0],32)
location.start[1] = nearest_multiple(location.start[1],32)
location.end[0] = nearest_multiple(location.end[0],32)
location.end[1] = nearest_multiple(location.end[1],32)
self.update_location(self.resize_location)
if button_event & EditLayer.MOUSE_UP:
self.current_event = []
self.resize_location = None
self.action.set_end_values(location, self.action.start_values.keys())
self.action = None
self.ui.end_undo_group()
class EditLayerUnits(EditLayer):
def __init__(self, ui):
EditLayer.__init__(self, ui, "Units")
self.selected_images = []
self.selection_images = {}
self.selecting_start = None
self.selecting_images = None
self.selection_box = None
self.selecting_moved = False
def deselect(self, image):
if image in self.selected_images:
self.selected_images.remove(image)
if image.ref in self.selection_images:
selection_image = self.selection_images.get(image.ref)
if selection_image != None:
self.ui.maplayer_images.remove_image(selection_image)
del self.selection_images[image.ref]
def deselect_all(self):
for image in list(self.selected_images):
self.deselect(image)
self.ui.mapCanvas.tag_raise('selection_box')
def select(self, image):
if image.unit_ref != None:
self.selected_images.append(image)
unit_sect = self.ui.chk.get_section(CHKSectionUNIT.NAME)
selected_unit = unit_sect.get_unit(image.unit_ref)
flingy_id = self.ui.unitsdat.get_value(selected_unit.unit_id, 'Graphics')
sprite_id = self.ui.flingydat.get_value(flingy_id, 'Sprite')
selection_image_id = self.ui.spritesdat.get_value(sprite_id, 'SelectionCircleImage')
y_offset = self.ui.spritesdat.get_value(sprite_id, 'SelectionCircleOffset')
selection_image = BWImage(self.ui, '%s-sel%d' % (image.unit_ref,selection_image_id), 561 + selection_image_id, (selected_unit.position[0], selected_unit.position[1]), image, 'unit\\', elevation=0, pos_offset=(0,y_offset))
self.ui.maplayer_images.add_image(selection_image)
self.selection_images[image.ref] = selection_image
self.ui.mapCanvas.tag_raise('selection_box')
def mouse_event(self, button, button_event, x1,y1, x2,y2, mouseX,mouseY):
print flags(button_event,8)
if button == EditLayer.MOUSE_LEFT:
x = x1 + mouseX
y = y1 + mouseY
if button_event & EditLayer.MOUSE_DOWN:
self.selecting_start = (x,y)
self.selecting_images = set()
print ('Reset', flags(button_event,8))
elif self.selecting_start != None:
if not button_event & (EditLayer.MODIFIER_SHIFT | EditLayer.MODIFIER_CTRL):
self.deselect_all()
if button_event & EditLayer.MOUSE_MOVE:
self.selecting_moved = True
sx1 = min(self.selecting_start[0],x)
sy1 = min(self.selecting_start[1],y)
sx2 = max(self.selecting_start[0],x)
sy2 = max(self.selecting_start[1],y)
if self.selecting_moved:
if self.selection_box == None:
self.selection_box = self.ui.mapCanvas.create_rectangle(sx1,sy1,sx2,sy2, outline='#FF0000', tags='selection_box')
else:
self.ui.mapCanvas.coords(self.selection_box, sx1,sy1,sx2,sy2)
selecting = []
unit_sect = self.ui.chk.get_section(CHKSectionUNIT.NAME)
units = sorted(unit_sect.units.values(), cmp=lambda u1,u2,dat=self.ui.unitsdat: compare_z(u1,u2,dat,True))
for unit in units:
w = self.ui.unitsdat.get_value(unit.unit_id, 'StarEditPlacementBoxWidth')
h = self.ui.unitsdat.get_value(unit.unit_id, 'StarEditPlacementBoxHeight')
ux1 = unit.position[0] - w/2.0
uy1 = unit.position[1] - h/2.0
ux2 = ux1 + w
uy2 = uy1 + h
if not (sx1 > ux2 or ux1 > sx2 or sy1 > uy2 or uy1 > sy2):
ref = 'unit%d' % unit.ref_id
image = self.ui.maplayer_images.images.get(ref)
if image:
selecting.append(image)
select = set(selecting)
if not self.selecting_moved and selecting:
select = set((selecting[0],))
to_deselect = self.selecting_images - select
to_select = select - set(self.selected_images)
if not self.selecting_moved and select and not to_deselect and not to_select and button_event & (EditLayer.MODIFIER_SHIFT | EditLayer.MODIFIER_CTRL):
to_deselect = select
for image in to_deselect:
self.deselect(image)
for image in to_select:
self.select(image)
self.selecting_images = select
if button_event & EditLayer.MOUSE_UP:
self.ui.mapCanvas.delete(self.selection_box)
self.selecting_start = None
self.selecting_images = None
self.selection_box = None
self.selecting_moved = False
class EditLayerSprites(EditLayer):
def __init__(self, ui):
EditLayer.__init__(self, ui, "Sprites")
class EditLayerPreviewFog(EditLayer):
def __init__(self, ui):
EditLayer.__init__(self, ui, "Preview Fog")
class MinimapLayer:
def __init__(self, ui):
self.ui = ui
self.image = None
self.scaled_raw = None
self.scaled = None
self.size = None
def redraw(self):
self.image = None
if not self.ui.chk:
return
dims = self.ui.chk.get_section(CHKSectionDIM.NAME)
points = min(256 / dims.width, 256 / dims.height)
size = (dims.width*points,dims.height*points)
self.image = PILImage.new('RGBA', size)
drawer = PILDraw.ImageDraw(self.image)
self.draw(drawer, size, points)
def draw(self, drawer, size, points):
pass
def get_image(self, size):
if size == self.size and self.scaled:
return self.scaled
if not self.image:
return None
scale = min(size[0] / float(self.image.size[0]),size[1] / float(self.image.size[1]))
self.size = (int(round(self.image.size[0] * scale)), int(round(self.image.size[1] * scale)))
self.scaled_raw = self.image.resize(self.size)
self.scaled = ImageTk.PhotoImage(self.scaled_raw)
return self.scaled
class MinimapLayerTerrain(MinimapLayer):
def draw(self, drawer, size, points):
dims = self.ui.chk.get_section(CHKSectionDIM.NAME)
tiles = self.ui.chk.get_section(CHKSectionTILE.NAME)
for y in range(dims.width):
for x in range(dims.height):
tile = [0,0]
if y < len(tiles.map) and x < len(tiles.map[y]):
tile = tiles.map[y][x]
group = self.ui.tileset.cv5.groups[tile[0]]
mega = group[13][tile[1]]
minis = self.ui.tileset.vx4.graphics[mega]
mini = minis[0]
mini_image = self.ui.tileset.vr4.images[mini[0]]
index = mini_image[7][2 if mini[1] else 6]
color = self.ui.tileset.wpe.palette[index] + [255]
drawer.rectangle((x*points,y*points,x*points+points,y*points+points), fill=tuple(color))
class MinimapLayerUnits(MinimapLayer):
def draw(self, drawer, size, points):
dims = self.ui.chk.get_section(CHKSectionDIM.NAME)
unit = self.ui.chk.get_section(CHKSectionUNIT.NAME)
units = sorted(unit.units.values(), cmp=lambda u1,u2,dat=self.ui.unitsdat: compare_z(u1,u2,dat))
colors = CHKSectionCOLR.DEFAULT_COLORS
colr = self.ui.chk.get_section(CHKSectionCOLR.NAME)
if colr:
colors = colr.colors
for unit in units:
w = self.ui.unitsdat.get_value(unit.unit_id, 'StarEditPlacementBoxWidth') / (dims.width*32.0) * size[0]
h = self.ui.unitsdat.get_value(unit.unit_id, 'StarEditPlacementBoxHeight') / (dims.height*32.0) * size[0]
x = unit.position[0] / (dims.width*32.0) * size[0] - w/2.0
y = unit.position[1] / (dims.height*32.0) * size[1] - h/2.0
color = CHKSectionCOLR.NEUTRAL
if unit.owner < 8:
color = colors[unit.owner]
color = self.ui.tileset.wpe.palette[CHKSectionCOLR.PALETTE_INDICES(color)] + [255]
drawer.rectangle((x, y, x+w, y+h), fill=tuple(color))
class MapLayer:
def __init__(self, ui):
self.ui = ui
def update_display(self, x1,y1, x2,y2):
pass
def tick(self, dt):
pass
class MapLayerTerrain(MapLayer):
def __init__(self, ui):
MapLayer.__init__(self, ui)
self.tile_cache = {}
self.map = {}
def update_display(self, x1,y1, x2,y2):
tiles = self.ui.chk.get_section(CHKSectionTILE.NAME)
tx1 = int(x1 / 32.0)
ty1 = int(y1 / 32.0)
tx2 = int(ceil(x2 / 32.0))
ty2 = int(ceil(y2 / 32.0))
for y in range(ty1,ty2):
for x in range(tx1,tx2):
tag = '%s,%s' % (x,y)
item = self.map.get(tag)
if not item:
tile = [0,0]
if y < len(tiles.map) and x < len(tiles.map[y]):
tile = tiles.map[y][x]
group = self.ui.tileset.cv5.groups[tile[0]]
mega = group[13][tile[1]]
image = self.tile_cache.get(mega)
if image == None:
image = Tilesets.megatile_to_photo(self.ui.tileset, mega)
self.tile_cache[mega] = image
self.map[tag] = self.ui.mapCanvas.create_image((x+0.5) * 32, (y+0.5) * 32, image=image, tags=tag)
self.ui.mapCanvas.tag_lower(tag)
class MapLayerImages(MapLayer):
def __init__(self, ui):
MapLayer.__init__(self, ui)
self.images = None
def update_zorder(self):
zorder = sorted(self.images.values(), key=lambda img: img.z_position())
for image in zorder:
self.ui.mapCanvas.tag_raise(image.ref)
def add_image(self, image, update=True):
self.images[image.ref] = image
frame = image.current_frame()
position = image.render_position()
self.ui.mapCanvas.create_image(position[0],position[1], image=frame, tags=image.ref)
if update:
self.update_zorder()
def remove_image(self, image):
del self.images[image.ref]
self.ui.mapCanvas.delete(image.ref)
def update_display(self, x1,y1, x2,y2):
if self.images == None:
units = self.ui.chk.get_section(CHKSectionUNIT.NAME)
self.images = {}
for n in range(units.unit_count()):
unit = units.nth_unit(n)
flingy_id = self.ui.unitsdat.get_value(unit.unit_id, 'Graphics')
sprite_id = self.ui.flingydat.get_value(flingy_id, 'Sprite')
image_id = self.ui.spritesdat.get_value(sprite_id,'ImageFile')
elevation = self.ui.unitsdat.get_value(unit.unit_id, 'ElevationLevel')
ref = 'unit%d' % unit.ref_id
image = BWImage(self.ui, ref, image_id, unit.position, None, 'unit\\', elevation, unit_ref=unit.ref_id)
self.add_image(image, update=False)
self.update_zorder()
def tick(self, dt):
for ref in self.images.keys():
image = self.images.get(ref)
if image:
image.tick(dt)
if ref in self.images:
frame = image.current_frame()
self.ui.mapCanvas.itemconfig(ref, image=frame)
class ListLayer:
NAME = None
def __init__(self, ui):
self.ui = ui
self.groupId = None
self.options = {}
def setup_group(self):
self.groupId = self.ui.listbox.insert('-1', self.NAME, False)
self.populate_list()
return self.groupId
def clear_list(self):
self.ui.listbox.delete('%s.%s' % (self.groupId,ALL))
self.options = {}