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