cyphesis/tests/MotionTest.cpp

542 lines
14 KiB
C++
Raw Permalink Normal View History

// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2009 Alistair Riddoch
//
// This program 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.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU 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
2011-02-15 09:30:46 +00:00
#ifdef NDEBUG
#undef NDEBUG
#endif
#ifndef DEBUG
#define DEBUG
#endif
2012-12-19 10:36:08 +00:00
#include "TestBase.h"
#include "rulesets/Motion.h"
#include "rulesets/Entity.h"
#include "rulesets/PhysicalDomain.h"
#include "rulesets/OutfitProperty.h"
#include "common/TypeNode.h"
2012-12-19 10:36:08 +00:00
class Motiontest : public Cyphesis::TestBase
{
2012-12-19 10:36:08 +00:00
protected:
Entity * tlve;
Entity * ent;
Entity * other;
TypeNode * type;
Motion * motion;
Domain* domain;
2012-12-19 10:36:08 +00:00
public:
Motiontest();
void setup();
void teardown();
void test_adjustPosition();
void test_genUpdateOperation();
void test_genMoveOperation();
void test_checkCollision_no_box();
void test_checkCollision_moving_box();
void test_checkCollision_two_boxes();
void test_checkCollision_close();
void test_checkCollision_broken();
void test_checkCollision_velocity();
void test_checkCollision_inner1();
void test_checkCollision_inner2();
void test_checkCollision_inner3();
void test_checkCollision_inner4();
};
void Motiontest::setup()
{
type = new TypeNode("test_type");
tlve = new Entity("0", 0);
domain = new PhysicalDomain(*tlve);
2012-12-19 10:36:08 +00:00
ent = new Entity("1", 1);
other = new Entity("2", 2);
2012-12-19 10:36:08 +00:00
ent->m_location.m_loc = tlve;
ent->m_location.m_pos = Point3D(1, 1, 0);
ent->m_location.m_velocity = Vector3D(1,0,0);
ent->setType(type);
// Set up another entity to test collisions with.
2012-12-19 10:36:08 +00:00
other->m_location.m_loc = tlve;
other->m_location.m_pos = Point3D(10, 0, 0);
other->setType(type);
2012-12-19 10:36:08 +00:00
tlve->m_contains = new LocatedEntitySet;
tlve->m_contains->insert(ent);
tlve->m_contains->insert(other);
tlve->setType(type);
tlve->incRef();
tlve->incRef();
2015-04-28 21:00:18 +02:00
motion = new Motion(*ent);
std::string example_mode("walking");
motion->setMode(example_mode);
assert(motion->mode() == example_mode);
2012-12-19 10:36:08 +00:00
}
Motiontest::Motiontest()
{
ADD_TEST(Motiontest::test_adjustPosition);
ADD_TEST(Motiontest::test_genUpdateOperation);
ADD_TEST(Motiontest::test_genMoveOperation);
ADD_TEST(Motiontest::test_checkCollision_no_box);
ADD_TEST(Motiontest::test_checkCollision_moving_box);
ADD_TEST(Motiontest::test_checkCollision_two_boxes);
ADD_TEST(Motiontest::test_checkCollision_close);
ADD_TEST(Motiontest::test_checkCollision_broken);
ADD_TEST(Motiontest::test_checkCollision_velocity);
ADD_TEST(Motiontest::test_checkCollision_inner1);
ADD_TEST(Motiontest::test_checkCollision_inner2);
ADD_TEST(Motiontest::test_checkCollision_inner3);
ADD_TEST(Motiontest::test_checkCollision_inner4);
}
2012-12-19 10:36:08 +00:00
void Motiontest::teardown()
{
ent->m_location.m_loc = 0;
other->m_location.m_loc = 0;
delete motion;
delete tlve;
delete ent;
delete other;
delete type;
}
void Motiontest::test_adjustPosition()
{
2013-08-09 22:05:17 +02:00
motion->adjustPosition();
2012-12-19 10:36:08 +00:00
}
2012-12-19 10:36:08 +00:00
void Motiontest::test_genUpdateOperation()
{
motion->genUpdateOperation();
2012-12-19 10:36:08 +00:00
}
2012-12-19 10:36:08 +00:00
void Motiontest::test_genMoveOperation()
{
motion->genMoveOperation();
2012-12-19 10:36:08 +00:00
}
2012-12-19 10:36:08 +00:00
void Motiontest::test_checkCollision_no_box()
{
// No collisions yet
2015-04-28 21:00:18 +02:00
motion->checkCollisions(*domain);
2012-12-19 10:36:08 +00:00
ASSERT_TRUE(!motion->collision());
}
2012-12-19 10:36:08 +00:00
void Motiontest::test_checkCollision_moving_box()
{
// Set up our moving entity with a bbox so collisions can be checked for.
2012-12-19 10:36:08 +00:00
ent->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(1,1,1));
// No collision yet, as other still has no big box
2015-04-28 21:00:18 +02:00
motion->checkCollisions(*domain);
assert(!motion->collision());
2012-12-19 10:36:08 +00:00
}
void Motiontest::test_checkCollision_two_boxes()
{
// Set up our moving entity with a bbox so collisions can be checked for.
ent->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(1,1,1));
// Set up the other entity with a bbox so collisions can be checked for.
2012-12-19 10:36:08 +00:00
other->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(5,1,1));
// No collision yet, as other is still too far away
2015-04-28 21:00:18 +02:00
motion->checkCollisions(*domain);
assert(!motion->collision());
2012-12-19 10:36:08 +00:00
}
void Motiontest::test_checkCollision_close()
{
// Set up our moving entity with a bbox so collisions can be checked for.
ent->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(1,1,1));
// Set up the other entity with a bbox so collisions can be checked for.
other->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(5,1,1));
// Move it closer
2012-12-19 10:36:08 +00:00
other->m_location.m_pos = Point3D(3, 0, 0);
// Now it can collide
2015-04-28 21:00:18 +02:00
motion->checkCollisions(*domain);
assert(motion->collision());
motion->resolveCollision();
2012-12-19 10:36:08 +00:00
}
void Motiontest::test_checkCollision_broken()
{
// Set up our moving entity with a bbox so collisions can be checked for.
ent->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(1,1,1));
2012-12-19 10:36:08 +00:00
// Set up the other entity with a bbox so collisions can be checked for.
other->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(5,1,1));
// Move it closer
other->m_location.m_pos = Point3D(3, 0, 0);
// Set up the collision again
2015-04-28 21:00:18 +02:00
motion->checkCollisions(*domain);
assert(motion->collision());
// But this time break the hierarchy to hit the error message
2012-12-19 10:36:08 +00:00
other->m_location.m_loc = ent;
motion->resolveCollision();
2012-12-19 10:36:08 +00:00
}
void Motiontest::test_checkCollision_velocity()
{
// Set up our moving entity with a bbox so collisions can be checked for.
ent->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(1,1,1));
// Set up the other entity with a bbox so collisions can be checked for.
other->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(5,1,1));
2012-12-19 10:36:08 +00:00
// Move it closer
other->m_location.m_pos = Point3D(3, 0, 0);
// Set up the collision again
2015-04-28 21:00:18 +02:00
motion->checkCollisions(*domain);
assert(motion->collision());
// Re-align the velocity so some is preserved by the collision normal
2012-12-19 10:36:08 +00:00
ent->m_location.m_velocity = Vector3D(1,1,0);
motion->resolveCollision();
2012-12-19 10:36:08 +00:00
}
void Motiontest::test_checkCollision_inner1()
{
// Set up our moving entity with a bbox so collisions can be checked for.
ent->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(1,1,1));
// Set up the other entity with a bbox so collisions can be checked for.
other->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(5,1,1));
2012-12-19 10:36:08 +00:00
// Move it closer
other->m_location.m_pos = Point3D(3, 0, 0);
// Add another entity inside other
Entity inner("3", 3);
2012-12-19 10:36:08 +00:00
inner.m_location.m_loc = other;
inner.m_location.m_pos = Point3D(0,0,0);
2012-12-19 10:36:08 +00:00
other->m_contains = new LocatedEntitySet;
other->m_contains->insert(&inner);
// Make other non-simple so that collision checks go inside
2012-12-19 10:36:08 +00:00
other->m_location.setSimple(false);
2015-04-28 21:00:18 +02:00
motion->checkCollisions(*domain);
2012-12-19 10:36:08 +00:00
assert(motion->collision());
inner.m_location.m_loc = 0;
}
void Motiontest::test_checkCollision_inner2()
{
// Set up our moving entity with a bbox so collisions can be checked for.
ent->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(1,1,1));
// Set up the other entity with a bbox so collisions can be checked for.
other->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(5,1,1));
// Move it closer
other->m_location.m_pos = Point3D(3, 0, 0);
// Add another entity inside other
Entity inner("3", 3);
inner.m_location.m_loc = other;
inner.m_location.m_pos = Point3D(0,0,0);
other->m_contains = new LocatedEntitySet;
other->m_contains->insert(&inner);
// Make other non-simple so that collision checks go inside
other->m_location.setSimple(false);
other->m_location.m_orientation = Quaternion(1,0,0,0);
2015-04-28 21:00:18 +02:00
motion->checkCollisions(*domain);
2012-12-19 10:36:08 +00:00
assert(motion->collision());
inner.m_location.m_loc = 0;
}
void Motiontest::test_checkCollision_inner3()
{
// Set up our moving entity with a bbox so collisions can be checked for.
ent->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(1,1,1));
// Set up the other entity with a bbox so collisions can be checked for.
other->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(5,1,1));
// Move it closer
other->m_location.m_pos = Point3D(3, 0, 0);
// Add another entity inside other
Entity inner("3", 3);
inner.m_location.m_loc = other;
inner.m_location.m_pos = Point3D(0,0,0);
other->m_contains = new LocatedEntitySet;
other->m_contains->insert(&inner);
// Make other non-simple so that collision checks go inside
other->m_location.setSimple(false);
other->m_location.m_orientation = Quaternion(1,0,0,0);
inner.m_location.m_bBox = BBox(Point3D(-0.1,-0.1,-0.1),
Point3D(0.1,0.1,0.1));
2015-04-28 21:00:18 +02:00
motion->checkCollisions(*domain);
2012-12-19 10:36:08 +00:00
assert(motion->collision());
inner.m_location.m_loc = 0;
}
void Motiontest::test_checkCollision_inner4()
{
// Set up our moving entity with a bbox so collisions can be checked for.
ent->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(1,1,1));
// Set up the other entity with a bbox so collisions can be checked for.
other->m_location.m_bBox = BBox(Point3D(-1,-1,-1), Point3D(5,1,1));
// Move it closer
other->m_location.m_pos = Point3D(3, 0, 0);
// Add another entity inside other
Entity inner("3", 3);
inner.m_location.m_loc = other;
inner.m_location.m_pos = Point3D(0,0,0);
other->m_contains = new LocatedEntitySet;
other->m_contains->insert(&inner);
// Make other non-simple so that collision checks go inside
other->m_location.setSimple(false);
other->m_location.m_orientation = Quaternion(1,0,0,0);
inner.m_location.m_bBox = BBox(Point3D(-0.1,-0.1,-0.1),
Point3D(0.1,0.1,0.1));
// Move the inner entity too far away for collision this interval
inner.m_location.m_pos = Point3D(3,0,0);
2015-04-28 21:00:18 +02:00
motion->checkCollisions(*domain);
2012-12-19 10:36:08 +00:00
assert(motion->collision());
ASSERT_EQUAL(motion->m_collEntity, other);
inner.m_location.m_loc = 0;
2012-12-19 10:36:08 +00:00
}
int main()
{
Motiontest t;
2012-12-19 10:36:08 +00:00
t.run();
}
2012-12-19 22:51:37 +00:00
// stubs
#include "common/const.h"
#include "common/log.h"
#include "common/Property_impl.h"
2012-12-19 22:51:37 +00:00
#include "stubs/rulesets/stubEntity.h"
#include "stubs/rulesets/stubDomain.h"
#include "stubs/rulesets/stubTerrainProperty.h"
#include "stubs/rulesets/stubOutfitProperty.h"
#include "stubs/common/stubCustom.h"
2012-12-19 22:51:37 +00:00
LocatedEntity::LocatedEntity(const std::string & id, long intId) :
Router(id, intId),
m_refCount(0), m_seq(0),
2013-01-14 09:59:54 +00:00
m_script(0), m_type(0), m_flags(0), m_contains(0)
2012-12-19 22:51:37 +00:00
{
}
LocatedEntity::~LocatedEntity()
{
}
bool LocatedEntity::hasAttr(const std::string & name) const
{
return false;
}
int LocatedEntity::getAttr(const std::string & name,
Atlas::Message::Element & attr) const
{
return -1;
}
int LocatedEntity::getAttrType(const std::string & name,
Atlas::Message::Element & attr,
int type) const
{
return -1;
}
PropertyBase * LocatedEntity::setAttr(const std::string & name,
const Atlas::Message::Element & attr)
{
return 0;
}
const PropertyBase * LocatedEntity::getProperty(const std::string & name) const
{
return 0;
}
PropertyBase * LocatedEntity::modProperty(const std::string & name)
{
return 0;
}
PropertyBase * LocatedEntity::setProperty(const std::string & name,
PropertyBase * prop)
{
return 0;
}
void LocatedEntity::installDelegate(int, const std::string &)
{
}
void LocatedEntity::removeDelegate(int class_no, const std::string & delegate)
{
}
void LocatedEntity::destroy()
{
}
Domain * LocatedEntity::getMovementDomain()
{
return 0;
}
2016-06-22 20:53:43 +02:00
const Domain * LocatedEntity::getMovementDomain() const
{
return 0;
}
void LocatedEntity::sendWorld(const Operation & op)
{
}
void LocatedEntity::onContainered(const LocatedEntity*)
2012-12-19 22:51:37 +00:00
{
}
void LocatedEntity::onUpdated()
{
}
void LocatedEntity::makeContainer()
{
if (m_contains == 0) {
m_contains = new LocatedEntitySet;
}
}
void LocatedEntity::changeContainer(LocatedEntity * new_loc)
{
assert(m_location.m_loc != 0);
assert(m_location.m_loc->m_contains != 0);
m_location.m_loc->m_contains->erase(this);
if (m_location.m_loc->m_contains->empty()) {
m_location.m_loc->onUpdated();
}
new_loc->makeContainer();
bool was_empty = new_loc->m_contains->empty();
new_loc->m_contains->insert(this);
if (was_empty) {
new_loc->onUpdated();
}
assert(m_location.m_loc->checkRef() > 0);
LocatedEntity* oldLoc = m_location.m_loc;
2012-12-19 22:51:37 +00:00
m_location.m_loc = new_loc;
m_location.m_loc->incRef();
assert(m_location.m_loc->checkRef() > 0);
onContainered(oldLoc);
oldLoc->decRef();
2012-12-19 22:51:37 +00:00
}
void LocatedEntity::merge(const Atlas::Message::MapType & ent)
{
}
void LocatedEntity::addChild(LocatedEntity& childEntity)
2012-12-19 22:51:37 +00:00
{
}
void LocatedEntity::removeChild(LocatedEntity& childEntity)
2012-12-19 22:51:37 +00:00
{
}
2015-04-09 14:50:45 +02:00
void LocatedEntity::setType(const TypeNode* t)
{
m_type = t;
}
2015-05-28 22:05:09 +02:00
std::vector<Atlas::Objects::Root> LocatedEntity::getThoughts() const
{
return std::vector<Atlas::Objects::Root>();
}
#include "stubs/common/stubRouter.h"
#include "stubs/modules/stubLocation.h"
#include "stubs/common/stubTypeNode.h"
#include "stubs/common/stubProperty.h"
#include "rulesets/EntityProperty.h"
#include "stubs/rulesets/stubEntityProperty.h"
2012-12-19 22:51:37 +00:00
void log(LogLevel lvl, const std::string & msg)
2012-12-19 22:51:37 +00:00
{
}
WFMath::CoordType squareDistance(const Point3D & u, const Point3D & v)
2012-12-19 22:51:37 +00:00
{
return 0.0f;
2012-12-19 22:51:37 +00:00
}