-
Notifications
You must be signed in to change notification settings - Fork 0
/
endGameReplay.py
930 lines (781 loc) · 37.8 KB
/
endGameReplay.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
from PyQt5 import QtWidgets
from utils.hexgrid import *
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
import sys # We need sys so that we can pass argv to QApplication
import os
from saveFileHandler.gameDataHandler import *
import io
from PIL import Image
from pygifsicle import gifsicle
import subprocess as sp
import argparse
import platform
platformWin7 = platform.release() == "7" and platform.system() == "Windows"
LANGUAGES = ["en_EN", "ru_RU", "de_DE", "es_ES", "fr_FR", "it_IT", "ja_JP", "ko_KR", "pl_PL", "pt_BR"]
use_original_name = False
def run_arg_parser():
print(
f"####################################################################\n"
f"Civ6EGRM: End Game Replay Map for Civ6 (Version 20.06.2021 V1.00.09)\n"
f"Created by SH\n"
f"If errors occur, send e.g. 10 autosave files in a zip\n"
f"####################################################################\n"
)
# Create the parser
arg_parser = argparse.ArgumentParser(description='endGameReplay.py reads all civ6 autosave files from -d "<location>"\n'
'and displays end game replay map turn by turn. You can also create\n'
'mp4 and gif files from your game.')
# Add the arguments
arg_parser.add_argument('-d', metavar='civ6 autosave directory', action='store', type=str,
required=False, help='the path to the autosave folder')
# Execute the parse_args() method
args = arg_parser.parse_args()
target_path = args.d
if target_path:
if not os.path.isdir(target_path):
target_path = None
else:
target_path = os.path.expanduser(target_path + "/")
if not target_path:
print('The path specified does not exist using default target folder ./auto/data, use -d <dir_path> to change')
target_path = os.getcwd() + "/data/auto/" # Default location where runFileWatcher copies all auto saves
return target_path
# pg.setConfigOptions(antialias=True)
# class for scrollable label
class ScrollLabel(QtWidgets.QScrollArea):
# constructor
def __init__(self, *args, **kwargs):
QtWidgets.QScrollArea.__init__(self, *args, **kwargs)
# making widget resizable
self.setWidgetResizable(True)
# making qwidget object
content = QtWidgets.QWidget(self)
self.setWidget(content)
# vertical box layout
lay = QtWidgets.QVBoxLayout(content)
# creating label
self.label = QtWidgets.QLabel() # content
# setting alignment to the text
self.label.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
# making label multi-line
self.label.setWordWrap(True)
# adding label to the layout
lay.addWidget(self.label)
def set_text(self, text):
# setting text to the label
self.label.setText(text)
class MapVisualizerWidget(QtWidgets.QWidget):
def __init__(self, parent, M, N, envColors, riverColors, outerBordersOnly, borderColors, borderColorsInner,
borderColorsSC, citiesColors, goodyHutColors, turnCount, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.parent = parent
layout = QtWidgets.QVBoxLayout(self)
layoutH = QtWidgets.QHBoxLayout(self)
# Civilization names
self.civNames = QtWidgets.QLabel(self)
self.civNames.setWordWrap(True)
self.civNames.setText("Finland\nis a great country!")
#self.civNames.setStyleSheet("outline: 2px black;")
layoutH.addWidget(self.civNames)
# creating a QGraphicsDropShadowEffect object
shadow = QtWidgets.QGraphicsDropShadowEffect()
# setting blur radius
shadow.setBlurRadius(1)
shadow.setOffset(1)
# adding shadow to the label
self.civNames.setGraphicsEffect(shadow)
self.graphWidget = pg.PlotWidget()
layoutH.addWidget(self.graphWidget)
self.graphWidget.setAspectLocked(True)
self.graphWidget.setAntialiasing(True)
self.graphWidget.setBackground((0, 0, 0, 0))
self.graphWidget.getPlotItem().hideAxis('bottom')
self.graphWidget.getPlotItem().hideAxis('left')
self.graphWidget.scene().sigMouseClicked.connect(self.parent.mouse_clicked)
self.graphWidget.setMouseTracking(True)
self.eventList = ScrollLabel()
layoutH.addWidget(self.eventList)
self.eventList.set_text("Test\nNothing\nTo\nSee\nHere\nYet\nSorry\n\U0001F3BC \n\u2699 \n\U0001F6E1 \nWar, peace, capture, raze, etc...\n\n\n\n\n^\n|\n|\n|\nv\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\U0001F64F \n\u2697 \n\U0001F4B0 \n\u2693 ")
self.eventList.setFrameShape(QtWidgets.QFrame.NoFrame)
# self.eventList.setAlignment(QtCore.Qt.AlignRight)
self.eventList.setMaximumWidth(200)
# self.eventList.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.eventList.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.eventList.setVisible(False)
shadow2 = QtWidgets.QGraphicsDropShadowEffect()
shadow2.setBlurRadius(1)
shadow2.setOffset(1)
self.eventList.label.setGraphicsEffect(shadow2)
layout.addLayout(layoutH)
self.environmentHG = HexGrid(M, N)
self.environmentHG.set_fc_colors(envColors)
#self.environmentHG.set_edges_visible(False)
#self.environmentHG.set_lw(0.1)
self.environmentHG.generatePicture()
self.graphWidget.addItem(self.environmentHG)
self.riversHG = HexGrid(M, N, 1.0, True)
self.riversHG.set_ec_colors(riverColors)
self.riversHG.generatePicture()
self.graphWidget.addItem(self.riversHG)
# Inner borders secondary color
self.bordersHG_inner = HexGrid(M, N, 0.55, outerBordersOnly)
self.bordersHG_inner.set_ec_colors(borderColorsInner)
self.bordersHG_inner.generatePicture()
self.graphWidget.addItem(self.bordersHG_inner)
# Outer borders primary color
self.bordersHG = HexGrid(M, N, 0.7, outerBordersOnly)
self.bordersHG.set_ec_colors(borderColors)
self.bordersHG.generatePicture()
self.graphWidget.addItem(self.bordersHG)
# City state borders color
self.bordersHG_cs = HexGrid(M, N, 0.85, outerBordersOnly)
self.bordersHG_cs.set_ec_colors(borderColorsSC)
self.bordersHG_cs.generatePicture()
self.graphWidget.addItem(self.bordersHG_cs)
self.citiesHG = HexGrid(M, N, 0.5)
self.citiesHG.set_fc_colors(citiesColors)
self.citiesHG.set_edges_visible(False)
self.citiesHG.generatePicture()
self.graphWidget.addItem(self.citiesHG)
self.goodyHutHG = HexGrid(M, N, 0.35)
self.goodyHutHG.set_fc_colors(goodyHutColors)
self.goodyHutHG.set_edges_visible(False)
self.goodyHutHG.generatePicture()
self.graphWidget.addItem(self.goodyHutHG)
slider_widget = QtWidgets.QWidget(self)
layout.addWidget(slider_widget)
slider_layout = QtWidgets.QHBoxLayout(slider_widget)
self.turnSlider = QtWidgets.QSlider(QtCore.Qt.Horizontal, self)
self.turnSlider.setMinimum(1)
self.turnSlider.setMaximum(turnCount)
self.turnSlider.setValue(1)
self.turnSlider.setTickPosition(QtWidgets.QSlider.TicksBelow)
self.turnSlider.setTickInterval(1)
slider_layout.addWidget(self.turnSlider)
self.turnSlider.valueChanged.connect(self.parent.updateTurn)
slider_value = QtWidgets.QLabel(self)
slider_layout.addWidget(slider_value)
slider_value.setNum(1)
self.turnSlider.valueChanged.connect(slider_value.setNum)
self.symbols = {} # pg.TextItem('', **{'color': '#FFF'})
# self.graphWidget.addItem(self.label_value)
# self.label_value.setPos(QtCore.QPointF(2, 10))
# self.label_value.setText("\U0001F6E1")
def add_symbol(self, key, x, y, text):
self.symbols[key] = pg.TextItem('', anchor=(0.5, 0.5)) # pg.TextItem('', **{'color': '#FFF'})
self.graphWidget.addItem(self.symbols[key])
self.symbols[key].setPos(QtCore.QPointF(x, y))
self.symbols[key].setHtml(text)
shadow = QtWidgets.QGraphicsDropShadowEffect()
shadow.setBlurRadius(1)
shadow.setOffset(1)
self.symbols[key].setGraphicsEffect(shadow)
def set_symbol_size(self, size):
font = QtGui.QFont()
font.setPixelSize(size)
for symbol in self.symbols:
if symbol[:9] != "CityName_":
self.symbols[symbol].setFont(font)
def set_symbol_shadow(self, key, shadowColor, blurRad=1, blurOffset=1):
shadow = QtWidgets.QGraphicsDropShadowEffect()
shadow.setBlurRadius(blurRad)
shadow.setOffset(blurOffset)
if shadowColor is not None:
shadow.setColor(QtGui.QColor(*shadowColor.astype(int)))
if key in self.symbols:
self.symbols[key].setGraphicsEffect(shadow)
def set_symbol_text_size(self, size):
font = QtGui.QFont()
font.setPixelSize(size)
for symbol in self.symbols:
if symbol[:9] == "CityName_":
self.symbols[symbol].setFont(font)
class EventFilterWindow(QtWidgets.QWidget):
def __init__(self, parent):
super().__init__()
self.setWindowTitle("Set event filters")
self.parent = parent
self.event_filters = parent.event_filters.copy()
layoutV0 = QtWidgets.QVBoxLayout()
layoutV1 = QtWidgets.QVBoxLayout()
layoutH1 = QtWidgets.QHBoxLayout()
self.filterRulesCheckBoxes = []
for key, value in parent.event_filters.items():
cbox = QtWidgets.QCheckBox(key)
cbox.setChecked(value)
self.filterRulesCheckBoxes.append(cbox)
self.filterRulesCheckBoxes[-1].stateChanged.connect(
lambda state, x=self.filterRulesCheckBoxes[-1]: self.checkBoxStatesChanged(x))
layoutV1.addWidget(cbox)
layoutV0.addLayout(layoutV1)
self.apply = QtWidgets.QPushButton("Apply filters")
self.apply.clicked.connect(self.applyNewFilters)
layoutH1.addWidget(self.apply)
self.cancel = QtWidgets.QPushButton("Cancel")
self.cancel.clicked.connect(self.cancelFilters)
layoutH1.addWidget(self.cancel)
layoutV0.addLayout(layoutH1)
self.setLayout(layoutV0)
self.setMinimumWidth(self.sizeHint().width()*2)
def checkBoxStatesChanged(self, cbox):
self.event_filters[cbox.text()] = cbox.isChecked()
def applyNewFilters(self):
self.parent.event_filters = self.event_filters
self.parent.applyEventFilter()
self.close()
def cancelFilters(self):
self.close()
class DebugWindow(QtWidgets.QWidget):
def __init__(self, parent):
#super().__init__(self)
QtWidgets.QWidget.__init__(self, parent)
self.setWindowTitle("Debug")
self.parent = parent
self.setWindowFlags(self.windowFlags() | QtCore.Qt.Dialog)
layout = QtWidgets.QHBoxLayout()
self.x_value = QtWidgets.QLabel()
layout.addWidget(self.x_value)
self.x_value.setText("X: " + str(int(self.parent.x_value)))
self.y_value = QtWidgets.QLabel()
layout.addWidget(self.y_value)
self.y_value.setText("Y: " + str(int(self.parent.y_value)))
self.hex_value = QtWidgets.QLabel()
layout.addWidget(self.hex_value)
self.hex_value.setText("HexaIdx: " + str(int(self.parent.hex_value)))
self.civ = QtWidgets.QLabel()
layout.addWidget(self.civ)
self.civ.setWordWrap(True)
self.civ.setText(self.parent.owner)
# creating a QGraphicsDropShadowEffect object
shadow = QtWidgets.QGraphicsDropShadowEffect()
# setting blur radius
shadow.setBlurRadius(1)
shadow.setOffset(1)
# adding shadow to the label
self.civ.setGraphicsEffect(shadow)
self.setLayout(layout)
self.setMinimumWidth(self.sizeHint().width())
self.move(100, 0)
self.parent.debug_signal.connect(self.updateDebugs)
def updateDebugs(self):
self.x_value.setText("X: " + str(int(self.parent.x_value)))
self.y_value.setText("Y: " + str(int(self.parent.y_value)))
self.hex_value.setText("HexaIdx: " + str(int(self.parent.hex_value)))
self.civ.setText(self.parent.owner)
self.setMinimumWidth(self.sizeHint().width())
class MainWindow(QtWidgets.QMainWindow):
debug_signal = QtCore.pyqtSignal()
def __init__(self, app, target_path=None, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Civ VI End Game Replay Map")
self.app = app
self.pause = True
self.currentIdx = 0
self.current_timer = None
self.timerCount = 0
self.timerCurrentCount = 0
self.imageList = []
self.hidden = False
self.enableTiming = False
self.outputFps = 10
self.symbolSize = 20
self.symbolTextSize = 10
self.eventListWidth = 200
self.civWidth = None
self.hiddenCityNames = False
self.backgroundTextColor = False
self.language = "en_EN"
self.filterWindow = None
self.debugWindow = None
self.x_value = 0
self.y_value = 0
self.hex_value = 0
self.owner = ""
self.event_filters = {
"War Major": False,
"War Minor": False,
"War Minor Minor": True,
"Peace Major": False,
"Peace Minor": False,
"Peace Minor Minor": True,
}
self.playSc = QtWidgets.QShortcut(QtGui.QKeySequence('Space'), self)
self.playSc.activated.connect(self.play)
# Options
self.OptimizeGif = True
self.outerBordersOnly = True
self.useCivColors = True
self.riversOn = True
self.drawWaterBorders = True
self.useInnerColorAsCity = True
if target_path:
self.saveDataLocation = target_path
else:
self.saveDataLocation = os.getcwd() + "/data/auto/" # Default location where runFileWatcher copies all auto saves
# Read and parse all files to memory
self.gdh = GameDataHandler(self.saveDataLocation)
self.gdh.parseData()
# Calculate border colors
self.gdh.calculateBorderColors(3, self.outerBordersOnly, self.useCivColors, self.drawWaterBorders)
self.gdh.calculateCityColors(self.useInnerColorAsCity)
# self.gdh.calculateMinorCityColors()
# Calculate environment colors
self.gdh.calculateEnvColors()
self.gdh.calculateRiverColors()
# Other stuff
self.gdh.calculateOtherStuff()
# MapSize
self.M, self.N = self.gdh.getMapSize()
# TurnCount
self.TurnCount = self.gdh.getTurnCount()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
main_layout = QtWidgets.QVBoxLayout(self._main)
self._createMenuBar()
self.plot_widget = \
MapVisualizerWidget(self, self.M, self.N, self.gdh.envColors, self.gdh.riverColors,
self.outerBordersOnly, self.gdh.borderColors[0], self.gdh.borderColorsInner[0],
self.gdh.borderColorsSC[0], self.gdh.cityColors[0], self.gdh.goodyHuts[0],
self.TurnCount)
main_layout.addWidget(self.plot_widget)
# Set civ names
self.gdh.parseCivNames()
self.setCivilizationNames(self.gdh.getCivNames(0))
# City state symbol locations
for i, minor in enumerate(self.gdh.minorOrigos):
x, y = self.plot_widget.environmentHG.get_hexa_xy(self.gdh.minorOrigos[minor])
# symbol = CS_UNICODE_MAP[self.gdh.minorCivTypes[minor]].replace(" ", "").replace(" ", "")
self.plot_widget.add_symbol("CityState_" + str(minor), x, y, "")
# Razed city symbols
for turn in self.gdh.razedCityLocs:
# Set new ones
for cityLoc in turn:
razedCity = "razedCity_" + str(cityLoc)
if razedCity not in self.plot_widget.symbols:
x, y = self.plot_widget.environmentHG.get_hexa_xy(cityLoc)
self.plot_widget.add_symbol(razedCity, x, y, "")
# City name symbols
offset = 0.8
for turn in self.gdh.cityData:
# Set new ones
for city in turn["cities"]:
cityName = "CityName_" + city["CityName"]
if cityName not in self.plot_widget.symbols:
x, y = self.plot_widget.environmentHG.get_hexa_xy(city["LocationIdx"])
self.plot_widget.add_symbol(cityName, x, y + offset, "")
self.plot_widget.set_symbol_size(self.symbolSize)
self.plot_widget.set_symbol_text_size(self.symbolTextSize)
self.gdh.createEvents()
self.showMaximized()
def _createMenuBar(self):
self._createActions()
menuBar = self.menuBar()
# Creating menus using a QMenu object
optionsMenu = QtWidgets.QMenu("&Options", self)
menuBar.addMenu(optionsMenu)
menuBar.addAction(self.playAction)
menuBar.addAction(self.debugAction)
self.comboBox = QtWidgets.QComboBox()
for lan in LANGUAGES:
self.comboBox.addItem(lan)
self.languageListAction = QtWidgets.QWidgetAction(None)
self.languageListAction.setDefaultWidget(self.comboBox)
self.languageMenu = QtWidgets.QMenu("&Set Language", self)
self.languageMenu.addAction(self.languageListAction)
menuBar.addMenu(self.languageMenu)
self.comboBox.activated[str].connect(self.setLanguage)
self.statusMenu = QtWidgets.QMenu("&Status: Ready", self)
self.statusMenu.setDisabled(True)
menuBar.addMenu(self.statusMenu)
borderMenu = optionsMenu.addMenu("Borders")
borderMenu.addAction(self.toggleBorderAction)
borderMenu.addAction(self.toggleWaterBorderAction)
civnameMenu = optionsMenu.addMenu("Civilization panel")
civnameMenu.addAction(self.toggleCivilizationNamesAction)
civnameMenu.addAction(self.set_civ_widthAction)
optionsMenu.addAction(self.updateFpsAction)
createMenu = optionsMenu.addMenu("Create ...")
createMenu.addAction(self.createGifAction)
createMenu.addAction(self.createMp4Action)
symbolMenu = optionsMenu.addMenu("Map symbol sizes")
symbolMenu.addAction(self.updateSymbolSizeAction)
symbolMenu.addAction(self.updateSymbolTextSizeAction)
cityMenu = optionsMenu.addMenu("City name options")
cityMenu.addAction(self.toggleCityNamesAction)
cityMenu.addAction(self.toggleCityNamesBgAction)
eventMenu = optionsMenu.addMenu("Event")
eventMenu.addAction(self.toggle_eventsAction)
eventMenu.addAction(self.set_event_widthAction)
eventMenu.addAction(self.setFilterAction)
def showEventFilterWindow(self):
self.filterWindow = EventFilterWindow(self)
self.filterWindow.show()
def showDebugWindow(self):
self.debugWindow = DebugWindow(self)
self.debugWindow.show()
def _createActions(self):
# self.newAction = QtWidgets.QAction(self)
# self.newAction.setText("&New")
# self.newAction.setIcon(QIcon(":file-new.svg"))
# self.openAction = QAction(QIcon(":file-open.svg"), "&Open...", self)
# self.exitAction = QAction("&Exit", self)
self.toggleBorderAction = QtWidgets.QAction("&Toggle borders", self)
self.toggleBorderAction.triggered.connect(self.toggleBorders)
self.toggleWaterBorderAction = QtWidgets.QAction("&Toggle borders on water", self)
self.toggleWaterBorderAction.triggered.connect(self.toggleWaterBorders)
self.updateSymbolSizeAction = QtWidgets.QAction("&Set symbol size on map", self)
self.updateSymbolSizeAction.triggered.connect(self.updateSymbolSize)
self.updateSymbolTextSizeAction = QtWidgets.QAction("&Set city name size on map", self)
self.updateSymbolTextSizeAction.triggered.connect(self.updateSymbolTextSize)
self.toggleCityNamesAction = QtWidgets.QAction("&Toggle city names", self)
self.toggleCityNamesAction.triggered.connect(self.toggleCityNames)
self.toggleCityNamesBgAction = QtWidgets.QAction("&Toggle city name background shadow color", self)
self.toggleCityNamesBgAction.triggered.connect(self.toggleCityNamesBg)
self.toggle_eventsAction = QtWidgets.QAction("&Toggle events", self)
self.toggle_eventsAction.triggered.connect(self.toggle_events)
self.set_event_widthAction = QtWidgets.QAction("&Set events width", self)
self.set_event_widthAction.triggered.connect(self.set_event_width)
self.setFilterAction = QtWidgets.QAction("&Set event filters", self)
self.setFilterAction.triggered.connect(self.showEventFilterWindow)
self.toggleCivilizationNamesAction = QtWidgets.QAction("&Toggle civilization names", self)
self.toggleCivilizationNamesAction.triggered.connect(self.toggleCivilizationNames)
self.set_civ_widthAction = QtWidgets.QAction("&Set civilization names panel width", self)
self.set_civ_widthAction.triggered.connect(self.set_civ_width)
self.createGifAction = QtWidgets.QAction("&a .gif", self)
self.createGifAction.triggered.connect(self.createGif)
self.createMp4Action = QtWidgets.QAction("&a .mp4", self)
self.createMp4Action.triggered.connect(self.createMp4)
self.updateFpsAction = QtWidgets.QAction("&Set output fps", self)
self.updateFpsAction.triggered.connect(self.updateFps)
self.playAction = QtWidgets.QAction("&Play/Pause", self)
self.playAction.triggered.connect(self.play)
self.debugAction = QtWidgets.QAction("&Debug", self)
self.debugAction.triggered.connect(self.showDebugWindow)
def applyEventFilter(self):
self.gdh.filterEvents(self.event_filters)
self.currentIdx = self.plot_widget.turnSlider.sliderPosition()
self.updateEvents(self.currentIdx - 1)
def setLanguage(self, language):
if language == LANGUAGES[0]:
self.gdh.parseCivNames()
else:
self.gdh.parseCivNames(language)
self.language = language
self.currentIdx = self.plot_widget.turnSlider.sliderPosition()
self.updateCivs(self.currentIdx - 1)
self.gdh.createEvents()
self.applyEventFilter()
self.updateEvents(self.currentIdx - 1)
def updateCivs(self, turn):
self.setCivilizationNames(self.gdh.getCivNames(turn))
self.setCityNameSymbolAtTurn(turn)
def updateFps(self):
num, ok = QtWidgets.QInputDialog.getInt(self, "Set output fps value", "Enter a number", self.outputFps)
if ok:
self.outputFps = num
#self.buttons_widget.fps.setNum(num)
def updateSymbolSize(self):
num, ok = QtWidgets.QInputDialog.getInt(self, "Set on map symbol size", "Enter a number", self.symbolSize)
if ok:
self.symbolSize = num
# self.buttons_widget.symbol_size.setNum(num)
self.plot_widget.set_symbol_size(num)
def updateSymbolTextSize(self):
num, ok = QtWidgets.QInputDialog.getInt(self, "Set on map text size", "Enter a number", self.symbolTextSize)
if ok:
self.symbolTextSize = num
self.plot_widget.set_symbol_text_size(num)
def set_civ_width(self):
num, ok = QtWidgets.QInputDialog.getInt(self, "Set on civ width", "Enter a number",
self.plot_widget.civNames.sizeHint().width())
if ok:
self.civWidth = num
self.plot_widget.civNames.setMaximumWidth(num)
# else:
# self.plot_widget.civNames.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Maximum)
# self.plot_widget.civNames.setMaximumSize(self.plot_widget.civNames.sizeHint())
def set_event_width(self):
num, ok = QtWidgets.QInputDialog.getInt(self, "Set on event list width", "Enter a number",
self.plot_widget.eventList.sizeHint().width())
if ok:
self.eventListWidth = num
self.plot_widget.eventList.setMaximumWidth(num)
def toggle_events(self):
self.plot_widget.eventList.setHidden(not self.plot_widget.eventList.isHidden())
def toggleCivilizationNames(self):
self.plot_widget.civNames.setHidden(not self.plot_widget.civNames.isHidden())
def toggleCityNames(self):
self.hiddenCityNames = not self.hiddenCityNames
if self.hiddenCityNames:
for symbol in self.plot_widget.symbols:
if symbol[:9] == "CityName_":
self.plot_widget.symbols[symbol].setHtml("")
else:
self.setCityNameSymbolAtTurn(self.plot_widget.turnSlider.sliderPosition() - 1)
def toggleCityNamesBg(self):
self.backgroundTextColor = not self.backgroundTextColor
if not self.hiddenCityNames:
self.setCityNameSymbolAtTurn(self.plot_widget.turnSlider.sliderPosition() - 1)
def setCivilizationNames(self, text):
self.plot_widget.civNames.setText(text)
def setCityStateSymbolAtTurn(self, idx):
for i in range(self.gdh.majorCivs, self.gdh.minorCivs + self.gdh.majorCivs):
cityState = "CityState_" + str(i)
if cityState in self.plot_widget.symbols:
symbol = "" # \u2b22
colorhexMinor = "181818"
if self.gdh.playersAlive[idx][i] and idx != 0:
symbol = CS_UNICODE_MAP[self.gdh.minorCivTypes[i]].replace(" ", "").replace(" ", "")
colorhexMinor = ''.join([format(int(c), '02x') for c in civColorsMinor[i]])
self.plot_widget.symbols[cityState].setHtml("<font color=#" + colorhexMinor + ">" + symbol + "</font>")
def setCityNameSymbolAtTurn(self, idx):
turn = self.gdh.cityData[idx]
bg = self.backgroundTextColor
for symbol in self.plot_widget.symbols:
if symbol[:9] == "CityName_":
self.plot_widget.symbols[symbol].setHtml("")
if not self.hiddenCityNames:
for cityIdx, city in enumerate(turn["cities"]):
cityName = city["CityName"]
cityNameTag = "CityName_" + cityName
# if additional city name data
if not use_original_name:
if "cityNameData" in city:
# if custom or not
if city["cityNameData"]["Orig"]:
# if localization data exists
if "cityLocData" in city:
# if english or not
if self.language != LANGUAGES[0]:
cityName = city["cityLocData"][self.language]
else:
cityName = city["cityNameData"]["CityName"] # Custom city name
if cityNameTag in self.plot_widget.symbols:
civIdx = city["CivIndex"]
buildOver = self.checkRemainingCityCoordinates(idx, cityIdx, city["LocationIdx"])
if civIdx < 0 or buildOver:
cityName = ""
colorhex = ''.join([format(int(c), '02x') for c in civColorsInner[civIdx]])
if bg:
colorbg = civColors[civIdx]
self.plot_widget.set_symbol_shadow(cityNameTag, colorbg)
else:
self.plot_widget.set_symbol_shadow(cityNameTag, None)
self.plot_widget.symbols[cityNameTag].setHtml("<font color=#" + colorhex + ">" + cityName + "</font>") # background-color=#FF0000 text-shadow=2px 2px #FF0000
def checkRemainingCityCoordinates(self, turnIdx, cityIdx, loc):
turn = self.gdh.cityData[turnIdx]
for city in turn["cities"][cityIdx+1:]:
if city["LocationIdx"] == loc:
return True
return False
def setCityRazedAtTurn(self, idx):
turn = self.gdh.razedCityLocs[idx]
# Clear old ones "razedCity_" symbols
for symbol in self.plot_widget.symbols:
if symbol[:10] == "razedCity_":
self.plot_widget.symbols[symbol].setHtml("")
# Set new ones
for cityLoc in turn:
razedCity = "razedCity_" + str(cityLoc)
if razedCity in self.plot_widget.symbols:
symbol = "\U0001F3DA"
colorhex = "181818"
self.plot_widget.symbols[razedCity].setHtml("<font color=#" + colorhex + ">" + symbol + "</font>")
def updateEvents(self, turn, highlightThreshold=2):
event_txt = ""
colorhex = ''.join([format(int(c), '02x') for c in COLORS_PRISM["COLOR_STANDARD_RED_DK"]])
for event in reversed(self.gdh.events):
if event["TurnIdx"] + highlightThreshold < turn:
event_txt += event["Event"] + "<br>"
elif event["TurnIdx"] <= turn:
event_list = event["Event"].split(":")
new_event = "<font color=#" + colorhex + ">" + event_list[0] + ":</font>" + event_list[1]
event_txt += new_event + "<br>"
else:
continue
self.plot_widget.eventList.label.setText(event_txt)
def updateTurn(self, turn):
t0 = time.time()
self.plot_widget.bordersHG_cs.set_ec_colors(self.gdh.borderColorsSC[turn - 1])
t1 = time.time()
tBorderSC = t1 - t0
t0 = time.time()
self.plot_widget.bordersHG_inner.set_ec_colors(self.gdh.borderColorsInner[turn - 1])
t1 = time.time()
tBorderInner = t1 - t0
t0 = time.time()
self.plot_widget.bordersHG.set_ec_colors(self.gdh.borderColors[turn - 1])
t1 = time.time()
tBorder = t1 - t0
t0 = t1
self.plot_widget.goodyHutHG.set_fc_colors(self.gdh.goodyHuts[turn - 1])
t1 = time.time()
tGoody = t1 - t0
t0 = t1
self.plot_widget.citiesHG.set_fc_colors(self.gdh.cityColors[turn - 1])
t1 = time.time()
tCity = t1 - t0
self.setCityStateSymbolAtTurn(turn - 1)
self.setCityRazedAtTurn(turn - 1)
self.setCityNameSymbolAtTurn(turn - 1)
self.updateCivs(turn - 1)
self.updateEvents(turn - 1)
if self.enableTiming:
print("Border update took {} s".format(tBorderSC))
print("Border update took {} s".format(tBorderInner))
print("Border update took {} s".format(tBorder))
print("GoodyHut update took {} s".format(tGoody))
print("City update took {} s".format(tCity))
def start_timer(self, count=1, interval=1000):
if self.current_timer:
self.current_timer.stop()
self.current_timer = None
self.timerCount = count
self.timerCurrentCount = 0
self.current_timer = QtCore.QTimer()
self.current_timer.timeout.connect(self.handler)
self.current_timer.start(interval)
def handler(self):
self.timerCurrentCount += 1
self.updateSlider(self.currentIdx)
self.currentIdx += 1
if self.timerCurrentCount >= self.timerCount or self.pause:
self.current_timer.stop()
self.current_timer = None
def toggleBorders(self):
self.currentIdx = self.plot_widget.turnSlider.sliderPosition()
if self.hidden:
self.plot_widget.bordersHG_cs.set_ec_colors(self.gdh.borderColorsSC[self.currentIdx - 1])
self.plot_widget.bordersHG_inner.set_ec_colors(self.gdh.borderColorsInner[self.currentIdx - 1])
self.plot_widget.bordersHG.set_ec_colors(self.gdh.borderColors[self.currentIdx - 1])
self.hidden = False
else:
self.plot_widget.bordersHG_cs.set_ec_colors([emptyPen] * len(self.gdh.borderColors[self.currentIdx - 1]))
self.plot_widget.bordersHG_inner.set_ec_colors([emptyPen] * len(self.gdh.borderColors[self.currentIdx - 1]))
self.plot_widget.bordersHG.set_ec_colors([emptyPen] * len(self.gdh.borderColors[self.currentIdx - 1]))
self.hidden = True
def toggleWaterBorders(self):
self.drawWaterBorders = not self.drawWaterBorders
print("Recalculating borders")
self.updateStatus("&Status: Recalculating borders")
self.gdh.calculateBorderColors(3, self.outerBordersOnly, self.useCivColors, self.drawWaterBorders)
print("Borders calculated")
self.updateStatus("&Status: Ready")
self.currentIdx = self.plot_widget.turnSlider.sliderPosition()
self.updateTurn(self.currentIdx)
def play(self):
if self.pause:
self.pause = False
self.currentIdx = self.plot_widget.turnSlider.sliderPosition()
self.start_timer(self.TurnCount-self.currentIdx+1, 100)
else:
self.pause = True
def updateStatus(self, status):
self.statusMenu.setTitle(status)
#self.buttons_widget.update()
self._main.update()
QtWidgets.QApplication.processEvents()
def createGif(self):
M = len(self.imageList)
if M == 0:
self.createImages()
M = len(self.imageList)
print("Please wait patiently: saving and optimizing gif!")
self.updateStatus("&Status: Creating gif, please wait")
fps = self.outputFps
self.imageList[0].save('endGameReplayMap.gif', save_all=True, append_images=self.imageList[1:], optimize=False, duration=1000/fps, loop=0)
self.updateStatus("&Status: Optimizing gif, please wait")
gifsicle(
sources=["endGameReplayMap.gif"], # or a single_file.gif
destination="endGameReplayMap.gif", # or just omit it and will use the first source provided.
optimize=False, # Whetever to add the optimize flag of not, optimized with -O3 option
colors=256, # Number of colors t use
options=["--verbose", "-O3"], # Options to use. , "--lossy=0" , "-O3"
)
self.updateStatus("&Status: Gif done!")
print("Gif done!")
def createMp4(self):
remove_row = False
remove_column = False
M = len(self.imageList)
if M == 0:
self.createImages()
M = len(self.imageList)
width, height = self.imageList[0].size
# To fix error when odd width/height size (in windows 7 version)
if height % 2 == 1:
remove_row = True
height -= 1
if width % 2 == 1:
remove_column = True
width -= 1
fps = self.outputFps
print("Creating mp4, please wait")
self.updateStatus("&Status: Creating mp4, please wait")
command = ["ffmpeg.exe",
"-f", "rawvideo",
"-vcodec", "rawvideo",
"-s", str(width) + "x" + str(height),
"-pix_fmt", "rgba",
"-r", str(fps),
"-loglevel", "error",
"-i", "pipe:",
"-vcodec", "h264",
"-pix_fmt", "yuv444p", # "yuv420p"
"-y", "endGameReplayMap.mp4"]
pipe = sp.Popen(command, stdin=sp.PIPE, stderr=sp.PIPE)
for image in self.imageList:
imtemp = np.array(image.copy())
if remove_row:
imtemp = np.delete(imtemp, 0, 0)
if remove_column:
imtemp = np.delete(imtemp, 0, 1)
pipe.stdin.write(imtemp.tobytes())
for ii in range(M % fps + 1):
imtemp = np.array(self.imageList[M - 1].copy())
if remove_row:
imtemp = np.delete(imtemp, 0, 0)
if remove_column:
imtemp = np.delete(imtemp, 0, 1)
pipe.stdin.write(imtemp.tobytes())
self.updateStatus("&Status: Mp4 done!")
print("Mp4 done!")
def createImages(self):
self.updateStatus("&Status: Gathering data")
for ii in range(1, self.TurnCount + 1):
self.plot_widget.turnSlider.setValue(ii)
self._main.update()
QtWidgets.QApplication.processEvents()
screenshot = self.plot_widget.grab()
buffer = QtCore.QBuffer()
buffer.open(QtCore.QBuffer.ReadWrite)
screenshot.save(buffer, "png")
self.imageList.append(Image.open(io.BytesIO(buffer.data())))
def updateSlider(self, idx):
self.plot_widget.turnSlider.setValue(idx)
def mouse_clicked(self, mouseClickEvent):
# mouseClickEvent is a pyqtgraph.GraphicsScene.mouseEvents.MouseClickEvent
pos = mouseClickEvent.pos()
if self.plot_widget.graphWidget.sceneBoundingRect().contains(pos):
mousePoint = self.plot_widget.graphWidget.plotItem.vb.mapSceneToView(pos)
# print("x: {}, y {}".format(mousePoint.x(), mousePoint.y()))
yidx = np.floor((mousePoint.y() + 1/np.sqrt(3)) / (np.sqrt(3) / 2))
xidx = np.floor(mousePoint.x() + 0.5 - (yidx % 2) * 0.5)
# print("x: {}, y {}".format(xidx, yidx))
if 0 <= xidx <= self.gdh.X and 0 <= yidx <= self.gdh.Y:
self.x_value = xidx
self.y_value = yidx
self.hex_value = yidx * self.gdh.X + xidx
self.currentIdx = self.plot_widget.turnSlider.sliderPosition()
language = self.comboBox.currentText()
owner = self.gdh.getOwner(self.currentIdx - 1, int(xidx), int(yidx), language)
self.owner = owner
self.debug_signal.emit()
def main():
app = QtWidgets.QApplication(sys.argv)
main = MainWindow(app, run_arg_parser())
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()