Fix for [#31122] "Properties cant have Spaces in names when using interval as a evalu...
[blender.git] / source / gameengine / GameLogic / SCA_PropertySensor.cpp
1 /*
2  * Property sensor
3  *
4  *
5  * ***** BEGIN GPL LICENSE BLOCK *****
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
22  * All rights reserved.
23  *
24  * The Original Code is: all of this file.
25  *
26  * Contributor(s): none yet.
27  *
28  * ***** END GPL LICENSE BLOCK *****
29  */
30
31 /** \file gameengine/GameLogic/SCA_PropertySensor.cpp
32  *  \ingroup gamelogic
33  */
34
35
36 #include <stddef.h>
37
38 #include <iostream>
39 #include "SCA_PropertySensor.h"
40 #include "Operator2Expr.h"
41 #include "ConstExpr.h"
42 #include "InputParser.h"
43 #include "StringValue.h"
44 #include "SCA_EventManager.h"
45 #include "SCA_LogicManager.h"
46 #include "BoolValue.h"
47 #include "FloatValue.h"
48 #include <stdio.h>
49
50 SCA_PropertySensor::SCA_PropertySensor(SCA_EventManager* eventmgr,
51                                                                          SCA_IObject* gameobj,
52                                                                          const STR_String& propname,
53                                                                          const STR_String& propval,
54                                                                          const STR_String& propmaxval,
55                                                                          KX_PROPSENSOR_TYPE checktype)
56         : SCA_ISensor(gameobj,eventmgr),
57           m_checktype(checktype),
58           m_checkpropval(propval),
59           m_checkpropmaxval(propmaxval),
60           m_checkpropname(propname)
61 {
62         //CParser pars;
63         //pars.SetContext(this->AddRef());
64         //CValue* resultval = m_rightexpr->Calculate();
65
66         CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
67         if (!orgprop->IsError())
68         {
69                 m_previoustext = orgprop->GetText();
70         }
71         orgprop->Release();
72
73         Init();
74 }
75
76 void SCA_PropertySensor::Init()
77 {
78         m_recentresult = false;
79         m_lastresult = m_invert?true:false;
80         m_reset = true;
81 }
82
83 CValue* SCA_PropertySensor::GetReplica()
84 {
85         SCA_PropertySensor* replica = new SCA_PropertySensor(*this);
86         // m_range_expr must be recalculated on replica!
87         replica->ProcessReplica();
88         replica->Init();
89         
90         return replica;
91 }
92
93
94
95 bool SCA_PropertySensor::IsPositiveTrigger()
96 {
97         bool result = m_recentresult;//CheckPropertyCondition();
98         if (m_invert)
99                 result = !result;
100
101         return result;
102 }
103
104
105
106 SCA_PropertySensor::~SCA_PropertySensor()
107 {
108 }
109
110
111
112 bool SCA_PropertySensor::Evaluate()
113 {
114         bool result = CheckPropertyCondition();
115         bool reset = m_reset && m_level;
116         
117         m_reset = false;
118         if (m_lastresult!=result)
119         {
120                 m_lastresult = result;
121                 return true;
122         }
123         return (reset) ? true : false;
124 }
125
126
127 bool    SCA_PropertySensor::CheckPropertyCondition()
128 {
129
130         m_recentresult=false;
131         bool result=false;
132         bool reverse = false;
133         switch (m_checktype)
134         {
135         case KX_PROPSENSOR_NOTEQUAL:
136                 reverse = true;
137         case KX_PROPSENSOR_EQUAL:
138                 {
139                         CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
140                         if (!orgprop->IsError())
141                         {
142                                 const STR_String& testprop = orgprop->GetText();
143                                 // Force strings to upper case, to avoid confusion in
144                                 // bool tests. It's stupid the prop's identity is lost
145                                 // on the way here...
146                                 if ((&testprop == &CBoolValue::sTrueString) || (&testprop == &CBoolValue::sFalseString)) {
147                                         m_checkpropval.Upper();
148                                 }
149                                 result = (testprop == m_checkpropval);
150                                 
151                                 /* Patch: floating point values cant use strings usefully since you can have "0.0" == "0.0000"
152                                  * this could be made into a generic Value class function for comparing values with a string.
153                                  */
154                                 if (result==false && dynamic_cast<CFloatValue *>(orgprop) != NULL) {
155                                         float f;
156                                         
157                                         if (EOF == sscanf(m_checkpropval.ReadPtr(), "%f", &f))
158                                         {
159                                                 //error
160                                         } 
161                                         else {
162                                                 result = (f == ((CFloatValue *)orgprop)->GetFloat());
163                                         }
164                                 }
165                                 /* end patch */
166                         }
167                         orgprop->Release();
168
169                         if (reverse)
170                                 result = !result;
171                         break;
172
173                 }
174
175         case KX_PROPSENSOR_EXPRESSION:
176                 {
177                         /*
178                         if (m_rightexpr)
179                         {
180                                 CValue* resultval = m_rightexpr->Calculate();
181                                 if (resultval->IsError())
182                                 {
183                                         int i=0;
184                                         STR_String errortest = resultval->GetText();
185                                         printf(errortest);
186
187                                 } else
188                                 {
189                                         result = resultval->GetNumber() != 0;
190                                 }
191                         }
192                         */
193                         break;
194                 }
195         case KX_PROPSENSOR_INTERVAL:
196                 {
197                         CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
198                         if (!orgprop->IsError())
199                         {
200                                 float val = orgprop->GetText().ToFloat(), min = m_checkpropval.ToFloat(), max = m_checkpropmaxval.ToFloat();
201
202                                 result = (min <= val) && (val <= max);
203                         }
204
205                 break;
206                 }
207         case KX_PROPSENSOR_CHANGED:
208                 {
209                         CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
210                                 
211                         if (!orgprop->IsError())
212                         {
213                                 if (m_previoustext != orgprop->GetText())
214                                 {
215                                         m_previoustext = orgprop->GetText();
216                                         result = true;
217                                 }
218                         }
219                         orgprop->Release();
220
221                         //cout << " \nSens:Prop:changed!"; /* need implementation here!!! */
222                         break;
223                 }
224         default:
225                 ; /* error */
226         }
227
228         //the concept of Edge and Level triggering has unwanted effect for KX_PROPSENSOR_CHANGED
229         //see Game Engine bugtracker [ #3809 ]
230         if (m_checktype != KX_PROPSENSOR_CHANGED)
231         {
232                 m_recentresult=result;
233         } else
234         {
235                 m_recentresult=result;//true;
236         }
237         return result;
238 }
239
240 CValue* SCA_PropertySensor::FindIdentifier(const STR_String& identifiername)
241 {
242         return  GetParent()->FindIdentifier(identifiername);
243 }
244
245 #ifdef WITH_PYTHON
246
247 /* ------------------------------------------------------------------------- */
248 /* Python functions                                                          */
249 /* ------------------------------------------------------------------------- */
250
251 int SCA_PropertySensor::validValueForProperty(void *self, const PyAttributeDef*)
252 {
253         /* If someone actually do type checking please make sure the 'max' and 'min'
254          * are checked as well (currently they are calling the PrecalculateRangeExpression
255          * function directly */
256
257         /*  There is no type checking at this moment, unfortunately...           */
258         return 0;
259 }
260
261 /* Integration hooks ------------------------------------------------------- */
262 PyTypeObject SCA_PropertySensor::Type = {
263         PyVarObject_HEAD_INIT(NULL, 0)
264         "SCA_PropertySensor",
265         sizeof(PyObjectPlus_Proxy),
266         0,
267         py_base_dealloc,
268         0,
269         0,
270         0,
271         0,
272         py_base_repr,
273         0,0,0,0,0,0,0,0,0,
274         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
275         0,0,0,0,0,0,0,
276         Methods,
277         0,
278         0,
279         &SCA_ISensor::Type,
280         0,0,0,0,0,0,
281         py_base_new
282 };
283
284 PyMethodDef SCA_PropertySensor::Methods[] = {
285         {NULL,NULL} //Sentinel
286 };
287
288 PyAttributeDef SCA_PropertySensor::Attributes[] = {
289         KX_PYATTRIBUTE_INT_RW("mode",KX_PROPSENSOR_NODEF,KX_PROPSENSOR_MAX-1,false,SCA_PropertySensor,m_checktype),
290         KX_PYATTRIBUTE_STRING_RW_CHECK("propName",0,MAX_PROP_NAME,false,SCA_PropertySensor,m_checkpropname,CheckProperty),
291         KX_PYATTRIBUTE_STRING_RW_CHECK("value",0,100,false,SCA_PropertySensor,m_checkpropval,validValueForProperty),
292         KX_PYATTRIBUTE_STRING_RW_CHECK("min",0,100,false,SCA_PropertySensor,m_checkpropval,validValueForProperty),
293         KX_PYATTRIBUTE_STRING_RW_CHECK("max",0,100,false,SCA_PropertySensor,m_checkpropmaxval,validValueForProperty),
294         { NULL }        //Sentinel
295 };
296
297 #endif // WITH_PYTHON
298
299 /* eof */