2010-03-12 08:42:55 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2011-12-14 08:40:48 +01:00
// Copyright (C) 2001-2008 Martiño Figueroa
2010-03-12 08:42:55 +01:00
//
// You can redistribute this code 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
// ==============================================================
# include "world.h"
# include <algorithm>
# include <cassert>
# include "config.h"
# include "faction.h"
# include "unit.h"
# include "game.h"
# include "logger.h"
# include "sound_renderer.h"
# include "game_settings.h"
2010-05-03 08:25:54 +02:00
# include "cache_manager.h"
2010-05-20 22:19:34 +02:00
# include <iostream>
2011-01-29 13:42:18 +01:00
# include "sound.h"
# include "sound_renderer.h"
2010-07-13 07:33:43 +02:00
2010-03-12 08:42:55 +01:00
# include "leak_dumper.h"
using namespace Shared : : Graphics ;
using namespace Shared : : Util ;
namespace Glest { namespace Game {
// =====================================================
// class World
// =====================================================
2010-07-17 00:15:09 +02:00
// This limit is to keep RAM use under control while offering better performance.
2010-09-09 03:44:25 +02:00
int MaxExploredCellsLookupItemCache = 9500 ;
2010-07-17 08:33:40 +02:00
time_t ExploredCellsLookupItem : : lastDebug = 0 ;
2010-03-12 08:42:55 +01:00
// ===================== PUBLIC ========================
2012-07-03 21:31:52 +02:00
World : : World ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
Config & config = Config : : getInstance ( ) ;
2013-05-21 02:38:35 +02:00
//staggeredFactionUpdates = config.getBool("StaggeredFactionUpdates","false");
staggeredFactionUpdates = false ;
2011-03-06 14:11:22 +01:00
unitParticlesEnabled = config . getBool ( " UnitParticles " , " true " ) ;
2010-09-17 06:00:48 +02:00
2013-06-11 08:44:26 +02:00
animatedTilesetObjectPosListLoaded = false ;
2010-07-17 00:15:09 +02:00
ExploredCellsLookupItemCache . clear ( ) ;
2010-07-17 03:00:31 +02:00
ExploredCellsLookupItemCacheTimer . clear ( ) ;
2010-07-17 00:17:36 +02:00
ExploredCellsLookupItemCacheTimerCount = 0 ;
2012-07-03 21:31:52 +02:00
// Disable this cache as it takes too much RAM (not sure if its worth the performance gain)
2013-05-21 02:38:35 +02:00
//enableFowAlphaCellsLookupItemCache = config.getBool("EnableFowCache","true");
enableFowAlphaCellsLookupItemCache = true ;
2010-07-17 00:15:09 +02:00
2011-07-05 06:37:35 +02:00
nextCommandGroupId = 0 ;
2010-05-03 08:25:54 +02:00
techTree = NULL ;
2010-04-23 04:48:56 +02:00
fogOfWarOverride = false ;
2013-01-11 19:18:58 +01:00
fogOfWarSkillTypeValue = - 1 ;
2010-04-23 04:48:56 +02:00
2010-03-12 08:42:55 +01:00
fogOfWarSmoothing = config . getBool ( " FogOfWarSmoothing " ) ;
fogOfWarSmoothingFrameSkip = config . getInt ( " FogOfWarSmoothingFrameSkip " ) ;
2013-05-21 07:43:57 +02:00
//MaxExploredCellsLookupItemCache= config.getInt("MaxExploredCellsLookupItemCache",intToStr(MaxExploredCellsLookupItemCache).c_str());
2010-07-17 03:00:31 +02:00
2010-03-12 08:42:55 +01:00
frameCount = 0 ;
2010-05-31 11:24:44 +02:00
//nextUnitId= 0;
2010-03-12 08:42:55 +01:00
scriptManager = NULL ;
this - > game = NULL ;
2010-03-13 22:10:45 +01:00
2011-09-01 03:11:23 +02:00
thisFactionIndex = 0 ;
thisTeamIndex = 0 ;
fogOfWar = false ;
2013-01-11 19:18:58 +01:00
originalGameFogOfWar = fogOfWar ;
2011-09-01 03:11:23 +02:00
perfTimerEnabled = false ;
2011-10-06 20:04:59 +02:00
queuedScenarioName = " " ;
2011-10-06 22:44:55 +02:00
queuedScenarioKeepFactions = false ;
2012-10-17 22:15:50 +02:00
disableAttackEffects = false ;
2011-09-01 03:11:23 +02:00
2012-03-13 00:08:22 +01:00
loadWorldNode = NULL ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-03 08:25:54 +02:00
}
2011-10-06 22:44:55 +02:00
void World : : cleanup ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-17 00:15:09 +02:00
2013-06-11 08:44:26 +02:00
animatedTilesetObjectPosListLoaded = false ;
2010-07-17 00:15:09 +02:00
ExploredCellsLookupItemCache . clear ( ) ;
2010-07-17 03:00:31 +02:00
ExploredCellsLookupItemCacheTimer . clear ( ) ;
2013-01-04 19:00:51 +01:00
//FowAlphaCellsLookupItemCache.clear();
2010-07-17 00:15:09 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-04 22:07:39 +02:00
for ( int i = 0 ; i < factions . size ( ) ; + + i ) {
2011-09-25 06:07:59 +02:00
factions [ i ] - > end ( ) ;
2010-08-04 22:07:39 +02:00
}
2013-01-10 22:16:28 +01:00
masterController . clearSlaves ( true ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-09-25 06:07:59 +02:00
for ( int i = 0 ; i < factions . size ( ) ; + + i ) {
delete factions [ i ] ;
}
2010-08-04 22:07:39 +02:00
factions . clear ( ) ;
2011-09-28 08:57:42 +02:00
# ifdef LEAK_CHECK_UNITS
printf ( " %s::%s \n " , __FILE__ , __FUNCTION__ ) ;
Unit : : dumpMemoryList ( ) ;
UnitPathBasic : : dumpMemoryList ( ) ;
# endif
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-04 22:07:39 +02:00
2010-05-03 08:25:54 +02:00
delete techTree ;
techTree = NULL ;
2010-08-04 18:56:24 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-04 18:56:24 +02:00
2011-01-29 13:42:18 +01:00
for ( std : : map < string , StaticSound * > : : iterator iterMap = staticSoundList . begin ( ) ;
2011-09-01 03:11:23 +02:00
iterMap ! = staticSoundList . end ( ) ; + + iterMap ) {
2011-01-29 13:42:18 +01:00
delete iterMap - > second ;
}
staticSoundList . clear ( ) ;
for ( std : : map < string , StrSound * > : : iterator iterMap = streamSoundList . begin ( ) ;
2011-09-01 03:11:23 +02:00
iterMap ! = streamSoundList . end ( ) ; + + iterMap ) {
2011-01-29 13:42:18 +01:00
delete iterMap - > second ;
}
streamSoundList . clear ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-04 18:56:24 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
2011-10-06 22:44:55 +02:00
World : : ~ World ( ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
cleanup ( ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2011-10-06 20:04:59 +02:00
void World : : endScenario ( ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-11-04 02:12:05 +01:00
Logger : : getInstance ( ) . add ( Lang : : getInstance ( ) . get ( " LogScreenGameUnLoadingWorld " , " " , true ) , true ) ;
2011-10-06 20:04:59 +02:00
2013-06-11 08:44:26 +02:00
animatedTilesetObjectPosListLoaded = false ;
2011-10-06 20:04:59 +02:00
ExploredCellsLookupItemCache . clear ( ) ;
ExploredCellsLookupItemCacheTimer . clear ( ) ;
fogOfWarOverride = false ;
2013-01-11 19:18:58 +01:00
originalGameFogOfWar = fogOfWar ;
fogOfWarSkillTypeValue = - 1 ;
2011-10-06 20:04:59 +02:00
map . end ( ) ;
//stats will be deleted by BattleEnd
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2010-03-12 08:42:55 +01:00
void World : : end ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-11-04 02:12:05 +01:00
Logger : : getInstance ( ) . add ( Lang : : getInstance ( ) . get ( " LogScreenGameUnLoadingWorld " , " " , true ) , true ) ;
2010-03-12 08:42:55 +01:00
2013-06-11 08:44:26 +02:00
animatedTilesetObjectPosListLoaded = false ;
2010-07-17 00:15:09 +02:00
ExploredCellsLookupItemCache . clear ( ) ;
2010-07-17 03:00:31 +02:00
ExploredCellsLookupItemCacheTimer . clear ( ) ;
2010-07-17 00:15:09 +02:00
2010-03-12 08:42:55 +01:00
for ( int i = 0 ; i < factions . size ( ) ; + + i ) {
2011-09-25 06:07:59 +02:00
factions [ i ] - > end ( ) ;
}
2013-01-10 22:16:28 +01:00
masterController . clearSlaves ( true ) ;
2011-09-25 06:07:59 +02:00
for ( int i = 0 ; i < factions . size ( ) ; + + i ) {
delete factions [ i ] ;
2010-03-12 08:42:55 +01:00
}
2010-08-04 22:07:39 +02:00
factions . clear ( ) ;
2011-09-25 06:07:59 +02:00
2011-09-28 08:57:42 +02:00
# ifdef LEAK_CHECK_UNITS
printf ( " %s::%s \n " , __FILE__ , __FUNCTION__ ) ;
Unit : : dumpMemoryList ( ) ;
UnitPathBasic : : dumpMemoryList ( ) ;
# endif
2010-04-23 04:48:56 +02:00
fogOfWarOverride = false ;
2013-01-11 19:18:58 +01:00
originalGameFogOfWar = fogOfWar ;
fogOfWarSkillTypeValue = - 1 ;
2011-01-29 13:42:18 +01:00
2011-03-06 23:50:04 +01:00
map . end ( ) ;
2010-03-12 08:42:55 +01:00
//stats will be deleted by BattleEnd
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
// ========================== init ===============================================
2013-01-11 19:18:58 +01:00
void World : : addFogOfWarSkillType ( const Unit * unit , const FogOfWarSkillType * fowst ) {
std : : pair < const Unit * , const FogOfWarSkillType * > fowData ;
fowData . first = unit ;
fowData . second = fowst ;
mapFogOfWarUnitList [ unit - > getId ( ) ] = fowData ;
if ( ( fowst - > getApplyToTeam ( ) = = true & & unit - > getTeam ( ) = = this - > getThisTeamIndex ( ) ) | |
( fowst - > getApplyToTeam ( ) = = false & & unit - > getFactionIndex ( ) = = this - > getThisFactionIndex ( ) ) ) {
if ( ( fowst - > getFowEnable ( ) = = false & & fogOfWarSkillTypeValue ! = 0 ) | |
( fowst - > getFowEnable ( ) = = true & & fogOfWarSkillTypeValue ! = 1 ) ) {
//printf("In [%s::%s Line: %d] current = %d new = %d\n",__FILE__,__FUNCTION__,__LINE__,fogOfWar,fowst->getFowEnable());
setFogOfWar ( fowst - > getFowEnable ( ) ) ;
}
}
}
2013-01-11 19:40:47 +01:00
bool World : : removeFogOfWarSkillTypeFromList ( const Unit * unit ) {
bool result = false ;
2013-01-11 19:18:58 +01:00
if ( mapFogOfWarUnitList . find ( unit - > getId ( ) ) ! = mapFogOfWarUnitList . end ( ) ) {
mapFogOfWarUnitList . erase ( unit - > getId ( ) ) ;
2013-01-11 19:40:47 +01:00
result = true ;
}
return result ;
}
2013-01-11 19:18:58 +01:00
2013-01-11 19:40:47 +01:00
void World : : removeFogOfWarSkillType ( const Unit * unit ) {
bool removedFromList = removeFogOfWarSkillTypeFromList ( unit ) ;
if ( removedFromList = = true ) {
2013-01-11 19:18:58 +01:00
if ( mapFogOfWarUnitList . empty ( ) = = true ) {
//printf("In [%s::%s Line: %d] current = %d new = %d\n",__FILE__,__FUNCTION__,__LINE__,fogOfWar,originalGameFogOfWar);
fogOfWarSkillTypeValue = - 1 ;
fogOfWarOverride = false ;
minimap . restoreFowTex ( ) ;
}
else {
bool fowEnabled = false ;
for ( std : : map < int , std : : pair < const Unit * , const FogOfWarSkillType * > > : : const_iterator iterMap = mapFogOfWarUnitList . begin ( ) ;
iterMap ! = mapFogOfWarUnitList . end ( ) ; + + iterMap ) {
const Unit * unit = iterMap - > second . first ;
const FogOfWarSkillType * fowst = iterMap - > second . second ;
if ( ( fowst - > getApplyToTeam ( ) = = true & & unit - > getTeam ( ) = = this - > getThisTeamIndex ( ) ) | |
( fowst - > getApplyToTeam ( ) = = false & & unit - > getFactionIndex ( ) = = this - > getThisFactionIndex ( ) ) ) {
if ( fowst - > getFowEnable ( ) = = true ) {
fowEnabled = true ;
break ;
}
}
}
if ( ( fowEnabled = = false & & fogOfWarSkillTypeValue ! = 0 ) | |
( fowEnabled = = true & & fogOfWarSkillTypeValue ! = 1 ) ) {
//printf("In [%s::%s Line: %d] current = %d new = %d\n",__FILE__,__FUNCTION__,__LINE__,fogOfWar,fowEnabled);
setFogOfWar ( fowEnabled ) ;
}
}
}
}
2010-04-23 04:48:56 +02:00
void World : : setFogOfWar ( bool value ) {
2013-01-11 19:18:58 +01:00
//printf("In [%s::%s Line: %d] current = %d new = %d\n",__FILE__,__FUNCTION__,__LINE__,fogOfWar,value);
2010-04-23 04:48:56 +02:00
2013-01-11 19:18:58 +01:00
if ( fogOfWarOverride = = false ) {
minimap . copyFowTex ( ) ;
}
if ( value = = true ) {
fogOfWarSkillTypeValue = 1 ;
2010-04-23 04:48:56 +02:00
}
2013-01-11 19:18:58 +01:00
else {
fogOfWarSkillTypeValue = 0 ;
}
fogOfWarOverride = true ;
2010-04-23 04:48:56 +02:00
}
2011-10-06 20:04:59 +02:00
void World : : clearTileset ( ) {
tileset = Tileset ( ) ;
}
2013-06-04 04:21:56 +02:00
void World : : restoreExploredFogOfWarCells ( ) {
for ( int i = 0 ; i < map . getSurfaceW ( ) ; + + i ) {
for ( int j = 0 ; j < map . getSurfaceH ( ) ; + + j ) {
for ( int k = 0 ; k < GameConstants : : maxPlayers + GameConstants : : specialFactions ; + + k ) {
if ( k = = thisTeamIndex ) {
if ( map . getSurfaceCell ( i , j ) - > isExplored ( k ) = = true ) {
const Vec2i pos ( i , j ) ;
Vec2i surfPos = pos ;
//compute max alpha
float maxAlpha = 0.0f ;
if ( surfPos . x > 1 & & surfPos . y > 1
& & surfPos . x < map . getSurfaceW ( ) - 2
& & surfPos . y < map . getSurfaceH ( ) - 2 ) {
maxAlpha = 1.f ;
} else if ( surfPos . x > 0 & & surfPos . y > 0
& & surfPos . x < map . getSurfaceW ( ) - 1
& & surfPos . y < map . getSurfaceH ( ) - 1 ) {
maxAlpha = 0.3f ;
}
//compute alpha
float alpha = maxAlpha ;
minimap . incFowTextureAlphaSurface ( surfPos , alpha ) ;
}
}
}
}
}
}
2011-10-06 20:04:59 +02:00
void World : : init ( Game * game , bool createUnits , bool initFactions ) {
2010-03-12 08:42:55 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-03 08:25:54 +02:00
2010-07-17 00:15:09 +02:00
ExploredCellsLookupItemCache . clear ( ) ;
2010-07-17 03:00:31 +02:00
ExploredCellsLookupItemCacheTimer . clear ( ) ;
2010-07-17 00:15:09 +02:00
this - > game = game ;
2010-03-12 08:42:55 +01:00
scriptManager = game - > getScriptManager ( ) ;
2010-03-27 08:09:34 +01:00
GameSettings * gs = game - > getGameSettings ( ) ;
2010-04-23 04:48:56 +02:00
if ( fogOfWarOverride = = false ) {
fogOfWar = gs - > getFogOfWar ( ) ;
}
2013-01-11 19:18:58 +01:00
originalGameFogOfWar = fogOfWar ;
2010-12-09 21:41:11 +01:00
2012-03-13 00:08:22 +01:00
if ( loadWorldNode ! = NULL ) {
timeFlow . loadGame ( loadWorldNode ) ;
}
2011-10-06 20:04:59 +02:00
if ( initFactions = = true ) {
initFactionTypes ( gs ) ;
}
2010-03-27 08:09:34 +01:00
initCells ( fogOfWar ) ; //must be done after knowing faction number and dimensions
2010-03-12 08:42:55 +01:00
initMap ( ) ;
initSplattedTextures ( ) ;
2010-07-13 07:33:43 +02:00
unitUpdater . init ( game ) ;
2012-03-13 16:21:25 +01:00
if ( loadWorldNode ! = NULL ) {
unitUpdater . loadGame ( loadWorldNode ) ;
}
2010-07-13 07:33:43 +02:00
2010-03-12 08:42:55 +01:00
//minimap must be init after sum computation
initMinimap ( ) ;
2012-07-18 01:04:30 +02:00
bool gotError = false ;
2012-07-21 01:51:10 +02:00
bool skipStackTrace = false ;
2012-07-21 01:07:44 +02:00
string sErrBuf = " " ;
2012-07-18 01:04:30 +02:00
try {
2012-07-21 00:18:34 +02:00
if ( createUnits ) {
2012-07-18 01:04:30 +02:00
initUnits ( ) ;
}
}
2012-07-21 01:51:10 +02:00
catch ( const megaglest_runtime_error & ex ) {
gotError = true ;
if ( ex . wantStackTrace ( ) = = true ) {
char szErrBuf [ 8096 ] = " " ;
2012-10-19 03:31:20 +02:00
snprintf ( szErrBuf , 8096 , " In [%s::%s %d] " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2012-07-21 01:51:10 +02:00
sErrBuf = string ( szErrBuf ) + string ( " \n error [ " ) + string ( ex . what ( ) ) + string ( " ] \n " ) ;
}
else {
skipStackTrace = true ;
sErrBuf = ex . what ( ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugError , sErrBuf . c_str ( ) ) ;
}
2012-07-18 01:04:30 +02:00
catch ( const std : : exception & ex ) {
gotError = true ;
2012-07-21 01:07:44 +02:00
char szErrBuf [ 8096 ] = " " ;
2012-10-19 03:31:20 +02:00
snprintf ( szErrBuf , 8096 , " In [%s::%s %d] " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2012-07-21 01:07:44 +02:00
sErrBuf = string ( szErrBuf ) + string ( " \n error [ " ) + string ( ex . what ( ) ) + string ( " ] \n " ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugError , sErrBuf . c_str ( ) ) ;
2010-03-12 08:42:55 +01:00
}
2012-03-13 16:21:25 +01:00
if ( loadWorldNode ! = NULL ) {
map . loadGame ( loadWorldNode , this ) ;
2013-03-09 21:57:06 +01:00
if ( fogOfWar = = false ) {
for ( int i = 0 ; i < map . getSurfaceW ( ) ; + + i ) {
for ( int j = 0 ; j < map . getSurfaceH ( ) ; + + j ) {
SurfaceCell * sc = map . getSurfaceCell ( i , j ) ;
if ( sc = = NULL ) {
throw megaglest_runtime_error ( " sc == NULL " ) ;
}
for ( int k = 0 ; k < GameConstants : : maxPlayers ; k + + ) {
//sc->setExplored(k, (game->getGameSettings()->getFlagTypes1() & ft1_show_map_resources) == ft1_show_map_resources);
sc - > setVisible ( k , ! fogOfWar ) ;
}
for ( int k = GameConstants : : maxPlayers ; k < GameConstants : : maxPlayers + GameConstants : : specialFactions ; k + + ) {
sc - > setExplored ( k , true ) ;
sc - > setVisible ( k , true ) ;
}
}
}
}
2013-06-04 04:21:56 +02:00
else {
restoreExploredFogOfWarCells ( ) ;
}
2013-03-09 21:57:06 +01:00
2013-06-04 04:21:56 +02:00
//minimap.loadGame(loadWorldNode);
2012-03-13 16:21:25 +01:00
}
2010-03-27 08:09:34 +01:00
//initExplorationState(); ... was only for !fog-of-war, now handled in initCells()
2010-03-12 08:42:55 +01:00
computeFow ( ) ;
2010-05-03 08:25:54 +02:00
2012-07-18 01:04:30 +02:00
if ( gotError = = true ) {
2012-07-21 01:51:10 +02:00
throw megaglest_runtime_error ( sErrBuf , ! skipStackTrace ) ;
2012-07-18 01:04:30 +02:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
//load tileset
2011-03-15 16:30:28 +01:00
Checksum World : : loadTileset ( const vector < string > pathList , const string & tilesetName ,
2011-05-06 09:47:31 +02:00
Checksum * checksum , std : : map < string , vector < pair < string , string > > > & loadedFileList ) {
2011-01-09 05:49:21 +01:00
Checksum tilsetChecksum ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-03-15 16:30:28 +01:00
tilsetChecksum = tileset . loadTileset ( pathList , tilesetName , checksum , loadedFileList ) ;
2011-01-09 05:49:21 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-18 22:26:40 +01:00
timeFlow . init ( & tileset ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-09 05:49:21 +01:00
return tilsetChecksum ;
2010-03-18 22:26:40 +01:00
}
2011-05-06 09:47:31 +02:00
Checksum World : : loadTileset ( const string & dir , Checksum * checksum , std : : map < string , vector < pair < string , string > > > & loadedFileList ) {
2011-01-09 05:49:21 +01:00
Checksum tilesetChecksum ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-03-15 16:30:28 +01:00
tileset . load ( dir , checksum , & tilesetChecksum , loadedFileList ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
timeFlow . init ( & tileset ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-09 05:49:21 +01:00
return tilesetChecksum ;
2010-03-12 08:42:55 +01:00
}
//load tech
2011-03-15 16:30:28 +01:00
Checksum World : : loadTech ( const vector < string > pathList , const string & techName ,
2011-05-06 09:47:31 +02:00
set < string > & factions , Checksum * checksum , std : : map < string , vector < pair < string , string > > > & loadedFileList ) {
2011-01-09 05:49:21 +01:00
Checksum techtreeChecksum ;
2010-05-03 08:25:54 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-03 08:25:54 +02:00
2012-01-17 02:34:50 +01:00
techTree = new TechTree ( pathList ) ;
techtreeChecksum = techTree - > loadTech ( techName , factions ,
2011-03-15 16:30:28 +01:00
checksum , loadedFileList ) ;
2011-01-09 05:49:21 +01:00
return techtreeChecksum ;
2010-03-12 08:42:55 +01:00
}
2010-07-30 09:51:39 +02:00
std : : vector < std : : string > World : : validateFactionTypes ( ) {
return techTree - > validateFactionTypes ( ) ;
}
std : : vector < std : : string > World : : validateResourceTypes ( ) {
return techTree - > validateResourceTypes ( ) ;
}
2010-03-12 08:42:55 +01:00
//load map
2011-01-09 05:49:21 +01:00
Checksum World : : loadMap ( const string & path , Checksum * checksum ) {
Checksum mapChecksum ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-03 08:25:54 +02:00
2010-03-12 08:42:55 +01:00
checksum - > addFile ( path ) ;
2011-01-09 05:49:21 +01:00
mapChecksum = map . load ( path , techTree , & tileset ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-09 05:49:21 +01:00
return mapChecksum ;
2010-03-12 08:42:55 +01:00
}
//load map
2012-03-26 08:48:58 +02:00
Checksum World : : loadScenario ( const string & path , Checksum * checksum , bool resetCurrentScenario , const XmlNode * rootNode ) {
2011-10-06 20:04:59 +02:00
//printf("[%s:%s] Line: %d path [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str());
2011-01-09 05:49:21 +01:00
Checksum scenarioChecksum ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
checksum - > addFile ( path ) ;
2011-01-09 05:49:21 +01:00
2011-10-06 20:04:59 +02:00
//printf("[%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
if ( resetCurrentScenario = = true ) {
scenario = Scenario ( ) ;
2013-05-28 17:42:54 +02:00
if ( scriptManager ) scriptManager - > init ( this , this - > getGame ( ) - > getGameCameraPtr ( ) , rootNode ) ;
2011-10-06 20:04:59 +02:00
}
2011-01-09 05:49:21 +01:00
scenarioChecksum = scenario . load ( path ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-09 05:49:21 +01:00
return scenarioChecksum ;
2010-03-12 08:42:55 +01:00
}
// ==================== misc ====================
2011-10-06 22:44:55 +02:00
void World : : setQueuedScenario ( string scenarioName , bool keepFactions ) {
2011-10-06 20:04:59 +02:00
queuedScenarioName = scenarioName ;
2011-10-06 22:44:55 +02:00
queuedScenarioKeepFactions = keepFactions ;
2011-10-06 20:04:59 +02:00
}
2012-03-27 01:24:29 +02:00
void World : : updateAllTilesetObjects ( ) {
2013-06-11 08:44:26 +02:00
Gui * gui = this - > game - > getGuiPtr ( ) ;
if ( gui ! = NULL ) {
Object * selObj = gui - > getHighlightedResourceObject ( ) ;
if ( selObj ! = NULL ) {
selObj - > updateHighlight ( ) ;
}
}
if ( animatedTilesetObjectPosListLoaded = = false ) {
animatedTilesetObjectPosListLoaded = true ;
animatedTilesetObjectPosList . clear ( ) ;
for ( int x = 0 ; x < map . getSurfaceW ( ) ; + + x ) {
for ( int y = 0 ; y < map . getSurfaceH ( ) ; + + y ) {
SurfaceCell * sc = map . getSurfaceCell ( x , y ) ;
if ( sc ! = NULL ) {
Object * obj = sc - > getObject ( ) ;
if ( obj ! = NULL & & obj - > isAnimated ( ) = = true ) {
//obj->update();
animatedTilesetObjectPosList . push_back ( Vec2i ( x , y ) ) ;
}
}
}
}
}
if ( animatedTilesetObjectPosList . empty ( ) = = false ) {
for ( unsigned int i = 0 ; i < animatedTilesetObjectPosList . size ( ) ; + + i ) {
const Vec2i & pos = animatedTilesetObjectPosList [ i ] ;
SurfaceCell * sc = map . getSurfaceCell ( pos ) ;
2012-03-27 01:24:29 +02:00
if ( sc ! = NULL ) {
Object * obj = sc - > getObject ( ) ;
2013-06-11 08:44:26 +02:00
if ( obj ! = NULL & & obj - > isAnimated ( ) = = true ) {
2012-03-27 01:24:29 +02:00
obj - > update ( ) ;
}
}
}
}
2013-06-11 08:44:26 +02:00
// for(int x = 0; x < map.getSurfaceW(); ++x) {
// for(int y = 0; y < map.getSurfaceH(); ++y) {
// SurfaceCell *sc = map.getSurfaceCell(x,y);
// if(sc != NULL) {
// Object *obj = sc->getObject();
// if(obj != NULL) {
// obj->update();
// }
// }
// }
// }
2012-03-27 01:24:29 +02:00
}
2010-08-21 20:50:56 +02:00
void World : : updateAllFactionUnits ( ) {
2013-02-22 07:52:51 +01:00
bool showPerfStats = Config : : getInstance ( ) . getBool ( " ShowPerfStats " , " false " ) ;
Chrono chronoPerf ;
if ( showPerfStats ) chronoPerf . start ( ) ;
char perfBuf [ 8096 ] = " " ;
std : : vector < string > perfList ;
2013-05-28 17:42:54 +02:00
if ( scriptManager ) scriptManager - > onTimerTriggerEvent ( ) ;
2011-03-18 04:53:06 +01:00
2011-07-05 06:37:35 +02:00
// Prioritize grouped command units so closest units to target go first
// units
2011-07-06 08:38:56 +02:00
int factionCount = getFactionCount ( ) ;
2012-05-10 01:56:14 +02:00
//printf("===== STARTING Frame: %d\n",frameCount);
2011-09-25 06:07:59 +02:00
// Config &config= Config::getInstance();
// bool sortedUnitsAllowed = config.getBool("AllowGroupedUnitCommands","true");
//
// int factionCount = getFactionCount();
// if(sortedUnitsAllowed == true) {
// for(int i = 0; i < factionCount; ++i) {
// Faction *faction = getFaction(i);
// if(faction == NULL) {
2012-04-14 23:21:09 +02:00
// throw megaglest_runtime_error("faction == NULL");
2011-09-25 06:07:59 +02:00
// }
//
// // Sort units by command groups
// faction->sortUnitsByCommandGroups();
// }
// }
2011-07-05 06:37:35 +02:00
2012-03-31 21:50:45 +02:00
// Clear pathfinder list restrictions
for ( int i = 0 ; i < factionCount ; + + i ) {
Faction * faction = getFaction ( i ) ;
faction - > clearUnitsPathfinding ( ) ;
2012-05-04 23:03:52 +02:00
faction - > clearAproxCanMoveSoonCached ( ) ;
2013-05-21 07:43:57 +02:00
faction - > clearWorldSynchThreadedLogList ( ) ;
2012-03-31 21:50:45 +02:00
}
2013-02-22 07:52:51 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2011-03-18 04:53:06 +01:00
Chrono chrono ;
chrono . start ( ) ;
2013-01-10 22:16:28 +01:00
const bool newThreadManager = Config : : getInstance ( ) . getBool ( " EnableNewThreadManager " , " false " ) ;
if ( newThreadManager = = true ) {
masterController . signalSlaves ( & frameCount ) ;
bool slavesCompleted = masterController . waitTillSlavesTrigger ( 20000 ) ;
if ( SystemFlags : : VERBOSE_MODE_ENABLED & & chrono . getMillis ( ) > = 10 ) printf ( " In [%s::%s Line: %d] *** Faction thread preprocessing took [%lld] msecs for %d factions for frameCount = %d slavesCompleted = %d. \n " , __FILE__ , __FUNCTION__ , __LINE__ , ( long long int ) chrono . getMillis ( ) , factionCount , frameCount , slavesCompleted ) ;
2013-02-22 07:52:51 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2013-01-10 22:16:28 +01:00
}
else {
// Signal the faction threads to do any pre-processing
2011-11-27 06:27:50 +01:00
for ( int i = 0 ; i < factionCount ; + + i ) {
Faction * faction = getFaction ( i ) ;
2013-01-10 22:16:28 +01:00
faction - > signalWorkerThread ( frameCount ) ;
}
2013-02-22 07:52:51 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2013-01-10 22:16:28 +01:00
bool workThreadsFinished = false ;
Chrono chrono ;
chrono . start ( ) ;
const int MAX_FACTION_THREAD_WAIT_MILLISECONDS = 20000 ;
for ( ; chrono . getMillis ( ) < MAX_FACTION_THREAD_WAIT_MILLISECONDS ; ) {
workThreadsFinished = true ;
for ( int i = 0 ; i < factionCount ; + + i ) {
Faction * faction = getFaction ( i ) ;
if ( faction - > isWorkerThreadSignalCompleted ( frameCount ) = = false ) {
workThreadsFinished = false ;
break ;
}
}
2013-01-11 19:18:58 +01:00
if ( workThreadsFinished = = true ) {
2011-11-27 06:27:50 +01:00
break ;
}
}
2011-03-18 04:53:06 +01:00
2013-02-22 07:52:51 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2013-01-10 22:16:28 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED & & chrono . getMillis ( ) > = 10 ) printf ( " In [%s::%s Line: %d] *** Faction thread preprocessing took [%lld] msecs for %d factions for frameCount = %d. \n " , __FILE__ , __FUNCTION__ , __LINE__ , ( long long int ) chrono . getMillis ( ) , factionCount , frameCount ) ;
}
2011-03-18 04:53:06 +01:00
2013-02-22 07:52:51 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2011-03-18 04:53:06 +01:00
//units
2013-02-22 07:52:51 +01:00
Chrono chronoPerfUnit ;
int totalUnitsChecked = 0 ;
int totalUnitsProcessed = 0 ;
2010-11-01 17:44:05 +01:00
for ( int i = 0 ; i < factionCount ; + + i ) {
Faction * faction = getFaction ( i ) ;
2013-05-21 07:43:57 +02:00
faction - > dumpWorldSynchThreadedLogList ( ) ;
2012-05-10 01:56:14 +02:00
faction - > clearUnitsPathfinding ( ) ;
2013-02-22 07:52:51 +01:00
std : : map < CommandClass , int > mapCommandCount ;
std : : map < SkillClass , int > mapSkillCount ;
int unitCountStuck = 0 ;
int unitCountUpdated = 0 ;
2011-07-06 07:16:25 +02:00
int unitCount = faction - > getUnitCount ( ) ;
2010-11-01 17:44:05 +01:00
for ( int j = 0 ; j < unitCount ; + + j ) {
2011-09-22 22:42:06 +02:00
Unit * unit = faction - > getUnit ( j ) ;
2010-11-01 17:44:05 +01:00
if ( unit = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " unit == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
2013-02-22 07:52:51 +01:00
CommandClass unitCommandClass = ccCount ;
if ( unit - > getCurrCommand ( ) ! = NULL ) {
unitCommandClass = unit - > getCurrCommand ( ) - > getCommandType ( ) - > getClass ( ) ;
}
SkillClass unitSkillClass = scCount ;
if ( unit - > getCurrSkill ( ) ! = NULL ) {
unitSkillClass = unit - > getCurrSkill ( ) - > getClass ( ) ;
}
if ( showPerfStats ) chronoPerfUnit . start ( ) ;
int unitBlockCount = unit - > getPath ( ) - > getBlockCount ( ) ;
bool isStuck = unit - > getPath ( ) - > isStuck ( ) ;
bool isStuckWithinTolerance = unit - > isLastStuckFrameWithinCurrentFrameTolerance ( ) ;
uint32 lastStuckFrame = unit - > getLastStuckFrame ( ) ;
if ( unitUpdater . updateUnit ( unit ) = = true ) {
unitCountUpdated + + ;
if ( unit - > getLastStuckFrame ( ) = = frameCount ) {
unitCountStuck + + ;
}
mapCommandCount [ unitCommandClass ] = mapCommandCount [ unitCommandClass ] + 1 ;
mapSkillCount [ unitSkillClass ] = mapSkillCount [ unitSkillClass ] + 1 ;
}
totalUnitsChecked + + ;
if ( showPerfStats & & chronoPerfUnit . getMillis ( ) > = 10 ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " stuck: %d [%d] for unit: \n %sBEFORE unitBlockCount = %d, AFTER = %d, BEFORE lastStuckFrame = %u, AFTER: %u \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerfUnit . getMillis ( ) , isStuck , isStuckWithinTolerance , unit - > toString ( ) . c_str ( ) , unitBlockCount , unit - > getPath ( ) - > getBlockCount ( ) , lastStuckFrame , unit - > getLastStuckFrame ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
}
totalUnitsProcessed + = unitCountUpdated ;
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " faction: %d / %d unitCount = %d unitCountUpdated = %d unitCountStuck = %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) , i + 1 , factionCount , unitCount , unitCountUpdated , unitCountStuck ) ;
perfList . push_back ( perfBuf ) ;
for ( std : : map < CommandClass , int > : : iterator iterMap = mapCommandCount . begin ( ) ;
iterMap ! = mapCommandCount . end ( ) ; + + iterMap ) {
sprintf ( perfBuf , " Command class = %d, count = %d \n " , iterMap - > first , iterMap - > second ) ;
perfList . push_back ( perfBuf ) ;
}
for ( std : : map < SkillClass , int > : : iterator iterMap = mapSkillCount . begin ( ) ;
iterMap ! = mapSkillCount . end ( ) ; + + iterMap ) {
sprintf ( perfBuf , " Skill class = %d, count = %d \n " , iterMap - > first , iterMap - > second ) ;
perfList . push_back ( perfBuf ) ;
}
}
}
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " totalUnitsProcessed = %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) , totalUnitsProcessed ) ;
perfList . push_back ( perfBuf ) ;
}
if ( showPerfStats & & chronoPerf . getMillis ( ) > = 50 ) {
for ( unsigned int x = 0 ; x < perfList . size ( ) ; + + x ) {
printf ( " %s " , perfList [ x ] . c_str ( ) ) ;
2010-03-12 08:42:55 +01:00
}
2011-09-21 08:51:28 +02:00
}
2011-07-06 08:38:56 +02:00
2011-03-18 04:53:06 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED & & chrono . getMillis ( ) > = 20 ) printf ( " In [%s::%s Line: %d] *** Faction MAIN thread processing took [%lld] msecs for %d factions for frameCount = %d. \n " , __FILE__ , __FUNCTION__ , __LINE__ , ( long long int ) chrono . getMillis ( ) , factionCount , frameCount ) ;
2010-08-21 20:50:56 +02:00
}
2010-03-12 08:42:55 +01:00
2010-08-21 20:50:56 +02:00
void World : : underTakeDeadFactionUnits ( ) {
2013-01-27 08:26:35 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-08-24 03:21:34 +02:00
int factionIdxToTick = - 1 ;
if ( staggeredFactionUpdates = = true ) {
factionIdxToTick = tickFactionIndex ( ) ;
if ( factionIdxToTick < 0 ) {
return ;
}
}
2010-11-01 17:44:05 +01:00
int factionCount = getFactionCount ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] factionIdxToTick = %d, factionCount = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , factionIdxToTick , factionCount ) ;
2013-01-27 08:26:35 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-08-24 03:21:34 +02:00
2010-03-12 08:42:55 +01:00
//undertake the dead
2010-11-01 17:44:05 +01:00
for ( int i = 0 ; i < factionCount ; + + i ) {
2010-08-24 03:21:34 +02:00
if ( factionIdxToTick = = - 1 | | factionIdxToTick = = i ) {
2010-11-01 17:44:05 +01:00
Faction * faction = getFaction ( i ) ;
int unitCount = faction - > getUnitCount ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] factionIdxToTick = %d, i = %d, unitCount = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , factionIdxToTick , i , unitCount ) ;
2010-11-01 17:44:05 +01:00
for ( int j = unitCount - 1 ; j > = 0 ; j - - ) {
Unit * unit = faction - > getUnit ( j ) ;
2010-08-24 03:21:34 +02:00
2010-11-01 17:44:05 +01:00
if ( unit = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " unit == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
if ( unit - > getToBeUndertaken ( ) = = true ) {
2013-01-27 08:26:35 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-08-24 03:21:34 +02:00
unit - > undertake ( ) ;
delete unit ;
2013-01-27 08:26:35 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-08-24 03:21:34 +02:00
}
2010-03-12 08:42:55 +01:00
}
}
}
2013-01-27 08:26:35 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-08-21 20:50:56 +02:00
}
2010-03-12 08:42:55 +01:00
2010-08-21 20:50:56 +02:00
void World : : updateAllFactionConsumableCosts ( ) {
2010-03-12 08:42:55 +01:00
//food costs
2010-11-01 17:44:05 +01:00
int resourceTypeCount = techTree - > getResourceTypeCount ( ) ;
int factionCount = getFactionCount ( ) ;
for ( int i = 0 ; i < resourceTypeCount ; + + i ) {
const ResourceType * rt = techTree - > getResourceType ( i ) ;
if ( rt ! = NULL & & rt - > getClass ( ) = = rcConsumable & & frameCount % ( rt - > getInterval ( ) * GameConstants : : updateFps ) = = 0 ) {
for ( int j = 0 ; j < factionCount ; + + j ) {
Faction * faction = getFaction ( j ) ;
if ( faction = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " faction == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
faction - > applyCostsOnInterval ( rt ) ;
2010-03-12 08:42:55 +01:00
}
}
}
2010-08-21 20:50:56 +02:00
}
2013-01-03 18:30:59 +01:00
void World : : update ( ) {
2013-01-30 07:08:14 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2013-01-03 18:30:59 +01:00
bool showPerfStats = Config : : getInstance ( ) . getBool ( " ShowPerfStats " , " false " ) ;
Chrono chronoPerf ;
char perfBuf [ 8096 ] = " " ;
std : : vector < string > perfList ;
if ( showPerfStats ) chronoPerf . start ( ) ;
2010-08-21 20:50:56 +02:00
+ + frameCount ;
//time
timeFlow . update ( ) ;
2013-05-28 17:42:54 +02:00
if ( scriptManager ) scriptManager - > onDayNightTriggerEvent ( ) ;
2010-08-21 20:50:56 +02:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2010-08-21 20:50:56 +02:00
//water effects
2010-12-19 23:33:08 +01:00
waterEffects . update ( 1.0f ) ;
2010-12-23 21:22:40 +01:00
// attack effects
2010-12-19 23:33:08 +01:00
attackEffects . update ( 0.25f ) ;
2010-08-21 20:50:56 +02:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2013-01-30 07:08:14 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2012-03-27 01:24:29 +02:00
// objects on the map from tilesets
updateAllTilesetObjects ( ) ;
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2010-08-25 01:15:33 +02:00
//units
2012-04-02 03:45:13 +02:00
if ( getFactionCount ( ) > 0 ) {
updateAllFactionUnits ( ) ;
2010-08-24 03:21:34 +02:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2012-04-02 03:45:13 +02:00
//undertake the dead
underTakeDeadFactionUnits ( ) ;
//}
2010-08-21 20:50:56 +02:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2013-01-30 07:08:14 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2012-04-02 03:45:13 +02:00
//food costs
updateAllFactionConsumableCosts ( ) ;
2010-03-12 08:42:55 +01:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2013-01-30 07:08:14 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2012-04-02 03:45:13 +02:00
//fow smoothing
if ( fogOfWarSmoothing & & ( ( frameCount + 1 ) % ( fogOfWarSmoothingFrameSkip + 1 ) ) = = 0 ) {
float fogFactor = static_cast < float > ( frameCount % GameConstants : : updateFps ) / GameConstants : : updateFps ;
minimap . updateFowTex ( clamp ( fogFactor , 0.f , 1.f ) ) ;
}
2010-03-12 08:42:55 +01:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2013-01-30 07:08:14 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2012-04-02 03:45:13 +02:00
//tick
bool needToTick = canTickWorld ( ) ;
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2012-04-02 03:45:13 +02:00
if ( needToTick = = true ) {
//printf("=========== World is about to be updated, current frameCount = %d\n",frameCount);
2013-01-30 07:08:14 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2012-04-02 03:45:13 +02:00
tick ( ) ;
}
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
}
if ( showPerfStats & & chronoPerf . getMillis ( ) > = 50 ) {
for ( unsigned int x = 0 ; x < perfList . size ( ) ; + + x ) {
printf ( " %s " , perfList [ x ] . c_str ( ) ) ;
}
2010-03-12 08:42:55 +01:00
}
2013-01-30 07:08:14 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
2010-08-24 04:49:55 +02:00
bool World : : canTickWorld ( ) const {
//tick
bool needToTick = ( frameCount % GameConstants : : updateFps = = 0 ) ;
if ( staggeredFactionUpdates = = true ) {
needToTick = ( frameCount % ( GameConstants : : updateFps / GameConstants : : maxPlayers ) = = 0 ) ;
}
return needToTick ;
}
2010-08-23 11:53:04 +02:00
bool World : : canTickFaction ( int factionIdx ) {
2010-08-23 16:48:33 +02:00
int factionUpdateInterval = ( GameConstants : : updateFps / GameConstants : : maxPlayers ) ;
int expectedFactionIdx = ( frameCount / factionUpdateInterval ) - 1 ;
2010-08-23 11:53:04 +02:00
if ( expectedFactionIdx > = GameConstants : : maxPlayers ) {
expectedFactionIdx = expectedFactionIdx % GameConstants : : maxPlayers ;
2010-03-12 08:42:55 +01:00
}
2010-08-23 11:53:04 +02:00
bool result = ( expectedFactionIdx = = factionIdx ) ;
2010-03-12 08:42:55 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] factionIdx = %d, frameCount = %d, GameConstants::updateFps = %d, expectedFactionIdx = %d, factionUpdateInterval = %d, result = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , factionIdx , frameCount , GameConstants : : updateFps , expectedFactionIdx , factionUpdateInterval , result ) ;
2010-08-23 11:53:04 +02:00
return result ;
}
2010-08-23 16:48:33 +02:00
int World : : tickFactionIndex ( ) {
2010-08-23 11:53:04 +02:00
int factionIdxToTick = - 1 ;
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
if ( canTickFaction ( i ) = = true ) {
factionIdxToTick = i ;
break ;
2010-03-12 08:42:55 +01:00
}
}
2010-08-23 16:48:33 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] factionIdxToTick = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , factionIdxToTick ) ;
2010-08-23 16:48:33 +02:00
return factionIdxToTick ;
}
void World : : tick ( ) {
2013-01-03 18:30:59 +01:00
bool showPerfStats = Config : : getInstance ( ) . getBool ( " ShowPerfStats " , " false " ) ;
Chrono chronoPerf ;
char perfBuf [ 8096 ] = " " ;
std : : vector < string > perfList ;
if ( showPerfStats ) chronoPerf . start ( ) ;
2010-08-23 21:35:55 +02:00
int factionIdxToTick = - 1 ;
2010-08-23 21:49:00 +02:00
if ( staggeredFactionUpdates = = true ) {
2010-08-24 03:21:34 +02:00
factionIdxToTick = tickFactionIndex ( ) ;
2010-08-23 21:49:00 +02:00
if ( factionIdxToTick < 0 ) {
return ;
}
}
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2010-08-23 16:48:33 +02:00
computeFow ( factionIdxToTick ) ;
2010-08-23 11:53:04 +02:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " fogOfWar: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) , fogOfWar ) ;
perfList . push_back ( perfBuf ) ;
}
2010-08-23 21:35:55 +02:00
if ( factionIdxToTick = = - 1 | | factionIdxToTick = = 0 ) {
2010-08-23 16:48:33 +02:00
if ( fogOfWarSmoothing = = false ) {
minimap . updateFowTex ( 1.f ) ;
}
2010-08-23 11:53:04 +02:00
}
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2010-08-23 11:53:04 +02:00
//increase hp
2010-11-01 17:44:05 +01:00
int factionCount = getFactionCount ( ) ;
for ( int i = 0 ; i < factionCount ; + + i ) {
2010-08-23 21:35:55 +02:00
if ( factionIdxToTick = = - 1 | | i = = factionIdxToTick ) {
2010-11-01 17:44:05 +01:00
Faction * faction = getFaction ( i ) ;
int unitCount = faction - > getUnitCount ( ) ;
for ( int j = 0 ; j < unitCount ; + + j ) {
Unit * unit = faction - > getUnit ( j ) ;
if ( unit = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " unit == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
unit - > tick ( ) ;
2010-08-23 11:53:04 +02:00
}
2010-08-23 21:35:55 +02:00
}
}
2010-03-12 08:42:55 +01:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2010-03-12 08:42:55 +01:00
//compute resources balance
2010-11-01 17:44:05 +01:00
factionCount = getFactionCount ( ) ;
for ( int k = 0 ; k < factionCount ; + + k ) {
2010-08-23 21:35:55 +02:00
if ( factionIdxToTick = = - 1 | | k = = factionIdxToTick ) {
2010-08-23 11:53:04 +02:00
Faction * faction = getFaction ( k ) ;
//for each resource
2010-11-01 17:44:05 +01:00
for ( int i = 0 ; i < techTree - > getResourceTypeCount ( ) ; + + i ) {
2010-08-23 11:53:04 +02:00
const ResourceType * rt = techTree - > getResourceType ( i ) ;
//if consumable
2010-11-01 17:44:05 +01:00
if ( rt ! = NULL & & rt - > getClass ( ) = = rcConsumable ) {
2010-08-23 11:53:04 +02:00
int balance = 0 ;
2010-11-01 17:44:05 +01:00
for ( int j = 0 ; j < faction - > getUnitCount ( ) ; + + j ) {
2010-08-23 11:53:04 +02:00
//if unit operative and has this cost
const Unit * u = faction - > getUnit ( j ) ;
2010-11-01 17:44:05 +01:00
if ( u ! = NULL & & u - > isOperative ( ) ) {
2010-08-23 11:53:04 +02:00
const Resource * r = u - > getType ( ) - > getCost ( rt ) ;
2010-11-01 17:44:05 +01:00
if ( r ! = NULL ) {
balance - = u - > getType ( ) - > getCost ( rt ) - > getAmount ( ) ;
2010-08-23 11:53:04 +02:00
}
2010-03-12 08:42:55 +01:00
}
}
2010-08-23 11:53:04 +02:00
faction - > setResourceBalance ( rt , balance ) ;
2010-03-12 08:42:55 +01:00
}
}
2010-08-23 21:35:55 +02:00
}
}
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
if ( showPerfStats & & chronoPerf . getMillis ( ) > = 50 ) {
for ( unsigned int x = 0 ; x < perfList . size ( ) ; + + x ) {
printf ( " %s " , perfList [ x ] . c_str ( ) ) ;
}
}
2010-03-12 08:42:55 +01:00
}
2010-10-19 06:38:55 +02:00
Unit * World : : findUnitById ( int id ) const {
2010-11-01 17:44:05 +01:00
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
2010-10-19 06:38:55 +02:00
const Faction * faction = getFaction ( i ) ;
2010-07-14 08:59:55 +02:00
Unit * unit = faction - > findUnit ( id ) ;
if ( unit ! = NULL ) {
return unit ;
}
2010-03-12 08:42:55 +01:00
}
return NULL ;
}
2010-11-01 17:44:05 +01:00
const UnitType * World : : findUnitTypeById ( const FactionType * factionType , int id ) {
if ( factionType = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " factionType == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
for ( int i = 0 ; i < factionType - > getUnitTypeCount ( ) ; + + i ) {
const UnitType * unitType = factionType - > getUnitType ( i ) ;
if ( unitType ! = NULL & & unitType - > getId ( ) = = id ) {
2010-03-12 08:42:55 +01:00
return unitType ;
}
}
return NULL ;
}
2010-11-01 17:44:05 +01:00
//looks for a place for a unit around a start location, returns true if succeded
bool World : : placeUnit ( const Vec2i & startLoc , int radius , Unit * unit , bool spaciated ) {
if ( unit = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " unit == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
bool freeSpace = false ;
2010-03-12 08:42:55 +01:00
int size = unit - > getType ( ) - > getSize ( ) ;
Field currField = unit - > getCurrField ( ) ;
2010-11-01 17:44:05 +01:00
for ( int r = 1 ; r < radius ; r + + ) {
for ( int i = - r ; i < r ; + + i ) {
for ( int j = - r ; j < r ; + + j ) {
Vec2i pos = Vec2i ( i , j ) + startLoc ;
if ( spaciated ) {
const int spacing = 2 ;
2010-03-12 08:42:55 +01:00
freeSpace = map . isFreeCells ( pos - Vec2i ( spacing ) , size + spacing * 2 , currField ) ;
}
2010-11-01 17:44:05 +01:00
else {
2010-03-12 08:42:55 +01:00
freeSpace = map . isFreeCells ( pos , size , currField ) ;
}
2010-11-01 17:44:05 +01:00
if ( freeSpace ) {
2010-03-12 08:42:55 +01:00
unit - > setPos ( pos ) ;
2011-02-24 23:13:40 +01:00
Vec2i meetingPos = pos - Vec2i ( 1 ) ;
unit - > setMeetingPos ( meetingPos ) ;
2010-03-12 08:42:55 +01:00
return true ;
}
}
}
}
return false ;
}
//clears a unit old position from map and places new position
2010-11-01 17:44:05 +01:00
void World : : moveUnitCells ( Unit * unit ) {
2010-11-09 10:06:52 +01:00
if ( unit = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " unit == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
2010-03-12 08:42:55 +01:00
Vec2i newPos = unit - > getTargetPos ( ) ;
//newPos must be free or the same pos as current
2010-11-09 10:06:52 +01:00
2010-11-01 17:44:05 +01:00
// Only change cell placement in map if the new position is different
2010-08-25 02:29:56 +02:00
// from the old one
if ( newPos ! = unit - > getPos ( ) ) {
map . clearUnitCells ( unit , unit - > getPos ( ) ) ;
map . putUnitCells ( unit , newPos ) ;
}
2010-10-21 22:31:09 +02:00
// Add resources close by to the faction's cache
unit - > getFaction ( ) - > addCloseResourceTargetToCache ( newPos ) ;
2010-03-12 08:42:55 +01:00
//water splash
2010-11-01 17:44:05 +01:00
if ( tileset . getWaterEffects ( ) & & unit - > getCurrField ( ) = = fLand ) {
if ( map . getSubmerged ( map . getCell ( unit - > getLastPos ( ) ) ) ) {
int unitSize = unit - > getType ( ) - > getSize ( ) ;
for ( int i = 0 ; i < 3 ; + + i ) {
2010-03-12 08:42:55 +01:00
waterEffects . addWaterSplash (
2010-05-07 10:01:14 +02:00
Vec2f ( unit - > getLastPos ( ) . x + ( float ) unitSize / 2.0f + random . randRange ( - 0.9f , - 0.1f ) , unit - > getLastPos ( ) . y + random . randRange ( - 0.9f , - 0.1f ) + ( float ) unitSize / 2.0f ) , unit - > getType ( ) - > getSize ( ) ) ;
2010-03-12 08:42:55 +01:00
}
}
}
2010-08-29 06:45:15 +02:00
2013-05-28 17:42:54 +02:00
if ( scriptManager ) scriptManager - > onCellTriggerEvent ( unit ) ;
2010-03-12 08:42:55 +01:00
}
2012-10-17 22:15:50 +02:00
void World : : addAttackEffects ( const Unit * unit ) {
2010-12-19 23:33:08 +01:00
attackEffects . addWaterSplash (
2013-02-04 09:30:43 +01:00
Vec2f ( unit - > getPosNotThreadSafe ( ) . x , unit - > getPosNotThreadSafe ( ) . y ) , 1 ) ;
2010-12-19 23:33:08 +01:00
}
2010-03-12 08:42:55 +01:00
//returns the nearest unit that can store a type of resource given a position and a faction
2010-11-01 17:44:05 +01:00
Unit * World : : nearestStore ( const Vec2i & pos , int factionIndex , const ResourceType * rt ) {
2010-03-12 08:42:55 +01:00
float currDist = infinity ;
Unit * currUnit = NULL ;
2010-11-01 17:44:05 +01:00
if ( factionIndex > = getFactionCount ( ) ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " factionIndex >= getFactionCount() " ) ;
2010-11-01 17:44:05 +01:00
}
for ( int i = 0 ; i < getFaction ( factionIndex ) - > getUnitCount ( ) ; + + i ) {
2010-03-12 08:42:55 +01:00
Unit * u = getFaction ( factionIndex ) - > getUnit ( i ) ;
2010-11-01 17:44:05 +01:00
if ( u ! = NULL ) {
float tmpDist = u - > getPos ( ) . dist ( pos ) ;
2013-02-05 01:01:40 +01:00
if ( tmpDist < currDist & & u - > getType ( ) ! = NULL & & u - > getType ( ) - > getStore ( rt ) > 0 & & u - > isOperative ( ) ) {
2010-11-01 17:44:05 +01:00
currDist = tmpDist ;
currUnit = u ;
}
}
2010-03-12 08:42:55 +01:00
}
return currUnit ;
}
2010-11-01 17:44:05 +01:00
bool World : : toRenderUnit ( const Unit * unit , const Quad2i & visibleQuad ) const {
if ( unit = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " unit == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
//a unit is rendered if it is in a visible cell or is attacking a unit in a visible cell
2013-02-04 09:30:43 +01:00
return visibleQuad . isInside ( unit - > getPosNotThreadSafe ( ) ) & & toRenderUnit ( unit ) ;
2010-03-12 08:42:55 +01:00
}
2010-11-01 17:44:05 +01:00
bool World : : toRenderUnit ( const Unit * unit ) const {
if ( unit = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " unit == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
2010-03-12 08:42:55 +01:00
2010-12-24 09:43:09 +01:00
if ( showWorldForPlayer ( thisFactionIndex ) = = true ) {
return true ;
}
2010-03-12 08:42:55 +01:00
return
2010-06-16 09:18:06 +02:00
( map . getSurfaceCell ( Map : : toSurfCoords ( unit - > getCenteredPos ( ) ) ) - > isVisible ( thisTeamIndex ) & &
map . getSurfaceCell ( Map : : toSurfCoords ( unit - > getCenteredPos ( ) ) ) - > isExplored ( thisTeamIndex ) ) | |
2010-03-12 08:42:55 +01:00
( unit - > getCurrSkill ( ) - > getClass ( ) = = scAttack & &
2010-06-16 09:18:06 +02:00
map . getSurfaceCell ( Map : : toSurfCoords ( unit - > getTargetPos ( ) ) ) - > isVisible ( thisTeamIndex ) & &
map . getSurfaceCell ( Map : : toSurfCoords ( unit - > getTargetPos ( ) ) ) - > isExplored ( thisTeamIndex ) ) ;
2010-03-12 08:42:55 +01:00
}
2012-06-22 15:30:15 +02:00
bool World : : toRenderUnit ( const UnitBuildInfo & pendingUnit ) const {
if ( pendingUnit . unit = = NULL ) {
2012-06-22 06:58:44 +02:00
throw megaglest_runtime_error ( " unit == NULL " ) ;
}
if ( showWorldForPlayer ( thisFactionIndex ) = = true ) {
return true ;
}
2012-06-22 15:30:15 +02:00
if ( pendingUnit . unit - > getTeam ( ) ! = thisTeamIndex ) {
2012-06-22 06:58:44 +02:00
return false ;
}
return
2012-06-22 15:30:15 +02:00
( map . getSurfaceCell ( Map : : toSurfCoords ( pendingUnit . pos ) ) - > isVisible ( thisTeamIndex ) & &
map . getSurfaceCell ( Map : : toSurfCoords ( pendingUnit . pos ) ) - > isExplored ( thisTeamIndex ) ) ;
2012-06-22 06:58:44 +02:00
}
2011-01-29 13:42:18 +01:00
void World : : morphToUnit ( int unitId , const string & morphName , bool ignoreRequirements ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] unit [%d] morphName [%s] forceUpgradesIfRequired = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitId , morphName . c_str ( ) , ignoreRequirements ) ;
2011-01-29 13:42:18 +01:00
Unit * unit = findUnitById ( unitId ) ;
if ( unit ! = NULL ) {
for ( int i = 0 ; i < unit - > getType ( ) - > getCommandTypeCount ( ) ; + + i ) {
const CommandType * ct = unit - > getType ( ) - > getCommandType ( i ) ;
const MorphCommandType * mct = dynamic_cast < const MorphCommandType * > ( ct ) ;
2013-06-13 10:55:48 +02:00
if ( mct ! = NULL & & mct - > getName ( false ) = = morphName ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] unit [%d] morphName [%s] comparing mct [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitId , morphName . c_str ( ) , mct - > getName ( false ) . c_str ( ) ) ;
2011-01-29 13:42:18 +01:00
2012-11-10 20:39:55 +01:00
std : : pair < CommandResult , string > cr ( crFailUndefined , " " ) ;
2011-01-29 13:42:18 +01:00
try {
if ( unit - > getFaction ( ) - > reqsOk ( mct ) = = false & & ignoreRequirements = = true ) {
2013-06-13 10:55:48 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] unit [%d] morphName [%s] comparing mct [%s] mct->getUpgradeReqCount() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitId , morphName . c_str ( ) , mct - > getName ( false ) . c_str ( ) , mct - > getUpgradeReqCount ( ) ) ;
2011-01-29 13:42:18 +01:00
unit - > setIgnoreCheckCommand ( true ) ;
}
const UnitType * unitType = mct - > getMorphUnit ( ) ;
cr = this - > game - > getCommander ( ) - > tryGiveCommand ( unit , mct , unit - > getPos ( ) , unitType , CardinalDir : : NORTH ) ;
}
catch ( const exception & ex ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-29 13:42:18 +01:00
unit - > setIgnoreCheckCommand ( false ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( ex . what ( ) ) ;
2011-01-29 13:42:18 +01:00
}
2012-11-10 20:39:55 +01:00
if ( cr . first = = crSuccess ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-29 13:42:18 +01:00
}
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-29 13:42:18 +01:00
}
2013-06-13 10:55:48 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] unit [%d] morphName [%s] comparing mct [%s] returned = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitId , morphName . c_str ( ) , mct - > getName ( false ) . c_str ( ) , cr . first ) ;
2011-01-29 13:42:18 +01:00
break ;
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-29 13:42:18 +01:00
}
}
2012-02-21 22:56:20 +01:00
void World : : createUnit ( const string & unitName , int factionIndex , const Vec2i & pos , bool spaciated ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] unitName [%s] factionIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitName . c_str ( ) , factionIndex ) ;
2010-05-01 12:46:56 +02:00
2010-11-01 17:44:05 +01:00
if ( factionIndex < factions . size ( ) ) {
2011-09-25 06:07:59 +02:00
Faction * faction = factions [ factionIndex ] ;
2010-05-29 11:38:00 +02:00
if ( faction - > getIndex ( ) ! = factionIndex ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " faction->getIndex() ! = factionIndex " ) ;
2010-05-29 11:38:00 +02:00
}
2010-03-12 08:42:55 +01:00
const FactionType * ft = faction - > getType ( ) ;
const UnitType * ut = ft - > getUnitType ( unitName ) ;
2010-07-21 20:21:40 +02:00
UnitPathInterface * newpath = NULL ;
switch ( game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
newpath = new UnitPathBasic ( ) ;
break ;
2010-07-21 22:40:11 +02:00
default :
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " detected unsupported pathfinder type! " ) ;
2010-07-21 20:21:40 +02:00
}
Unit * unit = new Unit ( getNextUnitId ( faction ) , newpath , pos , ut , faction , & map , CardinalDir : : NORTH ) ;
2010-03-12 08:42:55 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] unit created for unit [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > toString ( ) . c_str ( ) ) ;
2010-05-29 11:38:00 +02:00
2012-02-21 22:56:20 +01:00
if ( placeUnit ( pos , generationArea , unit , spaciated ) ) {
2010-03-12 08:42:55 +01:00
unit - > create ( true ) ;
2012-03-27 05:23:03 +02:00
unit - > born ( NULL ) ;
2013-05-28 17:42:54 +02:00
if ( scriptManager ) scriptManager - > onUnitCreated ( unit ) ;
2010-03-12 08:42:55 +01:00
}
2010-11-01 17:44:05 +01:00
else {
2011-03-23 01:49:21 +01:00
delete unit ;
unit = NULL ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Unit cant be placed " ) ;
2010-03-12 08:42:55 +01:00
}
2010-05-29 11:38:00 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] unit created for unit [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > toString ( ) . c_str ( ) ) ;
2010-03-12 08:42:55 +01:00
}
2010-11-01 17:44:05 +01:00
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid faction index in createUnitAtPosition: " + intToStr ( factionIndex ) ) ;
2010-03-12 08:42:55 +01:00
}
2010-05-01 12:46:56 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
2010-11-01 17:44:05 +01:00
void World : : giveResource ( const string & resourceName , int factionIndex , int amount ) {
if ( factionIndex < factions . size ( ) ) {
2011-09-25 06:07:59 +02:00
Faction * faction = factions [ factionIndex ] ;
2010-05-03 08:25:54 +02:00
const ResourceType * rt = techTree - > getResourceType ( resourceName ) ;
2010-03-12 08:42:55 +01:00
faction - > incResourceAmount ( rt , amount ) ;
}
2010-11-01 17:44:05 +01:00
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid faction index in giveResource: " + intToStr ( factionIndex ) ) ;
2010-03-12 08:42:55 +01:00
}
}
2012-03-30 09:10:14 +02:00
int World : : getUnitCurrentField ( int unitId ) {
int field = - 1 ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] unit [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitId ) ;
Unit * unit = findUnitById ( unitId ) ;
if ( unit ! = NULL ) {
field = unit - > getCurrField ( ) ;
}
return field ;
}
2012-03-31 22:17:19 +02:00
bool World : : getIsUnitAlive ( int unitId ) {
bool result = false ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] unit [%d] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unitId ) ;
Unit * unit = findUnitById ( unitId ) ;
if ( unit ! = NULL ) {
result = unit - > isAlive ( ) ;
}
return result ;
}
2012-03-30 09:10:14 +02:00
vector < int > World : : getUnitsForFaction ( int factionIndex , const string & commandTypeName , int field ) {
vector < int > units ;
if ( factionIndex < 0 | | factionIndex > getFactionCount ( ) ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid faction index in getUnitsForFaction: " + intToStr ( factionIndex ) ) ;
2012-03-30 09:10:14 +02:00
}
Faction * faction = getFaction ( factionIndex ) ;
if ( faction ! = NULL ) {
CommandType * commandType = NULL ;
if ( commandTypeName ! = " " ) {
commandType = CommandTypeFactory : : getInstance ( ) . newInstance ( commandTypeName ) ;
}
int unitCount = faction - > getUnitCount ( ) ;
for ( int i = 0 ; i < unitCount ; + + i ) {
Unit * unit = faction - > getUnit ( i ) ;
if ( unit ! = NULL ) {
bool addToList = true ;
if ( commandType ! = NULL ) {
if ( commandType - > getClass ( ) = = ccAttack & & field > = 0 ) {
const AttackCommandType * act = unit - > getType ( ) - > getFirstAttackCommand ( static_cast < Field > ( field ) ) ;
addToList = ( act ! = NULL ) ;
}
else if ( commandType - > getClass ( ) = = ccAttackStopped & & field > = 0 ) {
const AttackStoppedCommandType * asct = unit - > getType ( ) - > getFirstAttackStoppedCommand ( static_cast < Field > ( field ) ) ;
addToList = ( asct ! = NULL ) ;
}
else {
addToList = unit - > getType ( ) - > hasCommandClass ( commandType - > getClass ( ) ) ;
}
}
else if ( field > = 0 ) {
addToList = ( unit - > getCurrField ( ) = = static_cast < Field > ( field ) ) ;
}
if ( addToList = = true ) {
units . push_back ( unit - > getId ( ) ) ;
}
}
}
delete commandType ;
commandType = NULL ;
}
return units ;
}
2010-11-01 17:44:05 +01:00
void World : : givePositionCommand ( int unitId , const string & commandName , const Vec2i & pos ) {
2010-03-12 08:42:55 +01:00
Unit * unit = findUnitById ( unitId ) ;
2010-11-01 17:44:05 +01:00
if ( unit ! = NULL ) {
2010-03-12 08:42:55 +01:00
CommandClass cc ;
2010-11-01 17:44:05 +01:00
if ( commandName = = " move " ) {
2010-03-12 08:42:55 +01:00
cc = ccMove ;
}
2010-11-01 17:44:05 +01:00
else if ( commandName = = " attack " ) {
2010-03-12 08:42:55 +01:00
cc = ccAttack ;
}
2010-11-01 17:44:05 +01:00
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid position commmand: " + commandName ) ;
2010-03-12 08:42:55 +01:00
}
2012-03-30 07:53:33 +02:00
if ( unit - > getType ( ) - > getFirstCtOfClass ( cc ) = = NULL ) {
2013-06-13 10:55:48 +02:00
throw megaglest_runtime_error ( " Invalid commmand: [ " + commandName + " ] for unit: [ " + unit - > getType ( ) - > getName ( false ) + " ] id [ " + intToStr ( unit - > getId ( ) ) + " ] " ) ;
2012-03-30 07:53:33 +02:00
}
2013-06-13 10:55:48 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] cc = %d Unit [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , cc , unit - > getFullName ( false ) . c_str ( ) ) ;
2010-03-12 08:42:55 +01:00
unit - > giveCommand ( new Command ( unit - > getType ( ) - > getFirstCtOfClass ( cc ) , pos ) ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
2010-11-01 17:44:05 +01:00
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid unitId index in givePositionCommand: " + intToStr ( unitId ) + " commandName = " + commandName ) ;
2010-11-01 17:44:05 +01:00
}
2010-03-12 08:42:55 +01:00
}
2012-10-17 22:15:50 +02:00
void World : : giveStopCommand ( int unitId ) {
Unit * unit = findUnitById ( unitId ) ;
if ( unit ! = NULL ) {
const CommandType * ct = unit - > getType ( ) - > getFirstCtOfClass ( ccStop ) ;
if ( ct ! = NULL ) {
2013-06-13 10:55:48 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] Unit [%s] will stop \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > getFullName ( false ) . c_str ( ) ) ;
2012-10-17 22:15:50 +02:00
unit - > giveCommand ( new Command ( ct ) ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
else {
throw megaglest_runtime_error ( " Invalid ct in giveStopCommand: " + intToStr ( unitId ) ) ;
}
}
else {
throw megaglest_runtime_error ( " Invalid unitId index in giveStopCommand: " + intToStr ( unitId ) ) ;
}
}
bool World : : selectUnit ( int unitId ) {
bool result = false ;
Unit * unit = findUnitById ( unitId ) ;
if ( unit ! = NULL ) {
result = game - > addUnitToSelection ( unit ) ;
}
return result ;
}
void World : : unselectUnit ( int unitId ) {
Unit * unit = findUnitById ( unitId ) ;
if ( unit ! = NULL ) {
game - > removeUnitFromSelection ( unit ) ;
}
}
void World : : addUnitToGroupSelection ( int unitId , int groupIndex ) {
Unit * unit = findUnitById ( unitId ) ;
if ( unit ! = NULL ) {
game - > addUnitToGroupSelection ( unit , groupIndex ) ;
}
}
void World : : removeUnitFromGroupSelection ( int unitId , int groupIndex ) {
game - > removeUnitFromGroupSelection ( unitId , groupIndex ) ;
}
void World : : recallGroupSelection ( int groupIndex ) {
game - > recallGroupSelection ( groupIndex ) ;
}
void World : : setAttackWarningsEnabled ( bool enabled ) {
this - > disableAttackEffects = ! enabled ;
}
bool World : : getAttackWarningsEnabled ( ) {
return ( this - > disableAttackEffects = = false ) ;
}
2010-08-28 10:06:32 +02:00
void World : : giveAttackCommand ( int unitId , int unitToAttackId ) {
Unit * unit = findUnitById ( unitId ) ;
2010-11-01 17:44:05 +01:00
if ( unit ! = NULL ) {
2010-08-28 10:06:32 +02:00
Unit * targetUnit = findUnitById ( unitToAttackId ) ;
if ( targetUnit ! = NULL ) {
2010-11-01 17:44:05 +01:00
const CommandType * ct = unit - > getType ( ) - > getFirstAttackCommand ( targetUnit - > getCurrField ( ) ) ;
2010-08-28 10:06:32 +02:00
if ( ct ! = NULL ) {
2013-06-13 10:55:48 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] Unit [%s] is attacking [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > getFullName ( false ) . c_str ( ) , targetUnit - > getFullName ( false ) . c_str ( ) ) ;
2010-08-28 10:06:32 +02:00
unit - > giveCommand ( new Command ( ct , targetUnit ) ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-28 10:06:32 +02:00
}
2010-11-01 17:44:05 +01:00
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid ct in giveAttackCommand: " + intToStr ( unitId ) + " unitToAttackId = " + intToStr ( unitToAttackId ) ) ;
2010-11-01 17:44:05 +01:00
}
}
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid unitToAttackId index in giveAttackCommand: " + intToStr ( unitId ) + " unitToAttackId = " + intToStr ( unitToAttackId ) ) ;
2010-08-28 10:06:32 +02:00
}
}
2010-11-01 17:44:05 +01:00
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid unitId index in giveAttackCommand: " + intToStr ( unitId ) + " unitToAttackId = " + intToStr ( unitToAttackId ) ) ;
2010-11-01 17:44:05 +01:00
}
2010-08-28 10:06:32 +02:00
}
2010-11-01 17:44:05 +01:00
void World : : giveProductionCommand ( int unitId , const string & producedName ) {
2013-05-28 08:07:35 +02:00
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
2010-03-12 08:42:55 +01:00
Unit * unit = findUnitById ( unitId ) ;
2013-05-28 08:07:35 +02:00
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
2010-11-01 17:44:05 +01:00
if ( unit ! = NULL ) {
2013-05-28 08:07:35 +02:00
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
2010-03-12 08:42:55 +01:00
const UnitType * ut = unit - > getType ( ) ;
2013-05-28 08:07:35 +02:00
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
2010-03-12 08:42:55 +01:00
//Search for a command that can produce the unit
2010-11-01 17:44:05 +01:00
for ( int i = 0 ; i < ut - > getCommandTypeCount ( ) ; + + i ) {
2010-03-12 08:42:55 +01:00
const CommandType * ct = ut - > getCommandType ( i ) ;
2010-11-01 17:44:05 +01:00
if ( ct ! = NULL & & ct - > getClass ( ) = = ccProduce ) {
2013-05-28 08:07:35 +02:00
const ProduceCommandType * pct = dynamic_cast < const ProduceCommandType * > ( ct ) ;
if ( pct ! = NULL & & pct - > getProducedUnit ( ) ! = NULL & &
2013-06-13 10:55:48 +02:00
pct - > getProducedUnit ( ) - > getName ( false ) = = producedName ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-11-01 17:44:05 +01:00
2013-05-28 08:07:35 +02:00
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
2010-03-12 08:42:55 +01:00
unit - > giveCommand ( new Command ( pct ) ) ;
2013-05-28 08:07:35 +02:00
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
2010-11-01 17:44:05 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
break ;
}
}
}
}
2010-11-01 17:44:05 +01:00
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid unitId index in giveProductionCommand: " + intToStr ( unitId ) + " producedName = " + producedName ) ;
2010-11-01 17:44:05 +01:00
}
2013-05-28 08:07:35 +02:00
//printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__);
2010-03-12 08:42:55 +01:00
}
2011-01-29 13:42:18 +01:00
void World : : giveAttackStoppedCommand ( int unitId , const string & itemName , bool ignoreRequirements ) {
Unit * unit = findUnitById ( unitId ) ;
if ( unit ! = NULL ) {
const UnitType * ut = unit - > getType ( ) ;
//Search for a command that can produce the unit
for ( int i = 0 ; i < ut - > getCommandTypeCount ( ) ; + + i ) {
const CommandType * ct = ut - > getCommandType ( i ) ;
if ( ct ! = NULL & & ct - > getClass ( ) = = ccAttackStopped ) {
const AttackStoppedCommandType * act = static_cast < const AttackStoppedCommandType * > ( ct ) ;
2013-06-13 10:55:48 +02:00
if ( act ! = NULL & & act - > getName ( false ) = = itemName ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-29 13:42:18 +01:00
try {
if ( unit - > getFaction ( ) - > reqsOk ( act ) = = false & & ignoreRequirements = = true ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-29 13:42:18 +01:00
unit - > setIgnoreCheckCommand ( true ) ;
}
unit - > giveCommand ( new Command ( act ) ) ;
}
catch ( const exception & ex ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-29 13:42:18 +01:00
unit - > setIgnoreCheckCommand ( false ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( ex . what ( ) ) ;
2011-01-29 13:42:18 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-29 13:42:18 +01:00
break ;
}
}
}
}
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid unitId index in giveAttackStoppedCommand: " + intToStr ( unitId ) + " itemName = " + itemName ) ;
2011-01-29 13:42:18 +01:00
}
}
void World : : playStaticSound ( const string & playSound ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] playSound [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , playSound . c_str ( ) ) ;
2011-01-29 13:42:18 +01:00
2013-05-28 04:54:23 +02:00
string calculatedFilePath = playSound ;
bool changed = Properties : : applyTagsToValue ( calculatedFilePath ) ;
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " \n \n Play static sound changed: %d [%s] [%s] \n \n " , changed , playSound . c_str ( ) , calculatedFilePath . c_str ( ) ) ;
2011-01-29 13:42:18 +01:00
2013-05-28 04:54:23 +02:00
if ( staticSoundList . find ( calculatedFilePath ) = = staticSoundList . end ( ) ) {
2011-01-29 13:42:18 +01:00
StaticSound * sound = new StaticSound ( ) ;
2013-05-28 04:54:23 +02:00
sound - > load ( calculatedFilePath ) ;
staticSoundList [ calculatedFilePath ] = sound ;
2011-01-29 13:42:18 +01:00
}
2013-05-28 04:54:23 +02:00
StaticSound * playSoundItem = staticSoundList [ calculatedFilePath ] ;
SoundRenderer & soundRenderer = SoundRenderer : : getInstance ( ) ;
2011-01-29 13:42:18 +01:00
soundRenderer . playFx ( playSoundItem ) ;
}
void World : : playStreamingSound ( const string & playSound ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] playSound [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , playSound . c_str ( ) ) ;
2011-01-29 13:42:18 +01:00
2013-05-28 04:54:23 +02:00
string calculatedFilePath = playSound ;
bool changed = Properties : : applyTagsToValue ( calculatedFilePath ) ;
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " \n \n Play streaming sound changed: %d [%s] [%s] \n \n " , changed , playSound . c_str ( ) , calculatedFilePath . c_str ( ) ) ;
2011-01-29 13:42:18 +01:00
2013-05-28 04:54:23 +02:00
if ( streamSoundList . find ( calculatedFilePath ) = = streamSoundList . end ( ) ) {
2011-01-29 13:42:18 +01:00
StrSound * sound = new StrSound ( ) ;
2013-05-28 04:54:23 +02:00
sound - > open ( calculatedFilePath ) ;
2011-01-29 13:42:18 +01:00
sound - > setNext ( sound ) ;
2013-05-28 04:54:23 +02:00
streamSoundList [ calculatedFilePath ] = sound ;
2011-01-29 13:42:18 +01:00
}
2013-05-28 04:54:23 +02:00
StrSound * playSoundItem = streamSoundList [ calculatedFilePath ] ;
SoundRenderer & soundRenderer = SoundRenderer : : getInstance ( ) ;
2011-01-29 13:42:18 +01:00
soundRenderer . playMusic ( playSoundItem ) ;
}
void World : : stopStreamingSound ( const string & playSound ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] playSound [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , playSound . c_str ( ) ) ;
2011-01-29 13:42:18 +01:00
2013-05-28 04:54:23 +02:00
string calculatedFilePath = playSound ;
bool changed = Properties : : applyTagsToValue ( calculatedFilePath ) ;
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " \n \n Stop streaming sound changed: %d [%s] [%s] \n \n " , changed , playSound . c_str ( ) , calculatedFilePath . c_str ( ) ) ;
if ( streamSoundList . find ( calculatedFilePath ) ! = streamSoundList . end ( ) ) {
StrSound * playSoundItem = streamSoundList [ calculatedFilePath ] ;
SoundRenderer & soundRenderer = SoundRenderer : : getInstance ( ) ;
2011-01-29 13:42:18 +01:00
soundRenderer . stopMusic ( playSoundItem ) ;
}
}
void World : : stopAllSound ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugLUA ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugLUA , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-29 13:42:18 +01:00
SoundRenderer & soundRenderer = SoundRenderer : : getInstance ( ) ;
soundRenderer . stopAllSounds ( ) ;
}
2011-01-29 18:52:53 +01:00
void World : : moveToUnit ( int unitId , int destUnitId ) {
Unit * unit = findUnitById ( unitId ) ;
if ( unit ! = NULL ) {
CommandClass cc = ccMove ;
2013-06-13 10:55:48 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] cc = %d Unit [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , cc , unit - > getFullName ( false ) . c_str ( ) ) ;
2011-01-29 18:52:53 +01:00
Unit * destUnit = findUnitById ( destUnitId ) ;
if ( destUnit ! = NULL ) {
2013-06-13 10:55:48 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] cc = %d Unit [%s] destUnit [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , cc , unit - > getFullName ( false ) . c_str ( ) , destUnit - > getFullName ( false ) . c_str ( ) ) ;
2011-01-29 18:52:53 +01:00
unit - > giveCommand ( new Command ( unit - > getType ( ) - > getFirstCtOfClass ( cc ) , destUnit ) ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-29 18:52:53 +01:00
}
}
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid unitId index in followUnit: " + intToStr ( unitId ) ) ;
2011-01-29 18:52:53 +01:00
}
}
2011-01-29 13:42:18 +01:00
2013-02-17 08:15:01 +01:00
void World : : clearCaches ( ) {
ExploredCellsLookupItemCache . clear ( ) ;
ExploredCellsLookupItemCacheTimer . clear ( ) ;
ExploredCellsLookupItemCacheTimerCount = 0 ;
2013-02-19 05:53:24 +01:00
unitUpdater . clearCaches ( ) ;
2013-02-17 08:15:01 +01:00
}
2011-12-28 08:26:16 +01:00
void World : : togglePauseGame ( bool pauseStatus , bool forceAllowPauseStateChange ) {
2013-03-06 15:29:49 +01:00
game - > setPaused ( pauseStatus , forceAllowPauseStateChange , false , false ) ;
2011-01-30 00:50:21 +01:00
}
2011-03-19 13:19:22 +01:00
void World : : addConsoleText ( const string & text ) {
2011-04-09 08:20:43 +02:00
game - > getConsole ( ) - > addStdScenarioMessage ( text ) ;
2011-03-19 13:19:22 +01:00
}
2011-11-15 18:39:49 +01:00
void World : : addConsoleTextWoLang ( const string & text ) {
game - > getConsole ( ) - > addLine ( text ) ;
}
2011-03-19 13:19:22 +01:00
2012-01-05 01:04:55 +01:00
const string World : : getSystemMacroValue ( const string key ) {
2012-10-06 09:06:40 +02:00
std : : string result = key ;
2012-01-05 01:04:55 +01:00
bool tagApplied = Properties : : applyTagsToValue ( result ) ;
if ( tagApplied = = false ) {
result = " " ;
if ( key = = " $SCENARIO_PATH " ) {
result = extractDirectoryPathFromFile ( game - > getGameSettings ( ) - > getScenarioDir ( ) ) ;
}
}
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " system macro key [%s] returning [%s] \n " , key . c_str ( ) , result . c_str ( ) ) ;
return result ;
}
2012-01-05 01:45:17 +01:00
const string World : : getPlayerName ( int factionIndex ) {
return game - > getGameSettings ( ) - > getNetworkPlayerName ( factionIndex ) ;
}
2010-11-01 17:44:05 +01:00
void World : : giveUpgradeCommand ( int unitId , const string & upgradeName ) {
2010-03-12 08:42:55 +01:00
Unit * unit = findUnitById ( unitId ) ;
2010-11-01 17:44:05 +01:00
if ( unit ! = NULL ) {
2010-03-12 08:42:55 +01:00
const UnitType * ut = unit - > getType ( ) ;
//Search for a command that can produce the unit
2010-11-01 17:44:05 +01:00
for ( int i = 0 ; i < ut - > getCommandTypeCount ( ) ; + + i ) {
2010-03-12 08:42:55 +01:00
const CommandType * ct = ut - > getCommandType ( i ) ;
2010-11-01 17:44:05 +01:00
if ( ct ! = NULL & & ct - > getClass ( ) = = ccUpgrade ) {
2010-03-12 08:42:55 +01:00
const UpgradeCommandType * uct = static_cast < const UpgradeCommandType * > ( ct ) ;
2010-11-01 17:44:05 +01:00
if ( uct ! = NULL & & uct - > getProducedUpgrade ( ) - > getName ( ) = = upgradeName ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-11-01 17:44:05 +01:00
2010-03-12 08:42:55 +01:00
unit - > giveCommand ( new Command ( uct ) ) ;
2010-11-01 17:44:05 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugUnitCommands ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugUnitCommands , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
break ;
}
}
}
}
2010-11-01 17:44:05 +01:00
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid unitId index in giveUpgradeCommand: " + intToStr ( unitId ) + " upgradeName = " + upgradeName ) ;
2010-11-01 17:44:05 +01:00
}
2010-03-12 08:42:55 +01:00
}
2010-11-01 17:44:05 +01:00
int World : : getResourceAmount ( const string & resourceName , int factionIndex ) {
if ( factionIndex < factions . size ( ) ) {
2011-09-25 06:07:59 +02:00
Faction * faction = factions [ factionIndex ] ;
2010-05-03 08:25:54 +02:00
const ResourceType * rt = techTree - > getResourceType ( resourceName ) ;
2010-03-12 08:42:55 +01:00
return faction - > getResource ( rt ) - > getAmount ( ) ;
}
2010-11-01 17:44:05 +01:00
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid faction index in giveResource: " + intToStr ( factionIndex ) + " resourceName = " + resourceName ) ;
2010-03-12 08:42:55 +01:00
}
}
2010-11-01 17:44:05 +01:00
Vec2i World : : getStartLocation ( int factionIndex ) {
if ( factionIndex < factions . size ( ) ) {
2011-09-25 06:07:59 +02:00
Faction * faction = factions [ factionIndex ] ;
2010-03-12 08:42:55 +01:00
return map . getStartLocation ( faction - > getStartLocationIndex ( ) ) ;
}
2010-11-01 17:44:05 +01:00
else {
2012-03-27 05:23:03 +02:00
printf ( " \n ================================================= \n %s \n " , game - > getGameSettings ( ) - > toString ( ) . c_str ( ) ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid faction index in getStartLocation: " + intToStr ( factionIndex ) + " : " + intToStr ( factions . size ( ) ) ) ;
2010-03-12 08:42:55 +01:00
}
}
2010-11-01 17:44:05 +01:00
Vec2i World : : getUnitPosition ( int unitId ) {
2010-03-12 08:42:55 +01:00
Unit * unit = findUnitById ( unitId ) ;
2010-11-01 17:44:05 +01:00
if ( unit = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Can not find unit to get position unitId = " + intToStr ( unitId ) ) ;
2010-03-12 08:42:55 +01:00
}
return unit - > getPos ( ) ;
}
2012-04-11 07:41:40 +02:00
void World : : setUnitPosition ( int unitId , Vec2i pos ) {
Unit * unit = findUnitById ( unitId ) ;
if ( unit = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Can not find unit to set position unitId = " + intToStr ( unitId ) ) ;
2012-04-11 07:41:40 +02:00
}
2012-10-12 23:50:00 +02:00
unit - > setTargetPos ( pos ) ;
this - > moveUnitCells ( unit ) ;
2012-04-11 07:41:40 +02:00
}
2012-09-25 09:05:52 +02:00
void World : : addCellMarker ( Vec2i pos , int factionIndex , const string & note , const string textureFile ) {
const Faction * faction = NULL ;
if ( factionIndex > = 0 ) {
faction = this - > getFaction ( factionIndex ) ;
}
Vec2i surfaceCellPos = map . toSurfCoords ( pos ) ;
SurfaceCell * sc = map . getSurfaceCell ( surfaceCellPos ) ;
if ( sc = = NULL ) {
throw megaglest_runtime_error ( " sc == NULL " ) ;
}
Vec3f vertex = sc - > getVertex ( ) ;
Vec2i targetPos ( vertex . x , vertex . z ) ;
//printf("pos [%s] scPos [%s][%p] targetPos [%s]\n",pos.getString().c_str(),surfaceCellPos.getString().c_str(),sc,targetPos.getString().c_str());
MarkedCell mc ( targetPos , faction , note , ( faction ! = NULL ? faction - > getStartLocationIndex ( ) : - 1 ) ) ;
game - > addCellMarker ( surfaceCellPos , mc ) ;
}
void World : : removeCellMarker ( Vec2i pos , int factionIndex ) {
const Faction * faction = NULL ;
if ( factionIndex > = 0 ) {
faction = this - > getFaction ( factionIndex ) ;
}
Vec2i surfaceCellPos = map . toSurfCoords ( pos ) ;
game - > removeCellMarker ( surfaceCellPos , faction ) ;
}
2012-10-13 07:06:53 +02:00
void World : : showMarker ( Vec2i pos , int factionIndex , const string & note , const string textureFile , int flashCount ) {
const Faction * faction = NULL ;
if ( factionIndex > = 0 ) {
faction = this - > getFaction ( factionIndex ) ;
}
Vec2i surfaceCellPos = map . toSurfCoords ( pos ) ;
SurfaceCell * sc = map . getSurfaceCell ( surfaceCellPos ) ;
if ( sc = = NULL ) {
throw megaglest_runtime_error ( " sc == NULL " ) ;
}
Vec3f vertex = sc - > getVertex ( ) ;
Vec2i targetPos ( vertex . x , vertex . z ) ;
//printf("pos [%s] scPos [%s][%p] targetPos [%s]\n",pos.getString().c_str(),surfaceCellPos.getString().c_str(),sc,targetPos.getString().c_str());
MarkedCell mc ( targetPos , faction , note , ( faction ! = NULL ? faction - > getStartLocationIndex ( ) : - 1 ) ) ;
if ( flashCount > 0 ) {
mc . setAliveCount ( flashCount ) ;
}
game - > showMarker ( surfaceCellPos , mc ) ;
}
2012-10-13 09:33:33 +02:00
void World : : highlightUnit ( int unitId , float radius , float thickness , Vec4f color ) {
Unit * unit = findUnitById ( unitId ) ;
if ( unit = = NULL ) {
throw megaglest_runtime_error ( " Can not find unit to set highlight unitId = " + intToStr ( unitId ) ) ;
}
game - > highlightUnit ( unitId , radius , thickness , color ) ;
}
void World : : unhighlightUnit ( int unitId ) {
Unit * unit = findUnitById ( unitId ) ;
if ( unit = = NULL ) {
throw megaglest_runtime_error ( " Can not find unit to set highlight unitId = " + intToStr ( unitId ) ) ;
}
game - > unhighlightUnit ( unitId ) ;
}
2010-11-01 17:44:05 +01:00
int World : : getUnitFactionIndex ( int unitId ) {
2010-03-12 08:42:55 +01:00
Unit * unit = findUnitById ( unitId ) ;
2010-11-01 17:44:05 +01:00
if ( unit = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Can not find Faction unit to get position unitId = " + intToStr ( unitId ) ) ;
2010-03-12 08:42:55 +01:00
}
return unit - > getFactionIndex ( ) ;
}
2012-05-21 22:15:37 +02:00
const string World : : getUnitName ( int unitId ) {
Unit * unit = findUnitById ( unitId ) ;
if ( unit = = NULL ) {
throw megaglest_runtime_error ( " Can not find Faction unit to get position unitId = " + intToStr ( unitId ) ) ;
}
2013-06-13 10:55:48 +02:00
return unit - > getFullName ( game - > showTranslatedTechTree ( ) ) ;
2012-05-21 22:15:37 +02:00
}
2010-03-12 08:42:55 +01:00
2010-11-01 17:44:05 +01:00
int World : : getUnitCount ( int factionIndex ) {
if ( factionIndex < factions . size ( ) ) {
2011-09-25 06:07:59 +02:00
Faction * faction = factions [ factionIndex ] ;
2010-03-12 08:42:55 +01:00
int count = 0 ;
2010-11-01 17:44:05 +01:00
for ( int i = 0 ; i < faction - > getUnitCount ( ) ; + + i ) {
2010-03-12 08:42:55 +01:00
const Unit * unit = faction - > getUnit ( i ) ;
2010-11-01 17:44:05 +01:00
if ( unit ! = NULL & & unit - > isAlive ( ) ) {
2010-03-12 08:42:55 +01:00
+ + count ;
}
}
return count ;
}
2010-11-01 17:44:05 +01:00
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid faction index in getUnitCount: " + intToStr ( factionIndex ) ) ;
2010-03-12 08:42:55 +01:00
}
}
2010-11-01 17:44:05 +01:00
int World : : getUnitCountOfType ( int factionIndex , const string & typeName ) {
if ( factionIndex < factions . size ( ) ) {
2011-09-25 06:07:59 +02:00
Faction * faction = factions [ factionIndex ] ;
2010-03-12 08:42:55 +01:00
int count = 0 ;
2010-11-01 17:44:05 +01:00
for ( int i = 0 ; i < faction - > getUnitCount ( ) ; + + i ) {
2010-03-12 08:42:55 +01:00
const Unit * unit = faction - > getUnit ( i ) ;
2013-06-13 10:55:48 +02:00
if ( unit ! = NULL & & unit - > isAlive ( ) & & unit - > getType ( ) - > getName ( false ) = = typeName ) {
2010-03-12 08:42:55 +01:00
+ + count ;
}
}
return count ;
}
2010-11-01 17:44:05 +01:00
else {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " Invalid faction index in getUnitCountOfType: " + intToStr ( factionIndex ) ) ;
2010-03-12 08:42:55 +01:00
}
}
// ==================== PRIVATE ====================
// ==================== private init ====================
//init basic cell state
2010-11-01 17:44:05 +01:00
void World : : initCells ( bool fogOfWar ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
2011-11-04 02:12:05 +01:00
Logger : : getInstance ( ) . add ( Lang : : getInstance ( ) . get ( " LogScreenGameLoadingStateCells " , " " , true ) , true ) ;
2010-09-04 02:32:56 +02:00
for ( int i = 0 ; i < map . getSurfaceW ( ) ; + + i ) {
for ( int j = 0 ; j < map . getSurfaceH ( ) ; + + j ) {
2010-03-12 08:42:55 +01:00
SurfaceCell * sc = map . getSurfaceCell ( i , j ) ;
2010-11-01 17:44:05 +01:00
if ( sc = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " sc == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
2011-03-02 00:36:33 +01:00
if ( sc - > getObject ( ) ! = NULL ) {
sc - > getObject ( ) - > initParticles ( ) ;
}
2010-03-12 08:42:55 +01:00
sc - > setFowTexCoord ( Vec2f (
i / ( next2Power ( map . getSurfaceW ( ) ) - 1.f ) ,
j / ( next2Power ( map . getSurfaceH ( ) ) - 1.f ) ) ) ;
2010-04-23 06:29:11 +02:00
for ( int k = 0 ; k < GameConstants : : maxPlayers ; k + + ) {
2010-12-19 09:04:25 +01:00
sc - > setExplored ( k , ( game - > getGameSettings ( ) - > getFlagTypes1 ( ) & ft1_show_map_resources ) = = ft1_show_map_resources ) ;
2010-04-23 06:29:11 +02:00
sc - > setVisible ( k , ! fogOfWar ) ;
2010-03-27 08:09:34 +01:00
}
2010-09-04 02:32:56 +02:00
for ( int k = GameConstants : : maxPlayers ; k < GameConstants : : maxPlayers + GameConstants : : specialFactions ; k + + ) {
sc - > setExplored ( k , true ) ;
sc - > setVisible ( k , true ) ;
}
2010-03-12 08:42:55 +01:00
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
//init surface textures
2010-11-01 17:44:05 +01:00
void World : : initSplattedTextures ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-11-01 17:44:05 +01:00
for ( int i = 0 ; i < map . getSurfaceW ( ) - 1 ; + + i ) {
for ( int j = 0 ; j < map . getSurfaceH ( ) - 1 ; + + j ) {
2010-03-12 08:42:55 +01:00
Vec2f coord ;
2010-09-10 10:51:32 +02:00
const Texture2D * texture = NULL ;
2010-03-12 08:42:55 +01:00
SurfaceCell * sc00 = map . getSurfaceCell ( i , j ) ;
SurfaceCell * sc10 = map . getSurfaceCell ( i + 1 , j ) ;
SurfaceCell * sc01 = map . getSurfaceCell ( i , j + 1 ) ;
SurfaceCell * sc11 = map . getSurfaceCell ( i + 1 , j + 1 ) ;
2010-11-01 17:44:05 +01:00
if ( sc00 = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " sc00 == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
if ( sc10 = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " sc10 == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
if ( sc01 = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " sc01 == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
if ( sc11 = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " sc11 == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
tileset . addSurfTex ( sc00 - > getSurfaceType ( ) ,
sc10 - > getSurfaceType ( ) ,
sc01 - > getSurfaceType ( ) ,
sc11 - > getSurfaceType ( ) ,
2013-02-20 18:42:47 +01:00
coord , texture , j , i ) ;
2010-03-12 08:42:55 +01:00
sc00 - > setSurfTexCoord ( coord ) ;
sc00 - > setSurfaceTexture ( texture ) ;
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
//creates each faction looking at each faction name contained in GameSettings
2010-11-01 17:44:05 +01:00
void World : : initFactionTypes ( GameSettings * gs ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-11-04 02:12:05 +01:00
Logger : : getInstance ( ) . add ( Lang : : getInstance ( ) . get ( " LogScreenGameLoadingFactionTypes " , " " , true ) , true ) ;
2010-03-12 08:42:55 +01:00
2010-11-01 17:44:05 +01:00
if ( gs = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " gs == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
if ( gs - > getFactionCount ( ) > map . getMaxPlayers ( ) ) {
2012-10-15 02:48:05 +02:00
throw megaglest_runtime_error ( " This map only supports " + intToStr ( map . getMaxPlayers ( ) ) + " players, factionCount is " + intToStr ( gs - > getFactionCount ( ) ) ) ;
2010-03-12 08:42:55 +01:00
}
//create stats
2013-02-15 19:25:10 +01:00
//printf("World gs->getThisFactionIndex() = %d\n",gs->getThisFactionIndex());
2012-09-25 02:23:25 +02:00
stats . init ( gs - > getFactionCount ( ) , gs - > getThisFactionIndex ( ) , gs - > getDescription ( ) , gs - > getTech ( ) ) ;
2010-03-12 08:42:55 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-12 07:05:08 +02:00
2010-03-12 08:42:55 +01:00
//create factions
this - > thisFactionIndex = gs - > getThisFactionIndex ( ) ;
2011-09-25 06:07:59 +02:00
//factions.resize(gs->getFactionCount());
for ( int i = 0 ; i < gs - > getFactionCount ( ) ; + + i ) {
Faction * newFaction = new Faction ( ) ;
factions . push_back ( newFaction ) ;
}
2010-09-12 07:05:08 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] factions.size() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , factions . size ( ) ) ;
2010-09-12 07:05:08 +02:00
for ( int i = 0 ; i < factions . size ( ) ; + + i ) {
2010-10-28 02:51:25 +02:00
FactionType * ft = techTree - > getTypeByName ( gs - > getFactionTypeName ( i ) ) ;
2010-11-01 17:44:05 +01:00
if ( ft = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " ft == NULL " ) ;
2010-11-01 17:44:05 +01:00
}
2011-09-25 06:07:59 +02:00
factions [ i ] - > init ( ft , gs - > getFactionControl ( i ) , techTree , game , i , gs - > getTeam ( i ) ,
2012-03-13 00:08:22 +01:00
gs - > getStartLocationIndex ( i ) , i = = thisFactionIndex ,
gs - > getDefaultResources ( ) , loadWorldNode ) ;
2010-03-12 08:42:55 +01:00
stats . setTeam ( i , gs - > getTeam ( i ) ) ;
stats . setFactionTypeName ( i , formatString ( gs - > getFactionTypeName ( i ) ) ) ;
2010-09-03 09:12:40 +02:00
stats . setPersonalityType ( i , getFaction ( i ) - > getType ( ) - > getPersonalityType ( ) ) ;
2010-03-12 08:42:55 +01:00
stats . setControl ( i , gs - > getFactionControl ( i ) ) ;
2010-11-25 23:45:08 +01:00
stats . setResourceMultiplier ( i , ( gs - > getResourceMultiplierIndex ( i ) + 5 ) * 0.1f ) ;
2010-06-24 03:23:18 +02:00
stats . setPlayerName ( i , gs - > getNetworkPlayerName ( i ) ) ;
2011-09-27 07:29:57 +02:00
if ( getFaction ( i ) - > getTexture ( ) ) {
stats . setPlayerColor ( i , getFaction ( i ) - > getTexture ( ) - > getPixmapConst ( ) - > getPixel3f ( 0 , 0 ) ) ;
}
2010-03-12 08:42:55 +01:00
}
2010-12-09 21:41:11 +01:00
2013-01-10 22:16:28 +01:00
if ( Config : : getInstance ( ) . getBool ( " EnableNewThreadManager " , " false " ) = = true ) {
std : : vector < SlaveThreadControllerInterface * > slaveThreadList ;
for ( unsigned int i = 0 ; i < factions . size ( ) ; + + i ) {
Faction * faction = factions [ i ] ;
slaveThreadList . push_back ( faction - > getWorkerThread ( ) ) ;
}
masterController . setSlaves ( slaveThreadList ) ;
}
2012-03-13 16:21:25 +01:00
if ( loadWorldNode ! = NULL ) {
stats . loadGame ( loadWorldNode ) ;
random . setLastNumber ( loadWorldNode - > getAttribute ( " random " ) - > getIntValue ( ) ) ;
thisFactionIndex = loadWorldNode - > getAttribute ( " thisFactionIndex " ) - > getIntValue ( ) ;
// int thisTeamIndex;
thisTeamIndex = loadWorldNode - > getAttribute ( " thisTeamIndex " ) - > getIntValue ( ) ;
// int frameCount;
frameCount = loadWorldNode - > getAttribute ( " frameCount " ) - > getIntValue ( ) ;
2013-02-15 19:25:10 +01:00
//printf("**LOAD World thisFactionIndex = %d\n",thisFactionIndex);
2012-03-13 16:21:25 +01:00
MutexSafeWrapper safeMutex ( & mutexFactionNextUnitId , string ( __FILE__ ) + " _ " + intToStr ( __LINE__ ) ) ;
// std::map<int,int> mapFactionNextUnitId;
// for(std::map<int,int>::iterator iterMap = mapFactionNextUnitId.begin();
// iterMap != mapFactionNextUnitId.end(); ++iterMap) {
// XmlNode *factionNextUnitIdNode = worldNode->addChild("FactionNextUnitId");
//
// factionNextUnitIdNode->addAttribute("key",intToStr(iterMap->first), mapTagReplacements);
// factionNextUnitIdNode->addAttribute("value",intToStr(iterMap->second), mapTagReplacements);
// }
//!!!
vector < XmlNode * > factionNextUnitIdNodeList = loadWorldNode - > getChildList ( " FactionNextUnitId " ) ;
for ( unsigned int i = 0 ; i < factionNextUnitIdNodeList . size ( ) ; + + i ) {
XmlNode * factionNextUnitIdNode = factionNextUnitIdNodeList [ i ] ;
int key = factionNextUnitIdNode - > getAttribute ( " key " ) - > getIntValue ( ) ;
int value = factionNextUnitIdNode - > getAttribute ( " value " ) - > getIntValue ( ) ;
mapFactionNextUnitId [ key ] = value ;
}
safeMutex . ReleaseLock ( ) ;
// //config
// bool fogOfWarOverride;
2012-04-16 22:15:57 +02:00
fogOfWarOverride = loadWorldNode - > getAttribute ( " fogOfWarOverride " ) - > getIntValue ( ) ! = 0 ;
2012-03-13 16:21:25 +01:00
// bool fogOfWar;
2012-04-16 22:15:57 +02:00
fogOfWar = loadWorldNode - > getAttribute ( " fogOfWar " ) - > getIntValue ( ) ! = 0 ;
2012-03-13 16:21:25 +01:00
// int fogOfWarSmoothingFrameSkip;
fogOfWarSmoothingFrameSkip = loadWorldNode - > getAttribute ( " fogOfWarSmoothingFrameSkip " ) - > getIntValue ( ) ;
// bool fogOfWarSmoothing;
2012-04-16 22:15:57 +02:00
fogOfWarSmoothing = loadWorldNode - > getAttribute ( " fogOfWarSmoothing " ) - > getIntValue ( ) ! = 0 ;
2012-03-13 16:21:25 +01:00
// Game *game;
// Chrono chronoPerfTimer;
// bool perfTimerEnabled;
//
// bool unitParticlesEnabled;
2012-04-16 22:15:57 +02:00
unitParticlesEnabled = loadWorldNode - > getAttribute ( " unitParticlesEnabled " ) - > getIntValue ( ) ! = 0 ;
2012-03-13 16:21:25 +01:00
// bool staggeredFactionUpdates;
2012-04-16 22:15:57 +02:00
staggeredFactionUpdates = loadWorldNode - > getAttribute ( " staggeredFactionUpdates " ) - > getIntValue ( ) ! = 0 ;
2012-03-13 16:21:25 +01:00
// std::map<string,StaticSound *> staticSoundList;
// std::map<string,StrSound *> streamSoundList;
//
// uint32 nextCommandGroupId;
nextCommandGroupId = loadWorldNode - > getAttribute ( " nextCommandGroupId " ) - > getIntValue ( ) ;
// string queuedScenarioName;
queuedScenarioName = loadWorldNode - > getAttribute ( " queuedScenarioName " ) - > getValue ( ) ;
// bool queuedScenarioKeepFactions;
2012-04-16 22:15:57 +02:00
queuedScenarioKeepFactions = loadWorldNode - > getAttribute ( " queuedScenarioKeepFactions " ) - > getIntValue ( ) ! = 0 ;
2012-10-17 22:15:50 +02:00
if ( loadWorldNode - > hasAttribute ( " disableAttackEffects " ) = = true ) {
disableAttackEffects = loadWorldNode - > getAttribute ( " disableAttackEffects " ) - > getIntValue ( ) ! = 0 ;
}
2012-03-13 16:21:25 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-12 07:05:08 +02:00
2011-09-01 20:08:56 +02:00
if ( factions . empty ( ) = = false ) {
2010-09-12 07:05:08 +02:00
thisTeamIndex = getFaction ( thisFactionIndex ) - > getTeam ( ) ;
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
2010-11-01 17:44:05 +01:00
void World : : initMinimap ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-11-01 17:44:05 +01:00
minimap . init ( map . getW ( ) , map . getH ( ) , this , game - > getGameSettings ( ) - > getFogOfWar ( ) ) ;
2011-11-04 02:12:05 +01:00
Logger : : getInstance ( ) . add ( Lang : : getInstance ( ) . get ( " LogScreenGameLoadingMinimapSurface " , " " , true ) , true ) ;
2010-11-01 17:44:05 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
2011-10-07 00:19:58 +02:00
void World : : initUnitsForScenario ( ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-11-04 02:12:05 +01:00
Logger : : getInstance ( ) . add ( Lang : : getInstance ( ) . get ( " LogScreenGameLoadingGenerateGameElements " , " " , true ) , true ) ;
2011-10-07 00:19:58 +02:00
//put starting units
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
Faction * f = factions [ i ] ;
const FactionType * ft = f - > getType ( ) ;
for ( int j = 0 ; j < f - > getUnitCount ( ) ; + + j ) {
Unit * unit = f - > getUnit ( j ) ;
int startLocationIndex = f - > getStartLocationIndex ( ) ;
if ( placeUnit ( map . getStartLocation ( startLocationIndex ) , generationArea , unit , true ) ) {
2011-10-07 05:25:17 +02:00
map . putUnitCells ( unit , unit - > getPos ( ) ) ;
unit - > setCurrSkill ( scStop ) ;
2011-10-07 00:19:58 +02:00
//unit->create(true);
//unit->born();
}
else {
2013-06-13 10:55:48 +02:00
string unitName = unit - > getType ( ) - > getName ( false ) ;
2011-10-07 00:19:58 +02:00
delete unit ;
unit = NULL ;
2013-06-13 03:37:15 +02:00
throw megaglest_runtime_error ( " Unit: " + unitName + " can't be placed, this error is caused because there \n is not enough room to put all units near their start location. \n make a better/larger map. Faction: # " + intToStr ( i ) + " name: " + ft - > getName ( false ) ) ;
2011-10-07 00:19:58 +02:00
}
if ( unit - > getType ( ) - > hasSkillClass ( scBeBuilt ) ) {
map . flatternTerrain ( unit ) ;
}
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] unit created for unit [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > toString ( ) . c_str ( ) ) ;
}
// Ensure Starting Resource Amount are adjusted to max store levels
//f->limitResourcesToStore();
}
map . computeNormals ( ) ;
map . computeInterpolatedHeights ( ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2012-03-13 00:08:22 +01:00
void World : : placeUnitAtLocation ( const Vec2i & location , int radius , Unit * unit , bool spaciated ) {
if ( placeUnit ( location , generationArea , unit , spaciated ) ) {
unit - > create ( true ) ;
2012-03-27 05:23:03 +02:00
unit - > born ( NULL ) ;
2012-03-13 00:08:22 +01:00
}
else {
2013-06-13 10:55:48 +02:00
string unitName = unit - > getType ( ) - > getName ( false ) ;
2013-06-13 03:37:15 +02:00
string unitFactionName = unit - > getFaction ( ) - > getType ( ) - > getName ( false ) ;
2012-07-18 01:04:30 +02:00
int unitFactionIndex = unit - > getFactionIndex ( ) ;
2012-03-13 00:08:22 +01:00
delete unit ;
unit = NULL ;
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " Unit: [%s] can't be placed, this error is caused because there \n is not enough room to put all units near their start location. \n make a better/larger map. Faction: #%d name: [%s] " ,
2012-07-18 01:04:30 +02:00
unitName . c_str ( ) , unitFactionIndex , unitFactionName . c_str ( ) ) ;
2012-07-21 01:51:10 +02:00
throw megaglest_runtime_error ( szBuf , false ) ;
2012-03-13 00:08:22 +01:00
}
if ( unit - > getType ( ) - > hasSkillClass ( scBeBuilt ) ) {
map . flatternTerrain ( unit ) ;
}
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] unit created for unit [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , unit - > toString ( ) . c_str ( ) ) ;
}
2010-03-12 08:42:55 +01:00
//place units randomly aroud start location
2010-11-01 17:44:05 +01:00
void World : : initUnits ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-11-04 02:12:05 +01:00
Logger : : getInstance ( ) . add ( Lang : : getInstance ( ) . get ( " LogScreenGameLoadingGenerateGameElements " , " " , true ) , true ) ;
2010-03-12 08:42:55 +01:00
2012-07-18 01:04:30 +02:00
bool gotError = false ;
2012-07-21 01:51:10 +02:00
bool skipStackTrace = false ;
2012-07-21 01:07:44 +02:00
string sErrBuf = " " ;
2012-07-18 01:04:30 +02:00
try {
//put starting units
if ( loadWorldNode = = NULL ) {
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
Faction * f = factions [ i ] ;
const FactionType * ft = f - > getType ( ) ;
for ( int j = 0 ; j < ft - > getStartingUnitCount ( ) ; + + j ) {
const UnitType * ut = ft - > getStartingUnit ( j ) ;
int initNumber = ft - > getStartingUnitAmount ( j ) ;
for ( int l = 0 ; l < initNumber ; l + + ) {
UnitPathInterface * newpath = NULL ;
switch ( game - > getGameSettings ( ) - > getPathFinderType ( ) ) {
case pfBasic :
newpath = new UnitPathBasic ( ) ;
break ;
default :
throw megaglest_runtime_error ( " detected unsupported pathfinder type! " ) ;
}
2012-03-13 00:08:22 +01:00
2012-07-18 01:04:30 +02:00
Unit * unit = new Unit ( getNextUnitId ( f ) , newpath , Vec2i ( 0 ) , ut , f , & map , CardinalDir : : NORTH ) ;
int startLocationIndex = f - > getStartLocationIndex ( ) ;
placeUnitAtLocation ( map . getStartLocation ( startLocationIndex ) , generationArea , unit , true ) ;
}
2010-03-12 08:42:55 +01:00
}
2011-04-25 08:39:40 +02:00
2012-07-18 01:04:30 +02:00
// Ensure Starting Resource Amount are adjusted to max store levels
f - > limitResourcesToStore ( ) ;
}
2012-03-13 00:08:22 +01:00
}
2013-03-06 16:02:17 +01:00
else {
2013-05-27 10:26:01 +02:00
//printf("Load game setting unit pos\n");
2013-03-06 16:02:17 +01:00
refreshAllUnitExplorations ( ) ;
}
2010-03-12 08:42:55 +01:00
}
2012-07-21 01:51:10 +02:00
catch ( const megaglest_runtime_error & ex ) {
gotError = true ;
if ( ex . wantStackTrace ( ) = = true ) {
char szErrBuf [ 8096 ] = " " ;
2012-10-19 03:31:20 +02:00
snprintf ( szErrBuf , 8096 , " In [%s::%s %d] " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2012-07-21 01:51:10 +02:00
sErrBuf = string ( szErrBuf ) + string ( " \n error [ " ) + string ( ex . what ( ) ) + string ( " ] \n " ) ;
}
else {
skipStackTrace = true ;
sErrBuf = ex . what ( ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugError , sErrBuf . c_str ( ) ) ;
}
2012-07-18 01:04:30 +02:00
catch ( const std : : exception & ex ) {
gotError = true ;
2012-07-21 01:07:44 +02:00
char szErrBuf [ 8096 ] = " " ;
2012-10-19 03:31:20 +02:00
snprintf ( szErrBuf , 8096 , " In [%s::%s %d] " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2012-07-21 01:07:44 +02:00
sErrBuf = string ( szErrBuf ) + string ( " \n error [ " ) + string ( ex . what ( ) ) + string ( " ] \n " ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugError , sErrBuf . c_str ( ) ) ;
2012-07-18 01:04:30 +02:00
}
2010-03-12 08:42:55 +01:00
map . computeNormals ( ) ;
map . computeInterpolatedHeights ( ) ;
2012-07-18 01:04:30 +02:00
if ( gotError = = true ) {
2012-07-21 01:51:10 +02:00
throw megaglest_runtime_error ( sErrBuf , ! skipStackTrace ) ;
2012-07-18 01:04:30 +02:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
2013-03-06 16:02:17 +01:00
void World : : refreshAllUnitExplorations ( ) {
for ( unsigned int i = 0 ; i < getFactionCount ( ) ; + + i ) {
Faction * faction = factions [ i ] ;
for ( unsigned int j = 0 ; j < faction - > getUnitCount ( ) ; + + j ) {
Unit * unit = faction - > getUnit ( j ) ;
unit - > refreshPos ( ) ;
}
}
}
2010-11-01 17:44:05 +01:00
void World : : initMap ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-02-06 02:36:55 +01:00
map . init ( & tileset ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-12 08:42:55 +01:00
}
// ==================== exploration ====================
2010-11-01 17:44:05 +01:00
void World : : exploreCells ( const Vec2i & newPos , int sightRange , int teamIndex ) {
2011-09-01 03:11:23 +02:00
//bool cacheLookupPosResult = false;
//bool cacheLookupSightResult = false;
2010-08-25 01:36:23 +02:00
2010-12-24 09:43:09 +01:00
// cache lookup of previously calculated cells + sight range
2011-12-03 02:19:54 +01:00
//if(MaxExploredCellsLookupItemCache > 0 && game->isMasterserverMode() == false) {
if ( MaxExploredCellsLookupItemCache > 0 ) {
2010-07-17 08:33:40 +02:00
if ( difftime ( time ( NULL ) , ExploredCellsLookupItem : : lastDebug ) > = 10 ) {
ExploredCellsLookupItem : : lastDebug = time ( NULL ) ;
//printf("In [%s::%s Line: %d] ExploredCellsLookupItemCache.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,ExploredCellsLookupItemCache.size());
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] ExploredCellsLookupItemCache.size() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , ExploredCellsLookupItemCache . size ( ) ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] ExploredCellsLookupItemCache.size() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , ExploredCellsLookupItemCache . size ( ) ) ;
2010-07-17 08:33:40 +02:00
}
2010-07-17 00:15:09 +02:00
2010-08-25 01:15:33 +02:00
// Ok we limit the cache size due to possible RAM constraints when
2010-07-17 08:33:40 +02:00
// the threshold is met
bool MaxExploredCellsLookupItemCacheTriggered = false ;
if ( ExploredCellsLookupItemCache . size ( ) > = MaxExploredCellsLookupItemCache ) {
MaxExploredCellsLookupItemCacheTriggered = true ;
// Snag the oldest entry in the list
std : : map < int , ExploredCellsLookupKey > : : reverse_iterator purgeItem = ExploredCellsLookupItemCacheTimer . rbegin ( ) ;
2010-07-17 00:15:09 +02:00
2010-08-25 01:15:33 +02:00
ExploredCellsLookupItemCache [ purgeItem - > second . pos ] . erase ( purgeItem - > second . sightRange ) ;
2010-07-17 00:15:09 +02:00
2010-07-17 08:33:40 +02:00
if ( ExploredCellsLookupItemCache [ purgeItem - > second . pos ] . size ( ) = = 0 ) {
ExploredCellsLookupItemCache . erase ( purgeItem - > second . pos ) ;
}
ExploredCellsLookupItemCacheTimer . erase ( purgeItem - > first ) ;
2010-07-17 00:15:09 +02:00
}
2010-08-25 01:15:33 +02:00
// Check the cache for the pos, sightrange and use from
2010-07-17 08:33:40 +02:00
// cache if already found
2010-08-25 01:15:33 +02:00
std : : map < Vec2i , std : : map < int , ExploredCellsLookupItem > > : : iterator iterFind = ExploredCellsLookupItemCache . find ( newPos ) ;
2010-07-17 08:33:40 +02:00
if ( iterFind ! = ExploredCellsLookupItemCache . end ( ) ) {
2011-09-01 03:11:23 +02:00
//cacheLookupPosResult = true;
2010-08-25 01:36:23 +02:00
2010-08-25 01:15:33 +02:00
std : : map < int , ExploredCellsLookupItem > : : iterator iterFind2 = iterFind - > second . find ( sightRange ) ;
2010-07-17 08:33:40 +02:00
if ( iterFind2 ! = iterFind - > second . end ( ) ) {
2011-09-01 03:11:23 +02:00
//cacheLookupSightResult = true;
2010-07-17 00:15:09 +02:00
2010-08-25 01:15:33 +02:00
std : : vector < SurfaceCell * > & cellList = iterFind2 - > second . exploredCellList ;
for ( int idx2 = 0 ; idx2 < cellList . size ( ) ; + + idx2 ) {
SurfaceCell * sc = cellList [ idx2 ] ;
sc - > setExplored ( teamIndex , true ) ;
}
cellList = iterFind2 - > second . visibleCellList ;
for ( int idx2 = 0 ; idx2 < cellList . size ( ) ; + + idx2 ) {
SurfaceCell * sc = cellList [ idx2 ] ;
sc - > setVisible ( teamIndex , true ) ;
}
2010-07-17 00:15:09 +02:00
2010-08-25 01:15:33 +02:00
// Only start worrying about updating the cache timer if we
// have hit the threshold
if ( MaxExploredCellsLookupItemCacheTriggered = = true ) {
// Remove the old timer entry since the search key id is stale
ExploredCellsLookupItemCacheTimer . erase ( iterFind2 - > second . ExploredCellsLookupItemCacheTimerCountIndex ) ;
iterFind2 - > second . ExploredCellsLookupItemCacheTimerCountIndex = ExploredCellsLookupItemCacheTimerCount + + ;
2010-07-17 00:15:09 +02:00
2010-08-25 01:15:33 +02:00
ExploredCellsLookupKey lookupKey ;
lookupKey . pos = newPos ;
lookupKey . sightRange = sightRange ;
lookupKey . teamIndex = teamIndex ;
2010-07-17 00:15:09 +02:00
2010-08-25 01:15:33 +02:00
// Add a new search key since we just used this item
ExploredCellsLookupItemCacheTimer [ iterFind2 - > second . ExploredCellsLookupItemCacheTimerCountIndex ] = lookupKey ;
2010-07-17 08:33:40 +02:00
}
2010-08-25 01:15:33 +02:00
return ;
2010-07-14 08:59:55 +02:00
}
}
}
2010-03-12 08:42:55 +01:00
Vec2i newSurfPos = Map : : toSurfCoords ( newPos ) ;
int surfSightRange = sightRange / Map : : cellScale + 1 ;
2010-07-17 00:15:09 +02:00
// Explore, this code is quite expensive when we have lots of units
2010-07-17 03:00:31 +02:00
ExploredCellsLookupItem item ;
2010-09-09 07:42:19 +02:00
int loopCount = 0 ;
2010-08-25 01:15:33 +02:00
for ( int i = - surfSightRange - indirectSightRange - 1 ; i < = surfSightRange + indirectSightRange + 1 ; + + i ) {
for ( int j = - surfSightRange - indirectSightRange - 1 ; j < = surfSightRange + indirectSightRange + 1 ; + + j ) {
2010-09-09 07:42:19 +02:00
loopCount + + ;
Vec2i currRelPos = Vec2i ( i , j ) ;
2010-03-12 08:42:55 +01:00
Vec2i currPos = newSurfPos + currRelPos ;
if ( map . isInsideSurface ( currPos ) ) {
SurfaceCell * sc = map . getSurfaceCell ( currPos ) ;
2011-04-18 08:42:25 +02:00
if ( sc = = NULL ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " sc == NULL " ) ;
2011-04-18 08:42:25 +02:00
}
2010-03-12 08:42:55 +01:00
//explore
2013-06-12 08:14:55 +02:00
double posLength = currRelPos . length ( ) ;
2010-09-09 07:42:19 +02:00
//if(Vec2i(0).dist(currRelPos) < surfSightRange + indirectSightRange + 1) {
2012-07-03 21:31:52 +02:00
if ( posLength < surfSightRange + indirectSightRange + 1 ) {
2010-03-12 08:42:55 +01:00
sc - > setExplored ( teamIndex , true ) ;
2010-07-14 08:59:55 +02:00
item . exploredCellList . push_back ( sc ) ;
2010-03-12 08:42:55 +01:00
}
//visible
2010-09-09 07:42:19 +02:00
//if(Vec2i(0).dist(currRelPos) < surfSightRange) {
2012-07-03 21:31:52 +02:00
if ( posLength < surfSightRange ) {
2010-03-12 08:42:55 +01:00
sc - > setVisible ( teamIndex , true ) ;
2010-07-14 08:59:55 +02:00
item . visibleCellList . push_back ( sc ) ;
2010-03-12 08:42:55 +01:00
}
}
}
}
2010-07-14 08:59:55 +02:00
2010-07-17 00:15:09 +02:00
// Ok update our caches with the latest info for this position, sight and team
2010-07-17 08:33:40 +02:00
if ( MaxExploredCellsLookupItemCache > 0 ) {
2011-12-02 17:07:59 +01:00
if ( item . exploredCellList . empty ( ) = = false | | item . visibleCellList . empty ( ) = = false ) {
2010-07-17 08:33:40 +02:00
//ExploredCellsLookupItemCache.push_back(item);
item . ExploredCellsLookupItemCacheTimerCountIndex = ExploredCellsLookupItemCacheTimerCount + + ;
2010-08-25 01:15:33 +02:00
ExploredCellsLookupItemCache [ newPos ] [ sightRange ] = item ;
2010-07-17 08:33:40 +02:00
ExploredCellsLookupKey lookupKey ;
lookupKey . pos = newPos ;
lookupKey . sightRange = sightRange ;
lookupKey . teamIndex = teamIndex ;
ExploredCellsLookupItemCacheTimer [ item . ExploredCellsLookupItemCacheTimerCountIndex ] = lookupKey ;
}
2010-07-14 08:59:55 +02:00
}
2010-03-12 08:42:55 +01:00
}
2013-01-11 19:18:58 +01:00
bool World : : showWorldForPlayer ( int factionIndex , bool excludeFogOfWarCheck ) const {
2010-12-24 09:43:09 +01:00
bool ret = false ;
2013-01-11 19:18:58 +01:00
if ( excludeFogOfWarCheck = = false & & fogOfWarSkillTypeValue = = 0 & & fogOfWarOverride = = true ) {
ret = true ;
}
else if ( factionIndex = = thisFactionIndex & & game ! = NULL ) {
2010-12-24 09:43:09 +01:00
// Player is an Observer
if ( thisTeamIndex = = GameConstants : : maxPlayers - 1 + fpt_Observer ) {
ret = true ;
}
// Game over and not a network game
else if ( game - > getGameOver ( ) = = true & &
game - > getGameSettings ( ) - > isNetworkGame ( ) = = false ) {
ret = true ;
}
// Game is over but playing a Network game so check if we can
// turn off fog of war?
else if ( game - > getGameOver ( ) = = true & &
game - > getGameSettings ( ) - > isNetworkGame ( ) = = true & &
game - > getGameSettings ( ) - > getEnableObserverModeAtEndGame ( ) = = true ) {
ret = true ;
2010-12-25 22:30:54 +01:00
// If the faction is NOT on the winning team, don't let them see the map
// until all mobile units are dead
if ( getStats ( ) - > getVictory ( factionIndex ) = = false ) {
// If the player has at least 1 Unit alive that is mobile (can move)
// then we cannot turn off fog of war
for ( int i = 0 ; i < getFaction ( factionIndex ) - > getUnitCount ( ) ; + + i ) {
Unit * unit = getFaction ( factionIndex ) - > getUnit ( i ) ;
if ( unit ! = NULL & & unit - > isAlive ( ) & & unit - > getType ( ) - > isMobile ( ) = = true ) {
ret = false ;
break ;
}
2010-12-24 09:43:09 +01:00
}
}
}
}
return ret ;
}
2010-03-12 08:42:55 +01:00
//computes the fog of war texture, contained in the minimap
2010-08-23 16:48:33 +02:00
void World : : computeFow ( int factionIdxToTick ) {
2013-02-09 07:57:05 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s] Line: %d in frame: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , getFrameCount ( ) ) ;
2013-01-03 18:30:59 +01:00
bool showPerfStats = Config : : getInstance ( ) . getBool ( " ShowPerfStats " , " false " ) ;
Chrono chronoPerf ;
char perfBuf [ 8096 ] = " " ;
std : : vector < string > perfList ;
if ( showPerfStats ) chronoPerf . start ( ) ;
2013-02-09 07:57:05 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s] Line: %d in frame: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , getFrameCount ( ) ) ;
2010-08-25 01:15:33 +02:00
minimap . resetFowTex ( ) ;
2010-03-12 08:42:55 +01:00
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2013-02-09 07:57:05 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s] Line: %d in frame: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , getFrameCount ( ) ) ;
2010-03-12 08:42:55 +01:00
//reset cells
2010-08-23 16:48:33 +02:00
if ( factionIdxToTick = = - 1 | | factionIdxToTick = = this - > thisFactionIndex ) {
2013-02-09 07:57:05 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s] Line: %d in frame: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , getFrameCount ( ) ) ;
2010-09-04 02:32:56 +02:00
for ( int i = 0 ; i < map . getSurfaceW ( ) ; + + i ) {
for ( int j = 0 ; j < map . getSurfaceH ( ) ; + + j ) {
for ( int k = 0 ; k < GameConstants : : maxPlayers + GameConstants : : specialFactions ; + + k ) {
if ( fogOfWar | | k ! = thisTeamIndex ) {
2011-01-26 17:13:08 +01:00
map . getSurfaceCell ( i , j ) - > setVisible ( k , false ) ;
2010-12-24 09:43:09 +01:00
if ( showWorldForPlayer ( k ) = = true ) {
2010-09-04 02:32:56 +02:00
const Vec2i pos ( i , j ) ;
2010-09-04 03:32:50 +02:00
Vec2i surfPos = pos ;
2010-09-04 02:32:56 +02:00
//compute max alpha
float maxAlpha = 0.0f ;
2010-12-24 09:43:09 +01:00
if ( surfPos . x > 1 & & surfPos . y > 1 & &
surfPos . x < map . getSurfaceW ( ) - 2 & &
surfPos . y < map . getSurfaceH ( ) - 2 ) {
2010-09-04 02:32:56 +02:00
maxAlpha = 1.f ;
}
2010-12-24 09:43:09 +01:00
else if ( surfPos . x > 0 & & surfPos . y > 0 & &
surfPos . x < map . getSurfaceW ( ) - 1 & &
surfPos . y < map . getSurfaceH ( ) - 1 ) {
2010-09-04 02:32:56 +02:00
maxAlpha = 0.3f ;
}
//compute alpha
float alpha = maxAlpha ;
minimap . incFowTextureAlphaSurface ( surfPos , alpha ) ;
}
2011-01-26 17:13:08 +01:00
//else {
// map.getSurfaceCell(i, j)->setVisible(k, false);
//}
2010-08-23 16:48:33 +02:00
}
2010-03-12 08:42:55 +01:00
}
}
}
2013-02-09 07:57:05 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s] Line: %d in frame: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , getFrameCount ( ) ) ;
2010-03-12 08:42:55 +01:00
}
2010-09-16 01:32:29 +02:00
else {
2013-02-09 07:57:05 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s] Line: %d in frame: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , getFrameCount ( ) ) ;
2010-09-16 01:32:29 +02:00
// Deal with observers
for ( int i = 0 ; i < map . getSurfaceW ( ) ; + + i ) {
for ( int j = 0 ; j < map . getSurfaceH ( ) ; + + j ) {
for ( int k = 0 ; k < GameConstants : : maxPlayers + GameConstants : : specialFactions ; + + k ) {
if ( fogOfWar | | k ! = thisTeamIndex ) {
if ( k = = thisTeamIndex & & thisTeamIndex = = GameConstants : : maxPlayers - 1 + fpt_Observer ) {
2010-12-24 09:43:09 +01:00
//map.getSurfaceCell(i, j)->setVisible(k, true);
//map.getSurfaceCell(i, j)->setExplored(k, true);
2010-09-16 01:32:29 +02:00
const Vec2i pos ( i , j ) ;
Vec2i surfPos = pos ;
//compute max alpha
float maxAlpha = 0.0f ;
2010-12-24 09:43:09 +01:00
if ( surfPos . x > 1 & & surfPos . y > 1 & &
surfPos . x < map . getSurfaceW ( ) - 2 & &
surfPos . y < map . getSurfaceH ( ) - 2 ) {
2010-09-16 01:32:29 +02:00
maxAlpha = 1.f ;
}
2010-12-24 09:43:09 +01:00
else if ( surfPos . x > 0 & & surfPos . y > 0 & &
surfPos . x < map . getSurfaceW ( ) - 1 & &
surfPos . y < map . getSurfaceH ( ) - 1 ) {
2010-09-16 01:32:29 +02:00
maxAlpha = 0.3f ;
}
//compute alpha
float alpha = maxAlpha ;
minimap . incFowTextureAlphaSurface ( surfPos , alpha ) ;
}
}
}
}
}
2013-02-09 07:57:05 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s] Line: %d in frame: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , getFrameCount ( ) ) ;
2010-09-16 01:32:29 +02:00
}
2010-03-12 08:42:55 +01:00
2013-02-09 07:57:05 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s] Line: %d in frame: %d \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , getFrameCount ( ) ) ;
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2010-03-12 08:42:55 +01:00
//compute cells
2010-08-23 16:48:33 +02:00
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
if ( factionIdxToTick = = - 1 | | factionIdxToTick = = this - > thisFactionIndex ) {
for ( int j = 0 ; j < getFaction ( i ) - > getUnitCount ( ) ; + + j ) {
Unit * unit = getFaction ( i ) - > getUnit ( j ) ;
2010-03-12 08:42:55 +01:00
2010-08-23 16:48:33 +02:00
//exploration
2010-09-09 03:44:25 +02:00
unit - > exploreCells ( ) ;
2010-03-12 08:42:55 +01:00
}
}
}
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2010-03-12 08:42:55 +01:00
//fire
2010-08-23 16:48:33 +02:00
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
if ( factionIdxToTick = = - 1 | | factionIdxToTick = = this - > thisFactionIndex ) {
2013-02-09 07:57:05 +01:00
bool cellVisibleForFaction = showWorldForPlayer ( thisFactionIndex ) ;
2010-08-23 16:48:33 +02:00
for ( int j = 0 ; j < getFaction ( i ) - > getUnitCount ( ) ; + + j ) {
Unit * unit = getFaction ( i ) - > getUnit ( j ) ;
//fire
ParticleSystem * fire = unit - > getFire ( ) ;
2010-12-24 09:43:09 +01:00
if ( fire ! = NULL ) {
2013-02-09 07:57:05 +01:00
bool cellVisible = cellVisibleForFaction ;
2010-12-24 09:43:09 +01:00
if ( cellVisible = = false ) {
2013-02-09 07:57:05 +01:00
Vec2i sCoords = Map : : toSurfCoords ( unit - > getPos ( ) ) ;
SurfaceCell * sc = map . getSurfaceCell ( sCoords ) ;
if ( sc ! = NULL ) {
cellVisible = sc - > isVisible ( thisTeamIndex ) ;
}
2010-12-24 09:43:09 +01:00
}
fire - > setActive ( cellVisible ) ;
2010-08-23 16:48:33 +02:00
}
2010-03-12 08:42:55 +01:00
}
}
}
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
2010-03-12 08:42:55 +01:00
//compute texture
2011-12-03 02:19:54 +01:00
if ( fogOfWar = = true ) {
2010-08-23 17:10:37 +02:00
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
2010-08-26 01:55:59 +02:00
Faction * faction = getFaction ( i ) ;
2010-09-04 02:32:56 +02:00
if ( faction - > getTeam ( ) = = thisTeamIndex ) {
2010-08-26 01:55:59 +02:00
for ( int j = 0 ; j < faction - > getUnitCount ( ) ; + + j ) {
const Unit * unit = faction - > getUnit ( j ) ;
if ( unit - > isOperative ( ) ) {
2013-01-25 03:55:28 +01:00
//int sightRange= unit->getType()->getSight();
2010-08-26 01:55:59 +02:00
2013-01-04 19:00:51 +01:00
if ( enableFowAlphaCellsLookupItemCache = = true ) {
const FowAlphaCellsLookupItem & cellList = unit - > getCachedFow ( ) ;
for ( int k = 0 ; k < cellList . surfPosList . size ( ) ; + + k ) {
const Vec2i & surfPos = cellList . surfPosList [ k ] ;
const float & alpha = cellList . alphaList [ k ] ;
2013-01-11 19:18:58 +01:00
minimap . incFowTextureAlphaSurface ( surfPos , alpha , true ) ;
2013-01-04 19:00:51 +01:00
}
}
else {
const FowAlphaCellsLookupItem cellList = unit - > getFogOfWarRadius ( false ) ;
for ( int k = 0 ; k < cellList . surfPosList . size ( ) ; + + k ) {
const Vec2i & surfPos = cellList . surfPosList [ k ] ;
const float & alpha = cellList . alphaList [ k ] ;
2013-01-11 19:18:58 +01:00
minimap . incFowTextureAlphaSurface ( surfPos , alpha , true ) ;
2010-08-26 01:55:59 +02:00
}
}
2010-03-12 08:42:55 +01:00
}
}
2010-08-26 01:55:59 +02:00
}
2010-08-23 17:10:37 +02:00
}
2010-03-12 08:42:55 +01:00
}
2013-01-03 18:30:59 +01:00
if ( showPerfStats ) {
sprintf ( perfBuf , " In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER " \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , chronoPerf . getMillis ( ) ) ;
perfList . push_back ( perfBuf ) ;
}
if ( showPerfStats & & chronoPerf . getMillis ( ) > = 50 ) {
for ( unsigned int x = 0 ; x < perfList . size ( ) ; + + x ) {
printf ( " %s " , perfList [ x ] . c_str ( ) ) ;
}
}
2010-03-12 08:42:55 +01:00
}
2011-09-21 08:51:28 +02:00
GameSettings * World : : getGameSettingsPtr ( ) {
return ( game ! = NULL ? game - > getGameSettings ( ) : NULL ) ;
}
2010-12-19 09:04:25 +01:00
const GameSettings * World : : getGameSettings ( ) const {
return ( game ! = NULL ? game - > getReadOnlyGameSettings ( ) : NULL ) ;
}
2010-08-23 16:48:33 +02:00
// WARNING! This id is critical! Make sure it fits inside the network packet
2010-05-31 11:45:54 +02:00
// (currently cannot be larger than 2,147,483,647)
// Make sure each faction has their own unique section of id #'s for proper
// multi-platform play
// Calculates the unit unit ID for each faction
//
2010-05-31 11:24:44 +02:00
int World : : getNextUnitId ( Faction * faction ) {
2011-09-25 06:07:59 +02:00
MutexSafeWrapper safeMutex ( & mutexFactionNextUnitId , string ( __FILE__ ) + " _ " + intToStr ( __LINE__ ) ) ;
2010-05-31 11:24:44 +02:00
if ( mapFactionNextUnitId . find ( faction - > getIndex ( ) ) = = mapFactionNextUnitId . end ( ) ) {
2010-05-31 11:45:54 +02:00
mapFactionNextUnitId [ faction - > getIndex ( ) ] = faction - > getIndex ( ) * 100000 ;
2010-05-31 11:24:44 +02:00
}
return mapFactionNextUnitId [ faction - > getIndex ( ) ] + + ;
}
2011-07-05 06:37:35 +02:00
// Get a unique commandid when sending commands to a group of units
int World : : getNextCommandGroupId ( ) {
return + + nextCommandGroupId ;
}
2012-05-22 08:17:56 +02:00
void World : : playStaticVideo ( const string & playVideo ) {
2013-05-28 04:54:23 +02:00
string calculatedFilePath = playVideo ;
bool changed = Properties : : applyTagsToValue ( calculatedFilePath ) ;
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " \n \n Play static video changed: %d [%s] [%s] \n \n " , changed , playVideo . c_str ( ) , calculatedFilePath . c_str ( ) ) ;
this - > game - > playStaticVideo ( calculatedFilePath ) ;
2012-05-22 08:17:56 +02:00
}
void World : : playStreamingVideo ( const string & playVideo ) {
2013-05-28 04:54:23 +02:00
string calculatedFilePath = playVideo ;
bool changed = Properties : : applyTagsToValue ( calculatedFilePath ) ;
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " \n \n Play streaming video changed: %d [%s] [%s] \n \n " , changed , playVideo . c_str ( ) , calculatedFilePath . c_str ( ) ) ;
this - > game - > playStreamingVideo ( calculatedFilePath ) ;
2012-05-22 08:17:56 +02:00
}
void World : : stopStreamingVideo ( const string & playVideo ) {
2013-05-28 04:54:23 +02:00
string calculatedFilePath = playVideo ;
bool changed = Properties : : applyTagsToValue ( calculatedFilePath ) ;
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " \n \n Stop streaming video changed: %d [%s] [%s] \n \n " , changed , playVideo . c_str ( ) , calculatedFilePath . c_str ( ) ) ;
this - > game - > stopStreamingVideo ( calculatedFilePath ) ;
2012-05-22 08:17:56 +02:00
}
void World : : stopAllVideo ( ) {
this - > game - > stopAllVideo ( ) ;
}
2011-12-02 17:07:59 +01:00
void World : : removeResourceTargetFromCache ( const Vec2i & pos ) {
for ( int i = 0 ; i < factions . size ( ) ; + + i ) {
factions [ i ] - > removeResourceTargetFromCache ( pos ) ;
}
}
string World : : getExploredCellsLookupItemCacheStats ( ) {
string result = " " ;
int posCount = 0 ;
int sightCount = 0 ;
int exploredCellCount = 0 ;
int visibleCellCount = 0 ;
for ( std : : map < Vec2i , std : : map < int , ExploredCellsLookupItem > > : : iterator iterMap1 = ExploredCellsLookupItemCache . begin ( ) ;
iterMap1 ! = ExploredCellsLookupItemCache . end ( ) ; + + iterMap1 ) {
posCount + + ;
for ( std : : map < int , ExploredCellsLookupItem > : : iterator iterMap2 = iterMap1 - > second . begin ( ) ;
iterMap2 ! = iterMap1 - > second . end ( ) ; + + iterMap2 ) {
sightCount + + ;
exploredCellCount + = iterMap2 - > second . exploredCellList . size ( ) ;
visibleCellCount + = iterMap2 - > second . visibleCellList . size ( ) ;
}
}
uint64 totalBytes = exploredCellCount * sizeof ( SurfaceCell * ) ;
totalBytes + = visibleCellCount * sizeof ( SurfaceCell * ) ;
totalBytes / = 1000 ;
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
snprintf ( szBuf , 8096 , " pos [%d] sight [%d] [%d][%d] total KB: %s " , posCount , sightCount , exploredCellCount , visibleCellCount , formatNumber ( totalBytes ) . c_str ( ) ) ;
2011-12-02 17:07:59 +01:00
result = szBuf ;
return result ;
}
string World : : getFowAlphaCellsLookupItemCacheStats ( ) {
string result = " " ;
int surfPosCount = 0 ;
int alphaListCount = 0 ;
2013-01-04 19:00:51 +01:00
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
Faction * faction = getFaction ( i ) ;
if ( faction - > getTeam ( ) = = thisTeamIndex ) {
for ( int j = 0 ; j < faction - > getUnitCount ( ) ; + + j ) {
const Unit * unit = faction - > getUnit ( j ) ;
const FowAlphaCellsLookupItem & cache = unit - > getCachedFow ( ) ;
surfPosCount + = cache . surfPosList . size ( ) ;
alphaListCount + = cache . alphaList . size ( ) ;
}
2011-12-02 17:07:59 +01:00
}
}
uint64 totalBytes = surfPosCount * sizeof ( Vec2i ) ;
totalBytes + = alphaListCount * sizeof ( float ) ;
totalBytes / = 1000 ;
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
2013-01-12 08:36:14 +01:00
snprintf ( szBuf , 8096 , " surface count [%d] alpha count [%d] total KB: %s " , surfPosCount , alphaListCount , formatNumber ( totalBytes ) . c_str ( ) ) ;
2011-12-02 17:07:59 +01:00
result = szBuf ;
return result ;
}
string World : : getAllFactionsCacheStats ( ) {
string result = " " ;
uint64 totalBytes = 0 ;
uint64 totalCache1Size = 0 ;
uint64 totalCache2Size = 0 ;
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
uint64 cache1Size = 0 ;
uint64 cache2Size = 0 ;
totalBytes + = getFaction ( i ) - > getCacheKBytes ( & cache1Size , & cache2Size ) ;
totalCache1Size + = cache1Size ;
totalCache2Size + = cache2Size ;
}
2012-10-19 03:31:20 +02:00
char szBuf [ 8096 ] = " " ;
2012-10-26 06:35:14 +02:00
snprintf ( szBuf , 8096 , " totalCache1Size [%llu] totalCache1Size [%llu] total KB: %s " , ( long long unsigned ) totalCache1Size , ( long long unsigned ) totalCache2Size , formatNumber ( totalBytes ) . c_str ( ) ) ;
2011-12-02 17:07:59 +01:00
result = szBuf ;
return result ;
}
2010-05-20 22:19:34 +02:00
std : : string World : : DumpWorldToLog ( bool consoleBasicInfoOnly ) const {
2010-12-09 21:41:11 +01:00
2010-05-18 05:53:57 +02:00
string debugWorldLogFile = Config : : getInstance ( ) . getString ( " DebugWorldLogFile " , " debugWorld.log " ) ;
2010-12-09 21:41:11 +01:00
if ( getGameReadWritePath ( GameConstants : : path_logs_CacheLookupKey ) ! = " " ) {
debugWorldLogFile = getGameReadWritePath ( GameConstants : : path_logs_CacheLookupKey ) + debugWorldLogFile ;
2010-05-20 22:19:34 +02:00
}
2011-03-10 18:31:55 +01:00
else {
string userData = Config : : getInstance ( ) . getString ( " UserData_Root " , " " ) ;
if ( userData ! = " " ) {
2011-03-13 09:23:43 +01:00
endPathWithSlash ( userData ) ;
2011-03-10 18:31:55 +01:00
}
debugWorldLogFile = userData + debugWorldLogFile ;
}
2010-05-20 22:19:34 +02:00
if ( consoleBasicInfoOnly = = true ) {
std : : cout < < " World debug information: " < < std : : endl ;
std : : cout < < " ======================== " < < std : : endl ;
//factions (and their related info)
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
std : : cout < < " Faction detail for index: " < < i < < std : : endl ;
std : : cout < < " -------------------------- " < < std : : endl ;
2010-12-09 21:41:11 +01:00
2013-06-13 03:37:15 +02:00
std : : cout < < " FactionName = " < < getFaction ( i ) - > getType ( ) - > getName ( false ) < < std : : endl ;
2010-05-20 22:19:34 +02:00
std : : cout < < " FactionIndex = " < < intToStr ( getFaction ( i ) - > getIndex ( ) ) < < std : : endl ;
std : : cout < < " teamIndex = " < < intToStr ( getFaction ( i ) - > getTeam ( ) ) < < std : : endl ;
std : : cout < < " startLocationIndex = " < < intToStr ( getFaction ( i ) - > getStartLocationIndex ( ) ) < < std : : endl ;
std : : cout < < " thisFaction = " < < intToStr ( getFaction ( i ) - > getThisFaction ( ) ) < < std : : endl ;
std : : cout < < " control = " < < intToStr ( getFaction ( i ) - > getControlType ( ) ) < < std : : endl ;
}
}
else {
2010-05-18 05:53:57 +02:00
2011-05-25 00:51:45 +02:00
# if defined(WIN32) && !defined(__MINGW32__)
2011-05-18 23:49:11 +02:00
FILE * fp = _wfopen ( utf8_decode ( debugWorldLogFile ) . c_str ( ) , L " w " ) ;
std : : ofstream logFile ( fp ) ;
# else
2010-05-20 22:19:34 +02:00
std : : ofstream logFile ;
logFile . open ( debugWorldLogFile . c_str ( ) , ios_base : : out | ios_base : : trunc ) ;
2011-05-18 23:49:11 +02:00
# endif
2010-05-20 22:19:34 +02:00
logFile < < " World debug information: " < < std : : endl ;
logFile < < " ======================== " < < std : : endl ;
2010-05-18 05:53:57 +02:00
2010-05-20 22:19:34 +02:00
//factions (and their related info)
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
logFile < < " Faction detail for index: " < < i < < std : : endl ;
logFile < < " -------------------------- " < < std : : endl ;
logFile < < getFaction ( i ) - > toString ( ) < < std : : endl ;
}
2010-05-18 05:53:57 +02:00
2010-05-20 22:19:34 +02:00
//undertake the dead
logFile < < " Undertake stats: " < < std : : endl ;
for ( int i = 0 ; i < getFactionCount ( ) ; + + i ) {
2013-06-13 03:37:15 +02:00
logFile < < " Faction: " < < getFaction ( i ) - > getType ( ) - > getName ( false ) < < std : : endl ;
2010-05-20 22:19:34 +02:00
int unitCount = getFaction ( i ) - > getUnitCount ( ) ;
for ( int j = unitCount - 1 ; j > = 0 ; j - - ) {
Unit * unit = getFaction ( i ) - > getUnit ( j ) ;
if ( unit - > getToBeUndertaken ( ) ) {
2013-06-13 10:55:48 +02:00
logFile < < " Undertake unit index = " < < j < < unit - > getFullName ( false ) < < std : : endl ;
2010-05-20 22:19:34 +02:00
}
2010-05-18 05:53:57 +02:00
}
}
2010-05-20 22:19:34 +02:00
logFile . close ( ) ;
2011-05-25 00:51:45 +02:00
# if defined(WIN32) && !defined(__MINGW32__)
2011-05-23 21:23:00 +02:00
if ( fp ) {
fclose ( fp ) ;
}
2011-05-18 23:49:11 +02:00
# endif
2010-05-20 22:19:34 +02:00
}
2012-03-10 04:27:25 +01:00
//printf("Check savegame\n");
if ( this - > game ! = NULL ) {
//printf("Saving...\n");
this - > game - > saveGame ( GameConstants : : saveGameFileDefault ) ;
}
2010-05-18 05:53:57 +02:00
return debugWorldLogFile ;
}
2012-03-10 04:27:25 +01:00
void World : : saveGame ( XmlNode * rootNode ) {
std : : map < string , string > mapTagReplacements ;
XmlNode * worldNode = rootNode - > addChild ( " World " ) ;
// Map map;
2012-03-13 16:21:25 +01:00
map . saveGame ( worldNode ) ;
2012-03-10 04:27:25 +01:00
// Tileset tileset;
worldNode - > addAttribute ( " tileset " , tileset . getName ( ) , mapTagReplacements ) ;
// //TechTree techTree;
// TechTree *techTree;
if ( techTree ! = NULL ) {
techTree - > saveGame ( worldNode ) ;
}
2012-03-28 08:25:57 +02:00
worldNode - > addAttribute ( " techTree " , ( techTree ! = NULL ? techTree - > getName ( ) : " " ) , mapTagReplacements ) ;
2012-03-10 04:27:25 +01:00
// TimeFlow timeFlow;
timeFlow . saveGame ( worldNode ) ;
// Scenario scenario;
//
// UnitUpdater unitUpdater;
unitUpdater . saveGame ( worldNode ) ;
// WaterEffects waterEffects;
// WaterEffects attackEffects; // onMiniMap
// Minimap minimap;
2012-03-13 16:21:25 +01:00
minimap . saveGame ( worldNode ) ;
2012-03-10 04:27:25 +01:00
// Stats stats; //BattleEnd will delete this object
stats . saveGame ( worldNode ) ;
//
// Factions factions;
for ( unsigned int i = 0 ; i < factions . size ( ) ; + + i ) {
factions [ i ] - > saveGame ( worldNode ) ;
}
// RandomGen random;
worldNode - > addAttribute ( " random " , intToStr ( random . getLastNumber ( ) ) , mapTagReplacements ) ;
// ScriptManager* scriptManager;
//
// int thisFactionIndex;
worldNode - > addAttribute ( " thisFactionIndex " , intToStr ( thisFactionIndex ) , mapTagReplacements ) ;
// int thisTeamIndex;
worldNode - > addAttribute ( " thisTeamIndex " , intToStr ( thisTeamIndex ) , mapTagReplacements ) ;
// int frameCount;
worldNode - > addAttribute ( " frameCount " , intToStr ( frameCount ) , mapTagReplacements ) ;
// //int nextUnitId;
// Mutex mutexFactionNextUnitId;
2012-03-13 16:21:25 +01:00
MutexSafeWrapper safeMutex ( & mutexFactionNextUnitId , string ( __FILE__ ) + " _ " + intToStr ( __LINE__ ) ) ;
2012-03-10 04:27:25 +01:00
// std::map<int,int> mapFactionNextUnitId;
for ( std : : map < int , int > : : iterator iterMap = mapFactionNextUnitId . begin ( ) ;
iterMap ! = mapFactionNextUnitId . end ( ) ; + + iterMap ) {
XmlNode * factionNextUnitIdNode = worldNode - > addChild ( " FactionNextUnitId " ) ;
factionNextUnitIdNode - > addAttribute ( " key " , intToStr ( iterMap - > first ) , mapTagReplacements ) ;
factionNextUnitIdNode - > addAttribute ( " value " , intToStr ( iterMap - > second ) , mapTagReplacements ) ;
}
2012-03-13 16:21:25 +01:00
safeMutex . ReleaseLock ( ) ;
2012-03-10 04:27:25 +01:00
// //config
// bool fogOfWarOverride;
worldNode - > addAttribute ( " fogOfWarOverride " , intToStr ( fogOfWarOverride ) , mapTagReplacements ) ;
// bool fogOfWar;
worldNode - > addAttribute ( " fogOfWar " , intToStr ( fogOfWar ) , mapTagReplacements ) ;
// int fogOfWarSmoothingFrameSkip;
worldNode - > addAttribute ( " fogOfWarSmoothingFrameSkip " , intToStr ( fogOfWarSmoothingFrameSkip ) , mapTagReplacements ) ;
// bool fogOfWarSmoothing;
worldNode - > addAttribute ( " fogOfWarSmoothing " , intToStr ( fogOfWarSmoothing ) , mapTagReplacements ) ;
// Game *game;
// Chrono chronoPerfTimer;
// bool perfTimerEnabled;
//
// bool unitParticlesEnabled;
worldNode - > addAttribute ( " unitParticlesEnabled " , intToStr ( unitParticlesEnabled ) , mapTagReplacements ) ;
// bool staggeredFactionUpdates;
worldNode - > addAttribute ( " staggeredFactionUpdates " , intToStr ( staggeredFactionUpdates ) , mapTagReplacements ) ;
// std::map<string,StaticSound *> staticSoundList;
// std::map<string,StrSound *> streamSoundList;
//
// uint32 nextCommandGroupId;
worldNode - > addAttribute ( " nextCommandGroupId " , intToStr ( nextCommandGroupId ) , mapTagReplacements ) ;
// string queuedScenarioName;
worldNode - > addAttribute ( " queuedScenarioName " , queuedScenarioName , mapTagReplacements ) ;
// bool queuedScenarioKeepFactions;
worldNode - > addAttribute ( " queuedScenarioKeepFactions " , intToStr ( queuedScenarioKeepFactions ) , mapTagReplacements ) ;
2012-10-17 22:15:50 +02:00
worldNode - > addAttribute ( " disableAttackEffects " , intToStr ( disableAttackEffects ) , mapTagReplacements ) ;
2012-03-10 04:27:25 +01:00
}
2012-03-13 00:08:22 +01:00
void World : : loadGame ( const XmlNode * rootNode ) {
loadWorldNode = rootNode ;
}
2010-03-12 08:42:55 +01:00
} } //end namespace