| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name selection.cpp - The units' selection. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1999-2008 by Patrice Fortier, Lutz Sammer, and |
| 13 |
// 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 <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#include "stratagus.h" |
| 41 |
#include "unittype.h" |
| 42 |
#include "player.h" |
| 43 |
#include "unit.h" |
| 44 |
#include "unit_manager.h" |
| 45 |
#include "unit_cache.h" |
| 46 |
#include "interface.h" |
| 47 |
#include "map.h" |
| 48 |
#include "ui.h" |
| 49 |
#include "commands.h" |
| 50 |
#include "network.h" |
| 51 |
#include "iolib.h" |
| 52 |
|
| 53 |
#include "script.h" |
| 54 |
|
| 55 |
/*---------------------------------------------------------------------------- |
| 56 |
-- Variables |
| 57 |
----------------------------------------------------------------------------*/ |
| 58 |
|
| 59 |
int NumSelected; /// Number of selected units |
| 60 |
int TeamNumSelected[PlayerMax]; /// How many units selected |
| 61 |
int MaxSelectable; /// Maximum number of selected units |
| 62 |
CUnit **Selected; /// All selected units |
| 63 |
CUnit **TeamSelected[PlayerMax]; /// teams currently selected units |
| 64 |
|
| 65 |
static int _NumSelected; /// save of NumSelected |
| 66 |
static int _TeamNumSelected[PlayerMax]; /// save of TeamNumSelected |
| 67 |
static CUnit **_Selected; /// save of Selected |
| 68 |
static CUnit **_TeamSelected[PlayerMax]; /// save of TeamSelected |
| 69 |
|
| 70 |
static unsigned GroupId; /// Unique group # for automatic groups |
| 71 |
|
| 72 |
/*---------------------------------------------------------------------------- |
| 73 |
-- Functions |
| 74 |
----------------------------------------------------------------------------*/ |
| 75 |
|
| 76 |
/** |
| 77 |
** Save selection to restore after. |
| 78 |
*/ |
| 79 |
void SaveSelection(void) |
| 80 |
{ |
| 81 |
int i; |
| 82 |
int j; |
| 83 |
|
| 84 |
for (i = 0; i < PlayerMax; ++i) { |
| 85 |
_TeamNumSelected[i] = TeamNumSelected[i]; |
| 86 |
for (j = 0; j < TeamNumSelected[i]; ++j) { |
| 87 |
_TeamSelected[i][j] = TeamSelected[i][j]; |
| 88 |
} |
| 89 |
} |
| 90 |
_NumSelected = NumSelected; |
| 91 |
for (j = 0; j < NumSelected; ++j) { |
| 92 |
_Selected[j] = Selected[j]; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
** Restore selection. |
| 98 |
*/ |
| 99 |
void RestoreSelection(void) |
| 100 |
{ |
| 101 |
int i; |
| 102 |
int j; |
| 103 |
|
| 104 |
UnSelectAll(); |
| 105 |
for (i = 0; i < PlayerMax; ++i) { |
| 106 |
TeamNumSelected[i] = _TeamNumSelected[i]; |
| 107 |
for (j = 0; j < _TeamNumSelected[i]; ++j) { |
| 108 |
TeamSelected[i][j] = _TeamSelected[i][j]; |
| 109 |
TeamSelected[i][j]->TeamSelected |= (1 << i); |
| 110 |
} |
| 111 |
} |
| 112 |
NumSelected = _NumSelected; |
| 113 |
for (j = 0; j < _NumSelected; ++j) { |
| 114 |
Selected[j] = _Selected[j]; |
| 115 |
Selected[j]->Selected = 1; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
** Unselect all the units in the current selection |
| 121 |
*/ |
| 122 |
void UnSelectAll(void) |
| 123 |
{ |
| 124 |
CUnit *unit; |
| 125 |
|
| 126 |
while (!++GroupId) { // Advance group id, but keep non zero |
| 127 |
} |
| 128 |
|
| 129 |
while (NumSelected) { |
| 130 |
unit = Selected[--NumSelected]; |
| 131 |
Selected[NumSelected] = NoUnitP; // FIXME: only needed for old code |
| 132 |
unit->Selected = 0; |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
** Replace a group of selected units by an other group of units. |
| 139 |
** |
| 140 |
** @param units Array of units to be selected. |
| 141 |
** @param count Number of units in array to be selected. |
| 142 |
*/ |
| 143 |
void ChangeSelectedUnits(CUnit **units,int count) |
| 144 |
{ |
| 145 |
CUnit *unit; |
| 146 |
int i; |
| 147 |
int n; |
| 148 |
|
| 149 |
Assert(count <= MaxSelectable); |
| 150 |
|
| 151 |
UnSelectAll(); |
| 152 |
NetworkSendSelection(units, count); |
| 153 |
for (n = i = 0; i < count; ++i) { |
| 154 |
if (!units[i]->Removed && !units[i]->TeamSelected && !units[i]->Type->IsNotSelectable) { |
| 155 |
Selected[n++] = unit = units[i]; |
| 156 |
unit->Selected = 1; |
| 157 |
if (count > 1) { |
| 158 |
unit->LastGroup = GroupId; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
NumSelected = n; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
** Change A Unit Selection from my Team |
| 167 |
** |
| 168 |
** @param player The Player who is selecting the units |
| 169 |
** @param units The Units to add/remove |
| 170 |
** @param adjust 0 = reset, 1 = remove units, 2 = add units |
| 171 |
** @param count the number of units to be adjusted |
| 172 |
*/ |
| 173 |
void ChangeTeamSelectedUnits(CPlayer *player, CUnit **units, int adjust, int count) |
| 174 |
{ |
| 175 |
int i; |
| 176 |
int n; |
| 177 |
CUnit *unit; |
| 178 |
|
| 179 |
switch (adjust) { |
| 180 |
case 0: |
| 181 |
// UnSelectAllTeam(player); |
| 182 |
while (TeamNumSelected[player->Index]) { |
| 183 |
unit = TeamSelected[player->Index][--TeamNumSelected[player->Index]]; |
| 184 |
unit->TeamSelected &= ~(1 << player->Index); |
| 185 |
TeamSelected[player->Index][TeamNumSelected[player->Index]] = NoUnitP; // FIXME: only needed for old code |
| 186 |
} |
| 187 |
// FALL THROUGH |
| 188 |
case 2: |
| 189 |
for (i = 0; i < count; ++i) { |
| 190 |
Assert(!units[i]->Removed); |
| 191 |
if (!units[i]->Type->IsNotSelectable) { |
| 192 |
TeamSelected[player->Index][TeamNumSelected[player->Index]++] = units[i]; |
| 193 |
units[i]->TeamSelected |= 1 << player->Index; |
| 194 |
} |
| 195 |
} |
| 196 |
Assert(TeamNumSelected[player->Index] <= MaxSelectable); |
| 197 |
break; |
| 198 |
case 1: |
| 199 |
for (n = 0; n < TeamNumSelected[player->Index]; ++n) { |
| 200 |
for (i = 0; i < count; ++i) { |
| 201 |
if (units[i] == TeamSelected[player->Index][n]) { |
| 202 |
TeamSelected[player->Index][n] = |
| 203 |
TeamSelected[player->Index][TeamNumSelected[player->Index]--]; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
Assert(TeamNumSelected[player->Index] >= 0); |
| 208 |
break; |
| 209 |
default: |
| 210 |
Assert(0); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
** Add a unit to the other selected units. |
| 216 |
** |
| 217 |
** @param unit Pointer to unit to add. |
| 218 |
** |
| 219 |
** @return true if added to selection, false otherwise |
| 220 |
** (if NumSelected == MaxSelectable or |
| 221 |
** unit is already selected or unselectable) |
| 222 |
*/ |
| 223 |
int SelectUnit(CUnit *unit) |
| 224 |
{ |
| 225 |
if (unit->Type->Revealer) { // Revealers cannot be selected |
| 226 |
DebugPrint("Selecting revealer?\n"); |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
if (unit->Removed) { // Removed cannot be selected |
| 231 |
DebugPrint("Selecting removed?\n"); |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
if (NumSelected == MaxSelectable) { |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
if (unit->Selected) { |
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
if (unit->Type->IsNotSelectable && GameRunning) { |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
Selected[NumSelected++] = unit; |
| 248 |
unit->Selected = 1; |
| 249 |
if (NumSelected > 1) { |
| 250 |
Selected[0]->LastGroup = unit->LastGroup = GroupId; |
| 251 |
} |
| 252 |
return 1; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
** Select a single unit, unselecting the previous ones |
| 257 |
** |
| 258 |
** @param unit Pointer to unit to be selected. |
| 259 |
*/ |
| 260 |
void SelectSingleUnit(CUnit *unit) |
| 261 |
{ |
| 262 |
ChangeSelectedUnits(&unit, 1); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
** Unselect unit |
| 267 |
** |
| 268 |
** @param unit Pointer to unit to be unselected. |
| 269 |
*/ |
| 270 |
void UnSelectUnit(CUnit *unit) |
| 271 |
{ |
| 272 |
int i; |
| 273 |
int j; |
| 274 |
|
| 275 |
if (unit->TeamSelected) { |
| 276 |
for (i = 0; i < PlayerMax; ++i) { |
| 277 |
if (unit->TeamSelected & (1 << i)) { |
| 278 |
for (j = 0; TeamSelected[i][j] != unit; ++j) { |
| 279 |
; |
| 280 |
} |
| 281 |
Assert(j < TeamNumSelected[i]); |
| 282 |
|
| 283 |
if (j < --TeamNumSelected[i]) { |
| 284 |
|
| 285 |
TeamSelected[i][j] = TeamSelected[i][TeamNumSelected[i]]; |
| 286 |
} |
| 287 |
unit->TeamSelected &= ~(1 << i); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
if (!unit->Selected) { |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
for (i = 0; Selected[i] != unit; ++i) { |
| 296 |
; |
| 297 |
} |
| 298 |
Assert(i < NumSelected); |
| 299 |
|
| 300 |
if (i < --NumSelected) { |
| 301 |
Selected[i] = Selected[NumSelected]; |
| 302 |
} |
| 303 |
|
| 304 |
if (NumSelected > 1) { // Assign new group to remaining units |
| 305 |
while (!++GroupId) { // Advance group id, but keep non zero |
| 306 |
} |
| 307 |
for (i = 0; i < NumSelected; ++i) { |
| 308 |
Selected[i]->LastGroup = GroupId; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
Selected[NumSelected] = NoUnitP; // FIXME: only needed for old code |
| 313 |
unit->Selected = 0; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
** Toggle the selection of a unit in a group of selected units |
| 318 |
** |
| 319 |
** @param unit Pointer to unit to be toggled. |
| 320 |
** @return 0 if unselected, 1 otherwise |
| 321 |
*/ |
| 322 |
int ToggleSelectUnit(CUnit *unit) |
| 323 |
{ |
| 324 |
if (unit->Selected) { |
| 325 |
UnSelectUnit(unit); |
| 326 |
return 0; |
| 327 |
} |
| 328 |
SelectUnit(unit); |
| 329 |
return 1; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
** Select units from a particular type and belonging to the local player. |
| 334 |
** |
| 335 |
** The base is included in the selection and defines |
| 336 |
** the type of the other units to be selected. |
| 337 |
** |
| 338 |
** @param base Select all units of same type. |
| 339 |
** |
| 340 |
** @return Number of units found, 0 means selection unchanged |
| 341 |
** |
| 342 |
** FIXME: 0 can't happen. Maybe when scripting will use it? |
| 343 |
** |
| 344 |
** FIXME: should always select the nearest 9 units to the base! |
| 345 |
*/ |
| 346 |
int SelectUnitsByType(CUnit *base) |
| 347 |
{ |
| 348 |
CUnit *unit; |
| 349 |
CUnit *table[UnitMax]; |
| 350 |
const CUnitType *type; |
| 351 |
int r; |
| 352 |
int i; |
| 353 |
const CViewport *vp; |
| 354 |
|
| 355 |
Assert(UI.MouseViewport); |
| 356 |
|
| 357 |
type = base->Type; |
| 358 |
|
| 359 |
// select all visible units. |
| 360 |
// StephanR: should be (MapX,MapY,MapX+MapWidth-1,MapY+MapHeight-1) ??? |
| 361 |
/* FIXME: this should probably be cleaner implemented if SelectUnitsByType() |
| 362 |
* took parameters of the selection rectangle as arguments */ |
| 363 |
vp = UI.MouseViewport; |
| 364 |
r = UnitCache.Select(vp->MapX - 1, vp->MapY - 1, vp->MapX + vp->MapWidth + 1, |
| 365 |
vp->MapY + vp->MapHeight + 1, table, UnitMax); |
| 366 |
|
| 367 |
// if unit is a cadaver or hidden (not on map) |
| 368 |
// no unit can be selected. |
| 369 |
if (base->Removed || base->Orders[0]->Action == UnitActionDie) { |
| 370 |
return 0; |
| 371 |
} |
| 372 |
|
| 373 |
if (base->Type->IsNotSelectable && GameRunning) { |
| 374 |
return 0; |
| 375 |
} |
| 376 |
if (base->TeamSelected) { // Somebody else onteam has this unit |
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
UnSelectAll(); |
| 381 |
Selected[0] = base; |
| 382 |
base->Selected = 1; |
| 383 |
NumSelected = 1; |
| 384 |
|
| 385 |
// if unit isn't belonging to the player or allied player, or is a static unit |
| 386 |
// (like a building), only 1 unit can be selected at the same time. |
| 387 |
if (!CanSelectMultipleUnits(base->Player) || !type->SelectableByRectangle) { |
| 388 |
return NumSelected; |
| 389 |
} |
| 390 |
|
| 391 |
// |
| 392 |
// Search for other visible units of the same type |
| 393 |
// |
| 394 |
// FIXME: peon/peasant with gold/wood & co are considered from |
| 395 |
// different type... idem for tankers |
| 396 |
for (i = 0; i < r; ++i) { |
| 397 |
unit = table[i]; |
| 398 |
if (!CanSelectMultipleUnits(unit->Player) || unit->Type != type) { |
| 399 |
continue; |
| 400 |
} |
| 401 |
if (unit->IsUnusable()) { // guess SelectUnits doesn't check this |
| 402 |
continue; |
| 403 |
} |
| 404 |
if (unit == base) { // no need to have the same unit twice :) |
| 405 |
continue; |
| 406 |
} |
| 407 |
if (unit->TeamSelected) { // Somebody else onteam has this unit |
| 408 |
continue; |
| 409 |
} |
| 410 |
Selected[NumSelected++] = unit; |
| 411 |
unit->Selected = 1; |
| 412 |
if (NumSelected == MaxSelectable) { |
| 413 |
break; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
if (NumSelected > 1) { |
| 418 |
for (i = 0; i < NumSelected; ++i) { |
| 419 |
Selected[i]->LastGroup = GroupId; |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
NetworkSendSelection(Selected, NumSelected); |
| 424 |
return NumSelected; |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
** Toggle units from a particular type and belonging to the local player. |
| 429 |
** |
| 430 |
** The base is included in the selection and defines |
| 431 |
** the type of the other units to be selected. |
| 432 |
** |
| 433 |
** @param base Toggle all units of same type. |
| 434 |
** |
| 435 |
** @return Number of units found, 0 means selection unchanged |
| 436 |
** |
| 437 |
** FIXME: toggle not written |
| 438 |
** FIXME: should always select the nearest 9 units to the base! |
| 439 |
*/ |
| 440 |
int ToggleUnitsByType(CUnit *base) |
| 441 |
{ |
| 442 |
CUnit *unit; |
| 443 |
CUnit *table[UnitMax]; |
| 444 |
const CUnitType *type; |
| 445 |
int r; |
| 446 |
int i; |
| 447 |
|
| 448 |
type = base->Type; |
| 449 |
|
| 450 |
// select all visible units. |
| 451 |
// StephanR: should be (MapX,MapY,MapX+MapWidth-1,MapY+MapHeight-1) ??? |
| 452 |
// FIXME: this should probably be cleaner implemented if SelectUnitsByType() |
| 453 |
// took parameters of the selection rectangle as arguments */ |
| 454 |
r = UnitCache.Select(UI.MouseViewport->MapX - 1, |
| 455 |
UI.MouseViewport->MapY - 1, |
| 456 |
UI.MouseViewport->MapX + UI.MouseViewport->MapWidth + 1, |
| 457 |
UI.MouseViewport->MapY + UI.MouseViewport->MapHeight + 1, table, UnitMax); |
| 458 |
|
| 459 |
// if unit is a cadaver or hidden (not on map) |
| 460 |
// no unit can be selected. |
| 461 |
if (base->Removed || base->Orders[0]->Action == UnitActionDie) { |
| 462 |
return 0; |
| 463 |
} |
| 464 |
// if unit isn't belonging to the player, or is a static unit |
| 465 |
// (like a building), only 1 unit can be selected at the same time. |
| 466 |
if (!CanSelectMultipleUnits(base->Player) || !type->SelectableByRectangle) { |
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| 470 |
if (!SelectUnit(base)) { // Add base to selection |
| 471 |
return 0; |
| 472 |
} |
| 473 |
// |
| 474 |
// Search for other visible units of the same type |
| 475 |
// |
| 476 |
// FIXME: peon/peasant with gold/wood & co are considered from |
| 477 |
// different type... idem for tankers |
| 478 |
for (i = 0; i < r; ++i) { |
| 479 |
unit = table[i]; |
| 480 |
if (!CanSelectMultipleUnits(unit->Player) || unit->Type != type) { |
| 481 |
continue; |
| 482 |
} |
| 483 |
if (unit->IsUnusable()) { // guess SelectUnits doesn't check this |
| 484 |
continue; |
| 485 |
} |
| 486 |
if (unit == base) { // no need to have the same unit twice |
| 487 |
continue; |
| 488 |
} |
| 489 |
if (unit->TeamSelected) { // Somebody else onteam has this unit |
| 490 |
continue; |
| 491 |
} |
| 492 |
if (!SelectUnit(unit)) { // add unit to selection |
| 493 |
return NumSelected; |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
NetworkSendSelection(Selected, NumSelected); |
| 498 |
return NumSelected; |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
** Change selected units to units from group group_number |
| 503 |
** Doesn't change the selection if the group has no unit. |
| 504 |
** |
| 505 |
** @param group_number number of the group to be selected. |
| 506 |
** |
| 507 |
** @return number of units in the group. |
| 508 |
*/ |
| 509 |
int SelectGroup(int group_number) |
| 510 |
{ |
| 511 |
int nunits; |
| 512 |
|
| 513 |
Assert(group_number <= NUM_GROUPS); |
| 514 |
|
| 515 |
if (!(nunits = GetNumberUnitsOfGroup(group_number))) { |
| 516 |
return 0; |
| 517 |
} |
| 518 |
|
| 519 |
ChangeSelectedUnits(GetUnitsOfGroup(group_number), nunits); |
| 520 |
return NumSelected; |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
** Add units from group of a particular unit to selection. |
| 525 |
** |
| 526 |
** @param unit unit belonging to the group to be selected. |
| 527 |
** |
| 528 |
** @return 0 if the unit doesn't belong to a group, |
| 529 |
** or the number of units in the group. |
| 530 |
*/ |
| 531 |
int AddGroupFromUnitToSelection(CUnit *unit) |
| 532 |
{ |
| 533 |
int i; |
| 534 |
int group; |
| 535 |
|
| 536 |
if (!(group = unit->LastGroup)) { // belongs to no group |
| 537 |
return 0; |
| 538 |
} |
| 539 |
|
| 540 |
for (i = 0; i < NumUnits; ++i) { |
| 541 |
if (Units[i]->LastGroup == group && !Units[i]->Removed) { |
| 542 |
SelectUnit(Units[i]); |
| 543 |
if (NumSelected == MaxSelectable) { |
| 544 |
return NumSelected; |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
return NumSelected; |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
** Select units from group of a particular unit. |
| 553 |
** Doesn't change the selection if the group has no unit, |
| 554 |
** or the unit doesn't belong to any group. |
| 555 |
** |
| 556 |
** @param unit unit belonging to the group to be selected. |
| 557 |
** |
| 558 |
** @return 0 if the unit doesn't belong to a group, |
| 559 |
** or the number of units in the group. |
| 560 |
*/ |
| 561 |
int SelectGroupFromUnit(CUnit *unit) |
| 562 |
{ |
| 563 |
if (!unit->LastGroup) { // belongs to no group |
| 564 |
return 0; |
| 565 |
} |
| 566 |
|
| 567 |
UnSelectAll(); |
| 568 |
return AddGroupFromUnitToSelection(unit); |
| 569 |
} |
| 570 |
|
| 571 |
/** |
| 572 |
** Select the units selecteable by rectangle in a local table. |
| 573 |
** Act like a filter: The source table is modified. |
| 574 |
** Return the original table if no unit is found. |
| 575 |
** |
| 576 |
** @param table Input/Output table of units. |
| 577 |
** @param num_units Number of units in input table. |
| 578 |
** |
| 579 |
** @return the number of units found. |
| 580 |
*/ |
| 581 |
static int SelectOrganicUnitsInTable(CUnit **table, int num_units) |
| 582 |
{ |
| 583 |
CUnit *unit; |
| 584 |
int n; |
| 585 |
int i; |
| 586 |
|
| 587 |
for (n = i = 0; i < num_units; ++i) { |
| 588 |
unit = table[i]; |
| 589 |
if (!CanSelectMultipleUnits(unit->Player) || !unit->Type->SelectableByRectangle) { |
| 590 |
continue; |
| 591 |
} |
| 592 |
if (unit->IsUnusable()) { // guess SelectUnits doesn't check this |
| 593 |
continue; |
| 594 |
} |
| 595 |
if (unit->TeamSelected) { // Somebody else onteam has this unit |
| 596 |
continue; |
| 597 |
} |
| 598 |
table[n++] = unit; |
| 599 |
if (n == MaxSelectable) { |
| 600 |
break; |
| 601 |
} |
| 602 |
} |
| 603 |
return n; |
| 604 |
} |
| 605 |
|
| 606 |
/** |
| 607 |
** Selects units from the table whose sprite is at least partially |
| 608 |
** covered by the rectangle. The rectangle is determined by coordinates |
| 609 |
** of its upper left and lower right corner expressed in screen map |
| 610 |
** coordinate system. |
| 611 |
** |
| 612 |
** @param sx0 x-coord of upper left corner of the rectangle |
| 613 |
** @param sy0 y-coord of upper left corner of the rectangle |
| 614 |
** @param sx1 x-coord of lower right corner of the rectangle |
| 615 |
** @param sy1 y-coord of lower right corner of the rectangle |
| 616 |
** @param table table of units |
| 617 |
** @param num_units number of units in table |
| 618 |
** |
| 619 |
** @return number of units found |
| 620 |
*/ |
| 621 |
static int SelectSpritesInsideRectangle (int sx0, int sy0, int sx1, int sy1, |
| 622 |
CUnit **table, int num_units) |
| 623 |
{ |
| 624 |
int n; |
| 625 |
int i; |
| 626 |
|
| 627 |
for (i = n = 0; i < num_units; ++i) { |
| 628 |
int sprite_x; |
| 629 |
int sprite_y; |
| 630 |
CUnit *unit; |
| 631 |
const CUnitType *type; |
| 632 |
|
| 633 |
unit = table[i]; |
| 634 |
type = unit->Type; |
| 635 |
sprite_x = unit->X * TileSizeX + unit->IX; |
| 636 |
sprite_x -= (type->BoxWidth - TileSizeX * type->TileWidth) / 2; |
| 637 |
sprite_x += type->OffsetX; |
| 638 |
sprite_y = unit->Y * TileSizeY + unit->IY; |
| 639 |
sprite_y -= (type->BoxHeight - TileSizeY * type->TileHeight) / 2; |
| 640 |
sprite_y += type->OffsetY; |
| 641 |
if (sprite_x + type->BoxWidth < sx0) { |
| 642 |
continue; |
| 643 |
} |
| 644 |
if (sprite_x > sx1) { |
| 645 |
continue; |
| 646 |
} |
| 647 |
if (sprite_y + type->BoxHeight < sy0) { |
| 648 |
continue; |
| 649 |
} |
| 650 |
if (sprite_y > sy1) { |
| 651 |
continue; |
| 652 |
} |
| 653 |
table[n++] = unit; |
| 654 |
} |
| 655 |
return n; |
| 656 |
} |
| 657 |
|
| 658 |
/** |
| 659 |
** Add the units in the rectangle to the current selection |
| 660 |
** |
| 661 |
** @param x0 X start of selection rectangle in tile coordinates |
| 662 |
** @param y0 Y start of selection rectangle in tile coordinates |
| 663 |
** @param x1 X start of selection rectangle in tile coordinates |
| 664 |
** @param y1 Y start of selection rectangle in tile coordinates |
| 665 |
** |
| 666 |
** @return the _total_ number of units selected. |
| 667 |
*/ |
| 668 |
int AddSelectedUnitsInRectangle(int x0, int y0, int x1, int y1) |
| 669 |
{ |
| 670 |
CUnit *table[UnitMax]; |
| 671 |
int toggle_num; |
| 672 |
int n; |
| 673 |
int i; |
| 674 |
|
| 675 |
// If there is no selected unit yet, do a simple selection. |
| 676 |
if (!NumSelected) { |
| 677 |
return SelectUnitsInRectangle(x0, y0, x1, y1); |
| 678 |
} |
| 679 |
|
| 680 |
// Check if the original selected unit (if it's alone) is ours, |
| 681 |
// and can be selectable by rectangle. |
| 682 |
// In this case, do nothing. |
| 683 |
if (NumSelected == 1 && |
| 684 |
(!CanSelectMultipleUnits(Selected[0]->Player) || |
| 685 |
!Selected[0]->Type->SelectableByRectangle)) { |
| 686 |
return NumSelected; |
| 687 |
} |
| 688 |
|
| 689 |
// If no unit in rectangle area... do nothing |
| 690 |
toggle_num = UnitCache.Select((x0 / TileSizeX) - 2, (y0 / TileSizeY) - 2, |
| 691 |
(x1 / TileSizeX) + 2 + 1, (y1 / TileSizeX) + 2 + 1, table, UnitMax); |
| 692 |
if (!toggle_num) { |
| 693 |
return NumSelected; |
| 694 |
} |
| 695 |
toggle_num = SelectSpritesInsideRectangle(x0, y0, x1, y1, table, toggle_num); |
| 696 |
if (!toggle_num) { |
| 697 |
return NumSelected; |
| 698 |
} |
| 699 |
|
| 700 |
// Now we should only have mobile (organic) units belonging to us, |
| 701 |
// so if there's no such units in the rectangle, do nothing. |
| 702 |
if (!(n = SelectOrganicUnitsInTable(table, toggle_num))) { |
| 703 |
return NumSelected; |
| 704 |
} |
| 705 |
|
| 706 |
for (i = 0; i < n && NumSelected < MaxSelectable; ++i) { |
| 707 |
SelectUnit(table[i]); |
| 708 |
} |
| 709 |
return NumSelected; |
| 710 |
} |
| 711 |
|
| 712 |
/** |
| 713 |
** Select units in a rectangle. |
| 714 |
** Proceed in order in none found: |
| 715 |
** @li select local player mobile units |
| 716 |
** @li select one local player static unit (random) |
| 717 |
** @li select one neutral unit (critter, mine...) |
| 718 |
** @li select one enemy unit (random) |
| 719 |
** |
| 720 |
** @param sx0 X start of selection rectangle in tile coordinates |
| 721 |
** @param sy0 Y start of selection rectangle in tile coordinates |
| 722 |
** @param sx1 X start of selection rectangle in tile coordinates |
| 723 |
** @param sy1 Y start of selection rectangle in tile coordinates |
| 724 |
** |
| 725 |
** @return the number of units found. |
| 726 |
*/ |
| 727 |
int SelectUnitsInRectangle (int sx0, int sy0, int sx1, int sy1) |
| 728 |
{ |
| 729 |
CUnit *unit; |
| 730 |
CUnit *table[UnitMax]; |
| 731 |
CUnitType *type; |
| 732 |
int r; |
| 733 |
int n; |
| 734 |
int i; |
| 735 |
int tx0; |
| 736 |
int ty0; |
| 737 |
int tx1; |
| 738 |
int ty1; |
| 739 |
|
| 740 |
tx0 = sx0 / TileSizeX; |
| 741 |
ty0 = sy0 / TileSizeY; |
| 742 |
tx1 = sx1 / TileSizeX; |
| 743 |
ty1 = sy1 / TileSizeY; |
| 744 |
|
| 745 |
r = UnitCache.Select(tx0 - 2, ty0 - 2, tx1 + 2 + 1, ty1 + 2 + 1, table, UnitMax); |
| 746 |
r = SelectSpritesInsideRectangle(sx0, sy0, sx1, sy1, table, r); |
| 747 |
|
| 748 |
// |
| 749 |
// 1) search for the player units selectable with rectangle |
| 750 |
// |
| 751 |
if ((n = SelectOrganicUnitsInTable(table, r))) { |
| 752 |
ChangeSelectedUnits(table, n); |
| 753 |
return n; |
| 754 |
} |
| 755 |
|
| 756 |
// |
| 757 |
// 2) If no unit found, try a player's unit not selectable by rectangle |
| 758 |
// |
| 759 |
for (i = 0; i < r; ++i) { |
| 760 |
unit = table[i]; |
| 761 |
if (!CanSelectMultipleUnits(unit->Player)) { |
| 762 |
continue; |
| 763 |
} |
| 764 |
// FIXME: Can we get this? |
| 765 |
if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) { |
| 766 |
SelectSingleUnit(unit); |
| 767 |
return 1; |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
// |
| 772 |
// 3) If no unit found, try a resource or a neutral critter |
| 773 |
// |
| 774 |
for (i = 0; i < r; ++i) { |
| 775 |
unit = table[i]; |
| 776 |
// Unit visible FIXME: write function UnitSelectable |
| 777 |
if (!unit->IsVisibleInViewport(UI.SelectedViewport)) { |
| 778 |
continue; |
| 779 |
} |
| 780 |
type = unit->Type; |
| 781 |
// Buildings are visible but not selectable |
| 782 |
if (type->Building && !unit->IsVisibleOnMap(ThisPlayer)) { |
| 783 |
continue; |
| 784 |
} |
| 785 |
if ((type->CanHarvestFrom && !unit->Removed)) { // no built resources. |
| 786 |
SelectSingleUnit(unit); |
| 787 |
return 1; |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
// |
| 792 |
// 4) If no unit found, select an enemy unit (first found) |
| 793 |
// |
| 794 |
for (i = 0; i < r; ++i) { |
| 795 |
unit = table[i]; |
| 796 |
// Unit visible FIXME: write function UnitSelectable |
| 797 |
if (!unit->IsVisibleInViewport(UI.SelectedViewport)) { |
| 798 |
continue; |
| 799 |
} |
| 800 |
// Buildings are visible but not selectable |
| 801 |
if (unit->Type->Building && !unit->IsVisibleOnMap(ThisPlayer)) { |
| 802 |
continue; |
| 803 |
} |
| 804 |
if (!unit->Removed && unit->Orders[0]->Action != UnitActionDie) { |
| 805 |
SelectSingleUnit(unit); |
| 806 |
return 1; |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
return 0; |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
** Select own ground units in a rectangle. |
| 815 |
** |
| 816 |
** @param sx0 X start of selection rectangle in tile coordinates |
| 817 |
** @param sy0 Y start of selection rectangle in tile coordinates |
| 818 |
** @param sx1 X start of selection rectangle in tile coordinates |
| 819 |
** @param sy1 Y start of selection rectangle in tile coordinates |
| 820 |
** |
| 821 |
** @return the number of units found. |
| 822 |
*/ |
| 823 |
int SelectGroundUnitsInRectangle(int sx0, int sy0, int sx1, int sy1) |
| 824 |
{ |
| 825 |
CUnit *unit; |
| 826 |
CUnit *table[UnitMax]; |
| 827 |
int r; |
| 828 |
int n; |
| 829 |
int i; |
| 830 |
int tx0; |
| 831 |
int ty0; |
| 832 |
int tx1; |
| 833 |
int ty1; |
| 834 |
|
| 835 |
tx0 = sx0 / TileSizeX; |
| 836 |
ty0 = sy0 / TileSizeY; |
| 837 |
tx1 = sx1 / TileSizeX; |
| 838 |
ty1 = sy1 / TileSizeY; |
| 839 |
|
| 840 |
r = UnitCache.Select(tx0 - 2, ty0 - 2, tx1 + 2 + 1, ty1 + 2 + 1, table, UnitMax); |
| 841 |
r = SelectSpritesInsideRectangle(sx0, sy0, sx1, sy1, table, r); |
| 842 |
|
| 843 |
for (n = i = 0; i < r; ++i) { |
| 844 |
unit = table[i]; |
| 845 |
if (!CanSelectMultipleUnits(unit->Player) || !unit->Type->SelectableByRectangle) { |
| 846 |
continue; |
| 847 |
} |
| 848 |
if (unit->IsUnusable()) { // guess SelectUnits doesn't check this |
| 849 |
continue; |
| 850 |
} |
| 851 |
if (unit->Type->UnitType == UnitTypeFly) { |
| 852 |
continue; |
| 853 |
} |
| 854 |
if (unit->TeamSelected) { // Somebody else onteam has this unit |
| 855 |
continue; |
| 856 |
} |
| 857 |
table[n++] = unit; |
| 858 |
if (n == MaxSelectable) { |
| 859 |
break; |
| 860 |
} |
| 861 |
} |
| 862 |
if (n) { |
| 863 |
ChangeSelectedUnits(table, n); |
| 864 |
} |
| 865 |
return n; |
| 866 |
} |
| 867 |
|
| 868 |
/** |
| 869 |
** Select own air units in a rectangle. |
| 870 |
** |
| 871 |
** @param sx0 X start of selection rectangle in tile coordinates |
| 872 |
** @param sy0 Y start of selection rectangle in tile coordinates |
| 873 |
** @param sx1 X start of selection rectangle in tile coordinates |
| 874 |
** @param sy1 Y start of selection rectangle in tile coordinates |
| 875 |
** |
| 876 |
** @return the number of units found. |
| 877 |
*/ |
| 878 |
int SelectAirUnitsInRectangle(int sx0, int sy0, int sx1, int sy1) |
| 879 |
{ |
| 880 |
CUnit *unit; |
| 881 |
CUnit *table[UnitMax]; |
| 882 |
int r; |
| 883 |
int n; |
| 884 |
int i; |
| 885 |
int tx0; |
| 886 |
int ty0; |
| 887 |
int tx1; |
| 888 |
int ty1; |
| 889 |
|
| 890 |
tx0 = sx0 / TileSizeX; |
| 891 |
ty0 = sy0 / TileSizeY; |
| 892 |
tx1 = sx1 / TileSizeX; |
| 893 |
ty1 = sy1 / TileSizeY; |
| 894 |
|
| 895 |
r = UnitCache.Select(tx0 - 2, ty0 - 2, tx1 + 2 + 1, ty1 + 2 + 1, table, UnitMax); |
| 896 |
r = SelectSpritesInsideRectangle(sx0, sy0, sx1, sy1, table, r); |
| 897 |
|
| 898 |
for (n = i = 0; i < r; ++i) { |
| 899 |
unit = table[i]; |
| 900 |
if (!CanSelectMultipleUnits(unit->Player) || !unit->Type->SelectableByRectangle) { |
| 901 |
continue; |
| 902 |
} |
| 903 |
if (unit->IsUnusable()) { // guess SelectUnits doesn't check this |
| 904 |
continue; |
| 905 |
} |
| 906 |
if (unit->Type->UnitType != UnitTypeFly) { |
| 907 |
continue; |
| 908 |
} |
| 909 |
if (unit->TeamSelected) { // Somebody else onteam has this unit |
| 910 |
continue; |
| 911 |
} |
| 912 |
table[n++] = unit; |
| 913 |
if (n == MaxSelectable) { |
| 914 |
break; |
| 915 |
} |
| 916 |
} |
| 917 |
if (n) { |
| 918 |
ChangeSelectedUnits(table, n); |
| 919 |
} |
| 920 |
return n; |
| 921 |
} |
| 922 |
|
| 923 |
/** |
| 924 |
** Add the ground units in the rectangle to the current selection |
| 925 |
** |
| 926 |
** @param sx0 X start of selection rectangle in tile coordinates |
| 927 |
** @param sy0 Y start of selection rectangle in tile coordinates |
| 928 |
** @param sx1 X start of selection rectangle in tile coordinates |
| 929 |
** @param sy1 Y start of selection rectangle in tile coordinates |
| 930 |
** |
| 931 |
** @return the number of units found. |
| 932 |
*/ |
| 933 |
int AddSelectedGroundUnitsInRectangle(int sx0, int sy0, int sx1, int sy1) |
| 934 |
{ |
| 935 |
CUnit *unit; |
| 936 |
CUnit *table[UnitMax]; |
| 937 |
int r; |
| 938 |
int n; |
| 939 |
int i; |
| 940 |
int tx0; |
| 941 |
int ty0; |
| 942 |
int tx1; |
| 943 |
int ty1; |
| 944 |
|
| 945 |
// If there is no selected unit yet, do a simple selection. |
| 946 |
if (!NumSelected) { |
| 947 |
return SelectGroundUnitsInRectangle(sx0, sy0, sx1, sy1); |
| 948 |
} |
| 949 |
|
| 950 |
// Check if the original selected unit (if it's alone) is ours, |
| 951 |
// and can be selectable by rectangle. |
| 952 |
// In this case, do nothing. |
| 953 |
if (NumSelected == 1 && |
| 954 |
(!CanSelectMultipleUnits(Selected[0]->Player) || |
| 955 |
!Selected[0]->Type->SelectableByRectangle)) { |
| 956 |
return NumSelected; |
| 957 |
} |
| 958 |
|
| 959 |
tx0 = sx0 / TileSizeX; |
| 960 |
ty0 = sy0 / TileSizeY; |
| 961 |
tx1 = sx1 / TileSizeX; |
| 962 |
ty1 = sy1 / TileSizeY; |
| 963 |
|
| 964 |
r = UnitCache.Select(tx0 - 2, ty0 - 2, tx1 + 2 + 1, ty1 + 2 + 1, table, UnitMax); |
| 965 |
r = SelectSpritesInsideRectangle(sx0, sy0, sx1, sy1, table, r); |
| 966 |
|
| 967 |
for (n = i = 0; i < r; ++i) { |
| 968 |
unit = table[i]; |
| 969 |
if (!CanSelectMultipleUnits(unit->Player) || |
| 970 |
!unit->Type->SelectableByRectangle) { |
| 971 |
continue; |
| 972 |
} |
| 973 |
if (unit->IsUnusable()) { // guess SelectUnits doesn't check this |
| 974 |
continue; |
| 975 |
} |
| 976 |
if (unit->Type->UnitType == UnitTypeFly) { |
| 977 |
continue; |
| 978 |
} |
| 979 |
if (unit->TeamSelected) { // Somebody else onteam has this unit |
| 980 |
continue; |
| 981 |
} |
| 982 |
table[n++] = unit; |
| 983 |
if (n == MaxSelectable) { |
| 984 |
break; |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
// |
| 989 |
// Add the units to selected. |
| 990 |
// |
| 991 |
for (i = 0; i < n && NumSelected < MaxSelectable; ++i) { |
| 992 |
SelectUnit(table[i]); |
| 993 |
} |
| 994 |
return NumSelected; |
| 995 |
} |
| 996 |
|
| 997 |
/** |
| 998 |
** Add the air units in the rectangle to the current selection |
| 999 |
** |
| 1000 |
** @param sx0 X start of selection rectangle in tile coordinates |
| 1001 |
** @param sy0 Y start of selection rectangle in tile coordinates |
| 1002 |
** @param sx1 X start of selection rectangle in tile coordinates |
| 1003 |
** @param sy1 Y start of selection rectangle in tile coordinates |
| 1004 |
** |
| 1005 |
** @return the number of units found. |
| 1006 |
*/ |
| 1007 |
int AddSelectedAirUnitsInRectangle(int sx0, int sy0, int sx1, int sy1) |
| 1008 |
{ |
| 1009 |
CUnit *unit; |
| 1010 |
CUnit *table[UnitMax]; |
| 1011 |
int r; |
| 1012 |
int n; |
| 1013 |
int i; |
| 1014 |
int tx0; |
| 1015 |
int ty0; |
| 1016 |
int tx1; |
| 1017 |
int ty1; |
| 1018 |
|
| 1019 |
// If there is no selected unit yet, do a simple selection. |
| 1020 |
if (!NumSelected) { |
| 1021 |
return SelectAirUnitsInRectangle(sx0, sy0, sx1, sy1); |
| 1022 |
} |
| 1023 |
|
| 1024 |
// Check if the original selected unit (if it's alone) is ours, |
| 1025 |
// and can be selectable by rectangle. |
| 1026 |
// In this case, do nothing. |
| 1027 |
if (NumSelected == 1 && |
| 1028 |
(!CanSelectMultipleUnits(Selected[0]->Player) || |
| 1029 |
!Selected[0]->Type->SelectableByRectangle)) { |
| 1030 |
return NumSelected; |
| 1031 |
} |
| 1032 |
|
| 1033 |
tx0 = sx0 / TileSizeX; |
| 1034 |
ty0 = sy0 / TileSizeY; |
| 1035 |
tx1 = sx1 / TileSizeX; |
| 1036 |
ty1 = sy1 / TileSizeY; |
| 1037 |
|
| 1038 |
r = UnitCache.Select(tx0 - 2, ty0 - 2, tx1 + 2 + 1, ty1 + 2 + 1, table, UnitMax); |
| 1039 |
r = SelectSpritesInsideRectangle(sx0, sy0, sx1, sy1, table, r); |
| 1040 |
|
| 1041 |
for (n = i = 0; i < r; ++i) { |
| 1042 |
unit = table[i]; |
| 1043 |
if (!CanSelectMultipleUnits(unit->Player) || |
| 1044 |
!unit->Type->SelectableByRectangle) { |
| 1045 |
continue; |
| 1046 |
} |
| 1047 |
if (unit->IsUnusable()) { // guess SelectUnits doesn't check this |
| 1048 |
continue; |
| 1049 |
} |
| 1050 |
if (unit->Type->UnitType != UnitTypeFly) { |
| 1051 |
continue; |
| 1052 |
} |
| 1053 |
if (unit->TeamSelected) { // Somebody else onteam has this unit |
| 1054 |
continue; |
| 1055 |
} |
| 1056 |
table[n++] = unit; |
| 1057 |
if (n == MaxSelectable) { |
| 1058 |
break; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
// |
| 1063 |
// Add the units to selected. |
| 1064 |
// |
| 1065 |
for (i = 0; i < n && NumSelected < MaxSelectable; ++i) { |
| 1066 |
SelectUnit(table[i]); |
| 1067 |
} |
| 1068 |
return NumSelected; |
| 1069 |
} |
| 1070 |
|
| 1071 |
/** |
| 1072 |
** Initialize the selection module. |
| 1073 |
*/ |
| 1074 |
void InitSelections(void) |
| 1075 |
{ |
| 1076 |
// This could have been initialized already when loading a game |
| 1077 |
if (!Selected) { |
| 1078 |
Selected = new CUnit *[MaxSelectable]; |
| 1079 |
_Selected = new CUnit *[MaxSelectable]; |
| 1080 |
} |
| 1081 |
for (int i = 0; i < PlayerMax; ++i) { |
| 1082 |
if (!TeamSelected[i]) { |
| 1083 |
TeamSelected[i] = new CUnit *[MaxSelectable]; |
| 1084 |
_TeamSelected[i] = new CUnit *[MaxSelectable]; |
| 1085 |
} |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
/** |
| 1090 |
** Save current selection state. |
| 1091 |
** |
| 1092 |
** @param file Output file. |
| 1093 |
*/ |
| 1094 |
void SaveSelections(CFile *file) |
| 1095 |
{ |
| 1096 |
file->printf("\n--- -----------------------------------------\n"); |
| 1097 |
file->printf("--- MODULE: selection\n\n"); |
| 1098 |
|
| 1099 |
file->printf("SetGroupId(%d)\n", GroupId); |
| 1100 |
file->printf("Selection(%d, {", NumSelected); |
| 1101 |
for (int i = 0; i < NumSelected; ++i) { |
| 1102 |
file->printf("\"%s\", ", UnitReference(Selected[i]).c_str()); |
| 1103 |
} |
| 1104 |
file->printf("})\n"); |
| 1105 |
} |
| 1106 |
|
| 1107 |
/** |
| 1108 |
** Clean up the selection module. |
| 1109 |
*/ |
| 1110 |
void CleanSelections(void) |
| 1111 |
{ |
| 1112 |
GroupId = 0; |
| 1113 |
NumSelected = 0; |
| 1114 |
delete[] Selected; |
| 1115 |
Selected = NULL; |
| 1116 |
delete[] _Selected; |
| 1117 |
_Selected = NULL; |
| 1118 |
|
| 1119 |
for (int i = 0; i < PlayerMax; ++i) { |
| 1120 |
delete[] TeamSelected[i]; |
| 1121 |
TeamSelected[i] = NULL; |
| 1122 |
delete[] _TeamSelected[i]; |
| 1123 |
_TeamSelected[i] = NULL; |
| 1124 |
TeamNumSelected[i] = 0; |
| 1125 |
_TeamNumSelected[i] = 0; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
// ---------------------------------------------------------------------------- |
| 1130 |
|
| 1131 |
/** |
| 1132 |
** Set the current group id. (Needed for load/save) |
| 1133 |
** |
| 1134 |
** @param l Lua state. |
| 1135 |
*/ |
| 1136 |
static int CclSetGroupId(lua_State *l) |
| 1137 |
{ |
| 1138 |
int old; |
| 1139 |
|
| 1140 |
LuaCheckArgs(l, 1); |
| 1141 |
old = GroupId; |
| 1142 |
GroupId = LuaToNumber(l, 1); |
| 1143 |
|
| 1144 |
lua_pushnumber(l, old); |
| 1145 |
return 1; |
| 1146 |
} |
| 1147 |
|
| 1148 |
/** |
| 1149 |
** Define the current selection. |
| 1150 |
** |
| 1151 |
** @param l Lua state. |
| 1152 |
*/ |
| 1153 |
static int CclSelection(lua_State *l) |
| 1154 |
{ |
| 1155 |
int i; |
| 1156 |
int args; |
| 1157 |
int j; |
| 1158 |
|
| 1159 |
LuaCheckArgs(l, 2); |
| 1160 |
LuaCheckTable(l, 2); |
| 1161 |
InitSelections(); |
| 1162 |
NumSelected = LuaToNumber(l, 1); |
| 1163 |
i = 0; |
| 1164 |
args = lua_objlen(l, 2); |
| 1165 |
for (j = 0; j < args; ++j) { |
| 1166 |
const char *str; |
| 1167 |
|
| 1168 |
str = LuaToString(l, 2, j + 1); |
| 1169 |
Selected[i++] = UnitSlots[strtol(str + 1, NULL, 16)]; |
| 1170 |
} |
| 1171 |
|
| 1172 |
return 0; |
| 1173 |
} |
| 1174 |
|
| 1175 |
/** |
| 1176 |
** Register CCL features for selections. |
| 1177 |
*/ |
| 1178 |
void SelectionCclRegister(void) |
| 1179 |
{ |
| 1180 |
lua_register(Lua, "SetGroupId", CclSetGroupId); |
| 1181 |
lua_register(Lua, "Selection", CclSelection); |
| 1182 |
} |
| 1183 |
|
| 1184 |
//@} |