Blender V2.61 - r43446

SCA_ExpressionController.cpp

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