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

SCA_RandomSensor.cpp

Go to the documentation of this file.
00001 
00006 
00007 #include "SCA_RandomSensor.h"
00008 #include "SCA_EventManager.h"
00009 #include "SCA_RandomEventManager.h"
00010 #include "SCA_LogicManager.h"
00011 #include "ConstExpr.h"
00012 #include <iostream>
00013 
00014 /* ------------------------------------------------------------------------- */
00015 /* Native functions                                                          */
00016 /* ------------------------------------------------------------------------- */
00017 
00018 SCA_RandomSensor::SCA_RandomSensor(SCA_EventManager* eventmgr, 
00019                                  SCA_IObject* gameobj, 
00020                                  int startseed,
00021                                  PyTypeObject* T)
00022     : SCA_ISensor(gameobj,eventmgr, T)

00023 {
00024     m_iteration  = 0;
00025         m_lastdraw   = false;
00026         
00027         m_basegenerator = new SCA_RandomNumberGenerator(startseed);
00028     m_currentDraw = m_basegenerator->Draw();
00029         RegisterToManager();
00030 }
00031 
00032 SCA_RandomSensor::~SCA_RandomSensor() 

00033 {
00034     /* Nothing to be done here. */
00035 }
00036 
00037 bool SCA_RandomSensor::Evaluate(CValue* event)

00038 {
00039     /* Random generator is the generator from Line 25 of Table 1 in          */
00040     /* [KNUTH 1981, The Art of Computer Programming Vol. 2                   */
00041     /* (2nd Ed.), pp102]                                                     */
00042     /* It's a very simple max. length sequence generator. We can             */
00043     /* draw 32 bool values before having to generate the next                */
00044     /* sequence value. There are some theorems that will tell you            */
00045     /* this is a reasonable way of generating bools. Check Knuth.            */
00046     /* Furthermore, we only draw each <delay>-eth frame.                     */
00047 
00048     bool drawResult = false;
00049 
00050         if (m_iteration > 31) {
00051                 m_currentDraw = m_basegenerator->Draw();
00052                 drawResult = (m_currentDraw & 0x1) == 0;
00053                 m_iteration = 1;
00054         } else {
00055                 drawResult = ((m_currentDraw >> m_iteration) & 0x1) == 0;
00056                 m_iteration++;
00057         }
00058     
00059     /* now pass this result to some controller */
00060         m_lastdraw = drawResult;
00061         return drawResult;
00062 }
00063 
00064 /* ------------------------------------------------------------------------- */
00065 /* Python functions                                                          */
00066 /* ------------------------------------------------------------------------- */
00067 
00068 /* Integration hooks ------------------------------------------------------- */
00069 PyTypeObject SCA_RandomSensor::Type = {
00070         PyObject_HEAD_INIT(&PyType_Type)
00071         0,
00072         "SCA_RandomSensor",
00073         sizeof(SCA_RandomSensor),
00074         0,
00075         PyDestructor,
00076         0,
00077         __getattr,
00078         __setattr,
00079         0, //&MyPyCompare,
00080         __repr,
00081         0, //&cvalue_as_number,
00082         0,
00083         0,
00084         0,
00085         0
00086 };
00087 
00088 PyParentObject SCA_RandomSensor::Parents[] = {
00089         &SCA_RandomSensor::Type,
00090         &SCA_ISensor::Type,
00091         &SCA_ILogicBrick::Type,
00092         &CValue::Type,
00093         NULL
00094 };
00095 
00096 PyMethodDef SCA_RandomSensor::Methods[] = {
00097         {"setSeed",     (PyCFunction) SCA_RandomSensor::sPySetSeed, METH_VARARGS, SetSeed_doc},
00098         {"getSeed",     (PyCFunction) SCA_RandomSensor::sPyGetSeed, METH_VARARGS, GetSeed_doc},
00099         {"getLastDraw", (PyCFunction) SCA_RandomSensor::sPyGetLastDraw, METH_VARARGS, GetLastDraw_doc},
00100         {NULL,NULL} //Sentinel
00101 };
00102 
00103 PyObject* SCA_RandomSensor::_getattr(char* attr) {
00104         _getattr_up(SCA_ISensor);
00105 }
00106 
00107 /* 1. setSeed                                                            */
00108 char SCA_RandomSensor::SetSeed_doc[] = 
00109 "setSeed(seed)\n"
00110 "\t- seed: integer\n"
00111 "\tSet the initial seed of the generator. Equal seeds produce\n"
00112 "\tequal series. If the seed is 0, the generator will produce\n"
00113 "\tthe same value on every call.\n";
00114 PyObject* SCA_RandomSensor::PySetSeed(PyObject* self, PyObject* args, PyObject* kwds) {
00115         long seedArg;
00116         if(!PyArg_ParseTuple(args, "i", &seedArg)) {
00117                 return NULL;
00118         }
00119         
00120         m_basegenerator->SetSeed(seedArg);
00121         
00122         Py_Return;
00123 }
00124 
00125 /* 2. getSeed                                                            */
00126 char SCA_RandomSensor::GetSeed_doc[] = 
00127 "getSeed()\n"
00128 "\tReturns the initial seed of the generator. Equal seeds produce\n"
00129 "\tequal series.\n";
00130 PyObject* SCA_RandomSensor::PyGetSeed(PyObject* self, PyObject* args, PyObject* kwds) {
00131         return PyInt_FromLong(m_basegenerator->GetSeed());
00132 }
00133 
00134 /* 3. getLastDraw                                                            */
00135 char SCA_RandomSensor::GetLastDraw_doc[] = 
00136 "getLastDraw()\n"
00137 "\tReturn the last value that was drawn.\n";
00138 PyObject* SCA_RandomSensor::PyGetLastDraw(PyObject* self, PyObject* args, PyObject* kwds) {
00139         return PyInt_FromLong(m_lastdraw);
00140 }
00141 
00142 /* 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