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

       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"

selection.cpp File Reference

The units' selection. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stratagus.h"
#include "unittype.h"
#include "player.h"
#include "unit.h"
#include "unit_manager.h"
#include "unit_cache.h"
#include "interface.h"
#include "map.h"
#include "ui.h"
#include "commands.h"
#include "network.h"
#include "iolib.h"
#include "script.h"

Go to the source code of this file.

Functions

void SaveSelection (void)
 Restore selection.
void RestoreSelection (void)
 Clear current selection.
void UnSelectAll (void)
 Select group as selection.
void ChangeSelectedUnits (CUnit **units, int count)
 Changed TeamUnit Selection.
void ChangeTeamSelectedUnits (CPlayer *player, CUnit **units, int adjust, int count)
 Add a unit to selection.
int SelectUnit (CUnit *unit)
 Select one unit as selection.
void SelectSingleUnit (CUnit *unit)
 Remove a unit from selection.
void UnSelectUnit (CUnit *unit)
 Add a unit to selected if not already selected, remove it otherwise.
int ToggleSelectUnit (CUnit *unit)
 Select units from the same type (if selectable by rectangle).
int SelectUnitsByType (CUnit *base)
 Toggle units from the same type (if selectable by rectangle).
int ToggleUnitsByType (CUnit *base)
 Select the units belonging to a particular group.
int SelectGroup (int group_number)
 Add the units from the same group as the one in parameter.
int AddGroupFromUnitToSelection (CUnit *unit)
 Select the units from the same group as the one in parameter.
int SelectGroupFromUnit (CUnit *unit)
 Select the units in the selection rectangle.
static int SelectOrganicUnitsInTable (CUnit **table, int num_units)
static int SelectSpritesInsideRectangle (int sx0, int sy0, int sx1, int sy1, CUnit **table, int num_units)
int AddSelectedUnitsInRectangle (int x0, int y0, int x1, int y1)
 Add ground units in the selection rectangle to the current selection.
int SelectUnitsInRectangle (int sx0, int sy0, int sx1, int sy1)
 Select ground units in the selection rectangle.
int SelectGroundUnitsInRectangle (int sx0, int sy0, int sx1, int sy1)
 Select flying units in the selection rectangle.
int SelectAirUnitsInRectangle (int sx0, int sy0, int sx1, int sy1)
 Add the units in the selection rectangle to the current selection.
int AddSelectedGroundUnitsInRectangle (int sx0, int sy0, int sx1, int sy1)
 Add flying units in the selection rectangle to the current selection.
int AddSelectedAirUnitsInRectangle (int sx0, int sy0, int sx1, int sy1)
 Init selections.
void InitSelections (void)
 Save current selection state.
void SaveSelections (CFile *file)
 Clean up selections.
void CleanSelections (void)
 Register CCL selection features.
static int CclSetGroupId (lua_State *l)
static int CclSelection (lua_State *l)
void SelectionCclRegister (void)

Variables

int NumSelected
 Number of selected units.
int TeamNumSelected [PlayerMax]
 How many units selected.
int MaxSelectable
 Maximum number of selected units.
CUnit ** Selected
 All selected units.
CUnit ** TeamSelected [PlayerMax]
 teams currently selected units
static int _NumSelected
 save of NumSelected
static int _TeamNumSelected [PlayerMax]
 save of TeamNumSelected
static CUnit ** _Selected
 save of Selected
static CUnit ** _TeamSelected [PlayerMax]
 save of TeamSelected
static unsigned GroupId
 Unique group # for automatic groups.


Detailed Description

The units' selection.

Definition in file selection.cpp.


Function Documentation

int AddGroupFromUnitToSelection ( CUnit unit  ) 

Select the units from the same group as the one in parameter.

Add units from group of a particular unit to selection.

Parameters:
unit unit belonging to the group to be selected.
Returns:
0 if the unit doesn't belong to a group, or the number of units in the group.

Definition at line 531 of file selection.cpp.

References CUnit::LastGroup, MaxSelectable, NumSelected, NumUnits, SelectUnit(), and Units.

Referenced by DoSelectionButtons(), SelectGroupFromUnit(), and UIHandleButtonUp().

int AddSelectedAirUnitsInRectangle ( int  sx0,
int  sy0,
int  sx1,
int  sy1 
)

Init selections.

Add the air units in the rectangle to the current selection

Parameters:
sx0 X start of selection rectangle in tile coordinates
sy0 Y start of selection rectangle in tile coordinates
sx1 X start of selection rectangle in tile coordinates
sy1 Y start of selection rectangle in tile coordinates
Returns:
the number of units found.

Definition at line 1007 of file selection.cpp.

References CanSelectMultipleUnits, CUnit::IsUnusable(), MaxSelectable, NumSelected, CUnit::Player, CUnitCache::Select(), CUnitType::SelectableByRectangle, SelectAirUnitsInRectangle(), SelectSpritesInsideRectangle(), SelectUnit(), CUnit::TeamSelected, TileSizeX, TileSizeY, CUnit::Type, UnitCache, UnitMax, CUnitType::UnitType, and UnitTypeFly.

Referenced by UIHandleButtonUp().

int AddSelectedGroundUnitsInRectangle ( int  sx0,
int  sy0,
int  sx1,
int  sy1 
)

Add flying units in the selection rectangle to the current selection.

Add the ground units in the rectangle to the current selection

Parameters:
sx0 X start of selection rectangle in tile coordinates
sy0 Y start of selection rectangle in tile coordinates
sx1 X start of selection rectangle in tile coordinates
sy1 Y start of selection rectangle in tile coordinates
Returns:
the number of units found.

Definition at line 933 of file selection.cpp.

References CanSelectMultipleUnits, CUnit::IsUnusable(), MaxSelectable, NumSelected, CUnit::Player, CUnitCache::Select(), CUnitType::SelectableByRectangle, SelectGroundUnitsInRectangle(), SelectSpritesInsideRectangle(), SelectUnit(), CUnit::TeamSelected, TileSizeX, TileSizeY, CUnit::Type, UnitCache, UnitMax, CUnitType::UnitType, and UnitTypeFly.

Referenced by UIHandleButtonUp().

int AddSelectedUnitsInRectangle ( int  x0,
int  y0,
int  x1,
int  y1 
)

Add ground units in the selection rectangle to the current selection.

Add the units in the rectangle to the current selection

Parameters:
x0 X start of selection rectangle in tile coordinates
y0 Y start of selection rectangle in tile coordinates
x1 X start of selection rectangle in tile coordinates
y1 Y start of selection rectangle in tile coordinates
Returns:
the _total_ number of units selected.

Definition at line 668 of file selection.cpp.

References CanSelectMultipleUnits, MaxSelectable, NumSelected, CUnitCache::Select(), SelectOrganicUnitsInTable(), SelectSpritesInsideRectangle(), SelectUnit(), SelectUnitsInRectangle(), TileSizeX, TileSizeY, UnitCache, and UnitMax.

Referenced by UIHandleButtonUp().

static int CclSelection ( lua_State *  l  )  [static]

Define the current selection.

Parameters:
l Lua state.

Definition at line 1153 of file selection.cpp.

References InitSelections(), LuaCheckArgs, LuaError, LuaToNumber(), LuaToString(), NumSelected, and UnitSlots.

Referenced by SelectionCclRegister().

static int CclSetGroupId ( lua_State *  l  )  [static]

Set the current group id. (Needed for load/save)

Parameters:
l Lua state.

Definition at line 1136 of file selection.cpp.

References GroupId, LuaCheckArgs, and LuaToNumber().

Referenced by SelectionCclRegister().

void ChangeSelectedUnits ( CUnit **  units,
int  count 
)

Changed TeamUnit Selection.

Replace a group of selected units by an other group of units.

Parameters:
units Array of units to be selected.
count Number of units in array to be selected.

Definition at line 143 of file selection.cpp.

References Assert, GroupId, CUnit::LastGroup, MaxSelectable, NetworkSendSelection(), NumSelected, CUnit::Selected, and UnSelectAll().

Referenced by SelectAirUnitsInRectangle(), SelectGroundUnitsInRectangle(), SelectGroup(), SelectSingleUnit(), and SelectUnitsInRectangle().

void ChangeTeamSelectedUnits ( CPlayer player,
CUnit **  units,
int  adjust,
int  count 
)

Add a unit to selection.

Change A Unit Selection from my Team

Parameters:
player The Player who is selecting the units
units The Units to add/remove
adjust 0 = reset, 1 = remove units, 2 = add units
count the number of units to be adjusted

Definition at line 173 of file selection.cpp.

References Assert, CPlayer::Index, MaxSelectable, NoUnitP, TeamNumSelected, and CUnit::TeamSelected.

Referenced by CommandQuit(), and NetworkProcessSelection().

void CleanSelections ( void   ) 

Register CCL selection features.

Clean up the selection module.

Definition at line 1110 of file selection.cpp.

References _TeamNumSelected, GroupId, NumSelected, PlayerMax, and TeamNumSelected.

Referenced by CleanGame(), and CleanModules().

void InitSelections ( void   ) 

Save current selection state.

Initialize the selection module.

Definition at line 1074 of file selection.cpp.

References MaxSelectable, and PlayerMax.

Referenced by CclSelection(), CreateGame(), and InitModules().

void RestoreSelection ( void   ) 

Clear current selection.

Restore selection.

Definition at line 99 of file selection.cpp.

References _NumSelected, _TeamNumSelected, NumSelected, PlayerMax, CUnit::Selected, TeamNumSelected, CUnit::TeamSelected, and UnSelectAll().

void SaveSelection ( void   ) 

Restore selection.

Save selection to restore after.

Definition at line 79 of file selection.cpp.

References _NumSelected, _TeamNumSelected, NumSelected, PlayerMax, and TeamNumSelected.

void SaveSelections ( CFile file  ) 

Clean up selections.

Save current selection state.

Parameters:
file Output file.

Definition at line 1094 of file selection.cpp.

References GroupId, NumSelected, CFile::printf(), and UnitReference().

Referenced by SaveGame().

int SelectAirUnitsInRectangle ( int  sx0,
int  sy0,
int  sx1,
int  sy1 
)

Add the units in the selection rectangle to the current selection.

Select own air units in a rectangle.

Parameters:
sx0 X start of selection rectangle in tile coordinates
sy0 Y start of selection rectangle in tile coordinates
sx1 X start of selection rectangle in tile coordinates
sy1 Y start of selection rectangle in tile coordinates
Returns:
the number of units found.

Definition at line 878 of file selection.cpp.

References CanSelectMultipleUnits, ChangeSelectedUnits(), CUnit::IsUnusable(), MaxSelectable, CUnit::Player, CUnitCache::Select(), CUnitType::SelectableByRectangle, SelectSpritesInsideRectangle(), CUnit::TeamSelected, TileSizeX, TileSizeY, CUnit::Type, UnitCache, UnitMax, CUnitType::UnitType, and UnitTypeFly.

Referenced by AddSelectedAirUnitsInRectangle(), and UIHandleButtonUp().

int SelectGroundUnitsInRectangle ( int  sx0,
int  sy0,
int  sx1,
int  sy1 
)

Select flying units in the selection rectangle.

Select own ground units in a rectangle.

Parameters:
sx0 X start of selection rectangle in tile coordinates
sy0 Y start of selection rectangle in tile coordinates
sx1 X start of selection rectangle in tile coordinates
sy1 Y start of selection rectangle in tile coordinates
Returns:
the number of units found.

Definition at line 823 of file selection.cpp.

References CanSelectMultipleUnits, ChangeSelectedUnits(), CUnit::IsUnusable(), MaxSelectable, CUnit::Player, CUnitCache::Select(), CUnitType::SelectableByRectangle, SelectSpritesInsideRectangle(), CUnit::TeamSelected, TileSizeX, TileSizeY, CUnit::Type, UnitCache, UnitMax, CUnitType::UnitType, and UnitTypeFly.

Referenced by AddSelectedGroundUnitsInRectangle(), and UIHandleButtonUp().

int SelectGroup ( int  group_number  ) 

Add the units from the same group as the one in parameter.

Change selected units to units from group group_number Doesn't change the selection if the group has no unit.

Parameters:
group_number number of the group to be selected.
Returns:
number of units in the group.

Definition at line 509 of file selection.cpp.

References Assert, ChangeSelectedUnits(), GetNumberUnitsOfGroup(), GetUnitsOfGroup(), NUM_GROUPS, and NumSelected.

Referenced by UiSelectGroup().

int SelectGroupFromUnit ( CUnit unit  ) 

Select the units in the selection rectangle.

Select units from group of a particular unit. Doesn't change the selection if the group has no unit, or the unit doesn't belong to any group.

Parameters:
unit unit belonging to the group to be selected.
Returns:
0 if the unit doesn't belong to a group, or the number of units in the group.

Definition at line 561 of file selection.cpp.

References AddGroupFromUnitToSelection(), CUnit::LastGroup, and UnSelectAll().

Referenced by DoSelectionButtons(), and UIHandleButtonUp().

void SelectionCclRegister ( void   ) 

Register CCL features for selections.

Definition at line 1182 of file selection.cpp.

References CclSelection(), CclSetGroupId(), and Lua.

Referenced by InitCcl().

static int SelectOrganicUnitsInTable ( CUnit **  table,
int  num_units 
) [static]

Select the units selecteable by rectangle in a local table. Act like a filter: The source table is modified. Return the original table if no unit is found.

Parameters:
table Input/Output table of units.
num_units Number of units in input table.
Returns:
the number of units found.

Definition at line 581 of file selection.cpp.

References CanSelectMultipleUnits, CUnit::IsUnusable(), MaxSelectable, CUnit::Player, CUnitType::SelectableByRectangle, CUnit::TeamSelected, and CUnit::Type.

Referenced by AddSelectedUnitsInRectangle(), and SelectUnitsInRectangle().

void SelectSingleUnit ( CUnit unit  ) 

Remove a unit from selection.

Select a single unit, unselecting the previous ones

Parameters:
unit Pointer to unit to be selected.

Definition at line 260 of file selection.cpp.

References ChangeSelectedUnits().

Referenced by DoSelectionButtons(), SelectUnitsInRectangle(), UiFindIdleWorker(), and UIHandleButtonUp().

static int SelectSpritesInsideRectangle ( int  sx0,
int  sy0,
int  sx1,
int  sy1,
CUnit **  table,
int  num_units 
) [static]

Selects units from the table whose sprite is at least partially covered by the rectangle. The rectangle is determined by coordinates of its upper left and lower right corner expressed in screen map coordinate system.

Parameters:
sx0 x-coord of upper left corner of the rectangle
sy0 y-coord of upper left corner of the rectangle
sx1 x-coord of lower right corner of the rectangle
sy1 y-coord of lower right corner of the rectangle
table table of units
num_units number of units in table
Returns:
number of units found

Definition at line 621 of file selection.cpp.

References CUnitType::BoxHeight, CUnitType::BoxWidth, CUnit::IX, CUnit::IY, CUnitType::OffsetX, CUnitType::OffsetY, CUnitType::TileHeight, TileSizeX, TileSizeY, CUnitType::TileWidth, CUnit::Type, CUnit::X, and CUnit::Y.

Referenced by AddSelectedAirUnitsInRectangle(), AddSelectedGroundUnitsInRectangle(), AddSelectedUnitsInRectangle(), SelectAirUnitsInRectangle(), SelectGroundUnitsInRectangle(), and SelectUnitsInRectangle().

int SelectUnit ( CUnit unit  ) 

Select one unit as selection.

Add a unit to the other selected units.

Parameters:
unit Pointer to unit to add.
Returns:
true if added to selection, false otherwise (if NumSelected == MaxSelectable or unit is already selected or unselectable)

Definition at line 223 of file selection.cpp.

References DebugPrint, GameRunning, GroupId, CUnitType::IsNotSelectable, CUnit::LastGroup, MaxSelectable, NumSelected, CUnit::Removed, CUnitType::Revealer, CUnit::Selected, and CUnit::Type.

Referenced by AddGroupFromUnitToSelection(), AddSelectedAirUnitsInRectangle(), AddSelectedGroundUnitsInRectangle(), AddSelectedUnitsInRectangle(), ToggleSelectUnit(), ToggleUnitsByType(), and UiAddGroupToSelection().

int SelectUnitsByType ( CUnit base  ) 

Toggle units from the same type (if selectable by rectangle).

Select units from a particular type and belonging to the local player.

The base is included in the selection and defines the type of the other units to be selected.

Parameters:
base Select all units of same type.
Returns:
Number of units found, 0 means selection unchanged
FIXME: 0 can't happen. Maybe when scripting will use it?

FIXME: should always select the nearest 9 units to the base!

Definition at line 346 of file selection.cpp.

References Assert, CanSelectMultipleUnits, GameRunning, GroupId, CUnitType::IsNotSelectable, CUnit::IsUnusable(), CUnit::LastGroup, CViewport::MapHeight, CViewport::MapWidth, CViewport::MapX, CViewport::MapY, MaxSelectable, CUserInterface::MouseViewport, NetworkSendSelection(), NumSelected, CUnit::Orders, CUnit::Player, CUnit::Removed, CUnitCache::Select(), CUnitType::SelectableByRectangle, CUnit::Selected, CUnit::TeamSelected, CUnit::Type, UI, UnitActionDie, UnitCache, UnitMax, and UnSelectAll().

Referenced by DoSelectionButtons(), and UIHandleButtonUp().

int SelectUnitsInRectangle ( int  sx0,
int  sy0,
int  sx1,
int  sy1 
)

Select ground units in the selection rectangle.

Select units in a rectangle. Proceed in order in none found:

  • select local player mobile units
  • select one local player static unit (random)
  • select one neutral unit (critter, mine...)
  • select one enemy unit (random)
Parameters:
sx0 X start of selection rectangle in tile coordinates
sy0 Y start of selection rectangle in tile coordinates
sx1 X start of selection rectangle in tile coordinates
sy1 Y start of selection rectangle in tile coordinates
Returns:
the number of units found.

Definition at line 727 of file selection.cpp.

References CUnitType::Building, CUnitType::CanHarvestFrom, CanSelectMultipleUnits, ChangeSelectedUnits(), CUnit::IsVisibleInViewport(), CUnit::IsVisibleOnMap(), CUnit::Orders, CUnit::Player, CUnit::Removed, CUnitCache::Select(), CUserInterface::SelectedViewport, SelectOrganicUnitsInTable(), SelectSingleUnit(), SelectSpritesInsideRectangle(), ThisPlayer, TileSizeX, TileSizeY, CUnit::Type, UI, UnitActionDie, UnitCache, and UnitMax.

Referenced by AddSelectedUnitsInRectangle(), and UIHandleButtonUp().

int ToggleSelectUnit ( CUnit unit  ) 

Select units from the same type (if selectable by rectangle).

Toggle the selection of a unit in a group of selected units

Parameters:
unit Pointer to unit to be toggled.
Returns:
0 if unselected, 1 otherwise

Definition at line 322 of file selection.cpp.

References CUnit::Selected, SelectUnit(), and UnSelectUnit().

Referenced by DoSelectionButtons(), and UIHandleButtonUp().

int ToggleUnitsByType ( CUnit base  ) 

Select the units belonging to a particular group.

Toggle units from a particular type and belonging to the local player.

The base is included in the selection and defines the type of the other units to be selected.

Parameters:
base Toggle all units of same type.
Returns:
Number of units found, 0 means selection unchanged
FIXME: toggle not written FIXME: should always select the nearest 9 units to the base!

Definition at line 440 of file selection.cpp.

References CanSelectMultipleUnits, CUnit::IsUnusable(), CViewport::MapHeight, CViewport::MapWidth, CViewport::MapX, CViewport::MapY, CUserInterface::MouseViewport, NetworkSendSelection(), NumSelected, CUnit::Orders, CUnit::Player, CUnit::Removed, CUnitCache::Select(), CUnitType::SelectableByRectangle, SelectUnit(), CUnit::TeamSelected, CUnit::Type, UI, UnitActionDie, UnitCache, and UnitMax.

Referenced by DoSelectionButtons(), and UIHandleButtonUp().

void UnSelectAll ( void   ) 

Select group as selection.

Unselect all the units in the current selection

Definition at line 122 of file selection.cpp.

References GroupId, NoUnitP, NumSelected, and CUnit::Selected.

Referenced by ChangeSelectedUnits(), RestoreSelection(), SelectGroupFromUnit(), SelectUnitsByType(), and UiUnselectAll().

void UnSelectUnit ( CUnit unit  ) 

Add a unit to selected if not already selected, remove it otherwise.

Unselect unit

Parameters:
unit Pointer to unit to be unselected.

Definition at line 270 of file selection.cpp.

References Assert, GroupId, CUnit::LastGroup, NoUnitP, NumSelected, PlayerMax, CUnit::Selected, TeamNumSelected, and CUnit::TeamSelected.

Referenced by DoActionMove(), CUnit::Remove(), and ToggleSelectUnit().


Variable Documentation

int _NumSelected [static]

save of NumSelected

Definition at line 65 of file selection.cpp.

Referenced by RestoreSelection(), and SaveSelection().

CUnit** _Selected [static]

save of Selected

Definition at line 67 of file selection.cpp.

int _TeamNumSelected[PlayerMax] [static]

save of TeamNumSelected

Definition at line 66 of file selection.cpp.

Referenced by CleanSelections(), RestoreSelection(), and SaveSelection().

CUnit** _TeamSelected[PlayerMax] [static]

save of TeamSelected

Definition at line 68 of file selection.cpp.

unsigned GroupId [static]

Unique group # for automatic groups.

Definition at line 70 of file selection.cpp.

Referenced by CclSetGroupId(), ChangeSelectedUnits(), CleanSelections(), CUnit::Init(), SaveSelections(), SelectUnit(), SelectUnitsByType(), UnSelectAll(), and UnSelectUnit().

int TeamNumSelected[PlayerMax]

How many units selected.

Number of Units a team member has selected.

Definition at line 60 of file selection.cpp.

Referenced by ChangeTeamSelectedUnits(), CleanSelections(), RestoreSelection(), SaveSelection(), and UnSelectUnit().

CUnit** TeamSelected[PlayerMax]

teams currently selected units

Definition at line 63 of file selection.cpp.

Referenced by CUnit::Init().


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