| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name trigger.cpp - The trigger handling. */ |
| 11 |
// |
| 12 |
// (c) Copyright 2002-2007 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 |
#include <setjmp.h> |
| 39 |
|
| 40 |
#include "stratagus.h" |
| 41 |
#include "script.h" |
| 42 |
#include "unittype.h" |
| 43 |
#include "unit_cache.h" |
| 44 |
#include "player.h" |
| 45 |
#include "trigger.h" |
| 46 |
#include "results.h" |
| 47 |
#include "interface.h" |
| 48 |
#include "unit.h" |
| 49 |
#include "iolib.h" |
| 50 |
|
| 51 |
|
| 52 |
/*---------------------------------------------------------------------------- |
| 53 |
-- Variables |
| 54 |
----------------------------------------------------------------------------*/ |
| 55 |
|
| 56 |
CTimer GameTimer; /// The game timer |
| 57 |
|
| 58 |
/* Index of the trigger in the _trigger_ table where TriggersEachCycle will |
| 59 |
start at the next cycle. */ |
| 60 |
static int nextTrigger; |
| 61 |
|
| 62 |
/* Trigger activation flags loaded from a saved game. Only triggers defined |
| 63 |
by AddTrigger which have the true flag in ActiveTriggers will be |
| 64 |
effectively added. */ |
| 65 |
static bool *ActiveTriggers; |
| 66 |
|
| 67 |
/// Some data accessible for script during the game. |
| 68 |
TriggerDataType TriggerData; |
| 69 |
|
| 70 |
|
| 71 |
/*---------------------------------------------------------------------------- |
| 72 |
-- Functions |
| 73 |
----------------------------------------------------------------------------*/ |
| 74 |
|
| 75 |
/** |
| 76 |
** Get player number. |
| 77 |
** |
| 78 |
** @param l Lua state. |
| 79 |
** |
| 80 |
** @return The player number, -1 matches any. |
| 81 |
*/ |
| 82 |
int TriggerGetPlayer(lua_State *l) |
| 83 |
{ |
| 84 |
const char *player; |
| 85 |
int ret; |
| 86 |
|
| 87 |
if (lua_isnumber(l, -1)) { |
| 88 |
ret = LuaToNumber(l, -1); |
| 89 |
if (ret < 0 || ret > PlayerMax) { |
| 90 |
LuaError(l, "bad player: %d" _C_ ret); |
| 91 |
} |
| 92 |
return ret; |
| 93 |
} |
| 94 |
player = LuaToString(l, -1); |
| 95 |
if (!strcmp(player, "any")) { |
| 96 |
return -1; |
| 97 |
} else if (!strcmp(player, "this")) { |
| 98 |
return ThisPlayer->Index; |
| 99 |
} |
| 100 |
LuaError(l, "bad player: %s" _C_ player); |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
** Get the unit-type. |
| 107 |
** |
| 108 |
** @param l Lua state. |
| 109 |
** |
| 110 |
** @return The unit-type pointer. |
| 111 |
*/ |
| 112 |
const CUnitType *TriggerGetUnitType(lua_State *l) |
| 113 |
{ |
| 114 |
const char *unit; |
| 115 |
|
| 116 |
unit = LuaToString(l, -1); |
| 117 |
if (!strcmp(unit, "any")) { |
| 118 |
return ANY_UNIT; |
| 119 |
} else if (!strcmp(unit, "all")) { |
| 120 |
return ALL_UNITS; |
| 121 |
} else if (!strcmp(unit, "units")) { |
| 122 |
return ALL_FOODUNITS; |
| 123 |
} else if (!strcmp(unit, "buildings")) { |
| 124 |
return ALL_BUILDINGS; |
| 125 |
} |
| 126 |
|
| 127 |
return CclGetUnitType(l); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
** Check whether a unit is of the specified type or in the specified |
| 132 |
** category. |
| 133 |
** |
| 134 |
** @param unit Unit to examine; must not be NULL. |
| 135 |
** @param triggerUnitType As returned by TriggerGetUnitType. |
| 136 |
** |
| 137 |
** @return true if the unit matches the type or category, else false. |
| 138 |
*/ |
| 139 |
bool TriggerMatchUnitType(const CUnit *unit, const CUnitType *triggerUnitType) |
| 140 |
{ |
| 141 |
if (triggerUnitType == ANY_UNIT) { |
| 142 |
return true; |
| 143 |
} else if (triggerUnitType == ALL_UNITS) { |
| 144 |
// FIXME: ALL_UNITS should be "sum of units and buildings", |
| 145 |
// but that currently means just ANY_UNIT. |
| 146 |
return true; |
| 147 |
} else if (triggerUnitType == ALL_FOODUNITS) { |
| 148 |
return !unit->Type->Building; |
| 149 |
} else if (triggerUnitType == ALL_BUILDINGS) { |
| 150 |
return unit->Type->Building; |
| 151 |
} else { |
| 152 |
return unit->Type == triggerUnitType; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/*-------------------------------------------------------------------------- |
| 157 |
-- Conditions |
| 158 |
--------------------------------------------------------------------------*/ |
| 159 |
|
| 160 |
static int CompareEq(int a, int b) |
| 161 |
{ |
| 162 |
return a == b; |
| 163 |
} |
| 164 |
static int CompareNEq(int a, int b) |
| 165 |
{ |
| 166 |
return a != b; |
| 167 |
} |
| 168 |
static int CompareGrEq(int a, int b) |
| 169 |
{ |
| 170 |
return a >= b; |
| 171 |
} |
| 172 |
static int CompareGr(int a, int b) |
| 173 |
{ |
| 174 |
return a > b; |
| 175 |
} |
| 176 |
static int CompareLeEq(int a, int b) |
| 177 |
{ |
| 178 |
return a <= b; |
| 179 |
} |
| 180 |
static int CompareLe(int a, int b) |
| 181 |
{ |
| 182 |
return a < b; |
| 183 |
} |
| 184 |
|
| 185 |
typedef int (*CompareFunction)(int, int); |
| 186 |
|
| 187 |
/** |
| 188 |
** Returns a function pointer to the comparison function |
| 189 |
** |
| 190 |
** @param op The operation |
| 191 |
** |
| 192 |
** @return Function pointer to the compare function |
| 193 |
*/ |
| 194 |
static CompareFunction GetCompareFunction(const char *op) |
| 195 |
{ |
| 196 |
if (op[0] == '=') { |
| 197 |
if ((op[1] == '=' && op[2] == '\0') || (op[1] == '\0')) { |
| 198 |
return &CompareEq; |
| 199 |
} |
| 200 |
} else if (op[0] == '>') { |
| 201 |
if (op[1] == '=' && op[2] == '\0') { |
| 202 |
return &CompareGrEq; |
| 203 |
} else if (op[1] == '\0') { |
| 204 |
return &CompareGr; |
| 205 |
} |
| 206 |
} else if (op[0] == '<') { |
| 207 |
if (op[1] == '=' && op[2] == '\0') { |
| 208 |
return &CompareLeEq; |
| 209 |
} else if (op[1] == '\0') { |
| 210 |
return &CompareLe; |
| 211 |
} |
| 212 |
} else if (op[0] == '!' && op[1] == '=' && op[2] == '\0') { |
| 213 |
return &CompareNEq; |
| 214 |
} |
| 215 |
return NULL; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
** Return the number of units of a giver unit-type and player at a location. |
| 220 |
*/ |
| 221 |
static int CclGetNumUnitsAt(lua_State *l) |
| 222 |
{ |
| 223 |
int plynr; |
| 224 |
int x1; |
| 225 |
int y1; |
| 226 |
int x2; |
| 227 |
int y2; |
| 228 |
const CUnitType *unittype; |
| 229 |
CUnit *table[UnitMax]; |
| 230 |
CUnit *unit; |
| 231 |
int an; |
| 232 |
int j; |
| 233 |
int s; |
| 234 |
|
| 235 |
LuaCheckArgs(l, 4); |
| 236 |
|
| 237 |
plynr = LuaToNumber(l, 1); |
| 238 |
lua_pushvalue(l, 2); |
| 239 |
unittype = TriggerGetUnitType(l); |
| 240 |
lua_pop(l, 1); |
| 241 |
LuaCheckTableSize(l, 3, 2); |
| 242 |
x1 = LuaToNumber(l, 3, 1); |
| 243 |
y1 = LuaToNumber(l, 3, 2); |
| 244 |
LuaCheckTableSize(l, 4, 2); |
| 245 |
x2 = LuaToNumber(l, 4, 1); |
| 246 |
y2 = LuaToNumber(l, 4, 2); |
| 247 |
|
| 248 |
// |
| 249 |
// Get all unit types in location. |
| 250 |
// |
| 251 |
an = UnitCache.Select(x1, y1, x2 + 1, y2 + 1, table, UnitMax); |
| 252 |
// |
| 253 |
// Count the requested units |
| 254 |
// |
| 255 |
for (j = s = 0; j < an; ++j) { |
| 256 |
unit = table[j]; |
| 257 |
// |
| 258 |
// Check unit type |
| 259 |
// |
| 260 |
if (TriggerMatchUnitType(unit, unittype) && !unit->Constructed) { |
| 261 |
// |
| 262 |
// Check the player |
| 263 |
// |
| 264 |
if (plynr == -1 || plynr == unit->Player->Index) { |
| 265 |
++s; |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
lua_pushnumber(l, s); |
| 270 |
return 1; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
** Player has the quantity of unit-type near to unit-type. |
| 275 |
*/ |
| 276 |
static int CclIfNearUnit(lua_State *l) |
| 277 |
{ |
| 278 |
int plynr; |
| 279 |
int q; |
| 280 |
int n; |
| 281 |
int i; |
| 282 |
const CUnitType *unittype; |
| 283 |
const CUnitType *ut2; |
| 284 |
const char *op; |
| 285 |
CUnit *table[UnitMax]; |
| 286 |
CompareFunction compare; |
| 287 |
|
| 288 |
LuaCheckArgs(l, 5); |
| 289 |
|
| 290 |
lua_pushvalue(l, 1); |
| 291 |
plynr = TriggerGetPlayer(l); |
| 292 |
lua_pop(l, 1); |
| 293 |
op = LuaToString(l, 2); |
| 294 |
q = LuaToNumber(l, 3); |
| 295 |
lua_pushvalue(l, 4); |
| 296 |
unittype = TriggerGetUnitType(l); |
| 297 |
lua_pop(l, 1); |
| 298 |
ut2 = CclGetUnitType(l); |
| 299 |
|
| 300 |
compare = GetCompareFunction(op); |
| 301 |
if (!compare) { |
| 302 |
LuaError(l, "Illegal comparison operation in if-near-unit: %s" _C_ op); |
| 303 |
} |
| 304 |
|
| 305 |
// |
| 306 |
// Get all unit types 'near'. |
| 307 |
// |
| 308 |
n = FindUnitsByType(ut2, table, UnitMax); |
| 309 |
for (i = 0; i < n; ++i) { |
| 310 |
CUnit *unit; |
| 311 |
CUnit *around[UnitMax]; |
| 312 |
int an; |
| 313 |
int j; |
| 314 |
int s; |
| 315 |
|
| 316 |
unit = table[i]; |
| 317 |
|
| 318 |
if (unit->Type->UnitType == UnitTypeLand) { |
| 319 |
an = UnitCache.Select(unit->X - 1, unit->Y - 1, |
| 320 |
unit->X + unit->Type->TileWidth + 1, |
| 321 |
unit->Y + unit->Type->TileHeight + 1, around, UnitMax); |
| 322 |
} else { |
| 323 |
an = UnitCache.Select(unit->X - 2, unit->Y - 2, |
| 324 |
unit->X + unit->Type->TileWidth + 2, |
| 325 |
unit->Y + unit->Type->TileHeight + 2, around, UnitMax); |
| 326 |
} |
| 327 |
// |
| 328 |
// Count the requested units |
| 329 |
// |
| 330 |
for (j = s = 0; j < an; ++j) { |
| 331 |
unit = around[j]; |
| 332 |
// |
| 333 |
// Check unit type |
| 334 |
// |
| 335 |
if (TriggerMatchUnitType(unit, unittype)) { |
| 336 |
// |
| 337 |
// Check the player |
| 338 |
// |
| 339 |
if (plynr == -1 || plynr == unit->Player->Index) { |
| 340 |
++s; |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
// Check if we counted the unit near itself |
| 345 |
if (unittype == ANY_UNIT || |
| 346 |
(unittype == ALL_FOODUNITS && ut2->Building) || |
| 347 |
(unittype == ALL_BUILDINGS && ut2->Building)) { |
| 348 |
--s; |
| 349 |
} |
| 350 |
if (compare(s, q)) { |
| 351 |
lua_pushboolean(l, 1); |
| 352 |
return 1; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
lua_pushboolean(l, 0); |
| 357 |
return 1; |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
** Player has the quantity of rescued unit-type near to unit-type. |
| 362 |
*/ |
| 363 |
static int CclIfRescuedNearUnit(lua_State *l) |
| 364 |
{ |
| 365 |
int plynr; |
| 366 |
int q; |
| 367 |
int n; |
| 368 |
int i; |
| 369 |
const CUnitType *unittype; |
| 370 |
const CUnitType *ut2; |
| 371 |
const char *op; |
| 372 |
CUnit *table[UnitMax]; |
| 373 |
CompareFunction compare; |
| 374 |
|
| 375 |
LuaCheckArgs(l, 5); |
| 376 |
|
| 377 |
lua_pushvalue(l, 1); |
| 378 |
plynr = TriggerGetPlayer(l); |
| 379 |
lua_pop(l, 1); |
| 380 |
op = LuaToString(l, 2); |
| 381 |
q = LuaToNumber(l, 3); |
| 382 |
lua_pushvalue(l, 4); |
| 383 |
unittype = TriggerGetUnitType(l); |
| 384 |
lua_pop(l, 1); |
| 385 |
ut2 = CclGetUnitType(l); |
| 386 |
|
| 387 |
compare = GetCompareFunction(op); |
| 388 |
if (!compare) { |
| 389 |
LuaError(l, "Illegal comparison operation in if-rescued-near-unit: %s" _C_ op); |
| 390 |
} |
| 391 |
|
| 392 |
// |
| 393 |
// Get all unit types 'near'. |
| 394 |
// |
| 395 |
n = FindUnitsByType(ut2, table, UnitMax); |
| 396 |
for (i = 0; i < n; ++i) { |
| 397 |
CUnit *unit; |
| 398 |
CUnit *around[UnitMax]; |
| 399 |
int an; |
| 400 |
int j; |
| 401 |
int s; |
| 402 |
|
| 403 |
unit = table[i]; |
| 404 |
|
| 405 |
if (unit->Type->UnitType == UnitTypeLand) { |
| 406 |
an = UnitCache.Select(unit->X - 1, unit->Y - 1, |
| 407 |
unit->X + unit->Type->TileWidth + 1, |
| 408 |
unit->Y + unit->Type->TileHeight + 1, around, UnitMax); |
| 409 |
} else { |
| 410 |
an = UnitCache.Select(unit->X - 2, unit->Y - 2, |
| 411 |
unit->X + unit->Type->TileWidth + 2, |
| 412 |
unit->Y + unit->Type->TileHeight + 2, around, UnitMax); |
| 413 |
} |
| 414 |
// |
| 415 |
// Count the requested units |
| 416 |
// |
| 417 |
for (j = s = 0; j < an; ++j) { |
| 418 |
unit = around[j]; |
| 419 |
if (unit->RescuedFrom) { // only rescued units |
| 420 |
// |
| 421 |
// Check unit type |
| 422 |
// |
| 423 |
if (TriggerMatchUnitType(unit, unittype)) { |
| 424 |
// |
| 425 |
// Check the player |
| 426 |
// |
| 427 |
if (plynr == -1 || plynr == unit->Player->Index) { |
| 428 |
++s; |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
// Check if we counted the unit near itself |
| 434 |
if (unittype == ANY_UNIT || |
| 435 |
(unittype == ALL_FOODUNITS && ut2->Building) || |
| 436 |
(unittype == ALL_BUILDINGS && ut2->Building)) { |
| 437 |
--s; |
| 438 |
} |
| 439 |
if (compare(s, q)) { |
| 440 |
lua_pushboolean(l, 1); |
| 441 |
return 1; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
lua_pushboolean(l, 0); |
| 446 |
return 1; |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
** Returns the number of opponents of a given player. |
| 451 |
*/ |
| 452 |
int GetNumOpponents(int player) |
| 453 |
{ |
| 454 |
int n = 0; |
| 455 |
|
| 456 |
// Check the player opponents |
| 457 |
for (int i = 0; i < PlayerMax; ++i) { |
| 458 |
// This player is our enemy and has units left. |
| 459 |
if (((Players[player].Enemy & (1 << i)) || (Players[i].Enemy & (1 << player))) && |
| 460 |
Players[i].TotalNumUnits) { |
| 461 |
++n; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
return n; |
| 466 |
} |
| 467 |
|
| 468 |
/** |
| 469 |
** Check the timer value |
| 470 |
*/ |
| 471 |
int GetTimer() |
| 472 |
{ |
| 473 |
if (!GameTimer.Init) { |
| 474 |
return 0; |
| 475 |
} |
| 476 |
return GameTimer.Cycles; |
| 477 |
} |
| 478 |
|
| 479 |
/*--------------------------------------------------------------------------- |
| 480 |
-- Actions |
| 481 |
---------------------------------------------------------------------------*/ |
| 482 |
|
| 483 |
/** |
| 484 |
** Stop the running game with a given result |
| 485 |
*/ |
| 486 |
void StopGame(GameResults result) |
| 487 |
{ |
| 488 |
GameResult = result; |
| 489 |
GamePaused = true; |
| 490 |
GameRunning = false; |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
** Action condition player wins. |
| 495 |
*/ |
| 496 |
void ActionVictory() |
| 497 |
{ |
| 498 |
StopGame(GameVictory); |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
** Action condition player lose. |
| 503 |
*/ |
| 504 |
void ActionDefeat() |
| 505 |
{ |
| 506 |
StopGame(GameDefeat); |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
** Action condition player draw. |
| 511 |
*/ |
| 512 |
void ActionDraw() |
| 513 |
{ |
| 514 |
StopGame(GameDraw); |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
** Action set timer |
| 519 |
*/ |
| 520 |
void ActionSetTimer(int cycles, bool increasing) |
| 521 |
{ |
| 522 |
GameTimer.Cycles = cycles; |
| 523 |
GameTimer.Increasing = increasing; |
| 524 |
GameTimer.Init = true; |
| 525 |
GameTimer.LastUpdate = GameCycle; |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
** Action start timer |
| 530 |
*/ |
| 531 |
void ActionStartTimer() |
| 532 |
{ |
| 533 |
GameTimer.Running = true; |
| 534 |
GameTimer.Init = true; |
| 535 |
} |
| 536 |
|
| 537 |
/** |
| 538 |
** Action stop timer |
| 539 |
*/ |
| 540 |
void ActionStopTimer() |
| 541 |
{ |
| 542 |
GameTimer.Running = false; |
| 543 |
} |
| 544 |
|
| 545 |
/** |
| 546 |
** Add a trigger. |
| 547 |
*/ |
| 548 |
static int CclAddTrigger(lua_State *l) |
| 549 |
{ |
| 550 |
int i; |
| 551 |
|
| 552 |
LuaCheckArgs(l, 2); |
| 553 |
if (!lua_isfunction(l, 1) || |
| 554 |
(!lua_isfunction(l, 2) && !lua_istable(l, 2))) { |
| 555 |
LuaError(l, "incorrect argument"); |
| 556 |
} |
| 557 |
|
| 558 |
// |
| 559 |
// Make a list of all triggers. |
| 560 |
// A trigger is a pair of condition and action |
| 561 |
// |
| 562 |
lua_pushstring(l, "_triggers_"); |
| 563 |
lua_gettable(l, LUA_GLOBALSINDEX); |
| 564 |
|
| 565 |
if (lua_isnil(l, -1)) { |
| 566 |
DebugPrint("Trigger not set, defining trigger\n"); |
| 567 |
lua_pop(l, 1); |
| 568 |
lua_pushstring(l, "_triggers_"); |
| 569 |
lua_newtable(l); |
| 570 |
lua_settable(l, LUA_GLOBALSINDEX); |
| 571 |
lua_pushstring(l, "_triggers_"); |
| 572 |
lua_gettable(l, LUA_GLOBALSINDEX); |
| 573 |
} |
| 574 |
|
| 575 |
i = lua_objlen(l, -1); |
| 576 |
if (ActiveTriggers && !ActiveTriggers[i / 2]) { |
| 577 |
lua_pushnil(l); |
| 578 |
lua_rawseti(l, -2, i + 1); |
| 579 |
lua_pushnil(l); |
| 580 |
lua_rawseti(l, -2, i + 2); |
| 581 |
} else { |
| 582 |
lua_pushvalue(l, 1); |
| 583 |
lua_rawseti(l, -2, i + 1); |
| 584 |
lua_newtable(l); |
| 585 |
lua_pushvalue(l, 2); |
| 586 |
lua_rawseti(l, -2, 1); |
| 587 |
lua_rawseti(l, -2, i + 2); |
| 588 |
} |
| 589 |
lua_pop(l, 1); |
| 590 |
|
| 591 |
return 0; |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
** Set the trigger values |
| 596 |
*/ |
| 597 |
void SetTrigger(int trigger) |
| 598 |
{ |
| 599 |
nextTrigger = trigger; |
| 600 |
} |
| 601 |
|
| 602 |
/** |
| 603 |
** Set the active triggers |
| 604 |
*/ |
| 605 |
static int CclSetActiveTriggers(lua_State *l) |
| 606 |
{ |
| 607 |
int args; |
| 608 |
|
| 609 |
args = lua_gettop(l); |
| 610 |
ActiveTriggers = new bool[args]; |
| 611 |
for (int j = 0; j < args; ++j) { |
| 612 |
ActiveTriggers[j] = LuaToBoolean(l, j + 1); |
| 613 |
} |
| 614 |
|
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
** Execute a trigger action |
| 620 |
** |
| 621 |
** @param script Script to execute |
| 622 |
** |
| 623 |
** @return 1 if the trigger should be removed |
| 624 |
*/ |
| 625 |
static int TriggerExecuteAction(int script) |
| 626 |
{ |
| 627 |
int ret; |
| 628 |
int args; |
| 629 |
int j; |
| 630 |
int base = lua_gettop(Lua); |
| 631 |
|
| 632 |
ret = 0; |
| 633 |
|
| 634 |
lua_rawgeti(Lua, -1, script + 1); |
| 635 |
args = lua_objlen(Lua, -1); |
| 636 |
for (j = 0; j < args; ++j) { |
| 637 |
lua_rawgeti(Lua, -1, j + 1); |
| 638 |
LuaCall(0, 0); |
| 639 |
if (lua_gettop(Lua) > base + 1 && lua_toboolean(Lua, -1)) { |
| 640 |
ret = 1; |
| 641 |
} else { |
| 642 |
ret = 0; |
| 643 |
} |
| 644 |
lua_settop(Lua, base + 1); |
| 645 |
} |
| 646 |
lua_pop(Lua, 1); |
| 647 |
|
| 648 |
// If action returns false remove it |
| 649 |
return !ret; |
| 650 |
} |
| 651 |
|
| 652 |
/** |
| 653 |
** Remove a trigger |
| 654 |
** |
| 655 |
** @param trig Current trigger |
| 656 |
*/ |
| 657 |
static void TriggerRemoveTrigger(int trig) |
| 658 |
{ |
| 659 |
lua_pushnumber(Lua, -1); |
| 660 |
lua_rawseti(Lua, -2, trig + 1); |
| 661 |
lua_pushnumber(Lua, -1); |
| 662 |
lua_rawseti(Lua, -2, trig + 2); |
| 663 |
} |
| 664 |
|
| 665 |
/** |
| 666 |
** Check trigger each game cycle. |
| 667 |
*/ |
| 668 |
void TriggersEachCycle(void) |
| 669 |
{ |
| 670 |
int triggers; |
| 671 |
int base = lua_gettop(Lua); |
| 672 |
|
| 673 |
lua_pushstring(Lua, "_triggers_"); |
| 674 |
lua_gettable(Lua, LUA_GLOBALSINDEX); |
| 675 |
triggers = lua_objlen(Lua, -1); |
| 676 |
|
| 677 |
if (nextTrigger >= triggers) { |
| 678 |
nextTrigger = 0; |
| 679 |
} |
| 680 |
|
| 681 |
if (GamePaused) { |
| 682 |
lua_pop(Lua, 1); |
| 683 |
return; |
| 684 |
} |
| 685 |
|
| 686 |
// Skip to the next trigger |
| 687 |
while (nextTrigger < triggers) { |
| 688 |
lua_rawgeti(Lua, -1, nextTrigger + 1); |
| 689 |
if (!lua_isnumber(Lua, -1)) { |
| 690 |
break; |
| 691 |
} |
| 692 |
lua_pop(Lua, 1); |
| 693 |
nextTrigger += 2; |
| 694 |
} |
| 695 |
if (nextTrigger < triggers) { |
| 696 |
int currentTrigger = nextTrigger; |
| 697 |
nextTrigger += 2; |
| 698 |
LuaCall(0, 0); |
| 699 |
// If condition is true execute action |
| 700 |
if (lua_gettop(Lua) > base + 1 && lua_toboolean(Lua, -1)) { |
| 701 |
lua_settop(Lua, base + 1); |
| 702 |
if (TriggerExecuteAction(currentTrigger + 1)) { |
| 703 |
TriggerRemoveTrigger(currentTrigger); |
| 704 |
} |
| 705 |
} |
| 706 |
lua_settop(Lua, base + 1); |
| 707 |
} |
| 708 |
lua_pop(Lua, 1); |
| 709 |
} |
| 710 |
|
| 711 |
/** |
| 712 |
** Register CCL features for triggers. |
| 713 |
*/ |
| 714 |
void TriggerCclRegister(void) |
| 715 |
{ |
| 716 |
lua_register(Lua, "AddTrigger", CclAddTrigger); |
| 717 |
lua_register(Lua, "SetActiveTriggers", CclSetActiveTriggers); |
| 718 |
// Conditions |
| 719 |
lua_register(Lua, "GetNumUnitsAt", CclGetNumUnitsAt); |
| 720 |
lua_register(Lua, "IfNearUnit", CclIfNearUnit); |
| 721 |
lua_register(Lua, "IfRescuedNearUnit", CclIfRescuedNearUnit); |
| 722 |
} |
| 723 |
|
| 724 |
/** |
| 725 |
** Save the trigger module. |
| 726 |
** |
| 727 |
** @param file Open file to print to |
| 728 |
*/ |
| 729 |
void SaveTriggers(CFile *file) |
| 730 |
{ |
| 731 |
int i; |
| 732 |
int triggers; |
| 733 |
|
| 734 |
lua_pushstring(Lua, "_triggers_"); |
| 735 |
lua_gettable(Lua, LUA_GLOBALSINDEX); |
| 736 |
triggers = lua_objlen(Lua, -1); |
| 737 |
|
| 738 |
file->printf("SetActiveTriggers("); |
| 739 |
for (i = 0; i < triggers; i += 2) { |
| 740 |
lua_rawgeti(Lua, -1, i + 1); |
| 741 |
if (i) { |
| 742 |
file->printf(", "); |
| 743 |
} |
| 744 |
if (!lua_isnil(Lua, -1)) { |
| 745 |
file->printf("true"); |
| 746 |
} else { |
| 747 |
file->printf("false"); |
| 748 |
} |
| 749 |
lua_pop(Lua, 1); |
| 750 |
} |
| 751 |
file->printf(")\n"); |
| 752 |
|
| 753 |
file->printf("SetTrigger(%d)\n", nextTrigger); |
| 754 |
|
| 755 |
if (GameTimer.Init) { |
| 756 |
file->printf("ActionSetTimer(%ld, %s)\n", |
| 757 |
GameTimer.Cycles, (GameTimer.Increasing ? "true" : "false")); |
| 758 |
if (GameTimer.Running) { |
| 759 |
file->printf("ActionStartTimer()\n"); |
| 760 |
} |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
/** |
| 765 |
** Initialize the trigger module. |
| 766 |
*/ |
| 767 |
void InitTriggers(void) |
| 768 |
{ |
| 769 |
// |
| 770 |
// Setup default triggers |
| 771 |
// |
| 772 |
// FIXME: choose the triggers for game type |
| 773 |
|
| 774 |
lua_pushstring(Lua, "_triggers_"); |
| 775 |
lua_gettable(Lua, LUA_GLOBALSINDEX); |
| 776 |
if (lua_isnil(Lua, -1)) { |
| 777 |
lua_pushstring(Lua, "SinglePlayerTriggers"); |
| 778 |
lua_gettable(Lua, LUA_GLOBALSINDEX); |
| 779 |
LuaCall(0, 1); |
| 780 |
} |
| 781 |
lua_pop(Lua, 1); |
| 782 |
} |
| 783 |
|
| 784 |
/** |
| 785 |
** Clean up the trigger module. |
| 786 |
*/ |
| 787 |
void CleanTriggers(void) |
| 788 |
{ |
| 789 |
lua_pushstring(Lua, "_triggers_"); |
| 790 |
lua_pushnil(Lua); |
| 791 |
lua_settable(Lua, LUA_GLOBALSINDEX); |
| 792 |
|
| 793 |
nextTrigger = 0; |
| 794 |
|
| 795 |
delete[] ActiveTriggers; |
| 796 |
ActiveTriggers = NULL; |
| 797 |
|
| 798 |
GameTimer.Reset(); |
| 799 |
} |
| 800 |
|
| 801 |
//@} |