cyphesis/rulesets/PythonThingScript.cpp

101 lines
3.4 KiB
C++
Raw Normal View History

// This file may be redistributed and modified only under the terms of
// the GNU General Public License (See COPYING for details).
// Copyright (C) 2000,2001 Alistair Riddoch
#include "PythonThingScript.h"
#include "Py_Operation.h"
#include "Py_Oplist.h"
#include "Entity.h"
#include "common/log.h"
#include "common/debug.h"
2003-09-08 Al Riddoch <alriddoch@zepler.org> * client/Py_CreatorClient.cpp, client/Py_CreatorClient.h, rulesets/Py_BBox.cpp, rulesets/Py_BBox.h, rulesets/Py_Location.cpp, rulesets/Py_Location.h, rulesets/Py_Map.cpp, rulesets/Py_Map.h, rulesets/Py_Mind.cpp, rulesets/Py_Mind.h, rulesets/Py_Object.cpp, rulesets/Py_Object.h, rulesets/Py_Operation.cpp, rulesets/Py_Operation.h, rulesets/Py_Oplist.cpp, rulesets/Py_Oplist.h, rulesets/Py_Optime.cpp, rulesets/Py_Optime.h, rulesets/Py_Quaternion.cpp, rulesets/Py_Quaternion.h, rulesets/Py_Thing.cpp, rulesets/Py_Thing.h, rulesets/Py_Vector3D.cpp, rulesets/Py_Vector3D.h, rulesets/Py_World.cpp, rulesets/Py_World.h, rulesets/Py_WorldTime.cpp, rulesets/Py_WorldTime.h, rulesets/PythonMindScript.cpp, rulesets/PythonThingScript.cpp, rulesets/Python_API.cpp: Clean up python API code, with decent names for types, decent error reporting, and making the more paranoid checks only present in debug build. * common/BaseEntity.cpp, common/BaseEntity.h, server/Account.cpp: Remove pointless method setRefnoOp, and put it in the body as an inline method. * common/BaseEntity.h, common/BaseWorld.cpp, common/Setup.h, common/Tick.h, common/operations.h, common/custom.cpp, common/types.h, common/utility.cpp, common/utility.h, modules/Location.h, physics/Vector3D.h, rulesets/BaseMind.cpp, rulesets/Script.h, server/EntityFactory.h: Clean up the dependency trees using more forward declarations. * common/inheritance.cpp, common/inheritance.h: Move most of the implementation into the .cpp file as its not suited to inlining. * rulesets/MemMap.cpp, rulesets/MemMap.h, rulesets/MemMap_methods.h: Move contents of MemMap_methods.h into the .cpp file, as it is not suited to inlining, and remove the file from the build.
2003-09-07 23:38:32 +00:00
#include <Atlas/Objects/Operation/RootOperation.h>
static const bool debug_flag = false;
PythonEntityScript::PythonEntityScript(PyObject * o, Entity & t) :
PythonScript(o, t), m_entity(t)
{
}
PythonEntityScript::~PythonEntityScript()
{
}
bool PythonEntityScript::Operation(const std::string & op_type,
const Atlas::Objects::Operation::RootOperation & op,
OpVector & ret_list,
Atlas::Objects::Operation::RootOperation * sub_op)
{
if (scriptObject == NULL) {
debug( std::cout << "No script object asociated" << std::endl
<< std::flush;);
return false;
}
debug( std::cout << "Got script object for " << std::endl << std::flush;);
std::string op_name = op_type+"_operation";
// Construct apropriate python object thingies from op
if (!PyObject_HasAttrString(scriptObject, (char *)(op_name.c_str()))) {
debug( std::cout << "No method to be found for "
<< "." << op_name << std::endl << std::flush;);
return false;
}
2003-09-08 Al Riddoch <alriddoch@zepler.org> * client/Py_CreatorClient.cpp, client/Py_CreatorClient.h, rulesets/Py_BBox.cpp, rulesets/Py_BBox.h, rulesets/Py_Location.cpp, rulesets/Py_Location.h, rulesets/Py_Map.cpp, rulesets/Py_Map.h, rulesets/Py_Mind.cpp, rulesets/Py_Mind.h, rulesets/Py_Object.cpp, rulesets/Py_Object.h, rulesets/Py_Operation.cpp, rulesets/Py_Operation.h, rulesets/Py_Oplist.cpp, rulesets/Py_Oplist.h, rulesets/Py_Optime.cpp, rulesets/Py_Optime.h, rulesets/Py_Quaternion.cpp, rulesets/Py_Quaternion.h, rulesets/Py_Thing.cpp, rulesets/Py_Thing.h, rulesets/Py_Vector3D.cpp, rulesets/Py_Vector3D.h, rulesets/Py_World.cpp, rulesets/Py_World.h, rulesets/Py_WorldTime.cpp, rulesets/Py_WorldTime.h, rulesets/PythonMindScript.cpp, rulesets/PythonThingScript.cpp, rulesets/Python_API.cpp: Clean up python API code, with decent names for types, decent error reporting, and making the more paranoid checks only present in debug build. * common/BaseEntity.cpp, common/BaseEntity.h, server/Account.cpp: Remove pointless method setRefnoOp, and put it in the body as an inline method. * common/BaseEntity.h, common/BaseWorld.cpp, common/Setup.h, common/Tick.h, common/operations.h, common/custom.cpp, common/types.h, common/utility.cpp, common/utility.h, modules/Location.h, physics/Vector3D.h, rulesets/BaseMind.cpp, rulesets/Script.h, server/EntityFactory.h: Clean up the dependency trees using more forward declarations. * common/inheritance.cpp, common/inheritance.h: Move most of the implementation into the .cpp file as its not suited to inlining. * rulesets/MemMap.cpp, rulesets/MemMap.h, rulesets/MemMap_methods.h: Move contents of MemMap_methods.h into the .cpp file, as it is not suited to inlining, and remove the file from the build.
2003-09-07 23:38:32 +00:00
PyConstOperation * py_op = newPyConstOperation();
py_op->operation = &op;
py_op->own = 0;
py_op->from = m_entity.m_world->getEntity(op.getFrom());
py_op->to = m_entity.m_world->getEntity(op.getTo());
PyObject * ret;
ret = PyObject_CallMethod(scriptObject, (char *)(op_name.c_str()),
"(O)", py_op);
Py_DECREF(py_op);
if (ret != NULL) {
debug( std::cout << "Called python method " << op_name
<< " for object " << std::endl << std::flush;);
if (PyOperation_Check(ret)) {
2003-09-08 Al Riddoch <alriddoch@zepler.org> * client/Py_CreatorClient.cpp, client/Py_CreatorClient.h, rulesets/Py_BBox.cpp, rulesets/Py_BBox.h, rulesets/Py_Location.cpp, rulesets/Py_Location.h, rulesets/Py_Map.cpp, rulesets/Py_Map.h, rulesets/Py_Mind.cpp, rulesets/Py_Mind.h, rulesets/Py_Object.cpp, rulesets/Py_Object.h, rulesets/Py_Operation.cpp, rulesets/Py_Operation.h, rulesets/Py_Oplist.cpp, rulesets/Py_Oplist.h, rulesets/Py_Optime.cpp, rulesets/Py_Optime.h, rulesets/Py_Quaternion.cpp, rulesets/Py_Quaternion.h, rulesets/Py_Thing.cpp, rulesets/Py_Thing.h, rulesets/Py_Vector3D.cpp, rulesets/Py_Vector3D.h, rulesets/Py_World.cpp, rulesets/Py_World.h, rulesets/Py_WorldTime.cpp, rulesets/Py_WorldTime.h, rulesets/PythonMindScript.cpp, rulesets/PythonThingScript.cpp, rulesets/Python_API.cpp: Clean up python API code, with decent names for types, decent error reporting, and making the more paranoid checks only present in debug build. * common/BaseEntity.cpp, common/BaseEntity.h, server/Account.cpp: Remove pointless method setRefnoOp, and put it in the body as an inline method. * common/BaseEntity.h, common/BaseWorld.cpp, common/Setup.h, common/Tick.h, common/operations.h, common/custom.cpp, common/types.h, common/utility.cpp, common/utility.h, modules/Location.h, physics/Vector3D.h, rulesets/BaseMind.cpp, rulesets/Script.h, server/EntityFactory.h: Clean up the dependency trees using more forward declarations. * common/inheritance.cpp, common/inheritance.h: Move most of the implementation into the .cpp file as its not suited to inlining. * rulesets/MemMap.cpp, rulesets/MemMap.h, rulesets/MemMap_methods.h: Move contents of MemMap_methods.h into the .cpp file, as it is not suited to inlining, and remove the file from the build.
2003-09-07 23:38:32 +00:00
PyOperation * op = (PyOperation*)ret;
if (op->operation != NULL) {
ret_list.push_back(op->operation);
op->own = 0;
} else {
debug( std::cout << "Method returned invalid operation"
<< std::endl << std::flush;);
}
} else if (PyOplist_Check(ret)) {
2003-09-08 Al Riddoch <alriddoch@zepler.org> * client/Py_CreatorClient.cpp, client/Py_CreatorClient.h, rulesets/Py_BBox.cpp, rulesets/Py_BBox.h, rulesets/Py_Location.cpp, rulesets/Py_Location.h, rulesets/Py_Map.cpp, rulesets/Py_Map.h, rulesets/Py_Mind.cpp, rulesets/Py_Mind.h, rulesets/Py_Object.cpp, rulesets/Py_Object.h, rulesets/Py_Operation.cpp, rulesets/Py_Operation.h, rulesets/Py_Oplist.cpp, rulesets/Py_Oplist.h, rulesets/Py_Optime.cpp, rulesets/Py_Optime.h, rulesets/Py_Quaternion.cpp, rulesets/Py_Quaternion.h, rulesets/Py_Thing.cpp, rulesets/Py_Thing.h, rulesets/Py_Vector3D.cpp, rulesets/Py_Vector3D.h, rulesets/Py_World.cpp, rulesets/Py_World.h, rulesets/Py_WorldTime.cpp, rulesets/Py_WorldTime.h, rulesets/PythonMindScript.cpp, rulesets/PythonThingScript.cpp, rulesets/Python_API.cpp: Clean up python API code, with decent names for types, decent error reporting, and making the more paranoid checks only present in debug build. * common/BaseEntity.cpp, common/BaseEntity.h, server/Account.cpp: Remove pointless method setRefnoOp, and put it in the body as an inline method. * common/BaseEntity.h, common/BaseWorld.cpp, common/Setup.h, common/Tick.h, common/operations.h, common/custom.cpp, common/types.h, common/utility.cpp, common/utility.h, modules/Location.h, physics/Vector3D.h, rulesets/BaseMind.cpp, rulesets/Script.h, server/EntityFactory.h: Clean up the dependency trees using more forward declarations. * common/inheritance.cpp, common/inheritance.h: Move most of the implementation into the .cpp file as its not suited to inlining. * rulesets/MemMap.cpp, rulesets/MemMap.h, rulesets/MemMap_methods.h: Move contents of MemMap_methods.h into the .cpp file, as it is not suited to inlining, and remove the file from the build.
2003-09-07 23:38:32 +00:00
PyOplist * op = (PyOplist*)ret;
if (op->ops != NULL) {
const OpVector & o = *op->ops;
OpVector::const_iterator Iend = o.end();
for (OpVector::const_iterator I = o.begin(); I != Iend; ++I) {
ret_list.push_back(*I);
}
} else {
debug( std::cout << "Method returned invalid OpVector"
<< std::endl << std::flush;);
}
} else {
debug( std::cout << "Method returned invalid object" << std::endl
<< std::flush;);
}
Py_DECREF(ret);
return true;
} else {
if (PyErr_Occurred() == NULL) {
debug( std::cout << "No method to be found for " << std::endl
<< std::flush;);
} else {
log(ERROR, "Reporting python error");
PyErr_Print();
}
}
return false;
}
void PythonEntityScript::hook(const std::string &, Entity *)
{
}