diff options
Diffstat (limited to 'kue/modules')
-rw-r--r-- | kue/modules/8ball/8ball.cpp | 337 | ||||
-rw-r--r-- | kue/modules/8ball/8ball.h | 88 | ||||
-rw-r--r-- | kue/modules/8ball/8ball.plugin | 6 | ||||
-rw-r--r-- | kue/modules/8ball/CMakeLists.txt | 42 | ||||
-rw-r--r-- | kue/modules/9ball/9ball.cpp | 217 | ||||
-rw-r--r-- | kue/modules/9ball/9ball.h | 71 | ||||
-rw-r--r-- | kue/modules/9ball/9ball.plugin | 6 | ||||
-rw-r--r-- | kue/modules/9ball/CMakeLists.txt | 42 | ||||
-rw-r--r-- | kue/modules/CMakeLists.txt | 8 | ||||
-rw-r--r-- | kue/modules/freeplay/CMakeLists.txt | 42 | ||||
-rw-r--r-- | kue/modules/freeplay/freeplay.cpp | 93 | ||||
-rw-r--r-- | kue/modules/freeplay/freeplay.h | 46 | ||||
-rw-r--r-- | kue/modules/freeplay/freeplay.h.save | 85 | ||||
-rw-r--r-- | kue/modules/freeplay/freeplay.plugin | 5 |
14 files changed, 1088 insertions, 0 deletions
diff --git a/kue/modules/8ball/8ball.cpp b/kue/modules/8ball/8ball.cpp new file mode 100644 index 00000000..2ac2cff8 --- /dev/null +++ b/kue/modules/8ball/8ball.cpp @@ -0,0 +1,337 @@ +#include <kdebug.h> +#include <tdelocale.h> +#include <stdlib.h> +#include <stdio.h> + +#include "8ball.h" +#include "interface.h" +#include "physics.h" +#include "utility.h" +#include "team.h" +#include "player.h" +#include "global.h" + +const unsigned int BILLIARDS_COUNT = 15; + +K_EXPORT_COMPONENT_FACTORY( libkue8ball, EightBallFactory ) + +TQObject *EightBallFactory::createObject (TQObject *parent, const char* name, const char* classname, const TQStringList &args = TQStringList() ) +{ + Q_UNUSED(args); + + if (classname != TQString("KueRulesEngine")) + return 0; + + return new EightBall(parent, name); +} + +EightBall::EightBall(TQObject *parent, const char *name) : KueRulesEngine(parent, name) +{ + KueUtility::layoutTable(); + KueUtility::layoutPockets(); + KueUtility::layoutBilliards(KueUtility::Triangle); + + // Reset our state (NOTE: anyone see anything missing?) + _game_called = GAME_UNCALLED; + _current_team = 0; + _first_hit = -1; + _first_sunk = -1; + _scratch = false; + _broke = false; + + _current_player = KueGlobal::teams()->at(_current_team)->nextPlayer(); +} + +EightBall::~EightBall() +{ +} + +void EightBall::start() +{ + cuePlaced(); +} + +void EightBall::billiardSunk(unsigned int ball, unsigned int /* pocket */) +{ + // Called when the physics engine sinks a billiard + + // Somebody must win the game if the 8ball is sunk, the question is who + if (ballIsMagic(ball)) + { + if (onlyMagicLeft(_current_team)) + { + // It was our only ball left, we win + playerWins(_current_team); + } + else + { + // We messed up real bad + playerWins(!_current_team); + } + } + + // Have we sunk nothing yet? Or ist the cue ball? + if ((ownsBall(!_current_team, ball)) || ballIsCue(ball)) + { + // Oops, we shouldn't have sunk that... scratch! + _scratch = true; + } + else if (_first_sunk == -1) + { + // Ah, it's all good... + _first_sunk = ball; + } +} + +void EightBall::billiardHit(unsigned int ball1, unsigned int ball2) { + // Is this our first hit? + if (_first_hit == -1) + { + // Count the one that isn't the cue ball ;) + if (ballIsCue(ball1)) + { + _first_hit = ball2; + _broke = true; + } + else if (ballIsCue(ball2)) + { + _first_hit = ball1; + _broke = true; + } + } +} + +void EightBall::motionStopped() +{ + // The physics engine has finished its job, turn it off to save CPU time + KueGlobal::physics()->stop(); + + // Did we hit a ball? And did we own that ball? + if ((_first_hit == -1) || ownsBall(!_current_team, _first_hit)) + { + // Nope, scratch + _scratch = true; + } + + // Did we hit a magic ball first when there are other balls left? + if ((!onlyMagicLeft(_current_team)) && ballIsMagic(_first_hit)) + { + // Scratch! + _scratch = true; + } + + // We downright lose if we scratch on the 8-ball (HA!) + if (onlyMagicLeft(_current_team) && _scratch) + { + playerWins(!_current_team); + return; + } + + // We lose our turn if the shot was a scratch, or we sunk nothing + if ((_scratch) || (_first_sunk == -1)) + { + if (_current_team == 0) + _current_team = 1; + else + _current_team = 0; + + _current_player = KueGlobal::teams()->at(_current_team)->nextPlayer(); + } + + if (_first_sunk != -1) + { + if (_game_called == GAME_UNCALLED) + { + if (_current_team) + { + _game_called = (ballIsSolid(_first_sunk) ? GAME_PLAYER1_STRIPES : GAME_PLAYER1_SOLIDS); + } + else + { + _game_called = (ballIsSolid(_first_sunk) ? GAME_PLAYER1_SOLIDS : GAME_PLAYER1_STRIPES); + } + } + } + + // Reset our shot state + _first_hit = -1; + _first_sunk = -1; + + // Did we scratch? + if (_scratch) + { + // Create the cue ball again + KueBilliard cue( + KueGlobal::physics()->fieldWidth() / 4.0, + KueGlobal::physics()->fieldHeight() / 2.0, + KueUtility::defaultBilliardRadius() + ); + + + if (_broke) + { + // We scratched, the cue ball goes back home + emit(showMessage(placeCueBallMessage())); + _current_player->placeBilliard( + 0, + cue, + KueGlobal::physics()->fieldWidth() / 4.0, + this, + TQ_SLOT(cuePlaced()) + ); + } + else + { + KueGlobal::physics()->insertBilliard(0, cue); + cuePlaced(); + } + } + else + { + emit(showMessage(startShotMessage())); + // The cue ball stays where it is, go right to the shot + _current_player->takeShot(0, false, this, TQ_SLOT(shotTaken())); + } +} + +// Is a ball solid? +bool EightBall::ballIsSolid(unsigned int number) +{ + return ((number > 0) && (number < 8)); +} + +// Is a ball 'magic' (8 ball)? +bool EightBall::ballIsMagic(unsigned int number) +{ + return (number == 8); +} + +// Is a ball striped? +bool EightBall::ballIsStripe(unsigned int number) +{ + return (number > 8); +} + +// Is a ball the cue ball (ball 0) +bool EightBall::ballIsCue(unsigned int number) +{ + return (number == 0); +} + +// Does the given player only have the 8 ball left to sink? +bool EightBall::onlyMagicLeft(int player) +{ + // It's impossible if the game is uncalled (any game with a sunk ball is called) + if (_game_called == GAME_UNCALLED) + return false; + + // Check all the billiards belonging to the player + for (unsigned int x = 1;x < BILLIARDS_COUNT;x++) + { + // Does the player own it, and does it still exist + if (ownsBall(player, x) && KueGlobal::physics()->billiards()[x]) + { + // So apparently there is more than magic left + return false; + } + } + + // Nope, only magic + return true; +} + +void EightBall::playerWins(int player) +{ + Q_UNUSED(player); + TQString message; + + // Announce the winner + message = i18n("%1 wins!").arg(_current_player->name()); + + // Show the message + emit(showMessage(message)); + + // Tell the rest of the game about the stunning victory + emit(gameOver(message)); +} + +// Does the player own the given ball +bool EightBall::ownsBall(int player, unsigned int ball) +{ + // Until the game is called, nobody owns anything + if (_game_called == GAME_UNCALLED) + { + return false; + } + + // And nobody ever owns the 8 ball + if (ballIsMagic(ball)) + return false; + + if (player) + { + return (_game_called == GAME_PLAYER1_STRIPES) ? ballIsSolid(ball) : ballIsStripe(ball); + } + else + { + return (_game_called == GAME_PLAYER1_STRIPES) ? ballIsStripe(ball) : ballIsSolid(ball); + } +} + +TQString EightBall::startShotMessage() +{ + TQString message; + // What type of shot is this? + if (_broke) + message = i18n("%1's shot").arg(_current_player->name()); + else + message = i18n("%1's break shot").arg(_current_player->name()); + + message = message + " " + sideString(); + + return message; +} + +TQString EightBall::placeCueBallMessage() +{ + TQString message; + + // Tell the user what is going on + message = i18n("%1 placing cue ball").arg(_current_player->name()); + message = message + " " + sideString(); + + return message; +} + + +TQString EightBall::sideString() +{ + if (_game_called == GAME_UNCALLED) + return ""; + + if (_current_team) + return (_game_called == GAME_PLAYER1_STRIPES) ? i18n("(solids)") : i18n("(stripes)"); + else + return (_game_called == GAME_PLAYER1_STRIPES) ? i18n("(stripes)") : i18n("(solids)"); +} + +void EightBall::cuePlaced() +{ + // Tell the interface code to start the shot + emit(showMessage(startShotMessage())); + _current_player->takeShot(0, true, this, TQ_SLOT(shotTaken())); +} + +void EightBall::shotTaken() +{ + // Start the physics engine + KueGlobal::physics()->start(); + + // Reset the shot-related variables + _scratch = false; + _first_hit = -1; + _first_sunk = -1; +} + + +#include "8ball.moc" diff --git a/kue/modules/8ball/8ball.h b/kue/modules/8ball/8ball.h new file mode 100644 index 00000000..73ee3864 --- /dev/null +++ b/kue/modules/8ball/8ball.h @@ -0,0 +1,88 @@ +#ifndef _EIGHTBALL_H +#define _EIGHTBALL_H + +#include <tqobject.h> +#include <klibloader.h> + +#include "vector.h" +#include "point.h" +#include "rules.h" + + +// Forward declarations of our helper classes +class TQString; +class KuePlayer; + +// Possible values of _game_called +enum gameCallType {GAME_UNCALLED, GAME_PLAYER1_STRIPES, GAME_PLAYER1_SOLIDS}; + + +class EightBallFactory : KLibFactory { + public: + TQObject* createObject(TQObject*, const char*, const char*, const TQStringList &); +}; + +class EightBall : public KueRulesEngine { + TQ_OBJECT + public: + EightBall(TQObject *parent, const char *name); + ~EightBall(); + + void start(); + + protected slots: + // Called by physics engine when a billiard is sunk + void billiardSunk(unsigned int ball, unsigned int pocket); + // Called by physics engine when a billiard is struck (by the cue ball or another billiard) + void billiardHit(unsigned int ball1, unsigned int ball2); + // Called by the physics engine when all billiards have stopped moving + void motionStopped(); + + void cuePlaced(); + void shotTaken(); + + private: + // Ask the interface to start the shot + TQString startShotMessage(); + // Ask the interface to place the cue ball + TQString placeCueBallMessage(); + + // Does a player only have an 8 ball left to shoot at? + bool onlyMagicLeft(int player); + // Does a player own a given ball? + bool ownsBall(int player, unsigned int ball); + + // Is a ball solid? + bool ballIsSolid(unsigned int number); + // Is a ball stripped? + bool ballIsStripe(unsigned int number); + // Is a ball the cue ball? + bool ballIsCue(unsigned int number); + // Is a ball the magic ball (8)? + bool ballIsMagic(unsigned int number); + + // Handle a player's victory + void playerWins(int player); + + // Is this shot a scratch? + bool _scratch; + // First ball sunk + int _first_sunk; + // First ball hit + int _first_hit; + + // The current player + KuePlayer *_current_player; + // The current team + int _current_team; + // Who's stripes? And who's solids? + gameCallType _game_called; + + // Have we had a successful break? + bool _broke; + + TQString sideString(); +}; + + +#endif diff --git a/kue/modules/8ball/8ball.plugin b/kue/modules/8ball/8ball.plugin new file mode 100644 index 00000000..eb2e0669 --- /dev/null +++ b/kue/modules/8ball/8ball.plugin @@ -0,0 +1,6 @@ +Filename=libkue8ball +Type=rulesengine +Name=8-Ball +Description=A very simple version of 8-ball pool +MinTeams=2 +MaxTeams=2 diff --git a/kue/modules/8ball/CMakeLists.txt b/kue/modules/8ball/CMakeLists.txt new file mode 100644 index 00000000..5009d05c --- /dev/null +++ b/kue/modules/8ball/CMakeLists.txt @@ -0,0 +1,42 @@ +################################################################################ +# Improvements and feedback are welcome! # +# This software is licensed under the terms of the GNU GPL v3 license. # +################################################################################ + +include_directories( + ${CMAKE_BINARY_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_SOURCE_DIR} + ${TDE_INCLUDE_DIR} + ${TQT_INCLUDE_DIRS} +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + +### kue8ball (library) ######################################################### +tde_add_library( + kue8ball SHARED AUTOMOC + + SOURCES + 8ball.cpp + + LINK + tdeio-shared + kue-shared + + DESTINATION + ${LIB_INSTALL_DIR} +) + +### data ####################################################################### +install( + FILES + 8ball.plugin + + DESTINATION + ${DATA_INSTALL_DIR}/kue +) + +# kate: replace-tabs true; tab-width 2;
\ No newline at end of file diff --git a/kue/modules/9ball/9ball.cpp b/kue/modules/9ball/9ball.cpp new file mode 100644 index 00000000..eafaa2a8 --- /dev/null +++ b/kue/modules/9ball/9ball.cpp @@ -0,0 +1,217 @@ +#include <kdebug.h> +#include <tdelocale.h> +#include <stdlib.h> +#include <stdio.h> + +#include "9ball.h" +#include "interface.h" +#include "physics.h" +#include "utility.h" +#include "team.h" +#include "player.h" +#include "global.h" + +const unsigned int BILLIARDS_COUNT = 15; + +K_EXPORT_COMPONENT_FACTORY( libkue9ball, NineBallFactory ) + +TQObject *NineBallFactory::createObject (TQObject *parent, const char* name, const char* classname, const TQStringList &args = TQStringList() ) +{ + Q_UNUSED(args); + + if (classname != TQString("KueRulesEngine")) + return 0; + + return new NineBall(parent, name); +} + +NineBall::NineBall(TQObject *parent, const char *name) : KueRulesEngine(parent, name) +{ + KueUtility::layoutTable(); + KueUtility::layoutPockets(); + KueUtility::layoutBilliards(KueUtility::Diamond); + + // Reset our state (NOTE: anyone see anything missing?) + _current_team = 0; + _first_hit = -1; + _first_sunk = -1; + _foul = false; + _broke = false; + + _current_player = KueGlobal::teams()->at(_current_team)->nextPlayer(); +} + +NineBall::~NineBall() +{ +} + +void NineBall::start() +{ + cuePlaced(); +} + +void NineBall::billiardSunk(unsigned int ball, unsigned int pocket) +{ + Q_UNUSED(pocket); + + // Ah, it's all good... + if (_first_sunk == -1) + _first_sunk = ball; + + if (ball == 0) + _foul = true; +} + +void NineBall::billiardHit(unsigned int ball1, unsigned int ball2) +{ + // Is this our first hit? + if (_first_hit == -1) + { + // Select the ball involved which isn't the cue ball + _first_hit = ball1 ? ball1 : ball2; + + if (!ballIsLowest(_first_hit)) + _foul = true; + + _broke = true; + } +} + +void NineBall::motionStopped() +{ + // The physics engine has finished its job, turn it off to save CPU time + KueGlobal::physics()->stop(); + + // Are all the balls 1-9 gone? + if (ballIsLowest(10)) + { + playerWins(); + return; + } + + // We lose our turn if the shot was a scratch, or we sunk nothing + if ((_foul) || (_first_sunk == -1)) + { + if (_current_team == 0) + _current_team = 1; + else + _current_team = 0; + + _current_player = KueGlobal::teams()->at(_current_team)->nextPlayer(); + } + + // Reset our shot state + _first_hit = -1; + _first_sunk = -1; + + // Did we scratch? + if (_foul) + { + // Recreate the cue call + KueBilliard cue( + KueGlobal::physics()->fieldWidth() / 4.0, + KueGlobal::physics()->fieldHeight() / 2.0, + KueUtility::defaultBilliardRadius() + ); + + if (_broke) + { + // Ask the user where to place the billiard + emit(showMessage(placeCueBallMessage())); + _current_player->placeBilliard( + 0, + cue, + KueGlobal::physics()->fieldWidth() / 4.0, + this, + TQ_SLOT(cuePlaced()) + ); + } + else + { + // We scratched, the cue ball goes back home + KueGlobal::physics()->insertBilliard(0, cue); + cuePlaced(); + } + } + else + { + emit(showMessage(startShotMessage())); + // The cue ball stays where it is, go right to the shot + _current_player->takeShot(0, false, this, TQ_SLOT(shotTaken())); + } +} + +// Is a ball 'magic' (8 ball)? +bool NineBall::ballIsLowest(unsigned int number) +{ + for (unsigned int x = 1;x < number;x++) + if (KueGlobal::physics()->billiards()[x]) + return false; + + return true; +} + +// Is a ball the cue ball (ball 0) +bool NineBall::ballIsCue(unsigned int number) +{ + return (number == 0); +} + +void NineBall::playerWins() +{ + TQString message; + + // Announce the winner + message = i18n("%1 wins!").arg(_current_player->name()); + + // Show the message + emit(showMessage(message)); + + // Tell the rest of the game about the stunning victory + emit(gameOver(message)); +} + +TQString NineBall::startShotMessage() +{ + TQString message; + // What type of shot is this? + if (_broke) + message = i18n("%1's shot").arg(_current_player->name()); + else + message = i18n("%1's break shot").arg(_current_player->name()); + + return message; +} + +TQString NineBall::placeCueBallMessage() +{ + TQString message; + + // Tell the user what is going on + message = i18n("%1 placing cue ball").arg(_current_player->name()); + + return message; +} + + + +void NineBall::cuePlaced() +{ + // Tell the interface code to start the shot + emit(showMessage(startShotMessage())); + _current_player->takeShot(0, true, this, TQ_SLOT(shotTaken())); +} + +void NineBall::shotTaken() +{ + // Start the physics engine + KueGlobal::physics()->start(); + + // Reset the shot-related variables + _foul = false; + _first_hit = -1; + _first_sunk = -1; +} + + +#include "9ball.moc" diff --git a/kue/modules/9ball/9ball.h b/kue/modules/9ball/9ball.h new file mode 100644 index 00000000..25a1774e --- /dev/null +++ b/kue/modules/9ball/9ball.h @@ -0,0 +1,71 @@ +#ifndef _EIGHTBALL_H +#define _EIGHTBALL_H + +#include <tqobject.h> +#include <klibloader.h> + +#include "vector.h" +#include "point.h" +#include "rules.h" + + +// Forward declarations of our helper classes +class TQString; +class KuePlayer; + +class NineBallFactory : KLibFactory { + public: + TQObject* createObject(TQObject*, const char*, const char*, const TQStringList &); +}; + +class NineBall : public KueRulesEngine { + TQ_OBJECT + public: + NineBall(TQObject *parent, const char *name); + ~NineBall(); + + void start(); + + protected slots: + // Called by physics engine when a billiard is sunk + void billiardSunk(unsigned int ball, unsigned int pocket); + // Called by physics engine when a billiard is struck (by the cue ball or another billiard) + void billiardHit(unsigned int ball1, unsigned int ball2); + // Called by the physics engine when all billiards have stopped moving + void motionStopped(); + + void cuePlaced(); + void shotTaken(); + + private: + // Ask the interface to start the shot + TQString startShotMessage(); + // Ask the interface to place the cue ball + TQString placeCueBallMessage(); + + // Is a ball the cue ball? + bool ballIsCue(unsigned int number); + // Is a ball the magic ball (8)? + bool ballIsLowest(unsigned int number); + + // Handle a player's victory + void playerWins(); + + // Is this shot a scratch? + bool _foul; + // First ball sunk + int _first_sunk; + // First ball hit + int _first_hit; + + // The current player + KuePlayer *_current_player; + // The current team + int _current_team; + + // Have we had a successful break? + bool _broke; +}; + + +#endif diff --git a/kue/modules/9ball/9ball.plugin b/kue/modules/9ball/9ball.plugin new file mode 100644 index 00000000..8c6e0292 --- /dev/null +++ b/kue/modules/9ball/9ball.plugin @@ -0,0 +1,6 @@ +Filename=libkue9ball +Type=rulesengine +Name=9-Ball +Description=A very simple version of 9-ball pool +MinTeams=2 +MaxTeams=2 diff --git a/kue/modules/9ball/CMakeLists.txt b/kue/modules/9ball/CMakeLists.txt new file mode 100644 index 00000000..8e046cdc --- /dev/null +++ b/kue/modules/9ball/CMakeLists.txt @@ -0,0 +1,42 @@ +################################################################################ +# Improvements and feedback are welcome! # +# This software is licensed under the terms of the GNU GPL v3 license. # +################################################################################ + +include_directories( + ${CMAKE_BINARY_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_SOURCE_DIR} + ${TDE_INCLUDE_DIR} + ${TQT_INCLUDE_DIRS} +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + +### kue9ball (library) ######################################################### +tde_add_library( + kue9ball SHARED AUTOMOC + + SOURCES + 9ball.cpp + + LINK + tdeio-shared + kue-shared + + DESTINATION + ${LIB_INSTALL_DIR} +) + +### data ####################################################################### +install( + FILES + 9ball.plugin + + DESTINATION + ${DATA_INSTALL_DIR}/kue +) + +# kate: replace-tabs true; tab-width 2;
\ No newline at end of file diff --git a/kue/modules/CMakeLists.txt b/kue/modules/CMakeLists.txt new file mode 100644 index 00000000..bf9d6f43 --- /dev/null +++ b/kue/modules/CMakeLists.txt @@ -0,0 +1,8 @@ +################################################################################ +# Improvements and feedback are welcome! # +# This software is licensed under the terms of the GNU GPL v3 license. # +################################################################################ + +add_subdirectory(8ball) +add_subdirectory(9ball) +add_subdirectory(freeplay)
\ No newline at end of file diff --git a/kue/modules/freeplay/CMakeLists.txt b/kue/modules/freeplay/CMakeLists.txt new file mode 100644 index 00000000..0f935a3c --- /dev/null +++ b/kue/modules/freeplay/CMakeLists.txt @@ -0,0 +1,42 @@ +################################################################################ +# Improvements and feedback are welcome! # +# This software is licensed under the terms of the GNU GPL v3 license. # +################################################################################ + +include_directories( + ${CMAKE_BINARY_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_SOURCE_DIR} + ${TDE_INCLUDE_DIR} + ${TQT_INCLUDE_DIRS} +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + +### kuefreeplay (library) ####################################################### +tde_add_library( + kuefreeplay SHARED AUTOMOC + + SOURCES + freeplay.cpp + + LINK + tdeio-shared + kue-shared + + DESTINATION + ${LIB_INSTALL_DIR} +) + +### data ####################################################################### +install( + FILES + freeplay.plugin + + DESTINATION + ${DATA_INSTALL_DIR}/kue +) + +# kate: replace-tabs true; tab-width 2;
\ No newline at end of file diff --git a/kue/modules/freeplay/freeplay.cpp b/kue/modules/freeplay/freeplay.cpp new file mode 100644 index 00000000..022c0ec9 --- /dev/null +++ b/kue/modules/freeplay/freeplay.cpp @@ -0,0 +1,93 @@ +#include "freeplay.h" +#include "interface.h" +#include "physics.h" +#include "utility.h" +#include "player.h" +#include "global.h" +#include "team.h" + +#include <tdelocale.h> +#include <tdemessagebox.h> +#include <stdlib.h> +#include <stdio.h> +#include <krandomsequence.h> + +const int BILLIARDS_COUNT = 15; + +K_EXPORT_COMPONENT_FACTORY( libkuefreeplay, FreePlayFactory ) + +TQObject *FreePlayFactory::createObject (TQObject *parent, const char* name, const char* classname, const TQStringList &args = TQStringList() ) +{ + Q_UNUSED(args); + + if (classname != TQString("KueRulesEngine")) + return 0; + + return new FreePlay(parent, name); +} + +FreePlay::FreePlay(TQObject *parent, const char *name) : KueRulesEngine(parent, name) +{ + KueUtility::layoutTable(); + KueUtility::layoutPockets(); + KueUtility::layoutBilliards(KueUtility::Triangle); + + _current_team = 0; +} + +FreePlay::~FreePlay() +{ +} + +void FreePlay::start() +{ + startShot(); +} + +void FreePlay::motionStopped() +{ + // The physics engine has finished its job, turn it off to save CPU time + KueGlobal::physics()->stop(); + + _current_team++; + + if (_current_team == KueGlobal::teams()->count()) + _current_team = 0; + + startShot(); +} + +void FreePlay::shotTaken() +{ + // Start the physics engine + KueGlobal::physics()->start(); +} + +void FreePlay::startShot() +{ + TQString message; + KuePlayer *current_player = KueGlobal::teams()->at(_current_team)->nextPlayer(); + + // Did the cue ball get sunk? Replace it. + if (!KueGlobal::physics()->billiards()[0]) + { + KueBilliard cue( + KueGlobal::physics()->fieldWidth() / 4.0, + KueGlobal::physics()->fieldHeight() / 2.0, + KueUtility::defaultBilliardRadius() + ); + + KueGlobal::physics()->insertBilliard(0, cue); + } + + // What type of shot is this? + message = i18n("%1's shot").arg(current_player->name()); + + // Show the generated message + emit(showMessage(message)); + + // Tell the interface to start the shot UI. + current_player->takeShot(0, false, this, TQ_SLOT(shotTaken())); +} + +#include "freeplay.moc" diff --git a/kue/modules/freeplay/freeplay.h b/kue/modules/freeplay/freeplay.h new file mode 100644 index 00000000..f51ba950 --- /dev/null +++ b/kue/modules/freeplay/freeplay.h @@ -0,0 +1,46 @@ +#ifndef _FREEPLAY_H +#define _FREEPLAY_H + +#include <tqobject.h> +#include <klibloader.h> + +#include "vector.h" +#include "point.h" +#include "rules.h" + + +// Forward declarations of our helper classes +class TQString; + +// Possible values of _game_called +enum gameCallType {GAME_UNCALLED, GAME_PLAYER1_STRIPES, GAME_PLAYER1_SOLIDS}; + + +class FreePlayFactory : KLibFactory { + public: + TQObject* createObject(TQObject*, const char*, const char*, const TQStringList &); +}; + +class FreePlay : public KueRulesEngine { + TQ_OBJECT + public: + FreePlay(TQObject *parent, const char *name); + ~FreePlay(); + + void start(); + + protected slots: + // Called by the physics engine when all billiards have stopped moving + void motionStopped(); + // Called by the interface after the user has decided on a shot + void shotTaken(); + + private: + // Ask the interface to start the shot + void startShot(); + + unsigned int _current_team; +}; + + +#endif diff --git a/kue/modules/freeplay/freeplay.h.save b/kue/modules/freeplay/freeplay.h.save new file mode 100644 index 00000000..36b9404e --- /dev/null +++ b/kue/modules/freeplay/freeplay.h.save @@ -0,0 +1,85 @@ +#ifndef _EIGHTBALL_H +#define _EIGHTBALL_H + +#include <ntqobject.h> +#include <klibloader.h> + +#include "vector.h" +#include "point.h" +#include "rules.h" + + +// Forward declarations of our helper classes +class TQString; + +// Possible values of _game_called +enum gameCallType {GAME_UNCALLED, GAME_PLAYER1_STRIPES, GAME_PLAYER1_SOLIDS}; + + +class FreePlayFactory : KLibFactory { + public: + TQObject* createObject(TQObject*, const char*, const char*, const TQStringList &); +}; + +class FreePlay : public TQObject, public KueRulesEngine { + Q_OBJECT + public: + FreePlay(); + ~FreePlay(); + + // Is the game over? + bool gameIsOver() { return false; } + + protected slots: + // Called by physics engine when a billiard is sunk + void billiardSunk(unsigned int ball, unsigned int pocket); + // Called by physics engine when a billiard is struck (by the cue ball or another billiard) + void billiardHit(unsigned int ball1, unsigned int ball2); + // Called by the physics engine when all billiards have stopped moving + void motionStopped(); + + // Called by the interface when the user has decided on a cue location + void cuePlaced(point &location); + // Called by the interface when the user is deciding on a cue location + void previewCuePlace(point &location); + // Called by the interface after the user has decided on a hot + void shotTaken(vector &velocity); + + private: + // Ask the interface to start the shot + void startShot(bool forward_only); + // Ask the interface to place the cue ball + void placeCueBall(); + + // Does a player only have an 8 ball left to shoot at? + bool onlyMagicLeft(int player); + // Does a player own a given ball? + bool ownsBall(int player, unsigned int ball); + + // Is a ball solid? + bool ballIsSolid(unsigned int number); + // Is a ball stripped? + bool ballIsStripe(unsigned int number); + // Is a ball the cue ball? + bool ballIsCue(unsigned int number); + // Is a ball the magic ball (8)? + bool ballIsMagic(unsigned int number); + + // Handle a player's victory + void playerWins(int player); + + // Is this shot a scratch? + bool _scratch; + // First ball sunk + int _first_sunk; + // First ball hit + int _first_hit; + + // The current player + int _current_player; + // Who's stripes? And who's solids? + gameCallType _game_called; +}; + + +#endif diff --git a/kue/modules/freeplay/freeplay.plugin b/kue/modules/freeplay/freeplay.plugin new file mode 100644 index 00000000..9e559e15 --- /dev/null +++ b/kue/modules/freeplay/freeplay.plugin @@ -0,0 +1,5 @@ +Filename=libkuefreeplay +Type=rulesengine +Name=Free Play +Description=A very simple version of 8-ball pool +# There are no minimum or maximum number of teams |