cyphesis/tests/Creatortest.cpp
2013-02-22 09:57:10 +00:00

888 lines
16 KiB
C++

// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2003 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
// $Id$
#ifdef NDEBUG
#undef NDEBUG
#endif
#ifndef DEBUG
#define DEBUG
#endif
#include "IGEntityExerciser.h"
#include "TestBase.h"
#include "allOperations.h"
#include "TestWorld.h"
#include "rulesets/Creator.h"
#include "rulesets/Python_API.h"
#include "rulesets/Script.h"
#include "modules/EntityRef.h"
#include "common/id.h"
#include "common/log.h"
#include "common/Property_impl.h"
#include "common/TypeNode.h"
#include <cstdlib>
#include <cassert>
using Atlas::Message::MapType;
using Atlas::Message::ListType;
using Atlas::Objects::Operation::RootOperation;
class Creatortest : public Cyphesis::TestBase
{
private:
static Operation m_Character_filterExternalOperation_called;
Creator * m_creator;
TypeNode * m_type;
public:
Creatortest();
void setup();
void teardown();
void test_exerciser();
void test_externalOperation_normal();
static void Character_filterExternalOperation_called(const Operation & op);
};
Operation Creatortest::m_Character_filterExternalOperation_called(0);
void Creatortest::Character_filterExternalOperation_called(const Operation & op)
{
m_Character_filterExternalOperation_called = op;
}
Creatortest::Creatortest()
{
ADD_TEST(Creatortest::test_exerciser);
ADD_TEST(Creatortest::test_externalOperation_normal);
}
void Creatortest::setup()
{
m_creator = new Creator("1", 1);
m_type = new TypeNode("character");
m_creator->setType(m_type);
m_Character_filterExternalOperation_called = 0;
}
void Creatortest::teardown()
{
delete m_creator;
delete m_type;
}
void Creatortest::test_exerciser()
{
IGEntityExerciser ee(*m_creator);
// Throw an op of every type at the entity
ee.runOperations();
// Subscribe the entity to every class of op
std::set<std::string> opNames;
ee.addAllOperations(opNames);
// Throw an op of every type at the entity again now it is subscribed
ee.runOperations();
}
void Creatortest::test_externalOperation_normal()
{
RootOperation op;
op->setFrom(m_creator->getId());
m_creator->externalOperation(op, *(Link*)0);
ASSERT_TRUE(m_Character_filterExternalOperation_called.isValid());
ASSERT_EQUAL(m_Character_filterExternalOperation_called->getClassNo(),
Atlas::Objects::Operation::ROOT_OPERATION_NO);
}
int main(int argc, char ** argv)
{
Creatortest t;
return t.run();
}
// stubs
void TestWorld::message(const Operation & op, LocatedEntity & ent)
{
}
LocatedEntity * TestWorld::addNewEntity(const std::string &,
const Atlas::Objects::Entity::RootEntity &)
{
return 0;
}
namespace Atlas { namespace Objects { namespace Operation {
int ACTUATE_NO = -1;
int ATTACK_NO = -1;
int EAT_NO = -1;
int GOAL_INFO_NO = -1;
int NOURISH_NO = -1;
int SETUP_NO = -1;
int TICK_NO = -1;
int THOUGHT_NO = -1;
int UNSEEN_NO = -1;
int UPDATE_NO = -1;
} } }
Character::Character(const std::string & id, long intId) :
Thing(id, intId),
m_movement(*(Movement*)0),
m_mind(0), m_externalMind(0)
{
}
Character::~Character()
{
}
void Character::operation(const Operation & op, OpVector &)
{
}
void Character::externalOperation(const Operation & op, Link &)
{
filterExternalOperation(op);
}
void Character::filterExternalOperation(const Operation & op)
{
Creatortest::Character_filterExternalOperation_called(op);
}
void Character::ImaginaryOperation(const Operation & op, OpVector &)
{
}
void Character::InfoOperation(const Operation & op, OpVector &)
{
}
void Character::TickOperation(const Operation & op, OpVector &)
{
}
void Character::TalkOperation(const Operation & op, OpVector &)
{
}
void Character::NourishOperation(const Operation & op, OpVector &)
{
}
void Character::UseOperation(const Operation & op, OpVector &)
{
}
void Character::WieldOperation(const Operation & op, OpVector &)
{
}
void Character::AttackOperation(const Operation & op, OpVector &)
{
}
void Character::ActuateOperation(const Operation & op, OpVector &)
{
}
void Character::mindActuateOperation(const Operation &, OpVector &)
{
}
void Character::mindAttackOperation(const Operation &, OpVector &)
{
}
void Character::mindCombineOperation(const Operation &, OpVector &)
{
}
void Character::mindCreateOperation(const Operation &, OpVector &)
{
}
void Character::mindDeleteOperation(const Operation &, OpVector &)
{
}
void Character::mindDivideOperation(const Operation &, OpVector &)
{
}
void Character::mindEatOperation(const Operation &, OpVector &)
{
}
void Character::mindGoalInfoOperation(const Operation &, OpVector &)
{
}
void Character::mindImaginaryOperation(const Operation &, OpVector &)
{
}
void Character::mindLookOperation(const Operation &, OpVector &)
{
}
void Character::mindMoveOperation(const Operation &, OpVector &)
{
}
void Character::mindSetOperation(const Operation &, OpVector &)
{
}
void Character::mindSetupOperation(const Operation &, OpVector &)
{
}
void Character::mindTalkOperation(const Operation &, OpVector &)
{
}
void Character::mindThoughtOperation(const Operation &, OpVector &)
{
}
void Character::mindTickOperation(const Operation &, OpVector &)
{
}
void Character::mindTouchOperation(const Operation &, OpVector &)
{
}
void Character::mindUpdateOperation(const Operation &, OpVector &)
{
}
void Character::mindUseOperation(const Operation &, OpVector &)
{
}
void Character::mindWieldOperation(const Operation &, OpVector &)
{
}
void Character::mindOtherOperation(const Operation &, OpVector &)
{
}
void Character::sendMind(const Operation & op, OpVector & res)
{
}
Thing::Thing(const std::string & id, long intId) :
Entity(id, intId)
{
}
Thing::~Thing()
{
}
void Thing::DeleteOperation(const Operation & op, OpVector & res)
{
}
void Thing::MoveOperation(const Operation & op, OpVector & res)
{
}
void Thing::SetOperation(const Operation & op, OpVector & res)
{
}
void Thing::LookOperation(const Operation & op, OpVector & res)
{
}
void Thing::CreateOperation(const Operation & op, OpVector & res)
{
}
void Thing::UpdateOperation(const Operation & op, OpVector & res)
{
}
Entity::Entity(const std::string & id, long intId) :
LocatedEntity(id, intId), m_motion(0)
{
}
Entity::~Entity()
{
}
void Entity::destroy()
{
destroyed.emit();
}
void Entity::ActuateOperation(const Operation &, OpVector &)
{
}
void Entity::AppearanceOperation(const Operation &, OpVector &)
{
}
void Entity::AttackOperation(const Operation &, OpVector &)
{
}
void Entity::CombineOperation(const Operation &, OpVector &)
{
}
void Entity::CreateOperation(const Operation &, OpVector &)
{
}
void Entity::DeleteOperation(const Operation &, OpVector &)
{
}
void Entity::DisappearanceOperation(const Operation &, OpVector &)
{
}
void Entity::DivideOperation(const Operation &, OpVector &)
{
}
void Entity::EatOperation(const Operation &, OpVector &)
{
}
void Entity::GetOperation(const Operation &, OpVector &)
{
}
void Entity::InfoOperation(const Operation &, OpVector &)
{
}
void Entity::ImaginaryOperation(const Operation &, OpVector &)
{
}
void Entity::LookOperation(const Operation &, OpVector &)
{
}
void Entity::MoveOperation(const Operation &, OpVector &)
{
}
void Entity::NourishOperation(const Operation &, OpVector &)
{
}
void Entity::SetOperation(const Operation &, OpVector &)
{
}
void Entity::SightOperation(const Operation &, OpVector &)
{
}
void Entity::SoundOperation(const Operation &, OpVector &)
{
}
void Entity::TalkOperation(const Operation &, OpVector &)
{
}
void Entity::TickOperation(const Operation &, OpVector &)
{
}
void Entity::TouchOperation(const Operation &, OpVector &)
{
}
void Entity::UpdateOperation(const Operation &, OpVector &)
{
}
void Entity::UseOperation(const Operation &, OpVector &)
{
}
void Entity::WieldOperation(const Operation &, OpVector &)
{
}
void Entity::externalOperation(const Operation & op, Link &)
{
}
void Entity::operation(const Operation & op, OpVector & res)
{
}
void Entity::addToMessage(Atlas::Message::MapType & omap) const
{
}
void Entity::addToEntity(const Atlas::Objects::Entity::RootEntity & ent) const
{
}
PropertyBase * Entity::setAttr(const std::string & name,
const Atlas::Message::Element & attr)
{
return 0;
}
const PropertyBase * Entity::getProperty(const std::string & name) const
{
return 0;
}
PropertyBase * Entity::setProperty(const std::string & name,
PropertyBase * prop)
{
return 0;
}
void Entity::installDelegate(int class_no, const std::string & delegate)
{
}
Domain * Entity::getMovementDomain()
{
return 0;
}
void Entity::sendWorld(const Operation & op)
{
}
PropertyBase * Entity::modProperty(const std::string & name)
{
return 0;
}
void Entity::onContainered()
{
}
void Entity::onUpdated()
{
}
void Entity::callOperation(const Operation & op, OpVector & res)
{
}
LocatedEntity::LocatedEntity(const std::string & id, long intId) :
Router(id, intId),
m_refCount(0), m_seq(0),
m_script(0), m_type(0), m_flags(0), m_contains(0)
{
}
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::destroy()
{
}
Domain * LocatedEntity::getMovementDomain()
{
return 0;
}
void LocatedEntity::sendWorld(const Operation & op)
{
}
void LocatedEntity::onContainered()
{
}
void LocatedEntity::onUpdated()
{
}
void LocatedEntity::makeContainer()
{
if (m_contains == 0) {
m_contains = new LocatedEntitySet;
}
}
void LocatedEntity::merge(const MapType & ent)
{
}
Router::Router(const std::string & id, long intId) : m_id(id),
m_intId(intId)
{
}
Router::~Router()
{
}
void Router::addToMessage(Atlas::Message::MapType & omap) const
{
}
void Router::addToEntity(const Atlas::Objects::Entity::RootEntity & ent) const
{
}
void Router::error(const Operation & op,
const std::string & errstring,
OpVector & res,
const std::string & to) const
{
}
BaseWorld * BaseWorld::m_instance = 0;
BaseWorld::BaseWorld(LocatedEntity & gw) : m_gameWorld(gw)
{
m_instance = this;
}
BaseWorld::~BaseWorld()
{
m_instance = 0;
}
LocatedEntity * BaseWorld::getEntity(const std::string & id) const
{
long intId = integerId(id);
EntityDict::const_iterator I = m_eobjects.find(intId);
if (I != m_eobjects.end()) {
assert(I->second != 0);
return I->second;
} else {
return 0;
}
}
LocatedEntity * BaseWorld::getEntity(long id) const
{
EntityDict::const_iterator I = m_eobjects.find(id);
if (I != m_eobjects.end()) {
assert(I->second != 0);
return I->second;
} else {
return 0;
}
}
Script::Script()
{
}
/// \brief Script destructor
Script::~Script()
{
}
bool Script::operation(const std::string & opname,
const Atlas::Objects::Operation::RootOperation & op,
OpVector & res)
{
return false;
}
void Script::hook(const std::string & function, LocatedEntity * entity)
{
}
void Location::addToMessage(MapType & omap) const
{
}
Location::Location() : m_loc(0)
{
}
Location::Location(LocatedEntity * rf, const Point3D & pos)
{
}
void Location::addToEntity(const Atlas::Objects::Entity::RootEntity & ent) const
{
}
TypeNode::TypeNode(const std::string & name) : m_name(name), m_parent(0)
{
}
TypeNode::~TypeNode()
{
}
PropertyBase::PropertyBase(unsigned int flags) : m_flags(flags)
{
}
PropertyBase::~PropertyBase()
{
}
void PropertyBase::install(LocatedEntity *, const std::string & name)
{
}
void PropertyBase::apply(LocatedEntity *)
{
}
void PropertyBase::add(const std::string & s,
Atlas::Message::MapType & ent) const
{
get(ent[s]);
}
void PropertyBase::add(const std::string & s,
const Atlas::Objects::Entity::RootEntity & ent) const
{
}
HandlerResult PropertyBase::operation(LocatedEntity *,
const Operation &,
OpVector &)
{
return OPERATION_IGNORED;
}
template<>
void Property<int>::set(const Atlas::Message::Element & e)
{
if (e.isInt()) {
this->m_data = e.asInt();
}
}
template<>
void Property<double>::set(const Atlas::Message::Element & e)
{
if (e.isNum()) {
this->m_data = e.asNum();
}
}
template<>
void Property<std::string>::set(const Atlas::Message::Element & e)
{
if (e.isString()) {
this->m_data = e.String();
}
}
template class Property<int>;
template class Property<double>;
template class Property<std::string>;
SoftProperty::SoftProperty()
{
}
SoftProperty::SoftProperty(const Atlas::Message::Element & data) :
PropertyBase(0), m_data(data)
{
}
int SoftProperty::get(Atlas::Message::Element & val) const
{
val = m_data;
return 0;
}
void SoftProperty::set(const Atlas::Message::Element & val)
{
}
SoftProperty * SoftProperty::copy() const
{
return 0;
}
PropertyManager * PropertyManager::m_instance = 0;
PropertyManager::PropertyManager()
{
assert(m_instance == 0);
m_instance = this;
}
PropertyManager::~PropertyManager()
{
m_instance = 0;
}
int PropertyManager::installFactory(const std::string & type_name,
const Atlas::Objects::Root & type_desc,
PropertyKit * factory)
{
return 0;
}
void log(LogLevel lvl, const std::string & msg)
{
}
long integerId(const std::string & id)
{
long intId = strtol(id.c_str(), 0, 10);
if (intId == 0 && id != "0") {
intId = -1L;
}
return intId;
}
template <typename FloatT>
int fromStdVector(Point3D & p, const std::vector<FloatT> & vf)
{
if (vf.size() != 3) {
return -1;
}
p[0] = vf[0];
p[1] = vf[1];
p[2] = vf[2];
p.setValid();
return 0;
}
template <typename FloatT>
int fromStdVector(Vector3D & v, const std::vector<FloatT> & vf)
{
if (vf.size() != 3) {
return -1;
}
v[0] = vf[0];
v[1] = vf[1];
v[2] = vf[2];
v.setValid();
return 0;
}
template int fromStdVector<double>(Point3D & p, const std::vector<double> & vf);
template int fromStdVector<double>(Vector3D & v, const std::vector<double> & vf);
EntityRef::EntityRef(LocatedEntity* e) : m_inner(e)
{
}
EntityRef::EntityRef(const EntityRef& ref) : m_inner(ref.m_inner)
{
}
EntityRef& EntityRef::operator=(const EntityRef& ref)
{
m_inner = ref.m_inner;
return *this;
}
void EntityRef::onEntityDeleted()
{
}
const Vector3D distanceTo(const Location & self, const Location & other)
{
return Vector3D(1,0,0);
}
template<class V>
const Quaternion quaternionFromTo(const V & from, const V & to)
{
return Quaternion(1.f, 0.f, 0.f, 0.f);
}
template
const Quaternion quaternionFromTo<Vector3D>(const Vector3D &, const Vector3D&);
void addToEntity(const Point3D & p, std::vector<double> & vd)
{
}