diff options
Diffstat (limited to 'kue/utility.cpp')
-rw-r--r-- | kue/utility.cpp | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/kue/utility.cpp b/kue/utility.cpp new file mode 100644 index 00000000..7565228a --- /dev/null +++ b/kue/utility.cpp @@ -0,0 +1,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; +} + |