home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0104.zip / Timur / hexes.C < prev    next >
Text File  |  1992-05-12  |  5KB  |  146 lines

  1. /* HEXES.C - Hex map routines
  2.  
  3. Copyright (c) 1992 Timur Tabi                                                     
  4. Copyright (c) 1992 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. #include <os2.h>
  19. #include <string.h>
  20. #include "hexes.h"
  21.  
  22. TARGET target;                   // User-controlled targetting
  23.  
  24. POINTL HexCoord(HEXINDEX hi) {
  25.    POINTL ptl;
  26.  
  27.    if (hi.c%2 == 1)
  28.       ptl.x=(LONG) HEX_EXT+(HEX_SIDE+HEX_DIAM)*(hi.c-1)/2;
  29.    else
  30.       ptl.x=(LONG) HEX_EXT+(HEX_SIDE+HEX_EXT)+(HEX_SIDE+HEX_DIAM)*(hi.c-2)/2;
  31.    ptl.y=(LONG) (hi.r-1)*(HEX_HEIGHT/2);
  32.    return ptl;
  33. }
  34.  
  35. POINTL HexMidpoint(HEXINDEX hi) {
  36.    POINTL ptl;
  37.  
  38.    if (hi.c%2 == 1)
  39.       ptl.x=(LONG) (HEX_SIDE/2)+HEX_EXT+(HEX_SIDE+HEX_DIAM)*(hi.c-1)/2;
  40.    else
  41.       ptl.x=(LONG) (HEX_SIDE/2)+HEX_EXT+(HEX_SIDE+HEX_EXT)+(HEX_SIDE+HEX_DIAM)*(hi.c-2)/2;
  42.    ptl.y=(LONG) (HEX_HEIGHT/2)+(hi.r-1)*(HEX_HEIGHT/2);
  43.    return ptl;
  44. }
  45.  
  46. void HexDraw(HPS hps,HEXINDEX hi) {
  47. /* This function draws a the hexagon at index 'hi'.  'ptlHex' is a series of
  48.    X,Y positions of the vertices, relative to the lower-left vertex.  The
  49.    actual X,Y coordinate of the lower-left vertex is calculated and stored
  50.    in the last element of 'ptlHex'.  This value is then added to the remaining
  51.    5 elements in the array.
  52. */
  53.    POINTL ptlHex[]={ {HEX_SIDE,0},
  54.                        {HEX_SIDE+HEX_EXT,HEX_HEIGHT/2},
  55.                        {HEX_SIDE,HEX_HEIGHT},
  56.                        {0,HEX_HEIGHT},
  57.                        {-HEX_EXT,HEX_HEIGHT/2},
  58.                        {0,0}                         };
  59.    int i;
  60.  
  61.    ptlHex[5]=HexCoord(hi);
  62.    GpiMove(hps,&ptlHex[5]);
  63.    for (i=0;i<5;i++) {
  64.       ptlHex[i].x+=ptlHex[5].x;
  65.       ptlHex[i].y+=ptlHex[5].y;
  66.    }
  67.    GpiPolyLine(hps,6L,&ptlHex[0]);
  68. }
  69.  
  70. BOOL HexInPoint(POINTL ptl, HEXINDEX hi) {
  71. /* This function returns TRUE if the point 'ptl' is inside hex 'hi'.
  72.    Currently it only checks the rectangle bounded by the two two vertices
  73.    and the two lower vertices.  If anyone knows of a quick and easy algorithm
  74.    that checks the entire hexagon, please let me know.
  75. */
  76.    POINTL ptlHex=ptlHex=HexCoord(hi);
  77.  
  78.    if (ptl.x < ptlHex.x) return FALSE;
  79.    if (ptl.x > ptlHex.x+HEX_SIDE) return FALSE;
  80.    if (ptl.y < ptlHex.y) return FALSE;
  81.    if (ptl.y > ptlHex.y+HEX_HEIGHT) return FALSE;
  82.    return TRUE;
  83. }
  84.  
  85. BOOL HexLocate(POINTL ptl, HEXINDEX *phi) {
  86. /* This function attempts to locate the hex to which 'ptl' points.  It scans
  87.    each hex on the map until it finds a match.  A future version will be more
  88.    efficient.
  89. */
  90.    HEXINDEX hi;
  91.  
  92.    for (hi.c=1;hi.c<=NUM_COLUMNS;hi.c++)
  93.       for (hi.r=2-(hi.c%2);hi.r<=NUM_ROWS+(hi.c%2);hi.r+=2)
  94.          if (HexInPoint(ptl,hi)) {
  95.             phi->c=hi.c;
  96.             phi->r=hi.r;
  97.             return TRUE;
  98.          }
  99.    return FALSE;
  100. }
  101.  
  102. extern LONG lNumColors;
  103.  
  104. VOID APIENTRY HexHighlight(ULONG ulThreadArg) {
  105. /* This function changes the color of the origin hex during targetting.  It 
  106.    is started as a background thread and continues until target.fActive
  107.    becomes FALSE.  If there are more than 16 colors, then a routine which
  108.    through 256 shades of red is chosen.  Otherwise, the hex simply blinks red.
  109.    At termination, the color is set back to white and the hex is redrawn.
  110.  
  111.    For the color cycling, 'i' is a byte because the "i++" statement will
  112.    automatically cycle from 0-255.  'i' is used for a delay loop since
  113.    DosSleep() never returns, even when the parameter is 1 millisecond.
  114.  
  115.    At this writing the code for color-cycling has NOT been tested on a
  116.    monitor with 256-colors.  It has been tested on a 16-color monitor and
  117.    looks stupid.
  118. */
  119.    BYTE i;        
  120.  
  121.    if (lNumColors>16) {
  122.      GpiCreateLogColorTable(target.hpsHighlight,0,LCOLF_RGB,0,0,NULL);
  123.  
  124.      for (i=0; target.fActive; i++) {
  125.        GpiSetColor(target.hpsHighlight,(LONG) i<<16);
  126.        HexDraw(target.hpsHighlight,target.hiStart);
  127.      }
  128.      GpiCreateLogColorTable(target.hpsHighlight,LCOL_RESET,0,0,0,NULL);
  129.    } else
  130.      while (target.fActive) {
  131.        GpiSetColor(target.hpsHighlight,HEX_COLOR);
  132.        HexDraw(target.hpsHighlight,target.hiStart);
  133.        DosSleep(500L);
  134.        if (!target.fActive) break;
  135.        GpiSetColor(target.hpsHighlight,CLR_RED);
  136.        HexDraw(target.hpsHighlight,target.hiStart);
  137.        DosSleep(500L);
  138.      }
  139.  
  140.  
  141. // Redraw the starting hex before exiting
  142.    GpiSetColor(target.hpsHighlight,HEX_COLOR);
  143.    HexDraw(target.hpsHighlight,target.hiStart);
  144.    WinReleasePS(target.hpsHighlight);
  145. }
  146.