2001-04-10 19:05:14 +00:00
// This file may be redistributed and modified only under the terms of
// the GNU General Public License (See COPYING for details).
2001-04-16 16:26:44 +00:00
// Copyright (C) 2000,2001 Alistair Riddoch
2001-04-10 19:05:14 +00:00
# include "PythonMindScript.h"
* 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
# include "Py_Operation.h"
# include "Py_Oplist.h"
# include "Py_Thing.h"
2001-04-10 19:05:14 +00:00
# include "Entity.h"
# include "BaseMind.h"
2001-04-12 11:13:06 +00:00
# include "MemMap_methods.h"
2001-04-10 19:05:14 +00:00
2002-07-14 21:04:50 +00:00
# include <common/log.h>
# include <common/debug.h>
2001-04-10 19:05:14 +00:00
PythonMindScript : : PythonMindScript ( PyObject * o , BaseMind & m ) :
PythonScript ( o , m ) , mind ( m )
{
}
PythonMindScript : : ~ PythonMindScript ( )
{
}
2001-08-02 14:27:06 +00:00
bool PythonMindScript : : Operation ( const std : : string & op_type ,
const RootOperation & op ,
2002-03-08 14:58:40 +00:00
OpVector & ret_list , RootOperation * sub_op )
2001-04-10 19:05:14 +00:00
{
2001-08-14 17:52:50 +00:00
debug ( std : : cout < < " Got script object for " < < std : : endl < < std : : flush ; ) ;
2001-08-02 14:27:06 +00:00
std : : string op_name = op_type + " _operation " ;
2001-05-14 17:25:32 +00:00
// Construct apropriate python object thingies from op
if ( ! PyObject_HasAttrString ( scriptObject , ( char * ) ( op_name . c_str ( ) ) ) ) {
2001-08-14 17:52:50 +00:00
debug ( std : : cout < < " No method found for " < < op_name < < std : : endl
< < std : : flush ; ) ;
2001-05-14 17:25:32 +00:00
return false ;
}
2002-05-17 17:18:40 +00:00
ConstOperationObject * py_op = newAtlasConstRootOperation ( NULL ) ;
py_op - > operation = & op ;
2001-05-14 17:25:32 +00:00
py_op - > own = 0 ;
py_op - > from = mind . map . getAdd ( op . GetFrom ( ) ) ;
py_op - > to = mind . map . getAdd ( op . GetTo ( ) ) ;
PyObject * ret ;
if ( sub_op = = NULL ) {
ret = PyObject_CallMethod ( scriptObject , ( char * ) ( op_name . c_str ( ) ) ,
" (O) " , py_op ) ;
} else {
* 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
OperationObject * py_sub_op = newAtlasRootOperation ( NULL ) ;
2001-05-14 17:25:32 +00:00
py_sub_op - > operation = sub_op ;
py_sub_op - > own = 0 ;
py_sub_op - > from = mind . map . getAdd ( sub_op - > GetFrom ( ) ) ;
py_sub_op - > to = mind . map . getAdd ( sub_op - > GetTo ( ) ) ;
ret = PyObject_CallMethod ( scriptObject , ( char * ) ( op_name . c_str ( ) ) ,
" (OO) " , py_op , py_sub_op ) ;
Py_DECREF ( py_sub_op ) ;
}
Py_DECREF ( py_op ) ;
if ( ret ! = NULL ) {
2001-08-14 17:52:50 +00:00
debug ( std : : cout < < " Called python method " < < op_name < < std : : endl
< < std : : flush ; ) ;
2001-05-14 17:25:32 +00:00
if ( PyOperation_Check ( ret ) ) {
* 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
OperationObject * op = ( OperationObject * ) ret ;
2001-05-14 17:25:32 +00:00
if ( op - > operation ! = NULL ) {
ret_list . push_back ( op - > operation ) ;
op - > own = 0 ;
2001-04-10 19:05:14 +00:00
} else {
2001-08-14 17:52:50 +00:00
debug ( std : : cout < < " Method returned invalid op " < < std : : endl
< < std : : flush ; ) ;
2001-04-10 19:05:14 +00:00
}
2001-05-14 17:25:32 +00:00
} else if ( PyOplist_Check ( ret ) ) {
OplistObject * op = ( OplistObject * ) ret ;
if ( op - > ops ! = NULL ) {
ret_list = * op - > ops ;
2001-04-10 19:05:14 +00:00
} else {
2002-03-08 14:58:40 +00:00
debug ( std : : cout < < " Method returned invalid OpVector " < < std : : endl
2001-08-14 17:52:50 +00:00
< < std : : flush ; ) ;
2001-04-10 19:05:14 +00:00
}
2001-05-14 17:25:32 +00:00
} else {
2001-08-14 17:52:50 +00:00
debug ( std : : cout < < " Method returned invalid object " < < std : : endl < < std : : flush ; ) ;
2001-04-10 19:05:14 +00:00
}
2001-05-14 17:25:32 +00:00
Py_DECREF ( ret ) ;
return true ;
2001-04-10 19:05:14 +00:00
} else {
2001-05-14 17:25:32 +00:00
if ( PyErr_Occurred ( ) = = NULL ) {
2001-08-14 17:52:50 +00:00
debug ( std : : cout < < " No method to be found for " < < std : : endl
< < std : : flush ; ) ;
2001-05-14 17:25:32 +00:00
} else {
2002-07-14 21:04:50 +00:00
log ( ERROR , " Reporting python error " ) ;
2001-05-14 17:25:32 +00:00
PyErr_Print ( ) ;
2002-09-05 11:21:48 +00:00
if ( op_type = = " tick " ) {
std : : string msg = std : : string ( " Mind script for " ) + mind . getId ( ) + " , named " + mind . getName ( ) + " has reported an error processing a tick operation. This NPC is probably now inactive. " ;
log ( ERROR , msg . c_str ( ) ) ;
}
2001-05-14 17:25:32 +00:00
}
2001-04-10 19:05:14 +00:00
}
return false ;
}
2001-08-02 14:27:06 +00:00
void PythonMindScript : : hook ( const std : : string & method , Entity * object )
2001-04-10 19:05:14 +00:00
{
2001-05-14 17:25:32 +00:00
ThingObject * obj = newThingObject ( NULL ) ;
obj - > m_thing = object ;
2001-10-09 14:03:12 +00:00
PyObject * ret = PyObject_CallMethod ( scriptObject , ( char * ) ( method . c_str ( ) ) , " (O) " , obj ) ;
Py_DECREF ( ret ) ;
2001-05-14 17:25:32 +00:00
Py_DECREF ( obj ) ;
2001-04-10 19:05:14 +00:00
}