2010-07-13 16:23:46 +01:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2010 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
|
|
|
|
|
|
|
|
|
|
|
2018-07-26 16:08:21 +02:00
|
|
|
#include "python/CyPy_LocatedEntity.h"
|
2010-07-13 16:23:46 +01:00
|
|
|
|
2018-07-26 16:08:21 +02:00
|
|
|
#include "PythonArithmeticFactory.h"
|
2010-07-13 16:23:46 +01:00
|
|
|
|
2018-07-26 16:08:21 +02:00
|
|
|
#include "Python_Script_Utils.h"
|
|
|
|
|
#include "PythonArithmeticScript.h"
|
2010-07-13 16:23:46 +01:00
|
|
|
|
|
|
|
|
/// \brief PythonArithmeticFactory constructor
|
|
|
|
|
///
|
|
|
|
|
/// @param package Name of the package containing the script
|
|
|
|
|
/// @param name Name of the type within the package for the script
|
|
|
|
|
PythonArithmeticFactory::PythonArithmeticFactory(const std::string & package,
|
|
|
|
|
const std::string & name) :
|
2018-07-23 16:43:39 +02:00
|
|
|
PythonClass(package, name)
|
2010-07-13 16:23:46 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-19 11:35:41 +00:00
|
|
|
|
2018-10-30 19:31:27 +01:00
|
|
|
PythonArithmeticFactory::~PythonArithmeticFactory() = default;
|
2011-12-19 11:35:41 +00:00
|
|
|
|
|
|
|
|
int PythonArithmeticFactory::setup()
|
|
|
|
|
{
|
|
|
|
|
return load();
|
2010-07-13 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \brief Create a new arithmetic script for a character
|
|
|
|
|
///
|
|
|
|
|
/// @param chr the Character that requires a new script
|
2013-01-17 10:17:07 +00:00
|
|
|
ArithmeticScript * PythonArithmeticFactory::newScript(LocatedEntity * owner)
|
2010-07-13 16:23:46 +01:00
|
|
|
{
|
2018-07-24 21:18:26 +02:00
|
|
|
if (!m_class) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2010-07-13 16:23:46 +01:00
|
|
|
// Create the task, and use its script to add a script
|
2018-07-24 21:18:26 +02:00
|
|
|
if (m_class->isNull()) {
|
2018-07-23 16:43:39 +02:00
|
|
|
return nullptr;
|
2010-07-13 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME Pass in entity for initialisation of entity pointer in
|
|
|
|
|
// EntityWrapper.
|
2018-07-24 21:18:26 +02:00
|
|
|
Py::Object py_object;
|
2010-07-13 16:23:46 +01:00
|
|
|
|
2018-07-24 21:18:26 +02:00
|
|
|
try {
|
|
|
|
|
if (owner == nullptr) {
|
|
|
|
|
py_object = m_class->apply();
|
|
|
|
|
} else {
|
2018-07-26 00:33:48 +02:00
|
|
|
py_object = m_class->apply(Py::TupleN(CyPy_LocatedEntity::wrap(owner)));
|
2018-07-24 21:18:26 +02:00
|
|
|
}
|
|
|
|
|
} catch (const Py::BaseException& ex) {
|
2018-07-26 00:33:48 +02:00
|
|
|
if (owner) {
|
|
|
|
|
log(ERROR, "Could not create python stats instance for " + owner->describeEntity());
|
2010-07-13 16:23:46 +01:00
|
|
|
} else {
|
2018-07-26 00:33:48 +02:00
|
|
|
log(ERROR, "Could not create python stats instance");
|
|
|
|
|
}
|
|
|
|
|
if (PyErr_Occurred()) {
|
2010-07-13 16:23:46 +01:00
|
|
|
PyErr_Print();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArithmeticScript * script = new PythonArithmeticScript(py_object);
|
|
|
|
|
// FIXME This is now part of the property, not the character.
|
|
|
|
|
// chr.statistics().m_script = script;
|
|
|
|
|
|
|
|
|
|
return script;
|
|
|
|
|
}
|