2013-10-04 14:26:16 +02:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2009 Alistair Riddoch
|
|
|
|
|
// Copyright (C) 2013 Erik Ogenvik
|
|
|
|
|
//
|
|
|
|
|
// 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 "ArchetypeRuleHandler.h"
|
|
|
|
|
|
|
|
|
|
#include "EntityBuilder.h"
|
|
|
|
|
#include "Player.h"
|
2013-10-07 23:01:30 +02:00
|
|
|
#include "ArchetypeFactory.h"
|
2013-10-04 14:26:16 +02:00
|
|
|
|
|
|
|
|
#include "common/log.h"
|
|
|
|
|
#include "common/debug.h"
|
|
|
|
|
#include "common/compose.hpp"
|
|
|
|
|
|
2013-10-07 23:01:30 +02:00
|
|
|
#include <Atlas/Objects/objectFactory.h>
|
|
|
|
|
|
2013-10-04 14:26:16 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
using Atlas::Message::Element;
|
|
|
|
|
using Atlas::Message::MapType;
|
|
|
|
|
using Atlas::Message::ListType;
|
|
|
|
|
using Atlas::Objects::Root;
|
2013-10-07 23:01:30 +02:00
|
|
|
using Atlas::Objects::Entity::RootEntity;
|
|
|
|
|
using Atlas::Objects::smart_dynamic_cast;
|
|
|
|
|
using Atlas::Objects::Factories;
|
2013-10-04 14:26:16 +02:00
|
|
|
|
|
|
|
|
using String::compose;
|
|
|
|
|
|
|
|
|
|
static const bool debug_flag = false;
|
|
|
|
|
|
|
|
|
|
int ArchetypeRuleHandler::installArchetypeClass(const std::string & class_name,
|
2013-10-07 23:01:30 +02:00
|
|
|
const std::string & parent, const Root & class_desc,
|
|
|
|
|
std::string & dependent, std::string & reason)
|
2013-10-04 14:26:16 +02:00
|
|
|
{
|
|
|
|
|
assert(class_name == class_desc->getId());
|
|
|
|
|
|
|
|
|
|
// Get the new factory for this rule
|
2018-05-08 14:32:02 +02:00
|
|
|
auto* parent_factory = dynamic_cast<ArchetypeFactory*>(m_builder->getClassFactory(parent));
|
|
|
|
|
if (parent_factory == nullptr) {
|
2013-10-07 23:01:30 +02:00
|
|
|
debug(
|
|
|
|
|
std::cout << "class \"" << class_name
|
2013-10-04 14:26:16 +02:00
|
|
|
<< "\" has non existant parent \"" << parent
|
2013-10-07 23:01:30 +02:00
|
|
|
<< "\". Waiting." << std::endl << std::flush
|
|
|
|
|
;);
|
2013-10-04 14:26:16 +02:00
|
|
|
dependent = parent;
|
|
|
|
|
reason = compose("Entity rule \"%1\" has parent \"%2\" which does "
|
2013-10-07 23:01:30 +02:00
|
|
|
"not exist.", class_name, parent);
|
2013-10-04 14:26:16 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
2013-10-07 23:01:30 +02:00
|
|
|
ArchetypeFactory * factory = parent_factory->duplicateFactory();
|
2018-05-08 14:32:02 +02:00
|
|
|
if (factory == nullptr) {
|
2013-10-04 14:26:16 +02:00
|
|
|
log(ERROR,
|
2013-10-07 23:01:30 +02:00
|
|
|
compose(
|
|
|
|
|
"Attempt to install rule \"%1\" which has parent \"%2\" "
|
|
|
|
|
"which cannot be instantiated", class_name,
|
|
|
|
|
parent));
|
2013-10-04 14:26:16 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(factory->m_parent == parent_factory);
|
|
|
|
|
|
2013-10-07 23:01:30 +02:00
|
|
|
if (populateArchetypeFactory(class_name, factory, class_desc->asMessage(),
|
|
|
|
|
dependent, reason) != 0) {
|
2013-10-04 14:26:16 +02:00
|
|
|
delete factory;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-07 23:01:30 +02:00
|
|
|
debug(
|
|
|
|
|
std::cout << "INSTALLING " << class_name << ":" << parent
|
|
|
|
|
<< std::endl << std::flush
|
|
|
|
|
;);
|
2013-10-04 14:26:16 +02:00
|
|
|
|
|
|
|
|
// Install the factory in place.
|
|
|
|
|
if (m_builder->installFactory(class_name, class_desc, factory) != 0) {
|
|
|
|
|
delete factory;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
factory->addProperties();
|
|
|
|
|
|
|
|
|
|
// Add it as a child to its parent.
|
|
|
|
|
parent_factory->m_children.insert(factory);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ArchetypeRuleHandler::modifyArchetypeClass(const std::string & class_name,
|
2018-05-08 14:32:02 +02:00
|
|
|
const Root & class_desc, std::map<const TypeNode*, TypeNode::PropertiesUpdate>& changes)
|
2013-10-04 14:26:16 +02:00
|
|
|
{
|
|
|
|
|
assert(class_name == class_desc->getId());
|
|
|
|
|
|
2018-05-08 14:32:02 +02:00
|
|
|
auto factory =
|
|
|
|
|
dynamic_cast<ArchetypeFactory*>(m_builder->getClassFactory(class_name));
|
|
|
|
|
if (factory == nullptr) {
|
2013-10-04 14:26:16 +02:00
|
|
|
log(ERROR, compose("Could not find factory for existing entity class "
|
2013-10-07 23:01:30 +02:00
|
|
|
"\"%1\".", class_name));
|
2013-10-04 14:26:16 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
2018-05-08 14:32:02 +02:00
|
|
|
assert(factory != nullptr);
|
2013-10-07 23:01:30 +02:00
|
|
|
|
|
|
|
|
auto backup_entities = factory->m_entities;
|
|
|
|
|
auto backup_thoughts = factory->m_thoughts;
|
2013-10-04 14:26:16 +02:00
|
|
|
|
|
|
|
|
// Copy the defaults from the parent. In populateArchetypeFactory this may be
|
2016-09-06 22:29:36 -04:00
|
|
|
// overridden with the defaults for this class.
|
2018-05-08 14:32:02 +02:00
|
|
|
if (factory->m_parent != nullptr) {
|
2013-10-07 23:01:30 +02:00
|
|
|
factory->m_entities = factory->m_parent->m_entities;
|
|
|
|
|
factory->m_thoughts = factory->m_parent->m_thoughts;
|
2013-10-04 14:26:16 +02:00
|
|
|
} 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.
|
2013-10-07 23:01:30 +02:00
|
|
|
log(ERROR,
|
|
|
|
|
compose(
|
|
|
|
|
"ArchetypeRuleHandler::modifyEntityClass: \"%1\" modified "
|
|
|
|
|
"by client, but has no parent factory.",
|
|
|
|
|
class_name));
|
|
|
|
|
factory->m_entities.clear();
|
|
|
|
|
factory->m_thoughts.clear();
|
2013-10-04 14:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
2013-10-25 15:00:54 +02:00
|
|
|
factory->m_classEntities.clear();
|
|
|
|
|
factory->m_classThoughts.clear();
|
|
|
|
|
|
2013-10-04 14:26:16 +02:00
|
|
|
std::string dependent, reason;
|
2013-10-07 23:01:30 +02:00
|
|
|
if (populateArchetypeFactory(class_name, factory, class_desc->asMessage(),
|
|
|
|
|
dependent, reason) != 0) {
|
|
|
|
|
factory->m_entities = backup_entities;
|
|
|
|
|
factory->m_thoughts = backup_thoughts;
|
2013-10-04 14:26:16 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-08 14:32:02 +02:00
|
|
|
factory->updateProperties(changes);
|
2013-10-04 14:26:16 +02:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-07 23:01:30 +02:00
|
|
|
int ArchetypeRuleHandler::populateArchetypeFactory(
|
|
|
|
|
const std::string & class_name, ArchetypeFactory * factory,
|
|
|
|
|
const MapType & class_desc, std::string & dependent,
|
|
|
|
|
std::string & reason)
|
2013-10-04 14:26:16 +02:00
|
|
|
{
|
|
|
|
|
// assert(class_name == class_desc->getId());
|
|
|
|
|
// Establish whether this rule has an associated script, and
|
|
|
|
|
// if so, use it.
|
2013-10-07 23:01:30 +02:00
|
|
|
// 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();
|
|
|
|
|
// std::string script_package;
|
|
|
|
|
// std::string script_class;
|
|
|
|
|
// if (getScriptDetails(script, class_name, "Archetype",
|
|
|
|
|
// script_package, script_class) != 0) {
|
|
|
|
|
// return -1;
|
|
|
|
|
// }
|
|
|
|
|
// if (factory->m_scriptFactory == 0 ||
|
|
|
|
|
// factory->m_scriptFactory->package() != script_package) {
|
|
|
|
|
// PythonScriptFactory<LocatedEntity> * psf =
|
|
|
|
|
// new PythonScriptFactory<LocatedEntity>(script_package,
|
|
|
|
|
// script_class);
|
|
|
|
|
// if (psf->setup() == 0) {
|
|
|
|
|
// delete factory->m_scriptFactory;
|
|
|
|
|
// factory->m_scriptFactory = psf;
|
|
|
|
|
// } else {
|
|
|
|
|
// log(ERROR, compose("Python class \"%1.%2\" failed to load",
|
|
|
|
|
// script_package, script_class));
|
|
|
|
|
// delete psf;
|
|
|
|
|
// return -1;
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// // FIXME If this fails, that's bad.
|
|
|
|
|
// factory->m_scriptFactory->refreshClass();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
2013-10-25 15:00:54 +02:00
|
|
|
|
2013-10-07 23:01:30 +02:00
|
|
|
auto entitiesI = class_desc.find("entities");
|
|
|
|
|
if (entitiesI != class_desc.end() && entitiesI->second.isList()) {
|
|
|
|
|
for (auto& I : entitiesI->second.asList()) {
|
|
|
|
|
if (I.isMap()) {
|
2013-10-25 15:00:54 +02:00
|
|
|
const MapType& map = I.asMap();
|
|
|
|
|
std::string id;
|
|
|
|
|
auto idI = map.find("id");
|
|
|
|
|
if (idI != map.end() && idI->second.isString()) {
|
|
|
|
|
id = idI->second.asString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MapType& entity = factory->m_entities[id];
|
|
|
|
|
MapType& classEntity = factory->m_classEntities[id];
|
|
|
|
|
for (auto& entry : map) {
|
|
|
|
|
entity[entry.first] = entry.second;
|
|
|
|
|
classEntity[entry.first] = entry.second;
|
|
|
|
|
}
|
2013-10-04 14:26:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-07 23:01:30 +02:00
|
|
|
auto thoughtsI = class_desc.find("thoughts");
|
|
|
|
|
if (thoughtsI != class_desc.end() && thoughtsI->second.isList()) {
|
2013-10-25 15:00:54 +02:00
|
|
|
const ListType& thoughts = thoughtsI->second.asList();
|
|
|
|
|
factory->m_thoughts.insert(factory->m_thoughts.end(), thoughts.begin(),
|
|
|
|
|
thoughts.end());
|
|
|
|
|
factory->m_classThoughts.insert(factory->m_classThoughts.end(),
|
|
|
|
|
thoughts.begin(), thoughts.end());
|
2013-10-04 14:26:16 +02:00
|
|
|
}
|
|
|
|
|
// Check whether it should be available to players as a playable character.
|
2013-10-07 23:01:30 +02:00
|
|
|
auto playableI = class_desc.find("playable");
|
|
|
|
|
if (playableI != class_desc.end() && playableI->second.isInt()) {
|
2013-10-04 14:26:16 +02:00
|
|
|
Player::playableTypes.insert(class_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ArchetypeRuleHandler::check(const Atlas::Objects::Root & desc)
|
|
|
|
|
{
|
2017-07-24 13:39:22 +02:00
|
|
|
assert(!desc->getParent().empty());
|
2013-10-04 14:26:16 +02:00
|
|
|
if (desc->getObjtype() != "archetype") {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ArchetypeRuleHandler::install(const std::string & name,
|
2013-10-07 23:01:30 +02:00
|
|
|
const std::string & parent, const Atlas::Objects::Root & description,
|
2018-05-08 14:32:02 +02:00
|
|
|
std::string & dependent, std::string & reason,
|
|
|
|
|
std::map<const TypeNode*, TypeNode::PropertiesUpdate>& changes)
|
2013-10-04 14:26:16 +02:00
|
|
|
{
|
|
|
|
|
return installArchetypeClass(name, parent, description, dependent, reason);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ArchetypeRuleHandler::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)
|
2013-10-04 14:26:16 +02:00
|
|
|
{
|
2018-05-08 14:32:02 +02:00
|
|
|
return modifyArchetypeClass(name, desc, changes);
|
2013-10-04 14:26:16 +02:00
|
|
|
}
|