cyphesis/rulesets/PythonThingScript.cpp
Al Riddoch e1669c7947 * client/ClientConnection.cpp, client/ClientConnection.h,
tools/cycmd.cpp: Switched to using skstream to completely handle
	   client socket connections.

	* client/ClientConnection.cpp, client/ClientConnection.h,
	  common/log.cpp, rulesets/EntityFactory.h, rulesets/MemMap.h,
	  server/server.cpp, tools/cycmd.cpp: Removed unused includes.

	* client/Py_CreatorClient.cpp, client/client.cpp,
	  client/define_world.cpp, 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_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/PythonThingScript.h, rulesets/Python_API.cpp,
	  rulesets/Python_API.h, server/server.cpp:
	  Reorganised python header files so that each header includes
	  all the code relevant for a particular type. Tidied up
	  the type implementations. Added prototypes to init_python_api()
	  to the Python_API.h header, and removed other dependencies from
	  it.

Al Riddoch  <alriddoch@zepler.org>
2001-10-19 11:35:07 +00:00

97 lines
3.2 KiB
C++

// 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 "Thing.h"
#include <common/BaseWorld.h>
#include <common/debug.h>
using Atlas::Objects::Operation::RootOperation;
static const bool debug_flag = false;
PythonThingScript::PythonThingScript(PyObject * o, Thing & t) :
PythonScript(o, t), thing(t)
{
}
PythonThingScript::~PythonThingScript()
{
}
bool PythonThingScript::Operation(const std::string & op_type,
const RootOperation & op,
oplist & ret_list, 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;
}
OperationObject * py_op = newAtlasRootOperation(NULL);
py_op->operation = new RootOperation(op);
py_op->own = 0;
py_op->from = thing.world->getObject(op.GetFrom());
py_op->to = thing.world->getObject(op.GetTo());
PyObject * ret;
ret = PyObject_CallMethod(scriptObject, (char *)(op_name.c_str()),
"(O)", py_op);
delete py_op->operation;
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)) {
OperationObject * op = (OperationObject*)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)) {
OplistObject * op = (OplistObject*)ret;
if (op->ops != NULL) {
ret_list = *op->ops;
} else {
debug( std::cout << "Method returned invalid oplist"
<< 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 {
std::cerr << "Reporting python error for " << std::endl
<< std::flush;
PyErr_Print();
}
}
return false;
}
void PythonThingScript::hook(const std::string &, Entity *)
{
}