svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[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         : SCA_ISensor(gameobj,eventmgr),
56         m_repeat(repeat),
57         m_delay(delay),
58         m_duration(duration)
59 {
60         Init();
61 }
62
63 void SCA_DelaySensor::Init()
64 {
65         m_lastResult = false;
66         m_frameCount = -1;
67         m_reset = true;
68 }
69
70 SCA_DelaySensor::~SCA_DelaySensor()
71 {
72         /* intentionally empty */
73 }
74
75 CValue* SCA_DelaySensor::GetReplica()
76 {
77         CValue* replica = new SCA_DelaySensor(*this);
78         // this will copy properties and so on...
79         replica->ProcessReplica();
80
81         return replica;
82 }
83
84
85
86 bool SCA_DelaySensor::IsPositiveTrigger()
87
88         return (m_invert ? !m_lastResult : m_lastResult);
89 }
90
91 bool SCA_DelaySensor::Evaluate()
92 {
93         bool trigger = false;
94         bool result;
95
96         if (m_frameCount==-1) {
97                 // this is needed to ensure ON trigger in case delay==0
98                 // and avoid spurious OFF trigger when duration==0
99                 m_lastResult = false;
100                 m_frameCount = 0;
101         }
102
103         if (m_frameCount<m_delay) {
104                 m_frameCount++;
105                 result = false;
106         } else if (m_duration > 0) {
107                 if (m_frameCount < m_delay+m_duration) {
108                         m_frameCount++;
109                         result = true;
110                 } else {
111                         result = false;
112                         if (m_repeat)
113                                 m_frameCount = -1;
114                 }
115         } else {
116                 result = true;
117                 if (m_repeat)
118                         m_frameCount = -1;
119         }
120         if ((m_reset && m_level) || result != m_lastResult)
121                 trigger = true;
122         m_reset = false;
123         m_lastResult = result;
124         return trigger;
125 }
126
127 /* ------------------------------------------------------------------------- */
128 /* Python functions                                                          */
129 /* ------------------------------------------------------------------------- */
130
131 /* Integration hooks ------------------------------------------------------- */
132 PyTypeObject SCA_DelaySensor::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_DelaySensor",
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_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 const char SCA_DelaySensor::SetDelay_doc[] = 
182 "setDelay(delay)\n"
183 "\t- delay: length of the initial OFF period as number of frame\n"
184 "\t         0 for immediate trigger\n"
185 "\tSet the initial delay before the positive trigger\n";
186 PyObject* SCA_DelaySensor::PySetDelay(PyObject* args)
187 {
188         ShowDeprecationWarning("setDelay()", "the delay property");
189         int delay;
190         
191         if(!PyArg_ParseTuple(args, "i:setDelay", &delay)) {
192                 return NULL;
193         }
194         if (delay < 0) {
195                 PyErr_SetString(PyExc_ValueError, "Delay cannot be negative");
196                 return NULL;
197         }
198         m_delay = delay;
199         Py_RETURN_NONE;
200 }
201
202 const char SCA_DelaySensor::SetDuration_doc[] = 
203 "setDuration(duration)\n"
204 "\t- duration: length of the ON period in number of frame after the initial off period\n"
205 "\t            0 for no ON period\n"
206 "\tSet the duration of the ON pulse after initial delay.\n"
207 "\tIf > 0, a negative trigger is fired at the end of the ON pulse.\n";
208 PyObject* SCA_DelaySensor::PySetDuration(PyObject* args)
209 {
210         ShowDeprecationWarning("setDuration()", "the duration property");
211         int duration;
212         
213         if(!PyArg_ParseTuple(args, "i:setDuration", &duration)) {
214                 return NULL;
215         }
216         if (duration < 0) {
217                 PyErr_SetString(PyExc_ValueError, "Duration cannot be negative");
218                 return NULL;
219         }
220         m_duration = duration;
221         Py_RETURN_NONE;
222 }
223
224 const char SCA_DelaySensor::SetRepeat_doc[] = 
225 "setRepeat(repeat)\n"
226 "\t- repeat: 1 if the initial OFF-ON cycle should be repeated indefinately\n"
227 "\t          0 if the initial OFF-ON cycle should run only once\n"
228 "\tSet the sensor repeat mode\n";
229 PyObject* SCA_DelaySensor::PySetRepeat(PyObject* args)
230 {
231         ShowDeprecationWarning("setRepeat()", "the repeat property");
232         int repeat;
233         
234         if(!PyArg_ParseTuple(args, "i:setRepeat", &repeat)) {
235                 return NULL;
236         }
237         m_repeat = (repeat != 0);
238         Py_RETURN_NONE;
239 }
240
241 const char SCA_DelaySensor::GetDelay_doc[] = 
242 "getDelay()\n"
243 "\tReturn the delay parameter value\n";
244 PyObject* SCA_DelaySensor::PyGetDelay()
245 {
246         ShowDeprecationWarning("getDelay()", "the delay property");
247         return PyLong_FromSsize_t(m_delay);
248 }
249
250 const char SCA_DelaySensor::GetDuration_doc[] = 
251 "getDuration()\n"
252 "\tReturn the duration parameter value\n";
253 PyObject* SCA_DelaySensor::PyGetDuration()
254 {
255         ShowDeprecationWarning("getDuration()", "the duration property");
256         return PyLong_FromSsize_t(m_duration);
257 }
258
259 const char SCA_DelaySensor::GetRepeat_doc[] = 
260 "getRepeat()\n"
261 "\tReturn the repeat parameter value\n";
262 PyObject* SCA_DelaySensor::PyGetRepeat()
263 {
264         ShowDeprecationWarning("getRepeat()", "the repeat property");
265         return BoolToPyArg(m_repeat);
266 }
267
268 /* eof */