Skip to content

Commit

Permalink
Unit type variations are now stored in a vector instead of an array
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrettin committed Jan 10, 2019
1 parent d79b571 commit 81a3027
Show file tree
Hide file tree
Showing 14 changed files with 564 additions and 564 deletions.
1 change: 0 additions & 1 deletion src/include/stratagus.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ extern const char NameLine[];
#define MAX_RACES 128
#define FactionMax 128 /// Maximum number of factions a civilization can have
#define PlayerColorMax 32 /// How many player colors are supported
#define VariationMax 32 /// Maximum number of variations a unit can have

#define AuraRange 6 /// Range of auras
#define PlayerHeroMax 4 /// Maximum heroes per player
Expand Down
10 changes: 6 additions & 4 deletions src/include/unit/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,12 @@ class CUnit
void Retrain();
void HealingItemAutoUse();
void SetCharacter(const std::string &character_full_name, bool custom_hero = false);
bool CheckTerrainForVariation(const VariationInfo *varinfo) const;
bool CheckSeasonForVariation(const VariationInfo *varinfo) const;
bool CheckTerrainForVariation(const CUnitTypeVariation *variation) const;
bool CheckSeasonForVariation(const CUnitTypeVariation *variation) const;
void ChooseVariation(const CUnitType *new_type = nullptr, bool ignore_old_variation = false, int image_layer = -1);
void SetVariation(int new_variation, const CUnitType *new_type = nullptr, int image_layer = -1);
void SetVariation(CUnitTypeVariation *new_variation, const CUnitType *new_type = nullptr, int image_layer = -1);
const CUnitTypeVariation *GetVariation() const;
const CUnitTypeVariation *GetLayerVariation(const unsigned int image_layer) const;
void UpdateButtonIcons();
void ChooseButtonIcon(int button_action);
void EquipItem(CUnit &item, bool affect_character = true);
Expand Down Expand Up @@ -403,7 +405,7 @@ class CUnit
bool CanAutoCastSpell(const CSpell *spell) const;
bool IsItemEquipped(const CUnit *item) const;
bool IsItemClassEquipped(int item_class) const;
bool IsItemTypeEquipped(CUnitType *item_type) const;
bool IsItemTypeEquipped(const CUnitType *item_type) const;
bool IsUniqueItemEquipped(const CUniqueItem *unique) const;
bool CanEquipItem(CUnit *item) const;
bool CanEquipItemClass(int item_class) const;
Expand Down
57 changes: 28 additions & 29 deletions src/include/unit/unittype.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,47 +134,46 @@ class ResourceInfo

//Wyrmgus start
//unit variations
class VariationInfo
class CUnitTypeVariation
{
public:
VariationInfo() :
FrameWidth(0), FrameHeight(0), ResourceMin(0), ResourceMax(0), Weight(1),
Animations(nullptr), Construction(nullptr), Sprite(nullptr), ShadowSprite(nullptr), LightSprite(nullptr)
CUnitTypeVariation()
{
memset(LayerSprites, 0, sizeof(LayerSprites));
memset(SpriteWhenLoaded, 0, sizeof(SpriteWhenLoaded));
memset(SpriteWhenEmpty, 0, sizeof(SpriteWhenEmpty));
}

~VariationInfo();
~CUnitTypeVariation();

int ID = -1; /// The variation's index within the appropriate variation vector of its unit type
std::string VariationId; /// Variation's name.
std::string TypeName; /// Type name.
std::string File; /// Variation's graphics.
std::string ShadowFile; /// Variation's shadow graphics.
std::string LightFile; /// Variation's light graphics.
int FrameWidth;
int FrameHeight;
int ResourceMin;
int ResourceMax;
int Weight; /// The weight for when randomly choosing a variation
IconConfig Icon; /// Icon to display for this unit
CPlayerColorGraphic *Sprite; /// The graphic corresponding to File.
CGraphic *ShadowSprite; /// The graphic corresponding to ShadowFile.
CGraphic *LightSprite; /// The graphic corresponding to LightFile.
CAnimations *Animations; /// Animation scripts
CConstruction *Construction; /// What is shown in construction phase

std::string UpgradesRequired[VariationMax]; /// Upgrades required by variation
std::string UpgradesForbidden[VariationMax]; /// If player has one of these upgrades, unit can't have this variation
int FrameWidth = 0;
int FrameHeight = 0;
int ResourceMin = 0;
int ResourceMax = 0;
int Weight = 1; /// The weight for when randomly choosing a variation
IconConfig Icon; /// Icon to display for this unit
CPlayerColorGraphic *Sprite = nullptr; /// The graphic corresponding to File.
CGraphic *ShadowSprite = nullptr; /// The graphic corresponding to ShadowFile.
CGraphic *LightSprite = nullptr; /// The graphic corresponding to LightFile.
CAnimations *Animations = nullptr; /// Animation scripts
CConstruction *Construction = nullptr; /// What is shown in construction phase

std::vector<const CUpgrade *> UpgradesRequired; /// Upgrades required by variation
std::vector<const CUpgrade *> UpgradesForbidden; /// If the player has one of these upgrades, the unit can't have this variation
std::vector<int> ItemClassesEquipped;
std::vector<int> ItemClassesNotEquipped;
std::vector<CUnitType *> ItemsEquipped;
std::vector<CUnitType *> ItemsNotEquipped;
std::vector<CTerrainType *> Terrains;
std::vector<CTerrainType *> TerrainsForbidden;
std::vector<CSeason *> Seasons;
std::vector<CSeason *> ForbiddenSeasons;
std::vector<const CUnitType *> ItemsEquipped;
std::vector<const CUnitType *> ItemsNotEquipped;
std::vector<const CTerrainType *> Terrains;
std::vector<const CTerrainType *> TerrainsForbidden;
std::vector<const CSeason *> Seasons;
std::vector<const CSeason *> ForbiddenSeasons;

std::string LayerFiles[MaxImageLayers]; /// Variation's layer graphics.
std::string FileWhenLoaded[MaxCosts]; /// Change the graphic when the unit is loaded.
Expand Down Expand Up @@ -907,8 +906,8 @@ class CUnitType
void UpdateDefaultBoolFlags();
int GetAvailableLevelUpUpgrades() const;
int GetResourceStep(const int resource, const int player) const;
VariationInfo *GetDefaultVariation(CPlayer &player, int image_layer = -1) const;
VariationInfo *GetVariation(const std::string &variation_name, int image_layer = -1) const;
CUnitTypeVariation *GetDefaultVariation(CPlayer &player, int image_layer = -1) const;
CUnitTypeVariation *GetVariation(const std::string &variation_name, int image_layer = -1) const;
std::string GetRandomVariationIdent(int image_layer = -1) const;
std::string GetDefaultName(CPlayer &player) const;
CPlayerColorGraphic *GetDefaultLayerSprite(CPlayer &player, int image_layer) const;
Expand Down Expand Up @@ -1103,9 +1102,9 @@ class CUnitType
int GrandStrategyProductionEfficiencyModifier[MaxCosts]; /// production modifier for a particular resource for grand strategy mode (used for buildings)
//Wyrmgus end
ResourceInfo *ResInfo[MaxCosts]; /// Resource information.
std::vector<CUnitTypeVariation *> Variations; /// Variation information
//Wyrmgus start
VariationInfo *VarInfo[VariationMax]; /// Variation information.
std::vector<VariationInfo *> LayerVarInfo[MaxImageLayers]; /// Layer variation information.
std::vector<CUnitTypeVariation *> LayerVariations[MaxImageLayers]; /// Layer variation information
//Wyrmgus end
std::vector<CBuildRestriction *> BuildingRules; /// Rules list for building a building.
std::vector<CBuildRestriction *> AiBuildingRules; /// Rules list for for AI to build a building.
Expand Down
4 changes: 2 additions & 2 deletions src/map/map_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ void CMapLayer::SetSeason(CScheduledSeason *season)
if (
unit && unit->IsAlive() && unit->MapLayer == this
) {
VariationInfo *varinfo = unit->Type->VarInfo[unit->Variation];
if (varinfo && !unit->CheckSeasonForVariation(varinfo)) {
const CUnitTypeVariation *variation = unit->GetVariation();
if (variation && !unit->CheckSeasonForVariation(variation)) {
unit->ChooseVariation(); //choose a new variation, as the old one has become invalid due to the season change
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/map/mapfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ void CMapField::SetTerrain(CTerrainType *terrain_type)
this->Flags |= MapFieldUnpassable;
this->Flags |= MapFieldAirUnpassable;
}
VariationInfo *varinfo = unit.Type->VarInfo[unit.Variation];
if (varinfo && !unit.CheckTerrainForVariation(varinfo)) { // if a unit that is on the tile has a terrain-dependent variation that is not compatible with the current variation, repick the unit's variation
const CUnitTypeVariation *variation = unit.GetVariation();
if (variation && !unit.CheckTerrainForVariation(variation)) { // if a unit that is on the tile has a terrain-dependent variation that is not compatible with the current variation, repick the unit's variation
unit.ChooseVariation();
}
}
Expand Down
33 changes: 16 additions & 17 deletions src/sound/unitsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,29 @@ static void MapAnimSounds(CUnitType &type)
MapAnimSounds2(type.Animations->Harvest[i]);
}
//Wyrmgus start
for (int var_i = 0; var_i < VariationMax; ++var_i) {
VariationInfo *varinfo = type.VarInfo[var_i];
if (!varinfo) {
for (CUnitTypeVariation *variation : type.Variations) {
if (!variation) {
continue;
}
if (!varinfo->Animations) {
if (!variation->Animations) {
continue;
}
MapAnimSounds2(varinfo->Animations->Start);
MapAnimSounds2(varinfo->Animations->Still);
MapAnimSounds2(varinfo->Animations->Move);
MapAnimSounds2(varinfo->Animations->Attack);
MapAnimSounds2(varinfo->Animations->RangedAttack);
MapAnimSounds2(varinfo->Animations->SpellCast);
MapAnimSounds2(variation->Animations->Start);
MapAnimSounds2(variation->Animations->Still);
MapAnimSounds2(variation->Animations->Move);
MapAnimSounds2(variation->Animations->Attack);
MapAnimSounds2(variation->Animations->RangedAttack);
MapAnimSounds2(variation->Animations->SpellCast);
for (int i = 0; i <= ANIMATIONS_DEATHTYPES; ++i) {
MapAnimSounds2(varinfo->Animations->Death[i]);
MapAnimSounds2(variation->Animations->Death[i]);
}
MapAnimSounds2(varinfo->Animations->Repair);
MapAnimSounds2(varinfo->Animations->Train);
MapAnimSounds2(varinfo->Animations->Research);
MapAnimSounds2(varinfo->Animations->Upgrade);
MapAnimSounds2(varinfo->Animations->Build);
MapAnimSounds2(variation->Animations->Repair);
MapAnimSounds2(variation->Animations->Train);
MapAnimSounds2(variation->Animations->Research);
MapAnimSounds2(variation->Animations->Upgrade);
MapAnimSounds2(variation->Animations->Build);
for (int i = 0; i < MaxCosts; ++i) {
MapAnimSounds2(varinfo->Animations->Harvest[i]);
MapAnimSounds2(variation->Animations->Harvest[i]);
}
}
//Wyrmgus end
Expand Down
4 changes: 2 additions & 2 deletions src/ui/botpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1178,8 +1178,8 @@ void CButtonPanel::Draw()
button_icon = Selected[0]->UnitInside->GetButtonIcon(buttons[i].Action);
} else if (buttons[i].Icon.Name.empty() && Selected[0]->GetButtonIcon(buttons[i].Action) != nullptr) {
button_icon = Selected[0]->GetButtonIcon(buttons[i].Action);
} else if (buttons[i].Action == ButtonExperienceUpgradeTo && Selected[0]->Type->VarInfo[Selected[0]->Variation] && UnitTypes[buttons[i].Value]->GetVariation(Selected[0]->Type->VarInfo[Selected[0]->Variation]->VariationId) != nullptr && !UnitTypes[buttons[i].Value]->GetVariation(Selected[0]->Type->VarInfo[Selected[0]->Variation]->VariationId)->Icon.Name.empty()) {
button_icon = UnitTypes[buttons[i].Value]->GetVariation(Selected[0]->Type->VarInfo[Selected[0]->Variation]->VariationId)->Icon.Icon;
} else if (buttons[i].Action == ButtonExperienceUpgradeTo && Selected[0]->GetVariation() && UnitTypes[buttons[i].Value]->GetVariation(Selected[0]->GetVariation()->VariationId) != nullptr && !UnitTypes[buttons[i].Value]->GetVariation(Selected[0]->GetVariation()->VariationId)->Icon.Name.empty()) {
button_icon = UnitTypes[buttons[i].Value]->GetVariation(Selected[0]->GetVariation()->VariationId)->Icon.Icon;
} else if ((buttons[i].Action == ButtonTrain || buttons[i].Action == ButtonBuild || buttons[i].Action == ButtonUpgradeTo || buttons[i].Action == ButtonExperienceUpgradeTo) && buttons[i].Icon.Name.empty() && UnitTypes[buttons[i].Value]->GetDefaultVariation(*ThisPlayer) != nullptr && !UnitTypes[buttons[i].Value]->GetDefaultVariation(*ThisPlayer)->Icon.Name.empty()) {
button_icon = UnitTypes[buttons[i].Value]->GetDefaultVariation(*ThisPlayer)->Icon.Icon;
} else if ((buttons[i].Action == ButtonTrain || buttons[i].Action == ButtonBuild || buttons[i].Action == ButtonUpgradeTo || buttons[i].Action == ButtonExperienceUpgradeTo) && buttons[i].Icon.Name.empty() && !UnitTypes[buttons[i].Value]->Icon.Name.empty()) {
Expand Down
8 changes: 4 additions & 4 deletions src/ui/mainscr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,9 @@ static void DrawUnitInfo_Training(const CUnit &unit)
if (UI.SingleTrainingButton) {
const COrder_Train &order = *static_cast<COrder_Train *>(unit.CurrentOrder());
//Wyrmgus sta
VariationInfo *varinfo = order.GetUnitType().GetDefaultVariation(*ThisPlayer);
CUnitTypeVariation *variation = order.GetUnitType().GetDefaultVariation(*ThisPlayer);
// CIcon &icon = *order.GetUnitType().Icon.Icon;
CIcon &icon = (varinfo && varinfo->Icon.Icon) ? *varinfo->Icon.Icon : *order.GetUnitType().Icon.Icon;
CIcon &icon = (variation && variation->Icon.Icon) ? *variation->Icon.Icon : *order.GetUnitType().Icon.Icon;
//Wyrmgus end
//Wyrmgus start
// const unsigned int flags = (ButtonAreaUnderCursor == ButtonAreaTraining && ButtonUnderCursor == 0) ?
Expand Down Expand Up @@ -556,8 +556,8 @@ static void DrawUnitInfo_Training(const CUnit &unit)
if (j >= UI.TrainingButtons.size()) {
break;
}
VariationInfo *varinfo = order.GetUnitType().GetDefaultVariation(*ThisPlayer);
CIcon &icon = (varinfo && varinfo->Icon.Icon) ? *varinfo->Icon.Icon : *order.GetUnitType().Icon.Icon;
CUnitTypeVariation *variation = order.GetUnitType().GetDefaultVariation(*ThisPlayer);
CIcon &icon = (variation && variation->Icon.Icon) ? *variation->Icon.Icon : *order.GetUnitType().Icon.Icon;
//Wyrmgus start
// const int flag = (ButtonAreaUnderCursor == ButtonAreaTraining
int flag = (ButtonAreaUnderCursor == ButtonAreaTraining
Expand Down
13 changes: 9 additions & 4 deletions src/unit/script_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1965,15 +1965,20 @@ static int CclSetUnitVariable(lua_State *l)
} else if (!strcmp(name, "CustomHero")) {
unit->SetCharacter(LuaToString(l, 3), true);
} else if (!strcmp(name, "Variation")) {
value = LuaToNumber(l, 3);
unit->SetVariation(value);
unit->Variable[VARIATION_INDEX].Value = unit->Variation;
size_t variation_index = LuaToNumber(l, 3);
if (variation_index >= 0 && variation_index < unit->Type->Variations.size()) {
unit->SetVariation(unit->Type->Variations[variation_index]);
unit->Variable[VARIATION_INDEX].Value = unit->Variation;
}
} else if (!strcmp(name, "LayerVariation")) {
LuaCheckArgs(l, 4);
std::string image_layer_name = LuaToString(l, 3);
int image_layer = GetImageLayerIdByName(image_layer_name);
if (image_layer != -1) {
unit->SetVariation(LuaToNumber(l, 4), nullptr, image_layer);
size_t variation_index = LuaToNumber(l, 4);
if (variation_index >= 0 && variation_index < unit->Type->LayerVariations[image_layer].size()) {
unit->SetVariation(unit->Type->LayerVariations[image_layer][variation_index], nullptr, image_layer);
}
} else {
LuaError(l, "Image layer \"%s\" doesn't exist." _C_ image_layer_name.c_str());
}
Expand Down
Loading

0 comments on commit 81a3027

Please sign in to comment.