forked from alcaras/civ5benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathciv5replay.py
1836 lines (1659 loc) · 59.3 KB
/
civ5replay.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
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding:utf-8 -*-
#
# Read Civilization 5 Replay files
# Written by Daniel Fischer (dannythefool), [email protected]
#
# Contributions:
# * Collin Grady: Babylon colours, <html> header, replay files
# * BotYann: French translation, replay files
# * player1 fanatic: Usability suggestions
# * blind biker: Usability suggestions
# Forum names are on CivFanatics unless otherwise noted.
#
# Please note that this is based on reverse engineered serialiized data.
# If it doesn't work for you, please send me the replay file.
#
# This script can be used standalone or as a module.
#
# To run it standalone, just pass the path to a replay file as the only
# argument on the command line, it'll output a lot of text that's really
# inconvenient to read etc. etc.
#
# Run with -h to see available options (HTML export, etc.)
#
import os
import sys
import struct
import uuid
import optparse
import codecs
import signal
# safety belt, comment in if you want a one minute timeout on a
# web server that runs Linux
# signal.alarm(60)
# don't run in debug mode by default
debug = False
# cheap "I forgot about localization again even though English isn't even my native language" hack
locale = "en"
class L(object):
""" A localized string """
def __init__(self, en, **kwargs):
kwargs["en"] = en
for k,v in kwargs.items():
setattr(self, k, v.decode("utf-8"))
self.s = self.__str__
self.__repr__ = self.__str__
def __str__(self):
return self.__dict__.get(locale, self.en)
def __mod__(self, x):
return self.s() % x
def __eq__(self, x):
return self.s() == x
def p(*s):
""" Helper function replacing print for utf-8 output"""
for e in s:
if not isinstance(e, unicode):
e = unicode(e)
sys.stdout.write(e.encode("utf-8"))
sys.stdout.write(" ")
sys.stdout.write("\n")
# difficulty level names
difficulty_strings = [
L("Settler" , fr="Colon"),
L("Chieftain", fr="Chef tribal"),
L("Warlord", fr="Seigneur de Guerre"),
L("Prince", fr="Prince"),
L("King", fr="Roi"),
L("Emperor", fr="Empereur"),
L("Immortal", fr="Immortel"),
L("Deity", fr="Divinité"),
]
# map sizes
# this time, taken from the earth maps...
map_sizes = [
[ L("Duel", fr="Duel"), 40, 24 ],
[ L("Tiny", fr="Minuscule"), 56, 36 ],
[ L("Small", fr="Petite"), 66, 42 ],
[ L("Standard",fr="Normale"), 80, 52 ],
[ L("Large", fr="Grande"), 104, 64 ],
[ L("Huge", fr="Immense"), 128, 80 ],
]
# victory types
victory_types = {
-1: L("Loss", fr="Perdu"),
0: L("Time", fr="Temps"),
1: L("Science", fr="Scientifique"),
2: L("Domination", fr="Militaire"),
3: L("Culture", fr="Culturelle"),
4: L("Diplomatic", fr="Diplomatique"),
}
# advanced game options
game_options = {
0: L("No City Razing", fr="Impossible de raser les villes"),
1: L("No Barbarians", fr="Aucun barbare"),
2: L("Raging Barbarians", fr="Barbares déchaînés"),
3: L("Always war", ),
4: L("Always peace", ),
5: L("One-City Challenge", fr="Ville unique"),
6: L("No Changing War Peace", ),
7: L("New Random Seed", fr="Nouvelles valeurs aléatoires"),
8: L("Lock Mods", ),
9: L("Complete Kills", fr="Destruction totale"),
10: L("No Ancient Ruins", fr="Pas de ruines antiques"),
11: L("Random Personalities", fr="Personnalités aléatoires"),
12: L("Enable Turn Timer", fr="Active le chrono. tour"),
13: L("Quick Combat", fr="Combat rapide"),
14: L("Disable Start Bias", fr="Désactiver les préférences de départ"),
}
option_noraze = 0;
option_occ = 5;
# the colour to use for city state owned tiles
citystate_color = ["#dddddd", "black"]
# civs we recognize
# replay files don't actually contain the name of the civ,
# so we guess from the first city's name...
# the player civ is an exception.
civs = [
# Standard civs
[ L("American Empire", fr="Empire américain"), L("Washington"), "#ffffff", "#1f3378" ],
[ L("Arabian Empire", fr="Empire arabe"), L("Mecca", fr="La Mecque"), "#92dd09", "#2b572d" ],
[ L("Aztec Empire", fr="Empire aztèque"), L("Tenochtitlan"), "#88eed4", "#a13922" ],
[ L("Chinese Empire", fr="Empire chinois"), L("Beijing", fr="Pékin"), "#ffffff", "#009452" ],
[ L("Egyptian Empire", fr="Empire égyptien"), L("Thebes", fr="Thèbes" ), "#5200d0", "#fffb03" ],
[ L("English Empire", fr="Empire anglais"), L("London", fr="Londres"), "#ffffff", "#6c0200" ],
[ L("French Empire", fr="Empire français"), L("Paris"), "#ebeb8a", "#418dfd" ],
[ L("German Empire", fr="Empire allemand"), L("Berlin"), "#242b20", "#b3b1b8" ],
[ L("Greek Empire", fr="Empire grec"), L("Athens", fr="Athènes"), "#418dfd", "#ffffff" ],
[ L("Indian Empire", fr="Empire indien"), L("Delhi"), "#ff9931", "#128706" ],
[ L("Iroquois Empire", fr="Empire iroquois"), L("Onondaga"), "#fbc981", "#415656" ],
[ L("Japanese Empire", fr="Empire japonais"), L("Kyoto"), "#b80000", "#ffffff" ],
[ L("Ottoman Empire", fr="Empire ottoman"), L("Istanbul"), "#12521e", "#f7f8c7" ],
[ L("Persian Empire", fr="Empire perse"), L("Persepolis", fr="Persépolis"), "#f5e637", "#b00703" ],
[ L("Roman Empire", fr="Empire romain"), L("Rome"), "#efc600", "#460076" ],
[ L("Russian Empire", fr="Empire russe"), L("Moscow", fr="Moscou"), "#000000", "#eeb400" ],
[ L("Siamese Empire", fr="Empire siamois"), L("Sukhothai", fr="Sukhothaï"), "#b00703", "#f5e637" ],
[ L("Songhai Empire", fr="Empire songhaï"), L("Gao", fr="Gao"), "#5a0009", "#d59113" ],
# DLC civs
[ L("Babylonian Empire",fr="Empire babylonien"),L("Babylon", fr="Babylone"), "#c9f8ff", "#2b5162" ],
]
# colours for map features
map_colors = {
"TERRAIN_GRASS": "#b2d578",
"TERRAIN_PLAINS": "#e7c779",
"TERRAIN_DESERT": "#eedda3",
"TERRAIN_TUNDRA": "#bacac9",
"TERRAIN_SNOW": "#eaeaea",
"TERRAIN_COAST": "#7bbbde",
"TERRAIN_OCEAN": "#7cadc8",
"TERRAIN_MOUNTAIN": "#575757",
"TERRAIN_HILL": "#000000",
}
# Characters to be replaced before putting otherwise unsanitized text in HTML
html_escape = {
"&": "&",
"<": "<",
">": ">",
}
#
# HTML code to describe the map area and the event list.
# Instance variables of the Civ5Replay object are available
# via %(variable name)s as per normal python string formatting
# rules. The "id" variable is a reasonably unique identifier,
# so that multiple HTML replays can be embedded in the same
# web page.
#
# Note that % characters have to be escaped as %%.
#
html_header = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
%(leader_name)s
%(l_of_the)s%(civ_name)s
(%(difficulty)s, %(map_name)s, %(map_size)s, %(final_turn)s %(l_turns_played)s)
</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
"""
html_footer = """
</body>
</html>
"""
html_skeleton = """
<style type="text/css"><!--
div#%(id)s_main {
display: table;
border: 1px solid #333333;
}
div#%(id)s_gameinfo {
background: #333333;
color: white;
text-align: center;
padding: 4px;
}
div#%(id)s_canvas {
position: absolute;
z-index: 0;
}
div#%(id)s_overlay {
position: absolute;
z-index: 10;
padding: 4px;
color: #555555;
font-size: 80%%;
}
div#%(id)s_signs {
position: absolute;
z-index: 1;
}
div.%(id)s_sign {
position: absolute;
opacity: 0.5;
white-space: nowrap;
font-size: 60%%;
font-family: Helvetica;
font-stretch: condensed;
padding: 2px;
z-order: 3;
}
div.%(id)s_sign_text {
position: absolute;
white-space: nowrap;
font-size: 60%%;
font-family: Helvetica;
font-stretch: condensed;
padding: 2px;
background: transparent;
z-order: 5;
}
div#%(id)s_controls {
background: #666655;
color: white;
padding: 4px;
font-size: 80%%;
}
div#%(id)s_log {
background: #333333;
color: white;
height: 300px;
overflow-y: scroll;
}
a.%(id)s_button {
background: #333333;
border: 1px solid black;
padding-left: 10px;
padding-right: 10px;
cursor: pointer;
}
a.%(id)s_button:active {
background: #555555;
}
span#%(id)s_turncounter {
float: right;
}
td.%(id)s_base_turn {
font-size: 80%%;
padding-left: 0px;
padding-top: 0px;
padding-bottom: 0px;
padding-right: 20px;
}
td.%(id)s_base_text {
font-size: 80%%;
padding: 0px;
}
td.%(id)s_event_turn {
color: #aaaaaa;
}
td.%(id)s_event_text {
color: #dddddd;
}
span#%(id)s_title {
font-size: 150%%;
}
span#%(id)s_subtitle {
font-size: 80%%;
}
span.%(id)s_disabled_option {
text-decoration: line-through;
}
--></style>
</head>
<body>
<div id="%(id)s_main">
<div id="%(id)s_gameinfo">
<span id="%(id)s_title">
%(leader_name)s
%(l_of_the)s%(civ_name)s
</span><br/>
<span id="%(id)s_subtitle">
%(difficulty)s | %(map_name)s | %(map_size)s |%(options_pipe)s %(final_turn)s %(l_turns_played)s
<br/>
%(l_In)s %(final_year)s, %(victory_text)s
</span>
</div>
<div id="%(id)s_map">
<div id="%(id)s_overlay">If you see this, something didn't work.</div>
<div id="%(id)s_signs"></div>
<canvas id="%(id)s_canvas" width="%(html_w)d" height="%(html_h)d"></canvas>
</div>
<div id="%(id)s_controls" onselectstart="return false">
<a class="%(id)s_button" onclick="%(id)s_restart_animation()" onselectstart="return false">|<</a>
<a class="%(id)s_button" onclick="%(id)s_frame(-10)" onselectstart="return false"><<</a>
<a class="%(id)s_button" onclick="%(id)s_frame(-1)" onselectstart="return false"><</a>
<a class="%(id)s_button" onclick="%(id)s_toggle_animation()" onselectstart="return false">■</a>
<a class="%(id)s_button" onclick="%(id)s_frame(1)" onselectstart="return false">></a>
<a class="%(id)s_button" onclick="%(id)s_frame(10)" onselectstart="return false">>></a>
<a class="%(id)s_button" onclick="%(id)s_frame(99999)" onselectstart="return false">>|</a>
<a class="%(id)s_button" onclick="%(id)s_toggle_alpha()" onselectstart="return false">α</a>
<a class="%(id)s_button" onclick="%(id)s_show_histogram()" onselectstart="return false">score</a>
<span id="%(id)s_turncounter">error</span>
</div>
<div id="%(id)s_log">
%(html_event_list)s
</div>
</div>
"""
#
# All the static javascript code used to animate the map.
#
html_javascript = """
<script type="text/javascript"><!--
var %(id)s_c;
var %(id)s_last_turn_drawn = -1;
var %(id)s_refresh = 0;
var %(id)s_max_turn = %(final_turn)d;
var %(id)s_events = %(javascript_event_list)s;
var %(id)s_turn_to_event = %(javascript_turn_to_event)s;
var %(id)s_background = %(javascript_background)s;
var %(id)s_domain = [];
var %(id)s_civs = %(javascript_civs)s;
var %(id)s_histogram_w = %(histogram_w)d;
var %(id)s_histogram_h = %(histogram_h)d;
var %(id)s_histogram_scale_w = %(histogram_scale_w)f;
var %(id)s_histogram_scale_h = %(histogram_scale_h)f;
var %(id)s_histogram = %(javascript_histogram_score)s;
var %(id)s_timeout = null;
var %(id)s_border_alpha = 0.2;
// Set up the canvas and the other HTML areas
function %(id)s_setup() {
var canvas = document.getElementById('%(id)s_canvas');
if(canvas.getContext) {
canvas = canvas.getContext("2d");
%(id)s_c = canvas;
}
if(%(id)s_background.length <= 0) {
%(id)s_border_alpha = 1.0;
}
%(id)s_render_turn(%(start_turn)d);
%(id)s_advance_turn();
}
// Toggle alpha
function %(id)s_toggle_alpha() {
var ba = %(id)s_border_alpha;
if(ba < 0.5) {
ba = 1.0;
} else if(ba < 0.9) {
ba = 0.2;
} else {
ba = 0.6;
}
%(id)s_border_alpha = ba;
%(id)s_refresh = 1;
if(%(id)s_timeout === null) {
%(id)s_stop_animation();
}
}
// Stop the animation
function %(id)s_stop_animation() {
var turn = %(id)s_last_turn_drawn;
%(id)s_render_turn(turn);
if(%(id)s_timeout === null) return;
clearTimeout(%(id)s_timeout);
%(id)s_timeout = null;
}
// Toggle playback
function %(id)s_toggle_animation() {
if(%(id)s_timeout === null) %(id)s_advance_turn();
else %(id)s_stop_animation();
}
// Switch to histogram
function %(id)s_show_histogram() {
%(id)s_stop_animation();
document.getElementById("%(id)s_signs").style.display = "none";
%(id)s_draw_histogram(%(id)s_histogram, %(id)s_histogram_scale_w, %(id)s_histogram_scale_h);
%(id)s_refresh = 1;
}
// Restart the animation from turn 0
function %(id)s_restart_animation() {
%(id)s_last_turn_drawn = -1;
%(id)s_advance_turn();
}
// Advance x frames
function %(id)s_frame(x) {
x = %(id)s_last_turn_drawn + x;
if(x < %(start_turn)d) x = %(start_turn)d;
if(x > %(id)s_max_turn) x = %(id)s_max_turn;
%(id)s_refresh = 1;
%(id)s_render_turn(x);
}
// Draw the next turn and set up a timer to continue the animation
function %(id)s_advance_turn() {
if(%(id)s_timeout !== null) {
clearTimeout(%(id)s_timeout);
%(id)s_timeout = null;
}
turn = %(id)s_last_turn_drawn + 1;
if(turn == 0) turn = %(start_turn)d;
%(id)s_render_turn(turn);
if(turn >= %(id)s_max_turn) {
return;
}
%(id)s_timeout = setTimeout(%(id)s_advance_turn, 100);
}
// Find a neighbour sharing a given border with a hex tile
function %(id)s_neighbour(x, y, b) {
var x2 = x;
var y2 = y;
var xoff = 1-(y%%2);
switch(b) {
case 0:
x2 = x+xoff;
y2 = y-1;
break;
case 1:
x2 = x+1;
y2 = y;
break;
case 2:
x2 = x+xoff;
y2 = y+1;
break;
case 3:
x2 = x+xoff-1;
y2 = y+1;
break;
case 4:
x2 = x-1;
y2 = y;
break;
case 5:
x2 = x+xoff-1;
y2 = y-1;
break;
}
return [x2, y2];
}
// Determine if a border between tiles is internal to the empire
function %(id)s_is_inside(x, y, b) {
var pos = %(id)s_neighbour(x, y, b);
x2 = pos[0]; y2 = pos[1];
if(x2 == x && y2 == y) return 1;
if(x2 < 0 || y2 < 0 || x2 >= %(w)d || y2 >= %(h)d) return 0;
d1 = %(id)s_domain[y][x];
d2 = %(id)s_domain[y2][x2];
if(!d1 || !d2) return 0;
if(d1[0] == d2[0] && d1[1] == d2[1]) return 1;
return 0;
}
// Remove all signs
function %(id)s_reset_signs() {
var parent = document.getElementById("%(id)s_signs");
while(parent.childNodes.length >= 1) {
parent.removeChild(parent.firstChild);
}
}
// Put up a signpost
function %(id)s_set_sign(x, y, bg, fg, text) {
var parent = document.getElementById("%(id)s_signs");
parent.style.display = "block";
var id = "%(id)s_"+x+"_"+y;
var sign = document.getElementById(id);
if(sign) parent.removeChild(sign);
var stext = document.getElementById(id+"_text");
if(stext) parent.removeChild(stext);
if(text == "") return;
x = x * 1.0;
y = y * 1.0;
if((%(h)d-y)%%2 == 0) {
x += 0.5;
}
s = %(tile_size)f;
x = s * (x + 1.5);
y = (2*s/3) * (y + 0.5);
sign = document.createElement("div");
sign.setAttribute("id", id);
sign.setAttribute("class", "%(id)s_sign");
sign.setAttribute("style", "left: "+x+"px; top: "+y+"px; background: "+bg+"; color: "+fg+";");
sign.innerHTML = text;
parent.appendChild(sign);
stext = document.createElement("div");
stext.setAttribute("id", id+"_text");
stext.setAttribute("class", "%(id)s_sign_text");
stext.setAttribute("style", "left: "+x+"px; top: "+y+"px; color: "+fg+";");
stext.innerHTML = text;
parent.appendChild(stext);
}
// Draw the map as of a given turn
function %(id)s_render_turn(turn) {
if(turn < 0) {
turn = 0;
}
if(%(id)s_refresh) {
%(id)s_refresh = 0;
%(id)s_last_turn_drawn = -1;
}
var txt = [];
if(%(id)s_last_turn_drawn < 0 || %(id)s_last_turn_drawn > turn) {
%(id)s_reset_signs();
%(id)s_c.fillStyle = "rgb(220,220,180)";
%(id)s_c.fillRect(0, 0, %(html_w)d, %(html_h)d);
if(%(id)s_background.length > 0) {
for(var y=0; y<%(h)d; ++y) {
if(y >= %(id)s_background.length) break;
for(var x=0; x<%(w)d; ++x) {
if(x >= %(id)s_background[y].length) break;
bg = %(id)s_background[y][x][0];
hf = %(id)s_background[y][x][1];
rf = %(id)s_background[y][x][2];
%(id)s_draw_hex_tile(x, y, bg, "", 0, hf, rf);
}
}
}
%(id)s_domain = Array(%(h)d);
for(var y=0; y<=%(h)d; ++y) {
%(id)s_domain[y] = Array(%(w)d+1);
}
}
var start = 0;
if(%(id)s_last_turn_drawn < turn && %(id)s_last_turn_drawn >= 0) {
start = %(id)s_turn_to_event[turn];
}
var c = %(id)s_c;
tiles = {};
for(var i=start; i<%(id)s_events.length; ++i) {
evt = %(id)s_events[i];
if(evt[0] > turn) {
break;
}
if(evt[0] == turn && evt[5] != "") {
txt.push(evt[5]);
}
if(evt[3] != "") {
var x = evt[1];
var y = evt[2];
dom = %(id)s_domain[y][x]
if(dom) {
if(evt[6] == 1 && evt[7] == "") {
evt[7] = dom[3];
}
}
%(id)s_domain[y][x] = [evt[3], evt[4], evt[6], evt[7]];
tiles[""+x+","+y] = [x, y];
for(var j=0; j<6; ++j) {
var pos = %(id)s_neighbour(x, y, j);
if(pos[0] < 0 || pos[1] < 0 || pos[0] >= %(w)d || pos[1] >= %(h)d) continue;
tiles[""+pos[0]+","+pos[1]] = pos;
}
}
}
for(var i in tiles) {
t = tiles[i];
x = t[0];
y = t[1];
d = %(id)s_domain[y][x];
if(%(id)s_background.length > 0) {
%(id)s_draw_hex_tile(x, y, %(id)s_background[y][x][0], "", 0, %(id)s_background[y][x][1], %(id)s_background[y][x][2]);
} else {
%(id)s_draw_hex_tile(x, y, "rgb(220,220,180)", "", 0, 0, 0);
}
if(!d) continue;
if(%(id)s_background.length <= 0) {
%(id)s_draw_hex_tile(x, y, d[0], "", 0, 0, 0, %(id)s_border_alpha);
} else {
%(id)s_draw_hex_tile(x, y, d[0], "", 0, 0, 0, %(id)s_border_alpha);
}
borders = [];
for(var j=0; j<6; ++j) {
if(%(id)s_is_inside(x, y, j)) continue;
borders.push([x,y,j]);
}
if(borders.length > 0) {
for(var j=0; j<borders.length; ++j) {
%(id)s_draw_hex_border(borders[j][0], borders[j][1], d[0], borders[j][2], 0);
}
for(var j=0; j<borders.length; ++j) {
%(id)s_draw_hex_border(borders[j][0], borders[j][1], d[1], borders[j][2], 1);
}
}
if(d[2] <= 0) {
%(id)s_set_sign(x, y, "", "", "");
} else if(d[2] == 1) {
if(d[3] != "") %(id)s_set_sign(x, y, d[0], d[1], d[3]);
%(id)s_draw_city(x, y, d[0], d[1]);
}
}
html = "";
for(var t in txt) {
t = txt[t];
html += t + "<br/>";
}
overlay = document.getElementById("%(id)s_overlay");
overlay.style.display = "block";
if(html != "") {
html = "%(l_Turn)s " + turn + "<br/>" + html;
overlay.innerHTML = html;
}
turncounter = document.getElementById("%(id)s_turncounter");
if(turn > %(id)s_max_turn) {
turncounter.innerHTML = %(id)s_max_turn;
} else {
turncounter.innerHTML = turn;
}
%(id)s_last_turn_drawn = turn;
}
// Draw a city symbol
function %(id)s_draw_city(x, y, bg, fg) {
var c = %(id)s_c;
x = x * 1.0;
y = y * 1.0;
if((%(h)d-y)%%2 == 0) {
x += 0.5;
}
s = %(tile_size)f;
x = s * (x + 0.5);
y = (2*s/3) * (y + 0.5);
c.beginPath();
c.arc(x+s/2, y+s/2, s/4, 0, Math.PI*2, true);
c.fillStyle = bg;
c.fill();
c.beginPath();
c.arc(x+s/2, y+s/2, s/6, 0, Math.PI*2, true);
c.fillStyle = fg;
c.fill();
}
// Draw a single border
function %(id)s_draw_hex_border(x, y, col, b, outer) {
var c = %(id)s_c;
x = x * 1.0;
y = y * 1.0;
if((%(h)d-y)%%2 == 0) {
x += 0.5;
}
s = %(tile_size)f;
x = s * (x + 0.5);
y = (2*s/3) * (y + 0.5);
switch(b) {
case 0:
xo1 = x+s/2;
yo1 = y;
xo2 = x+s;
yo2 = y+s/3;
xi1 = xo1-s/8;
yi1 = yo1+s/12;
xi2 = xo2;
yi2 = yo2+s/6;
break;
case 1:
xo1 = x+s;
yo1 = y+s/3;
xo2 = x+s;
yo2 = y+2*s/3;
xi1 = xo1-s/9;
yi1 = yo1-s/12;
xi2 = xo2-s/9;
yi2 = yo2+s/12;
break;
case 2:
xo1 = x+s;
yo1 = y+2*s/3;
xo2 = x+s/2;
yo2 = y+s;
xi1 = xo1;
yi1 = yo1-s/6;
xi2 = xo2-s/8;
yi2 = yo2-s/12;
break;
case 3:
xo1 = x;
yo1 = y+2*s/3;
xo2 = x+s/2;
yo2 = y+s;
xi1 = xo1;
yi1 = yo1-s/6;
xi2 = xo2+s/8;
yi2 = yo2-s/12;
break;
case 4:
xo1 = x;
yo1 = y+s/3;
xo2 = x;
yo2 = y+2*s/3;
xi1 = xo1+s/9;
yi1 = yo1-s/12;
xi2 = xo2+s/9;
yi2 = yo2+s/12;
break;
case 5:
xo1 = x+s/2;
yo1 = y;
xo2 = x;
yo2 = y+s/3;
xi1 = xo1+s/8;
yi1 = yo1+s/12;
xi2 = xo2;
yi2 = yo2+s/6;
break;
default:
return;
}
xm1 = (xo1+xi1)/2;
ym1 = (yo1+yi1)/2;
xm2 = (xo2+xi2)/2;
ym2 = (yo2+yi2)/2;
c.fillStyle = col;
c.beginPath();
if(outer == 0) {
c.moveTo(xo1, yo1);
c.lineTo(xo2, yo2);
c.lineTo(xi2, yi2);
c.lineTo(xi1, yi1);
} else {
c.moveTo(xo1, yo1);
c.lineTo(xo2, yo2);
c.lineTo(xm2, ym2);
c.lineTo(xm1, ym1);
}
c.fill();
}
// Draw a single hex tile
// tx, ty - position
// bg, fg - background and foreground colour if applicable
// et - marker flag, 1 = city dot
// hf - hill flag, 1 = hill, 2 = mountain, -1 = ice
// rf - river bitfield, 1 = right, 2 = bottom right, 4 = bottom left
// bgalpha, fgalpha - background and foreground alpha values
function %(id)s_draw_hex_tile(tx, ty, bg, fg, et, hf, rf, bgalpha, fgalpha) {
if(typeof(bgalpha) == 'undefined') bgalpha = 1.0;
if(typeof(fgalpha) == 'undefined') fgalpha = 1.0;
var c = %(id)s_c;
c.save();
c.lineWidth = 2;
var x = tx * 1.0;
var y = ty * 1.0;
if((%(h)d-y)%%2 == 0) {
x += 0.5;
}
s = %(tile_size)f;
x = s * (x + 0.5);
y = (2*s/3) * (y + 0.5);
c.fillStyle = bg;
c.beginPath();
c.moveTo(x+s/2, y);
c.lineTo(x+s, y+s/3);
c.lineTo(x+s, y+2*s/3);
c.lineTo(x+s/2, y+s);
c.lineTo(x, y+2*s/3);
c.lineTo(x, y+s/3);
c.lineTo(x+s/2, y);
c.globalAlpha = bgalpha;
c.fill();
c.globalAlpha = fgalpha;
if(fg != "") {
c.strokeStyle = fg;
c.stroke();
}
if(et == 1) {
c.fillStyle = fg;
c.beginPath();
c.arc(x+s/2, y+s/2, s/4, 0, Math.PI*2, true);
c.fill();
}
if(hf == -1) { // ice
c.strokeStyle = "#ffffff";
c.globalAlpha = 0.7;
c.beginPath();
c.moveTo(x+2*s/6, y+3*s/5);
c.lineTo(x+4*s/6, y+3*s/5);
c.moveTo(x+3*s/6, y+2*s/5);
c.lineTo(x+5*s/6, y+2*s/5);
c.stroke();
} else if(hf == 1) { // hill
c.strokeStyle = "#000000";
c.globalAlpha = 0.1;
c.beginPath();
c.moveTo(x+s/7, y+8*s/12);
c.lineTo(x+2*s/7, y+6*s/12);
c.lineTo(x+3*s/7, y+8*s/12);
c.moveTo(x+4*s/7, y+7*s/12);
c.lineTo(x+5*s/7, y+5*s/12);
c.lineTo(x+6*s/7, y+7*s/12);
c.stroke()
} else if(hf == 2) { // mountain
c.strokeStyle = "#000000";
c.globalAlpha = 0.3;
c.beginPath();
c.moveTo(x+s/7, y+5*s/7);
c.lineTo(x+2*s/7, y+3*s/7);
c.lineTo(x+3*s/7, y+5*s/7);
c.moveTo(x+4*s/7, y+4*s/7);
c.lineTo(x+5*s/7, y+2*s/7);
c.lineTo(x+6*s/7, y+4*s/7);
c.stroke()
}
if((rf & 7) != 0) {
c.globalAlpha = 1.0;
if( (rf & 1) == 1) { // river on the right
%(id)s_draw_hex_border(tx, ty, "#7bbdde", 1, 1);
}
if( (rf & 2) == 2) { // river on bottom right
%(id)s_draw_hex_border(tx, ty, "#7bbdde", 2, 1);
}
if( (rf & 4) == 4) { // river on bottom left
%(id)s_draw_hex_border(tx, ty, "#7bbdde", 3, 1);
}
}
c.restore();
}
// Draw a histogram
function %(id)s_draw_histogram(hdata, sx, sy) {
overlay = document.getElementById("%(id)s_overlay");
overlay.style.display = "none";
%(id)s_c.fillStyle = "rgb(220,220,180)";
%(id)s_c.fillRect(0, 0, %(html_w)d, %(html_h)d);
x = 0;
for(var h in hdata) {
h = hdata[h];
y = %(html_h)d; n = 0;
for(var s in h) {
s = h[s];
st = s/9;
for(var tt=0; tt<9; ++tt) {
%(id)s_c.fillStyle = %(id)s_civs[n][2+(tt%%2)];
%(id)s_c.fillRect(x, y-s*sy/9.0, 2*sx, s*sy/9.0);
y -= s*sy/9.0;
}
n += 1;
}
x += sx;
}
}
// Make sure our setup code runs when the page has loaded
if(window.addEventListener) {
addEventListener("load", %(id)s_setup, false);
} else {
attachEvent("onload", %(id)s_setup);
}
--></script>
"""
class Civ5FileReader(object):
""" Some basic functionality for reading data from Civ 5 files. """
def __init__(self, input):
if isinstance(input, str):
input = file(input, "rb")
self.r = input
def read_int(self):
""" Read a single little endian 4 byte integer """
# My *guess* is that they're all signed
t = self.r.read(4)
if len(t) != 4:
self.eof = True
return 0
return struct.unpack("<i", t)[0]
def read_ints(self, count=None, esize=1):
""" Read count tuples of esize little endian 4 byte integers and return them in a list. If count is omitted, read it as a 4 byte integer first """
if count is None:
count = self.read_int()
list = []
while count > 0:
if esize > 1:
t = []
for i in range(esize):
t.append(self.read_int())
list.append(tuple(t))
else:
list.append(self.read_int())
count -= 1
return list
def read_string(self):
""" Read an undelimited string with the length given in the first 4 bytes """
return self.r.read(self.read_int()).decode("utf-8")
def read_terminated_string(self):
""" Read a nul-terminated string. """
s = ""
while True:
c = self.r.read(1)
if ord(c) == 0:
return s.decode("utf-8")
s += c
def read_terminated_string_list(self):
""" Read a list of nul-terminated strings, terminated by a zero-length string. """
l = []
while True:
s = self.read_terminated_string().decode("utf-8")
if s == "":
return l
l.append(s)
def read_sized_string_list(self, size):
""" Read a block of data with a given size, and split in null-terminated strings. """
block = self.r.read(size)
if block.endswith("\0"):
block = block[:-1]
return block.split("\0")
class Civ5Map(Civ5FileReader):
""" Encapsulates a Civ V map, and can load Civ5Map files. """
def __init__(self, input):
Civ5FileReader.__init__(self, input)
# map dimensions
self.w = 0
self.h = 0
self.load_file(self.r)
def load_file(self, f):
""" Loads a Civ5Map file from a stream. """