File: snooker.C

package info (click to toggle)
flying 6.20-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 752 kB
  • ctags: 1,873
  • sloc: cpp: 10,966; makefile: 189
file content (245 lines) | stat: -rw-r--r-- 5,737 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#ifndef _global_h
#	include "global.h"
#endif

#ifndef _snooker_h
#	include "snooker.h"
#endif
#ifndef _pocket_
#	include "pocket.h"
#endif
#ifndef _graph_h
#	include "graph.h"
#endif
#ifndef _mover_h
#	include "mover.h"
#endif


//
// Voreinstellungen
//


Snooker::Snooker(double wx, double wy) :
Pool(wx,wy)
{
	InitArea( TableWidth, TableHeight );

	SelectTable(-1);

#ifndef __TURBOC__
	red_col = AddBallColor( "red3" );
	cols[0] = AddBallColor( "yellow1" );
	cols[1] = AddBallColor( "green4" );
	cols[2] = AddBallColor( "brown4" );
	cols[3] = AddBallColor( "blue" );
	cols[4] = AddBallColor( "HotPink3" );
	cols[5] = AddBallColor( "black" );
#else
	red_col = AddBallColor( "red3" );
	floor   = CreateColorMix( table_col, table_line_col );

	cols[5] = AddBallColor( "black" );
	cols[0] = AddBallColor( "yellow1" );
	cols[1] = AddBallColor( "green4" );
	cols[2] = CreateColorMix( red_col, cols[5] );		// braun
	cols[3] = AddBallColor( "blue" );
	cols[4] = CreateColorMix( red_col, cue_col );	// Pink
#endif

	cueball=0;
	color_in_pocket = 0;
	reds_in_pocket  = 0;
}


Snooker::~Snooker() {
	if (cueball) {
		delete cueball;
		for (int c=0;c<6;c++)		delete colored[c];
		for (int r=0;r<15;r++)		delete red[r];
		cueball=0;
	}
}

const Real & Snooker::GetNormalBallSize() const {
	return BallRadius;
}


void Snooker::Triangle( double x, double y )
{
const int count = 5;
int		c=0;
double	cdist = sqrt( 3.*(GetNormalBallSize()+Offset)*(GetNormalBallSize()+Offset) );

//	x-=(cdist*(count-1)/2);
	for( int col=0; col<count; col++ ) {
		for( int row=0; row<=col; row++ ) {
			reddefs[c] = Vec2( x+col*cdist, y+(row-col/2.0)*2*(GetNormalBallSize()+Offset) ); 
			red[c]=new Ball( reddefs[c], GetNormalBallSize() );
			red[c]->state = new BallState( m, red_col, red[c]->P() );
			c++;
		}
	}
}

void Snooker::SetupBalls() {
	defs[0] = Vec2( AreaOffX()+AreaWidth()*0.25,  AreaOffY()+AreaHeight()/2.+14.6 );
	defs[1] = Vec2( AreaOffX()+AreaWidth()*0.25,  AreaOffY()+AreaHeight()/2.-14.6 );
	defs[2] = Vec2( AreaOffX()+AreaWidth()*0.25,  AreaOffY()+AreaHeight()*0.50 );
	defs[3] = Vec2( AreaOffX()+AreaWidth()*0.50,  AreaOffY()+AreaHeight()*0.50 );
	defs[4] = Vec2( AreaOffX()+AreaWidth()*0.75,  AreaOffY()+AreaHeight()*0.50 );
	defs[5] = Vec2( AreaOffX()+AreaWidth()-32.0, AreaOffY()+AreaHeight()*0.50 );

	for (int i=0;i<6;i++) {
		colored[i] = new Ball( defs[i], BallRadius );
		colored[i]->state = new BallState( m, cols[i], colored[i]->P() );
	}

	cuedef  = Vec2( AreaOffX()+AreaWidth()*0.125, AreaOffY()+AreaHeight()*0.375 );
	cueball = new Ball( cuedef, BallRadius );
	cueball->state = new BallState( m, cue_col, cueball->P() );

	tridef  = Vec2(	AreaOffX()+AreaWidth()*0.75+GetNormalBallSize()*2.5,
							AreaOffY()+AreaHeight()/2. );
	Triangle(tridef);
}

void Snooker::InitPlayground() {
	Billard::InitPlayground();
	BallRadius = m->GetActRadius();
	InitTable(6.0);
	SetupBalls();
}

void Snooker::DrawBackground() const {
	Pool::DrawBackground();
	SetBgColor(table_line_col);
	DrawLine( defs[0], defs[1] );
	DrawArc( defs[2], 14.6, 90.0, 180.0 );
	for (int i=0;i<6;i++) 	FillCircle( defs[i], 1.0 );
}

void Snooker::ResetGame() {
int i;
	cueball->SetPV( cuedef );
	for (i=0;i<6;i++)			colored[i]->SetPV( defs[i] );
	for (i=0;i<15;i++)		red[i]->SetPV( reddefs[i] );

	reds_in_pocket  = 0;
	color_in_pocket = 0;

	Billard::ResetGame();
}

// -------------------------------------------------------------------------

SnookerDemo::~SnookerDemo() {}

const Real &SnookerDemo::GetPresetA() const			{ return PresetA; }
const Real &SnookerDemo::GetSlowGranularity() const	{ return SlowGranularity; }

void SnookerDemo::InitPlayground() {
	Billard::InitPlayground();
	BallRadius = m->GetActRadius();
	Billard::InitTable();
	SetupBalls();
	cueball->v = Vec2( shot_speed, shot_speed );
}

void SnookerDemo::DrawBackground() const {
	Billard::DrawBackground();
	SetBgColor(table_line_col);
	DrawLine( defs[0], defs[1] );
	DrawArc( defs[2], 14.6, 90.0, 180.0 );
	for (int i=0;i<6;i++) 	FillCircle( defs[i], 1.0 );
}

////////////////////////////////////////////////////////////////////////////

void Snooker::InPocket( Ball *b ) {
int i;
	for (i=0;i<6;i++) {
		if (b==colored[i]) {
			color_in_pocket |= (1<<i);
			return;
		}
	}
	for (i=0;i<15;i++) {
		if (b==red[i]) {
			reds_in_pocket  |= (1<<i);
			return;
		}
	}
	Pool::InPocket(b);
}

void Snooker::AllBallsStopped() {
	if (color_in_pocket) {
		int	mask=0;
		for (int i=0;i<6;i++) {
			if ((reds_in_pocket!=0x7fff)||((color_in_pocket&mask)!=mask)) {
				if (color_in_pocket & (1<<i)) {
					(void)IsSelectable(colored[i]);
				}
			}
			mask = (mask<<1)|1;
		}
	}
	Pool::AllBallsStopped();
}

int Snooker::IsSelectable(Ball *b) {
int i;
	for (i=0;i<6;i++) {
		if (b==colored[i]) {
			if (color_in_pocket & (1<<i)) {
				if (b->FitsAt(defs[i])) {
					b->SetP(defs[i]);		// reset to default position
				}
				else if (b->FitsAt(defs[5])) {
					b->SetP(defs[5]);		// reset to black position
				}
				else if (i<4) {
					Vec2	newpos;
					b->FitsNextTo(defs[i],Vec2(-1.0,RealZero),&newpos);
					b->SetP(newpos);		// closer to left wall
				}
				else {
					Vec2	newpos;
					b->FitsNextTo(defs[i],Vec2(1.0,RealZero),&newpos);
					b->SetP(newpos);		// closer to right wall
				}

				b->ChgV(Vec2Zero);
				color_in_pocket &= ~(1<<i);		// it's back in the game

				return 0;
			}
			else {
				return 1;
			}
		}
	}
	for (i=0;i<15;i++) {
		if (b==red[i]) {
			if (reds_in_pocket & (1<<i)) {
				Vec2	newpos;
				b->FitsNextTo(tridef,Vec2(1.0,RealZero),&newpos);
				b->SetP(newpos);					// closer to right wall

				b->ChgV(Vec2Zero);
				reds_in_pocket &= ~(1<<i);		// it's back in the game

				return 0;
			}
			else {
				return 1;
			}
		}
	}

	return Pool::IsSelectable(b);
}