home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / graphics / acksrc.zip / ACKREAD.C < prev    next >
Text File  |  1993-06-20  |  9KB  |  504 lines

  1. /*           ACK-3D ( Animation Construction Kit 3D )              */
  2. /* Reading files      */
  3. /* Author: Lary Myers */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <dos.h>
  8. #include <mem.h>
  9. #include <alloc.h>
  10. #include <io.h>
  11. #include <fcntl.h>
  12. #include <time.h>
  13. #include <string.h>
  14. #include <sys\stat.h>
  15. #include "ack3d.h"
  16. #include "ackext.h"
  17.  
  18. /****************************************************************************
  19. **                                       **
  20. ****************************************************************************/
  21. int LoadBitmap(int BitmapNumber,char *BitmapName,int BitmapType)
  22. {
  23.     int        handle,bFlag;
  24.     int        x,y;
  25.     int        sPos,dPos;
  26.     unsigned char ch;
  27.     unsigned char *buf;
  28.     unsigned char far *bmp;
  29.  
  30. bFlag = 0;
  31.  
  32. if (!(stricmp(GetExtent(BitmapName),"BBM")))
  33.     {
  34.     buf = Readiff(BitmapName);
  35.     if (buf == NULL)
  36.     return(-1);
  37.  
  38.     memmove(buf,&buf[4],BITMAP_SIZE);
  39.     bFlag = 1;
  40.     }
  41. else
  42.     {
  43.     buf = malloc(BITMAP_SIZE);
  44.     if (buf == NULL)
  45.     {
  46.     ErrorCode = ERR_NOMEMORY;
  47.     return(-1);
  48.     }
  49.     }
  50.  
  51. bmp = malloc(BITMAP_SIZE);
  52. if (bmp == NULL)
  53.     {
  54.     ErrorCode = ERR_NOMEMORY;
  55.     free(buf);
  56.     return(-1);
  57.     }
  58.  
  59. if (BitmapType == TYPE_WALL)
  60.     bMaps[BitmapNumber] = bmp;
  61.  
  62. if (BitmapType == TYPE_OBJECT)
  63.     oMaps[BitmapNumber] = bmp;
  64.  
  65. if (!bFlag)
  66.     {
  67.     handle = open(BitmapName,O_RDWR|O_BINARY);
  68.     if (handle < 1)
  69.     {
  70.     free(buf);
  71.     free(bmp);
  72.     WrapUp();
  73.     perror(BitmapName);
  74.     ErrorCode = ERR_BADFILE;
  75.     exit(1);
  76.     }
  77.  
  78.     read(handle,buf,4);        /* Skip width and height for now */
  79.     read(handle,buf,BITMAP_SIZE);
  80.     close(handle);
  81.     }
  82.  
  83. for (y = 0; y < BITMAP_HEIGHT; y++)
  84.     {
  85.     sPos = y;
  86.     dPos = y * BITMAP_WIDTH;
  87.     for (x = 0; x < BITMAP_WIDTH; x++)
  88.     {
  89.     ch = buf[sPos];
  90.     bmp[dPos++] = ch;
  91.     sPos += BITMAP_WIDTH;
  92.     }
  93.     }
  94.  
  95. free(buf);
  96. return(0);
  97. }
  98.  
  99. /****************************************************************************
  100. **                                       **
  101. ****************************************************************************/
  102. int LoadPalette(char *PalName)
  103. {
  104.     int        handle;
  105.     char    *buf;
  106.  
  107. buf = malloc(800);
  108. if (buf == NULL)
  109.     return(-1);
  110.  
  111. handle = open(PalName,O_RDWR|O_BINARY);
  112. if (handle > 0)
  113.     {
  114.     read(handle,buf,768);
  115.     close(handle);
  116.     SetPalette(buf);
  117.     }
  118.  
  119. free(buf);
  120. return(0);
  121. }
  122.  
  123. /****************************************************************************
  124. **                                       **
  125. ****************************************************************************/
  126. int ReadMasterFile(char *fname)
  127. {
  128.     FILE    *fp;
  129.     int        bType,bNum,result;
  130.     int        value,ObjIndex;
  131.     int        Mode = 0;
  132.     int        fMode = 0;
  133.     char    *s;
  134.     char    bName[128];
  135.     char    fBuf[80];
  136.  
  137. fp = fopen(fname,"rt");
  138. if (fp == NULL)
  139.     {
  140.     printf("Master file %s not found.\n");
  141.     ErrorCode = ERR_BADFILE;
  142.     return(-1);
  143.     }
  144.  
  145. bNum = 1;
  146. bType = TYPE_WALL;
  147. result = 0;
  148.  
  149. while (!result)
  150.     {
  151.     if (feof(fp))
  152.     break;
  153.  
  154.     *bName = '\0';
  155.     fgets(bName,127,fp);
  156.  
  157.     if (*bName == ';')
  158.     continue;
  159.  
  160.     StripEndOfLine(bName);
  161.     SkipSpaces(bName);
  162.  
  163.     if (!strlen(bName))
  164.     continue;
  165.  
  166.     if (!stricmp(bName,"WALLS:"))
  167.     {
  168.     bType = TYPE_WALL;
  169.     bNum = 1;
  170.     Mode = 1;
  171.     continue;
  172.     }
  173.  
  174.     if (!stricmp(bName,"ENDWALLS:"))
  175.     {
  176.     if (Mode != 1)
  177.         {
  178.         printf("Invalid place for command: %s.\n",bName);
  179.         ErrorCode = ERR_BADCOMMAND;
  180.         result = -1;
  181.         }
  182.  
  183.     Mode = 0;
  184.     continue;
  185.     }
  186.  
  187.     if (!stricmp(bName,"OBJECTS:"))
  188.     {
  189.     bType = TYPE_OBJECT;
  190.     bNum = 40;
  191.     ObjIndex = 1;
  192.     Mode = 2;
  193.     continue;
  194.     }
  195.  
  196.     if (!stricmp(bName,"FILES:"))
  197.     {
  198.     fMode = 1;
  199.     continue;
  200.     }
  201.  
  202.     if (!stricmp(bName,"ENDFILES:"))
  203.     {
  204.     fMode = 0;
  205.     continue;
  206.     }
  207.  
  208.     if (fMode)
  209.     {
  210.     value = atoi(bName);
  211.     if (value < 1 || value > 255)
  212.         {
  213.         printf("Invalid number for object: %s.\n",bName);
  214.         ErrorCode = ERR_BADOBJNUMBER;
  215.         result = -1;
  216.         continue;
  217.         }
  218.  
  219.     s = strpbrk(bName,", \t");
  220.     if (s == NULL)
  221.         {
  222.         printf("Unable to locate bitmap name for object: %s.\n",bName);
  223.         ErrorCode = ERR_BADSYNTAX;
  224.         result = -1;
  225.         continue;
  226.         }
  227.  
  228.     strcpy(fBuf,SkipSpaces(s));
  229.     AddExtent(fBuf,".img");
  230.  
  231.     if (LoadBitmap(value,fBuf,bType))
  232.         {
  233.         printf("Error loading bitmap \"%s\".\n",fBuf);
  234.  
  235.     #if 0
  236.         ErrorCode = ERR_LOADINGBITMAP;
  237.     #endif
  238.         result = -1;
  239.         }
  240.     continue;
  241.     }
  242.  
  243.  
  244.  
  245.     if (Mode == 2)
  246.     {
  247.     if (!strnicmp(bName,"NUMBER:",7))
  248.         {
  249.         value = atoi(&bName[7]);
  250.  
  251.         if (value < 1 || value >= MAX_OBJECTS)
  252.         {
  253.         printf("Invalid object number:\n%s\n",bName);
  254.         ErrorCode = ERR_BADOBJNUMBER;
  255.         result = -1;
  256.         break;
  257.         }
  258.         ObjIndex = value;
  259.         if (value >= MaxObjects)
  260.         MaxObjects = value + 1;
  261.         continue;
  262.         }
  263.  
  264.     if (!strnicmp(bName,"DIRECTION:",10))
  265.         {
  266.         value = atoi(&bName[10]);
  267.         if (value < 0 || value > 10)
  268.         {
  269.         printf("Invalid direction: %d.\n",value);
  270.         ErrorCode = ERR_BADDIRECTION;
  271.         result = -1;
  272.         continue;
  273.         }
  274.  
  275.         ObjList[ObjIndex].Dir = value;
  276.         if (value == 10)
  277.         ObjList[ObjIndex].Sides = 0;
  278.  
  279.         continue;
  280.         }
  281.  
  282.     if (!strnicmp(bName,"SPEED:",6))
  283.         {
  284.         ObjList[ObjIndex].Speed = atoi(&bName[6]);
  285.         continue;
  286.         }
  287.  
  288.     if (!strnicmp(bName,"BITMAPS:",8))
  289.         {
  290.         strcpy(bName,SkipSpaces(&bName[8]));
  291.         value = 0;
  292.         while (value < MAX_VIEWS && *bName)
  293.         {
  294.         strcpy(bName,CopyToComma(fBuf,bName));
  295.         SkipSpaces(fBuf);
  296.         bNum = atoi(fBuf);
  297.  
  298.         if (bNum < 1 || bNum > 255)
  299.             {
  300.             printf("Invalid bitmap number for object: %d\n",ObjIndex);
  301.             ErrorCode = ERR_BADOBJNUMBER;
  302.             result = -1;
  303.             break;
  304.             }
  305.  
  306.         ObjList[ObjIndex].bmNum[value] = bNum;
  307.         value++;
  308.         }
  309.  
  310.         ObjList[ObjIndex].MaxNum = value - 1;
  311.         ObjList[ObjIndex].Sides = 0;
  312.         if (value > 1 && ObjList[ObjIndex].Dir != 10)
  313.         ObjList[ObjIndex].Sides = INT_ANGLE_360 / value;
  314.         continue;
  315.         }
  316.  
  317.     if (!strnicmp(bName,"ENDOBJECTS:",11))
  318.         {
  319.         Mode = 0;
  320.         continue;
  321.         }
  322.  
  323.     }
  324.  
  325.     if (!strnicmp(bName,"PALFILE:",8))
  326.     {
  327.     strcpy(PalFile,SkipSpaces(&bName[8]));
  328.     continue;
  329.     }
  330.  
  331.     if (!strnicmp(bName,"MAPFILE:",8))
  332.     {
  333.     strcpy(GridFile,SkipSpaces(&bName[8]));
  334.     continue;
  335.     }
  336.  
  337.     if (!strnicmp(bName,"GOALSCREEN:",11))
  338.     {
  339.     strcpy(GoalFile,SkipSpaces(&bName[11]));
  340.     continue;
  341.     }
  342.  
  343.     if (!strnicmp(bName,"GOALPAL:",8))
  344.     {
  345.     strcpy(GoalPalFile,SkipSpaces(&bName[8]));
  346.     continue;
  347.     }
  348.  
  349.     if (!strnicmp(bName,"SKYCOLOR:",9))
  350.     {
  351.     SkyColor = atoi(&bName[9]);
  352.     continue;
  353.     }
  354.  
  355.     if (!strnicmp(bName,"FLOORCOLOR:",11))
  356.     {
  357.     FloorColor = atoi(&bName[11]);
  358.     continue;
  359.     }
  360.  
  361.     if (!strnicmp(bName,"FLASHCOLOR:",11))
  362.     {
  363.     FlashColor = atoi(&bName[11]);
  364.     continue;
  365.     }
  366.  
  367.     if (!strnicmp(bName,"DOORSPEED:",10))
  368.     {
  369.     DoorSpeed = atoi(&bName[10]);
  370.     continue;
  371.     }
  372.  
  373.     if (!strnicmp(bName,"NONSECRETCODE:",14))
  374.     {
  375.     NonSecretCode = atoi(&bName[14]);
  376.     continue;
  377.     }
  378.  
  379.  
  380.     if (!strnicmp(bName,"STARTX:",7))
  381.     {
  382.     StartX = atoi(&bName[7]);
  383.     if (StartX < 0 || StartX > GRID_XMAX)
  384.         {
  385.         printf("Invalid start X coordinate: %d\n",StartX);
  386.         ErrorCode = ERR_BADSTARTX;
  387.         result = -1;
  388.         break;
  389.         }
  390.  
  391.     continue;
  392.     }
  393.  
  394.     if (!strnicmp(bName,"STARTY:",7))
  395.     {
  396.     StartY = atoi(&bName[7]);
  397.     if (StartY < 0 || StartY > GRID_YMAX)
  398.         {
  399.         printf("Invalid start Y coordinate: %d\n",StartX);
  400.         ErrorCode = ERR_BADSTARTY;
  401.         result = -1;
  402.         break;
  403.         }
  404.  
  405.     continue;
  406.     }
  407.  
  408.     if (!strnicmp(bName,"STARTANGLE:",11))
  409.     {
  410.     StartAngle = atoi(&bName[11]);
  411.     if (StartAngle < 0 || StartAngle > INT_ANGLE_360)
  412.         {
  413.         printf("Invalid start angle: %d\n",StartAngle);
  414.         ErrorCode = ERR_BADANGLE;
  415.         result = -1;
  416.         break;
  417.         }
  418.     continue;
  419.     }
  420.  
  421.     }
  422.  
  423. fclose(fp);
  424. return(result);
  425. }
  426.  
  427. /****************************************************************************
  428. **                                       **
  429. ****************************************************************************/
  430. int ReadMapFile(void)
  431. {
  432.     int        len,handle;
  433.  
  434.  
  435. handle = open(GridFile,O_RDWR|O_BINARY);
  436. if (handle < 1)
  437.     {
  438.     printf("Error opening map file %s.\n",GridFile);
  439.     ErrorCode = ERR_BADMAPFILE;
  440.     return(-1);
  441.     }
  442.  
  443. len = read(handle,Grid,GRID_MAX);
  444. close(handle);
  445.  
  446. if (len != GRID_MAX)
  447.     {
  448.     printf("Error reading map file %s.\n",GridFile);
  449.     ErrorCode = ERR_READINGMAP;
  450.     return(-1);
  451.     }
  452.  
  453. return(0);
  454. }
  455.  
  456. /****************************************************************************
  457. **                                       **
  458. ****************************************************************************/
  459. int ReadNewLevel(void)
  460. {
  461.     int        i,result;
  462.     unsigned char *bmp;
  463.     char    fName[14];
  464.  
  465. for (i = 0; i < 255; i++)
  466.     {
  467.     if (bMaps[i] != NULL)
  468.     free(bMaps[i]);
  469.  
  470.     if (oMaps[i] != NULL)
  471.     free(oMaps[i]);
  472.  
  473.     bMaps[i] = NULL;
  474.     oMaps[i] = NULL;
  475.     }
  476.  
  477. MaxObjects = 0;
  478. result       = 0;
  479. ErrorCode  = 0;
  480.  
  481. sprintf(fName,"ack3d.l%02d",CurrentLevel);
  482.  
  483. if (ReadMasterFile(fName))
  484.     result = -1;
  485.  
  486. if (ReadMapFile())
  487.     result = -2;
  488.  
  489. LoadPalette(PalFile);
  490.  
  491. BuildXYgrid();
  492.  
  493. if (result)
  494.     {
  495.     WrapUp();
  496.     printf("A fatal error occurred reading level %d data, %s!\n",CurrentLevel,fName);
  497.     printf("Error code = %d\n",ErrorCode);
  498.     exit(1);
  499.     }
  500.  
  501. return(0);
  502. }
  503.  
  504.