a09d83289062d5e98a758d47893f2f6bd0d7ceae
[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     : SCA_ISensor(gameobj,eventmgr)
51 {
52         m_basegenerator = new SCA_RandomNumberGenerator(startseed);
53         Init();
54 }
55
56
57
58 SCA_RandomSensor::~SCA_RandomSensor() 
59 {
60         m_basegenerator->Release();
61 }
62
63 void SCA_RandomSensor::Init()
64 {
65     m_iteration  = 0;
66         m_interval = 0;
67         m_lastdraw   = false;
68     m_currentDraw = m_basegenerator->Draw();
69 }
70
71
72 CValue* SCA_RandomSensor::GetReplica()
73 {
74         CValue* replica = new SCA_RandomSensor(*this);
75         // this will copy properties and so on...
76         replica->ProcessReplica();
77
78         return replica;
79 }
80
81 void SCA_RandomSensor::ProcessReplica()
82 {
83         SCA_ISensor::ProcessReplica();
84         // increment reference count so that we can release the generator at this end
85         m_basegenerator->AddRef();
86 }
87
88
89 bool SCA_RandomSensor::IsPositiveTrigger()
90
91         return (m_invert !=m_lastdraw);
92 }
93
94
95 bool SCA_RandomSensor::Evaluate()
96 {
97     /* Random generator is the generator from Line 25 of Table 1 in          */
98     /* [KNUTH 1981, The Art of Computer Programming Vol. 2                   */
99     /* (2nd Ed.), pp102]                                                     */
100     /* It's a very simple max. length sequence generator. We can             */
101     /* draw 32 bool values before having to generate the next                */
102     /* sequence value. There are some theorems that will tell you            */
103     /* this is a reasonable way of generating bools. Check Knuth.            */
104     /* Furthermore, we only draw each <delay>-eth frame.                     */
105
106         bool evaluateResult = false;
107
108         if (++m_interval > m_pulse_frequency) {
109             bool drawResult = false;
110                 m_interval = 0;
111                 if (m_iteration > 31) {
112                         m_currentDraw = m_basegenerator->Draw();
113                         drawResult = (m_currentDraw & 0x1) == 0;
114                         m_iteration = 1;
115                 } else {
116                         drawResult = ((m_currentDraw >> m_iteration) & 0x1) == 0;
117                         m_iteration++;
118                 }
119                 evaluateResult = drawResult != m_lastdraw;
120                 m_lastdraw = drawResult;
121         }
122     
123     /* now pass this result to some controller */
124         return evaluateResult;
125 }
126
127 /* ------------------------------------------------------------------------- */
128 /* Python functions                                                          */
129 /* ------------------------------------------------------------------------- */
130
131 /* Integration hooks ------------------------------------------------------- */
132 PyTypeObject SCA_RandomSensor::Type = {
133         PyVarObject_HEAD_INIT(NULL, 0)
134         "SCA_RandomSensor",
135         sizeof(PyObjectPlus_Proxy),
136         0,
137         py_base_dealloc,
138         0,
139         0,
140         0,
141         0,
142         py_base_repr,
143         0,0,0,0,0,0,0,0,0,
144         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
145         0,0,0,0,0,0,0,
146         Methods,
147         0,
148         0,
149         &SCA_ISensor::Type,
150         0,0,0,0,0,0,
151         py_base_new
152 };
153
154 PyMethodDef SCA_RandomSensor::Methods[] = {
155         //Deprecated functions ----->
156         {"setSeed",     (PyCFunction) SCA_RandomSensor::sPySetSeed, METH_VARARGS, (const char *)SetSeed_doc},
157         {"getSeed",     (PyCFunction) SCA_RandomSensor::sPyGetSeed, METH_NOARGS, (const char *)GetSeed_doc},
158         {"getLastDraw", (PyCFunction) SCA_RandomSensor::sPyGetLastDraw, METH_NOARGS, (const char *)GetLastDraw_doc},
159         //<----- Deprecated
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 /* 1. setSeed                                                            */
170 const char SCA_RandomSensor::SetSeed_doc[] = 
171 "setSeed(seed)\n"
172 "\t- seed: integer\n"
173 "\tSet the initial seed of the generator. Equal seeds produce\n"
174 "\tequal series. If the seed is 0, the generator will produce\n"
175 "\tthe same value on every call.\n";
176 PyObject* SCA_RandomSensor::PySetSeed(PyObject* args) {
177         ShowDeprecationWarning("setSeed()", "the seed property");
178         long seedArg;
179         if(!PyArg_ParseTuple(args, "i:setSeed", &seedArg)) {
180                 return NULL;
181         }
182         
183         m_basegenerator->SetSeed(seedArg);
184         
185         Py_RETURN_NONE;
186 }
187
188 /* 2. getSeed                                                            */
189 const char SCA_RandomSensor::GetSeed_doc[] = 
190 "getSeed()\n"
191 "\tReturns the initial seed of the generator. Equal seeds produce\n"
192 "\tequal series.\n";
193 PyObject* SCA_RandomSensor::PyGetSeed() {
194         ShowDeprecationWarning("getSeed()", "the seed property");
195         return PyLong_FromSsize_t(m_basegenerator->GetSeed());
196 }
197
198 /* 3. getLastDraw                                                            */
199 const char SCA_RandomSensor::GetLastDraw_doc[] = 
200 "getLastDraw()\n"
201 "\tReturn the last value that was drawn.\n";
202 PyObject* SCA_RandomSensor::PyGetLastDraw() {
203         ShowDeprecationWarning("getLastDraw()", "the lastDraw property");
204         return PyLong_FromSsize_t(m_lastdraw);
205 }
206
207
208 PyObject* SCA_RandomSensor::pyattr_get_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
209 {
210         SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
211         return PyLong_FromSsize_t(self->m_basegenerator->GetSeed());
212 }
213
214 int SCA_RandomSensor::pyattr_set_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
215 {
216         SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
217         if (!PyLong_Check(value)) {
218                 PyErr_SetString(PyExc_TypeError, "sensor.seed = int: Random Sensor, expected an integer");
219                 return PY_SET_ATTR_FAIL;
220         }
221         self->m_basegenerator->SetSeed(PyLong_AsSsize_t(value));
222         return PY_SET_ATTR_SUCCESS;
223 }
224
225 /* eof */