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_call_omap.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 "cmd/unit_generic.h"
39 #include "xml_support.h"
40 
41 #include "vegastrike.h"
42 
43 #include "mission.h"
44 #include "easydom.h"
45 
46 
47 varInst* Mission::call_omap( missionNode *node, int mode )
48 {
49  varInst *viret = NULL;
50  if (mode == SCRIPT_PARSE) {
51  string cmd = node->attr_value( "name" );
52  node->script.method_id = module_omap_map[cmd];
53  }
55  if (method_id == CMT_OMAP_new) {
56  viret = call_omap_new( node, mode );
57 
58  return viret;
59  } else {
60  varInst *ovi = getObjectArg( node, mode );
61  omap_t *my_object = getOMapObject( node, mode, ovi );
62  if (method_id == CMT_OMAP_delete) {
63  if (mode == SCRIPT_RUN) {
64  omap_t::iterator iter;
65  for (iter = my_object->begin(); iter != my_object->end(); iter++) {
66  string varname = (*iter).first;
67  varInst *vi = (*iter).second;
68  deleteVarInst( vi, true );
69  }
70  my_object->clear();
71  delete my_object;
72  }
73  viret = newVarInst( VI_TEMP );
74  viret->type = VAR_VOID;
75  } else if (method_id == CMT_OMAP_set) {
76  missionNode *snode = getArgument( node, mode, 2 );
77  varInst *var_vi = checkExpression( snode, mode ); //should be getObjExpr
78 
79  string name = getStringArgument( node, mode, 1 );
80 
81  debug( 3, node, mode, "omap set" );
82  if (mode == SCRIPT_RUN) {
83  varInst *push_vi = newVarInst( VI_IN_OBJECT );
84  push_vi->type = var_vi->type;
85  assignVariable( push_vi, var_vi );
86 
87  (*my_object)[name] = push_vi;
88  }
89  deleteVarInst( var_vi );
90  viret = newVarInst( VI_TEMP );
91  viret->type = VAR_VOID;
92  } else if (method_id == CMT_OMAP_get) {
93  debug( 3, node, mode, "omap.get" );
94 
95  string name = getStringArgument( node, mode, 1 );
96 
97  viret = newVarInst( VI_TEMP );
98  viret->type = VAR_ANY;
99  if (mode == SCRIPT_RUN) {
100  varInst *back_vi = (*my_object)[name];
101  assignVariable( viret, back_vi );
102  if (back_vi->type > 10)
103  assert( 0 );
104  deleteVarInst( back_vi ); //this won't delete it
105  }
106  } else if (method_id == CMT_OMAP_toxml) {
107  if (node->subnodes.size() != 1) {
108  fatalError( node, mode, "olist.toxml needs no arguments" );
109  assert( 0 );
110  }
111  debug( 3, node, mode, "omap.toxml" );
112  if (mode == SCRIPT_RUN) {
113  //call_olist_toxml(node,mode,ovi); DELETE ME FIXME ?
114  }
115  viret = newVarInst( VI_TEMP );
116  viret->type = VAR_VOID;
117  } else if (method_id == CMT_OMAP_size) {
118  if (node->subnodes.size() != 1) {
119  fatalError( node, mode, "olist.size needs one arguments" );
120  assert( 0 );
121  }
122  debug( 3, node, mode, "omap.size" );
123 
124  viret = newVarInst( VI_TEMP );
125  if (mode == SCRIPT_RUN) {
126  int len = my_object->size();
127  viret->int_val = len;
128  }
129  viret->type = VAR_INT;
130  } else {
131  fatalError( node, mode, "unknown command "+node->script.name+" for callback omap" );
132  assert( 0 );
133  }
134  deleteVarInst( ovi );
135  return viret;
136  } //else (objects)
137 
138  return NULL; //never reach
139 }
140 
141 omap_t* Mission::getOMapObject( missionNode *node, int mode, varInst *ovi )
142 {
143  omap_t *my_object = NULL;
144  if (mode == SCRIPT_RUN) {
145  my_object = (omap_t*) ovi->object;
146  if (my_object == NULL) {
147  fatalError( node, mode, "omap: no object" );
148  assert( 0 );
149  }
150  }
151  return my_object;
152 }
153 
154 varInst* Mission::call_omap_new( missionNode *node, int mode )
155 {
156  varInst *viret = newVarInst( VI_TEMP );
157 
158  omap_t *my_object = new omap_t;
159 
160  viret->type = VAR_OBJECT;
161  viret->objectname = "omap";
162 
163  viret->object = (void*) my_object;
164 
165  debug( 3, node, mode, "omap new object: " );
166  printVarInst( 3, viret );
167 
168  return viret;
169 }
170