Debian Codegraph @

engine/game/construct.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name construct.cpp - The constructions. */
11 //
12 // (c) Copyright 1998-2008 by Lutz Sammer and Jimmy Salmon
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "stratagus.h"
40 #include "video.h"
41 #include "map.h"
42 #include "construct.h"
43 #include "script.h"
44
45 /*----------------------------------------------------------------------------
46 -- Variables
47 ----------------------------------------------------------------------------*/
48
49 /**
50 ** Constructions.
51 */
52 static std::vector<CConstruction *> Constructions;
53
54 /*----------------------------------------------------------------------------
55 -- Functions
56 ----------------------------------------------------------------------------*/
57
58 /**
59 ** Initialize the constructions.
60 */
61 void InitConstructions(void)
62 {
63 }
64
65 /**
66 ** Load the graphics for the constructions.
67 */
68 void LoadConstructions(void)
69 {
70 const char *file;
71 std::vector<CConstruction *>::iterator i;
72
73 for (i = Constructions.begin(); i != Constructions.end(); ++i) {
74 if ((*i)->Ident.empty()) {
75 continue;
76 }
77 file = (*i)->File.File.c_str();
78 (*i)->Width = (*i)->File.Width;
79 (*i)->Height = (*i)->File.Height;
80 if (file && *file) {
81 ShowLoadProgress("Construction %s", file);
82 (*i)->Sprite = CPlayerColorGraphic::New(file, (*i)->Width, (*i)->Height);
83 (*i)->Sprite->Load();
84 }
85 file = (*i)->ShadowFile.File.c_str();
86 (*i)->ShadowWidth = (*i)->ShadowFile.Width;
87 (*i)->ShadowHeight = (*i)->ShadowFile.Height;
88 if (file && *file) {
89 ShowLoadProgress("Construction %s", file);
90 (*i)->ShadowSprite = CGraphic::ForceNew(file,
91 (*i)->ShadowWidth, (*i)->ShadowHeight);
92 (*i)->ShadowSprite->Load();
93 (*i)->ShadowSprite->MakeShadow();
94 }
95 }
96 }
97
98 /**
99 ** Cleanup the constructions.
100 */
101 void CleanConstructions(void)
102 {
103 CConstructionFrame *cframe;
104 CConstructionFrame *tmp;
105 std::vector<CConstruction *>::iterator i;
106
107 //
108 // Free the construction table.
109 //
110 for (i = Constructions.begin(); i != Constructions.end(); ++i) {
111 CGraphic::Free((*i)->Sprite);
112 CGraphic::Free((*i)->ShadowSprite);
113 cframe = (*i)->Frames;
114 while (cframe) {
115 tmp = cframe->Next;
116 delete cframe;
117 cframe = tmp;
118 }
119 delete *i;
120 }
121 Constructions.clear();
122 }
123
124 /**
125 ** Get construction by identifier.
126 **
127 ** @param ident Identfier of the construction
128 **
129 ** @return Construction structure pointer
130 */
131 CConstruction *ConstructionByIdent(const std::string &ident)
132 {
133 std::vector<CConstruction *>::iterator i;
134
135 for (i = Constructions.begin(); i != Constructions.end(); ++i) {
136 if (ident == (*i)->Ident) {
137 return *i;
138 }
139 }
140 DebugPrint("Construction `%s' not found.\n" _C_ ident.c_str());
141 return NULL;
142 }
143
144 // ----------------------------------------------------------------------------
145
146 /**
147 ** Parse the construction.
148 **
149 ** @param l Lua state.
150 **
151 ** @note make this more flexible
152 */
153 static int CclDefineConstruction(lua_State *l)
154 {
155 const char *value;
156 std::string str;
157 CConstruction *construction;
158 std::vector<CConstruction *>::iterator i;
159 int subargs;
160 int k;
161
162 LuaCheckArgs(l, 2);
163 LuaCheckTable(l, 2);
164
165 // Slot identifier
166
167 str = LuaToString(l, 1);
168
169 for (i = Constructions.begin(); i != Constructions.end(); ++i) {
170 if ((*i)->Ident == str) {
171 // Redefine
172 construction = *i;
173 break;
174 }
175 }
176 if (i == Constructions.end()) {
177 construction = new CConstruction;
178 Constructions.push_back(construction);
179 }
180 construction->Ident = str;
181
182 //
183 // Parse the arguments, in tagged format.
184 //
185 lua_pushnil(l);
186 while (lua_next(l, 2)) {
187 int files;
188
189 value = LuaToString(l, -2);
190
191 if ((files = !strcmp(value, "Files")) ||
192 !strcmp(value, "ShadowFiles")) {
193 const char *file = NULL;
194 int w = 0;
195 int h = 0;
196
197 LuaCheckTable(l, -1);
198 lua_pushnil(l);
199 while (lua_next(l, -2)) {
200 value = LuaToString(l, -2);
201
202 if (!strcmp(value, "File")) {
203 file = LuaToString(l, -1);
204 } else if (!strcmp(value, "Size")) {
205 LuaCheckTableSize(l, -1, 2);
206 w = LuaToNumber(l, -1, 1);
207 h = LuaToNumber(l, -1, 2);
208 } else {
209 LuaError(l, "Unsupported tag: %s" _C_ value);
210 }
211 lua_pop(l, 1);
212 }
213 if (files) {
214 construction->File.File = file;
215 construction->File.Width = w;
216 construction->File.Height = h;
217 } else {
218 construction->ShadowFile.File = file;
219 construction->ShadowFile.Width = w;
220 construction->ShadowFile.Height = h;
221 }
222 } else if (!strcmp(value, "Constructions")) {
223 subargs = lua_objlen(l, -1);
224 for (k = 0; k < subargs; ++k) {
225 int percent;
226 ConstructionFileType file;
227 int frame;
228 CConstructionFrame **cframe;
229
230 percent = 0;
231 file = ConstructionFileConstruction;
232 frame = 0;
233
234 lua_rawgeti(l, -1, k + 1);
235 LuaCheckTable(l, -1);
236 lua_pushnil(l);
237 while (lua_next(l, -2)) {
238 value = LuaToString(l, -2);
239
240 if (!strcmp(value, "Percent")) {
241 percent = LuaToNumber(l, -1);
242 } else if (!strcmp(value, "File")) {
243 value = LuaToString(l, -1);
244 if (!strcmp(value, "construction")) {
245 file = ConstructionFileConstruction;
246 } else if (!strcmp(value, "main")) {
247 file = ConstructionFileMain;
248 } else {
249 LuaError(l, "Unsupported tag: %s" _C_ value);
250 }
251 } else if (!strcmp(value, "Frame")) {
252 frame = LuaToNumber(l, -1);
253 } else {
254 LuaError(l, "Unsupported tag: %s" _C_ value);
255 }
256 lua_pop(l, 1);
257 }
258 lua_pop(l, 1);
259 cframe = &construction->Frames;
260 while (*cframe) {
261 cframe = &((*cframe)->Next);
262 }
263 (*cframe) = new CConstructionFrame;
264 (*cframe)->Percent = percent;
265 (*cframe)->File = file;
266 (*cframe)->Frame = frame;
267 (*cframe)->Next = NULL;
268 }
269 } else {
270 LuaError(l, "Unsupported tag: %s" _C_ value);
271 }
272 lua_pop(l, 1);
273 }
274
275 return 0;
276 }
277
278 // ----------------------------------------------------------------------------
279
280 /**
281 ** Register CCL features for construction.
282 */
283 void ConstructionCclRegister(void)
284 {
285 lua_register(Lua, "DefineConstruction", CclDefineConstruction);
286 }
287
288 //@}