1
0
forked from 0ad/0ad
0ad/source/simulation/EntityTemplateCollection.cpp
Ykkrosh 35e91718c5 # Added tool for viewing models and animations outside the game.
Atlas: Added ActorViewer. Moved GL canvas into separate class for shared
use. Disabled message-handling callback while blocked on the game, and
stopped creating dialog boxes inside the game thread in order to avoid
deadlocks (hopefully). Support multiple Views (for independent sets of
camera/update/render code). Recalculate territory boundaries when
necessary. Changed default list of animations to match those currently
used by actors.
# Tidied up more code.
Moved some more #includes out of .h files, to minimise unnecessary
compilation.
MathUtil: Deleted unused/unuseful macros (M_PI (use PI instead), M_PI_2
(use PI/2), MAX3, ABS (use abs)).
ObjectManager: Removed some ScEd-specific things.
Unit: Moved creation out of UnitManager, so units can be created without
adding to the manager. Changed CStr8 to the more conventional CStr.
app_hooks: Removed warning for setting multiple times.
win: Restored SEH catcher.
GameSetup, GameView: Removed RenderNoCull, because it doesn't seem to do
what it says it does ("force renderer to load everything") since we're
loading-on-demand most stuff and it doesn't seem especially useful since
we'd prefer to minimise loading times (but feel free to correct me if
I'm wrong). (And because it crashes when things need to be initialised
in a different order, so it's easier to remove than to understand and
fix it.)
PatchRData, Renderer: Work sensibly when there's no game (hence no LOS
manager, water, etc).
LOSManager: Use entity position instead of actor position when possible.
TerritoryManager: Allow delayed recalculations (so Atlas can issue lots
of move+recalculate commands per frame).
Cinematic: Non-pointer wxTimer, so it doesn't leak and doesn't have to
be deleted manually.

This was SVN commit r4261.
2006-08-28 17:36:42 +00:00

110 lines
3.6 KiB
C++

#include "precompiled.h"
#include "EntityTemplateCollection.h"
#include "EntityTemplate.h"
#include "graphics/ObjectManager.h"
#include "graphics/Model.h"
#include "ps/CLogger.h"
#include "ps/VFSUtil.h"
#include "ps/Player.h"
#include "ps/Game.h"
#define LOG_CATEGORY "entity"
void CEntityTemplateCollection::LoadFile( const char* path )
{
// Build the entity name -> filename mapping. This is done so that
// the entity 'x' can be in units/x.xml, structures/x.xml, etc, and
// we don't have to search every directory for x.xml.
// Extract the filename out of the path+name+extension.
// Equivalent to /.*\/(.*)\.xml/, but not as pretty.
CStrW tag = CStr(path).AfterLast("/").BeforeLast(".xml");
m_templateFilenames[tag] = path;
}
static void LoadFileThunk( const char* path, const DirEnt* UNUSED(ent), void* context )
{
CEntityTemplateCollection* this_ = (CEntityTemplateCollection*)context;
this_->LoadFile(path);
}
int CEntityTemplateCollection::loadTemplates()
{
// List all files in entities/ and its subdirectories.
THROW_ERR( vfs_dir_enum( "entities/", VFS_DIR_RECURSIVE, "*.xml",
LoadFileThunk, this ) );
/*// Load all the templates; this is necessary so that we can apply techs to them
// (otherwise a tech can't affect the template of a unit that doesn't yet exist)
for( TemplateFilenameMap::iterator it = m_templateFilenames.begin(); it != m_templateFilenames.end(); ++it )
{
// Load the no-player version of this template (used by techs for base values)
getTemplate( it->first, 0 );
for( uint i=0; i<=g_Game->GetNumPlayers(); i++ )
{
// TODO: Load the template just once and clone it to get these player templates
getTemplate( it->first, g_Game->GetPlayer(i) );
}
}*/
return 0;
}
CEntityTemplate* CEntityTemplateCollection::getTemplate( const CStrW& name, CPlayer* player )
{
// Find player ID
int id = ( player == 0 ? NULL_PLAYER : player->GetPlayerID() );
// Check whether this template has already been loaded
TemplateMap::iterator it = m_templates[id].find( name );
if( it != m_templates[id].end() )
return( it->second );
// Find the filename corresponding to this template
TemplateFilenameMap::iterator filename_it = m_templateFilenames.find( name );
if( filename_it == m_templateFilenames.end() )
return( NULL );
CStr path( filename_it->second );
// Try to load to the entity
CEntityTemplate* newTemplate = new CEntityTemplate( player );
if( !newTemplate->loadXML( path ) )
{
LOG(ERROR, LOG_CATEGORY, "CEntityTemplateCollection::getTemplate(): Couldn't load template \"%s\"", path.c_str());
delete newTemplate;
return( NULL );
}
LOG(NORMAL, LOG_CATEGORY, "CEntityTemplateCollection::getTemplate(): Loaded template \"%s\"", path.c_str());
m_templates[id][name] = newTemplate;
return newTemplate;
}
void CEntityTemplateCollection::getEntityTemplateNames( std::vector<CStrW>& names )
{
for( TemplateFilenameMap::iterator it = m_templateFilenames.begin(); it != m_templateFilenames.end(); ++it )
if( ! (it->first.Length() > 8 && it->first.Left(8) == L"template"))
names.push_back( it->first );
}
void CEntityTemplateCollection::getPlayerTemplates( CPlayer* player, std::vector<CEntityTemplate*>& dest )
{
int id = ( player == 0 ? NULL_PLAYER : player->GetPlayerID() );
for( TemplateMap::iterator it = m_templates[id].begin(); it != m_templates[id].end(); ++it )
{
dest.push_back( it->second );
}
}
CEntityTemplateCollection::~CEntityTemplateCollection()
{
for( int id = 0; id < PS_MAX_PLAYERS + 2; id++ )
for( TemplateMap::iterator it = m_templates[id].begin(); it != m_templates[id].end(); ++it )
delete( it->second );
}