| 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_spell.cpp - The spell script functions.. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2016 by Joris Dauphin and Crestez Leonard |
| 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 |
-- Includes |
| 32 |
----------------------------------------------------------------------------*/ |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <string.h> |
| 37 |
|
| 38 |
#include "stratagus.h" |
| 39 |
#include "spells.h" |
| 40 |
#include "sound.h" |
| 41 |
#include "script.h" |
| 42 |
#include "missile.h" |
| 43 |
#include "unittype.h" |
| 44 |
#include "upgrade.h" |
| 45 |
|
| 46 |
|
| 47 |
// ************************************************************************** |
| 48 |
// Action parsers for spellAction |
| 49 |
// ************************************************************************** |
| 50 |
|
| 51 |
/** |
| 52 |
** Parse the missile location description for a spell action. |
| 53 |
** |
| 54 |
** @param l Lua state. |
| 55 |
** @param location Pointer to missile location description. |
| 56 |
** |
| 57 |
** @note This is only here to avoid code duplication. You don't have |
| 58 |
** any reason to USE this:) |
| 59 |
*/ |
| 60 |
static void CclSpellMissileLocation(lua_State *l, SpellActionMissileLocation *location) |
| 61 |
{ |
| 62 |
const char *value; |
| 63 |
int args; |
| 64 |
int j; |
| 65 |
|
| 66 |
Assert(location != NULL); |
| 67 |
|
| 68 |
LuaCheckTable(l, -1); |
| 69 |
args = lua_objlen(l, -1); |
| 70 |
j = 0; |
| 71 |
|
| 72 |
for (j = 0; j < args; ++j) { |
| 73 |
value = LuaToString(l, -1, j + 1); |
| 74 |
++j; |
| 75 |
if (!strcmp(value, "base")) { |
| 76 |
value = LuaToString(l, -1, j + 1); |
| 77 |
if (!strcmp(value, "caster")) { |
| 78 |
location->Base = LocBaseCaster; |
| 79 |
} else if (!strcmp(value, "target")) { |
| 80 |
location->Base = LocBaseTarget; |
| 81 |
} else { |
| 82 |
LuaError(l, "Unsupported missile location base flag: %s" _C_ value); |
| 83 |
} |
| 84 |
} else if (!strcmp(value, "add-x")) { |
| 85 |
location->AddX = LuaToNumber(l, -1, j + 1); |
| 86 |
} else if (!strcmp(value, "add-y")) { |
| 87 |
location->AddY = LuaToNumber(l, -1, j + 1); |
| 88 |
} else if (!strcmp(value, "add-rand-x")) { |
| 89 |
location->AddRandX = LuaToNumber(l, -1, j + 1); |
| 90 |
} else if (!strcmp(value, "add-rand-y")) { |
| 91 |
location->AddRandY = LuaToNumber(l, -1, j + 1); |
| 92 |
} else { |
| 93 |
LuaError(l, "Unsupported missile location description flag: %s" _C_ value); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
** Parse the action for spell. |
| 100 |
** |
| 101 |
** @param l Lua state. |
| 102 |
*/ |
| 103 |
static SpellActionType *CclSpellAction(lua_State *l) |
| 104 |
{ |
| 105 |
const char *value; |
| 106 |
int args; |
| 107 |
int j; |
| 108 |
|
| 109 |
LuaCheckTable(l, -1); |
| 110 |
args = lua_objlen(l, -1); |
| 111 |
j = 0; |
| 112 |
|
| 113 |
value = LuaToString(l, -1, j + 1); |
| 114 |
++j; |
| 115 |
|
| 116 |
if (!strcmp(value, "spawn-missile")) { |
| 117 |
SpawnMissile *spellaction = new SpawnMissile; |
| 118 |
for (; j < args; ++j) { |
| 119 |
value = LuaToString(l, -1, j + 1); |
| 120 |
++j; |
| 121 |
if (!strcmp(value, "damage")) { |
| 122 |
spellaction->Damage = LuaToNumber(l, -1, j + 1); |
| 123 |
} else if (!strcmp(value, "delay")) { |
| 124 |
spellaction->Delay = LuaToNumber(l, -1, j + 1); |
| 125 |
} else if (!strcmp(value, "ttl")) { |
| 126 |
spellaction->TTL = LuaToNumber(l, -1, j + 1); |
| 127 |
} else if (!strcmp(value, "start-point")) { |
| 128 |
lua_rawgeti(l, -1, j + 1); |
| 129 |
CclSpellMissileLocation(l, &spellaction->StartPoint); |
| 130 |
lua_pop(l, 1); |
| 131 |
} else if (!strcmp(value, "end-point")) { |
| 132 |
lua_rawgeti(l, -1, j + 1); |
| 133 |
CclSpellMissileLocation(l, &spellaction->EndPoint); |
| 134 |
lua_pop(l, 1); |
| 135 |
} else if (!strcmp(value, "missile")) { |
| 136 |
value = LuaToString(l, -1, j + 1); |
| 137 |
spellaction->Missile = MissileTypeByIdent(value); |
| 138 |
if (spellaction->Missile == NULL) { |
| 139 |
DebugPrint("in spawn-missile : missile %s does not exist\n" _C_ value); |
| 140 |
} |
| 141 |
} else { |
| 142 |
LuaError(l, "Unsupported spawn-missile tag: %s" _C_ value); |
| 143 |
} |
| 144 |
} |
| 145 |
// Now, checking value. |
| 146 |
if (spellaction->Missile == NULL) { |
| 147 |
LuaError(l, "Use a missile for spawn-missile (with missile)"); |
| 148 |
} |
| 149 |
return spellaction; |
| 150 |
} else if (!strcmp(value, "area-adjust-vitals")) { |
| 151 |
AreaAdjustVitals *spellaction = new AreaAdjustVitals; |
| 152 |
for (; j < args; ++j) { |
| 153 |
value = LuaToString(l, -1, j + 1); |
| 154 |
++j; |
| 155 |
if (!strcmp(value, "hit-points")) { |
| 156 |
spellaction->HP = LuaToNumber(l, -1, j + 1); |
| 157 |
} else if (!strcmp(value, "charge-points")) { |
| 158 |
spellaction->Charge = LuaToNumber(l, -1, j + 1); |
| 159 |
} else { |
| 160 |
LuaError(l, "Unsupported area-adjust-vitals tag: %s" _C_ value); |
| 161 |
} |
| 162 |
} |
| 163 |
return spellaction; |
| 164 |
} else if (!strcmp(value, "area-bombardment")) { |
| 165 |
AreaBombardment *spellaction = new AreaBombardment; |
| 166 |
for (; j < args; ++j) { |
| 167 |
value = LuaToString(l, -1, j + 1); |
| 168 |
++j; |
| 169 |
if (!strcmp(value, "fields")) { |
| 170 |
spellaction->Fields = LuaToNumber(l, -1, j + 1); |
| 171 |
} else if (!strcmp(value, "shards")) { |
| 172 |
spellaction->Shards = LuaToNumber(l, -1, j + 1); |
| 173 |
} else if (!strcmp(value, "damage")) { |
| 174 |
spellaction->Damage = LuaToNumber(l, -1, j + 1); |
| 175 |
} else if (!strcmp(value, "start-offset-x")) { |
| 176 |
spellaction->StartOffsetX = LuaToNumber(l, -1, j + 1); |
| 177 |
} else if (!strcmp(value, "start-offset-y")) { |
| 178 |
spellaction->StartOffsetY = LuaToNumber(l, -1, j + 1); |
| 179 |
} else if (!strcmp(value, "missile")) { |
| 180 |
value = LuaToString(l, -1, j + 1); |
| 181 |
spellaction->Missile = MissileTypeByIdent(value); |
| 182 |
if (spellaction->Missile == NULL) { |
| 183 |
DebugPrint("in area-bombardement : missile %s does not exist\n" _C_ value); |
| 184 |
} |
| 185 |
} else { |
| 186 |
LuaError(l, "Unsupported area-bombardment tag: %s" _C_ value); |
| 187 |
} |
| 188 |
} |
| 189 |
// Now, checking value. |
| 190 |
if (spellaction->Missile == NULL) { |
| 191 |
LuaError(l, "Use a missile for area-bombardment (with missile)"); |
| 192 |
} |
| 193 |
return spellaction; |
| 194 |
} else if (!strcmp(value, "demolish")) { |
| 195 |
Demolish *spellaction = new Demolish; |
| 196 |
for (; j < args; ++j) { |
| 197 |
value = LuaToString(l, -1, j + 1); |
| 198 |
++j; |
| 199 |
if (!strcmp(value, "range")) { |
| 200 |
spellaction->Range = LuaToNumber(l, -1, j + 1); |
| 201 |
} else if (!strcmp(value, "damage")) { |
| 202 |
spellaction->Damage = LuaToNumber(l, -1, j + 1); |
| 203 |
} else { |
| 204 |
LuaError(l, "Unsupported demolish tag: %s" _C_ value); |
| 205 |
} |
| 206 |
} |
| 207 |
return spellaction; |
| 208 |
} else if (!strcmp(value, "adjust-variable")) { |
| 209 |
AdjustVariable *spellaction = new AdjustVariable; |
| 210 |
lua_rawgeti(l, -1, j + 1); |
| 211 |
LuaCheckTable(l, -1); |
| 212 |
spellaction->Var = new SpellActionTypeAdjustVariable[UnitTypeVar.NumberVariable]; |
| 213 |
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) { |
| 214 |
int i; |
| 215 |
|
| 216 |
i = GetVariableIndex(LuaToString(l, -2)); |
| 217 |
if (i == -1) { |
| 218 |
LuaError(l, "in adjust-variable : Bad variable index : '%s'" _C_ LuaToString(l, -2)); |
| 219 |
} |
| 220 |
if (lua_isnumber(l, -1)) { |
| 221 |
spellaction->Var[i].Enable = (LuaToNumber(l, -1) != 0); |
| 222 |
spellaction->Var[i].ModifEnable = 1; |
| 223 |
spellaction->Var[i].Value = LuaToNumber(l, -1); |
| 224 |
spellaction->Var[i].ModifValue = 1; |
| 225 |
spellaction->Var[i].Max = LuaToNumber(l, -1); |
| 226 |
spellaction->Var[i].ModifMax = 1; |
| 227 |
} else if (lua_istable(l, -1)) { |
| 228 |
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) { |
| 229 |
const char *key; |
| 230 |
|
| 231 |
key = LuaToString(l, -2); |
| 232 |
if (!strcmp(key, "Enable")) { |
| 233 |
spellaction->Var[i].Enable = LuaToBoolean(l, -1); |
| 234 |
spellaction->Var[i].ModifEnable = 1; |
| 235 |
} else if (!strcmp(key, "Value")) { |
| 236 |
spellaction->Var[i].Value = LuaToNumber(l, -1); |
| 237 |
spellaction->Var[i].ModifValue = 1; |
| 238 |
} else if (!strcmp(key, "Max")) { |
| 239 |
spellaction->Var[i].Max = LuaToNumber(l, -1); |
| 240 |
spellaction->Var[i].ModifMax = 1; |
| 241 |
} else if (!strcmp(key, "Increase")) { |
| 242 |
spellaction->Var[i].Increase = LuaToNumber(l, -1); |
| 243 |
spellaction->Var[i].ModifIncrease = 1; |
| 244 |
} else if (!strcmp(key, "InvertEnable")) { |
| 245 |
spellaction->Var[i].InvertEnable = LuaToBoolean(l, -1); |
| 246 |
} else if (!strcmp(key, "AddValue")) { |
| 247 |
spellaction->Var[i].AddValue = LuaToNumber(l, -1); |
| 248 |
} else if (!strcmp(key, "AddMax")) { |
| 249 |
spellaction->Var[i].AddMax = LuaToNumber(l, -1); |
| 250 |
} else if (!strcmp(key, "AddIncrease")) { |
| 251 |
spellaction->Var[i].AddIncrease = LuaToNumber(l, -1); |
| 252 |
} else if (!strcmp(key, "IncreaseTime")) { |
| 253 |
spellaction->Var[i].IncreaseTime = LuaToNumber(l, -1); |
| 254 |
} else if (!strcmp(key, "TargetIsCaster")) { |
| 255 |
value = LuaToString(l, -1); |
| 256 |
if (!strcmp(value, "caster")) { |
| 257 |
spellaction->Var[i].TargetIsCaster = 1; |
| 258 |
} else if (!strcmp(value, "target")) { |
| 259 |
spellaction->Var[i].TargetIsCaster = 0; |
| 260 |
} else { // Error |
| 261 |
LuaError(l, "key '%s' not valid for TargetIsCaster in adjustvariable" _C_ value); |
| 262 |
} |
| 263 |
} else { // Error |
| 264 |
LuaError(l, "key '%s' not valid for adjustvariable" _C_ key); |
| 265 |
} |
| 266 |
} |
| 267 |
} else { |
| 268 |
LuaError(l, "in adjust-variable : Bad variable value"); |
| 269 |
} |
| 270 |
} |
| 271 |
lua_pop(l, 1); // pop table |
| 272 |
return spellaction; |
| 273 |
} else if (!strcmp(value, "summon")) { |
| 274 |
Summon *spellaction = new Summon; |
| 275 |
for (; j < args; ++j) { |
| 276 |
value = LuaToString(l, -1, j + 1); |
| 277 |
++j; |
| 278 |
if (!strcmp(value, "unit-type")) { |
| 279 |
value = LuaToString(l, -1, j + 1); |
| 280 |
spellaction->UnitType = UnitTypeByIdent(value); |
| 281 |
if (!spellaction->UnitType) { |
| 282 |
spellaction->UnitType = 0; |
| 283 |
DebugPrint("unit type \"%s\" not found for summon spell.\n" _C_ value); |
| 284 |
} |
| 285 |
} else if (!strcmp(value, "time-to-live")) { |
| 286 |
spellaction->TTL = LuaToNumber(l, -1, j + 1); |
| 287 |
} else if (!strcmp(value, "require-corpse")) { |
| 288 |
spellaction->RequireCorpse = 1; |
| 289 |
--j; |
| 290 |
} else { |
| 291 |
LuaError(l, "Unsupported summon tag: %s" _C_ value); |
| 292 |
} |
| 293 |
} |
| 294 |
// Now, checking value. |
| 295 |
if (spellaction->UnitType == NULL) { |
| 296 |
LuaError(l, "Use a unittype for summon (with unit-type)"); |
| 297 |
} |
| 298 |
return spellaction; |
| 299 |
} else if (!strcmp(value, "spawn-portal")) { |
| 300 |
SpawnPortal *spellaction = new SpawnPortal; |
| 301 |
for (; j < args; ++j) { |
| 302 |
value = LuaToString(l, -1, j + 1); |
| 303 |
++j; |
| 304 |
if (!strcmp(value, "portal-type")) { |
| 305 |
value = LuaToString(l, -1, j + 1); |
| 306 |
spellaction->PortalType = UnitTypeByIdent(value); |
| 307 |
if (!spellaction->PortalType) { |
| 308 |
spellaction->PortalType = 0; |
| 309 |
DebugPrint("unit type \"%s\" not found for spawn-portal.\n" _C_ value); |
| 310 |
} |
| 311 |
} else { |
| 312 |
LuaError(l, "Unsupported spawn-portal tag: %s" _C_ value); |
| 313 |
} |
| 314 |
} |
| 315 |
// Now, checking value. |
| 316 |
if (spellaction->PortalType == NULL) { |
| 317 |
LuaError(l, "Use a unittype for spawn-portal (with portal-type)"); |
| 318 |
} |
| 319 |
return spellaction; |
| 320 |
} else if (!strcmp(value, "capture")) { |
| 321 |
Capture *spellaction = new Capture; |
| 322 |
for (; j < args; ++j) { |
| 323 |
value = LuaToString(l, -1, j + 1); |
| 324 |
++j; |
| 325 |
if (!strcmp(value, "sacrifice")) { |
| 326 |
spellaction->SacrificeEnable = 1; |
| 327 |
} else if (!strcmp(value, "damage")) { |
| 328 |
spellaction->Damage = LuaToNumber(l, -1, j + 1); |
| 329 |
} else if (!strcmp(value, "percent")) { |
| 330 |
spellaction->DamagePercent = LuaToNumber(l, -1, j + 1); |
| 331 |
} else { |
| 332 |
LuaError(l, "Unsupported Capture tag: %s" _C_ value); |
| 333 |
} |
| 334 |
} |
| 335 |
return spellaction; |
| 336 |
} else if (!strcmp(value, "polymorph")) { |
| 337 |
Polymorph *spellaction = new Polymorph; |
| 338 |
for (; j < args; ++j) { |
| 339 |
value = LuaToString(l, -1, j + 1); |
| 340 |
++j; |
| 341 |
if (!strcmp(value, "new-form")) { |
| 342 |
value = LuaToString(l, -1, j + 1); |
| 343 |
spellaction->NewForm = UnitTypeByIdent(value); |
| 344 |
if (!spellaction->NewForm) { |
| 345 |
spellaction->NewForm= 0; |
| 346 |
DebugPrint("unit type \"%s\" not found for polymorph spell.\n" _C_ value); |
| 347 |
} |
| 348 |
// FIXME: temp polymorphs? hard to do. |
| 349 |
} else if (!strcmp(value, "player-neutral")) { |
| 350 |
spellaction->PlayerNeutral = 1; |
| 351 |
--j; |
| 352 |
} else { |
| 353 |
LuaError(l, "Unsupported polymorph tag: %s" _C_ value); |
| 354 |
} |
| 355 |
} |
| 356 |
// Now, checking value. |
| 357 |
if (spellaction->NewForm == NULL) { |
| 358 |
LuaError(l, "Use a unittype for polymorph (with new-form)"); |
| 359 |
} |
| 360 |
return spellaction; |
| 361 |
} else if (!strcmp(value, "adjust-vitals")) { |
| 362 |
AdjustVitals *spellaction = new AdjustVitals; |
| 363 |
for (; j < args; ++j) { |
| 364 |
value = LuaToString(l, -1, j + 1); |
| 365 |
++j; |
| 366 |
if (!strcmp(value, "hit-points")) { |
| 367 |
spellaction->HP = LuaToNumber(l, -1, j + 1); |
| 368 |
} else if (!strcmp(value, "charge-points")) { |
| 369 |
spellaction->Charge = LuaToNumber(l, -1, j + 1); |
| 370 |
} else if (!strcmp(value, "max-multi-cast")) { |
| 371 |
spellaction->MaxMultiCast = LuaToNumber(l, -1, j + 1); |
| 372 |
} else { |
| 373 |
LuaError(l, "Unsupported adjust-vitals tag: %s" _C_ value); |
| 374 |
} |
| 375 |
} |
| 376 |
return spellaction; |
| 377 |
} else { |
| 378 |
LuaError(l, "Unsupported action type: %s" _C_ value); |
| 379 |
} |
| 380 |
return NULL; |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
** Get a condition value from a scm object. |
| 385 |
** |
| 386 |
** @param l Lua state. |
| 387 |
** @param value scm value to convert. |
| 388 |
** |
| 389 |
** @return CONDITION_TRUE, CONDITION_FALSE, CONDITION_ONLY or -1 on error. |
| 390 |
** @note This is a helper function to make CclSpellCondition shorter |
| 391 |
** and easier to understand. |
| 392 |
*/ |
| 393 |
char Ccl2Condition(lua_State *l, const char *value) |
| 394 |
{ |
| 395 |
if (!strcmp(value, "true")) { |
| 396 |
return CONDITION_TRUE; |
| 397 |
} else if (!strcmp(value, "false")) { |
| 398 |
return CONDITION_FALSE; |
| 399 |
} else if (!strcmp(value, "only")) { |
| 400 |
return CONDITION_ONLY; |
| 401 |
} else { |
| 402 |
LuaError(l, "Bad condition result: %s" _C_ value); |
| 403 |
return -1; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
** Parse the Condition for spell. |
| 409 |
** |
| 410 |
** @param l Lua state. |
| 411 |
** @param condition pointer to condition to fill with data. |
| 412 |
** |
| 413 |
** @note Conditions must be allocated. All data already in is LOST. |
| 414 |
*/ |
| 415 |
static void CclSpellCondition(lua_State *l, ConditionInfo *condition) |
| 416 |
{ |
| 417 |
const char *value; |
| 418 |
int i; |
| 419 |
int args; |
| 420 |
int j; |
| 421 |
|
| 422 |
// |
| 423 |
// Initializations: |
| 424 |
// |
| 425 |
|
| 426 |
// Flags are defaulted to 0(CONDITION_TRUE) |
| 427 |
condition->Variable = new ConditionInfoVariable[UnitTypeVar.NumberVariable]; |
| 428 |
// Initialize min/max stuff to values with no effect. |
| 429 |
for (i = 0; i < UnitTypeVar.NumberVariable; i++) { |
| 430 |
condition->Variable[i].MinValue = -1; |
| 431 |
condition->Variable[i].MaxValue = -1; |
| 432 |
condition->Variable[i].MinMax = -1; |
| 433 |
condition->Variable[i].MinValuePercent = -8; |
| 434 |
condition->Variable[i].MaxValuePercent = 1024; |
| 435 |
} |
| 436 |
// Now parse the list and set values. |
| 437 |
LuaCheckTable(l, -1); |
| 438 |
args = lua_objlen(l, -1); |
| 439 |
for (j = 0; j < args; ++j) { |
| 440 |
value = LuaToString(l, -1, j + 1); |
| 441 |
++j; |
| 442 |
if (!strcmp(value, "alliance")) { |
| 443 |
condition->Alliance = Ccl2Condition(l, LuaToString(l, -1, j + 1)); |
| 444 |
} else if (!strcmp(value, "opponent")) { |
| 445 |
condition->Opponent = Ccl2Condition(l, LuaToString(l, -1, j + 1)); |
| 446 |
} else if (!strcmp(value, "self")) { |
| 447 |
condition->TargetSelf = Ccl2Condition(l, LuaToString(l, -1, j + 1)); |
| 448 |
} else if (!strcmp(value, "Building")) { |
| 449 |
condition->Building = Ccl2Condition(l, LuaToString(l, -1, j + 1)); |
| 450 |
} else if (!strcmp(value, "Organic")) { |
| 451 |
condition->Organic = Ccl2Condition(l, LuaToString(l, -1, j + 1)); |
| 452 |
} else { |
| 453 |
i = GetVariableIndex(value); |
| 454 |
if (i != -1) { // Valid index. |
| 455 |
lua_rawgeti(l, -1, j + 1); |
| 456 |
LuaCheckTable(l, -1); |
| 457 |
for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) { |
| 458 |
const char *key; |
| 459 |
|
| 460 |
key = LuaToString(l, -2); |
| 461 |
if (!strcmp(key, "Enable")) { |
| 462 |
condition->Variable[i].Enable = Ccl2Condition(l, LuaToString(l, -1)); |
| 463 |
} else if (!strcmp(key, "MinValue")) { |
| 464 |
condition->Variable[i].MinValue = LuaToNumber(l, -1); |
| 465 |
} else if (!strcmp(key, "MaxValue")) { |
| 466 |
condition->Variable[i].MaxValue = LuaToNumber(l, -1); |
| 467 |
} else if (!strcmp(key, "MinMax")) { |
| 468 |
condition->Variable[i].MinMax = LuaToNumber(l, -1); |
| 469 |
} else if (!strcmp(key, "MinValuePercent")) { |
| 470 |
condition->Variable[i].MinValuePercent = LuaToNumber(l, -1); |
| 471 |
} else if (!strcmp(key, "MaxValuePercent")) { |
| 472 |
condition->Variable[i].MaxValuePercent = LuaToNumber(l, -1); |
| 473 |
} else if (!strcmp(key, "ConditionApplyOnCaster")) { |
| 474 |
condition->Variable[i].ConditionApplyOnCaster = LuaToBoolean(l, -1); |
| 475 |
} else { // Error |
| 476 |
LuaError(l, "%s invalid for Variable in condition" _C_ key); |
| 477 |
} |
| 478 |
} |
| 479 |
lua_pop(l, 1); // lua_rawgeti() |
| 480 |
continue; |
| 481 |
} |
| 482 |
LuaError(l, "Unsupported condition tag: %s" _C_ value); |
| 483 |
} |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
** Parse the Condition for spell. |
| 489 |
** |
| 490 |
** @param l Lua state. |
| 491 |
** @param autocast pointer to autocast to fill with data. |
| 492 |
** |
| 493 |
** @note: autocast must be allocated. All data already in is LOST. |
| 494 |
*/ |
| 495 |
static void CclSpellAutocast(lua_State *l, AutoCastInfo *autocast) |
| 496 |
{ |
| 497 |
const char *value; |
| 498 |
int args; |
| 499 |
int j; |
| 500 |
|
| 501 |
LuaCheckTable(l, -1); |
| 502 |
args = lua_objlen(l, -1); |
| 503 |
for (j = 0; j < args; ++j) { |
| 504 |
value = LuaToString(l, -1, j + 1); |
| 505 |
++j; |
| 506 |
if (!strcmp(value, "range")) { |
| 507 |
autocast->Range = LuaToNumber(l, -1, j + 1); |
| 508 |
} else if (!strcmp(value, "combat")) { |
| 509 |
autocast->Combat = Ccl2Condition(l, LuaToString(l, -1, j + 1)); |
| 510 |
} else if (!strcmp(value, "condition")) { |
| 511 |
if (!autocast->Condition) { |
| 512 |
autocast->Condition = new ConditionInfo; |
| 513 |
} |
| 514 |
lua_rawgeti(l, -1, j + 1); |
| 515 |
CclSpellCondition(l, autocast->Condition); |
| 516 |
lua_pop(l, 1); |
| 517 |
} else { |
| 518 |
LuaError(l, "Unsupported autocast tag: %s" _C_ value); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
** Parse Spell. |
| 525 |
** |
| 526 |
** @param l Lua state. |
| 527 |
*/ |
| 528 |
static int CclDefineSpell(lua_State *l) |
| 529 |
{ |
| 530 |
std::string identname; |
| 531 |
SpellType *spell; |
| 532 |
const char *value; |
| 533 |
int args; |
| 534 |
|
| 535 |
args = lua_gettop(l); |
| 536 |
identname = LuaToString(l, 1); |
| 537 |
spell = SpellTypeByIdent(identname); |
| 538 |
if (spell != NULL) { |
| 539 |
DebugPrint("Redefining spell-type `%s'\n" _C_ identname.c_str()); |
| 540 |
} else { |
| 541 |
spell = new SpellType(SpellTypeTable.size(), identname); |
| 542 |
for (size_t i = 0; i < UnitTypes.size(); ++i) { // adjust array for caster already defined |
| 543 |
if (UnitTypes[i]->CanCastSpell) { |
| 544 |
char *newc = new char[(SpellTypeTable.size() + 1) * sizeof(char)]; |
| 545 |
memcpy(newc, UnitTypes[i]->CanCastSpell, SpellTypeTable.size() * sizeof(char)); |
| 546 |
delete[] UnitTypes[i]->CanCastSpell; |
| 547 |
UnitTypes[i]->CanCastSpell = newc; |
| 548 |
UnitTypes[i]->CanCastSpell[SpellTypeTable.size()] = 0; |
| 549 |
} |
| 550 |
if (UnitTypes[i]->AutoCastActive) { |
| 551 |
char *newc = new char[(SpellTypeTable.size() + 1) * sizeof(char)]; |
| 552 |
memcpy(newc, UnitTypes[i]->AutoCastActive, SpellTypeTable.size() * sizeof(char)); |
| 553 |
delete[] UnitTypes[i]->AutoCastActive; |
| 554 |
UnitTypes[i]->AutoCastActive = newc; |
| 555 |
UnitTypes[i]->AutoCastActive[SpellTypeTable.size()] = 0; |
| 556 |
} |
| 557 |
} |
| 558 |
SpellTypeTable.push_back(spell); |
| 559 |
} |
| 560 |
for (int i = 1; i < args; ++i) { |
| 561 |
value = LuaToString(l, i + 1); |
| 562 |
++i; |
| 563 |
if (!strcmp(value, "showname")) { |
| 564 |
spell->Name = LuaToString(l, i + 1); |
| 565 |
} else if (!strcmp(value, "chargecost")) { |
| 566 |
spell->ChargeCost = LuaToNumber(l, i + 1); |
| 567 |
} else if (!strcmp(value, "range")) { |
| 568 |
if (!lua_isstring(l, i + 1) && !lua_isnumber(l, i + 1)) { |
| 569 |
LuaError(l, "incorrect argument"); |
| 570 |
} |
| 571 |
if (lua_isstring(l, i + 1) && !strcmp(lua_tostring(l, i + 1), "infinite")) { |
| 572 |
spell->Range = INFINITE_RANGE; |
| 573 |
} else if (lua_isnumber(l, i + 1)) { |
| 574 |
spell->Range = static_cast<int>(lua_tonumber(l, i + 1)); |
| 575 |
} else { |
| 576 |
LuaError(l, "Invalid range"); |
| 577 |
} |
| 578 |
} else if (!strcmp(value, "repeat-cast")) { |
| 579 |
spell->RepeatCast = 1; |
| 580 |
--i; |
| 581 |
} else if (!strcmp(value, "target")) { |
| 582 |
value = LuaToString(l, i + 1); |
| 583 |
if (!strcmp(value, "self")) { |
| 584 |
spell->Target = TargetSelf; |
| 585 |
} else if (!strcmp(value, "unit")) { |
| 586 |
spell->Target = TargetUnit; |
| 587 |
} else if (!strcmp(value, "position")) { |
| 588 |
spell->Target = TargetPosition; |
| 589 |
} else { |
| 590 |
LuaError(l, "Unsupported spell target type tag: %s" _C_ value); |
| 591 |
} |
| 592 |
} else if (!strcmp(value, "action")) { |
| 593 |
int subargs; |
| 594 |
int k; |
| 595 |
|
| 596 |
LuaCheckTable(l, i + 1); |
| 597 |
subargs = lua_objlen(l, i + 1); |
| 598 |
for (k = 0; k < subargs; ++k) { |
| 599 |
lua_rawgeti(l, i + 1, k + 1); |
| 600 |
spell->Action.push_back(CclSpellAction(l)); |
| 601 |
lua_pop(l, 1); |
| 602 |
} |
| 603 |
} else if (!strcmp(value, "condition")) { |
| 604 |
if (!spell->Condition) { |
| 605 |
spell->Condition = new ConditionInfo; |
| 606 |
} |
| 607 |
lua_pushvalue(l, i + 1); |
| 608 |
CclSpellCondition(l, spell->Condition); |
| 609 |
lua_pop(l, 1); |
| 610 |
} else if (!strcmp(value, "autocast")) { |
| 611 |
if (!spell->AutoCast) { |
| 612 |
spell->AutoCast = new AutoCastInfo(); |
| 613 |
} |
| 614 |
lua_pushvalue(l, i + 1); |
| 615 |
CclSpellAutocast(l, spell->AutoCast); |
| 616 |
lua_pop(l, 1); |
| 617 |
} else if (!strcmp(value, "ai-cast")) { |
| 618 |
if (!spell->AICast) { |
| 619 |
spell->AICast = new AutoCastInfo(); |
| 620 |
} |
| 621 |
lua_pushvalue(l, i + 1); |
| 622 |
CclSpellAutocast(l, spell->AICast); |
| 623 |
lua_pop(l, 1); |
| 624 |
} else if (!strcmp(value, "sound-when-cast")) { |
| 625 |
// Free the old name, get the new one |
| 626 |
spell->SoundWhenCast.Name = LuaToString(l, i + 1); |
| 627 |
spell->SoundWhenCast.Sound = SoundForName(spell->SoundWhenCast.Name); |
| 628 |
// Check for sound. |
| 629 |
if (!spell->SoundWhenCast.Sound) { |
| 630 |
spell->SoundWhenCast.Name.clear(); |
| 631 |
} |
| 632 |
} else { |
| 633 |
LuaError(l, "Unsupported tag: %s" _C_ value); |
| 634 |
} |
| 635 |
} |
| 636 |
return 0; |
| 637 |
} |
| 638 |
|
| 639 |
/** |
| 640 |
** Register CCL features for Spell. |
| 641 |
*/ |
| 642 |
void SpellCclRegister(void) |
| 643 |
{ |
| 644 |
lua_register(Lua, "DefineSpell", CclDefineSpell); |
| 645 |
} |
| 646 |
|
| 647 |
//@} |