python api for ID property access by Joseph Eager, copied from blender 2.4x.
[blender.git] / source / gameengine / GameLogic / SCA_PropertySensor.cpp
1 /**
2  * Property sensor
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 <iostream>
33 #include "SCA_PropertySensor.h"
34 #include "Operator2Expr.h"
35 #include "ConstExpr.h"
36 #include "InputParser.h"
37 #include "StringValue.h"
38 #include "SCA_EventManager.h"
39 #include "SCA_LogicManager.h"
40 #include "BoolValue.h"
41 #include "FloatValue.h"
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 SCA_PropertySensor::SCA_PropertySensor(SCA_EventManager* eventmgr,
48                                                                          SCA_IObject* gameobj,
49                                                                          const STR_String& propname,
50                                                                          const STR_String& propval,
51                                                                          const STR_String& propmaxval,
52                                                                          KX_PROPSENSOR_TYPE checktype)
53         : SCA_ISensor(gameobj,eventmgr),
54           m_checktype(checktype),
55           m_checkpropval(propval),
56           m_checkpropmaxval(propmaxval),
57           m_checkpropname(propname),
58           m_range_expr(NULL)
59 {
60         //CParser pars;
61         //pars.SetContext(this->AddRef());
62         //CValue* resultval = m_rightexpr->Calculate();
63
64         CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
65         if (!orgprop->IsError())
66         {
67                 m_previoustext = orgprop->GetText();
68         }
69         orgprop->Release();
70
71         if (m_checktype==KX_PROPSENSOR_INTERVAL)
72         {
73                 PrecalculateRangeExpression();
74         }
75         Init();
76 }
77
78 void SCA_PropertySensor::Init()
79 {
80         m_recentresult = false;
81         m_lastresult = m_invert?true:false;
82         m_reset = true;
83 }
84
85 void SCA_PropertySensor::PrecalculateRangeExpression()
86 {
87                 CParser pars;
88                 //The context is needed to retrieve the property at runtime but it creates
89                 //loop of references
90                 pars.SetContext(this->AddRef());
91                 STR_String checkstr = "(" + m_checkpropval + " <= " 
92                                                         + m_checkpropname + ") && ( " 
93                                                         + m_checkpropname + " <= " 
94                                                         + m_checkpropmaxval + ")";
95
96                 m_range_expr = pars.ProcessText(checkstr);
97 }
98
99 // Forced deletion of precalculated range expression to break reference loop
100 // Use this function when you know that you won't use the sensor anymore
101 void SCA_PropertySensor::Delete()
102 {
103         if (m_range_expr)
104         {
105                 m_range_expr->Release();
106                 m_range_expr = NULL;
107         }
108         Release();
109 }
110
111 CValue* SCA_PropertySensor::GetReplica()
112 {
113         SCA_PropertySensor* replica = new SCA_PropertySensor(*this);
114         // m_range_expr must be recalculated on replica!
115         replica->ProcessReplica();
116         replica->Init();
117
118         replica->m_range_expr = NULL;
119         if (replica->m_checktype==KX_PROPSENSOR_INTERVAL)
120         {
121                 replica->PrecalculateRangeExpression();
122         }
123         
124         
125         return replica;
126 }
127
128
129
130 bool SCA_PropertySensor::IsPositiveTrigger()
131 {
132         bool result = m_recentresult;//CheckPropertyCondition();
133         if (m_invert)
134                 result = !result;
135
136         return result;
137 }
138
139
140
141 SCA_PropertySensor::~SCA_PropertySensor()
142 {
143         //if (m_rightexpr)
144         //      m_rightexpr->Release();
145
146         if (m_range_expr)
147         {
148                 m_range_expr->Release();
149                 m_range_expr=NULL;
150         }
151
152 }
153
154
155
156 bool SCA_PropertySensor::Evaluate()
157 {
158         bool result = CheckPropertyCondition();
159         bool reset = m_reset && m_level;
160         
161         m_reset = false;
162         if (m_lastresult!=result)
163         {
164                 m_lastresult = result;
165                 return true;
166         }
167         return (reset) ? true : false;
168 }
169
170
171 bool    SCA_PropertySensor::CheckPropertyCondition()
172 {
173
174         m_recentresult=false;
175         bool result=false;
176         bool reverse = false;
177         switch (m_checktype)
178         {
179         case KX_PROPSENSOR_NOTEQUAL:
180                 reverse = true;
181         case KX_PROPSENSOR_EQUAL:
182                 {
183                         CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
184                         if (!orgprop->IsError())
185                         {
186                                 const STR_String& testprop = orgprop->GetText();
187                                 // Force strings to upper case, to avoid confusion in
188                                 // bool tests. It's stupid the prop's identity is lost
189                                 // on the way here...
190                                 if ((&testprop == &CBoolValue::sTrueString) || (&testprop == &CBoolValue::sFalseString)) {
191                                         m_checkpropval.Upper();
192                                 }
193                                 result = (testprop == m_checkpropval);
194                                 
195                                 /* Patch: floating point values cant use strings usefully since you can have "0.0" == "0.0000"
196                                  * this could be made into a generic Value class function for comparing values with a string.
197                                  */
198                                 if(result==false && dynamic_cast<CFloatValue *>(orgprop) != NULL) {
199                                         float f;
200                                         
201                                         if(EOF == sscanf(m_checkpropval.ReadPtr(), "%f", &f))
202                                         {
203                                                 //error
204                                         } 
205                                         else {
206                                                 result = (f == ((CFloatValue *)orgprop)->GetFloat());
207                                         }
208                                 }
209                                 /* end patch */
210                         }
211                         orgprop->Release();
212
213                         if (reverse)
214                                 result = !result;
215                         break;
216
217                 }
218
219         case KX_PROPSENSOR_EXPRESSION:
220                 {
221                         /*
222                         if (m_rightexpr)
223                         {
224                                 CValue* resultval = m_rightexpr->Calculate();
225                                 if (resultval->IsError())
226                                 {
227                                         int i=0;
228                                         STR_String errortest = resultval->GetText();
229                                         printf(errortest);
230
231                                 } else
232                                 {
233                                         result = resultval->GetNumber() != 0;
234                                 }
235                         }
236                         */
237                         break;
238                 }
239         case KX_PROPSENSOR_INTERVAL:
240                 {
241                         //CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
242                         //if (orgprop)
243                         //{
244                                 if (m_range_expr)
245                                 {
246                                         CValue* vallie = m_range_expr->Calculate();
247                                         if (vallie)
248                                         {
249                                                 const STR_String& errtext = vallie->GetText();
250                                                 if (&errtext == &CBoolValue::sTrueString)
251                                                 {
252                                                         result = true;
253                                                 } else
254                                                 {
255                                                         if (vallie->IsError())
256                                                         {
257                                                                 //printf (errtext.ReadPtr());
258                                                         } 
259                                                 }
260                                                 
261                                                 vallie->Release();
262                                         }
263                                 }
264
265                                 
266                         //}
267                         
268                 //cout << " \nSens:Prop:interval!"; /* need implementation here!!! */
269
270                 break;
271                 }
272         case KX_PROPSENSOR_CHANGED:
273                 {
274                         CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
275                                 
276                         if (!orgprop->IsError())
277                         {
278                                 if (m_previoustext != orgprop->GetText())
279                                 {
280                                         m_previoustext = orgprop->GetText();
281                                         result = true;
282                                 }
283                         }
284                         orgprop->Release();
285
286                         //cout << " \nSens:Prop:changed!"; /* need implementation here!!! */
287                         break;
288                 }
289         default:
290                 ; /* error */
291         }
292
293         //the concept of Edge and Level triggering has unwanted effect for KX_PROPSENSOR_CHANGED
294         //see Game Engine bugtracker [ #3809 ]
295         if (m_checktype != KX_PROPSENSOR_CHANGED)
296         {
297                 m_recentresult=result;
298         } else
299         {
300                 m_recentresult=result;//true;
301         }
302         return result;
303 }
304
305 CValue* SCA_PropertySensor::FindIdentifier(const STR_String& identifiername)
306 {
307         return  GetParent()->FindIdentifier(identifiername);
308 }
309
310 #ifndef DISABLE_PYTHON
311
312 /* ------------------------------------------------------------------------- */
313 /* Python functions                                                          */
314 /* ------------------------------------------------------------------------- */
315
316 int SCA_PropertySensor::validValueForProperty(void *self, const PyAttributeDef*)
317 {
318         /*  There is no type checking at this moment, unfortunately...           */
319         return 0;
320 }
321
322 /* Integration hooks ------------------------------------------------------- */
323 PyTypeObject SCA_PropertySensor::Type = {
324         PyVarObject_HEAD_INIT(NULL, 0)
325         "SCA_PropertySensor",
326         sizeof(PyObjectPlus_Proxy),
327         0,
328         py_base_dealloc,
329         0,
330         0,
331         0,
332         0,
333         py_base_repr,
334         0,0,0,0,0,0,0,0,0,
335         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
336         0,0,0,0,0,0,0,
337         Methods,
338         0,
339         0,
340         &SCA_ISensor::Type,
341         0,0,0,0,0,0,
342         py_base_new
343 };
344
345 PyMethodDef SCA_PropertySensor::Methods[] = {
346         {NULL,NULL} //Sentinel
347 };
348
349 PyAttributeDef SCA_PropertySensor::Attributes[] = {
350         KX_PYATTRIBUTE_INT_RW("mode",KX_PROPSENSOR_NODEF,KX_PROPSENSOR_MAX-1,false,SCA_PropertySensor,m_checktype),
351         KX_PYATTRIBUTE_STRING_RW_CHECK("propName",0,100,false,SCA_PropertySensor,m_checkpropname,CheckProperty),
352         KX_PYATTRIBUTE_STRING_RW_CHECK("value",0,100,false,SCA_PropertySensor,m_checkpropval,validValueForProperty),
353         { NULL }        //Sentinel
354 };
355
356 #endif // DISABLE_PYTHON
357
358 /* eof */