svn merge -r 22571:22800 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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                                                                    : SCA_IActuator(gameobj)
54 {
55         m_mode = mode;
56         m_filename = filename;
57         m_loadinganimationname = loadinganimationname;
58         m_scene = scene;
59         m_ketsjiengine = ketsjiengine;
60 } /* End of constructor */
61
62
63
64 KX_GameActuator::~KX_GameActuator()
65
66         // there's nothing to be done here, really....
67 } /* end of destructor */
68
69
70
71 CValue* KX_GameActuator::GetReplica()
72 {
73         KX_GameActuator* replica = new KX_GameActuator(*this);
74         replica->ProcessReplica();
75         
76         return replica;
77 }
78
79
80
81 bool KX_GameActuator::Update()
82 {
83         // bool result = false;  /*unused*/
84         bool bNegativeEvent = IsNegativeEvent();
85         RemoveAllEvents();
86
87         if (bNegativeEvent)
88                 return false; // do nothing on negative events
89
90         switch (m_mode)
91         {
92         case KX_GAME_LOAD:
93         case KX_GAME_START:
94                 {
95                         if (m_ketsjiengine)
96                         {
97                                 STR_String exitstring = "start other game";
98                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_START_OTHER_GAME);
99                                 m_ketsjiengine->SetNameNextGame(m_filename);
100                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
101                         }
102
103                         break;
104                 }
105         case KX_GAME_RESTART:
106                 {
107                         if (m_ketsjiengine)
108                         {
109                                 STR_String exitstring = "restarting game";
110                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_RESTART_GAME);
111                                 m_ketsjiengine->SetNameNextGame(m_filename);
112                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
113                         }
114                         break;
115                 }
116         case KX_GAME_QUIT:
117                 {
118                         if (m_ketsjiengine)
119                         {
120                                 STR_String exitstring = "quiting game";
121                                 m_ketsjiengine->RequestExit(KX_EXIT_REQUEST_QUIT_GAME);
122                                 m_scene->AddDebugProperty((this)->GetParent(), exitstring);
123                         }
124                         break;
125                 }
126         case KX_GAME_SAVECFG:
127                 {
128                         if (m_ketsjiengine)
129                         {
130                                 char mashal_path[512];
131                                 char *marshal_buffer = NULL;
132                                 unsigned int marshal_length;
133                                 FILE *fp = NULL;
134                                 
135                                 pathGamePythonConfig(mashal_path);
136                                 marshal_length = saveGamePythonConfig(&marshal_buffer);
137                                 
138                                 if (marshal_length && marshal_buffer) {
139                                         fp = fopen(mashal_path, "wb");
140                                         if (fp) {
141                                                 if (fwrite(marshal_buffer, 1, marshal_length, fp) != marshal_length) {
142                                                         printf("Warning: could not write marshal data\n");
143                                                 }
144                                                 fclose(fp);
145                                         } else {
146                                                 printf("Warning: could not open marshal file\n");
147                                         }
148                                 } else {
149                                         printf("Warning: could not create marshal buffer\n");
150                                 }
151                                 if (marshal_buffer)
152                                         delete [] marshal_buffer;
153                         }
154                         break;
155                 }
156         case KX_GAME_LOADCFG:
157                 {
158                         if (m_ketsjiengine)
159                         {
160                                 char mashal_path[512];
161                                 char *marshal_buffer;
162                                 int marshal_length;
163                                 FILE *fp = NULL;
164                                 int result;
165                                 
166                                 pathGamePythonConfig(mashal_path);
167                                 
168                                 fp = fopen(mashal_path, "rb");
169                                 if (fp) {
170                                         // obtain file size:
171                                         fseek (fp , 0 , SEEK_END);
172                                         marshal_length = ftell(fp);
173                                         rewind(fp);
174                                         
175                                         marshal_buffer = (char*) malloc (sizeof(char)*marshal_length);
176                                         
177                                         result = fread (marshal_buffer, 1, marshal_length, fp);
178                                         
179                                         if (result == marshal_length) {
180                                                 loadGamePythonConfig(marshal_buffer, marshal_length);
181                                         } else {
182                                                 printf("warning: could not read all of '%s'\n", mashal_path);
183                                         }
184                                         
185                                         free(marshal_buffer);
186                                         fclose(fp);
187                                 } else {
188                                         printf("warning: could not open '%s'\n", mashal_path);
189                                 }
190                         }
191                         break;
192                 }
193         default:
194                 ; /* do nothing? this is an internal error !!! */
195         }
196         
197         return false;
198 }
199
200
201
202
203
204 /* ------------------------------------------------------------------------- */
205 /* Python functions                                                          */
206 /* ------------------------------------------------------------------------- */
207
208 /* Integration hooks ------------------------------------------------------- */
209 PyTypeObject KX_GameActuator::Type = {
210         PyVarObject_HEAD_INIT(NULL, 0)
211         "KX_GameActuator",
212         sizeof(PyObjectPlus_Proxy),
213         0,
214         py_base_dealloc,
215         0,
216         0,
217         0,
218         0,
219         py_base_repr,
220         0,0,0,0,0,0,0,0,0,
221         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
222         0,0,0,0,0,0,0,
223         Methods,
224         0,
225         0,
226         &SCA_IActuator::Type,
227         0,0,0,0,0,0,
228         py_base_new
229 };
230
231 PyMethodDef KX_GameActuator::Methods[] =
232 {
233         // Deprecated ----->
234         {"getFile",     (PyCFunction) KX_GameActuator::sPyGetFile, METH_VARARGS, (const char *)GetFile_doc},
235         {"setFile", (PyCFunction) KX_GameActuator::sPySetFile, METH_VARARGS, (const char *)SetFile_doc},
236         // <-----
237         {NULL,NULL} //Sentinel
238 };
239
240 PyAttributeDef KX_GameActuator::Attributes[] = {
241         KX_PYATTRIBUTE_STRING_RW("fileName",0,100,false,KX_GameActuator,m_filename),
242         KX_PYATTRIBUTE_INT_RW("mode", KX_GAME_NODEF+1, KX_GAME_MAX-1, true, KX_GameActuator, m_mode),
243         { NULL }        //Sentinel
244 };
245
246 // Deprecated ----->
247 /* getFile */
248 const char KX_GameActuator::GetFile_doc[] = 
249 "getFile()\n"
250 "get the name of the file to start.\n";
251 PyObject* KX_GameActuator::PyGetFile(PyObject* args, PyObject* kwds)
252 {       
253         ShowDeprecationWarning("getFile()", "the fileName property");
254         return PyUnicode_FromString(m_filename);
255 }
256
257 /* setFile */
258 const char KX_GameActuator::SetFile_doc[] =
259 "setFile(name)\n"
260 "set the name of the file to start.\n";
261 PyObject* KX_GameActuator::PySetFile(PyObject* args, PyObject* kwds)
262 {
263         char* new_file;
264
265         ShowDeprecationWarning("setFile()", "the fileName property");
266         
267         if (!PyArg_ParseTuple(args, "s:setFile", &new_file))
268         {
269                 return NULL;
270         }
271         
272         m_filename = STR_String(new_file);
273
274         Py_RETURN_NONE;
275
276 }
277 // <-----