forked from dculyba/cc_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcc_data.py
More file actions
479 lines (404 loc) · 15.7 KB
/
cc_data.py
File metadata and controls
479 lines (404 loc) · 15.7 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
"""
Data structures for manipulating Chip's Challenge (CC) data
Created for the class Programming for Game Designers
"""
BYTE_ORDER = "little"
class CCField:
"""The base field class
Member vars:
type_val (int): the type identifier of this class (set to 3)
byte_val (bytes): the byte data of the field
"""
def __init__(self, type_val, byte_val):
self.type_val = type
self.byte_val = byte_val
@property
def byte_data(self):
return self.byte_val
def __str__(self):
return_str = " Generic Field (type="+self.type_val+")\n"
return_str += " data = "+str(self.byte_val)
return return_str
@property
def json_data(self):
json_field = {}
json_field["type"] = self.int_type
json_field["value"] = self.byte_val.decode()
return json_field
class CCMapTitleField(CCField):
"""A class defining the map title field
Member vars:
type_val (int): the type identifier of this class (set to 3)
title (string): the title, max length 63 characters
"""
TYPE = 3
def __init__(self, title):
if __debug__:
if len(title) >= 64: raise AssertionError("Map Title must be 63 characters or fewer. Current title is '"+title+"'("+str(len(title))+")")
self.type_val = CCMapTitleField.TYPE
self.title = title
def __str__(self):
return_str = " Map Title Field (type=3)\n"
return_str += " title = '"+str(self.title)+"'"
return return_str
@property
def json_data(self):
json_field = {}
json_field["type"] = self.type_val
json_field["title"] = self.title
return json_field
@property
def byte_data(self):
title_bytes = b""
title_length = len(self.title) + 1
title_bytes += title_length.to_bytes(1, BYTE_ORDER)
title_bytes += self.title.encode("ascii")
title_bytes += b'\x00'
return title_bytes
class CCCoordinate:
"""A class defining a single coordinate
Member vars:
x (int): x position, a value from 0 to 31
y (int): y position, a value from 0 to 31
"""
def __init__(self, x, y):
if __debug__:
if (x<0 or x>31) or (y<0 or y>31):
raise AssertionError("Coordinates: ("+str(x)+", "+str(y)+") out of range. Coordinates must be from 0 to 31")
self.x = x
self.y = y
def __str__(self):
return "("+str(self.x)+", "+str(self.y)+")"
@property
def json_data(self):
return {"x":self.x, "y":self.y}
class CCTrapControl:
"""A class defining a single trap control
Member vars:
button_coord (CCCoordinate): the location of the brown button
trap_coord (CCCoordinate): the location of the trap
"""
def __init__(self, bx, by, tx, ty):
"""Traps are defined by a pairs of coordinates (bx, by, tx, ty)
Note that all coordinates must be from 0 to 31
Args:
bx, by (int, int): the position of the button
tx, ty (int, int): the position of the trap
"""
self.button_coord = CCCoordinate(bx, by)
self.trap_coord = CCCoordinate(tx, ty)
def __str__(self):
return "button"+str(self.button_coord)+", trap"+str(self.trap_coord)
@property
def json_data(self):
json_val = {}
json_val["button_coord"] = self.button_coord.json_data
json_val["trap_coord"] = self.trap_coord.json_data
return json_val
class CCTrapControlsField(CCField):
"""A class defining the trap controls field
Member vars:
type_val (int) : the type identifier of this class (set to 4)
traps (list of CCTrapControl): a list of traps for the map
"""
TYPE = 4
def __init__(self, traps):
"""A Trap Control Field is defined by a list of traps
Note that there is a max of 25 traps per level
Args:
traps (list of CCTrapControl): the traps
"""
if __debug__:
if len(traps) > 25:
raise AssertionError("Max trap count exceeded. Max trap count is 25. Number of traps passed = "+str(len(traps)))
self.type_val = CCTrapControlsField.TYPE
self.traps = traps
def __str__(self):
return_str = " Trap Controls Field (type=4)\n"
for trap in self.traps:
return_str += " trap = "+str(trap)
if trap != self.traps[-1]:
return_str += "\n"
return return_str
@property
def json_data(self):
json_field = {}
json_field["type"] = self.type_val
traps_data = []
for trap in self.traps:
traps_data.append(trap.json_data)
json_field["traps"] = traps_data
return json_field
@property
def byte_data(self):
byte_value = b""
byte_value += (len(self.traps)*10).to_bytes(2, BYTE_ORDER)
for trap in self.traps:
byte_value += trap.button_coord.x.to_bytes(2, BYTE_ORDER)
byte_value += trap.button_coord.y.to_bytes(2, BYTE_ORDER)
byte_value += trap.trap_coord.x.to_bytes(2, BYTE_ORDER)
byte_value += trap.trap_coord.y.to_bytes(2, BYTE_ORDER)
byte_value += b'\x00\x00' #DAT format says to append 0 to the end of the coordinates
return byte_value
class CCCloningMachineControl:
"""A class defining a single cloning machine control
Member vars:
button_coord (CCCoordinate): the location of the red button
machine_coord (CCCoordinate): the location of the cloning machine
"""
def __init__(self, bx, by, tx, ty):
"""Cloning Machines are defined by a pairs of coordinates (bx, by, tx, ty)
Note that all coordinates must be from 0 to 31
Args:
bx, by (int, int): the position of the button
tx, ty (int, int): the position of the machine
"""
self.button_coord = CCCoordinate(bx, by)
self.machine_coord = CCCoordinate(tx, ty)
def __str__(self):
return "button"+str(self.button_coord)+", machine"+str(self.machine_coord)
@property
def json_data(self):
json_val = {}
json_val["button_coord"] = self.button_coord.json_data
json_val["machine_coord"] = self.machine_coord.json_data
return json_val
class CCCloningMachineControlsField(CCField):
"""A class defining the cloning machine controls field
Member vars:
type_val (int) : the type identifier of this class (set to 5)
machine (list of CCCloningMachineControl): a list of cloning machines for the map
"""
TYPE = 5
def __init__(self, machines):
"""A cloning machine control field is defined by a list of machines
Note that there is a max of 31 machines per level
Args:
machines (list of CCCloningMachineControl): the machines
"""
if __debug__:
if len(machines) > 31:
raise AssertionError("Max cloning machine count of 31 exceeded. Number of cloning machines passed = "+str(len(machines)))
self.type_val = CCCloningMachineControlsField.TYPE
self.machines = machines
def __str__(self):
return_str = " Cloning Machine Controls Field (type=5)\n"
for machine in self.machines:
return_str += " machine = "+str(machine)
if machine != self.machines[-1]:
return_str += "\n"
return return_str
@property
def json_data(self):
json_field = {}
json_field["type"] = self.type_val
machine_data = []
for machine in self.machines:
machine_data.append(machine.json_data)
json_field["machines"] = machine_data
return json_field
@property
def byte_data(self):
byte_value = b""
byte_value += (len(self.machines)*8).to_bytes(2, BYTE_ORDER)
for machine in self.machines:
byte_value += machine.button_coord.x.to_bytes(2, BYTE_ORDER)
byte_value += machine.button_coord.y.to_bytes(2, BYTE_ORDER)
byte_value += machine.machine_coord.x.to_bytes(2, BYTE_ORDER)
byte_value += machine.machine_coord.y.to_bytes(2, BYTE_ORDER)
return byte_value
class CCEncodedPasswordField(CCField):
"""A class defining an encoded password
Member vars:
type_val (int): the type identifier of this class (set to 6)
password (list of ints): a password encoded as a list of ints from 4 to 9 ints in length
"""
TYPE = 6
def __init__(self, password):
"""Initializes an encoded password
Args:
password (list of ints) : the integer values of an encoded password
"""
if __debug__:
if len(password) > 9 or len(password) < 4:
raise AssertionError("Encoded password must be from 4 to 9 characters in length. Password passed is '"+str(password)+"'")
self.type_val = CCEncodedPasswordField.TYPE
self.password = password
def __str__(self):
return_str = " Encoded Password Field (type=8)\n"
return_str += " password = "+str(self.password)
return return_str
@property
def json_data(self):
json_field = {}
json_field["type"] = self.type_val
json_field["password"] = self.password
return json_field
@property
def byte_data(self):
password_bytes = b""
for i in self.password:
password_bytes += i.to_bytes(1, BYTE_ORDER)
password_bytes += b'\x00'
return password_bytes
class CCMapHintField(CCField):
"""A class defining a hint
Member vars:
type_val (int): the type identifier of this class (set to 7)
hint (string): the hint for the level max length 127 characters
"""
TYPE = 7
def __init__(self, hint):
if __debug__:
if len(hint) > 127 or len(hint) < 0:
raise AssertionError("Hint must be from 0 to 127 characters in length. Hint passed is '"+hint+"'")
self.type_val = CCMapHintField.TYPE
self.hint = hint
def __str__(self):
return_str = " Map Hint Field (type=7)\n"
return_str += " hint = '"+str(self.hint)+"'"
return return_str
@property
def json_data(self):
json_field = {}
json_field["type"] = self.type_val
json_field["hint"] = self.hint
return json_field
@property
def byte_data(self):
hint_bytes = b""
hint_length = len(self.hint)+1
hint_bytes += hint_length.to_bytes(1, BYTE_ORDER)
hint_bytes += self.hint.encode("ascii")
hint_bytes += b'\x00'
return hint_bytes
class CCPasswordField(CCField):
"""A class defining an unencoded password
Member vars:
type_val (int): the type identifier of this class (set to 8)
password (string): the password string, length from 4 to 9 characters
"""
TYPE = 8
password = ""
def __init__(self, password):
if __debug__:
if len(password) > 9 or len(password) < 4:
raise AssertionError("Password must be from 4 to 9 characters in length. Password passed is '"+password+"'")
self.type_val = CCPasswordField.TYPE
self.password = password
def __str__(self):
return_str = " Password Field (type=8)\n"
return_str += " password = '"+str(self.password)+"'"
return return_str
@property
def json_data(self):
json_field = {}
json_field["type"] = self.type_val
json_field["password"] = self.password
return json_field
@property
def byte_data(self):
password_bytes = b""
password_length = len(self.password)+1
password_bytes += password_length.to_bytes(1, BYTE_ORDER)
password_bytes += self.password.encode("ascii")
password_bytes + b'\x00'
return password_bytes
class CCMonsterMovementField(CCField):
"""A class defining the monsters that move in a given level
Member vars:
type_val (int): the type identifier of this class (set to 10)
monsters (list of CCCoordinate): the coordinates of each monster
"""
TYPE = 10
def __init__(self, monsters):
if __debug__:
if len(monsters) > 128:
raise AssertionError("Max monster count of 128 exceeded. Number of monsters passed = "+str(len(monsters)))
self.type_val = CCMonsterMovementField.TYPE
self.monsters = monsters
def __str__(self):
return_str = " Monster Movement Field (type=10)\n"
for monster in self.monsters:
return_str += " monster = "+str(monster)
if monster != self.monsters[-1]:
return_str += "\n"
return return_str
@property
def json_data(self):
json_field = {}
json_field["type"] = self.type_val
monster_data = []
for monster in self.monsters:
monster_data.append(monster.json_data)
json_field["monsters"] = monster_data
return json_field
@property
def byte_data(self):
byte_value = b""
byte_value += (len(self.monsters)*2).to_bytes(2, BYTE_ORDER)
for monster in self.monsters:
byte_value += monster.x.to_bytes(2, BYTE_ORDER)
byte_value += monster.y.to_bytes(2, BYTE_ORDER)
return byte_value
class CCLevel:
"""A class defining the data of a single level
Member vars:
level_number (int): the sequence number for this level. it corresponds to it's order in the list of levels
time (int): the time limit in seconds for the level. 0 means no time limit
num_chips (int): the number of computer chips to be collected in the level
Layers: Chip's Challenge maps are 32x32 grids in 2 layers: upper and lower
A single map layer is stored as an array of 1024 ints
upper_layer (int list): the layer data for the upper (main) layer
lower_layer (int list): the lower layer data. this allows for objects to be placed under other objects
optional_fields (list of CCField types): the fields that augment the data of this level. all levels have a title and a password
"""
def __init__(self):
self.level_number = -1
self.time = -1
self.num_chips = -1
self.upper_layer = []
self.lower_layer = []
self.optional_fields = []
def __str__(self):
return_str = ""
return_str += " Level #"+str(self.level_number)+"\n"
return_str += " Time Limit = "+str(self.time)+"\n"
return_str += " Chip Count = "+str(self.num_chips)+"\n"
for field in self.optional_fields:
return_str += str(field) + "\n"
return_str += " Upper Layer:\n"
for r in range(32):
return_str += " "
row = self.upper_layer[(r*32):(r*32+32)]
for v in row:
return_str += " {0:3d}".format(v)
return_str += "\n"
return_str += " Lower Layer:\n"
for row in range(32):
return_str += " "
row = self.lower_layer[(r*32):(r*32+32)]
for v in row:
return_str += " {0:3d}".format(v)
return_str += "\n"
return return_str
def add_field(self, field):
self.optional_fields.append(field)
class CCDataFile:
"""A class defining the data of dat file
Member vars:
levels (list of CCLevels): the levels of this dat file
"""
def __init__(self):
self.levels = []
def __str__(self):
return_str = ""
return_str += "Level Pack:\n"
for level in self.levels:
return_str += str(level)
return return_str
@property
def level_count(self):
return len(self.levels)
def add_level(self, level):
self.levels.append(level)