svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[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 #if (PY_VERSION_HEX >= 0x02060000)
134         PyVarObject_HEAD_INIT(NULL, 0)
135 #else
136         /* python 2.5 and below */
137         PyObject_HEAD_INIT( NULL )  /* required py macro */
138         0,                          /* ob_size */
139 #endif
140         "SCA_RandomSensor",
141         sizeof(PyObjectPlus_Proxy),
142         0,
143         py_base_dealloc,
144         0,
145         0,
146         0,
147         0,
148         py_base_repr,
149         0,0,0,0,0,0,0,0,0,
150         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
151         0,0,0,0,0,0,0,
152         Methods,
153         0,
154         0,
155         &SCA_ISensor::Type,
156         0,0,0,0,0,0,
157         py_base_new
158 };
159
160 PyMethodDef SCA_RandomSensor::Methods[] = {
161         //Deprecated functions ----->
162         {"setSeed",     (PyCFunction) SCA_RandomSensor::sPySetSeed, METH_VARARGS, (PY_METHODCHAR)SetSeed_doc},
163         {"getSeed",     (PyCFunction) SCA_RandomSensor::sPyGetSeed, METH_NOARGS, (PY_METHODCHAR)GetSeed_doc},
164         {"getLastDraw", (PyCFunction) SCA_RandomSensor::sPyGetLastDraw, METH_NOARGS, (PY_METHODCHAR)GetLastDraw_doc},
165         //<----- Deprecated
166         {NULL,NULL} //Sentinel
167 };
168
169 PyAttributeDef SCA_RandomSensor::Attributes[] = {
170         KX_PYATTRIBUTE_BOOL_RO("lastDraw",SCA_RandomSensor,m_lastdraw),
171         KX_PYATTRIBUTE_RW_FUNCTION("seed", SCA_RandomSensor, pyattr_get_seed, pyattr_set_seed),
172         {NULL} //Sentinel
173 };
174
175 /* 1. setSeed                                                            */
176 const char SCA_RandomSensor::SetSeed_doc[] = 
177 "setSeed(seed)\n"
178 "\t- seed: integer\n"
179 "\tSet the initial seed of the generator. Equal seeds produce\n"
180 "\tequal series. If the seed is 0, the generator will produce\n"
181 "\tthe same value on every call.\n";
182 PyObject* SCA_RandomSensor::PySetSeed(PyObject* args) {
183         ShowDeprecationWarning("setSeed()", "the seed property");
184         long seedArg;
185         if(!PyArg_ParseTuple(args, "i:setSeed", &seedArg)) {
186                 return NULL;
187         }
188         
189         m_basegenerator->SetSeed(seedArg);
190         
191         Py_RETURN_NONE;
192 }
193
194 /* 2. getSeed                                                            */
195 const char SCA_RandomSensor::GetSeed_doc[] = 
196 "getSeed()\n"
197 "\tReturns the initial seed of the generator. Equal seeds produce\n"
198 "\tequal series.\n";
199 PyObject* SCA_RandomSensor::PyGetSeed() {
200         ShowDeprecationWarning("getSeed()", "the seed property");
201         return PyLong_FromSsize_t(m_basegenerator->GetSeed());
202 }
203
204 /* 3. getLastDraw                                                            */
205 const char SCA_RandomSensor::GetLastDraw_doc[] = 
206 "getLastDraw()\n"
207 "\tReturn the last value that was drawn.\n";
208 PyObject* SCA_RandomSensor::PyGetLastDraw() {
209         ShowDeprecationWarning("getLastDraw()", "the lastDraw property");
210         return PyLong_FromSsize_t(m_lastdraw);
211 }
212
213
214 PyObject* SCA_RandomSensor::pyattr_get_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
215 {
216         SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
217         return PyLong_FromSsize_t(self->m_basegenerator->GetSeed());
218 }
219
220 int SCA_RandomSensor::pyattr_set_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
221 {
222         SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
223         if (!PyLong_Check(value)) {
224                 PyErr_SetString(PyExc_TypeError, "sensor.seed = int: Random Sensor, expected an integer");
225                 return PY_SET_ATTR_FAIL;
226         }
227         self->m_basegenerator->SetSeed(PyLong_AsSsize_t(value));
228         return PY_SET_ATTR_SUCCESS;
229 }
230
231 /* eof */