home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume6 / xplumb / part01 / flow.C < prev    next >
C/C++ Source or Header  |  1990-04-11  |  5KB  |  185 lines

  1. /*
  2.  * Copyright 1990 Digital Equipment Corporation
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted,
  6.  * provided that the above copyright notice appear in all copies and that
  7.  * both that copyright notice and this permission notice appear in
  8.  * supporting documentation, and that the name of Digital Equipment
  9.  * Corporation not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  Digital Equipment Corporation makes no representations
  12.  * about the suitability of this software for any purpose.  It is
  13.  * provided "as is" without express or implied warranty.
  14.  *
  15.  * DIGITAL EQUIPMENT CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16.  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR
  18.  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20.  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Terry Weissman
  24.  *          weissman@wsl.dec.com
  25.  */
  26.  
  27. #include "plumbing.h"
  28.  
  29. static int fx, fy;
  30. static Direction indir, outdir;
  31. static int perc;        // Percentage through * 100.
  32. static int deltaperc;
  33. static int deltatime;
  34.  
  35. static int DirX[4] = {0, -1, 1, 0};
  36. static int DirY[4] = {-1, 0, 0, 1};
  37.  
  38. static void Paint(int fx, int fy, Direction indir, Direction outdir,
  39.           int perc) {
  40.     Scrn scrn;
  41.     Display *dpy;
  42.     Window window;
  43.     int i, ssize, flowwidth, lssize, hssize, numpix, ipix, opix, x0, y0;
  44.     GC gc;
  45.     for (i=0 ; i<numscreens ; i++) {
  46.     scrn = screen[i];
  47.     dpy = scrn->GetDpy();
  48.     window = scrn->GetWindow();
  49.     ssize = scrn->GetSSize();
  50.     flowwidth = scrn->GetFlowWidth();
  51.     x0 = scrn->GetBoardX() + ssize * fx;
  52.     y0 = scrn->GetBoardY() + ssize * fy;
  53.     lssize = (ssize - flowwidth) / 2;
  54.     hssize = (ssize + flowwidth) / 2;
  55.     gc = scrn->GetFlowGC();
  56.     numpix = perc * ssize / 10000;
  57.     if (numpix <= 0) continue;
  58.     ipix = Min(numpix, hssize);
  59.     if (fx != startx || fy != starty) {
  60.         switch (indir) {
  61.         case Up:
  62.         XFillRectangle(dpy, window, gc, x0 + lssize, y0 + 0,
  63.                    flowwidth, ipix);
  64.         break;
  65.         case Down:
  66.         XFillRectangle(dpy, window, gc, x0 + lssize, y0 + ssize - ipix,
  67.                    flowwidth, ipix);
  68.         break;
  69.         case Left:
  70.         XFillRectangle(dpy, window, gc, x0 + 0, y0 + lssize,
  71.                    ipix, flowwidth);
  72.         break;
  73.         case Right:
  74.         XFillRectangle(dpy, window, gc, x0 + ssize - ipix, y0 + lssize,
  75.                    ipix, flowwidth);
  76.         break;
  77.         case BadDir:
  78.         Punt("Bad in direction in Paint!");
  79.         }
  80.     }
  81.     opix = numpix - ipix;
  82.     if (opix <= 0) continue;
  83.     switch(outdir) {
  84.       case Up:
  85.         XFillRectangle(dpy, window, gc, x0 + lssize, y0 + lssize - opix,
  86.                flowwidth, opix);
  87.         break;
  88.       case Down:
  89.         XFillRectangle(dpy, window, gc, x0 + lssize, y0 + hssize,
  90.                flowwidth, opix);
  91.         break;
  92.       case Left:
  93.         XFillRectangle(dpy, window, gc, x0 + lssize - opix, y0 + lssize,
  94.                opix, flowwidth);
  95.         break;
  96.       case Right:
  97.         XFillRectangle(dpy, window, gc, x0 + hssize, y0 + lssize,
  98.                opix, flowwidth);
  99.         break;
  100.       case BadDir:
  101.         Punt("Bad out direction in Paint!");
  102.     }
  103.     }
  104. }
  105.  
  106.  
  107. static void TripFlow(void *) {
  108.     TimerAddTimeout(deltatime, TripFlow, NULL);
  109.     perc += deltaperc;
  110.     if (perc >= 10000) {
  111.     Paint(fx, fy, indir, outdir, 10000);
  112.     perc -= 10000;
  113.     fx += DirX[outdir];
  114.     fy += DirY[outdir];
  115.     if (fx < 0 || fx >= HSQUARES || fy < 0 || fy >= VSQUARES) {
  116.         GameOver();
  117.         return;
  118.     }
  119.     indir = OppositeDirection(outdir);
  120.     outdir = board[fx][fy].GetPiece()->GetOutDir(indir);
  121.     if (outdir == BadDir) {
  122.         GameOver();
  123.         return;
  124.     }
  125.     if (numtoget > 0) numtoget--;
  126.     score->AddScore(LevelFlowScore());
  127.     if (!board[fx][fy].GetRemovable()) {
  128.         score->AddScore(LevelCrossOverScore());
  129.     }
  130.     board[fx][fy].ClearRemovable();
  131.     }
  132.     Paint(fx, fy, indir, outdir, perc);
  133. }
  134.  
  135.  
  136. void FlowRefigureDeltas() {
  137.     int i;
  138.     int ssize = MINSSIZE ;
  139.     for (i=0 ; i<numscreens ; i++) {
  140.     if (ssize < screen[i]->GetSSize())
  141.         ssize = screen[i]->GetSSize();
  142.     }
  143.     deltaperc = 10000 / ssize;
  144.     deltatime = (fastflow ? 250 : LevelTimePerPiece()) / ssize;
  145. }
  146.  
  147.  
  148.  
  149. void FlowRepaint() {
  150.     int x = startx;
  151.     int y = starty;
  152.     Direction in = startdir;
  153.     Direction out = startdir;
  154.     while (x != fx || y != fy || in != indir || out != outdir) {
  155.     Paint(x, y, in, out, 10000);
  156.     x += DirX[out];
  157.     y += DirY[out];
  158.     if (x < 0 || y < 0 || x >= HSQUARES || y >= VSQUARES) return;
  159.     in = OppositeDirection(out);
  160.     out = board[x][y].GetPiece()->GetOutDir(in);
  161.     }
  162.     Paint(fx, fy, indir, outdir, perc);
  163. }
  164.  
  165.  
  166.  
  167. void FlowFast() {
  168.     if (score->GetCountDown() > 0) {
  169.     score->SetCountDown(0);
  170.     }
  171.     fastflow = True;
  172.     FlowRefigureDeltas();
  173. }
  174.  
  175.  
  176. void FlowStart() {
  177.     fx = startx;
  178.     fy = starty;
  179.     indir = outdir = startdir;
  180.     perc = 5000;
  181.     fastflow = False;
  182.     FlowRefigureDeltas();
  183.     TripFlow(NULL);
  184. }
  185.