2008-12-27 03:01:59 +02:00
# ifndef __CGAMESTATE_H__
# define __CGAMESTATE_H__
2009-05-20 13:08:56 +03:00
# include "../global.h"
2009-12-01 23:05:57 +02:00
# include <cassert>
2010-05-02 21:20:26 +03:00
2008-12-27 03:01:59 +02:00
# ifndef _MSC_VER
2010-12-20 23:22:53 +02:00
# include "CCreatureHandler.h"
2009-05-20 13:08:56 +03:00
# include "VCMI_Lib.h"
2009-01-30 20:36:00 +02:00
# include "map.h"
2008-12-27 03:01:59 +02:00
# endif
2010-05-02 21:20:26 +03:00
2008-12-27 03:01:59 +02:00
# include <set>
# include <vector>
2010-02-01 19:51:33 +02:00
# include <list>
2010-02-10 04:56:00 +02:00
# include "HeroBonus.h"
2010-05-02 21:20:26 +03:00
# include "CCreatureSet.h"
2008-12-27 03:01:59 +02:00
# ifdef _WIN32
# include <tchar.h>
# else
2009-05-21 03:55:30 +03:00
# include "../tchar_amigaos4.h"
2008-12-27 03:01:59 +02:00
# endif
2010-12-20 15:04:24 +02:00
# include "ConstTransitivePtr.h"
2011-05-10 01:20:47 +03:00
# include "IGameCallback.h"
2011-07-05 09:14:07 +03:00
# include "ResourceSet.h"
2010-12-20 15:04:24 +02:00
2010-05-02 21:20:26 +03:00
2009-04-15 17:03:31 +03:00
/*
* CGameState . h , part of VCMI engine
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
*/
2008-12-27 03:01:59 +02:00
class CTown ;
class CScriptCallback ;
class CCallback ;
2010-06-07 08:28:12 +03:00
class IGameCallback ;
2008-12-27 03:01:59 +02:00
class CLuaCallback ;
class CCPPObjectScript ;
class CCreatureSet ;
class CStack ;
class CGHeroInstance ;
class CGTownInstance ;
class CArmedInstance ;
2009-10-24 22:21:32 +03:00
class CGDwelling ;
2008-12-27 03:01:59 +02:00
class CGDefInfo ;
class CObjectScript ;
class CGObjectInstance ;
class CCreature ;
struct Mapa ;
struct StartInfo ;
struct SDL_Surface ;
class CMapHandler ;
class CPathfinder ;
struct SetObjectProperty ;
struct MetaString ;
2009-03-07 00:25:19 +02:00
struct CPack ;
2009-05-07 18:19:52 +03:00
class CSpell ;
2009-08-30 15:47:40 +03:00
struct TerrainTile ;
2010-02-07 17:06:14 +02:00
class CHeroClass ;
2010-05-08 21:56:38 +03:00
class CCampaign ;
2010-08-04 14:18:13 +03:00
class CCampaignState ;
2010-11-10 02:06:25 +02:00
class IModableArt ;
2011-05-03 06:14:18 +03:00
class CGGarrison ;
2008-12-27 03:01:59 +02:00
namespace boost
{
class shared_mutex ;
}
2010-12-06 01:10:02 +02:00
//numbers of creatures are exact numbers if detailed else they are quantity ids (0 - a few, 1 - several and so on; additionaly -1 - unknown)
2011-05-26 04:03:35 +03:00
struct ArmyDescriptor : public std : : map < TSlot , CStackBasicDescriptor >
2010-12-06 01:10:02 +02:00
{
2011-08-25 18:24:37 +03:00
bool isDetailed ;
2011-05-26 04:03:35 +03:00
DLL_EXPORT ArmyDescriptor ( const CArmedInstance * army , bool detailed ) ; //not detailed -> quantity ids as count
DLL_EXPORT ArmyDescriptor ( ) ;
2011-08-25 18:24:37 +03:00
DLL_EXPORT int getStrength ( ) const ;
2010-12-06 01:10:02 +02:00
} ;
2010-02-07 17:06:14 +02:00
struct DLL_EXPORT InfoAboutHero
{
2010-02-07 19:56:06 +02:00
private :
void assign ( const InfoAboutHero & iah ) ;
public :
2010-02-07 17:06:14 +02:00
struct DLL_EXPORT Details
{
std : : vector < int > primskills ;
int mana , luck , morale ;
} * details ;
char owner ;
const CHeroClass * hclass ;
std : : string name ;
int portrait ;
2010-12-06 01:10:02 +02:00
ArmyDescriptor army ;
2010-02-07 17:06:14 +02:00
InfoAboutHero ( ) ;
2010-02-07 19:56:06 +02:00
InfoAboutHero ( const InfoAboutHero & iah ) ;
InfoAboutHero & operator = ( const InfoAboutHero & iah ) ;
2010-02-07 17:06:14 +02:00
~ InfoAboutHero ( ) ;
void initFromHero ( const CGHeroInstance * h , bool detailed ) ;
} ;
2011-05-03 06:14:18 +03:00
/// Struct which holds a short information about a town
struct DLL_EXPORT InfoAboutTown
{
struct Details
{
int hallLevel , goldIncome ;
bool customRes ;
bool garrisonedHero ;
} * details ;
const CArmedInstance * obj ;
char fortLevel ; //0 - none
char owner ;
std : : string name ;
CTown * tType ;
bool built ;
ArmyDescriptor army ; //numbers of creatures are valid only if details
InfoAboutTown ( ) ;
~ InfoAboutTown ( ) ;
void initFromTown ( const CGTownInstance * t , bool detailed ) ;
void initFromGarrison ( const CGGarrison * garr , bool detailed ) ;
} ;
2010-07-15 06:04:57 +03:00
// typedef si32 TResourceUnit;
// typedef std::vector<si32> TResourceVector;
// typedef std::set<si32> TResourceSet;
2010-02-07 17:06:14 +02:00
2010-02-01 19:51:33 +02:00
struct DLL_EXPORT SThievesGuildInfo
{
std : : vector < ui8 > playerColors ; //colors of players that are in-game
std : : vector < std : : list < ui8 > > numOfTowns , numOfHeroes , gold , woodOre , mercSulfCrystGems , obelisks , artifacts , army , income ; // [place] -> [colours of players]
2010-02-04 20:40:40 +02:00
std : : map < ui8 , InfoAboutHero > colorToBestHero ; //maps player's color to his best heros'
2010-02-01 19:51:33 +02:00
2010-02-06 15:49:14 +02:00
std : : map < ui8 , si8 > personality ; // color to personality // -1 - human, AI -> (00 - random, 01 - warrior, 02 - builder, 03 - explorer)
std : : map < ui8 , si32 > bestCreature ; // color to ID // id or -1 if not known
2010-02-07 17:06:14 +02:00
// template <typename Handler> void serialize(Handler &h, const int version)
// {
// h & playerColors & numOfTowns & numOfHeroes & gold & woodOre & mercSulfCrystGems & obelisks & artifacts & army & income;
// h & colorToBestHero & personality & bestCreature;
// }
2010-02-01 19:51:33 +02:00
} ;
2010-04-03 06:33:46 +03:00
struct DLL_EXPORT PlayerState : public CBonusSystemNode
2008-12-27 03:01:59 +02:00
{
public :
2010-01-29 22:52:45 +02:00
enum EStatus { INGAME , LOSER , WINNER } ;
2010-08-03 14:36:52 +03:00
ui8 color ;
2009-03-09 21:40:43 +02:00
ui8 human ; //true if human controlled player, false for AI
2008-12-27 03:01:59 +02:00
ui32 currentSelection ; //id of hero/town, 0xffffffff if none
2010-08-03 15:34:06 +03:00
ui8 team ;
2011-07-05 09:14:07 +03:00
TResources resources ;
2010-12-20 15:04:24 +02:00
std : : vector < ConstTransitivePtr < CGHeroInstance > > heroes ;
std : : vector < ConstTransitivePtr < CGTownInstance > > towns ;
std : : vector < ConstTransitivePtr < CGHeroInstance > > availableHeroes ; //heroes available in taverns
std : : vector < ConstTransitivePtr < CGDwelling > > dwellings ; //used for town growth
2009-09-07 05:29:44 +03:00
2010-07-30 14:29:42 +03:00
ui8 enteredWinningCheatCode , enteredLosingCheatCode ; //if true, this player has entered cheat codes for loss / victory
2010-01-29 22:52:45 +02:00
ui8 status ; //0 - in game, 1 - loser, 2 - winner <- uses EStatus enum
ui8 daysWithoutCastle ;
2009-09-07 05:29:44 +03:00
PlayerState ( ) ;
2010-12-12 01:11:26 +02:00
std : : string nodeName ( ) const OVERRIDE ;
2010-05-14 05:18:37 +03:00
//override
2010-11-13 22:26:15 +02:00
//void getParents(TCNodes &out, const CBonusSystemNode *root = NULL) const;
//void getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root = NULL) const;
2009-09-07 05:29:44 +03:00
2008-12-27 03:01:59 +02:00
template < typename Handler > void serialize ( Handler & h , const int version )
{
2010-08-03 15:34:06 +03:00
h & color & human & currentSelection & team & resources & status ;
2011-07-13 21:39:02 +03:00
h & heroes & towns & availableHeroes & dwellings & getBonusList ( ) ;
2010-11-10 02:06:25 +02:00
h & status & daysWithoutCastle ;
2010-07-30 14:29:42 +03:00
h & enteredLosingCheatCode & enteredWinningCheatCode ;
2010-05-02 21:20:26 +03:00
h & static_cast < CBonusSystemNode & > ( * this ) ;
2008-12-27 03:01:59 +02:00
}
} ;
2010-08-03 15:34:06 +03:00
struct DLL_EXPORT TeamState : public CBonusSystemNode
{
public :
2010-08-06 16:14:10 +03:00
ui8 id ; //position in gameState::teams
2010-08-03 15:34:06 +03:00
std : : set < ui8 > players ; // members of this team
std : : vector < std : : vector < std : : vector < ui8 > > > fogOfWarMap ; //true - visible, false - hidden
2011-02-21 06:13:00 +02:00
TeamState ( ) ;
2010-08-03 15:34:06 +03:00
template < typename Handler > void serialize ( Handler & h , const int version )
{
2010-12-22 22:14:40 +02:00
h & id & players & fogOfWarMap ;
2010-08-03 15:34:06 +03:00
h & static_cast < CBonusSystemNode & > ( * this ) ;
}
} ;
2008-12-27 03:01:59 +02:00
struct UpgradeInfo
{
int oldID ; //creature to be upgraded
std : : vector < int > newID ; //possible upgrades
2011-07-05 09:14:07 +03:00
std : : vector < TResources > cost ; // cost[upgrade_serial] -> set of pairs<resource_ID,resource_amount>; cost is for single unit (not entire stack)
2008-12-27 03:01:59 +02:00
UpgradeInfo ( ) { oldID = - 1 ; } ;
} ;
2009-03-19 21:04:46 +02:00
struct CPathNode
{
2009-10-16 05:09:58 +03:00
bool accessible ; //true if a hero can be on this node
2009-03-19 21:04:46 +02:00
int dist ; //distance from the first node of searching; -1 is infinity
CPathNode * theNodeBefore ;
int3 coord ; //coordiantes
bool visited ;
} ;
2009-08-28 12:03:58 +03:00
struct CGPathNode
{
2009-12-20 19:14:14 +02:00
enum
{
ACCESSIBLE = 1 , //tile can be entered and passed
VISITABLE , //tile can be entered as the last tile in path
BLOCKVIS , //visitable from neighbouring tile but not passable
2010-05-15 18:00:19 +03:00
BLOCKED , //tile can't be entered nor visited
FLYABLE //if hero flies, he can pass this tile
2009-12-20 19:14:14 +02:00
} ;
2009-08-28 12:03:58 +03:00
ui8 accessible ; //the enum above
2009-08-30 15:47:40 +03:00
ui8 land ;
2009-08-28 12:03:58 +03:00
ui8 turns ;
ui32 moveRemains ;
2009-08-30 15:47:40 +03:00
CGPathNode * theNodeBefore ;
int3 coord ; //coordinates
2009-08-28 12:03:58 +03:00
CGPathNode ( ) ;
} ;
2009-03-19 21:04:46 +02:00
struct DLL_EXPORT CPath
{
std : : vector < CPathNode > nodes ; //just get node by node
int3 startPos ( ) const ; // start point
int3 endPos ( ) const ; //destination point
void convert ( ui8 mode ) ; //mode=0 -> from 'manifest' to 'object'
2009-03-19 16:17:19 +02:00
} ;
2009-08-30 15:47:40 +03:00
struct DLL_EXPORT CGPath
{
std : : vector < CGPathNode > nodes ; //just get node by node
int3 startPos ( ) const ; // start point
int3 endPos ( ) const ; //destination point
void convert ( ui8 mode ) ; //mode=0 -> from 'manifest' to 'object'
} ;
struct DLL_EXPORT CPathsInfo
2009-08-28 12:03:58 +03:00
{
2009-09-07 05:29:44 +03:00
const CGHeroInstance * hero ;
int3 hpos ;
2009-08-28 12:03:58 +03:00
int3 sizes ;
CGPathNode * * * nodes ; //[w][h][level]
2009-08-30 15:47:40 +03:00
bool getPath ( const int3 & dst , CGPath & out ) ;
CPathsInfo ( const int3 & Sizes ) ;
2009-08-28 12:03:58 +03:00
~ CPathsInfo ( ) ;
} ;
2011-01-21 04:36:30 +02:00
struct DLL_EXPORT DuelParameters
{
si32 terType , bfieldType ;
struct SideSettings
{
struct StackSettings
{
si32 type ;
si32 count ;
template < typename Handler > void serialize ( Handler & h , const int version )
{
h & type & count ;
}
StackSettings ( ) ;
StackSettings ( si32 Type , si32 Count ) ;
} stacks [ ARMY_SIZE ] ;
si32 heroId ; //-1 if none
std : : set < si32 > spells ;
SideSettings ( ) ;
template < typename Handler > void serialize ( Handler & h , const int version )
{
h & stacks & heroId & spells ;
}
} sides [ 2 ] ;
DuelParameters ( ) ;
template < typename Handler > void serialize ( Handler & h , const int version )
{
h & terType & bfieldType & sides ;
}
} ;
2011-05-04 05:38:58 +03:00
struct BattleInfo ;
2010-12-25 21:23:30 +02:00
2011-05-10 01:20:47 +03:00
class DLL_EXPORT CGameState : public CNonConstInfoCallback
2008-12-27 03:01:59 +02:00
{
2009-03-07 00:11:17 +02:00
public :
2010-12-20 15:04:24 +02:00
ConstTransitivePtr < StartInfo > scenarioOps , initialOpts ; //second one is a copy of settings received from pregame (not randomized)
2010-12-20 23:22:53 +02:00
ConstTransitivePtr < CCampaignState > campaign ;
2008-12-27 03:01:59 +02:00
ui32 seed ;
ui8 currentPlayer ; //ID of player currently having turn
2010-12-20 23:22:53 +02:00
ConstTransitivePtr < BattleInfo > curB ; //current battle
2008-12-27 03:01:59 +02:00
ui32 day ; //total number of days in game
2010-12-20 15:04:24 +02:00
ConstTransitivePtr < Mapa > map ;
bmap < ui8 , PlayerState > players ; //ID <-> player state
bmap < ui8 , TeamState > teams ; //ID <-> team state
bmap < int , ConstTransitivePtr < CGDefInfo > > villages , forts , capitols ; //def-info for town graphics
2010-05-02 21:20:26 +03:00
CBonusSystemNode globalEffects ;
2011-05-10 01:20:47 +03:00
bmap < const CGHeroInstance * , const CGObjectInstance * > ongoingVisits ;
2008-12-27 03:01:59 +02:00
struct DLL_EXPORT HeroesPool
{
2010-12-20 15:04:24 +02:00
bmap < ui32 , ConstTransitivePtr < CGHeroInstance > > heroesPool ; //[subID] - heroes available to buy; NULL if not available
bmap < ui32 , ui8 > pavailable ; // [subid] -> which players can recruit hero (binary flags)
2008-12-27 03:01:59 +02:00
2010-12-20 15:04:24 +02:00
CGHeroInstance * pickHeroFor ( bool native , int player , const CTown * town , bmap < ui32 , ConstTransitivePtr < CGHeroInstance > > & available , const CHeroClass * bannedClass = NULL ) const ;
2009-01-06 20:42:20 +02:00
template < typename Handler > void serialize ( Handler & h , const int version )
{
h & heroesPool & pavailable ;
}
2008-12-27 03:01:59 +02:00
} hpool ; //we have here all heroes available on this map that are not hired
boost : : shared_mutex * mx ;
2011-05-10 01:20:47 +03:00
2010-05-08 21:56:38 +03:00
void init ( StartInfo * si , ui32 checksum , int Seed ) ;
2008-12-27 03:01:59 +02:00
void loadTownDInfos ( ) ;
void randomizeObject ( CGObjectInstance * cur ) ;
2009-09-28 17:21:48 +03:00
std : : pair < int , int > pickObject ( CGObjectInstance * obj ) ; //chooses type of object to be randomized, returns <type, subtype>
2008-12-27 03:01:59 +02:00
int pickHero ( int owner ) ;
2009-03-07 00:25:19 +02:00
void apply ( CPack * pack ) ;
2009-02-09 16:50:32 +02:00
int battleGetBattlefieldType ( int3 tile = int3 ( ) ) ; // 1. sand/shore 2. sand/mesas 3. dirt/birches 4. dirt/hills 5. dirt/pines 6. grass/hills 7. grass/pines 8. lava 9. magic plains 10. snow/mountains 11. snow/trees 12. subterranean 13. swamp/trees 14. fiery fields 15. rock lands 16. magic clouds 17. lucid pools 18. holy ground 19. clover field 20. evil fog 21. "favourable winds" text on magic plains background 22. cursed ground 23. rough 24. ship to ship 25. ship
2010-07-15 06:04:57 +03:00
UpgradeInfo getUpgradeInfo ( const CStackInstance & stack ) ;
2010-08-13 13:46:08 +03:00
int getPlayerRelations ( ui8 color1 , ui8 color2 ) ; // 0 = enemy, 1 = ally, 2 = same player
2010-05-18 10:01:54 +03:00
//float getMarketEfficiency(int player, int mode=0);
2009-08-30 15:47:40 +03:00
bool checkForVisitableDir ( const int3 & src , const int3 & dst ) const ; //check if src tile is visitable from dst tile
bool checkForVisitableDir ( const int3 & src , const TerrainTile * pom , const int3 & dst ) const ; //check if src tile is visitable from dst tile
2009-06-11 20:21:06 +03:00
bool getPath ( int3 src , int3 dest , const CGHeroInstance * hero , CPath & ret ) ; //calculates path between src and dest; returns pointer to newly allocated CPath or NULL if path does not exists
2009-08-30 15:47:40 +03:00
void calculatePaths ( const CGHeroInstance * hero , CPathsInfo & out , int3 src = int3 ( - 1 , - 1 , - 1 ) , int movement = - 1 ) ; //calculates possible paths for hero, by default uses current hero position and movement left; returns pointer to newly allocated CPath or NULL if path does not exists
2010-05-06 15:13:31 +03:00
int3 guardingCreaturePosition ( int3 pos ) const ;
2010-01-29 22:52:45 +02:00
int victoryCheck ( ui8 player ) const ; //checks if given player is winner; -1 if std victory, 1 if special victory, 0 else
int lossCheck ( ui8 player ) const ; //checks if given player is loser; -1 if std loss, 1 if special, 0 else
ui8 checkForStandardWin ( ) const ; //returns color of player that accomplished standard victory conditions or 255 if no winner
bool checkForStandardLoss ( ui8 player ) const ; //checks if given player lost the game
2010-02-01 19:51:33 +02:00
void obtainPlayersStats ( SThievesGuildInfo & tgi , int level ) ; //fills tgi with info about other players that is available at given level of thieves' guild
2010-12-20 15:04:24 +02:00
bmap < ui32 , ConstTransitivePtr < CGHeroInstance > > unusedHeroesFromPool ( ) ; //heroes pool without heroes that are available in taverns
2010-12-22 22:14:40 +02:00
BattleInfo * setupBattle ( int3 tile , const CArmedInstance * armies [ 2 ] , const CGHeroInstance * heroes [ 2 ] , bool creatureBank , const CGTownInstance * town ) ;
2011-02-04 16:58:14 +02:00
void buildBonusSystemTree ( ) ;
2011-02-22 11:47:25 +02:00
void attachArmedObjects ( ) ;
void buildGlobalTeamPlayerTree ( ) ;
void deserializationFix ( ) ;
2009-03-07 00:11:17 +02:00
2009-07-30 15:49:45 +03:00
bool isVisible ( int3 pos , int player ) ;
bool isVisible ( const CGObjectInstance * obj , int player ) ;
2009-05-07 20:20:41 +03:00
CGameState ( ) ; //c-tor
~ CGameState ( ) ; //d-tor
2010-07-10 05:15:49 +03:00
void getNeighbours ( const TerrainTile & srct , int3 tile , std : : vector < int3 > & vec , const boost : : logic : : tribool & onLand , bool limitCoastSailing ) ;
2009-08-30 15:47:40 +03:00
int getMovementCost ( const CGHeroInstance * h , const int3 & src , const int3 & dest , int remainingMovePoints = - 1 , bool checkLast = true ) ;
2008-12-27 03:01:59 +02:00
int getDate ( int mode = 0 ) const ; //mode=0 - total days in game, mode=1 - day of week, mode=2 - current week, mode=3 - current month
template < typename Handler > void serialize ( Handler & h , const int version )
{
2011-02-22 11:47:25 +02:00
h & scenarioOps & initialOpts & seed & currentPlayer & day & map & players & teams & hpool & globalEffects & campaign ;
2010-08-01 17:04:48 +03:00
h & villages & forts & capitols ;
2008-12-27 03:01:59 +02:00
if ( ! h . saving )
{
loadTownDInfos ( ) ;
}
2011-02-23 05:57:45 +02:00
BONUS_TREE_DESERIALIZATION_FIX
2008-12-27 03:01:59 +02:00
}
friend class CCallback ;
friend class CLuaCallback ;
friend class CClient ;
friend void initGameState ( Mapa * map , CGameInfo * cgi ) ;
friend class IGameCallback ;
friend class CMapHandler ;
friend class CGameHandler ;
} ;
# endif // __CGAMESTATE_H__
2011-05-04 05:38:58 +03:00