Skip to content

Commit

Permalink
Replace memcpy by ranges::copy.
Browse files Browse the repository at this point in the history
So fix copy of `ResInfo` in `CclCopyUnitType`
  • Loading branch information
Jarod42 committed May 20, 2024
1 parent 66f1a50 commit 27e9a2b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
15 changes: 6 additions & 9 deletions src/ui/botpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,24 +528,20 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int
LastDrawnButtonPopup = const_cast<ButtonAction *>(&button);
}

int popupWidth, popupHeight;
int Costs[ManaResCost + 1];
memset(Costs, 0, sizeof(Costs));

int Costs[ManaResCost + 1]{};
switch (button.Action) {
case ButtonCmd::Research:
memcpy(Costs, AllUpgrades[button.Value]->Costs, sizeof(AllUpgrades[button.Value]->Costs));
ranges::copy(AllUpgrades[button.Value]->Costs, std::begin(Costs));
break;
case ButtonCmd::SpellCast:
memcpy(Costs, SpellTypeTable[button.Value]->Costs, sizeof(SpellTypeTable[button.Value]->Costs));
ranges::copy(SpellTypeTable[button.Value]->Costs, std::begin(Costs));
Costs[ManaResCost] = SpellTypeTable[button.Value]->ManaCost;
break;
case ButtonCmd::Build:
case ButtonCmd::Train:
case ButtonCmd::UpgradeTo:
memcpy(Costs,
getUnitTypes()[button.Value]->Stats[ThisPlayer->Index].Costs,
sizeof(getUnitTypes()[button.Value]->Stats[ThisPlayer->Index].Costs));
ranges::copy(getUnitTypes()[button.Value]->Stats[ThisPlayer->Index].Costs,
std::begin(Costs));
Costs[FoodCost] = getUnitTypes()[button.Value]
->Stats[ThisPlayer->Index]
.Variables[DEMAND_INDEX]
Expand All @@ -555,6 +551,7 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int
break;
}

int popupWidth, popupHeight;
if (useCache) {
popupWidth = popupCache.popupWidth;
popupHeight = popupCache.popupHeight;
Expand Down
14 changes: 8 additions & 6 deletions src/unit/script_unittype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,9 +1239,9 @@ static int CclCopyUnitType(lua_State *l)
to->Portrait.Files = from.Portrait.Files;
to->Portrait.Mngs.resize(to->Portrait.Files.size());
#endif
memcpy(to->DefaultStat.Costs, from.DefaultStat.Costs, sizeof(from.DefaultStat.Costs));
memcpy(to->DefaultStat.Storing, from.DefaultStat.Storing, sizeof(from.DefaultStat.Storing));
memcpy(to->DefaultStat.ImproveIncomes, from.DefaultStat.ImproveIncomes, sizeof(from.DefaultStat.ImproveIncomes));
ranges::copy(from.DefaultStat.Costs, std::begin(to->DefaultStat.Costs));
ranges::copy(from.DefaultStat.Storing, std::begin(to->DefaultStat.Storing));
ranges::copy(from.DefaultStat.ImproveIncomes, std::begin(to->DefaultStat.ImproveIncomes));
to->Construction = from.Construction;
to->DrawLevel = from.DrawLevel;
to->MaxOnBoard = from.MaxOnBoard;
Expand Down Expand Up @@ -1310,7 +1310,7 @@ static int CclCopyUnitType(lua_State *l)
to->CanAttack = from.CanAttack;
to->RepairRange = from.RepairRange;
to->RepairHP = from.RepairHP;
memcpy(to->RepairCosts, from.RepairCosts, sizeof(from.RepairCosts));
ranges::copy(from.RepairCosts, std::begin(to->RepairCosts));
to->CanTarget = from.CanTarget;
to->Building = from.Building;
to->BuildingRules.clear();
Expand Down Expand Up @@ -1343,9 +1343,11 @@ static int CclCopyUnitType(lua_State *l)
to->BoolFlag[i].CanTargetFlag = from.BoolFlag[i].CanTargetFlag;
to->BoolFlag[i].AiPriorityTarget = from.BoolFlag[i].AiPriorityTarget;
}
memcpy(to->ResInfo, from.ResInfo, sizeof(from.ResInfo));
for (std::size_t i = 0; i != std::size(to->ResInfo); ++i) {
from.ResInfo[i] = to->ResInfo[i] ? std::make_unique<ResourceInfo>(*to->ResInfo[i]) : nullptr;
}
to->GivesResource = from.GivesResource;
memcpy(to->CanStore, from.CanStore, sizeof(from.CanStore));
ranges::copy(from.CanStore, std::begin(to->CanStore));
to->CanCastSpell = from.CanCastSpell;
to->AutoCastActive = from.AutoCastActive;
to->Sound.Selected.Name = from.Sound.Selected.Name;
Expand Down

0 comments on commit 27e9a2b

Please sign in to comment.