svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / gameengine / GameLogic / SCA_ExpressionController.cpp
1 /**
2  * 'Expression Controller enables to calculate an expression that wires inputs to output
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 "SCA_ExpressionController.h"
33 #include "SCA_ISensor.h"
34 #include "SCA_LogicManager.h"
35 #include "BoolValue.h"
36 #include "InputParser.h"
37 #include "MT_Transform.h" // for fuzzyZero
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43
44 /* ------------------------------------------------------------------------- */
45 /* Native functions                                                          */
46 /* ------------------------------------------------------------------------- */
47
48 SCA_ExpressionController::SCA_ExpressionController(SCA_IObject* gameobj,
49                                                                                                    const STR_String& exprtext)
50         :SCA_IController(gameobj),
51         m_exprText(exprtext),
52         m_exprCache(NULL)
53 {
54 }
55
56
57
58 SCA_ExpressionController::~SCA_ExpressionController()
59 {
60         if (m_exprCache)
61                 m_exprCache->Release();
62 }
63
64
65
66 CValue* SCA_ExpressionController::GetReplica()
67 {
68         SCA_ExpressionController* replica = new SCA_ExpressionController(*this);
69         replica->m_exprText = m_exprText;
70         replica->m_exprCache = NULL;
71         // this will copy properties and so on...
72         replica->ProcessReplica();
73
74         return replica;
75 }
76
77
78 // Forced deletion of precalculated expression to break reference loop
79 // Use this function when you know that you won't use the sensor anymore
80 void SCA_ExpressionController::Delete()
81 {
82         if (m_exprCache)
83         {
84                 m_exprCache->Release();
85                 m_exprCache = NULL;
86         }
87         Release();
88 }
89
90
91 void SCA_ExpressionController::Trigger(SCA_LogicManager* logicmgr)
92 {
93
94         bool expressionresult = false;
95         if (!m_exprCache)
96         {
97                 CParser parser;
98                 parser.SetContext(this->AddRef());
99                 m_exprCache = parser.ProcessText(m_exprText);
100         }
101         if (m_exprCache)
102         {
103                 CValue* value = m_exprCache->Calculate();
104                 if (value)
105                 {
106                         if (value->IsError())
107                         {
108                                 printf(value->GetText());
109                         } else
110                         {
111                                 float num = (float)value->GetNumber();
112                                 expressionresult = !MT_fuzzyZero(num);
113                         }
114                         value->Release();
115
116                 }
117         }
118
119         for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin();
120         !(i==m_linkedactuators.end());i++)
121         {
122                 SCA_IActuator* actua = *i;
123                 logicmgr->AddActiveActuator(actua,expressionresult);
124         }
125 }
126
127
128
129 CValue* SCA_ExpressionController::FindIdentifier(const STR_String& identifiername)
130 {
131
132         CValue* identifierval = NULL;
133
134         for (vector<SCA_ISensor*>::const_iterator is=m_linkedsensors.begin();
135         !(is==m_linkedsensors.end());is++)
136         {
137                 SCA_ISensor* sensor = *is;
138                 if (sensor->GetName() == identifiername)
139                 {
140                         identifierval = new CBoolValue(sensor->GetState());
141                         //identifierval = sensor->AddRef();
142                 }
143
144                 //if (!sensor->IsPositiveTrigger())
145                 //{
146                 //      sensorresult = false;
147                 //      break;
148                 //}
149         }
150
151         if (identifierval)
152                 return identifierval;
153
154         return  GetParent()->FindIdentifier(identifiername);
155
156 }