home *** CD-ROM | disk | FTP | other *** search
/ MORE WinGames / WINGAMES.iso / winpool / winevent.c < prev    next >
C/C++ Source or Header  |  1992-08-09  |  6KB  |  239 lines

  1.  
  2.  
  3.  
  4.  
  5. /*
  6.  * I. ARIT 1992 Hidirbeyli,AYDIN,TR.  09400 Golden,    CO,   USA. 80401 
  7.  *
  8.  *
  9.  * Copyright (C) 1992 Ismail ARIT 
  10.  *
  11.  * This file  is distributed in the hope that it will be useful, but without any
  12.  * warranty.  No author or distributor accepts responsibility to anyone for
  13.  * the consequences of using it or for whether it serves any particular
  14.  * purpose or works at all. 
  15.  *
  16.  *
  17.  * Everyone is granted permission to copy, modify and redistribute this file
  18.  * under the following conditions: 
  19.  *
  20.  * Permission is granted to anyone to make or distribute copies of the source
  21.  * code, either as received or modified, in any medium, provided that all
  22.  * copyright notices, permission and nonwarranty notices are preserved, and
  23.  * that the distributor grants the recipient permission for further
  24.  * redistribution as permitted by this document. 
  25.  *
  26.  * No part of this program can be used in any commercial product. 
  27.  */
  28.  
  29.  
  30.  
  31.  
  32.  
  33. #include <windows.h>
  34. #include <stdlib.h>
  35. #include <math.h>
  36. #include "definiti.h"
  37.  
  38.  
  39. int             WhoseTurn = MYTURN;
  40.  
  41.  
  42. extern int      WhiteBallOut, BlackBallOut;
  43. extern int      YourScore, MyScore;
  44. extern int      StartX, StartY, NewX, NewY;
  45. extern void     BuildTheTable(HDC hDC);
  46.  
  47.  
  48. extern Distance *
  49. DistanceChecker_Between(Ball * ThisBall, Ball * ThatBall);
  50.  
  51. extern Stick   *PoolStick;
  52. extern Stick   *Trajectory;
  53. extern Coordinator *Dept_Head;
  54.  
  55.  
  56. int             WantsTrajectory = NO;
  57.  
  58.  
  59.  
  60. int             ButtonPressedBefore = NO;
  61. int             pane_x, pane_y;
  62. extern int      LeftButton;
  63.  
  64.  
  65. void
  66. handle_scratch(HDC hdc);    /* defined down here somewhere */
  67. /* need to use in handle_stick() */
  68.  
  69.  
  70. handle_stick(HDC hdc, int button)
  71. {
  72.  
  73.     float          hip;
  74.     float          lenght;
  75.     float          Speed;
  76.     int             i;
  77.     int             Trax, Tray, TraEndx, TraEndy;
  78.  
  79.     Trax = StartX = (int) Dept_Head->Objects[0]->CurrentL.x;
  80.     Tray = StartY = (int) Dept_Head->Objects[0]->CurrentL.y;
  81.  
  82.     NewX = pane_x;
  83.     NewY = pane_y;
  84.     TraEndx = Trax + (Trax - NewX);
  85.     TraEndy = Tray + (Tray - NewY);
  86.  
  87.     /* we don't want to draw on the ball */
  88.     hip = hypot((float) (NewX - StartX), (float) (NewY - StartY));
  89.  
  90.     StartX += (int) (((float) (NewX - StartX) / hip) * (Dept_Head->Objects[0]->radius + 10));
  91.  
  92.     StartY += (int) (((float) (NewY - StartY) / hip) * (Dept_Head->Objects[0]->radius + 10));
  93.  
  94.  
  95.  
  96.     /* left button */
  97.     if (button == TRUE) {
  98.  
  99.         PoolStick->MoveTo(PoolStick, hdc, button, StartX, StartY, NewX, NewY);
  100.         PoolStick->DoneWithTheStick(PoolStick, NO);
  101.         if (WantsTrajectory == YES) {
  102.             Trajectory->MoveTo(Trajectory, hdc, button, Trax, Tray, TraEndx, TraEndy);
  103.             Trajectory->DoneWithTheStick(Trajectory, NO);
  104.         }
  105.         ButtonPressedBefore = YES;
  106.  
  107.     } else if (ButtonPressedBefore) {    /* button is false now */
  108.  
  109.         /* this is to delete cue stick */
  110.         PoolStick->Delete(PoolStick, hdc);
  111.         /*
  112.          * here we are done with the stick, hit the ball now
  113.          */
  114.         PoolStick->DoneWithTheStick(PoolStick, YES);
  115.  
  116.  
  117.         if (WantsTrajectory == YES) {
  118.             /* this is to delete traj.. */
  119.             Trajectory->Delete(Trajectory, hdc);
  120.             Trajectory->DoneWithTheStick(Trajectory, YES);
  121.         }
  122.         ButtonPressedBefore = NO;
  123.  
  124.         lenght = PoolStick->Lenght(PoolStick);
  125.  
  126.         Speed = lenght * (float) SPEEDLIMIT / (float) MAXSTICKLENGHT;
  127.         Dept_Head->Objects[0]->CurrentV.x = -(float) ((NewX
  128.                                 - StartX) / hip)
  129.             * Speed;
  130.         Dept_Head->Objects[0]->CurrentV.y = -(float) ((NewY
  131.                                 - StartY) / hip)
  132.             * Speed;
  133.         Dept_Head->Objects[0]->Stopped = NO;
  134.  
  135.  
  136.         Dept_Head->StartAction(Dept_Head, hdc);
  137.  
  138.  
  139.         /* let's see what the hell happenned  */
  140.         if (WhiteBallOut)
  141.             handle_scratch(hdc);
  142.  
  143.         if (BlackBallOut)
  144.                {
  145.                BuildTheTable(hdc);
  146.                Dept_Head->InitConfig(Dept_Head, hdc);}
  147.  
  148.  
  149.  
  150.  
  151.     }
  152. };
  153.  
  154.  
  155.  
  156. void
  157. handle_white_ball(HDC hdc, int button)
  158. {
  159.     if (!ButtonPressedBefore) {
  160.         if ((pane_x > WHITE_BALL_LIMIT) && (button == TRUE)) {
  161.             Dept_Head->Objects[0]->Hide(Dept_Head->Objects[0], hdc);
  162.  
  163.             Dept_Head->Objects[0]->CurrentL.x = pane_x;
  164.             Dept_Head->Objects[0]->CurrentL.y = pane_y;
  165.  
  166.             Dept_Head->Objects[0]->Show(Dept_Head->Objects[0], hdc);
  167.         }
  168.         /*
  169.          * we need this sucker to avoid fast movement of the mouse /*
  170.          * PoolStick -> Old.x = PoolStick -> New.x = 1000;/* some far
  171.          * place 
  172.          */
  173.         PoolStick->Old.xx = PoolStick->New.xx = 1000;
  174.         PoolStick->Old.y = PoolStick->New.y = 1000;
  175.         PoolStick->Old.yy = PoolStick->New.yy = 1000;
  176.     }
  177. };
  178.  
  179.  
  180. void
  181. handle_scratch(HDC hdc)
  182. {
  183.  
  184.     int             j, Done = NO, i, TryAgain = YES;
  185.     Distance       *theDistance;
  186.     while (TryAgain) {
  187.         TryAgain = NO;
  188.         /*
  189.          * if random()  doesn't work you, fix something  for your
  190.          * machine 
  191.          */
  192.         /*
  193.          * you only need to pick two random numbers for white ball
  194.          * position
  195.          */
  196.  
  197.         randomize();    /* it looks like working on my machine ( DEC
  198.                  * 3100 ) */
  199.  
  200.         do {        /* this thing sets the x position somewhere
  201.                  * between 500 & 650 */
  202.             Dept_Head->Objects[0]->CurrentL.x = (float) (random(500));
  203.             if (Dept_Head->Objects[0]->CurrentL.x > 400)
  204.               { if (Dept_Head->Objects[0]->CurrentL.x < 500)
  205.                  Done = YES; }
  206.  
  207.         } while (!Done);
  208.  
  209.         Done = NO;    /* for the next pick */
  210.  
  211.         do {        /* this thing sets the y position somewhere
  212.                  * between 20 & 400 */
  213.             Dept_Head->Objects[0]->CurrentL.y = (float) (random(400));
  214.             if (Dept_Head->Objects[0]->CurrentL.y > 60 )
  215.               { if( Dept_Head->Objects[0]->CurrentL.y < 300)
  216.                 Done = YES; }
  217.         } while (!Done);
  218.  
  219.  
  220.         /* let's see if this sucker overlaps */
  221.         for (i = 1; i < Dept_Head->NumberOfObjects; i++) {
  222.             theDistance = DistanceChecker_Between(Dept_Head->Objects[0], Dept_Head->Objects[i]);
  223.             if (theDistance->amount > 0.1)
  224.                 TryAgain = YES;    /* this sucker overlaps, try
  225.                          * again */
  226.         }
  227.     }
  228.  
  229.     /* time to refresh ourself and start movin' */
  230.     Dept_Head->Objects[0]->In = YES;
  231.     WhiteBallOut = NO;
  232.     Dept_Head->Objects[0]->Stopped = NO;
  233.     Dept_Head->Objects[0]->ShowOut = NO;
  234.     Dept_Head->Objects[0]->Visible = NO;
  235.     Dept_Head->Objects[0]->NeedOneMoreDrawing = YES;
  236.     Dept_Head->Objects[0]->Show(Dept_Head->Objects[0], hdc);
  237.  
  238. };
  239.