| |
1 /* |
| |
2 * botc source code |
| |
3 * Copyright (C) 2012 Santeri `Dusk` Piippo |
| |
4 * All rights reserved. |
| |
5 * |
| |
6 * Redistribution and use in source and binary forms, with or without |
| |
7 * modification, are permitted provided that the following conditions are met: |
| |
8 * |
| |
9 * 1. Redistributions of source code must retain the above copyright notice, |
| |
10 * this list of conditions and the following disclaimer. |
| |
11 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| |
12 * this list of conditions and the following disclaimer in the documentation |
| |
13 * and/or other materials provided with the distribution. |
| |
14 * 3. Neither the name of the developer nor the names of its contributors may |
| |
15 * be used to endorse or promote products derived from this software without |
| |
16 * specific prior written permission. |
| |
17 * 4. Redistributions in any form must be accompanied by information on how to |
| |
18 * obtain complete source code for the software and any accompanying |
| |
19 * software that uses the software. The source code must either be included |
| |
20 * in the distribution or be available for no more than the cost of |
| |
21 * distribution plus a nominal fee, and must be freely redistributable |
| |
22 * under reasonable conditions. For an executable file, complete source |
| |
23 * code means the source code for all modules it contains. It does not |
| |
24 * include source code for modules or files that typically accompany the |
| |
25 * major components of the operating system on which the executable file |
| |
26 * runs. |
| |
27 * |
| |
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| |
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| |
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| |
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| |
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| |
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| |
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| |
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| |
38 * POSSIBILITY OF SUCH DAMAGE. |
| |
39 */ |
| |
40 |
| |
41 #define __VARIABLES_CXX__ |
| |
42 #include <stdio.h> |
| |
43 #include <stdlib.h> |
| |
44 #include <string.h> |
| |
45 #include "common.h" |
| |
46 #include "bots.h" |
| |
47 #include "botcommands.h" |
| |
48 #include "objwriter.h" |
| |
49 #include "stringtable.h" |
| |
50 #include "variables.h" |
| |
51 #include "scriptreader.h" |
| |
52 |
| |
53 ScriptVar* g_GlobalVariables[MAX_SCRIPT_VARIABLES]; |
| |
54 ScriptVar* g_LocalVariable; |
| |
55 |
| |
56 void InitVariables () { |
| |
57 unsigned int u; |
| |
58 ITERATE_GLOBAL_VARS (u) |
| |
59 g_GlobalVariables[u] = NULL; |
| |
60 } |
| |
61 |
| |
62 // Tries to declare a new global-scope variable. Returns pointer |
| |
63 // to new global variable, NULL if declaration failed. |
| |
64 ScriptVar* DeclareGlobalVariable (ScriptReader* r, str name, int type) { |
| |
65 // Apparently the Zandronum engine does not support string variables.. |
| |
66 if (type == RETURNVAL_VOID || type == RETURNVAL_STRING) |
| |
67 return NULL; |
| |
68 |
| |
69 // Find a NULL pointer to a global variable |
| |
70 ScriptVar* g; |
| |
71 unsigned int u = 0; |
| |
72 ITERATE_GLOBAL_VARS (u) { |
| |
73 // Check that it doesn't exist already |
| |
74 if (!g_GlobalVariables[u]) |
| |
75 break; |
| |
76 |
| |
77 if (!g_GlobalVariables[u]->name.icompare (name)) |
| |
78 r->ParserError ("tried to declare global scope variable %s twice", name.chars()); |
| |
79 } |
| |
80 |
| |
81 if (u == MAX_SCRIPT_VARIABLES) |
| |
82 r->ParserError ("too many global variables!"); |
| |
83 |
| |
84 g = new ScriptVar; |
| |
85 g->index = u; |
| |
86 g->name = name; |
| |
87 g->statename = ""; |
| |
88 g->type = type; |
| |
89 g->next = NULL; |
| |
90 g->value = (type == RETURNVAL_BOOLEAN) ? "false" : |
| |
91 (type == RETURNVAL_INT) ? "0" : ""; |
| |
92 |
| |
93 g_GlobalVariables[u] = g; |
| |
94 return g; |
| |
95 } |
| |
96 |
| |
97 /*void AssignScriptVariable (ScriptReader* r, str name, str value) { |
| |
98 ScriptVar* g = FindScriptVariable (name); |
| |
99 if (!g) |
| |
100 r->ParserError ("global variable %s not declared", name.chars()); |
| |
101 }*/ |
| |
102 |
| |
103 // Find a global variable by name |
| |
104 /* ScriptVar* FindScriptVariable (str name) { |
| |
105 ScriptVar* g; |
| |
106 ITERATE_SCRIPT_VARS (g) |
| |
107 if (!g->name.compare (name)) |
| |
108 return g; |
| |
109 |
| |
110 return NULL; |
| |
111 }*/ |
| |
112 |
| |
113 unsigned int CountGlobalVars () { |
| |
114 ScriptVar* var; |
| |
115 unsigned int count = 0; |
| |
116 unsigned int u = 0; |
| |
117 ITERATE_GLOBAL_VARS (u) { |
| |
118 if (g_GlobalVariables[u] != NULL) |
| |
119 count++; |
| |
120 } |
| |
121 return count; |
| |
122 } |