2012-09-25 00:27:32 +02:00
|
|
|
/* Copyright (C) 2012 Wildfire Games.
|
2010-09-23 14:13:13 +02:00
|
|
|
* 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 "ICmpVision.h"
|
|
|
|
|
2012-09-25 00:27:32 +02:00
|
|
|
#include "simulation2/MessageTypes.h"
|
|
|
|
#include "simulation2/components/ICmpOwnership.h"
|
|
|
|
#include "simulation2/components/ICmpPlayerManager.h"
|
|
|
|
#include "simulation2/components/ICmpTechnologyManager.h"
|
|
|
|
|
2011-01-07 21:31:48 +01:00
|
|
|
class CCmpVision : public ICmpVision
|
2010-09-23 14:13:13 +02:00
|
|
|
{
|
|
|
|
public:
|
2012-09-25 00:27:32 +02:00
|
|
|
static void ClassInit(CComponentManager& componentManager)
|
2010-09-23 14:13:13 +02:00
|
|
|
{
|
2012-09-25 00:27:32 +02:00
|
|
|
componentManager.SubscribeGloballyToMessageType(MT_TechnologyModification);
|
2010-09-23 14:13:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_COMPONENT_ALLOCATOR(Vision)
|
|
|
|
|
2010-10-23 21:59:40 +02:00
|
|
|
// Template state:
|
|
|
|
|
2012-09-25 00:27:32 +02:00
|
|
|
entity_pos_t m_Range, m_BaseRange;
|
2010-09-23 14:13:13 +02:00
|
|
|
bool m_RetainInFog;
|
2010-10-04 19:34:33 +02:00
|
|
|
bool m_AlwaysVisible;
|
2010-09-23 14:13:13 +02:00
|
|
|
|
|
|
|
static std::string GetSchema()
|
|
|
|
{
|
|
|
|
return
|
|
|
|
"<element name='Range'>"
|
|
|
|
"<data type='nonNegativeInteger'/>"
|
|
|
|
"</element>"
|
|
|
|
"<element name='RetainInFog'>"
|
|
|
|
"<data type='boolean'/>"
|
2010-10-04 19:34:33 +02:00
|
|
|
"</element>"
|
|
|
|
"<element name='AlwaysVisible'>"
|
|
|
|
"<data type='boolean'/>"
|
2010-09-23 14:13:13 +02:00
|
|
|
"</element>";
|
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Init(const CParamNode& paramNode)
|
2010-09-23 14:13:13 +02:00
|
|
|
{
|
2012-09-25 00:27:32 +02:00
|
|
|
m_BaseRange = m_Range = paramNode.GetChild("Range").ToFixed();
|
2010-09-23 14:13:13 +02:00
|
|
|
m_RetainInFog = paramNode.GetChild("RetainInFog").ToBool();
|
2010-10-04 19:34:33 +02:00
|
|
|
m_AlwaysVisible = paramNode.GetChild("AlwaysVisible").ToBool();
|
2010-09-23 14:13:13 +02:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Deinit()
|
2010-09-23 14:13:13 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Serialize(ISerializer& UNUSED(serialize))
|
|
|
|
{
|
2010-10-23 21:59:40 +02:00
|
|
|
// No dynamic state to serialize
|
2010-09-23 14:13:13 +02:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize))
|
2010-09-23 14:13:13 +02:00
|
|
|
{
|
2011-01-16 15:08:38 +01:00
|
|
|
Init(paramNode);
|
2010-09-23 14:13:13 +02:00
|
|
|
}
|
|
|
|
|
2012-09-25 00:27:32 +02:00
|
|
|
virtual void HandleMessage(const CMessage& msg, bool UNUSED(global))
|
|
|
|
{
|
|
|
|
switch (msg.GetType())
|
|
|
|
{
|
|
|
|
case MT_TechnologyModification:
|
|
|
|
{
|
|
|
|
const CMessageTechnologyModification& msgData = static_cast<const CMessageTechnologyModification&> (msg);
|
|
|
|
if (msgData.component == L"Vision")
|
|
|
|
{
|
2013-09-11 22:41:53 +02:00
|
|
|
CmpPtr<ICmpOwnership> cmpOwnership(GetEntityHandle());
|
2012-09-25 00:27:32 +02:00
|
|
|
if (cmpOwnership)
|
|
|
|
{
|
|
|
|
player_id_t owner = cmpOwnership->GetOwner();
|
|
|
|
if (owner != INVALID_PLAYER && owner == msgData.player)
|
|
|
|
{
|
2013-09-11 22:41:53 +02:00
|
|
|
CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSystemEntity());
|
2012-09-25 00:27:32 +02:00
|
|
|
entity_id_t playerEnt = cmpPlayerManager->GetPlayerByID(owner);
|
|
|
|
CmpPtr<ICmpTechnologyManager> cmpTechnologyManager(GetSimContext(), playerEnt);
|
|
|
|
if (playerEnt != INVALID_ENTITY && cmpTechnologyManager)
|
|
|
|
{
|
|
|
|
entity_pos_t newRange = cmpTechnologyManager->ApplyModifications(L"Vision/Range", m_BaseRange, GetEntityId());
|
|
|
|
if (newRange != m_Range)
|
|
|
|
{
|
|
|
|
// Update our vision range and broadcast message
|
|
|
|
entity_pos_t oldRange = m_Range;
|
|
|
|
m_Range = newRange;
|
|
|
|
CMessageVisionRangeChanged msg(GetEntityId(), oldRange, newRange);
|
|
|
|
GetSimContext().GetComponentManager().BroadcastMessage(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-23 14:13:13 +02:00
|
|
|
virtual entity_pos_t GetRange()
|
|
|
|
{
|
|
|
|
return m_Range;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool GetRetainInFog()
|
|
|
|
{
|
|
|
|
return m_RetainInFog;
|
|
|
|
}
|
2010-10-04 19:34:33 +02:00
|
|
|
|
|
|
|
virtual bool GetAlwaysVisible()
|
|
|
|
{
|
|
|
|
return m_AlwaysVisible;
|
|
|
|
}
|
2010-09-23 14:13:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_COMPONENT_TYPE(Vision)
|