Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

SCA_PythonController.cpp

Go to the documentation of this file.
00001 
00006 
00007 #include "SCA_PythonController.h"
00008 #include "SCA_LogicManager.h"
00009 #include "SCA_ISensor.h"
00010 #include "SCA_IActuator.h"
00011 
00012 
00013 #include "compile.h"
00014 #include "eval.h"
00015 
00016 // initialize static member variables
00017 SCA_PythonController* SCA_PythonController::m_sCurrentController = NULL;
00018 
00019 
00020 char* SCA_PythonController::sPyGetCurrentController__doc__ = "getCurrentController()";
00021 char* SCA_PythonController::sPyAddActiveActuator__doc__= "addActiveActuator(actuator,bool)";
00022 
00023 char* SCA_PythonController::sPyGetActuators__doc__ = "getActuators";
00024 char* SCA_PythonController::sPyGetSensors__doc__   = "getSensors";
00025 
00026 PyTypeObject SCA_PythonController::Type = {
00027         PyObject_HEAD_INIT(&PyType_Type)
00028                 0,
00029                 "SCA_PythonController",
00030                 sizeof(SCA_PythonController),
00031                 0,
00032                 PyDestructor,
00033                 0,
00034                 __getattr,
00035                 __setattr,
00036                 0, //&MyPyCompare,
00037                 __repr,
00038                 0, //&cvalue_as_number,
00039                 0,
00040                 0,
00041                 0,
00042                 0
00043 };
00044 
00045 PyParentObject SCA_PythonController::Parents[] = {
00046         &SCA_PythonController::Type,
00047         &SCA_IController::Type,
00048         &CValue::Type,
00049         NULL
00050 };
00051 PyMethodDef SCA_PythonController::Methods[] = {
00052         {"getActuators", (PyCFunction) SCA_PythonController::sPyGetActuators, 
00053                 METH_VARARGS, SCA_PythonController::sPyGetActuators__doc__},
00054         {"getSensors", (PyCFunction) SCA_PythonController::sPyGetSensors, 
00055         METH_VARARGS, SCA_PythonController::sPyGetSensors__doc__},
00056         {"getSensor", (PyCFunction) SCA_PythonController::sPyGetSensor, 
00057         METH_VARARGS, SCA_PythonController::GetSensor_doc}
00058         ,
00059         {NULL,NULL} //Sentinel
00060 };
00061 
00062 SCA_PythonController::SCA_PythonController(SCA_IObject* gameobj,PyTypeObject* T)
00063 : SCA_IController(gameobj, T),
00064 m_pythondictionary(NULL),
00065 m_bModified(true)

00066 {
00067 }
00068 
00069 SCA_PythonController::~SCA_PythonController()

00070 {
00071 }
00072 
00073 void 
00074 SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)

00075 {
00076 //      printf("Run the script ...\n");
00077         m_sCurrentController = this;
00078         m_sCurrentLogicManager = logicmgr;
00079         
00080         if (m_bModified)
00081         {
00082                 // recompile the scripttext into bytecode
00083                 m_bytecode = Py_CompileString(m_scriptText.Ptr(), m_scriptName.Ptr(), Py_file_input);
00084                 if (m_bytecode)
00085                 {
00086                         // store the
00087                         int i=0;
00088                         i+=2; // so compiler doesn't complain about unused variable
00089                         PyRun_SimpleString("import GameLogic\n");
00090                 } else
00091                 {
00092                         // didn't compile, so instead of compile, complain
00093                         int i=0;
00094                         i++; // so compiler doesn't complain about unused variable
00095                 }
00096                 m_bModified=false;
00097         }
00098         
00099         struct _object* resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode,
00100                 m_pythondictionary,
00101                 m_pythondictionary
00102                 );
00103         
00104         
00105         if (!resultobj)
00106         {
00107                 // something is wrong, tell the user what went wrong
00108                 printf("PYTHON SCRIPT ERROR:\n");
00109                 PyRun_SimpleString(m_scriptText.Ptr());
00110         }
00111         // run the script
00112         m_sCurrentController = NULL;
00113 }
00114 
00115 PyObject*
00116 SCA_PythonController::_getattr(char* attr)

00117 {
00118         _getattr_up(SCA_IController);
00119 }
00120 
00121 
00122 PyObject*
00123 SCA_PythonController::PyGetActuators(PyObject* self, 
00124                                                                    PyObject* args, 
00125                                                                    PyObject* kwds)

00126 {
00127         int index;
00128         
00129         PyObject* resultlist = PyList_New(m_linkedactuators.size());
00130         for (index=0;index<m_linkedactuators.size();index++)
00131         {
00132                 PyList_SetItem(resultlist,index,m_linkedactuators[index]->AddRef());
00133         }
00134 
00135         return resultlist;
00136 }
00137 
00138 char SCA_PythonController::GetSensor_doc[] = 
00139 "GetSensor (char sensorname) return linked sensor that is named [sensorname]\n";
00140 PyObject*
00141 SCA_PythonController::PyGetSensor(PyObject* self, 
00142                                                                  PyObject* args, 
00143                                                                  PyObject* kwds)

00144 {
00145 
00146         char *scriptArg;
00147         PyObject* foundsensor = NULL;
00148 
00149         if (!PyArg_ParseTuple(args, "s", &scriptArg)) {
00150                 return foundsensor;
00151         }
00152         
00153         int index;
00154         for (index=0;index<m_linkedsensors.size();index++)
00155         {
00156                 SCA_ISensor* sensor = m_linkedsensors[index];
00157                 CCString realname = sensor->GetName();
00158                 if (realname == scriptArg)
00159                 {
00160                         foundsensor = sensor->AddRef();
00161                 }
00162         }
00163                 
00164         return foundsensor;
00165 }
00166 
00167 
00168 
00169 PyObject*
00170 SCA_PythonController::PyGetSensors(PyObject* self, 
00171                                                                  PyObject* args, 
00172                                                                  PyObject* kwds)

00173 {
00174         int index;
00175         
00176         PyObject* resultlist = PyList_New(m_linkedsensors.size());
00177         for (index=0;index<m_linkedsensors.size();index++)
00178         {
00179                 PyList_SetItem(resultlist,index,m_linkedsensors[index]->AddRef());
00180         }
00181         
00182         return resultlist;
00183 }
00184 
00185 /* 1. getScript */
00186 PyObject* SCA_PythonController::PyGetScript(PyObject* self, 
00187                                                                                    PyObject* args, 
00188                                                                                    PyObject* kwds)

00189 {
00190         return PyString_FromString(m_scriptText);
00191 }
00192 
00193 /* 2. setScript */
00194 PyObject* SCA_PythonController::PySetScript(PyObject* self, 
00195                                                                                    PyObject* args, 
00196                                                                                    PyObject* kwds)

00197 {
00198         char *scriptArg;
00199         if (!PyArg_ParseTuple(args, "s", &scriptArg)) {
00200                 return NULL;
00201         }
00202         
00203         /* set scripttext sets m_bModified to true, 

00204                 so next time the script is needed, a reparse into byte code is done */
00205 
00206         this->SetScriptText(scriptArg);
00207         
00208         Py_Return;
00209 }
00210 
00211 /* eof */

Generated at Thu Feb 1 13:03:11 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000