home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0108.zip / Timur / hexes.c < prev    next >
Text File  |  1993-03-14  |  13KB  |  398 lines

  1. /* HEXES.C - Hex map routines
  2.  
  3. Copyright (c) 1993 Timur Tabi
  4. Copyright (c) 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. This module contains all the code pertaining to the hexagonal grid of the
  11. combat map.  This includes drawing and interpreting mouse input.  Hexes are
  12. identified by a column/row index passed as two integers.  X,Y coordinates
  13. are identified with a POINTL structure.
  14. */
  15.  
  16. #define INCL_DOSPROCESS
  17. #define INCL_GPILOGCOLORTABLE
  18. #define INCL_GPIPRIMITIVES
  19. #define INCL_GPIBITMAPS
  20. #include <os2.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <math.h>
  24.  
  25. #define HEXES_C
  26.  
  27. #include "header.h"
  28. #include "resource.h"
  29. #include "target.h"
  30. #include "mech.h"
  31. #include "terrain.h"
  32. #include "bitmap.h"
  33. #include "window.h"
  34.  
  35. // Array of each vertex in a hexagon, ending with the lower-left corner at relative position (0,0)
  36. static POINTL aptlHex[]={ {HEX_SIDE,0},
  37.   {HEX_SIDE+HEX_EXT,HEX_HEIGHT/2},
  38.   {HEX_SIDE,HEX_HEIGHT},
  39.   {0,HEX_HEIGHT},
  40.   {-HEX_EXT,HEX_HEIGHT/2},
  41.   {0,0}                         };
  42.  
  43. // The PS handle for all hex drawing
  44. static HPS hpsHex;
  45.  
  46. POINTL HexCoord(HEXINDEX hi) {
  47. /* This function returns the X,Y coordinate of the lower-left vertex for a given hex
  48.    index.
  49. */
  50.    POINTL ptl;
  51.  
  52. // Odd numbered columns are drawn a little to the right of even columns
  53. // HEX_SIDE+HEX_EXT is the X-coordinate of column #1
  54.    if (hi.c & 1)
  55.       ptl.x=XLAG+HEX_DIAM+(2*XLAG+HEX_SIDE+HEX_DIAM)*(hi.c-1)/2;
  56.    else
  57.       ptl.x=HEX_EXT+(2*XLAG+HEX_SIDE+HEX_DIAM)*hi.c/2;
  58.    ptl.y=hi.r*(YLAG-1+HEX_HEIGHT/2);
  59.    return ptl;
  60. }
  61.  
  62. POINTL HexMidpoint(HEXINDEX hi) {
  63. /* This function is identical to HexCoord(), except that it returns the coordinates of
  64.    the midpoint (centerpoint) of the hexagon.
  65. */
  66.    POINTL ptl;
  67.  
  68.    if (hi.c & 1)
  69.       ptl.x=(HEX_SIDE/2)+XLAG+HEX_DIAM+(2*XLAG+HEX_SIDE+HEX_DIAM)*(hi.c-1)/2;
  70.    else
  71.       ptl.x=(HEX_SIDE/2)+HEX_EXT+(2*XLAG+HEX_SIDE+HEX_DIAM)*hi.c/2;
  72.    ptl.y=(HEX_HEIGHT/2)+hi.r*(YLAG-1+HEX_HEIGHT/2);
  73.    return ptl;
  74. }
  75.  
  76. void HexDraw(HPS hps, HEXINDEX hi) {
  77. /* This function draws the hexagon at index 'hi'.
  78.    Future enhancement: instead of calculating all six x,y coordinates, just relocate origin.
  79. */
  80.   int i;
  81.   POINTL aptl[6];
  82.  
  83.   aptl[5]=HexCoord(hi);                       // Move to the last vertex
  84.   GpiMove(hps,&aptl[5]);
  85.   for (i=0; i<5; i++) {
  86.     aptl[i].x=aptl[5].x+aptlHex[i].x;
  87.     aptl[i].y=aptl[5].y+aptlHex[i].y;
  88.   }
  89.   GpiPolyLine(hps,6L,aptl);                   // Draw all six lines at once
  90. }
  91.  
  92. void HexFillDraw(HEXINDEX hi) {
  93. /* This function is identical to HexDraw() except that it draws the terrain inside the
  94.    hexagon and always uses hpsHex.
  95. */
  96.   int iTerrain=amap[hi.c][hi.r].iTerrain;
  97.   POINTL ptl;
  98.  
  99.   if (ater[iTerrain].hbm) {
  100.     ptl=HexCoord(hi);
  101.     ptl.x-=HEX_EXT;
  102.     BitmapDraw(ater[iTerrain].hbm,hbmHexMask,ptl);
  103.   } else {
  104.     GpiSetColor(hpsHex,ater[iTerrain].bColor);
  105.     GpiSetPattern(hpsHex,ater[iTerrain].bPattern);
  106.     GpiBeginArea(hpsHex,BA_NOBOUNDARY);
  107.     HexDraw(hpsHex,hi);
  108.     GpiEndArea(hpsHex);
  109.   }
  110. }
  111.  
  112. void HexInitMap(HWND hwnd) {
  113. /* This routine sets the default terrain type, configures the map presentation space
  114.    paramters, and initializes the 'amap' array.  Assumes that hpsHex has already been
  115.    initialized.
  116.  
  117.    Must be called after TerrainInit().
  118. */
  119.   int c,r;
  120.   MAP map;      // default MAP type
  121.  
  122. // Initialize the default map type
  123.   map.iTerrain=TerrainIdFromMenu(IDM_TER_CLEAR_GROUND);
  124.   map.iHeight=0;
  125.  
  126. // Initialize some variables
  127.   hbmHexMask=BitmapLoad(IDB_HEX_MASK);
  128.   hpsHex=WinGetPS(hwnd);
  129.  
  130.   GpiSetBackMix(hpsHex,BM_OVERPAINT);
  131.   for (c=0;c<NUM_COLUMNS;c++)
  132.     for (r=c & 1;r<NUM_ROWS-(c & 1);r+=2)
  133.        amap[c][r]=map;
  134. }
  135.  
  136. void HexDrawMap(HWND hwnd) {
  137. /* Draws the combat map.
  138.    Future enhancement: Draw all hexagons of a given terrain first
  139. */
  140.   RECTL rcl;
  141.   POINTL ptl={0,0};
  142.   HPS hps;
  143.   HEXINDEX hi;
  144.  
  145.   target.fActive=FALSE;                            // Cancel any targetting
  146.   hps=WinBeginPaint(hwnd,0UL,NULL);
  147.   WinQueryWindowRect(hwnd,&rcl);
  148.   WinFillRect(hps,&rcl,HEX_COLOR);
  149.   WinEndPaint(hps);
  150.  
  151.   for (hi.c=0;hi.c<NUM_COLUMNS;hi.c++)
  152.     for (hi.r=hi.c & 1;hi.r<NUM_ROWS-(hi.c & 1);hi.r+=2)
  153.       HexFillDraw(hi);
  154.  
  155.   MechDraw();
  156. }
  157.  
  158. // -----------------------------------------------------------------------
  159. //   Hex Locator routines
  160. // -----------------------------------------------------------------------
  161.  
  162. unsigned int * InitHexLimit(unsigned int uiHeight) {
  163. /* Contributed by: BCL
  164.    This functions initializes the integer array of hexagonal y-deltas.
  165. */
  166.   unsigned int * HexLimit = (unsigned int *) malloc(uiHeight * sizeof(int));
  167.   unsigned int HalfWay = uiHeight / 2;
  168.   unsigned int ndx;
  169.  
  170.   for (ndx=0;ndx <= HalfWay; ndx++) {
  171.     HexLimit[ndx] = ((4 * ndx) + 2) / 7;
  172.     HexLimit[uiHeight - 1 - ndx] = HexLimit[ndx];
  173.   }
  174.   return HexLimit;
  175. }
  176.  
  177. BOOL HexInPoint(POINTL ptl, HEXINDEX hi) {
  178. /* Contributed by: BCL
  179.    This function returns TRUE if the point 'ptl' is inside hex 'hi'.
  180. */
  181.    static unsigned int * Limits = NULL;
  182.    POINTL ptlHex=HexCoord(hi);
  183.    int dy;
  184.  
  185. // Initialize the limit array the first time this function is called
  186.    if (Limits == NULL) Limits = InitHexLimit(HEX_HEIGHT);
  187.  
  188. // Test if hi is a valid hex index
  189.    if (hi.c<0 || hi.r<0) return FALSE;
  190.    if (hi.c&1 && hi.r<1) return FALSE;
  191.  
  192.    if (ptl.y < ptlHex.y) return FALSE;
  193.    if (ptl.y > ptlHex.y+HEX_HEIGHT) return FALSE;
  194. // The point is definitely not within the hexagon's inner rectangle.
  195. //  Let's try the side triangles.
  196.    dy = ptl.y - ptlHex.y;
  197.    if (ptl.x < ptlHex.x - Limits[dy]) return FALSE;
  198.    if (ptl.x > ptlHex.x+HEX_SIDE + Limits[dy]) return FALSE;
  199.    return TRUE;
  200. }
  201.  
  202. BOOL HexLocate(POINTL ptl, HEXINDEX *phi) {
  203. /* Original by: BCL
  204.    Redesigned by: TT
  205.    This routine identifies the hexagon underneath point ptl.  It returns
  206.    TRUE if it found one, FALSE otherwise.  *phi is modified only if the
  207.    function returns TRUE.
  208. */
  209.   HEXINDEX hi;
  210.   int GuessC, GuessR;
  211.  
  212.   if (ptl.x < HEX_SIDE+HEX_EXT)
  213.     GuessC = 0;
  214.   else
  215.     GuessC = (ptl.x-HEX_EXT)/(3*HEX_EXT+XLAG);
  216.  
  217.   if (GuessC & 1) {
  218.     GuessR = (ptl.y-(HEX_HEIGHT/2)-YLAG)/(HEX_HEIGHT+YLAG);
  219.     GuessR = 1+2*GuessR;                                      // Force the multiplication last
  220.   } else {
  221.     GuessR = ptl.y/(HEX_HEIGHT+YLAG);
  222.     GuessR *= 2;
  223.   }
  224.  
  225.   hi.c=GuessC;
  226.   hi.r=GuessR;
  227.   if (HexInPoint(ptl,hi)) {
  228.     *phi=hi;
  229.     return TRUE;
  230.   }
  231.  
  232.   hi.c=GuessC+1;
  233.   hi.r=GuessR+1;
  234.   if (HexInPoint(ptl,hi)) {
  235.     *phi=hi;
  236.     return TRUE;
  237.   }
  238.  
  239.   hi.r=GuessR-1;
  240.   if (HexInPoint(ptl,hi)) {
  241.     *phi=hi;
  242.     return TRUE;
  243.   }
  244.  
  245.   return FALSE;
  246. }
  247.  
  248. VOID APIENTRY HexHighlight(ULONG ulThreadArg) {
  249. /* This function changes the color of the origin hex during targetting.  It
  250.    is started as a background thread and continues until target.fActive
  251.    becomes FALSE.  If there are more than 16 colors, then a routine which cycles
  252.    through 256 shades of red is chosen.  Otherwise, the hex simply blinks red.
  253.    At termination, the color is set back and the hex is redrawn.
  254.  
  255.    For the color cycling, 'i' is a byte because the "i++" statement will
  256.    automatically cycle from 0-255.  By the way, can someone tell me if it
  257.    even works on a 256-color monitor?  I haven't even seen it yet.
  258.  
  259.    At this writing the code for color-cycling has NOT been tested on a
  260.    monitor with 256-colors.  It has been tested on a 16-color monitor and
  261.    looks stupid.
  262.  
  263.    This function belongs in module TARGET.  It will eventually be moved.
  264. */
  265.   BYTE i;
  266.  
  267.   if (lNumColors>16) {
  268.     GpiCreateLogColorTable(target.hpsHighlight,0,LCOLF_RGB,0,0,NULL);
  269.  
  270.     for (i=0; target.fActive; i++) {
  271.       GpiSetColor(target.hpsHighlight,(LONG) i<<16);
  272.       HexDraw(target.hpsHighlight,target.hiStart);
  273.     }
  274.     GpiCreateLogColorTable(target.hpsHighlight,LCOL_RESET,0,0,0,NULL);
  275.   } else
  276.     while (target.fActive) {
  277.       GpiSetColor(target.hpsHighlight,CLR_BLACK);
  278.       HexDraw(target.hpsHighlight,target.hiStart);
  279.       DosSleep(300L);
  280.       if (!target.fActive) break;
  281.         GpiSetColor(target.hpsHighlight,CLR_RED);
  282.       HexDraw(target.hpsHighlight,target.hiStart);
  283.       DosSleep(300L);
  284.     }
  285.  
  286. // Redraw the starting hex before exiting
  287.   GpiSetColor(target.hpsHighlight,HEX_COLOR);
  288.   HexDraw(target.hpsHighlight,target.hiStart);
  289. }
  290.  
  291. HEXINDEX HexNextShortest(HEXINDEX hiFrom, HEXINDEX hiTo) {
  292. /* Contributed by: BCL
  293.    This routine, given a starting and finishing hex, will calculate the next
  294.    hex along the shortest (in count of hexes) path between them.
  295. */
  296.   int i;                                              // The change in column
  297.  
  298.   if (hiFrom.c < hiTo.c)
  299.     i = 1;                                            // Move from E to W.
  300.   else
  301.     i = -1;                                           // Move from W to E.
  302.  
  303.   if (hiFrom.c != hiTo.c) {
  304.       hiFrom.c += i;                                  // Advance one col in the appropriate dir.
  305.       if (hiFrom.r < hiTo.r)
  306.         hiFrom.r++;                                   // Target lies N; go N in next column.
  307.       else if (hiFrom.r > hiTo.r)
  308.         hiFrom.r--;                                   // Target lies S; go S in next column.
  309.       else if (hiFrom.r > 0)
  310.         hiFrom.r--;                                   // Target lies E or W; go S in next column
  311.                                                       // since we can (row > 0).
  312.       else
  313.         hiFrom.r++;                                   // Target lies E or W; go N in next column
  314.                                                       // since we can't go S (row = 0).
  315.   } else {
  316.  
  317. // Now that we have the right column, lets match the rows.
  318.  
  319.     if (hiFrom.r < hiTo.r)
  320.       hiFrom.r += 2;                                  // Move from S to N.
  321.     if (hiFrom.r > hiTo.r)
  322.       hiFrom.r -= 2;                                  // Move from N to S.
  323.   }
  324.  
  325.   return hiFrom;
  326. }
  327.  
  328. // -----------------------------------------------------------------------
  329. //   Targetting-line path routines
  330. // -----------------------------------------------------------------------
  331.  
  332. /* Algorithms for the targetting-line path contributed by CM
  333.  
  334.    The problem with this algorithm is that it is _too_ good.  It catches hexagons
  335.    that, although are theoretically under the trajectory, would not normally be
  336.    counted in a real Battletech game.  Try it, and you'll see what I mean.  I'm
  337.    open to suggestions on this one.
  338. */
  339.  
  340. HEXINDEX HexFromSide(HEXINDEX hi, int iSide) {
  341. /* This function returns the hex index of the hexagon that is bordering on
  342.    side iSide of hexagon hi.
  343. */
  344.   switch (iSide) {
  345.     case 0: hi.c++;         // S.E.
  346.             hi.r--;
  347.             break;
  348.     case 1: hi.c++;         // N.E.
  349.             hi.r++;
  350.             break;
  351.     case 2: hi.r+=2;        // North
  352.             break;
  353.     case 3: hi.c--;         // N.W.
  354.             hi.r++;
  355.             break;
  356.     case 4: hi.c--;         // S.W.
  357.             hi.r--;
  358.             break;
  359.     default:hi.r-=2;        // South
  360.   }
  361.   return hi;
  362. }
  363.  
  364. int HexFirstSide(HEXINDEX hiFrom, HEXINDEX hiTo) {
  365. /* This function returns the side to the first hexagon that follows the trajectory
  366.    from hiFrom to hiTo.  If the targetting line passes through a vertex, this function
  367.    returns a -1
  368. */
  369.   int dx,dy=hiTo.r - hiFrom.r;
  370.   float m;
  371.  
  372.   if (dy == 0) return -1;
  373.  
  374.   dx=hiTo.c - hiFrom.c;
  375.   if (dx == 0) return (dy>0) ? 2 : 5;           // Vertical line?
  376.  
  377.   m=(float) dy/dx;
  378.   if (fabs(m) == 3.0) return -1;
  379.  
  380.   if (fabs(m)>3) return (dy>0) ? 2 : 5;               // Almost a vertical line?
  381.  
  382.   if (m>0) return (dx>0) ? 1 : 4;
  383.  
  384.   return (dx>0) ? 0 : 3;
  385. }
  386.  
  387. void HexPointFromSide(HEXINDEX hi, int iSide, PPOINTL pptl1, PPOINTL pptl2) {
  388. /* This function returns the x,y coordinates of the two endpoints of side
  389.    iSide of hexagon hi.  Two two points are returned in pptl1 and pptl2
  390. */
  391.    POINTL ptl=HexCoord(hi);      // All coordinates are offsets from this point
  392.  
  393.    pptl1->x=ptl.x+aptlHex[iSide].x;             // Calculate the two endpoints of that side
  394.    pptl1->y=ptl.y+aptlHex[iSide].y;
  395.    pptl2->x=ptl.x+aptlHex[(iSide+1) % 6].x;
  396.    pptl2->y=ptl.y+aptlHex[(iSide+1) % 6].y;
  397. }
  398.