1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

Add an activeobject manager to hold active objects (#7939)

* Add an activeobject manager to hold active objects
* Add unittests
This commit is contained in:
Loïc Blot 2018-12-13 20:18:54 +01:00 committed by GitHub
parent 839e935ba0
commit eda35100b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 844 additions and 320 deletions

View file

@ -21,6 +21,7 @@ set (UNITTEST_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_random.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_schematic.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_serialization.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_serveractiveobjectmgr.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_server_shutdown_state.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_settings.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_socket.cpp
@ -33,6 +34,7 @@ set (UNITTEST_SRCS
PARENT_SCOPE)
set (UNITTEST_CLIENT_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_clientactiveobjectmgr.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_eventmanager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_gameui.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_keycode.cpp

View file

@ -0,0 +1,117 @@
/*
Minetest
Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "client/activeobjectmgr.h"
#include <algorithm>
#include "test.h"
#include "profiler.h"
class TestClientActiveObject : public ClientActiveObject
{
public:
TestClientActiveObject() : ClientActiveObject(0, nullptr, nullptr) {}
~TestClientActiveObject() = default;
ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_TEST; }
};
class TestClientActiveObjectMgr : public TestBase
{
public:
TestClientActiveObjectMgr() { TestManager::registerTestModule(this); }
const char *getName() { return "TestClientActiveObjectMgr"; }
void runTests(IGameDef *gamedef);
void testFreeID();
void testRegisterObject();
void testRemoveObject();
};
static TestClientActiveObjectMgr g_test_instance;
void TestClientActiveObjectMgr::runTests(IGameDef *gamedef)
{
TEST(testFreeID);
TEST(testRegisterObject)
TEST(testRemoveObject)
}
////////////////////////////////////////////////////////////////////////////////
void TestClientActiveObjectMgr::testFreeID()
{
client::ActiveObjectMgr caomgr;
std::vector<u16> aoids;
u16 aoid = caomgr.getFreeId();
// Ensure it's not the same id
UASSERT(caomgr.getFreeId() != aoid);
aoids.push_back(aoid);
// Register basic objects, ensure we never found
for (u8 i = 0; i < UINT8_MAX; i++) {
// Register an object
auto tcao = new TestClientActiveObject();
caomgr.registerObject(tcao);
aoids.push_back(tcao->getId());
// Ensure next id is not in registered list
UASSERT(std::find(aoids.begin(), aoids.end(), caomgr.getFreeId()) ==
aoids.end());
}
caomgr.clear();
}
void TestClientActiveObjectMgr::testRegisterObject()
{
client::ActiveObjectMgr caomgr;
auto tcao = new TestClientActiveObject();
UASSERT(caomgr.registerObject(tcao));
u16 id = tcao->getId();
auto tcaoToCompare = caomgr.getActiveObject(id);
UASSERT(tcaoToCompare->getId() == id);
UASSERT(tcaoToCompare == tcao);
tcao = new TestClientActiveObject();
UASSERT(caomgr.registerObject(tcao));
UASSERT(caomgr.getActiveObject(tcao->getId()) == tcao);
UASSERT(caomgr.getActiveObject(tcao->getId()) != tcaoToCompare);
caomgr.clear();
}
void TestClientActiveObjectMgr::testRemoveObject()
{
client::ActiveObjectMgr caomgr;
auto tcao = new TestClientActiveObject();
UASSERT(caomgr.registerObject(tcao));
u16 id = tcao->getId();
UASSERT(caomgr.getActiveObject(id) != nullptr)
caomgr.removeObject(tcao->getId());
UASSERT(caomgr.getActiveObject(id) == nullptr)
caomgr.clear();
}

View file

@ -0,0 +1,188 @@
/*
Minetest
Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "server/activeobjectmgr.h"
#include <algorithm>
#include <queue>
#include "test.h"
#include "profiler.h"
class TestServerActiveObject : public ServerActiveObject
{
public:
TestServerActiveObject(const v3f &p = v3f()) : ServerActiveObject(nullptr, p) {}
~TestServerActiveObject() = default;
ActiveObjectType getType() const override { return ACTIVEOBJECT_TYPE_TEST; }
bool getCollisionBox(aabb3f *toset) const override { return false; }
bool getSelectionBox(aabb3f *toset) const override { return false; }
bool collideWithObjects() const override { return false; }
};
class TestServerActiveObjectMgr : public TestBase
{
public:
TestServerActiveObjectMgr() { TestManager::registerTestModule(this); }
const char *getName() { return "TestServerActiveObjectMgr"; }
void runTests(IGameDef *gamedef);
void testFreeID();
void testRegisterObject();
void testRemoveObject();
void testGetObjectsInsideRadius();
void testGetAddedActiveObjectsAroundPos();
};
static TestServerActiveObjectMgr g_test_instance;
void TestServerActiveObjectMgr::runTests(IGameDef *gamedef)
{
TEST(testFreeID);
TEST(testRegisterObject)
TEST(testRemoveObject)
TEST(testGetObjectsInsideRadius);
TEST(testGetAddedActiveObjectsAroundPos);
}
void clearSAOMgr(server::ActiveObjectMgr *saomgr)
{
auto clear_cb = [](ServerActiveObject *obj, u16 id) {
delete obj;
return true;
};
saomgr->clear(clear_cb);
}
////////////////////////////////////////////////////////////////////////////////
void TestServerActiveObjectMgr::testFreeID()
{
server::ActiveObjectMgr saomgr;
std::vector<u16> aoids;
u16 aoid = saomgr.getFreeId();
// Ensure it's not the same id
UASSERT(saomgr.getFreeId() != aoid);
aoids.push_back(aoid);
// Register basic objects, ensure we never found
for (u8 i = 0; i < UINT8_MAX; i++) {
// Register an object
auto tsao = new TestServerActiveObject();
saomgr.registerObject(tsao);
aoids.push_back(tsao->getId());
// Ensure next id is not in registered list
UASSERT(std::find(aoids.begin(), aoids.end(), saomgr.getFreeId()) ==
aoids.end());
}
clearSAOMgr(&saomgr);
}
void TestServerActiveObjectMgr::testRegisterObject()
{
server::ActiveObjectMgr saomgr;
auto tsao = new TestServerActiveObject();
UASSERT(saomgr.registerObject(tsao));
u16 id = tsao->getId();
auto tsaoToCompare = saomgr.getActiveObject(id);
UASSERT(tsaoToCompare->getId() == id);
UASSERT(tsaoToCompare == tsao);
tsao = new TestServerActiveObject();
UASSERT(saomgr.registerObject(tsao));
UASSERT(saomgr.getActiveObject(tsao->getId()) == tsao);
UASSERT(saomgr.getActiveObject(tsao->getId()) != tsaoToCompare);
clearSAOMgr(&saomgr);
}
void TestServerActiveObjectMgr::testRemoveObject()
{
server::ActiveObjectMgr saomgr;
auto tsao = new TestServerActiveObject();
UASSERT(saomgr.registerObject(tsao));
u16 id = tsao->getId();
UASSERT(saomgr.getActiveObject(id) != nullptr)
saomgr.removeObject(tsao->getId());
UASSERT(saomgr.getActiveObject(id) == nullptr);
clearSAOMgr(&saomgr);
}
void TestServerActiveObjectMgr::testGetObjectsInsideRadius()
{
server::ActiveObjectMgr saomgr;
static const v3f sao_pos[] = {
v3f(10, 40, 10),
v3f(740, 100, -304),
v3f(-200, 100, -304),
v3f(740, -740, -304),
v3f(1500, -740, -304),
};
for (const auto &p : sao_pos) {
saomgr.registerObject(new TestServerActiveObject(p));
}
std::vector<u16> result;
saomgr.getObjectsInsideRadius(v3f(), 50, result);
UASSERTCMP(int, ==, result.size(), 1);
result.clear();
saomgr.getObjectsInsideRadius(v3f(), 750, result);
UASSERTCMP(int, ==, result.size(), 2);
clearSAOMgr(&saomgr);
}
void TestServerActiveObjectMgr::testGetAddedActiveObjectsAroundPos()
{
server::ActiveObjectMgr saomgr;
static const v3f sao_pos[] = {
v3f(10, 40, 10),
v3f(740, 100, -304),
v3f(-200, 100, -304),
v3f(740, -740, -304),
v3f(1500, -740, -304),
};
for (const auto &p : sao_pos) {
saomgr.registerObject(new TestServerActiveObject(p));
}
std::queue<u16> result;
std::set<u16> cur_objects;
saomgr.getAddedActiveObjectsAroundPos(v3f(), 100, 50, cur_objects, result);
UASSERTCMP(int, ==, result.size(), 1);
result = std::queue<u16>();
cur_objects.clear();
saomgr.getAddedActiveObjectsAroundPos(v3f(), 740, 50, cur_objects, result);
UASSERTCMP(int, ==, result.size(), 2);
clearSAOMgr(&saomgr);
}

View file

@ -171,6 +171,8 @@ void TestUtilities::testWrapDegrees_0_360_v3f()
void TestUtilities::testLowercase()
{
UASSERT(lowercase("Foo bAR") == "foo bar");
UASSERT(lowercase("eeeeeeaaaaaaaaaaaààààà") == "eeeeeeaaaaaaaaaaaààààà");
UASSERT(lowercase("MINETEST-powa") == "minetest-powa");
}