doxygen: prevent GPL license block from being parsed as doxygen comment.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 <stddef.h>
33
34 #include "SCA_IActuator.h"
35 #include "KX_GameActuator.h"
36 //#include <iostream>
37 #include "KX_Scene.h"
38 #include "KX_KetsjiEngine.h"
39 #include "KX_PythonInit.h" /* for config load/saving */
40
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 /* ------------------------------------------------------------------------- */
45 /* Native functions                                                          */
46 /* ------------------------------------------------------------------------- */
47
48 KX_GameActuator::KX_GameActuator(SCA_IObject *gameobj, 
49                                                                    int mode,
50                                                                    const STR_String& filename,
51                                                                    const STR_String& loadinganimationname,
52                                                                    SCA_IScene* scene,
53                                                                    KX_KetsjiEngine* ketsjiengine)
54                                                                    : SCA_IActuator(gameobj, KX_ACT_GAME)
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 #ifdef WITH_PYTHON
130                         if (m_ketsjiengine)
131                         {
132                                 char mashal_path[512];
133                                 char *marshal_buffer = NULL;
134                                 unsigned int marshal_length;
135                                 FILE *fp = NULL;
136                                 
137                                 pathGamePythonConfig(mashal_path);
138                                 marshal_length = saveGamePythonConfig(&marshal_buffer);
139                                 
140                                 if (marshal_length && marshal_buffer) {
141                                         fp = fopen(mashal_path, "wb");
142                                         if (fp) {
143                                                 if (fwrite(marshal_buffer, 1, marshal_length, fp) != marshal_length) {
144                                                         printf("Warning: could not write marshal data\n");
145                                                 }
146                                                 fclose(fp);
147                                         } else {
148                                                 printf("Warning: could not open marshal file\n");
149                                         }
150                                 } else {
151                                         printf("Warning: could not create marshal buffer\n");
152                                 }
153                                 if (marshal_buffer)
154                                         delete [] marshal_buffer;
155                         }
156                         break;
157 #endif // WITH_PYTHON
158                 }
159         case KX_GAME_LOADCFG:
160                 {
161 #ifdef WITH_PYTHON
162                         if (m_ketsjiengine)
163                         {
164                                 char mashal_path[512];
165                                 char *marshal_buffer;
166                                 int marshal_length;
167                                 FILE *fp = NULL;
168                                 int result;
169                                 
170                                 pathGamePythonConfig(mashal_path);
171                                 
172                                 fp = fopen(mashal_path, "rb");
173                                 if (fp) {
174                                         // obtain file size:
175                                         fseek (fp , 0 , SEEK_END);
176                                         marshal_length = ftell(fp);
177                                         rewind(fp);
178                                         
179                                         marshal_buffer = (char*) malloc (sizeof(char)*marshal_length);
180                                         
181                                         result = fread (marshal_buffer, 1, marshal_length, fp);
182                                         
183                                         if (result == marshal_length) {
184                                                 loadGamePythonConfig(marshal_buffer, marshal_length);
185                                         } else {
186                                                 printf("warning: could not read all of '%s'\n", mashal_path);
187                                         }
188                                         
189                                         free(marshal_buffer);
190                                         fclose(fp);
191                                 } else {
192                                         printf("warning: could not open '%s'\n", mashal_path);
193                                 }
194                         }
195                         break;
196 #endif // WITH_PYTHON
197                 }
198         default:
199                 ; /* do nothing? this is an internal error !!! */
200         }
201         
202         return false;
203 }
204
205
206 #ifdef WITH_PYTHON
207
208 /* ------------------------------------------------------------------------- */
209 /* Python functions                                                          */
210 /* ------------------------------------------------------------------------- */
211
212 /* Integration hooks ------------------------------------------------------- */
213 PyTypeObject KX_GameActuator::Type = {
214         PyVarObject_HEAD_INIT(NULL, 0)
215         "KX_GameActuator",
216         sizeof(PyObjectPlus_Proxy),
217         0,
218         py_base_dealloc,
219         0,
220         0,
221         0,
222         0,
223         py_base_repr,
224         0,0,0,0,0,0,0,0,0,
225         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
226         0,0,0,0,0,0,0,
227         Methods,
228         0,
229         0,
230         &SCA_IActuator::Type,
231         0,0,0,0,0,0,
232         py_base_new
233 };
234
235 PyMethodDef KX_GameActuator::Methods[] =
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 #endif // WITH_PYTHON