Debian Codegraph @

terps/plus/apple2detect.c

raw symbols SCIP
1 //
2 // apple2detect.c
3 // Part of Plus, an interpreter for Scott Adams Graphic Adventures Plus
4 //
5 // Created by Petter Sjölund on 2022-08-03.
6 //
7
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "common.h"
12 #include "companionfile.h"
13 #include "definitions.h"
14 #include "graphics.h"
15
16 #include "apple2detect.h"
17
18 typedef struct imglist {
19 char *filename;
20 size_t offset;
21 } imglist;
22
23 static const imglist a2listFantastic[] = {
24 { "R001", 0x1e800 },
25 { "R002", 0x1df00 },
26 { "R004", 0x1db00 },
27 { "R005", 0x1cb00 },
28 { "R006", 0x1c200 },
29 { "R007", 0x1bb00 },
30 { "R008", 0x1b300 },
31 { "R009", 0x12400 },
32 { "R010", 0x1aa00 },
33 { "R011", 0x1a100 },
34 { "R012", 0x18e00 },
35 { "R013", 0x18b00 },
36 { "R014", 0x18100 },
37 { "R015", 0x17800 },
38 { "R016", 0x16d00 },
39 { "R017", 0x16800 },
40 { "R018", 0x15c00 },
41 { "R019", 0x15500 },
42 { "R020", 0x14d00 },
43 { "R021", 0x13e00 },
44 { "R022", 0x13300 },
45 { "B020", 0xc10e },
46 { "B002", 0x11500 },
47 { "B003", 0x10f00 },
48 { "B004", 0x10a00 },
49 { "B005", 0x10000 },
50 { "B006", 0xfb00 },
51 { "B007", 0xf900 },
52 { "B008", 0xf700 },
53 { "B009", 0xf200 },
54 { "B010", 0xe800 },
55 { "B011", 0xe600 },
56 { "B012", 0xe400 },
57 { "B013", 0xe300 },
58 { "B014", 0xc600 },
59 { "B015", 0xde00 },
60 { "B016", 0xda00 },
61 { "B017", 0xd800 },
62 { "B018", 0xd700 },
63 { "B019", 0xd000 },
64 { "B020", 0xcc00 },
65 { "B021", 0xca00 },
66 { "B022", 0xc900 },
67 { "B023", 0xc800 },
68 { "S000", 0x1000 },
69 { "S001", 0x2000 },
70 { "S002", 0x2400 },
71 { "S003", 0x2600 },
72 { "S004", 0x2800 },
73 { "S005", 0x2a00 },
74 { "S006", 0x2c00 },
75 { "S007", 0x2e00 },
76 { "S008", 0x3500 },
77 { "S009", 0x3600 },
78 { "S010", 0x3700 },
79 { "S011", 0x3900 },
80 { "S012", 0x3b00 },
81 { "S013", 0x3c00 },
82 { "S014", 0x3f00 },
83 { "S015", 0x4100 },
84 { "S016", 0x4200 },
85 { "S017", 0x4300 },
86 { "S018", 0x4400 },
87 { "S019", 0x4500 },
88 { "S020", 0x4600 },
89 { NULL, 0, }
90 };
91
92 static const imglist a2listSpidey[] = {
93 { "R001", 0x19f00 },
94 { "R002", 0x19900 },
95 { "R003", 0x14200 },
96 { "R005", 0xdf00 },
97 { "R006", 0x19300 },
98 { "R007", 0x18500 },
99 { "R008", 0x17800 },
100 { "R013", 0x16f00 },
101 { "R015", 0xd700 },
102 { "R017", 0x16100 },
103 { "R018", 0xce00 },
104 { "R019", 0x13400 },
105 { "R022", 0x12400 },
106 { "R024", 0x10000 },
107 { "R025", 0x15700 },
108 { "R034", 0x14f00 },
109 { "R037", 0x11500 },
110 { "R040", 0xf100 },
111 { "B015", 0x5d00 }, // Bio gem
112 { "B016", 0xbd00 }, // Egg
113 { "B021", 0xc100 }, // Stopped fan
114 { "B023", 0xb900 }, // Shaft
115 { "B025", 0xc600 }, // wall mirror
116 { "B027", 0xb700 }, // open air duct
117 { "B028", 0xb500 }, // strange cloud
118 { "B031", 0xae00 }, // Octopus
119 { "B033", 0xa900 }, // Electro
120 { "B034", 0xa400 }, // Stunned Ock
121 { "B035", 0x9f00 }, // Stunned Electro
122 { "B038", 0x9c00 }, // Lizard
123 { "B044", 0x7300 },
124 { "B049", 0x7700 },
125 { "B050", 0x6500 },
126 { "B051", 0x9100 },
127 { "B052", 0x8d00 },
128 { "B053", 0x7000 },
129 { "B055", 0x6800 },
130 { "B056", 0x6b00 },
131 { "B058", 0x8700 },
132 { "B059", 0x8500 },
133 { "B060", 0x8100 },
134 { "B062", 0x7f00 }, // Ringmaster
135 { "B071", 0x7800 }, // Clock
136 { "S000", 0x5100 },
137 { "S001", 0x6e00 },
138 { "S002", 0x7100 },
139 { "S003", 0x7400 },
140 { "S004", 0x7700 },
141 { "S005", 0x7a00 },
142 { "S009", 0x4200 },
143 { "S010", 0x3800 },
144 { "S011", 0x1900 },
145 { "S012", 0x2300 },
146 { "S013", 0x2900 },
147 { "S014", 0x6200 },
148 { "S015", 0x6700 },
149 { "S016", 0x6900 },
150 { "S017", 0x6a00 },
151 { "S018", 0x6c00 },
152 { "S020", 0x7d00 },
153 { "S021", 0x8300 },
154 { "S022", 0x8900 },
155 { "S023", 0x16179 },
156 { "S024", 0x16682 },
157 { "S026", 0x3100 },
158 { NULL, 0 }
159 };
160
161 static const struct imglist a2listBanzai[] = {
162 { "R001", 0x1dc00 },
163 { "R002", 0x1d600 },
164 { "R003", 0x1cd00 },
165 { "R004", 0x1c000 },
166 { "R005", 0x1b400 },
167 { "R006", 0x1a900 },
168 { "R008", 0x1a000 },
169 { "R009", 0x19500 },
170 { "R010", 0x16f00 },
171 { "R011", 0x18c00 },
172 { "R012", 0x18700 },
173 { "R013", 0x18000 },
174 { "R014", 0x17600 },
175 { "R015", 0x16f00 },
176 { "R016", 0xf300 },
177 { "R017", 0x16c00 },
178 { "R018", 0x15f00 },
179 { "R019", 0x15400 },
180 { "R020", 0x14d00 },
181 { "R021", 0x14100 },
182 { "R022", 0x13900 },
183 { "R023", 0x13100 },
184 { "R024", 0x12a00 },
185 { "R025", 0x11f00 },
186 { "R026", 0x11500 },
187 { "R028", 0x10d00 },
188 { "R029", 0x10600 },
189 { "R035", 0xfd00 },
190 { "S000", 0x4700 },
191 { "B027", 0xf000 },
192 { "B004", 0xe600 },
193 { "B005", 0xe500 },
194 { "B006", 0xdf00 },
195 { "B019", 0xda00 },
196 { "B007", 0xd700 },
197 { "B030", 0xd600 },
198 { "B025", 0xd100 },
199 { "B026", 0xd000 },
200 { "B051", 0xcf00 },
201 { "B023", 0xcd00 },
202 { "B055", 0xcc00 },
203 { "B056", 0xc300 },
204 { "B070", 0xb900 },
205 { "B071", 0xb400 },
206 { "B073", 0xb200 },
207 { "B074", 0xb000 },
208 { "B037", 0xb100 },
209 { "B034", 0xaf00 },
210 { "B035", 0xa700 },
211 { "S001", 0x1000 },
212 { "S002", 0x1900 },
213 { "S003", 0x1a00 },
214 { "S004", 0x1d00 },
215 { "S005", 0x2000 },
216 { "S006", 0x2300 },
217 { "S007", 0x2600 },
218 { "S008", 0x2900 },
219 { "S009", 0x2c00 },
220 { "S010", 0x2f00 },
221 { "S011", 0x3700 },
222 { "S012", 0x3a00 },
223 { "S013", 0x3e00 },
224 { "S014", 0x4100 },
225 { "S015", 0x4300 },
226 { "S016", 0x4500 },
227 { "S017", 0x4600 },
228 { NULL, 0, }
229 };
230
231 uint8_t *new = NULL;
232
233 int DetectApple2(uint8_t **sf, size_t *extent)
234 {
235 const size_t dsk_image_size = 35 * 16 * 256;
236 if (*extent > MAX_LENGTH || *extent < dsk_image_size)
237 return 0;
238
239 new = MemAlloc(dsk_image_size);
240 size_t offset = dsk_image_size - 256;
241 for (int i = 0; i < *extent && i < dsk_image_size; i += 256) {
242 memcpy(new + offset, *sf + i, 256);
243 offset -= 256;
244 }
245
246 int actionsize = new[125490] + (new[125491] << 8);
247 debug_print("Actionsize: %d\n", actionsize);
248 if (actionsize < 4000 || actionsize > 7000) {
249 size_t companionsize;
250 uint8_t *companionfile = GetCompanionFile(&companionsize);
251 if (companionfile && companionsize >= dsk_image_size) {
252 free(new);
253 new = MemAlloc(companionsize);
254 offset = companionsize - 256;
255 for (int i = 0; i < companionsize; i += 256) {
256 memcpy(new + offset, companionfile + i, 256);
257 offset -= 256;
258 }
259 actionsize = new[125490] + (new[125491] << 8);
260 }
261 if (actionsize < 4000 || actionsize > 7000) {
262 free(new);
263 return 0;
264 }
265 }
266
267 size_t datasize = dsk_image_size - 125438;
268 uint8_t *gamedata = MemAlloc(datasize);
269 memcpy(gamedata, new + 125438, datasize);
270
271 if (gamedata) {
272 free(*sf);
273 *sf = gamedata;
274 *extent = datasize;
275 CurrentSys = SYS_APPLE2;
276 ImageWidth = 280;
277 ImageHeight = 152;
278 return 1;
279 }
280
281 return 0;
282 }
283
284 void LookForApple2Images(void)
285 {
286 if (new == NULL)
287 return;
288
289 const struct imglist *list = a2listSpidey;
290
291 switch (CurrentGame) {
292 case SPIDERMAN:
293 list = a2listSpidey;
294 break;
295 case BANZAI:
296 list = a2listBanzai;
297 break;
298 case FANTASTIC4:
299 list = a2listFantastic;
300 break;
301 default:
302 Fatal("Unknown game!");
303 break;
304 }
305
306 int count = Game->no_of_room_images + Game->no_of_item_images + Game->no_of_special_images;
307
308 Images = MemAlloc((count + 1) * sizeof(struct imgrec));
309
310 int created = 0;
311
312 for (int i = 0; i <= GameHeader.NumRooms; i++) {
313 int exists = 0;
314 for (int j = 0; j < created; j++) {
315 if (Images[j].DiskOffset == Rooms[i].Image) {
316 exists = 1;
317 break;
318 }
319 }
320 if (exists || Rooms[i].Image == 0)
321 continue;
322
323 size_t offset = 0x23000 - Rooms[i].Image - 0x100;
324 size_t size = new[offset] + new[offset + 1] * 0x100;
325
326 int ct = 0;
327 int found = 0;
328 while (list[ct].filename != NULL) {
329 if (list[ct].offset == Rooms[i].Image) {
330 Images[created].Filename = list[ct].filename;
331 Images[created].DiskOffset = list[ct].offset;
332 Rooms[i].Image = (list[ct].filename[2] - '0') * 10 + list[ct].filename[3] - '0';
333 found = 1;
334 break;
335 }
336 ct++;
337 }
338 if (found == 0) {
339 fprintf(stderr, "Error, room with offset %x not found!\n", Rooms[i].Image);
340 continue;
341 }
342
343 Images[created].Data = MemAlloc(size);
344 memcpy(Images[created].Data, new + offset, size);
345 Images[created].Size = size;
346 created++;
347 }
348
349 for (int i = 0; i <= GameHeader.NumRooms; i++) {
350 int ct = 0;
351 while (list[ct].filename != NULL) {
352 if (list[ct].offset == Rooms[i].Image) {
353 Rooms[i].Image = (list[ct].filename[2] - '0') * 10 + list[ct].filename[3] - '0';
354 break;
355 }
356 ct++;
357 }
358 }
359
360 for (int i = 0; i <= GameHeader.NumObjImg; i++) {
361 int exists = 0;
362 for (int j = 0; j < created; j++) {
363 if (Images[j].DiskOffset == ObjectImages[i].Image) {
364 exists = 1;
365 break;
366 }
367 }
368 if (exists || ObjectImages[i].Image == 0)
369 continue;
370 size_t offset = 0x23000 - ObjectImages[i].Image - 0x100;
371 if (offset > 0x23000)
372 continue;
373 size_t size = new[offset] + new[offset + 1] * 0x100 + 2;
374 if (offset + size > 0x23000)
375 size = 0x23000 - offset;
376
377 int ct = 0;
378 int found = 0;
379 while (list[ct].filename != NULL) {
380 if (list[ct].offset == ObjectImages[i].Image) {
381 Images[created].Filename = list[ct].filename;
382 ObjectImages[i].Image = (list[ct].filename[2] - '0') * 10 + list[ct].filename[3] - '0';
383 found = 1;
384 break;
385 }
386 ct++;
387 }
388 if (found == 0) {
389 fprintf(stderr, "Error, image with offset %x not found!\n", ObjectImages[i].Image);
390 continue;
391 }
392
393 Images[created].Data = MemAlloc(size);
394 memcpy(Images[created].Data, new + offset, size);
395 Images[created].Size = size;
396 Images[created].DiskOffset = list[ct].offset;
397 created++;
398 if (created > count) {
399 fprintf(stderr, "Error!\n");
400 created--;
401 break;
402 }
403 }
404
405 for (int i = 0; i <= GameHeader.NumObjImg; i++) {
406 int ct = 0;
407 while (list[ct].filename != NULL) {
408 if (list[ct].offset == ObjectImages[i].Image) {
409 ObjectImages[i].Image = (list[ct].filename[2] - '0') * 10 + list[ct].filename[3] - '0';
410 break;
411 }
412 ct++;
413 }
414 }
415
416 int ct = 0;
417 while (list[ct].filename != NULL) {
418 if (list[ct].filename[0] == 'S') {
419 Images[created].Filename = list[ct].filename;
420 size_t size = new[list[ct].offset] + new[list[ct].offset + 1] * 256;
421 Images[created].Size = size;
422 Images[created].Data = MemAlloc(size);
423 memcpy(Images[created].Data, new + list[ct].offset, size);
424 created++;
425 }
426 ct++;
427 }
428
429 Images[created].Filename = NULL;
430
431 free(new);
432 }