svn merge -r 20651:20822 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[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                                 if (marshal_buffer)
153                                         delete [] marshal_buffer;
154                         }
155                         break;
156                 }
157         case KX_GAME_LOADCFG:
158                 {
159                         if (m_ketsjiengine)
160                         {
161                                 char mashal_path[512];
162                                 char *marshal_buffer;
163                                 int marshal_length;
164                                 FILE *fp = NULL;
165                                 int result;
166                                 
167                                 pathGamePythonConfig(mashal_path);
168                                 
169                                 fp = fopen(mashal_path, "rb");
170                                 if (fp) {
171                                         // obtain file size:
172                                         fseek (fp , 0 , SEEK_END);
173                                         marshal_length = ftell(fp);
174                                         rewind(fp);
175                                         
176                                         marshal_buffer = (char*) malloc (sizeof(char)*marshal_length);
177                                         
178                                         result = fread (marshal_buffer, 1, marshal_length, fp);
179                                         
180                                         if (result == marshal_length) {
181                                                 loadGamePythonConfig(marshal_buffer, marshal_length);
182                                         } else {
183                                                 printf("warning: could not read all of '%s'\n", mashal_path);
184                                         }
185                                         
186                                         free(marshal_buffer);
187                                         fclose(fp);
188                                 } else {
189                                         printf("warning: could not open '%s'\n", mashal_path);
190                                 }
191                         }
192                         break;
193                 }
194         default:
195                 ; /* do nothing? this is an internal error !!! */
196         }
197         
198         return false;
199 }
200
201
202
203
204
205 /* ------------------------------------------------------------------------- */
206 /* Python functions                                                          */
207 /* ------------------------------------------------------------------------- */
208
209 /* Integration hooks ------------------------------------------------------- */
210 PyTypeObject KX_GameActuator::Type = {
211 #if (PY_VERSION_HEX >= 0x02060000)
212         PyVarObject_HEAD_INIT(NULL, 0)
213 #else
214         /* python 2.5 and below */
215         PyObject_HEAD_INIT( NULL )  /* required py macro */
216         0,                          /* ob_size */
217 #endif
218                 "KX_GameActuator",
219                 sizeof(PyObjectPlus_Proxy),
220                 0,
221                 py_base_dealloc,
222                 0,
223                 0,
224                 0,
225                 0,
226                 py_base_repr,
227                 0,0,0,0,0,0,
228                 py_base_getattro,
229                 py_base_setattro,
230                 0,0,0,0,0,0,0,0,0,
231                 Methods
232 };
233
234
235
236 PyParentObject KX_GameActuator::Parents[] =
237 {
238         &KX_GameActuator::Type,
239                 &SCA_IActuator::Type,
240                 &SCA_ILogicBrick::Type,
241                 &CValue::Type,
242                 NULL
243 };
244
245
246
247 PyMethodDef KX_GameActuator::Methods[] =
248 {
249         // Deprecated ----->
250         {"getFile",     (PyCFunction) KX_GameActuator::sPyGetFile, METH_VARARGS, (PY_METHODCHAR)GetFile_doc},
251         {"setFile", (PyCFunction) KX_GameActuator::sPySetFile, METH_VARARGS, (PY_METHODCHAR)SetFile_doc},
252         // <-----
253         {NULL,NULL} //Sentinel
254 };
255
256 PyAttributeDef KX_GameActuator::Attributes[] = {
257         KX_PYATTRIBUTE_STRING_RW("fileName",0,100,false,KX_GameActuator,m_filename),
258         KX_PYATTRIBUTE_INT_RW("mode", KX_GAME_NODEF+1, KX_GAME_MAX-1, true, KX_GameActuator, m_mode),
259         { NULL }        //Sentinel
260 };
261
262 PyObject* KX_GameActuator::py_getattro(PyObject *attr)
263 {
264         py_getattro_up(SCA_IActuator);
265 }
266
267 PyObject* KX_GameActuator::py_getattro_dict() {
268         py_getattro_dict_up(SCA_IActuator);
269 }
270
271 int KX_GameActuator::py_setattro(PyObject *attr, PyObject *value)
272 {
273         py_setattro_up(SCA_IActuator);
274 }
275
276
277 // Deprecated ----->
278 /* getFile */
279 const char KX_GameActuator::GetFile_doc[] = 
280 "getFile()\n"
281 "get the name of the file to start.\n";
282 PyObject* KX_GameActuator::PyGetFile(PyObject* args, PyObject* kwds)
283 {       
284         ShowDeprecationWarning("getFile()", "the fileName property");
285         return PyString_FromString(m_filename);
286 }
287
288 /* setFile */
289 const char KX_GameActuator::SetFile_doc[] =
290 "setFile(name)\n"
291 "set the name of the file to start.\n";
292 PyObject* KX_GameActuator::PySetFile(PyObject* args, PyObject* kwds)
293 {
294         char* new_file;
295
296         ShowDeprecationWarning("setFile()", "the fileName property");
297         
298         if (!PyArg_ParseTuple(args, "s:setFile", &new_file))
299         {
300                 return NULL;
301         }
302         
303         m_filename = STR_String(new_file);
304
305         Py_RETURN_NONE;
306
307 }
308 // <-----