removed costab
[awish.git] / src / awish.c
blobefa1f926118124bd1c12c13d8526dc87482f8b30
1 /*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 #include <ctype.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
20 #include "SDL.h"
21 #include "SDL_endian.h"
23 #ifdef WIN32
24 # include <windows.h>
25 #endif
27 #include "resfile.h"
28 #include "video.h"
29 #include "mainloop.h"
30 #include "vm.h"
31 #include "gameglobals.h"
32 #include "game.h"
33 #include "title.h"
34 #include "polymod.h"
37 int goobers = 0;
38 int datfirst = 0;
41 ////////////////////////////////////////////////////////////////////////////////
42 static SDL_Surface *loadCFScreen (const char *fname, SDL_Surface *old, int w, int h, int chained) {
43 SDL_Surface *new;
44 static Uint8 pal[256][3];
45 static Uint32 opal[256];
46 static Uint8 scr[64000];
47 int rsz;
48 uint8_t *diskdata = tryDiskFile(fname, &rsz);
50 if (diskdata == NULL) return old;
51 if (rsz < w*h+(chained >= 0 ? 768 : 0)) { free(diskdata); return old; }
53 new = createSurface(320*2, 200*2);
54 if (!new) { free(diskdata); return old; }
55 SDL_SetColorKey(new, 0, 0); // clear colorkey info (it's the fullscreen image after all!)
57 for (int dy = 0; dy < 200; ++dy) {
58 for (int dx = 0; dx < 320; ++dx) {
59 putPixel2x(new, dx, dy, 0);
63 memcpy(scr, diskdata, w*h);
64 if (chained >= 0) memcpy(pal, diskdata+w*h, 768);
65 free(diskdata);
67 if (chained >= 0) {
68 memcpy(opal, palette, 256*4);
69 for (int f = 0; f < 256; ++f) {
70 pal[f][0] <<= 2;
71 if (pal[f][0] != 0) pal[f][0] |= 3;
72 pal[f][1] <<= 2;
73 if (pal[f][1] != 0) pal[f][1] |= 3;
74 pal[f][2] <<= 2;
75 if (pal[f][2] != 0) pal[f][2] |= 3;
76 palette[f] = SDL_MapRGB(screen->format, pal[f][0], pal[f][1], pal[f][2]);
80 if (chained > 0) {
81 for (int dy = 0; dy < h; ++dy) {
82 for (int dx = 0; dx < w/4; ++dx) {
83 for (int c = 0; c < 4; ++c) {
84 putPixel2x(new, dx*4+c, dy, scr[dy*(w/4)+(w*h)/4*c+dx]);
88 } else {
89 for (int dy = 0; dy < h; ++dy) {
90 for (int dx = 0; dx < w; ++dx) {
91 putPixel2x(new, dx, dy, scr[dy*w+dx]);
96 memcpy(palette, opal, 256*4);
97 SDL_FreeSurface(old);
98 return new;
102 ////////////////////////////////////////////////////////////////////////////////
103 static int loadFont (void) {
104 int sz;
105 uint8_t *buf = loadResFile(&resfile, 92, &sz);
107 if (buf == NULL) return -1;
108 if (sz != 256*8) { free(buf); return -1; }
109 memcpy(font, buf, 256*8);
110 free(buf);
111 return 0;
115 static int loadPalette (void) {
116 int sz;
117 uint8_t *buf = loadResFile(&resfile, 83, &sz);
119 if (buf == NULL) return -1;
120 if (sz != 768) { free(buf); return -1; }
121 for (int f = 0; f < 768; ++f) {
122 buf[f] <<= 2;
123 if (buf[f] != 0) buf[f] |= 3;
125 for (int f = 0; f < 256; ++f) palette[f] = SDL_MapRGB(screen->format, buf[f*3+0], buf[f*3+1], buf[f*3+2]);
126 free(buf);
127 return 0;
131 static SDL_Surface *loadImage (ResFile *resfile, int idx) {
132 int sz;
133 static Uint8 *img;
134 SDL_Surface *res;
136 img = loadResFile(resfile, idx, &sz);
137 if (img == NULL) return NULL;
138 if (sz < 320*200) { free(img); return NULL; }
139 res = createSurface(320*2, 200*2);
140 if (res == NULL) { free(img); return NULL; }
141 SDL_SetColorKey(res, 0, 0); // clear colorkey info (it's the fullscreen image after all!)
142 for (int f = 0; f < 320*200; ++f) putPixel2x(res, f%320, f/320, img[f]);
143 free(img);
144 return res;
148 // return # of sprites loaded or <0 on error
149 static int loadSpriteBank (SpriteBank *bank, ResFile *resfile, int idx) {
150 Uint8 *buf;
151 int sz, pos;
153 if ((buf = loadResFile(resfile, idx, &sz)) == NULL) return -1;
154 if (sz < 3) { free(buf); return -1; }
155 pos = 0;
156 for (int f = bank->count-1; f >= 0; --f) {
157 SDL_FreeSurface(bank->spr[f][1]);
158 SDL_FreeSurface(bank->spr[f][0]);
160 bank->count = 0;
161 while (pos+4 <= sz) {
162 int h = ((Uint32)buf[pos+0])+256*((Uint32)buf[pos+1]);
163 int w = ((Uint32)buf[pos+2])+256*((Uint32)buf[pos+3]);
165 pos += 4;
166 if (h > 0 && w > 0) {
167 SDL_Surface *s0 = createSurface(w*2, h*2);
168 SDL_Surface *s1 = createSurface(w*2, h*2);
170 if (!s0 || !s1) { free(buf); return -1; }
172 for (int y = 0; y < h; ++y) {
173 for (int x = 0; x < w; ++x) {
174 if (pos < sz) {
175 putPixel2x(s0, x, y, buf[pos]);
176 putPixel2x(s1, w-x-1, y, buf[pos]);
177 ++pos;
181 bank->spr[bank->count][0] = s0;
182 bank->spr[bank->count][1] = s1;
183 ++(bank->count);
186 free(buf);
187 return bank->count;
192 static void freeSpriteBank (SpriteBank *bank) {
193 for (int f = bank->count-1; f >= 0; --f) {
194 SDL_FreeSurface(bank->spr[f][1]);
195 SDL_FreeSurface(bank->spr[f][0]);
201 ////////////////////////////////////////////////////////////////////////////////
202 static void quitCleanupRes (void) {
203 vmFreeLabels();
204 //if (!disableSound) unloadAllSounds();
205 deinitResFile(&sndfile);
206 deinitResFile(&resfile);
207 //for (int f = 0; f < sizeof(banks)/sizeof(SpriteBank); ++f) freeSpriteBank(&banks[f]);
211 ////////////////////////////////////////////////////////////////////////////////
212 #include "sincostab.c"
213 static int awishRST (int tid, int opcode, int argc, int argv[], int *argp[]) {
214 vmGVars[GVAR_RST_RESULT] = 0;
216 if (argv[0] == CONST_FRST_ML_TITLE) {
217 setMainLoopTitle();
218 } else if (argv[0] == CONST_FRST_ML_GAME) {
219 setMainLoopGame();
220 } else if (argv[0] == CONST_FRST_GET_MAX_THREADS) {
221 vmGVars[GVAR_RST_RESULT] = VM_MAX_THREADS;
222 } else if (argv[0] == CONST_FRST_GET_MAX_THREAD_ID) {
223 vmGVars[GVAR_RST_RESULT] = vmLastThread();
224 } else if (argv[0] == CONST_FRST_GET_RAND) {
225 int nmin = 0, nmax = 32767;
227 switch (argc) {
228 case 2: nmax = argv[1]; break;
229 case 3: nmin = argv[1]; nmax = argv[2]; break;
231 if (nmin >= nmax) {
232 vmGVars[GVAR_RST_RESULT] = nmin;
233 } else {
234 vmGVars[GVAR_RST_RESULT] = nmin+(randUInt32()%(nmax-nmin+1));
236 } else if (argv[0] == CONST_FRST_GET_SEED_H) {
237 vmGVars[GVAR_RST_RESULT] = getSeedH();
238 } else if (argv[0] == CONST_FRST_GET_SEED_L) {
239 vmGVars[GVAR_RST_RESULT] = getSeedL();
240 } else if (argv[0] == CONST_FRST_SET_SEED_H) {
241 if (argc >= 2) setSeedH(argv[1]);
242 } else if (argv[0] == CONST_FRST_SET_SEED_L) {
243 if (argc >= 2) setSeedL(argv[1]);
244 } else if (argv[0] == CONST_FRST_DEBUG_PRINT_STR) {
245 int pos = 0, len = 0;
247 switch (argc) {
248 case 1:
249 pos = vmPop(tid);
250 goto dolen;
251 case 2:
252 pos = argv[1];
253 dolen: if (pos >= 0) {
254 while (pos < vmCodeSize && vmCode[pos+len]) ++len;
256 break;
257 case 3:
258 pos = argv[1];
259 len = argv[2];
260 break;
262 if (goobers) {
263 if (pos >= -255 && pos <= -1) {
264 fputc(-pos, stderr);
265 } else {
266 for (; len > 0; --len, ++pos) if (pos >= 0 && pos < vmCodeSize) fputc(vmCode[pos], stderr);
269 } else if (argv[0] == CONST_FRST_DEBUG_PRINT_NUM) {
270 switch (argc) {
271 case 1: argv[1] = vmPop(tid); // fallthru
272 case 2: if (goobers) fprintf(stderr, "%d", argv[1]); break;
273 case 3: if (goobers) fprintf(stderr, "%d,%d", argv[1], argv[2]); break;
275 } else if (argv[0] == CONST_FRST_PLAY_SOUND) {
276 // arg1: sound index; arg2 (if present) channel; rst_result: -1 or channel
277 if (argc == 1) argv[1] = vmPop(tid);
278 if (argc < 2) argv[2] = -1;
279 vmGVars[GVAR_RST_RESULT] = playSound(argv[1], argv[2]);
280 } else if (argv[0] == CONST_FRST_STOP_CHANNEL) {
281 // arg1: channel
282 if (argc == 1) argv[1] = vmPop(tid);
283 vmGVars[GVAR_RST_RESULT] = stopChannel(argv[1]);
284 } else if (argv[0] == CONST_FRST_IS_CHANNEL_PLAYING) {
285 // arg1: channel; rst_result: bool
286 if (argc == 1) argv[1] = vmPop(tid);
287 vmGVars[GVAR_RST_RESULT] = isChannelPlaying(argv[1]);
288 } else if (argv[0] == CONST_FRST_LOAD_SOUND) {
289 // arg1: sound index; rst_result: bool
290 if (argc == 1) argv[1] = vmPop(tid);
291 vmGVars[GVAR_RST_RESULT] = loadSound(argv[1]) ? 0 : 1;
292 } else if (argv[0] == CONST_FRST_UNLOAD_SOUND) {
293 // arg1: sound index; rst_result: bool
294 if (argc == 1) argv[1] = vmPop(tid);
295 vmGVars[GVAR_RST_RESULT] = unloadSound(argv[1]) ? 0 : 1;
296 } else if (argv[0] == CONST_FRST_IS_SOUND_LOADED) {
297 // arg1: sound index; rst_result: bool
298 if (argc == 1) argv[1] = vmPop(tid);
299 vmGVars[GVAR_RST_RESULT] = isSoundLoaded(argv[1]) ? 0 : 1;
300 } else if (argv[0] == CONST_FRST_LOAD_SPRITES) {
301 // arg1: bank, arg2: filename (0-terminated)
302 int fnpos;
304 switch (argc) {
305 case 1:
306 argv[2] = vmPop(tid);
307 argv[1] = vmPop(tid);
308 break;
309 case 2:
310 argv[2] = vmPop(tid);
311 break;
314 fnpos = argv[2];
315 vmGVars[GVAR_RST_RESULT] = -1;
316 if (fnpos >= 0 && fnpos < vmCodeSize-1 && argv[1] >= 0 && argv[1] <= 256) {
317 int end;
319 for (end = fnpos; end < vmCodeSize && vmCode[end]; ++end) ;
320 if (end < vmCodeSize) {
321 //fprintf(stderr, "!!! %d: [%s]\n", argv[1], (const char *)(vmCode+fnpos));
322 vmGVars[GVAR_RST_RESULT] = loadSpriteBankFromFile(&banks[argv[1]], (const char *)(vmCode+fnpos));
325 } else if (argv[0] == CONST_FRST_ADD_LEVEL_SPRITE || argv[0] == CONST_FRST_ADD_SPRITE) {
326 // x, y, layer, bank, num, dir (can take 1st 2 args from stack too)
327 int inlevel = (argv[0] == CONST_FRST_ADD_LEVEL_SPRITE);
328 int x = 0, y = 0, layer, bank, num, dir;
330 dir = vmPop(tid);
331 num = vmPop(tid);
332 bank = vmPop(tid);
333 layer = vmPop(tid);
334 switch (argc) {
335 case 1:
336 y = vmPop(tid);
337 x = vmPop(tid);
338 break;
339 case 2:
340 y = vmPop(tid);
341 x = argv[1];
342 break;
343 case 3:
344 y = argv[2];
345 x = argv[1];
346 break;
348 //fprintf(stderr, "x=%d, y=%d, layer=%d, bank=%d, num=%d, dir=%d, inlevel=%d\n", x, y, layer, bank, num, dir, inlevel);
349 addSpriteToLayer(x, y, layer, bank, num, dir, inlevel);
350 } else if (argv[0] == CONST_FRST_START_POLY) {
351 int angle = vmPop(tid);
352 int scale = vmPop(tid);
353 int ofsx = 0, ofsy = 0;
355 switch (argc) {
356 case 1:
357 ofsy = vmPop(tid);
358 ofsx = vmPop(tid);
359 break;
360 case 2:
361 ofsy = vmPop(tid);
362 ofsx = argv[1];
363 break;
364 case 3:
365 ofsy = argv[2];
366 ofsx = argv[1];
367 break;
369 pmStart(ofsx, ofsy, scale, angle);
370 } else if (argv[0] == CONST_FRST_ADD_POLY_POINT) {
371 switch (argc) {
372 case 1:
373 argv[2] = vmPop(tid);
374 argv[1] = vmPop(tid);
375 break;
376 case 2:
377 argv[2] = vmPop(tid);
378 break;
380 pmAddPoint(argv[1], argv[2]);
381 } else if (argv[0] == CONST_FRST_END_POLY) {
382 switch (argc) {
383 case 1:
384 argv[2] = vmPop(tid);
385 argv[1] = vmPop(tid);
386 break;
387 case 2:
388 argv[2] = vmPop(tid);
389 break;
391 pmDone(argv[1], argv[2]);
392 } else if (argv[0] == CONST_FRST_DRAW_TEXT) {
393 // arg1: addr; arg2: len, x, y, scale, angle, color
394 int color = vmPop(tid);
395 int angle = vmPop(tid);
396 int scale = vmPop(tid);
397 int y = vmPop(tid);
398 int x = vmPop(tid);
399 int addr = 0, len = -1;
400 const char *str = (const char *)vmCode;
402 switch (argc) {
403 case 1:
404 addr = vmPop(tid);
405 break;
406 case 2:
407 addr = argv[1];
408 break;
409 case 3:
410 addr = argv[1];
411 len = argv[2];
412 break;
414 if (len != 0 && addr >= 0 && addr < vmCodeSize) {
415 str += addr;
416 if (len < 0) {
417 for (len = 0; addr+len < vmCodeSize && str[len]; ++len) ;
418 } else {
419 if (addr+len > vmCodeSize) len = vmCodeSize-addr;
421 if (str != NULL && len > 0) textAdd(str, len, x, y, scale, angle, color, 255);
423 } else if (argv[0] == CONST_FRST_COS || argv[0] == CONST_FRST_SIN) {
424 if (argc < 2) argv[1] = vmPop(tid);
425 argv[1] %= 360; if (argv[1] < 0) argv[1] += 360;
426 if (argv[0] == CONST_FRST_COS) argv[1] = (argv[1]+90)%360;
427 vmGVars[GVAR_RST_RESULT] = sintab[argv[1]];
428 } else {
429 if (gameRSTCB) return gameRSTCB(tid, opcode, argc, argv, argp);
430 fatal("invalid RST: %d", argv[0]);
432 return 0; // continue
436 ////////////////////////////////////////////////////////////////////////////////
437 static int checkLevelCode (const char *t) {
438 int ctrd;
440 if (!t || !t[0]) return -1;
441 ctrd = vmNewThread(0);
442 for (int level = 0; level < vmGVars[GVAR_MAX_LEVEL]; ++level) {
443 //fprintf(stderr, "%d/%d\n", level+1, vmGVars[GVAR_MAX_LEVEL]);
444 vmPush(ctrd, level);
445 if (vmExecuteBSR(ctrd, CODE_ENTRY_GET_LEVEL_CODE, 0) == 0) {
446 int pos = vmGVars[GVAR_LEVEL_CODE_OFS], len = vmGVars[GVAR_LEVEL_CODE_LEN], ok = 1;
448 if (strlen(t) == len && pos >= 0 && len > 0 && pos+len <= vmCodeSize) {
449 //fwrite(vmCode+pos, len, 1, stderr);
450 //fprintf(stderr, " [%s]\n", t);
451 for (int f = 0; f < len; ++f) {
452 //fprintf(stderr, "%c %c\n", tolower(t[f]), tolower(vmCode[pos+f]));
453 if (tolower(t[f]) != tolower(vmCode[pos+f])) { ok = 0; break; }
455 if (ok) {
456 if (goobers) fprintf(stderr, "found code for level #%02d\n", level+1);
457 return level;
460 } else {
461 if (goobers) fprintf(stderr, "sorry!\n");
462 break;
465 vmKillThread(ctrd);
466 return -1;
470 ////////////////////////////////////////////////////////////////////////////////
471 #ifdef _WIN32
472 # include "cmdline.c"
473 #endif
476 ////////////////////////////////////////////////////////////////////////////////
477 int main (int argc, char *argv[]) {
478 int csz;
480 #ifdef _WIN32
481 cmdLineParse();
482 argc = k8argc;
483 argv = k8argv;
484 #endif
486 memset(banks, 0, sizeof(banks));
488 polymodInitialize();
490 for (int f = 1; f < argc; ++f) {
491 int eaten = 1;
493 if (strcmp(argv[f], "-goobers") == 0) goobers = 1;
494 else if (strcmp(argv[f], "--goobers") == 0) goobers = 1;
495 else if (strcmp(argv[f], "-dat") == 0) datfirst = 1;
496 else if (strcmp(argv[f], "--dat") == 0) datfirst = 1;
497 else if (strcmp(argv[f], "-trace") == 0) vmDebugTrace = 1;
498 else if (strcmp(argv[f], "--trace") == 0) vmDebugTrace = 1;
499 else if (strcmp(argv[f], "-nosound") == 0) disableSound = 1;
500 else if (strcmp(argv[f], "--nosound") == 0) disableSound = 1;
501 else if (strcmp(argv[f], "-tracelog") == 0 || strcmp(argv[f], "--tracelog") == 0) {
502 eaten = 1;
503 vmDebugTrace = 1;
504 vmDebugOutput = fopen("ztrace.log", "w");
505 } else {
506 eaten = 0;
508 if (eaten > 0) {
509 for (int c = f+eaten; c < argc; ++c) argv[c-1] = argv[c];
510 argv[argc -= eaten] = NULL;
511 f -= eaten;
515 initResFile(&resfile, "RESOURCE.DAT");
516 initResFile(&sndfile, "RESOURCE.SND");
517 atexit(quitCleanupRes);
519 if (loadFont() != 0) {
520 fprintf(stderr, "FATAL: can't load font!\n");
521 exit(1);
524 sdlInit();
525 initVideo();
527 clearSpriteLayers();
529 if (loadPalette() != 0) {
530 fprintf(stderr, "FATAL: can't load palette!\n");
531 exit(1);
535 SurfaceLock lock;
537 lockSurface(&lock, screen);
538 drawString(screen, "loading...", 2, 200-10, 1);
539 unlockSurface(&lock);
540 SDL_Flip(screen);
543 for (int f = 0; f < 8; ++f) {
544 if ((backs[(f+1)%8] = loadImage(&resfile, f)) == NULL) {
545 fprintf(stderr, "FATAL: can't load image #%d!\n", f);
546 exit(1);
549 if ((backs[8] = loadImage(&resfile, 8)) == NULL) {
550 fprintf(stderr, "FATAL: can't load image #%d!\n", 8);
551 exit(1);
554 backs[0] = loadCFScreen("CFTITLE.DAT", backs[0], 320, 200, 1);
555 backs[0] = loadCFScreen("cftitle.dat", backs[0], 320, 200, 1);
557 memset(banks, 0, sizeof(banks));
558 for (int f = 84; f <= 90; ++f) {
559 if (loadSpriteBank(&banks[f], &resfile, f) <= 0) {
560 fprintf(stderr, "FATAL: can't load sprite bank #%d!\n", f);
561 exit(1);
564 if (loadSpriteBankFromFile(&banks[256], "sprites/cursors.spr") <= 0) {
565 fprintf(stderr, "FATAL: can't load 'cursors' sprite bank!\n");
566 exit(1);
569 if (vmInitialize() != 0) fatal("can't init VM");
571 memset(vmGVars, 0, sizeof(vmGVars));
572 vmRSTCB = awishRST;
574 vmAddLabel("flag_skip_title", LB_GVAR, 120, 1); // 1: public
575 vmGVars[120] = 0;
577 vmCodeSize = 0;
578 if ((csz = loadCodeFile(&resfile, vmCodeSize, 93, 0)) < 1) {
579 fprintf(stderr, "FATAL: can't load VM code!\n");
580 exit(1);
582 vmCodeSize += csz;
583 initLabels();
584 vmGVars[GVAR_POLYFIX_BASE] = POLYFIX_BASE;
587 int rsz = 0;
588 uint8_t *buf;
590 if ((buf = loadDiskFileEx("goobers.vmd", &rsz)) != NULL) {
591 if (buf != NULL) {
592 csz = vmLoadCodeFileFromDump(buf, rsz, vmCodeSize, vmMaxGVar, vmMaxTVar, &vmMaxGVar, &vmMaxTVar);
593 free(buf);
594 if (csz > 0) vmCodeSize += csz;
599 //vmGVars[GVAR_KEY_QUIT] = 0;
600 //vmGVars[GVAR_KEY_START] = 0;
601 vmGVars[GVAR_GOOBERS] = goobers;
603 vmSetPC(0, CODE_ENTRY_TITLE);
604 if (vmExecuteBSR(0, CODE_ENTRY_MAIN_INIT, 0) != 0) fatal("can't initialize game");
605 //if (goobers) fprintf(stderr, "MAX LEVEL: %d\n", vmGVars[GVAR_MAX_LEVEL]);
608 VMLabelInfo *l = vmFindLabel("entry_goobers_init");
610 if (l != NULL && l->type == LB_CODE) {
611 if (vmExecuteBSR(0, l->value, 0) != 0) fatal("can't initialize game");
615 if (argc > 1) {
616 // check level code
617 for (int f = 1; f < argc; ++f) {
618 int lvl = checkLevelCode(argv[f]);
620 if (lvl >= 0) {
621 vmGVars[GVAR_START_LEVEL] = lvl;
622 break;
627 mainLoop();
629 return 0;
633 #ifdef WIN32
634 int CALLBACK WinMain (HINSTANCE hInstance, HINSTANCE unused__, LPSTR lpszCmdLine, int nCmdShow) {
635 char *shit[] = { (char *)"shit", NULL };
636 return SDL_main(1, shit);
638 #endif