2010-01-09 20:20:14 +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 "ICmpTerrain.h"
|
|
|
|
|
2010-07-25 16:11:21 +02:00
|
|
|
#include "ICmpObstructionManager.h"
|
2010-09-19 20:08:56 +02:00
|
|
|
#include "ICmpRangeManager.h"
|
2010-05-28 01:31:03 +02:00
|
|
|
#include "simulation2/MessageTypes.h"
|
|
|
|
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "graphics/Terrain.h"
|
|
|
|
|
|
|
|
class CCmpTerrain : public ICmpTerrain
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void ClassInit(CComponentManager& UNUSED(componentManager))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_COMPONENT_ALLOCATOR(Terrain)
|
|
|
|
|
|
|
|
CTerrain* m_Terrain; // not null
|
|
|
|
|
2010-04-23 18:09:03 +02:00
|
|
|
static std::string GetSchema()
|
|
|
|
{
|
|
|
|
return "<a:component type='system'/><empty/>";
|
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Init(const CParamNode& UNUSED(paramNode))
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2011-01-16 15:08:38 +01:00
|
|
|
m_Terrain = &GetSimContext().GetTerrain();
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Deinit()
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Serialize(ISerializer& UNUSED(serialize))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-16 15:08:38 +01:00
|
|
|
virtual void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize))
|
2010-01-09 20:20:14 +01:00
|
|
|
{
|
2011-01-16 15:08:38 +01:00
|
|
|
Init(paramNode);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual CFixedVector3D CalcNormal(entity_pos_t x, entity_pos_t z)
|
|
|
|
{
|
|
|
|
CFixedVector3D normal;
|
2010-04-30 01:36:05 +02:00
|
|
|
m_Terrain->CalcNormalFixed((x / (int)CELL_SIZE).ToInt_RoundToZero(), (z / (int)CELL_SIZE).ToInt_RoundToZero(), normal);
|
2010-01-09 20:20:14 +01:00
|
|
|
return normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual entity_pos_t GetGroundLevel(entity_pos_t x, entity_pos_t z)
|
|
|
|
{
|
2011-01-16 00:35:20 +01:00
|
|
|
// TODO: this can crash if the terrain heightmap isn't initialised yet
|
|
|
|
|
2010-04-06 19:31:38 +02:00
|
|
|
return m_Terrain->GetExactGroundLevelFixed(x, z);
|
2010-01-09 20:20:14 +01:00
|
|
|
}
|
2010-03-18 00:13:21 +01:00
|
|
|
|
2010-05-28 01:23:53 +02:00
|
|
|
virtual float GetExactGroundLevel(float x, float z)
|
2010-03-18 00:13:21 +01:00
|
|
|
{
|
|
|
|
return m_Terrain->GetExactGroundLevel(x, z);
|
|
|
|
}
|
2010-05-28 01:31:03 +02:00
|
|
|
|
2011-04-05 22:32:03 +02:00
|
|
|
virtual uint32_t GetVerticesPerSide()
|
|
|
|
{
|
|
|
|
return m_Terrain->GetVerticesPerSide();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual CTerrain* GetCTerrain()
|
|
|
|
{
|
|
|
|
return m_Terrain;
|
|
|
|
}
|
|
|
|
|
2010-07-25 16:11:21 +02:00
|
|
|
virtual void ReloadTerrain()
|
|
|
|
{
|
2010-09-19 20:08:56 +02:00
|
|
|
// TODO: should refactor this code to be nicer
|
2010-07-25 16:11:21 +02:00
|
|
|
|
2010-09-19 20:08:56 +02:00
|
|
|
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSimContext(), SYSTEM_ENTITY);
|
|
|
|
if (!cmpObstructionManager.null())
|
|
|
|
{
|
|
|
|
cmpObstructionManager->SetBounds(entity_pos_t::Zero(), entity_pos_t::Zero(),
|
|
|
|
entity_pos_t::FromInt(m_Terrain->GetTilesPerSide()*CELL_SIZE),
|
|
|
|
entity_pos_t::FromInt(m_Terrain->GetTilesPerSide()*CELL_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
CmpPtr<ICmpRangeManager> cmpRangeManager(GetSimContext(), SYSTEM_ENTITY);
|
|
|
|
if (!cmpRangeManager.null())
|
|
|
|
{
|
|
|
|
cmpRangeManager->SetBounds(entity_pos_t::Zero(), entity_pos_t::Zero(),
|
|
|
|
entity_pos_t::FromInt(m_Terrain->GetTilesPerSide()*CELL_SIZE),
|
2010-09-23 14:13:13 +02:00
|
|
|
entity_pos_t::FromInt(m_Terrain->GetTilesPerSide()*CELL_SIZE),
|
|
|
|
m_Terrain->GetVerticesPerSide());
|
2010-09-19 20:08:56 +02:00
|
|
|
}
|
2011-05-29 17:02:02 +02:00
|
|
|
|
|
|
|
MakeDirty(0, 0, m_Terrain->GetTilesPerSide()+1, m_Terrain->GetTilesPerSide()+1);
|
2010-07-25 16:11:21 +02:00
|
|
|
}
|
|
|
|
|
2010-05-28 01:31:03 +02:00
|
|
|
virtual void MakeDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1)
|
|
|
|
{
|
|
|
|
CMessageTerrainChanged msg(i0, j0, i1, j1);
|
|
|
|
GetSimContext().GetComponentManager().PostMessage(GetEntityId(), msg);
|
|
|
|
}
|
2010-01-09 20:20:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_COMPONENT_TYPE(Terrain)
|