2011-09-18 01:33:07 +01:00
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "EntityRuleHandler.h"
|
|
|
|
|
|
|
|
|
|
#include "EntityBuilder.h"
|
2013-10-07 23:01:30 +02:00
|
|
|
#include "EntityFactory.h"
|
2011-09-18 01:33:07 +01:00
|
|
|
#include "Player.h"
|
|
|
|
|
|
2011-12-15 18:26:37 +00:00
|
|
|
#include "rulesets/PythonScriptFactory.h"
|
2011-09-18 01:33:07 +01:00
|
|
|
|
|
|
|
|
#include "common/log.h"
|
|
|
|
|
#include "common/debug.h"
|
|
|
|
|
#include "common/compose.hpp"
|
2011-12-21 00:26:43 +00:00
|
|
|
#include "common/EntityKit.h"
|
2011-09-18 01:33:07 +01:00
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
using Atlas::Message::Element;
|
|
|
|
|
using Atlas::Message::MapType;
|
|
|
|
|
using Atlas::Message::ListType;
|
|
|
|
|
using Atlas::Objects::Root;
|
|
|
|
|
|
|
|
|
|
using String::compose;
|
|
|
|
|
|
|
|
|
|
static const bool debug_flag = false;
|
|
|
|
|
|
|
|
|
|
int EntityRuleHandler::installEntityClass(const std::string & class_name,
|
|
|
|
|
const std::string & parent,
|
|
|
|
|
const Root & class_desc,
|
|
|
|
|
std::string & dependent,
|
|
|
|
|
std::string & reason)
|
|
|
|
|
{
|
|
|
|
|
assert(class_name == class_desc->getId());
|
|
|
|
|
|
|
|
|
|
// Get the new factory for this rule
|
2013-10-07 23:01:30 +02:00
|
|
|
EntityFactoryBase * parent_factory = dynamic_cast<EntityFactoryBase*>(m_builder->getClassFactory(parent));
|
2011-09-18 01:33:07 +01:00
|
|
|
if (parent_factory == 0) {
|
|
|
|
|
debug(std::cout << "class \"" << class_name
|
|
|
|
|
<< "\" has non existant parent \"" << parent
|
|
|
|
|
<< "\". Waiting." << std::endl << std::flush;);
|
|
|
|
|
dependent = parent;
|
|
|
|
|
reason = compose("Entity rule \"%1\" has parent \"%2\" which does "
|
|
|
|
|
"not exist.", class_name, parent);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2013-10-07 23:01:30 +02:00
|
|
|
EntityFactoryBase * factory = parent_factory->duplicateFactory();
|
2011-09-18 01:33:07 +01:00
|
|
|
if (factory == 0) {
|
|
|
|
|
log(ERROR,
|
|
|
|
|
compose("Attempt to install rule \"%1\" which has parent \"%2\" "
|
|
|
|
|
"which cannot be instantiated", class_name, parent));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(factory->m_parent == parent_factory);
|
|
|
|
|
|
|
|
|
|
if (populateEntityFactory(class_name, factory,
|
|
|
|
|
class_desc->asMessage(),
|
|
|
|
|
dependent,
|
|
|
|
|
reason) != 0) {
|
|
|
|
|
delete factory;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug(std::cout << "INSTALLING " << class_name << ":" << parent
|
|
|
|
|
<< std::endl << std::flush;);
|
|
|
|
|
|
|
|
|
|
// Install the factory in place.
|
2013-02-21 21:53:11 +00:00
|
|
|
if (m_builder->installFactory(class_name, class_desc, factory) != 0) {
|
|
|
|
|
delete factory;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-09-18 01:33:07 +01:00
|
|
|
|
|
|
|
|
factory->addProperties();
|
|
|
|
|
|
|
|
|
|
// Add it as a child to its parent.
|
|
|
|
|
parent_factory->m_children.insert(factory);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EntityRuleHandler::modifyEntityClass(const std::string & class_name,
|
|
|
|
|
const Root & class_desc)
|
|
|
|
|
{
|
|
|
|
|
assert(class_name == class_desc->getId());
|
|
|
|
|
|
2013-10-07 23:01:30 +02:00
|
|
|
EntityFactoryBase* factory = dynamic_cast<EntityFactoryBase*>(m_builder->getClassFactory(class_name));
|
2011-09-18 01:33:07 +01:00
|
|
|
if (factory == 0) {
|
|
|
|
|
log(ERROR, compose("Could not find factory for existing entity class "
|
|
|
|
|
"\"%1\".", class_name));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
assert(factory != 0);
|
|
|
|
|
|
|
|
|
|
MapType backup_attributes = factory->m_attributes,
|
|
|
|
|
backup_class_attributes = factory->m_classAttributes;
|
|
|
|
|
|
|
|
|
|
// Copy the defaults from the parent. In populateEntityFactory this may be
|
|
|
|
|
// overriden with the defaults for this class.
|
|
|
|
|
if (factory->m_parent != 0) {
|
|
|
|
|
factory->m_attributes = factory->m_parent->m_attributes;
|
|
|
|
|
} else {
|
|
|
|
|
// This is non fatal, but nice to know it has happened.
|
|
|
|
|
// This should only happen if the client attempted to modify the
|
|
|
|
|
// type data for a core hard coded type.
|
|
|
|
|
log(ERROR, compose("EntityRuleHandler::modifyEntityClass: \"%1\" modified "
|
|
|
|
|
"by client, but has no parent factory.",
|
|
|
|
|
class_name));
|
|
|
|
|
factory->m_attributes = MapType();
|
|
|
|
|
}
|
|
|
|
|
factory->m_classAttributes = MapType();
|
|
|
|
|
|
|
|
|
|
std::string dependent, reason;
|
|
|
|
|
if (populateEntityFactory(class_name, factory,
|
|
|
|
|
class_desc->asMessage(),
|
|
|
|
|
dependent, reason) != 0) {
|
|
|
|
|
factory->m_attributes = backup_attributes;
|
|
|
|
|
factory->m_classAttributes = backup_class_attributes;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-13 00:40:43 +00:00
|
|
|
factory->updateProperties();
|
2011-09-18 01:33:07 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int EntityRuleHandler::populateEntityFactory(const std::string & class_name,
|
2013-10-07 23:01:30 +02:00
|
|
|
EntityFactoryBase * factory,
|
2011-09-18 01:33:07 +01:00
|
|
|
const MapType & class_desc,
|
|
|
|
|
std::string & dependent,
|
|
|
|
|
std::string & reason)
|
|
|
|
|
{
|
|
|
|
|
// assert(class_name == class_desc->getId());
|
|
|
|
|
// Establish whether this rule has an associated script, and
|
|
|
|
|
// if so, use it.
|
|
|
|
|
MapType::const_iterator J = class_desc.find("script");
|
|
|
|
|
MapType::const_iterator Jend = class_desc.end();
|
|
|
|
|
if (J != Jend && J->second.isMap()) {
|
|
|
|
|
const MapType & script = J->second.asMap();
|
2011-09-19 00:40:40 +01:00
|
|
|
std::string script_package;
|
|
|
|
|
std::string script_class;
|
|
|
|
|
if (getScriptDetails(script, class_name, "Entity",
|
|
|
|
|
script_package, script_class) != 0) {
|
2011-09-18 01:33:07 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
2011-12-18 21:09:48 +00:00
|
|
|
if (factory->m_scriptFactory == 0 ||
|
|
|
|
|
factory->m_scriptFactory->package() != script_package) {
|
2013-01-17 10:17:07 +00:00
|
|
|
PythonScriptFactory<LocatedEntity> * psf =
|
|
|
|
|
new PythonScriptFactory<LocatedEntity>(script_package,
|
|
|
|
|
script_class);
|
2011-11-08 19:55:51 +00:00
|
|
|
if (psf->setup() == 0) {
|
2011-12-18 21:09:48 +00:00
|
|
|
delete factory->m_scriptFactory;
|
2011-09-18 01:33:07 +01:00
|
|
|
factory->m_scriptFactory = psf;
|
|
|
|
|
} else {
|
2011-11-08 19:55:51 +00:00
|
|
|
log(ERROR, compose("Python class \"%1.%2\" failed to load",
|
|
|
|
|
script_package, script_class));
|
2011-09-18 01:33:07 +01:00
|
|
|
delete psf;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-12-18 20:48:01 +00:00
|
|
|
} else {
|
|
|
|
|
// FIXME If this fails, that's bad.
|
|
|
|
|
factory->m_scriptFactory->refreshClass();
|
2011-09-18 01:33:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store the default attribute for entities create by this rule.
|
|
|
|
|
J = class_desc.find("attributes");
|
|
|
|
|
if (J != Jend && J->second.isMap()) {
|
|
|
|
|
const MapType & attrs = J->second.asMap();
|
|
|
|
|
MapType::const_iterator Kend = attrs.end();
|
|
|
|
|
for (MapType::const_iterator K = attrs.begin(); K != Kend; ++K) {
|
|
|
|
|
if (!K->second.isMap()) {
|
|
|
|
|
log(ERROR, compose("Attribute description in rule %1 is not a "
|
|
|
|
|
"map.", class_name));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const MapType & attr = K->second.asMap();
|
|
|
|
|
MapType::const_iterator L = attr.find("default");
|
|
|
|
|
if (L != attr.end()) {
|
|
|
|
|
// Store this value in the defaults for this class
|
|
|
|
|
factory->m_classAttributes[K->first] = L->second;
|
|
|
|
|
// and merge it with the defaults inherited from the parent
|
|
|
|
|
factory->m_attributes[K->first] = L->second;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check whether it should be available to players as a playable character.
|
|
|
|
|
J = class_desc.find("playable");
|
|
|
|
|
if (J != Jend && J->second.isInt()) {
|
|
|
|
|
Player::playableTypes.insert(class_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EntityRuleHandler::check(const Atlas::Objects::Root & desc)
|
|
|
|
|
{
|
2011-09-21 11:18:30 +01:00
|
|
|
assert(!desc->getParents().empty());
|
2011-09-18 01:33:07 +01:00
|
|
|
if (desc->getObjtype() != "class") {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return m_builder->isTask(desc->getParents().front()) ? -1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EntityRuleHandler::install(const std::string & name,
|
|
|
|
|
const std::string & parent,
|
|
|
|
|
const Atlas::Objects::Root & description,
|
|
|
|
|
std::string & dependent,
|
|
|
|
|
std::string & reason)
|
|
|
|
|
{
|
|
|
|
|
return installEntityClass(name, parent, description, dependent, reason);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EntityRuleHandler::update(const std::string & name,
|
|
|
|
|
const Atlas::Objects::Root & desc)
|
|
|
|
|
{
|
|
|
|
|
return modifyEntityClass(name, desc);
|
|
|
|
|
}
|