BGE Python API cleanup - no functionality changes
[blender.git] / source / gameengine / GameLogic / SCA_RandomSensor.cpp
1 /**
2  * Generate random pulses
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include "SCA_RandomSensor.h"
33 #include "SCA_EventManager.h"
34 #include "SCA_RandomEventManager.h"
35 #include "SCA_LogicManager.h"
36 #include "ConstExpr.h"
37 #include <iostream>
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 /* ------------------------------------------------------------------------- */
44 /* Native functions                                                          */
45 /* ------------------------------------------------------------------------- */
46
47 SCA_RandomSensor::SCA_RandomSensor(SCA_EventManager* eventmgr, 
48                                  SCA_IObject* gameobj, 
49                                  int startseed,
50                                  PyTypeObject* T)
51     : SCA_ISensor(gameobj,eventmgr, T)
52 {
53         // m_basegenerator is never deleted => memory leak
54         m_basegenerator = new SCA_RandomNumberGenerator(startseed);
55         Init();
56 }
57
58
59
60 SCA_RandomSensor::~SCA_RandomSensor() 
61 {
62     /* Nothing to be done here. */
63 }
64
65 void SCA_RandomSensor::Init()
66 {
67     m_iteration  = 0;
68         m_interval = 0;
69         m_lastdraw   = false;
70     m_currentDraw = m_basegenerator->Draw();
71 }
72
73
74 CValue* SCA_RandomSensor::GetReplica()
75 {
76         CValue* replica = new SCA_RandomSensor(*this);
77         // replication copies m_basegenerator pointer => share same generator
78         // this will copy properties and so on...
79         CValue::AddDataToReplica(replica);
80
81         return replica;
82 }
83
84
85
86 bool SCA_RandomSensor::IsPositiveTrigger()
87
88         return (m_invert !=m_lastdraw);
89 }
90
91
92 bool SCA_RandomSensor::Evaluate(CValue* event)
93 {
94     /* Random generator is the generator from Line 25 of Table 1 in          */
95     /* [KNUTH 1981, The Art of Computer Programming Vol. 2                   */
96     /* (2nd Ed.), pp102]                                                     */
97     /* It's a very simple max. length sequence generator. We can             */
98     /* draw 32 bool values before having to generate the next                */
99     /* sequence value. There are some theorems that will tell you            */
100     /* this is a reasonable way of generating bools. Check Knuth.            */
101     /* Furthermore, we only draw each <delay>-eth frame.                     */
102
103         bool evaluateResult = false;
104
105         if (++m_interval > m_pulse_frequency) {
106             bool drawResult = false;
107                 m_interval = 0;
108                 if (m_iteration > 31) {
109                         m_currentDraw = m_basegenerator->Draw();
110                         drawResult = (m_currentDraw & 0x1) == 0;
111                         m_iteration = 1;
112                 } else {
113                         drawResult = ((m_currentDraw >> m_iteration) & 0x1) == 0;
114                         m_iteration++;
115                 }
116                 evaluateResult = drawResult != m_lastdraw;
117                 m_lastdraw = drawResult;
118         }
119     
120     /* now pass this result to some controller */
121         return evaluateResult;
122 }
123
124 /* ------------------------------------------------------------------------- */
125 /* Python functions                                                          */
126 /* ------------------------------------------------------------------------- */
127
128 /* Integration hooks ------------------------------------------------------- */
129 PyTypeObject SCA_RandomSensor::Type = {
130         PyObject_HEAD_INIT(NULL)
131         0,
132         "SCA_RandomSensor",
133         sizeof(PyObjectPlus_Proxy),
134         0,
135         py_base_dealloc,
136         0,
137         0,
138         0,
139         0,
140         py_base_repr,
141         0,0,0,0,0,0,
142         py_base_getattro,
143         py_base_setattro,
144         0,0,0,0,0,0,0,0,0,
145         Methods
146 };
147
148 PyParentObject SCA_RandomSensor::Parents[] = {
149         &SCA_RandomSensor::Type,
150         &SCA_ISensor::Type,
151         &SCA_ILogicBrick::Type,
152         &CValue::Type,
153         NULL
154 };
155
156 PyMethodDef SCA_RandomSensor::Methods[] = {
157         {"setSeed",     (PyCFunction) SCA_RandomSensor::sPySetSeed, METH_VARARGS, (PY_METHODCHAR)SetSeed_doc},
158         {"getSeed",     (PyCFunction) SCA_RandomSensor::sPyGetSeed, METH_VARARGS, (PY_METHODCHAR)GetSeed_doc},
159         {"getLastDraw", (PyCFunction) SCA_RandomSensor::sPyGetLastDraw, METH_VARARGS, (PY_METHODCHAR)GetLastDraw_doc},
160         {NULL,NULL} //Sentinel
161 };
162
163 PyAttributeDef SCA_RandomSensor::Attributes[] = {
164         KX_PYATTRIBUTE_BOOL_RO("lastDraw",SCA_RandomSensor,m_lastdraw),
165         KX_PYATTRIBUTE_RW_FUNCTION("seed", SCA_RandomSensor, pyattr_get_seed, pyattr_set_seed),
166         {NULL} //Sentinel
167 };
168
169 PyObject* SCA_RandomSensor::py_getattro(PyObject *attr) {
170         py_getattro_up(SCA_ISensor);
171 }
172
173 int SCA_RandomSensor::py_setattro(PyObject *attr, PyObject *value)
174 {
175         py_setattro_up(SCA_ISensor);
176 }
177
178 /* 1. setSeed                                                            */
179 const char SCA_RandomSensor::SetSeed_doc[] = 
180 "setSeed(seed)\n"
181 "\t- seed: integer\n"
182 "\tSet the initial seed of the generator. Equal seeds produce\n"
183 "\tequal series. If the seed is 0, the generator will produce\n"
184 "\tthe same value on every call.\n";
185 PyObject* SCA_RandomSensor::PySetSeed(PyObject* self, PyObject* args, PyObject* kwds) {
186         ShowDeprecationWarning("setSeed()", "the seed property");
187         long seedArg;
188         if(!PyArg_ParseTuple(args, "i:setSeed", &seedArg)) {
189                 return NULL;
190         }
191         
192         m_basegenerator->SetSeed(seedArg);
193         
194         Py_RETURN_NONE;
195 }
196
197 /* 2. getSeed                                                            */
198 const char SCA_RandomSensor::GetSeed_doc[] = 
199 "getSeed()\n"
200 "\tReturns the initial seed of the generator. Equal seeds produce\n"
201 "\tequal series.\n";
202 PyObject* SCA_RandomSensor::PyGetSeed(PyObject* self, PyObject* args, PyObject* kwds) {
203         ShowDeprecationWarning("getSeed()", "the seed property");
204         return PyInt_FromLong(m_basegenerator->GetSeed());
205 }
206
207 /* 3. getLastDraw                                                            */
208 const char SCA_RandomSensor::GetLastDraw_doc[] = 
209 "getLastDraw()\n"
210 "\tReturn the last value that was drawn.\n";
211 PyObject* SCA_RandomSensor::PyGetLastDraw(PyObject* self, PyObject* args, PyObject* kwds) {
212         ShowDeprecationWarning("getLastDraw()", "the lastDraw property");
213         return PyInt_FromLong(m_lastdraw);
214 }
215
216
217 PyObject* SCA_RandomSensor::pyattr_get_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
218 {
219         SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
220         return PyInt_FromLong(self->m_basegenerator->GetSeed());
221 }
222
223 int SCA_RandomSensor::pyattr_set_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
224 {
225         SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
226         if (!PyInt_Check(value)) {
227                 PyErr_SetString(PyExc_TypeError, "expected an integer");
228                 return -1;
229         }
230         self->m_basegenerator->SetSeed(PyInt_AsLong(value));
231         return 0;
232 }
233
234 /* eof */