summaryrefslogtreecommitdiffstats
path: root/kue/modules/8ball
diff options
context:
space:
mode:
authorMavridis Philippe <mavridisf@gmail.com>2024-08-07 19:13:02 +0300
committerMavridis Philippe <mavridisf@gmail.com>2024-08-07 19:23:24 +0300
commit04b5a62b8d9f5ff8240f25361046f2a5d58e8262 (patch)
tree98b126454cdf68d544e138d7e8b31d5fd45b72c2 /kue/modules/8ball
parent83ba00b7e569587d50383ff06a70148042ca780e (diff)
downloadtdegames-04b5a62b8d9f5ff8240f25361046f2a5d58e8262.tar.gz
tdegames-04b5a62b8d9f5ff8240f25361046f2a5d58e8262.zip
Add Kue billiards gamefeat/kue
Signed-off-by: Mavridis Philippe <mavridisf@gmail.com>
Diffstat (limited to 'kue/modules/8ball')
-rw-r--r--kue/modules/8ball/8ball.cpp337
-rw-r--r--kue/modules/8ball/8ball.h88
-rw-r--r--kue/modules/8ball/8ball.plugin6
-rw-r--r--kue/modules/8ball/CMakeLists.txt42
4 files changed, 473 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