2009-04-18 19:00:33 +02:00
/* Copyright (C) 2009 Wildfire Games.
* This file is part of 0 A . D .
*
* 0 A . D . is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 2 of the License , or
* ( at your option ) any later version .
*
* 0 A . D . is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with 0 A . D . If not , see < http : //www.gnu.org/licenses/>.
*/
2006-04-24 01:14:18 +02:00
# include "precompiled.h"
2006-06-10 01:07:11 +02:00
# include "Formation.h"
2006-06-02 04:10:27 +02:00
# include "ps/CLogger.h"
# include "ps/CStr.h"
2006-04-24 01:14:18 +02:00
# include "maths/MathUtil.h"
2009-11-03 22:46:35 +01:00
# define LOG_CATEGORY L"Formation"
2006-04-24 01:14:18 +02:00
2006-06-10 01:07:11 +02:00
CFormation : : CFormation ( )
2006-04-24 01:14:18 +02:00
{
m_numSlots = 0 ;
}
2009-11-03 22:46:35 +01:00
bool CFormation : : LoadXml ( const VfsPath & filename )
2006-04-24 01:14:18 +02:00
{
2006-10-20 03:06:43 +02:00
CXeromyces XeroFile ;
2006-04-24 01:14:18 +02:00
2006-10-20 03:06:43 +02:00
if ( XeroFile . Load ( filename ) ! = PSRETURN_OK )
return false ;
2006-04-24 01:14:18 +02:00
2007-05-02 14:07:08 +02:00
# define EL(x) int el_##x = XeroFile.GetElementID(#x)
# define AT(x) int at_##x = XeroFile.GetAttributeID(#x)
2006-04-24 01:14:18 +02:00
EL ( formation ) ;
EL ( fl ) ;
EL ( rk ) ;
EL ( blank ) ;
AT ( tag ) ;
AT ( bonus ) ;
2006-05-04 06:14:48 +02:00
AT ( bonusbase ) ;
2006-04-24 01:14:18 +02:00
AT ( bonustype ) ;
AT ( bonusval ) ;
AT ( penalty ) ;
2006-05-04 06:14:48 +02:00
AT ( penaltybase ) ;
2006-04-24 01:14:18 +02:00
AT ( penaltytype ) ;
AT ( penaltyval ) ;
AT ( anglepenalty ) ;
AT ( anglepenaltydivs ) ;
AT ( anglepenaltytype ) ;
AT ( anglepenaltyval ) ;
AT ( required ) ;
AT ( next ) ;
AT ( prior ) ;
AT ( movement ) ;
AT ( filespacing ) ;
AT ( rankspacing ) ;
AT ( category ) ;
AT ( order ) ;
# undef AT
# undef EL
2007-05-02 14:07:08 +02:00
XMBElement Root = XeroFile . GetRoot ( ) ;
if ( Root . GetNodeName ( ) ! = el_formation )
2006-04-24 01:14:18 +02:00
{
2009-11-03 22:46:35 +01:00
LOG ( CLogger : : Error , LOG_CATEGORY , L " CFormation::LoadXml: XML root was not \" Formation \" in file %ls. Load failed. " , filename . string ( ) . c_str ( ) ) ;
2006-04-24 01:14:18 +02:00
return ( false ) ;
}
//Load in single attributes
2007-05-02 14:07:08 +02:00
XMBAttributeList Attributes = Root . GetAttributes ( ) ;
2006-04-24 01:14:18 +02:00
for ( int i = 0 ; i < Attributes . Count ; + + i )
{
2007-05-02 14:07:08 +02:00
XMBAttribute Attr = Attributes . Item ( i ) ;
2006-04-24 01:14:18 +02:00
if ( Attr . Name = = at_tag )
m_tag = CStr ( Attr . Value ) ;
else if ( Attr . Name = = at_bonus )
m_bonus = CStr ( Attr . Value ) ;
2006-05-04 06:14:48 +02:00
else if ( Attr . Name = = at_bonusbase )
m_bonusBase = CStr ( Attr . Value ) ;
2006-04-24 01:14:18 +02:00
else if ( Attr . Name = = at_bonustype )
m_bonusType = CStr ( Attr . Value ) ;
else if ( Attr . Name = = at_bonusval )
m_bonusVal = CStr ( Attr . Value ) . ToFloat ( ) ;
else if ( Attr . Name = = at_penalty )
m_penalty = CStr ( Attr . Value ) ;
2006-05-04 06:14:48 +02:00
else if ( Attr . Name = = at_penaltybase )
m_penaltyBase = CStr ( Attr . Value ) ;
2006-04-24 01:14:18 +02:00
else if ( Attr . Name = = at_penaltytype )
m_penaltyType = CStr ( Attr . Value ) ;
else if ( Attr . Name = = at_penaltyval )
m_penaltyVal = CStr ( Attr . Value ) . ToFloat ( ) ;
else if ( Attr . Name = = at_anglepenalty )
m_anglePenalty = CStr ( Attr . Value ) ;
else if ( Attr . Name = = at_anglepenaltydivs )
m_anglePenaltyDivs = CStr ( Attr . Value ) . ToInt ( ) ;
else if ( Attr . Name = = at_anglepenaltytype )
m_anglePenaltyType = CStr ( Attr . Value ) ;
else if ( Attr . Name = = at_anglepenaltyval )
m_anglePenaltyVal = CStr ( Attr . Value ) . ToFloat ( ) ;
else if ( Attr . Name = = at_required )
m_required = CStr ( Attr . Value ) . ToInt ( ) ;
else if ( Attr . Name = = at_next )
m_next = CStr ( Attr . Value ) ;
else if ( Attr . Name = = at_prior )
m_prior = CStr ( Attr . Value ) ;
else if ( Attr . Name = = at_movement )
m_movement = CStr ( Attr . Value ) ;
else if ( Attr . Name = = at_rankspacing )
m_rankSpacing = CStr ( Attr . Value ) . ToFloat ( ) ;
else if ( Attr . Name = = at_filespacing )
m_fileSpacing = CStr ( Attr . Value ) . ToFloat ( ) ;
else
{
2009-11-04 16:29:28 +01:00
LOG ( CLogger : : Error , LOG_CATEGORY , L " CFormation::LoadXml: Invalid attribute %hs defined in formation file %ls. Load failed. " , XeroFile . GetAttributeString ( Attr . Name ) . c_str ( ) , filename . string ( ) . c_str ( ) ) ;
2006-04-24 01:14:18 +02:00
return ( false ) ;
}
}
2007-05-02 14:07:08 +02:00
XMBElementList RootChildren = Root . GetChildNodes ( ) ;
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
size_t file = 0 ;
size_t rank = 0 ;
size_t maxrank = 0 ;
2006-04-24 01:14:18 +02:00
//Read in files and ranks
for ( int i = 0 ; i < RootChildren . Count ; + + i )
{
2007-05-02 14:07:08 +02:00
XMBElement RootChild = RootChildren . Item ( i ) ;
int ChildName = RootChild . GetNodeName ( ) ;
2006-04-24 01:14:18 +02:00
if ( ChildName = = el_fl )
{
rank = 0 ;
2007-05-02 14:07:08 +02:00
XMBAttributeList FileAttribList = RootChild . GetAttributes ( ) ;
2006-04-24 01:14:18 +02:00
//Load default category
2007-05-02 14:07:08 +02:00
CStr FileCatValue = FileAttribList . GetNamedItem ( at_category ) ;
2006-04-24 01:14:18 +02:00
//Specific slots in this file (row)
2007-05-02 14:07:08 +02:00
XMBElementList RankNodes = RootChild . GetChildNodes ( ) ;
2006-04-24 01:14:18 +02:00
for ( int r = 0 ; r < RankNodes . Count ; + + r )
{
2007-05-02 14:07:08 +02:00
XMBElement Rank = RankNodes . Item ( r ) ;
if ( Rank . GetNodeName ( ) = = el_blank )
2006-04-24 01:14:18 +02:00
{
+ + rank ;
continue ;
}
2007-05-02 14:07:08 +02:00
else if ( Rank . GetNodeName ( ) ! = el_rk )
2006-04-24 01:14:18 +02:00
return false ;
//error
2007-05-02 14:07:08 +02:00
XMBAttributeList RankAttribList = Rank . GetAttributes ( ) ;
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
size_t order = CStr ( RankAttribList . GetNamedItem ( at_order ) ) . ToInt ( ) ;
2007-05-02 14:07:08 +02:00
CStr category = CStr ( RankAttribList . GetNamedItem ( at_category ) ) ;
2006-04-24 01:14:18 +02:00
if ( order < = 0 )
{
2009-11-03 22:46:35 +01:00
LOG ( CLogger : : Error , LOG_CATEGORY , L " CFormation::LoadXml: Invalid (negative number or 0) order defined in formation file %ls. The game will try to continue anyway. " , filename . string ( ) . c_str ( ) ) ;
2006-04-24 01:14:18 +02:00
continue ;
}
- - order ; //We need this to be in line with arrays, so start at 0
2007-05-02 14:07:08 +02:00
//if ( category.Length() )
2006-04-24 01:14:18 +02:00
//AssignCategory(order, category);
//else
AssignCategory ( order , FileCatValue ) ;
m_slots [ order ] . fileOff = file * m_fileSpacing ;
m_slots [ order ] . rankOff = rank * m_rankSpacing ;
+ + m_numSlots ;
+ + rank ;
}
if ( rank > maxrank )
maxrank = rank ;
+ + file ;
} //if el_fl
else if ( ChildName = = el_blank )
+ + file ;
}
float centerx = maxrank * m_rankSpacing / 2.0f ;
float centery = file * m_fileSpacing / 2.0f ;
//Here we check to make sure no order was skipped over. If so, failure, because we rely
//on a linearly accessible slots in entityformation.cpp.
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
for ( size_t i = 0 ; i < m_numSlots ; + + i )
2006-04-24 01:14:18 +02:00
{
if ( m_slots . find ( i ) = = m_slots . end ( ) )
{
2009-11-03 22:46:35 +01:00
LOG ( CLogger : : Error , LOG_CATEGORY , L " CFormation::LoadXml: Missing orders in %ls. Load failed. " , filename . string ( ) . c_str ( ) ) ;
2006-04-24 01:14:18 +02:00
return false ;
}
else
{
m_slots [ i ] . rankOff = m_slots [ i ] . rankOff - centerx ;
m_slots [ i ] . fileOff = m_slots [ i ] . fileOff - centery ;
}
}
return true ;
}
2008-07-17 19:00:00 +02:00
void CFormation : : AssignCategory ( size_t order , const CStr & category_ )
2006-04-24 01:14:18 +02:00
{
2008-07-17 19:00:00 +02:00
CStr category ( category_ ) ;
2006-04-24 01:14:18 +02:00
category . Remove ( CStr ( " , " ) ) ;
category = category + " " ; //So the final word will be pushed as well
CStr temp ;
//Push categories until last space
while ( ( temp = category . BeforeFirst ( " " ) ) ! = " " )
{
m_slots [ order ] . category . push_back ( temp ) ;
//Don't use remove because certain categories could be substrings of others
size_t off = category . find ( temp ) ;
category . erase ( off , temp . length ( ) ) ;
}
2006-10-20 03:06:43 +02:00
}