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