4916d8a0a570e8ca0cb23aeff9597588c26cff37
[blender.git] / source / gameengine / GameLogic / SCA_ILogicBrick.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/GameLogic/SCA_ILogicBrick.cpp
29  *  \ingroup gamelogic
30  */
31
32
33 #include <stddef.h>
34
35 #include "SCA_ILogicBrick.h"
36 #include "PyObjectPlus.h"
37
38 SCA_LogicManager* SCA_ILogicBrick::m_sCurrentLogicManager = NULL;
39
40 SCA_ILogicBrick::SCA_ILogicBrick(SCA_IObject* gameobj)
41         :
42         CValue(),
43         m_gameobj(gameobj),
44         m_Execute_Priority(0),
45         m_Execute_Ueber_Priority(0),
46         m_bActive(false),
47         m_eventval(0)
48 {
49         m_text = "KX_LogicBrick";
50 }
51
52
53
54 SCA_ILogicBrick::~SCA_ILogicBrick()
55 {
56         RemoveEvent();
57 }
58
59
60
61 void SCA_ILogicBrick::SetExecutePriority(int execute_Priority)
62 {
63         m_Execute_Priority = execute_Priority;
64 }
65
66
67
68 void SCA_ILogicBrick::SetUeberExecutePriority(int execute_Priority)
69 {
70         m_Execute_Ueber_Priority = execute_Priority;
71 }
72
73
74
75 void SCA_ILogicBrick::ReParent(SCA_IObject* parent)
76 {
77         m_gameobj = parent;
78 }
79
80 void SCA_ILogicBrick::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
81 {
82         // nothing to do
83 }
84
85 CValue* SCA_ILogicBrick::Calc(VALUE_OPERATOR op, CValue *val)
86 {
87         CValue* temp = new CBoolValue(false,"");
88         CValue* result = temp->Calc(op,val);
89         temp->Release();
90
91         return result;
92
93
94
95
96 CValue* SCA_ILogicBrick::CalcFinal(VALUE_DATA_TYPE dtype,
97                                                                    VALUE_OPERATOR op,
98                                                                    CValue *val)
99 {
100         // same as bool implementation, so...
101         CValue* temp = new CBoolValue(false,"");
102         CValue* result = temp->CalcFinal(dtype,op,val);
103         temp->Release();
104
105         return result;
106 }
107
108
109
110 const STR_String& SCA_ILogicBrick::GetText()
111
112         if (m_name.Length())
113                 return m_name;
114
115         return m_text;
116 }
117
118
119
120 double SCA_ILogicBrick::GetNumber()
121 {
122         return -1;
123 }
124
125
126
127 STR_String& SCA_ILogicBrick::GetName()
128 {
129         return m_name;
130 }
131
132
133
134 void SCA_ILogicBrick::SetName(const char *name)
135 {
136         m_name = name;
137 }
138
139 bool SCA_ILogicBrick::LessComparedTo(SCA_ILogicBrick* other)
140 {
141         return (this->m_Execute_Ueber_Priority < other->m_Execute_Ueber_Priority) 
142                 || ((this->m_Execute_Ueber_Priority == other->m_Execute_Ueber_Priority) && 
143                 (this->m_Execute_Priority < other->m_Execute_Priority));
144 }
145
146 void SCA_ILogicBrick::RegisterEvent(CValue* eventval)
147 {
148         if (m_eventval)
149                 m_eventval->Release();
150
151         m_eventval = eventval->AddRef();
152 }
153
154
155 void SCA_ILogicBrick::RemoveEvent()
156 {
157         if (m_eventval)
158         {
159                 m_eventval->Release();
160                 m_eventval = NULL;
161         }
162 }
163
164
165
166 CValue* SCA_ILogicBrick::GetEvent()
167 {
168         if (m_eventval)
169         {
170                 return m_eventval->AddRef();
171         }
172         
173         return NULL;
174 }
175
176
177
178 #ifdef WITH_PYTHON
179
180 /* python stuff */
181
182 PyTypeObject SCA_ILogicBrick::Type = {
183         PyVarObject_HEAD_INIT(NULL, 0)
184         "SCA_ILogicBrick",
185         sizeof(PyObjectPlus_Proxy),
186         0,
187         py_base_dealloc,
188         0,
189         0,
190         0,
191         0,
192         py_base_repr,
193         0,0,0,0,0,0,0,0,0,
194         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
195         0,0,0,0,0,0,0,
196         Methods,
197         0,
198         0,
199         &CValue::Type,
200         0,0,0,0,0,0,
201         py_base_new
202 };
203
204 PyMethodDef SCA_ILogicBrick::Methods[] = {
205         {NULL,NULL} //Sentinel
206 };
207
208 PyAttributeDef SCA_ILogicBrick::Attributes[] = {
209         KX_PYATTRIBUTE_RO_FUNCTION("owner",     SCA_ILogicBrick, pyattr_get_owner),
210         KX_PYATTRIBUTE_INT_RW("executePriority",0,100000,false,SCA_ILogicBrick,m_Execute_Priority),
211         KX_PYATTRIBUTE_STRING_RO("name", SCA_ILogicBrick, m_name),
212         {NULL} //Sentinel
213 };
214
215 int SCA_ILogicBrick::CheckProperty(void *self, const PyAttributeDef *attrdef)
216 {
217         if (attrdef->m_type != KX_PYATTRIBUTE_TYPE_STRING || attrdef->m_length != 1) {
218                 PyErr_SetString(PyExc_AttributeError, "inconsistent check function for attribute type, report to blender.org");
219                 return 1;
220         }
221         SCA_ILogicBrick* brick = reinterpret_cast<SCA_ILogicBrick*>(self);
222         STR_String* var = reinterpret_cast<STR_String*>((char*)self+attrdef->m_offset);
223         CValue* prop = brick->GetParent()->FindIdentifier(*var);
224         bool error = prop->IsError();
225         prop->Release();
226         if (error) {
227                 PyErr_SetString(PyExc_ValueError, "string does not correspond to a property");
228                 return 1;
229         }
230         return 0;
231 }
232
233 /*Attribute functions */
234 PyObject *SCA_ILogicBrick::pyattr_get_owner(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
235 {
236         SCA_ILogicBrick* self = static_cast<SCA_ILogicBrick*>(self_v);
237         CValue* parent = self->GetParent();
238         
239         if (parent)
240                 return parent->GetProxy();
241         
242         Py_RETURN_NONE;
243 }
244
245
246
247 /* Conversions for making life better. */
248 bool SCA_ILogicBrick::PyArgToBool(int boolArg)
249 {
250         if (boolArg) {
251                 return true;
252         } else {
253                 return false;
254         }
255 }
256
257 PyObject *SCA_ILogicBrick::BoolToPyArg(bool boolarg)
258 {
259         return PyLong_FromSsize_t(boolarg? KX_TRUE: KX_FALSE);
260 }
261
262 #endif // WITH_PYTHON