doxygen: prevent GPL license block from being parsed as doxygen comment.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include <stdio.h>
40
41
42 /* ------------------------------------------------------------------------- */
43 /* Native functions                                                          */
44 /* ------------------------------------------------------------------------- */
45
46 SCA_ExpressionController::SCA_ExpressionController(SCA_IObject* gameobj,
47                                                                                                    const STR_String& exprtext)
48         :SCA_IController(gameobj),
49         m_exprText(exprtext),
50         m_exprCache(NULL)
51 {
52 }
53
54
55
56 SCA_ExpressionController::~SCA_ExpressionController()
57 {
58         if (m_exprCache)
59                 m_exprCache->Release();
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         replica->m_exprCache = NULL;
69         // this will copy properties and so on...
70         replica->ProcessReplica();
71
72         return replica;
73 }
74
75
76 // Forced deletion of precalculated expression to break reference loop
77 // Use this function when you know that you won't use the sensor anymore
78 void SCA_ExpressionController::Delete()
79 {
80         if (m_exprCache)
81         {
82                 m_exprCache->Release();
83                 m_exprCache = NULL;
84         }
85         Release();
86 }
87
88
89 void SCA_ExpressionController::Trigger(SCA_LogicManager* logicmgr)
90 {
91
92         bool expressionresult = false;
93         if (!m_exprCache)
94         {
95                 CParser parser;
96                 parser.SetContext(this->AddRef());
97                 m_exprCache = parser.ProcessText(m_exprText);
98         }
99         if (m_exprCache)
100         {
101                 CValue* value = m_exprCache->Calculate();
102                 if (value)
103                 {
104                         if (value->IsError())
105                         {
106                                 printf("%s\n", value->GetText().ReadPtr());
107                         } else
108                         {
109                                 float num = (float)value->GetNumber();
110                                 expressionresult = !MT_fuzzyZero(num);
111                         }
112                         value->Release();
113
114                 }
115         }
116
117         for (vector<SCA_IActuator*>::const_iterator i=m_linkedactuators.begin();
118         !(i==m_linkedactuators.end());i++)
119         {
120                 SCA_IActuator* actua = *i;
121                 logicmgr->AddActiveActuator(actua,expressionresult);
122         }
123 }
124
125
126
127 CValue* SCA_ExpressionController::FindIdentifier(const STR_String& identifiername)
128 {
129
130         CValue* identifierval = NULL;
131
132         for (vector<SCA_ISensor*>::const_iterator is=m_linkedsensors.begin();
133         !(is==m_linkedsensors.end());is++)
134         {
135                 SCA_ISensor* sensor = *is;
136                 if (sensor->GetName() == identifiername)
137                 {
138                         identifierval = new CBoolValue(sensor->GetState());
139                         //identifierval = sensor->AddRef();
140                 }
141
142                 //if (!sensor->IsPositiveTrigger())
143                 //{
144                 //      sensorresult = false;
145                 //      break;
146                 //}
147         }
148
149         if (identifierval)
150                 return identifierval;
151
152         return  GetParent()->FindIdentifier(identifiername);
153
154 }