Sculpt:
[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 #include <stdio.h>
40 #include <stdlib.h>
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 /* ------------------------------------------------------------------------- */
47 /* Native functions                                                          */
48 /* ------------------------------------------------------------------------- */
49
50 KX_GameActuator::KX_GameActuator(SCA_IObject *gameobj, 
51                                                                    int mode,
52                                                                    const STR_String& filename,
53                                                                    const STR_String& loadinganimationname,
54                                                                    SCA_IScene* scene,
55                                                                    KX_KetsjiEngine* ketsjiengine)
56                                                                    : SCA_IActuator(gameobj, KX_ACT_GAME)
57 {
58         m_mode = mode;
59         m_filename = filename;
60         m_loadinganimationname = loadinganimationname;
61         m_scene = scene;
62         m_ketsjiengine = ketsjiengine;
63 } /* End of constructor */
64
65
66
67 KX_GameActuator::~KX_GameActuator()
68
69         // there's nothing to be done here, really....
70 } /* end of destructor */
71
72
73
74 CValue* KX_GameActuator::GetReplica()
75 {
76         KX_GameActuator* replica = new KX_GameActuator(*this);
77         replica->ProcessReplica();
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 #ifndef DISABLE_PYTHON
132                         if (m_ketsjiengine)
133                         {
134                                 char mashal_path[512];
135                                 char *marshal_buffer = NULL;
136                                 unsigned int marshal_length;
137                                 FILE *fp = NULL;
138                                 
139                                 pathGamePythonConfig(mashal_path);
140                                 marshal_length = saveGamePythonConfig(&marshal_buffer);
141                                 
142                                 if (marshal_length && marshal_buffer) {
143                                         fp = fopen(mashal_path, "wb");
144                                         if (fp) {
145                                                 if (fwrite(marshal_buffer, 1, marshal_length, fp) != marshal_length) {
146                                                         printf("Warning: could not write marshal data\n");
147                                                 }
148                                                 fclose(fp);
149                                         } else {
150                                                 printf("Warning: could not open marshal file\n");
151                                         }
152                                 } else {
153                                         printf("Warning: could not create marshal buffer\n");
154                                 }
155                                 if (marshal_buffer)
156                                         delete [] marshal_buffer;
157                         }
158                         break;
159 #endif // DISABLE_PYTHON
160                 }
161         case KX_GAME_LOADCFG:
162                 {
163 #ifndef DISABLE_PYTHON
164                         if (m_ketsjiengine)
165                         {
166                                 char mashal_path[512];
167                                 char *marshal_buffer;
168                                 int marshal_length;
169                                 FILE *fp = NULL;
170                                 int result;
171                                 
172                                 pathGamePythonConfig(mashal_path);
173                                 
174                                 fp = fopen(mashal_path, "rb");
175                                 if (fp) {
176                                         // obtain file size:
177                                         fseek (fp , 0 , SEEK_END);
178                                         marshal_length = ftell(fp);
179                                         rewind(fp);
180                                         
181                                         marshal_buffer = (char*) malloc (sizeof(char)*marshal_length);
182                                         
183                                         result = fread (marshal_buffer, 1, marshal_length, fp);
184                                         
185                                         if (result == marshal_length) {
186                                                 loadGamePythonConfig(marshal_buffer, marshal_length);
187                                         } else {
188                                                 printf("warning: could not read all of '%s'\n", mashal_path);
189                                         }
190                                         
191                                         free(marshal_buffer);
192                                         fclose(fp);
193                                 } else {
194                                         printf("warning: could not open '%s'\n", mashal_path);
195                                 }
196                         }
197                         break;
198 #endif // DISABLE_PYTHON
199                 }
200         default:
201                 ; /* do nothing? this is an internal error !!! */
202         }
203         
204         return false;
205 }
206
207
208 #ifndef DISABLE_PYTHON
209
210 /* ------------------------------------------------------------------------- */
211 /* Python functions                                                          */
212 /* ------------------------------------------------------------------------- */
213
214 /* Integration hooks ------------------------------------------------------- */
215 PyTypeObject KX_GameActuator::Type = {
216         PyVarObject_HEAD_INIT(NULL, 0)
217         "KX_GameActuator",
218         sizeof(PyObjectPlus_Proxy),
219         0,
220         py_base_dealloc,
221         0,
222         0,
223         0,
224         0,
225         py_base_repr,
226         0,0,0,0,0,0,0,0,0,
227         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
228         0,0,0,0,0,0,0,
229         Methods,
230         0,
231         0,
232         &SCA_IActuator::Type,
233         0,0,0,0,0,0,
234         py_base_new
235 };
236
237 PyMethodDef KX_GameActuator::Methods[] =
238 {
239         {NULL,NULL} //Sentinel
240 };
241
242 PyAttributeDef KX_GameActuator::Attributes[] = {
243         KX_PYATTRIBUTE_STRING_RW("fileName",0,100,false,KX_GameActuator,m_filename),
244         KX_PYATTRIBUTE_INT_RW("mode", KX_GAME_NODEF+1, KX_GAME_MAX-1, true, KX_GameActuator, m_mode),
245         { NULL }        //Sentinel
246 };
247
248 #endif // DISABLE_PYTHON