BGE: bookmark option on controller to make them run before all other controllers.
[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                                                                                                    PyTypeObject* T)
51         :SCA_IController(gameobj,T),
52         m_exprText(exprtext),
53         m_exprCache(NULL)
54 {
55 }
56
57
58
59 SCA_ExpressionController::~SCA_ExpressionController()
60 {
61         if (m_exprCache)
62                 m_exprCache->Release();
63 }
64
65
66
67 CValue* SCA_ExpressionController::GetReplica()
68 {
69         SCA_ExpressionController* replica = new SCA_ExpressionController(*this);
70         replica->m_exprText = m_exprText;
71         replica->m_exprCache = NULL;
72         // this will copy properties and so on...
73         replica->ProcessReplica();
74
75         return replica;
76 }
77
78
79 // Forced deletion of precalculated expression to break reference loop
80 // Use this function when you know that you won't use the sensor anymore
81 void SCA_ExpressionController::Delete()
82 {
83         if (m_exprCache)
84         {
85                 m_exprCache->Release();
86                 m_exprCache = NULL;
87         }
88         Release();
89 }
90
91
92 void SCA_ExpressionController::Trigger(SCA_LogicManager* logicmgr)
93 {
94
95         bool expressionresult = false;
96         if (!m_exprCache)
97         {
98                 CParser parser;
99                 parser.SetContext(this->AddRef());
100                 m_exprCache = parser.ProcessText(m_exprText);
101         }
102         if (m_exprCache)
103         {
104                 CValue* value = m_exprCache->Calculate();
105                 if (value)
106                 {
107                         if (value->IsError())
108                         {
109                                 printf(value->GetText());
110                         } else
111                         {
112                                 float num = (float)value->GetNumber();
113                                 expressionresult = !MT_fuzzyZero(num);
114                         }
115                         value->Release();
116
117                 }
118         }
119
120         for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin();
121         !(i==m_linkedactuators.end());i++)
122         {
123                 SCA_IActuator* actua = *i;
124                 logicmgr->AddActiveActuator(actua,expressionresult);
125         }
126 }
127
128
129
130 CValue* SCA_ExpressionController::FindIdentifier(const STR_String& identifiername)
131 {
132
133         CValue* identifierval = NULL;
134
135         for (vector<SCA_ISensor*>::const_iterator is=m_linkedsensors.begin();
136         !(is==m_linkedsensors.end());is++)
137         {
138                 SCA_ISensor* sensor = *is;
139                 if (sensor->GetName() == identifiername)
140                 {
141                         identifierval = new CBoolValue(sensor->GetState());
142                         //identifierval = sensor->AddRef();
143                 }
144
145                 //if (!sensor->IsPositiveTrigger())
146                 //{
147                 //      sensorresult = false;
148                 //      break;
149                 //}
150         }
151
152         if (identifierval)
153                 return identifierval;
154
155         return  GetParent()->FindIdentifier(identifiername);
156
157 }