home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / acksrc.zip / MAPEDIT.C < prev    next >
Text File  |  1993-06-20  |  26KB  |  1,298 lines

  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <dos.h>
  5. #include <mem.h>
  6. #include <alloc.h>
  7. #include <io.h>
  8. #include <fcntl.h>
  9. #include <time.h>
  10. #include <string.h>
  11. #include <sys\stat.h>
  12. #include "ack3d.h"
  13. #include "mapedit.h"
  14.  
  15.     RECT        HotSpots[] = {
  16.             1,25,161,153,    /* 0  Main grid */
  17.             163,23,172,32,    /* 1  Up arrow    */
  18.             163,146,172,155,    /* 2  Dn arrow    */
  19.             0,155,11,164,    /* 3  Lt arrow    */
  20.             152,155,163,164,    /* 4  Rt arrow    */
  21.             186,2,196,130,    /* 5  1st wall    */
  22.             198,2,208,130,    /* 6  2nd wall    */
  23.             210,2,220,130,    /* 7  3rd wall    */
  24.             222,2,232,130,    /* 8  4th wall    */
  25.             259,2,269,130,    /* 9  1st obj    */
  26.             271,2,281,130,    /* 10 2nd obj    */
  27.             283,2,293,130,    /* 11 3rd obj    */
  28.             295,2,305,130,    /* 12 4th obj    */
  29.             4,173,37,185,    /* 13 Load    */
  30.             41,173,75,185,    /* 14 Save    */
  31.             134,173,163,185,    /* 15 Exit    */
  32.             241,22,251,30,    /* 16 blank sq    */
  33.             243,2,249,8,    /* 17 Pass box    */
  34.             80,173,112,185,    /* 18 New    */
  35.             241,41,251,49,    /* 19 Start box */
  36.             241,62,251,70,    /* 20 Up box    */
  37.             241,83,251,91,    /* 21 Dn box    */
  38.             241,103,251,111,    /* 22 Goal box    */
  39.             -1,-1,-1,-1
  40.             };
  41.  
  42.     unsigned    char far    *ScreenBuffer;
  43.  
  44.     unsigned    char        Grid[4096];
  45.  
  46.     unsigned    char        *bMaps[256];
  47.     unsigned    char        *oMaps[256];
  48.         char        GridFile[128];
  49.         char        PalFile[128];
  50.     unsigned    char        Palette[768];
  51.  
  52.         int        GridX;
  53.         int        GridY;
  54.         int        CurrentType;
  55.         int        CurrentSquare;
  56.         int        PassFlag;
  57.         int        ModifiedFlag;
  58.         int        ErrorCode;
  59.  
  60.     unsigned    char        *WallSquares[256];
  61.     unsigned    char        *ObjSquares[256];
  62.  
  63.     unsigned    char        ObjbmNum[256];
  64.  
  65.  
  66.     RECT    WallCoords[] = {
  67.             186,2,196,130,    /* 1st wall  */
  68.             198,2,208,130,    /* 2nd wall  */
  69.             210,2,220,130,    /* 3rd wall  */
  70.             222,2,232,130    /* 4th wall  */
  71.             };
  72.  
  73.     RECT    ObjCoords[] = {
  74.             259,2,269,130,    /* 1st obj   */
  75.             271,2,281,130,    /* 2nd obj   */
  76.             283,2,293,130,    /* 3rd obj   */
  77.             295,2,305,130    /* 4th obj   */
  78.         };
  79.  
  80.     RECT    SpecialActions[] = {
  81.             241,41,251,49,    /* Start box */
  82.             241,62,251,70,    /* Up box    */
  83.             241,83,251,91,    /* Dn box    */
  84.             241,103,251,111    /* Goal box  */
  85.         };
  86.  
  87. #define TOTAL_SPECIAL    4
  88.  
  89.  
  90.         unsigned char Pal0[] = {0,0,0,0,0,0x2A,0,0x2A,0};
  91.         unsigned char Pal11[] = {0x15,0x3f,0x3f,0x3f,0x15,0x15,
  92.                      0x3f,0x15,0x3f};
  93.         unsigned char Pal15[] = {0x3f,0x3f,0x3f,0x3b,0x3b,0x3b,0x37,
  94.                      0x37,0x37};
  95.  
  96.  
  97.  
  98.     BOXES    MsgBox[] = {
  99.         82,48,"mapbox1",1,1,136,81,161,93,0,0,0,0,0,0,0,0,0,0,0,0,
  100.         61,49,"mapbox2",1,2,101,87,132,101,166,87,197,101,0,0,0,0,0,0,0,0,
  101.         61,49,"mapbox3",1,2,101,87,132,101,166,87,197,101,0,0,0,0,0,0,0,0,
  102.         97,54,"mapbox4",0,1,130,88,151,100,0,0,0,0,0,0,0,0,0,0,0,0,
  103.         82,48,"mapbox5",1,1,136,81,161,93,0,0,0,0,0,0,0,0,0,0,0,0,
  104.         82,48,"mapbox6",1,1,136,81,161,93,0,0,0,0,0,0,0,0,0,0,0,0,
  105.         };
  106.  
  107. unsigned char far * Readiff(char *picname);
  108.  
  109. /****************************************************************************
  110. **                                       **
  111. ****************************************************************************/
  112. void SoundBeep(void)
  113. {
  114. sound(440);
  115. delay(50);
  116. nosound();
  117. }
  118.  
  119. /****************************************************************************
  120. **                                       **
  121. ****************************************************************************/
  122. void ScreenToBuffer(int x,int y,int wt,int ht,unsigned char *Buffer)
  123. {
  124.     int        row;
  125.     unsigned char *Video;
  126.  
  127. Video = MK_FP(0xA000,(y * 320) + x);
  128.  
  129. for (row = 0; row < ht; row++)
  130.     {
  131.     memmove(Buffer,Video,wt);
  132.     Buffer += wt;
  133.     Video += 320;
  134.     }
  135.  
  136. }
  137.  
  138. /****************************************************************************
  139. **                                       **
  140. ****************************************************************************/
  141. void BufferToScreen(int x,int y,int wt,int ht,unsigned char *Buffer)
  142. {
  143.     int        row;
  144.     unsigned char *Video;
  145.  
  146. Video = MK_FP(0xA000,(y * 320) + x);
  147.  
  148. for (row = 0; row < ht; row++)
  149.     {
  150.     memmove(Video,Buffer,wt);
  151.     Buffer += wt;
  152.     Video += 320;
  153.     }
  154.  
  155. }
  156.  
  157.  
  158.  
  159. /****************************************************************************
  160. **                                       **
  161. ****************************************************************************/
  162. int ShowMsgBox(int Number)
  163. {
  164.     int        i,handle,len,result;
  165.     int        mx,my,mButton;
  166.     int        x,y,wt,ht;
  167.     char    *BackBuffer;
  168.     char    *BoxBuffer;
  169.     char    size[4];
  170.     char    fName[14];
  171.  
  172. strcpy(fName,MsgBox[Number].FileName);
  173. AddExtent(fName,".bbm");
  174.  
  175. BoxBuffer = Readiff(fName);
  176.  
  177. if (BoxBuffer == NULL)
  178.     return(0);
  179.  
  180. wt = (*(int *)BoxBuffer);
  181. ht = (*(int *)&BoxBuffer[2]);
  182. len = wt * ht;
  183.  
  184. BackBuffer = malloc(len);
  185. if (BackBuffer == NULL)
  186.     {
  187.     free(BoxBuffer);
  188.     return(0);
  189.     }
  190.  
  191. #if 0
  192. handle = open(fName,O_RDWR|O_BINARY);
  193. if (handle < 1)
  194.     return(0);
  195.  
  196. read(handle,size,4);
  197.  
  198. ht = (*(int *)size);
  199. wt = (*(int *)&size[2]);
  200. len = wt * ht;
  201.  
  202. BackBuffer = malloc(len);
  203. BoxBuffer = malloc(len);
  204.  
  205. if (BackBuffer == NULL || BoxBuffer == NULL)
  206.     {
  207.     close(handle);
  208.     return(0);
  209.     }
  210.  
  211. read(handle,BoxBuffer,len);
  212. close(handle);
  213. #endif
  214.  
  215. x = MsgBox[Number].BoxX;
  216. y = MsgBox[Number].BoxY;
  217.  
  218. mouse_hide_cursor();
  219. ScreenToBuffer(x,y,wt,ht,BackBuffer);
  220. BufferToScreen(x,y,wt,ht,&BoxBuffer[4]);
  221. mouse_show_cursor();
  222.  
  223. if (MsgBox[Number].DoBeep)
  224.     SoundBeep();
  225.  
  226. len = 0;
  227.  
  228. while (!len)
  229.     {
  230.     if (inkey() == 0x1C0D)
  231.     break;
  232.  
  233.     mouse_read_cursor(&mButton,&my,&mx);
  234.  
  235.     if (mButton & 1)
  236.     {
  237.     for (i = 0; i < MsgBox[Number].NumButtons; i++)
  238.         {
  239.         if (mx >= MsgBox[Number].ButtonCoords[i].x &&
  240.         mx <= MsgBox[Number].ButtonCoords[i].x1 &&
  241.         my >= MsgBox[Number].ButtonCoords[i].y &&
  242.         my <= MsgBox[Number].ButtonCoords[i].y1)
  243.         {
  244.         result = i;
  245.         mouse_released();
  246.         len = 1;
  247.         break;
  248.         }
  249.         }
  250.  
  251.     }
  252.  
  253.     }
  254.  
  255. mouse_hide_cursor();
  256. BufferToScreen(x,y,wt,ht,BackBuffer);
  257. mouse_show_cursor();
  258.  
  259. free(BackBuffer);
  260. free(BoxBuffer);
  261.  
  262. return(result);
  263. }
  264.  
  265.  
  266.  
  267. /****************************************************************************
  268. **                                       **
  269. ****************************************************************************/
  270. int GetAction(int mx,int my)
  271. {
  272.     int        i = 0;
  273.  
  274. while (1)
  275.     {
  276.     if (HotSpots[i].x < 0)
  277.     return(-1);
  278.  
  279.     if (mx >= HotSpots[i].x &&
  280.     mx <= HotSpots[i].x1 &&
  281.     my >= HotSpots[i].y &&
  282.     my <= HotSpots[i].y1)
  283.     break;
  284.  
  285.     i++;
  286.     }
  287.  
  288. return(i);
  289. }
  290.  
  291. /****************************************************************************
  292. **                                       **
  293. ****************************************************************************/
  294. void ShowScreen(void)
  295. {
  296.     unsigned char far *Video;
  297.  
  298. Video = MK_FP(0xA000,0);
  299. memmove(Video,ScreenBuffer,64000);
  300.  
  301. }
  302.  
  303. /****************************************************************************
  304. **                                       **
  305. ****************************************************************************/
  306. void GetBufferSquare(int x,int y,unsigned char *buffer)
  307. {
  308.     int        row;
  309.     unsigned char *src;
  310.  
  311. src = ScreenBuffer + ((y * 320) + x);
  312.  
  313. for (row = 0; row < 8; row++)
  314.     {
  315.     memmove(buffer,src,10);
  316.     buffer += 10;
  317.     src += 320;
  318.     }
  319. }
  320.  
  321.  
  322. /****************************************************************************
  323. **                                       **
  324. ****************************************************************************/
  325. int BuildSquares(void)
  326. {
  327.         int    i,j,x,y,num;
  328.     unsigned    char    *Square;
  329.  
  330. Square = malloc(80);
  331. if (Square == NULL)
  332.     {
  333.     ErrorCode = ERR_NOMEMORY;
  334.     return(-1);
  335.     }
  336.  
  337. WallSquares[0] = Square;
  338. GetBufferSquare(1,25,Square);
  339.  
  340. num = 1;
  341. for (i = 0; i < 4; i++)
  342.     {
  343.     x = WallCoords[i].x;
  344.     y = WallCoords[i].y;
  345.  
  346.     for (j = 0; j < 16; j++)
  347.     {
  348.     Square = malloc(80);
  349.     if (Square == NULL)
  350.         return(-1);
  351.     WallSquares[num++] = Square;
  352.     GetBufferSquare(x,y,Square);
  353.     y += 8;
  354.     }
  355.  
  356.     }
  357.  
  358. num = 1;
  359. for (i = 0; i < 4; i++)
  360.     {
  361.     x = ObjCoords[i].x;
  362.     y = ObjCoords[i].y;
  363.  
  364.     for (j = 0; j < 16; j++)
  365.     {
  366.     Square = malloc(80);
  367.     if (Square == NULL)
  368.         return(-1);
  369.     ObjSquares[num++] = Square;
  370.     GetBufferSquare(x,y,Square);
  371.     y += 8;
  372.     }
  373.  
  374.     }
  375.  
  376. num = MAP_STARTCODE;
  377. for (i = 0; i < TOTAL_SPECIAL; i++)
  378.     {
  379.     Square = malloc(80);
  380.     if (Square == NULL)
  381.     {
  382.     ErrorCode = ERR_NOMEMORY;
  383.     return(-1);
  384.     }
  385.     WallSquares[num++] = Square;
  386.     GetBufferSquare(SpecialActions[i].x,SpecialActions[i].y,Square);
  387.     }
  388.  
  389. return(0);
  390. }
  391.  
  392.  
  393. /****************************************************************************
  394. **                                       **
  395. ****************************************************************************/
  396. void PutSquare(int x,int y,unsigned char *buffer)
  397. {
  398.     int        row;
  399.     unsigned char *Video;
  400.  
  401. Video = MK_FP(0xA000,(y * 320) + x);
  402.  
  403. for (row = 0; row < 8; row++)
  404.     {
  405.     memmove(Video,buffer,10);
  406.     buffer += 10;
  407.     Video += 320;
  408.     }
  409. }
  410.  
  411. /****************************************************************************
  412. **                                       **
  413. ****************************************************************************/
  414. void ShowPassBox(void)
  415. {
  416.          char   color;
  417.     unsigned char   *Video;
  418.  
  419. if (PassFlag)
  420.     color = 0;
  421. else
  422.     color = 15;
  423.  
  424. mouse_hide_cursor();
  425. Video = MK_FP(0xA000,(PASS_Y * 320) + PASS_X);
  426.  
  427. *Video = color;
  428. Video[4] = color;
  429. Video += 320;
  430. Video[1] = color;
  431. Video[3] = color;
  432. Video += 320;
  433. Video[2] = color;
  434. Video += 320;
  435. Video[1] = color;
  436. Video[3] = color;
  437. Video += 320;
  438. *Video = color;
  439. Video[4] = color;
  440. mouse_show_cursor();
  441. }
  442.  
  443. /****************************************************************************
  444. **                                       **
  445. ****************************************************************************/
  446. void ShowGrid(void)
  447. {
  448.     int        row,col,x,y;
  449.     int        VidX,VidY;
  450.     int        pos,type,coord;
  451.     unsigned char ch;
  452.     unsigned char *Video;
  453.     unsigned char Square[90];
  454.  
  455. pos = (GridY * 64) + GridX;
  456. VidY = 25;
  457. mouse_hide_cursor();
  458.  
  459. for (row = 0; row < 16; row++)
  460.     {
  461.     VidX = 1;
  462.     for (col = 0; col < 16; col++)
  463.     {
  464.     ch = Grid[pos + col];
  465.  
  466.     if (ch < 0x80 || ch >= MAP_STARTCODE)
  467.         PutSquare(VidX,VidY,WallSquares[ch]);
  468.     else
  469.         PutSquare(VidX,VidY,ObjSquares[ch & 0x3F]);
  470.  
  471.     if (ch & 0x40 && ch < MAP_STARTCODE)
  472.         {
  473.         Video = MK_FP(0xA000,((VidY+3) * 320) + VidX + 1);
  474.         *Video = 0;
  475.         }
  476.  
  477.     VidX += 10;
  478.     }
  479.  
  480.     VidY += 8;
  481.     pos += 64;
  482.     }
  483. mouse_show_cursor();
  484. }
  485.  
  486. /****************************************************************************
  487. **                                       **
  488. ****************************************************************************/
  489. int LoadScreen(char *fName)
  490. {
  491.     int        handle,len;
  492.  
  493. #if 0
  494. handle = open(fName,O_RDWR|O_BINARY);
  495. if (handle < 1)
  496.     {
  497.     printf("Unable to find screen file: %s\n",fName);
  498.     return(-1);
  499.     }
  500.  
  501. ScreenBuffer = malloc(64000);
  502. if (ScreenBuffer == NULL)
  503.     {
  504.     printf("Unable to get screen memory.\n");
  505.     close(handle);
  506.     return(-2);
  507.     }
  508.  
  509. read(handle,ScreenBuffer,4);    /* Skip width and height */
  510.  
  511. len = read(handle,ScreenBuffer,32000);
  512. if (len != 32000)
  513.     {
  514.     printf("Invalid screen file: %s\n",fName);
  515.     close(handle);
  516.     return(-3);
  517.     }
  518.  
  519. if (read(handle,&ScreenBuffer[32000],32000) != 32000)
  520.     {
  521.     printf("Invalid screen file: %s\n",fName);
  522.     close(handle);
  523.     return(-3);
  524.     }
  525.  
  526. close(handle);
  527. #endif
  528. ScreenBuffer = Readiff(fName);
  529. if (ScreenBuffer == NULL)
  530.     {
  531.     printf("Error loading screen file: %s\n",fName);
  532.     ErrorCode = ERR_NOMEMORY;
  533.     return(-1);
  534.     }
  535.  
  536. memmove(ScreenBuffer,&ScreenBuffer[4],64000);
  537.  
  538. return(0);
  539. }
  540.  
  541. /****************************************************************************
  542. **                                       **
  543. ****************************************************************************/
  544. char *GetExtent(char *s)
  545. {
  546.     char    *e;
  547.  
  548. e = strchr(s,'.');
  549. if (e == NULL)
  550.     return(s);
  551. e++;
  552.  
  553. return(e);
  554. }
  555.  
  556. /****************************************************************************
  557. **                                       **
  558. ****************************************************************************/
  559. int LoadBitmap(int BitmapNumber,char *BitmapName,int BitmapType)
  560. {
  561.     int        handle;
  562.     int        x,y;
  563.     int        sPos,dPos;
  564.     unsigned char ch;
  565.     unsigned char *bmp;
  566.  
  567. if (!(stricmp(GetExtent(BitmapName),"BBM")))
  568.     {
  569.     bmp = Readiff(BitmapName);
  570.     if (bmp == NULL)
  571.     {
  572.     ErrorCode = ERR_NOMEMORY;
  573.     return(-1);
  574.     }
  575.  
  576.     if (BitmapType == TYPE_WALL)
  577.     bMaps[BitmapNumber] = bmp;
  578.  
  579.     if (BitmapType == TYPE_OBJECT)
  580.     oMaps[BitmapNumber] = bmp;
  581.  
  582.     memmove(bmp,&bmp[4],4096);
  583.     return(0);
  584.     }
  585.  
  586.  
  587. bmp = farmalloc(4096);
  588. if (bmp == NULL)
  589.     {
  590.     ErrorCode = ERR_NOMEMORY;
  591.     return(-1);
  592.     }
  593.  
  594. if (BitmapType == TYPE_WALL)
  595.     bMaps[BitmapNumber] = bmp;
  596.  
  597. if (BitmapType == TYPE_OBJECT)
  598.     oMaps[BitmapNumber] = bmp;
  599.  
  600.  
  601. handle = open(BitmapName,O_RDWR|O_BINARY);
  602. if (handle < 1)
  603.     {
  604.     farfree(bmp);
  605.     ErrorCode = ERR_BADFILE;
  606.     return(-1);
  607.     }
  608.  
  609. read(handle,bmp,4);        /* Skip width and height for now */
  610. read(handle,bmp,4096);
  611. close(handle);
  612.  
  613. return(0);
  614. }
  615.  
  616. /****************************************************************************
  617. **                                       **
  618. ****************************************************************************/
  619. char *StripEndOfLine(char *s)
  620. {
  621.     int        len;
  622.     char    ch;
  623.  
  624. len = strlen(s);
  625.  
  626. while (--len >= 0)
  627.     {
  628.     ch = s[len];
  629.     if (ch != ' ' && ch != ';' && ch != '\t' && ch != 13 && ch != 10)
  630.     break;
  631.  
  632.     s[len] = '\0';
  633.     }
  634.  
  635. return(s);
  636. }
  637.  
  638. /****************************************************************************
  639. **                                       **
  640. ****************************************************************************/
  641. char *SkipSpaces(char *s)
  642. {
  643.  
  644. while (*s == ' ' || *s == '\t' || *s == ',')
  645.     strcpy(s,&s[1]);
  646.  
  647. return(s);
  648. }
  649.  
  650. /****************************************************************************
  651. **                                       **
  652. ****************************************************************************/
  653. char *AddExtent(char *s,char *ext)
  654. {
  655. if (strchr(s,'.') == NULL)
  656.     strcat(s,ext);
  657.  
  658. return(s);
  659. }
  660.  
  661. /****************************************************************************
  662. **                                       **
  663. ****************************************************************************/
  664. char *CopyToComma(char *dest,char *src)
  665. {
  666.     char    ch;
  667.  
  668. while (*src)
  669.     {
  670.     ch = *src++;
  671.     if (ch == ' ' || ch == '\t' || ch == ',')
  672.     break;
  673.  
  674.     *dest++ = ch;
  675.     }
  676.  
  677. *dest = '\0';
  678.  
  679. return(src);
  680. }
  681.  
  682.  
  683.  
  684. /****************************************************************************
  685. **                                       **
  686. ****************************************************************************/
  687. int LoadDescFile(char *fName)
  688. {
  689.     FILE    *fp;
  690.     int        Mode,fMode,result;
  691.     int        bType,value,bNum,ObjIndex;
  692.     char    LineBuf[128];
  693.     char    fBuf[128];
  694.     char    *s;
  695.  
  696. fp = fopen(fName,"rt");
  697. if (fp == NULL)
  698.     {
  699.     printf("Unable to open description file: %s\n",fName);
  700.     return(-1);
  701.     }
  702.  
  703. ObjIndex = 0;
  704. Mode = 0;
  705. result = 0;
  706. *GridFile = '\0';
  707.  
  708. while (1)
  709.     {
  710.     if (feof(fp))
  711.     break;
  712.  
  713.     *LineBuf = '\0';
  714.     fgets(LineBuf,127,fp);
  715.  
  716.     if (*LineBuf == ';')
  717.     continue;
  718.  
  719.     StripEndOfLine(LineBuf);
  720.     SkipSpaces(LineBuf);
  721.  
  722.     if (!strlen(LineBuf))
  723.     continue;
  724.  
  725.     if (!stricmp(LineBuf,"WALLS:"))
  726.     {
  727.     bType = TYPE_WALL;
  728.     Mode = 1;
  729.     continue;
  730.     }
  731.  
  732.     if (!stricmp(LineBuf,"ENDWALLS:"))
  733.     {
  734.     if (Mode != 1)
  735.         {
  736.         printf("Invalid place for command: %s.\n",LineBuf);
  737.         result = -1;
  738.         }
  739.  
  740.     Mode = 0;
  741.     continue;
  742.     }
  743.  
  744.     if (!stricmp(LineBuf,"OBJECTS:"))
  745.     {
  746.     bType = TYPE_OBJECT;
  747.     Mode = 2;
  748.     continue;
  749.     }
  750.  
  751.     if (!stricmp(LineBuf,"FILES:"))
  752.     {
  753.     fMode = 1;
  754.     continue;
  755.     }
  756.  
  757.     if (!stricmp(LineBuf,"ENDFILES:"))
  758.     {
  759.     fMode = 0;
  760.     continue;
  761.     }
  762.  
  763.     if (!strnicmp(LineBuf,"PALFILE:",8))
  764.     {
  765.     strcpy(PalFile,SkipSpaces(&LineBuf[8]));
  766.     continue;
  767.     }
  768.  
  769.     if (!strnicmp(LineBuf,"MAPFILE:",8))
  770.     {
  771.     strcpy(GridFile,SkipSpaces(&LineBuf[8]));
  772.     continue;
  773.     }
  774.  
  775.     if (Mode == 2)
  776.     {
  777.     if (!strnicmp(LineBuf,"NUMBER:",7))
  778.         {
  779.         value = atoi(&LineBuf[7]);
  780.  
  781.         if (value < 1 || value >= 255)
  782.         {
  783.         printf("Invalid object number:\n%s\n",LineBuf);
  784.         result = -1;
  785.         break;
  786.         }
  787.         ObjIndex = value;
  788.         continue;
  789.         }
  790.  
  791.     if (!strnicmp(LineBuf,"BITMAPS:",8))
  792.         {
  793.         strcpy(LineBuf,SkipSpaces(&LineBuf[8]));
  794.         value = 0;
  795.         strcpy(LineBuf,CopyToComma(fBuf,LineBuf));
  796.         SkipSpaces(fBuf);
  797.         bNum = atoi(fBuf);
  798.  
  799.         if (bNum < 1 || bNum > 255)
  800.         {
  801.         printf("Invalid bitmap number for object: %d\n",ObjIndex);
  802.         result = -1;
  803.         break;
  804.         }
  805.  
  806.         ObjbmNum[ObjIndex] = bNum;
  807.         continue;
  808.         }
  809.     }
  810.  
  811.     if (fMode)
  812.     {
  813.     value = atoi(LineBuf);
  814.     if (value < 1 || value > 255)
  815.         {
  816.         printf("Invalid number for object: %s.\n",LineBuf);
  817.         result = -1;
  818.         continue;
  819.         }
  820.  
  821.     s = strpbrk(LineBuf,", \t");
  822.     if (s == NULL)
  823.         {
  824.         printf("Unable to locate bitmap name for object: %s.\n",LineBuf);
  825.         result = -1;
  826.         continue;
  827.         }
  828.  
  829.     strcpy(fBuf,SkipSpaces(s));
  830.     AddExtent(fBuf,".img");
  831.  
  832.     if (LoadBitmap(value,fBuf,bType))
  833.         {
  834.         printf("Error loading bitmap \"%s\".\n",fBuf);
  835.         result = -1;
  836.         }
  837.     continue;
  838.     }
  839.  
  840.     }
  841.  
  842. fclose(fp);
  843. return(result);
  844. }
  845.  
  846. /****************************************************************************
  847. **                                       **
  848. ****************************************************************************/
  849. int LoadGrid(void)
  850. {
  851.     int        handle;
  852.  
  853. handle = open(GridFile,O_RDWR|O_BINARY);
  854. if (handle < 1)
  855.     {
  856.     printf("Unable to open MapFile: %s\n",GridFile);
  857.     return(-1);
  858.     }
  859.  
  860. if (read(handle,Grid,4096) != 4096)
  861.     {
  862.     close(handle);
  863.     printf("Error reading MapFile: %s\n",GridFile);
  864.     return(-1);
  865.     }
  866.  
  867. close(handle);
  868. return(0);
  869. }
  870.  
  871.  
  872. /****************************************************************************
  873. **                                       **
  874. ****************************************************************************/
  875. int SaveGrid(void)
  876. {
  877.     int        handle;
  878.  
  879. handle = open(GridFile,O_RDWR|O_BINARY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE);
  880. if (handle < 1)
  881.     {
  882.     return(-1);
  883.     }
  884.  
  885. if (write(handle,Grid,4096) != 4096)
  886.     {
  887.     close(handle);
  888.     return(-2);
  889.     }
  890.  
  891. close(handle);
  892. ShowMsgBox(BOX_SAVED);
  893. return(0);
  894. }
  895.  
  896.  
  897.  
  898. /****************************************************************************
  899. **                                       **
  900. ****************************************************************************/
  901. int LoadPalette(char *pName)
  902. {
  903.     int        handle;
  904.  
  905. handle = open(pName,O_RDWR|O_BINARY);
  906. if (handle < 1)
  907.     {
  908.     printf("Unable to open PalFile: %s\n",pName);
  909.     ErrorCode = ERR_BADPALFILE;
  910.     return(-1);
  911.     }
  912.  
  913. if (read(handle,Palette,768) != 768)
  914.     {
  915.     close(handle);
  916.     printf("Error reading PalFile: %s\n",pName);
  917.     ErrorCode = ERR_BADPALFILE;
  918.     return(-1);
  919.     }
  920.  
  921. close(handle);
  922. return(0);
  923. }
  924.  
  925. /****************************************************************************
  926. **                                       **
  927. ****************************************************************************/
  928. void ShowBitmap(int Type,int Number)
  929. {
  930.         int    y,BegX,BegY;
  931.     unsigned    char    *bmp,*Video;
  932.  
  933. BegY = 134;
  934.  
  935. if (Type == TYPE_WALL)
  936.     {
  937.     BegX = 176;
  938.     bmp = bMaps[Number];
  939.     }
  940. else
  941.     {
  942.     BegX = 250;
  943.     bmp = oMaps[Number];
  944.     }
  945.  
  946. Video = MK_FP(0xA000,(BegY * 320) + BegX);
  947.  
  948. if (bmp == NULL)
  949.     {
  950.     for (y = 0; y < 64; y++)
  951.     {
  952.     memset(Video,15,64);
  953.     Video += 320;
  954.     }
  955.     return;
  956.     }
  957.  
  958.  
  959. for (y = 0; y < 64; y++)
  960.     {
  961.     memmove(Video,bmp,64);
  962.     Video += 320;
  963.     bmp += 64;
  964.     }
  965.  
  966. }
  967.  
  968. /****************************************************************************
  969. **                                       **
  970. ****************************************************************************/
  971. void mouse_released(void)
  972. {
  973.     int        x,y,button;
  974.  
  975. button = 1;
  976. while (button)
  977.     mouse_read_cursor(&button,&y,&x);
  978.  
  979. }
  980.  
  981. /****************************************************************************
  982. **                                       **
  983. ****************************************************************************/
  984. int CheckDupObjects(unsigned char oNum)
  985. {
  986.     int        i,result;
  987.     unsigned char ch;
  988.  
  989. result = 0;
  990.  
  991. for (i = 0; i < 4096; i++)
  992.     {
  993.     ch = Grid[i];
  994.  
  995.     if (!(ch & 0x80))
  996.     continue;
  997.  
  998.     if ((ch & 0x3F) == oNum)
  999.     {
  1000.     result = 1;
  1001.     ShowMsgBox(BOX_ALREADY_1_OBJECT);
  1002.     break;
  1003.     }
  1004.     }
  1005.  
  1006. return(result);
  1007. }
  1008.  
  1009. /****************************************************************************
  1010. **                                       **
  1011. ****************************************************************************/
  1012. int CountSpecialCodes(unsigned char mCode)
  1013. {
  1014.     int        i,count;
  1015.  
  1016. count = 0;
  1017. for (i = 0; i < 4096; i++)
  1018.     {
  1019.     if (Grid[i] == mCode)
  1020.     count++;
  1021.     }
  1022.  
  1023. return(count);
  1024. }
  1025.  
  1026.  
  1027. /****************************************************************************
  1028. **                                       **
  1029. ****************************************************************************/
  1030. void main(int argc,char *argv[])
  1031. {
  1032.     int        done,Action;
  1033.     int        my,mx,mButton;
  1034.     int        pos;
  1035.  
  1036. ErrorCode = 0;
  1037.  
  1038. if (mouse_installed() != -1)
  1039.     {
  1040.     printf("Mouse required\n");
  1041.     exit(1);
  1042.     }
  1043.  
  1044. if (argc < 2)
  1045.     {
  1046.     printf("Syntax: mapedit filename.ext where filename is name of desc. file\n");
  1047.     exit(1);
  1048.     }
  1049.  
  1050. printf("Loading screen\n");
  1051. if (LoadScreen("Mapedit.lbm"))
  1052.     {
  1053.     printf("\nErrorCode = %d\n",ErrorCode);
  1054.     exit(1);
  1055.     }
  1056.  
  1057. printf("Loading master file\n");
  1058. if (LoadDescFile(argv[1]))
  1059.     {
  1060.     printf("\nErrorCode = %d\n",ErrorCode);
  1061.     exit(1);
  1062.     }
  1063.  
  1064. printf("Loading map file\n");
  1065. if (LoadGrid())
  1066.     {
  1067.     printf("\nErrorCode = %d\n",ErrorCode);
  1068.     exit(1);
  1069.     }
  1070.  
  1071. printf("Loading palette file\n");
  1072. if (LoadPalette(PalFile))
  1073.     {
  1074.     printf("\nErrorCode = %d\n",ErrorCode);
  1075.     exit(1);
  1076.     }
  1077.  
  1078. printf("Building map squares\n");
  1079. if (BuildSquares())
  1080.     {
  1081.     printf("Not enough memory!\n");
  1082.     exit(1);
  1083.     }
  1084.  
  1085. SetVGAmode();
  1086.  
  1087. memmove(Palette,Pal0,9);
  1088. memmove(&Palette[0x21],Pal11,9);
  1089. memmove(&Palette[0x2d],Pal15,9);
  1090. SetPalette(Palette);
  1091.  
  1092. ShowScreen();
  1093. mouse_show_cursor();
  1094. GridX = GridY = 0;
  1095. ShowGrid();
  1096.  
  1097. CurrentType = TYPE_WALL;
  1098. CurrentSquare = 0;
  1099. ModifiedFlag = 0;
  1100. done = 0;
  1101. while (!done)
  1102.     {
  1103.     if (inkey() == 0x11B)
  1104.     {
  1105.     if (ModifiedFlag > 0 && ShowMsgBox(BOX_MODIFIED_WARNING))
  1106.         continue;
  1107.     done = 1;
  1108.     break;
  1109.     }
  1110.  
  1111.     mouse_read_cursor(&mButton,&my,&mx);
  1112.  
  1113.     if (mButton & 2)
  1114.     {
  1115.     if (GetAction(mx,my))
  1116.         continue;
  1117.  
  1118.     my -= 25;
  1119.     mx--;
  1120.     pos = (((my / 8)+GridY) * 64) + (mx / 10) + GridX;
  1121.     Grid[pos] = 0;
  1122.     ModifiedFlag = 1;
  1123.     ShowGrid();
  1124.     continue;
  1125.     }
  1126.  
  1127.     if (mButton & 1)
  1128.     {
  1129.     Action = GetAction(mx,my);
  1130.     if (Action < 0)
  1131.         continue;
  1132.  
  1133.  
  1134.     switch (Action)
  1135.         {
  1136.  
  1137.         case 0:        /* Main grid */
  1138.         my -= 25;
  1139.         mx--;
  1140.         pos = (((my / 8)+GridY) * 64) + (mx / 10) + GridX;
  1141.         mx = CurrentSquare;
  1142.  
  1143.         if (CurrentType == TYPE_OBJECT)
  1144.             {
  1145.             if (CheckDupObjects(CurrentSquare))
  1146.             break;
  1147.             mx |= 0x80;
  1148.             }
  1149.  
  1150.         if (CurrentSquare == MAP_UPCODE ||
  1151.             CurrentSquare == MAP_DOWNCODE)
  1152.             {
  1153.             if (CountSpecialCodes(CurrentSquare) > MAX_UPDOWN)
  1154.             {
  1155.             ShowMsgBox(BOX_MAX_SPECIAL_CODE);
  1156.             break;
  1157.             }
  1158.             }
  1159.  
  1160.         if (CurrentSquare == MAP_GOALCODE ||
  1161.             CurrentSquare == MAP_STARTCODE)
  1162.             {
  1163.             if (CountSpecialCodes(CurrentSquare) > 0)
  1164.             {
  1165.             ShowMsgBox(BOX_MAX_SPECIAL_CODE);
  1166.             break;
  1167.             }
  1168.  
  1169.             }
  1170.  
  1171.  
  1172.         if (PassFlag)
  1173.             mx |= 0x40;
  1174.  
  1175.         ModifiedFlag = 1;
  1176.         Grid[pos] = mx;
  1177.         ShowGrid();
  1178.         if (mx & 0x80)
  1179.             mouse_released();
  1180.         break;
  1181.  
  1182.         case 1:        /* Up arrow */
  1183.         if (GridY)
  1184.             {
  1185.             GridY--;
  1186.             ShowGrid();
  1187.             }
  1188.         break;
  1189.  
  1190.         case 2:        /* Dn arrow */
  1191.         if (GridY < 48)
  1192.             {
  1193.             GridY++;
  1194.             ShowGrid();
  1195.             }
  1196.         break;
  1197.  
  1198.         case 3:        /* Lt arrow */
  1199.         if (GridX)
  1200.             {
  1201.             GridX--;
  1202.             ShowGrid();
  1203.             }
  1204.         break;
  1205.  
  1206.         case 4:        /* Rt arrow */
  1207.         if (GridX < 48)
  1208.             {
  1209.             GridX++;
  1210.             ShowGrid();
  1211.             }
  1212.         break;
  1213.  
  1214.         case 14:        /* Save */
  1215.         if (!SaveGrid())
  1216.             ModifiedFlag = 0;
  1217.         break;
  1218.  
  1219.         case 15:        /* Exit */
  1220.         if (ModifiedFlag > 0 && ShowMsgBox(BOX_MODIFIED_WARNING))
  1221.             break;
  1222.         done = 1;
  1223.         break;
  1224.  
  1225.         case 16:        /* Blank square */
  1226.         CurrentType = TYPE_WALL;
  1227.         CurrentSquare = 0;
  1228.         mouse_hide_cursor();
  1229.         PutSquare(CURRENT_SQUAREX,CURRENT_SQUAREY,WallSquares[0]);
  1230.         mouse_show_cursor();
  1231.         break;
  1232.  
  1233.         case 17:        /* Pass box */
  1234.         PassFlag ^= 1;
  1235.         ShowPassBox();
  1236.         mouse_released();
  1237.         break;
  1238.  
  1239.         case 18:        /* New */
  1240.         if (ShowMsgBox(BOX_NEW_WARNING))
  1241.             break;
  1242.         memset(Grid,0,4096);
  1243.         GridX = GridY = 0;
  1244.         ShowGrid();
  1245.         ModifiedFlag = 1;
  1246.         break;
  1247.  
  1248.         default:
  1249.         if (Action >= 5 && Action <= 8)
  1250.             {
  1251.             my -= 2;
  1252.             my /= 8;
  1253.             CurrentSquare = ((Action - 5) * 16) + my;
  1254.             CurrentType = TYPE_WALL;
  1255.             CurrentSquare++;
  1256.             mouse_hide_cursor();
  1257.             ShowBitmap(CurrentType,CurrentSquare);
  1258.             PutSquare(CURRENT_SQUAREX,CURRENT_SQUAREY,WallSquares[CurrentSquare]);
  1259.             mouse_show_cursor();
  1260.             }
  1261.  
  1262.         if (Action >= 9 && Action <= 12)
  1263.             {
  1264.             my -= 2;
  1265.             my /= 8;
  1266.             CurrentSquare = ((Action - 9) * 16) + my;
  1267.             CurrentSquare++;
  1268.             CurrentType = TYPE_OBJECT;
  1269.             mouse_hide_cursor();
  1270.             ShowBitmap(CurrentType,ObjbmNum[CurrentSquare]);
  1271.             PutSquare(CURRENT_SQUAREX,CURRENT_SQUAREY,
  1272.                ObjSquares[CurrentSquare]);
  1273.             mouse_show_cursor();
  1274.             }
  1275.  
  1276.         if (Action > 18)
  1277.             {
  1278.             CurrentType = TYPE_WALL;
  1279.             CurrentSquare = MAP_STARTCODE + (Action - 19);
  1280.             mouse_hide_cursor();
  1281.             PutSquare(CURRENT_SQUAREX,CURRENT_SQUAREY,
  1282.                WallSquares[CurrentSquare]);
  1283.             mouse_show_cursor();
  1284.             }
  1285.  
  1286.         break;
  1287.         }
  1288.  
  1289.  
  1290.     }
  1291.  
  1292.  
  1293.     }
  1294.  
  1295. textmode(3);
  1296. }
  1297.  
  1298.