CUnit::Type is now const.

This commit is contained in:
Joris 2012-06-11 17:27:54 +02:00
parent ee349a9fa8
commit 61029b1669
13 changed files with 61 additions and 65 deletions

View file

@ -107,7 +107,7 @@ static bool AnimateActionDie(CUnit &unit)
if (unit.Anim.Unbreakable) {
return ;
}
CUnitType &type = *unit.Type;
const CUnitType &type = *unit.Type;
// Die sequence terminated, generate corpse.
if (type.CorpseType == NULL) {
@ -116,7 +116,7 @@ static bool AnimateActionDie(CUnit &unit)
return ;
}
CUnitType &corpseType = *type.CorpseType;
const CUnitType &corpseType = *type.CorpseType;
Assert(type.TileWidth >= corpseType.TileWidth && type.TileHeight >= corpseType.TileHeight);
// Update sight for new corpse

View file

@ -187,7 +187,7 @@ static void AnimateActionTrain(CUnit &unit)
return ;
}
CPlayer &player = *unit.Player;
CUnitType &nType = *this->Type;
const CUnitType &nType = *this->Type;
const int cost = nType.Stats[player.Index].Costs[TimeCost];
this->Ticks += SpeedTrain;

View file

@ -112,7 +112,7 @@ enum EditorActionType {
struct EditorAction {
EditorActionType Type;
Vec2i tilePos;
CUnitType *UnitType;
const CUnitType *UnitType;
CPlayer *Player;
};
@ -314,7 +314,7 @@ void EditTiles(const Vec2i &pos, int tile, int size)
** @todo FIXME: Check if the player has already a start-point.
** @bug This function does not support mirror editing!
*/
static void EditorActionPlaceUnit(const Vec2i &pos, CUnitType &type, CPlayer *player)
static void EditorActionPlaceUnit(const Vec2i &pos, const CUnitType &type, CPlayer *player)
{
Assert(Map.Info.IsPointOnMap(pos));

View file

@ -497,9 +497,9 @@ public:
unsigned int Offset;/// Map position as flat index offset (x + y * w)
CUnitType *Type; /// Pointer to unit-type (peon,...)
const CUnitType *Type; /// Pointer to unit-type (peon,...)
CPlayer *Player; /// Owner of this unit
CUnitStats *Stats; /// Current unit stats
const CUnitStats *Stats; /// Current unit stats
int CurrentSightRange; /// Unit's Current Sight Range
// Pathfinding stuff:
@ -548,7 +548,7 @@ public:
struct _seen_stuff_ {
const CConstructionFrame *CFrame; /// Seen construction frame
int Frame; /// last seen frame/stage of buildings
CUnitType *Type; /// Pointer to last seen unit-type
const CUnitType *Type; /// Pointer to last seen unit-type
Vec2i tilePos; /// Last unit->tilePos Seen
signed char IX; /// Seen X image displacement to map position
signed char IY; /// seen Y image displacement to map position
@ -604,7 +604,7 @@ unsigned ByPlayer : PlayerMax; /// Track unit seen by player
void RefsDecrease();
/// Initialize unit structure with default values
void Init(CUnitType &type);
void Init(const CUnitType &type);
/// Assign unit to player
void AssignToPlayer(CPlayer &player);
@ -870,9 +870,9 @@ void UnmarkUnitFieldFlags(const CUnit &unit);
/// Update unit->CurrentSightRange.
void UpdateUnitSightRange(CUnit &unit);
/// Create a new unit
extern CUnit *MakeUnit(CUnitType &type, CPlayer *player);
extern CUnit *MakeUnit(const CUnitType &type, CPlayer *player);
/// Create a new unit and place on map
extern CUnit *MakeUnitAndPlace(const Vec2i &pos, CUnitType &type, CPlayer *player);
extern CUnit *MakeUnitAndPlace(const Vec2i &pos, const CUnitType &type, CPlayer *player);
/// Handle the loss of a unit (food,...)
extern void UnitLost(CUnit &unit);
/// Remove the Orders of a Unit
@ -927,7 +927,7 @@ extern void DropOutNearest(CUnit &unit, const Vec2i &goalPos, const CUnit *conta
extern void DropOutAll(const CUnit &unit);
/// Return the rule used to build this building.
extern CBuildRestrictionOnTop *OnTopDetails(std::vector<CBuildRestriction *> &restr, const CUnit &unit, const CUnitType *parent);
extern CBuildRestrictionOnTop *OnTopDetails(const std::vector<CBuildRestriction *> &restr, const CUnit &unit, const CUnitType *parent);
/// @todo more docu
extern CUnit *CanBuildHere(const CUnit *unit, const CUnitType &type, const Vec2i &pos);
/// @todo more docu

View file

@ -1055,7 +1055,7 @@ public:
/* API */
bool CheckUserBoolFlags(char *BoolFlags);
bool CheckUserBoolFlags(const char *BoolFlags) const;
bool CanTransport() const { return MaxOnBoard > 0 && !GivesResource; }
bool CanMove() const;

View file

@ -421,7 +421,7 @@ void CMinimap::UpdateXY(const Vec2i &pos)
*/
static void DrawUnitOn(CUnit &unit, int red_phase)
{
CUnitType *type;
const CUnitType *type;
Uint32 color;
SDL_Color c;

View file

@ -715,7 +715,7 @@ static int DoSelectUnitsInRectangle(int sx0, int sy0, int sx1, int sy1)
if (!unit.IsVisibleInViewport(UI.SelectedViewport)) {
continue;
}
CUnitType &type = *unit.Type;
const CUnitType &type = *unit.Type;
// Buildings are visible but not selectable
if (type.Building && !unit.IsVisibleOnMap(*ThisPlayer)) {
continue;

View file

@ -696,7 +696,7 @@ static void DrawUnitInfo(CUnit &unit)
}
CUnitType &type = *unit.Type;
const CUnitType &type = *unit.Type;
Assert(&type);
// Draw IconUnit

View file

@ -55,7 +55,7 @@
**
** @return the BuildingRestrictionDetails
*/
CBuildRestrictionOnTop *OnTopDetails(std::vector<CBuildRestriction *> &restr,
CBuildRestrictionOnTop *OnTopDetails(const std::vector<CBuildRestriction *> &restr,
const CUnit &unit, const CUnitType *parent)
{

View file

@ -303,6 +303,40 @@ static void ParseBuildingRules(lua_State *l, std::vector<CBuildRestriction *> &b
blist.push_back(andlist);
}
static void UpdateDefaultBoolFlags(CUnitType& type)
{
// BoolFlag
type.BoolFlag[COWARD_INDEX].value = type.Coward;
type.BoolFlag[BUILDING_INDEX].value = type.Building;
type.BoolFlag[FLIP_INDEX].value = type.Flip;
type.BoolFlag[REVEALER_INDEX].value = type.Revealer;
type.BoolFlag[LANDUNIT_INDEX].value = type.LandUnit;
type.BoolFlag[AIRUNIT_INDEX].value = type.AirUnit;
type.BoolFlag[SEAUNIT_INDEX].value = type.SeaUnit;
type.BoolFlag[EXPLODEWHENKILLED_INDEX].value = type.ExplodeWhenKilled;
type.BoolFlag[VISIBLEUNDERFOG_INDEX].value = type.VisibleUnderFog;
type.BoolFlag[PERMANENTCLOAK_INDEX].value = type.PermanentCloak;
type.BoolFlag[DETECTCLOAK_INDEX].value = type.DetectCloak;
type.BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value = type.AttackFromTransporter;
type.BoolFlag[VANISHES_INDEX].value = type.Vanishes;
type.BoolFlag[GROUNDATTACK_INDEX].value = type.GroundAttack;
type.BoolFlag[SHOREBUILDING_INDEX].value = type.ShoreBuilding;
type.BoolFlag[CANATTACK_INDEX].value = type.CanAttack;
type.BoolFlag[BUILDEROUTSIDE_INDEX].value = type.BuilderOutside;
type.BoolFlag[BUILDERLOST_INDEX].value = type.BuilderLost;
type.BoolFlag[CANHARVEST_INDEX].value = type.CanHarvest;
type.BoolFlag[HARVESTER_INDEX].value = type.Harvester;
type.BoolFlag[SELECTABLEBYRECTANGLE_INDEX].value = type.SelectableByRectangle;
type.BoolFlag[ISNOTSELECTABLE_INDEX].value = type.IsNotSelectable;
type.BoolFlag[DECORATION_INDEX].value = type.Decoration;
type.BoolFlag[INDESTRUCTIBLE_INDEX].value = type.Indestructible;
type.BoolFlag[TELEPORTER_INDEX].value = type.Teleporter;
type.BoolFlag[SHIELDPIERCE_INDEX].value = type.ShieldPiercing;
type.BoolFlag[SAVECARGO_INDEX].value = type.SaveCargo;
type.BoolFlag[NONSOLID_INDEX].value = type.NonSolid;
type.BoolFlag[WALL_INDEX].value = type.Wall;
}
/**
** Parse unit-type.
**
@ -1134,6 +1168,7 @@ static int CclDefineUnitType(lua_State *l)
if (type->MouseAction == MouseActionAttack && !type->CanAttack) {
LuaError(l, "Unit-type `%s': right-attack is set, but can-attack is not\n" _C_ type->Name.c_str());
}
UpdateDefaultBoolFlags(*type);
return 0;
}
@ -1607,7 +1642,7 @@ static int CclDefineDecorations(lua_State *l)
*/
void UpdateUnitVariables(CUnit &unit)
{
CUnitType *type = unit.Type;
const CUnitType *type = unit.Type;
for (int i = 0; i < NVARALREADYDEFINED; i++) { // default values
if (i == ARMOR_INDEX || i == PIERCINGDAMAGE_INDEX || i == BASICDAMAGE_INDEX
@ -1690,40 +1725,8 @@ void UpdateUnitVariables(CUnit &unit)
#endif
Assert(unit.Variable[i].Value <= unit.Variable[i].Max);
}
// BoolFlag
type->BoolFlag[COWARD_INDEX].value = type->Coward;
type->BoolFlag[BUILDING_INDEX].value = type->Building;
type->BoolFlag[FLIP_INDEX].value = type->Flip;
type->BoolFlag[REVEALER_INDEX].value = type->Revealer;
type->BoolFlag[LANDUNIT_INDEX].value = type->LandUnit;
type->BoolFlag[AIRUNIT_INDEX].value = type->AirUnit;
type->BoolFlag[SEAUNIT_INDEX].value = type->SeaUnit;
type->BoolFlag[EXPLODEWHENKILLED_INDEX].value = type->ExplodeWhenKilled;
type->BoolFlag[VISIBLEUNDERFOG_INDEX].value = type->VisibleUnderFog;
type->BoolFlag[PERMANENTCLOAK_INDEX].value = type->PermanentCloak;
type->BoolFlag[DETECTCLOAK_INDEX].value = type->DetectCloak;
type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value = type->AttackFromTransporter;
type->BoolFlag[VANISHES_INDEX].value = type->Vanishes;
type->BoolFlag[GROUNDATTACK_INDEX].value = type->GroundAttack;
type->BoolFlag[SHOREBUILDING_INDEX].value = type->ShoreBuilding;
type->BoolFlag[CANATTACK_INDEX].value = type->CanAttack;
type->BoolFlag[BUILDEROUTSIDE_INDEX].value = type->BuilderOutside;
type->BoolFlag[BUILDERLOST_INDEX].value = type->BuilderLost;
type->BoolFlag[CANHARVEST_INDEX].value = type->CanHarvest;
type->BoolFlag[HARVESTER_INDEX].value = type->Harvester;
type->BoolFlag[SELECTABLEBYRECTANGLE_INDEX].value = type->SelectableByRectangle;
type->BoolFlag[ISNOTSELECTABLE_INDEX].value = type->IsNotSelectable;
type->BoolFlag[DECORATION_INDEX].value = type->Decoration;
type->BoolFlag[INDESTRUCTIBLE_INDEX].value = type->Indestructible;
type->BoolFlag[TELEPORTER_INDEX].value = type->Teleporter;
type->BoolFlag[SHIELDPIERCE_INDEX].value = type->ShieldPiercing;
type->BoolFlag[SAVECARGO_INDEX].value = type->SaveCargo;
type->BoolFlag[NONSOLID_INDEX].value = type->NonSolid;
type->BoolFlag[WALL_INDEX].value = type->Wall;
}
/**
** Register CCL features for unit-type.
*/

View file

@ -292,7 +292,7 @@ int CUnit::GetDrawLevel() const
**
** @param type Unit-type
*/
void CUnit::Init(CUnitType &type)
void CUnit::Init(const CUnitType &type)
{
// Set refs to 1. This is the "I am alive ref", lost in ReleaseUnit.
Refs = 1;
@ -417,7 +417,7 @@ bool CUnit::StoreOrder(COrder *order)
*/
void CUnit::AssignToPlayer(CPlayer &player)
{
CUnitType &type = *Type;
const CUnitType &type = *Type;
// Build player unit table
if (!type.Vanishes && CurrentAction() != UnitActionDie) {
@ -465,7 +465,7 @@ void CUnit::AssignToPlayer(CPlayer &player)
**
** @return Pointer to created unit.
*/
CUnit *MakeUnit(CUnitType &type, CPlayer *player)
CUnit *MakeUnit(const CUnitType &type, CPlayer *player)
{
// Game unit limit reached.
if (NumUnits >= UnitMax) {
@ -843,7 +843,7 @@ void CUnit::Place(const Vec2i &pos)
**
** @return Pointer to created unit.
*/
CUnit *MakeUnitAndPlace(const Vec2i &pos, CUnitType &type, CPlayer *player)
CUnit *MakeUnitAndPlace(const Vec2i &pos, const CUnitType &type, CPlayer *player)
{
CUnit *unit = MakeUnit(type, player);
@ -2562,7 +2562,7 @@ void LetUnitDie(CUnit &unit)
unit.TTL = 0;
unit.Anim.Unbreakable = 0;
CUnitType *type = unit.Type;
const CUnitType *type = unit.Type;
while (unit.Resource.Workers) {
unit.Resource.Workers->DeAssignWorkerFromMine(unit);

View file

@ -912,15 +912,8 @@ void CUnit::Draw(const CViewport *vp) const
CPlayerColorGraphic *sprite;
ResourceInfo *resinfo;
const CConstructionFrame *cframe;
CUnitType *type;
const CUnitType *type;
/*
* Since we can draw in parallel to game logic units may be already destroyed
* or removed (this->Container != NULL) most dangerus is destroyed state
* but due existence of UnitCashe, unit memory is always valid only we need check
* Destroyed flag... the hack is that this->Type == NULL but 'or' logic
* should secure this scenario and retir before this->Type->Revealer check
*/
if (this->Destroyed || this->Container || this->Type->Revealer) { // Revealers are not drawn
return;
}
@ -944,7 +937,7 @@ void CUnit::Draw(const CViewport *vp) const
if (action == UnitActionUpgradeTo) {
const COrder_UpgradeTo &order = *static_cast<COrder_UpgradeTo *>(this->CurrentOrder());
type = const_cast<CUnitType *>(&order.GetUnitType());
type = &order.GetUnitType();
}
if (this->CurrentAction() == UnitActionBuilt) {

View file

@ -221,7 +221,7 @@ CUnitType::~CUnitType()
}
bool CUnitType::CheckUserBoolFlags(char *BoolFlags)
bool CUnitType::CheckUserBoolFlags(const char *BoolFlags) const
{
for (unsigned int i = 0; i < UnitTypeVar.GetNumberBoolFlag(); ++i) { // User defined flags
if (BoolFlags[i] != CONDITION_TRUE &&