3f08f301dc1efd17f6cf2ea11cdeaa8b3e8191d2
[blender.git] / source / gameengine / GameLogic / SCA_DelaySensor.cpp
1 /**
2  * Delay trigger
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 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #ifdef WIN32
37 // This warning tells us about truncation of __long__ stl-generated names.
38 // It can occasionally cause DevStudio to have internal compiler warnings.
39 #pragma warning( disable : 4786 )     
40 #endif
41
42 #include "SCA_DelaySensor.h"
43 #include "SCA_LogicManager.h"
44 #include "SCA_EventManager.h"
45
46 /* ------------------------------------------------------------------------- */
47 /* Native functions                                                          */
48 /* ------------------------------------------------------------------------- */
49
50 SCA_DelaySensor::SCA_DelaySensor(class SCA_EventManager* eventmgr,
51                                                                  SCA_IObject* gameobj,
52                                                                  int delay,
53                                                                  int duration,
54                                                                  bool repeat,
55                                                                  PyTypeObject* T)
56         : SCA_ISensor(gameobj,eventmgr, T),
57         m_repeat(repeat),
58         m_delay(delay),
59         m_duration(duration)
60 {
61         Init();
62 }
63
64 void SCA_DelaySensor::Init()
65 {
66         m_lastResult = false;
67         m_frameCount = -1;
68         m_reset = true;
69 }
70
71 SCA_DelaySensor::~SCA_DelaySensor()
72 {
73         /* intentionally empty */
74 }
75
76 CValue* SCA_DelaySensor::GetReplica()
77 {
78         CValue* replica = new SCA_DelaySensor(*this);
79         // this will copy properties and so on...
80         CValue::AddDataToReplica(replica);
81
82         return replica;
83 }
84
85
86
87 bool SCA_DelaySensor::IsPositiveTrigger()
88
89         return (m_invert ? !m_lastResult : m_lastResult);
90 }
91
92 bool SCA_DelaySensor::Evaluate(CValue* event)
93 {
94         bool trigger = false;
95         bool result;
96
97         if (m_frameCount==-1) {
98                 // this is needed to ensure ON trigger in case delay==0
99                 // and avoid spurious OFF trigger when duration==0
100                 m_lastResult = false;
101                 m_frameCount = 0;
102         }
103
104         if (m_frameCount<m_delay) {
105                 m_frameCount++;
106                 result = false;
107         } else if (m_duration > 0) {
108                 if (m_frameCount < m_delay+m_duration) {
109                         m_frameCount++;
110                         result = true;
111                 } else {
112                         result = false;
113                         if (m_repeat)
114                                 m_frameCount = -1;
115                 }
116         } else {
117                 result = true;
118                 if (m_repeat)
119                         m_frameCount = -1;
120         }
121         if ((m_reset && m_level) || result != m_lastResult)
122                 trigger = true;
123         m_reset = false;
124         m_lastResult = result;
125         return trigger;
126 }
127
128 /* ------------------------------------------------------------------------- */
129 /* Python functions                                                          */
130 /* ------------------------------------------------------------------------- */
131
132 /* Integration hooks ------------------------------------------------------- */
133 PyTypeObject SCA_DelaySensor::Type = {
134         PyObject_HEAD_INIT(NULL)
135         0,
136         "SCA_DelaySensor",
137         sizeof(SCA_DelaySensor),
138         0,
139         PyDestructor,
140         0,
141         0,
142         0,
143         0,
144         py_base_repr,
145         0,0,0,0,0,0,
146         py_base_getattro,
147         py_base_setattro,
148         0,0,0,0,0,0,0,0,0,
149         Methods
150 };
151
152 PyParentObject SCA_DelaySensor::Parents[] = {
153         &SCA_DelaySensor::Type,
154         &SCA_ISensor::Type,
155         &SCA_ILogicBrick::Type,
156         &CValue::Type,
157         NULL
158 };
159
160 PyMethodDef SCA_DelaySensor::Methods[] = {
161         //Deprecated functions ------>
162         /* setProperty */
163         {"setDelay", (PyCFunction) SCA_DelaySensor::sPySetDelay, METH_VARARGS, (PY_METHODCHAR)SetDelay_doc},
164         {"setDuration", (PyCFunction) SCA_DelaySensor::sPySetDuration, METH_VARARGS, (PY_METHODCHAR)SetDuration_doc},
165         {"setRepeat", (PyCFunction) SCA_DelaySensor::sPySetRepeat, METH_VARARGS, (PY_METHODCHAR)SetRepeat_doc},
166         /* getProperty */
167         {"getDelay", (PyCFunction) SCA_DelaySensor::sPyGetDelay, METH_NOARGS, (PY_METHODCHAR)GetDelay_doc},
168         {"getDuration", (PyCFunction) SCA_DelaySensor::sPyGetDuration, METH_NOARGS, (PY_METHODCHAR)GetDuration_doc},
169         {"getRepeat", (PyCFunction) SCA_DelaySensor::sPyGetRepeat, METH_NOARGS, (PY_METHODCHAR)GetRepeat_doc},
170         //<----- Deprecated
171         {NULL,NULL} //Sentinel
172 };
173
174 PyAttributeDef SCA_DelaySensor::Attributes[] = {
175         KX_PYATTRIBUTE_INT_RW("delay",0,100000,true,SCA_DelaySensor,m_delay),
176         KX_PYATTRIBUTE_INT_RW("duration",0,100000,true,SCA_DelaySensor,m_duration),
177         KX_PYATTRIBUTE_BOOL_RW("repeat",SCA_DelaySensor,m_repeat),
178         { NULL }        //Sentinel
179 };
180
181 PyObject* SCA_DelaySensor::py_getattro(PyObject *attr) {
182         py_getattro_up(SCA_ISensor);
183 }
184
185 int SCA_DelaySensor::py_setattro(PyObject *attr, PyObject *value) {
186         py_setattro_up(SCA_ISensor);
187 }
188
189
190 const char SCA_DelaySensor::SetDelay_doc[] = 
191 "setDelay(delay)\n"
192 "\t- delay: length of the initial OFF period as number of frame\n"
193 "\t         0 for immediate trigger\n"
194 "\tSet the initial delay before the positive trigger\n";
195 PyObject* SCA_DelaySensor::PySetDelay(PyObject* self, PyObject* args, PyObject* kwds)
196 {
197         ShowDeprecationWarning("setDelay()", "the delay property");
198         int delay;
199         
200         if(!PyArg_ParseTuple(args, "i:setDelay", &delay)) {
201                 return NULL;
202         }
203         if (delay < 0) {
204                 PyErr_SetString(PyExc_ValueError, "Delay cannot be negative");
205                 return NULL;
206         }
207         m_delay = delay;
208         Py_RETURN_NONE;
209 }
210
211 const char SCA_DelaySensor::SetDuration_doc[] = 
212 "setDuration(duration)\n"
213 "\t- duration: length of the ON period in number of frame after the initial off period\n"
214 "\t            0 for no ON period\n"
215 "\tSet the duration of the ON pulse after initial delay.\n"
216 "\tIf > 0, a negative trigger is fired at the end of the ON pulse.\n";
217 PyObject* SCA_DelaySensor::PySetDuration(PyObject* self, PyObject* args, PyObject* kwds)
218 {
219         ShowDeprecationWarning("setDuration()", "the duration property");
220         int duration;
221         
222         if(!PyArg_ParseTuple(args, "i:setDuration", &duration)) {
223                 return NULL;
224         }
225         if (duration < 0) {
226                 PyErr_SetString(PyExc_ValueError, "Duration cannot be negative");
227                 return NULL;
228         }
229         m_duration = duration;
230         Py_RETURN_NONE;
231 }
232
233 const char SCA_DelaySensor::SetRepeat_doc[] = 
234 "setRepeat(repeat)\n"
235 "\t- repeat: 1 if the initial OFF-ON cycle should be repeated indefinately\n"
236 "\t          0 if the initial OFF-ON cycle should run only once\n"
237 "\tSet the sensor repeat mode\n";
238 PyObject* SCA_DelaySensor::PySetRepeat(PyObject* self, PyObject* args, PyObject* kwds)
239 {
240         ShowDeprecationWarning("setRepeat()", "the repeat property");
241         int repeat;
242         
243         if(!PyArg_ParseTuple(args, "i:setRepeat", &repeat)) {
244                 return NULL;
245         }
246         m_repeat = (repeat != 0);
247         Py_RETURN_NONE;
248 }
249
250 const char SCA_DelaySensor::GetDelay_doc[] = 
251 "getDelay()\n"
252 "\tReturn the delay parameter value\n";
253 PyObject* SCA_DelaySensor::PyGetDelay(PyObject* self)
254 {
255         ShowDeprecationWarning("getDelay()", "the delay property");
256         return PyInt_FromLong(m_delay);
257 }
258
259 const char SCA_DelaySensor::GetDuration_doc[] = 
260 "getDuration()\n"
261 "\tReturn the duration parameter value\n";
262 PyObject* SCA_DelaySensor::PyGetDuration(PyObject* self)
263 {
264         ShowDeprecationWarning("getDuration()", "the duration property");
265         return PyInt_FromLong(m_duration);
266 }
267
268 const char SCA_DelaySensor::GetRepeat_doc[] = 
269 "getRepeat()\n"
270 "\tReturn the repeat parameter value\n";
271 PyObject* SCA_DelaySensor::PyGetRepeat(PyObject* self)
272 {
273         ShowDeprecationWarning("getRepeat()", "the repeat property");
274         return BoolToPyArg(m_repeat);
275 }
276
277 /* eof */