Index: ps/trunk/source/maths/Random.h =================================================================== --- ps/trunk/source/maths/Random.h (revision 22174) +++ ps/trunk/source/maths/Random.h (nonexistent) @@ -1,131 +0,0 @@ -/* Copyright (C) 2011 Wildfire Games. - * This file is part of 0 A.D. - * - * 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 - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * 0 A.D. is distributed in the hope that it will be useful, - * 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 - * along with 0 A.D. If not, see . - */ - -#ifndef INCLUDED_MATHS_RANDOM -#define INCLUDED_MATHS_RANDOM - -/** - * Random number generator with period 2^{512}-1; - * effectively a better version of MT19937 (smaller state, similarly fast, - * simpler code, better distribution). - * Implements Boost.Random's PseudoRandomNumberGenerator concept. - */ -class WELL512 -{ -private: - uint32_t state[16]; - uint32_t index; - -public: - WELL512() - { - seed((uint32_t)0); - } - - uint32_t operator()() - { - // WELL512 implementation by Chris Lomont (Game Programming Gems 7; - // http://lomont.org/Math/Papers/2008/Lomont_PRNG_2008.pdf) - - uint32_t a, b, c, d; - a = state[index]; - c = state[(index + 13) & 15]; - b = a ^ c ^ (a << 16) ^ (c << 15); - c = state[(index + 9) & 15]; - c ^= (c >> 11); - a = state[index] = b ^ c; - d = a ^ ((a << 5) & 0xDA442D24UL); - index = (index + 15) & 15; - a = state[index]; - state[index] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28); - return state[index]; - } - - void seed(uint32_t value) - { - index = 0; - - // Expand the seed with the same algorithm as boost::random::mersenne_twister - const uint32_t mask = ~0u; - state[0] = value & mask; - for (uint32_t i = 1; i < ARRAY_SIZE(state); ++i) - state[i] = (1812433253UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i) & mask; - } - - void seed(uint32_t values[16]) - { - index = 0; - - for (uint32_t i = 0; i < ARRAY_SIZE(state); ++i) - state[i] = values[i]; - } - - // Implement UniformRandomNumberGenerator concept: - - typedef uint32_t result_type; - - uint32_t min() const - { - return std::numeric_limits::min(); - } - - uint32_t max() const - { - return std::numeric_limits::max(); - } - - // Implement EqualityComparable concept: - - friend bool operator==(const WELL512& x, const WELL512& y) - { - if (x.index != y.index) - return false; - for (uint32_t i = 0; i < ARRAY_SIZE(x.state); ++i) - if (x.state[i] != y.state[i]) - return false; - return true; - } - - friend bool operator!=(const WELL512& x, const WELL512& y) - { - return !(x == y); - } - - // Implement Streamable concept (based on boost::random::mersenne_twister): - - template - friend std::basic_ostream& - operator<<(std::basic_ostream& os, const WELL512& rng) - { - os << rng.index << " "; - for (uint32_t i = 0; i < ARRAY_SIZE(rng.state); ++i) - os << rng.state[i] << " "; - return os; - } - - template - friend std::basic_istream& - operator>>(std::basic_istream& is, WELL512& rng) - { - is >> rng.index >> std::ws; - for (uint32_t i = 0; i < ARRAY_SIZE(rng.state); ++i) - is >> rng.state[i] >> std::ws; - return is; - } -}; - -#endif // INCLUDED_MATHS_RANDOM Property changes on: ps/trunk/source/maths/Random.h ___________________________________________________________________ Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Index: ps/trunk/source/maths/tests/test_Random.h =================================================================== --- ps/trunk/source/maths/tests/test_Random.h (revision 22174) +++ ps/trunk/source/maths/tests/test_Random.h (nonexistent) @@ -1,115 +0,0 @@ -/* Copyright (C) 2011 Wildfire Games. - * This file is part of 0 A.D. - * - * 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 - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. - * - * 0 A.D. is distributed in the hope that it will be useful, - * 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 - * along with 0 A.D. If not, see . - */ - -#include "lib/self_test.h" - -#include "maths/Random.h" - -class TestRandom : public CxxTest::TestSuite -{ -public: - void test_sequence() - { - uint32_t seed[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; - WELL512 rng; - rng.seed(seed); - - // Result vector was generated via http://www.iro.umontreal.ca/~panneton/well/WELL512a.c - - uint32_t expected[] = { 2423521338U, 2295858494U, 4237567038U, 3767796794U, 3498837026U, 3236431142U, 3162776622U, - 2695103786U, 2495358234U, 3292178890U, 1194984719U, 1635922038U, 1902646777U, 3540127820U, 2776150123U, - 3803026854U, 4022481096U, 2890771780U, 2383096164U, 2598906205U, 641858878U, 703208507U, 3187021161U, 4164927930U, - 1729358216U, 3970992993U, 55500867U, 1064458966U, 3142916837U, 2792079210U, 415718876U, 2069253743U, 1090459178U, - 1150648555U, 2130138587U, 85555256U, 938493562U, 573358422U, 2500660050U, 3673342299U, 4062731563U, 4281407583U, - 131831842U, 2943104616U, 260524413U, 4185358860U, 1146840142U, 173433925U, 962932907U, 3625667774U, 2966176140U, - 623826899U, 3222323490U, 1781272619U, 2042445736U, 145932495U, 2676651172U, 1245572857U, 2279822797U, 1079936135U, - 3912374806U, 3819369956U, 2524610988U, 2526918697U }; - - for (size_t i = 0; i < ARRAY_SIZE(expected); ++i) - TS_ASSERT_EQUALS(rng(), expected[i]); - - for (size_t i = 0; i < 1024*1024; ++i) - rng(); - - TS_ASSERT_EQUALS(rng(), 2143457027U); - } - - void test_seed() - { - WELL512 rng; - rng.seed(1234); - - // Equivalent to: - // uint32_t seed[] = { 1234, 3159640283, 4062961311, 3954462607, 3112783424, 2849714703, 731821095, 2232873578, 1251953424, - // 3917199038, 231030171, 268845362, 2344188614, 1849447265, 2383103214, 2227731755 }; - - // Result vector was generated via http://www.iro.umontreal.ca/~panneton/well/WELL512a.c - - uint32_t expected[] = { 3796049263U, 3121637295U, 2314947441U, 124483343U, 1062552979U, 2701629854U, 2863421760U, - 1833958548U, 3210932400U, 3192737225U, 3835676748U, 248447399U, 1120349102U, 2349012450U, 2210895795U, 564651791U, - 4272914221U, 3396120565U, 419954947U, 1558309715U, 2980408554U, 3700173484U, 843419536U, 1226049545U, 383882930U, - 4147762817U, 3046810299U, 2822903886U, 2009534132U, 3375709471U, 816292113U, 4153554971U, 1025098704U, 3084937380U, - 3413388140U, 1868518045U, 1137536168U, 2321169U, 1171973932U, 2527937887U, 2851724729U, 180120257U, 3972785449U, - 3106766194U, 4238600917U, 3828143376U, 3332379518U, 3410880226U, 1358510220U, 1680847693U, 3264109669U, - 4214219167U, 635098860U, 3999351741U, 4274606842U, 2037105003U, 1521922362U, 2677146791U, 3449144249U, 3347816516U, - 44838202U, 3818140137U, 1521668346U, 260162572U }; - - for (size_t i = 0; i < ARRAY_SIZE(expected); ++i) - TS_ASSERT_EQUALS(rng(), expected[i]); - } - - void test_comparable() - { - WELL512 rng1; - WELL512 rng2; - TS_ASSERT(rng1 == rng2); - - rng1.seed(1234); - TS_ASSERT(!(rng1 == rng2)); - - rng2.seed(1234); - TS_ASSERT(rng1 == rng2); - - rng1(); - TS_ASSERT(!(rng1 == rng2)); - - rng2 = rng1; - TS_ASSERT(rng1 == rng2); - - WELL512 rng3(rng2); - TS_ASSERT(rng2 == rng3); - } - - void test_stream() - { - WELL512 rng1; - WELL512 rng2; - rng1(); - - std::stringstream str; - str << rng1; - - TS_ASSERT_STR_EQUALS(str.str(), - "15 2666258328 1 1812433255 1900727105 1208447044 2481403966 " - "4042607538 337614300 3232553940 1018809052 3202401494 " - "1775180719 3192392114 594215549 184016991 1235243591 ") - - TS_ASSERT(!(rng1 == rng2)); - str >> rng2; - TS_ASSERT(rng1 == rng2); - } -}; Property changes on: ps/trunk/source/maths/tests/test_Random.h ___________________________________________________________________ Deleted: svn:eol-style ## -1 +0,0 ## -native \ No newline at end of property Index: ps/trunk/source/simulation2/components/tests/test_RangeManager.h =================================================================== --- ps/trunk/source/simulation2/components/tests/test_RangeManager.h (revision 22174) +++ ps/trunk/source/simulation2/components/tests/test_RangeManager.h (revision 22175) @@ -1,158 +1,156 @@ -/* Copyright (C) 2017 Wildfire Games. +/* Copyright (C) 2019 Wildfire Games. * This file is part of 0 A.D. * * 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 * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * 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 * along with 0 A.D. If not, see . */ #include "simulation2/system/ComponentTest.h" - #include "simulation2/components/ICmpRangeManager.h" #include "simulation2/components/ICmpPosition.h" #include "simulation2/components/ICmpVision.h" -#include "maths/Random.h" - +#include #include class MockVision : public ICmpVision { public: DEFAULT_MOCK_COMPONENT() virtual entity_pos_t GetRange() const { return entity_pos_t::FromInt(66); } virtual bool GetRevealShore() const { return false; } }; class MockPosition : public ICmpPosition { public: DEFAULT_MOCK_COMPONENT() virtual void SetTurretParent(entity_id_t UNUSED(id), const CFixedVector3D& UNUSED(pos)) {} virtual entity_id_t GetTurretParent() const {return INVALID_ENTITY;} virtual void UpdateTurretPosition() {} virtual std::set* GetTurrets() { return NULL; } virtual bool IsInWorld() const { return true; } virtual void MoveOutOfWorld() { } virtual void MoveTo(entity_pos_t UNUSED(x), entity_pos_t UNUSED(z)) { } virtual void MoveAndTurnTo(entity_pos_t UNUSED(x), entity_pos_t UNUSED(z), entity_angle_t UNUSED(a)) { } virtual void JumpTo(entity_pos_t UNUSED(x), entity_pos_t UNUSED(z)) { } virtual void SetHeightOffset(entity_pos_t UNUSED(dy)) { } virtual entity_pos_t GetHeightOffset() const { return entity_pos_t::Zero(); } virtual void SetHeightFixed(entity_pos_t UNUSED(y)) { } virtual entity_pos_t GetHeightFixed() const { return entity_pos_t::Zero(); } virtual bool IsHeightRelative() const { return true; } virtual void SetHeightRelative(bool UNUSED(relative)) { } virtual bool CanFloat() const { return false; } virtual void SetFloating(bool UNUSED(flag)) { } virtual void SetActorFloating(bool UNUSED(flag)) { } virtual void SetConstructionProgress(fixed UNUSED(progress)) { } virtual CFixedVector3D GetPosition() const { return CFixedVector3D(); } virtual CFixedVector2D GetPosition2D() const { return CFixedVector2D(); } virtual CFixedVector3D GetPreviousPosition() const { return CFixedVector3D(); } virtual CFixedVector2D GetPreviousPosition2D() const { return CFixedVector2D(); } virtual void TurnTo(entity_angle_t UNUSED(y)) { } virtual void SetYRotation(entity_angle_t UNUSED(y)) { } virtual void SetXZRotation(entity_angle_t UNUSED(x), entity_angle_t UNUSED(z)) { } virtual CFixedVector3D GetRotation() const { return CFixedVector3D(); } virtual fixed GetDistanceTravelled() const { return fixed::Zero(); } virtual void GetInterpolatedPosition2D(float UNUSED(frameOffset), float& x, float& z, float& rotY) const { x = z = rotY = 0; } virtual CMatrix3D GetInterpolatedTransform(float UNUSED(frameOffset)) const { return CMatrix3D(); } }; class TestCmpRangeManager : public CxxTest::TestSuite { public: void setUp() { CXeromyces::Startup(); } void tearDown() { CXeromyces::Terminate(); } // TODO It would be nice to call Verify() with the shore revealing system // but that means testing on an actual map, with water and land. void test_basic() { ComponentTestHelper test(g_ScriptRuntime); ICmpRangeManager* cmp = test.Add(CID_RangeManager, "", SYSTEM_ENTITY); MockVision vision; test.AddMock(100, IID_Vision, vision); MockPosition position; test.AddMock(100, IID_Position, position); // This tests that the incremental computation produces the correct result // in various edge cases cmp->SetBounds(entity_pos_t::FromInt(0), entity_pos_t::FromInt(0), entity_pos_t::FromInt(512), entity_pos_t::FromInt(512), 512/TERRAIN_TILE_SIZE + 1); cmp->Verify(); { CMessageCreate msg(100); cmp->HandleMessage(msg, false); } cmp->Verify(); { CMessageOwnershipChanged msg(100, -1, 1); cmp->HandleMessage(msg, false); } cmp->Verify(); { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(247), entity_pos_t::FromDouble(257.95), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } cmp->Verify(); { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(247), entity_pos_t::FromInt(253), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } cmp->Verify(); { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(256), entity_pos_t::FromInt(256), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } cmp->Verify(); { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(256)+entity_pos_t::Epsilon(), entity_pos_t::FromInt(256), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } cmp->Verify(); { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(256)-entity_pos_t::Epsilon(), entity_pos_t::FromInt(256), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } cmp->Verify(); { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(256), entity_pos_t::FromInt(256)+entity_pos_t::Epsilon(), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } cmp->Verify(); { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(256), entity_pos_t::FromInt(256)-entity_pos_t::Epsilon(), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } cmp->Verify(); { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(383), entity_pos_t::FromInt(84), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } cmp->Verify(); { CMessagePositionChanged msg(100, true, entity_pos_t::FromInt(348), entity_pos_t::FromInt(83), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } cmp->Verify(); - WELL512 rng; + boost::mt19937 rng; for (size_t i = 0; i < 1024; ++i) { - double x = boost::uniform_real<>(0.0, 512.0)(rng); - double z = boost::uniform_real<>(0.0, 512.0)(rng); + double x = boost::uniform_real_distribution(0.0, 512.0)(rng); + double z = boost::uniform_real_distribution(0.0, 512.0)(rng); { CMessagePositionChanged msg(100, true, entity_pos_t::FromDouble(x), entity_pos_t::FromDouble(z), entity_angle_t::Zero()); cmp->HandleMessage(msg, false); } cmp->Verify(); } // Test OwnershipChange, GetEntitiesByPlayer, GetNonGaiaEntities { player_id_t previousOwner = -1; for (player_id_t newOwner = 0; newOwner < 8; ++newOwner) { CMessageOwnershipChanged msg(100, previousOwner, newOwner); cmp->HandleMessage(msg, false); for (player_id_t i = 0; i < 8; ++i) TS_ASSERT_EQUALS(cmp->GetEntitiesByPlayer(i).size(), i == newOwner ? 1 : 0); TS_ASSERT_EQUALS(cmp->GetNonGaiaEntities().size(), newOwner > 0 ? 1 : 0); previousOwner = newOwner; } } } };