Patch to change license to GPL only, from GSR.
[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 {
54 }
55
56
57
58 SCA_ExpressionController::~SCA_ExpressionController()
59 {
60 }
61
62
63
64 CValue* SCA_ExpressionController::GetReplica()
65 {
66         SCA_ExpressionController* replica = new SCA_ExpressionController(*this);
67         replica->m_exprText = m_exprText;
68         // this will copy properties and so on...
69         CValue::AddDataToReplica(replica);
70
71         return replica;
72 }
73
74
75
76 void SCA_ExpressionController::Trigger(SCA_LogicManager* logicmgr)
77 {
78
79         bool expressionresult = false;
80
81         CParser parser;
82         parser.SetContext(this->AddRef());
83         CExpression* expr = parser.ProcessText(m_exprText);
84         if (expr)
85         {
86                 CValue* value = expr->Calculate();
87                 if (value)
88                 {
89                         if (value->IsError())
90                         {
91                                 printf(value->GetText());
92                         } else
93                         {
94                                 float num = value->GetNumber();
95                                 expressionresult = !MT_fuzzyZero(num);
96                         }
97                         value->Release();
98
99                 }
100                 expr->Release();
101         }
102
103         /*
104
105         for (vector<SCA_ISensor*>::const_iterator is=m_linkedsensors.begin();
106         !(is==m_linkedsensors.end());is++)
107         {
108                 SCA_ISensor* sensor = *is;
109                 if (!sensor->IsPositiveTrigger())
110                 {
111                         sensorresult = false;
112                         break;
113                 }
114         }
115         
116           */
117         
118         CValue* newevent = new CBoolValue(expressionresult);
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,newevent);
125         }
126         //printf("expr %d.",expressionresult);
127         // every actuator that needs the event, has a it's own reference to it now so
128         // release it (so to be clear: if there is no actuator, it's deleted right now)
129         newevent->Release();
130 }
131
132
133
134 CValue* SCA_ExpressionController::FindIdentifier(const STR_String& identifiername)
135 {
136
137         CValue* identifierval = NULL;
138
139         for (vector<SCA_ISensor*>::const_iterator is=m_linkedsensors.begin();
140         !(is==m_linkedsensors.end());is++)
141         {
142                 SCA_ISensor* sensor = *is;
143                 if (sensor->GetName() == identifiername)
144                 {
145                         identifierval = new CBoolValue(sensor->IsPositiveTrigger());
146                         //identifierval = sensor->AddRef();
147                 }
148
149                 //if (!sensor->IsPositiveTrigger())
150                 //{
151                 //      sensorresult = false;
152                 //      break;
153                 //}
154         }
155
156         if (identifierval)
157                 return identifierval;
158
159         return  GetParent()->FindIdentifier(identifiername);
160
161 }