|
Blender
V2.59
|
00001 /* 00002 * 'Expression Controller enables to calculate an expression that wires inputs to output 00003 * 00004 * $Id: SCA_ExpressionController.cpp 35169 2011-02-25 13:32:11Z jesterking $ 00005 * 00006 * ***** BEGIN GPL LICENSE BLOCK ***** 00007 * 00008 * This program is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU General Public License 00010 * as published by the Free Software Foundation; either version 2 00011 * of the License, or (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program; if not, write to the Free Software Foundation, 00020 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00021 * 00022 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00023 * All rights reserved. 00024 * 00025 * The Original Code is: all of this file. 00026 * 00027 * Contributor(s): none yet. 00028 * 00029 * ***** END GPL LICENSE BLOCK ***** 00030 */ 00031 00037 #include "SCA_ExpressionController.h" 00038 #include "SCA_ISensor.h" 00039 #include "SCA_LogicManager.h" 00040 #include "BoolValue.h" 00041 #include "InputParser.h" 00042 #include "MT_Transform.h" // for fuzzyZero 00043 00044 #include <stdio.h> 00045 00046 00047 /* ------------------------------------------------------------------------- */ 00048 /* Native functions */ 00049 /* ------------------------------------------------------------------------- */ 00050 00051 SCA_ExpressionController::SCA_ExpressionController(SCA_IObject* gameobj, 00052 const STR_String& exprtext) 00053 :SCA_IController(gameobj), 00054 m_exprText(exprtext), 00055 m_exprCache(NULL) 00056 { 00057 } 00058 00059 00060 00061 SCA_ExpressionController::~SCA_ExpressionController() 00062 { 00063 if (m_exprCache) 00064 m_exprCache->Release(); 00065 } 00066 00067 00068 00069 CValue* SCA_ExpressionController::GetReplica() 00070 { 00071 SCA_ExpressionController* replica = new SCA_ExpressionController(*this); 00072 replica->m_exprText = m_exprText; 00073 replica->m_exprCache = NULL; 00074 // this will copy properties and so on... 00075 replica->ProcessReplica(); 00076 00077 return replica; 00078 } 00079 00080 00081 // Forced deletion of precalculated expression to break reference loop 00082 // Use this function when you know that you won't use the sensor anymore 00083 void SCA_ExpressionController::Delete() 00084 { 00085 if (m_exprCache) 00086 { 00087 m_exprCache->Release(); 00088 m_exprCache = NULL; 00089 } 00090 Release(); 00091 } 00092 00093 00094 void SCA_ExpressionController::Trigger(SCA_LogicManager* logicmgr) 00095 { 00096 00097 bool expressionresult = false; 00098 if (!m_exprCache) 00099 { 00100 CParser parser; 00101 parser.SetContext(this->AddRef()); 00102 m_exprCache = parser.ProcessText(m_exprText); 00103 } 00104 if (m_exprCache) 00105 { 00106 CValue* value = m_exprCache->Calculate(); 00107 if (value) 00108 { 00109 if (value->IsError()) 00110 { 00111 printf("%s\n", value->GetText().ReadPtr()); 00112 } else 00113 { 00114 float num = (float)value->GetNumber(); 00115 expressionresult = !MT_fuzzyZero(num); 00116 } 00117 value->Release(); 00118 00119 } 00120 } 00121 00122 for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin(); 00123 !(i==m_linkedactuators.end());i++) 00124 { 00125 SCA_IActuator* actua = *i; 00126 logicmgr->AddActiveActuator(actua,expressionresult); 00127 } 00128 } 00129 00130 00131 00132 CValue* SCA_ExpressionController::FindIdentifier(const STR_String& identifiername) 00133 { 00134 00135 CValue* identifierval = NULL; 00136 00137 for (vector<SCA_ISensor*>::const_iterator is=m_linkedsensors.begin(); 00138 !(is==m_linkedsensors.end());is++) 00139 { 00140 SCA_ISensor* sensor = *is; 00141 if (sensor->GetName() == identifiername) 00142 { 00143 identifierval = new CBoolValue(sensor->GetState()); 00144 //identifierval = sensor->AddRef(); 00145 } 00146 00147 //if (!sensor->IsPositiveTrigger()) 00148 //{ 00149 // sensorresult = false; 00150 // break; 00151 //} 00152 } 00153 00154 if (identifierval) 00155 return identifierval; 00156 00157 return GetParent()->FindIdentifier(identifiername); 00158 00159 }