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

       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.cpp File Reference

The units. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include "stratagus.h"
#include "unit.h"
#include "unit_manager.h"
#include "unit_cache.h"
#include "video.h"
#include "unitsound.h"
#include "unittype.h"
#include "animation.h"
#include "player.h"
#include "map.h"
#include "actions.h"
#include "sound_server.h"
#include "missile.h"
#include "interface.h"
#include "sound.h"
#include "ai.h"
#include "pathfinder.h"
#include "network.h"
#include "ui.h"
#include "script.h"
#include "editor.h"
#include "spells.h"
#include "luacallback.h"

Go to the source code of this file.

Functions

static void RemoveUnitFromContainer (CUnit *unit)
CUnitMakeUnit (CUnitType *type, CPlayer *player)
 Create a new unit and place on map.
static void MapMarkUnitSightRec (const CUnit *unit, int x, int y, int width, int height, MapMarkerFunc *f)
static CUnitGetFirstContainer (CUnit *unit)
void MapMarkUnitSight (CUnit *unit)
 Mark on vision table the Sight of the unit.
void MapUnmarkUnitSight (CUnit *unit)
 Unmark on vision table the Sight of the unit.
void UpdateUnitSightRange (CUnit *unit)
 Create a new unit.
void MarkUnitFieldFlags (const CUnit *unit)
 < Mark the field with the FieldFlags.
void UnmarkUnitFieldFlags (const CUnit *unit)
 Update unit->CurrentSightRange.
static void UnitInXY (CUnit *unit, int x, int y)
CUnitMakeUnitAndPlace (int x, int y, CUnitType *type, CPlayer *player)
 Handle the loss of a unit (food,...).
void UnitLost (CUnit *unit)
 Remove the Orders of a Unit.
void UnitClearOrders (CUnit *unit)
void UpdateForNewUnit (const CUnit *unit, int upgrade)
void NearestOfUnit (const CUnit *unit, int tx, int ty, int *dx, int *dy)
 Call when an Unit goes under fog.
static void UnitFillSeenValues (CUnit *unit)
void UnitGoesUnderFog (CUnit *unit, const CPlayer *player)
 Call when an Unit goes out of fog.
void UnitGoesOutOfFog (CUnit *unit, const CPlayer *player)
 Marks a unit as seen.
void UnitsOnTileMarkSeen (const CPlayer *player, int x, int y)
 Unmarks a unit as seen.
void UnitsOnTileUnmarkSeen (const CPlayer *player, int x, int y)
 Does a recount for VisCount.
void UnitCountSeen (CUnit *unit)
 Check for rescue each second.
void RescueUnits (void)
 Convert direction (dx,dy) to heading (0-255).
bool UnitHoldsResources (const CUnit *unit)
 Find resource.
static int myatan (int val)
int DirectionToHeading (int delta_x, int delta_y)
 Update frame from heading.
void UnitUpdateHeading (CUnit *unit)
 Heading and frame from delta direction x,y.
void UnitHeadingFromDeltaXY (CUnit *unit, int dx, int dy)
void DropOutOnSide (CUnit *unit, int heading, int addx, int addy)
void DropOutNearest (CUnit *unit, int gx, int gy, int addx, int addy)
 Drop out all units in the unit.
void DropOutAll (const CUnit *source)
 Return the rule used to build this building.
CUnitUnitFindResource (const CUnit *unit, int x, int y, int range, int resource)
 Find the next idle worker.
CUnitFindIdleWorker (const CPlayer *player)
CUnitUnitOnScreen (int x, int y)
 Check if a unit should be removed from UnitsConsumingResources.
void UnitRemoveConsumingResources (CUnit *unit)
 Let a unit die.
void LetUnitDie (CUnit *unit)
 Destory all units inside another unit.
void DestroyAllInside (CUnit *source)
 Hit unit with damage, if destroyed give attacker the points.
void HitUnit (CUnit *attacker, CUnit *target, int damage)
 Returns the map distance between two points.
int MapDistance (int x1, int y1, int x2, int y2)
 Returns the map distance between two points with unit-type.
int MapDistanceToType (int x1, int y1, const CUnitType *type, int x2, int y2)
 Returns the map distance to unit.
int MapDistanceToUnit (int x, int y, const CUnit *dest)
 Returns the map diestance between to unittype as locations.
int MapDistanceBetweenUnits (const CUnit *src, const CUnit *dst)
 Calculate the distance from current view point to coordinate.
int MapDistanceBetweenTypes (const CUnitType *src, int x1, int y1, const CUnitType *dst, int x2, int y2)
 Returns the map distance between two units.
int ViewPointDistance (int x, int y)
 Calculate the distance from current view point to unit.
int ViewPointDistanceToUnit (const CUnit *dest)
 Can this unit-type attack the other (destination).
int CanTarget (const CUnitType *source, const CUnitType *dest)
 Can transporter transport the other unit.
int CanTransport (const CUnit *transporter, const CUnit *unit)
 Check if unit can move.
void InitUnits (void)
 Clean unit module.
void CleanUnits (void)

Variables

CUnitUnits [MAX_UNIT_SLOTS]
 Array of used slots.
int NumUnits
 Number of slots used.
bool EnableBuildingCapture
 Config: capture buildings enabled.
static unsigned long HelpMeLastCycle
 Last cycle HelpMe sound played.
static int HelpMeLastX
 Last X coordinate HelpMe sound played.
static int HelpMeLastY
 Last Y coordinate HelpMe sound played.


Detailed Description

The units.

Definition in file unit.cpp.


Function Documentation

int CanTarget ( const CUnitType source,
const CUnitType dest 
)

Can transporter transport the other unit.

Can the source unit attack the destination unit.

Parameters:
source Unit type pointer of the attacker.
dest Unit type pointer of the target.
Returns:
0 if attacker can't target the unit, else a positive number.

Definition at line 2425 of file unit.cpp.

References CUnitType::CanTarget, CanTargetAir, CanTargetLand, CanTargetSea, CUnitType::ShoreBuilding, CUnitType::UnitType, UnitTypeFly, UnitTypeLand, and UnitTypeNaval.

Referenced by AiEnemyUnitsInDistance(), AttackUnitsInDistance(), DoRightButton(), EnemyOnMapTile(), FindRangeAttack(), HitUnit(), MissileHit(), SendAttack(), and TargetOnMap().

int CanTransport ( const CUnit transporter,
const CUnit unit 
)

Check if unit can move.

Can the transporter transport the other unit.

Parameters:
transporter Unit which is the transporter.
unit Unit which wants to go in the transporter.
Returns:
1 if transporter can transport unit, 0 else.

Definition at line 2450 of file unit.cpp.

References CUnit::BoardCount, CUnitType::CanTransport, CUnit::IsTeamed(), CUnitType::MaxOnBoard, CUnit::Orders, CUnitType::Organic, CUnit::Type, UnitActionBuilt, CUnitType::UnitType, and UnitTypeLand.

Referenced by DoRightButton(), SendMove(), and WaitForTransporter().

void CleanUnits ( void   ) 

Clean up unit module.

Definition at line 2611 of file unit.cpp.

References HelpMeLastCycle, CUnitManager::Init(), NumUnits, and UnitManager.

Referenced by CleanGame(), and CleanModules().

void DestroyAllInside ( CUnit source  ) 

Hit unit with damage, if destroyed give attacker the points.

Destroy all units inside unit.

Parameters:
source container.

Definition at line 2027 of file unit.cpp.

References DestroyAllInside(), CUnit::InsideCount, CUnit::NextContained, CUnit::Release(), UnitClearOrders(), CUnit::UnitInside, and UnitLost().

Referenced by DestroyAllInside(), and LetUnitDie().

int DirectionToHeading ( int  delta_x,
int  delta_y 
)

Update frame from heading.

Convert direction to heading.

Parameters:
delta_x Delta X.
delta_y Delta Y.
Returns:
Angle (0..255)

Definition at line 1424 of file unit.cpp.

References myatan().

Referenced by MissileNewHeadingFromXY(), StartBuilding(), and UnitHeadingFromDeltaXY().

void DropOutAll ( const CUnit source  ) 

Return the rule used to build this building.

Drop out all units inside unit.

Parameters:
source All units inside source are dropped out.

Definition at line 1663 of file unit.cpp.

References Assert, DropOutOnSide(), CUnit::InsideCount, LookingW, CUnit::NextContained, CUnit::Orders, CUnit::SubAction, CUnitType::TileHeight, CUnitType::TileWidth, CUnit::Type, UnitActionStill, and CUnit::UnitInside.

void DropOutNearest ( CUnit unit,
int  gx,
int  gy,
int  addx,
int  addy 
)

Drop out all units in the unit.

Place a unit on the map nearest to x, y.

Parameters:
unit Unit to drop out.
gx Goal X map tile position.
gy Goal Y map tile position.
addx Tile width of unit it's dropping out of.
addy Tile height of unit it's dropping out of.

Definition at line 1578 of file unit.cpp.

References Assert, CUnit::Container, MapDistance(), CUnit::Place(), CUnit::Removed, UnitCanBeAt(), CUnit::X, CUnit::Y, and y.

void DropOutOnSide ( CUnit unit,
int  heading,
int  addx,
int  addy 
)

Place a unit on the map to the side of a unit.

Parameters:
unit Unit to drop out.
heading Direction in which the unit should appear.
addx Tile width of unit it's dropping out of.
addy Tile height of unit it's dropping out of.

Definition at line 1502 of file unit.cpp.

References CUnit::Container, LookingNW, LookingSE, LookingSW, CUnit::Place(), UnitCanBeAt(), CUnit::X, CUnit::Y, and y.

Referenced by Summon::Cast(), CclCreateUnit(), CclMoveUnit(), DropOutAll(), HandleActionBuilt(), and HandleActionTrain().

CUnit* FindIdleWorker ( const CPlayer player  ) 

Find the next idle worker

Parameters:
player Player's units to search through
Returns:
NoUnitP or next idle worker

Definition at line 1816 of file unit.cpp.

References CUnitType::Harvester, IsOnlySelected, NoUnitP, CUnit::Orders, CUnit::Removed, CPlayer::TotalNumUnits, CUnit::Type, UnitActionStill, and CPlayer::Units.

Referenced by UiFindIdleWorker().

static CUnit* GetFirstContainer ( CUnit unit  )  [static]

Return the unit not transported, by viewing the container recursively.

Parameters:
unit unit from where look the first conatiner.
Returns:
Container of container of ... of unit. It is not null.

Definition at line 361 of file unit.cpp.

References CUnit::Container.

Referenced by MapMarkUnitSight(), and MapUnmarkUnitSight().

void HitUnit ( CUnit attacker,
CUnit target,
int  damage 
)

Returns the map distance between two points.

Unit is hit by missile or other damage.

Parameters:
attacker Unit that attacks.
target Unit that is hit.
damage How many damage to take.

Definition at line 2056 of file unit.cpp.

References _, COrder::Action, CPlayer::AiEnabled, AiHelpMe(), Assert, CUnit::Attacked, AttackUnitsInReactRange(), CUnitType::Building, CUnit::Burning, CUnitType::CanAttack, CanMove(), CanTarget(), CUnit::ChangeOwner(), CommandAttack(), CommandMove(), CommandStopUnit(), CUnitType::Coward, CYCLES_PER_SECOND, Missile::Damage, DamageMissile, DebugPrint, CVariable::Enable, EnableBuildingCapture, FlushCommands, GameCycle, GodMode, HelpMeLastCycle, HelpMeLastX, HelpMeLastY, HP_INDEX, CUnitType::Indestructible, CMap::Info, CUnit::IsEnemy(), isqrt(), CUnit::IsVisibleOnMap(), KILL_INDEX, LetUnitDie(), MakeLocalMissile(), MakeMissile(), Map, CMapInfo::MapHeight, CMapInfo::MapWidth, CVariable::Max, MissileBurningBuilding(), MissileTypeByIdent(), CUnitType::Name, CPlayer::Notify(), NotifyRed, NoUnitP, CUnit::Orders, CUnitType::Organic, CUnit::Player, PlayUnitSound(), CUnitType::Points, CUnit::RefsIncrease(), CUnit::Removed, CUnitType::RepairRange, ReplayRevealMap, CUnit::SavedOrder, CPlayer::Score, Missile::SourceUnit, SyncRand(), ThisPlayer, CUnitType::TileHeight, TileSizeX, TileSizeY, CUnitType::TileWidth, CPlayer::TotalKills, CPlayer::TotalRazings, CUnit::Type, UnitActionDie, UnitActionStill, CVariable::Value, CUnitType::Vanishes, CUnit::Variable, VoiceHelpMe, CUnit::X, y, and CUnit::Y.

Referenced by Capture::Cast(), AdjustVitals::Cast(), AreaAdjustVitals::Cast(), Demolish::Cast(), FireMissile(), HandleRegenerations(), and MissileHitsGoal().

void InitUnits ( void   ) 

Clean unit module.

Initialize unit module.

Definition at line 2600 of file unit.cpp.

References CUnitManager::Init(), NumUnits, SaveGameLoading, and UnitManager.

Referenced by InitModules().

void LetUnitDie ( CUnit unit  ) 

Destory all units inside another unit.

Let a unit die.

Parameters:
unit Unit to be destroyed.

Definition at line 1927 of file unit.cpp.

References CUnit::Anim, CUnitType::Animations, CUnit::_order_data_::Built, CUnitType::CanHarvestFrom, CUnitType::CorpseType, CUnit::CurrentSightRange, CUnit::Data, CAnimations::Death, CUnitType::DeathExplosion, DebugPrint, DestroyAllInside(), CUnitType::ExplodeWhenKilled, CUnitType::Explosion, CGraphic::Height, CUnitType::Height, CPlayer::Index, CUnitCache::Insert(), CUnit::IX, CUnit::IY, MakeMissile(), MapMarkUnitSight(), CVariable::Max, MaxCosts, MissileConfig::Missile, CUnit::Moving, CUnit::Orders, CUnit::Player, PlayUnitSound(), CUnit::ProductionEfficiency, CUnitType::ProductionRate, CPlayer::ProductionRate, LuaCallback::pushInteger(), LuaCallback::pushPreamble(), CUnit::Release(), CUnit::Remove(), CUnit::Removed, CUnit::ResourcesHeld, LuaCallback::run(), SIGHTRANGE_INDEX, CUnitType::Sprite, CUnit::State, CUnitType::Stats, CUnitType::StorageCapacity, CPlayer::StorageCapacity, CPlayer::StoredResources, CUnit::SubAction, CUnitType::TileHeight, TileSizeX, TileSizeY, CUnitType::TileWidth, CUnit::TTL, CUnit::Type, CUnit::_unit_anim_::Unbreakable, UnitActionBuilt, UnitActionDie, UnitCache, UnitClearOrders(), CUnit::UnitInside, UnitLost(), UnitRemoveConsumingResources(), CUnitStats::Variables, VoiceDying, CGraphic::Width, CUnitType::Width, CUnit::_order_data_::_order_built_::Worker, CUnit::X, and CUnit::Y.

Referenced by CclKillUnit(), CclKillUnitAt(), CommandDismiss(), GatherResource(), HandleActionBuilt(), HandleBuffs(), and HitUnit().

CUnit* MakeUnit ( CUnitType type,
CPlayer player 
)

Create a new unit and place on map.

Create a new unit.

Parameters:
type Pointer to unit-type.
player Pointer to owning player.
Returns:
Pointer to created unit.

Definition at line 302 of file unit.cpp.

References _C_, CUnitManager::AllocUnit(), CUnit::AssignToPlayer(), DebugPrint, CUnit::Init(), NoUnitP, NumUnits, UnitManager, and UnitMax.

Referenced by Summon::Cast(), CclCreateUnit(), HandleActionTrain(), MakeUnitAndPlace(), and StartBuilding().

CUnit* MakeUnitAndPlace ( int  x,
int  y,
CUnitType type,
CPlayer player 
)

Handle the loss of a unit (food,...).

Create new unit and place on map.

Parameters:
x X map tile position.
y Y map tile position.
type Pointer to unit-type.
player Pointer to owning player.
Returns:
Pointer to created unit.

Definition at line 636 of file unit.cpp.

References MakeUnit(), NoUnitP, and CUnit::Place().

Referenced by Polymorph::Cast(), SpawnPortal::Cast(), EditorActionPlaceUnit(), and UnitLost().

int MapDistance ( int  x1,
int  y1,
int  x2,
int  y2 
)

Returns the map distance between two points with unit-type.

Returns the map distance between two points.

Parameters:
x1 X map tile position.
y1 Y map tile position.
x2 X map tile position.
y2 Y map tile position.
Returns:
The distance between in tiles.

Definition at line 2272 of file unit.cpp.

References isqrt().

Referenced by DropOutNearest(), MissileInitMove(), NextMissileFrame(), and ViewPointDistance().

int MapDistanceBetweenTypes ( const CUnitType src,
int  x1,
int  y1,
const CUnitType dst,
int  x2,
int  y2 
)

Returns the map distance between two units.

Returns the map distance between two points with unit type.

Parameters:
src src unittype
x1 X map tile position of src (upperleft).
y1 Y map tile position of src.
dst Unit type to take into account.
x2 X map tile position of dst.
y2 Y map tile position of dst.
Returns:
The distance between the types.

Definition at line 2354 of file unit.cpp.

References isqrt(), CUnitType::TileHeight, and CUnitType::TileWidth.

Referenced by CBuildRestrictionDistance::Check(), and MapDistanceBetweenUnits().

int MapDistanceBetweenUnits ( const CUnit src,
const CUnit dst 
)

Calculate the distance from current view point to coordinate.

Returns the map distance between two units.

Parameters:
src Distance from this unit.
dst Distance to this unit.
Returns:
The distance between in tiles.

Definition at line 2336 of file unit.cpp.

References MapDistanceBetweenTypes(), CUnit::Type, CUnit::X, and CUnit::Y.

Referenced by AttackTarget(), AttackUnitsInDistance(), CompareUnitDistance(), FindRangeAttack(), FireMissile(), HandleActionAttack(), MoveToLocation(), MoveToTarget(), RepairUnit(), SpellMoveToTarget(), StartGathering(), and WaitForTransporter().

int MapDistanceToType ( int  x1,
int  y1,
const CUnitType type,
int  x2,
int  y2 
)

Returns the map distance to unit.

Returns the map distance between two points with unit type.

Parameters:
x1 X map tile position.
y1 Y map tile position.
type Unit type to take into account.
x2 X map tile position.
y2 Y map tile position.
Returns:
The distance between in tiles.

Definition at line 2288 of file unit.cpp.

References isqrt(), CUnitType::TileHeight, and CUnitType::TileWidth.

Referenced by MapDistanceToUnit().

int MapDistanceToUnit ( int  x,
int  y,
const CUnit dest 
)

Returns the map diestance between to unittype as locations.

Returns the map distance to unit.

Parameters:
x X map tile position.
y Y map tile position.
dest Distance to this unit.
Returns:
The distance between in tiles.

Definition at line 2323 of file unit.cpp.

References MapDistanceToType(), CUnit::Type, CUnit::X, and CUnit::Y.

Referenced by Demolish::Cast(), MissileHit(), MoveToTarget(), SpellMoveToTarget(), and ViewPointDistanceToUnit().

void MapMarkUnitSight ( CUnit unit  ) 

static void MapMarkUnitSightRec ( const CUnit unit,
int  x,
int  y,
int  width,
int  height,
MapMarkerFunc f 
) [static]

(Un)Mark on vision table the Sight of the unit (and units inside for transporter (recursively))

Parameters:
unit Unit to (un)mark.
x X coord of first container of unit.
y Y coord of first container of unit.
width Width of the first container of unit.
height Height of the first container of unit.
f Function to (un)mark for normal vision.

Definition at line 340 of file unit.cpp.

References CUnit::Container, CUnit::CurrentSightRange, CUnit::InsideCount, MapSight(), CUnit::NextContained, CUnit::Player, and CUnit::UnitInside.

Referenced by MapMarkUnitSight(), and MapUnmarkUnitSight().

void MapUnmarkUnitSight ( CUnit unit  ) 

Unmark on vision table the Sight of the unit.

Unmark on vision table the Sight of the unit (and units inside for transporter)

Parameters:
unit unit to unmark its vision.

Definition at line 402 of file unit.cpp.

References GetFirstContainer(), CUnit::IsUnusable(), MapMarkUnitSightRec(), MapUnmarkRadar(), MapUnmarkRadarJammer(), MapUnmarkTileSight, CUnit::Player, RADAR_INDEX, RADARJAMMER_INDEX, CUnit::Stats, CUnitType::TileHeight, CUnitType::TileWidth, CUnit::Type, CVariable::Value, CUnitStats::Variables, CUnit::X, and CUnit::Y.

Referenced by CUnit::ChangeOwner(), CommandSharedVision(), HandleActionDie(), CUnit::MoveToXY(), CUnit::Place(), CUnit::Release(), and CUnit::Remove().

void MarkUnitFieldFlags ( const CUnit unit  ) 

< Mark the field with the FieldFlags.

Mark the field with the FieldFlags.

Parameters:
unit unit to mark.

Definition at line 467 of file unit.cpp.

References CMap::Field(), CUnitType::FieldFlags, CMapField::Flags, Map, CUnitType::TileHeight, CUnitType::TileWidth, CUnit::Type, CUnit::X, and CUnit::Y.

Referenced by CanBuildUnitType(), DoActionMove(), CUnit::MoveToXY(), and CUnit::Place().

static int myatan ( int  val  )  [static]

Fast arc tangent function.

Parameters:
val atan argument
Returns:
atan(val)

Definition at line 1398 of file unit.cpp.

Referenced by DirectionToHeading().

void NearestOfUnit ( const CUnit unit,
int  tx,
int  ty,
int *  dx,
int *  dy 
)

Call when an Unit goes under fog.

Find nearest point of unit.

Parameters:
unit Pointer to unit.
tx X tile map postion.
ty Y tile map postion.
dx Out: nearest point X tile map postion to (tx,ty).
dy Out: nearest point Y tile map postion to (tx,ty).

Definition at line 828 of file unit.cpp.

References CUnitType::TileHeight, CUnitType::TileWidth, CUnit::Type, CUnit::X, CUnit::Y, and y.

Referenced by FireMissile().

static void RemoveUnitFromContainer ( CUnit unit  )  [static]

Remove unit from a container. It only updates linked list stuff.

Parameters:
unit Pointer to unit.

Definition at line 528 of file unit.cpp.

References Assert, CUnit::Container, CUnit::InsideCount, CUnit::NextContained, NoUnitP, CUnit::PrevContained, and CUnit::UnitInside.

Referenced by CUnit::Place(), and CUnit::Release().

void RescueUnits ( void   ) 

void UnitClearOrders ( CUnit unit  ) 

Removes all orders from a unit.

Parameters:
unit The unit that will have all its orders cleared

Definition at line 770 of file unit.cpp.

References COrder::Goal, CUnit::NewOrder, NoUnitP, CUnit::OrderCount, CUnit::Orders, CUnit::RefsDecrease(), CUnit::SavedOrder, CUnit::State, CUnit::SubAction, and UnitActionStill.

Referenced by Capture::Cast(), Polymorph::Cast(), DestroyAllInside(), EditorActionPlaceUnit(), EditorActionRemoveUnit(), LetUnitDie(), and StartBuilding().

void UnitCountSeen ( CUnit unit  ) 

Check for rescue each second.

Recalculates a units visiblity count. This happens really often, Like every time a unit moves. It's really fast though, since we have per-tile counts.

Parameters:
unit pointer to the unit to check if seen

Definition at line 1013 of file unit.cpp.

References CMap::Field(), CUnit::IsVisible(), Map, CMap::NoFogOfWar, PlayerMax, PlayerNobody, Players, CUnitType::TileHeight, CUnitType::TileWidth, CUnit::Type, UnitGoesOutOfFog(), UnitGoesUnderFog(), CUnit::VisCount, CMapField::Visible, CUnit::X, CUnit::Y, and y.

Referenced by CUnit::MoveToXY(), CUnit::Place(), CMap::Reveal(), and UpdateFogOfWarChange().

static void UnitFillSeenValues ( CUnit unit  )  [static]

CUnit* UnitFindResource ( const CUnit unit,
int  x,
int  y,
int  range,
int  resource 
)

Find the next idle worker.

Find Resource.

Parameters:
unit The unit that wants to find a resource.
x Closest to x
y Closest to y
range Maximum distance to the resource.
resource The resource id.
Note:
This will return an usable resource building that doesn't belong to the player or one of his allies.
Returns:
NoUnitP or resource unit

Definition at line 1696 of file unit.cpp.

References CUnitType::CanHarvestFrom, CanMoveToMask(), CreateMatrix(), CMap::Info, CMap::IsFieldExplored(), Map, MapFieldAirUnit, MapFieldLandUnit, MapFieldSeaUnit, CMapInfo::MapHeight, CMapInfo::MapWidth, CUnitType::MovementMask, NoUnitP, CUnit::Player, PlayerNeutral, ResourceOnMap(), CPlayer::Type, CUnit::Type, CUnit::X, and CUnit::Y.

Referenced by AiAssignHarvester(), FindNewResource(), and StartGathering().

void UnitGoesOutOfFog ( CUnit unit,
const CPlayer player 
)

Marks a unit as seen.

This function should get called when a unit goes out of fog of war.

Parameters:
unit The unit that goes out of fog.
player The player the unit goes out of fog for.
Note:
For units that are visible under fog (mostly buildings) we use reference counts, from the players that know about the building. When an building goes under fog it gets a refs increase, and when it shows up it gets a decrease. It must not get an decrease the first time it's seen, so we have to keep track of what player saw what units, with SeenByPlayer.

Definition at line 923 of file unit.cpp.

References CUnit::_unit_seen_::ByPlayer, CUnit::_unit_seen_::Destroyed, CPlayer::Index, PlayerPerson, CUnit::RefsDecrease(), CUnit::Seen, CPlayer::Type, CUnit::Type, and CUnitType::VisibleUnderFog.

Referenced by CMap::Reveal(), UnitCountSeen(), and UnitsOnTileMarkSeen().

void UnitGoesUnderFog ( CUnit unit,
const CPlayer player 
)

Call when an Unit goes out of fog.

This function should get called when a unit goes under fog of war.

Parameters:
unit The unit that goes under fog.
player The player the unit goes out of fog for.

Definition at line 882 of file unit.cpp.

References CUnit::_unit_seen_::Destroyed, CUnit::Destroyed, CPlayer::Index, PlayerPerson, CUnit::RefsIncrease(), CUnit::Seen, ThisPlayer, CPlayer::Type, CUnit::Type, UnitFillSeenValues(), and CUnitType::VisibleUnderFog.

Referenced by CMap::Reveal(), UnitCountSeen(), and UnitsOnTileUnmarkSeen().

void UnitHeadingFromDeltaXY ( CUnit unit,
int  dx,
int  dy 
)

Change unit heading/frame from delta direction x, y.

Parameters:
unit Unit for new direction looking.
dx X map tile delta direction.
dy Y map tile delta direction.

Definition at line 1484 of file unit.cpp.

References CUnit::Direction, DirectionToHeading(), and UnitUpdateHeading().

Referenced by AttackTarget(), AutoAttack(), DoActionMove(), HandleActionAttack(), MoveToLocation(), MoveToTarget(), SpellMoveToTarget(), and StartGathering().

bool UnitHoldsResources ( const CUnit unit  ) 

Find resource.

Check if a unit holds any resources

Parameters:
unit Unit to check
Returns:
True if the unit holds resources, false if it doesn't

Definition at line 1377 of file unit.cpp.

References MaxCosts, and CUnit::ResourcesHeld.

Referenced by GatherResource(), ResourceOnMap(), and UnitLost().

static void UnitInXY ( CUnit unit,
int  x,
int  y 
) [static]

Affect Tile coord of a unit (with units inside) to tile (x, y).

Parameters:
unit unit to move.
x X map tile position.
y Y map tile position.

Definition at line 560 of file unit.cpp.

References CUnit::InsideCount, CUnit::NextContained, CUnit::UnitInside, CUnit::X, and CUnit::Y.

Referenced by CUnit::MoveToXY(), CUnit::Place(), and CUnit::Remove().

void UnitLost ( CUnit unit  ) 

CUnit* UnitOnScreen ( int  x,
int  y 
)

Check if a unit should be removed from UnitsConsumingResources.

Select unit on screen. (x, y are in pixels relative to map 0,0).

Parameters:
x X pixel position.
y Y pixel position.
Returns:
Unit on x, y position.

Definition at line 1865 of file unit.cpp.

References CUnitType::BoxHeight, CUnitType::BoxWidth, CUnitType::DrawLevel, CUnit::IsVisibleAsGoal(), CUnit::IX, CUnit::IY, NumUnits, ReplayRevealMap, ThisPlayer, CUnitType::TileHeight, TileSizeX, TileSizeY, CUnitType::TileWidth, CUnit::Type, CUnit::X, and CUnit::Y.

Referenced by EditorCallbackMouse(), UIHandleButtonUp(), and UIHandleMouseMove().

void UnitRemoveConsumingResources ( CUnit unit  ) 

void UnitsOnTileMarkSeen ( const CPlayer player,
int  x,
int  y 
)

Unmarks a unit as seen.

Mark all units on a tile as now visible.

Parameters:
player The player this is for.
x x location to check
y y location to check

Definition at line 944 of file unit.cpp.

References CPlayer::Index, CPlayer::IsBothSharedVision(), CUnit::IsVisible(), PlayerMax, Players, CUnitCache::Select(), UnitCache, UnitGoesOutOfFog(), UnitMax, and CUnit::VisCount.

Referenced by MapMarkTileSight().

void UnitsOnTileUnmarkSeen ( const CPlayer player,
int  x,
int  y 
)

Does a recount for VisCount.

This function unmarks units on x, y as seen. It uses a reference count.

Parameters:
player The player to mark for.
x x location to check if building is on, and mark as seen
y y location to check if building is on, and mark as seen

Definition at line 975 of file unit.cpp.

References Assert, CPlayer::Index, CPlayer::IsBothSharedVision(), CUnit::IsVisible(), PlayerMax, Players, CUnitCache::Select(), CUnitType::TileHeight, CUnitType::TileWidth, CUnit::Type, UnitCache, UnitGoesUnderFog(), UnitMax, CUnit::VisCount, CUnit::X, and CUnit::Y.

Referenced by MapUnmarkTileSight().

void UnitUpdateHeading ( CUnit unit  ) 

Heading and frame from delta direction x,y.

Update sprite frame for new heading.

Definition at line 1448 of file unit.cpp.

References CUnitType::Building, CUnit::Direction, CUnit::Frame, LookingS, CUnitType::NumDirections, and CUnit::Type.

Referenced by DoActionMove(), HandleActionBuilt(), HandleActionDie(), HandleActionFollow(), CUnit::Init(), StartBuilding(), UnitHeadingFromDeltaXY(), UnitRotate(), and UnitShowAnimationScaled().

void UnmarkUnitFieldFlags ( const CUnit unit  ) 

Update unit->CurrentSightRange.

Mark the field with the FieldFlags.

Parameters:
unit unit to mark.

Definition at line 483 of file unit.cpp.

References CMap::Field(), CUnitType::FieldFlags, CMapField::Flags, Map, CUnit::Orders, CUnitCache::Select(), CUnitType::TileHeight, CUnitType::TileWidth, CUnit::Type, UnitActionDie, UnitCache, UnitMax, CUnit::X, and CUnit::Y.

Referenced by CanBuildUnitType(), DoActionMove(), CUnit::MoveToXY(), and CUnit::Remove().

void UpdateForNewUnit ( const CUnit unit,
int  upgrade 
)

Update for new unit. Food and income ...

Parameters:
unit New unit pointer.
upgrade True unit was upgraded.

Definition at line 805 of file unit.cpp.

References MaxCosts, CUnit::Player, CUnit::ProductionEfficiency, CUnitType::ProductionRate, CPlayer::ProductionRate, CUnitType::StorageCapacity, CPlayer::StorageCapacity, and CUnit::Type.

Referenced by CclCreateUnit(), CclUnit(), CUnit::ChangeOwner(), HandleActionBuilt(), and HandleActionTrain().

void UpdateUnitSightRange ( CUnit unit  ) 

Create a new unit.

Update the Unit Current sight range to good value and transported units inside.

Parameters:
unit unit to update SightRange

Definition at line 435 of file unit.cpp.

References Assert, CUnit::Constructed, CUnit::Container, CUnit::CurrentSightRange, CUnit::InsideCount, CVariable::Max, CUnit::NextContained, SaveGameLoading, SIGHTRANGE_INDEX, CUnit::Stats, CUnit::UnitInside, UpdateUnitSightRange(), and CUnitStats::Variables.

Referenced by CUnit::ChangeOwner(), CUnit::Place(), CUnit::Remove(), and UpdateUnitSightRange().

int ViewPointDistance ( int  x,
int  y 
)

Calculate the distance from current view point to unit.

Compute the distance from the view point to a given point.

Parameters:
x X map tile position.
y Y map tile position.
Todo:
FIXME: is it the correct place to put this function in?

Definition at line 2391 of file unit.cpp.

References MapDistance(), CViewport::MapHeight, CViewport::MapWidth, CViewport::MapX, CViewport::MapY, CUserInterface::SelectedViewport, and UI.

Referenced by ViewPointDistanceToMissile().

int ViewPointDistanceToUnit ( const CUnit dest  ) 

Can this unit-type attack the other (destination).

Compute the distance from the view point to a given unit.

Parameters:
dest Distance to this unit.
Todo:
FIXME: is it the correct place to put this function in?

Definition at line 2410 of file unit.cpp.

References MapDistanceToUnit(), CViewport::MapHeight, CViewport::MapWidth, CViewport::MapX, CViewport::MapY, CUserInterface::SelectedViewport, and UI.

Referenced by PlayUnitSound().


Variable Documentation

Config: capture buildings enabled.

Config: building capture enabled.

Definition at line 73 of file unit.cpp.

Referenced by CclSetBuildingCapture(), and HitUnit().

unsigned long HelpMeLastCycle [static]

Last cycle HelpMe sound played.

Definition at line 75 of file unit.cpp.

Referenced by CleanUnits(), and HitUnit().

int HelpMeLastX [static]

Last X coordinate HelpMe sound played.

Definition at line 76 of file unit.cpp.

Referenced by HitUnit().

int HelpMeLastY [static]

Last Y coordinate HelpMe sound played.

Definition at line 77 of file unit.cpp.

Referenced by HitUnit().

int NumUnits

CUnit* Units[MAX_UNIT_SLOTS]


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