| 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_ai.cpp - The AI ccl functions. */ |
| 11 |
// |
| 12 |
// (c) Copyright 2000-2009 by Lutz Sammer, Ludovic Pollet, |
| 13 |
// and Jimmy Salmon |
| 14 |
// |
| 15 |
// This program is free software; you can redistribute it and/or modify |
| 16 |
// it under the terms of the GNU General Public License as published by |
| 17 |
// the Free Software Foundation; only version 2 of the License. |
| 18 |
// |
| 19 |
// This program is distributed in the hope that it will be useful, |
| 20 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
// GNU General Public License for more details. |
| 23 |
// |
| 24 |
// You should have received a copy of the GNU General Public License |
| 25 |
// along with this program; if not, write to the Free Software |
| 26 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 27 |
// 02111-1307, USA. |
| 28 |
// |
| 29 |
|
| 30 |
//@{ |
| 31 |
|
| 32 |
/*---------------------------------------------------------------------------- |
| 33 |
-- Includes |
| 34 |
----------------------------------------------------------------------------*/ |
| 35 |
|
| 36 |
#include <string.h> |
| 37 |
#include <stdio.h> |
| 38 |
#include <stdlib.h> |
| 39 |
|
| 40 |
#include "stratagus.h" |
| 41 |
#include "unit_manager.h" |
| 42 |
#include "unittype.h" |
| 43 |
#include "upgrade.h" |
| 44 |
#include "script.h" |
| 45 |
#include "ai.h" |
| 46 |
#include "pathfinder.h" |
| 47 |
#include "ai_local.h" |
| 48 |
#include "player.h" |
| 49 |
#include "interface.h" |
| 50 |
|
| 51 |
/** |
| 52 |
** Insert new unit-type element. |
| 53 |
** |
| 54 |
** @param table Table with elements. |
| 55 |
** @param n Index to insert new into table |
| 56 |
** @param base Base type to insert into table. |
| 57 |
*/ |
| 58 |
static void AiHelperInsert(std::vector<std::vector<CUnitType *> > &table, |
| 59 |
unsigned int n, CUnitType *base) |
| 60 |
{ |
| 61 |
Assert(base != NULL); |
| 62 |
|
| 63 |
if (n >= table.size()) |
| 64 |
{ |
| 65 |
table.resize(n + 1); |
| 66 |
} |
| 67 |
|
| 68 |
// Look if already known |
| 69 |
std::vector<CUnitType *>::const_iterator i; |
| 70 |
for (i = table[n].begin(); i != table[n].end(); ++i) |
| 71 |
{ |
| 72 |
if (*i == base) |
| 73 |
{ |
| 74 |
return; |
| 75 |
} |
| 76 |
} |
| 77 |
table[n].push_back(base); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
** Transform list of unit separed with coma to a true list. |
| 82 |
*/ |
| 83 |
static std::vector<CUnitType *> getUnitTypeFromString(const std::string& list) |
| 84 |
{ |
| 85 |
std::vector<CUnitType *> res; |
| 86 |
|
| 87 |
if (list == "*") |
| 88 |
{ |
| 89 |
return UnitTypes; |
| 90 |
} |
| 91 |
int begin = 1; |
| 92 |
int end = 0; |
| 93 |
std::string unitName; |
| 94 |
end = list.find(",", begin); |
| 95 |
while (end != -1) |
| 96 |
{ |
| 97 |
unitName = list.substr(begin, end - begin); |
| 98 |
begin = end + 1; |
| 99 |
end = list.find(",", begin); |
| 100 |
if (!unitName.empty()) |
| 101 |
{ |
| 102 |
Assert(unitName[0] != ','); |
| 103 |
res.push_back(UnitTypeByIdent(unitName)); |
| 104 |
} |
| 105 |
} |
| 106 |
return res; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
** Get list of unittype which can be repared. |
| 111 |
*/ |
| 112 |
static std::vector<CUnitType *> getReparableUnits() |
| 113 |
{ |
| 114 |
std::vector<CUnitType *> res; |
| 115 |
|
| 116 |
for (std::vector<CUnitType *>::const_iterator i = UnitTypes.begin(); i != UnitTypes.end(); ++i) |
| 117 |
{ |
| 118 |
CUnitType *type = *i; |
| 119 |
|
| 120 |
if (type->RepairHP > 0) |
| 121 |
{ |
| 122 |
res.push_back(type); |
| 123 |
} |
| 124 |
} |
| 125 |
return res; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
** Init AiHelper. |
| 130 |
** |
| 131 |
** @param aiHelper variable to initialise. |
| 132 |
** |
| 133 |
** @todo missing Equiv initialisation. |
| 134 |
*/ |
| 135 |
static void InitAiHelper(AiHelper &aiHelper) |
| 136 |
{ |
| 137 |
std::vector<CUnitType *> reparableUnits = getReparableUnits(); |
| 138 |
|
| 139 |
for (int i = 0; i < (int)UnitButtonTable.size(); ++i) |
| 140 |
{ |
| 141 |
const ButtonAction &button = *UnitButtonTable[i]; |
| 142 |
const std::vector<CUnitType *> &unitmask = getUnitTypeFromString(button.UnitMask); |
| 143 |
|
| 144 |
switch (button.Action) |
| 145 |
{ |
| 146 |
case ButtonRepair: |
| 147 |
for (std::vector<CUnitType *>::const_iterator j = unitmask.begin(); j != unitmask.end(); ++j) |
| 148 |
{ |
| 149 |
for (std::vector<CUnitType *>::const_iterator k = reparableUnits.begin(); k != reparableUnits.end(); ++k) |
| 150 |
{ |
| 151 |
AiHelperInsert(aiHelper.Repair, (*k)->Slot, *j); |
| 152 |
} |
| 153 |
} |
| 154 |
break; |
| 155 |
case ButtonBuild: |
| 156 |
{ |
| 157 |
CUnitType *buildingType = UnitTypeByIdent(button.ValueStr); |
| 158 |
|
| 159 |
for (std::vector<CUnitType *>::const_iterator j = unitmask.begin(); j != unitmask.end(); ++j) |
| 160 |
{ |
| 161 |
AiHelperInsert(aiHelper.Build, buildingType->Slot, (*j)); |
| 162 |
} |
| 163 |
break; |
| 164 |
} |
| 165 |
case ButtonTrain : |
| 166 |
{ |
| 167 |
CUnitType *trainingType = UnitTypeByIdent(button.ValueStr); |
| 168 |
|
| 169 |
for (std::vector<CUnitType *>::const_iterator j = unitmask.begin(); j != unitmask.end(); ++j) |
| 170 |
{ |
| 171 |
AiHelperInsert(aiHelper.Train, trainingType->Slot, (*j)); |
| 172 |
} |
| 173 |
break; |
| 174 |
} |
| 175 |
default: |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
** Define helper for AI. |
| 183 |
** |
| 184 |
** @param l Lua state. |
| 185 |
** |
| 186 |
** @todo FIXME: the first unit could be a list see ../doc/ccl/ai.html |
| 187 |
*/ |
| 188 |
static int CclDefineAiHelper(lua_State *l) |
| 189 |
{ |
| 190 |
const char *value; |
| 191 |
int what; |
| 192 |
CUnitType *base; |
| 193 |
CUnitType *type = NULL; |
| 194 |
int args; |
| 195 |
int j; |
| 196 |
int subargs; |
| 197 |
int k; |
| 198 |
|
| 199 |
InitAiHelper(AiHelpers); |
| 200 |
|
| 201 |
args = lua_gettop(l); |
| 202 |
for (j = 0; j < args; ++j) |
| 203 |
{ |
| 204 |
LuaCheckTable(l, j + 1); |
| 205 |
subargs = lua_objlen(l, j + 1); |
| 206 |
k = 0; |
| 207 |
value = LuaToString(l, j + 1, k + 1); |
| 208 |
++k; |
| 209 |
|
| 210 |
// |
| 211 |
// Type build,train. |
| 212 |
// |
| 213 |
if (!strcmp(value, "build")) |
| 214 |
{ |
| 215 |
what = -1; |
| 216 |
} |
| 217 |
else if (!strcmp(value, "train")) |
| 218 |
{ |
| 219 |
what = -1; |
| 220 |
} |
| 221 |
else if (!strcmp(value, "unit-limit")) |
| 222 |
{ |
| 223 |
what = -1; |
| 224 |
} |
| 225 |
else if (!strcmp(value, "unit-equiv")) |
| 226 |
{ |
| 227 |
what = 5; |
| 228 |
} |
| 229 |
else if (!strcmp(value, "repair")) |
| 230 |
{ |
| 231 |
what = -1; |
| 232 |
} |
| 233 |
else |
| 234 |
{ |
| 235 |
LuaError(l, "unknown tag: %s" _C_ value); |
| 236 |
what = -1; |
| 237 |
} |
| 238 |
|
| 239 |
if (what == -1) |
| 240 |
{ |
| 241 |
continue; |
| 242 |
} |
| 243 |
|
| 244 |
// |
| 245 |
// Get the base unit type, which could handle the action. |
| 246 |
// |
| 247 |
|
| 248 |
// FIXME: support value as list! |
| 249 |
value = LuaToString(l, j + 1, k + 1); |
| 250 |
++k; |
| 251 |
base = UnitTypeByIdent(value); |
| 252 |
if (!base) |
| 253 |
{ |
| 254 |
LuaError(l, "unknown unittype: %s" _C_ value); |
| 255 |
} |
| 256 |
|
| 257 |
// |
| 258 |
// Get the unit types, which could be produced |
| 259 |
// |
| 260 |
for (; k < subargs; ++k) |
| 261 |
{ |
| 262 |
value = LuaToString(l, j + 1, k + 1); |
| 263 |
type = UnitTypeByIdent(value); |
| 264 |
if (!type) |
| 265 |
{ |
| 266 |
LuaError(l, "unknown unittype: %s" _C_ value); |
| 267 |
} |
| 268 |
AiHelperInsert(AiHelpers.Equiv, base->Slot, type); |
| 269 |
AiNewUnitTypeEquiv(base, type); |
| 270 |
} |
| 271 |
} |
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
** Define an AI type. |
| 277 |
** |
| 278 |
** @param l Lua state. |
| 279 |
** |
| 280 |
** @par Lua parameters; |
| 281 |
** @b ai_type A table that describes the AI type. |
| 282 |
** See the documentation of the AiTypes Lua variable for details. |
| 283 |
*/ |
| 284 |
static int CclDefineAiType(lua_State *l) |
| 285 |
{ |
| 286 |
// Index of the ai_type parameter from Lua. |
| 287 |
const int aitype_index = 1; |
| 288 |
|
| 289 |
CAiType *aitype; |
| 290 |
|
| 291 |
LuaCheckArgs(l, 1); |
| 292 |
luaL_checktype(l, aitype_index, LUA_TTABLE); |
| 293 |
// The following type checks do not use luaL_checktype because |
| 294 |
// it is intended for arguments of functions only and generates |
| 295 |
// error messages accordingly. |
| 296 |
|
| 297 |
lua_getfield(l, aitype_index, "Ident"); |
| 298 |
const int ident_index = lua_gettop(l); |
| 299 |
if (!lua_isstring(l, ident_index)) |
| 300 |
{ |
| 301 |
LuaError(l, "incorrect argument: Ident of ai_type must be a string"); |
| 302 |
} |
| 303 |
size_t ident_length = 0; |
| 304 |
const char *ident_ptr = lua_tolstring(l, ident_index, &ident_length); |
| 305 |
// Leave it on the stack. |
| 306 |
|
| 307 |
lua_getfield(l, aitype_index, "Init"); |
| 308 |
if (!lua_isfunction(l, -1) && !lua_isnil(l, -1)) |
| 309 |
{ |
| 310 |
LuaError(l, "incorrect argument: Init of ai_type must be a function or nil"); |
| 311 |
} |
| 312 |
lua_pop(l, 1); |
| 313 |
|
| 314 |
lua_getfield(l, aitype_index, "EachSecond"); |
| 315 |
if (!lua_isfunction(l, -1)) |
| 316 |
{ |
| 317 |
LuaError(l, "incorrect argument: EachSecond must be a function"); |
| 318 |
} |
| 319 |
lua_pop(l, 1); |
| 320 |
|
| 321 |
aitype = new CAiType; |
| 322 |
AiTypes.push_back(aitype); |
| 323 |
|
| 324 |
// |
| 325 |
// AI Name |
| 326 |
// |
| 327 |
aitype->Name.assign(ident_ptr, ident_ptr + ident_length); |
| 328 |
|
| 329 |
#ifdef DEBUG |
| 330 |
for (size_t i = 0; i < AiTypes.size() - 1; ++i) |
| 331 |
{ |
| 332 |
const CAiType *ait = AiTypes[i]; |
| 333 |
if (aitype->Name == ait->Name) |
| 334 |
{ |
| 335 |
DebugPrint("Warning two or more AI's with the same name '%s'\n" _C_ ait->Name.c_str()); |
| 336 |
} |
| 337 |
} |
| 338 |
#endif |
| 339 |
|
| 340 |
// |
| 341 |
// Add to the AiTypes Lua variable. |
| 342 |
// |
| 343 |
lua_getfield(l, LUA_GLOBALSINDEX, "AiTypes"); |
| 344 |
lua_pushvalue(l, ident_index); |
| 345 |
lua_pushvalue(l, aitype_index); |
| 346 |
lua_settable(l, -3); |
| 347 |
|
| 348 |
// Lua will discard the values we left on the stack. |
| 349 |
return 0; |
| 350 |
} |
| 351 |
|
| 352 |
/*---------------------------------------------------------------------------- |
| 353 |
-- AI script functions |
| 354 |
----------------------------------------------------------------------------*/ |
| 355 |
|
| 356 |
/** |
| 357 |
** Append unit-type to request table. |
| 358 |
** |
| 359 |
** @param type Unit-type to be appended. |
| 360 |
** @param count How many unit-types to build. |
| 361 |
*/ |
| 362 |
static void InsertUnitTypeRequests(CUnitType *type, int count) |
| 363 |
{ |
| 364 |
AiRequestType ait; |
| 365 |
|
| 366 |
ait.Type = type; |
| 367 |
ait.Count = count; |
| 368 |
|
| 369 |
AiPlayer->UnitTypeRequests.push_back(ait); |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
** Find unit-type in request table. |
| 374 |
** |
| 375 |
** @param type Unit-type to be found. |
| 376 |
*/ |
| 377 |
static AiRequestType *FindInUnitTypeRequests(const CUnitType *type) |
| 378 |
{ |
| 379 |
for (size_t i = 0; i < AiPlayer->UnitTypeRequests.size(); ++i) |
| 380 |
{ |
| 381 |
if (AiPlayer->UnitTypeRequests[i].Type == type) |
| 382 |
{ |
| 383 |
return &AiPlayer->UnitTypeRequests[i]; |
| 384 |
} |
| 385 |
} |
| 386 |
return NULL; |
| 387 |
} |
| 388 |
|
| 389 |
//---------------------------------------------------------------------------- |
| 390 |
|
| 391 |
/** |
| 392 |
** Set debugging flag of AI script |
| 393 |
** |
| 394 |
** @param l Lua state |
| 395 |
** |
| 396 |
** @return Number of return values |
| 397 |
*/ |
| 398 |
static int CclAiDebug(lua_State *l) |
| 399 |
{ |
| 400 |
LuaCheckArgs(l, 1); |
| 401 |
AiPlayer->ScriptDebug = LuaToBoolean(l, 1); |
| 402 |
return 0; |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
** Activate AI debugging for the given player(s) |
| 407 |
** Player can be a number for a specific player |
| 408 |
** "self" for current human player (ai me) |
| 409 |
** "none" to disable |
| 410 |
** |
| 411 |
** @param l Lua State |
| 412 |
** |
| 413 |
** @return Number of return values |
| 414 |
*/ |
| 415 |
static int CclAiDebugPlayer(lua_State *l) |
| 416 |
{ |
| 417 |
const char *item; |
| 418 |
int playerid; |
| 419 |
int args; |
| 420 |
int j; |
| 421 |
|
| 422 |
args = lua_gettop(l); |
| 423 |
for (j = 0; j < args; ++j) |
| 424 |
{ |
| 425 |
if (lua_isstring(l, j + 1)) |
| 426 |
{ |
| 427 |
item = LuaToString(l, j + 1); |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
item = NULL; |
| 432 |
} |
| 433 |
|
| 434 |
if (item && !strcmp(item, "none")) |
| 435 |
{ |
| 436 |
for (playerid = 0; playerid < NumPlayers; ++playerid) |
| 437 |
{ |
| 438 |
if (!Players[playerid].AiEnabled || !Players[playerid].Ai) |
| 439 |
{ |
| 440 |
continue; |
| 441 |
} |
| 442 |
((PlayerAi *)Players[playerid].Ai)->ScriptDebug = 0; |
| 443 |
} |
| 444 |
} |
| 445 |
else |
| 446 |
{ |
| 447 |
if (item && !strcmp(item, "self")) |
| 448 |
{ |
| 449 |
if (!ThisPlayer) |
| 450 |
{ |
| 451 |
continue; |
| 452 |
} |
| 453 |
playerid = ThisPlayer->Index; |
| 454 |
} |
| 455 |
else |
| 456 |
{ |
| 457 |
playerid = LuaToNumber(l, j + 1); |
| 458 |
} |
| 459 |
|
| 460 |
if (!Players[playerid].AiEnabled || !Players[playerid].Ai) |
| 461 |
{ |
| 462 |
continue; |
| 463 |
} |
| 464 |
((PlayerAi *)Players[playerid].Ai)->ScriptDebug = 1; |
| 465 |
} |
| 466 |
} |
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
** Need a unit. |
| 472 |
** |
| 473 |
** @param l Lua state. |
| 474 |
** |
| 475 |
** @return Number of return values |
| 476 |
*/ |
| 477 |
static int CclAiNeed(lua_State *l) |
| 478 |
{ |
| 479 |
LuaCheckArgs(l, 1); |
| 480 |
InsertUnitTypeRequests(CclGetUnitType(l), 1); |
| 481 |
|
| 482 |
lua_pushboolean(l, 0); |
| 483 |
return 1; |
| 484 |
} |
| 485 |
|
| 486 |
/** |
| 487 |
** Set the number of units. |
| 488 |
** |
| 489 |
** @param l Lua state |
| 490 |
** |
| 491 |
** @return Number of return values |
| 492 |
*/ |
| 493 |
static int CclAiSet(lua_State *l) |
| 494 |
{ |
| 495 |
AiRequestType *autt; |
| 496 |
CUnitType *type; |
| 497 |
|
| 498 |
LuaCheckArgs(l, 2); |
| 499 |
lua_pushvalue(l, 1); |
| 500 |
type = CclGetUnitType(l); |
| 501 |
lua_pop(l, 1); |
| 502 |
if ((autt = FindInUnitTypeRequests(type))) |
| 503 |
{ |
| 504 |
autt->Count = LuaToNumber(l, 2); |
| 505 |
// FIXME: 0 should remove it. |
| 506 |
} |
| 507 |
else |
| 508 |
{ |
| 509 |
InsertUnitTypeRequests(type, LuaToNumber(l, 2)); |
| 510 |
} |
| 511 |
|
| 512 |
lua_pushboolean(l, 0); |
| 513 |
return 1; |
| 514 |
} |
| 515 |
|
| 516 |
/** |
| 517 |
** Wait for a unit. |
| 518 |
** |
| 519 |
** @param l Lua State. |
| 520 |
** |
| 521 |
** @return Number of return values |
| 522 |
*/ |
| 523 |
static int CclAiWait(lua_State *l) |
| 524 |
{ |
| 525 |
const AiRequestType *autt; |
| 526 |
const CUnitType *type; |
| 527 |
const int *unit_types_count; |
| 528 |
int j; |
| 529 |
int n; |
| 530 |
|
| 531 |
LuaCheckArgs(l, 1); |
| 532 |
type = CclGetUnitType(l); |
| 533 |
unit_types_count = AiPlayer->Player->UnitTypesCount; |
| 534 |
if (!(autt = FindInUnitTypeRequests(type))) |
| 535 |
{ |
| 536 |
// |
| 537 |
// Look if we have this unit-type. |
| 538 |
// |
| 539 |
if (unit_types_count[type->Slot]) |
| 540 |
{ |
| 541 |
lua_pushboolean(l, 0); |
| 542 |
return 1; |
| 543 |
} |
| 544 |
|
| 545 |
// |
| 546 |
// Look if we have equivalent unit-types. |
| 547 |
// |
| 548 |
if (type->Slot < (int)AiHelpers.Equiv.size()) |
| 549 |
{ |
| 550 |
for (j = 0; j < (int)AiHelpers.Equiv[type->Slot].size(); ++j) |
| 551 |
{ |
| 552 |
if (unit_types_count[AiHelpers.Equiv[type->Slot][j]->Slot]) |
| 553 |
{ |
| 554 |
lua_pushboolean(l, 0); |
| 555 |
return 1; |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
DebugPrint("Broken? waiting on %s which wasn't requested.\n" _C_ type->Ident.c_str()); |
| 560 |
lua_pushboolean(l, 0); |
| 561 |
return 1; |
| 562 |
} |
| 563 |
// |
| 564 |
// Add equivalent units |
| 565 |
// |
| 566 |
n = unit_types_count[type->Slot]; |
| 567 |
if (type->Slot < (int)AiHelpers.Equiv.size()) |
| 568 |
{ |
| 569 |
for (j = 0; j < (int)AiHelpers.Equiv[type->Slot].size(); ++j) |
| 570 |
{ |
| 571 |
n += unit_types_count[AiHelpers.Equiv[type->Slot][j]->Slot]; |
| 572 |
} |
| 573 |
} |
| 574 |
// units available? |
| 575 |
|
| 576 |
if (n >= autt->Count) |
| 577 |
{ |
| 578 |
lua_pushboolean(l, 0); |
| 579 |
return 1; |
| 580 |
} |
| 581 |
|
| 582 |
lua_pushboolean(l, 1); |
| 583 |
return 1; |
| 584 |
} |
| 585 |
|
| 586 |
/** |
| 587 |
** Define a force, a groups of units. |
| 588 |
** |
| 589 |
** @param l Lua state. |
| 590 |
*/ |
| 591 |
static int CclAiForce(lua_State *l) |
| 592 |
{ |
| 593 |
AiUnitType *aiut; |
| 594 |
CUnitType *type; |
| 595 |
int count; |
| 596 |
int force; |
| 597 |
int args; |
| 598 |
int j; |
| 599 |
size_t i; |
| 600 |
|
| 601 |
LuaCheckArgs(l, 2); |
| 602 |
LuaCheckTable(l, 2); |
| 603 |
force = LuaToNumber(l, 1); |
| 604 |
if (force < 0 || force >= AI_MAX_FORCES) |
| 605 |
{ |
| 606 |
LuaError(l, "Force out of range: %d" _C_ force); |
| 607 |
} |
| 608 |
|
| 609 |
args = lua_objlen(l, 2); |
| 610 |
for (j = 0; j < args; ++j) |
| 611 |
{ |
| 612 |
lua_rawgeti(l, 2, j + 1); |
| 613 |
type = CclGetUnitType(l); |
| 614 |
lua_pop(l, 1); |
| 615 |
++j; |
| 616 |
count = LuaToNumber(l, 2, j + 1); |
| 617 |
|
| 618 |
if (!type) |
| 619 |
{ |
| 620 |
continue; |
| 621 |
} |
| 622 |
|
| 623 |
// Use the equivalent unittype. |
| 624 |
type = UnitTypes[UnitTypeEquivs[type->Slot]]; |
| 625 |
|
| 626 |
// |
| 627 |
// Look if already in force. |
| 628 |
// |
| 629 |
for (i = 0; i < AiPlayer->Force[force].UnitTypes.size(); ++i) |
| 630 |
{ |
| 631 |
aiut = &AiPlayer->Force[force].UnitTypes[i]; |
| 632 |
if (aiut->Type->Slot == type->Slot) |
| 633 |
{ |
| 634 |
// found |
| 635 |
if (count) |
| 636 |
{ |
| 637 |
aiut->Want = count; |
| 638 |
} |
| 639 |
else |
| 640 |
{ |
| 641 |
AiPlayer->Force[force].UnitTypes.erase( |
| 642 |
AiPlayer->Force[force].UnitTypes.begin() + i); |
| 643 |
} |
| 644 |
break; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
// |
| 649 |
// New type append it. |
| 650 |
// |
| 651 |
if (i == AiPlayer->Force[force].UnitTypes.size()) |
| 652 |
{ |
| 653 |
AiUnitType newaiut; |
| 654 |
newaiut.Want = count; |
| 655 |
newaiut.Type = type; |
| 656 |
AiPlayer->Force[force].UnitTypes.push_back(newaiut); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
AiAssignFreeUnitsToForce(); |
| 661 |
|
| 662 |
lua_pushboolean(l, 0); |
| 663 |
return 1; |
| 664 |
} |
| 665 |
|
| 666 |
/** |
| 667 |
** Define the role of a force. |
| 668 |
** |
| 669 |
** @param l Lua state. |
| 670 |
*/ |
| 671 |
static int CclAiForceRole(lua_State *l) |
| 672 |
{ |
| 673 |
int force; |
| 674 |
const char *flag; |
| 675 |
|
| 676 |
LuaCheckArgs(l, 2); |
| 677 |
force = LuaToNumber(l, 1); |
| 678 |
if (force < 0 || force >= AI_MAX_FORCES) |
| 679 |
{ |
| 680 |
LuaError(l, "Force %i out of range" _C_ force); |
| 681 |
} |
| 682 |
flag = LuaToString(l, 2); |
| 683 |
if (!strcmp(flag, "attack")) |
| 684 |
{ |
| 685 |
AiPlayer->Force[force].Role = AiForceRoleAttack; |
| 686 |
} |
| 687 |
else if (!strcmp(flag, "defend")) |
| 688 |
{ |
| 689 |
AiPlayer->Force[force].Role = AiForceRoleDefend; |
| 690 |
} |
| 691 |
else |
| 692 |
{ |
| 693 |
LuaError(l, "Unknown force role '%s'" _C_ flag); |
| 694 |
} |
| 695 |
|
| 696 |
lua_pushboolean(l, 0); |
| 697 |
return 1; |
| 698 |
} |
| 699 |
|
| 700 |
/** |
| 701 |
** Check if a force ready. |
| 702 |
** |
| 703 |
** @param l Lua state. |
| 704 |
*/ |
| 705 |
static int CclAiCheckForce(lua_State *l) |
| 706 |
{ |
| 707 |
int force; |
| 708 |
|
| 709 |
LuaCheckArgs(l, 1); |
| 710 |
force = LuaToNumber(l, 1); |
| 711 |
if (force < 0 || force >= AI_MAX_FORCES) |
| 712 |
{ |
| 713 |
lua_pushfstring(l, "Force out of range: %d", force); |
| 714 |
} |
| 715 |
if (AiPlayer->Force[force].Completed) |
| 716 |
{ |
| 717 |
lua_pushboolean(l, 1); |
| 718 |
return 1; |
| 719 |
} |
| 720 |
lua_pushboolean(l, 0); |
| 721 |
return 1; |
| 722 |
} |
| 723 |
|
| 724 |
/** |
| 725 |
** Wait for a force ready. |
| 726 |
** |
| 727 |
** @param l Lua state. |
| 728 |
*/ |
| 729 |
static int CclAiWaitForce(lua_State *l) |
| 730 |
{ |
| 731 |
int force; |
| 732 |
|
| 733 |
LuaCheckArgs(l, 1); |
| 734 |
force = LuaToNumber(l, 1); |
| 735 |
if (force < 0 || force >= AI_MAX_FORCES) |
| 736 |
{ |
| 737 |
LuaError(l, "Force out of range: %d" _C_ force); |
| 738 |
} |
| 739 |
if (AiPlayer->Force[force].Completed) |
| 740 |
{ |
| 741 |
lua_pushboolean(l, 0); |
| 742 |
return 1; |
| 743 |
} |
| 744 |
|
| 745 |
#if 0 |
| 746 |
// Debuging |
| 747 |
AiCleanForces(); |
| 748 |
Assert(!AiPlayer->Force[force].Completed); |
| 749 |
#endif |
| 750 |
|
| 751 |
lua_pushboolean(l, 1); |
| 752 |
return 1; |
| 753 |
} |
| 754 |
|
| 755 |
/** |
| 756 |
** Attack with force. |
| 757 |
** |
| 758 |
** @param l Lua state. |
| 759 |
*/ |
| 760 |
static int CclAiAttackWithForce(lua_State *l) |
| 761 |
{ |
| 762 |
int force; |
| 763 |
|
| 764 |
LuaCheckArgs(l, 1); |
| 765 |
force = LuaToNumber(l, 1); |
| 766 |
if (force < 0 || force >= AI_MAX_FORCES) |
| 767 |
{ |
| 768 |
LuaError(l, "Force out of range: %d" _C_ force); |
| 769 |
} |
| 770 |
|
| 771 |
AiAttackWithForce(force); |
| 772 |
|
| 773 |
lua_pushboolean(l, 0); |
| 774 |
return 1; |
| 775 |
} |
| 776 |
|
| 777 |
/** |
| 778 |
** Sleep n cycles. |
| 779 |
** |
| 780 |
** @param l Lua state. |
| 781 |
*/ |
| 782 |
static int CclAiSleep(lua_State *l) |
| 783 |
{ |
| 784 |
int i; |
| 785 |
|
| 786 |
LuaCheckArgs(l, 1); |
| 787 |
i = LuaToNumber(l, 1); |
| 788 |
if (AiPlayer->SleepCycles || i == 0) |
| 789 |
{ |
| 790 |
if (AiPlayer->SleepCycles < GameCycle) |
| 791 |
{ |
| 792 |
AiPlayer->SleepCycles = 0; |
| 793 |
lua_pushboolean(l, 0); |
| 794 |
return 1; |
| 795 |
} |
| 796 |
} |
| 797 |
else |
| 798 |
{ |
| 799 |
AiPlayer->SleepCycles = GameCycle + i; |
| 800 |
} |
| 801 |
|
| 802 |
lua_pushboolean(l, 1); |
| 803 |
return 1; |
| 804 |
} |
| 805 |
|
| 806 |
/** |
| 807 |
** Return the player of the running AI. |
| 808 |
** |
| 809 |
** @param l Lua state. |
| 810 |
** |
| 811 |
** @return Player number of the AI. |
| 812 |
*/ |
| 813 |
static int CclAiPlayer(lua_State *l) |
| 814 |
{ |
| 815 |
LuaCheckArgs(l, 0); |
| 816 |
lua_pushnumber(l, AiPlayer->Player->Index); |
| 817 |
return 1; |
| 818 |
} |
| 819 |
|
| 820 |
/** |
| 821 |
** Get the default resource number |
| 822 |
** |
| 823 |
** @param name Resource name. |
| 824 |
** |
| 825 |
** @return The number of the resource in DefaultResourceNames |
| 826 |
*/ |
| 827 |
static int DefaultResourceNumber(const char *name) |
| 828 |
{ |
| 829 |
for (int i = 0; i < MaxCosts; ++i) |
| 830 |
{ |
| 831 |
if (DefaultResourceNames[i] == name) |
| 832 |
{ |
| 833 |
return i; |
| 834 |
} |
| 835 |
} |
| 836 |
// Resource not found, should never happen |
| 837 |
Assert(0); |
| 838 |
return -1; |
| 839 |
} |
| 840 |
|
| 841 |
/** |
| 842 |
** Define an AI player. |
| 843 |
** |
| 844 |
** @param l Lua state. |
| 845 |
*/ |
| 846 |
static int CclDefineAiPlayer(lua_State *l) |
| 847 |
{ |
| 848 |
const char *value; |
| 849 |
int i; |
| 850 |
PlayerAi *ai; |
| 851 |
int args; |
| 852 |
int j; |
| 853 |
int subargs; |
| 854 |
int k; |
| 855 |
|
| 856 |
args = lua_gettop(l); |
| 857 |
j = 0; |
| 858 |
|
| 859 |
i = LuaToNumber(l, j + 1); |
| 860 |
++j; |
| 861 |
|
| 862 |
Assert(i >= 0 && i <= PlayerMax); |
| 863 |
DebugPrint("%p %d\n" _C_ Players[i].Ai _C_ Players[i].AiEnabled ); |
| 864 |
// FIXME: lose this: |
| 865 |
// Assert(!Players[i].Ai && Players[i].AiEnabled); |
| 866 |
|
| 867 |
ai = Players[i].Ai = new PlayerAi; |
| 868 |
ai->Player = &Players[i]; |
| 869 |
|
| 870 |
// |
| 871 |
// Parse the list: (still everything could be changed!) |
| 872 |
// |
| 873 |
for (; j < args; ++j) |
| 874 |
{ |
| 875 |
value = LuaToString(l, j + 1); |
| 876 |
++j; |
| 877 |
|
| 878 |
if (!strcmp(value, "ai-type")) |
| 879 |
{ |
| 880 |
CAiType *ait = NULL; |
| 881 |
|
| 882 |
value = LuaToString(l, j + 1); |
| 883 |
for (k = 0; k < (int)AiTypes.size(); ++k) |
| 884 |
{ |
| 885 |
ait = AiTypes[k]; |
| 886 |
if (ait->Name == value) |
| 887 |
{ |
| 888 |
break; |
| 889 |
} |
| 890 |
} |
| 891 |
if (k == (int)AiTypes.size()) |
| 892 |
{ |
| 893 |
lua_pushfstring(l, "ai-type not found: %s", value); |
| 894 |
} |
| 895 |
ai->AiType = ait; |
| 896 |
} |
| 897 |
else if (!strcmp(value, "script")) |
| 898 |
{ |
| 899 |
// Ignore, for compatibility with games saved |
| 900 |
// by earlier versions. |
| 901 |
} |
| 902 |
else if (!strcmp(value, "script-debug")) |
| 903 |
{ |
| 904 |
ai->ScriptDebug = LuaToBoolean(l, j + 1); |
| 905 |
} |
| 906 |
else if (!strcmp(value, "sleep-cycles")) |
| 907 |
{ |
| 908 |
ai->SleepCycles = LuaToNumber(l, j + 1); |
| 909 |
} |
| 910 |
else if (!strcmp(value, "force")) |
| 911 |
{ |
| 912 |
LuaCheckTable(l, j + 1); |
| 913 |
subargs = lua_objlen(l, j + 1); |
| 914 |
k = 0; |
| 915 |
i = LuaToNumber(l, j + 1, k + 1); |
| 916 |
++k; |
| 917 |
for (; k < subargs; ++k) |
| 918 |
{ |
| 919 |
value = LuaToString(l, j + 1, k + 1); |
| 920 |
++k; |
| 921 |
if (!strcmp(value, "complete")) |
| 922 |
{ |
| 923 |
ai->Force[i].Completed = true; |
| 924 |
--k; |
| 925 |
} |
| 926 |
else if (!strcmp(value, "recruit")) |
| 927 |
{ |
| 928 |
ai->Force[i].Completed = false; |
| 929 |
--k; |
| 930 |
} |
| 931 |
else if (!strcmp(value, "attack")) |
| 932 |
{ |
| 933 |
ai->Force[i].Attacking = true; |
| 934 |
--k; |
| 935 |
} |
| 936 |
else if (!strcmp(value, "defend")) |
| 937 |
{ |
| 938 |
ai->Force[i].Defending = true; |
| 939 |
--k; |
| 940 |
} |
| 941 |
else if (!strcmp(value, "role")) |
| 942 |
{ |
| 943 |
value = LuaToString(l, j + 1, k + 1); |
| 944 |
if (!strcmp(value, "attack")) |
| 945 |
{ |
| 946 |
ai->Force[i].Role = AiForceRoleAttack; |
| 947 |
} |
| 948 |
else if (!strcmp(value, "defend")) |
| 949 |
{ |
| 950 |
ai->Force[i].Role = AiForceRoleDefend; |
| 951 |
} |
| 952 |
else |
| 953 |
{ |
| 954 |
LuaError(l, "Unsupported force tag: %s" _C_ value); |
| 955 |
} |
| 956 |
} |
| 957 |
else if (!strcmp(value, "types")) |
| 958 |
{ |
| 959 |
int subsubargs; |
| 960 |
int subk; |
| 961 |
|
| 962 |
lua_rawgeti(l, j + 1, k + 1); |
| 963 |
LuaCheckTable(l, -1); |
| 964 |
subsubargs = lua_objlen(l, -1); |
| 965 |
for (subk = 0; subk < subsubargs; ++subk) |
| 966 |
{ |
| 967 |
int num; |
| 968 |
const char *ident; |
| 969 |
AiUnitType queue; |
| 970 |
|
| 971 |
num = LuaToNumber(l, -1, subk + 1); |
| 972 |
++subk; |
| 973 |
ident = LuaToString(l, -1, subk + 1); |
| 974 |
queue.Want = num; |
| 975 |
queue.Type = UnitTypeByIdent(ident); |
| 976 |
ai->Force[i].UnitTypes.push_back(queue); |
| 977 |
} |
| 978 |
lua_pop(l, 1); |
| 979 |
} |
| 980 |
else if (!strcmp(value, "units")) |
| 981 |
{ |
| 982 |
int subsubargs; |
| 983 |
int subk; |
| 984 |
|
| 985 |
lua_rawgeti(l, j + 1, k + 1); |
| 986 |
LuaCheckTable(l, -1); |
| 987 |
subsubargs = lua_objlen(l, -1); |
| 988 |
for (subk = 0; subk < subsubargs; ++subk) |
| 989 |
{ |
| 990 |
int num; |
| 991 |
|
| 992 |
num = LuaToNumber(l, -1, subk + 1); |
| 993 |
++subk; |
| 994 |
LuaToString(l, -1, subk + 1); |
| 995 |
ai->Force[i].Units.push_back(UnitSlots[num]); |
| 996 |
} |
| 997 |
lua_pop(l, 1); |
| 998 |
} |
| 999 |
else if (!strcmp(value, "state")) |
| 1000 |
{ |
| 1001 |
ai->Force[i].State = LuaToNumber(l, j + 1, k + 1); |
| 1002 |
} |
| 1003 |
else if (!strcmp(value, "goalx")) |
| 1004 |
{ |
| 1005 |
ai->Force[i].GoalX = LuaToNumber(l, j + 1, k + 1); |
| 1006 |
} |
| 1007 |
else if (!strcmp(value, "goaly")) |
| 1008 |
{ |
| 1009 |
ai->Force[i].GoalY = LuaToNumber(l, j + 1, k + 1); |
| 1010 |
} |
| 1011 |
else |
| 1012 |
{ |
| 1013 |
LuaError(l, "Unsupported tag: %s" _C_ value); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
} |
| 1017 |
else if (!strcmp(value, "needed")) |
| 1018 |
{ |
| 1019 |
LuaCheckTable(l, j + 1); |
| 1020 |
subargs = lua_objlen(l, j + 1); |
| 1021 |
for (k = 0; k < subargs; ++k) |
| 1022 |
{ |
| 1023 |
const char *type; |
| 1024 |
int num; |
| 1025 |
|
| 1026 |
type = LuaToString(l, j + 1, k + 1); |
| 1027 |
++k; |
| 1028 |
num = LuaToNumber(l, j + 1, k + 1); |
| 1029 |
ai->Needed[DefaultResourceNumber(type)] = num; |
| 1030 |
} |
| 1031 |
} |
| 1032 |
else if (!strcmp(value, "need-mask")) |
| 1033 |
{ |
| 1034 |
LuaCheckTable(l, j + 1); |
| 1035 |
subargs = lua_objlen(l, j + 1); |
| 1036 |
for (k = 0; k < subargs; ++k) |
| 1037 |
{ |
| 1038 |
const char *type; |
| 1039 |
|
| 1040 |
type = LuaToString(l, j + 1, k + 1); |
| 1041 |
ai->NeededMask |= (1 << DefaultResourceNumber(type)); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
else if (!strcmp(value, "last-can-not-move-cycle")) |
| 1045 |
{ |
| 1046 |
ai->LastCanNotMoveGameCycle = LuaToNumber(l, j + 1); |
| 1047 |
} |
| 1048 |
else if (!strcmp(value, "unit-type")) |
| 1049 |
{ |
| 1050 |
LuaCheckTable(l, j + 1); |
| 1051 |
subargs = lua_objlen(l, j + 1); |
| 1052 |
i = 0; |
| 1053 |
if (subargs) |
| 1054 |
{ |
| 1055 |
ai->UnitTypeRequests.resize(subargs / 2); |
| 1056 |
} |
| 1057 |
for (k = 0; k < subargs; ++k) |
| 1058 |
{ |
| 1059 |
const char *ident; |
| 1060 |
int count; |
| 1061 |
|
| 1062 |
ident = LuaToString(l, j + 1, k + 1); |
| 1063 |
++k; |
| 1064 |
count = LuaToNumber(l, j + 1, k + 1); |
| 1065 |
ai->UnitTypeRequests[i].Type = UnitTypeByIdent(ident); |
| 1066 |
ai->UnitTypeRequests[i].Count = count; |
| 1067 |
++i; |
| 1068 |
} |
| 1069 |
} |
| 1070 |
else if (!strcmp(value, "building")) |
| 1071 |
{ |
| 1072 |
LuaCheckTable(l, j + 1); |
| 1073 |
subargs = lua_objlen(l, j + 1); |
| 1074 |
for (k = 0; k < subargs; ++k) |
| 1075 |
{ |
| 1076 |
const char *ident; |
| 1077 |
int made; |
| 1078 |
int want; |
| 1079 |
AiBuildQueue queue; |
| 1080 |
|
| 1081 |
ident = LuaToString(l, j + 1, k + 1); |
| 1082 |
++k; |
| 1083 |
made = LuaToNumber(l, j + 1, k + 1); |
| 1084 |
++k; |
| 1085 |
want = LuaToNumber(l, j + 1, k + 1); |
| 1086 |
queue.Type = UnitTypeByIdent(ident); |
| 1087 |
queue.Want = want; |
| 1088 |
queue.Made = made; |
| 1089 |
ai->UnitTypeBuilt.push_back(queue); |
| 1090 |
} |
| 1091 |
} |
| 1092 |
else if (!strcmp(value, "repair-building")) |
| 1093 |
{ |
| 1094 |
ai->LastRepairBuilding = LuaToNumber(l, j + 1); |
| 1095 |
} |
| 1096 |
else if (!strcmp(value, "repair-workers")) |
| 1097 |
{ |
| 1098 |
LuaCheckTable(l, j + 1); |
| 1099 |
subargs = lua_objlen(l, j + 1); |
| 1100 |
for (k = 0; k < subargs; ++k) |
| 1101 |
{ |
| 1102 |
int num; |
| 1103 |
int workers; |
| 1104 |
|
| 1105 |
num = LuaToNumber(l, j + 1, k + 1); |
| 1106 |
++k; |
| 1107 |
workers = LuaToNumber(l, j + 1, k + 1); |
| 1108 |
ai->TriedRepairWorkers[num] = workers; |
| 1109 |
++i; |
| 1110 |
} |
| 1111 |
} |
| 1112 |
else |
| 1113 |
{ |
| 1114 |
LuaError(l, "Unsupported tag: %s" _C_ value); |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
return 0; |
| 1119 |
} |
| 1120 |
|
| 1121 |
/** |
| 1122 |
** Register CCL features for unit-type. |
| 1123 |
*/ |
| 1124 |
void AiCclRegister() |
| 1125 |
{ |
| 1126 |
// FIXME: Need to save memory here. |
| 1127 |
// Loading all into memory isn't necessary. |
| 1128 |
|
| 1129 |
lua_register(Lua, "DefineAiHelper", CclDefineAiHelper); |
| 1130 |
lua_register(Lua, "DefineAiType", CclDefineAiType); |
| 1131 |
|
| 1132 |
lua_register(Lua, "AiDebug", CclAiDebug); |
| 1133 |
lua_register(Lua, "AiDebugPlayer", CclAiDebugPlayer); |
| 1134 |
lua_register(Lua, "AiNeed", CclAiNeed); |
| 1135 |
lua_register(Lua, "AiSet", CclAiSet); |
| 1136 |
lua_register(Lua, "AiWait", CclAiWait); |
| 1137 |
|
| 1138 |
lua_register(Lua, "AiForce", CclAiForce); |
| 1139 |
|
| 1140 |
lua_register(Lua, "AiForceRole", CclAiForceRole); |
| 1141 |
lua_register(Lua, "AiCheckForce", CclAiCheckForce); |
| 1142 |
lua_register(Lua, "AiWaitForce", CclAiWaitForce); |
| 1143 |
|
| 1144 |
lua_register(Lua, "AiAttackWithForce", CclAiAttackWithForce); |
| 1145 |
lua_register(Lua, "AiSleep", CclAiSleep); |
| 1146 |
|
| 1147 |
lua_register(Lua, "AiPlayer", CclAiPlayer); |
| 1148 |
|
| 1149 |
lua_register(Lua, "DefineAiPlayer", CclDefineAiPlayer); |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
//@} |