2010-01-29 22:13:18 +01:00
/* Copyright (C) 2010 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/>.
*/
# include "precompiled.h"
# include "simulation2/system/Component.h"
# include "ICmpObstruction.h"
2010-03-18 00:13:21 +01:00
# include "ICmpObstructionManager.h"
2010-04-30 01:36:05 +02:00
# include "ICmpPosition.h"
2010-01-29 22:13:18 +01:00
# include "simulation2/MessageTypes.h"
/**
* Obstruction implementation . This keeps the ICmpPathfinder ' s model of the world updated when the
* entities move and die , with shapes derived from ICmpFootprint .
*/
class CCmpObstruction : public ICmpObstruction
{
public :
static void ClassInit ( CComponentManager & componentManager )
{
componentManager . SubscribeToMessageType ( MT_PositionChanged ) ;
2010-04-30 01:36:05 +02:00
componentManager . SubscribeToMessageType ( MT_MotionChanged ) ;
2010-01-29 22:13:18 +01:00
componentManager . SubscribeToMessageType ( MT_Destroy ) ;
}
DEFAULT_COMPONENT_ALLOCATOR ( Obstruction )
2010-04-30 01:36:05 +02:00
// Template state:
enum {
STATIC ,
UNIT
} m_Type ;
entity_pos_t m_Size0 ; // radius or width
entity_pos_t m_Size1 ; // radius or depth
2010-03-18 00:13:21 +01:00
bool m_Active ; // whether the obstruction is obstructing or just an inactive placeholder
2010-04-30 01:36:05 +02:00
// Dynamic state:
bool m_Moving ;
2010-03-18 00:13:21 +01:00
ICmpObstructionManager : : tag_t m_Tag ;
2010-04-09 21:02:39 +02:00
static std : : string GetSchema ( )
{
return
2010-04-23 18:09:03 +02:00
" <a:example/> "
" <a:help>Causes this entity's footprint to obstruct the motion of other units.</a:help> "
2010-04-30 01:36:05 +02:00
" <choice> "
" <element name='Static'> "
" <attribute name='width'> "
" <ref name='positiveDecimal'/> "
" </attribute> "
" <attribute name='depth'> "
" <ref name='positiveDecimal'/> "
" </attribute> "
" </element> "
" <element name='Unit'> "
" <attribute name='radius'> "
" <ref name='positiveDecimal'/> "
" </attribute> "
" </element> "
" </choice> "
2010-04-09 21:02:39 +02:00
" <optional> "
2010-04-23 18:09:03 +02:00
" <element name='Inactive' a:help='If this element is present, this entity will be ignored in collision tests by other units but can still perform its own collision tests'> "
" <empty/> "
" </element> "
2010-04-09 21:02:39 +02:00
" </optional> " ;
}
2010-04-23 18:09:03 +02:00
2010-05-01 11:48:39 +02:00
virtual void Init ( const CSimContext & UNUSED ( context ) , const CParamNode & paramNode )
2010-01-29 22:13:18 +01:00
{
2010-04-30 01:36:05 +02:00
if ( paramNode . GetChild ( " Unit " ) . IsOk ( ) )
{
m_Type = UNIT ;
m_Size0 = m_Size1 = paramNode . GetChild ( " Unit " ) . GetChild ( " @radius " ) . ToFixed ( ) ;
}
else
{
m_Type = STATIC ;
m_Size0 = paramNode . GetChild ( " Static " ) . GetChild ( " @width " ) . ToFixed ( ) ;
m_Size1 = paramNode . GetChild ( " Static " ) . GetChild ( " @depth " ) . ToFixed ( ) ;
}
2010-03-18 00:13:21 +01:00
if ( paramNode . GetChild ( " Inactive " ) . IsOk ( ) )
m_Active = false ;
else
m_Active = true ;
2010-07-04 19:19:38 +02:00
m_Tag = ICmpObstructionManager : : tag_t ( ) ;
2010-04-30 01:36:05 +02:00
m_Moving = false ;
2010-01-29 22:13:18 +01:00
}
virtual void Deinit ( const CSimContext & UNUSED ( context ) )
{
}
virtual void Serialize ( ISerializer & UNUSED ( serialize ) )
{
2010-03-18 00:13:21 +01:00
// TODO: Coordinate with CCmpObstructionManager serialisation
2010-01-29 22:13:18 +01:00
}
virtual void Deserialize ( const CSimContext & context , const CParamNode & paramNode , IDeserializer & UNUSED ( deserialize ) )
{
Init ( context , paramNode ) ;
2010-03-18 00:13:21 +01:00
// TODO: Coordinate with CCmpObstructionManager serialisation
2010-01-29 22:13:18 +01:00
}
virtual void HandleMessage ( const CSimContext & context , const CMessage & msg , bool UNUSED ( global ) )
{
switch ( msg . GetType ( ) )
{
case MT_PositionChanged :
{
2010-03-18 00:13:21 +01:00
if ( ! m_Active )
break ;
2010-01-29 22:13:18 +01:00
const CMessagePositionChanged & data = static_cast < const CMessagePositionChanged & > ( msg ) ;
2010-07-04 19:19:38 +02:00
if ( ! data . inWorld & & ! m_Tag . valid ( ) )
2010-03-18 00:13:21 +01:00
break ; // nothing needs to change
2010-01-29 22:13:18 +01:00
2010-03-18 00:13:21 +01:00
CmpPtr < ICmpObstructionManager > cmpObstructionManager ( context , SYSTEM_ENTITY ) ;
if ( cmpObstructionManager . null ( ) )
2010-01-29 22:13:18 +01:00
break ;
2010-07-04 19:19:38 +02:00
if ( data . inWorld & & m_Tag . valid ( ) )
2010-01-29 22:13:18 +01:00
{
2010-03-18 00:13:21 +01:00
cmpObstructionManager - > MoveShape ( m_Tag , data . x , data . z , data . a ) ;
2010-01-29 22:13:18 +01:00
}
2010-07-04 19:19:38 +02:00
else if ( data . inWorld & & ! m_Tag . valid ( ) )
2010-01-29 22:13:18 +01:00
{
// Need to create a new pathfinder shape:
2010-04-30 01:36:05 +02:00
if ( m_Type = = STATIC )
m_Tag = cmpObstructionManager - > AddStaticShape ( data . x , data . z , data . a , m_Size0 , m_Size1 ) ;
2010-01-29 22:13:18 +01:00
else
2010-04-30 01:36:05 +02:00
m_Tag = cmpObstructionManager - > AddUnitShape ( data . x , data . z , m_Size0 , m_Moving ) ;
2010-01-29 22:13:18 +01:00
}
2010-07-04 19:19:38 +02:00
else if ( ! data . inWorld & & m_Tag . valid ( ) )
2010-01-29 22:13:18 +01:00
{
2010-03-18 00:13:21 +01:00
cmpObstructionManager - > RemoveShape ( m_Tag ) ;
2010-07-04 19:19:38 +02:00
m_Tag = ICmpObstructionManager : : tag_t ( ) ;
2010-01-29 22:13:18 +01:00
}
break ;
}
2010-04-30 01:36:05 +02:00
case MT_MotionChanged :
{
const CMessageMotionChanged & data = static_cast < const CMessageMotionChanged & > ( msg ) ;
m_Moving = ! data . speed . IsZero ( ) ;
2010-07-04 19:19:38 +02:00
if ( m_Tag . valid ( ) & & m_Type = = UNIT )
2010-04-30 01:36:05 +02:00
{
CmpPtr < ICmpObstructionManager > cmpObstructionManager ( context , SYSTEM_ENTITY ) ;
if ( cmpObstructionManager . null ( ) )
break ;
cmpObstructionManager - > SetUnitMovingFlag ( m_Tag , m_Moving ) ;
}
break ;
}
2010-01-29 22:13:18 +01:00
case MT_Destroy :
{
2010-07-04 19:19:38 +02:00
if ( m_Tag . valid ( ) )
2010-01-29 22:13:18 +01:00
{
2010-03-18 00:13:21 +01:00
CmpPtr < ICmpObstructionManager > cmpObstructionManager ( context , SYSTEM_ENTITY ) ;
if ( cmpObstructionManager . null ( ) )
2010-01-29 22:13:18 +01:00
break ;
2010-03-18 00:13:21 +01:00
cmpObstructionManager - > RemoveShape ( m_Tag ) ;
2010-07-04 19:19:38 +02:00
m_Tag = ICmpObstructionManager : : tag_t ( ) ;
2010-01-29 22:13:18 +01:00
}
break ;
}
}
}
2010-03-18 00:13:21 +01:00
2010-04-30 01:36:05 +02:00
virtual ICmpObstructionManager : : tag_t GetObstruction ( )
2010-03-18 00:13:21 +01:00
{
2010-04-30 01:36:05 +02:00
return m_Tag ;
}
2010-03-18 00:13:21 +01:00
2010-04-30 01:36:05 +02:00
virtual entity_pos_t GetUnitRadius ( )
{
if ( m_Type = = UNIT )
return m_Size0 ;
else
return entity_pos_t : : Zero ( ) ;
}
virtual bool CheckCollisions ( )
{
2010-05-01 11:48:39 +02:00
CmpPtr < ICmpPosition > cmpPosition ( GetSimContext ( ) , GetEntityId ( ) ) ;
2010-03-18 00:13:21 +01:00
if ( cmpPosition . null ( ) )
return false ;
CFixedVector3D pos = cmpPosition - > GetPosition ( ) ;
2010-05-01 11:48:39 +02:00
CmpPtr < ICmpObstructionManager > cmpObstructionManager ( GetSimContext ( ) , SYSTEM_ENTITY ) ;
2010-03-18 00:13:21 +01:00
SkipTagObstructionFilter filter ( m_Tag ) ; // ignore collisions with self
2010-04-30 01:36:05 +02:00
if ( m_Type = = STATIC )
return cmpObstructionManager - > TestStaticShape ( filter , pos . X , pos . Z , cmpPosition - > GetRotation ( ) . Y , m_Size0 , m_Size1 ) ;
2010-03-18 00:13:21 +01:00
else
2010-04-30 01:36:05 +02:00
return cmpObstructionManager - > TestUnitShape ( filter , pos . X , pos . Z , m_Size0 ) ;
2010-03-18 00:13:21 +01:00
}
2010-01-29 22:13:18 +01:00
} ;
REGISTER_COMPONENT_TYPE ( Obstruction )