forked from alcaras/civ5benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.py
470 lines (346 loc) · 12.8 KB
/
bench.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
import os
import sys
import struct
import ctypes
import uuid
import optparse
import codecs
import signal
class Civ5FileReader(object):
""" Some basic functionality for reading data from Civ 5 files. """
def read_map(self):
lookup = {
0 : { # 4 of these
'\x00' : '0',
'\x01' : '1',
'\x02' : '2',
'\x03' : ' ',
},
1 : { # 7 of these
'\x00' : '0',
'\x01' : '1',
'\x02' : '2',
'\x03' : '3',
'\x04' : '4',
'\x05' : '5',
'\x06' : ' ',
},
2 : { # 25 of these
'\n' : 'n',
'\r' : 'r',
'\t' : 't',
'\x00' : '0',
'\x01' : '1',
'\x02' : '2',
'\x03' : '3',
'\x04' : '4',
'\x05' : '5',
'\x06' : '6',
'\x07' : '7',
'\x08' : '8',
'\x0b' : 'b',
'\x0c' : 'c',
'\x0f' : 'f',
'\x10' : ')',
'\x11' : '!',
'\x12' : '@',
'\x13' : '#',
'\x14' : '$',
'\x15' : '%',
'\x16' : '^',
'\x17' : '&',
'\x18' : '*',
'\xff' : ' ',
},
3 : { # 11 of these
'\n' : 'n',
'\x00' : ' ',
'\x01' : '1',
'\x02' : '2',
'\x03' : '3',
'\x04' : '4',
'\x05' : '5',
'\x06' : '6',
'\x08' : '8',
'\x0c' : 'c',
'\x0e' : 'e',
},
}
unknowns = []
print "now reading map..."
x = self.read_int()
y = self.read_int()
hexes = self.read_int()
print "map dimensions", x, y, "hexes:", hexes
assert(x*y==hexes)
map = {}
for i in range(0, x):
map[i] = {}
for i in range(0, hexes):
a = self.read_int() # not sure what these represent yet
b = self.read_int() # not sure what these represent yet
c = self.read_int() # not sure what these represent yet
map[i/y][i%y] = c
# a seems to always be 1
# b seems to always be the number of turns
# if i had to guess, c is terrain + resources
print a, b, c,
data=ctypes.create_string_buffer(4)
struct.pack_into("<i", data, 0, c)
print list(data)
for k in range(0, 4):
print "data", k, "map"
print
unknowns = []
for i in range(0, y):
for j in range(0, x):
data=ctypes.create_string_buffer(4)
struct.pack_into("<i", data, 0, map[j][i])
if data[k] in lookup[k]:
sys.stdout.write(lookup[k][data[k]])
else:
sys.stdout.write('?')
if data[k] not in unknowns:
unknowns += data[k]
sys.stdout.write('\n')
print unknowns
print
return map
def read_header(self):
print "now reading header..."
print self.read_sized_string_list(4)[0] # CIV5
print self.read_int(), "1?" # 1, not sure what it means, seems consistent
print self.read_string() # game version
print self.read_string() # build
self.r.read(5) # 5 bytes, not sure of what
our_civ = self.read_string() # our civ
print our_civ, "<-- our civ"
print self.read_string() # Difficulty Level
print self.read_string() # Starting Era
print self.read_string() # Ending? Era
print self.read_string() # Game Speed
print self.read_string() # World Size
print self.read_string() # Map Script
n_dlcs = self.read_int() # a number, looks to be 10? maybe # of DLCs?
print n_dlcs, "DLCS loaded:"
# look for a 01 00 00 00, this is the marker that starts DLCs
# looks like we're here for DLC's...
for i in range(0, n_dlcs):
while self.read_int() != 1:
continue
print " ", self.read_string()
n_mods = self.read_int() # always 2, not sure what it means (mods?)
print n_mods, "mods loaded:"
for i in range(0, n_mods):
print " ", self.read_string() #check sum
print " ", self.read_int()
print " ", self.read_string()
print self.read_int(), "0?"
print self.read_int(), "0?" # should be 2 zeros
print self.read_string()
print self.read_int(), "1?"
print self.read_int(), "0?" # should be a 1 and a 0
print self.read_string() # map script again
print self.read_int(), "3? game options" # these are almost certainly game options
print self.read_int(), "0?"
print self.read_int(), "1?"
print self.read_int(), "0?"
print self.read_int(), "3?"
print self.read_int(), "1?"
print self.read_int(), "" # fixing alignment
print self.read_int(), "" # fixing alignment
print self.read_int(), "15?"
print self.read_int(), "5?"
print self.read_int(), "0"
print self.read_int(), "1"
print self.read_int(), "2"
print self.read_int(), "3"
print self.read_int(), "4"
# -1 loss
# 0 time
# 1 science
# 2 diplo
# 3 culture
# 4 military
victory_types = { -1 : "Loss",
0 : "Time",
1 : "Science",
2 : "Diplomatic",
3 : "Cultural",
4 : "Military" }
vt= self.read_int()
assert(vt in (-1, 0, 1, 2, 3, 4))
print "Victory Type:", victory_types[vt], vt
print self.read_int(), "0"
self.r.read(9) # 9 bytes for what i do not know
print self.read_string(), "end date"
print self.read_int(), "0"
n_events = self.read_int()
print n_events, "?"
entities = self.read_int()
print entities, "number of entities"
print self.read_int(),
print self.read_int(),
print self.read_int(),
print self.read_int(),
our_leader = self.read_string()
# pass this on to mark
ld = open('our_leader.py', 'w')
ld.write("our_leader = '" + our_leader + "'")
ld.close()
print "***" + our_leader + "***",
print self.read_string(), self.read_string(), self.read_string()
for i in range(1, entities):
print self.read_int(), self.read_int(), self.read_int(), self.read_int(),
print self.read_string(), self.read_string(), self.read_string(), self.read_string()
# oh score, histogram data!
data_sets = self.read_int()
print data_sets, "data sets?"
for i in range(0, data_sets):
print self.read_string()
n_ent = self.read_int()
print n_ent, "civs"
for i in range(0, n_ent):
print
print "histograms from civ #", i
n_data = self.read_int()
print n_data, "<-- n_data"
for j in range(0, n_data):
n_turns = self.read_int()
print n_turns, "<-- turns", j
for k in range(0, n_turns):
print self.read_int(),
print self.read_int()
print
print self.read_int(), "2?"
n_events = self.read_int()
print "n_events = ", n_events
print "now at", self.r.tell()
return self.r.tell(), n_events
# the first bit
def turn_event(self):
# event type seems to be one of the following values:
# 0 general information
# 1 city founded
# 2 culture gained
# 5 religion founded
# 6 pantheon founded
super_type_definitions = {0 : "Announcement",
1 : "City Founding",
2 : "Borders", # not sure on the difference
3 : "Borders", # between these
4 : "Borders",
5 : "Religion Founded",
6 : "Pantheon Founded",
}
turn = self.read_int()
print "Turn", str(turn)
super_type= self.read_int()
print "super_type", super_type,
if super_type in super_type_definitions:
print super_type_definitions[super_type]
elif super_type == 56:
print "End of parse"
sys.exit()
else:
print "Unknown super_type @", self.r.tell()
sys.exit()
count = self.read_int() # number of tiles at play here
print "count", count
for i in range(0, count): # each tile is a short
print i, self.read_short_int(), self.read_short_int()
team = self.read_int() # which team is this?
print "Team", str(team).rjust(3)
text = self.read_string() # any message?
print text.encode('utf-8')
if text != "": # ignore messages with no content
print
print "^^^" + str(turn) + "\t" + str(team) + "\t" + text.encode('utf-8')
print
def __init__(self, input):
if isinstance(input, str):
input = file(input, "rb")
self.r = input
def read_short_int(self):
""" Read a single little endian 2 byte integer """
# My *guess* is that they're all signed
t = self.r.read(2)
if len(t) != 2:
self.eof = True
return 0
return struct.unpack("<h", t)[0]
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 """
len = self.read_int()
# print "(read",len, "@", self.r.tell() , ")"
return self.r.read(len).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")
if __name__ == "__main__":
# set up some command line options
op = optparse.OptionParser()
(options, args) = op.parse_args()
if len(args) == 0:
print "bench.py <replay-file>"
sys.exit(1)
replay = None
if len(args) > 0:
print "Replaying: %s" % (args[0],)
print
fn = args[0]
r = file(fn, "rb")
all = r.read()
cf = Civ5FileReader(fn)
cf.r.seek(0,0)
data_starts_at, n_events = cf.read_header()
cf.r.seek(data_starts_at,0)
print "now reading", n_events, "events..."
for i in range(n_events):
cf.turn_event()
print "left off", cf.r.tell()
# now we read the map
cf.read_map()
print "left off", cf.r.tell()