use long long rather then int for storing game logic properties.
[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         CValue::AddDataToReplica(replica);
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                 //m_exprCache->Release();
119                 //m_exprCache = NULL;
120         }
121
122         /*
123
124         for (vector<SCA_ISensor*>::const_iterator is=m_linkedsensors.begin();
125         !(is==m_linkedsensors.end());is++)
126         {
127                 SCA_ISensor* sensor = *is;
128                 if (!sensor->IsPositiveTrigger())
129                 {
130                         sensorresult = false;
131                         break;
132                 }
133         }
134         
135           */
136         
137         CValue* newevent = new CBoolValue(expressionresult);
138
139         for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin();
140         !(i==m_linkedactuators.end());i++)
141         {
142                 SCA_IActuator* actua = *i;
143                 logicmgr->AddActiveActuator(actua,newevent);
144         }
145         //printf("expr %d.",expressionresult);
146         // every actuator that needs the event, has a it's own reference to it now so
147         // release it (so to be clear: if there is no actuator, it's deleted right now)
148         newevent->Release();
149 }
150
151
152
153 CValue* SCA_ExpressionController::FindIdentifier(const STR_String& identifiername)
154 {
155
156         CValue* identifierval = NULL;
157
158         for (vector<SCA_ISensor*>::const_iterator is=m_linkedsensors.begin();
159         !(is==m_linkedsensors.end());is++)
160         {
161                 SCA_ISensor* sensor = *is;
162                 if (sensor->GetName() == identifiername)
163                 {
164                         identifierval = new CBoolValue(sensor->IsPositiveTrigger());
165                         //identifierval = sensor->AddRef();
166                 }
167
168                 //if (!sensor->IsPositiveTrigger())
169                 //{
170                 //      sensorresult = false;
171                 //      break;
172                 //}
173         }
174
175         if (identifierval)
176                 return identifierval;
177
178         return  GetParent()->FindIdentifier(identifiername);
179
180 }