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"
|
2018-04-29 16:26:32 +02:00
|
|
|
#include "common/Inheritance.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;
|
|
|
|
|
|
2018-01-19 21:35:48 +01:00
|
|
|
|
|
|
|
|
class Character;
|
|
|
|
|
|
|
|
|
|
class Creator;
|
|
|
|
|
|
|
|
|
|
class Entity;
|
|
|
|
|
|
|
|
|
|
class Plant;
|
|
|
|
|
|
|
|
|
|
class Stackable;
|
|
|
|
|
|
|
|
|
|
class Thing;
|
|
|
|
|
|
|
|
|
|
class World;
|
|
|
|
|
|
|
|
|
|
extern template
|
|
|
|
|
class EntityFactory<Character>;
|
|
|
|
|
|
|
|
|
|
extern template
|
|
|
|
|
class EntityFactory<Creator>;
|
|
|
|
|
|
|
|
|
|
extern template
|
|
|
|
|
class EntityFactory<Thing>;
|
|
|
|
|
|
|
|
|
|
extern template
|
|
|
|
|
class EntityFactory<Plant>;
|
|
|
|
|
|
|
|
|
|
extern template
|
|
|
|
|
class EntityFactory<Stackable>;
|
|
|
|
|
|
|
|
|
|
extern template
|
|
|
|
|
class EntityFactory<World>;
|
|
|
|
|
|
2011-09-18 01:33:07 +01:00
|
|
|
static const bool debug_flag = false;
|
|
|
|
|
|
2018-01-19 21:35:48 +01:00
|
|
|
|
|
|
|
|
EntityRuleHandler::EntityRuleHandler(EntityBuilder * eb)
|
|
|
|
|
: m_builder(eb)
|
|
|
|
|
{
|
|
|
|
|
|
2018-04-29 11:58:35 +02:00
|
|
|
mFactories["world"] = [](EntityFactoryBase* parent) -> EntityFactoryBase* {
|
|
|
|
|
auto factory = new EntityFactory<World>();
|
|
|
|
|
factory->m_parent = parent;
|
|
|
|
|
return factory;
|
|
|
|
|
};
|
|
|
|
|
mFactories["thing"] = [](EntityFactoryBase* parent) -> EntityFactoryBase* {
|
|
|
|
|
auto factory = new EntityFactory<Thing>();
|
|
|
|
|
factory->m_parent = parent;
|
|
|
|
|
return factory;
|
|
|
|
|
};
|
|
|
|
|
mFactories["character"] = [](EntityFactoryBase* parent) -> EntityFactoryBase* {
|
|
|
|
|
auto factory = new EntityFactory<Character>();
|
|
|
|
|
factory->m_parent = parent;
|
|
|
|
|
return factory;
|
|
|
|
|
};
|
|
|
|
|
mFactories["creator"] = [](EntityFactoryBase* parent) -> EntityFactoryBase* {
|
|
|
|
|
auto factory = new EntityFactory<Creator>();
|
|
|
|
|
factory->m_parent = parent;
|
|
|
|
|
return factory;
|
|
|
|
|
};
|
|
|
|
|
mFactories["plant"] = [](EntityFactoryBase* parent) -> EntityFactoryBase* {
|
|
|
|
|
auto factory = new EntityFactory<Plant>();
|
|
|
|
|
factory->m_parent = parent;
|
|
|
|
|
return factory;
|
|
|
|
|
};
|
|
|
|
|
mFactories["stackable"] = [](EntityFactoryBase* parent) -> EntityFactoryBase* {
|
|
|
|
|
auto factory = new EntityFactory<Stackable>();
|
|
|
|
|
factory->m_parent = parent;
|
|
|
|
|
return factory;
|
2018-01-19 21:35:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-29 11:58:35 +02:00
|
|
|
|
2011-09-18 01:33:07 +01:00
|
|
|
int EntityRuleHandler::installEntityClass(const std::string & class_name,
|
|
|
|
|
const std::string & parent,
|
|
|
|
|
const Root & class_desc,
|
|
|
|
|
std::string & dependent,
|
2018-05-08 14:32:02 +02:00
|
|
|
std::string & reason,
|
|
|
|
|
std::map<const TypeNode*, TypeNode::PropertiesUpdate>& changes)
|
2011-09-18 01:33:07 +01:00
|
|
|
{
|
|
|
|
|
assert(class_name == class_desc->getId());
|
|
|
|
|
|
2018-04-29 11:58:35 +02:00
|
|
|
EntityFactoryBase* factory;
|
|
|
|
|
if (parent == "game_entity") {
|
|
|
|
|
auto I = mFactories.find(class_name);
|
|
|
|
|
if (I != mFactories.end()) {
|
|
|
|
|
factory = I->second(nullptr);
|
|
|
|
|
} else {
|
|
|
|
|
debug(std::cout << "class \"" << class_name
|
|
|
|
|
<< "\" has non existent parent \"" << parent
|
|
|
|
|
<< "\". Waiting." << std::endl << std::flush;);
|
|
|
|
|
dependent = parent;
|
|
|
|
|
reason = compose("Entity rule \"%1\" has parent 'game_entity' and requires a "
|
|
|
|
|
"pre-defined entity factory, which could not be found.", class_name);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
auto parent_factory = dynamic_cast<EntityFactoryBase*>(m_builder->getClassFactory(parent));
|
|
|
|
|
// Get the new factory for this rule
|
|
|
|
|
if (parent_factory == nullptr) {
|
|
|
|
|
debug(std::cout << "class \"" << class_name
|
|
|
|
|
<< "\" has non existent 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto I = mFactories.find(class_name);
|
|
|
|
|
if (I != mFactories.end()) {
|
|
|
|
|
factory = I->second(parent_factory);
|
|
|
|
|
} else {
|
|
|
|
|
factory = parent_factory->duplicateFactory();
|
|
|
|
|
}
|
|
|
|
|
assert(factory->m_parent == parent_factory);
|
2011-09-18 01:33:07 +01:00
|
|
|
}
|
2018-01-19 21:35:48 +01:00
|
|
|
|
2018-05-08 14:32:02 +02:00
|
|
|
return installEntityClass(class_name, parent, class_desc, dependent, reason, factory, changes);
|
2018-04-29 11:58:35 +02:00
|
|
|
|
2018-01-19 21:35:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EntityRuleHandler::installEntityClass(const std::string & class_name,
|
|
|
|
|
const std::string & parent,
|
|
|
|
|
const Root & class_desc,
|
|
|
|
|
std::string & dependent,
|
|
|
|
|
std::string & reason,
|
2018-05-08 14:32:02 +02:00
|
|
|
EntityFactoryBase* factory,
|
|
|
|
|
std::map<const TypeNode*, TypeNode::PropertiesUpdate>& changes)
|
2018-01-19 21:35:48 +01:00
|
|
|
{
|
|
|
|
|
// Get the new factory for this rule
|
|
|
|
|
|
|
|
|
|
if (factory == nullptr) {
|
2011-09-18 01:33:07 +01:00
|
|
|
log(ERROR,
|
|
|
|
|
compose("Attempt to install rule \"%1\" which has parent \"%2\" "
|
2018-01-19 21:35:48 +01:00
|
|
|
"which cannot be instantiated", class_name, parent));
|
2011-09-18 01:33:07 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
2018-04-29 11:58:35 +02:00
|
|
|
auto parent_factory = dynamic_cast<EntityFactoryBase*>(m_builder->getClassFactory(parent));
|
2018-01-19 21:35:48 +01:00
|
|
|
if (parent_factory) {
|
|
|
|
|
// Add it as a child to its parent.
|
|
|
|
|
parent_factory->m_children.insert(factory);
|
2018-05-08 14:32:02 +02:00
|
|
|
parent_factory->updateProperties(changes);
|
2018-01-19 21:35:48 +01:00
|
|
|
}
|
2011-09-18 01:33:07 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int EntityRuleHandler::modifyEntityClass(const std::string & class_name,
|
2018-05-08 14:32:02 +02:00
|
|
|
const Root & class_desc,
|
|
|
|
|
std::map<const TypeNode*, TypeNode::PropertiesUpdate>& changes)
|
2011-09-18 01:33:07 +01:00
|
|
|
{
|
|
|
|
|
assert(class_name == class_desc->getId());
|
|
|
|
|
|
2018-04-29 11:58:35 +02:00
|
|
|
auto factory = dynamic_cast<EntityFactoryBase*>(m_builder->getClassFactory(class_name));
|
|
|
|
|
if (factory == nullptr) {
|
2011-09-18 01:33:07 +01:00
|
|
|
log(ERROR, compose("Could not find factory for existing entity class "
|
|
|
|
|
"\"%1\".", class_name));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2018-04-29 11:58:35 +02:00
|
|
|
assert(factory != nullptr);
|
2011-09-18 01:33:07 +01:00
|
|
|
|
|
|
|
|
MapType backup_attributes = factory->m_attributes,
|
|
|
|
|
backup_class_attributes = factory->m_classAttributes;
|
|
|
|
|
|
|
|
|
|
// Copy the defaults from the parent. In populateEntityFactory this may be
|
2016-09-06 22:29:36 -04:00
|
|
|
// overridden with the defaults for this class.
|
2018-04-29 11:58:35 +02:00
|
|
|
if (factory->m_parent != nullptr) {
|
2011-09-18 01:33:07 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-08 14:32:02 +02:00
|
|
|
factory->updateProperties(changes);
|
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());
|
|
|
|
|
|
|
|
|
|
// Store the default attribute for entities create by this rule.
|
2018-07-04 19:18:03 +02:00
|
|
|
auto J = class_desc.find("attributes");
|
|
|
|
|
if (J != class_desc.end() && J->second.isMap()) {
|
2011-09-18 01:33:07 +01:00
|
|
|
const MapType & attrs = J->second.asMap();
|
2018-04-29 11:58:35 +02:00
|
|
|
auto Kend = attrs.end();
|
|
|
|
|
for (auto K = attrs.begin(); K != Kend; ++K) {
|
2011-09-18 01:33:07 +01:00
|
|
|
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();
|
2018-04-29 11:58:35 +02:00
|
|
|
auto L = attr.find("default");
|
2011-09-18 01:33:07 +01:00
|
|
|
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");
|
2018-07-04 19:18:03 +02:00
|
|
|
if (J != class_desc.end() && J->second.isInt()) {
|
2011-09-18 01:33:07 +01:00
|
|
|
Player::playableTypes.insert(class_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EntityRuleHandler::check(const Atlas::Objects::Root & desc)
|
|
|
|
|
{
|
2018-04-29 11:58:35 +02:00
|
|
|
assert(!desc->getParent().empty());
|
2011-09-18 01:33:07 +01:00
|
|
|
if (desc->getObjtype() != "class") {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2017-07-24 13:39:22 +02:00
|
|
|
return m_builder->isTask(desc->getParent()) ? -1 : 0;
|
2011-09-18 01:33:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EntityRuleHandler::install(const std::string & name,
|
|
|
|
|
const std::string & parent,
|
|
|
|
|
const Atlas::Objects::Root & description,
|
|
|
|
|
std::string & dependent,
|
2018-05-08 14:32:02 +02:00
|
|
|
std::string & reason,
|
|
|
|
|
std::map<const TypeNode*, TypeNode::PropertiesUpdate>& changes)
|
2011-09-18 01:33:07 +01:00
|
|
|
{
|
2018-05-08 14:32:02 +02:00
|
|
|
return installEntityClass(name, parent, description, dependent, reason, changes);
|
2011-09-18 01:33:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EntityRuleHandler::update(const std::string & name,
|
2018-05-08 14:32:02 +02:00
|
|
|
const Atlas::Objects::Root & desc,
|
|
|
|
|
std::map<const TypeNode*, TypeNode::PropertiesUpdate>& changes)
|
2011-09-18 01:33:07 +01:00
|
|
|
{
|
2018-05-08 14:32:02 +02:00
|
|
|
return modifyEntityClass(name, desc, changes);
|
2011-09-18 01:33:07 +01:00
|
|
|
}
|