1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-23 22:37:55 +02:00

code review

This commit is contained in:
Laserlicht
2025-10-28 14:39:14 +01:00
parent ea001c6973
commit b218542d51
3 changed files with 24 additions and 24 deletions

View File

@@ -140,12 +140,12 @@ BattleOnlyModeWindow::BattleOnlyModeWindow()
startInfo->selectedTerrain = TerrainId::DIRT; startInfo->selectedTerrain = TerrainId::DIRT;
startInfo->selectedTown = std::nullopt; startInfo->selectedTown = std::nullopt;
startInfo->selectedHero[0] = std::nullopt; startInfo->selectedHero[0] = std::nullopt;
startInfo->selectedArmy[0].fill(std::nullopt); startInfo->selectedArmy[0].fill(CStackBasicDescriptor(CreatureID::NONE, 1));
for(size_t i=0; i<GameConstants::ARMY_SIZE; i++) for(size_t i=0; i<GameConstants::ARMY_SIZE; i++)
heroSelector1->selectedArmyInput.at(i)->setText("1"); heroSelector1->selectedArmyInput.at(i)->setText("1");
} }
startInfo->selectedHero[1] = std::nullopt; startInfo->selectedHero[1] = std::nullopt;
startInfo->selectedArmy[1].fill(std::nullopt); startInfo->selectedArmy[1].fill(CStackBasicDescriptor(CreatureID::NONE, 1));
for(size_t i=0; i<GameConstants::ARMY_SIZE; i++) for(size_t i=0; i<GameConstants::ARMY_SIZE; i++)
heroSelector2->selectedArmyInput.at(i)->setText("1"); heroSelector2->selectedArmyInput.at(i)->setText("1");
onChange(); onChange();
@@ -204,7 +204,7 @@ void BattleOnlyModeWindow::setTerrainButtonText()
void BattleOnlyModeWindow::setOkButtonEnabled() void BattleOnlyModeWindow::setOkButtonEnabled()
{ {
bool army2Empty = std::all_of(startInfo->selectedArmy[1].begin(), startInfo->selectedArmy[1].end(), [](const auto x) { return !x; }); bool army2Empty = std::all_of(startInfo->selectedArmy[1].begin(), startInfo->selectedArmy[1].end(), [](const auto x) { return x.getId() == CreatureID::NONE; });
bool canStart = (startInfo->selectedTerrain || startInfo->selectedTown); bool canStart = (startInfo->selectedTerrain || startInfo->selectedTown);
canStart &= (startInfo->selectedHero[0] && ((startInfo->selectedHero[1]) || (startInfo->selectedTown && !army2Empty))); canStart &= (startInfo->selectedHero[0] && ((startInfo->selectedHero[1]) || (startInfo->selectedTown && !army2Empty)));
@@ -256,9 +256,9 @@ BattleOnlyModeHeroSelector::BattleOnlyModeHeroSelector(int id, BattleOnlyModeWin
selectedArmyInput.back()->setFilterNumber(1, 10000000, 3); selectedArmyInput.back()->setFilterNumber(1, 10000000, 3);
selectedArmyInput.back()->setText("1"); selectedArmyInput.back()->setText("1");
selectedArmyInput.back()->setCallback([this, i, id](const std::string & text){ selectedArmyInput.back()->setCallback([this, i, id](const std::string & text){
if(parent.startInfo->selectedArmy[id][i]) if(parent.startInfo->selectedArmy[id][i].getId() != CreatureID::NONE)
{ {
(*parent.startInfo->selectedArmy[id][i]).second = TextOperations::parseMetric<int>(text); parent.startInfo->selectedArmy[id][i].setCount(TextOperations::parseMetric<int>(text));
parent.onChange(); parent.onChange();
} }
else else
@@ -358,17 +358,17 @@ void BattleOnlyModeHeroSelector::setCreatureIcons()
for(int i = 0; i < creatureImage.size(); i++) for(int i = 0; i < creatureImage.size(); i++)
{ {
if(!parent.startInfo->selectedArmy[id][i]) if(parent.startInfo->selectedArmy[id][i].getId() == CreatureID::NONE)
{ {
creatureImage[i] = std::make_shared<CPicture>(drawBlackBox(Point(32, 32), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeSelect"), id == 1 ? Colors::WHITE : parent.disabledColor), Point(6 + i * 36, 78)); creatureImage[i] = std::make_shared<CPicture>(drawBlackBox(Point(32, 32), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeSelect"), id == 1 ? Colors::WHITE : parent.disabledColor), Point(6 + i * 36, 78));
selectedArmyInput[i]->setText("1"); selectedArmyInput[i]->setText("1");
} }
else else
{ {
auto unit = (*parent.startInfo->selectedArmy[id][i]); auto unit = parent.startInfo->selectedArmy[id][i];
auto creatureID = unit.first; auto creatureID = unit.getId();
creatureImage[i] = std::make_shared<CPicture>(ENGINE->renderHandler().loadAnimation(AnimationPath::builtin("CPRSMALL"), EImageBlitMode::COLORKEY)->getImage(LIBRARY->creh->objects.at(creatureID)->getIconIndex()), Point(6 + i * 36, 78)); creatureImage[i] = std::make_shared<CPicture>(ENGINE->renderHandler().loadAnimation(AnimationPath::builtin("CPRSMALL"), EImageBlitMode::COLORKEY)->getImage(LIBRARY->creh->objects.at(creatureID)->getIconIndex()), Point(6 + i * 36, 78));
selectedArmyInput[i]->setText(TextOperations::formatMetric(unit.second, 3)); selectedArmyInput[i]->setText(TextOperations::formatMetric(unit.getCount(), 3));
} }
creatureImage[i]->addLClickCallback([this, i](){ creatureImage[i]->addLClickCallback([this, i](){
@@ -384,8 +384,8 @@ void BattleOnlyModeHeroSelector::setCreatureIcons()
return creatureA->getFactionID() < creatureB->getFactionID(); return creatureA->getFactionID() < creatureB->getFactionID();
}); });
int selectedIndex = !parent.startInfo->selectedArmy[id][i] ? 0 : (1 + std::distance(creatures.begin(), std::find_if(creatures.begin(), creatures.end(), [this, i](auto creatureID) { int selectedIndex = parent.startInfo->selectedArmy[id][i].getId() == CreatureID::NONE ? 0 : (1 + std::distance(creatures.begin(), std::find_if(creatures.begin(), creatures.end(), [this, i](auto creatureID) {
return creatureID == (*parent.startInfo->selectedArmy[id][i]).first; return creatureID == parent.startInfo->selectedArmy[id][i].getId();
}))); })));
std::vector<std::string> texts; std::vector<std::string> texts;
@@ -404,15 +404,15 @@ void BattleOnlyModeHeroSelector::setCreatureIcons()
auto window = std::make_shared<CObjectListWindow>(texts, nullptr, LIBRARY->generaltexth->translate("core.genrltxt.42"), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeCreatureSelect"), [this, creatures, i](int index){ auto window = std::make_shared<CObjectListWindow>(texts, nullptr, LIBRARY->generaltexth->translate("core.genrltxt.42"), LIBRARY->generaltexth->translate("vcmi.lobby.battleOnlyModeCreatureSelect"), [this, creatures, i](int index){
if(index == 0) if(index == 0)
{ {
if(parent.startInfo->selectedArmy[id][i]) if(parent.startInfo->selectedArmy[id][i].getId() == CreatureID::NONE)
parent.startInfo->selectedArmy[id][i] = std::nullopt; parent.startInfo->selectedArmy[id][i] = CStackBasicDescriptor(CreatureID::NONE, 1);
parent.onChange(); parent.onChange();
return; return;
} }
index--; index--;
auto creature = creatures.at(index).toCreature(); auto creature = creatures.at(index).toCreature();
parent.startInfo->selectedArmy[id][i] = std::make_pair(creature->getId(), 100); parent.startInfo->selectedArmy[id][i] = CStackBasicDescriptor(creature->getId(), 100);
parent.onChange(); parent.onChange();
}, selectedIndex, images, true, true); }, selectedIndex, images, true, true);
window->onPopup = [creatures](int index) { window->onPopup = [creatures](int index) {
@@ -426,10 +426,10 @@ void BattleOnlyModeHeroSelector::setCreatureIcons()
}); });
creatureImage[i]->addRClickCallback([this, i](){ creatureImage[i]->addRClickCallback([this, i](){
if(!parent.startInfo->selectedArmy[id][i]) if(parent.startInfo->selectedArmy[id][i].getId() == CreatureID::NONE)
return; return;
ENGINE->windows().createAndPushWindow<CStackWindow>(LIBRARY->creh->objects.at((*parent.startInfo->selectedArmy[id][i]).first).get(), true); ENGINE->windows().createAndPushWindow<CStackWindow>(LIBRARY->creh->objects.at(parent.startInfo->selectedArmy[id][i].getId()).get(), true);
}); });
} }
} }
@@ -463,8 +463,8 @@ void BattleOnlyModeWindow::startBattle()
obj->pushPrimSkill(PrimarySkill(i), startInfo->primSkillLevel[sel][i]); obj->pushPrimSkill(PrimarySkill(i), startInfo->primSkillLevel[sel][i]);
obj->clearSlots(); obj->clearSlots();
for(int slot = 0; slot < GameConstants::ARMY_SIZE; slot++) for(int slot = 0; slot < GameConstants::ARMY_SIZE; slot++)
if(startInfo->selectedArmy[sel][slot]) if(startInfo->selectedArmy[sel][slot].getId() != CreatureID::NONE)
obj->setCreature(SlotID(slot), (*startInfo->selectedArmy[sel][slot]).first, (*startInfo->selectedArmy[sel][slot]).second); obj->setCreature(SlotID(slot), startInfo->selectedArmy[sel][slot].getId(), startInfo->selectedArmy[sel][slot].getCount());
map->getEditManager()->insertObject(obj); map->getEditManager()->insertObject(obj);
}; };
@@ -484,8 +484,8 @@ void BattleOnlyModeWindow::startBattle()
if(!startInfo->selectedHero[1]) if(!startInfo->selectedHero[1])
{ {
for(int slot = 0; slot < GameConstants::ARMY_SIZE; slot++) for(int slot = 0; slot < GameConstants::ARMY_SIZE; slot++)
if(startInfo->selectedArmy[1][slot]) if(startInfo->selectedArmy[1][slot].getId() != CreatureID::NONE)
townObj->getArmy()->setCreature(SlotID(slot), (*startInfo->selectedArmy[1][slot]).first, (*startInfo->selectedArmy[1][slot]).second); townObj->getArmy()->setCreature(SlotID(slot), startInfo->selectedArmy[1][slot].getId(), startInfo->selectedArmy[1][slot].getCount());
} }
else else
addHero(1, PlayerColor(1), int3(5, 5, 0)); addHero(1, PlayerColor(1), int3(5, 5, 0));

View File

@@ -18,6 +18,7 @@
#include "serializer/GameConnectionID.h" #include "serializer/GameConnectionID.h"
#include "serializer/Serializeable.h" #include "serializer/Serializeable.h"
#include "serializer/PlayerConnectionID.h" #include "serializer/PlayerConnectionID.h"
#include "mapObjects/army/CStackBasicDescriptor.h"
#include "ResourceSet.h" #include "ResourceSet.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
@@ -246,7 +247,7 @@ public:
std::optional<FactionID> selectedTown; std::optional<FactionID> selectedTown;
std::array<std::optional<HeroTypeID>, 2> selectedHero; std::array<std::optional<HeroTypeID>, 2> selectedHero;
std::array<std::array<std::optional<std::pair<CreatureID, int>>, 7>, 2> selectedArmy; std::array<std::array<CStackBasicDescriptor, 7>, 2> selectedArmy;
std::array<std::array<int, GameConstants::PRIMARY_SKILLS>, 2> primSkillLevel; std::array<std::array<int, GameConstants::PRIMARY_SKILLS>, 2> primSkillLevel;

View File

@@ -124,7 +124,6 @@ inline std::string TextOperations::formatMetric(Arithmetic number, int maxDigits
template<typename Arithmetic> template<typename Arithmetic>
inline Arithmetic TextOperations::parseMetric(const std::string &text) inline Arithmetic TextOperations::parseMetric(const std::string &text)
{ {
static const std::string symbols = " kMGTPE";
if (text.empty()) if (text.empty())
return 0; return 0;
@@ -163,7 +162,7 @@ inline Arithmetic TextOperations::parseMetric(const std::string &text)
try try
{ {
Arithmetic value = boost::lexical_cast<Arithmetic>(numberPart); Arithmetic value = std::stoll(numberPart);
for (int i = 0; i < power; ++i) for (int i = 0; i < power; ++i)
{ {
@@ -178,7 +177,7 @@ inline Arithmetic TextOperations::parseMetric(const std::string &text)
return value; return value;
} }
catch (boost::bad_lexical_cast &) catch (std::invalid_argument &)
{ {
return 0; return 0;
} }