Changeset View
Standalone View
source/simulation2/system/ComponentTest.h
/* Copyright (C) 2017 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
* GNU General Public License for more details. | * GNU General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * 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/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "lib/self_test.h" | #include "lib/self_test.h" | ||||
#include "maths/Matrix3D.h" | #include "maths/Matrix3D.h" | ||||
#include "maths/Vector3D.h" | #include "maths/Vector3D.h" | ||||
#include "ps/XML/Xeromyces.h" | #include "ps/XML/Xeromyces.h" | ||||
#include "simulation2/MessageTypes.h" | #include "simulation2/MessageTypes.h" | ||||
#include "simulation2/system/Component.h" | #include "simulation2/system/Component.h" | ||||
#include "simulation2/components/ICmpTerrain.h" | #include "simulation2/components/ICmpTerrain.h" | ||||
#include "simulation2/components/ICmpRangeManager.h" | |||||
Stan: whitespace | |||||
#include "simulation2/serialization/DebugSerializer.h" | #include "simulation2/serialization/DebugSerializer.h" | ||||
#include "simulation2/serialization/HashSerializer.h" | #include "simulation2/serialization/HashSerializer.h" | ||||
#include "simulation2/serialization/StdSerializer.h" | #include "simulation2/serialization/StdSerializer.h" | ||||
#include "simulation2/serialization/StdDeserializer.h" | #include "simulation2/serialization/StdDeserializer.h" | ||||
#include <iostream> | #include <iostream> | ||||
/** | /** | ||||
Show All 9 Lines | |||||
* - Call methods on the component pointer | * - Call methods on the component pointer | ||||
* - Use Roundtrip to test the consistency of serialization | * - Use Roundtrip to test the consistency of serialization | ||||
*/ | */ | ||||
class ComponentTestHelper | class ComponentTestHelper | ||||
{ | { | ||||
CSimContext m_Context; | CSimContext m_Context; | ||||
CComponentManager m_ComponentManager; | CComponentManager m_ComponentManager; | ||||
CParamNode m_Param; | CParamNode m_Param; | ||||
IComponent* m_Cmp; | IComponent* m_Cmp; | ||||
Not Done Inline Actionsdelete Silier: delete | |||||
EComponentTypeId m_Cid; | EComponentTypeId m_Cid; | ||||
bool m_isSystemEntityInit = false; | bool m_isSystemEntityInit = false; | ||||
public: | public: | ||||
ComponentTestHelper(shared_ptr<ScriptRuntime> runtime) : | ComponentTestHelper(shared_ptr<ScriptRuntime> runtime) : | ||||
m_Context(), m_ComponentManager(m_Context, runtime), m_Cmp(NULL) | m_Context(), m_ComponentManager(m_Context, runtime), m_Cmp(NULL) | ||||
{ | { | ||||
m_ComponentManager.LoadComponentTypes(); | m_ComponentManager.LoadComponentTypes(); | ||||
} | } | ||||
const ScriptInterface& GetScriptInterface() | const ScriptInterface& GetScriptInterface() | ||||
{ | { | ||||
return m_ComponentManager.GetScriptInterface(); | return m_ComponentManager.GetScriptInterface(); | ||||
} | } | ||||
CSimContext& GetSimContext() | CSimContext& GetSimContext() | ||||
{ | { | ||||
return m_Context; | return m_Context; | ||||
} | } | ||||
StanUnsubmitted Not Done Inline Actionsdelete Stan: delete | |||||
/** | /** | ||||
Not Done Inline Actionsdelete Silier: delete | |||||
* Call this once to initialise the test helper with a component. | * Call this once to initialise the test helper with a component. | ||||
*/ | */ | ||||
template<typename T> | template<typename T> | ||||
T* Add(EComponentTypeId cid, const std::string& xml, entity_id_t ent = 10) | T* Add(EComponentTypeId cid, const std::string& xml, entity_id_t ent = 10) | ||||
{ | { | ||||
TS_ASSERT(m_Cmp == NULL); | TS_ASSERT(m_Cmp == NULL); | ||||
CEntityHandle handle; | CEntityHandle handle; | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | public: | ||||
} | } | ||||
}; | }; | ||||
/** | /** | ||||
* Simple terrain implementation with constant height of 50. | * Simple terrain implementation with constant height of 50. | ||||
*/ | */ | ||||
class MockTerrain : public ICmpTerrain | class MockTerrain : public ICmpTerrain | ||||
{ | { | ||||
private: | |||||
CTerrain* m_Terrain; | |||||
bool m_useTerrain = false; | |||||
StanUnsubmitted Not Done Inline Actionsm_UseTerrain also you could set it to false in the Init function. Stan: m_UseTerrain also you could set it to false in the Init function. | |||||
public: | public: | ||||
DEFAULT_MOCK_COMPONENT() | virtual int GetComponentTypeId() const | ||||
{ | |||||
return -1; | |||||
} | |||||
static void ClassInit(CComponentManager& UNUSED(componentManager)) | |||||
{ | |||||
} | |||||
virtual bool IsLoaded() const | virtual bool IsLoaded() const | ||||
{ | { | ||||
if (!m_useTerrain) | |||||
return true; | return true; | ||||
return m_Terrain->GetVerticesPerSide() != 0; | |||||
StanUnsubmitted Not Done Inline Actionsternary ? Stan: ternary ? | |||||
} | } | ||||
virtual CFixedVector3D CalcNormal(entity_pos_t UNUSED(x), entity_pos_t UNUSED(z)) const | virtual void Init(const CParamNode& UNUSED(paramNode)) | ||||
{ | { | ||||
if (m_Terrain) | |||||
m_useTerrain = true; | |||||
StanUnsubmitted Not Done Inline Actionsm_useTerrain = true = m_Terrain ? Stan: m_useTerrain = true = m_Terrain ? | |||||
} | |||||
virtual void Deinit() | |||||
{ | |||||
if (!m_useTerrain) | |||||
StanUnsubmitted Not Done Inline Actionsternary ? Stan: ternary ? | |||||
return; | |||||
m_Terrain = nullptr; | |||||
} | |||||
virtual void Serialize(ISerializer& UNUSED(serialize)) | |||||
{ | |||||
} | |||||
virtual void Deserialize(const CParamNode& paramNode, IDeserializer& UNUSED(deserialize)) | |||||
{ | |||||
Init(paramNode); | |||||
} | |||||
virtual CFixedVector3D CalcNormal(entity_pos_t x, entity_pos_t z) const | |||||
{ | |||||
if (!m_useTerrain) | |||||
return CFixedVector3D(fixed::FromInt(0), fixed::FromInt(1), fixed::FromInt(0)); | return CFixedVector3D(fixed::FromInt(0), fixed::FromInt(1), fixed::FromInt(0)); | ||||
CFixedVector3D normal; | |||||
m_Terrain->CalcNormalFixed((x / (int)TERRAIN_TILE_SIZE).ToInt_RoundToZero(), (z / (int)TERRAIN_TILE_SIZE).ToInt_RoundToZero(), normal); | |||||
StanUnsubmitted Not Done Inline Actionsstatic_cast<int>(...) though probably no need to cast as there is a cast just after. One might want to cast to float otherwise. Stan: static_cast<int>(...) though probably no need to cast as there is a cast just after. One might… | |||||
SilierAuthorUnsubmitted Done Inline Actionsyou cannot do fixed / float operation and it is exactly what is in CCTerrain :) Silier: you cannot do fixed / float operation
it is well defined op fixed / int :) you are not loosing… | |||||
return normal; | |||||
} | } | ||||
virtual CVector3D CalcExactNormal(float UNUSED(x), float UNUSED(z)) const | virtual CVector3D CalcExactNormal(float x, float z) const | ||||
{ | { | ||||
if (!m_useTerrain) | |||||
StanUnsubmitted Not Done Inline Actionsternary ? Stan: ternary ? | |||||
return CVector3D(0.f, 1.f, 0.f); | return CVector3D(0.f, 1.f, 0.f); | ||||
return m_Terrain->CalcExactNormal(x, z); | |||||
} | } | ||||
virtual entity_pos_t GetGroundLevel(entity_pos_t UNUSED(x), entity_pos_t UNUSED(z)) const | virtual entity_pos_t GetGroundLevel(entity_pos_t x, entity_pos_t z) const | ||||
{ | { | ||||
if (!m_useTerrain) | |||||
StanUnsubmitted Not Done Inline Actionsternary ? Stan: ternary ? | |||||
return entity_pos_t::FromInt(50); | return entity_pos_t::FromInt(50); | ||||
return m_Terrain->GetExactGroundLevelFixed(x, z); | |||||
} | } | ||||
virtual float GetExactGroundLevel(float UNUSED(x), float UNUSED(z)) const | virtual float GetExactGroundLevel(float x, float z) const | ||||
{ | { | ||||
if (!m_useTerrain) | |||||
StanUnsubmitted Not Done Inline Actionsternary ? Stan: ternary ? | |||||
return 50.f; | return 50.f; | ||||
return m_Terrain->GetExactGroundLevel(x, z); | |||||
} | } | ||||
virtual u16 GetTilesPerSide() const | virtual u16 GetTilesPerSide() const | ||||
{ | { | ||||
if (!m_useTerrain) | |||||
return 16; | return 16; | ||||
ssize_t tiles = m_Terrain->GetTilesPerSide(); | |||||
if (tiles == -1) | |||||
return 0; | |||||
ENSURE(1 <= tiles && tiles <= 65535); | |||||
StanUnsubmitted Not Done Inline ActionsOne could use std::numeric_limits<short>::max() Stan: One could use std::numeric_limits<short>::max() | |||||
return (u16)tiles; | |||||
} | } | ||||
virtual u32 GetMapSize() const | virtual u32 GetMapSize() const | ||||
{ | { | ||||
return GetTilesPerSide() * TERRAIN_TILE_SIZE; | return GetTilesPerSide() * TERRAIN_TILE_SIZE; | ||||
} | } | ||||
virtual u16 GetVerticesPerSide() const | virtual u16 GetVerticesPerSide() const | ||||
{ | { | ||||
if (!m_useTerrain) | |||||
return 17; | return 17; | ||||
ssize_t vertices = m_Terrain->GetVerticesPerSide(); | |||||
ENSURE(1 <= vertices && vertices <= 65535); | |||||
StanUnsubmitted Not Done Inline Actionsone could use std::numeric_limits<u16>::max() Stan: one could use std::numeric_limits<u16>::max() | |||||
return (u16)vertices; | |||||
} | } | ||||
virtual CTerrain* GetCTerrain() | virtual CTerrain* GetCTerrain() | ||||
{ | { | ||||
if (!m_useTerrain) | |||||
return NULL; | return NULL; | ||||
StanUnsubmitted Not Done Inline Actionsnullptr. Maybe ternary ? Stan: nullptr. Maybe ternary ? | |||||
return m_Terrain; | |||||
} | |||||
virtual void SetTerrain(CTerrain* t) | |||||
{ | |||||
m_Terrain = t; | |||||
m_useTerrain = true; | |||||
} | } | ||||
virtual void MakeDirty(i32 UNUSED(i0), i32 UNUSED(j0), i32 UNUSED(i1), i32 UNUSED(j1)) | virtual void MakeDirty(i32 i0, i32 j0, i32 i1, i32 j1) | ||||
StanUnsubmitted Not Done Inline ActionsAny reason we are using i32 specifically instead of int ? Stan: Any reason we are using i32 specifically instead of int ? | |||||
SilierAuthorUnsubmitted Done Inline Actionsdont know, maybe just to be sure its 32 and not 64 Silier: dont know, maybe just to be sure its 32 and not 64 | |||||
{ | { | ||||
if (!m_useTerrain) | |||||
return; | |||||
CMessageTerrainChanged msg(i0, j0, i1, j1); | |||||
GetSimContext().GetComponentManager().BroadcastMessage(msg); | |||||
} | } | ||||
virtual void ReloadTerrain(bool UNUSED(ReloadWater)) | virtual void ReloadTerrain(bool UNUSED(ReloadWater)) | ||||
{ | { | ||||
if (!m_useTerrain) | |||||
return; | |||||
u16 tiles = GetTilesPerSide(); | |||||
u16 vertices = GetVerticesPerSide(); | |||||
CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSystemEntity()); | |||||
if (cmpObstructionManager) | |||||
{ | |||||
cmpObstructionManager->SetBounds(entity_pos_t::Zero(), entity_pos_t::Zero(), | |||||
entity_pos_t::FromInt(tiles*(int)TERRAIN_TILE_SIZE), | |||||
StanUnsubmitted Not Done Inline Actionsstatic_cast<int>(...) spaces between operators. Stan: static_cast<int>(...) spaces between operators. | |||||
entity_pos_t::FromInt(tiles*(int)TERRAIN_TILE_SIZE)); | |||||
StanUnsubmitted Not Done Inline Actionsstatic_cast<int>(...) spaces between operators. Stan: static_cast<int>(...) spaces between operators. | |||||
} | |||||
CmpPtr<ICmpRangeManager> cmpRangeManager(GetSystemEntity()); | |||||
if (cmpRangeManager) | |||||
{ | |||||
cmpRangeManager->SetBounds(entity_pos_t::Zero(), entity_pos_t::Zero(), | |||||
entity_pos_t::FromInt(tiles*(int)TERRAIN_TILE_SIZE), | |||||
StanUnsubmitted Not Done Inline Actionsstatic_cast<int>(...) spaces between operators. Stan: static_cast<int>(...) spaces between operators. | |||||
entity_pos_t::FromInt(tiles*(int)TERRAIN_TILE_SIZE), | |||||
StanUnsubmitted Not Done Inline Actionsstatic_cast<int>(...) spaces between operators. Stan: static_cast<int>(...) spaces between operators. | |||||
vertices); | |||||
} | |||||
MakeDirty(0, 0, tiles + 1, tiles + 1); | |||||
} | } | ||||
}; | }; |
whitespace