home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / tblt / tblt⁄grid2.c < prev    next >
Encoding:
Text File  |  1986-09-06  |  3.2 KB  |  124 lines  |  [TEXT/MACA]

  1. /*
  2.  * grid2.c - misc routines supporting the Tablut board grid
  3.  */
  4.  
  5. #include <quickdraw.h>
  6. #include <window.h>
  7. #include <memory.h>
  8. #include "tablut.h"
  9.  
  10. /*
  11.  * gridtopoint() - convert the given grid coordinates to window coordinates.
  12.  *  NOTE: this routine is also used to calculate off-board positions.
  13.  */
  14. gridtopoint(placep)
  15. Point *placep;        /* the grid coords to be converted in place */
  16. {
  17.     placep->h *= squaredim.h;
  18.     placep->v *= squaredim.v;
  19.     AddPt(pass(boardcenter), placep);
  20. }
  21.  
  22. /*
  23.  * pointtogrid() - convert the given window coordinates to grid coordinates.
  24.  *  Points outside the board convert to NOGRID.
  25.  * The rounding operation is done in a strange way (keeping coordinates
  26.  * positive before rounding) because this machine truncates negative
  27.  * numbers toward zero rather than truncating down.
  28.  *
  29.  */
  30. pointtogrid(placep)
  31. Point *placep;        /* the window coords to be converted in place */
  32. {
  33.     placep->h = (placep->h - boardcenter.h
  34.       + squaredim.h / 2 + squaredim.h * 10) / squaredim.h - 10;
  35.     placep->v = (placep->v - boardcenter.v
  36.       + squaredim.v / 2 + squaredim.v * 10) / squaredim.v - 10;
  37.     if (!(placep->h >= -4 && placep->h <= 4) ||
  38.       !(placep->v >= -4 && placep->v <= 4)) {
  39.     placep->h = NOGRID;
  40.     placep->v = NOGRID;
  41.     }
  42. }
  43.  
  44. /*
  45.  * gridtorect() - given a point in grid coordinates, set the rectangle
  46.  *  to one that will enclose that board-square.
  47.  */
  48. gridtorect(gp, rp)
  49. Point *gp;    /* point to convert */
  50. Rect *rp;    /* where to put the result */
  51. {
  52.     Point wpoint;
  53.  
  54.     wpoint.h = gp->h; wpoint.v = gp->v;
  55.     gridtopoint(&wpoint);
  56.     rp->left = wpoint.h - (squaredim.h - SQUARESEP) / 2;
  57.     rp->top  = wpoint.v - (squaredim.v - SQUARESEP) / 2;
  58.     rp->right = rp->left + squaredim.h - SQUARESEP;
  59.     rp->bottom = rp->top + squaredim.v - SQUARESEP;
  60. }
  61.  
  62. /*
  63.  * findpiece() - given a local point, find the first piece (if any) that
  64.  *  is touched by that point.
  65.  */
  66. int    /* returns the index of the found piece (or -1 if none)    */
  67. findpiece(pointp)
  68. Point *pointp;
  69. {
  70.     struct pieceimage *p;
  71.  
  72.     for (p = &piece[0]; p < &piece[NUMPIECES]; ++p) {
  73.     if (PtInRect(pass(*pointp), &p->prevlook.bounds)) {
  74.         if (!insetup) {
  75.         if (!onboard(p)) {
  76.             continue;    /* the piece is not on the board */
  77.         }
  78.         if ((blacksmove() && !blackpiece(p)) ||
  79.           (!blacksmove() && !whitepiece(p))) {
  80.             continue;    /* the piece is not the right color */
  81.         }
  82.         }
  83.         return((int)(p - &piece[0]));
  84.     }
  85.     }
  86.     return(-1);
  87. }
  88.  
  89. /*
  90.  * blackpiece(), whitepiece() - "boolean" functions true if the given
  91.  *  pointer points to a black (white) piece.
  92.  */
  93. int
  94. blackpiece(p)
  95. struct pieceimage *p;
  96. {
  97.     return(&piece[FIRSTMUSC] <= p && p <= &piece[LASTMUSC]);
  98. }
  99.  
  100. int
  101. whitepiece(p)
  102. struct pieceimage *p;
  103. {
  104.     return(p == &piece[THEKING] ||
  105.       (&piece[FIRSTSWEDE] <= p && p <= &piece[LASTSWEDE]));
  106. }
  107.  
  108. /*
  109.  * classbase() - given a pointer to a piece, return the base index
  110.  *  of that type of piece. Useful for finding out what kind of piece
  111.  *  we have.
  112.  */
  113. int    /* base index (e.g., FIRSTSWEDE) */
  114. classbase(p)
  115. struct pieceimage *p;
  116. {
  117.     if (&piece[FIRSTSWEDE] <= p && p <= &piece[LASTSWEDE]) {
  118.     return(FIRSTSWEDE);
  119.     } else if (&piece[FIRSTMUSC] <= p && p <= &piece[LASTMUSC]) {
  120.     return(FIRSTMUSC);
  121.     }
  122.     return(THEKING);
  123. }
  124.