Index: source/graphics/Terrain.h =================================================================== --- source/graphics/Terrain.h +++ source/graphics/Terrain.h @@ -101,6 +101,8 @@ // resize this terrain such that each side has given number of patches void Resize(ssize_t size); + void ChangeHeight(ssize_t x, ssize_t z, u16 height); + // set up a new heightmap from 16 bit data; assumes heightmap matches current terrain size void SetHeightMap(u16* heightmap); // return a pointer to the heightmap Index: source/graphics/Terrain.cpp =================================================================== --- source/graphics/Terrain.cpp +++ source/graphics/Terrain.cpp @@ -608,6 +608,19 @@ m_HeightMipmap.Initialize(m_MapSize,m_Heightmap); } +void CTerrain::ChangeHeight(ssize_t x, ssize_t z, u16 height) +{ + x = clamp((ssize_t)floor(x / (int)TERRAIN_TILE_SIZE), (ssize_t)0, m_MapSize-2); + z = clamp((ssize_t)floor(z / (int)TERRAIN_TILE_SIZE), (ssize_t)0, m_MapSize-2); + + u16 heightValue = height/HEIGHT_SCALE; + // Maybe support for changing verteces height instead of the whole tile would be useful + m_Heightmap[z * m_MapSize + x] = heightValue; + m_Heightmap[(z+1) * m_MapSize + x] = heightValue; + m_Heightmap[z * m_MapSize + (x+1)] = heightValue; + m_Heightmap[(z+1) * m_MapSize + (x+1)] = heightValue; +} + /////////////////////////////////////////////////////////////////////////////// // InitialisePatches: initialise patch data void CTerrain::InitialisePatches() Index: source/simulation2/components/CCmpTerrain.cpp =================================================================== --- source/simulation2/components/CCmpTerrain.cpp +++ source/simulation2/components/CCmpTerrain.cpp @@ -18,6 +18,7 @@ #include "precompiled.h" #include "simulation2/system/Component.h" +#include "simulation2/serialization/SerializeTemplates.h" #include "ICmpTerrain.h" #include "ICmpObstructionManager.h" @@ -25,6 +26,7 @@ #include "simulation2/MessageTypes.h" #include "graphics/Terrain.h" +#include "graphics/RenderableObject.h" #include "renderer/Renderer.h" #include "renderer/WaterManager.h" #include "maths/Vector3D.h" @@ -54,13 +56,29 @@ { } - virtual void Serialize(ISerializer& UNUSED(serialize)) + // serialize only heightmaps for now. + virtual void Serialize(ISerializer& serialize) { + u16* heightMap = m_Terrain->GetHeightMap(); + const ssize_t mapSizeSquared = pow(m_Terrain->GetVerticesPerSide(), 2); + + for (ssize_t i = 0; i < mapSizeSquared; ++i) + serialize.NumberU16_Unbounded("heightval", heightMap[i]); } - virtual void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize)) + virtual void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) { Init(paramNode); + + const ssize_t mapSizeSquared = pow(m_Terrain->GetVerticesPerSide(), 2); + u16* newHeightMap = new u16[mapSizeSquared]; + + for (ssize_t i = 0; i < mapSizeSquared; ++i) + deserialize.NumberU16_Unbounded("heightval", newHeightMap[i]); + + m_Terrain->SetHeightMap(newHeightMap); + + delete[] newHeightMap; } virtual bool IsLoaded() const @@ -92,6 +110,15 @@ return m_Terrain->GetExactGroundLevel(x, z); } + virtual void ChangeHeight(int x, int z, u16 height) + { + m_Terrain->ChangeHeight((ssize_t) x, (ssize_t) z, height); + + m_Terrain->MakeDirty(x-1, z-1, x+2, z+2, RENDERDATA_UPDATE_VERTICES); + + MakeDirty(x-1, z-1, x+1, z+1); + } + virtual u16 GetTilesPerSide() const { ssize_t tiles = m_Terrain->GetTilesPerSide(); Index: source/simulation2/components/ICmpTerrain.h =================================================================== --- source/simulation2/components/ICmpTerrain.h +++ source/simulation2/components/ICmpTerrain.h @@ -57,6 +57,8 @@ */ virtual u32 GetMapSize() const = 0; + virtual void ChangeHeight(int x, int z, u16 height) = 0; + virtual CTerrain* GetCTerrain() = 0; /** Index: source/simulation2/components/ICmpTerrain.cpp =================================================================== --- source/simulation2/components/ICmpTerrain.cpp +++ source/simulation2/components/ICmpTerrain.cpp @@ -22,6 +22,7 @@ #include "simulation2/system/InterfaceScripted.h" BEGIN_INTERFACE_WRAPPER(Terrain) +DEFINE_INTERFACE_METHOD_3("ChangeHeight", void, ICmpTerrain, ChangeHeight, int, int, u16) DEFINE_INTERFACE_METHOD_CONST_2("GetGroundLevel", entity_pos_t, ICmpTerrain, GetGroundLevel, entity_pos_t, entity_pos_t) DEFINE_INTERFACE_METHOD_CONST_2("CalcNormal", CFixedVector3D, ICmpTerrain, CalcNormal, entity_pos_t, entity_pos_t) DEFINE_INTERFACE_METHOD_CONST_0("GetTilesPerSide", u16, ICmpTerrain, GetTilesPerSide) Index: source/simulation2/system/ComponentTest.h =================================================================== --- source/simulation2/system/ComponentTest.h +++ source/simulation2/system/ComponentTest.h @@ -236,6 +236,10 @@ return NULL; } + virtual void ChangeHeight(int UNUSED(x), int UNUSED(z), u16 UNUSED(height)) + { + } + virtual void MakeDirty(i32 UNUSED(i0), i32 UNUSED(j0), i32 UNUSED(i1), i32 UNUSED(j1)) { }