Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "ICmpTerrain.h" 21 : 22 : #include "graphics/Terrain.h" 23 : #include "lib/debug.h" 24 : #include "lib/posix/posix_types.h" 25 : #include "maths/Fixed.h" 26 : #include "maths/FixedVector3D.h" 27 : #include "maths/Vector3D.h" 28 : #include "renderer/Renderer.h" 29 : #include "renderer/SceneRenderer.h" 30 : #include "renderer/WaterManager.h" 31 : #include "simulation2/MessageTypes.h" 32 : #include "simulation2/components/ICmpObstructionManager.h" 33 : #include "simulation2/components/ICmpRangeManager.h" 34 : #include "simulation2/helpers/Position.h" 35 : #include "simulation2/system/Component.h" 36 : 37 : #include <string> 38 : 39 7 : class CCmpTerrain final : public ICmpTerrain 40 : { 41 : public: 42 : static void ClassInit(CComponentManager&) 43 : { 44 : } 45 : 46 14 : DEFAULT_COMPONENT_ALLOCATOR(Terrain) 47 : 48 : CTerrain* m_Terrain; // not null 49 : 50 137 : static std::string GetSchema() 51 : { 52 274 : return "<a:component type='system'/><empty/>"; 53 : } 54 : 55 7 : void Init(const CParamNode&) override 56 : { 57 7 : m_Terrain = &GetSimContext().GetTerrain(); 58 7 : } 59 : 60 7 : void Deinit() override 61 : { 62 7 : } 63 : 64 18 : void Serialize(ISerializer&) override 65 : { 66 18 : } 67 : 68 0 : void Deserialize(const CParamNode& paramNode, IDeserializer&) override 69 : { 70 0 : Init(paramNode); 71 0 : } 72 : 73 3201 : bool IsLoaded() const override 74 : { 75 3201 : return m_Terrain->GetVerticesPerSide() != 0; 76 : } 77 : 78 0 : CFixedVector3D CalcNormal(entity_pos_t x, entity_pos_t z) const override 79 : { 80 0 : CFixedVector3D normal; 81 0 : m_Terrain->CalcNormalFixed((x / (int)TERRAIN_TILE_SIZE).ToInt_RoundToZero(), (z / (int)TERRAIN_TILE_SIZE).ToInt_RoundToZero(), normal); 82 0 : return normal; 83 : } 84 : 85 3201 : CVector3D CalcExactNormal(float x, float z) const override 86 : { 87 3201 : return m_Terrain->CalcExactNormal(x, z); 88 : } 89 : 90 3978 : entity_pos_t GetGroundLevel(entity_pos_t x, entity_pos_t z) const override 91 : { 92 : // TODO: this can crash if the terrain heightmap isn't initialised yet 93 : 94 3978 : return m_Terrain->GetExactGroundLevelFixed(x, z); 95 : } 96 : 97 14286 : float GetExactGroundLevel(float x, float z) const override 98 : { 99 14286 : return m_Terrain->GetExactGroundLevel(x, z); 100 : } 101 : 102 207 : std::uint16_t GetTilesPerSide() const override 103 : { 104 207 : ssize_t tiles = m_Terrain->GetTilesPerSide(); 105 : 106 207 : if (tiles == -1) 107 : return 0; 108 207 : ENSURE(1 <= tiles && tiles <= 65535); 109 207 : return static_cast<std::uint16_t>(tiles); 110 : } 111 : 112 204 : std::uint32_t GetMapSize() const override 113 : { 114 204 : return GetTilesPerSide() * TERRAIN_TILE_SIZE; 115 : } 116 : 117 6 : std::uint16_t GetVerticesPerSide() const override 118 : { 119 6 : ssize_t vertices = m_Terrain->GetVerticesPerSide(); 120 6 : ENSURE(1 <= vertices && vertices <= 65535); 121 6 : return static_cast<std::uint16_t>(vertices); 122 : } 123 : 124 0 : CTerrain* GetCTerrain() override 125 : { 126 0 : return m_Terrain; 127 : } 128 : 129 3 : void ReloadTerrain(bool ReloadWater) override 130 : { 131 : // TODO: should refactor this code to be nicer 132 : 133 3 : std::uint16_t tiles = GetTilesPerSide(); 134 3 : std::uint16_t vertices = GetVerticesPerSide(); 135 : 136 3 : CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSystemEntity()); 137 3 : if (cmpObstructionManager) 138 : { 139 3 : cmpObstructionManager->SetBounds(entity_pos_t::Zero(), entity_pos_t::Zero(), 140 : entity_pos_t::FromInt(tiles*(int)TERRAIN_TILE_SIZE), 141 3 : entity_pos_t::FromInt(tiles*(int)TERRAIN_TILE_SIZE)); 142 : } 143 : 144 3 : CmpPtr<ICmpRangeManager> cmpRangeManager(GetSystemEntity()); 145 3 : if (cmpRangeManager) 146 : { 147 3 : cmpRangeManager->SetBounds(entity_pos_t::Zero(), entity_pos_t::Zero(), 148 : entity_pos_t::FromInt(tiles*(int)TERRAIN_TILE_SIZE), 149 3 : entity_pos_t::FromInt(tiles*(int)TERRAIN_TILE_SIZE)); 150 : } 151 : 152 3 : if (ReloadWater && CRenderer::IsInitialised()) 153 : { 154 0 : g_Renderer.GetSceneRenderer().GetWaterManager().SetMapSize(vertices); 155 0 : g_Renderer.GetSceneRenderer().GetWaterManager().RecomputeWaterData(); 156 : } 157 3 : MakeDirty(0, 0, tiles+1, tiles+1); 158 3 : } 159 : 160 3 : void MakeDirty(std::int32_t i0, std::int32_t j0, std::int32_t i1, std::int32_t j1) override 161 : { 162 3 : CMessageTerrainChanged msg(i0, j0, i1, j1); 163 3 : GetSimContext().GetComponentManager().BroadcastMessage(msg); 164 3 : } 165 : }; 166 : 167 274 : REGISTER_COMPONENT_TYPE(Terrain)