1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
|
// ____ _ __
// / __ )____ _____ | | / /___ ___________
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
// /_____/\____/____/ |__/|__/\__,_/_/ /____/
//
// A futuristic real-time strategy game.
// This file is part of Bos Wars.
//
/**@name unit_find.cpp - The find/select for units. */
//
// (c) Copyright 1998-2008 by Lutz Sammer and Jimmy Salmon
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; only version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//@{
/*----------------------------------------------------------------------------
-- Includes
----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include "stratagus.h"
#include "video.h"
#include "sound.h"
#include "unitsound.h"
#include "unittype.h"
#include "actions.h"
#include "player.h"
#include "missile.h"
#include "unit.h"
#include "unit_cache.h"
#include "interface.h"
#include "map.h"
#include "pathfinder.h"
/*----------------------------------------------------------------------------
-- Defines
----------------------------------------------------------------------------*/
/*
** Configuration of the small (unit) AI.
*/
#define PRIORITY_FACTOR 0x00010000
#define HEALTH_FACTOR 0x00000001
#define DISTANCE_FACTOR 0x00100000
#define INRANGE_FACTOR 0x00010000
#define INRANGE_BONUS 0x01000000
#define CANATTACK_BONUS 0x00100000
/*----------------------------------------------------------------------------
-- Local Data
----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
-- Functions
----------------------------------------------------------------------------*/
/**
** Find all units of type.
**
** @param type type of unit requested
** @param table table in which we have to store the units
** @param tablesize size of table array
**
** @return Returns the number of units found.
*/
int FindUnitsByType(const CUnitType *type, CUnit **table, int tablesize)
{
int num = 0;
for (int i = 0; i < NumUnits && num < tablesize; ++i) {
CUnit *unit = Units[i];
if (unit->Type == type && !unit->IsUnusable()) {
table[num++] = unit;
}
}
return num;
}
/**
** Find all units of type.
**
** @param player we're looking for the units of this player
** @param type type of unit requested
** @param table table in which we have to store the units
** @param tablesize size of table array
**
** @return Returns the number of units found.
*/
int FindPlayerUnitsByType(const CPlayer *player, const CUnitType *type,
CUnit **table, int tablesize)
{
int nunits = player->TotalNumUnits;
int typecount = player->UnitTypesCount[type->Slot];
int num = 0;
for (int i = 0; i < nunits && typecount && num < tablesize; ++i) {
CUnit *unit = player->Units[i];
if (unit->Type == type) {
if (!unit->IsUnusable()) {
table[num++] = unit;
}
--typecount;
}
}
return num;
}
/**
** Unit on map tile.
**
** @param tx X position on map, tile-based.
** @param ty Y position on map, tile-based.
** @param type UnitTypeType, (unsigned)-1 for any type.
**
** @return Returns first found unit on tile.
*/
CUnit *UnitOnMapTile(int tx, int ty, unsigned type)
{
CUnit *table[UnitMax];
int n;
n = UnitCache.Select(tx, ty, table, UnitMax);
for (int i = 0; i < n; ++i) {
// Note: this is less restrictive than UnitActionDie...
// Is it normal?
if (table[i]->Type->Vanishes) {
continue;
}
if (type != (unsigned)-1 && (unsigned)table[i]->Type->UnitType != type) {
continue;
}
return table[i];
}
return NoUnitP;
}
/**
** Choose target on map area.
**
** @param source Unit which want to attack.
** @param x1 X position on map, tile-based.
** @param y1 Y position on map, tile-based.
** @param x2 X position on map, tile-based.
** @param y2 Y position on map, tile-based.
**
** @return Returns ideal target on map tile.
*/
CUnit *TargetOnMap(const CUnit *source, int x1, int y1, int x2, int y2)
{
CUnit *table[UnitMax];
CUnit *best = NoUnitP;
int n;
n = UnitCache.Select(x1, y1, x2, y2, table, UnitMax);
for (int i = 0; i < n; ++i) {
CUnit *unit = table[i];
if (!unit->IsVisibleAsGoal(source->Player)) {
continue;
}
if (x2 < unit->X || x1 >= unit->X + unit->Type->TileWidth ||
y2 < unit->Y || y1 >= unit->Y + unit->Type->TileHeight) {
continue;
}
if (!CanTarget(source->Type, unit->Type)) {
continue;
}
//
// Choose the best target.
//
if (!best || best->Type->Priority < unit->Type->Priority) {
best = unit;
}
}
return best;
}
/*----------------------------------------------------------------------------
-- Finding special units
----------------------------------------------------------------------------*/
/**
** Resource on map tile
**
** @param tx X position on map, tile-based.
** @param ty Y position on map, tile-based.
** @param resource Resource number or -1 for any.
**
** @return Returns the resource if found, or NoUnitP.
*/
CUnit *ResourceOnMap(int tx, int ty, int resource)
{
CUnit *table[UnitMax];
int n;
n = UnitCache.Select(tx, ty, table, UnitMax);
for (int i = 0; i < n; ++i) {
if (table[i]->IsUnusable() || !table[i]->Type->CanHarvestFrom) {
continue;
}
if (resource == -1) {
if (!UnitHoldsResources(table[i])) {
continue;
}
} else {
if (table[i]->ResourcesHeld[resource] == 0) {
continue;
}
}
return table[i];
}
return NoUnitP;
}
/*----------------------------------------------------------------------------
-- Finding units for attack
----------------------------------------------------------------------------*/
/**
** Attack units in distance, with large missile
**
** Choose the best target, that can be attacked. It takes into
** account allied unit which could be hit by the missile
**
** @param u Find in distance for this unit.
** @param range Distance range to look.
**
** @return Unit to be attacked.
**
** @note This could be improved, for better performance / better trade.
** @note Limited to attack range smaller than 16.
** @note Will be moved to unit_ai.c soon.
*/
static CUnit *FindRangeAttack(const CUnit *u, int range)
{
int x;
int y;
int n;
int cost;
int d;
int effective_hp;
int enemy_count;
int missile_range;
int attackrange;
int hp_damage_evaluate;
int good[32][32];
int bad[32][32];
CUnit *table[UnitMax];
CUnit *dest;
const CUnitType *dtype;
const CUnitType *type;
const CPlayer *player;
int xx;
int yy;
int best_cost;
int i;
int sbad;
int sgood;
CUnit *best;
type = u->Type;
player = u->Player;
// If catapult, count units near the target...
// FIXME : make it configurable
//
missile_range = type->Missile.Missile->Range + range - 1;
attackrange = u->Stats->Variables[ATTACKRANGE_INDEX].Max;
// Evaluation of possible damage...
hp_damage_evaluate = u->Stats->Variables[BASICDAMAGE_INDEX].Value
+ u->Stats->Variables[PIERCINGDAMAGE_INDEX].Value;
Assert(2 * missile_range + 1 < 32);
//
// If unit is removed, use containers x and y
if (u->Removed) {
x = u->Container->X;
y = u->Container->Y;
n = UnitCache.Select(x - missile_range, y - missile_range,
x + missile_range + u->Container->Type->TileWidth,
y + missile_range + u->Container->Type->TileHeight, table, UnitMax);
} else {
x = u->X;
y = u->Y;
n = UnitCache.Select(x - missile_range, y - missile_range,
x + missile_range + u->Type->TileWidth,
y + missile_range + u->Type->TileHeight, table, UnitMax);
}
if (!n) {
return NoUnitP;
}
for (y = 0; y < 2 * missile_range + 1; ++y) {
for (x = 0; x < 2 * missile_range + 1; ++x) {
good[y][x] = 0;
bad[y][x] = 0;
}
}
enemy_count = 0;
// FILL good/bad...
for (i = 0; i < n; ++i) {
dest = table[i];
dtype = dest->Type;
if (!dest->IsVisibleAsGoal(u->Player)) {
table[i] = 0;
continue;
}
// won't be a target...
if (!CanTarget(type, dtype)) { // can't be attacked.
table[i] = 0;
continue;
}
if (!player->IsEnemy(dest)) { // a friend or neutral
table[i] = 0;
// Calc a negative cost
// The gost is more important when the unit would be killed
// by our fire.
// It costs (is positive) if hp_damage_evaluate>dest->HP ...)
// FIXME : assume that PRIORITY_FACTOR>HEALTH_FACTOR
cost = HEALTH_FACTOR * (2 * hp_damage_evaluate - dest->Variable[HP_INDEX].Value) /
(dtype->TileWidth * dtype->TileWidth);
if (cost < 1) {
cost = 1;
}
cost = (-cost);
} else {
//
// Calculate the costs to attack the unit.
// Unit with the smallest attack costs will be taken.
//
cost = 0;
//
// Priority 0-255
//
cost += dtype->Priority * PRIORITY_FACTOR;
//
// Remaining HP (Health) 0-65535
//
// Give a boost to unit we can kill in one shoot only
//
// calculate HP which will remain in the enemy unit, after hit
//
effective_hp = (dest->Variable[HP_INDEX].Value - 2 * hp_damage_evaluate);
//
// Unit we won't kill are evaluated the same
//
if (effective_hp > 0) {
effective_hp = 0;
}
//
// Unit we are sure to kill are all evaluated the same (except PRIORITY)
//
if (effective_hp < -hp_damage_evaluate) {
effective_hp = -hp_damage_evaluate;
}
//
// Here, effective_hp vary from -hp_damage_evaluate (unit will be killed) to 0 (unit can't be killed)
// => we prefer killing rather than only hitting...
//
cost += -effective_hp * HEALTH_FACTOR;
//
// Unit can attack back.
//
if (CanTarget(dtype, type)) {
cost += CANATTACK_BONUS;
}
//
// the cost may be divided accros multiple cells
//
cost = cost / (dtype->TileWidth * dtype->TileWidth);
if (cost < 1) {
cost = 1;
}
//
// Removed Unit's are in bunkers
//
if (u->Removed) {
d = MapDistanceBetweenUnits(u->Container, dest);
} else {
d = MapDistanceBetweenUnits(u, dest);
}
if (d <= attackrange || (d <= range && UnitReachable(u, dest, attackrange))) {
++enemy_count;
} else {
table[i] = 0;
}
}
x = dest->X - u->X + missile_range + 1;
y = dest->Y - u->Y + missile_range + 1;
// Mark the good/bad array...
for (xx = 0; xx < dtype->TileWidth; ++xx) {
for (yy = 0; yy < dtype->TileWidth; ++yy) {
if ((x + xx < 0) || (y + yy < 0) ||
(x + xx >= 2 * missile_range + 1) ||
(y + yy >= 2 * missile_range + 1)) {
continue;
}
if (cost < 0) {
good[y + yy][x + xx] -= cost;
} else {
bad[y + yy][x + xx] += cost;
}
}
}
}
if (!enemy_count) {
return NoUnitP;
}
// Find the best area...
// The target which provide the best bad/good ratio is choosen...
best_cost = -1;
best = NoUnitP;
for (i = 0; i < n; ++i) {
if (!table[i]) {
continue;
}
dest = table[i];
dtype = dest->Type;
// put in x-y the real point which will be hit...
// (only meaningful when dtype->TileWidth > 1)
if (u->X < dest->X) {
x = dest->X;
} else if (u->X > dest->X + dtype->TileWidth - 1) {
x = dest->X + dtype->TileWidth - 1;
} else {
x = u->X;
}
if (u->Y < dest->Y) {
y = dest->Y;
} else if (u->Y > dest->Y + dtype->TileHeight - 1) {
y = dest->Y + dtype->TileHeight - 1;
} else {
y = u->Y;
}
// Make x,y relative to u->x...
x = x - u->X + missile_range + 1;
y = y - u->Y + missile_range + 1;
sbad = 0;
sgood = 0;
for (yy = -(type->Missile.Missile->Range - 1);
yy <= type->Missile.Missile->Range - 1; ++yy) {
for (xx = -(type->Missile.Missile->Range - 1);
xx <= type->Missile.Missile->Range - 1; ++xx) {
if ((x + xx < 0) || (y + yy < 0) ||
((x + xx) >= 2 * missile_range + 1) ||
((y + yy) >= 2 * missile_range + 1)) {
continue;
}
sbad += bad[y + yy][x + xx];
sgood += good[y + yy][x + xx];
if (!yy && !xx) {
sbad += bad[y + yy][x + xx];
sgood += good[y + yy][x + xx];
}
}
}
// don't consider small damages...
if (sgood < 20) {
sgood = 20;
}
cost = sbad / sgood;
if (cost > best_cost) {
best_cost = cost;
best = dest;
}
}
return best;
}
/**
** Reference unit used by CompareUnitDistance.
*/
static const CUnit *referenceunit;
/**
** Returns a value less then 0, 0 or bigger then 0,
** when the first unit is repectively nearer, at the same distance
** or further away then the 2nd from the referenceunit.
*/
static int CompareUnitDistance(const void *v1, const void *v2)
{
CUnit *c1 = *(CUnit **)v1;
CUnit *c2 = *(CUnit **)v2;
int d1 = MapDistanceBetweenUnits(referenceunit, c1);
int d2 = MapDistanceBetweenUnits(referenceunit, c2);
if (d1 - d2 != 0) {
return d1 - d2;
} else {
return c1->Slot - c2->Slot;
}
}
/**
** Attack units in distance.
**
** If the unit can attack must be handled by caller.
** Choose the best target, that can be attacked.
**
** @param unit Find in distance for this unit.
** @param range Distance range to look.
**
** @return Unit to be attacked.
**
*/
CUnit *AttackUnitsInDistance(const CUnit *unit, int range)
{
CUnit *dest;
const CUnitType *type;
const CUnitType *dtype;
CUnit *table[UnitMax];
int x;
int y;
int n;
int i;
int d;
int attackrange;
int cost;
int best_cost;
const CPlayer *player;
CUnit *best_unit;
// if necessary, take possible damage on allied units into account...
if (unit->Type->Missile.Missile->Range > 1 &&
(range + unit->Type->Missile.Missile->Range < 15)) {
return FindRangeAttack(unit, range);
}
//
// Select all units in range.
//
x = unit->X;
y = unit->Y;
type = unit->Type;
n = UnitCache.Select(x - range, y - range, x + range + type->TileWidth,
y + range + type->TileHeight, table, UnitMax);
if (range > 25 && n > 9) {
referenceunit = unit;
qsort((void*)table, n, sizeof(CUnit*), &CompareUnitDistance);
}
best_unit = NoUnitP;
best_cost = INT_MAX;
player = unit->Player;
attackrange = unit->Stats->Variables[ATTACKRANGE_INDEX].Max;
//
// Find the best unit to attack
//
for (i = 0; i < n; ++i) {
dest = table[i];
if (!dest->IsVisibleAsGoal(unit->Player)) {
continue;
}
if (!player->IsEnemy(dest)) { // a friend or neutral
continue;
}
dtype = dest->Type;
if (!CanTarget(type, dtype)) { // can't be attacked.
continue;
}
//
// Calculate the costs to attack the unit.
// Unit with the smallest attack costs will be taken.
//
cost = 0;
//
// Priority 0-255
//
cost -= dtype->Priority * PRIORITY_FACTOR;
//
// Remaining HP (Health) 0-65535
//
cost += dest->Variable[HP_INDEX].Value * HEALTH_FACTOR;
//
// Unit in attack range?
//
d = MapDistanceBetweenUnits(unit, dest);
// Use Circle, not square :)
if (d > range) {
continue;
}
if (d <= attackrange && d >= type->MinAttackRange) {
cost += d * INRANGE_FACTOR;
cost -= INRANGE_BONUS;
} else {
cost += d * DISTANCE_FACTOR;
}
//
// Unit can attack back.
//
if (CanTarget(dtype, type)) {
cost -= CANATTACK_BONUS;
}
//
// Take this target?
//
if (cost < best_cost && (d <= attackrange ||
UnitReachable(unit, dest, attackrange))) {
best_unit = dest;
best_cost = cost;
}
}
return best_unit;
}
/**
** Attack units in attack range.
**
** @param unit Find unit in attack range for this unit.
**
** @return Pointer to unit which should be attacked.
*/
CUnit *AttackUnitsInRange(const CUnit *unit)
{
Assert(unit->Type->CanAttack);
return AttackUnitsInDistance(unit, unit->Stats->Variables[ATTACKRANGE_INDEX].Max);
}
/**
** Attack units in reaction range.
**
** @param unit Find unit in reaction range for this unit.
**
** @return Pointer to unit which should be attacked.
*/
CUnit *AttackUnitsInReactRange(const CUnit *unit)
{
int range;
Assert(unit->Type->CanAttack);
if (unit->Player->Type == PlayerPerson) {
range = unit->Type->ReactRangePerson;
} else {
range = unit->Type->ReactRangeComputer;
}
return AttackUnitsInDistance(unit, range);
}
//@}
|