Filling in branch from trunk
[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/BL DUAL 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. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL/BL DUAL LICENSE BLOCK *****
33  */
34
35 #include "SCA_ExpressionController.h"
36 #include "SCA_ISensor.h"
37 #include "SCA_LogicManager.h"
38 #include "BoolValue.h"
39 #include "InputParser.h"
40 #include "MT_Transform.h" // for fuzzyZero
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46
47 /* ------------------------------------------------------------------------- */
48 /* Native functions                                                          */
49 /* ------------------------------------------------------------------------- */
50
51 SCA_ExpressionController::SCA_ExpressionController(SCA_IObject* gameobj,
52                                                                                                    const STR_String& exprtext,
53                                                                                                    PyTypeObject* T)
54         :SCA_IController(gameobj,T),
55         m_exprText(exprtext)
56 {
57 }
58
59
60
61 SCA_ExpressionController::~SCA_ExpressionController()
62 {
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         // this will copy properties and so on...
72         CValue::AddDataToReplica(replica);
73
74         return replica;
75 }
76
77
78
79 void SCA_ExpressionController::Trigger(SCA_LogicManager* logicmgr)
80 {
81
82         bool expressionresult = false;
83
84         CParser parser;
85         parser.SetContext(this->AddRef());
86         CExpression* expr = parser.ProcessText(m_exprText);
87         if (expr)
88         {
89                 CValue* value = expr->Calculate();
90                 if (value)
91                 {
92                         if (value->IsError())
93                         {
94                                 printf(value->GetText());
95                         } else
96                         {
97                                 float num = value->GetNumber();
98                                 expressionresult = !MT_fuzzyZero(num);
99                         }
100                         value->Release();
101
102                 }
103                 expr->Release();
104         }
105
106         /*
107
108         for (vector<SCA_ISensor*>::const_iterator is=m_linkedsensors.begin();
109         !(is==m_linkedsensors.end());is++)
110         {
111                 SCA_ISensor* sensor = *is;
112                 if (!sensor->IsPositiveTrigger())
113                 {
114                         sensorresult = false;
115                         break;
116                 }
117         }
118         
119           */
120         
121         CValue* newevent = new CBoolValue(expressionresult);
122
123         for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin();
124         !(i==m_linkedactuators.end());i++)
125         {
126                 SCA_IActuator* actua = *i;
127                 logicmgr->AddActiveActuator(actua,newevent);
128         }
129         //printf("expr %d.",expressionresult);
130         // every actuator that needs the event, has a it's own reference to it now so
131         // release it (so to be clear: if there is no actuator, it's deleted right now)
132         newevent->Release();
133 }
134
135
136
137 CValue* SCA_ExpressionController::FindIdentifier(const STR_String& identifiername)
138 {
139
140         CValue* identifierval = NULL;
141
142         for (vector<SCA_ISensor*>::const_iterator is=m_linkedsensors.begin();
143         !(is==m_linkedsensors.end());is++)
144         {
145                 SCA_ISensor* sensor = *is;
146                 if (sensor->GetName() == identifiername)
147                 {
148                         identifierval = new CBoolValue(sensor->IsPositiveTrigger());
149                         //identifierval = sensor->AddRef();
150                 }
151
152                 //if (!sensor->IsPositiveTrigger())
153                 //{
154                 //      sensorresult = false;
155                 //      break;
156                 //}
157         }
158
159         if (identifierval)
160                 return identifierval;
161
162         return  GetParent()->FindIdentifier(identifiername);
163
164 }