Index: source/graphics/Decal.h =================================================================== --- source/graphics/Decal.h +++ source/graphics/Decal.h @@ -22,6 +22,7 @@ #include "graphics/ModelAbstract.h" class CTerrain; +class CWater; /** * Terrain decal definition. @@ -49,8 +50,8 @@ class CModelDecal : public CModelAbstract { public: - CModelDecal(CTerrain* terrain, const SDecal& decal) - : m_Terrain(terrain), m_Decal(decal) + CModelDecal(CTerrain* terrain, CWater* water, const SDecal& decal) + : m_Terrain(terrain), m_Water(water), m_Decal(decal) { ENSURE(terrain != NULL); } @@ -81,6 +82,7 @@ void CalcVertexExtents(ssize_t& i0, ssize_t& j0, ssize_t& i1, ssize_t& j1); CTerrain* m_Terrain; + CWater* m_Water; SDecal m_Decal; }; Index: source/graphics/Decal.cpp =================================================================== --- source/graphics/Decal.cpp +++ source/graphics/Decal.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -25,7 +25,7 @@ CModelAbstract* CModelDecal::Clone() const { - CModelDecal* clone = new CModelDecal(m_Terrain, m_Decal); + CModelDecal* clone = new CModelDecal(m_Terrain, m_Water, m_Decal); return clone; } @@ -56,7 +56,7 @@ { ssize_t i0, j0, i1, j1; CalcVertexExtents(i0, j0, i1, j1); - m_WorldBounds = m_Terrain->GetVertexesBound(i0, j0, i1, j1); + m_WorldBounds = m_Terrain->GetVertexesBound(m_Water, i0, j0, i1, j1); } void CModelDecal::SetTerrainDirty(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1) Index: source/graphics/GameView.cpp =================================================================== --- source/graphics/GameView.cpp +++ source/graphics/GameView.cpp @@ -71,7 +71,7 @@ CGameViewImpl(CGame* game) : Game(game), ColladaManager(g_VFS), MeshManager(ColladaManager), SkeletonAnimManager(ColladaManager), - ObjectManager(MeshManager, SkeletonAnimManager, *game->GetSimulation2()), + ObjectManager(MeshManager, SkeletonAnimManager, *game->GetWorld(), *game->GetSimulation2()), LOSTexture(*game->GetSimulation2()), TerritoryTexture(*game->GetSimulation2()), MiniMapTexture(*game->GetSimulation2()), Index: source/graphics/Model.h =================================================================== --- source/graphics/Model.h +++ source/graphics/Model.h @@ -32,7 +32,8 @@ class CObjectEntry; class CSkeletonAnim; class CSkeletonAnimDef; -class CSimulation2; +class CTerrain; +class CWater; #define MODELFLAG_CASTSHADOWS (1<<0) #define MODELFLAG_NOLOOPANIMATION (1<<1) @@ -76,7 +77,7 @@ public: // constructor - CModel(CSimulation2& simulation); + CModel(CTerrain& terrain, CWater& water); // destructor ~CModel(); @@ -236,7 +237,8 @@ void ReleaseData(); // Needed for terrain aligned props - CSimulation2& m_Simulation; + CTerrain& m_Terrain; + CWater& m_Water; // object flags int m_Flags; Index: source/graphics/Model.cpp =================================================================== --- source/graphics/Model.cpp +++ source/graphics/Model.cpp @@ -25,6 +25,8 @@ #include "graphics/ObjectEntry.h" #include "graphics/SkeletonAnim.h" #include "graphics/SkeletonAnimDef.h" +#include "graphics/Terrain.h" +#include "graphics/Water.h" #include "maths/BoundingBoxAligned.h" #include "maths/Quaternion.h" #include "lib/sysdep/rtl.h" @@ -32,15 +34,13 @@ #include "ps/CStrInternStatic.h" #include "ps/Profile.h" #include "renderer/RenderingOptions.h" -#include "simulation2/components/ICmpTerrain.h" -#include "simulation2/components/ICmpWaterManager.h" #include "simulation2/Simulation2.h" ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // Constructor -CModel::CModel(CSimulation2& simulation) - : m_Flags(0), m_Anim(NULL), m_AnimTime(0), m_Simulation(simulation), +CModel::CModel(CTerrain& terrain, CWater& water) + : m_Flags(0), m_Anim(NULL), m_AnimTime(0), m_Terrain(terrain), m_Water(water), m_BoneMatrices(NULL), m_AmmoPropPoint(NULL), m_AmmoLoadedProp(0) { } @@ -292,18 +292,12 @@ CVector3D objTranslation = m_Transform.GetTranslation(); float objectHeight = 0.0f; - CmpPtr cmpTerrain(m_Simulation, SYSTEM_ENTITY); - if (cmpTerrain) - objectHeight = cmpTerrain->GetExactGroundLevel(objTranslation.X, objTranslation.Z); + objectHeight = m_Terrain.GetExactGroundLevel(objTranslation.X, objTranslation.Z); // Object height is incorrect for floating objects. We use water height instead. - CmpPtr cmpWaterManager(m_Simulation, SYSTEM_ENTITY); - if (cmpWaterManager) - { - float waterHeight = cmpWaterManager->GetExactWaterLevel(objTranslation.X, objTranslation.Z); - if (waterHeight >= objectHeight && m_Flags & MODELFLAG_FLOATONWATER) - objectHeight = waterHeight; - } + float waterHeight = m_Water.GetExactWaterHeight(objTranslation.X, objTranslation.Z); + if (waterHeight >= objectHeight && m_Flags & MODELFLAG_FLOATONWATER) + objectHeight = waterHeight; // re-position and validate all props for (const Prop& prop : m_Props) @@ -323,10 +317,10 @@ } // Adjust prop height to terrain level when needed - if (cmpTerrain && (prop.m_MaxHeight != 0.f || prop.m_MinHeight != 0.f)) + if (prop.m_MaxHeight != 0.f || prop.m_MinHeight != 0.f) { const CVector3D& propTranslation = proptransform.GetTranslation(); - const float propTerrain = cmpTerrain->GetExactGroundLevel(propTranslation.X, propTranslation.Z); + const float propTerrain = m_Terrain.GetExactGroundLevel(propTranslation.X, propTranslation.Z); const float translateHeight = std::min(prop.m_MaxHeight, std::max(prop.m_MinHeight, propTerrain - objectHeight)); translate.SetTranslation(0.f, translateHeight, 0.f); proptransform.Concatenate(translate); @@ -494,7 +488,7 @@ // Clone: return a clone of this model CModelAbstract* CModel::Clone() const { - CModel* clone = new CModel(m_Simulation); + CModel* clone = new CModel(m_Terrain, m_Water); clone->m_ObjectBounds = m_ObjectBounds; clone->InitModel(m_pModelDef); clone->SetMaterial(m_Material); Index: source/graphics/ObjectEntry.h =================================================================== --- source/graphics/ObjectEntry.h +++ source/graphics/ObjectEntry.h @@ -22,7 +22,6 @@ class CSkeletonAnim; class CObjectBase; class CObjectManager; -class CSimulation2; #include #include @@ -40,7 +39,7 @@ NONCOPYABLE(CObjectEntry); public: - CObjectEntry(const std::shared_ptr& base, CSimulation2& simulation); + CObjectEntry(const std::shared_ptr& base); ~CObjectEntry(); // Construct this actor, using the specified variation selections @@ -82,8 +81,6 @@ private: - CSimulation2& m_Simulation; - using SkeletonAnimMap = std::multimap>; SkeletonAnimMap m_Animations; // TODO: something more memory-efficient than storing loads of similar strings for each unit? Index: source/graphics/ObjectEntry.cpp =================================================================== --- source/graphics/ObjectEntry.cpp +++ source/graphics/ObjectEntry.cpp @@ -38,12 +38,11 @@ #include "ps/World.h" #include "renderer/Renderer.h" #include "renderer/SceneRenderer.h" -#include "simulation2/Simulation2.h" #include -CObjectEntry::CObjectEntry(const std::shared_ptr& base, CSimulation2& simulation) : - m_Base(base), m_Color(1.0f, 1.0f, 1.0f, 1.0f), m_Model(NULL), m_Simulation(simulation) +CObjectEntry::CObjectEntry(const std::shared_ptr& base) : + m_Base(base), m_Color(1.0f, 1.0f, 1.0f, 1.0f), m_Model(NULL) { } @@ -95,7 +94,7 @@ variation.decal.m_SizeX, variation.decal.m_SizeZ, variation.decal.m_Angle, variation.decal.m_OffsetX, variation.decal.m_OffsetZ, m_Base->m_Properties.m_FloatOnWater); - m_Model = new CModelDecal(objectManager.GetTerrain(), decal); + m_Model = new CModelDecal(objectManager.GetTerrain(), objectManager.GetWater(), decal); return true; } @@ -128,7 +127,7 @@ } // delete old model, create new - CModel* model = new CModel(m_Simulation); + CModel* model = new CModel(*objectManager.GetTerrain(), *objectManager.GetWater()); delete m_Model; m_Model = model; model->SetMaterial(g_Renderer.GetSceneRenderer().GetMaterialManager().LoadMaterial(m_Base->m_Material)); Index: source/graphics/ObjectManager.h =================================================================== --- source/graphics/ObjectManager.h +++ source/graphics/ObjectManager.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -35,6 +35,8 @@ class CSkeletonAnimManager; class CSimulation2; class CTerrain; +class CWater; +class CWorld; /////////////////////////////////////////////////////////////////////////////////////////// // CObjectManager: manager class for all possible actor types @@ -68,7 +70,7 @@ public: // constructor, destructor - CObjectManager(CMeshManager& meshManager, CSkeletonAnimManager& skeletonAnimManager, CSimulation2& simulation); + CObjectManager(CMeshManager& meshManager, CSkeletonAnimManager& skeletonAnimManager, CWorld& world, CSimulation2& simulation); ~CObjectManager(); // Provide access to the manager classes for meshes and animations - they're @@ -107,6 +109,12 @@ */ CTerrain* GetTerrain(); + /** + * Get the water object that actors managed by this manager should be linked + * with (primarily for the purpose of decals & floating units) + */ + CWater* GetWater(); + VariantDiversity GetVariantDiversity() const; /** @@ -128,6 +136,7 @@ CMeshManager& m_MeshManager; CSkeletonAnimManager& m_SkeletonAnimManager; CSimulation2& m_Simulation; + CWorld& m_World; u8 m_QualityLevel = 100; std::unique_ptr m_QualityHook; Index: source/graphics/ObjectManager.cpp =================================================================== --- source/graphics/ObjectManager.cpp +++ source/graphics/ObjectManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -26,9 +26,9 @@ #include "ps/Game.h" #include "ps/Profile.h" #include "ps/Filesystem.h" +#include "ps/World.h" #include "ps/XML/Xeromyces.h" #include "simulation2/Simulation2.h" -#include "simulation2/components/ICmpTerrain.h" #include "simulation2/components/ICmpVisual.h" bool CObjectManager::ObjectKey::operator< (const CObjectManager::ObjectKey& a) const @@ -46,8 +46,8 @@ return static_cast(param)->ReloadChangedFile(path); } -CObjectManager::CObjectManager(CMeshManager& meshManager, CSkeletonAnimManager& skeletonAnimManager, CSimulation2& simulation) -: m_MeshManager(meshManager), m_SkeletonAnimManager(skeletonAnimManager), m_Simulation(simulation) +CObjectManager::CObjectManager(CMeshManager& meshManager, CSkeletonAnimManager& skeletonAnimManager, CWorld& world, CSimulation2& simulation) +: m_MeshManager(meshManager), m_SkeletonAnimManager(skeletonAnimManager), m_World(world), m_Simulation(simulation) { RegisterFileReloadFunc(ReloadChangedFileCB, this); @@ -124,7 +124,7 @@ // If it hasn't been loaded, load it now. - std::unique_ptr obj = std::make_unique(base, m_Simulation); + std::unique_ptr obj = std::make_unique(base); // TODO (for some efficiency): use the pre-calculated choices for this object, // which has already worked out what to do for props, instead of passing the @@ -138,10 +138,12 @@ CTerrain* CObjectManager::GetTerrain() { - CmpPtr cmpTerrain(m_Simulation, SYSTEM_ENTITY); - if (!cmpTerrain) - return NULL; - return cmpTerrain->GetCTerrain(); + return m_World.GetTerrain(); +} + +CWater* CObjectManager::GetWater() +{ + return m_World.GetWater(); } CObjectManager::VariantDiversity CObjectManager::GetVariantDiversity() const Index: source/graphics/Terrain.h =================================================================== --- source/graphics/Terrain.h +++ source/graphics/Terrain.h @@ -32,6 +32,7 @@ class CMiniPatch; class CFixedVector3D; class CBoundingBoxAligned; +class CWater; /////////////////////////////////////////////////////////////////////////////// // Terrain Constants: @@ -148,7 +149,7 @@ /** * Returns a 3D bounding box encompassing the given vertex range (inclusive) */ - CBoundingBoxAligned GetVertexesBound(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1); + CBoundingBoxAligned GetVertexesBound(CWater* water, ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1); // get the base color for the terrain (typically pure white - other colors // will interact badly with LOS - but used by the Actor Viewer tool) Index: source/graphics/Terrain.cpp =================================================================== --- source/graphics/Terrain.cpp +++ source/graphics/Terrain.cpp @@ -23,6 +23,7 @@ #include "graphics/TerrainProperties.h" #include "graphics/TerrainTextureEntry.h" #include "graphics/TerrainTextureManager.h" +#include "graphics/Water.h" #include "lib/sysdep/cpu.h" #include "maths/FixedVector3D.h" #include "maths/MathUtil.h" @@ -801,7 +802,7 @@ m_HeightMipmap.Update(m_Heightmap); } -CBoundingBoxAligned CTerrain::GetVertexesBound(ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1) +CBoundingBoxAligned CTerrain::GetVertexesBound(CWater* water, ssize_t i0, ssize_t j0, ssize_t i1, ssize_t j1) { i0 = Clamp(i0, 0, m_MapSize - 1); j0 = Clamp(j0, 0, m_MapSize - 1); @@ -820,6 +821,10 @@ } } + u16 waterHeight = water->GetWaterHeight() * HEIGHT_SCALE; + minH = std::min(waterHeight, minH); + maxH = std::max(waterHeight, maxH); + CBoundingBoxAligned bound; bound[0].X = (float)(i0*TERRAIN_TILE_SIZE); bound[0].Y = (float)(minH*HEIGHT_SCALE); Index: source/renderer/DecalRData.h =================================================================== --- source/renderer/DecalRData.h +++ source/renderer/DecalRData.h @@ -28,16 +28,15 @@ class CModelDecal; class CShaderDefines; -class CSimulation2; class ShadowMap; class CDecalRData : public CRenderData { public: - CDecalRData(CModelDecal* decal, CSimulation2* simulation); + CDecalRData(CModelDecal* decal); ~CDecalRData(); - void Update(CSimulation2* simulation); + void Update(); static void RenderDecals( Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext, @@ -60,8 +59,6 @@ CVertexBufferManager::Handle m_VBDecalsIndices; CModelDecal* m_Decal; - - CSimulation2* m_Simulation; }; #endif // INCLUDED_DECALRDATA Index: source/renderer/DecalRData.cpp =================================================================== --- source/renderer/DecalRData.cpp +++ source/renderer/DecalRData.cpp @@ -24,6 +24,7 @@ #include "graphics/ShaderManager.h" #include "graphics/Terrain.h" #include "graphics/TextureManager.h" +#include "graphics/Water.h" #include "lib/allocators/DynamicArena.h" #include "lib/allocators/STLAllocators.h" #include "ps/CLogger.h" @@ -32,8 +33,6 @@ #include "ps/Profile.h" #include "renderer/Renderer.h" #include "renderer/TerrainRenderer.h" -#include "simulation2/components/ICmpWaterManager.h" -#include "simulation2/Simulation2.h" #include @@ -69,17 +68,16 @@ } // anonymous namespace -CDecalRData::CDecalRData(CModelDecal* decal, CSimulation2* simulation) - : m_Decal(decal), m_Simulation(simulation) +CDecalRData::CDecalRData(CModelDecal* decal) + : m_Decal(decal) { BuildVertexData(); } CDecalRData::~CDecalRData() = default; -void CDecalRData::Update(CSimulation2* simulation) +void CDecalRData::Update() { - m_Simulation = simulation; if (m_UpdateFlags != 0) { BuildVertexData(); @@ -248,8 +246,6 @@ return; } - CmpPtr cmpWaterManager(*m_Simulation, SYSTEM_ENTITY); - std::vector vertices((i1 - i0 + 1) * (j1 - j0 + 1)); for (ssize_t j = j0, idx = 0; j <= j1; ++j) @@ -259,11 +255,11 @@ SDecalVertex& vertex = vertices[idx]; m_Decal->m_Terrain->CalcPosition(i, j, vertex.m_Position); - if (decal.m_Floating && cmpWaterManager) + if (decal.m_Floating && m_Decal->m_Water) { vertex.m_Position.Y = std::max( vertex.m_Position.Y, - cmpWaterManager->GetExactWaterLevel(vertex.m_Position.X, vertex.m_Position.Z)); + m_Decal->m_Water->GetExactWaterHeight(vertex.m_Position.X, vertex.m_Position.Z)); } m_Decal->m_Terrain->CalcNormal(i, j, vertex.m_Normal); Index: source/renderer/PatchRData.cpp =================================================================== --- source/renderer/PatchRData.cpp +++ source/renderer/PatchRData.cpp @@ -46,7 +46,6 @@ #include "renderer/SceneRenderer.h" #include "renderer/TerrainRenderer.h" #include "renderer/WaterRenderer.h" -#include "simulation2/components/ICmpWaterManager.h" #include "simulation2/Simulation2.h" #include @@ -542,7 +541,7 @@ { ssize_t vsize = PATCH_SIZE + 1; CTerrain* terrain = m_Patch->m_Parent; - CmpPtr cmpWaterManager(*m_Simulation, SYSTEM_ENTITY); + CWater* water = g_Game->GetWorld()->GetWater(); for (ssize_t k = 0; k < vsize; k++) { @@ -559,11 +558,13 @@ CVector3D pos; terrain->CalcPosition(gx, gz, pos); - // Clamp the height to the water level - float waterHeight = 0.f; - if (cmpWaterManager) - waterHeight = cmpWaterManager->GetExactWaterLevel(pos.X, pos.Z); - pos.Y = std::max(pos.Y, waterHeight); + // Clamp the height to the water level if water is enabled. + if (water) + { + float waterHeight = 0.f; + waterHeight = water->GetExactWaterHeight(pos.X, pos.Z); + pos.Y = std::max(pos.Y, waterHeight); + } SSideVertex v0, v1; v0.m_Position = pos; @@ -1208,12 +1209,6 @@ m_WaterBounds.SetEmpty(); - // We need to use this to access the water manager or we may not have the - // actual values but some compiled-in defaults - CmpPtr cmpWaterManager(*m_Simulation, SYSTEM_ENTITY); - if (!cmpWaterManager) - return; - // Build data for water std::vector water_vertex_data; std::vector water_indices; @@ -1226,10 +1221,10 @@ u16 water_shore_index_map[PATCH_SIZE+1][PATCH_SIZE+1]; memset(water_shore_index_map, 0xFF, sizeof(water_shore_index_map)); - WaterRData& waterRData = g_Game->GetWorld()->GetWater()->GetRData(); - CPatch* patch = m_Patch; CTerrain* terrain = patch->m_Parent; + CWater* water = g_Game->GetWorld()->GetWater(); + WaterRData& waterRData = water->GetRData(); ssize_t mapSize = terrain->GetVerticesPerSide(); @@ -1238,7 +1233,7 @@ ssize_t pz = m_Patch->m_Z * PATCH_SIZE; // To whoever implements different water heights, this is a TODO: water height) - float waterHeight = cmpWaterManager->GetExactWaterLevel(0.0f,0.0f); + float waterHeight = water->GetExactWaterHeight(0.0f,0.0f); const float* windStrength = waterRData.GetWindStrength(); Index: source/renderer/TerrainRenderer.cpp =================================================================== --- source/renderer/TerrainRenderer.cpp +++ source/renderer/TerrainRenderer.cpp @@ -128,10 +128,10 @@ if (data == 0) { // no renderdata for decal, create it now - data = new CDecalRData(decal, m->simulation); + data = new CDecalRData(decal); decal->SetRenderData(data); } - data->Update(m->simulation); + data->Update(); m->visibleDecals[cullGroup].push_back(data); } Index: source/renderer/TexturedLineRData.cpp =================================================================== --- source/renderer/TexturedLineRData.cpp +++ source/renderer/TexturedLineRData.cpp @@ -21,15 +21,15 @@ #include "graphics/ShaderProgram.h" #include "graphics/Terrain.h" +#include "graphics/Water.h" #include "maths/Frustum.h" #include "maths/MathUtil.h" #include "maths/Quaternion.h" #include "ps/CStrInternStatic.h" +#include "ps/World.h" #include "renderer/OverlayRenderer.h" #include "renderer/Renderer.h" -#include "simulation2/Simulation2.h" #include "simulation2/system/SimContext.h" -#include "simulation2/components/ICmpWaterManager.h" /* Note: this implementation uses g_VBMan directly rather than access it through the nicer VertexArray interface, * because it allows you to work with variable amounts of vertices and indices more easily. New code should prefer @@ -74,7 +74,7 @@ m_VBIndices.Reset(); m_VB.Reset(); - if (!line.m_SimContext) + if (!line.m_SimContext || !line.m_SimContext->GetWorld()) { debug_warn(L"[TexturedLineRData] No SimContext set for textured overlay line, cannot render (no terrain data)"); return; @@ -112,8 +112,7 @@ // each point was floating on water, for normal computation later) // TODO: if we ever support more than one water level per map, recompute this per point - CmpPtr cmpWaterManager(*line.m_SimContext, SYSTEM_ENTITY); - float w = cmpWaterManager ? cmpWaterManager->GetExactWaterLevel(p0.X, p0.Z) : 0.f; + float w = line.m_SimContext->GetWorld()->GetWater()->GetExactWaterHeight(p0.X, p0.Z); const CTerrain& terrain = line.m_SimContext->GetTerrain(); Index: source/tools/atlas/GameInterface/ActorViewer.cpp =================================================================== --- source/tools/atlas/GameInterface/ActorViewer.cpp +++ source/tools/atlas/GameInterface/ActorViewer.cpp @@ -73,7 +73,7 @@ MeshManager(ColladaManager), SkeletonAnimManager(ColladaManager), Simulation2(g_ScriptContext, &World), - ObjectManager(MeshManager, SkeletonAnimManager, Simulation2), + ObjectManager(MeshManager, SkeletonAnimManager, World, Simulation2), LOSTexture(Simulation2), TerritoryTexture(Simulation2), MiniMapTexture(Simulation2)