cyphesis/server/CorePropertyManager.cpp
Al Riddoch fab8a833ff 2008-09-08 Al Riddoch <alriddoch@googlemail.com>
* rulesets/ActivePropertyFactory_impl.h: Re-work the active property
	  factories so they just pass handler information to the property
	  classes rather than installing it themselves, so the property
	  can now handle installing the handlers each time they are installed
	  on an entity, making class default properties work.

	* client/ClientPropertyManager.cpp, client/ClientPropertyManager.h,
	  common/PropertyFactory.h, common/PropertyFactory_impl.h,
	  common/PropertyManager.h, rulesets/ActivePropertyFactory.h,
	  server/CorePropertyManager.cpp, server/CorePropertyManager.h:
	  Modify the property manager and property factory interfaces so they
	  do not take a pointer to the owner, as this is never required any
	  more.

	* rulesets/Character.cpp, rulesets/Entity.cpp: Modify code that uses
	  property managers to use the new interface.

	* rulesets/SolidProperty.cpp, rulesets/SolidProperty.h: Make use of
	  the bool flag to implement this, preventing it from being tied to
	  having a pointer to the owner at creation time.

	* rulesets/StatusProperty.cpp, rulesets/StatusProperty.h: Remove
	  the need for an owner pointer at construction time by using the
	  apply method.

	* rulesets/TerrainModProperty.cpp, rulesets/TerrainModProperty.h:
	  Use new MultiActivePropertyManager and install method to handle
	  installing handlers rather than relying on the factory to do it.

	* server/CorePropertyManager.cpp: Change many of the property
	  factories now that none get get an owner pointer.

	* server/EntityFactory.cpp: Set up default properties on all type
	  nodes when a new class is installed, and don't apply instance
	  properties based on class defaults, relying on class properties.

	* rulesets/Entity.cpp: Remove hard coded BBox property now that the
	  functionality is handled by a more special property.
2008-09-08 00:17:08 +00:00

342 lines
11 KiB
C++

// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2006 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$
#include "CorePropertyManager.h"
#include "rulesets/ActivePropertyFactory_impl.h"
#include "rulesets/LineProperty.h"
#include "rulesets/OutfitProperty.h"
#include "rulesets/SolidProperty.h"
#include "rulesets/StatusProperty.h"
#include "rulesets/Entity.h"
#include "rulesets/TerrainModProperty.h"
#include "rulesets/BBoxProperty.h"
#include "common/Eat.h"
#include "common/Burn.h"
#include "common/Nourish.h"
#include "common/Setup.h"
#include "common/types.h"
#include "common/PropertyFactory_impl.h"
#include "common/DynamicProperty_impl.h"
#include "common/debug.h"
#include <Atlas/Objects/Operation.h>
#include <Atlas/Objects/Anonymous.h>
#include <wfmath/atlasconv.h>
#include <iostream>
using Atlas::Objects::Root;
using Atlas::Message::Element;
using Atlas::Objects::Operation::Set;
using Atlas::Objects::Operation::Burn;
using Atlas::Objects::Operation::Create;
using Atlas::Objects::Operation::Nourish;
using Atlas::Objects::Entity::Anonymous;
static const bool debug_flag = false;
template class PropertyFactory<Dynamic<LineProperty, CoordList> >;
HandlerResult test_handler(Entity *, const Operation &, OpVector & res)
{
debug(std::cout << "TEST HANDLER CALLED" << std::endl << std::flush;);
return OPERATION_IGNORED;
}
HandlerResult del_handler(Entity * e, const Operation &, OpVector & res)
{
debug(std::cout << "Delete HANDLER CALLED" << std::endl << std::flush;);
PropertyBase * pb = e->getProperty("decays");
if (pb == NULL) {
debug(std::cout << "Delete HANDLER no decays" << std::endl
<< std::flush;);
return OPERATION_IGNORED;
}
Element val;
pb->get(val);
if (!val.isString()) {
debug(std::cout << "Delete HANDLER decays non-string" << std::endl
<< std::flush;);
return OPERATION_IGNORED;
}
const std::string & type = val.String();
Anonymous create_arg;
create_arg->setParents(std::list<std::string>(1, type));
::addToEntity(e->m_location.pos(), create_arg->modifyPos());
create_arg->setLoc(e->m_location.m_loc->getId());
create_arg->setAttr("orientation", e->m_location.orientation().toAtlas());
Create create;
create->setTo(e->m_location.m_loc->getId());
create->setArgs1(create_arg);
res.push_back(create);
return OPERATION_IGNORED;
}
HandlerResult eat_handler(Entity * e, const Operation & op, OpVector & res)
{
PropertyBase * pb = e->getProperty("biomass");
if (pb == NULL) {
debug(std::cout << "Eat HANDLER no biomass" << std::endl
<< std::flush;);
return OPERATION_IGNORED;
}
Element val;
pb->get(val);
if (!val.isNum()) {
debug(std::cout << "Eat HANDLER biomass non-float" << std::endl
<< std::flush;);
return OPERATION_IGNORED;
}
double biomass = val.asNum();
Anonymous self;
self->setId(e->getId());
self->setAttr("status", -1);
Set s;
s->setTo(e->getId());
s->setArgs1(self);
const std::string & to = op->getFrom();
Anonymous nour_arg;
nour_arg->setId(to);
nour_arg->setAttr("mass", biomass);
Nourish n;
n->setTo(to);
n->setArgs1(nour_arg);
res.push_back(s);
res.push_back(n);
return OPERATION_IGNORED;
}
HandlerResult burn_handler(Entity * e, const Operation & op, OpVector & res)
{
if (op->getArgs().empty()) {
e->error(op, "Fire op has no argument", res, e->getId());
return OPERATION_IGNORED;
}
PropertyBase * pb = e->getProperty("burn_speed");
if (pb == NULL) {
debug(std::cout << "Eat HANDLER no burn_speed" << std::endl
<< std::flush;);
return OPERATION_IGNORED;
}
Element val;
pb->get(val);
if (!val.isNum()) {
debug(std::cout << "Burn HANDLER burn_speed non-float" << std::endl
<< std::flush;);
return OPERATION_IGNORED;
}
double burn_speed = val.asNum();
const Root & fire_ent = op->getArgs().front();
double consumed = burn_speed * fire_ent->getAttr("status").asNum();
const std::string & to = fire_ent->getId();
Anonymous nour_ent;
nour_ent->setId(to);
nour_ent->setAttr("mass", consumed);
Set s;
s->setTo(e->getId());
Element new_status;
PropertyBase * status = e->getProperty("status");
if (status == 0 || !status->get(new_status) || new_status.isNum()) {
new_status = 1.f;
}
Element mass_attr(1.f);
e->getAttr("mass", mass_attr);
if (!mass_attr.isFloat()) {
mass_attr = 1.f;
}
new_status = new_status.asNum() - (consumed / mass_attr.Float());
Anonymous self_ent;
self_ent->setId(e->getId());
self_ent->setAttr("status", new_status);
s->setArgs1(self_ent);
res.push_back(s);
Nourish n;
n->setTo(to);
n->setArgs1(nour_ent);
res.push_back(n);
return OPERATION_IGNORED;
}
HandlerResult transient_handler(Entity * e,
const Operation & op,
OpVector & res)
{
Element transient_time;
if (!e->getAttr("transient", transient_time)) {
return OPERATION_IGNORED;
}
if (!transient_time.isFloat()) {
return OPERATION_IGNORED;
}
Set s;
s->setTo(e->getId());
s->setFutureSeconds(transient_time.Float());
Anonymous set_arg;
set_arg->setId(e->getId());
set_arg->setAttr("status", -1);
s->setArgs1(set_arg);
res.push_back(s);
return OPERATION_IGNORED;
}
HandlerResult terrainmod_moveHandler(Entity * e,
const Operation & op,
OpVector & res)
{
Element modifier;
if (!e->getAttr("terrainmod", modifier)) {
return OPERATION_IGNORED;
}
// Check the validity of the operation.
const std::vector<Root> & args = op->getArgs();
if (args.empty()) {
return OPERATION_IGNORED;
}
Atlas::Objects::Entity::RootEntity ent = Atlas::Objects::smart_dynamic_cast<Atlas::Objects::Entity::RootEntity>(args.front());
if (!ent.isValid()) {
return OPERATION_IGNORED;
}
if (e->getId() != ent->getId()) {
return OPERATION_IGNORED;
}
Point3D newPos(ent->getPos()[0], ent->getPos()[1], ent->getPos()[2]);
// If we have any terrain mods applied, remove them from the previous pos and apply them to the new one
if (e->hasAttr("terrainmod")) {
dynamic_cast<TerrainModProperty*>(e->getProperty("terrainmod"))->move(e, newPos);
}
return OPERATION_IGNORED;
}
HandlerResult terrainmod_setupHandler(Entity *e,
const Operation & op,
OpVector & res)
{
Element modifier;
if (!e->hasAttr("terrainmod")) {
return OPERATION_IGNORED;
}
// initialize the property with its entity's position
if (e->hasAttr("terrainmod")) {
dynamic_cast<TerrainModProperty*>(e->getProperty("terrainmod"))->setup(e);
}
return OPERATION_IGNORED;
}
HandlerResult terrainmod_deleteHandler(Entity * e,
const Operation & op,
OpVector & res)
{
Element modifier;
if (!e->getAttr("terrainmod", modifier)) {
return OPERATION_IGNORED;
}
// If we have any terrain mods applied, remove them from the previous pos and apply them to the new one
if (e->hasAttr("terrainmod")) {
dynamic_cast<TerrainModProperty*>(e->getProperty("terrainmod"))->remove();
}
return OPERATION_IGNORED;
}
CorePropertyManager::CorePropertyManager()
{
m_propertyFactories["stamina"] = new PropertyFactory<DynamicProperty<double> >;
m_propertyFactories["coords"] = new PropertyFactory<Dynamic<LineProperty, CoordList> >;
m_propertyFactories["points"] = new PropertyFactory<Dynamic<LineProperty, CoordList> >;
m_propertyFactories["start_intersections"] = new PropertyFactory<DynamicProperty<IdList> >;
m_propertyFactories["end_intersections"] = new PropertyFactory<DynamicProperty<IdList> >;
m_propertyFactories["attachment"] = new ActivePropertyFactory<int>(Atlas::Objects::Operation::MOVE_NO, test_handler);
m_propertyFactories["decays"] = new ActivePropertyFactory<std::string>(Atlas::Objects::Operation::DELETE_NO, del_handler);
m_propertyFactories["outfit"] = new PropertyFactory<OutfitProperty>;
m_propertyFactories["solid"] = new PropertyFactory<SolidProperty>;
m_propertyFactories["status"] = new PropertyFactory<StatusProperty>;
m_propertyFactories["biomass"] = new ActivePropertyFactory<double>(Atlas::Objects::Operation::EAT_NO, eat_handler);
m_propertyFactories["burn_speed"] = new ActivePropertyFactory<double>(Atlas::Objects::Operation::BURN_NO, burn_handler);
m_propertyFactories["transient"] = new ActivePropertyFactory<double>(Atlas::Objects::Operation::SETUP_NO, transient_handler);
m_propertyFactories["food"] = new PropertyFactory<DynamicProperty<double> >;
m_propertyFactories["mass"] = new PropertyFactory<DynamicProperty<double> >;
m_propertyFactories["bbox"] = new PropertyFactory<BBoxProperty>;
// m_propertyFactories["terrainmod"] = new EntityPropertyFactory<TerrainModProperty>;
// m_propertyFactories["terrainmod"] = new ActivePropertyFactory<Dynamic<TerrainModProperty, Entity*> >(Atlas::Objects::Operation::MOVE_NO, terrainmod_handler);
HandlerMap terrainModHandles;
terrainModHandles[Atlas::Objects::Operation::MOVE_NO] = terrainmod_moveHandler;
terrainModHandles[Atlas::Objects::Operation::DELETE_NO] = terrainmod_deleteHandler;
terrainModHandles[Atlas::Objects::Operation::SETUP_NO] = terrainmod_setupHandler;
m_propertyFactories["terrainmod"] = new MultiActivePropertyFactory<TerrainModProperty>(terrainModHandles);
}
CorePropertyManager::~CorePropertyManager()
{
std::map<std::string, PropertyKit *>::const_iterator I = m_propertyFactories.begin();
std::map<std::string, PropertyKit *>::const_iterator Iend = m_propertyFactories.end();
for (; I != Iend; ++I) {
assert(I->second != 0);
delete I->second;
}
}
PropertyBase * CorePropertyManager::addProperty(const std::string & name)
{
assert(!name.empty());
assert(name != "objtype");
PropertyFactoryDict::const_iterator I = m_propertyFactories.find(name);
if (I == m_propertyFactories.end()) {
return 0;
}
debug(std::cout << name << " property found. " << std::endl << std::flush;);
PropertyBase * p = I->second->newProperty();
return p;
}