00001
00002
00003
00004
00005
00006
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <allegro.h>
00010
00011 #ifdef __cplusplus
00012 #error "Hey, stop compiling mappyal.c as C++!, see MappyAL readme.txt"
00013 #endif
00014
00015
00016 #define RB8BITTOPINK
00017
00018
00019 #define MER_NONE 0
00020 #define MER_OUTOFMEM 1
00021 #define MER_MAPLOADERROR 2
00022 #define MER_NOOPEN 3
00023 #define MER_NOSCREEN 4
00024 #define MER_NOACCELERATION 5
00025 #define MER_CVBFAILED 6
00026 #define MER_MAPTOONEW 7
00027
00028 #define AN_END -1
00029 #define AN_NONE 0
00030 #define AN_LOOPF 1
00031 #define AN_LOOPR 2
00032 #define AN_ONCE 3
00033 #define AN_ONCEH 4
00034 #define AN_PPFF 5
00035 #define AN_PPRR 6
00036 #define AN_PPRF 7
00037 #define AN_PPFR 8
00038 #define AN_ONCES 9
00039
00040 typedef struct {
00041 long int bgoff, fgoff;
00042 long int fgoff2, fgoff3;
00043 unsigned long int user1, user2;
00044 unsigned short int user3, user4;
00045 unsigned char user5, user6, user7;
00046 unsigned char tl : 1;
00047 unsigned char tr : 1;
00048 unsigned char bl : 1;
00049 unsigned char br : 1;
00050 unsigned char trigger : 1;
00051 unsigned char unused1 : 1;
00052 unsigned char unused2 : 1;
00053 unsigned char unused3 : 1;
00054 } BLKSTR;
00055
00056 typedef struct {
00057 signed char antype;
00058 signed char andelay;
00059 signed char ancount;
00060 signed char anuser;
00061 long int ancuroff;
00062 long int anstartoff;
00063 long int anendoff;
00064 } ANISTR;
00065
00066
00067
00068 int maperror, mapgfxinbitmaps;
00069 short int mapwidth, mapheight, mapblockwidth, mapblockheight, mapdepth;
00070 short int mapblockstrsize, mapnumblockstr, mapnumblockgfx;
00071 PACKFILE * mapfilept;
00072 short int * mappt = NULL;
00073 short int ** maparraypt = NULL;
00074 char * mapcmappt = NULL;
00075 char * mapblockgfxpt = NULL;
00076 char * mapblockstrpt = NULL;
00077 ANISTR * mapanimstrpt = NULL;
00078 int * mapanimseqpt = NULL;
00079 ANISTR * mapanimstrendpt;
00080 RGB mapcmap6bit[256];
00081 short int * mapmappt[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
00082 short int ** mapmaparraypt[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
00083 BITMAP ** abmTiles = NULL;
00084
00085 int mapaltdepth;
00086 int maptype, mapislsb, mapclickmask;
00087 int mapblockgapx, mapblockgapy, mapblockstaggerx, mapblockstaggery;
00088 int mapblocksinvidmem, mapblocksinsysmem;
00089 char mapnovctext[80];
00090
00091
00092 int MapGenerateYLookup (void)
00093 {
00094 int i, j;
00095
00096 for (i=0;i<8;i++) {
00097 if (mapmaparraypt[i]!=NULL) { free (mapmaparraypt[i]); mapmaparraypt[i] = NULL; }
00098 if (mapmappt[i]!=NULL) {
00099 mapmaparraypt[i] = malloc (mapheight*sizeof(short int *));
00100 if (mapmaparraypt[i] == NULL) return -1;
00101 for (j=0;j<mapheight;j++) mapmaparraypt[i][j] = (mapmappt[i]+(j*mapwidth));
00102 if (mapmappt[i] == mappt) maparraypt = mapmaparraypt[i];
00103 }
00104 }
00105 return 0;
00106 }
00107
00108 static int MEClickmask (int x, int y, int xory)
00109 {
00110 if (abmTiles == NULL) return 0;
00111
00112 x %= mapblockgapx; y %= mapblockgapy;
00113
00114 if (x >= mapblockwidth && xory == 0) return 0;
00115 if (x >= mapblockwidth && xory == 1) {
00116 if (y < mapblockstaggery) return -1;
00117 else return 1;
00118 }
00119 if (y >= mapblockheight && xory == 1) return 1;
00120 if (y >= mapblockheight && xory == 0) {
00121 if (x < mapblockstaggerx) return -1;
00122 else return 0;
00123 }
00124
00125 switch (mapdepth) {
00126 case 8:
00127 if (getpixel (abmTiles[mapclickmask], x, y) == 0) {
00128 if (x < (mapblockwidth/2) && xory == 0) return -1;
00129 if (x >= (mapblockwidth/2) && xory == 0) return 0;
00130 if (y < (mapblockheight/2) && xory == 1) return -1;
00131 if (y >= (mapblockheight/2) && xory == 1) return 1;
00132 }
00133 return 0;
00134 default:
00135 if (getpixel (abmTiles[mapclickmask], x, y) == makecol (255,0,255)) {
00136 if (x < (mapblockwidth/2) && xory == 0) return -1;
00137 if (x >= (mapblockwidth/2) && xory == 0) return 0;
00138 if (y < (mapblockheight/2) && xory == 1) return -1;
00139 if (y >= (mapblockheight/2) && xory == 1) return 1;
00140 }
00141 return 0;
00142 }
00143 return 0;
00144 }
00145
00146 int MapGetXOffset (int xpix, int ypix)
00147 {
00148 int xb;
00149
00150 if (mapblockstaggerx || mapblockstaggery) {
00151 xpix += (mapblockstaggerx);
00152 ypix += (mapblockstaggery);
00153 }
00154 xb = xpix/mapblockgapx;
00155
00156 if ((mapblockstaggerx || mapblockstaggery) && mapclickmask) xb += MEClickmask (xpix, ypix, 0);
00157
00158 if (xb < 0) xb = 0;
00159 if (xb >= mapwidth) xb = mapwidth-1;
00160 return xb;
00161 }
00162
00163 int MapGetYOffset (int xpix, int ypix)
00164 {
00165 int yb;
00166
00167 if (mapblockstaggerx || mapblockstaggery) {
00168 xpix += (mapblockstaggerx);
00169 ypix += (mapblockstaggery);
00170 }
00171 yb = ypix/mapblockgapy;
00172
00173 if ((mapblockstaggerx || mapblockstaggery) && mapclickmask) {
00174 yb *= 2;
00175 yb += MEClickmask (xpix, ypix, 1);
00176 }
00177
00178 if (yb < 0) yb = 0;
00179 if (yb >= mapheight) yb = mapheight-1;
00180 return yb;
00181 }
00182
00183 BLKSTR * MapGetBlockInPixels (int x, int y)
00184 {
00185 int xp, yp;
00186 short int * mymappt;
00187 ANISTR * myanpt;
00188
00189 if (x < 0 || y < 0 || x >= (mapwidth*mapblockwidth) || y >= (mapheight*mapblockheight)) return NULL;
00190
00191 xp = x; yp = y;
00192 x = MapGetXOffset (xp, yp);
00193 y = MapGetYOffset (xp, yp);
00194
00195 if (maparraypt!= NULL) {
00196 mymappt = maparraypt[y]+x;
00197 } else {
00198 mymappt = mappt;
00199 mymappt += x;
00200 mymappt += y*mapwidth;
00201 }
00202 if (*mymappt>=0) return ((BLKSTR*) mapblockstrpt) + *mymappt;
00203 else { myanpt = mapanimstrendpt + *mymappt;
00204 return ((BLKSTR *) mapblockstrpt) + mapanimseqpt[myanpt->ancuroff]; }
00205 }
00206
00207 BLKSTR * MapGetBlock (int x, int y)
00208 {
00209 short int * mymappt;
00210 ANISTR * myanpt;
00211
00212 if (maparraypt!= NULL) {
00213 mymappt = maparraypt[y]+x;
00214 } else {
00215 mymappt = mappt;
00216 mymappt += x;
00217 mymappt += y*mapwidth;
00218 }
00219 if (*mymappt>=0) return ((BLKSTR*) mapblockstrpt) + *mymappt;
00220 else { myanpt = mapanimstrendpt + *mymappt;
00221 return ((BLKSTR *) mapblockstrpt) + mapanimseqpt[myanpt->ancuroff]; }
00222 }
00223
00224 void MapSetBlockInPixels (int x, int y, int strvalue)
00225 {
00226 int xp, yp;
00227 short int * mymappt;
00228
00229 if (x < 0 || y < 0 || x >= (mapwidth*mapblockwidth) || y >= (mapheight*mapblockheight)) return;
00230 xp = x; yp = y;
00231 x = MapGetXOffset (xp, yp);
00232 y = MapGetYOffset (xp, yp);
00233
00234 if (maparraypt!= NULL) {
00235 mymappt = maparraypt[y]+x;
00236 } else {
00237 mymappt = mappt;
00238 mymappt += x;
00239 mymappt += y*mapwidth;
00240 }
00241 *mymappt = strvalue;
00242 }
00243
00244 void MapSetBlock (int x, int y, int strvalue)
00245 {
00246 short int * mymappt;
00247
00248 if (maparraypt!= NULL) {
00249 mymappt = maparraypt[y]+x;
00250 } else {
00251 mymappt = mappt;
00252 mymappt += x;
00253 mymappt += y*mapwidth;
00254 }
00255 *mymappt = strvalue;
00256 }
00257
00258 int MapChangeLayer (int newlyr)
00259 {
00260 if (newlyr<0 || newlyr>7 || mapmappt[newlyr] == NULL) return -1;
00261 mappt = mapmappt[newlyr]; maparraypt = mapmaparraypt[newlyr];
00262 return newlyr;
00263 }
00264
00265 int MapGetBlockID (int blid, int usernum)
00266 {
00267 int i;
00268 BLKSTR * myblkpt;
00269
00270 myblkpt = (BLKSTR *) mapblockstrpt;
00271 if (myblkpt == NULL) return -1;
00272
00273 for (i=0;i<mapnumblockstr;i++) {
00274 switch (usernum) {
00275 case 1:
00276 if (myblkpt[i].user1 == blid) return i;
00277 break;
00278 case 2:
00279 if (myblkpt[i].user2 == blid) return i;
00280 break;
00281 case 3:
00282 if (myblkpt[i].user3 == blid) return i;
00283 break;
00284 case 4:
00285 if (myblkpt[i].user4 == blid) return i;
00286 break;
00287 case 5:
00288 if (myblkpt[i].user5 == blid) return i;
00289 break;
00290 case 6:
00291 if (myblkpt[i].user6 == blid) return i;
00292 break;
00293 case 7:
00294 if (myblkpt[i].user7 == blid) return i;
00295 break;
00296 }
00297 }
00298
00299 return -1;
00300 }
00301
00302 int MapDecodeMAR (unsigned char * mrpt, int marlyr)
00303 {
00304 int i, j;
00305 short int * mymarpt;
00306
00307 if (marlyr < 0 || marlyr > 7) return -1;
00308
00309 if (mapmappt[marlyr] == NULL)
00310 mapmappt[marlyr] = malloc (mapwidth*mapheight*sizeof(short int));
00311
00312 memcpy (mapmappt[marlyr], mrpt, (mapwidth*mapheight*sizeof(short int)));
00313
00314 mymarpt = mapmappt[marlyr];
00315 j = 0; for (i=0;i<(mapwidth*mapheight);i++) { if (mymarpt[i]&0xF) j = 1; }
00316 if (j == 0) {
00317 for (i=0;i<(mapwidth*mapheight);i++) {
00318 if (mymarpt[i] >= 0) mymarpt[i] /= 32;
00319 else mymarpt[i] /= 16;
00320 }
00321 }
00322
00323 return 0;
00324 }
00325
00326 int MapLoadMAR (char * mname, int marlyr)
00327 {
00328 int i, j;
00329 short int * mymarpt;
00330 PACKFILE * marfpt;
00331
00332 if (marlyr < 0 || marlyr > 7) return -1;
00333
00334 marfpt = pack_fopen (mname, "rp");
00335 if (marfpt==NULL) { marfpt = pack_fopen (mname, "r");
00336 if (marfpt==NULL) { return -1; } }
00337
00338 if (mapmappt[marlyr] == NULL)
00339 mapmappt[marlyr] = malloc (mapwidth*mapheight*sizeof(short int));
00340
00341 if (pack_fread (mapmappt[marlyr], (mapwidth*mapheight*sizeof(short int)), marfpt) !=
00342 (mapwidth*mapheight*sizeof(short int))) { pack_fclose (marfpt); return -1; }
00343
00344 pack_fclose (marfpt);
00345
00346 mymarpt = mapmappt[marlyr];
00347 j = 0; for (i=0;i<(mapwidth*mapheight);i++) { if (mymarpt[i]&0xF) j = 1; }
00348 if (j == 0) {
00349 for (i=0;i<(mapwidth*mapheight);i++) {
00350 if (mymarpt[i] >= 0) mymarpt[i] /= 32;
00351 else mymarpt[i] /= 16;
00352 }
00353 }
00354
00355 return 0;
00356 }
00357
00358 void MapInitAnims (void)
00359 {
00360 ANISTR * myanpt;
00361 if (mapanimstrpt==NULL) return;
00362 myanpt = (ANISTR *) mapanimstrendpt; myanpt--;
00363 while (myanpt->antype!=-1)
00364 {
00365 if (myanpt->antype==AN_PPFR) myanpt->antype = AN_PPFF;
00366 if (myanpt->antype==AN_PPRF) myanpt->antype = AN_PPRR;
00367 if (myanpt->antype==AN_ONCES) myanpt->antype = AN_ONCE;
00368 if ((myanpt->antype==AN_LOOPR) || (myanpt->antype==AN_PPRR))
00369 {
00370 myanpt->ancuroff = myanpt->anstartoff;
00371 if ((myanpt->anstartoff)!=(myanpt->anendoff)) myanpt->ancuroff=(myanpt->anendoff)-1;
00372 } else {
00373 myanpt->ancuroff = myanpt->anstartoff;
00374 }
00375 myanpt->ancount = myanpt->andelay;
00376 myanpt--;
00377 }
00378 }
00379
00380 void MapUpdateAnims (void)
00381 {
00382 ANISTR * myanpt;
00383
00384 if (mapanimstrpt==NULL) return;
00385 myanpt = (ANISTR *) mapanimstrendpt; myanpt--;
00386 while (myanpt->antype!=-1)
00387 {
00388 if (myanpt->antype!=AN_NONE) { myanpt->ancount--; if (myanpt->ancount<0) {
00389 myanpt->ancount = myanpt->andelay;
00390 if (myanpt->antype==AN_LOOPF)
00391 {
00392 if (myanpt->anstartoff!=myanpt->anendoff) { myanpt->ancuroff++;
00393 if (myanpt->ancuroff==myanpt->anendoff) myanpt->ancuroff = myanpt->anstartoff;
00394 } }
00395 if (myanpt->antype==AN_LOOPR)
00396 {
00397 if (myanpt->anstartoff!=myanpt->anendoff) { myanpt->ancuroff--;
00398 if (myanpt->ancuroff==((myanpt->anstartoff)-1))
00399 myanpt->ancuroff = (myanpt->anendoff)-1;
00400 } }
00401 if (myanpt->antype==AN_ONCE)
00402 {
00403 if (myanpt->anstartoff!=myanpt->anendoff) { myanpt->ancuroff++;
00404 if (myanpt->ancuroff==myanpt->anendoff) { myanpt->antype = AN_ONCES;
00405 myanpt->ancuroff = myanpt->anstartoff; }
00406 } }
00407 if (myanpt->antype==AN_ONCEH)
00408 {
00409 if (myanpt->anstartoff!=myanpt->anendoff) {
00410 if (myanpt->ancuroff!=((myanpt->anendoff)-1)) myanpt->ancuroff++;
00411 } }
00412 if (myanpt->antype==AN_PPFF)
00413 {
00414 if (myanpt->anstartoff!=myanpt->anendoff) { myanpt->ancuroff++;
00415 if (myanpt->ancuroff==myanpt->anendoff) { myanpt->ancuroff -= 2;
00416 myanpt->antype = AN_PPFR;
00417 if (myanpt->ancuroff<myanpt->anstartoff) myanpt->ancuroff++; }
00418 } } else {
00419 if (myanpt->antype==AN_PPFR)
00420 {
00421 if (myanpt->anstartoff!=myanpt->anendoff) { myanpt->ancuroff--;
00422 if (myanpt->ancuroff==((myanpt->anstartoff)-1)) { myanpt->ancuroff += 2;
00423 myanpt->antype = AN_PPFF;
00424 if (myanpt->ancuroff>myanpt->anendoff) myanpt->ancuroff --; }
00425 } } }
00426 if (myanpt->antype==AN_PPRR)
00427 {
00428 if (myanpt->anstartoff!=myanpt->anendoff) { myanpt->ancuroff--;
00429 if (myanpt->ancuroff==((myanpt->anstartoff)-1)) { myanpt->ancuroff += 2;
00430 myanpt->antype = AN_PPRF;
00431 if (myanpt->ancuroff>myanpt->anendoff) myanpt->ancuroff--; }
00432 } } else {
00433 if (myanpt->antype==AN_PPRF)
00434 {
00435 if (myanpt->anstartoff!=myanpt->anendoff) { myanpt->ancuroff++;
00436 if (myanpt->ancuroff==myanpt->anendoff) { myanpt->ancuroff -= 2;
00437 myanpt->antype = AN_PPRR;
00438 if (myanpt->ancuroff<myanpt->anstartoff) myanpt->ancuroff++; }
00439 } } }
00440 } } myanpt--; }
00441 }
00442
00443 void Mapconv8to6pal (unsigned char * palpt)
00444 {
00445 int i;
00446 for (i=0;i<256;i++)
00447 {
00448 mapcmap6bit[i].r=(*(palpt)>>2);
00449 mapcmap6bit[i].g=(*(palpt+1)>>2);
00450 mapcmap6bit[i].b=(*(palpt+2)>>2);
00451 palpt+=3;
00452 }
00453 }
00454
00455 void MapFreeMem (void)
00456 {
00457 int i;
00458 for (i=0;i<8;i++) { if (mapmappt[i]!=NULL) { free (mapmappt[i]); mapmappt[i] = NULL; } }
00459 mappt = NULL;
00460 for (i=0;i<8;i++) { if (mapmaparraypt[i]!=NULL) { free (mapmaparraypt[i]); mapmaparraypt[i] = NULL; } }
00461 maparraypt = NULL;
00462 if (mapcmappt!=NULL) { free (mapcmappt); mapcmappt = NULL; }
00463 if (mapblockgfxpt!=NULL) { free (mapblockgfxpt); mapblockgfxpt = NULL; }
00464 if (mapblockstrpt!=NULL) { free (mapblockstrpt); mapblockstrpt = NULL; }
00465 if (mapanimseqpt!=NULL) { free (mapanimseqpt); mapanimseqpt = NULL; }
00466 if (mapanimstrpt!=NULL) { free (mapanimstrpt); mapanimstrpt = NULL; }
00467 if (abmTiles != NULL) {
00468 i = 0; while (abmTiles[i]!=NULL) { destroy_bitmap (abmTiles[i]); i++; }
00469 free (abmTiles); abmTiles = NULL;
00470 }
00471 mapnovctext[0] = 0;
00472 mapblocksinvidmem = 0; mapblocksinsysmem = 0;
00473 }
00474
00475 void MapSetPal8 (void)
00476 {
00477 if (screen!=NULL) { if ((bitmap_color_depth (screen)==8) && mapdepth == 8) set_palette (mapcmap6bit); }
00478 }
00479
00480 void MapCorrectColours (void)
00481 {
00482
00483
00484
00485 return;
00486 }
00487
00488 void MapRestore (void)
00489 {
00490 int i, j, k;
00491 unsigned char * newgfxpt;
00492
00493 if (mapgfxinbitmaps!=1 || abmTiles == NULL) return;
00494 i = 0; newgfxpt = mapblockgfxpt; while (abmTiles[i]!=NULL) {
00495 acquire_bitmap (abmTiles[i]);
00496 for (k=0;k<mapblockheight;k++) {
00497 for (j=0;j<mapblockwidth;j++) {
00498 switch (mapdepth) {
00499 case 8:
00500 putpixel (abmTiles[i], j, k, *((unsigned char *) newgfxpt));
00501 newgfxpt++;
00502 break;
00503 case 15:
00504 case 16:
00505 putpixel (abmTiles[i], j, k, *((unsigned short int *) newgfxpt));
00506 newgfxpt+=2;
00507 break;
00508 case 24:
00509 putpixel (abmTiles[i], j, k, makecol (newgfxpt[0], newgfxpt[1], newgfxpt[2]));
00510 newgfxpt+=3;
00511 break;
00512 case 32:
00513 putpixel (abmTiles[i], j, k, makecol (newgfxpt[1], newgfxpt[2], newgfxpt[3]));
00514 newgfxpt+=4;
00515 break;
00516 } } }
00517 release_bitmap (abmTiles[i]);
00518 i++;
00519 }
00520 }
00521
00522 int MapRelocate2 (void)
00523 {
00524 int i, j, k;
00525 BLKSTR * myblkstrpt;
00526
00527 unsigned char * newgfxpt, * novcarray;
00528 char ascnum[80];
00529
00530
00531 i = mapnumblockstr;
00532 myblkstrpt = (BLKSTR *) mapblockstrpt;
00533
00534 novcarray = malloc (mapnumblockgfx);
00535 memset (novcarray, 0, mapnumblockgfx);
00536 i = 0; while (mapnovctext[i] != 0) {
00537 j = 0; while (mapnovctext[i] >= '0' && mapnovctext[i] <= '9') {
00538 ascnum[j] = mapnovctext[i];
00539 i++; j++;
00540 }
00541 ascnum[j] = 0;
00542 k = atoi(ascnum);
00543 if (k < 0 || k >= mapnumblockgfx) break;
00544 if (mapnovctext[i] == '-') {
00545 i++;
00546 j = 0; while (mapnovctext[i] >= '0' && mapnovctext[i] <= '9') {
00547 ascnum[j] = mapnovctext[i];
00548 i++; j++;
00549 }
00550 ascnum[j] = 0;
00551 j = atoi(ascnum);
00552 if (j < k || j >= mapnumblockgfx) break;
00553 while (k <= j) {
00554 novcarray[k] = 1; k++;
00555 }
00556 } else {
00557 novcarray[k] = 1;
00558 }
00559 if (mapnovctext[i] == ',') i++;
00560 }
00561
00562 if (abmTiles == NULL) abmTiles = malloc ((sizeof (BITMAP *))*(mapnumblockgfx+2));
00563 abmTiles[0] = NULL;
00564 i = 0; newgfxpt = mapblockgfxpt; while (i<mapnumblockgfx) {
00565 abmTiles[i+1] = NULL;
00566 if (mapgfxinbitmaps==1 && novcarray[i]==0) {
00567 abmTiles[i] = create_video_bitmap (mapblockwidth, mapblockheight);
00568 if (abmTiles[i] != NULL) {
00569 mapblocksinvidmem++;
00570 acquire_bitmap (abmTiles[i]);
00571 } else {
00572 abmTiles[i] = create_bitmap (mapblockwidth, mapblockheight);
00573 if (abmTiles[i] == NULL) { MapFreeMem (); maperror = MER_CVBFAILED ; return -1; }
00574 mapblocksinsysmem++;
00575 }
00576 set_clip_rect(abmTiles[i], 0, 0, 0, 0);
00577 } else {
00578 abmTiles[i] = create_bitmap (mapblockwidth, mapblockheight);
00579 if (abmTiles[i] == NULL) { MapFreeMem (); maperror = MER_CVBFAILED ; return -1; }
00580 mapblocksinsysmem++;
00581 set_clip(abmTiles[i], 0, 0, 0, 0);
00582 }
00583 for (k=0;k<mapblockheight;k++) {
00584 for (j=0;j<mapblockwidth;j++) {
00585 switch (mapdepth) {
00586 case 8:
00587 putpixel (abmTiles[i], j, k, *((unsigned char *) newgfxpt));
00588 newgfxpt++;
00589 break;
00590 case 15:
00591 case 16:
00592 putpixel (abmTiles[i], j, k, *((unsigned short int *) newgfxpt));
00593 newgfxpt+=2;
00594 break;
00595 case 24:
00596 putpixel (abmTiles[i], j, k, makecol (newgfxpt[0], newgfxpt[1], newgfxpt[2]));
00597 newgfxpt+=3;
00598 break;
00599 case 32:
00600 putpixel (abmTiles[i], j, k, makecol (newgfxpt[1], newgfxpt[2], newgfxpt[3]));
00601 newgfxpt+=4;
00602 break;
00603 } } }
00604 if (is_video_bitmap(abmTiles[i])) release_bitmap (abmTiles[i]);
00605 i++;
00606 }
00607 i = mapnumblockstr; while (i) {
00608 ((BITMAP *) myblkstrpt->bgoff) = abmTiles[myblkstrpt->bgoff];
00609 if (myblkstrpt->fgoff!=0)
00610 ((BITMAP *) myblkstrpt->fgoff) = abmTiles[myblkstrpt->fgoff];
00611 if (myblkstrpt->fgoff2!=0)
00612 ((BITMAP *) myblkstrpt->fgoff2) = abmTiles[myblkstrpt->fgoff2];
00613 if (myblkstrpt->fgoff3!=0)
00614 ((BITMAP *) myblkstrpt->fgoff3) = abmTiles[myblkstrpt->fgoff3];
00615 myblkstrpt++; i--;
00616 }
00617
00618 free (novcarray);
00619 return 0;
00620 }
00621
00622 int MapRelocate (void)
00623 {
00624 int i, j, cdepth, pixcol, ccr, ccg, ccb;
00625
00626 unsigned char * oldgfxpt;
00627 unsigned char * mycmappt;
00628 unsigned char * newgfxpt;
00629 unsigned char * newgfx2pt;
00630
00631 if (screen == NULL) { MapFreeMem (); maperror = MER_NOSCREEN; return -1; }
00632 if (!gfx_capabilities&GFX_HW_VRAM_BLIT && mapgfxinbitmaps==1)
00633 { MapFreeMem (); maperror = MER_NOACCELERATION; return -1; }
00634 cdepth = bitmap_color_depth (screen);
00635 oldgfxpt = (unsigned char *) mapblockgfxpt;
00636 newgfxpt = (unsigned char *)
00637 malloc (mapblockwidth*mapblockheight*((mapdepth+1)/8)*mapnumblockgfx*((cdepth+1)/8));
00638 if (newgfxpt==NULL) { MapFreeMem (); maperror = MER_OUTOFMEM; return -1; }
00639 newgfx2pt = newgfxpt;
00640 mycmappt = (unsigned char *) mapcmappt; pixcol = 0;
00641 for (i=0;i<(mapblockwidth*mapblockheight*mapnumblockgfx);i++)
00642 {
00643 switch (mapdepth) {
00644 case 8:
00645 if (cdepth==8) pixcol = (int) *oldgfxpt; else {
00646 j = (*oldgfxpt)*3;
00647 pixcol = makecol (mycmappt[j], mycmappt[j+1], mycmappt[j+2]);
00648 #ifdef RB8BITTOPINK
00649 if (j == 0 && cdepth!=8) pixcol = makecol (255, 0, 255); }
00650 #endif
00651 oldgfxpt++;
00652 break;
00653 case 15:
00654 ccr = ((((int) *oldgfxpt)&0x7C)<<1);
00655 ccg = ((((((int) *oldgfxpt)&0x3)<<3)|(((int) *(oldgfxpt+1))>>5))<<3);
00656 ccb = (((int) *(oldgfxpt+1)&0x1F)<<3);
00657 ccr |= ((ccr>>5)&0x07);
00658 ccg |= ((ccg>>5)&0x07);
00659 ccb |= ((ccb>>5)&0x07);
00660 pixcol = makecol (ccr, ccg, ccb);
00661 if (cdepth==8) { if (ccr == 0xFF && ccg == 0 && ccb == 0xFF) pixcol = 0; }
00662 oldgfxpt += 2;
00663 break;
00664 case 16:
00665 ccr = (((int) *oldgfxpt)&0xF8);
00666 ccg = ((((((int) *oldgfxpt)&0x7)<<3)|(((int) *(oldgfxpt+1))>>5))<<2);
00667 ccb = (((int) *(oldgfxpt+1)&0x1F)<<3);
00668 ccr |= ((ccr>>5)&0x07);
00669 ccg |= ((ccg>>6)&0x03);
00670 ccb |= ((ccb>>5)&0x07);
00671 pixcol = makecol (ccr, ccg, ccb);
00672 if (cdepth==8) { if (ccr == 0xFF && ccg == 0 && ccb == 0xFF) pixcol = 0; }
00673 oldgfxpt += 2;
00674 break;
00675 case 24:
00676 pixcol = makecol (*oldgfxpt, *(oldgfxpt+1), *(oldgfxpt+2));
00677 if (cdepth==8) { if (*oldgfxpt == 0xFF && *(oldgfxpt+1) == 0 &&
00678 *(oldgfxpt+2) == 0xFF) pixcol = 0; }
00679 oldgfxpt += 3;
00680 break;
00681 case 32:
00682 pixcol = makecol (*(oldgfxpt+1), *(oldgfxpt+2), *(oldgfxpt+3));
00683 if (cdepth==8) { if (*(oldgfxpt+1) == 0xFF && *(oldgfxpt+2) == 0 &&
00684 *(oldgfxpt+3) == 0xFF) pixcol = 0; }
00685 oldgfxpt += 4;
00686 break;
00687 }
00688 switch (cdepth) {
00689 case 8:
00690 *newgfxpt = (unsigned char) pixcol;
00691 newgfxpt++;
00692 break;
00693 case 15:
00694 case 16:
00695 *((unsigned short int *) newgfxpt) = (unsigned short int) pixcol;
00696 newgfxpt+=2;
00697 break;
00698 case 24:
00699 *newgfxpt = (unsigned char) (pixcol>>16)&0xFF;
00700 *(newgfxpt+1) = (unsigned char) (pixcol>>8)&0xFF;
00701 *(newgfxpt+2) = (unsigned char) pixcol&0xFF;
00702 newgfxpt+=3;
00703 break;
00704 case 32:
00705 *newgfxpt = 0;
00706 *(newgfxpt+1) = (unsigned char) (pixcol>>16)&0xFF;
00707 *(newgfxpt+2) = (unsigned char) (pixcol>>8)&0xFF;
00708 *(newgfxpt+3) = (unsigned char) pixcol&0xFF;
00709 newgfxpt+=4;
00710 break;
00711 }
00712 }
00713 free (mapblockgfxpt); mapblockgfxpt = (char *) newgfx2pt;
00714
00715 mapdepth = cdepth;
00716
00717 return MapRelocate2 ();
00718 }
00719
00720 static int MapGetchksz (unsigned char * locpt)
00721 {
00722 return ((((int) (locpt[0]))<<24)|(((int) (locpt[1]))<<16)|
00723 (((int) (locpt[2]))<<8)|((int) (locpt[3])));
00724 }
00725
00726 static int MapGetshort (unsigned char * locpt)
00727 {
00728 int rval;
00729
00730 if (mapislsb)
00731 rval = ((((int) (locpt[1]))<<8)|((int) (locpt[0])));
00732 else
00733 rval = ((((int) (locpt[0]))<<8)|((int) (locpt[1])));
00734 if (rval & 0x8000) rval -= 0x10000;
00735 return rval;
00736 }
00737
00738 static int MapGetlong (unsigned char * locpt)
00739 {
00740 if (mapislsb)
00741 return ((((int) (locpt[3]))<<24)|(((int) (locpt[2]))<<16)|
00742 (((int) (locpt[1]))<<8)|((int) (locpt[0])));
00743 else
00744 return ((((int) (locpt[0]))<<24)|(((int) (locpt[1]))<<16)|
00745 (((int) (locpt[2]))<<8)|((int) (locpt[3])));
00746 }
00747
00748 int MapDecodeMPHD (unsigned char * mdatpt)
00749 {
00750 mdatpt += 8;
00751 if (mdatpt[0] > 1) { maperror = MER_MAPTOONEW; return -1; }
00752 if (mdatpt[2] == 1) mapislsb = 1; else mapislsb = 0;
00753
00754 maptype = (int) mdatpt[3];
00755 if (maptype > 3) { maperror = MER_MAPTOONEW; return -1; }
00756 mapwidth = (short) MapGetshort (mdatpt+4);
00757 mapheight = (short) MapGetshort (mdatpt+6);
00758 mapblockwidth = (short) MapGetshort (mdatpt+12);
00759 mapblockheight = (short) MapGetshort (mdatpt+14);
00760 mapdepth = (short) MapGetshort (mdatpt+16);
00761 mapaltdepth = mapdepth;
00762 mapblockstrsize = (short) MapGetshort (mdatpt+18);
00763 mapnumblockstr = (short) MapGetshort (mdatpt+20);
00764 mapnumblockgfx = (short) MapGetshort (mdatpt+22);
00765
00766 if (MapGetchksz (mdatpt-4) > 28) {
00767 mapblockgapx = (int) MapGetshort (mdatpt+28);
00768 mapblockgapy = (int) MapGetshort (mdatpt+30);
00769 mapblockstaggerx = (int) MapGetshort (mdatpt+32);
00770 mapblockstaggery = (int) MapGetshort (mdatpt+34);
00771 } else {
00772 mapblockgapx = (int) mapblockwidth;
00773 mapblockgapy = (int) mapblockheight;
00774 mapblockstaggerx = 0;
00775 mapblockstaggery = 0;
00776 }
00777 if (MapGetchksz (mdatpt-4) > 36) mapclickmask = (short) MapGetshort (mdatpt+36);
00778 else mapclickmask = 0;
00779
00780 return 0;
00781 }
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890 int MapDecodeCMAP (unsigned char * mdatpt)
00891 {
00892 mdatpt += 8;
00893 mapcmappt = (unsigned char *) malloc (MapGetchksz (mdatpt-4));
00894 if (mapcmappt==NULL) { maperror = MER_OUTOFMEM; return -1; }
00895 memcpy (mapcmappt, mdatpt, MapGetchksz (mdatpt-4));
00896 Mapconv8to6pal (mapcmappt);
00897 return 0;
00898 }
00899
00900 int MapDecodeBKDT (unsigned char * mdatpt)
00901 {
00902 int i, j;
00903 BLKSTR * myblkpt;
00904
00905 mdatpt += 8;
00906 mapblockstrpt = malloc (mapnumblockstr*sizeof(BLKSTR));
00907 if (mapblockstrpt==NULL) { maperror = MER_OUTOFMEM; return -1; }
00908
00909 myblkpt = (BLKSTR *) mapblockstrpt;
00910 j = MapGetchksz (mdatpt-4);
00911 i = 0; while (i < (mapnumblockstr*mapblockstrsize)) {
00912 myblkpt->bgoff = (int) MapGetlong (mdatpt+i);
00913 myblkpt->fgoff = (int) MapGetlong (mdatpt+i+4);
00914 myblkpt->fgoff2 = (int) MapGetlong (mdatpt+i+8);
00915 myblkpt->fgoff3 = (int) MapGetlong (mdatpt+i+12);
00916 if (maptype == 0) {
00917 myblkpt->bgoff /= (mapblockwidth*mapblockheight*((mapdepth+1)/8));
00918 myblkpt->fgoff /= (mapblockwidth*mapblockheight*((mapdepth+1)/8));
00919 myblkpt->fgoff2 /= (mapblockwidth*mapblockheight*((mapdepth+1)/8));
00920 myblkpt->fgoff3 /= (mapblockwidth*mapblockheight*((mapdepth+1)/8));
00921 }
00922 myblkpt->user1 = (unsigned int) MapGetlong (mdatpt+i+16);
00923 myblkpt->user2 = (unsigned int) MapGetlong (mdatpt+i+20);
00924 myblkpt->user3 = (unsigned short int) MapGetshort (mdatpt+i+24);
00925 myblkpt->user4 = (unsigned short int) MapGetshort (mdatpt+i+26);
00926 myblkpt->user5 = mdatpt[i+28];
00927 myblkpt->user6 = mdatpt[i+29];
00928 myblkpt->user7 = mdatpt[i+30];
00929 if (mdatpt[i+31]&0x80) myblkpt->unused3 = 1; else myblkpt->unused3 = 0;
00930 if (mdatpt[i+31]&0x40) myblkpt->unused2 = 1; else myblkpt->unused2 = 0;
00931 if (mdatpt[i+31]&0x20) myblkpt->unused1 = 1; else myblkpt->unused1 = 0;
00932 if (mdatpt[i+31]&0x10) myblkpt->trigger = 1; else myblkpt->trigger = 0;
00933 if (mdatpt[i+31]&0x08) myblkpt->br = 1; else myblkpt->br = 0;
00934 if (mdatpt[i+31]&0x04) myblkpt->bl = 1; else myblkpt->bl = 0;
00935 if (mdatpt[i+31]&0x02) myblkpt->tr = 1; else myblkpt->tr = 0;
00936 if (mdatpt[i+31]&0x01) myblkpt->tl = 1; else myblkpt->tl = 0;
00937 i += mapblockstrsize;
00938 myblkpt++;
00939 }
00940 return 0;
00941 }
00942
00943 int MapDecodeANDT (unsigned char * mdatpt)
00944 {
00945 int numani, i, ancksz;
00946 unsigned char * mdatendpt;
00947
00948 mdatpt += 8;
00949 ancksz = MapGetchksz(mdatpt-4);
00950 mdatendpt = mdatpt+ancksz;
00951
00952 numani = 0; while (1) {
00953 mdatendpt -= 16;
00954 numani++;
00955 if (*mdatendpt == 255) break;
00956 }
00957
00958 mapanimseqpt = malloc (((mdatendpt-mdatpt)/4)*sizeof(int));
00959 if (mapanimseqpt == NULL) { maperror = MER_OUTOFMEM; return -1; }
00960 i = 0; while (mdatpt != mdatendpt) {
00961 mapanimseqpt[i] = MapGetlong (mdatpt);
00962 if (maptype == 0) mapanimseqpt[i] /= mapblockstrsize;
00963 mdatpt += 4; i++;
00964 }
00965
00966 mapanimstrpt = malloc (numani*sizeof(ANISTR));
00967 if (mapanimstrpt == NULL) { maperror = MER_OUTOFMEM; return -1; }
00968 mapanimstrendpt = mapanimstrpt;
00969 mapanimstrendpt += numani;
00970
00971 i = 0; while (i<numani) {
00972 mapanimstrpt[i].antype = mdatendpt[0];
00973 mapanimstrpt[i].andelay = mdatendpt[1];
00974 mapanimstrpt[i].ancount = mdatendpt[2];
00975 mapanimstrpt[i].anuser = mdatendpt[3];
00976 if (maptype == 0) {
00977 mapanimstrpt[i].ancuroff = (int) ((MapGetlong (mdatendpt+4)+ancksz)/4);
00978 mapanimstrpt[i].anstartoff = (int) ((MapGetlong (mdatendpt+8)+ancksz)/4);
00979 mapanimstrpt[i].anendoff = (int) ((MapGetlong (mdatendpt+12)+ancksz)/4);
00980 } else {
00981 mapanimstrpt[i].ancuroff = (int) MapGetlong (mdatendpt+4);
00982 mapanimstrpt[i].anstartoff = (int) MapGetlong (mdatendpt+8);
00983 mapanimstrpt[i].anendoff = (int) MapGetlong (mdatendpt+12);
00984 }
00985 mdatendpt += 16; i++;
00986 }
00987
00988 MapInitAnims ();
00989 return 0;
00990 }
00991
00992 int MapDecodeAGFX (unsigned char * mdatpt)
00993 {
00994 if (bitmap_color_depth (screen) > 8) return 0;
00995 if (mapblockgfxpt != NULL) free (mapblockgfxpt);
00996 mapblockgfxpt = malloc (MapGetchksz (mdatpt+4));
00997 if (mapblockgfxpt==NULL) { maperror = MER_OUTOFMEM; return -1; }
00998 memcpy (mapblockgfxpt, mdatpt+8, MapGetchksz(mdatpt+4));
00999 mapaltdepth = 8;
01000 return 0;
01001 }
01002
01003 int MapDecodeBGFX (unsigned char * mdatpt)
01004 {
01005 if (mapblockgfxpt != NULL) return 0;
01006 mapblockgfxpt = malloc (MapGetchksz (mdatpt+4));
01007 if (mapblockgfxpt==NULL) { maperror = MER_OUTOFMEM; return -1; }
01008 memcpy (mapblockgfxpt, mdatpt+8, MapGetchksz(mdatpt+4));
01009 return 0;
01010 }
01011
01012 int MapDecodeNOVC (unsigned char * mdatpt)
01013 {
01014 memset(mapnovctext, 0, 70);
01015 if (MapGetchksz (mdatpt+4) < 70) strncpy(mapnovctext, mdatpt+8, sizeof(mdatpt+8));
01016 return 0;
01017 }
01018
01019 int MapDecodeLayer (unsigned char * mdatpt, int lnum)
01020 {
01021 int i, j, k, l;
01022 short int * mymappt, * mymap2pt;
01023
01024 mapmappt[lnum] = malloc (mapwidth*mapheight*sizeof(short int));
01025 if (mapmappt[lnum] == NULL) { maperror = MER_OUTOFMEM; return -1; }
01026
01027 mdatpt += 8;
01028 mymappt = mapmappt[lnum];
01029 if (maptype == 0) {
01030 for (j=0;j<mapheight;j++) {
01031 for (i=0;i<mapwidth;i++) {
01032 *mymappt = (short int) MapGetshort (mdatpt);
01033 if (*mymappt >= 0) { *mymappt /= mapblockstrsize; }
01034 else { *mymappt /= 16; }
01035 mdatpt+=2; mymappt++;
01036 }
01037 }
01038 } else {
01039 if (maptype == 1) {
01040 for (j=0;j<mapheight;j++) {
01041 for (i=0;i<mapwidth;i++) {
01042 *mymappt = (short int) MapGetshort (mdatpt);
01043 mdatpt+=2; mymappt++;
01044 }
01045 }
01046 } else {
01047 if (maptype == 2) {
01048 for (j=0;j<mapheight;j++) {
01049 for (i=0;i<mapwidth;) {
01050 k = (int) MapGetshort (mdatpt);
01051 mdatpt += 2;
01052 if (k > 0) {
01053 while (k) {
01054 *mymappt = (short int) MapGetshort (mdatpt);
01055 mymappt++; mdatpt += 2;
01056 i++; k--;
01057 }
01058 } else {
01059 if (k < 0) {
01060 l = (int) MapGetshort (mdatpt); mdatpt += 2;
01061 while (k) {
01062 *mymappt = (short int) l;
01063 mymappt++;
01064 i++; k++;
01065 }
01066 } else {
01067 } }
01068 }
01069 }
01070 } else {
01071 if (maptype == 3) {
01072 for (j=0;j<mapheight;j++) {
01073 for (i=0;i<mapwidth;) {
01074 k = (int) MapGetshort (mdatpt);
01075 mdatpt += 2;
01076 if (k > 0) {
01077 while (k) {
01078 *mymappt = (short int) MapGetshort (mdatpt);
01079 mymappt++; mdatpt += 2;
01080 i++; k--;
01081 }
01082 } else {
01083 if (k < 0) {
01084 mymap2pt = mymappt + (int) MapGetshort (mdatpt); mdatpt += 2;
01085 while (k) {
01086 *mymappt = *mymap2pt;
01087 mymappt++; mymap2pt++;
01088 i++; k++;
01089 }
01090 } else {
01091 } }
01092 }
01093 }
01094 } } } }
01095
01096 if (lnum == 0) { mappt = mapmappt[lnum]; }
01097 return 0;
01098 }
01099
01100 int MapDecodeNULL (unsigned char * mdatpt)
01101 {
01102 return 0;
01103 }
01104
01105 int MapRealDecode (PACKFILE * mfpt, unsigned char * mmpt, long int mpfilesize)
01106 {
01107 int chkdn;
01108 unsigned char * fmappospt;
01109 char mphdr[8];
01110
01111 MapFreeMem ();
01112 mpfilesize -= 12;
01113
01114 while (mpfilesize > 0) {
01115
01116 if (mfpt != NULL) {
01117 if (pack_fread (mphdr, 8, mfpt) != 8) {
01118 maperror = MER_MAPLOADERROR;
01119 MapFreeMem ();
01120 return -1;
01121 }
01122 fmappospt = malloc (MapGetchksz(mphdr+4)+8);
01123 if (fmappospt == NULL) {
01124 maperror = MER_OUTOFMEM;
01125 MapFreeMem ();
01126 return -1;
01127 }
01128 memcpy (fmappospt, mphdr, 8);
01129 if (pack_fread (fmappospt+8, MapGetchksz(mphdr+4), mfpt) != MapGetchksz(mphdr+4)) {
01130 maperror = MER_MAPLOADERROR;
01131 MapFreeMem ();
01132 return -1;
01133 }
01134 } else {
01135 fmappospt = mmpt;
01136 mmpt += MapGetchksz(mmpt+4);
01137 mmpt += 8;
01138 }
01139
01140 chkdn = 0;
01141 if (!strncmp (fmappospt, "MPHD", 4)) { chkdn = 1; MapDecodeMPHD (fmappospt); }
01142
01143
01144
01145 if (!strncmp (fmappospt, "CMAP", 4)) { chkdn = 1; MapDecodeCMAP (fmappospt); }
01146 if (!strncmp (fmappospt, "BKDT", 4)) { chkdn = 1; MapDecodeBKDT (fmappospt); }
01147 if (!strncmp (fmappospt, "ANDT", 4)) { chkdn = 1; MapDecodeANDT (fmappospt); }
01148 if (!strncmp (fmappospt, "AGFX", 4)) { chkdn = 1; MapDecodeAGFX (fmappospt); }
01149 if (!strncmp (fmappospt, "BGFX", 4)) { chkdn = 1; MapDecodeBGFX (fmappospt); }
01150 if (!strncmp (fmappospt, "NOVC", 4)) { chkdn = 1; MapDecodeNOVC (fmappospt); }
01151 if (!strncmp (fmappospt, "BODY", 4)) { chkdn = 1; MapDecodeLayer (fmappospt, 0); }
01152 if (!strncmp (fmappospt, "LYR1", 4)) { chkdn = 1; MapDecodeLayer (fmappospt, 1); }
01153 if (!strncmp (fmappospt, "LYR2", 4)) { chkdn = 1; MapDecodeLayer (fmappospt, 2); }
01154 if (!strncmp (fmappospt, "LYR3", 4)) { chkdn = 1; MapDecodeLayer (fmappospt, 3); }
01155 if (!strncmp (fmappospt, "LYR4", 4)) { chkdn = 1; MapDecodeLayer (fmappospt, 4); }
01156 if (!strncmp (fmappospt, "LYR5", 4)) { chkdn = 1; MapDecodeLayer (fmappospt, 5); }
01157 if (!strncmp (fmappospt, "LYR6", 4)) { chkdn = 1; MapDecodeLayer (fmappospt, 6); }
01158 if (!strncmp (fmappospt, "LYR7", 4)) { chkdn = 1; MapDecodeLayer (fmappospt, 7); }
01159 if (!chkdn) MapDecodeNULL (fmappospt);
01160
01161 mpfilesize -= 8;
01162 mpfilesize -= MapGetchksz (fmappospt+4);
01163 if (mfpt != NULL) free (fmappospt);
01164
01165 if (maperror != MER_NONE) { MapFreeMem (); return -1; }
01166 }
01167
01168 mapdepth = mapaltdepth;
01169 return MapRelocate ();
01170 }
01171
01172 int MapRealLoad (char * mname)
01173 {
01174 int mretval;
01175 char idtag[4];
01176 unsigned char tempc;
01177 int mapfilesize = 0;
01178
01179 maperror = MER_NONE;
01180 mapfilept = pack_fopen (mname, "rp");
01181 if (mapfilept==NULL) { mapfilept = pack_fopen (mname, "r");
01182 if (mapfilept==NULL) { maperror = MER_NOOPEN; return -1; } }
01183
01184 maperror = MER_MAPLOADERROR;
01185 if (pack_fread (&idtag[0], 1, mapfilept) == 1) {
01186 if (pack_fread (&idtag[1], 1, mapfilept) == 1) {
01187 if (pack_fread (&idtag[2], 1, mapfilept) == 1) {
01188 if (pack_fread (&idtag[3], 1, mapfilept) == 1) {
01189 if (pack_fread (&tempc, 1, mapfilept) == 1) {
01190 mapfilesize = (((int) tempc)<<24);
01191 if (pack_fread (&tempc, 1, mapfilept) == 1) {
01192 mapfilesize |= (((int) tempc)<<16);
01193 if (pack_fread (&tempc, 1, mapfilept) == 1) {
01194 mapfilesize |= (((int) tempc)<<8);
01195 if (pack_fread (&tempc, 1, mapfilept) == 1) {
01196 mapfilesize |= (((int) tempc));
01197 mapfilesize += 8;
01198 if (!strncmp (idtag, "FORM", 4)) {
01199 if (pack_fread (&idtag[0], 1, mapfilept) == 1) {
01200 if (pack_fread (&idtag[1], 1, mapfilept) == 1) {
01201 if (pack_fread (&idtag[2], 1, mapfilept) == 1) {
01202 if (pack_fread (&idtag[3], 1, mapfilept) == 1) {
01203 if (!strncmp (idtag, "FMAP", 4)) maperror = MER_NONE;
01204 } } } } }
01205 } } } } } } } }
01206
01207 if (maperror != MER_NONE) { pack_fclose (mapfilept); return -1; }
01208
01209 mretval = MapRealDecode (mapfilept, NULL, mapfilesize);
01210 pack_fclose (mapfilept);
01211
01212 return mretval;
01213 }
01214
01215 int MapLoad (char * mapname)
01216 {
01217 mapgfxinbitmaps = 2;
01218 return MapRealLoad (mapname);
01219 }
01220
01221 int MapLoadVRAM (char * mapname)
01222 {
01223 mapgfxinbitmaps = 1;
01224 return MapRealLoad (mapname);
01225 }
01226
01227 int MapLoadABM (char * mapname)
01228 {
01229 mapgfxinbitmaps = 2;
01230 return MapRealLoad (mapname);
01231 }
01232
01233 int MapPreRealDecode (unsigned char * mapmempt)
01234 {
01235 long int maplength;
01236
01237 MapFreeMem ();
01238 maperror = 0;
01239
01240 if (*mapmempt!='F') maperror = MER_MAPLOADERROR;
01241 if (*(mapmempt+1)!='O') maperror = MER_MAPLOADERROR;
01242 if (*(mapmempt+2)!='R') maperror = MER_MAPLOADERROR;
01243 if (*(mapmempt+3)!='M') maperror = MER_MAPLOADERROR;
01244 mapmempt += 4;
01245 maplength = (MapGetchksz (mapmempt))+8;
01246
01247 if (maperror) return -1;
01248 mapmempt += 4;
01249
01250 if (*mapmempt!='F') maperror = MER_MAPLOADERROR;
01251 if (*(mapmempt+1)!='M') maperror = MER_MAPLOADERROR;
01252 if (*(mapmempt+2)!='A') maperror = MER_MAPLOADERROR;
01253 if (*(mapmempt+3)!='P') maperror = MER_MAPLOADERROR;
01254 mapmempt+=4;
01255
01256 if (maperror) return -1;
01257 return MapRealDecode (NULL, mapmempt, maplength);
01258 }
01259
01260 int MapDecode (unsigned char * mapmempt)
01261 {
01262 mapgfxinbitmaps = 2;
01263 return MapPreRealDecode (mapmempt);
01264 }
01265
01266 int MapDecodeVRAM (unsigned char * mapmempt)
01267 {
01268 mapgfxinbitmaps = 1;
01269 return MapPreRealDecode (mapmempt);
01270 }
01271
01272 int MapDecodeABM (unsigned char * mapmempt)
01273 {
01274 mapgfxinbitmaps = 2;
01275 return MapPreRealDecode (mapmempt);
01276 }
01277
01278 BITMAP * MapMakeParallaxBitmap (BITMAP * sourcebm, int style)
01279 {
01280 BITMAP * newbm;
01281
01282 if (mappt == NULL) return NULL;
01283 if (style < 0 || style > 1) return NULL;
01284
01285 if (style) newbm = create_video_bitmap (sourcebm->w+mapblockwidth, sourcebm->h+mapblockheight);
01286 else newbm = create_bitmap (sourcebm->w+mapblockwidth, sourcebm->h+mapblockheight);
01287
01288 if (newbm == NULL) return NULL;
01289 blit (sourcebm, newbm, 0, 0, 0, 0, sourcebm->w, sourcebm->h);
01290 blit (sourcebm, newbm, 0, 0, 0, sourcebm->h, sourcebm->w, mapblockheight);
01291 blit (newbm, newbm, 0, 0, sourcebm->w, 0, mapblockwidth, sourcebm->h+mapblockheight);
01292 return newbm;
01293 }
01294
01295 void MapDrawParallax (BITMAP * mapdestpt, BITMAP * parbm, int mapxo, int mapyo, int mapx, int mapy,
01296 int mapw, int maph)
01297
01298
01299
01300
01301
01302
01303
01304
01305
01306 {
01307 int mycl, mycr, myct, mycb;
01308 int i, i2, j;
01309 int paraxo, paraxo2, parayo;
01310 short int * mymappt, * mymappt2;
01311 BLKSTR * blkdatapt;
01312 ANISTR * myanpt;
01313
01314 if (mapblockstaggerx || mapblockstaggery) return;
01315 mycl = mapdestpt->cl;
01316 mycr = mapdestpt->cr;
01317 myct = mapdestpt->ct;
01318 mycb = mapdestpt->cb;
01319 set_clip_rect (mapdestpt, mapx, mapy, mapx+mapw-1, mapy+maph-1);
01320
01321 mymappt = (short int *) mappt;
01322 mymappt += (mapxo/mapblockwidth)+((mapyo/mapblockheight)*mapwidth);
01323
01324 paraxo = ((mapxo-(mapxo%mapblockwidth))%(parbm->w-mapblockwidth))-((mapxo/2)%(parbm->w-mapblockwidth));
01325 parayo = ((mapyo-(mapyo%mapblockheight))%(parbm->h-mapblockheight))-((mapyo/2)%(parbm->h-mapblockheight));
01326 while (paraxo < 0) paraxo += (parbm->w-mapblockwidth);
01327 while (parayo < 0) parayo += (parbm->h-mapblockheight);
01328
01329 i = mapx-(mapxo%mapblockwidth);
01330 j = mapy-(mapyo%mapblockheight);
01331
01332 i2 = i; paraxo2 = paraxo; mymappt2 = mymappt;
01333 while (j < (mapy+maph)) {
01334 while (i < (mapx+mapw)) {
01335 if (*mymappt>=0) blkdatapt = ((BLKSTR*) mapblockstrpt) + *mymappt;
01336 else { myanpt = mapanimstrendpt + *mymappt;
01337 blkdatapt = ((BLKSTR *) mapblockstrpt) + mapanimseqpt[myanpt->ancuroff]; }
01338 if (blkdatapt->trigger)
01339 blit (parbm, mapdestpt, paraxo, parayo, i, j, mapblockwidth, mapblockheight);
01340 paraxo += mapblockwidth;
01341 if (paraxo >= (parbm->w-mapblockwidth)) paraxo -= (parbm->w-mapblockwidth);
01342 i += mapblockwidth; mymappt++;
01343 }
01344 parayo += mapblockheight;
01345 if (parayo >= (parbm->h-mapblockheight)) parayo -= (parbm->h-mapblockheight);
01346 i = i2; paraxo = paraxo2; mymappt2 += mapwidth; mymappt = mymappt2;
01347 j += mapblockheight;
01348 }
01349 set_clip_rect(mapdestpt, mycl, myct, mycr+1, mycb+1);
01350 }
01351
01352 void MapDrawBG (BITMAP * mapdestpt, int mapxo, int mapyo, int mapx, int mapy,
01353 int mapw, int maph)
01354 {
01355 int i, j, mycl, mycr, myct, mycb, mapvclip, maphclip;
01356 int mbgx, mbgy;
01357 short int *mymappt;
01358 short int *mymap2pt;
01359 BLKSTR *blkdatapt;
01360 ANISTR *myanpt;
01361
01362 if (!mapgfxinbitmaps) {
01363 return;
01364 } else {
01365 mycl = mapdestpt->cl;
01366 mycr = mapdestpt->cr;
01367 myct = mapdestpt->ct;
01368 mycb = mapdestpt->cb;
01369 set_clip_rect(mapdestpt, mapx, mapy, mapx+mapw-1, mapy+maph-1);
01370 mapxo -= mapblockstaggerx;
01371 mapyo -= mapblockstaggery;
01372 mymappt = (short int *) mappt;
01373 if (mapblockstaggerx || mapblockstaggery) {
01374 mymappt += (mapxo/mapblockgapx)+((mapyo/mapblockgapy)*mapwidth*2);
01375 mbgx = mapblockgapx;
01376 mbgy = mapblockgapy;
01377 } else {
01378 mymappt += (mapxo/mapblockgapx)+((mapyo/mapblockgapy)*mapwidth);
01379 mbgx = 0;
01380 mbgy = 0;
01381 }
01382 mapvclip = mapyo%mapblockgapy;
01383 maphclip = mapxo%mapblockgapx;
01384
01385 mymap2pt = mymappt;
01386 for (j=((mapy-mapvclip)-mbgy);j<((mapy+maph));j+=mapblockgapy) {
01387 for (i=((mapx-maphclip)-mbgx);i<((mapx+mapw));i+=mapblockgapx) {
01388 if (*mymappt>=0) blkdatapt = ((BLKSTR*) mapblockstrpt) + *mymappt;
01389 else { myanpt = mapanimstrendpt + *mymappt;
01390 blkdatapt = ((BLKSTR *) mapblockstrpt) + mapanimseqpt[myanpt->ancuroff]; }
01391 if (mapblockstaggerx || mapblockstaggery) {
01392 if (abmTiles[0] != (BITMAP *) blkdatapt->bgoff)
01393 masked_blit ((BITMAP *) blkdatapt->bgoff, mapdestpt, 0, 0, i, j, mapblockwidth, mapblockheight);
01394 } else {
01395 blit ((BITMAP *) blkdatapt->bgoff, mapdestpt, 0, 0, i, j, mapblockwidth, mapblockheight);
01396 }
01397 mymappt++;
01398 }
01399 if (mapblockstaggerx || mapblockstaggery) {
01400 mymap2pt += mapwidth;
01401 mymappt = mymap2pt;
01402 for (i=(((mapx-maphclip)-mbgx)+mapblockstaggerx);i<((mapx+mapw));i+=mapblockgapx) {
01403 if (*mymappt>=0) blkdatapt = ((BLKSTR*) mapblockstrpt) + *mymappt;
01404 else { myanpt = mapanimstrendpt + *mymappt;
01405 blkdatapt = ((BLKSTR *) mapblockstrpt) + mapanimseqpt[myanpt->ancuroff]; }
01406 if (abmTiles[0] != (BITMAP *) blkdatapt->bgoff)
01407 masked_blit ((BITMAP *) blkdatapt->bgoff, mapdestpt, 0, 0, i, j+mapblockstaggery, mapblockwidth, mapblockheight);
01408 mymappt++;
01409 }
01410 }
01411 mymap2pt += mapwidth;
01412 mymappt = mymap2pt;
01413 }
01414
01415 set_clip_rect(mapdestpt, mycl, myct, mycr+1, mycb+1);
01416 }
01417 }
01418
01419 void MapDrawBGT (BITMAP * mapdestpt, int mapxo, int mapyo, int mapx, int mapy,
01420 int mapw, int maph)
01421 {
01422 int i, j, mycl, mycr, myct, mycb, mapvclip, maphclip;
01423 short int *mymappt;
01424 short int *mymap2pt;
01425 BLKSTR *blkdatapt;
01426 ANISTR *myanpt;
01427
01428 if (mapblockstaggerx || mapblockstaggery) {
01429 MapDrawBG (mapdestpt, mapxo, mapyo, mapx, mapy, mapw, maph);
01430 return;
01431 }
01432 if (!mapgfxinbitmaps) {
01433 return;
01434 } else {
01435 mycl = mapdestpt->cl;
01436 mycr = mapdestpt->cr;
01437 myct = mapdestpt->ct;
01438 mycb = mapdestpt->cb;
01439 set_clip_rect(mapdestpt, mapx, mapy, mapx+mapw-1, mapy+maph-1);
01440 mymappt = (short int *) mappt;
01441 mymappt += (mapxo/mapblockgapx)+((mapyo/mapblockgapy)*mapwidth);
01442 mapvclip = mapyo%mapblockgapy;
01443 maphclip = mapxo%mapblockgapx;
01444
01445 mymap2pt = mymappt;
01446 for (j=(mapy-mapvclip);j<((mapy+maph));j+=mapblockgapy) {
01447 for (i=(mapx-maphclip);i<((mapx+mapw));i+=mapblockgapx) {
01448 if (*mymappt>=0) blkdatapt = ((BLKSTR*) mapblockstrpt) + *mymappt;
01449 else { myanpt = mapanimstrendpt + *mymappt;
01450 blkdatapt = ((BLKSTR *) mapblockstrpt) + mapanimseqpt[myanpt->ancuroff]; }
01451 if (blkdatapt->trigger) {
01452 if (abmTiles[0] != (BITMAP *) blkdatapt->bgoff)
01453 masked_blit ((BITMAP *) blkdatapt->bgoff, mapdestpt, 0, 0, i, j, mapblockwidth, mapblockheight);
01454 } else {
01455 blit ((BITMAP *) blkdatapt->bgoff, mapdestpt, 0, 0, i, j, mapblockwidth, mapblockheight);
01456 }
01457 mymappt++;
01458 }
01459 mymap2pt += mapwidth;
01460 mymappt = mymap2pt;
01461 }
01462
01463 set_clip_rect(mapdestpt, mycl, myct, mycr+1, mycb+1);
01464 }
01465 }
01466
01467 void MapDrawFG (BITMAP * mapdestpt, int mapxo, int mapyo, int mapx, int mapy,
01468 int mapw, int maph, int mapfg)
01469 {
01470 int i, j, mycl, mycr, myct, mycb, mapvclip, maphclip;
01471 int mbgx, mbgy;
01472 short int *mymappt;
01473 short int *mymap2pt;
01474 BLKSTR *blkdatapt;
01475 ANISTR *myanpt;
01476 BITMAP *mapgfxpt;
01477
01478 if (!mapgfxinbitmaps) {
01479 return;
01480 } else {
01481 mycl = mapdestpt->cl;
01482 mycr = mapdestpt->cr;
01483 myct = mapdestpt->ct;
01484 mycb = mapdestpt->cb;
01485 set_clip_rect(mapdestpt, mapx, mapy, mapx+mapw-1, mapy+maph-1);
01486 mapxo -= mapblockstaggerx;
01487 mapyo -= mapblockstaggery;
01488 mymappt = (short int *) mappt;
01489 if (mapblockstaggerx || mapblockstaggery) {
01490 mymappt += (mapxo/mapblockgapx)+((mapyo/mapblockgapy)*mapwidth*2);
01491 mbgx = mapblockgapx;
01492 mbgy = mapblockgapy;
01493 } else {
01494 mymappt += (mapxo/mapblockgapx)+((mapyo/mapblockgapy)*mapwidth);
01495 mbgx = 0;
01496 mbgy = 0;
01497 }
01498 mapvclip = mapyo%mapblockgapy;
01499 maphclip = mapxo%mapblockgapx;
01500
01501 mymap2pt = mymappt;
01502 for (j=((mapy-mapvclip)-mbgy);j<((mapy+maph));j+=mapblockgapy) {
01503 for (i=((mapx-maphclip)-mbgx);i<((mapx+mapw));i+=mapblockgapx) {
01504 if (*mymappt>=0) blkdatapt = ((BLKSTR*) mapblockstrpt) + *mymappt;
01505 else { myanpt = mapanimstrendpt + *mymappt;
01506 blkdatapt = ((BLKSTR *) mapblockstrpt) + mapanimseqpt[myanpt->ancuroff]; }
01507 if (!mapfg) mapgfxpt = (BITMAP *) blkdatapt->fgoff;
01508 else if (mapfg == 1) mapgfxpt = (BITMAP *) blkdatapt->fgoff2;
01509 else mapgfxpt = (BITMAP *) blkdatapt->fgoff3;
01510 if (((int)mapgfxpt) != 0)
01511 masked_blit (mapgfxpt, mapdestpt, 0, 0, i, j, mapblockwidth, mapblockheight);
01512 mymappt++;
01513 }
01514 if (mapblockstaggerx || mapblockstaggery) {
01515 mymap2pt += mapwidth;
01516 mymappt = mymap2pt;
01517 for (i=(((mapx-maphclip)-mbgx)+mapblockstaggerx);i<((mapx+mapw));i+=mapblockgapx) {
01518 if (*mymappt>=0) blkdatapt = ((BLKSTR*) mapblockstrpt) + *mymappt;
01519 else { myanpt = mapanimstrendpt + *mymappt;
01520 blkdatapt = ((BLKSTR *) mapblockstrpt) + mapanimseqpt[myanpt->ancuroff]; }
01521 if (!mapfg) mapgfxpt = (BITMAP *) blkdatapt->fgoff;
01522 else if (mapfg == 1) mapgfxpt = (BITMAP *) blkdatapt->fgoff2;
01523 else mapgfxpt = (BITMAP *) blkdatapt->fgoff3;
01524 if (((int)mapgfxpt) != 0)
01525 masked_blit (mapgfxpt, mapdestpt, 0, 0, i, j+mapblockstaggery, mapblockwidth, mapblockheight);
01526 mymappt++;
01527 }
01528 }
01529 mymap2pt += mapwidth;
01530 mymappt = mymap2pt;
01531 }
01532
01533 set_clip_rect(mapdestpt, mycl, myct, mycr+1, mycb+1);
01534 }
01535 }
01536
01537 void MapDrawRow (BITMAP * mapdestpt, int mapxo, int mapyo, int mapx, int mapy,
01538 int mapw, int maph, int maprw, void (*cellcall) (int cx, int cy, int dx, int dy))
01539 {
01540 int i, j, mycl, mycr, myct, mycb, mapvclip, maphclip;
01541 int mbgx, mbgy, bfield, bysub;
01542 int cx, cy;
01543 short int *mymappt;
01544 short int *mymap2pt;
01545 BLKSTR *blkdatapt;
01546 ANISTR *myanpt;
01547 BITMAP *mapgfxpt;
01548
01549 if (((mapyo/mapblockgapy)+maprw) >= mapheight) return;
01550 if (mapblockstaggerx || mapblockstaggery) {
01551 mapxo -= mapblockstaggerx;
01552 mapyo -= mapblockstaggery;
01553 if ((((mapyo/mapblockgapy)*2)+maprw) >= (mapheight-1)) return;
01554 }
01555 if (!mapgfxinbitmaps) {
01556 return;
01557 } else {
01558 mycl = mapdestpt->cl;
01559 mycr = mapdestpt->cr;
01560 myct = mapdestpt->ct;
01561 mycb = mapdestpt->cb;
01562 set_clip_rect(mapdestpt, mapx, mapy, mapx+mapw-1, mapy+maph-1);
01563 mymappt = (short int *) mappt;
01564 mapvclip = mapyo%mapblockgapy;
01565 maphclip = mapxo%mapblockgapx;
01566 j = (mapy-mapvclip); i = 0;
01567 if (mapblockstaggerx || mapblockstaggery) {
01568 cx = mapxo/mapblockgapx;
01569 cy = (((mapyo/mapblockgapy)*2)+maprw);
01570 mymappt += (cx)+(cy*mapwidth);
01571 mbgx = mapblockgapx;
01572 mbgy = mapblockgapy;
01573 j -= mbgy;
01574 j += ((maprw/2)*mapblockgapy);
01575 if (maprw&1) { j += mapblockstaggery; i = mapblockstaggerx; }
01576 } else {
01577 cx = mapxo/mapblockgapx;
01578 cy = ((mapyo/mapblockgapy)+maprw);
01579 mymappt += (cx)+(cy*mapwidth);
01580 mbgx = 0;
01581 mbgy = 0;
01582 j += (maprw*mapblockgapy);
01583 }
01584
01585 mymap2pt = mymappt;
01586 for (i+=((mapx-maphclip)-mbgx);i<((mapx+mapw));i+=mapblockgapx) {
01587 if (cellcall != NULL) cellcall (cx, cy, i, j);
01588 if (*mymappt>=0) blkdatapt = ((BLKSTR*) mapblockstrpt) + *mymappt;
01589 else { myanpt = mapanimstrendpt + *mymappt;
01590 blkdatapt = ((BLKSTR *) mapblockstrpt) + mapanimseqpt[myanpt->ancuroff]; }
01591 bfield = 1; bysub = 0;
01592 do {
01593 if (!bfield) blkdatapt++;
01594 for (;bfield<4;bfield++) {
01595 switch (bfield) {
01596 case 0: mapgfxpt = (BITMAP *) blkdatapt->bgoff; break;
01597 case 1: mapgfxpt = (BITMAP *) blkdatapt->fgoff; break;
01598 case 2: mapgfxpt = (BITMAP *) blkdatapt->fgoff2; break;
01599 default:
01600 case 3: mapgfxpt = (BITMAP *) blkdatapt->fgoff3; break;
01601 }
01602 if (((int)mapgfxpt) != 0) {
01603 if (blkdatapt->unused2 && !blkdatapt->unused3) {
01604 masked_blit (mapgfxpt, mapdestpt, 0, 0, i, j-bysub, mapblockwidth/2, mapblockheight);
01605 } else {
01606 if (!blkdatapt->unused2 && blkdatapt->unused3) {
01607 masked_blit (mapgfxpt, mapdestpt, mapblockwidth/2, 0, i, j-bysub, mapblockwidth/2, mapblockheight);
01608 } else {
01609 masked_blit (mapgfxpt, mapdestpt, 0, 0, i, j-bysub, mapblockwidth, mapblockheight);
01610 }
01611 }
01612 }
01613 bysub += mapblockheight;
01614 }
01615 bfield = 0;
01616 } while (blkdatapt->unused1);
01617 mymappt++; cx++;
01618 }
01619
01620 set_clip_rect(mapdestpt, mycl, myct, mycr+1, mycb+1);
01621 }
01622 }