BGE Python API
[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         // this will copy properties and so on...
77         CValue::AddDataToReplica(replica);
78         
79         return replica;
80 }
81
82
83
84 bool KX_GameActuator::Update()
85 {
86         // bool result = false;  /*unused*/
87         bool bNegativeEvent = IsNegativeEvent();
88         RemoveAllEvents();
89
90         if (bNegativeEvent)
91                 return false; // do nothing on negative events
92
93         switch (m_mode)
94         {
95         case KX_GAME_LOAD:
96         case KX_GAME_START:
97                 {
98                         if (m_ketsjiengine)
99                         {
100                                 STR_String exitstring = "start other game";
101                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_START_OTHER_GAME);
102                                 m_ketsjiengine->SetNameNextGame(m_filename);
103                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
104                         }
105
106                         break;
107                 }
108         case KX_GAME_RESTART:
109                 {
110                         if (m_ketsjiengine)
111                         {
112                                 STR_String exitstring = "restarting game";
113                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_RESTART_GAME);
114                                 m_ketsjiengine->SetNameNextGame(m_filename);
115                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
116                         }
117                         break;
118                 }
119         case KX_GAME_QUIT:
120                 {
121                         if (m_ketsjiengine)
122                         {
123                                 STR_String exitstring = "quiting game";
124                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_QUIT_GAME);
125                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
126                         }
127                         break;
128                 }
129         case KX_GAME_SAVECFG:
130                 {
131                         if (m_ketsjiengine)
132                         {
133                                 char mashal_path[512];
134                                 char *marshal_buffer = NULL;
135                                 unsigned int marshal_length;
136                                 FILE *fp = NULL;
137                                 
138                                 pathGamePythonConfig(mashal_path);
139                                 marshal_length = saveGamePythonConfig(&marshal_buffer);
140                                 
141                                 if (marshal_length && marshal_buffer) {
142                                         fp = fopen(mashal_path, "wb");
143                                         if (fp) {
144                                                 if (fwrite(marshal_buffer, 1, marshal_length, fp) != marshal_length) {
145                                                         printf("Warning: could not write marshal data\n");
146                                                 }
147                                                 fclose(fp);
148                                         } else {
149                                                 printf("Warning: could not open marshal file\n");
150                                         }
151                                 } else {
152                                         printf("Warning: could not create marshal buffer\n");
153                                 }
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         PyObject_HEAD_INIT(NULL)
212                 0,
213                 "KX_GameActuator",
214                 sizeof(KX_GameActuator),
215                 0,
216                 PyDestructor,
217                 0,
218                 0,
219                 0,
220                 0,
221                 py_base_repr,
222                 0,0,0,0,0,0,
223                 py_base_getattro,
224                 py_base_setattro,
225                 0,0,0,0,0,0,0,0,0,
226                 Methods
227 };
228
229
230
231 PyParentObject KX_GameActuator::Parents[] =
232 {
233         &KX_GameActuator::Type,
234                 &SCA_IActuator::Type,
235                 &SCA_ILogicBrick::Type,
236                 &CValue::Type,
237                 NULL
238 };
239
240
241
242 PyMethodDef KX_GameActuator::Methods[] =
243 {
244         // Deprecated ----->
245         {"getFile",     (PyCFunction) KX_GameActuator::sPyGetFile, METH_VARARGS, (PY_METHODCHAR)GetFile_doc},
246         {"setFile", (PyCFunction) KX_GameActuator::sPySetFile, METH_VARARGS, (PY_METHODCHAR)SetFile_doc},
247         // <-----
248         {NULL,NULL} //Sentinel
249 };
250
251 PyAttributeDef KX_GameActuator::Attributes[] = {
252         KX_PYATTRIBUTE_STRING_RW("file",0,100,false,KX_GameActuator,m_filename),
253         { NULL }        //Sentinel
254 };
255
256 PyObject*
257 KX_GameActuator::py_getattro(PyObject *attr)
258 {
259         py_getattro_up(SCA_IActuator);
260 }
261
262 int KX_GameActuator::py_setattro(PyObject *attr, PyObject *value)
263 {
264         py_setattro_up(SCA_IActuator);
265 }
266
267
268 // Deprecated ----->
269 /* getFile */
270 const char KX_GameActuator::GetFile_doc[] = 
271 "getFile()\n"
272 "get the name of the file to start.\n";
273 PyObject* KX_GameActuator::PyGetFile(PyObject* self, PyObject* args, PyObject* kwds)
274 {       
275         ShowDeprecationWarning("getFile()", "the file property");
276         return PyString_FromString(m_filename);
277 }
278
279 /* setFile */
280 const char KX_GameActuator::SetFile_doc[] =
281 "setFile(name)\n"
282 "set the name of the file to start.\n";
283 PyObject* KX_GameActuator::PySetFile(PyObject* self, PyObject* args, PyObject* kwds)
284 {
285         char* new_file;
286
287         ShowDeprecationWarning("setFile()", "the file property");
288         
289         if (!PyArg_ParseTuple(args, "s", &new_file))
290         {
291                 return NULL;
292         }
293         
294         m_filename = STR_String(new_file);
295
296         Py_RETURN_NONE;
297
298 }
299 // <-----