



/*
 * I. ARIT 1992 Hidirbeyli,AYDIN,TR.  09400 Golden,    CO,   USA. 80401 
 *
 *
 * Copyright (C) 1992 Ismail ARIT 
 *
 * This file  is distributed in the hope that it will be useful, but without any
 * warranty.  No author or distributor accepts responsibility to anyone for
 * the consequences of using it or for whether it serves any particular
 * purpose or works at all. 
 *
 *
 * Everyone is granted permission to copy, modify and redistribute this file
 * under the following conditions: 
 *
 * Permission is granted to anyone to make or distribute copies of the source
 * code, either as received or modified, in any medium, provided that all
 * copyright notices, permission and nonwarranty notices are preserved, and
 * that the distributor grants the recipient permission for further
 * redistribution as permitted by this document. 
 *
 * No part of this program can be used in any commercial product. 
 */





#include <windows.h>
#include <stdlib.h>
#include <math.h>
#include "definiti.h"


int             WhoseTurn = MYTURN;


extern int      WhiteBallOut, BlackBallOut;
extern int      YourScore, MyScore;
extern int      StartX, StartY, NewX, NewY;
extern void 	BuildTheTable(HDC hDC);


extern Distance *
DistanceChecker_Between(Ball * ThisBall, Ball * ThatBall);

extern Stick   *PoolStick;
extern Stick   *Trajectory;
extern Coordinator *Dept_Head;


int             WantsTrajectory = NO;



int             ButtonPressedBefore = NO;
int             pane_x, pane_y;
extern int      LeftButton;


void
handle_scratch(HDC hdc);	/* defined down here somewhere */
/* need to use in handle_stick() */


handle_stick(HDC hdc, int button)
{

	float          hip;
	float          lenght;
	float          Speed;
	int             i;
	int             Trax, Tray, TraEndx, TraEndy;

	Trax = StartX = (int) Dept_Head->Objects[0]->CurrentL.x;
	Tray = StartY = (int) Dept_Head->Objects[0]->CurrentL.y;

	NewX = pane_x;
	NewY = pane_y;
	TraEndx = Trax + (Trax - NewX);
	TraEndy = Tray + (Tray - NewY);

	/* we don't want to draw on the ball */
	hip = hypot((float) (NewX - StartX), (float) (NewY - StartY));

	StartX += (int) (((float) (NewX - StartX) / hip) * (Dept_Head->Objects[0]->radius + 10));

	StartY += (int) (((float) (NewY - StartY) / hip) * (Dept_Head->Objects[0]->radius + 10));



	/* left button */
	if (button == TRUE) {

		PoolStick->MoveTo(PoolStick, hdc, button, StartX, StartY, NewX, NewY);
		PoolStick->DoneWithTheStick(PoolStick, NO);
		if (WantsTrajectory == YES) {
			Trajectory->MoveTo(Trajectory, hdc, button, Trax, Tray, TraEndx, TraEndy);
			Trajectory->DoneWithTheStick(Trajectory, NO);
		}
		ButtonPressedBefore = YES;

	} else if (ButtonPressedBefore) {	/* button is false now */

		/* this is to delete cue stick */
		PoolStick->Delete(PoolStick, hdc);
		/*
		 * here we are done with the stick, hit the ball now
		 */
		PoolStick->DoneWithTheStick(PoolStick, YES);


		if (WantsTrajectory == YES) {
			/* this is to delete traj.. */
			Trajectory->Delete(Trajectory, hdc);
			Trajectory->DoneWithTheStick(Trajectory, YES);
		}
		ButtonPressedBefore = NO;

		lenght = PoolStick->Lenght(PoolStick);

		Speed = lenght * (float) SPEEDLIMIT / (float) MAXSTICKLENGHT;
		Dept_Head->Objects[0]->CurrentV.x = -(float) ((NewX
							    - StartX) / hip)
			* Speed;
		Dept_Head->Objects[0]->CurrentV.y = -(float) ((NewY
							    - StartY) / hip)
			* Speed;
		Dept_Head->Objects[0]->Stopped = NO;


		Dept_Head->StartAction(Dept_Head, hdc);


		/* let's see what the hell happenned  */
		if (WhiteBallOut)
			handle_scratch(hdc);

		if (BlackBallOut)
		       {
		       BuildTheTable(hdc);
		       Dept_Head->InitConfig(Dept_Head, hdc);}




	}
};



void
handle_white_ball(HDC hdc, int button)
{
	if (!ButtonPressedBefore) {
		if ((pane_x > WHITE_BALL_LIMIT) && (button == TRUE)) {
			Dept_Head->Objects[0]->Hide(Dept_Head->Objects[0], hdc);

			Dept_Head->Objects[0]->CurrentL.x = pane_x;
			Dept_Head->Objects[0]->CurrentL.y = pane_y;

			Dept_Head->Objects[0]->Show(Dept_Head->Objects[0], hdc);
		}
		/*
		 * we need this sucker to avoid fast movement of the mouse /*
		 * PoolStick -> Old.x = PoolStick -> New.x = 1000;/* some far
		 * place 
		 */
		PoolStick->Old.xx = PoolStick->New.xx = 1000;
		PoolStick->Old.y = PoolStick->New.y = 1000;
		PoolStick->Old.yy = PoolStick->New.yy = 1000;
	}
};


void
handle_scratch(HDC hdc)
{

	int             j, Done = NO, i, TryAgain = YES;
	Distance       *theDistance;
	while (TryAgain) {
		TryAgain = NO;
		/*
		 * if random()  doesn't work you, fix something  for your
		 * machine 
		 */
		/*
		 * you only need to pick two random numbers for white ball
		 * position
		 */

		randomize();	/* it looks like working on my machine ( DEC
				 * 3100 ) */

		do {		/* this thing sets the x position somewhere
				 * between 500 & 650 */
			Dept_Head->Objects[0]->CurrentL.x = (float) (random(500));
			if (Dept_Head->Objects[0]->CurrentL.x > 400)
			  { if (Dept_Head->Objects[0]->CurrentL.x < 500)
				 Done = YES; }

		} while (!Done);

		Done = NO;	/* for the next pick */

		do {		/* this thing sets the y position somewhere
				 * between 20 & 400 */
			Dept_Head->Objects[0]->CurrentL.y = (float) (random(400));
			if (Dept_Head->Objects[0]->CurrentL.y > 60 )
			  { if( Dept_Head->Objects[0]->CurrentL.y < 300)
				Done = YES; }
		} while (!Done);


		/* let's see if this sucker overlaps */
		for (i = 1; i < Dept_Head->NumberOfObjects; i++) {
			theDistance = DistanceChecker_Between(Dept_Head->Objects[0], Dept_Head->Objects[i]);
			if (theDistance->amount > 0.1)
				TryAgain = YES;	/* this sucker overlaps, try
						 * again */
		}
	}

	/* time to refresh ourself and start movin' */
	Dept_Head->Objects[0]->In = YES;
	WhiteBallOut = NO;
	Dept_Head->Objects[0]->Stopped = NO;
	Dept_Head->Objects[0]->ShowOut = NO;
	Dept_Head->Objects[0]->Visible = NO;
	Dept_Head->Objects[0]->NeedOneMoreDrawing = YES;
	Dept_Head->Objects[0]->Show(Dept_Head->Objects[0], hdc);

};
