Skip to content

Commit

Permalink
Refactored the way units contained in another unit are stored, to mak…
Browse files Browse the repository at this point in the history
…e handling them easier
  • Loading branch information
Andrettin committed Jan 4, 2022
1 parent 3cb8a32 commit e2191df
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 156 deletions.
4 changes: 2 additions & 2 deletions src/action/action_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ int COrder_Resource::StartGathering(CUnit &unit)
if (!goal->Type->BoolFlag[HARVESTFROMOUTSIDE_INDEX].value) {
//Wyrmgus end
//Wyrmgus start
// if (goal->Variable[MAXHARVESTERS_INDEX].Value == 0 || goal->Variable[MAXHARVESTERS_INDEX].Value > goal->InsideCount) {
// if (goal->Variable[MAXHARVESTERS_INDEX].Value == 0 || goal->Variable[MAXHARVESTERS_INDEX].Value > goal->get_units_inside().size()) {
if (goal->Variable[MAXHARVESTERS_INDEX].Value == 0 || goal->Variable[MAXHARVESTERS_INDEX].Value > goal->Resource.Active) {
//Wyrmgus end
this->clear_goal();
Expand All @@ -706,7 +706,7 @@ int COrder_Resource::StartGathering(CUnit &unit)
unit.Removed = 1;
}
//Wyrmgus start
// } else if (goal->Variable[MAXHARVESTERS_INDEX].Value <= goal->InsideCount) {
// } else if (goal->Variable[MAXHARVESTERS_INDEX].Value <= goal->get_units_inside().size()) {
} else if (goal->Variable[MAXHARVESTERS_INDEX].Value <= goal->Resource.Active) {
//Wyrmgus end
//Resource is full, wait
Expand Down
17 changes: 9 additions & 8 deletions src/action/action_unload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,7 @@ static bool IsDropZonePossible(const CUnit &transporter, const Vec2i &pos, int z
Vec2i dummyPos;
//Wyrmgus start
/*
CUnit *unit = transporter.UnitInside;
for (int i = 0; i < transporter.InsideCount; ++i, unit = unit->NextContained) {
for (const CUnit *unit : transporter.get_units_inside()) {
//Wyrmgus start
// if (FindUnloadPosition(transporter, *unit, pos, maxUnloadRange, &dummyPos)) {
if (FindUnloadPosition(transporter, *unit, pos, maxUnloadRange, &dummyPos, z, landmass)) {
Expand All @@ -385,8 +384,7 @@ static bool IsDropZonePossible(const CUnit &transporter, const Vec2i &pos, int z
return true;
}
} else {
CUnit *unit = transporter.UnitInside;
for (int i = 0; i < transporter.InsideCount; ++i, unit = unit->NextContained) {
for (const CUnit *unit : transporter.get_units_inside()) {
if (FindUnloadPosition(transporter, *unit, pos, maxUnloadRange, &dummyPos, z, landmass)) {
return true;
}
Expand Down Expand Up @@ -482,9 +480,10 @@ static int ClosestFreeDropZone(CUnit &transporter, const Vec2i &startPos, int ma
//Wyrmgus end
{
// Check there are units onboard
if (!transporter.UnitInside) {
if (!transporter.has_units_inside()) {
return 0;
}

const bool isTransporterRemoved = transporter.Removed;
const bool selected = transporter.Selected;

Expand Down Expand Up @@ -567,9 +566,11 @@ bool COrder_Unload::LeaveTransporter(CUnit &transporter)
++stillonboard;
}
} else {
// Unload all units.
CUnit *goal = transporter.UnitInside;
for (int i = transporter.InsideCount; i; --i, goal = goal->NextContained) {
//copy the vector since we may modify it
const std::vector<CUnit *> units_inside = transporter.get_units_inside();

//unload all units
for (CUnit *goal : units_inside) {
if (goal->Boarded) {
//Wyrmgus start
// if (!UnloadUnit(transporter, *goal)) {
Expand Down
12 changes: 9 additions & 3 deletions src/action/action_upgradeto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,20 @@ int TransformUnitIntoType(CUnit &unit, const wyrmgus::unit_type &newtype)
}

//drop units that can no longer be in the container
if (unit.UnitInside) {
CUnit *uins = unit.UnitInside;
for (int i = unit.InsideCount; i && unit.BoardCount > newtype.MaxOnBoard; --i, uins = uins->NextContained) {
if (unit.has_units_inside() && unit.BoardCount > newtype.MaxOnBoard) {
//copy the vector since we may modify it
const std::vector<CUnit *> units_inside = unit.get_units_inside();

for (CUnit *uins : units_inside) {
if (uins->Boarded) {
uins->Boarded = 0;
unit.BoardCount -= uins->Type->BoardSize;
DropOutOnSide(*uins, LookingW, &unit);
}

if (unit.BoardCount <= newtype.MaxOnBoard) {
break;
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/ai/ai_plan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,7 @@ void PlayerAi::check_transporters()
continue;
}

CUnit *uins = ai_transporter->UnitInside;
for (int j = 0; j < ai_transporter->InsideCount; ++j, uins = uins->NextContained) {
for (CUnit *uins : ai_transporter->get_units_inside()) {
if (uins->GroupId == 0 && !this->is_site_transport_unit(uins)) {
//if the unit no longer is part of a force, then it likely has been reset and the attack through water has been cancelled, so unload it
CommandUnload(*ai_transporter, ai_transporter->tilePos, uins, 0, ai_transporter->MapLayer->ID);
Expand Down
2 changes: 1 addition & 1 deletion src/animation/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ int ParseAnimInt(const CUnit &unit, const std::string &parseint)
} else if (cur == "ResourceActive") {
return goal->Resource.Active;
} else if (cur == "InsideCount") {
return goal->InsideCount;
return static_cast<int>(goal->get_units_inside().size());
} else if (cur == "_Distance") {
return unit.MapDistanceTo(*goal);
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/loadgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ static void PlaceUnits()

//Wyrmgus start
//calculate attack range for containers now, as when loading a game it couldn't be done when the container was initially loaded
if (unit->BoardCount > 0 && unit->InsideCount > 0) {
if (unit->BoardCount > 0 && unit->has_units_inside()) {
unit->UpdateContainerAttackRange();
}

Expand Down
7 changes: 4 additions & 3 deletions src/player/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2957,10 +2957,11 @@ bool CPlayer::capture_unit(CUnit *unit)
}
}

if (unit->UnitInside != nullptr) {
CUnit *unit_inside = unit->UnitInside;
if (unit->has_units_inside()) {
//copy the vector since we may modify it
const std::vector<CUnit *> units_inside = unit->get_units_inside();

for (int i = unit->InsideCount; i; --i, unit_inside = unit_inside->NextContained) {
for (CUnit *unit_inside : units_inside) {
if (unit_inside->Player->is_neutral_player()) {
continue;
}
Expand Down
18 changes: 9 additions & 9 deletions src/ui/botpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1121,8 +1121,8 @@ void CButtonPanel::Draw(std::vector<std::function<void(renderer *)>> &render_com
QColor border_color;

// if there is a single unit selected, show the icon of its weapon/shield/boots/arrows equipped for the appropriate buttons
if (button->Icon.Name.empty() && button->Action == ButtonCmd::Attack && Selected[0]->Type->CanTransport() && Selected[0]->Type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value && Selected[0]->BoardCount > 0 && Selected[0]->UnitInside != nullptr && Selected[0]->UnitInside->Type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value && Selected[0]->UnitInside->GetButtonIcon(button->Action) != nullptr) {
button_icon = Selected[0]->UnitInside->GetButtonIcon(button->Action);
if (button->Icon.Name.empty() && button->Action == ButtonCmd::Attack && Selected[0]->Type->CanTransport() && Selected[0]->Type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value && Selected[0]->BoardCount > 0 && Selected[0]->has_units_inside() && Selected[0]->get_units_inside().at(0)->Type->BoolFlag[ATTACKFROMTRANSPORTER_INDEX].value && Selected[0]->get_units_inside().at(0)->GetButtonIcon(button->Action) != nullptr) {
button_icon = Selected[0]->get_units_inside().at(0)->GetButtonIcon(button->Action);
} else if (button->Icon.Name.empty() && Selected[0]->GetButtonIcon(button->Action) != nullptr) {
button_icon = Selected[0]->GetButtonIcon(button->Action);
} else if (button->Action == ButtonCmd::ExperienceUpgradeTo && Selected[0]->GetVariation() && button_unit_type->GetVariation(Selected[0]->GetVariation()->get_identifier()) != nullptr && !button_unit_type->GetVariation(Selected[0]->GetVariation()->get_identifier())->Icon.Name.empty()) {
Expand Down Expand Up @@ -1211,23 +1211,23 @@ void CButtonPanel::Draw(std::vector<std::function<void(renderer *)>> &render_com

//Wyrmgus start
if (ButtonAreaUnderCursor == ButtonAreaTransporting) {
CUnit *uins = Selected[0]->UnitInside;
size_t j = 0;
size_t i = 0;

for (int i = 0; i < Selected[0]->InsideCount; ++i, uins = uins->NextContained) {
if (!uins->Boarded || j >= UI.TransportingButtons.size() || (Selected[0]->Player != CPlayer::GetThisPlayer() && uins->Player != CPlayer::GetThisPlayer())) {
for (const CUnit *uins : Selected[0]->get_units_inside()) {
if (!uins->Boarded || i >= UI.TransportingButtons.size() || (Selected[0]->Player != CPlayer::GetThisPlayer() && uins->Player != CPlayer::GetThisPlayer())) {
continue;
}
if (static_cast<size_t>(ButtonUnderCursor) == j) {
if (static_cast<size_t>(ButtonUnderCursor) == i) {
const wyrmgus::font_color *text_color = nullptr;
if (uins->get_unique() != nullptr || uins->get_character() != nullptr) {
text_color = wyrmgus::defines::get()->get_unique_font_color();
} else if (uins->Prefix != nullptr || uins->Suffix != nullptr) {
text_color = wyrmgus::defines::get()->get_magic_font_color();
}
DrawGenericPopup(uins->GetMessageName(), UI.TransportingButtons[j].X, UI.TransportingButtons[j].Y, text_color, nullptr, render_commands);
DrawGenericPopup(uins->GetMessageName(), UI.TransportingButtons[i].X, UI.TransportingButtons[i].Y, text_color, nullptr, render_commands);
}
++j;

++i;
}
}
//Wyrmgus end
Expand Down
6 changes: 5 additions & 1 deletion src/ui/contenttype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ static const CUnit *GetUnitRef(const CUnit &unit, EnumUnit e)
case UnitRefItSelf:
return &unit;
case UnitRefInside:
return unit.UnitInside;
if (unit.has_units_inside()) {
return unit.get_units_inside().at(0);
}

return nullptr;
case UnitRefContainer:
return unit.Container;
case UnitRefWorker :
Expand Down
15 changes: 6 additions & 9 deletions src/ui/mainscr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,9 @@ static bool DrawUnitInfo_single_selection(const CUnit &unit, std::vector<std::fu

static void DrawUnitInfo_transporter(CUnit &unit, std::vector<std::function<void(renderer *)>> &render_commands)
{
CUnit *uins = unit.UnitInside;
size_t j = 0;

for (int i = 0; i < unit.InsideCount; ++i, uins = uins->NextContained) {
for (const CUnit *uins : unit.get_units_inside()) {
//Wyrmgus start
// if (!uins->Boarded || j >= UI.TransportingButtons.size()) {
if (!uins->Boarded || j >= UI.TransportingButtons.size() || (unit.Player != CPlayer::GetThisPlayer() && uins->Player != CPlayer::GetThisPlayer())) {
Expand Down Expand Up @@ -639,10 +638,9 @@ static void DrawUnitInfo_transporter(CUnit &unit, std::vector<std::function<void
//Wyrmgus start
static void DrawUnitInfo_inventory(CUnit &unit, std::vector<std::function<void(renderer *)>> &render_commands)
{
CUnit *uins = unit.UnitInside;
size_t j = 0;

for (int i = 0; i < unit.InsideCount; ++i, uins = uins->NextContained) {
for (const CUnit *uins : unit.get_units_inside()) {
if (!uins->Type->BoolFlag[ITEM_INDEX].value || j >= UI.InventoryButtons.size()) {
continue;
}
Expand Down Expand Up @@ -709,7 +707,7 @@ static void DrawUnitInfo(CUnit &unit, std::vector<std::function<void(renderer *)
}

//Wyrmgus start
if (unit.HasInventory() && unit.InsideCount && CurrentButtonLevel == wyrmgus::defines::get()->get_inventory_button_level()) {
if (unit.HasInventory() && unit.has_units_inside() && CurrentButtonLevel == defines::get()->get_inventory_button_level()) {
DrawUnitInfo_inventory(unit, render_commands);
return;
}
Expand Down Expand Up @@ -830,11 +828,10 @@ void DrawPopups(std::vector<std::function<void(renderer *)>> &render_commands)
LastDrawnButtonPopup = nullptr;
}

if (!(Selected[0]->Player != CPlayer::GetThisPlayer() && !CPlayer::GetThisPlayer()->is_allied_with(*Selected[0]->Player) && !CPlayer::GetThisPlayer()->has_building_access(Selected[0])) && Selected[0]->HasInventory() && Selected[0]->InsideCount && CurrentButtonLevel == wyrmgus::defines::get()->get_inventory_button_level()) {
CUnit *uins = Selected[0]->UnitInside;
size_t j = 0;
if (!(Selected[0]->Player != CPlayer::GetThisPlayer() && !CPlayer::GetThisPlayer()->is_allied_with(*Selected[0]->Player) && !CPlayer::GetThisPlayer()->has_building_access(Selected[0])) && Selected[0]->HasInventory() && Selected[0]->has_units_inside() && CurrentButtonLevel == defines::get()->get_inventory_button_level()) {
size_t j = 0;

for (int i = 0; i < Selected[0]->InsideCount; ++i, uins = uins->NextContained) {
for (const CUnit *uins : Selected[0]->get_units_inside()) {
if (!uins->Type->BoolFlag[ITEM_INDEX].value || j >= UI.InventoryButtons.size()) {
continue;
}
Expand Down
13 changes: 7 additions & 6 deletions src/ui/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ static void HandleMouseOn(const PixelPos screenPos)
if (Selected.size() == 1 && Selected[0]->HasInventory() && CurrentButtonLevel == wyrmgus::defines::get()->get_inventory_button_level()) {
const size_t size = UI.InventoryButtons.size();

for (size_t i = std::min<size_t>(Selected[0]->InsideCount, size); i != 0;) {
for (size_t i = std::min<size_t>(Selected[0]->get_units_inside().size(), size); i != 0;) {
--i;
if (UI.InventoryButtons[i].Contains(screenPos)) {
ButtonAreaUnderCursor = ButtonAreaInventory;
Expand Down Expand Up @@ -2522,10 +2522,9 @@ static void UIHandleButtonUp_OnButton(unsigned button, const Qt::KeyboardModifie
if (!GameObserve && !game::get()->is_paused() && !GameEstablishing && (CPlayer::GetThisPlayer()->IsTeamed(*Selected[0]) || CPlayer::GetThisPlayer()->is_allied_with(*Selected[0]) || CPlayer::GetThisPlayer()->has_building_access(Selected[0]))) {
//Wyrmgus end
if (Selected[0]->BoardCount >= ButtonUnderCursor) {
CUnit *uins = Selected[0]->UnitInside;
size_t j = 0;

for (int i = 0; i < Selected[0]->InsideCount; ++i, uins = uins->NextContained) {
for (CUnit *uins : Selected[0]->get_units_inside()) {
if (!uins->Boarded || j >= UI.TransportingButtons.size() || (Selected[0]->Player != CPlayer::GetThisPlayer() && uins->Player != CPlayer::GetThisPlayer())) {
continue;
}
Expand All @@ -2544,11 +2543,13 @@ static void UIHandleButtonUp_OnButton(unsigned button, const Qt::KeyboardModifie
} else if (ButtonAreaUnderCursor == ButtonAreaInventory) {
// for inventory unit
if (!GameObserve && !game::get()->is_paused() && !GameEstablishing && CPlayer::GetThisPlayer()->IsTeamed(*Selected[0])) {
if (Selected[0]->InsideCount >= ButtonUnderCursor) {
CUnit *uins = Selected[0]->UnitInside;
if (static_cast<int>(Selected[0]->get_units_inside().size()) >= ButtonUnderCursor) {
//copy the vector since we may modify it
const std::vector<CUnit *> units_inside = Selected[0]->get_units_inside();

size_t j = 0;

for (int i = 0; i < Selected[0]->InsideCount; ++i, uins = uins->NextContained) {
for (CUnit *uins : units_inside) {
if (!uins->Type->BoolFlag[ITEM_INDEX].value || j >= UI.InventoryButtons.size() || (Selected[0]->Player != CPlayer::GetThisPlayer() && uins->Player != CPlayer::GetThisPlayer())) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/unit/script_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1626,8 +1626,8 @@ static int CclGetUnitsInsideUnit(lua_State *l)

lua_newtable(l);

CUnit *unit = transporter.UnitInside;
for (int i = 0; i < transporter.InsideCount; ++i, unit = unit->NextContained) {
for (size_t i = 0; i < transporter.get_units_inside().size(); ++i) {
const CUnit *unit = transporter.get_units_inside().at(i);
lua_pushnumber(l, UnitNumber(*unit));
lua_rawseti(l, -2, i + 1);
}
Expand Down
Loading

0 comments on commit e2191df

Please sign in to comment.