____                _       __
    / __ )____  _____   | |     / /___ ___________
   / __  / __ \/ ___/   | | /| / / __ `/ ___/ ___/
  / /_/ / /_/ (__  )    | |/ |/ / /_/ / /  (__  )
 /_____/\____/____/     |__/|__/\__,_/_/  /____/

       A futuristic real-time strategy game.
          This file is part of Bos Wars.
(C) Copyright 2001-2007 by the Bos Wars and Stratagus Project. Distributed under the "GNU General Public License"

unit_find.cpp File Reference

The find/select for units. More...

#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"

Go to the source code of this file.

Defines

#define PRIORITY_FACTOR   0x00010000
#define HEALTH_FACTOR   0x00000001
#define DISTANCE_FACTOR   0x00100000
#define INRANGE_FACTOR   0x00010000
#define INRANGE_BONUS   0x01000000
#define CANATTACK_BONUS   0x00100000

Functions

int FindUnitsByType (const CUnitType *type, CUnit **table, int tablesize)
 < Find all units of this type
int FindPlayerUnitsByType (const CPlayer *player, const CUnitType *type, CUnit **table, int tablesize)
 Return any unit on that map tile.
CUnitUnitOnMapTile (int tx, int ty, unsigned type)
 Return possible attack target on that map area.
CUnitTargetOnMap (const CUnit *source, int x1, int y1, int x2, int y2)
 Return resource, if on map tile.
CUnitResourceOnMap (int tx, int ty, int resource)
 Find best enemy in numeric range to attack.
static CUnitFindRangeAttack (const CUnit *u, int range)
static int CompareUnitDistance (const void *v1, const void *v2)
CUnitAttackUnitsInDistance (const CUnit *unit, int range)
 Find best enemy in attack range to attack.
CUnitAttackUnitsInRange (const CUnit *unit)
 Find best enemy in reaction range to attack.
CUnitAttackUnitsInReactRange (const CUnit *unit)

Variables

static const CUnitreferenceunit


Detailed Description

The find/select for units.

Definition in file unit_find.cpp.


Define Documentation

#define CANATTACK_BONUS   0x00100000

Definition at line 66 of file unit_find.cpp.

Referenced by AttackUnitsInDistance(), and FindRangeAttack().

#define DISTANCE_FACTOR   0x00100000

Definition at line 63 of file unit_find.cpp.

Referenced by AttackUnitsInDistance().

#define HEALTH_FACTOR   0x00000001

Definition at line 62 of file unit_find.cpp.

Referenced by AttackUnitsInDistance(), and FindRangeAttack().

#define INRANGE_BONUS   0x01000000

Definition at line 65 of file unit_find.cpp.

Referenced by AttackUnitsInDistance().

#define INRANGE_FACTOR   0x00010000

Definition at line 64 of file unit_find.cpp.

Referenced by AttackUnitsInDistance().

#define PRIORITY_FACTOR   0x00010000

Definition at line 61 of file unit_find.cpp.

Referenced by AttackUnitsInDistance(), and FindRangeAttack().


Function Documentation

CUnit* AttackUnitsInDistance ( const CUnit unit,
int  range 
)

CUnit* AttackUnitsInRange ( const CUnit unit  ) 

Find best enemy in reaction range to attack.

Attack units in attack range.

Parameters:
unit Find unit in attack range for this unit.
Returns:
Pointer to unit which should be attacked.

Definition at line 677 of file unit_find.cpp.

References Assert, ATTACKRANGE_INDEX, AttackUnitsInDistance(), CUnitType::CanAttack, CVariable::Max, CUnit::Stats, CUnit::Type, and CUnitStats::Variables.

Referenced by AutoAttack().

CUnit* AttackUnitsInReactRange ( const CUnit unit  ) 

Attack units in reaction range.

Parameters:
unit Find unit in reaction range for this unit.
Returns:
Pointer to unit which should be attacked.

Definition at line 690 of file unit_find.cpp.

References Assert, AttackUnitsInDistance(), CUnitType::CanAttack, CUnit::Player, PlayerPerson, CUnitType::ReactRangeComputer, CUnitType::ReactRangePerson, CPlayer::Type, and CUnit::Type.

Referenced by AiForceManager(), AttackTarget(), AutoAttack(), CheckForTargetInRange(), HandleActionFollow(), HandleActionPatrol(), and HitUnit().

static int CompareUnitDistance ( const void *  v1,
const void *  v2 
) [static]

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.

Definition at line 531 of file unit_find.cpp.

References MapDistanceBetweenUnits(), and CUnit::Slot.

Referenced by AttackUnitsInDistance().

int FindPlayerUnitsByType ( const CPlayer player,
const CUnitType type,
CUnit **  table,
int  tablesize 
)

Return any unit on that map tile.

Find all units of type.

Parameters:
player we're looking for the units of this player
type type of unit requested
table table in which we have to store the units
tablesize size of table array
Returns:
Returns the number of units found.

Definition at line 108 of file unit_find.cpp.

References CUnit::IsUnusable(), CUnitType::Slot, CPlayer::TotalNumUnits, CUnit::Type, CPlayer::Units, and CPlayer::UnitTypesCount.

Referenced by AiBuildBuilding(), AiRepairBuilding(), and AiTrainUnit().

static CUnit* FindRangeAttack ( const CUnit u,
int  range 
) [static]

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

Parameters:
u Find in distance for this unit.
range Distance range to look.
Returns:
Unit to be attacked.
Note:
This could be improved, for better performance / better trade.

Limited to attack range smaller than 16.

Will be moved to unit_ai.c soon.

Definition at line 254 of file unit_find.cpp.

References Assert, ATTACKRANGE_INDEX, BASICDAMAGE_INDEX, CANATTACK_BONUS, CanTarget(), CUnit::Container, HEALTH_FACTOR, HP_INDEX, CPlayer::IsEnemy(), CUnit::IsVisibleAsGoal(), MapDistanceBetweenUnits(), CVariable::Max, MissileConfig::Missile, CUnitType::Missile, NoUnitP, PIERCINGDAMAGE_INDEX, CUnit::Player, CUnitType::Priority, PRIORITY_FACTOR, MissileType::Range, CUnit::Removed, CUnitCache::Select(), CUnit::Stats, CUnitType::TileHeight, CUnitType::TileWidth, CUnit::Type, UnitCache, UnitMax, UnitReachable(), CVariable::Value, CUnit::Variable, CUnitStats::Variables, CUnit::X, CUnit::Y, and y.

Referenced by AttackUnitsInDistance().

int FindUnitsByType ( const CUnitType type,
CUnit **  table,
int  tablesize 
)

< Find all units of this type

Find all units of type.

Parameters:
type type of unit requested
table table in which we have to store the units
tablesize size of table array
Returns:
Returns the number of units found.

Definition at line 85 of file unit_find.cpp.

References CUnit::IsUnusable(), NumUnits, CUnit::Type, and Units.

Referenced by CclIfNearUnit(), and CclIfRescuedNearUnit().

CUnit* ResourceOnMap ( int  tx,
int  ty,
int  resource 
)

Find best enemy in numeric range to attack.

Resource on map tile

Parameters:
tx X position on map, tile-based.
ty Y position on map, tile-based.
resource Resource number or -1 for any.
Returns:
Returns the resource if found, or NoUnitP.

Definition at line 211 of file unit_find.cpp.

References NoUnitP, CUnitCache::Select(), UnitCache, UnitHoldsResources(), and UnitMax.

Referenced by UnitFindResource().

CUnit* TargetOnMap ( const CUnit source,
int  x1,
int  y1,
int  x2,
int  y2 
)

Return resource, if on map tile.

Choose target on map area.

Parameters:
source Unit which want to attack.
x1 X position on map, tile-based.
y1 Y position on map, tile-based.
x2 X position on map, tile-based.
y2 Y position on map, tile-based.
Returns:
Returns ideal target on map tile.

Definition at line 168 of file unit_find.cpp.

References CanTarget(), CUnit::IsVisibleAsGoal(), NoUnitP, CUnit::Player, CUnitType::Priority, CUnitCache::Select(), CUnitType::TileHeight, CUnitType::TileWidth, CUnit::Type, UnitCache, UnitMax, CUnit::X, and CUnit::Y.

Referenced by CclOrderUnit().

CUnit* UnitOnMapTile ( int  tx,
int  ty,
unsigned  type 
)

Return possible attack target on that map area.

Unit on map tile.

Parameters:
tx X position on map, tile-based.
ty Y position on map, tile-based.
type UnitTypeType, (unsigned)-1 for any type.
Returns:
Returns first found unit on tile.

Definition at line 136 of file unit_find.cpp.

References NoUnitP, CUnitCache::Select(), UnitCache, and UnitMax.

Referenced by ClosestFreeCoast(), CostMoveTo(), EditorRedoAction(), EditorUndoAction(), UIHandleButtonDown(), and UIHandleMouseMove().


Variable Documentation

const CUnit* referenceunit [static]

Reference unit used by CompareUnitDistance.

Definition at line 524 of file unit_find.cpp.


Generated on Sat Feb 21 00:28:22 2009 for Bos Wars by  doxygen 1.5.6