File indexing completed on 2024-04-14 04:00:52

0001 /*
0002     SPDX-FileCopyrightText: 2003 Russell Steffen <rsteffen@bayarea.net>
0003     SPDX-FileCopyrightText: 2003 Stephan Zehetner <s.zehetner@nevox.org>
0004     SPDX-FileCopyrightText: 2006 Dmitry Suzdalev <dimsuz@gmail.com>
0005     SPDX-FileCopyrightText: 2006 Inge Wallin <inge@lysator.liu.se>
0006     SPDX-FileCopyrightText: 2006 Pierre Ducroquet <pinaraf@gmail.com>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef GAME_H
0012 #define GAME_H
0013 
0014 #include <QObject>
0015 #include <QStateMachine>
0016 #include <QFinalState>
0017 #include <QState>
0018 #include <QRandomGenerator>
0019 
0020 #include "fleet.h"
0021 #include "players/player.h"
0022 #include "players/neutralplayer.h"
0023 #include "map/map.h"
0024 
0025 class KLocalizedString;
0026 
0027 struct GameOptions
0028 {
0029    bool BlindMap, CumulativeProduction, ProductionAfterConquere;
0030    bool NeutralsShowShips, NeutralsShowStats;
0031    int  NeutralsProduction;
0032 };
0033 
0034 /**
0035   * This is the main class of the game.
0036   * It contains most of the game logic, ie. the finite state machine.
0037   */
0038 class Game : public QObject
0039 {
0040     Q_OBJECT
0041 public:
0042     explicit Game(QObject *parent = nullptr);
0043     ~Game() override;
0044 
0045     bool isRunning();
0046 
0047     virtual void start() = 0;
0048     
0049     virtual void stop() = 0;
0050 
0051     bool attack( Planet *sourcePlanet, Planet *destPlanet, long long shipCount, bool standingOrder = false);
0052 
0053     static Coordinate generatePlanetCoordinates (int rows, int cols);
0054     static double generateKillPercentage();
0055     static int generatePlanetProduction();
0056     const QList<Planet *>  planets();
0057 
0058     Map *map() { return m_map; }
0059 
0060     Player *currentPlayer() { return m_currentPlayer; }
0061 
0062     const QList<Player *> players() { return m_players; }
0063 
0064     void findWinner();
0065 
0066     GameOptions &options() { return m_options; }
0067 
0068     NeutralPlayer *neutral() { return m_neutral; }
0069 
0070     void setPlayers (const QList<Player *> &players);
0071 
0072     int turnCounter() const { return m_turnCounter; }
0073 
0074     bool doFleetArrival (AttackFleet *fleet);
0075 Q_SIGNALS:
0076     void started();
0077     void finished();
0078     void gameMsg( const KLocalizedString &msg, Player *player = nullptr,
0079                   Planet *planet = nullptr, Player *planetPlayer = nullptr );
0080 
0081 private:
0082     static QRandomGenerator random;
0083 
0084     Player *m_currentPlayer;
0085 
0086     void makeKill(Fleet *fleet, Player *player);
0087 
0088 
0089 private:
0090     void newTurn() { m_turnCounter++; }
0091 
0092 protected:
0093     // Points to the Map we're playing on.
0094     Map     *m_map;
0095     GameOptions m_options;
0096 
0097     virtual void buildMachine() = 0;
0098     QFinalState *m_finalState;
0099     QStateMachine m_gameMachine;
0100     NeutralPlayer *m_neutral;
0101 
0102     int m_turnCounter;
0103 
0104     QList<Player *> m_players;
0105 
0106 
0107 
0108     void setCurrentPlayer(Player *player);
0109 
0110     friend class Player;
0111 };
0112 
0113 #endif // GAME_H