home *** CD-ROM | disk | FTP | other *** search
Text File | 1986-09-06 | 3.2 KB | 124 lines | [TEXT/MACA] |
- /*
- * grid2.c - misc routines supporting the Tablut board grid
- */
-
- #include <quickdraw.h>
- #include <window.h>
- #include <memory.h>
- #include "tablut.h"
-
- /*
- * gridtopoint() - convert the given grid coordinates to window coordinates.
- * NOTE: this routine is also used to calculate off-board positions.
- */
- gridtopoint(placep)
- Point *placep; /* the grid coords to be converted in place */
- {
- placep->h *= squaredim.h;
- placep->v *= squaredim.v;
- AddPt(pass(boardcenter), placep);
- }
-
- /*
- * pointtogrid() - convert the given window coordinates to grid coordinates.
- * Points outside the board convert to NOGRID.
- * The rounding operation is done in a strange way (keeping coordinates
- * positive before rounding) because this machine truncates negative
- * numbers toward zero rather than truncating down.
- *
- */
- pointtogrid(placep)
- Point *placep; /* the window coords to be converted in place */
- {
- placep->h = (placep->h - boardcenter.h
- + squaredim.h / 2 + squaredim.h * 10) / squaredim.h - 10;
- placep->v = (placep->v - boardcenter.v
- + squaredim.v / 2 + squaredim.v * 10) / squaredim.v - 10;
- if (!(placep->h >= -4 && placep->h <= 4) ||
- !(placep->v >= -4 && placep->v <= 4)) {
- placep->h = NOGRID;
- placep->v = NOGRID;
- }
- }
-
- /*
- * gridtorect() - given a point in grid coordinates, set the rectangle
- * to one that will enclose that board-square.
- */
- gridtorect(gp, rp)
- Point *gp; /* point to convert */
- Rect *rp; /* where to put the result */
- {
- Point wpoint;
-
- wpoint.h = gp->h; wpoint.v = gp->v;
- gridtopoint(&wpoint);
- rp->left = wpoint.h - (squaredim.h - SQUARESEP) / 2;
- rp->top = wpoint.v - (squaredim.v - SQUARESEP) / 2;
- rp->right = rp->left + squaredim.h - SQUARESEP;
- rp->bottom = rp->top + squaredim.v - SQUARESEP;
- }
-
- /*
- * findpiece() - given a local point, find the first piece (if any) that
- * is touched by that point.
- */
- int /* returns the index of the found piece (or -1 if none) */
- findpiece(pointp)
- Point *pointp;
- {
- struct pieceimage *p;
-
- for (p = &piece[0]; p < &piece[NUMPIECES]; ++p) {
- if (PtInRect(pass(*pointp), &p->prevlook.bounds)) {
- if (!insetup) {
- if (!onboard(p)) {
- continue; /* the piece is not on the board */
- }
- if ((blacksmove() && !blackpiece(p)) ||
- (!blacksmove() && !whitepiece(p))) {
- continue; /* the piece is not the right color */
- }
- }
- return((int)(p - &piece[0]));
- }
- }
- return(-1);
- }
-
- /*
- * blackpiece(), whitepiece() - "boolean" functions true if the given
- * pointer points to a black (white) piece.
- */
- int
- blackpiece(p)
- struct pieceimage *p;
- {
- return(&piece[FIRSTMUSC] <= p && p <= &piece[LASTMUSC]);
- }
-
- int
- whitepiece(p)
- struct pieceimage *p;
- {
- return(p == &piece[THEKING] ||
- (&piece[FIRSTSWEDE] <= p && p <= &piece[LASTSWEDE]));
- }
-
- /*
- * classbase() - given a pointer to a piece, return the base index
- * of that type of piece. Useful for finding out what kind of piece
- * we have.
- */
- int /* base index (e.g., FIRSTSWEDE) */
- classbase(p)
- struct pieceimage *p;
- {
- if (&piece[FIRSTSWEDE] <= p && p <= &piece[LASTSWEDE]) {
- return(FIRSTSWEDE);
- } else if (&piece[FIRSTMUSC] <= p && p <= &piece[LASTMUSC]) {
- return(FIRSTMUSC);
- }
- return(THEKING);
- }
-