doxygen: prevent GPL license block from being parsed as doxygen comment.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <stddef.h>
33
34 #include "SCA_RandomSensor.h"
35 #include "SCA_EventManager.h"
36 #include "SCA_RandomEventManager.h"
37 #include "SCA_LogicManager.h"
38 #include "ConstExpr.h"
39 #include <iostream>
40
41 /* ------------------------------------------------------------------------- */
42 /* Native functions                                                          */
43 /* ------------------------------------------------------------------------- */
44
45 SCA_RandomSensor::SCA_RandomSensor(SCA_EventManager* eventmgr, 
46                                  SCA_IObject* gameobj, 
47                                  int startseed)
48     : SCA_ISensor(gameobj,eventmgr)
49 {
50         m_basegenerator = new SCA_RandomNumberGenerator(startseed);
51         Init();
52 }
53
54
55
56 SCA_RandomSensor::~SCA_RandomSensor() 
57 {
58         m_basegenerator->Release();
59 }
60
61 void SCA_RandomSensor::Init()
62 {
63     m_iteration  = 0;
64         m_interval = 0;
65         m_lastdraw   = false;
66     m_currentDraw = m_basegenerator->Draw();
67 }
68
69
70 CValue* SCA_RandomSensor::GetReplica()
71 {
72         CValue* replica = new SCA_RandomSensor(*this);
73         // this will copy properties and so on...
74         replica->ProcessReplica();
75
76         return replica;
77 }
78
79 void SCA_RandomSensor::ProcessReplica()
80 {
81         SCA_ISensor::ProcessReplica();
82         // increment reference count so that we can release the generator at this end
83         m_basegenerator->AddRef();
84 }
85
86
87 bool SCA_RandomSensor::IsPositiveTrigger()
88
89         return (m_invert !=m_lastdraw);
90 }
91
92
93 bool SCA_RandomSensor::Evaluate()
94 {
95     /* Random generator is the generator from Line 25 of Table 1 in          */
96     /* [KNUTH 1981, The Art of Computer Programming Vol. 2                   */
97     /* (2nd Ed.), pp102]                                                     */
98     /* It's a very simple max. length sequence generator. We can             */
99     /* draw 32 bool values before having to generate the next                */
100     /* sequence value. There are some theorems that will tell you            */
101     /* this is a reasonable way of generating bools. Check Knuth.            */
102     /* Furthermore, we only draw each <delay>-eth frame.                     */
103
104         bool evaluateResult = false;
105
106         if (++m_interval > m_pulse_frequency) {
107             bool drawResult = false;
108                 m_interval = 0;
109                 if (m_iteration > 31) {
110                         m_currentDraw = m_basegenerator->Draw();
111                         drawResult = (m_currentDraw & 0x1) == 0;
112                         m_iteration = 1;
113                 } else {
114                         drawResult = ((m_currentDraw >> m_iteration) & 0x1) == 0;
115                         m_iteration++;
116                 }
117                 evaluateResult = drawResult != m_lastdraw;
118                 m_lastdraw = drawResult;
119         }
120     
121     /* now pass this result to some controller */
122         return evaluateResult;
123 }
124
125 #ifdef WITH_PYTHON
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         {NULL,NULL} //Sentinel
156 };
157
158 PyAttributeDef SCA_RandomSensor::Attributes[] = {
159         KX_PYATTRIBUTE_BOOL_RO("lastDraw",SCA_RandomSensor,m_lastdraw),
160         KX_PYATTRIBUTE_RW_FUNCTION("seed", SCA_RandomSensor, pyattr_get_seed, pyattr_set_seed),
161         {NULL} //Sentinel
162 };
163
164 PyObject* SCA_RandomSensor::pyattr_get_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
165 {
166         SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
167         return PyLong_FromSsize_t(self->m_basegenerator->GetSeed());
168 }
169
170 int SCA_RandomSensor::pyattr_set_seed(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
171 {
172         SCA_RandomSensor* self= static_cast<SCA_RandomSensor*>(self_v);
173         if (!PyLong_Check(value)) {
174                 PyErr_SetString(PyExc_TypeError, "sensor.seed = int: Random Sensor, expected an integer");
175                 return PY_SET_ATTR_FAIL;
176         }
177         self->m_basegenerator->SetSeed(PyLong_AsSsize_t(value));
178         return PY_SET_ATTR_SUCCESS;
179 }
180
181 #endif // WITH_PYTHON
182
183 /* eof */