File: carrom.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 (425 lines) | stat: -rw-r--r-- 11,650 bytes parent folder | download | duplicates (3)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#ifndef _global_h
#	include "global.h"
#endif

#ifndef _carrom_h
#	include "carrom.h"
#endif
#ifndef _wall_h
#	include "wall.h"
#endif
#ifndef _graph_h
#	include "graph.h"
#endif
#ifndef _mover_h
#	include "mover.h"
#endif
#ifndef _arcs_h
#	include "arcs.h"
#endif
#ifndef _pocket_h
#	include "pocket.h"
#endif



void Carrom::InitArea( double wx, double wy ) {
	area_width	= wx;
	area_height	= wy;
	area_off_x = (MaxX()-ToPocket-PocketWidth-area_width)/2.0;
	area_off_y = (MaxY()-area_height)/2.0;
}


Carrom::Carrom(double wx, double wy) :
	Game(wx+2.0*FrameOffset+PocketWidth+ToPocket,wy+2.0*FrameOffset)
{
	InitArea(wx,wy);

	table_col	= SetMainBgColor( "Tan" );		// GlobalBackground

	table_black	= AddBgColor( "black" );
	table_red	= AddBgColor( "Brown" );
	table_green = AddBgColor( "DarkGreen" );
	pocket_col	= CreateColorMix( table_col, table_green );

#ifndef __TURBOC__
	white_col	= AddBallColor( "DarkGoldenrod" );
	black_col	= AddBallColor( "black" );
	queen_col	= AddBallColor( "red" );
	striker_col	= AddBallColor( "DeepSkyBlue" );
#else
	white_col	= AddBallColor( "DarkGoldenrod" );
	floor			= CreateColorMix( table_green, table_black );

	black_col	= AddBallColor( "black" );
	queen_col	= AddBallColor( "red" );
	striker_col	= CreateColorMix( white_col, black_col );
#endif

	AddShadeColor( "grey", 1, 2 );
	SetCursorColor( "White" );
	striker = 0;
	striker_in_pocket = 0;
	queen_in_pocket   = 0;
	whites_in_pocket  = 0;
	blacks_in_pocket  = 0;

	keeper = new StackKeeper(PocketWidth,PocketFrame,table_black);
}

Carrom::~Carrom() {
	if (striker) {
		delete queen;
		delete striker;
		for (int i=0;i<9;i++) {
			delete white[i];
			delete black[i];
		}
		striker=0;
	}

	for (int i=0;i<4;i++) {
		if (e[i][0])		delete e[i][0];
		if (e[i][1])		delete e[i][1];
		if (p[i])			delete p[i];
		if (w[i])			delete w[i];
	}

	if (mstone)		delete mstone;
	if (mstriker)	delete mstriker;
}

const Real & Carrom::GetPresetA() const			{ return PresetA; }
const Real & Carrom::GetPresetHaft() const		{ return PresetHaft; }
const Real & Carrom::GetSlowGranularity() const	{ return SlowGranularity; }

const Real & Carrom::GetNormalBallSize() const	{ return StoneRadius; }

const Real & Carrom::GetChargeGranularity() const	{ return ChargeGranularity; }
const Real & Carrom::GetChargeSpeed() const			{ return ChargeSpeed; }
const Real & Carrom::GetMaxCharge() const			{ return MaxCharge; }
const Real & Carrom::GetShootTime() const			{ return ShootTime; }

const Real Carrom::AreaOffX() const			{ return area_off_x; }
const Real Carrom::AreaOffY() const			{ return area_off_y; }
const Real Carrom::AreaWidth() const		{ return area_width; }
const Real Carrom::AreaHeight() const		{ return area_height; }

const Real Carrom::TAreaOffX() const		{ return area_off_x-Cushion; }
const Real Carrom::TAreaOffY() const		{ return area_off_y-Cushion; }
const Real Carrom::TAreaWidth() const		{ return area_width+2.0*Cushion; }
const Real Carrom::TAreaHeight() const		{ return area_height+2.0*Cushion; }

void Carrom::Setup( double x, double y )
{
static int	nc[5] = { 3, 4, 5, 4, 3 };		// Steine pro Spalte
static t[19] = { -1,  1, -2,					// Steinverteilung
					 2, -3,  3,  4,
				 -4,  5,  0, -5, -6,
					 6, -7,  7,  8,
					  -8,  9, -9 };

const int count = 5;
int		c=0;
double	cdist = sqrt( 3.*(GetNormalBallSize()+Offset)*(GetNormalBallSize()+Offset) );
Ball	*r;

	x-=(cdist*(count-1)/2.0);
	for( int col=0; col<count; col++ ) {
		for( int row=0; row<nc[col]; row++ ) {
			r=new Ball( x+col*cdist, y+(row-nc[col]/2.0+0.5)*2.0*(GetNormalBallSize()+Offset), 0.0, 0.0, GetNormalBallSize(), StoneWeight );
			if (t[c]>0) {
				r->state = new BallState( mstone, white_col, r->P() );
				white[t[c]-1] = r;
			}
			else if (t[c]<0) {
				r->state = new BallState( mstone, black_col, r->P() );
				black[-t[c]-1] = r;
			}
			else {
				r->state = new BallState( mstone, queen_col, r->P() );
				queen = r;
			}
			c++;
		}
	}
}




void Carrom::InitPlayground() {
int i;

	mstone = new DiscMover( GetNormalBallSize() );
	mstone->Init();
	DBG2(UnixTrace, "Carrom: Stone-Radius set back from %g to %g\n",
					(double)StoneRadius, (double)mstone->GetActRadius() );
	StoneRadius = mstone->GetActRadius();

	mstriker = new BallMover( StrikerRadius );
	mstriker->Init();
	DBG2(UnixTrace, "Carrom: Striker-Radius set back from %g to %g\n",
					(double)StrikerRadius, (double)mstriker->GetActRadius() );
	StrikerRadius = mstriker->GetActRadius();

	Game::InitPlayground();
#if (0)
#ifdef DEBUG
	if (debug&ShowLight) {
		mstone->CreateLightWindow();
	}
#endif
#endif

	p[0] = new Pocket( AreaOffX(), AreaOffY(), PocketRadius );
	p[1] = new Pocket( AreaOffX()+AreaWidth(), AreaOffY(), PocketRadius );
	p[2] = new Pocket( AreaOffX()+AreaWidth(), AreaOffY()+AreaHeight(), PocketRadius );
	p[3] = new Pocket( AreaOffX(), AreaOffY()+AreaHeight(),	PocketRadius );

	for (i=0;i<4;i++) {
		e[i][0] = new StaticBall( p[i]->P()+PocketRadius*Vec2(1.0,RealZero).TurnAngleDeg(-90.*i),	0.0 );
		e[i][1] = new StaticBall( p[i]->P()+PocketRadius*Vec2(1.0,RealZero).TurnAngleDeg(-90.*i-90.),0.0 );
	}
	for (i=0;i<4;i++) {
		w[i] = new Wall( e[i][0]->P(), e[(i+1)%4][1]->P() );
	}

	mid = Vec2( AreaOffX()+AreaWidth()/2.0, AreaOffY()+AreaHeight()/2.0 );
	Vec2 d(1.0,0.0);
	for (i=0;i<4;i++) {
		base[i][0] = mid+25.*d.TurnAngleDeg(-90.*i)+22.*d.TurnAngleDeg(90.*(i+1));
		base[i][1] = mid+25.*d.TurnAngleDeg(-90.*i)+22.*d.TurnAngleDeg(90.*(i-1));
	}
	Setup(AreaOffX()+AreaWidth()/2.0, AreaOffY()+AreaHeight()/2.0);

	striker = new Ball( (base[1][0]+base[1][1])/2.0, StrikerRadius, StrikerWeight );
	striker->state = new BallState( mstriker, striker_col, striker->P() );
}




void Carrom::DrawBackground() const {
int i;
int j;

const Real	w8  =  45.0;
const Real	w16 =  w8/2.0;
const Real	w32 = w16/2.;
const Real	w64 = w32/2.;

Vec2	d(1.0,0.0);
Vec2	p1,p2,p3,p4;

	Game::DrawBackground();

// normales Spielfeld
	SetBgColor( table_black );
	FillRectangle( TAreaOffX(), TAreaOffY(), TAreaWidth(), TAreaHeight() );
	SetBgColor(0);
	FillRectangle( AreaOffX(), AreaOffY(), AreaWidth(), AreaHeight() );

//
// Randelemente und Taschen aufbauen (4er-Schleife)
//
	for (i=0;i<4;i++) {
		SetBgColor(pocket_col);
		FillCircle( p[i]->P(), p[i]->R() );
	Real	ang = w8*Real(2*i+1);
		SetBgColor(table_black);
		DrawLine( mid+16.0*d.TurnAngleDeg(ang), mid+42.0*d.TurnAngleDeg(ang) );
		DrawArc(  mid+19.5*d.TurnAngleDeg(ang), 3.5, ang+30.0, 300. );

		SetBgColor( table_black );
		p1 = base[i][0] + 1.8*d.TurnAngleDeg(90.*i);
		p2 = base[i][0] + 1.3*d.TurnAngleDeg(90.*i);
		p3 = base[i][1] + 1.3*d.TurnAngleDeg(90.*i);
		p4 = base[i][1] + 1.8*d.TurnAngleDeg(90.*i);
		FillPoly( 4, &p1, &p2, &p3, &p4 );
		p1 = base[i][0] - 1.8*d.TurnAngleDeg(90.*i);
		p2 = base[i][0] - 1.6*d.TurnAngleDeg(90.*i);
		p3 = base[i][1] - 1.6*d.TurnAngleDeg(90.*i);
		p4 = base[i][1] - 1.8*d.TurnAngleDeg(90.*i);
		FillPoly( 4, &p1, &p2, &p3, &p4 );

		for (j=0;j<2;j++) {
			SetBgColor( table_black );			FillCircle( base[i][j], 3.6/2 );
			SetBgColor( table_col );			FillCircle( base[i][j], 3.3/2 );
			SetBgColor( table_red );			FillCircle( base[i][j], 3.5/2 );
		}
	}

//
// Kreise in der Mitte ziehen
//
	SetBgColor( table_black );  	FillCircle( mid, 21.5 / 2 );
	SetBgColor( table_red );  		FillCircle( mid, 21.2 / 2 );
	SetBgColor( table_black );  	FillCircle( mid, 20.8 / 2 );
	SetBgColor( table_col );	  	FillCircle( mid, 20.5 / 2 );

	SetBgColor( table_black );  	FillCircle( mid, 16.1 / 2 );
	SetBgColor( table_col );  		FillCircle( mid, 15.9 / 2 );

	SetBgColor( table_black );  	FillCircle( mid, 15.6 / 2 );
	SetBgColor( table_red );  		FillCircle( mid, 15.5 / 2 );
	SetBgColor( table_black );  	FillCircle( mid, 15.1 / 2 );
	SetBgColor( table_col );	  	FillCircle( mid, 15.0 / 2 );

	SetBgColor( table_black );  	FillCircle( mid, 14.60 / 2 );
	SetBgColor( table_col );  		FillCircle( mid, 14.35 / 2 );

//
// mittleren Kreis in 8 Segmenten aufbauen
//
	for (i=0;i<8;i++) {
		d = Vec2(1.0,RealZero).TurnAngleDeg( w8 * (double)i );

		SetBgColor(table_black);
		for (double r=0.;r<4.;r++) {
			p1 = mid+8.0*d.TurnAngleDeg(r*w32);
			p2 = mid+8.7*d.TurnAngleDeg(r*w32+5.0);
			p3 = mid+8.7*d.TurnAngleDeg(r*w32-5.0);
			FillPoly(3, &p1, &p2, &p3 );
			Real	ang=w8*Real(i)+w32*r+w64;
			DrawArc(mid+9.*Vec2(1.0,RealZero).TurnAngleDeg(ang),0.95,ang-90.,180.);
		}

		SetBgColor(table_red);
		FillCircle( mid+3.0*d.TurnAngleDeg(w16), 0.7 );
		for (j=1;j<8;j+=4)	FillCircle(mid+9.0*d.TurnAngleDeg(w64*j),0.6);
		SetBgColor(table_green);
		for (j=3;j<8;j+=4)	FillCircle(mid+9.0*d.TurnAngleDeg(w64*j),0.6);

			p1=mid+3.0*d;
			p2=mid+4.0*d.TurnAngleDeg( 10.0);
			p3=mid+8.5*d;
			p4=mid+4.0*d.TurnAngleDeg(-10.0);
		FillPoly( 4, &p1, &p2, &p3, &p4 );
			p2 = mid+1.5*d.TurnAngleDeg( 5.0*w16);
			p3 = mid+1.5*d.TurnAngleDeg(-5.0*w16);
		FillPoly( 3, &p1, &p2, &p3 );

		Vec2	s[10];		// Stern
			s[0] = mid+4.7*d.TurnAngleDeg(w16);
			s[1] = mid+4.3*d.TurnAngleDeg(w16+12.);
			s[2] = mid+5.2*d.TurnAngleDeg(w16+9.);
			s[3] = mid+6.2*d.TurnAngleDeg(w16+13.);
			s[4] = mid+6.2*d.TurnAngleDeg(w16+5.);

			s[5] = mid+7.2*d.TurnAngleDeg(w16);
			s[6] = mid+6.2*d.TurnAngleDeg(w16-5.);
			s[7] = mid+6.2*d.TurnAngleDeg(w16-13.);
			s[8] = mid+5.2*d.TurnAngleDeg(w16-9.);
			s[9] = mid+4.3*d.TurnAngleDeg(w16-12.);
		FillPoly( 10, s );
	}

	SetBgColor(table_red);
	FillCircle(mid,1.2);
}

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

void Carrom::InPocket( Ball *b ) {
	Game::InPocket(b);
	if (b==striker)		{	striker_in_pocket = 1;	return;	}
	if (b==queen)			{	queen_in_pocket   = 1;	return;	}
	for (int i=0;i<9;i++) {
		if (b==white[i])	{	whites_in_pocket |= (1<<i);	return;	}
		if (b==black[i])	{	blacks_in_pocket |= (1<<i);	return;	}
	}
}

void Carrom::AllBallsStopped() {
	Game::AllBallsStopped();
	(void)IsSelectable(striker);		// back on the table
}

void Carrom::ResetToCenter(Ball *b) {
Vec2	center(AreaOffX()+AreaWidth()/2.0, AreaOffY()+AreaHeight()/2.0);

	Vec2	newpos;
	b->FitsNextTo(center,Vec2(1.0,RealZero),&newpos);
	b->SetP(newpos);					// closer to right wall

	b->ChgV(Vec2Zero);
}

int Carrom::IsSelectable(Ball *b) {
	if (b==striker) {
		if (striker_in_pocket) {
			Vec2	inspos(AreaOffX()+AreaWidth()-2.0*StrikerRadius-EPS,
														AreaOffY()+AreaHeight()/2.0);
			Vec2	newpos;

			b->FitsNextTo(inspos,Vec2(-1.0,RealZero),&newpos);
			striker_in_pocket = 0;
			b->SetP(newpos);
			b->ChgV(Vec2Zero);

			return 0;
		}
		else {
			return 1;
		}
	}
	for (int i=0;i<9;i++) {
		if ( b==white[i] ) {
			if (whites_in_pocket&(1<<i)) {
					ResetToCenter(b);
					whites_in_pocket &= ~(1U<<i);
					return 0;
			}
			else	return 1;
		}
		if ( b==black[i] ) {
			if (blacks_in_pocket&(1<<i)) {
					ResetToCenter(b);
					blacks_in_pocket &= ~(1U<<i);
					return 0;
			}
			else	return 1;
		}
	}
	if ( b==queen ) {
		if (queen_in_pocket) {
				ResetToCenter(b);
				queen_in_pocket = 0;
				return 0;
		}
		else	return 1;
	}

	return Game::IsSelectable(b);
}

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

CarromDemo::~CarromDemo() {}

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

void CarromDemo::InitPlayground() {
	Carrom::InitPlayground();
	for (int i=0;i<4;i++) {
		delete e[i][0];	e[i][0]=0L;
		delete e[i][1];	e[i][1]=0L;
		Vec2	pos( p[i]->P() );
		delete p[i];
		p[i] = new StaticBall( pos, PocketRadius );
	}

	striker->v = Vec2(1.0,-shot_speed);
}

void CarromDemo::DrawBackground() const {
	Carrom::DrawBackground();
	SetBgColor(table_black);
	for (int i=0;i<4;i++) {
		FillCircle( p[i]->P(), p[i]->R() );
	}
}