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