Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
script_util.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 /*
23  * xml Mission Scripting written by Alexander Rawass <alexannika@users.sourceforge.net>
24  */
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <time.h>
30 #include <ctype.h>
31 #include <assert.h>
32 #ifndef WIN32
33 //this file isn't available on my system (all win32 machines?) i dun even know what it has or if we need it as I can compile without it
34 #include <unistd.h>
35 #endif
36 
37 #include <expat.h>
38 #include "xml_support.h"
39 
40 #include "vegastrike.h"
41 
42 #include "cmd/unit_generic.h"
43 #include "mission.h"
44 #include "easydom.h"
45 
46 #include "vs_globals.h"
47 #include "config_xml.h"
48 
49 using std::cout;
50 using std::cerr;
51 using std::endl;
52 
53 /* *********************************************************** */
54 
55 string Mission::modestring( int mode )
56 {
57  if (mode == SCRIPT_PARSE) {
58  if (parsemode == PARSE_DECL)
59  return "parsedecl";
60  else
61  return "parsefull";
62  } else {
63  return "run";
64  }
65 }
66 
67 /* *********************************************************** */
68 
69 void Mission::trace( missionNode *node, int mode )
70 {
71  if (!do_trace || mode == SCRIPT_PARSE)
72  return;
73  cout<<"trace: ";
74  missionNode *module_node = runtime.cur_thread->module_stack.back();
75  if (module_node)
76  cout<<module_node->script.name<<":"<<node->attr_value( "line" );
77  cout<<" tag "<<node->Name()<<"name "<<node->attr_value( "name" )<<" module "<<node->attr_value( "module" )<<endl;
78 }
79 
80 /* *********************************************************** */
81 
82 void Mission::fatalError( missionNode *node, int mode, string message )
83 {
84  cout<<"fatal ("<<modestring( mode )<<") "<<message<<" : ";
85  printNode( node, mode );
86  if (node)
87  cout<<"semantic error at line "<<node->attr_value( "line" )<<endl;
88  if (mode == SCRIPT_RUN) {
89  missionNode *module_node = runtime.cur_thread->module_stack.back();
90  if (module_node)
91  cout<<"ERROR: "<<module_node->script.name<<":"<<node->attr_value( "line" )<<endl;
92  cout<<endl<<"Stackdump: "<<endl;
93  for (unsigned int i = 0; i < runtime.cur_thread->exec_stack.size(); i++) {
96  unsigned int classid = runtime.cur_thread->classid_stack[i];
97  for (unsigned int j = 0; j < cstack->contexts.size(); j++) {
98  scriptContext *context = cstack->contexts[j];
99  missionNode *bnode = context->block_node;
100  if (bnode) {
101  cout<<mnode->script.name<<":"<<classid<<" line "<<bnode->attr_value( "line" )<<" "<<bnode->script.name<<" ";
102 
103  printNode( bnode, mode );
104  }
105  }
106  }
107  }
108  cout<<endl;
109 }
110 
111 /* *********************************************************** */
112 
113 void Mission::runtimeFatal( string message )
114 {
115  cout<<"runtime fatalError: "<<message<<endl;
116 }
117 
118 /* *********************************************************** */
119 
120 void Mission::warning( string message )
121 {
122  cout<<"warning: "<<message<<endl;
123 }
124 
125 /* *********************************************************** */
126 
127 void Mission::debug( int level, missionNode *node, int mode, string message )
128 {
129  if (level <= debuglevel)
130  debug( node, mode, message );
131 }
132 
133 /* *********************************************************** */
134 
135 void Mission::debug( missionNode *node, int mode, string message )
136 {
137  cout<<"debug ("<<modestring( mode )<<") "<<message<<" : ";
138  printNode( node, mode );
139 }
140 
141 /* *********************************************************** */
142 
143 void Mission::printNode( missionNode *node, int mode )
144 {
145  if (node)
146  node->printNode( cout, 0, 0 );
147 }
148 
149 /* *********************************************************** */
150 
151 bool Mission::have_return( int mode )
152 {
153  if (mode == SCRIPT_PARSE)
154  return false;
155  contextStack *cstack = runtime.cur_thread->exec_stack.back();
156  if (cstack->return_value == NULL)
157  return false;
158  return true;
159 }
160 
161 /* *********************************************************** */
162 
163 void Mission::initTagMap()
164 {
165  tagmap["module"] = DTAG_MODULE;
166  tagmap["script"] = DTAG_SCRIPT;
167  tagmap["if"] = DTAG_IF;
168  tagmap["block"] = DTAG_BLOCK;
169  tagmap["setvar"] = DTAG_SETVAR;
170  tagmap["exec"] = DTAG_EXEC;
171  tagmap["call"] = DTAG_CALL;
172  tagmap["while"] = DTAG_WHILE;
173  tagmap["and"] = DTAG_AND_EXPR;
174  tagmap["or"] = DTAG_OR_EXPR;
175  tagmap["not"] = DTAG_NOT_EXPR;
176  tagmap["test"] = DTAG_TEST_EXPR;
177  tagmap["fmath"] = DTAG_FMATH;
178  tagmap["vmath"] = DTAG_VMATH;
179  tagmap["var"] = DTAG_VAR_EXPR;
180  tagmap["defvar"] = DTAG_DEFVAR;
181  tagmap["const"] = DTAG_CONST;
182  tagmap["arguments"] = DTAG_ARGUMENTS;
183  tagmap["globals"] = DTAG_GLOBALS;
184  tagmap["return"] = DTAG_RETURN;
185  tagmap["import"] = DTAG_IMPORT;
186 }
187 
188 /* *********************************************************** */
189 
190 void Mission::printVarInst( varInst *vi ) {}
191 
192 /* *********************************************************** */
193 
194 void Mission::printVarInst( int dbg_level, varInst *vi )
195 {
196  if (dbg_level <= debuglevel)
197  saveVarInst( vi, cout );
198 }
199 
200 /* *********************************************************** */
201 
202 void Mission::printVarmap( const varInstMap &vmap )
203 {
204  vsUMap< string, varInst* >::const_iterator iter;
205  for (iter = vmap.begin(); iter != vmap.end(); iter++) {
206  cout<<"variable "<<(*iter).first;
207  varInst *vi = (*iter).second;
208 
209  printVarInst( vi );
210  }
211 }
212 
213 /* *********************************************************** */
214 
215 void Mission::printModules()
216 {
217  vsUMap< string, missionNode* >::iterator iter;
218  for (iter = runtime.modules.begin(); iter != runtime.modules.end(); iter++) {
219  cout<<" module "<<(*iter).first;
220  missionNode *mnode = (*iter).second;
221  printNode( mnode, 0 );
222  cout<<" scripts"<<endl;
223 
224  vsUMap< string, missionNode* >::iterator iter2;
225  for (iter2 = mnode->script.scripts.begin(); iter2 != mnode->script.scripts.end(); iter2++) {
226  cout<<" script "<<(*iter2).first;
227  missionNode *snode = (*iter2).second;
228  printNode( snode, 0 );
229  }
230  }
231 }
232 
233 /* *********************************************************** */
234 
235 void Mission::printRuntime()
236 {
237  return;
238 
239  cout<<"RUNTIME"<<endl;
240  cout<<"MODULES:"<<endl;
241 
242  vsUMap< string, missionNode* >::iterator iter;
243  for (iter = runtime.modules.begin(); iter != runtime.modules.end(); iter++) {
244  cout<<" module "<<(*iter).first;
245  printNode( (*iter).second, 0 );
246  }
247  cout<<"CURRENT THREAD:"<<endl;
248 
249  printThread( runtime.cur_thread );
250 }
251 
252 void Mission::printGlobals( int dbg_level )
253 {
254  if (dbg_level > debuglevel)
255  return;
256  vsUMap< string, missionNode* >::iterator iter;
257  for (iter = runtime.global_variables.begin(); iter != runtime.global_variables.end(); iter++) {
258  cout<<" global var "<<(*iter).first;
259  printNode( (*iter).second, 0 );
260  }
261 }
262 
263 /* *********************************************************** */
264 
265 void Mission::printThread( missionThread *thread )
266 {
267  return;
268 
269  vector< contextStack* >::const_iterator siter;
270  for (siter = thread->exec_stack.begin(); siter != thread->exec_stack.end(); siter++) {
271  contextStack *stack = *siter;
272 
273  vector< scriptContext* >::const_iterator iter2;
274 
275  cout<<"SCRIPT CONTEXTS"<<endl;
276  for (iter2 = stack->contexts.begin(); iter2 != stack->contexts.end(); iter2++) {
277  scriptContext *context = *iter2;
278 
279  cout<<"VARMAP "<<endl;
280  printVarmap( *(context->varinsts) );
281  }
282  }
283 }
284 
285 /* *********************************************************** */
286 
287 varInst* Mission::searchScopestack( string name )
288 {
289  int elem = scope_stack.size()-1;
290  varInst *vi = NULL;
291  while (vi == NULL && elem >= 0) {
292  missionNode *scope = scope_stack[elem];
293 
294  vi = scope->script.variables[name];
295  if (vi == NULL) {
296  if (scope->script.classvars.size() > 0) {
297  varInstMap *cvmap = scope->script.classvars[0];
298  vi = (*cvmap)[name];
299  debug( 10, scope, 0, "found var "+name+" as classvar" );
300  }
301  if (vi == NULL)
302  debug( 5, scope, 0, "variable "+name+" not found in that scope" );
303  } else {
304  debug( 5, scope, 0, "variable "+name+" FOUND in that scope" );
305  }
306  elem--;
307  }
308  return vi;
309 }
310 
311 /* *********************************************************** */
312 
313 missionNode* Mission::lookupScript( string scriptname, string modulename )
314 {
315  missionNode *module = runtime.modules[modulename];
316  if (module == NULL) {
317  fatalError( module, SCRIPT_PARSE, "module "+modulename+" not found - maybe you forgot to import it?" );
318  assert( 0 );
319  }
320  missionNode *scriptnode = module->script.scripts[scriptname];
321  if (scriptnode == NULL) {
322  fatalError( module, SCRIPT_PARSE, "script "+scriptname+" not found in module "+modulename );
323  assert( 0 );
324  }
325  return scriptnode;
326 }
327