46 #include "str.h" |
46 #include "str.h" |
47 #include "objwriter.h" |
47 #include "objwriter.h" |
48 #include "scriptreader.h" |
48 #include "scriptreader.h" |
49 #include "events.h" |
49 #include "events.h" |
50 #include "commands.h" |
50 #include "commands.h" |
|
51 #include "stringtable.h" |
51 |
52 |
52 #define MUST_TOPLEVEL if (g_CurMode != MODE_TOPLEVEL) \ |
53 #define MUST_TOPLEVEL if (g_CurMode != MODE_TOPLEVEL) \ |
53 ParserError ("%ss may only be defined at top level!", token.chars()); |
54 ParserError ("%ss may only be defined at top level!", token.chars()); |
54 |
55 |
55 int g_NumStates = 0; |
56 int g_NumStates = 0; |
96 w->Write (DH_MAINLOOP); |
97 w->Write (DH_MAINLOOP); |
97 w->Write (DH_ENDMAINLOOP); |
98 w->Write (DH_ENDMAINLOOP); |
98 } |
99 } |
99 |
100 |
100 w->Write (DH_STATENAME); |
101 w->Write (DH_STATENAME); |
101 w->Write (statename.len()); |
|
102 w->WriteString (statename); |
102 w->WriteString (statename); |
103 w->Write (DH_STATEIDX); |
103 w->Write (DH_STATEIDX); |
104 w->Write (g_NumStates); |
104 w->Write (g_NumStates); |
105 |
105 |
106 g_NumStates++; |
106 g_NumStates++; |
172 if (!gotMainLoop) { |
172 if (!gotMainLoop) { |
173 w->Write (DH_MAINLOOP); |
173 w->Write (DH_MAINLOOP); |
174 w->Write (DH_ENDMAINLOOP); |
174 w->Write (DH_ENDMAINLOOP); |
175 } |
175 } |
176 |
176 |
177 /* |
177 // If we added strings here, we need to write a list of them. |
178 // State |
178 unsigned int stringcount = g_StringTable->Count(); |
179 w->WriteState ("stateSpawn"); |
179 printf ("Write %u strings\n", stringcount); |
180 |
180 if (stringcount) { |
181 w->Write (DH_ONENTER); |
181 w->Write<long> (DH_STRINGLIST); |
182 w->Write (DH_ENDONENTER); |
182 w->Write<long> (stringcount); |
183 |
183 |
184 w->Write (DH_MAINLOOP); |
184 for (unsigned int a = 0; a < stringcount; a++) { |
185 w->Write (DH_ENDMAINLOOP); |
185 printf ("Write string: `%s`\n", g_StringTable->table[a]); |
186 |
186 w->WriteString (g_StringTable->table[a]); |
187 w->Write (DH_EVENT); |
187 } |
188 w->Write (BOTEVENT_PLAYER_FIREDSSG); |
188 } |
189 w->Write (DH_COMMAND); |
|
190 w->Write (BOTCMD_BEGINJUMPING); |
|
191 w->Write (0); |
|
192 w->Write (DH_ENDEVENT); |
|
193 */ |
|
194 } |
189 } |
195 |
190 |
196 void ScriptReader::ParseCommand (CommandDef* comm, ObjWriter* w) { |
191 void ScriptReader::ParseCommand (CommandDef* comm, ObjWriter* w) { |
197 // If this was defined at top-level, we stop right at square one! |
192 // If this was defined at top-level, we stop right at square one! |
198 if (g_CurMode == MODE_TOPLEVEL) |
193 if (g_CurMode == MODE_TOPLEVEL) |
208 MustNext (")"); |
203 MustNext (")"); |
209 curarg++; |
204 curarg++; |
210 break; |
205 break; |
211 } |
206 } |
212 |
207 |
|
208 /* |
213 if (!Next ()) |
209 if (!Next ()) |
214 ParserError ("unexpected end-of-file, unterminated command"); |
210 ParserError ("unexpected end-of-file, unterminated command"); |
215 |
211 |
216 // If we get a ")" now, the user probably gave too few parameters |
212 // If we get a ")" now, the user probably gave too few parameters |
217 if (!token.compare (")")) |
213 if (!token.compare (")")) |
218 ParserError ("unexpected `)`, did you pass too few parameters? (need %d)", comm->numargs); |
214 ParserError ("unexpected `)`, did you pass too few parameters? (need %d)", comm->numargs); |
219 |
215 */ |
220 // For now, it takes in just numbers. |
216 |
221 // Needs to cater for string arguments too... |
217 switch (comm->argtypes[curarg]) { |
222 if (!token.isnumber()) |
218 case RETURNVAL_INT: |
223 ParserError ("argument %d (`%s`) is not a number", curarg, token.chars()); |
219 MustNumber(); |
224 |
220 w->Write<long> (DH_PUSHNUMBER); |
225 int i = atoi (token.chars ()); |
221 w->Write<long> (atoi (token.chars ())); |
226 w->Write<long> (DH_PUSHNUMBER); |
222 break; |
227 w->Write<long> (i); |
223 case RETURNVAL_BOOLEAN: |
|
224 MustBool(); |
|
225 w->Write<long> (DH_PUSHNUMBER); |
|
226 w->Write<long> (BoolValue ()); |
|
227 break; |
|
228 case RETURNVAL_STRING: |
|
229 MustString(); |
|
230 w->Write<long> (DH_PUSHSTRINGINDEX); |
|
231 w->Write<long> (g_StringTable->Push (token.chars())); |
|
232 break; |
|
233 } |
228 |
234 |
229 if (curarg < comm->numargs - 1) { |
235 if (curarg < comm->numargs - 1) { |
230 MustNext (","); |
236 MustNext (","); |
231 } else if (curarg < comm->maxargs - 1) { |
237 } else if (curarg < comm->maxargs - 1) { |
232 // Can continue, but can terminate as well. |
238 // Can continue, but can terminate as well. |