BGE Py API patch from Mitchell Stokes, adds extra attributes and docs
[blender.git] / source / gameengine / Ketsji / KX_GameActuator.cpp
1 /**
2 * global game stuff
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_IActuator.h"
33 #include "KX_GameActuator.h"
34 //#include <iostream>
35 #include "KX_Scene.h"
36 #include "KX_KetsjiEngine.h"
37 #include "KX_PythonInit.h" /* for config load/saving */
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 /* ------------------------------------------------------------------------- */
44 /* Native functions                                                          */
45 /* ------------------------------------------------------------------------- */
46
47 KX_GameActuator::KX_GameActuator(SCA_IObject *gameobj, 
48                                                                    int mode,
49                                                                    const STR_String& filename,
50                                                                    const STR_String& loadinganimationname,
51                                                                    KX_Scene* scene,
52                                                                    KX_KetsjiEngine* ketsjiengine,
53                                                                    PyTypeObject* T)
54                                                                    : SCA_IActuator(gameobj, T)
55 {
56         m_mode = mode;
57         m_filename = filename;
58         m_loadinganimationname = loadinganimationname;
59         m_scene = scene;
60         m_ketsjiengine = ketsjiengine;
61 } /* End of constructor */
62
63
64
65 KX_GameActuator::~KX_GameActuator()
66
67         // there's nothing to be done here, really....
68 } /* end of destructor */
69
70
71
72 CValue* KX_GameActuator::GetReplica()
73 {
74         KX_GameActuator* replica = new KX_GameActuator(*this);
75         replica->ProcessReplica();
76         
77         return replica;
78 }
79
80
81
82 bool KX_GameActuator::Update()
83 {
84         // bool result = false;  /*unused*/
85         bool bNegativeEvent = IsNegativeEvent();
86         RemoveAllEvents();
87
88         if (bNegativeEvent)
89                 return false; // do nothing on negative events
90
91         switch (m_mode)
92         {
93         case KX_GAME_LOAD:
94         case KX_GAME_START:
95                 {
96                         if (m_ketsjiengine)
97                         {
98                                 STR_String exitstring = "start other game";
99                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_START_OTHER_GAME);
100                                 m_ketsjiengine->SetNameNextGame(m_filename);
101                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
102                         }
103
104                         break;
105                 }
106         case KX_GAME_RESTART:
107                 {
108                         if (m_ketsjiengine)
109                         {
110                                 STR_String exitstring = "restarting game";
111                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_RESTART_GAME);
112                                 m_ketsjiengine->SetNameNextGame(m_filename);
113                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
114                         }
115                         break;
116                 }
117         case KX_GAME_QUIT:
118                 {
119                         if (m_ketsjiengine)
120                         {
121                                 STR_String exitstring = "quiting game";
122                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_QUIT_GAME);
123                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
124                         }
125                         break;
126                 }
127         case KX_GAME_SAVECFG:
128                 {
129                         if (m_ketsjiengine)
130                         {
131                                 char mashal_path[512];
132                                 char *marshal_buffer = NULL;
133                                 unsigned int marshal_length;
134                                 FILE *fp = NULL;
135                                 
136                                 pathGamePythonConfig(mashal_path);
137                                 marshal_length = saveGamePythonConfig(&marshal_buffer);
138                                 
139                                 if (marshal_length && marshal_buffer) {
140                                         fp = fopen(mashal_path, "wb");
141                                         if (fp) {
142                                                 if (fwrite(marshal_buffer, 1, marshal_length, fp) != marshal_length) {
143                                                         printf("Warning: could not write marshal data\n");
144                                                 }
145                                                 fclose(fp);
146                                         } else {
147                                                 printf("Warning: could not open marshal file\n");
148                                         }
149                                 } else {
150                                         printf("Warning: could not create marshal buffer\n");
151                                 }
152                         }
153                         break;
154                 }
155         case KX_GAME_LOADCFG:
156                 {
157                         if (m_ketsjiengine)
158                         {
159                                 char mashal_path[512];
160                                 char *marshal_buffer;
161                                 int marshal_length;
162                                 FILE *fp = NULL;
163                                 int result;
164                                 
165                                 pathGamePythonConfig(mashal_path);
166                                 
167                                 fp = fopen(mashal_path, "rb");
168                                 if (fp) {
169                                         // obtain file size:
170                                         fseek (fp , 0 , SEEK_END);
171                                         marshal_length = ftell(fp);
172                                         rewind(fp);
173                                         
174                                         marshal_buffer = (char*) malloc (sizeof(char)*marshal_length);
175                                         
176                                         result = fread (marshal_buffer, 1, marshal_length, fp);
177                                         
178                                         if (result == marshal_length) {
179                                                 loadGamePythonConfig(marshal_buffer, marshal_length);
180                                         } else {
181                                                 printf("warning: could not read all of '%s'\n", mashal_path);
182                                         }
183                                         
184                                         free(marshal_buffer);
185                                         fclose(fp);
186                                 } else {
187                                         printf("warning: could not open '%s'\n", mashal_path);
188                                 }
189                         }
190                         break;
191                 }
192         default:
193                 ; /* do nothing? this is an internal error !!! */
194         }
195         
196         return false;
197 }
198
199
200
201
202
203 /* ------------------------------------------------------------------------- */
204 /* Python functions                                                          */
205 /* ------------------------------------------------------------------------- */
206
207 /* Integration hooks ------------------------------------------------------- */
208 PyTypeObject KX_GameActuator::Type = {
209 #if (PY_VERSION_HEX >= 0x02060000)
210         PyVarObject_HEAD_INIT(NULL, 0)
211 #else
212         /* python 2.5 and below */
213         PyObject_HEAD_INIT( NULL )  /* required py macro */
214         0,                          /* ob_size */
215 #endif
216                 "KX_GameActuator",
217                 sizeof(PyObjectPlus_Proxy),
218                 0,
219                 py_base_dealloc,
220                 0,
221                 0,
222                 0,
223                 0,
224                 py_base_repr,
225                 0,0,0,0,0,0,
226                 py_base_getattro,
227                 py_base_setattro,
228                 0,0,0,0,0,0,0,0,0,
229                 Methods
230 };
231
232
233
234 PyParentObject KX_GameActuator::Parents[] =
235 {
236         &KX_GameActuator::Type,
237                 &SCA_IActuator::Type,
238                 &SCA_ILogicBrick::Type,
239                 &CValue::Type,
240                 NULL
241 };
242
243
244
245 PyMethodDef KX_GameActuator::Methods[] =
246 {
247         // Deprecated ----->
248         {"getFile",     (PyCFunction) KX_GameActuator::sPyGetFile, METH_VARARGS, (PY_METHODCHAR)GetFile_doc},
249         {"setFile", (PyCFunction) KX_GameActuator::sPySetFile, METH_VARARGS, (PY_METHODCHAR)SetFile_doc},
250         // <-----
251         {NULL,NULL} //Sentinel
252 };
253
254 PyAttributeDef KX_GameActuator::Attributes[] = {
255         KX_PYATTRIBUTE_STRING_RW("file",0,100,false,KX_GameActuator,m_filename),
256         KX_PYATTRIBUTE_INT_RW("mode", KX_GAME_NODEF+1, KX_GAME_MAX-1, true, KX_GameActuator, m_mode),
257         { NULL }        //Sentinel
258 };
259
260 PyObject* KX_GameActuator::py_getattro(PyObject *attr)
261 {
262         py_getattro_up(SCA_IActuator);
263 }
264
265 PyObject* KX_GameActuator::py_getattro_dict() {
266         py_getattro_dict_up(SCA_IActuator);
267 }
268
269 int KX_GameActuator::py_setattro(PyObject *attr, PyObject *value)
270 {
271         py_setattro_up(SCA_IActuator);
272 }
273
274
275 // Deprecated ----->
276 /* getFile */
277 const char KX_GameActuator::GetFile_doc[] = 
278 "getFile()\n"
279 "get the name of the file to start.\n";
280 PyObject* KX_GameActuator::PyGetFile(PyObject* args, PyObject* kwds)
281 {       
282         ShowDeprecationWarning("getFile()", "the file property");
283         return PyString_FromString(m_filename);
284 }
285
286 /* setFile */
287 const char KX_GameActuator::SetFile_doc[] =
288 "setFile(name)\n"
289 "set the name of the file to start.\n";
290 PyObject* KX_GameActuator::PySetFile(PyObject* args, PyObject* kwds)
291 {
292         char* new_file;
293
294         ShowDeprecationWarning("setFile()", "the file property");
295         
296         if (!PyArg_ParseTuple(args, "s:setFile", &new_file))
297         {
298                 return NULL;
299         }
300         
301         m_filename = STR_String(new_file);
302
303         Py_RETURN_NONE;
304
305 }
306 // <-----