|
| 1 | +/* |
| 2 | +Minetest |
| 3 | +Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr> |
| 4 | +
|
| 5 | +This program is free software; you can redistribute it and/or modify |
| 6 | +it under the terms of the GNU Lesser General Public License as published by |
| 7 | +the Free Software Foundation; either version 2.1 of the License, or |
| 8 | +(at your option) any later version. |
| 9 | +
|
| 10 | +This program is distributed in the hope that it will be useful, |
| 11 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +GNU Lesser General Public License for more details. |
| 14 | +
|
| 15 | +You should have received a copy of the GNU Lesser General Public License along |
| 16 | +with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | +*/ |
| 19 | + |
| 20 | +#include "test.h" |
| 21 | + |
| 22 | +#include "server/serveractiveobjectmap.h" |
| 23 | +#include "content_sao.h" |
| 24 | + |
| 25 | +class TestServerActiveObjectMap : public TestBase |
| 26 | +{ |
| 27 | +public: |
| 28 | + TestServerActiveObjectMap() { TestManager::registerTestModule(this); } |
| 29 | + const char *getName() { return "TestServerActiveObjectMap"; } |
| 30 | + |
| 31 | + void runTests(IGameDef *gamedef); |
| 32 | + |
| 33 | + void testAddObject(); |
| 34 | + void testRemoveObject(); |
| 35 | + void testUpdateObject(); |
| 36 | + void testGetObject(); |
| 37 | + void testIsFreeID(); |
| 38 | + void testGetFreeID(); |
| 39 | + void testGetObjectsInsideRadius(); |
| 40 | + void testGetObjectsTouchingBox(); |
| 41 | +}; |
| 42 | + |
| 43 | +static TestServerActiveObjectMap g_test_instance; |
| 44 | + |
| 45 | +void TestServerActiveObjectMap::runTests(IGameDef *gamedef) |
| 46 | +{ |
| 47 | + TEST(testAddObject); |
| 48 | + TEST(testRemoveObject); |
| 49 | + TEST(testUpdateObject); |
| 50 | + TEST(testGetObject); |
| 51 | + TEST(testIsFreeID); |
| 52 | + TEST(testGetFreeID); |
| 53 | + TEST(testGetObjectsInsideRadius); |
| 54 | + TEST(testGetObjectsTouchingBox); |
| 55 | +} |
| 56 | + |
| 57 | +void TestServerActiveObjectMap::testAddObject() |
| 58 | +{ |
| 59 | + ServerActiveObjectMap saom; |
| 60 | + UASSERT(saom.getObjects().empty()); |
| 61 | + |
| 62 | + LuaEntitySAO ob1(nullptr, v3f(0, 0, 0), "", ""); |
| 63 | + ob1.setId(saom.getFreeId()); |
| 64 | + saom.addObject(&ob1); |
| 65 | + |
| 66 | + UASSERT(saom.getObjects().size() == 1); |
| 67 | + bool found = false; |
| 68 | + for (const auto &pair : saom.getObjects()) { |
| 69 | + UASSERT(pair.second.object == &ob1); |
| 70 | + found = true; |
| 71 | + } |
| 72 | + UASSERT(found); |
| 73 | +} |
| 74 | + |
| 75 | +void TestServerActiveObjectMap::testRemoveObject() |
| 76 | +{ |
| 77 | + ServerActiveObjectMap saom; |
| 78 | + UASSERT(saom.getObjects().empty()); |
| 79 | + |
| 80 | + LuaEntitySAO ob1(nullptr, v3f(0, 0, 0), "", ""); |
| 81 | + ob1.setId(saom.getFreeId()); |
| 82 | + saom.addObject(&ob1); |
| 83 | + |
| 84 | + UASSERT(saom.getObjects().size() == 1); |
| 85 | + bool found = false; |
| 86 | + for (const auto &pair : saom.getObjects()) { |
| 87 | + UASSERT(pair.second.object == &ob1); |
| 88 | + found = true; |
| 89 | + } |
| 90 | + UASSERT(found); |
| 91 | + |
| 92 | + saom.removeObject(&ob1); |
| 93 | +} |
| 94 | + |
| 95 | +void TestServerActiveObjectMap::testUpdateObject() |
| 96 | +{ |
| 97 | + ServerActiveObjectMap saom; |
| 98 | + LuaEntitySAO ob1(nullptr, v3f(1, 0, 0), "", ""); |
| 99 | + ob1.accessObjectProperties()->physical = true; |
| 100 | + ob1.setId(saom.getFreeId()); |
| 101 | + saom.addObject(&ob1); |
| 102 | + UASSERT(saom.getObjectsInsideRadius(v3f(0, 0, 0), 2).size() == 1); |
| 103 | + UASSERT(saom.getObjectsInsideRadius(v3f(6, 0, 0), 2).size() == 0); |
| 104 | + ob1.setBasePosition(v3f(5, 0, 0)); |
| 105 | + saom.updateObject(&ob1); |
| 106 | + UASSERT(saom.getObjectsInsideRadius(v3f(0, 0, 0), 2).size() == 0); |
| 107 | + UASSERT(saom.getObjectsInsideRadius(v3f(6, 0, 0), 2).size() == 1); |
| 108 | +} |
| 109 | + |
| 110 | +void TestServerActiveObjectMap::testGetObject() |
| 111 | +{ |
| 112 | + ServerActiveObjectMap saom; |
| 113 | + LuaEntitySAO ob1(nullptr, v3f(0, 0, 0), "", ""); |
| 114 | + u16 id = saom.getFreeId(); |
| 115 | + ob1.setId(id); |
| 116 | + saom.addObject(&ob1); |
| 117 | + UASSERT(saom.getObject(0) == nullptr); |
| 118 | + UASSERT(saom.getObject(id) == &ob1); |
| 119 | + UASSERT(saom.getObject(id + 1) == nullptr); |
| 120 | +} |
| 121 | + |
| 122 | +void TestServerActiveObjectMap::testIsFreeID() |
| 123 | +{ |
| 124 | + ServerActiveObjectMap saom; |
| 125 | + UASSERT(!saom.isFreeId(0)); |
| 126 | + UASSERT(saom.isFreeId(1)); |
| 127 | + UASSERT(saom.isFreeId(2)); |
| 128 | +} |
| 129 | + |
| 130 | +void TestServerActiveObjectMap::testGetFreeID() |
| 131 | +{ |
| 132 | + ServerActiveObjectMap saom; |
| 133 | + u16 first_id = saom.getFreeId(); |
| 134 | + UASSERT(first_id > 0); |
| 135 | + UASSERT(saom.getFreeId() > first_id); |
| 136 | +} |
| 137 | + |
| 138 | +void TestServerActiveObjectMap::testGetObjectsInsideRadius() |
| 139 | +{ |
| 140 | + ServerActiveObjectMap saom; |
| 141 | +#define ADD_OBJECT_IMPL(name, pos) \ |
| 142 | + LuaEntitySAO name(nullptr, pos, "", ""); \ |
| 143 | + name.accessObjectProperties()->physical = true; \ |
| 144 | + name.setId(saom.getFreeId()); \ |
| 145 | + saom.addObject(&name) |
| 146 | +#define ADD_OBJECT(pos) ADD_OBJECT_IMPL(NEWNAME(ob), pos) |
| 147 | +#define OBJECT_COUNT (saom.getObjectsInsideRadius(v3f(0, 0, 0), 5).size()) |
| 148 | + |
| 149 | + UASSERT(OBJECT_COUNT == 0); |
| 150 | + |
| 151 | + ADD_OBJECT(v3f(0, 0, 0)); |
| 152 | + UASSERT(OBJECT_COUNT == 1); |
| 153 | + |
| 154 | + ADD_OBJECT(v3f(-1, -1, -1)); |
| 155 | + UASSERT(OBJECT_COUNT == 2); |
| 156 | + |
| 157 | + ADD_OBJECT(v3f(4.9, 0, 0)); |
| 158 | + UASSERT(OBJECT_COUNT == 3); |
| 159 | + |
| 160 | + ADD_OBJECT(v3f(5.1, 0, 0)); |
| 161 | + UASSERT(OBJECT_COUNT == 3); |
| 162 | + |
| 163 | + ADD_OBJECT(v3f(3, 3, 3)); |
| 164 | + UASSERT(OBJECT_COUNT == 3); |
| 165 | +} |
| 166 | + |
| 167 | +void TestServerActiveObjectMap::testGetObjectsTouchingBox() |
| 168 | +{ |
| 169 | + ServerActiveObjectMap saom; |
| 170 | + |
| 171 | + LuaEntitySAO ob1(nullptr, v3f(1 * BS, 0, 0), "", ""); |
| 172 | + ob1.accessObjectProperties()->physical = true; |
| 173 | + // Collision boxes are in nodes, not in world units: |
| 174 | + ob1.accessObjectProperties()->collisionbox = {-1, -1, -1, 1, 1, 1}; |
| 175 | + ob1.setId(saom.getFreeId()); |
| 176 | + saom.addObject(&ob1); |
| 177 | + |
| 178 | + LuaEntitySAO ob2(nullptr, v3f(1.5 * BS, 2.5 * BS, 0), "", ""); |
| 179 | + ob2.accessObjectProperties()->physical = true; |
| 180 | + ob2.accessObjectProperties()->collisionbox = {-0.5, -0.5, -1, 3.5, 2, 1}; |
| 181 | + ob2.setId(saom.getFreeId()); |
| 182 | + saom.addObject(&ob2); |
| 183 | + |
| 184 | + std::vector<u16> list; |
| 185 | + |
| 186 | + list = saom.getObjectsTouchingBox( |
| 187 | + {2.1 * BS, -1.0 * BS, -1.0 * BS, 2.5 * BS, 1.0 * BS, 1.0 * BS}); |
| 188 | + UASSERT(list.size() == 0); |
| 189 | + |
| 190 | + // intersecting ob1 |
| 191 | + list = saom.getObjectsTouchingBox( |
| 192 | + {1.9 * BS, -1.0 * BS, -1.0 * BS, 2.5 * BS, 1.0 * BS, 1.0 * BS}); |
| 193 | + UASSERT(list.size() == 1 && list[0] == ob1.getId()); |
| 194 | + |
| 195 | + // intersecting ob2 |
| 196 | + list = saom.getObjectsTouchingBox( |
| 197 | + {2.1 * BS, -1.0 * BS, -1.0 * BS, 2.5 * BS, 2.1 * BS, 1.0 * BS}); |
| 198 | + UASSERT(list.size() == 1 && list[0] == ob2.getId()); |
| 199 | + |
| 200 | + // contained in ob1 |
| 201 | + list = saom.getObjectsTouchingBox( |
| 202 | + {1.5 * BS, -0.1 * BS, -0.1 * BS, 1.9 * BS, 0.1 * BS, 0.1 * BS}); |
| 203 | + UASSERT(list.size() == 1 && list[0] == ob1.getId()); |
| 204 | + |
| 205 | + // contains ob2 |
| 206 | + list = saom.getObjectsTouchingBox( |
| 207 | + {0.9 * BS, 1.5 * BS, -5.0 * BS, 6.0 * BS, 20.0 * BS, 500.0 * BS}); |
| 208 | + UASSERT(list.size() == 1 && list[0] == ob2.getId()); |
| 209 | + |
| 210 | + // intersecting both |
| 211 | + list = saom.getObjectsTouchingBox( |
| 212 | + {1.9 * BS, -1.0 * BS, -1.0 * BS, 2.5 * BS, 2.1 * BS, 1.0 * BS}); |
| 213 | + UASSERT(list.size() == 2); |
| 214 | +} |
0 commit comments