summaryrefslogtreecommitdiffstats
path: root/kue/utility.cpp
blob: 7565228ad68469a68a17e2bbf14c57cc5d08dbbb (plain)
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
#include <kdebug.h>
#include <krandomsequence.h>
#include <stdlib.h>

#include "physics.h"
#include "main.h"
#include "utility.h"
#include "global.h"

const double PLAY_AREA_WIDTH = 0.254;
const double PLAY_AREA_HEIGHT = 0.127;

void rackTriangle()
{
	int next_ball = 0;
	unsigned int rack_order[] = { 1, 2, 3, 4, 8, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15 };

	double field_width = KueGlobal::physics()->fieldWidth();
	double field_height = KueGlobal::physics()->fieldHeight();

	// The initial spacing of the billiards
	const double initial_spacing = (0.00286 * 2.0);

	// The location of the cue and rack lines
	double rack_line = (field_width * 3.0) / 4.0;

	// The location of the mid line
	double mid_line = field_height / 2.0;

	KRandomSequence r;

	for (int x = 0;x < 15;x++)
	{
		// The eight-ball must stay where it is
		if (rack_order[x] != 8)
		{
			int swap_index;

			swap_index = r.getLong(14);

			if (rack_order[swap_index] != 8) {
				int temp = rack_order[x];

				rack_order[x] = rack_order[swap_index];
				rack_order[swap_index] = temp;
			}
		}

	}

	// These loops build a triangle out of the billiards
	for (int row = 0;row < 5;row++)
	{
		double row_start = mid_line - ((row / 2.0) * initial_spacing);

		for (int pos = 0;pos <= row;pos++)
		{
			// Calculate its position
			double x = rack_line + (row * initial_spacing);
			double y = row_start + (pos * initial_spacing);

			// Create the billiard
			KueBilliard billiard(x, y, KueUtility::defaultBilliardRadius(), KueUtility::textureForBilliard(rack_order[next_ball]));

			// Actually place it
			KueGlobal::physics()->insertBilliard(rack_order[next_ball], billiard);
			next_ball++;
		}
	}
}

void rackDiamond() 
{
	int next_ball = 0;
	unsigned int rack_order[] = { 1, 2, 3, 4, 8, 5, 6, 7, 9 };

	double field_width = KueGlobal::physics()->fieldWidth();
	double field_height = KueGlobal::physics()->fieldHeight();

	// The initial spacing of the billiards
	const double initial_spacing = (0.00286 * 2.0);
	// The location of the cue and rack lines
	double rack_line = (field_width * 3.0) / 4.0;
	// The location of the mid line
	double mid_line = field_height / 2.0;

	KRandomSequence r;
	
	// Randomize the billiard order of billiards [1] -> [7]
	for (int x = 1;x < 8;x++)
	{
		// The the value of another billiard in the same range
		int swap_index = r.getLong(6) + 1;

		int temp = rack_order[x];
		rack_order[x] = rack_order[swap_index];
		rack_order[swap_index] = temp;
	}

	// These loops build a triangle out of the billiards
	for (int row = 0;row < 5;row++) {
		// Number of billiards on this row
		int row_count = 3 - abs(row - 2);
		double row_start = mid_line - ((row_count / 2.0) * initial_spacing);

		for (int pos = 0;pos <  row_count;pos++)
		{
			// Calculate its position
			double x = rack_line + (row * initial_spacing);
			double y = row_start + (pos * initial_spacing);
		
			// Create the billiard
			KueBilliard billiard(x, y, KueUtility::defaultBilliardRadius(), TQString::number(rack_order[next_ball]));

			// Actually place it
			KueGlobal::physics()->insertBilliard(rack_order[next_ball], billiard);
			next_ball++;
		}
	}
}

void KueUtility::layoutTable() {
	KueGlobal::physics()->setFieldWidth(PLAY_AREA_WIDTH);
	KueGlobal::physics()->setFieldHeight(PLAY_AREA_HEIGHT);
}

void KueUtility::layoutPockets()
{
	double field_width = KueGlobal::physics()->fieldWidth();
	double field_height = KueGlobal::physics()->fieldHeight();
	double radius = KueUtility::defaultPocketRadius();

	// Place the pockets in the four corners
	KueGlobal::physics()->insertPocket(0, KuePocket(0.0, 0.0, radius));
	KueGlobal::physics()->insertPocket(1, KuePocket(field_width / 2.0, 0.0, radius));
	KueGlobal::physics()->insertPocket(2, KuePocket(field_width, 0.0, radius));

	KueGlobal::physics()->insertPocket(3, KuePocket(0.0, field_height, radius));
	KueGlobal::physics()->insertPocket(4, KuePocket(field_width / 2.0, field_height, radius));
	KueGlobal::physics()->insertPocket(5, KuePocket(field_width, field_height, radius));
}

void KueUtility::layoutBilliards(rackType rack_type)
{
	if (rack_type == Triangle)
	{
		rackTriangle();
	}
	else if (rack_type == Diamond)
	{
		rackDiamond();
	}
	else if (rack_type == None)
	{
		// Do nothing
	}
	else
	{
		kdWarning() << "Unknown rack type, no racking done" << endl;
	}

	// Place the cue ball
	KueBilliard cue(KueGlobal::physics()->fieldWidth() / 4.0, KueGlobal::physics()->fieldHeight() / 2.0, KueUtility::defaultBilliardRadius());
	KueGlobal::physics()->insertBilliard(0, cue);
}

KueTexture KueUtility::textureForBilliard(unsigned int index)
{
	if (index)
	{
		return KueTexture(TQString::number(index));
	}
	else
	{
		return KueTexture::null();
	}
}

// Regulation radius of billiards, in meters
double KueUtility::defaultBilliardRadius()
{
	return 0.00286;
}

// Regulation radius of pockets, in meters
double KueUtility::defaultPocketRadius()
{
	return 0.006;
}