$treeview $search $mathjax
Stratagus  2.2.6
$projectbrief
$projectbrief
$searchbox
-->
         _________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
           Stratagus - A free fantasy real time strategy game engine

src/unit/unit.cpp File Reference

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <sstream>
#include <iomanip>
#include "stratagus.h"
#include "unit.h"
#include "unit_manager.h"
#include "video.h"
#include "unitsound.h"
#include "unittype.h"
#include "animation.h"
#include "player.h"
#include "tileset.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"
#include "construct.h"
#include "iolib.h"

Classes

struct  _UnmarkUnitFieldFlags
struct  _TileSeen< MARK >
class  BestDepotFinder< NEARLOCATION >

unit.cpp - The units.

CUnitUnits [MAX_UNIT_SLOTS]
int NumUnits
 Array of used slots.
bool EnableTrainingQueue
 Number of slots used.
bool EnableBuildingCapture
 Config: training queues enabled.
bool RevealAttacker
 Config: capture buildings enabled.
static unsigned long HelpMeLastCycle
 Config: reveal attacker enabled.
static int HelpMeLastX
 Last cycle HelpMe sound played.
static int HelpMeLastY
 Last X coordinate HelpMe sound played.
static void RemoveUnitFromContainer (CUnit &unit)
 Last Y coordinate HelpMe sound played.
int ExtraDeathIndex (const char *death)
CUnitMakeUnit (CUnitType &type, CPlayer *player)
 Create a new unit.
static void MapMarkUnitSightRec (const CUnit &unit, const Vec2i &pos, int width, int height, MapMarkerFunc *f, MapMarkerFunc *f2)
static CUnitGetFirstContainer (const 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)
 Update unit->CurrentSightRange.
void MarkUnitFieldFlags (const CUnit &unit)
 Number of Units a team member has selected.
void UnmarkUnitFieldFlags (const CUnit &unit)
 Unmark the field with the FieldFlags.
static void UnitInXY (CUnit &unit, const Vec2i &pos)
CUnitMakeUnitAndPlace (const Vec2i &pos, CUnitType &type, CPlayer *player)
void UnitLost (CUnit &unit)
 Handle the loss of a unit (food,...).
void UnitClearOrders (CUnit &unit)
 Remove the Orders of a Unit.
void UpdateForNewUnit (const CUnit &unit, int upgrade)
void NearestOfUnit (const CUnit &unit, const Vec2i &pos, Vec2i *dpos)
static void UnitFillSeenValues (CUnit &unit)
void UnitGoesUnderFog (CUnit &unit, const CPlayer &player)
 Call when an Unit goes under fog.
void UnitGoesOutOfFog (CUnit &unit, const CPlayer &player)
 Call when an Unit goes out of fog.
void UnitsOnTileMarkSeen (const CPlayer &player, const unsigned int index, int cloak)
void UnitsOnTileMarkSeen (const CPlayer &player, int x, int y, int cloak)
 Marks a unit as seen.
void UnitsOnTileUnmarkSeen (const CPlayer &player, const unsigned int index, int cloak)
void UnitsOnTileUnmarkSeen (const CPlayer &player, int x, int y, int cloak)
 Unmarks a unit as seen.
void UnitCountSeen (CUnit &unit)
 Does a recount for VisCount.
static void ChangePlayerOwner (CPlayer &oldplayer, CPlayer &newplayer)
void RescueUnits ()
 Check for rescue each second.
static int myatan (int val)
int DirectionToHeading (const Vec2i &delta)
 Convert direction (dx,dy) to heading (0-255).
void UnitUpdateHeading (CUnit &unit)
 Update frame from heading.
void UnitHeadingFromDeltaXY (CUnit &unit, const Vec2i &delta)
void DropOutOnSide (CUnit &unit, int heading, const CUnit *container)
void DropOutNearest (CUnit &unit, const Vec2i &goalPos, const CUnit *container)
void DropOutAll (const CUnit &source)
 Drop out all units in the unit.
int FindWoodInSight (const CUnit &unit, Vec2i *pos)
int FindTerrainType (int movemask, int resmask, int rvresult, int range, const CPlayer *player, const Vec2i &startPos, Vec2i *terrainPos)
CUnitFindDepositNearLoc (CPlayer &p, const Vec2i &pos, int range, int resource)
CUnitUnitFindResource (const CUnit &unit, int x, int y, int range, int resource, bool check_usage, const CUnit *destu)
 Find resource.
CUnitUnitFindMiningArea (const CUnit &unit, int x, int y, int range, int resource)
CUnitFindDeposit (const CUnit &unit, int range, int resource)
 Find nearest deposit.
CUnitFindIdleWorker (const CPlayer &player, const CUnit *last)
 Find the next idle worker.
CUnitUnitOnScreen (CUnit *ounit, int x, int y)
void LetUnitDie (CUnit &unit)
 Let a unit die.
void DestroyAllInside (CUnit &source)
 Destory all units inside another unit.
void HitUnit (CUnit *attacker, CUnit &target, int damage)
 Hit unit with damage, if destroyed give attacker the points.
int MapDistanceToType (const Vec2i &pos1, const CUnitType &type, const Vec2i &pos2)
int MapDistanceBetweenTypes (const CUnitType &src, const Vec2i &pos1, const CUnitType &dst, const Vec2i &pos2)
int ViewPointDistance (const Vec2i &pos)
int ViewPointDistanceToUnit (const CUnit &dest)
 Calculate the distance from current view point to unit.
int CanTarget (const CUnitType *source, const CUnitType *dest)
 Can this unit-type attack the other (destination).
int CanTransport (const CUnit &transporter, const CUnit &unit)
 Can transporter transport the other unit.
int GetAnimationDamagedState (CUnit &unit, int anim)
 Get the suitable animation frame when unit is damaged.
void InitUnits ()
 Initialize unit module.
void CleanUnits ()
 Clean unit module.

Function Documentation

int CanTarget ( const CUnitType source,
const CUnitType dest 
)

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

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.

int CanTransport ( const CUnit transporter,
const CUnit unit 
)

Can transporter transport the other unit.

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.

static void ChangePlayerOwner ( CPlayer oldplayer,
CPlayer newplayer 
) [static]

Change the owner of all units of a player.

Parameters:
oldplayer Old owning player.
newplayer New owning player.

void CleanUnits (  ) 

Clean unit module.

Clean up unit module.

void DestroyAllInside ( CUnit source  ) 

Destory all units inside another unit.

Destroy all units inside unit.

Parameters:
source container.

int DirectionToHeading ( const Vec2i delta  ) 

Convert direction (dx,dy) to heading (0-255).

Convert direction to heading.

Parameters:
delta Delta.
Returns:
Angle (0..255)

void DropOutAll ( const CUnit source  ) 

Drop out all units in the unit.

Drop out all units inside unit.

Parameters:
source All units inside source are dropped out.

void DropOutNearest ( CUnit unit,
const Vec2i goalPos,
const CUnit container 
)

Place a unit on the map nearest to goalPos.

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

void DropOutOnSide ( CUnit unit,
int  heading,
const CUnit container 
)

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.
container Unit "containing" unit to drop (may be different of unit.Container).

int ExtraDeathIndex ( const char *  death  ) 

Find the index of a extra death type

CUnit* FindDeposit ( const CUnit unit,
int  range,
int  resource 
)

Find nearest deposit.

Find deposit. This will find a deposit for a resource

Parameters:
unit The unit that wants to find a resource.
x Closest to x
y Closest to y
range Maximum distance to the deposit.
resource Resource to find deposit from.
Note:
This will return a reachable allied depot.
Returns:
NoUnitP or deposit unit

CUnit* FindDepositNearLoc ( CPlayer p,
const Vec2i pos,
int  range,
int  resource 
)

CUnit* FindIdleWorker ( const CPlayer player,
const CUnit last 
)

Find the next idle worker.

Find the next idle worker

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

int FindTerrainType ( int  movemask,
int  resmask,
int  rvresult,
int  range,
const CPlayer player,
const Vec2i startPos,
Vec2i terrainPos 
)

Find the closest piece of terrain with the given flags.

Parameters:
movemask The movement mask to reach that location.
resmask Result tile mask.
rvresult Return a tile that doesn't match.
range Maximum distance for the search.
player Only search fields explored by player
startPos Map start position for the search.
pos OUT: Map position of tile.
Note:
Movement mask can be 0xFFFFFFFF to have no effect Range is not circular, but square. Player is ignored if nil(search the entire map) Use rvresult if you search for a tile that doesn't match resmask. Like for a tile where an unit can go with it's movement mask.
Returns:
True if wood was found.

int FindWoodInSight ( const CUnit unit,
Vec2i pos 
)

Find the closest piece of wood for an unit.

Parameters:
unit The unit.
pos OUT: Map position of tile.

int GetAnimationDamagedState ( CUnit unit,
int  anim 
)

Get the suitable animation frame when unit is damaged.

Get the suitable animation frame when unit is damaged.

static CUnit* GetFirstContainer ( const 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.

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

Hit unit with damage, if destroyed give attacker the 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.

void InitUnits (  ) 

Initialize unit module.

Initialize unit module.

void LetUnitDie ( CUnit unit  ) 

Let a unit die.

Let an unit die.

Parameters:
unit Unit to be destroyed.

CUnit* MakeUnit ( CUnitType type,
CPlayer player 
)

Create a new unit.

Create a new unit.

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

CUnit* MakeUnitAndPlace ( const Vec2i pos,
CUnitType type,
CPlayer player 
)

Create new unit and place on map.

Parameters:
pos map tile position.
type Pointer to unit-type.
player Pointer to owning player.
Returns:
Pointer to created unit.

int MapDistanceBetweenTypes ( const CUnitType src,
const Vec2i pos1,
const CUnitType dst,
const Vec2i pos2 
)

Returns the map distance between two points with unit type.

Parameters:
src src unittype
pos1 map tile position of src (upperleft).
dst Unit type to take into account.
pos2 map tile position of dst.
Returns:
The distance between the types.

int MapDistanceToType ( const Vec2i pos1,
const CUnitType type,
const Vec2i pos2 
)

Returns the map distance between two points with unit type.

Parameters:
pos1 map tile position.
type Unit type to take into account.
pos2 map tile position.
Returns:
The distance between in tiles.

void MapMarkUnitSight ( CUnit unit  ) 

Mark on vision table the Sight of the unit.

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

Parameters:
unit unit to unmark its vision.
See also:
MapUnmarkUnitSight.

static void MapMarkUnitSightRec ( const CUnit unit,
const Vec2i pos,
int  width,
int  height,
MapMarkerFunc f,
MapMarkerFunc f2 
) [static]

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

Parameters:
unit Unit to (un)mark.
pos 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.
f2 Function to (un)mark for cloaking vision.

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.
See also:
MapMarkUnitSight.

void MarkUnitFieldFlags ( const CUnit unit  ) 

Number of Units a team member has selected.

Mark the field with the FieldFlags.

Parameters:
unit unit to mark.

static int myatan ( int  val  )  [static]

Fast arc tangent function.

Parameters:
val atan argument
Returns:
atan(val)

void NearestOfUnit ( const CUnit unit,
const Vec2i pos,
Vec2i dpos 
)

Find nearest point of unit.

Parameters:
unit Pointer to unit.
pos tile map postion.
dpos Out: nearest point tile map postion to (tx,ty).

static void RemoveUnitFromContainer ( CUnit unit  )  [static]

Last Y coordinate HelpMe sound played.

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

Parameters:
unit Pointer to unit.

void RescueUnits (  ) 

Check for rescue each second.

Rescue units.

Look through all rescueable players, if they could be rescued.

void UnitClearOrders ( CUnit unit  ) 

Remove the Orders of a Unit.

Removes all orders from a unit.

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

void UnitCountSeen ( CUnit unit  ) 

Does a recount for VisCount.

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

static void UnitFillSeenValues ( CUnit unit  )  [static]

Copy the unit look in Seen variables. This should be called when buildings go under fog of war for ThisPlayer.

Parameters:
unit The unit to work on

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

Find Mining Area for 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

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

Find resource.

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

void UnitGoesOutOfFog ( CUnit unit,
const CPlayer player 
)

Call when an Unit goes out of fog.

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.

void UnitGoesUnderFog ( CUnit unit,
const CPlayer player 
)

Call when an Unit goes under 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.

void UnitHeadingFromDeltaXY ( CUnit unit,
const Vec2i delta 
)

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

Parameters:
unit Unit for new direction looking.
delta map tile delta direction.

static void UnitInXY ( CUnit unit,
const Vec2i pos 
) [static]

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

Parameters:
unit unit to move.
pos map tile position.

void UnitLost ( CUnit unit  ) 

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

Update information for lost units.

Parameters:
unit Pointer to unit.
Note:
Also called by ChangeUnitOwner

CUnit* UnitOnScreen ( CUnit ounit,
int  x,
int  y 
)

Unit on map screen.

Select units on screen. (x, y are in pixels relative to map 0,0). Not GAMEPLAY safe, uses ReplayRevealMap

More units on same position. Cycle through units. ounit is the old one. First take highest unit.

Parameters:
ounit Old selected unit.
x X pixel position.
y Y pixel position.
Returns:
An unit on x, y position.

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

Marks a unit as seen.

void UnitsOnTileMarkSeen ( const CPlayer player,
const unsigned int  index,
int  cloak 
)

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
cloak If we mark cloaked units too.

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

Unmarks a unit as seen.

void UnitsOnTileUnmarkSeen ( const CPlayer player,
const unsigned int  index,
int  cloak 
)

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
cloak If this is for cloaked units.

void UnitUpdateHeading ( CUnit unit  ) 

Update frame from heading.

Update sprite frame for new heading.

void UnmarkUnitFieldFlags ( const CUnit unit  ) 

Unmark the field with the FieldFlags.

Mark the field with the FieldFlags.

Parameters:
unit unit to mark.

void UpdateForNewUnit ( const CUnit unit,
int  upgrade 
)

Update for new unit. Food and income ...

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

void UpdateUnitSightRange ( CUnit unit  ) 

Update unit->CurrentSightRange.

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

Parameters:
unit unit to update SightRange

int ViewPointDistance ( const Vec2i pos  ) 

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

Parameters:
pos map tile position.
Todo:
FIXME: is it the correct place to put this function in?

int ViewPointDistanceToUnit ( const CUnit dest  ) 

Calculate the distance from current view point to unit.

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?


Variable Documentation

Config: training queues enabled.

Number of slots used.

Show orders for some time.

unsigned long HelpMeLastCycle [static]

Config: reveal attacker enabled.

int HelpMeLastX [static]

Last cycle HelpMe sound played.

int HelpMeLastY [static]

Last X coordinate HelpMe sound played.

int NumUnits

Array of used slots.

Units used.

Config: capture buildings enabled.

Config: building capture enabled.

CUnit* Units[MAX_UNIT_SLOTS]

(C) Copyright 1998-2011 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.