BGE: bookmark option on controller to make them run before all other controllers.
[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         replica->ProcessReplica();
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()
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 #if (PY_VERSION_HEX >= 0x02060000)
131         PyVarObject_HEAD_INIT(NULL, 0)
132 #else
133         /* python 2.5 and below */
134         PyObject_HEAD_INIT( NULL )  /* required py macro */
135         0,                          /* ob_size */
136 #endif
137         "SCA_RandomSensor",
138         sizeof(PyObjectPlus_Proxy),
139         0,
140         py_base_dealloc,
141         0,
142         0,
143         0,
144         0,
145         py_base_repr,
146         0,0,0,0,0,0,
147         py_base_getattro,
148         py_base_setattro,
149         0,0,0,0,0,0,0,0,0,
150         Methods
151 };
152
153 PyParentObject SCA_RandomSensor::Parents[] = {
154         &SCA_RandomSensor::Type,
155         &SCA_ISensor::Type,
156         &SCA_ILogicBrick::Type,
157         &CValue::Type,
158         NULL
159 };
160
161 PyMethodDef SCA_RandomSensor::Methods[] = {
162         //Deprecated functions ----->
163         {"setSeed",     (PyCFunction) SCA_RandomSensor::sPySetSeed, METH_VARARGS, (PY_METHODCHAR)SetSeed_doc},
164         {"getSeed",     (PyCFunction) SCA_RandomSensor::sPyGetSeed, METH_NOARGS, (PY_METHODCHAR)GetSeed_doc},
165         {"getLastDraw", (PyCFunction) SCA_RandomSensor::sPyGetLastDraw, METH_NOARGS, (PY_METHODCHAR)GetLastDraw_doc},
166         //<----- Deprecated
167         {NULL,NULL} //Sentinel
168 };
169
170 PyAttributeDef SCA_RandomSensor::Attributes[] = {
171         KX_PYATTRIBUTE_BOOL_RO("lastDraw",SCA_RandomSensor,m_lastdraw),
172         KX_PYATTRIBUTE_RW_FUNCTION("seed", SCA_RandomSensor, pyattr_get_seed, pyattr_set_seed),
173         {NULL} //Sentinel
174 };
175
176 PyObject* SCA_RandomSensor::py_getattro(PyObject *attr) {
177         py_getattro_up(SCA_ISensor);
178 }
179
180 PyObject* SCA_RandomSensor::py_getattro_dict() {
181         py_getattro_dict_up(SCA_ISensor);
182 }
183
184 int SCA_RandomSensor::py_setattro(PyObject *attr, PyObject *value)
185 {
186         py_setattro_up(SCA_ISensor);
187 }
188
189 /* 1. setSeed                                                            */
190 const char SCA_RandomSensor::SetSeed_doc[] = 
191 "setSeed(seed)\n"
192 "\t- seed: integer\n"
193 "\tSet the initial seed of the generator. Equal seeds produce\n"
194 "\tequal series. If the seed is 0, the generator will produce\n"
195 "\tthe same value on every call.\n";
196 PyObject* SCA_RandomSensor::PySetSeed(PyObject* args) {
197         ShowDeprecationWarning("setSeed()", "the seed property");
198         long seedArg;
199         if(!PyArg_ParseTuple(args, "i:setSeed", &seedArg)) {
200                 return NULL;
201         }
202         
203         m_basegenerator->SetSeed(seedArg);
204         
205         Py_RETURN_NONE;
206 }
207
208 /* 2. getSeed                                                            */
209 const char SCA_RandomSensor::GetSeed_doc[] = 
210 "getSeed()\n"
211 "\tReturns the initial seed of the generator. Equal seeds produce\n"
212 "\tequal series.\n";
213 PyObject* SCA_RandomSensor::PyGetSeed() {
214         ShowDeprecationWarning("getSeed()", "the seed property");
215         return PyInt_FromLong(m_basegenerator->GetSeed());
216 }
217
218 /* 3. getLastDraw                                                            */
219 const char SCA_RandomSensor::GetLastDraw_doc[] = 
220 "getLastDraw()\n"
221 "\tReturn the last value that was drawn.\n";
222 PyObject* SCA_RandomSensor::PyGetLastDraw() {
223         ShowDeprecationWarning("getLastDraw()", "the lastDraw property");
224         return PyInt_FromLong(m_lastdraw);
225 }
226
227
228 PyObject* SCA_RandomSensor::pyattr_get_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
229 {
230         SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
231         return PyInt_FromLong(self->m_basegenerator->GetSeed());
232 }
233
234 int SCA_RandomSensor::pyattr_set_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
235 {
236         SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
237         if (!PyInt_Check(value)) {
238                 PyErr_SetString(PyExc_TypeError, "sensor.seed = int: Random Sensor, expected an integer");
239                 return -1;
240         }
241         self->m_basegenerator->SetSeed(PyInt_AsLong(value));
242         return 0;
243 }
244
245 /* eof */