2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2006-07-13 05:29:33 +02:00
|
|
|
#include "EntityTemplateCollection.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "graphics/ObjectManager.h"
|
|
|
|
#include "graphics/Model.h"
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
#include "ps/VFSUtil.h"
|
|
|
|
#include "ps/Player.h"
|
2006-07-18 06:17:46 +02:00
|
|
|
#include "ps/Game.h"
|
2004-05-22 01:46:16 +02:00
|
|
|
|
2004-08-15 22:57:31 +02:00
|
|
|
#define LOG_CATEGORY "entity"
|
|
|
|
|
2006-07-13 05:29:33 +02:00
|
|
|
void CEntityTemplateCollection::LoadFile( const char* path )
|
2005-03-27 03:44:41 +02:00
|
|
|
{
|
2005-03-29 22:50:04 +02:00
|
|
|
// 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;
|
2005-03-27 03:44:41 +02:00
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2005-08-09 17:55:44 +02:00
|
|
|
static void LoadFileThunk( const char* path, const DirEnt* UNUSED(ent), void* context )
|
2005-03-27 03:44:41 +02:00
|
|
|
{
|
2006-07-13 05:29:33 +02:00
|
|
|
CEntityTemplateCollection* this_ = (CEntityTemplateCollection*)context;
|
2005-03-27 03:44:41 +02:00
|
|
|
this_->LoadFile(path);
|
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2006-07-13 05:29:33 +02:00
|
|
|
int CEntityTemplateCollection::loadTemplates()
|
2005-03-27 03:44:41 +02:00
|
|
|
{
|
2006-07-18 06:17:46 +02:00
|
|
|
// List all files in entities/ and its subdirectories.
|
2006-04-14 08:32:05 +02:00
|
|
|
THROW_ERR( vfs_dir_enum( "entities/", VFS_DIR_RECURSIVE, "*.xml",
|
2005-08-09 17:55:44 +02:00
|
|
|
LoadFileThunk, this ) );
|
2006-07-18 06:17:46 +02:00
|
|
|
|
|
|
|
/*// 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) );
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
2005-05-03 23:36:57 +02:00
|
|
|
return 0;
|
2005-03-29 22:50:04 +02:00
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2006-07-20 16:37:58 +02:00
|
|
|
CEntityTemplate* CEntityTemplateCollection::getTemplate( const CStrW& name, CPlayer* player )
|
2005-03-29 22:50:04 +02:00
|
|
|
{
|
2006-05-14 00:11:46 +02:00
|
|
|
// Find player ID
|
|
|
|
int id = ( player == 0 ? NULL_PLAYER : player->GetPlayerID() );
|
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
// Check whether this template has already been loaded
|
2006-07-18 06:17:46 +02:00
|
|
|
TemplateMap::iterator it = m_templates[id].find( name );
|
2006-05-14 00:11:46 +02:00
|
|
|
if( it != m_templates[id].end() )
|
2005-03-29 22:50:04 +02:00
|
|
|
return( it->second );
|
2004-10-07 21:23:35 +02:00
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
// Find the filename corresponding to this template
|
2006-07-18 06:17:46 +02:00
|
|
|
TemplateFilenameMap::iterator filename_it = m_templateFilenames.find( name );
|
2005-03-29 22:50:04 +02:00
|
|
|
if( filename_it == m_templateFilenames.end() )
|
|
|
|
return( NULL );
|
2004-11-11 08:09:32 +01:00
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
CStr path( filename_it->second );
|
2004-10-07 21:23:35 +02:00
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
// Try to load to the entity
|
2006-07-13 05:29:33 +02:00
|
|
|
CEntityTemplate* newTemplate = new CEntityTemplate( player );
|
2005-03-29 22:50:04 +02:00
|
|
|
if( !newTemplate->loadXML( path ) )
|
2004-11-11 08:09:32 +01:00
|
|
|
{
|
2006-07-18 00:18:37 +02:00
|
|
|
LOG(ERROR, LOG_CATEGORY, "CEntityTemplateCollection::getTemplate(): Couldn't load template \"%s\"", path.c_str());
|
2006-06-06 08:31:17 +02:00
|
|
|
delete newTemplate;
|
2005-03-29 22:50:04 +02:00
|
|
|
return( NULL );
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|
2004-05-22 01:46:16 +02:00
|
|
|
|
2006-07-18 00:18:37 +02:00
|
|
|
LOG(NORMAL, LOG_CATEGORY, "CEntityTemplateCollection::getTemplate(): Loaded template \"%s\"", path.c_str());
|
2006-05-14 00:11:46 +02:00
|
|
|
m_templates[id][name] = newTemplate;
|
2004-05-22 01:46:16 +02:00
|
|
|
|
2005-03-29 22:50:04 +02:00
|
|
|
return newTemplate;
|
2004-05-22 01:46:16 +02:00
|
|
|
}
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2006-07-13 05:29:33 +02:00
|
|
|
void CEntityTemplateCollection::getEntityTemplateNames( std::vector<CStrW>& names )
|
2005-03-23 00:31:30 +01:00
|
|
|
{
|
2006-07-18 06:17:46 +02:00
|
|
|
for( TemplateFilenameMap::iterator it = m_templateFilenames.begin(); it != m_templateFilenames.end(); ++it )
|
2005-03-30 04:06:00 +02:00
|
|
|
if( ! (it->first.Length() > 8 && it->first.Left(8) == L"template"))
|
|
|
|
names.push_back( it->first );
|
2005-03-23 00:31:30 +01:00
|
|
|
}
|
|
|
|
|
2006-07-18 06:17:46 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-13 05:29:33 +02:00
|
|
|
CEntityTemplateCollection::~CEntityTemplateCollection()
|
2004-06-11 00:24:03 +02:00
|
|
|
{
|
2006-05-14 00:11:46 +02:00
|
|
|
for( int id = 0; id < PS_MAX_PLAYERS + 2; id++ )
|
2006-07-18 06:17:46 +02:00
|
|
|
for( TemplateMap::iterator it = m_templates[id].begin(); it != m_templates[id].end(); ++it )
|
2006-05-14 00:11:46 +02:00
|
|
|
delete( it->second );
|
2004-06-11 00:24:03 +02:00
|
|
|
}
|