home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0109.zip / Timur / terrain.c < prev    next >
Text File  |  1993-06-07  |  7KB  |  223 lines

  1. /* TERRAIN.C - hex map terrain support
  2.  
  3. Copyright (c) 1992-1993 Timur Tabi
  4. Copyright (c) 1992-1993 Fasa Corporation
  5.  
  6. The following trademarks are the property of Fasa Corporation:
  7. BattleTech, CityTech, AeroTech, MechWarrior, BattleMech, and 'Mech.
  8. The use of these trademarks should not be construed as a challenge to these marks.
  9.  
  10. */
  11.  
  12. #define INCL_GPIPRIMITIVES
  13. #define INCL_GPIBITMAPS
  14. #define INCL_WINPOINTERS
  15. #include <os2.h>
  16.  
  17. #define TERRAIN_C
  18. #include "header.h"
  19. #include "errors.h"
  20. #include "resource.h"
  21. #include "terrain.h"
  22. #include "hexes.h"
  23. #include "target.h"
  24. #include "bitmap.h"
  25. #include "mech.h"
  26. #include "files.h"
  27. #include "window.h"
  28.  
  29. int TerrainIdFromMenu(int iMenuID) {
  30. /* Given an IDM_TER_xxx value, this function returns the index into ater[] that corresponds to that
  31.    terrain.  It returns -1 if it can't find a correspondence.
  32. */
  33.   int i;
  34.  
  35.   for (i=0; i<NUM_TERRAINS; i++)
  36.     if (ater[i].iMenuID==iMenuID) return i;
  37.  
  38.   return -1;
  39. }
  40.  
  41. ERROR TerrainInit(void) {
  42. /* Initialize the terrain data.  This routine will eventually read the values from a configuration
  43.    file.  In the meantime, they are hard-coded
  44. */
  45.   int i;
  46.   int c,r;
  47.   MAP map;      // default MAP type
  48.  
  49.   ater[0].iMenuID=IDM_TER_CLEAR_GROUND;
  50.   ater[1].iMenuID=IDM_TER_ROUGH_GROUND;
  51.   ater[2].iMenuID=IDM_TER_WATER;
  52.   ater[3].iMenuID=IDM_TER_LIGHT_WOODS;
  53.   ater[4].iMenuID=IDM_TER_HEAVY_WOODS;
  54.   ater[5].iMenuID=IDM_TER_PAVEMENT;
  55.   ater[6].iMenuID=IDM_TER_BRIDGE;
  56.   ater[7].iMenuID=IDM_TER_LIGHT_BLDG;
  57.   ater[8].iMenuID=IDM_TER_MEDIUM_BLDG;
  58.   ater[9].iMenuID=IDM_TER_HEAVY_BLDG;
  59.   ater[10].iMenuID=IDM_TER_HARD_BLDG;
  60.  
  61. // Zero out all bitmap handles
  62.   for (i=0; i<NUM_TERRAINS; i++)
  63.     ater[i].hbm=0;
  64.  
  65. // Now load the bitmaps for terrains that have them
  66.   if (ErrorBox(BitmapLoad(IDB_TER_BLDGLIGHT,&ater[7].hbm))) return ERR_TERR_INIT;
  67.   if (ErrorBox(BitmapLoad(IDB_TER_BLDGMEDIUM,&ater[8].hbm))) return ERR_TERR_INIT;
  68.   if (ErrorBox(BitmapLoad(IDB_TER_BLDGHEAVY,&ater[9].hbm))) return ERR_TERR_INIT;
  69.   if (ErrorBox(BitmapLoad(IDB_TER_BLDGHARD,&ater[10].hbm))) return ERR_TERR_INIT;
  70.  
  71. // The remaining terrains have colors and patterns
  72.   ater[0].bColor=CLR_BROWN;
  73.   ater[1].bColor=CLR_DARKGRAY;
  74.   ater[2].bColor=CLR_DARKBLUE;
  75.   ater[3].bColor=CLR_GREEN;
  76.   ater[4].bColor=CLR_DARKGREEN;
  77.   ater[5].bColor=CLR_PALEGRAY;
  78.   ater[6].bColor=CLR_BROWN;
  79.  
  80.   ater[0].bPattern=PATSYM_DENSE5;
  81.   ater[1].bPattern=PATSYM_DENSE6;
  82.   ater[2].bPattern=PATSYM_DEFAULT;
  83.   ater[3].bPattern=PATSYM_DENSE3;
  84.   ater[4].bPattern=PATSYM_DENSE5;
  85.   ater[5].bPattern=PATSYM_DEFAULT;
  86.   ater[6].bPattern=PATSYM_DIAG4;
  87.  
  88.   ater[0].iVisibility=0;
  89.   ater[1].iVisibility=0;
  90.   ater[2].iVisibility=0;
  91.   ater[3].iVisibility=3;
  92.   ater[4].iVisibility=2;
  93.   ater[5].iVisibility=0;
  94.   ater[6].iVisibility=0;
  95.   ater[7].iVisibility=1;
  96.   ater[8].iVisibility=1;
  97.   ater[9].iVisibility=1;
  98.   ater[10].iVisibility=1;
  99.  
  100. // Initialize the default map type
  101.   map.iTerrain=TerrainIdFromMenu(IDM_TER_CLEAR_GROUND);
  102.   map.iHeight=0;
  103.  
  104. // Initialize some variables
  105.   if (ErrorBox(BitmapLoad(IDB_HEX_MASK,&hbmHexMask))) return ERR_TERR_INIT;
  106.  
  107.   for (c=0; c<NUM_COLUMNS; c++)
  108.     for (r=c & 1; r<NUM_ROWS-(c & 1); r+=2)
  109.        amap[c][r]=map;
  110.  
  111.   return ERR_NOERROR;
  112. }
  113.  
  114. // -------- Drawing the map
  115.  
  116. void TerrainDrawMap(HWND hwnd) {
  117. /* Draws the combat map.
  118.    Future enhancement: Draw all hexagons of a given terrain first
  119. */
  120.   RECTL rcl;
  121.   HPS hps=WinBeginPaint(hwnd,0UL,NULL);
  122.   HEXINDEX hi;
  123.  
  124.   target.fActive=FALSE;                             // Cancel any targetting
  125.   WinQueryWindowRect(hwnd,&rcl);
  126.   WinFillRect(hps,&rcl,HEX_COLOR);
  127.  
  128.   WinSetPointer(HWND_DESKTOP,WinQuerySysPointer(HWND_DESKTOP,SPTR_WAIT,FALSE));
  129.   for (hi.c=0;hi.c<NUM_COLUMNS;hi.c++)
  130.     for (hi.r=hi.c & 1;hi.r<NUM_ROWS-(hi.c & 1);hi.r+=2)
  131.       HexFillDraw(hps,hi);
  132.   WinSetPointer(HWND_DESKTOP,WinQuerySysPointer(HWND_DESKTOP,SPTR_ARROW,FALSE));
  133.   WinEndPaint(hps);
  134.  
  135.   MechDraw();
  136. }
  137.  
  138. // -------- Loading, saving, and erasing maps ----------------------------------------------------
  139.  
  140. #define LOAD_ACTION (OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW )
  141. #define SAVE_ACTION (OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS)
  142.  
  143. #define READ_ATTRS OPEN_FLAGS_NO_CACHE|              /* No need to take up precious cache space */ \
  144.                    OPEN_FLAGS_SEQUENTIAL|            /* One-time read, remember?                */ \
  145.                    OPEN_SHARE_DENYWRITE|             /* We don't want anyone changing it        */ \
  146.                    OPEN_ACCESS_READONLY              // To prevent accidentally writing to it
  147.  
  148. #define WRITE_ATTRS OPEN_FLAGS_NO_CACHE|              /* No need to take up precious cache space */ \
  149.                     OPEN_FLAGS_SEQUENTIAL|            /* One-time write, remember?               */ \
  150.                     OPEN_SHARE_DENYREADWRITE|         /* We don't want anyone touching it        */ \
  151.                     OPEN_ACCESS_WRITEONLY             // That's how we're gonna do it
  152.  
  153. static char szCurrentMap[128]="";
  154.  
  155. void TerrainNewMap(void) {
  156. /* This function clears the current map
  157. */
  158.   *szCurrentMap=0;
  159.   WindowSetTitle(NULL);
  160. }
  161.  
  162. static APIRET OpenMap(char *szName) {
  163. /* This function loads the map specified by the filename szName.
  164. */
  165.   HFILE hfile;
  166.   ULONG ulAction,ulBytesRead;
  167.   FILESTATUS3 fs3;
  168.  
  169.   RETURN(DosQueryPathInfo(szName,1,&fs3,sizeof(fs3)));
  170.   RETURN(DosOpen(szName,&hfile,&ulAction,0,FILE_NORMAL,LOAD_ACTION,READ_ATTRS,NULL));
  171.   RETURN(DosRead(hfile,amap,sizeof(amap),&ulBytesRead));
  172.   return DosClose(hfile);
  173. }
  174.  
  175. static APIRET SaveMap(char *szName) {
  176. /* This function saves the map specified under the filename szName
  177. */
  178.   HFILE hfile;
  179.   ULONG ulAction,ulBytesWritten;
  180.  
  181.   RETURN(DosOpen(szName,&hfile,&ulAction,sizeof(amap),FILE_NORMAL,SAVE_ACTION,WRITE_ATTRS,NULL));
  182.   RETURN(DosWrite(hfile,amap,sizeof(amap),&ulBytesWritten));
  183.   return DosClose(hfile);
  184. }
  185.  
  186. void TerrainOpenMap(void) {
  187. /* This function prompts the user for a filename, and then loads that file as a map
  188.    Returns FALSE if the open request was cancelled by the user.
  189. */
  190.   gec=FileOpen("Open Map",szCurrentMap);                  // Here, the user selects the file
  191.   if (gec) {
  192.     if (gec != ERR_FILES_CANCEL)
  193.       ErrorBox(gec);
  194.     return;
  195.   }
  196.  
  197.   if (ErrorDosError(OpenMap(szCurrentMap))) return;       // Open and read the map, exit if error
  198.  
  199.   WinInvalidateRect(hwndClient,NULL,FALSE);               // Make sure the old map is erased
  200.   WindowSetTitle(szCurrentMap);                           // Change the window title to reflect it
  201.   WinPostMsg(hwndClient,WM_PAINT,0,0);                    // Draw the new map
  202.   return;
  203. }
  204.  
  205. void TerrainSaveMap(void) {
  206. /* This routine saves the current map under the current filename
  207. */
  208.   ErrorDosError(SaveMap(szCurrentMap));
  209. }
  210.  
  211. void TerrainSaveMapAs(void) {
  212. /* This function prompts the user for a new filename as which to save the current map.
  213. */
  214.   gec=FileSave("Save Map",szCurrentMap);
  215.   if (gec) {
  216.     if (gec != ERR_FILES_CANCEL) ErrorBox(gec);
  217.     return;
  218.   }
  219.  
  220.   if (ErrorDosError(SaveMap(szCurrentMap))) return;
  221.   WindowSetTitle(szCurrentMap);
  222. }
  223.