Debian Codegraph @

engine/game/script_missile.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 script_missile.cpp - The missile-type ccl functions. */
11 //
12 // (c) Copyright 2002-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 "missile.h"
42 #include "script.h"
43 #include "unit.h"
44 #include "unit_manager.h"
45 #include "particle.h"
46 #include "luacallback.h"
47
48 /*----------------------------------------------------------------------------
49 -- Variables
50 ----------------------------------------------------------------------------*/
51
52 /**
53 ** Missile class names, used to load/save the missiles.
54 */
55 static const char *MissileClassNames[] = {
56 "missile-class-none",
57 "missile-class-point-to-point",
58 "missile-class-point-to-point-with-hit",
59 "missile-class-point-to-point-cycle-once",
60 "missile-class-point-to-point-bounce",
61 "missile-class-stay",
62 "missile-class-cycle-once",
63 "missile-class-fire",
64 "missile-class-hit",
65 "missile-class-parabolic",
66 NULL
67 };
68
69 /*----------------------------------------------------------------------------
70 -- Functions
71 ----------------------------------------------------------------------------*/
72
73 /**
74 ** Parse missile-type.
75 **
76 ** @param l Lua state.
77 */
78 static int CclDefineMissileType(lua_State *l)
79 {
80 const char *value;
81 const char *str;
82 MissileType *mtype;
83 unsigned i;
84 std::string file;
85
86 LuaCheckArgs(l, 2);
87 LuaCheckTable(l, 2);
88
89 // Slot identifier
90
91 str = LuaToString(l, 1);
92 mtype = MissileTypeByIdent(str);
93
94 if (mtype) {
95 DebugPrint("Redefining missile-type `%s'\n" _C_ str);
96 } else {
97 mtype = NewMissileTypeSlot(str);
98 }
99
100 mtype->NumDirections = 1;
101 mtype->Flip = true;
102 // Ensure we don't divide by zero.
103 mtype->SplashFactor = 100;
104
105 //
106 // Parse the arguments
107 //
108 for (lua_pushnil(l); lua_next(l, 2); lua_pop(l, 1)) {
109 value = LuaToString(l, -2);
110 if (!strcmp(value, "File")) {
111 file = LuaToString(l, -1);
112 } else if (!strcmp(value, "Size")) {
113 LuaCheckTableSize(l, -1, 2);
114 mtype->Width = LuaToNumber(l, -1, 1);
115 mtype->Height = LuaToNumber(l, -1, 2);
116 } else if (!strcmp(value, "Frames")) {
117 mtype->SpriteFrames = LuaToNumber(l, -1);
118 } else if (!strcmp(value, "Flip")) {
119 mtype->Flip = LuaToBoolean(l, -1);
120 } else if (!strcmp(value, "NumDirections")) {
121 mtype->NumDirections = LuaToNumber(l, -1);
122 } else if (!strcmp(value, "transparency")) {
123 mtype->Transparency = LuaToNumber(l, -1);
124 } else if (!strcmp(value, "FiredSound")) {
125 mtype->FiredSound.Name = LuaToString(l, -1);
126 } else if (!strcmp(value, "ImpactSound")) {
127 mtype->ImpactSound.Name = LuaToString(l, -1);
128 } else if (!strcmp(value, "Class")) {
129 value = LuaToString(l, -1);
130 for (i = 0; MissileClassNames[i]; ++i) {
131 if (!strcmp(value, MissileClassNames[i])) {
132 mtype->Class = i;
133 break;
134 }
135 }
136 if (!MissileClassNames[i]) {
137 LuaError(l, "Unsupported class: %s" _C_ value);
138 }
139 } else if (!strcmp(value, "NumBounces")) {
140 mtype->NumBounces = LuaToNumber(l, -1);
141 } else if (!strcmp(value, "Delay")) {
142 mtype->StartDelay = LuaToNumber(l, -1);
143 } else if (!strcmp(value, "Sleep")) {
144 mtype->Sleep = LuaToNumber(l, -1);
145 } else if (!strcmp(value, "Speed")) {
146 mtype->Speed = LuaToNumber(l, -1);
147 } else if (!strcmp(value, "DrawLevel")) {
148 mtype->DrawLevel = LuaToNumber(l, -1);
149 } else if (!strcmp(value, "Range")) {
150 mtype->Range = LuaToNumber(l, -1);
151 } else if (!strcmp(value, "ImpactMissile")) {
152 mtype->ImpactName = LuaToString(l, -1);
153 } else if (!strcmp(value, "SmokeMissile")) {
154 mtype->SmokeName = LuaToString(l, -1);
155 } else if (!strcmp(value, "ImpactParticle")) {
156 mtype->ImpactParticle = new LuaCallback(l, -1);
157 } else if (!strcmp(value, "CanHitOwner")) {
158 mtype->CanHitOwner = LuaToBoolean(l, -1);
159 } else if (!strcmp(value, "FriendlyFire")) {
160 mtype->FriendlyFire = LuaToBoolean(l, -1);
161 } else if (!strcmp(value, "SplashFactor")) {
162 mtype->SplashFactor = LuaToNumber(l, -1);
163 } else {
164 LuaError(l, "Unsupported tag: %s" _C_ value);
165 }
166 }
167
168 if (!file.empty()) {
169 mtype->G = CGraphic::New(file, mtype->Width, mtype->Height);
170 }
171
172 return 0;
173 }
174
175 /**
176 ** Create a missile.
177 **
178 ** @param l Lua state.
179 */
180 static int CclMissile(lua_State *l)
181 {
182 const char *value;
183 MissileType *type;
184 int x;
185 int y;
186 int dx;
187 int dy;
188 int sx;
189 int sy;
190 Missile *missile;
191 int args;
192 int j;
193
194 missile = NULL;
195 type = NULL;
196 x = dx = y = dy = sx = sy = -1;
197
198 args = lua_gettop(l);
199 for (j = 0; j < args; ++j) {
200 value = LuaToString(l, j + 1);
201 ++j;
202
203 if (!strcmp(value, "type")) {
204 type = MissileTypeByIdent(LuaToString(l, j + 1));
205 } else if (!strcmp(value, "pos")) {
206 LuaCheckTableSize(l, j + 1, 2);
207 x = LuaToNumber(l, j + 1, 1);
208 y = LuaToNumber(l, j + 1, 2);
209 } else if (!strcmp(value, "origin-pos")) {
210 LuaCheckTableSize(l, j + 1, 2);
211 sx = LuaToNumber(l, j + 1, 1);
212 sy = LuaToNumber(l, j + 1, 2);
213 } else if (!strcmp(value, "goal")) {
214 LuaCheckTableSize(l, j + 1, 2);
215 dx = LuaToNumber(l, j + 1, 1);
216 dy = LuaToNumber(l, j + 1, 2);
217 } else if (!strcmp(value, "local")) {
218 Assert(type);
219 missile = MakeLocalMissile(type, x, y, dx, dy);
220 missile->Local = 1;
221 --j;
222 } else if (!strcmp(value, "global")) {
223 Assert(type);
224 missile = MakeMissile(type, x, y, dx, dy);
225 missile->X = x;
226 missile->Y = y;
227 missile->SourceX = sx;
228 missile->SourceY = sy;
229 missile->DX = dx;
230 missile->DY = dy;
231 missile->Local = 0;
232 --j;
233 } else if (!strcmp(value, "frame")) {
234 Assert(missile);
235 missile->SpriteFrame = LuaToNumber(l, j + 1);
236 } else if (!strcmp(value, "state")) {
237 Assert(missile);
238 missile->State = LuaToNumber(l, j + 1);
239 } else if (!strcmp(value, "anim-wait")) {
240 Assert(missile);
241 missile->AnimWait = LuaToNumber(l, j + 1);
242 } else if (!strcmp(value, "wait")) {
243 Assert(missile);
244 missile->Wait = LuaToNumber(l, j + 1);
245 } else if (!strcmp(value, "delay")) {
246 Assert(missile);
247 missile->Delay = LuaToNumber(l, j + 1);
248 } else if (!strcmp(value, "source")) {
249 Assert(missile);
250 value = LuaToString(l, j + 1);
251 missile->SourceUnit = UnitSlots[strtol(value + 1, 0, 16)];
252 missile->SourceUnit->RefsIncrease();
253 } else if (!strcmp(value, "target")) {
254 Assert(missile);
255 value = LuaToString(l, j + 1);
256 missile->TargetUnit = UnitSlots[strtol(value + 1, 0, 16)];
257 missile->TargetUnit->RefsIncrease();
258 } else if (!strcmp(value, "damage")) {
259 Assert(missile);
260 missile->Damage = LuaToNumber(l, j + 1);
261 } else if (!strcmp(value, "ttl")) {
262 Assert(missile);
263 missile->TTL = LuaToNumber(l, j + 1);
264 } else if (!strcmp(value, "hidden")) {
265 Assert(missile);
266 missile->Hidden = 1;
267 --j;
268 } else if (!strcmp(value, "step")) {
269 Assert(missile);
270 LuaCheckTableSize(l, j + 1, 2);
271 missile->CurrentStep = LuaToNumber(l, j + 1, 1);
272 missile->TotalStep = LuaToNumber(l, j + 1, 2);
273 } else {
274 LuaError(l, "Unsupported tag: %s" _C_ value);
275 }
276 }
277
278 // we need to reinitialize position parameters - that's because of
279 // the way InitMissile() (called from MakeLocalMissile()) computes
280 // them - it works for creating a missile during a game but breaks
281 // loading the missile from a file.
282 missile->X = x;
283 missile->Y = y;
284 missile->SourceX = sx;
285 missile->SourceY = sy;
286 missile->DX = dx;
287 missile->DY = dy;
288 return 0;
289 }
290
291 /**
292 ** Define burning building missiles.
293 **
294 ** @param l Lua state.
295 */
296 static int CclDefineBurningBuilding(lua_State *l)
297 {
298 for (std::vector<BurningBuildingFrame *>::iterator i = BurningBuildingFrames.begin();
299 i != BurningBuildingFrames.end(); ++i) {
300 delete *i;
301 }
302 BurningBuildingFrames.clear();
303
304 int args = lua_gettop(l);
305 for (int j = 0; j < args; ++j) {
306 LuaCheckTable(l, j + 1);
307
308 const char *value;
309 BurningBuildingFrame *ptr = new BurningBuildingFrame;
310 int subargs = lua_objlen(l, j + 1);
311
312 for (int k = 0; k < subargs; ++k) {
313 value = LuaToString(l, j + 1, k + 1);
314 ++k;
315
316 if (!strcmp(value, "percent")) {
317 ptr->Percent = LuaToNumber(l, j + 1, k + 1);
318 } else if (!strcmp(value, "missile")) {
319 ptr->Missile = MissileTypeByIdent(LuaToString(l, j + 1, k + 1));
320 }
321 }
322 BurningBuildingFrames.insert(BurningBuildingFrames.begin(), ptr);
323 }
324 return 0;
325 }
326
327 /**
328 ** Register CCL features for missile-type.
329 */
330 void MissileCclRegister(void)
331 {
332 lua_register(Lua, "DefineMissileType", CclDefineMissileType);
333 lua_register(Lua, "Missile", CclMissile);
334 lua_register(Lua, "DefineBurningBuilding", CclDefineBurningBuilding);
335 }
336
337 //@}