2025-06-03 23:10:15 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2010-05-27 16:23:53 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2010-05-27 16:23:53 -07:00
|
|
|
* 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.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2010-05-27 16:23:53 -07:00
|
|
|
* 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
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2010-05-27 16:23:53 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "ICmpWaterManager.h"
|
|
|
|
|
|
2011-02-03 06:36:54 -08:00
|
|
|
#include "graphics/RenderableObject.h"
|
|
|
|
|
#include "graphics/Terrain.h"
|
2010-05-27 16:23:53 -07:00
|
|
|
#include "renderer/Renderer.h"
|
2022-01-04 05:29:01 -08:00
|
|
|
#include "renderer/SceneRenderer.h"
|
2010-05-27 16:23:53 -07:00
|
|
|
#include "renderer/WaterManager.h"
|
2012-10-15 12:36:04 -07:00
|
|
|
#include "simulation2/MessageTypes.h"
|
2025-08-02 12:24:35 -07:00
|
|
|
#include "simulation2/helpers/Position.h"
|
|
|
|
|
#include "simulation2/system/Component.h"
|
|
|
|
|
#include "simulation2/system/Message.h"
|
|
|
|
|
|
|
|
|
|
#include <string>
|
2014-01-05 08:15:20 -08:00
|
|
|
|
2022-03-03 14:42:26 -08:00
|
|
|
class CCmpWaterManager final : public ICmpWaterManager
|
2010-05-27 16:23:53 -07:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static void ClassInit(CComponentManager& componentManager)
|
|
|
|
|
{
|
2014-07-01 09:05:05 -07:00
|
|
|
// No need to subscribe to WaterChanged since we're actually the one sending those.
|
2012-10-15 12:36:04 -07:00
|
|
|
componentManager.SubscribeToMessageType(MT_Interpolate);
|
2012-11-05 04:14:04 -08:00
|
|
|
componentManager.SubscribeToMessageType(MT_TerrainChanged);
|
2010-05-27 16:23:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DEFAULT_COMPONENT_ALLOCATOR(WaterManager)
|
|
|
|
|
|
2010-10-23 12:59:40 -07:00
|
|
|
// Dynamic state:
|
|
|
|
|
|
2010-05-27 16:23:53 -07:00
|
|
|
entity_pos_t m_WaterHeight;
|
|
|
|
|
|
|
|
|
|
static std::string GetSchema()
|
|
|
|
|
{
|
|
|
|
|
return "<a:component type='system'/><empty/>";
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-03 23:10:15 -07:00
|
|
|
void Init(const CParamNode&) override
|
2010-05-27 16:23:53 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Deinit() override
|
2010-05-27 16:23:53 -07:00
|
|
|
{
|
2021-01-22 00:14:27 -08:00
|
|
|
// Clear the map size & data.
|
|
|
|
|
if (CRenderer::IsInitialised())
|
2022-01-04 05:29:01 -08:00
|
|
|
g_Renderer.GetSceneRenderer().GetWaterManager().SetMapSize(0);
|
2010-05-27 16:23:53 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Serialize(ISerializer& serialize) override
|
2010-05-27 16:23:53 -07:00
|
|
|
{
|
2010-10-23 12:59:40 -07:00
|
|
|
serialize.NumberFixed_Unbounded("height", m_WaterHeight);
|
2010-05-27 16:23:53 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Deserialize(const CParamNode& paramNode, IDeserializer& deserialize) override
|
2010-05-27 16:23:53 -07:00
|
|
|
{
|
2011-01-16 06:08:38 -08:00
|
|
|
Init(paramNode);
|
2010-10-23 12:59:40 -07:00
|
|
|
|
|
|
|
|
deserialize.NumberFixed_Unbounded("height", m_WaterHeight);
|
2017-06-30 21:15:49 -07:00
|
|
|
|
2021-01-23 13:13:26 -08:00
|
|
|
if (CRenderer::IsInitialised())
|
2022-01-04 05:29:01 -08:00
|
|
|
g_Renderer.GetSceneRenderer().GetWaterManager().SetMapSize(GetSimContext().GetTerrain().GetVerticesPerSide());
|
2021-01-23 13:13:26 -08:00
|
|
|
|
2017-06-30 21:15:49 -07:00
|
|
|
RecomputeWaterData();
|
2010-05-27 16:23:53 -07:00
|
|
|
}
|
|
|
|
|
|
2025-06-03 05:13:41 -07:00
|
|
|
void HandleMessage(const CMessage& msg, bool /*global*/) override
|
2010-05-27 16:23:53 -07:00
|
|
|
{
|
|
|
|
|
switch (msg.GetType())
|
|
|
|
|
{
|
2013-04-27 05:20:42 -07:00
|
|
|
case MT_Interpolate:
|
|
|
|
|
{
|
|
|
|
|
const CMessageInterpolate& msgData = static_cast<const CMessageInterpolate&> (msg);
|
|
|
|
|
if (CRenderer::IsInitialised())
|
2022-01-04 05:29:01 -08:00
|
|
|
g_Renderer.GetSceneRenderer().GetWaterManager().m_WaterTexTimer += msgData.deltaSimTime;
|
2013-04-27 05:20:42 -07:00
|
|
|
break;
|
|
|
|
|
}
|
2012-11-05 04:14:04 -08:00
|
|
|
case MT_TerrainChanged:
|
|
|
|
|
{
|
2014-02-24 22:27:15 -08:00
|
|
|
// Tell the renderer to redraw part of the map.
|
|
|
|
|
if (CRenderer::IsInitialised())
|
2012-11-05 04:14:04 -08:00
|
|
|
{
|
2013-08-18 02:27:11 -07:00
|
|
|
const CMessageTerrainChanged& msgData = static_cast<const CMessageTerrainChanged&> (msg);
|
|
|
|
|
GetSimContext().GetTerrain().MakeDirty(msgData.i0,msgData.j0,msgData.i1,msgData.j1,RENDERDATA_UPDATE_VERTICES);
|
2012-11-05 04:14:04 -08:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-05-27 16:23:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void RecomputeWaterData() override
|
2014-01-05 08:15:20 -08:00
|
|
|
{
|
2014-07-04 02:03:21 -07:00
|
|
|
if (CRenderer::IsInitialised())
|
|
|
|
|
{
|
2022-01-04 05:29:01 -08:00
|
|
|
g_Renderer.GetSceneRenderer().GetWaterManager().RecomputeWaterData();
|
|
|
|
|
g_Renderer.GetSceneRenderer().GetWaterManager().m_WaterHeight = m_WaterHeight.ToFloat();
|
2014-07-04 02:03:21 -07:00
|
|
|
}
|
2015-07-14 10:08:02 -07:00
|
|
|
|
2014-01-05 08:15:20 -08:00
|
|
|
// Tell the terrain it'll need to recompute its cached render data
|
|
|
|
|
GetSimContext().GetTerrain().MakeDirty(RENDERDATA_UPDATE_VERTICES);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void SetWaterLevel(entity_pos_t h) override
|
2010-05-27 16:23:53 -07:00
|
|
|
{
|
2015-07-14 10:08:02 -07:00
|
|
|
if (m_WaterHeight == h)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-05-27 16:23:53 -07:00
|
|
|
m_WaterHeight = h;
|
2011-02-03 06:36:54 -08:00
|
|
|
|
2017-06-30 21:15:49 -07:00
|
|
|
RecomputeWaterData();
|
2015-07-14 10:08:02 -07:00
|
|
|
|
2014-05-26 06:44:24 -07:00
|
|
|
CMessageWaterChanged msg;
|
|
|
|
|
GetSimContext().GetComponentManager().BroadcastMessage(msg);
|
2010-05-27 16:23:53 -07:00
|
|
|
}
|
|
|
|
|
|
2025-06-03 05:13:41 -07:00
|
|
|
entity_pos_t GetWaterLevel(entity_pos_t /*x*/, entity_pos_t /*z*/) const override
|
2010-05-27 16:23:53 -07:00
|
|
|
{
|
|
|
|
|
return m_WaterHeight;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-03 05:13:41 -07:00
|
|
|
float GetExactWaterLevel(float /*x*/, float /*z*/) const override
|
2010-05-27 16:23:53 -07:00
|
|
|
{
|
|
|
|
|
return m_WaterHeight.ToFloat();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
REGISTER_COMPONENT_TYPE(WaterManager)
|