2019-08-25 13:02:55 +02:00
|
|
|
/* Copyright (C) 2019 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_GAME
|
|
|
|
#define INCLUDED_GAME
|
2004-07-27 23:00:53 +02:00
|
|
|
|
2004-08-05 15:07:51 +02:00
|
|
|
#include <vector>
|
2004-07-27 23:00:53 +02:00
|
|
|
|
2017-06-25 16:54:00 +02:00
|
|
|
#include "ps/Errors.h"
|
|
|
|
#include "ps/Filesystem.h"
|
2011-01-12 13:29:00 +01:00
|
|
|
#include "scriptinterface/ScriptVal.h"
|
2015-06-06 10:45:49 +02:00
|
|
|
#include "simulation2/helpers/Player.h"
|
2011-01-12 13:29:00 +01:00
|
|
|
|
2006-03-21 21:55:45 +01:00
|
|
|
class CWorld;
|
2010-01-09 20:20:14 +01:00
|
|
|
class CSimulation2;
|
2006-03-21 21:55:45 +01:00
|
|
|
class CGameView;
|
2017-01-24 03:04:50 +01:00
|
|
|
class CTurnManager;
|
2010-08-07 00:16:05 +02:00
|
|
|
class IReplayLogger;
|
2010-06-30 23:41:04 +02:00
|
|
|
struct CColor;
|
2005-01-23 05:14:49 +01:00
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
|
|
|
* The container that holds the rules, resources and attributes of the game.
|
|
|
|
* The CGame object is responsible for creating a game that is defined by
|
|
|
|
* a set of attributes provided. The CGame object is also responsible for
|
|
|
|
* maintaining the relations between CPlayer and CWorld, CSimulation and CWorld.
|
|
|
|
**/
|
2009-01-03 19:40:28 +01:00
|
|
|
class CGame
|
2004-07-27 23:00:53 +02:00
|
|
|
{
|
2009-01-03 19:40:28 +01:00
|
|
|
NONCOPYABLE(CGame);
|
2016-05-19 21:42:07 +02:00
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
|
|
|
* pointer to the CWorld object representing the game world.
|
|
|
|
**/
|
2006-03-21 21:55:45 +01:00
|
|
|
CWorld *m_World;
|
2016-05-19 21:42:07 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
/**
|
|
|
|
* pointer to the CSimulation2 object operating on the game world.
|
|
|
|
**/
|
|
|
|
CSimulation2 *m_Simulation2;
|
2016-05-19 21:42:07 +02:00
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
|
|
|
* pointer to the CGameView object representing the view into the game world.
|
|
|
|
**/
|
2006-03-21 21:55:45 +01:00
|
|
|
CGameView *m_GameView;
|
2016-05-19 21:42:07 +02:00
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
|
|
|
* the game has been initialized and ready for use if true.
|
|
|
|
**/
|
2004-09-21 16:40:43 +02:00
|
|
|
bool m_GameStarted;
|
2016-05-19 21:42:07 +02:00
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
2012-06-06 21:37:03 +02:00
|
|
|
* Timescale multiplier for simulation rate.
|
2007-02-01 02:34:17 +01:00
|
|
|
**/
|
2006-06-06 08:31:17 +02:00
|
|
|
float m_SimRate;
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2016-05-19 21:42:07 +02:00
|
|
|
/**
|
|
|
|
* Index assigned to the current player.
|
|
|
|
* 1-8 to control players, 0 for gaia, -1 for observer.
|
|
|
|
*/
|
2015-06-06 10:45:49 +02:00
|
|
|
player_id_t m_PlayerID;
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2016-05-19 21:42:07 +02:00
|
|
|
/**
|
|
|
|
* Differs from m_PlayerID if a defeated player or observer views another player.
|
|
|
|
*/
|
|
|
|
player_id_t m_ViewedPlayerID;
|
|
|
|
|
2017-01-24 03:04:50 +01:00
|
|
|
CTurnManager* m_TurnManager;
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2004-07-27 23:00:53 +02:00
|
|
|
public:
|
2019-08-25 13:02:55 +02:00
|
|
|
CGame(bool replayLog);
|
2004-08-05 15:07:51 +02:00
|
|
|
~CGame();
|
2004-07-27 23:00:53 +02:00
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
|
|
|
* the game is paused and no updates will be performed if true.
|
|
|
|
**/
|
2006-01-02 04:07:29 +01:00
|
|
|
bool m_Paused;
|
|
|
|
|
2015-01-24 15:46:52 +01:00
|
|
|
void StartGame(JS::MutableHandleValue attribs, const std::string& savedState);
|
2005-03-22 03:17:55 +01:00
|
|
|
PSRETURN ReallyStartGame();
|
|
|
|
|
2017-06-25 16:54:00 +02:00
|
|
|
bool StartVisualReplay(const OsPath& replayPath);
|
2015-06-06 10:45:49 +02:00
|
|
|
|
2012-06-06 21:37:03 +02:00
|
|
|
/**
|
|
|
|
* Periodic heartbeat that controls the process. performs all per-frame updates.
|
|
|
|
* Simulation update is called and game status update is called.
|
|
|
|
*
|
|
|
|
* @param deltaRealTime Elapsed real time since last beat/frame, in seconds.
|
|
|
|
* @param doInterpolate Perform graphics interpolation if true.
|
|
|
|
* @return bool false if it can't keep up with the desired simulation rate
|
|
|
|
* indicating that you might want to render less frequently.
|
|
|
|
*/
|
2016-01-25 10:57:33 +01:00
|
|
|
void Update(const double deltaRealTime, bool doInterpolate = true);
|
2007-01-24 21:17:28 +01:00
|
|
|
|
2012-06-06 21:37:03 +02:00
|
|
|
void Interpolate(float simFrameLength, float realFrameLength);
|
2010-05-20 02:59:01 +02:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
int GetPlayerID();
|
2015-06-06 10:45:49 +02:00
|
|
|
void SetPlayerID(player_id_t playerID);
|
2005-01-18 01:46:18 +01:00
|
|
|
|
2016-05-19 21:42:07 +02:00
|
|
|
int GetViewedPlayerID();
|
|
|
|
void SetViewedPlayerID(player_id_t playerID);
|
|
|
|
|
2017-05-23 21:26:33 +02:00
|
|
|
/**
|
|
|
|
* Check if the game is finished by testing if there's a winner.
|
|
|
|
* It is used to end a non visual autostarted game.
|
|
|
|
*
|
|
|
|
* @return true if there's a winner, false otherwise.
|
|
|
|
*/
|
|
|
|
bool IsGameFinished() const;
|
|
|
|
|
2011-03-26 21:17:21 +01:00
|
|
|
/**
|
2015-03-16 00:59:48 +01:00
|
|
|
* Retrieving player colors from scripts is slow, so this updates an
|
|
|
|
* internal cache of all players' colors.
|
2011-03-26 21:17:21 +01:00
|
|
|
* Call this just before rendering, so it will always have the latest
|
2015-03-16 00:59:48 +01:00
|
|
|
* colors.
|
2011-03-26 21:17:21 +01:00
|
|
|
*/
|
2015-03-16 00:59:48 +01:00
|
|
|
void CachePlayerColors();
|
2011-03-26 21:17:21 +01:00
|
|
|
|
2015-06-06 10:45:49 +02:00
|
|
|
CColor GetPlayerColor(player_id_t player) const;
|
2004-08-16 17:19:17 +02:00
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
|
|
|
* Get m_GameStarted.
|
|
|
|
*
|
|
|
|
* @return bool the value of m_GameStarted.
|
|
|
|
**/
|
|
|
|
inline bool IsGameStarted() const
|
2004-09-21 16:40:43 +02:00
|
|
|
{
|
|
|
|
return m_GameStarted;
|
|
|
|
}
|
|
|
|
|
2015-09-13 21:03:33 +02:00
|
|
|
/**
|
|
|
|
* Get m_IsVisualReplay.
|
|
|
|
*
|
|
|
|
* @return bool the value of m_IsVisualReplay.
|
|
|
|
**/
|
2015-09-13 22:59:10 +02:00
|
|
|
inline bool IsVisualReplay() const
|
2015-09-13 21:03:33 +02:00
|
|
|
{ return m_IsVisualReplay; }
|
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
|
|
|
* Get the pointer to the game world object.
|
|
|
|
*
|
|
|
|
* @return CWorld * the value of m_World.
|
|
|
|
**/
|
2004-07-27 23:00:53 +02:00
|
|
|
inline CWorld *GetWorld()
|
2006-03-21 21:55:45 +01:00
|
|
|
{ return m_World; }
|
2010-08-01 22:56:34 +02:00
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
|
|
|
* Get the pointer to the game view object.
|
|
|
|
*
|
|
|
|
* @return CGameView * the value of m_GameView.
|
|
|
|
**/
|
2004-07-27 23:00:53 +02:00
|
|
|
inline CGameView *GetView()
|
2006-03-21 21:55:45 +01:00
|
|
|
{ return m_GameView; }
|
2010-08-01 22:56:34 +02:00
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
/**
|
|
|
|
* Get the pointer to the simulation2 object.
|
|
|
|
*
|
|
|
|
* @return CSimulation2 * the value of m_Simulation2.
|
|
|
|
**/
|
|
|
|
inline CSimulation2 *GetSimulation2()
|
|
|
|
{ return m_Simulation2; }
|
2005-03-22 03:17:55 +01:00
|
|
|
|
2007-02-01 02:34:17 +01:00
|
|
|
/**
|
|
|
|
* Set the simulation scale multiplier.
|
|
|
|
*
|
2010-07-29 17:55:41 +02:00
|
|
|
* @param simRate Float value to set m_SimRate to.
|
2007-02-01 02:34:17 +01:00
|
|
|
* Because m_SimRate is also used to
|
|
|
|
* scale TimeSinceLastFrame it must be
|
|
|
|
* clamped to 0.0f.
|
|
|
|
**/
|
2006-06-06 08:31:17 +02:00
|
|
|
inline void SetSimRate(float simRate)
|
2013-04-04 01:24:22 +02:00
|
|
|
{ if (isfinite(simRate)) m_SimRate = std::max(simRate, 0.0f); }
|
|
|
|
|
2015-09-13 22:59:10 +02:00
|
|
|
inline float GetSimRate() const
|
2013-04-04 01:24:22 +02:00
|
|
|
{ return m_SimRate; }
|
2006-06-06 08:31:17 +02:00
|
|
|
|
2017-06-25 16:54:00 +02:00
|
|
|
inline OsPath GetReplayPath() const
|
2016-04-14 16:45:03 +02:00
|
|
|
{ return m_ReplayPath; }
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
/**
|
|
|
|
* Replace the current turn manager.
|
|
|
|
* This class will take ownership of the pointer.
|
|
|
|
*/
|
2017-01-24 03:04:50 +01:00
|
|
|
void SetTurnManager(CTurnManager* turnManager);
|
2010-05-20 02:59:01 +02:00
|
|
|
|
2017-01-24 03:04:50 +01:00
|
|
|
CTurnManager* GetTurnManager() const
|
2010-06-30 23:41:04 +02:00
|
|
|
{ return m_TurnManager; }
|
2010-05-20 02:59:01 +02:00
|
|
|
|
2010-08-07 00:16:05 +02:00
|
|
|
IReplayLogger& GetReplayLogger() const
|
|
|
|
{ return *m_ReplayLogger; }
|
|
|
|
|
2005-03-22 03:17:55 +01:00
|
|
|
private:
|
2014-07-27 00:33:16 +02:00
|
|
|
void RegisterInit(const JS::HandleValue attribs, const std::string& savedState);
|
2010-08-07 00:16:05 +02:00
|
|
|
IReplayLogger* m_ReplayLogger;
|
2011-03-26 21:17:21 +01:00
|
|
|
|
2015-03-16 00:59:48 +01:00
|
|
|
std::vector<CColor> m_PlayerColors;
|
2011-10-30 02:07:28 +02:00
|
|
|
|
|
|
|
int LoadInitialState();
|
|
|
|
std::string m_InitialSavedState; // valid between RegisterInit and LoadInitialState
|
2011-12-22 15:04:32 +01:00
|
|
|
bool m_IsSavedGame; // true if loading a saved game; false for a new game
|
2015-06-06 10:45:49 +02:00
|
|
|
|
2015-09-13 21:03:33 +02:00
|
|
|
int LoadVisualReplayData();
|
2017-06-25 16:54:00 +02:00
|
|
|
OsPath m_ReplayPath;
|
2015-09-13 21:03:33 +02:00
|
|
|
bool m_IsVisualReplay;
|
2015-06-06 10:45:49 +02:00
|
|
|
std::istream* m_ReplayStream;
|
|
|
|
u32 m_FinalReplayTurn;
|
2004-07-27 23:00:53 +02:00
|
|
|
};
|
|
|
|
|
2004-07-31 17:57:18 +02:00
|
|
|
extern CGame *g_Game;
|
|
|
|
|
2004-07-27 23:00:53 +02:00
|
|
|
#endif
|