| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name unit.cpp - The units. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2008 by Lutz Sammer and Jimmy Salmon |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
/*---------------------------------------------------------------------------- |
| 32 |
-- Includes |
| 33 |
----------------------------------------------------------------------------*/ |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <limits.h> |
| 39 |
#include <math.h> |
| 40 |
|
| 41 |
#include "stratagus.h" |
| 42 |
|
| 43 |
#include "unit.h" |
| 44 |
#include "unit_manager.h" |
| 45 |
#include "unit_cache.h" |
| 46 |
#include "video.h" |
| 47 |
#include "unitsound.h" |
| 48 |
#include "unittype.h" |
| 49 |
#include "animation.h" |
| 50 |
#include "player.h" |
| 51 |
#include "map.h" |
| 52 |
#include "actions.h" |
| 53 |
#include "sound_server.h" |
| 54 |
#include "missile.h" |
| 55 |
#include "interface.h" |
| 56 |
#include "sound.h" |
| 57 |
#include "ai.h" |
| 58 |
#include "pathfinder.h" |
| 59 |
#include "network.h" |
| 60 |
#include "ui.h" |
| 61 |
#include "script.h" |
| 62 |
#include "editor.h" |
| 63 |
#include "spells.h" |
| 64 |
#include "luacallback.h" |
| 65 |
|
| 66 |
/*---------------------------------------------------------------------------- |
| 67 |
-- Variables |
| 68 |
----------------------------------------------------------------------------*/ |
| 69 |
|
| 70 |
CUnit *Units[MAX_UNIT_SLOTS]; /// Array of used slots |
| 71 |
int NumUnits; /// Number of slots used |
| 72 |
|
| 73 |
bool EnableBuildingCapture; /// Config: capture buildings enabled |
| 74 |
|
| 75 |
static unsigned long HelpMeLastCycle; /// Last cycle HelpMe sound played |
| 76 |
static int HelpMeLastX; /// Last X coordinate HelpMe sound played |
| 77 |
static int HelpMeLastY; /// Last Y coordinate HelpMe sound played |
| 78 |
|
| 79 |
|
| 80 |
/*---------------------------------------------------------------------------- |
| 81 |
-- Functions |
| 82 |
----------------------------------------------------------------------------*/ |
| 83 |
|
| 84 |
static void RemoveUnitFromContainer(CUnit *unit); |
| 85 |
static void UnitRemoveProductionAndStorage(CUnit *unit); |
| 86 |
|
| 87 |
/** |
| 88 |
** Increase a unit's reference count. |
| 89 |
*/ |
| 90 |
void CUnit::RefsIncrease() |
| 91 |
{ |
| 92 |
Assert(Refs && !Destroyed); |
| 93 |
if (!SaveGameLoading) { |
| 94 |
++Refs; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
** Decrease a unit's reference count. |
| 100 |
*/ |
| 101 |
void CUnit::RefsDecrease() |
| 102 |
{ |
| 103 |
Assert(Refs); |
| 104 |
// FIXME: shouldn't have to check this here |
| 105 |
if (!SaveGameLoading) { |
| 106 |
if (Destroyed) { |
| 107 |
if (!--Refs) { |
| 108 |
Release(); |
| 109 |
} |
| 110 |
} else { |
| 111 |
--Refs; |
| 112 |
Assert(Refs); |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
** Release a unit. |
| 119 |
** |
| 120 |
** The unit is only released, if all references are dropped. |
| 121 |
*/ |
| 122 |
void CUnit::Release() |
| 123 |
{ |
| 124 |
CUnit *temp; |
| 125 |
|
| 126 |
Assert(Type); // already free. |
| 127 |
Assert(OrderCount == 1); |
| 128 |
Assert(!Orders[0]->Goal); |
| 129 |
// Must be removed before here |
| 130 |
Assert(Removed); |
| 131 |
|
| 132 |
// |
| 133 |
// First release, remove from lists/tables. |
| 134 |
// |
| 135 |
if (!Destroyed) { |
| 136 |
DebugPrint("First release %d\n" _C_ Slot); |
| 137 |
|
| 138 |
// |
| 139 |
// Are more references remaining? |
| 140 |
// |
| 141 |
Destroyed = 1; // mark as destroyed |
| 142 |
|
| 143 |
if (Container) { |
| 144 |
MapUnmarkUnitSight(this); |
| 145 |
RemoveUnitFromContainer(this); |
| 146 |
} |
| 147 |
|
| 148 |
if (--Refs > 0) { |
| 149 |
return; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
Assert(!Refs); |
| 154 |
|
| 155 |
// |
| 156 |
// No more references remaining, but the network could have an order |
| 157 |
// on the way. We must wait a little time before we could free the |
| 158 |
// memory. |
| 159 |
// |
| 160 |
|
| 161 |
// |
| 162 |
// Remove the unit from the global units table. |
| 163 |
// |
| 164 |
Assert(*UnitSlot == this); |
| 165 |
temp = Units[--NumUnits]; |
| 166 |
temp->UnitSlot = UnitSlot; |
| 167 |
*UnitSlot = temp; |
| 168 |
Units[NumUnits] = NULL; |
| 169 |
|
| 170 |
Type = NULL; |
| 171 |
|
| 172 |
delete[] AutoCastSpell; |
| 173 |
delete[] Variable; |
| 174 |
for (std::vector<COrder *>::iterator order = Orders.begin(); order != Orders.end(); ++order) { |
| 175 |
delete *order; |
| 176 |
} |
| 177 |
Orders.clear(); |
| 178 |
|
| 179 |
UnitManager.ReleaseUnit(this); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
** Initialize the unit slot with default values. |
| 184 |
** |
| 185 |
** @param type Unit-type |
| 186 |
*/ |
| 187 |
void CUnit::Init(CUnitType *type) |
| 188 |
{ |
| 189 |
// Set refs to 1. This is the "I am alive ref", lost in ReleaseUnit. |
| 190 |
Refs = 1; |
| 191 |
|
| 192 |
// |
| 193 |
// Build all unit table |
| 194 |
// |
| 195 |
UnitSlot = &Units[NumUnits]; // back pointer |
| 196 |
Units[NumUnits++] = this; |
| 197 |
|
| 198 |
// |
| 199 |
// Initialise unit structure (must be zero filled!) |
| 200 |
// |
| 201 |
Type = type; |
| 202 |
|
| 203 |
Seen.Frame = UnitNotSeen; // Unit isn't yet seen |
| 204 |
|
| 205 |
Frame = type->StillFrame; |
| 206 |
|
| 207 |
if (UnitTypeVar.NumberVariable) { |
| 208 |
Assert(!Variable); |
| 209 |
Variable = new CVariable[UnitTypeVar.NumberVariable]; |
| 210 |
memcpy(Variable, Type->Variable, |
| 211 |
UnitTypeVar.NumberVariable * sizeof(*Variable)); |
| 212 |
} |
| 213 |
|
| 214 |
// Set a heading for the unit if it Handles Directions |
| 215 |
// Don't set a building heading, as only 1 construction direction |
| 216 |
// is allowed. |
| 217 |
if (type->NumDirections > 1 && type->Sprite && !type->Building) { |
| 218 |
Direction = (MyRand() >> 3) & 0xFF; // random heading |
| 219 |
UnitUpdateHeading(this); |
| 220 |
} |
| 221 |
|
| 222 |
if (type->CanCastSpell) { |
| 223 |
AutoCastSpell = new char[SpellTypeTable.size()]; |
| 224 |
if (Type->AutoCastActive) { |
| 225 |
memcpy(AutoCastSpell, Type->AutoCastActive, SpellTypeTable.size()); |
| 226 |
} else { |
| 227 |
memset(AutoCastSpell, 0, SpellTypeTable.size()); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
Removed = 1; |
| 232 |
|
| 233 |
for (int i = 0; i < MaxCosts; ++i) { |
| 234 |
ResourcesHeld[i] = type->ProductionCosts[i]; |
| 235 |
} |
| 236 |
|
| 237 |
Assert(Orders.empty()); |
| 238 |
|
| 239 |
Orders.push_back(new COrder); |
| 240 |
|
| 241 |
OrderCount = 1; // No orders |
| 242 |
Orders[0]->Action = UnitActionStill; |
| 243 |
Orders[0]->X = Orders[0]->Y = -1; |
| 244 |
Assert(!Orders[0]->Goal); |
| 245 |
NewOrder.Action = UnitActionStill; |
| 246 |
NewOrder.X = NewOrder.Y = -1; |
| 247 |
Assert(!NewOrder.Goal); |
| 248 |
SavedOrder.Action = UnitActionStill; |
| 249 |
SavedOrder.X = SavedOrder.Y = -1; |
| 250 |
Assert(!SavedOrder.Goal); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
** Assigns a unit to a player, adjusting buildings, food and totals |
| 255 |
** |
| 256 |
** @param player player which have the unit. |
| 257 |
*/ |
| 258 |
void CUnit::AssignToPlayer(CPlayer *player) |
| 259 |
{ |
| 260 |
// |
| 261 |
// Build player unit table |
| 262 |
// |
| 263 |
if (!Type->Vanishes && Orders[0]->Action != UnitActionDie) { |
| 264 |
PlayerSlot = player->Units + player->TotalNumUnits++; |
| 265 |
if (!SaveGameLoading) { |
| 266 |
// If unit is dying, it's already been lost by all players |
| 267 |
// don't count again |
| 268 |
if (Type->Building) { |
| 269 |
player->TotalBuildings++; |
| 270 |
} else { |
| 271 |
player->TotalUnits++; |
| 272 |
} |
| 273 |
} |
| 274 |
*PlayerSlot = this; |
| 275 |
|
| 276 |
player->UnitTypesCount[Type->Slot]++; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
// Don't Add the building if it's dieing, used to load a save game |
| 281 |
if (Type->Building && Orders[0]->Action != UnitActionDie) { |
| 282 |
player->NumBuildings++; |
| 283 |
} |
| 284 |
Player = player; |
| 285 |
Stats = &Type->Stats[Player->Index]; |
| 286 |
Colors = &player->UnitColors; |
| 287 |
if (!SaveGameLoading) { |
| 288 |
if (UnitTypeVar.NumberVariable) { |
| 289 |
memcpy(Variable, Stats->Variables, |
| 290 |
UnitTypeVar.NumberVariable * sizeof(*Variable)); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
** Create a new unit. |
| 297 |
** |
| 298 |
** @param type Pointer to unit-type. |
| 299 |
** @param player Pointer to owning player. |
| 300 |
** |
| 301 |
** @return Pointer to created unit. |
| 302 |
*/ |
| 303 |
CUnit *MakeUnit(CUnitType *type, CPlayer *player) |
| 304 |
{ |
| 305 |
CUnit *unit; |
| 306 |
|
| 307 |
// |
| 308 |
// Game unit limit reached. |
| 309 |
// |
| 310 |
if (NumUnits >= UnitMax) { |
| 311 |
DebugPrint("Over all unit limit (%d) reached.\n" _C_ UnitMax); |
| 312 |
return NoUnitP; |
| 313 |
} |
| 314 |
|
| 315 |
unit = UnitManager.AllocUnit(); |
| 316 |
if (unit == NoUnitP) { |
| 317 |
return NoUnitP; |
| 318 |
} |
| 319 |
|
| 320 |
unit->Init(type); |
| 321 |
|
| 322 |
// Only Assign if a Player was specified |
| 323 |
if (player) { |
| 324 |
unit->AssignToPlayer(player); |
| 325 |
} |
| 326 |
|
| 327 |
return unit; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
** (Un)Mark on vision table the Sight of the unit |
| 332 |
** (and units inside for transporter (recursively)) |
| 333 |
** |
| 334 |
** @param unit Unit to (un)mark. |
| 335 |
** @param x X coord of first container of unit. |
| 336 |
** @param y Y coord of first container of unit. |
| 337 |
** @param width Width of the first container of unit. |
| 338 |
** @param height Height of the first container of unit. |
| 339 |
** @param f Function to (un)mark for normal vision. |
| 340 |
*/ |
| 341 |
static void MapMarkUnitSightRec(const CUnit *unit, int x, int y, int width, int height, |
| 342 |
MapMarkerFunc *f) |
| 343 |
{ |
| 344 |
CUnit *unit_inside; |
| 345 |
|
| 346 |
MapSight(unit->Player, x, y, width, height, |
| 347 |
unit->Container ? unit->Container->CurrentSightRange : unit->CurrentSightRange, f); |
| 348 |
|
| 349 |
unit_inside = unit->UnitInside; |
| 350 |
for (int i = unit->InsideCount; i--; unit_inside = unit_inside->NextContained) { |
| 351 |
MapMarkUnitSightRec(unit_inside, x, y, width, height, f); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
** Return the unit not transported, by viewing the container recursively. |
| 357 |
** |
| 358 |
** @param unit unit from where look the first conatiner. |
| 359 |
** |
| 360 |
** @return Container of container of ... of unit. It is not null. |
| 361 |
*/ |
| 362 |
static CUnit *GetFirstContainer(CUnit *unit) |
| 363 |
{ |
| 364 |
while (unit->Container) { |
| 365 |
unit = unit->Container; |
| 366 |
} |
| 367 |
return unit; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
** Mark on vision table the Sight of the unit |
| 372 |
** (and units inside for transporter) |
| 373 |
** |
| 374 |
** @param unit unit to unmark its vision. |
| 375 |
*/ |
| 376 |
void MapMarkUnitSight(CUnit *unit) |
| 377 |
{ |
| 378 |
CUnit *container = GetFirstContainer(unit); |
| 379 |
|
| 380 |
MapMarkUnitSightRec(unit, |
| 381 |
container->X, container->Y, container->Type->TileWidth, container->Type->TileHeight, |
| 382 |
MapMarkTileSight); |
| 383 |
|
| 384 |
// Never mark radar, except if the top unit, and unit is usable |
| 385 |
if (unit == container && !unit->IsUnusable()) { |
| 386 |
if (unit->Stats->Variables[RADAR_INDEX].Value) { |
| 387 |
MapMarkRadar(unit->Player, unit->X, unit->Y, unit->Type->TileWidth, |
| 388 |
unit->Type->TileHeight, unit->Stats->Variables[RADAR_INDEX].Value); |
| 389 |
} |
| 390 |
if (unit->Stats->Variables[RADARJAMMER_INDEX].Value) { |
| 391 |
MapMarkRadarJammer(unit->Player, unit->X, unit->Y, unit->Type->TileWidth, |
| 392 |
unit->Type->TileHeight, unit->Stats->Variables[RADARJAMMER_INDEX].Value); |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
** Unmark on vision table the Sight of the unit |
| 399 |
** (and units inside for transporter) |
| 400 |
** |
| 401 |
** @param unit unit to unmark its vision. |
| 402 |
*/ |
| 403 |
void MapUnmarkUnitSight(CUnit *unit) |
| 404 |
{ |
| 405 |
CUnit *container = GetFirstContainer(unit); |
| 406 |
|
| 407 |
MapMarkUnitSightRec(unit, |
| 408 |
container->X, container->Y, container->Type->TileWidth, container->Type->TileHeight, |
| 409 |
MapUnmarkTileSight); |
| 410 |
|
| 411 |
// Never mark radar, except if the top unit? |
| 412 |
if (unit == container && !unit->IsUnusable()) { |
| 413 |
if (unit->Stats->Variables[RADAR_INDEX].Value) { |
| 414 |
MapUnmarkRadar(unit->Player, unit->X, unit->Y, unit->Type->TileWidth, |
| 415 |
unit->Type->TileHeight, unit->Stats->Variables[RADAR_INDEX].Value); |
| 416 |
} |
| 417 |
if (unit->Stats->Variables[RADARJAMMER_INDEX].Value) { |
| 418 |
MapUnmarkRadarJammer(unit->Player, unit->X, unit->Y, unit->Type->TileWidth, |
| 419 |
unit->Type->TileHeight, unit->Stats->Variables[RADARJAMMER_INDEX].Value); |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
** Update the Unit Current sight range to good value and transported units inside. |
| 426 |
** |
| 427 |
** @param unit unit to update SightRange |
| 428 |
** |
| 429 |
** @internal before using it, MapUnmarkUnitSight(unit) |
| 430 |
** and after MapMarkUnitSight(unit) |
| 431 |
** are often necessary. |
| 432 |
** |
| 433 |
** FIXME @todo manage differently unit inside with option. |
| 434 |
** (no vision, min, host value, own value, bonus value, ...) |
| 435 |
*/ |
| 436 |
void UpdateUnitSightRange(CUnit *unit) |
| 437 |
{ |
| 438 |
CUnit *unit_inside; // iterator on units inside unit. |
| 439 |
int i; // number of units inside to process. |
| 440 |
|
| 441 |
#if 0 // which is the better ? caller check ? |
| 442 |
if (SaveGameLoading) { |
| 443 |
return ; |
| 444 |
} |
| 445 |
#else |
| 446 |
Assert(!SaveGameLoading); |
| 447 |
#endif |
| 448 |
// FIXME : these values must be configurable. |
| 449 |
if (unit->Constructed) { // Units under construction have no sight range. |
| 450 |
unit->CurrentSightRange = 0; |
| 451 |
} else if (!unit->Container) { // proper value. |
| 452 |
unit->CurrentSightRange = unit->Stats->Variables[SIGHTRANGE_INDEX].Max; |
| 453 |
} else { // value of it container. |
| 454 |
unit->CurrentSightRange = unit->Container->CurrentSightRange; |
| 455 |
} |
| 456 |
|
| 457 |
unit_inside = unit->UnitInside; |
| 458 |
for (i = unit->InsideCount; i--; unit_inside = unit_inside->NextContained) { |
| 459 |
UpdateUnitSightRange(unit_inside); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
** Mark the field with the FieldFlags. |
| 465 |
** |
| 466 |
** @param unit unit to mark. |
| 467 |
*/ |
| 468 |
void MarkUnitFieldFlags(const CUnit *unit) |
| 469 |
{ |
| 470 |
unsigned flags = unit->Type->FieldFlags; |
| 471 |
|
| 472 |
for (int h = unit->Type->TileHeight; h--;) { |
| 473 |
for (int w = unit->Type->TileWidth; w--;) { |
| 474 |
Map.Field(unit->X + w, unit->Y + h)->Flags |= flags; |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
** Mark the field with the FieldFlags. |
| 481 |
** |
| 482 |
** @param unit unit to mark. |
| 483 |
*/ |
| 484 |
void UnmarkUnitFieldFlags(const CUnit *unit) |
| 485 |
{ |
| 486 |
unsigned flags = unit->Type->FieldFlags;; |
| 487 |
CUnit *table[UnitMax]; |
| 488 |
|
| 489 |
for (int h = unit->Type->TileHeight; h--;) { |
| 490 |
for (int w = unit->Type->TileWidth; w--;) { |
| 491 |
Map.Field(unit->X + w, unit->Y + h)->Flags &= ~flags; |
| 492 |
|
| 493 |
int n = UnitCache.Select(unit->X + w, unit->Y + h, table, UnitMax); |
| 494 |
while (n--) { |
| 495 |
if (table[n] != unit && table[n]->Orders[0]->Action != UnitActionDie) { |
| 496 |
Map.Field(unit->X + w, unit->Y + h)->Flags |= table[n]->Type->FieldFlags; |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
** Add unit to a container. It only updates linked list stuff. |
| 505 |
** |
| 506 |
** @param host Pointer to container. |
| 507 |
*/ |
| 508 |
void CUnit::AddInContainer(CUnit *host) |
| 509 |
{ |
| 510 |
Assert(Container == NoUnitP); |
| 511 |
Container = host; |
| 512 |
if (host->InsideCount == 0) { |
| 513 |
NextContained = PrevContained = this; |
| 514 |
} else { |
| 515 |
NextContained = host->UnitInside; |
| 516 |
PrevContained = host->UnitInside->PrevContained; |
| 517 |
host->UnitInside->PrevContained->NextContained = this; |
| 518 |
host->UnitInside->PrevContained = this; |
| 519 |
} |
| 520 |
host->UnitInside = this; |
| 521 |
host->InsideCount++; |
| 522 |
} |
| 523 |
|
| 524 |
/** |
| 525 |
** Remove unit from a container. It only updates linked list stuff. |
| 526 |
** |
| 527 |
** @param unit Pointer to unit. |
| 528 |
*/ |
| 529 |
static void RemoveUnitFromContainer(CUnit *unit) |
| 530 |
{ |
| 531 |
CUnit *host; // transporter which contain unit. |
| 532 |
|
| 533 |
host = unit->Container; |
| 534 |
Assert(unit->Container); |
| 535 |
Assert(unit->Container->InsideCount > 0); |
| 536 |
host->InsideCount--; |
| 537 |
unit->NextContained->PrevContained = unit->PrevContained; |
| 538 |
unit->PrevContained->NextContained = unit->NextContained; |
| 539 |
if (host->InsideCount == 0) { |
| 540 |
host->UnitInside = NoUnitP; |
| 541 |
} else { |
| 542 |
if (host->UnitInside == unit) { |
| 543 |
host->UnitInside = unit->NextContained; |
| 544 |
} |
| 545 |
} |
| 546 |
unit->Container = NoUnitP; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
/** |
| 551 |
** Affect Tile coord of a unit (with units inside) to tile (x, y). |
| 552 |
** |
| 553 |
** @param unit unit to move. |
| 554 |
** @param x X map tile position. |
| 555 |
** @param y Y map tile position. |
| 556 |
** |
| 557 |
** @internal before use it, UnitCacheRemove(unit), MapUnmarkUnitSight(unit) |
| 558 |
** and after UnitCacheInsert(unit), MapMarkUnitSight(unit) |
| 559 |
** are often necessary. Check Flag also for Pathfinder. |
| 560 |
*/ |
| 561 |
static void UnitInXY(CUnit *unit, int x, int y) |
| 562 |
{ |
| 563 |
CUnit *unit_inside = unit->UnitInside; |
| 564 |
|
| 565 |
unit->X = x; |
| 566 |
unit->Y = y; |
| 567 |
|
| 568 |
for (int i = unit->InsideCount; i--; unit_inside = unit_inside->NextContained) { |
| 569 |
UnitInXY(unit_inside, x, y); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
/** |
| 574 |
** Move a unit (with units inside) to tile (x, y). |
| 575 |
** (Do stuff with vision, cachelist and pathfinding). |
| 576 |
** |
| 577 |
** @param x X map tile position. |
| 578 |
** @param y Y map tile position. |
| 579 |
** |
| 580 |
*/ |
| 581 |
void CUnit::MoveToXY(int x, int y) |
| 582 |
{ |
| 583 |
MapUnmarkUnitSight(this); |
| 584 |
UnitCache.Remove(this); |
| 585 |
UnmarkUnitFieldFlags(this); |
| 586 |
|
| 587 |
Assert(UnitCanBeAt(this, x, y)); |
| 588 |
// Move the unit. |
| 589 |
UnitInXY(this, x, y); |
| 590 |
|
| 591 |
UnitCache.Insert(this); |
| 592 |
MarkUnitFieldFlags(this); |
| 593 |
// Recalculate the seen count. |
| 594 |
UnitCountSeen(this); |
| 595 |
MapMarkUnitSight(this); |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
** Place unit on map. |
| 600 |
** |
| 601 |
** @param x X map tile position. |
| 602 |
** @param y Y map tile position. |
| 603 |
*/ |
| 604 |
void CUnit::Place(int x, int y) |
| 605 |
{ |
| 606 |
Assert(Removed); |
| 607 |
|
| 608 |
if (Container) { |
| 609 |
MapUnmarkUnitSight(this); |
| 610 |
RemoveUnitFromContainer(this); |
| 611 |
} |
| 612 |
if (!SaveGameLoading) { |
| 613 |
UpdateUnitSightRange(this); |
| 614 |
} |
| 615 |
Removed = 0; |
| 616 |
UnitInXY(this, x, y); |
| 617 |
// Pathfinding info. |
| 618 |
MarkUnitFieldFlags(this); |
| 619 |
// Tha cache list. |
| 620 |
UnitCache.Insert(this); |
| 621 |
// Calculate the seen count. |
| 622 |
UnitCountSeen(this); |
| 623 |
// Vision |
| 624 |
MapMarkUnitSight(this); |
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
** Create new unit and place on map. |
| 629 |
** |
| 630 |
** @param x X map tile position. |
| 631 |
** @param y Y map tile position. |
| 632 |
** @param type Pointer to unit-type. |
| 633 |
** @param player Pointer to owning player. |
| 634 |
** |
| 635 |
** @return Pointer to created unit. |
| 636 |
*/ |
| 637 |
CUnit *MakeUnitAndPlace(int x, int y, CUnitType *type, CPlayer *player) |
| 638 |
{ |
| 639 |
CUnit *unit = MakeUnit(type, player); |
| 640 |
|
| 641 |
if (unit != NoUnitP) { |
| 642 |
unit->Place(x, y); |
| 643 |
} |
| 644 |
|
| 645 |
return unit; |
| 646 |
} |
| 647 |
|
| 648 |
/** |
| 649 |
** Remove unit from map. |
| 650 |
** |
| 651 |
** Update selection. |
| 652 |
** Update panels. |
| 653 |
** Update map. |
| 654 |
** |
| 655 |
** @param host Pointer to housing unit. |
| 656 |
*/ |
| 657 |
void CUnit::Remove(CUnit *host) |
| 658 |
{ |
| 659 |
if (Removed) { // could happen! |
| 660 |
// If unit is removed (inside) and building is destroyed. |
| 661 |
DebugPrint("unit '%s(%d)' already removed\n" _C_ Type->Ident.c_str() _C_ Slot); |
| 662 |
return; |
| 663 |
} |
| 664 |
|
| 665 |
UnitCache.Remove(this); |
| 666 |
MapUnmarkUnitSight(this); |
| 667 |
UnmarkUnitFieldFlags(this); |
| 668 |
|
| 669 |
if (host) { |
| 670 |
AddInContainer(host); |
| 671 |
UpdateUnitSightRange(this); |
| 672 |
UnitInXY(this, host->X, host->Y); |
| 673 |
MapMarkUnitSight(this); |
| 674 |
} |
| 675 |
|
| 676 |
Removed = 1; |
| 677 |
|
| 678 |
// Remove unit from the current selection |
| 679 |
if (Selected) { |
| 680 |
if (NumSelected == 1) { // Remove building cursor |
| 681 |
CancelBuildingMode(); |
| 682 |
} |
| 683 |
UnSelectUnit(this); |
| 684 |
SelectionChanged(); |
| 685 |
} |
| 686 |
// Remove unit from team selections |
| 687 |
if (!Selected && TeamSelected) { |
| 688 |
UnSelectUnit(this); |
| 689 |
} |
| 690 |
|
| 691 |
// Unit is seen as under cursor |
| 692 |
if (this == UnitUnderCursor) { |
| 693 |
UnitUnderCursor = NULL; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
** Update information for lost units. |
| 699 |
** |
| 700 |
** @param unit Pointer to unit. |
| 701 |
** |
| 702 |
** @note Also called by ChangeUnitOwner |
| 703 |
*/ |
| 704 |
void UnitLost(CUnit *unit) |
| 705 |
{ |
| 706 |
CUnit *temp; |
| 707 |
CBuildRestrictionOnTop *b; |
| 708 |
const CUnitType *type; |
| 709 |
CPlayer *player; |
| 710 |
|
| 711 |
player = unit->Player; |
| 712 |
Assert(player); // Next code didn't support no player! |
| 713 |
|
| 714 |
// |
| 715 |
// Call back to AI, for killed or lost units. |
| 716 |
// |
| 717 |
if (player && player->AiEnabled) { |
| 718 |
AiUnitKilled(unit); |
| 719 |
} |
| 720 |
|
| 721 |
// |
| 722 |
// Remove unit from its groups |
| 723 |
// |
| 724 |
if (unit->GroupId) { |
| 725 |
RemoveUnitFromGroups(unit); |
| 726 |
} |
| 727 |
|
| 728 |
// |
| 729 |
// Remove the unit from the player's units table. |
| 730 |
// |
| 731 |
type = unit->Type; |
| 732 |
if (player && !type->Vanishes) { |
| 733 |
Assert(*unit->PlayerSlot == unit); |
| 734 |
temp = player->Units[--player->TotalNumUnits]; |
| 735 |
temp->PlayerSlot = unit->PlayerSlot; |
| 736 |
*unit->PlayerSlot = temp; |
| 737 |
player->Units[player->TotalNumUnits] = NULL; |
| 738 |
|
| 739 |
if (unit->Type->Building) { |
| 740 |
player->NumBuildings--; |
| 741 |
} |
| 742 |
|
| 743 |
if (unit->Orders[0]->Action != UnitActionBuilt) { |
| 744 |
player->UnitTypesCount[type->Slot]--; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
DebugPrint("Lost %s(%d)\n" _C_ unit->Type->Ident.c_str() _C_ UnitNumber(unit)); |
| 749 |
|
| 750 |
// Destroy resource-platform, must re-make resource patch. |
| 751 |
if ((b = OnTopDetails(unit, NULL)) != NULL) { |
| 752 |
if (b->ReplaceOnDie && (unit->Type->CanHarvestFrom && UnitHoldsResources(unit))) { |
| 753 |
temp = MakeUnitAndPlace(unit->X, unit->Y, b->Parent, &Players[PlayerNumNeutral]); |
| 754 |
if (temp == NoUnitP) { |
| 755 |
DebugPrint("Unable to allocate Unit"); |
| 756 |
} else { |
| 757 |
memcpy(temp->ResourcesHeld, unit->ResourcesHeld, sizeof(temp->ResourcesHeld)); |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
Assert(player->NumBuildings <= UnitMax); |
| 762 |
Assert(player->TotalNumUnits <= UnitMax); |
| 763 |
Assert(player->UnitTypesCount[type->Slot] <= UnitMax); |
| 764 |
} |
| 765 |
|
| 766 |
/** |
| 767 |
** Removes all orders from a unit. |
| 768 |
** |
| 769 |
** @param unit The unit that will have all its orders cleared |
| 770 |
*/ |
| 771 |
void UnitClearOrders(CUnit *unit) |
| 772 |
{ |
| 773 |
// |
| 774 |
// Release all references of the unit. |
| 775 |
// |
| 776 |
for (int i = unit->OrderCount; i-- > 0;) { |
| 777 |
if (unit->Orders[i]->Goal) { |
| 778 |
unit->Orders[i]->Goal->RefsDecrease(); |
| 779 |
unit->Orders[i]->Goal = NoUnitP; |
| 780 |
} |
| 781 |
if (i != 0) { |
| 782 |
COrder *order = unit->Orders.back(); |
| 783 |
delete order; |
| 784 |
unit->Orders.pop_back(); |
| 785 |
} |
| 786 |
} |
| 787 |
unit->OrderCount = 1; |
| 788 |
if (unit->NewOrder.Goal) { |
| 789 |
unit->NewOrder.Goal->RefsDecrease(); |
| 790 |
unit->NewOrder.Goal = NoUnitP; |
| 791 |
} |
| 792 |
if (unit->SavedOrder.Goal) { |
| 793 |
unit->SavedOrder.Goal->RefsDecrease(); |
| 794 |
unit->SavedOrder.Goal = NoUnitP; |
| 795 |
} |
| 796 |
unit->Orders[0]->Action = UnitActionStill; |
| 797 |
unit->SubAction = unit->State = 0; |
| 798 |
} |
| 799 |
|
| 800 |
/** |
| 801 |
** Update for new unit. Food and income ... |
| 802 |
** |
| 803 |
** @param unit New unit pointer. |
| 804 |
** @param upgrade True unit was upgraded. |
| 805 |
*/ |
| 806 |
void UpdateForNewUnit(const CUnit *unit, int upgrade) |
| 807 |
{ |
| 808 |
const CUnitType *type = unit->Type; |
| 809 |
CPlayer *player = unit->Player; |
| 810 |
|
| 811 |
// Update resources. Until the unit has been fully built, it |
| 812 |
// does not produce any resources or provide storage capacity. |
| 813 |
// There is a corresponding check in UnitRemoveProductionAndStorage, |
| 814 |
// which subtracts them back out. |
| 815 |
if (unit->Orders[0]->Action != UnitActionBuilt) { |
| 816 |
for (int u = 0; u < MaxCosts; ++u) { |
| 817 |
player->ProductionRate[u] += type->ProductionRate[u] * unit->ProductionEfficiency / 100; |
| 818 |
player->StorageCapacity[u] += type->StorageCapacity[u]; |
| 819 |
} |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
/** |
| 824 |
** Find nearest point of unit. |
| 825 |
** |
| 826 |
** @param unit Pointer to unit. |
| 827 |
** @param tx X tile map postion. |
| 828 |
** @param ty Y tile map postion. |
| 829 |
** @param dx Out: nearest point X tile map postion to (tx,ty). |
| 830 |
** @param dy Out: nearest point Y tile map postion to (tx,ty). |
| 831 |
*/ |
| 832 |
void NearestOfUnit(const CUnit *unit, int tx, int ty, int *dx, int *dy) |
| 833 |
{ |
| 834 |
int x = unit->X; |
| 835 |
int y = unit->Y; |
| 836 |
|
| 837 |
if (tx >= x + unit->Type->TileWidth) { |
| 838 |
*dx = x + unit->Type->TileWidth - 1; |
| 839 |
} else if (tx < x) { |
| 840 |
*dx = x; |
| 841 |
} else { |
| 842 |
*dx = tx; |
| 843 |
} |
| 844 |
if (ty >= y + unit->Type->TileHeight) { |
| 845 |
*dy = y + unit->Type->TileHeight - 1; |
| 846 |
} else if (ty < y) { |
| 847 |
*dy = y; |
| 848 |
} else { |
| 849 |
*dy = ty; |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
/** |
| 854 |
** Copy the unit look in Seen variables. This should be called when |
| 855 |
** buildings go under fog of war for ThisPlayer. |
| 856 |
** |
| 857 |
** @param unit The unit to work on |
| 858 |
*/ |
| 859 |
static void UnitFillSeenValues(CUnit *unit) |
| 860 |
{ |
| 861 |
// Seen values are undefined for visible units. |
| 862 |
unit->Seen.Y = unit->Y; |
| 863 |
unit->Seen.X = unit->X; |
| 864 |
unit->Seen.IY = unit->IY; |
| 865 |
unit->Seen.IX = unit->IX; |
| 866 |
unit->Seen.Frame = unit->Frame; |
| 867 |
unit->Seen.State = (unit->Orders[0]->Action == UnitActionBuilt); |
| 868 |
if (unit->Orders[0]->Action == UnitActionDie) { |
| 869 |
unit->Seen.State = 3; |
| 870 |
} |
| 871 |
unit->Seen.Type = unit->Type; |
| 872 |
unit->Seen.Constructed = unit->Constructed; |
| 873 |
if (unit->Orders[0]->Action == UnitActionBuilt) { |
| 874 |
unit->Seen.CFrame = unit->Data.Built.Frame; |
| 875 |
} else { |
| 876 |
unit->Seen.CFrame = NULL; |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
/** |
| 881 |
** This function should get called when a unit goes under fog of war. |
| 882 |
** |
| 883 |
** @param unit The unit that goes under fog. |
| 884 |
** @param player The player the unit goes out of fog for. |
| 885 |
*/ |
| 886 |
void UnitGoesUnderFog(CUnit *unit, const CPlayer *player) |
| 887 |
{ |
| 888 |
if (unit->Type->VisibleUnderFog) { |
| 889 |
if (player->Type == PlayerPerson && !unit->Destroyed) { |
| 890 |
unit->RefsIncrease(); |
| 891 |
} |
| 892 |
// |
| 893 |
// Icky yucky icky Seen.Destroyed trickery. |
| 894 |
// We track for each player if he's seen the unit as destroyed. |
| 895 |
// Remember, a unit is marked Destroyed when it's gone as in |
| 896 |
// completely gone, the corpses vanishes. In that case the unit |
| 897 |
// only survives since some players did NOT see the unit destroyed. |
| 898 |
// Keeping trackof that is hard, mostly due to complex shared vision |
| 899 |
// configurations. |
| 900 |
// A unit does NOT get a reference when it goes under fog if it's |
| 901 |
// Destroyed. Furthermore, it shouldn't lose a reference if it was |
| 902 |
// Seen destroyed. That only happend with complex shared vision, and |
| 903 |
// it's sort of the whole point of this tracking. |
| 904 |
// |
| 905 |
if (unit->Destroyed) { |
| 906 |
unit->Seen.Destroyed |= (1 << player->Index); |
| 907 |
} |
| 908 |
if (player == ThisPlayer) { |
| 909 |
UnitFillSeenValues(unit); |
| 910 |
} |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
/** |
| 915 |
** This function should get called when a unit goes out of fog of war. |
| 916 |
** |
| 917 |
** @param unit The unit that goes out of fog. |
| 918 |
** @param player The player the unit goes out of fog for. |
| 919 |
** |
| 920 |
** @note For units that are visible under fog (mostly buildings) |
| 921 |
** we use reference counts, from the players that know about |
| 922 |
** the building. When an building goes under fog it gets a refs |
| 923 |
** increase, and when it shows up it gets a decrease. It must |
| 924 |
** not get an decrease the first time it's seen, so we have to |
| 925 |
** keep track of what player saw what units, with SeenByPlayer. |
| 926 |
*/ |
| 927 |
void UnitGoesOutOfFog(CUnit *unit, const CPlayer *player) |
| 928 |
{ |
| 929 |
if (unit->Type->VisibleUnderFog) { |
| 930 |
if (unit->Seen.ByPlayer & (1 << (player->Index))) { |
| 931 |
if ((player->Type == PlayerPerson) && |
| 932 |
(!( unit->Seen.Destroyed & (1 << player->Index) )) ) { |
| 933 |
unit->RefsDecrease(); |
| 934 |
} |
| 935 |
} else { |
| 936 |
unit->Seen.ByPlayer |= (1 << (player->Index)); |
| 937 |
} |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
/** |
| 942 |
** Mark all units on a tile as now visible. |
| 943 |
** |
| 944 |
** @param player The player this is for. |
| 945 |
** @param x x location to check |
| 946 |
** @param y y location to check |
| 947 |
*/ |
| 948 |
void UnitsOnTileMarkSeen(const CPlayer *player, int x, int y) |
| 949 |
{ |
| 950 |
int n; |
| 951 |
CUnit *units[UnitMax]; |
| 952 |
|
| 953 |
n = UnitCache.Select(x, y, units, UnitMax); |
| 954 |
while (n) { |
| 955 |
CUnit *unit = units[--n]; |
| 956 |
// |
| 957 |
// If the unit goes out of fog, this can happen for any player that |
| 958 |
// this player shares vision with, and can't YET see the unit. |
| 959 |
// It will be able to see the unit after the Unit->VisCount ++ |
| 960 |
// |
| 961 |
for (int p = 0; p < PlayerMax; ++p) { |
| 962 |
if (player->IsBothSharedVision(&Players[p]) || (p == player->Index)) { |
| 963 |
if (!unit->IsVisible(Players + p)) { |
| 964 |
UnitGoesOutOfFog(unit, Players + p); |
| 965 |
} |
| 966 |
} |
| 967 |
} |
| 968 |
unit->VisCount[player->Index]++; |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
/** |
| 973 |
** This function unmarks units on x, y as seen. It uses a reference count. |
| 974 |
** |
| 975 |
** @param player The player to mark for. |
| 976 |
** @param x x location to check if building is on, and mark as seen |
| 977 |
** @param y y location to check if building is on, and mark as seen |
| 978 |
*/ |
| 979 |
void UnitsOnTileUnmarkSeen(const CPlayer *player, int x, int y) |
| 980 |
{ |
| 981 |
int n; |
| 982 |
CUnit *units[UnitMax]; |
| 983 |
|
| 984 |
n = UnitCache.Select(x, y, units, UnitMax); |
| 985 |
while (n) { |
| 986 |
CUnit *unit = units[--n]; |
| 987 |
Assert(unit->X <= x && unit->X + unit->Type->TileWidth - 1 >= x && |
| 988 |
unit->Y <= y && unit->Y + unit->Type->TileHeight - 1 >= y); |
| 989 |
|
| 990 |
Assert(unit->VisCount[player->Index]); |
| 991 |
unit->VisCount[player->Index]--; |
| 992 |
// |
| 993 |
// If the unit goes under of fog, this can happen for any player that |
| 994 |
// this player shares vision to. First of all, before unmarking, |
| 995 |
// every player that this player shares vision to can see the unit. |
| 996 |
// Now we have to check who can't see the unit anymore. |
| 997 |
// |
| 998 |
if (!unit->VisCount[player->Index]) { |
| 999 |
for (int p = 0; p < PlayerMax; ++p) { |
| 1000 |
if (player->IsBothSharedVision(&Players[p]) || p == player->Index) { |
| 1001 |
if (!unit->IsVisible(Players + p)) { |
| 1002 |
UnitGoesUnderFog(unit, Players + p); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
} |
| 1006 |
} |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
/** |
| 1011 |
** Recalculates a units visiblity count. This happens really often, |
| 1012 |
** Like every time a unit moves. It's really fast though, since we |
| 1013 |
** have per-tile counts. |
| 1014 |
** |
| 1015 |
** @param unit pointer to the unit to check if seen |
| 1016 |
*/ |
| 1017 |
void UnitCountSeen(CUnit *unit) |
| 1018 |
{ |
| 1019 |
int x; |
| 1020 |
int y; |
| 1021 |
int p; |
| 1022 |
int oldv[PlayerMax]; |
| 1023 |
int newv; |
| 1024 |
|
| 1025 |
// FIXME: optimize, only work on certain players? |
| 1026 |
// This is for instance good for updating shared vision... |
| 1027 |
|
| 1028 |
// |
| 1029 |
// Store old values in oldv[p]. This store if the player could see the |
| 1030 |
// unit before this calc. |
| 1031 |
// |
| 1032 |
for (p = 0; p < PlayerMax; ++p) { |
| 1033 |
if (Players[p].Type != PlayerNobody) { |
| 1034 |
oldv[p] = unit->IsVisible(&Players[p]); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
// Calculate new VisCount values. |
| 1039 |
for (p = 0; p < PlayerMax; ++p) { |
| 1040 |
if (Players[p].Type != PlayerNobody) { |
| 1041 |
newv = 0; |
| 1042 |
for (x = 0; x < unit->Type->TileWidth; ++x) { |
| 1043 |
for (y = 0; y < unit->Type->TileHeight; ++y) { |
| 1044 |
// Icky ugly code trick. With NoFogOfWar we have to be > 0; |
| 1045 |
if (Map.Field(unit->X + x, unit->Y + y)->Visible[p] > |
| 1046 |
1 - (Map.NoFogOfWar ? 1 : 0)) { |
| 1047 |
newv++; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
} |
| 1051 |
unit->VisCount[p] = newv; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
|
| 1055 |
// |
| 1056 |
// Now here comes the tricky part. We have to go in and out of fog |
| 1057 |
// for players. Hopefully this works with shared vision just great. |
| 1058 |
// |
| 1059 |
for (p = 0; p < PlayerMax; ++p) { |
| 1060 |
if (Players[p].Type != PlayerNobody) { |
| 1061 |
newv = unit->IsVisible(Players + p); |
| 1062 |
if (!oldv[p] && newv) { |
| 1063 |
UnitGoesOutOfFog(unit, Players + p); |
| 1064 |
// Might have revealed a destroyed unit which caused it to |
| 1065 |
// be released |
| 1066 |
if (!unit->Type) { |
| 1067 |
break; |
| 1068 |
} |
| 1069 |
} |
| 1070 |
if (oldv[p] && !newv) { |
| 1071 |
UnitGoesUnderFog(unit, Players + p); |
| 1072 |
} |
| 1073 |
} |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
/** |
| 1078 |
** Returns true, if the unit is visible. It check the Viscount of |
| 1079 |
** the player and everyone who shares vision with him. |
| 1080 |
** |
| 1081 |
** @note This understands shared vision, and should be used all around. |
| 1082 |
** |
| 1083 |
** @param player The player to check. |
| 1084 |
*/ |
| 1085 |
bool CUnit::IsVisible(const CPlayer *player) const |
| 1086 |
{ |
| 1087 |
if (VisCount[player->Index]) { |
| 1088 |
return true; |
| 1089 |
} |
| 1090 |
for (int p = 0; p < PlayerMax; ++p) { |
| 1091 |
if (player->IsBothSharedVision(&Players[p])) { |
| 1092 |
if (VisCount[p]) { |
| 1093 |
return true; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
} |
| 1097 |
return false; |
| 1098 |
} |
| 1099 |
|
| 1100 |
/** |
| 1101 |
** Returns true, if unit is visible as an action goal for a player |
| 1102 |
** on the map. |
| 1103 |
** |
| 1104 |
** @param player Player to check for. |
| 1105 |
** |
| 1106 |
** @return True if visible, false otherwise. |
| 1107 |
*/ |
| 1108 |
bool CUnit::IsVisibleAsGoal(const CPlayer *player) const |
| 1109 |
{ |
| 1110 |
if (IsVisible(player) || player->Type == PlayerComputer || |
| 1111 |
UnitVisibleOnRadar(player, this)) { |
| 1112 |
return !Removed && !Destroyed && Orders[0]->Action != UnitActionDie; |
| 1113 |
} else { |
| 1114 |
return Type->VisibleUnderFog && |
| 1115 |
(Seen.ByPlayer & (1 << player->Index)) && |
| 1116 |
!(Seen.Destroyed & (1 << player->Index)); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
/** |
| 1121 |
** Returns true if unit is alive and on the map. |
| 1122 |
** Another unit can interact only with alive map units. |
| 1123 |
** |
| 1124 |
** @return True if alive, false otherwise. |
| 1125 |
*/ |
| 1126 |
bool CUnit::IsAliveOnMap() const |
| 1127 |
{ |
| 1128 |
return !Removed && !Destroyed && Orders[0]->Action != UnitActionDie; |
| 1129 |
} |
| 1130 |
|
| 1131 |
/** |
| 1132 |
** Returns true, if unit is visible for this player on the map. |
| 1133 |
** The unit has to be out of fog of war and alive |
| 1134 |
** |
| 1135 |
** @param player Player to check for. |
| 1136 |
** |
| 1137 |
** @return True if visible, false otherwise. |
| 1138 |
*/ |
| 1139 |
bool CUnit::IsVisibleOnMap(const CPlayer *player) const |
| 1140 |
{ |
| 1141 |
return !Removed && !Destroyed && |
| 1142 |
Orders[0]->Action != UnitActionDie && IsVisible(player); |
| 1143 |
} |
| 1144 |
|
| 1145 |
/** |
| 1146 |
** Returns true, if unit is shown on minimap. |
| 1147 |
** |
| 1148 |
** @warning This is for ::ThisPlayer only. |
| 1149 |
** @todo radar support |
| 1150 |
** |
| 1151 |
** @return True if visible, false otherwise. |
| 1152 |
*/ |
| 1153 |
bool CUnit::IsVisibleOnMinimap() const |
| 1154 |
{ |
| 1155 |
if (IsVisible(ThisPlayer) || ReplayRevealMap || |
| 1156 |
UnitVisibleOnRadar(ThisPlayer, this)) { |
| 1157 |
return !Removed && !Destroyed && (Orders[0]->Action != UnitActionDie); |
| 1158 |
} else { |
| 1159 |
if (!Type->VisibleUnderFog) { |
| 1160 |
return false; |
| 1161 |
} |
| 1162 |
return (Seen.ByPlayer & (1 << ThisPlayer->Index)) && |
| 1163 |
Seen.State != 3 && !(Seen.Destroyed & (1 << ThisPlayer->Index)); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
/** |
| 1168 |
** Returns true, if unit is visible in viewport. |
| 1169 |
** |
| 1170 |
** @warning This is only true for ::ThisPlayer |
| 1171 |
** |
| 1172 |
** @param vp Viewport pointer. |
| 1173 |
** |
| 1174 |
** @return True if visible, false otherwise. |
| 1175 |
*/ |
| 1176 |
bool CUnit::IsVisibleInViewport(const CViewport *vp) const |
| 1177 |
{ |
| 1178 |
// |
| 1179 |
// Check if the graphic is inside the viewport. |
| 1180 |
// |
| 1181 |
int x = X * TileSizeX + IX - (Type->Width - Type->TileWidth * TileSizeX) / 2 + Type->OffsetX; |
| 1182 |
int y = Y * TileSizeY + IY - (Type->Height - Type->TileHeight * TileSizeY) / 2 + Type->OffsetY; |
| 1183 |
|
| 1184 |
if (x + Type->Width < vp->MapX * TileSizeX + vp->OffsetX || |
| 1185 |
x > vp->MapX * TileSizeX + vp->OffsetX + (vp->EndX - vp->X) || |
| 1186 |
y + Type->Height < vp->MapY * TileSizeY + vp->OffsetY || |
| 1187 |
y > vp->MapY * TileSizeY + vp->OffsetY + (vp->EndY - vp->Y)) |
| 1188 |
{ |
| 1189 |
return false; |
| 1190 |
} |
| 1191 |
|
| 1192 |
Assert(ThisPlayer); |
| 1193 |
if (!ThisPlayer) { |
| 1194 |
return false; |
| 1195 |
} |
| 1196 |
|
| 1197 |
if (IsVisible(ThisPlayer) || ReplayRevealMap) { |
| 1198 |
return !Destroyed; |
| 1199 |
} else { |
| 1200 |
// Unit has to be 'discovered' |
| 1201 |
// Destroyed units ARE visible under fog of war, if we haven't seen them like that. |
| 1202 |
if (!Destroyed || !(Seen.Destroyed & (1 << ThisPlayer->Index))) { |
| 1203 |
return (Type->VisibleUnderFog && (Seen.ByPlayer & (1 << ThisPlayer->Index))); |
| 1204 |
} else { |
| 1205 |
return false; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
/** |
| 1211 |
** Returns true, if unit is visible on current map view (any viewport). |
| 1212 |
** |
| 1213 |
** @return True if visible, false otherwise. |
| 1214 |
*/ |
| 1215 |
bool CUnit::IsVisibleOnScreen() const |
| 1216 |
{ |
| 1217 |
for (CViewport *vp = UI.Viewports; vp < UI.Viewports + UI.NumViewports; ++vp) { |
| 1218 |
if (IsVisibleInViewport(vp)) { |
| 1219 |
return true; |
| 1220 |
} |
| 1221 |
} |
| 1222 |
return false; |
| 1223 |
} |
| 1224 |
|
| 1225 |
/** |
| 1226 |
** Get area of map tiles covered by unit, including its displacement. |
| 1227 |
** |
| 1228 |
** @param sx Out: Top left X tile map postion. |
| 1229 |
** @param sy Out: Top left Y tile map postion. |
| 1230 |
** @param ex Out: Bottom right X tile map postion. |
| 1231 |
** @param ey Out: Bottom right Y tile map postion. |
| 1232 |
** |
| 1233 |
** @return sx,sy,ex,ey defining area in Map |
| 1234 |
*/ |
| 1235 |
void CUnit::GetMapArea(int *sx, int *sy, int *ex, int *ey) const |
| 1236 |
{ |
| 1237 |
*sx = X - (IX < 0); |
| 1238 |
*ex = *sx + Type->TileWidth - !IX; |
| 1239 |
*sy = Y - (IY < 0); |
| 1240 |
*ey = *sy + Type->TileHeight - !IY; |
| 1241 |
} |
| 1242 |
|
| 1243 |
/** |
| 1244 |
** Change the unit's owner |
| 1245 |
** |
| 1246 |
** @param newplayer New owning player. |
| 1247 |
*/ |
| 1248 |
void CUnit::ChangeOwner(CPlayer *newplayer) |
| 1249 |
{ |
| 1250 |
int i; |
| 1251 |
CUnit *uins; |
| 1252 |
CPlayer *oldplayer; |
| 1253 |
int requestedCosts[MaxCosts]; |
| 1254 |
bool hasRequestedCosts = false; |
| 1255 |
bool isProducingResources = false; |
| 1256 |
|
| 1257 |
oldplayer = Player; |
| 1258 |
|
| 1259 |
// This shouldn't happen |
| 1260 |
if (oldplayer == newplayer) { |
| 1261 |
DebugPrint("Change the unit owner to the same player???\n"); |
| 1262 |
return; |
| 1263 |
} |
| 1264 |
|
| 1265 |
// Rescue all units in buildings/transporters. |
| 1266 |
uins = UnitInside; |
| 1267 |
for (i = InsideCount; i; --i, uins = uins->NextContained) { |
| 1268 |
uins->ChangeOwner(newplayer); |
| 1269 |
} |
| 1270 |
|
| 1271 |
// Stop the unit from consuming resources, and remember how |
| 1272 |
// much it did. |
| 1273 |
if (oldplayer->UnitsConsumingResourcesRequested.count(this) != 0) { |
| 1274 |
memcpy(requestedCosts, |
| 1275 |
oldplayer->UnitsConsumingResourcesRequested[this], |
| 1276 |
MaxCosts * sizeof(int)); |
| 1277 |
hasRequestedCosts = true; |
| 1278 |
oldplayer->RemoveFromUnitsConsumingResources(this); |
| 1279 |
} |
| 1280 |
|
| 1281 |
// Likewise, disconnect any resource production from the old |
| 1282 |
// owner. Data.Harvest.CurrentProduction is unfortunately |
| 1283 |
// uninitialized until SubAction SUB_START_GATHERING, and |
| 1284 |
// then zero until SUB_GATHER_RESOURCE. |
| 1285 |
UnitRemoveProductionAndStorage(this); |
| 1286 |
if (Orders[0]->Action == UnitActionResource |
| 1287 |
&& SubAction == /* SUB_GATHER_RESOURCE */ 60) { |
| 1288 |
isProducingResources = true; |
| 1289 |
for (i = 0; i < MaxCosts; ++i) { |
| 1290 |
oldplayer->ProductionRate[i] -= Data.Harvest.CurrentProduction[i]; |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
// |
| 1295 |
// Must change food/gold and other. |
| 1296 |
// |
| 1297 |
UnitLost(this); |
| 1298 |
|
| 1299 |
// |
| 1300 |
// Now the new side! |
| 1301 |
// |
| 1302 |
|
| 1303 |
// Insert into new player table. |
| 1304 |
|
| 1305 |
PlayerSlot = newplayer->Units + newplayer->TotalNumUnits++; |
| 1306 |
if (Type->Building) { |
| 1307 |
newplayer->TotalBuildings++; |
| 1308 |
} |
| 1309 |
else { |
| 1310 |
newplayer->TotalUnits++; |
| 1311 |
} |
| 1312 |
*PlayerSlot = this; |
| 1313 |
|
| 1314 |
MapUnmarkUnitSight(this); |
| 1315 |
Player = newplayer; |
| 1316 |
Stats = &Type->Stats[newplayer->Index]; |
| 1317 |
UpdateUnitSightRange(this); |
| 1318 |
MapMarkUnitSight(this); |
| 1319 |
|
| 1320 |
// If the unit was trying to consume resources from the old |
| 1321 |
// player, then request similar resources from the new player. |
| 1322 |
// The resources actually granted will be computed by |
| 1323 |
// CalculateCosts called by PlayersEachCycle. |
| 1324 |
if (hasRequestedCosts) { |
| 1325 |
newplayer->AddToUnitsConsumingResources(this, requestedCosts); |
| 1326 |
} |
| 1327 |
|
| 1328 |
if (isProducingResources) { |
| 1329 |
for (i = 0; i < MaxCosts; ++i) { |
| 1330 |
newplayer->ProductionRate[i] += Data.Harvest.CurrentProduction[i]; |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
// |
| 1335 |
// Must change food/gold and other. |
| 1336 |
// |
| 1337 |
if (Type->CanHarvestFrom) { |
| 1338 |
DebugPrint("Resource transfer not supported\n"); |
| 1339 |
} |
| 1340 |
if (Type->Building) { |
| 1341 |
newplayer->NumBuildings++; |
| 1342 |
} |
| 1343 |
newplayer->UnitTypesCount[Type->Slot]++; |
| 1344 |
|
| 1345 |
UpdateForNewUnit(this, 1); |
| 1346 |
} |
| 1347 |
|
| 1348 |
|
| 1349 |
/** |
| 1350 |
** Rescue units. |
| 1351 |
** |
| 1352 |
** Look through all rescueable players, if they could be rescued. |
| 1353 |
*/ |
| 1354 |
void RescueUnits(void) |
| 1355 |
{ |
| 1356 |
CUnit *table[UnitMax]; |
| 1357 |
CUnit *around[UnitMax]; |
| 1358 |
int n; |
| 1359 |
int l; |
| 1360 |
|
| 1361 |
if (NoRescueCheck) { // all possible units are rescued |
| 1362 |
return; |
| 1363 |
} |
| 1364 |
NoRescueCheck = 1; |
| 1365 |
|
| 1366 |
// |
| 1367 |
// Look if player could be rescued. |
| 1368 |
// |
| 1369 |
for (CPlayer *p = Players; p < Players + NumPlayers; ++p) { |
| 1370 |
if (p->Type != PlayerRescuePassive && p->Type != PlayerRescueActive) { |
| 1371 |
continue; |
| 1372 |
} |
| 1373 |
if (p->TotalNumUnits) { |
| 1374 |
NoRescueCheck = 0; |
| 1375 |
// NOTE: table is changed. |
| 1376 |
l = p->TotalNumUnits; |
| 1377 |
memcpy(table, p->Units, l * sizeof(CUnit *)); |
| 1378 |
for (int j = 0; j < l; ++j) { |
| 1379 |
CUnit *unit = table[j]; |
| 1380 |
// Do not rescue removed units. Units inside something are |
| 1381 |
// rescued by ChangeUnitOwner |
| 1382 |
if (unit->Removed) { |
| 1383 |
continue; |
| 1384 |
} |
| 1385 |
|
| 1386 |
if (unit->Type->UnitType == UnitTypeLand) { |
| 1387 |
n = UnitCache.Select( |
| 1388 |
unit->X - 1, unit->Y - 1, |
| 1389 |
unit->X + unit->Type->TileWidth + 1, |
| 1390 |
unit->Y + unit->Type->TileHeight + 1, around, UnitMax); |
| 1391 |
} else { |
| 1392 |
n = UnitCache.Select( |
| 1393 |
unit->X - 2, unit->Y - 2, |
| 1394 |
unit->X + unit->Type->TileWidth + 2, |
| 1395 |
unit->Y + unit->Type->TileHeight + 2, around, UnitMax); |
| 1396 |
} |
| 1397 |
// |
| 1398 |
// Look if ally near the unit. |
| 1399 |
// |
| 1400 |
for (int i = 0; i < n; ++i) { |
| 1401 |
if (unit->IsAllied(around[i])) { |
| 1402 |
unit->RescuedFrom = unit->Player; |
| 1403 |
unit->ChangeOwner(around[i]->Player); |
| 1404 |
unit->Blink = 5; |
| 1405 |
PlayGameSound(GameSounds.Rescue.Sound, MaxSampleVolume); |
| 1406 |
break; |
| 1407 |
} |
| 1408 |
} |
| 1409 |
} |
| 1410 |
} |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
/** |
| 1415 |
** Check if a unit holds any resources |
| 1416 |
** |
| 1417 |
** @param unit Unit to check |
| 1418 |
** |
| 1419 |
** @return True if the unit holds resources, false if it doesn't |
| 1420 |
*/ |
| 1421 |
bool UnitHoldsResources(const CUnit *unit) |
| 1422 |
{ |
| 1423 |
for (int i = 0; i < MaxCosts; ++i) { |
| 1424 |
if (unit->ResourcesHeld[i] != 0) { |
| 1425 |
return true; |
| 1426 |
} |
| 1427 |
} |
| 1428 |
return false; |
| 1429 |
} |
| 1430 |
|
| 1431 |
/*---------------------------------------------------------------------------- |
| 1432 |
-- Unit headings |
| 1433 |
----------------------------------------------------------------------------*/ |
| 1434 |
|
| 1435 |
/** |
| 1436 |
** Fast arc tangent function. |
| 1437 |
** |
| 1438 |
** @param val atan argument |
| 1439 |
** |
| 1440 |
** @return atan(val) |
| 1441 |
*/ |
| 1442 |
static int myatan(int val) |
| 1443 |
{ |
| 1444 |
static int init; |
| 1445 |
static unsigned char atan_table[2608]; |
| 1446 |
|
| 1447 |
if (val >= 2608) { |
| 1448 |
return 63; |
| 1449 |
} |
| 1450 |
if (!init) { |
| 1451 |
for (; init < 2608; ++init) { |
| 1452 |
atan_table[init] = |
| 1453 |
(unsigned char)(atan((double)init / 64) * (64 * 4 / 6.2831853)); |
| 1454 |
} |
| 1455 |
} |
| 1456 |
|
| 1457 |
return atan_table[val]; |
| 1458 |
} |
| 1459 |
|
| 1460 |
/** |
| 1461 |
** Convert direction to heading. |
| 1462 |
** |
| 1463 |
** @param delta_x Delta X. |
| 1464 |
** @param delta_y Delta Y. |
| 1465 |
** |
| 1466 |
** @return Angle (0..255) |
| 1467 |
*/ |
| 1468 |
int DirectionToHeading(int delta_x, int delta_y) |
| 1469 |
{ |
| 1470 |
// |
| 1471 |
// Check which quadrant. |
| 1472 |
// |
| 1473 |
if (delta_x > 0) { |
| 1474 |
if (delta_y < 0) { // Quadrant 1? |
| 1475 |
return myatan((delta_x * 64) / -delta_y); |
| 1476 |
} |
| 1477 |
// Quadrant 2? |
| 1478 |
return myatan((delta_y * 64) / delta_x) + 64; |
| 1479 |
} |
| 1480 |
if (delta_y>0) { // Quadrant 3? |
| 1481 |
return myatan((delta_x * -64) / delta_y) + 64 * 2; |
| 1482 |
} |
| 1483 |
if (delta_x) { // Quadrant 4. |
| 1484 |
return myatan((delta_y * -64) / -delta_x) + 64 * 3; |
| 1485 |
} |
| 1486 |
return 0; |
| 1487 |
} |
| 1488 |
|
| 1489 |
/** |
| 1490 |
** Update sprite frame for new heading. |
| 1491 |
*/ |
| 1492 |
void UnitUpdateHeading(CUnit *unit) |
| 1493 |
{ |
| 1494 |
int dir; |
| 1495 |
int nextdir; |
| 1496 |
bool neg; |
| 1497 |
|
| 1498 |
if (unit->Frame < 0) { |
| 1499 |
unit->Frame = -unit->Frame - 1; |
| 1500 |
neg = true; |
| 1501 |
} else { |
| 1502 |
neg = false; |
| 1503 |
} |
| 1504 |
unit->Frame /= unit->Type->NumDirections / 2 + 1; |
| 1505 |
unit->Frame *= unit->Type->NumDirections / 2 + 1; |
| 1506 |
// Remove heading, keep animation frame |
| 1507 |
|
| 1508 |
nextdir = 256 / unit->Type->NumDirections; |
| 1509 |
dir = ((unit->Direction + nextdir / 2) & 0xFF) / nextdir; |
| 1510 |
if (dir <= LookingS / nextdir) { // north->east->south |
| 1511 |
unit->Frame += dir; |
| 1512 |
} else { |
| 1513 |
unit->Frame += 256 / nextdir - dir; |
| 1514 |
unit->Frame = -unit->Frame - 1; |
| 1515 |
} |
| 1516 |
if (neg && !unit->Frame && unit->Type->Building) { |
| 1517 |
unit->Frame = -1; |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
/** |
| 1522 |
** Change unit heading/frame from delta direction x, y. |
| 1523 |
** |
| 1524 |
** @param unit Unit for new direction looking. |
| 1525 |
** @param dx X map tile delta direction. |
| 1526 |
** @param dy Y map tile delta direction. |
| 1527 |
*/ |
| 1528 |
void UnitHeadingFromDeltaXY(CUnit *unit, int dx, int dy) |
| 1529 |
{ |
| 1530 |
unit->Direction = DirectionToHeading(dx, dy); |
| 1531 |
UnitUpdateHeading(unit); |
| 1532 |
} |
| 1533 |
|
| 1534 |
/*---------------------------------------------------------------------------- |
| 1535 |
-- Drop out units |
| 1536 |
----------------------------------------------------------------------------*/ |
| 1537 |
|
| 1538 |
/** |
| 1539 |
** Place a unit on the map to the side of a unit. |
| 1540 |
** |
| 1541 |
** @param unit Unit to drop out. |
| 1542 |
** @param heading Direction in which the unit should appear. |
| 1543 |
** @param addx Tile width of unit it's dropping out of. |
| 1544 |
** @param addy Tile height of unit it's dropping out of. |
| 1545 |
*/ |
| 1546 |
void DropOutOnSide(CUnit *unit, int heading, int addx, int addy) |
| 1547 |
{ |
| 1548 |
int x; |
| 1549 |
int y; |
| 1550 |
int i; |
| 1551 |
|
| 1552 |
if (unit->Container) { |
| 1553 |
x = unit->Container->X; |
| 1554 |
y = unit->Container->Y; |
| 1555 |
} else { |
| 1556 |
x = unit->X; |
| 1557 |
y = unit->Y; |
| 1558 |
} |
| 1559 |
|
| 1560 |
if (heading < LookingNE || heading > LookingNW) { |
| 1561 |
x += addx - 1; |
| 1562 |
--y; |
| 1563 |
goto startn; |
| 1564 |
} |
| 1565 |
if (heading < LookingSE) { |
| 1566 |
x += addx; |
| 1567 |
y += addy - 1; |
| 1568 |
goto starte; |
| 1569 |
} |
| 1570 |
if (heading < LookingSW) { |
| 1571 |
y += addy; |
| 1572 |
goto starts; |
| 1573 |
} |
| 1574 |
--x; |
| 1575 |
goto startw; |
| 1576 |
|
| 1577 |
// FIXME: don't search outside of the map |
| 1578 |
for (;;) { |
| 1579 |
startw: |
| 1580 |
for (i = addy; i--; ++y) { |
| 1581 |
if (UnitCanBeAt(unit, x, y)) { |
| 1582 |
goto found; |
| 1583 |
} |
| 1584 |
} |
| 1585 |
++addx; |
| 1586 |
starts: |
| 1587 |
for (i = addx; i--; ++x) { |
| 1588 |
if (UnitCanBeAt(unit, x, y)) { |
| 1589 |
goto found; |
| 1590 |
} |
| 1591 |
} |
| 1592 |
++addy; |
| 1593 |
starte: |
| 1594 |
for (i = addy; i--; --y) { |
| 1595 |
if (UnitCanBeAt(unit, x, y)) { |
| 1596 |
goto found; |
| 1597 |
} |
| 1598 |
} |
| 1599 |
++addx; |
| 1600 |
startn: |
| 1601 |
for (i = addx; i--; --x) { |
| 1602 |
if (UnitCanBeAt(unit, x, y)) { |
| 1603 |
goto found; |
| 1604 |
} |
| 1605 |
} |
| 1606 |
++addy; |
| 1607 |
} |
| 1608 |
|
| 1609 |
found: |
| 1610 |
unit->Place(x, y); |
| 1611 |
} |
| 1612 |
|
| 1613 |
/** |
| 1614 |
** Place a unit on the map nearest to x, y. |
| 1615 |
** |
| 1616 |
** @param unit Unit to drop out. |
| 1617 |
** @param gx Goal X map tile position. |
| 1618 |
** @param gy Goal Y map tile position. |
| 1619 |
** @param addx Tile width of unit it's dropping out of. |
| 1620 |
** @param addy Tile height of unit it's dropping out of. |
| 1621 |
*/ |
| 1622 |
void DropOutNearest(CUnit *unit, int gx, int gy, int addx, int addy) |
| 1623 |
{ |
| 1624 |
int x; |
| 1625 |
int y; |
| 1626 |
int i; |
| 1627 |
int bestx; |
| 1628 |
int besty; |
| 1629 |
int bestd; |
| 1630 |
int n; |
| 1631 |
|
| 1632 |
Assert(unit->Removed); |
| 1633 |
|
| 1634 |
x = y = -1; |
| 1635 |
if (unit->Container) { |
| 1636 |
x = unit->Container->X; |
| 1637 |
y = unit->Container->Y; |
| 1638 |
} else { |
| 1639 |
x = unit->X; |
| 1640 |
y = unit->Y; |
| 1641 |
} |
| 1642 |
|
| 1643 |
Assert(x != -1 && y != -1); |
| 1644 |
|
| 1645 |
bestd = 99999; |
| 1646 |
bestx = besty = 0; |
| 1647 |
|
| 1648 |
// FIXME: if we reach the map borders we can go fast up, left, ... |
| 1649 |
--x; |
| 1650 |
for (;;) { |
| 1651 |
for (i = addy; i--; ++y) { // go down |
| 1652 |
if (UnitCanBeAt(unit, x, y)) { |
| 1653 |
n = MapDistance(gx, gy, x, y); |
| 1654 |
if (n < bestd) { |
| 1655 |
bestd = n; |
| 1656 |
bestx = x; |
| 1657 |
besty = y; |
| 1658 |
} |
| 1659 |
} |
| 1660 |
} |
| 1661 |
++addx; |
| 1662 |
for (i = addx; i--; ++x) { // go right |
| 1663 |
if (UnitCanBeAt(unit, x, y)) { |
| 1664 |
n = MapDistance(gx, gy, x, y); |
| 1665 |
if (n < bestd) { |
| 1666 |
bestd = n; |
| 1667 |
bestx = x; |
| 1668 |
besty = y; |
| 1669 |
} |
| 1670 |
} |
| 1671 |
} |
| 1672 |
++addy; |
| 1673 |
for (i = addy; i--; --y) { // go up |
| 1674 |
if (UnitCanBeAt(unit, x, y)) { |
| 1675 |
n = MapDistance(gx, gy, x, y); |
| 1676 |
if (n < bestd) { |
| 1677 |
bestd = n; |
| 1678 |
bestx = x; |
| 1679 |
besty = y; |
| 1680 |
} |
| 1681 |
} |
| 1682 |
} |
| 1683 |
++addx; |
| 1684 |
for (i = addx; i--; --x) { // go left |
| 1685 |
if (UnitCanBeAt(unit, x, y)) { |
| 1686 |
n = MapDistance(gx, gy, x, y); |
| 1687 |
if (n < bestd) { |
| 1688 |
bestd = n; |
| 1689 |
bestx = x; |
| 1690 |
besty = y; |
| 1691 |
} |
| 1692 |
} |
| 1693 |
} |
| 1694 |
if (bestd != 99999) { |
| 1695 |
unit->Place(bestx, besty); |
| 1696 |
return; |
| 1697 |
} |
| 1698 |
++addy; |
| 1699 |
} |
| 1700 |
} |
| 1701 |
|
| 1702 |
/** |
| 1703 |
** Drop out all units inside unit. |
| 1704 |
** |
| 1705 |
** @param source All units inside source are dropped out. |
| 1706 |
*/ |
| 1707 |
void DropOutAll(const CUnit *source) |
| 1708 |
{ |
| 1709 |
CUnit *unit; |
| 1710 |
int i; |
| 1711 |
|
| 1712 |
unit = source->UnitInside; |
| 1713 |
for (i = source->InsideCount; i; --i, unit = unit->NextContained) { |
| 1714 |
DropOutOnSide(unit, LookingW, |
| 1715 |
source->Type->TileWidth, source->Type->TileHeight); |
| 1716 |
Assert(!unit->Orders[0]->Goal); |
| 1717 |
unit->Orders[0]->Action = UnitActionStill; |
| 1718 |
unit->SubAction = 0; |
| 1719 |
} |
| 1720 |
} |
| 1721 |
|
| 1722 |
/*---------------------------------------------------------------------------- |
| 1723 |
-- Finding units |
| 1724 |
----------------------------------------------------------------------------*/ |
| 1725 |
|
| 1726 |
/** |
| 1727 |
** Find Resource. |
| 1728 |
** |
| 1729 |
** @param unit The unit that wants to find a resource. |
| 1730 |
** @param x Closest to x |
| 1731 |
** @param y Closest to y |
| 1732 |
** @param range Maximum distance to the resource. |
| 1733 |
** @param resource The resource id. |
| 1734 |
** |
| 1735 |
** @note This will return an usable resource building that doesn't |
| 1736 |
** belong to the player or one of his allies. |
| 1737 |
** |
| 1738 |
** @return NoUnitP or resource unit |
| 1739 |
*/ |
| 1740 |
CUnit *UnitFindResource(const CUnit *unit, int x, int y, int range, int resource) |
| 1741 |
{ |
| 1742 |
static const int xoffset[] = { 0,-1,+1, 0, -1,+1,-1,+1 }; |
| 1743 |
static const int yoffset[] = { -1, 0, 0,+1, -1,-1,+1,+1 }; |
| 1744 |
struct p { |
| 1745 |
unsigned short X; |
| 1746 |
unsigned short Y; |
| 1747 |
} *points; |
| 1748 |
int size; |
| 1749 |
int rx; |
| 1750 |
int ry; |
| 1751 |
int mask; |
| 1752 |
int wp; |
| 1753 |
int rp; |
| 1754 |
int ep; |
| 1755 |
int i; |
| 1756 |
int w; |
| 1757 |
unsigned char *m; |
| 1758 |
unsigned char *matrix; |
| 1759 |
CUnit *res; |
| 1760 |
CUnit *bestres; |
| 1761 |
int bestd; |
| 1762 |
int cdist; |
| 1763 |
|
| 1764 |
size = std::min(Map.Info.MapWidth * Map.Info.MapHeight / 4, range * range * 5); |
| 1765 |
points = new p[size]; |
| 1766 |
|
| 1767 |
bestd = 99999; |
| 1768 |
// Make movement matrix. FIXME: can create smaller matrix. |
| 1769 |
matrix = CreateMatrix(); |
| 1770 |
w = Map.Info.MapWidth + 2; |
| 1771 |
matrix += w + w + 2; |
| 1772 |
// Unit movement mask |
| 1773 |
mask = unit->Type->MovementMask; |
| 1774 |
// Ignore all units along the way. Might seem weird, but otherwise |
| 1775 |
// peasants would lock at a resource with a lot of workers. |
| 1776 |
mask &= ~(MapFieldLandUnit | MapFieldSeaUnit | MapFieldAirUnit); |
| 1777 |
points[0].X = x; |
| 1778 |
points[0].Y = y; |
| 1779 |
rp = 0; |
| 1780 |
matrix[x + y * w] = 1; // mark start point |
| 1781 |
ep = wp = 1; // start with one point |
| 1782 |
cdist = 0; // current distance is 0 |
| 1783 |
bestres = NoUnitP; |
| 1784 |
|
| 1785 |
// |
| 1786 |
// Pop a point from stack, push all neighbors which could be entered. |
| 1787 |
// |
| 1788 |
for (;;) { |
| 1789 |
while (rp != ep) { |
| 1790 |
rx = points[rp].X; |
| 1791 |
ry = points[rp].Y; |
| 1792 |
for (i = 0; i < 8; ++i) { // mark all neighbors |
| 1793 |
x = rx + xoffset[i]; |
| 1794 |
y = ry + yoffset[i]; |
| 1795 |
m = matrix + x + y * w; |
| 1796 |
if (*m) { // already checked |
| 1797 |
continue; |
| 1798 |
} |
| 1799 |
|
| 1800 |
if (!Map.IsFieldExplored(unit->Player, x, y)) { // Unknown. |
| 1801 |
continue; |
| 1802 |
} |
| 1803 |
|
| 1804 |
// |
| 1805 |
// Look if there is a resource |
| 1806 |
// |
| 1807 |
if ((res = ResourceOnMap(x, y, resource)) && |
| 1808 |
res->Type->CanHarvestFrom && |
| 1809 |
res->Player->Type == PlayerNeutral) { |
| 1810 |
delete[] points; |
| 1811 |
return res; |
| 1812 |
} |
| 1813 |
|
| 1814 |
if (CanMoveToMask(x, y, mask)) { // reachable |
| 1815 |
*m = 1; |
| 1816 |
points[wp].X = x; // push the point |
| 1817 |
points[wp].Y = y; |
| 1818 |
if (++wp >= size) { // round about |
| 1819 |
wp = 0; |
| 1820 |
} |
| 1821 |
if (wp == ep) { |
| 1822 |
// We are out of points, give up! |
| 1823 |
break; |
| 1824 |
} |
| 1825 |
} else { // unreachable |
| 1826 |
*m = 99; |
| 1827 |
} |
| 1828 |
} |
| 1829 |
if (++rp >= size) { // round about |
| 1830 |
rp = 0; |
| 1831 |
} |
| 1832 |
} |
| 1833 |
// Take best of this frame, if any. |
| 1834 |
if (bestd != 99999) { |
| 1835 |
delete[] points; |
| 1836 |
return bestres; |
| 1837 |
} |
| 1838 |
++cdist; |
| 1839 |
if (rp == wp || cdist >= range) { // unreachable, no more points available |
| 1840 |
break; |
| 1841 |
} |
| 1842 |
// Continue with next set. |
| 1843 |
ep = wp; |
| 1844 |
} |
| 1845 |
delete[] points; |
| 1846 |
return NoUnitP; |
| 1847 |
} |
| 1848 |
|
| 1849 |
/** |
| 1850 |
** Find the next idle worker |
| 1851 |
** |
| 1852 |
** @param player Player's units to search through |
| 1853 |
** |
| 1854 |
** @return NoUnitP or next idle worker |
| 1855 |
*/ |
| 1856 |
CUnit *FindIdleWorker(const CPlayer *player) |
| 1857 |
{ |
| 1858 |
CUnit *unit; |
| 1859 |
CUnit *firstUnitFound; |
| 1860 |
int nunits; |
| 1861 |
bool selectNextUnit = false; |
| 1862 |
|
| 1863 |
firstUnitFound = NoUnitP; |
| 1864 |
|
| 1865 |
nunits = player->TotalNumUnits; |
| 1866 |
|
| 1867 |
for (int i = 0; i < nunits; ++i) { |
| 1868 |
unit = player->Units[i]; |
| 1869 |
if (unit->Type->Harvester && !unit->Removed) { |
| 1870 |
if (unit->Orders[0]->Action == UnitActionStill) { |
| 1871 |
if (selectNextUnit) { |
| 1872 |
return unit; |
| 1873 |
} |
| 1874 |
if (firstUnitFound == NoUnitP) { |
| 1875 |
// Use the first possible unit if we can't select next |
| 1876 |
firstUnitFound = unit; |
| 1877 |
} |
| 1878 |
if (!selectNextUnit && IsOnlySelected(unit)) { |
| 1879 |
// If this unit is selected, select next unit |
| 1880 |
selectNextUnit = true; |
| 1881 |
} |
| 1882 |
} |
| 1883 |
} |
| 1884 |
} |
| 1885 |
|
| 1886 |
if (firstUnitFound != NoUnitP && !IsOnlySelected(firstUnitFound)) { |
| 1887 |
return firstUnitFound; |
| 1888 |
} |
| 1889 |
|
| 1890 |
return NoUnitP; |
| 1891 |
} |
| 1892 |
|
| 1893 |
/*---------------------------------------------------------------------------- |
| 1894 |
-- Select units |
| 1895 |
----------------------------------------------------------------------------*/ |
| 1896 |
|
| 1897 |
/** |
| 1898 |
** Select unit on screen. (x, y are in pixels relative to map 0,0). |
| 1899 |
** |
| 1900 |
** @param x X pixel position. |
| 1901 |
** @param y Y pixel position. |
| 1902 |
** |
| 1903 |
** @return Unit on x, y position. |
| 1904 |
*/ |
| 1905 |
CUnit *UnitOnScreen(int x, int y) |
| 1906 |
{ |
| 1907 |
CUnit *bestUnit = NULL; |
| 1908 |
|
| 1909 |
for (int i = 0; i < NumUnits; ++i) { |
| 1910 |
CUnit *unit = Units[i]; |
| 1911 |
CUnitType *type = unit->Type; |
| 1912 |
int gx, gy; |
| 1913 |
|
| 1914 |
// Must be visible |
| 1915 |
if (!unit->IsVisibleAsGoal(ThisPlayer) && !ReplayRevealMap) { |
| 1916 |
continue; |
| 1917 |
} |
| 1918 |
|
| 1919 |
// Mouse is in the unit's box |
| 1920 |
gx = unit->X * TileSizeX + unit->IX; |
| 1921 |
if (x + (type->BoxWidth - type->TileWidth * TileSizeX) / 2 < gx) { |
| 1922 |
continue; |
| 1923 |
} |
| 1924 |
if (x > gx + (type->TileWidth * TileSizeX + type->BoxWidth) / 2) { |
| 1925 |
continue; |
| 1926 |
} |
| 1927 |
|
| 1928 |
gy = unit->Y * TileSizeY + unit->IY; |
| 1929 |
if (y + (type->BoxHeight - type->TileHeight * TileSizeY) / 2 < gy) { |
| 1930 |
continue; |
| 1931 |
} |
| 1932 |
if (y > gy + (type->TileHeight * TileSizeY + type->BoxHeight) / 2) { |
| 1933 |
continue; |
| 1934 |
} |
| 1935 |
|
| 1936 |
// This could be taken. |
| 1937 |
if (!bestUnit || unit->Type->DrawLevel > bestUnit->Type->DrawLevel) { |
| 1938 |
bestUnit = unit; |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
return bestUnit; |
| 1943 |
} |
| 1944 |
|
| 1945 |
/** |
| 1946 |
** Check if a unit should be removed from UnitsConsumingResources |
| 1947 |
*/ |
| 1948 |
void UnitRemoveConsumingResources(CUnit *unit) |
| 1949 |
{ |
| 1950 |
if (unit->Orders[0]->Action == UnitActionRepair && unit->SubAction == 20) { |
| 1951 |
unit->Player->RemoveFromUnitsConsumingResources(unit); |
| 1952 |
} else if (unit->Orders[0]->Action == UnitActionResource && unit->SubAction >= 55) { |
| 1953 |
for (int u = 0; u < MaxCosts; ++u) { |
| 1954 |
unit->Player->ProductionRate[u] -= unit->Data.Harvest.CurrentProduction[u]; |
| 1955 |
} |
| 1956 |
} else if (unit->Orders[0]->Action == UnitActionTrain && unit->SubAction != 0) { |
| 1957 |
unit->Player->RemoveFromUnitsConsumingResources(unit); |
| 1958 |
} |
| 1959 |
} |
| 1960 |
|
| 1961 |
/** |
| 1962 |
** Let a unit die. |
| 1963 |
** |
| 1964 |
** @param unit Unit to be destroyed. |
| 1965 |
*/ |
| 1966 |
void LetUnitDie(CUnit *unit) |
| 1967 |
{ |
| 1968 |
CUnitType *type; |
| 1969 |
|
| 1970 |
unit->Moving = 0; |
| 1971 |
unit->TTL = 0; |
| 1972 |
unit->Anim.Unbreakable = 0; |
| 1973 |
|
| 1974 |
type = unit->Type; |
| 1975 |
|
| 1976 |
// removed units, just remove. |
| 1977 |
if (unit->Removed) { |
| 1978 |
DebugPrint("Killing a removed unit?\n"); |
| 1979 |
UnitLost(unit); |
| 1980 |
UnitClearOrders(unit); |
| 1981 |
unit->Release(); |
| 1982 |
return; |
| 1983 |
} |
| 1984 |
|
| 1985 |
PlayUnitSound(unit, VoiceDying); |
| 1986 |
|
| 1987 |
// |
| 1988 |
// Catapults,... explodes. |
| 1989 |
// |
| 1990 |
if (type->ExplodeWhenKilled) { |
| 1991 |
MakeMissile(type->Explosion.Missile, |
| 1992 |
unit->X * TileSizeX + type->TileWidth * TileSizeX / 2, |
| 1993 |
unit->Y * TileSizeY + type->TileHeight * TileSizeY / 2, |
| 1994 |
0, 0); |
| 1995 |
} |
| 1996 |
if (type->DeathExplosion) { |
| 1997 |
type->DeathExplosion->pushPreamble(); |
| 1998 |
type->DeathExplosion->pushInteger(unit->X * TileSizeX + |
| 1999 |
type->TileWidth * TileSizeX / 2); |
| 2000 |
type->DeathExplosion->pushInteger(unit->Y * TileSizeY + |
| 2001 |
type->TileHeight * TileSizeY / 2); |
| 2002 |
type->DeathExplosion->run(); |
| 2003 |
} |
| 2004 |
|
| 2005 |
UnitRemoveConsumingResources(unit); |
| 2006 |
UnitRemoveProductionAndStorage(unit); |
| 2007 |
|
| 2008 |
// Transporters lose their units and building their workers |
| 2009 |
if (unit->UnitInside) { |
| 2010 |
// FIXME: destroy or unload : do a flag. |
| 2011 |
DestroyAllInside(unit); |
| 2012 |
} |
| 2013 |
unit->Remove(NULL); |
| 2014 |
UnitLost(unit); |
| 2015 |
UnitClearOrders(unit); |
| 2016 |
|
| 2017 |
// |
| 2018 |
// Unit has death animation. |
| 2019 |
// |
| 2020 |
|
| 2021 |
// Not good: UnitUpdateHeading(unit); |
| 2022 |
unit->SubAction = 0; |
| 2023 |
unit->State = 0; |
| 2024 |
unit->Orders[0]->Action = UnitActionDie; |
| 2025 |
if (type->CorpseType) { |
| 2026 |
unit->IX = (type->CorpseType->Width - type->CorpseType->Sprite->Width) / 2; |
| 2027 |
unit->IY = (type->CorpseType->Height - type->CorpseType->Sprite->Height) / 2; |
| 2028 |
|
| 2029 |
unit->CurrentSightRange = type->CorpseType->Stats[unit->Player->Index].Variables[SIGHTRANGE_INDEX].Max; |
| 2030 |
} else { |
| 2031 |
unit->CurrentSightRange = 0; |
| 2032 |
} |
| 2033 |
|
| 2034 |
// If we have a corpse, or a death animation, we are put back on the map |
| 2035 |
// This enables us to be tracked. Possibly for spells (eg raise dead) |
| 2036 |
if (type->CorpseType || (type->Animations && type->Animations->Death)) { |
| 2037 |
unit->Removed = 0; |
| 2038 |
UnitCache.Insert(unit); |
| 2039 |
} |
| 2040 |
MapMarkUnitSight(unit); |
| 2041 |
} |
| 2042 |
|
| 2043 |
/** |
| 2044 |
** Subtract the production rate and storage capacity of the unit from |
| 2045 |
** the player that owns it. However, do nothing if the unit hasn't |
| 2046 |
** yet been fully built, because that means the production rate and |
| 2047 |
** storage capacity haven't even been added to the player yet. |
| 2048 |
** |
| 2049 |
** This function is called when the player is losing the unit: either |
| 2050 |
** because the unit is dying, or because the unit is being transferred |
| 2051 |
** to another player. |
| 2052 |
** |
| 2053 |
** This function ignores harvesting. If the unit may be harvesting |
| 2054 |
** some resources, the caller must deduct that from the player in some |
| 2055 |
** other way. |
| 2056 |
*/ |
| 2057 |
static void UnitRemoveProductionAndStorage(CUnit *unit) |
| 2058 |
{ |
| 2059 |
if (unit->Orders[0]->Action != UnitActionBuilt) { |
| 2060 |
for (int u = 0; u < MaxCosts; ++u) { |
| 2061 |
unit->Player->ProductionRate[u] -= unit->Type->ProductionRate[u] * unit->ProductionEfficiency / 100; |
| 2062 |
unit->Player->StorageCapacity[u] -= unit->Type->StorageCapacity[u]; |
| 2063 |
if (unit->Player->StoredResources[u] > unit->Player->StorageCapacity[u]) { |
| 2064 |
unit->Player->StoredResources[u] = unit->Player->StorageCapacity[u]; |
| 2065 |
} |
| 2066 |
} |
| 2067 |
} |
| 2068 |
} |
| 2069 |
|
| 2070 |
/** |
| 2071 |
** Destroy all units inside unit. |
| 2072 |
** |
| 2073 |
** @param source container. |
| 2074 |
*/ |
| 2075 |
void DestroyAllInside(CUnit *source) |
| 2076 |
{ |
| 2077 |
CUnit *unit; |
| 2078 |
|
| 2079 |
// No Corpses, we are inside something, and we can't be seen |
| 2080 |
unit = source->UnitInside; |
| 2081 |
for (int i = source->InsideCount; i; --i, unit = unit->NextContained) { |
| 2082 |
// Transporter inside a transporter? |
| 2083 |
if (unit->UnitInside) { |
| 2084 |
DestroyAllInside(unit); |
| 2085 |
} |
| 2086 |
UnitLost(unit); |
| 2087 |
UnitClearOrders(unit); |
| 2088 |
unit->Release(); |
| 2089 |
} |
| 2090 |
} |
| 2091 |
|
| 2092 |
|
| 2093 |
/*---------------------------------------------------------------------------- |
| 2094 |
-- Unit AI |
| 2095 |
----------------------------------------------------------------------------*/ |
| 2096 |
|
| 2097 |
/** |
| 2098 |
** Unit is hit by missile or other damage. |
| 2099 |
** |
| 2100 |
** @param attacker Unit that attacks. |
| 2101 |
** @param target Unit that is hit. |
| 2102 |
** @param damage How many damage to take. |
| 2103 |
*/ |
| 2104 |
void HitUnit(CUnit *attacker, CUnit *target, int damage) |
| 2105 |
{ |
| 2106 |
CUnitType *type; |
| 2107 |
CUnit *goal; |
| 2108 |
unsigned long lastattack; |
| 2109 |
|
| 2110 |
// Can now happen by splash damage |
| 2111 |
// Multiple places send x/y as damage, which may be zero |
| 2112 |
if (!damage) { |
| 2113 |
return; |
| 2114 |
} |
| 2115 |
|
| 2116 |
Assert(damage != 0 && target->Orders[0]->Action != UnitActionDie && !target->Type->Vanishes); |
| 2117 |
|
| 2118 |
if (target->Type->Indestructible) { |
| 2119 |
return; |
| 2120 |
} |
| 2121 |
|
| 2122 |
if (target->Removed) { |
| 2123 |
DebugPrint("Removed target hit\n"); |
| 2124 |
return; |
| 2125 |
} |
| 2126 |
|
| 2127 |
if (GodMode) { |
| 2128 |
if (attacker && attacker->Player == ThisPlayer) { |
| 2129 |
damage = target->Variable[HP_INDEX].Value; |
| 2130 |
} |
| 2131 |
if (target->Player == ThisPlayer) { |
| 2132 |
damage = 0; |
| 2133 |
} |
| 2134 |
} |
| 2135 |
|
| 2136 |
type = target->Type; |
| 2137 |
lastattack = target->Attacked; |
| 2138 |
target->Attacked = GameCycle ? GameCycle : 1; |
| 2139 |
|
| 2140 |
|
| 2141 |
if (!lastattack || lastattack + 2 * CYCLES_PER_SECOND < GameCycle) { |
| 2142 |
// NOTE: perhaps this should also be moved into the notify? |
| 2143 |
if (target->Player == ThisPlayer) { |
| 2144 |
// FIXME: Problem with load+save. |
| 2145 |
|
| 2146 |
// |
| 2147 |
// One help cry each 2 second is enough |
| 2148 |
// If on same area ignore it for 2 minutes. |
| 2149 |
// |
| 2150 |
if (HelpMeLastCycle < GameCycle) { |
| 2151 |
if (!HelpMeLastCycle || |
| 2152 |
HelpMeLastCycle + CYCLES_PER_SECOND * 120 < GameCycle || |
| 2153 |
target->X < HelpMeLastX - 14 || |
| 2154 |
target->X > HelpMeLastX + 14 || |
| 2155 |
target->Y < HelpMeLastY - 14 || |
| 2156 |
target->Y > HelpMeLastY + 14) { |
| 2157 |
HelpMeLastCycle = GameCycle + CYCLES_PER_SECOND * 2; |
| 2158 |
HelpMeLastX = target->X; |
| 2159 |
HelpMeLastY = target->Y; |
| 2160 |
PlayUnitSound(target, VoiceHelpMe); |
| 2161 |
} |
| 2162 |
} |
| 2163 |
} |
| 2164 |
target->Player->Notify(NotifyRed, target->X, target->Y, |
| 2165 |
_("%s attacked"), target->Type->Name.c_str()); |
| 2166 |
if (target->Player->AiEnabled) { |
| 2167 |
AiHelpMe(attacker, target); |
| 2168 |
} |
| 2169 |
} |
| 2170 |
|
| 2171 |
if (target->Variable[HP_INDEX].Value <= damage) { // unit is killed or destroyed |
| 2172 |
// increase scores of the attacker, but not if attacking it's own units. |
| 2173 |
// prevents cheating by killing your own units. |
| 2174 |
if (attacker && target->IsEnemy(attacker)) { |
| 2175 |
attacker->Player->Score += target->Type->Points; |
| 2176 |
if (type->Building) { |
| 2177 |
attacker->Player->TotalRazings++; |
| 2178 |
} else { |
| 2179 |
attacker->Player->TotalKills++; |
| 2180 |
} |
| 2181 |
attacker->Variable[KILL_INDEX].Value++; |
| 2182 |
attacker->Variable[KILL_INDEX].Max++; |
| 2183 |
attacker->Variable[KILL_INDEX].Enable = 1; |
| 2184 |
} |
| 2185 |
LetUnitDie(target); |
| 2186 |
return; |
| 2187 |
} |
| 2188 |
target->Variable[HP_INDEX].Value -= damage; |
| 2189 |
|
| 2190 |
// FIXME: this is dumb. I made repairers capture. crap. |
| 2191 |
// david: capture enemy buildings |
| 2192 |
// Only worker types can capture. |
| 2193 |
// Still possible to destroy building if not careful (too many attackers) |
| 2194 |
if (EnableBuildingCapture && attacker && |
| 2195 |
type->Building && target->Variable[HP_INDEX].Value <= damage * 3 && |
| 2196 |
attacker->IsEnemy(target) && |
| 2197 |
attacker->Type->RepairRange) { |
| 2198 |
target->ChangeOwner(attacker->Player); |
| 2199 |
CommandStopUnit(attacker); // Attacker shouldn't continue attack! |
| 2200 |
} |
| 2201 |
|
| 2202 |
if ((target->IsVisibleOnMap(ThisPlayer) || ReplayRevealMap) && !DamageMissile.empty()) { |
| 2203 |
MakeLocalMissile(MissileTypeByIdent(DamageMissile), |
| 2204 |
target->X * TileSizeX + target->Type->TileWidth * TileSizeX / 2, |
| 2205 |
target->Y * TileSizeY + target->Type->TileHeight * TileSizeY / 2, |
| 2206 |
target->X * TileSizeX + target->Type->TileWidth * TileSizeX / 2 + 3, |
| 2207 |
target->Y * TileSizeY + target->Type->TileHeight * TileSizeY / 2 - |
| 2208 |
MissileTypeByIdent(DamageMissile)->Range)->Damage = -damage; |
| 2209 |
} |
| 2210 |
|
| 2211 |
#if 0 |
| 2212 |
// FIXME: want to show hits. |
| 2213 |
if (type->Organic) { |
| 2214 |
MakeMissile(MissileBlood, |
| 2215 |
target->X * TileSizeX + TileSizeX / 2, |
| 2216 |
target->Y * TileSizeY + TileSizeY / 2, 0, 0); |
| 2217 |
} |
| 2218 |
if (type->Building) { |
| 2219 |
MakeMissile(MissileSmallFire, |
| 2220 |
target->X * TileSizeX + (type->TileWidth * TileSizeX) / 2, |
| 2221 |
target->Y * TileSizeY + (type->TileHeight * TileSizeY) / 2, 0, 0); |
| 2222 |
} |
| 2223 |
#endif |
| 2224 |
|
| 2225 |
if (type->Building && !target->Burning) { |
| 2226 |
int f; |
| 2227 |
Missile *missile; |
| 2228 |
MissileType *fire; |
| 2229 |
|
| 2230 |
f = (100 * target->Variable[HP_INDEX].Value) / target->Variable[HP_INDEX].Max; |
| 2231 |
fire = MissileBurningBuilding(f); |
| 2232 |
if (fire) { |
| 2233 |
missile = MakeMissile(fire, |
| 2234 |
target->X * TileSizeX + (type->TileWidth * TileSizeX) / 2, |
| 2235 |
target->Y * TileSizeY + (type->TileHeight * TileSizeY) / 2 - TileSizeY, |
| 2236 |
0, 0); |
| 2237 |
missile->SourceUnit = target; |
| 2238 |
target->Burning = 1; |
| 2239 |
target->RefsIncrease(); |
| 2240 |
} |
| 2241 |
} |
| 2242 |
|
| 2243 |
// |
| 2244 |
// Unit is working? |
| 2245 |
// |
| 2246 |
if (target->Orders[0]->Action != UnitActionStill) { |
| 2247 |
return; |
| 2248 |
} |
| 2249 |
|
| 2250 |
// |
| 2251 |
// Attack units in range (which or the attacker?) |
| 2252 |
// |
| 2253 |
if (attacker && !type->Coward) { |
| 2254 |
if (type->CanAttack) { |
| 2255 |
if (CanTarget(target->Type, attacker->Type)) { |
| 2256 |
// Attack unit that is attacking |
| 2257 |
goal = attacker; |
| 2258 |
} else { |
| 2259 |
// Check for any other units in range |
| 2260 |
goal = AttackUnitsInReactRange(target); |
| 2261 |
} |
| 2262 |
if (goal) { |
| 2263 |
if (target->SavedOrder.Action == UnitActionStill) { |
| 2264 |
// FIXME: should rewrite command handling |
| 2265 |
CommandAttack(target, target->X, target->Y, NoUnitP, |
| 2266 |
FlushCommands); |
| 2267 |
target->SavedOrder = *target->Orders[1]; |
| 2268 |
} |
| 2269 |
CommandAttack(target, goal->X, goal->Y, NoUnitP, FlushCommands); |
| 2270 |
return; |
| 2271 |
} |
| 2272 |
} |
| 2273 |
} |
| 2274 |
|
| 2275 |
// |
| 2276 |
// Can't attack run away. |
| 2277 |
// |
| 2278 |
if (CanMove(target)) { |
| 2279 |
int x; |
| 2280 |
int y; |
| 2281 |
int d; |
| 2282 |
|
| 2283 |
x = target->X - attacker->X; |
| 2284 |
y = target->Y - attacker->Y; |
| 2285 |
d = isqrt(x * x + y * y); |
| 2286 |
if (!d) { |
| 2287 |
d = 1; |
| 2288 |
} |
| 2289 |
x = target->X + (x * 5) / d + (SyncRand() & 3); |
| 2290 |
if (x < 0) { |
| 2291 |
x = 0; |
| 2292 |
} else if (x >= Map.Info.MapWidth) { |
| 2293 |
x = Map.Info.MapWidth - 1; |
| 2294 |
} |
| 2295 |
y = target->Y + (y * 5) / d + (SyncRand() & 3); |
| 2296 |
if (y < 0) { |
| 2297 |
y = 0; |
| 2298 |
} else if (y >= Map.Info.MapHeight) { |
| 2299 |
y = Map.Info.MapHeight - 1; |
| 2300 |
} |
| 2301 |
CommandStopUnit(target); |
| 2302 |
CommandMove(target, x, y, 0); |
| 2303 |
} |
| 2304 |
} |
| 2305 |
|
| 2306 |
/*---------------------------------------------------------------------------- |
| 2307 |
-- Conflicts |
| 2308 |
----------------------------------------------------------------------------*/ |
| 2309 |
|
| 2310 |
/** |
| 2311 |
** Returns the map distance between two points. |
| 2312 |
** |
| 2313 |
** @param x1 X map tile position. |
| 2314 |
** @param y1 Y map tile position. |
| 2315 |
** @param x2 X map tile position. |
| 2316 |
** @param y2 Y map tile position. |
| 2317 |
** |
| 2318 |
** @return The distance between in tiles. |
| 2319 |
*/ |
| 2320 |
int MapDistance(int x1, int y1, int x2, int y2) |
| 2321 |
{ |
| 2322 |
return isqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); |
| 2323 |
} |
| 2324 |
|
| 2325 |
/** |
| 2326 |
** Returns the map distance between two points with unit type. |
| 2327 |
** |
| 2328 |
** @param x1 X map tile position. |
| 2329 |
** @param y1 Y map tile position. |
| 2330 |
** @param type Unit type to take into account. |
| 2331 |
** @param x2 X map tile position. |
| 2332 |
** @param y2 Y map tile position. |
| 2333 |
** |
| 2334 |
** @return The distance between in tiles. |
| 2335 |
*/ |
| 2336 |
int MapDistanceToType(int x1, int y1, const CUnitType *type, int x2, int y2) |
| 2337 |
{ |
| 2338 |
int dx; |
| 2339 |
int dy; |
| 2340 |
|
| 2341 |
if (x1 <= x2) { |
| 2342 |
dx = x2 - x1; |
| 2343 |
} else { |
| 2344 |
dx = x1 - x2 - type->TileWidth + 1; |
| 2345 |
if (dx < 0) { |
| 2346 |
dx = 0; |
| 2347 |
} |
| 2348 |
} |
| 2349 |
|
| 2350 |
if (y1 <= y2) { |
| 2351 |
dy = y2 - y1; |
| 2352 |
} else { |
| 2353 |
dy = y1 - y2 - type->TileHeight + 1; |
| 2354 |
if (dy < 0) { |
| 2355 |
dy = 0; |
| 2356 |
} |
| 2357 |
} |
| 2358 |
|
| 2359 |
return isqrt(dy * dy + dx * dx); |
| 2360 |
} |
| 2361 |
|
| 2362 |
/** |
| 2363 |
** Returns the map distance to unit. |
| 2364 |
** |
| 2365 |
** @param x X map tile position. |
| 2366 |
** @param y Y map tile position. |
| 2367 |
** @param dest Distance to this unit. |
| 2368 |
** |
| 2369 |
** @return The distance between in tiles. |
| 2370 |
*/ |
| 2371 |
int MapDistanceToUnit(int x, int y, const CUnit *dest) |
| 2372 |
{ |
| 2373 |
return MapDistanceToType(x, y, dest->Type, dest->X, dest->Y); |
| 2374 |
} |
| 2375 |
|
| 2376 |
/** |
| 2377 |
** Returns the map distance between two units. |
| 2378 |
** |
| 2379 |
** @param src Distance from this unit. |
| 2380 |
** @param dst Distance to this unit. |
| 2381 |
** |
| 2382 |
** @return The distance between in tiles. |
| 2383 |
*/ |
| 2384 |
int MapDistanceBetweenUnits(const CUnit *src, const CUnit *dst) |
| 2385 |
{ |
| 2386 |
return MapDistanceBetweenTypes(src->Type, src->X, src->Y, |
| 2387 |
dst->Type, dst->X, dst->Y); |
| 2388 |
} |
| 2389 |
|
| 2390 |
/** |
| 2391 |
** Returns the map distance between two points with unit type. |
| 2392 |
** |
| 2393 |
** @param src src unittype |
| 2394 |
** @param x1 X map tile position of src (upperleft). |
| 2395 |
** @param y1 Y map tile position of src. |
| 2396 |
** @param dst Unit type to take into account. |
| 2397 |
** @param x2 X map tile position of dst. |
| 2398 |
** @param y2 Y map tile position of dst. |
| 2399 |
** |
| 2400 |
** @return The distance between the types. |
| 2401 |
*/ |
| 2402 |
int MapDistanceBetweenTypes(const CUnitType *src, int x1, int y1, const CUnitType *dst, int x2, int y2) |
| 2403 |
{ |
| 2404 |
int dx; |
| 2405 |
int dy; |
| 2406 |
|
| 2407 |
if (x1 + src->TileWidth <= x2) { |
| 2408 |
dx = x2 - x1 - src->TileWidth + 1; |
| 2409 |
if (dx < 0) { |
| 2410 |
dx = 0; |
| 2411 |
} |
| 2412 |
} else { |
| 2413 |
dx = x1 - x2 - dst->TileWidth + 1; |
| 2414 |
if (dx < 0) { |
| 2415 |
dx = 0; |
| 2416 |
} |
| 2417 |
} |
| 2418 |
|
| 2419 |
if (y1 + src->TileHeight <= y2) { |
| 2420 |
dy = y2 - y1 - src->TileHeight + 1; |
| 2421 |
} else { |
| 2422 |
dy = y1 - y2 - dst->TileHeight + 1; |
| 2423 |
if (dy < 0) { |
| 2424 |
dy = 0; |
| 2425 |
} |
| 2426 |
} |
| 2427 |
|
| 2428 |
return isqrt(dy * dy + dx * dx); |
| 2429 |
} |
| 2430 |
|
| 2431 |
/** |
| 2432 |
** Compute the distance from the view point to a given point. |
| 2433 |
** |
| 2434 |
** @param x X map tile position. |
| 2435 |
** @param y Y map tile position. |
| 2436 |
** |
| 2437 |
** @todo FIXME: is it the correct place to put this function in? |
| 2438 |
*/ |
| 2439 |
int ViewPointDistance(int x, int y) |
| 2440 |
{ |
| 2441 |
const CViewport *vp; |
| 2442 |
|
| 2443 |
// first compute the view point coordinate |
| 2444 |
vp = UI.SelectedViewport; |
| 2445 |
|
| 2446 |
// then use MapDistance |
| 2447 |
return MapDistance(vp->MapX + vp->MapWidth / 2, |
| 2448 |
vp->MapY + vp->MapHeight / 2, x, y); |
| 2449 |
} |
| 2450 |
|
| 2451 |
/** |
| 2452 |
** Compute the distance from the view point to a given unit. |
| 2453 |
** |
| 2454 |
** @param dest Distance to this unit. |
| 2455 |
** |
| 2456 |
** @todo FIXME: is it the correct place to put this function in? |
| 2457 |
*/ |
| 2458 |
int ViewPointDistanceToUnit(const CUnit *dest) |
| 2459 |
{ |
| 2460 |
const CViewport *vp = UI.SelectedViewport;; |
| 2461 |
return MapDistanceToUnit(vp->MapX + vp->MapWidth / 2, |
| 2462 |
vp->MapY + vp->MapHeight / 2, dest); |
| 2463 |
} |
| 2464 |
|
| 2465 |
/** |
| 2466 |
** Can the source unit attack the destination unit. |
| 2467 |
** |
| 2468 |
** @param source Unit type pointer of the attacker. |
| 2469 |
** @param dest Unit type pointer of the target. |
| 2470 |
** |
| 2471 |
** @return 0 if attacker can't target the unit, else a positive number. |
| 2472 |
*/ |
| 2473 |
int CanTarget(const CUnitType *source, const CUnitType *dest) |
| 2474 |
{ |
| 2475 |
switch (dest->UnitType) { |
| 2476 |
case UnitTypeLand: |
| 2477 |
case UnitTypeNaval: |
| 2478 |
// A building that straddles the shoreline should be |
| 2479 |
// targetable with both CanTargetLand and |
| 2480 |
// CanTargetSea, even if it isn't a ShoreBuilding. |
| 2481 |
// (ShoreBuilding would require at least one |
| 2482 |
// MapFieldCoastAllowed under the building, which is |
| 2483 |
// not feasible with some patch sets.) |
| 2484 |
// |
| 2485 |
// To support such units, treat UnitTypeLand and |
| 2486 |
// UnitTypeNaval as equivalent, and instead examine |
| 2487 |
// MovementMask. Another possibility would be to |
| 2488 |
// look at the map fields under the individual |
| 2489 |
// CUnit, but that would be slower and could cause |
| 2490 |
// weird effects if an already targeted unit becomes |
| 2491 |
// untargetable as a result of moving from the land |
| 2492 |
// to the sea or vice versa. |
| 2493 |
// |
| 2494 |
// Ignore MapFieldCoastAllowed in ~MovementMask |
| 2495 |
// because it is typically used by transporter ships |
| 2496 |
// that CanTargetLand should not cover and |
| 2497 |
// CanTargetSea already covers by virtue of the |
| 2498 |
// other water flags. |
| 2499 |
if ((source->CanTarget & CanTargetLand) |
| 2500 |
&& (~dest->MovementMask & MapFieldLandAllowed)) |
| 2501 |
return 1; |
| 2502 |
if ((source->CanTarget & CanTargetSea) |
| 2503 |
&& (~dest->MovementMask & (MapFieldShallowWater | MapFieldDeepWater))) |
| 2504 |
return 1; |
| 2505 |
return 0; |
| 2506 |
|
| 2507 |
case UnitTypeFly: |
| 2508 |
return source->CanTarget & CanTargetAir; |
| 2509 |
|
| 2510 |
default: |
| 2511 |
return 0; |
| 2512 |
} |
| 2513 |
} |
| 2514 |
|
| 2515 |
/** |
| 2516 |
** Can the transporter transport the other unit. |
| 2517 |
** |
| 2518 |
** @param transporter Unit which is the transporter. |
| 2519 |
** @param unit Unit which wants to go in the transporter. |
| 2520 |
** |
| 2521 |
** @return 1 if transporter can transport unit, 0 else. |
| 2522 |
*/ |
| 2523 |
int CanTransport(const CUnit *transporter, const CUnit *unit) |
| 2524 |
{ |
| 2525 |
if (!transporter->Type->CanTransport) { |
| 2526 |
return 0; |
| 2527 |
} |
| 2528 |
if (transporter->Orders[0]->Action == UnitActionBuilt) { // Under construction |
| 2529 |
return 0; |
| 2530 |
} |
| 2531 |
if (transporter == unit) { // Cannot transporter itself. |
| 2532 |
return 0; |
| 2533 |
} |
| 2534 |
if (transporter->BoardCount >= transporter->Type->MaxOnBoard) { // full |
| 2535 |
return 0; |
| 2536 |
} |
| 2537 |
// FIXME: remove UnitTypeLand requirement |
| 2538 |
if (unit->Type->UnitType != UnitTypeLand) { |
| 2539 |
return 0; |
| 2540 |
} |
| 2541 |
// Can transport only allied unit. |
| 2542 |
// FIXME : should be parametrable. |
| 2543 |
if (!transporter->IsTeamed(unit)) { |
| 2544 |
return 0; |
| 2545 |
} |
| 2546 |
// Only organic units |
| 2547 |
// FIXME: should we add a CanBeTransported flag instead? |
| 2548 |
if (!unit->Type->Organic) { |
| 2549 |
return 0; |
| 2550 |
} |
| 2551 |
return 1; |
| 2552 |
} |
| 2553 |
|
| 2554 |
/** |
| 2555 |
** Check if the player is an enemy |
| 2556 |
** |
| 2557 |
** @param x Player to check |
| 2558 |
*/ |
| 2559 |
bool CUnit::IsEnemy(const CPlayer *x) const |
| 2560 |
{ |
| 2561 |
return (this->Player->Enemy & (1 << x->Index)) != 0; |
| 2562 |
} |
| 2563 |
|
| 2564 |
/** |
| 2565 |
** Check if the unit is an enemy |
| 2566 |
** |
| 2567 |
** @param x Unit to check |
| 2568 |
*/ |
| 2569 |
bool CUnit::IsEnemy(const CUnit *x) const |
| 2570 |
{ |
| 2571 |
return IsEnemy(x->Player); |
| 2572 |
} |
| 2573 |
|
| 2574 |
/** |
| 2575 |
** Check if the player is an ally |
| 2576 |
** |
| 2577 |
** @param x Player to check |
| 2578 |
*/ |
| 2579 |
bool CUnit::IsAllied(const CPlayer *x) const |
| 2580 |
{ |
| 2581 |
return Player->IsAllied(x); |
| 2582 |
} |
| 2583 |
|
| 2584 |
/** |
| 2585 |
** Check if the unit is an ally |
| 2586 |
** |
| 2587 |
** @param x Unit to check |
| 2588 |
*/ |
| 2589 |
bool CUnit::IsAllied(const CUnit *x) const |
| 2590 |
{ |
| 2591 |
return IsAllied(x->Player); |
| 2592 |
} |
| 2593 |
|
| 2594 |
/** |
| 2595 |
** Check if unit shares vision with the player |
| 2596 |
** |
| 2597 |
** @param x Player to check |
| 2598 |
*/ |
| 2599 |
bool CUnit::IsSharedVision(const CPlayer *x) const |
| 2600 |
{ |
| 2601 |
return (this->Player->SharedVision & (1 << x->Index)) != 0; |
| 2602 |
} |
| 2603 |
|
| 2604 |
/** |
| 2605 |
** Check if the unit shares vision with the unit |
| 2606 |
** |
| 2607 |
** @param x Unit to check |
| 2608 |
*/ |
| 2609 |
bool CUnit::IsSharedVision(const CUnit *x) const |
| 2610 |
{ |
| 2611 |
return IsSharedVision(x->Player); |
| 2612 |
} |
| 2613 |
|
| 2614 |
/** |
| 2615 |
** Check if both players share vision |
| 2616 |
** |
| 2617 |
** @param x Player to check |
| 2618 |
*/ |
| 2619 |
bool CUnit::IsBothSharedVision(const CPlayer *x) const |
| 2620 |
{ |
| 2621 |
return (this->Player->SharedVision & (1 << x->Index)) != 0 && |
| 2622 |
(x->SharedVision & (1 << this->Player->Index)) != 0; |
| 2623 |
} |
| 2624 |
|
| 2625 |
/** |
| 2626 |
** Check if both units share vision |
| 2627 |
** |
| 2628 |
** @param x Unit to check |
| 2629 |
*/ |
| 2630 |
bool CUnit::IsBothSharedVision(const CUnit *x) const |
| 2631 |
{ |
| 2632 |
return IsBothSharedVision(x->Player); |
| 2633 |
} |
| 2634 |
|
| 2635 |
/** |
| 2636 |
** Check if the player is on the same team |
| 2637 |
** |
| 2638 |
** @param x Player to check |
| 2639 |
*/ |
| 2640 |
bool CUnit::IsTeamed(const CPlayer *x) const |
| 2641 |
{ |
| 2642 |
return (this->Player->Team == x->Team); |
| 2643 |
} |
| 2644 |
|
| 2645 |
/** |
| 2646 |
** Check if the unit is on the same team |
| 2647 |
** |
| 2648 |
** @param x Unit to check |
| 2649 |
*/ |
| 2650 |
bool CUnit::IsTeamed(const CUnit *x) const |
| 2651 |
{ |
| 2652 |
return this->IsTeamed(x->Player); |
| 2653 |
} |
| 2654 |
|
| 2655 |
/** |
| 2656 |
** Check if the unit is unusable (for attacking...) |
| 2657 |
** @todo look if correct used (UnitActionBuilt is no problem if attacked)? |
| 2658 |
*/ |
| 2659 |
bool CUnit::IsUnusable() const |
| 2660 |
{ |
| 2661 |
return this->Removed || this->Orders[0]->Action == UnitActionDie || |
| 2662 |
this->Orders[0]->Action == UnitActionBuilt || this->Destroyed; |
| 2663 |
} |
| 2664 |
|
| 2665 |
|
| 2666 |
/*---------------------------------------------------------------------------- |
| 2667 |
-- Initialize/Cleanup |
| 2668 |
----------------------------------------------------------------------------*/ |
| 2669 |
|
| 2670 |
/** |
| 2671 |
** Initialize unit module. |
| 2672 |
*/ |
| 2673 |
void InitUnits(void) |
| 2674 |
{ |
| 2675 |
if (!SaveGameLoading) { |
| 2676 |
NumUnits = 0; |
| 2677 |
UnitManager.Init(); |
| 2678 |
} |
| 2679 |
} |
| 2680 |
|
| 2681 |
/** |
| 2682 |
** Clean up unit module. |
| 2683 |
*/ |
| 2684 |
void CleanUnits(void) |
| 2685 |
{ |
| 2686 |
CUnit **table; |
| 2687 |
|
| 2688 |
// |
| 2689 |
// Free memory for all units in unit table. |
| 2690 |
// |
| 2691 |
for (table = Units; table < &Units[NumUnits]; ++table) { |
| 2692 |
delete[] (*table)->AutoCastSpell; |
| 2693 |
delete[] (*table)->Variable; |
| 2694 |
for (std::vector<COrder *>::iterator order = (*table)->Orders.begin(); order != (*table)->Orders.end(); ++order) { |
| 2695 |
delete *order; |
| 2696 |
} |
| 2697 |
(*table)->Orders.clear(); |
| 2698 |
delete *table; |
| 2699 |
*table = NULL; |
| 2700 |
} |
| 2701 |
NumUnits = 0; |
| 2702 |
|
| 2703 |
UnitManager.Init(); |
| 2704 |
|
| 2705 |
HelpMeLastCycle = 0; |
| 2706 |
} |
| 2707 |
|
| 2708 |
//@} |