1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Merge pull request #1562 from rilian-la-te/modernize-netpacks

vcmi: modernize lib/NetPacks.h and lib/NetPacksBase.h
This commit is contained in:
Andrii Danylchenko 2023-02-12 23:10:21 +02:00 committed by GitHub
commit c41a0a37e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 505 additions and 637 deletions

View File

@ -274,7 +274,7 @@ DLL_LINKAGE std::string MetaString::buildList () const
return lista; return lista;
} }
void MetaString::addCreReplacement(CreatureID id, TQuantity count) //adds sing or plural name; void MetaString::addCreReplacement(const CreatureID & id, TQuantity count) //adds sing or plural name;
{ {
if (!count) if (!count)
addReplacement (CRE_PL_NAMES, id); //no creatures - just empty name (eg. defeat Angels) addReplacement (CRE_PL_NAMES, id); //no creatures - just empty name (eg. defeat Angels)

File diff suppressed because it is too large Load Diff

View File

@ -34,8 +34,8 @@ struct DLL_LINKAGE CPack
{ {
std::shared_ptr<CConnection> c; // Pointer to connection that pack received from std::shared_ptr<CConnection> c; // Pointer to connection that pack received from
CPack() : c(nullptr) {}; CPack() = default;
virtual ~CPack() {}; virtual ~CPack() = default;
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {
@ -49,8 +49,6 @@ struct DLL_LINKAGE CPack
struct CPackForClient : public CPack struct CPackForClient : public CPack
{ {
CPackForClient(){};
CGameState* GS(CClient *cl); CGameState* GS(CClient *cl);
void applyFirstCl(CClient *cl)//called before applying to gs void applyFirstCl(CClient *cl)//called before applying to gs
{} {}
@ -60,14 +58,9 @@ struct CPackForClient : public CPack
struct CPackForServer : public CPack struct CPackForServer : public CPack
{ {
mutable PlayerColor player; mutable PlayerColor player = PlayerColor::NEUTRAL;
mutable si32 requestID; mutable si32 requestID;
CGameState* GS(CGameHandler *gh); CGameState * GS(CGameHandler * gh);
CPackForServer():
player(PlayerColor::NEUTRAL)
{
}
bool applyGh(CGameHandler *gh) //called after applying to gs bool applyGh(CGameHandler *gh) //called after applying to gs
{ {
logGlobal->error("Should not happen... applying plain CPackForServer"); logGlobal->error("Should not happen... applying plain CPackForServer");
@ -115,7 +108,7 @@ public:
void addTxt(ui8 type, ui32 serial) void addTxt(ui8 type, ui32 serial)
{ {
message.push_back(TLOCAL_STRING); message.push_back(TLOCAL_STRING);
localStrings.push_back(std::pair<ui8,ui32>(type, serial)); localStrings.emplace_back(type, serial);
} }
MetaString& operator<<(const std::pair<ui8,ui32> &txt) MetaString& operator<<(const std::pair<ui8,ui32> &txt)
{ {
@ -138,7 +131,7 @@ public:
void addReplacement(ui8 type, ui32 serial) void addReplacement(ui8 type, ui32 serial)
{ {
message.push_back(TREPLACE_LSTRING); message.push_back(TREPLACE_LSTRING);
localStrings.push_back(std::pair<ui8,ui32>(type, serial)); localStrings.emplace_back(type, serial);
} }
void addReplacement(const std::string &txt) void addReplacement(const std::string &txt)
{ {
@ -155,7 +148,7 @@ public:
message.push_back(TREPLACE_PLUSNUMBER); message.push_back(TREPLACE_PLUSNUMBER);
numbers.push_back(txt); numbers.push_back(txt);
} }
void addCreReplacement(CreatureID id, TQuantity count); //adds sing or plural name; void addCreReplacement(const CreatureID & id, TQuantity count); //adds sing or plural name;
void addReplacement(const CStackBasicDescriptor &stack); //adds sing or plural name; void addReplacement(const CStackBasicDescriptor &stack); //adds sing or plural name;
std::string buildList () const; std::string buildList () const;
void clear() void clear()
@ -167,17 +160,16 @@ public:
} }
void toString(std::string &dst) const; void toString(std::string &dst) const;
std::string toString() const; std::string toString() const;
void getLocalString(const std::pair<ui8,ui32> &txt, std::string &dst) const; void getLocalString(const std::pair<ui8, ui32> & txt, std::string & dst) const;
MetaString(){}
}; };
struct Component struct Component
{ {
enum EComponentType {PRIM_SKILL, SEC_SKILL, RESOURCE, CREATURE, ARTIFACT, EXPERIENCE, SPELL, MORALE, LUCK, BUILDING, HERO_PORTRAIT, FLAG}; enum EComponentType {PRIM_SKILL, SEC_SKILL, RESOURCE, CREATURE, ARTIFACT, EXPERIENCE, SPELL, MORALE, LUCK, BUILDING, HERO_PORTRAIT, FLAG};
ui16 id, subtype; //id uses ^^^ enums, when id==EXPPERIENCE subtype==0 means exp points and subtype==1 levels) ui16 id = 0, subtype = 0; //id uses ^^^ enums, when id==EXPPERIENCE subtype==0 means exp points and subtype==1 levels)
si32 val; // + give; - take si32 val = 0; // + give; - take
si16 when; // 0 - now; +x - within x days; -x - per x days si16 when = 0; // 0 - now; +x - within x days; -x - per x days
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
{ {
@ -186,10 +178,7 @@ struct Component
h & val; h & val;
h & when; h & when;
} }
Component() Component() = default;
:id(0), subtype(0), val(0), when(0)
{
}
DLL_LINKAGE explicit Component(const CStackBasicDescriptor &stack); DLL_LINKAGE explicit Component(const CStackBasicDescriptor &stack);
Component(Component::EComponentType Type, ui16 Subtype, si32 Val, si16 When) Component(Component::EComponentType Type, ui16 Subtype, si32 Val, si16 When)
:id(Type),subtype(Subtype),val(Val),when(When) :id(Type),subtype(Subtype),val(Val),when(When)
@ -197,28 +186,27 @@ struct Component
} }
}; };
typedef boost::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance> > TArtHolder; using TArtHolder = boost::variant<ConstTransitivePtr<CGHeroInstance>, ConstTransitivePtr<CStackInstance>>;
struct ArtifactLocation struct ArtifactLocation
{ {
TArtHolder artHolder;//TODO: identify holder by id TArtHolder artHolder;//TODO: identify holder by id
ArtifactPosition slot; ArtifactPosition slot = ArtifactPosition::PRE_FIRST;
ArtifactLocation() ArtifactLocation()
: artHolder(ConstTransitivePtr<CGHeroInstance>())
{ {
artHolder = ConstTransitivePtr<CGHeroInstance>();
slot = ArtifactPosition::PRE_FIRST;
} }
template <typename T> template<typename T>
ArtifactLocation(const T *ArtHolder, ArtifactPosition Slot) ArtifactLocation(const T * ArtHolder, ArtifactPosition Slot)
: artHolder(const_cast<T *>(ArtHolder)) //we are allowed here to const cast -> change will go through one of our packages... do not abuse!
, slot(Slot)
{ {
artHolder = const_cast<T*>(ArtHolder); //we are allowed here to const cast -> change will go through one of our packages... do not abuse!
slot = Slot;
} }
ArtifactLocation(TArtHolder ArtHolder, ArtifactPosition Slot) ArtifactLocation(TArtHolder ArtHolder, const ArtifactPosition & Slot)
: artHolder(std::move(std::move(ArtHolder)))
, slot(Slot)
{ {
artHolder = ArtHolder;
slot = Slot;
} }
template <typename T> template <typename T>
@ -253,15 +241,9 @@ struct ArtifactLocation
class EntityChanges class EntityChanges
{ {
public: public:
Metatype metatype; Metatype metatype = Metatype::UNKNOWN;
int32_t entityIndex; int32_t entityIndex = 0;
JsonNode data; JsonNode data;
EntityChanges()
: metatype(Metatype::UNKNOWN),
entityIndex(0),
data()
{
}
template <typename Handler> void serialize(Handler & h, const int version) template <typename Handler> void serialize(Handler & h, const int version)
{ {
h & metatype; h & metatype;
@ -284,17 +266,11 @@ public:
}; };
JsonNode data; JsonNode data;
EOperation operation; EOperation operation = EOperation::RESET_STATE;
BattleChanges()
: operation(EOperation::RESET_STATE),
data()
{
}
BattleChanges() = default;
BattleChanges(EOperation operation_) BattleChanges(EOperation operation_)
: operation(operation_), : operation(operation_)
data()
{ {
} }
}; };
@ -302,20 +278,13 @@ public:
class UnitChanges : public BattleChanges class UnitChanges : public BattleChanges
{ {
public: public:
uint32_t id; uint32_t id = 0;
int64_t healthDelta; int64_t healthDelta = 0;
UnitChanges()
: BattleChanges(EOperation::RESET_STATE),
id(0),
healthDelta(0)
{
}
UnitChanges() = default;
UnitChanges(uint32_t id_, EOperation operation_) UnitChanges(uint32_t id_, EOperation operation_)
: BattleChanges(operation_), : BattleChanges(operation_)
id(id_), , id(id_)
healthDelta(0)
{ {
} }
@ -331,13 +300,9 @@ public:
class ObstacleChanges : public BattleChanges class ObstacleChanges : public BattleChanges
{ {
public: public:
uint32_t id; uint32_t id = 0;
ObstacleChanges() ObstacleChanges() = default;
: BattleChanges(EOperation::RESET_STATE),
id(0)
{
}
ObstacleChanges(uint32_t id_, EOperation operation_) ObstacleChanges(uint32_t id_, EOperation operation_)
: BattleChanges(operation_), : BattleChanges(operation_),

View File

@ -31,8 +31,7 @@
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
DLL_LINKAGE void SetResources::applyGs(CGameState * gs) const
DLL_LINKAGE void SetResources::applyGs(CGameState *gs)
{ {
assert(player < PlayerColor::PLAYER_LIMIT); assert(player < PlayerColor::PLAYER_LIMIT);
if(abs) if(abs)
@ -46,14 +45,14 @@ DLL_LINKAGE void SetResources::applyGs(CGameState *gs)
gs->getPlayerState(player)->resources.positive(); gs->getPlayerState(player)->resources.positive();
} }
DLL_LINKAGE void SetPrimSkill::applyGs(CGameState *gs) DLL_LINKAGE void SetPrimSkill::applyGs(CGameState * gs) const
{ {
CGHeroInstance * hero = gs->getHero(id); CGHeroInstance * hero = gs->getHero(id);
assert(hero); assert(hero);
hero->setPrimarySkill(which, val, abs); hero->setPrimarySkill(which, val, abs);
} }
DLL_LINKAGE void SetSecSkill::applyGs(CGameState *gs) DLL_LINKAGE void SetSecSkill::applyGs(CGameState * gs) const
{ {
CGHeroInstance *hero = gs->getHero(id); CGHeroInstance *hero = gs->getHero(id);
hero->setSecSkillLevel(which, val, abs); hero->setSecSkillLevel(which, val, abs);
@ -88,17 +87,17 @@ DLL_LINKAGE void SetCommanderProperty::applyGs(CGameState *gs)
} }
} }
DLL_LINKAGE void AddQuest::applyGs(CGameState *gs) DLL_LINKAGE void AddQuest::applyGs(CGameState * gs) const
{ {
assert (vstd::contains(gs->players, player)); assert (vstd::contains(gs->players, player));
auto vec = &gs->players[player].quests; auto * vec = &gs->players[player].quests;
if (!vstd::contains(*vec, quest)) if (!vstd::contains(*vec, quest))
vec->push_back (quest); vec->push_back (quest);
else else
logNetwork->warn("Warning! Attempt to add duplicated quest"); logNetwork->warn("Warning! Attempt to add duplicated quest");
} }
DLL_LINKAGE void UpdateArtHandlerLists::applyGs(CGameState *gs) DLL_LINKAGE void UpdateArtHandlerLists::applyGs(CGameState * gs) const
{ {
VLC->arth->minors = minors; VLC->arth->minors = minors;
VLC->arth->majors = majors; VLC->arth->majors = majors;
@ -106,24 +105,23 @@ DLL_LINKAGE void UpdateArtHandlerLists::applyGs(CGameState *gs)
VLC->arth->relics = relics; VLC->arth->relics = relics;
} }
DLL_LINKAGE void UpdateMapEvents::applyGs(CGameState *gs) DLL_LINKAGE void UpdateMapEvents::applyGs(CGameState * gs) const
{ {
gs->map->events = events; gs->map->events = events;
} }
DLL_LINKAGE void UpdateCastleEvents::applyGs(CGameState * gs) const
DLL_LINKAGE void UpdateCastleEvents::applyGs(CGameState *gs)
{ {
auto t = gs->getTown(town); auto * t = gs->getTown(town);
t->events = events; t->events = events;
} }
DLL_LINKAGE void ChangeFormation::applyGs(CGameState *gs) DLL_LINKAGE void ChangeFormation::applyGs(CGameState * gs) const
{ {
gs->getHero(hid)->setFormation(formation); gs->getHero(hid)->setFormation(formation);
} }
DLL_LINKAGE void HeroVisitCastle::applyGs(CGameState *gs) DLL_LINKAGE void HeroVisitCastle::applyGs(CGameState * gs) const
{ {
CGHeroInstance *h = gs->getHero(hid); CGHeroInstance *h = gs->getHero(hid);
CGTownInstance *t = gs->getTown(tid); CGTownInstance *t = gs->getTown(tid);
@ -142,14 +140,14 @@ DLL_LINKAGE void ChangeSpells::applyGs(CGameState *gs)
CGHeroInstance *hero = gs->getHero(hid); CGHeroInstance *hero = gs->getHero(hid);
if(learn) if(learn)
for(auto sid : spells) for(const auto & sid : spells)
hero->addSpellToSpellbook(sid); hero->addSpellToSpellbook(sid);
else else
for(auto sid : spells) for(const auto & sid : spells)
hero->removeSpellFromSpellbook(sid); hero->removeSpellFromSpellbook(sid);
} }
DLL_LINKAGE void SetMana::applyGs(CGameState *gs) DLL_LINKAGE void SetMana::applyGs(CGameState * gs) const
{ {
CGHeroInstance * hero = gs->getHero(hid); CGHeroInstance * hero = gs->getHero(hid);
@ -163,7 +161,7 @@ DLL_LINKAGE void SetMana::applyGs(CGameState *gs)
vstd::amax(hero->mana, 0); //not less than 0 vstd::amax(hero->mana, 0); //not less than 0
} }
DLL_LINKAGE void SetMovePoints::applyGs(CGameState *gs) DLL_LINKAGE void SetMovePoints::applyGs(CGameState * gs) const
{ {
CGHeroInstance *hero = gs->getHero(hid); CGHeroInstance *hero = gs->getHero(hid);
@ -181,7 +179,7 @@ DLL_LINKAGE void FoWChange::applyGs(CGameState *gs)
{ {
TeamState * team = gs->getPlayerTeam(player); TeamState * team = gs->getPlayerTeam(player);
auto fogOfWarMap = team->fogOfWarMap; auto fogOfWarMap = team->fogOfWarMap;
for(int3 t : tiles) for(const int3 & t : tiles)
(*fogOfWarMap)[t.z][t.x][t.y] = mode; (*fogOfWarMap)[t.z][t.x][t.y] = mode;
if (mode == 0) //do not hide too much if (mode == 0) //do not hide too much
{ {
@ -203,7 +201,7 @@ DLL_LINKAGE void FoWChange::applyGs(CGameState *gs)
} }
} }
} }
for(int3 t : tilesRevealed) //probably not the most optimal solution ever for(const int3 & t : tilesRevealed) //probably not the most optimal solution ever
(*fogOfWarMap)[t.z][t.x][t.y] = 1; (*fogOfWarMap)[t.z][t.x][t.y] = 1;
} }
} }
@ -218,7 +216,7 @@ DLL_LINKAGE void SetAvailableHeroes::applyGs(CGameState *gs)
CGHeroInstance *h = (hid[i]>=0 ? gs->hpool.heroesPool[hid[i]].get() : nullptr); CGHeroInstance *h = (hid[i]>=0 ? gs->hpool.heroesPool[hid[i]].get() : nullptr);
if(h && army[i]) if(h && army[i])
h->setToArmy(army[i]); h->setToArmy(army[i]);
p->availableHeroes.push_back(h); p->availableHeroes.emplace_back(h);
} }
} }
@ -248,8 +246,7 @@ DLL_LINKAGE void GiveBonus::applyGs(CGameState *gs)
std::string &descr = b->description; std::string &descr = b->description;
if(!bdescr.message.size() if(bdescr.message.empty() && (bonus.type == Bonus::LUCK || bonus.type == Bonus::MORALE))
&& (bonus.type == Bonus::LUCK || bonus.type == Bonus::MORALE))
{ {
if (bonus.source == Bonus::OBJECT) if (bonus.source == Bonus::OBJECT)
{ {
@ -270,8 +267,8 @@ DLL_LINKAGE void GiveBonus::applyGs(CGameState *gs)
bdescr.toString(descr); bdescr.toString(descr);
} }
// Some of(?) versions of H3 use %s here instead of %d. Try to replace both of them // Some of(?) versions of H3 use %s here instead of %d. Try to replace both of them
boost::replace_first(descr,"%d",boost::lexical_cast<std::string>(std::abs(bonus.val))); boost::replace_first(descr, "%d", std::to_string(std::abs(bonus.val)));
boost::replace_first(descr,"%s",boost::lexical_cast<std::string>(std::abs(bonus.val))); boost::replace_first(descr, "%s", std::to_string(std::abs(bonus.val)));
} }
DLL_LINKAGE void ChangeObjPos::applyGs(CGameState *gs) DLL_LINKAGE void ChangeObjPos::applyGs(CGameState *gs)
@ -287,7 +284,7 @@ DLL_LINKAGE void ChangeObjPos::applyGs(CGameState *gs)
gs->map->addBlockVisTiles(obj); gs->map->addBlockVisTiles(obj);
} }
DLL_LINKAGE void ChangeObjectVisitors::applyGs(CGameState *gs) DLL_LINKAGE void ChangeObjectVisitors::applyGs(CGameState * gs) const
{ {
switch (mode) { switch (mode) {
case VISITOR_ADD: case VISITOR_ADD:
@ -297,7 +294,7 @@ DLL_LINKAGE void ChangeObjectVisitors::applyGs(CGameState *gs)
case VISITOR_ADD_TEAM: case VISITOR_ADD_TEAM:
{ {
TeamState *ts = gs->getPlayerTeam(gs->getHero(hero)->tempOwner); TeamState *ts = gs->getPlayerTeam(gs->getHero(hero)->tempOwner);
for (auto & color : ts->players) for(const auto & color : ts->players)
{ {
gs->getPlayerState(color)->visitedObjects.insert(object); gs->getPlayerState(color)->visitedObjects.insert(object);
} }
@ -324,7 +321,7 @@ DLL_LINKAGE void ChangeObjectVisitors::applyGs(CGameState *gs)
} }
} }
DLL_LINKAGE void PlayerEndsGame::applyGs(CGameState *gs) DLL_LINKAGE void PlayerEndsGame::applyGs(CGameState * gs) const
{ {
PlayerState *p = gs->getPlayerState(player); PlayerState *p = gs->getPlayerState(player);
if(victoryLossCheckResult.victory()) if(victoryLossCheckResult.victory())
@ -374,14 +371,14 @@ DLL_LINKAGE void PlayerReinitInterface::applyGs(CGameState *gs)
//TODO: what does mean if more that one player connected? //TODO: what does mean if more that one player connected?
if(playerConnectionId == PlayerSettings::PLAYER_AI) if(playerConnectionId == PlayerSettings::PLAYER_AI)
{ {
for(auto player : players) for(const auto & player : players)
gs->scenarioOps->getIthPlayersSettings(player).connectedPlayerIDs.clear(); gs->scenarioOps->getIthPlayersSettings(player).connectedPlayerIDs.clear();
} }
} }
DLL_LINKAGE void RemoveBonus::applyGs(CGameState *gs) DLL_LINKAGE void RemoveBonus::applyGs(CGameState *gs)
{ {
CBonusSystemNode *node; CBonusSystemNode * node = nullptr;
if (who == HERO) if (who == HERO)
node = gs->getHero(ObjectInstanceID(whoID)); node = gs->getHero(ObjectInstanceID(whoID));
else else
@ -389,9 +386,8 @@ DLL_LINKAGE void RemoveBonus::applyGs(CGameState *gs)
BonusList &bonuses = node->getExportedBonusList(); BonusList &bonuses = node->getExportedBonusList();
for (int i = 0; i < bonuses.size(); i++) for(const auto & b : bonuses)
{ {
auto b = bonuses[i];
if(b->source == source && b->sid == id) if(b->source == source && b->sid == id)
{ {
bonus = *b; //backup bonus (to show to interfaces later) bonus = *b; //backup bonus (to show to interfaces later)
@ -411,7 +407,8 @@ DLL_LINKAGE void RemoveObject::applyGs(CGameState *gs)
if(obj->ID == Obj::HERO) //remove beaten hero if(obj->ID == Obj::HERO) //remove beaten hero
{ {
CGHeroInstance * beatenHero = static_cast<CGHeroInstance*>(obj); auto * beatenHero = dynamic_cast<CGHeroInstance *>(obj);
assert(beatenHero);
PlayerState * p = gs->getPlayerState(beatenHero->tempOwner); PlayerState * p = gs->getPlayerState(beatenHero->tempOwner);
gs->map->heroesOnMap -= beatenHero; gs->map->heroesOnMap -= beatenHero;
p->heroes -= beatenHero; p->heroes -= beatenHero;
@ -450,7 +447,7 @@ DLL_LINKAGE void RemoveObject::applyGs(CGameState *gs)
return; return;
} }
auto quest = dynamic_cast<const IQuestObject *>(obj); const auto * quest = dynamic_cast<const IQuestObject *>(obj);
if (quest) if (quest)
{ {
gs->map->quests[quest->quest->qid] = nullptr; gs->map->quests[quest->quest->qid] = nullptr;
@ -492,7 +489,7 @@ DLL_LINKAGE void RemoveObject::applyGs(CGameState *gs)
gs->map->calculateGuardingGreaturePositions(); gs->map->calculateGuardingGreaturePositions();
} }
static int getDir(int3 src, int3 dst) static int getDir(const int3 & src, const int3 & dst)
{ {
int ret = -1; int ret = -1;
if(dst.x+1 == src.x && dst.y+1 == src.y) //tl if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
@ -553,7 +550,8 @@ void TryMoveHero::applyGs(CGameState *gs)
{ {
const TerrainTile &tt = gs->map->getTile(h->convertToVisitablePos(end)); const TerrainTile &tt = gs->map->getTile(h->convertToVisitablePos(end));
assert(tt.visitableObjects.size() >= 1 && tt.visitableObjects.back()->ID == Obj::BOAT); //the only visitable object at destination is Boat assert(tt.visitableObjects.size() >= 1 && tt.visitableObjects.back()->ID == Obj::BOAT); //the only visitable object at destination is Boat
CGBoat *boat = static_cast<CGBoat*>(tt.visitableObjects.back()); auto * boat = dynamic_cast<CGBoat *>(tt.visitableObjects.back());
assert(boat);
gs->map->removeBlockVisTiles(boat); //hero blockvis mask will be used, we don't need to duplicate it with boat gs->map->removeBlockVisTiles(boat); //hero blockvis mask will be used, we don't need to duplicate it with boat
h->boat = boat; h->boat = boat;
@ -561,7 +559,7 @@ void TryMoveHero::applyGs(CGameState *gs)
} }
else if(result == DISEMBARK) //hero leaves boat to destination tile else if(result == DISEMBARK) //hero leaves boat to destination tile
{ {
CGBoat *b = const_cast<CGBoat *>(h->boat); auto * b = const_cast<CGBoat *>(h->boat);
b->direction = h->moveDir; b->direction = h->moveDir;
b->pos = start; b->pos = start;
b->hero = nullptr; b->hero = nullptr;
@ -573,13 +571,13 @@ void TryMoveHero::applyGs(CGameState *gs)
{ {
gs->map->removeBlockVisTiles(h); gs->map->removeBlockVisTiles(h);
h->pos = end; h->pos = end;
if(CGBoat *b = const_cast<CGBoat *>(h->boat)) if(auto * b = const_cast<CGBoat *>(h->boat))
b->pos = end; b->pos = end;
gs->map->addBlockVisTiles(h); gs->map->addBlockVisTiles(h);
} }
auto fogOfWarMap = gs->getPlayerTeam(h->getOwner())->fogOfWarMap; auto fogOfWarMap = gs->getPlayerTeam(h->getOwner())->fogOfWarMap;
for(int3 t : fowRevealed) for(const int3 & t : fowRevealed)
(*fogOfWarMap)[t.z][t.x][t.y] = 1; (*fogOfWarMap)[t.z][t.x][t.y] = 1;
} }
@ -597,7 +595,7 @@ DLL_LINKAGE void NewStructures::applyGs(CGameState *gs)
if(currentBuilding->overrideBids.empty()) if(currentBuilding->overrideBids.empty())
continue; continue;
for(auto overrideBid : currentBuilding->overrideBids) for(const auto & overrideBid : currentBuilding->overrideBids)
{ {
t->overriddenBuildings.insert(overrideBid); t->overriddenBuildings.insert(overrideBid);
t->deleteTownBonus(overrideBid); t->deleteTownBonus(overrideBid);
@ -620,19 +618,19 @@ DLL_LINKAGE void RazeStructures::applyGs(CGameState *gs)
t->recreateBuildingsBonuses(); t->recreateBuildingsBonuses();
} }
DLL_LINKAGE void SetAvailableCreatures::applyGs(CGameState *gs) DLL_LINKAGE void SetAvailableCreatures::applyGs(CGameState * gs) const
{ {
CGDwelling *dw = dynamic_cast<CGDwelling*>(gs->getObjInstance(tid)); auto * dw = dynamic_cast<CGDwelling *>(gs->getObjInstance(tid));
assert(dw); assert(dw);
dw->creatures = creatures; dw->creatures = creatures;
} }
DLL_LINKAGE void SetHeroesInTown::applyGs(CGameState *gs) DLL_LINKAGE void SetHeroesInTown::applyGs(CGameState * gs) const
{ {
CGTownInstance *t = gs->getTown(tid); CGTownInstance *t = gs->getTown(tid);
CGHeroInstance *v = gs->getHero(visiting), CGHeroInstance * v = gs->getHero(visiting);
*g = gs->getHero(garrison); CGHeroInstance * g = gs->getHero(garrison);
bool newVisitorComesFromGarrison = v && v == t->garrisonHero; bool newVisitorComesFromGarrison = v && v == t->garrisonHero;
bool newGarrisonComesFromVisiting = g && g == t->visitingHero; bool newGarrisonComesFromVisiting = g && g == t->visitingHero;
@ -656,7 +654,7 @@ DLL_LINKAGE void SetHeroesInTown::applyGs(CGameState *gs)
} }
} }
DLL_LINKAGE void HeroRecruited::applyGs(CGameState *gs) DLL_LINKAGE void HeroRecruited::applyGs(CGameState * gs) const
{ {
assert(vstd::contains(gs->hpool.heroesPool, hid)); assert(vstd::contains(gs->hpool.heroesPool, hid));
CGHeroInstance *h = gs->hpool.heroesPool[hid]; CGHeroInstance *h = gs->hpool.heroesPool[hid];
@ -676,14 +674,14 @@ DLL_LINKAGE void HeroRecruited::applyGs(CGameState *gs)
gs->hpool.heroesPool.erase(hid); gs->hpool.heroesPool.erase(hid);
if(h->id == ObjectInstanceID()) if(h->id == ObjectInstanceID())
{ {
h->id = ObjectInstanceID((si32)gs->map->objects.size()); h->id = ObjectInstanceID(static_cast<si32>(gs->map->objects.size()));
gs->map->objects.push_back(h); gs->map->objects.emplace_back(h);
} }
else else
gs->map->objects[h->id.getNum()] = h; gs->map->objects[h->id.getNum()] = h;
gs->map->heroesOnMap.push_back(h); gs->map->heroesOnMap.emplace_back(h);
p->heroes.push_back(h); p->heroes.emplace_back(h);
h->attachTo(*p); h->attachTo(*p);
if(fresh) if(fresh)
{ {
@ -697,7 +695,7 @@ DLL_LINKAGE void HeroRecruited::applyGs(CGameState *gs)
} }
} }
DLL_LINKAGE void GiveHero::applyGs(CGameState *gs) DLL_LINKAGE void GiveHero::applyGs(CGameState * gs) const
{ {
CGHeroInstance *h = gs->getHero(id); CGHeroInstance *h = gs->getHero(id);
@ -712,8 +710,8 @@ DLL_LINKAGE void GiveHero::applyGs(CGameState *gs)
h->setOwner(player); h->setOwner(player);
h->movement = h->maxMovePoints(true); h->movement = h->maxMovePoints(true);
h->pos = h->convertFromVisitablePos(oldVisitablePos); h->pos = h->convertFromVisitablePos(oldVisitablePos);
gs->map->heroesOnMap.push_back(h); gs->map->heroesOnMap.emplace_back(h);
gs->getPlayerState(h->getOwner())->heroes.push_back(h); gs->getPlayerState(h->getOwner())->heroes.emplace_back(h);
gs->map->addBlockVisTiles(h); gs->map->addBlockVisTiles(h);
h->inTownGarrison = false; h->inTownGarrison = false;
@ -750,9 +748,10 @@ DLL_LINKAGE void NewObject::applyGs(CGameState *gs)
o = new CGCreature(); o = new CGCreature();
{ {
//CStackInstance hlp; //CStackInstance hlp;
CGCreature *cre = static_cast<CGCreature*>(o); auto * cre = dynamic_cast<CGCreature *>(o);
//cre->slots[0] = hlp; //cre->slots[0] = hlp;
cre->notGrowingTeam = cre->neverFlees = 0; assert(cre);
cre->notGrowingTeam = cre->neverFlees = false;
cre->character = 2; cre->character = 2;
cre->gainedArtifact = ArtifactID::NONE; cre->gainedArtifact = ArtifactID::NONE;
cre->identifier = -1; cre->identifier = -1;
@ -767,9 +766,9 @@ DLL_LINKAGE void NewObject::applyGs(CGameState *gs)
o->subID = subID; o->subID = subID;
o->pos = pos; o->pos = pos;
o->appearance = VLC->objtypeh->getHandlerFor(o->ID, o->subID)->getTemplates(terrainType).front(); o->appearance = VLC->objtypeh->getHandlerFor(o->ID, o->subID)->getTemplates(terrainType).front();
id = o->id = ObjectInstanceID((si32)gs->map->objects.size()); id = o->id = ObjectInstanceID(static_cast<si32>(gs->map->objects.size()));
gs->map->objects.push_back(o); gs->map->objects.emplace_back(o);
gs->map->addBlockVisTiles(o); gs->map->addBlockVisTiles(o);
o->initObj(gs->getRandomGenerator()); o->initObj(gs->getRandomGenerator());
gs->map->calculateGuardingGreaturePositions(); gs->map->calculateGuardingGreaturePositions();
@ -784,7 +783,7 @@ DLL_LINKAGE void NewArtifact::applyGs(CGameState *gs)
assert(!art->getParentNodes().size()); assert(!art->getParentNodes().size());
art->setType(art->artType); art->setType(art->artType);
if (CCombinedArtifactInstance* cart = dynamic_cast<CCombinedArtifactInstance*>(art.get())) if(auto * cart = dynamic_cast<CCombinedArtifactInstance *>(art.get()))
cart->createConstituents(); cart->createConstituents();
} }
@ -834,7 +833,7 @@ DLL_LINKAGE const CArmedInstance * ArtifactLocation::relatedObj() const
DLL_LINKAGE PlayerColor ArtifactLocation::owningPlayer() const DLL_LINKAGE PlayerColor ArtifactLocation::owningPlayer() const
{ {
auto obj = relatedObj(); const auto * obj = relatedObj();
return obj ? obj->tempOwner : PlayerColor::NEUTRAL; return obj ? obj->tempOwner : PlayerColor::NEUTRAL;
} }
@ -850,7 +849,7 @@ DLL_LINKAGE CBonusSystemNode *ArtifactLocation::getHolderNode()
DLL_LINKAGE const CArtifactInstance *ArtifactLocation::getArt() const DLL_LINKAGE const CArtifactInstance *ArtifactLocation::getArt() const
{ {
auto s = getSlot(); const auto * s = getSlot();
if(s) if(s)
return s->getArt(); return s->getArt();
else else
@ -859,13 +858,13 @@ DLL_LINKAGE const CArtifactInstance *ArtifactLocation::getArt() const
DLL_LINKAGE const CArtifactSet * ArtifactLocation::getHolderArtSet() const DLL_LINKAGE const CArtifactSet * ArtifactLocation::getHolderArtSet() const
{ {
ArtifactLocation *t = const_cast<ArtifactLocation*>(this); auto * t = const_cast<ArtifactLocation *>(this);
return t->getHolderArtSet(); return t->getHolderArtSet();
} }
DLL_LINKAGE const CBonusSystemNode * ArtifactLocation::getHolderNode() const DLL_LINKAGE const CBonusSystemNode * ArtifactLocation::getHolderNode() const
{ {
ArtifactLocation *t = const_cast<ArtifactLocation*>(this); auto * t = const_cast<ArtifactLocation *>(this);
return t->getHolderNode(); return t->getHolderNode();
} }
@ -882,7 +881,7 @@ DLL_LINKAGE const ArtSlotInfo *ArtifactLocation::getSlot() const
DLL_LINKAGE void ChangeStackCount::applyGs(CGameState * gs) DLL_LINKAGE void ChangeStackCount::applyGs(CGameState * gs)
{ {
auto srcObj = gs->getArmyInstance(army); auto * srcObj = gs->getArmyInstance(army);
if(!srcObj) if(!srcObj)
logNetwork->error("[CRITICAL] ChangeStackCount: invalid army object %d, possible game state corruption.", army.getNum()); logNetwork->error("[CRITICAL] ChangeStackCount: invalid army object %d, possible game state corruption.", army.getNum());
@ -894,7 +893,7 @@ DLL_LINKAGE void ChangeStackCount::applyGs(CGameState * gs)
DLL_LINKAGE void SetStackType::applyGs(CGameState * gs) DLL_LINKAGE void SetStackType::applyGs(CGameState * gs)
{ {
auto srcObj = gs->getArmyInstance(army); auto * srcObj = gs->getArmyInstance(army);
if(!srcObj) if(!srcObj)
logNetwork->error("[CRITICAL] SetStackType: invalid army object %d, possible game state corruption.", army.getNum()); logNetwork->error("[CRITICAL] SetStackType: invalid army object %d, possible game state corruption.", army.getNum());
@ -903,7 +902,7 @@ DLL_LINKAGE void SetStackType::applyGs(CGameState * gs)
DLL_LINKAGE void EraseStack::applyGs(CGameState * gs) DLL_LINKAGE void EraseStack::applyGs(CGameState * gs)
{ {
auto srcObj = gs->getArmyInstance(army); auto * srcObj = gs->getArmyInstance(army);
if(!srcObj) if(!srcObj)
logNetwork->error("[CRITICAL] EraseStack: invalid army object %d, possible game state corruption.", army.getNum()); logNetwork->error("[CRITICAL] EraseStack: invalid army object %d, possible game state corruption.", army.getNum());
@ -912,11 +911,11 @@ DLL_LINKAGE void EraseStack::applyGs(CGameState * gs)
DLL_LINKAGE void SwapStacks::applyGs(CGameState * gs) DLL_LINKAGE void SwapStacks::applyGs(CGameState * gs)
{ {
auto srcObj = gs->getArmyInstance(srcArmy); auto * srcObj = gs->getArmyInstance(srcArmy);
if(!srcObj) if(!srcObj)
logNetwork->error("[CRITICAL] SwapStacks: invalid army object %d, possible game state corruption.", srcArmy.getNum()); logNetwork->error("[CRITICAL] SwapStacks: invalid army object %d, possible game state corruption.", srcArmy.getNum());
auto dstObj = gs->getArmyInstance(dstArmy); auto * dstObj = gs->getArmyInstance(dstArmy);
if(!dstObj) if(!dstObj)
logNetwork->error("[CRITICAL] SwapStacks: invalid army object %d, possible game state corruption.", dstArmy.getNum()); logNetwork->error("[CRITICAL] SwapStacks: invalid army object %d, possible game state corruption.", dstArmy.getNum());
@ -929,7 +928,7 @@ DLL_LINKAGE void SwapStacks::applyGs(CGameState * gs)
DLL_LINKAGE void InsertNewStack::applyGs(CGameState *gs) DLL_LINKAGE void InsertNewStack::applyGs(CGameState *gs)
{ {
if(auto obj = gs->getArmyInstance(army)) if(auto * obj = gs->getArmyInstance(army))
obj->putStack(slot, new CStackInstance(type, count)); obj->putStack(slot, new CStackInstance(type, count));
else else
logNetwork->error("[CRITICAL] InsertNewStack: invalid army object %d, possible game state corruption.", army.getNum()); logNetwork->error("[CRITICAL] InsertNewStack: invalid army object %d, possible game state corruption.", army.getNum());
@ -937,11 +936,11 @@ DLL_LINKAGE void InsertNewStack::applyGs(CGameState *gs)
DLL_LINKAGE void RebalanceStacks::applyGs(CGameState * gs) DLL_LINKAGE void RebalanceStacks::applyGs(CGameState * gs)
{ {
auto srcObj = gs->getArmyInstance(srcArmy); auto * srcObj = gs->getArmyInstance(srcArmy);
if(!srcObj) if(!srcObj)
logNetwork->error("[CRITICAL] RebalanceStacks: invalid army object %d, possible game state corruption.", srcArmy.getNum()); logNetwork->error("[CRITICAL] RebalanceStacks: invalid army object %d, possible game state corruption.", srcArmy.getNum());
auto dstObj = gs->getArmyInstance(dstArmy); auto * dstObj = gs->getArmyInstance(dstArmy);
if(!dstObj) if(!dstObj)
logNetwork->error("[CRITICAL] RebalanceStacks: invalid army object %d, possible game state corruption.", dstArmy.getNum()); logNetwork->error("[CRITICAL] RebalanceStacks: invalid army object %d, possible game state corruption.", dstArmy.getNum());
@ -960,13 +959,13 @@ DLL_LINKAGE void RebalanceStacks::applyGs(CGameState * gs)
MAYBE_UNUSED(c); MAYBE_UNUSED(c);
auto alHere = ArtifactLocation (src.getStack(), ArtifactPosition::CREATURE_SLOT); auto alHere = ArtifactLocation (src.getStack(), ArtifactPosition::CREATURE_SLOT);
auto alDest = ArtifactLocation (dst.getStack(), ArtifactPosition::CREATURE_SLOT); auto alDest = ArtifactLocation (dst.getStack(), ArtifactPosition::CREATURE_SLOT);
auto artHere = alHere.getArt(); auto * artHere = alHere.getArt();
auto artDest = alDest.getArt(); auto * artDest = alDest.getArt();
if (artHere) if (artHere)
{ {
if (alDest.getArt()) if (alDest.getArt())
{ {
auto hero = dynamic_cast <CGHeroInstance *>(src.army.get()); auto * hero = dynamic_cast<CGHeroInstance *>(src.army.get());
if (hero) if (hero)
{ {
artDest->move (alDest, ArtifactLocation (hero, alDest.getArt()->firstBackpackSlot (hero))); artDest->move (alDest, ArtifactLocation (hero, alDest.getArt()->firstBackpackSlot (hero)));
@ -1058,13 +1057,13 @@ DLL_LINKAGE void PutArtifact::applyGs(CGameState *gs)
DLL_LINKAGE void EraseArtifact::applyGs(CGameState *gs) DLL_LINKAGE void EraseArtifact::applyGs(CGameState *gs)
{ {
auto slot = al.getSlot(); const auto * slot = al.getSlot();
if(slot->locked) if(slot->locked)
{ {
logGlobal->debug("Erasing locked artifact: %s", slot->artifact->artType->getNameTranslated()); logGlobal->debug("Erasing locked artifact: %s", slot->artifact->artType->getNameTranslated());
DisassembledArtifact dis; DisassembledArtifact dis;
dis.al.artHolder = al.artHolder; dis.al.artHolder = al.artHolder;
auto aset = al.getHolderArtSet(); auto * aset = al.getHolderArtSet();
#ifndef NDEBUG #ifndef NDEBUG
bool found = false; bool found = false;
#endif #endif
@ -1123,9 +1122,9 @@ DLL_LINKAGE void BulkMoveArtifacts::applyGs(CGameState * gs)
{ {
srcPos = ArtifactPosition(srcPos.num - numBackpackArtifactsMoved); srcPos = ArtifactPosition(srcPos.num - numBackpackArtifactsMoved);
} }
auto slotInfo = artSet->getSlot(srcPos); const auto * slotInfo = artSet->getSlot(srcPos);
assert(slotInfo); assert(slotInfo);
auto art = const_cast<CArtifactInstance*>(slotInfo->getArt()); auto * art = const_cast<CArtifactInstance *>(slotInfo->getArt());
assert(art); assert(art);
switch(operation) switch(operation)
{ {
@ -1153,8 +1152,8 @@ DLL_LINKAGE void BulkMoveArtifacts::applyGs(CGameState * gs)
if(swap) if(swap)
{ {
// Swap // Swap
auto leftSet = getSrcHolderArtSet(); auto * leftSet = getSrcHolderArtSet();
auto rightSet = getDstHolderArtSet(); auto * rightSet = getDstHolderArtSet();
CArtifactFittingSet artFittingSet(leftSet->bearerType()); CArtifactFittingSet artFittingSet(leftSet->bearerType());
artFittingSet.artifactsWorn = rightSet->artifactsWorn; artFittingSet.artifactsWorn = rightSet->artifactsWorn;
@ -1182,7 +1181,7 @@ DLL_LINKAGE void AssembledArtifact::applyGs(CGameState *gs)
})); }));
MAYBE_UNUSED(transformedArt); MAYBE_UNUSED(transformedArt);
auto combinedArt = new CCombinedArtifactInstance(builtArt); auto * combinedArt = new CCombinedArtifactInstance(builtArt);
gs->map->addNewArtifactInstance(combinedArt); gs->map->addNewArtifactInstance(combinedArt);
// Retrieve all constituents // Retrieve all constituents
for(const CArtifact * constituent : *builtArt->constituents) for(const CArtifact * constituent : *builtArt->constituents)
@ -1213,7 +1212,7 @@ DLL_LINKAGE void AssembledArtifact::applyGs(CGameState *gs)
DLL_LINKAGE void DisassembledArtifact::applyGs(CGameState *gs) DLL_LINKAGE void DisassembledArtifact::applyGs(CGameState *gs)
{ {
CCombinedArtifactInstance *disassembled = dynamic_cast<CCombinedArtifactInstance*>(al.getArt()); auto * disassembled = dynamic_cast<CCombinedArtifactInstance *>(al.getArt());
assert(disassembled); assert(disassembled);
std::vector<CCombinedArtifactInstance::ConstituentInfo> constituents = disassembled->constituentsInfo; std::vector<CCombinedArtifactInstance::ConstituentInfo> constituents = disassembled->constituentsInfo;
@ -1233,11 +1232,11 @@ DLL_LINKAGE void HeroVisit::applyGs(CGameState *gs)
{ {
} }
DLL_LINKAGE void SetAvailableArtifacts::applyGs(CGameState *gs) DLL_LINKAGE void SetAvailableArtifacts::applyGs(CGameState * gs) const
{ {
if(id >= 0) if(id >= 0)
{ {
if(CGBlackMarket *bm = dynamic_cast<CGBlackMarket*>(gs->map->objects[id].get())) if(auto * bm = dynamic_cast<CGBlackMarket *>(gs->map->objects[id].get()))
{ {
bm->artifacts = arts; bm->artifacts = arts;
} }
@ -1262,7 +1261,7 @@ DLL_LINKAGE void NewTurn::applyGs(CGameState *gs)
gs->globalEffects.reduceBonusDurations(Bonus::OneWeek); gs->globalEffects.reduceBonusDurations(Bonus::OneWeek);
//TODO not really a single root hierarchy, what about bonuses placed elsewhere? [not an issue with H3 mechanics but in the future...] //TODO not really a single root hierarchy, what about bonuses placed elsewhere? [not an issue with H3 mechanics but in the future...]
for(NewTurn::Hero h : heroes) //give mana/movement point for(const NewTurn::Hero & h : heroes) //give mana/movement point
{ {
CGHeroInstance *hero = gs->getHero(h.id); CGHeroInstance *hero = gs->getHero(h.id);
if(!hero) if(!hero)
@ -1286,13 +1285,13 @@ DLL_LINKAGE void NewTurn::applyGs(CGameState *gs)
hero->mana = h.mana; hero->mana = h.mana;
} }
for(auto i = res.cbegin(); i != res.cend(); i++) for(const auto & re : res)
{ {
assert(i->first < PlayerColor::PLAYER_LIMIT); assert(i->first < PlayerColor::PLAYER_LIMIT);
gs->getPlayerState(i->first)->resources = i->second; gs->getPlayerState(re.first)->resources = re.second;
} }
for(auto creatureSet : cres) //set available creatures in towns for(const auto & creatureSet : cres) //set available creatures in towns
creatureSet.second.applyGs(gs); creatureSet.second.applyGs(gs);
for(CGTownInstance* t : gs->map->towns) for(CGTownInstance* t : gs->map->towns)
@ -1322,7 +1321,7 @@ DLL_LINKAGE void NewTurn::applyGs(CGameState *gs)
} }
} }
DLL_LINKAGE void SetObjectProperty::applyGs(CGameState *gs) DLL_LINKAGE void SetObjectProperty::applyGs(CGameState * gs) const
{ {
CGObjectInstance *obj = gs->getObjInstance(id); CGObjectInstance *obj = gs->getObjInstance(id);
if(!obj) if(!obj)
@ -1331,18 +1330,19 @@ DLL_LINKAGE void SetObjectProperty::applyGs(CGameState *gs)
return; return;
} }
CArmedInstance *cai = dynamic_cast<CArmedInstance *>(obj); auto * cai = dynamic_cast<CArmedInstance *>(obj);
if(what == ObjProperty::OWNER && cai) if(what == ObjProperty::OWNER && cai)
{ {
if(obj->ID == Obj::TOWN) if(obj->ID == Obj::TOWN)
{ {
CGTownInstance *t = static_cast<CGTownInstance*>(obj); auto * t = dynamic_cast<CGTownInstance *>(obj);
assert(t);
if(t->tempOwner < PlayerColor::PLAYER_LIMIT) if(t->tempOwner < PlayerColor::PLAYER_LIMIT)
gs->getPlayerState(t->tempOwner)->towns -= t; gs->getPlayerState(t->tempOwner)->towns -= t;
if(val < PlayerColor::PLAYER_LIMIT_I) if(val < PlayerColor::PLAYER_LIMIT_I)
{ {
PlayerState * p = gs->getPlayerState(PlayerColor(val)); PlayerState * p = gs->getPlayerState(PlayerColor(val));
p->towns.push_back(t); p->towns.emplace_back(t);
//reset counter before NewTurn to avoid no town message if game loaded at turn when one already captured //reset counter before NewTurn to avoid no town message if game loaded at turn when one already captured
if(p->daysWithoutCastle) if(p->daysWithoutCastle)
@ -1363,7 +1363,7 @@ DLL_LINKAGE void SetObjectProperty::applyGs(CGameState *gs)
DLL_LINKAGE void PrepareHeroLevelUp::applyGs(CGameState * gs) DLL_LINKAGE void PrepareHeroLevelUp::applyGs(CGameState * gs)
{ {
auto hero = gs->getHero(heroId); auto * hero = gs->getHero(heroId);
assert(hero); assert(hero);
auto proposedSkills = hero->getLevelUpProposedSecondarySkills(); auto proposedSkills = hero->getLevelUpProposedSecondarySkills();
@ -1378,39 +1378,39 @@ DLL_LINKAGE void PrepareHeroLevelUp::applyGs(CGameState * gs)
} }
} }
DLL_LINKAGE void HeroLevelUp::applyGs(CGameState * gs) DLL_LINKAGE void HeroLevelUp::applyGs(CGameState * gs) const
{ {
auto hero = gs->getHero(heroId); auto * hero = gs->getHero(heroId);
assert(hero); assert(hero);
hero->levelUp(skills); hero->levelUp(skills);
} }
DLL_LINKAGE void CommanderLevelUp::applyGs(CGameState * gs) DLL_LINKAGE void CommanderLevelUp::applyGs(CGameState * gs) const
{ {
auto hero = gs->getHero(heroId); auto * hero = gs->getHero(heroId);
assert(hero); assert(hero);
auto commander = hero->commander; auto commander = hero->commander;
assert(commander); assert(commander);
commander->levelUp(); commander->levelUp();
} }
DLL_LINKAGE void BattleStart::applyGs(CGameState *gs) DLL_LINKAGE void BattleStart::applyGs(CGameState * gs) const
{ {
gs->curB = info; gs->curB = info;
gs->curB->localInit(); gs->curB->localInit();
} }
DLL_LINKAGE void BattleNextRound::applyGs(CGameState *gs) DLL_LINKAGE void BattleNextRound::applyGs(CGameState * gs) const
{ {
gs->curB->nextRound(round); gs->curB->nextRound(round);
} }
DLL_LINKAGE void BattleSetActiveStack::applyGs(CGameState *gs) DLL_LINKAGE void BattleSetActiveStack::applyGs(CGameState * gs) const
{ {
gs->curB->nextTurn(stack); gs->curB->nextTurn(stack);
} }
DLL_LINKAGE void BattleTriggerEffect::applyGs(CGameState *gs) DLL_LINKAGE void BattleTriggerEffect::applyGs(CGameState * gs) const
{ {
CStack * st = gs->curB->getStack(stackID); CStack * st = gs->curB->getStack(stackID);
assert(st); assert(st);
@ -1448,7 +1448,7 @@ DLL_LINKAGE void BattleTriggerEffect::applyGs(CGameState *gs)
} }
} }
DLL_LINKAGE void BattleUpdateGateState::applyGs(CGameState *gs) DLL_LINKAGE void BattleUpdateGateState::applyGs(CGameState * gs) const
{ {
if(gs->curB) if(gs->curB)
gs->curB->si.gateState = state; gs->curB->si.gateState = state;
@ -1462,7 +1462,7 @@ void BattleResult::applyGs(CGameState *gs)
for(int i = 0; i < 2; ++i) for(int i = 0; i < 2; ++i)
{ {
if(auto h = gs->curB->battleGetFightingHero(i)) if(auto * h = gs->curB->battleGetFightingHero(i))
{ {
h->removeBonusesRecursive(Bonus::OneBattle); //remove any "until next battle" bonuses h->removeBonusesRecursive(Bonus::OneBattle); //remove any "until next battle" bonuses
if (h->commander && h->commander->alive) if (h->commander && h->commander->alive)
@ -1556,7 +1556,7 @@ DLL_LINKAGE void StartAction::applyGs(CGameState *gs)
} }
else else
{ {
gs->curB->sides[ba.side].usedSpellsHistory.push_back(SpellID(ba.actionSubtype)); gs->curB->sides[ba.side].usedSpellsHistory.emplace_back(ba.actionSubtype);
} }
switch(ba.actionType) switch(ba.actionType)
@ -1581,7 +1581,7 @@ DLL_LINKAGE void StartAction::applyGs(CGameState *gs)
} }
} }
DLL_LINKAGE void BattleSpellCast::applyGs(CGameState *gs) DLL_LINKAGE void BattleSpellCast::applyGs(CGameState * gs) const
{ {
assert(gs->curB); assert(gs->curB);
@ -1647,7 +1647,7 @@ DLL_LINKAGE void BattleUnitsChanged::applyBattle(IBattleState * battleState)
battleState->updateUnit(elem.id, elem.data); battleState->updateUnit(elem.id, elem.data);
break; break;
default: default:
logNetwork->error("Unknown unit operation %d", (int)elem.operation); logNetwork->error("Unknown unit operation %d", static_cast<int>(elem.operation));
break; break;
} }
} }
@ -1676,20 +1676,15 @@ DLL_LINKAGE void BattleObstaclesChanged::applyBattle(IBattleState * battleState)
battleState->updateObstacle(change); battleState->updateObstacle(change);
break; break;
default: default:
logNetwork->error("Unknown obstacle operation %d", (int)change.operation); logNetwork->error("Unknown obstacle operation %d", static_cast<int>(change.operation));
break; break;
} }
} }
} }
DLL_LINKAGE CatapultAttack::CatapultAttack() DLL_LINKAGE CatapultAttack::CatapultAttack() = default;
{
attacker = -1;
}
DLL_LINKAGE CatapultAttack::~CatapultAttack() DLL_LINKAGE CatapultAttack::~CatapultAttack() = default;
{
}
DLL_LINKAGE void CatapultAttack::applyGs(CGameState * gs) DLL_LINKAGE void CatapultAttack::applyGs(CGameState * gs)
{ {
@ -1699,7 +1694,7 @@ DLL_LINKAGE void CatapultAttack::applyGs(CGameState * gs)
DLL_LINKAGE void CatapultAttack::applyBattle(IBattleState * battleState) DLL_LINKAGE void CatapultAttack::applyBattle(IBattleState * battleState)
{ {
auto town = battleState->getDefendedTown(); const auto * town = battleState->getDefendedTown();
if(!town) if(!town)
return; return;
@ -1713,7 +1708,7 @@ DLL_LINKAGE void CatapultAttack::applyBattle(IBattleState * battleState)
} }
} }
DLL_LINKAGE void BattleSetStackProperty::applyGs(CGameState * gs) DLL_LINKAGE void BattleSetStackProperty::applyGs(CGameState * gs) const
{ {
CStack * stack = gs->curB->getStack(stackID); CStack * stack = gs->curB->getStack(stackID);
switch(which) switch(which)
@ -1754,7 +1749,7 @@ DLL_LINKAGE void BattleSetStackProperty::applyGs(CGameState * gs)
} }
} }
DLL_LINKAGE void PlayerCheated::applyGs(CGameState *gs) DLL_LINKAGE void PlayerCheated::applyGs(CGameState * gs) const
{ {
if(!player.isValidPlayer()) if(!player.isValidPlayer())
return; return;
@ -1763,7 +1758,7 @@ DLL_LINKAGE void PlayerCheated::applyGs(CGameState *gs)
gs->getPlayerState(player)->enteredWinningCheatCode = winningCheatCode; gs->getPlayerState(player)->enteredWinningCheatCode = winningCheatCode;
} }
DLL_LINKAGE void YourTurn::applyGs(CGameState *gs) DLL_LINKAGE void YourTurn::applyGs(CGameState * gs) const
{ {
gs->currentPlayer = player; gs->currentPlayer = player;
@ -1771,8 +1766,10 @@ DLL_LINKAGE void YourTurn::applyGs(CGameState *gs)
playerState.daysWithoutCastle = daysWithoutCastle; playerState.daysWithoutCastle = daysWithoutCastle;
} }
DLL_LINKAGE Component::Component(const CStackBasicDescriptor &stack) DLL_LINKAGE Component::Component(const CStackBasicDescriptor & stack)
: id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0) : id(CREATURE)
, subtype(stack.type->idNumber)
, val(stack.count)
{ {
} }

View File

@ -63,14 +63,10 @@ struct LobbyClientConnected : public CLobbyPackToPropagate
// Set by client before sending pack to server // Set by client before sending pack to server
std::string uuid; std::string uuid;
std::vector<std::string> names; std::vector<std::string> names;
StartInfo::EMode mode; StartInfo::EMode mode = StartInfo::INVALID;
// Changed by server before announcing pack // Changed by server before announcing pack
int clientId; int clientId = -1;
int hostClientId; int hostClientId = -1;
LobbyClientConnected()
: mode(StartInfo::INVALID), clientId(-1), hostClientId(-1)
{}
bool checkClientPermissions(CVCMIServer * srv) const; bool checkClientPermissions(CVCMIServer * srv) const;
bool applyOnLobbyHandler(CServerHandler * handler); bool applyOnLobbyHandler(CServerHandler * handler);
@ -92,9 +88,8 @@ struct LobbyClientConnected : public CLobbyPackToPropagate
struct LobbyClientDisconnected : public CLobbyPackToPropagate struct LobbyClientDisconnected : public CLobbyPackToPropagate
{ {
int clientId; int clientId;
bool shutdownServer; bool shutdownServer = false;
LobbyClientDisconnected() : shutdownServer(false) {}
bool checkClientPermissions(CVCMIServer * srv) const; bool checkClientPermissions(CVCMIServer * srv) const;
bool applyOnServer(CVCMIServer * srv); bool applyOnServer(CVCMIServer * srv);
void applyOnServerAfterAnnounce(CVCMIServer * srv); void applyOnServerAfterAnnounce(CVCMIServer * srv);
@ -126,9 +121,8 @@ struct LobbyGuiAction : public CLobbyPackToPropagate
{ {
enum EAction : ui8 { enum EAction : ui8 {
NONE, NO_TAB, OPEN_OPTIONS, OPEN_SCENARIO_LIST, OPEN_RANDOM_MAP_OPTIONS NONE, NO_TAB, OPEN_OPTIONS, OPEN_SCENARIO_LIST, OPEN_RANDOM_MAP_OPTIONS
} action; } action = NONE;
LobbyGuiAction() : action(NONE) {}
bool checkClientPermissions(CVCMIServer * srv) const; bool checkClientPermissions(CVCMIServer * srv) const;
void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler); void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
@ -157,11 +151,10 @@ struct LobbyEndGame : public CLobbyPackToPropagate
struct LobbyStartGame : public CLobbyPackToPropagate struct LobbyStartGame : public CLobbyPackToPropagate
{ {
// Set by server // Set by server
std::shared_ptr<StartInfo> initializedStartInfo; std::shared_ptr<StartInfo> initializedStartInfo = nullptr;
CGameState * initializedGameState; CGameState * initializedGameState = nullptr;
int clientId; //-1 means to all clients int clientId = -1; //-1 means to all clients
LobbyStartGame() : initializedStartInfo(nullptr), initializedGameState(nullptr), clientId(-1) {}
bool checkClientPermissions(CVCMIServer * srv) const; bool checkClientPermissions(CVCMIServer * srv) const;
bool applyOnServer(CVCMIServer * srv); bool applyOnServer(CVCMIServer * srv);
void applyOnServerAfterAnnounce(CVCMIServer * srv); void applyOnServerAfterAnnounce(CVCMIServer * srv);
@ -181,9 +174,8 @@ struct LobbyStartGame : public CLobbyPackToPropagate
struct LobbyChangeHost : public CLobbyPackToPropagate struct LobbyChangeHost : public CLobbyPackToPropagate
{ {
int newHostConnectionId; int newHostConnectionId = -1;
LobbyChangeHost() : newHostConnectionId(-1) {}
bool checkClientPermissions(CVCMIServer * srv) const; bool checkClientPermissions(CVCMIServer * srv) const;
bool applyOnServer(CVCMIServer * srv); bool applyOnServer(CVCMIServer * srv);
bool applyOnServerAfterAnnounce(CVCMIServer * srv); bool applyOnServerAfterAnnounce(CVCMIServer * srv);
@ -197,9 +189,8 @@ struct LobbyChangeHost : public CLobbyPackToPropagate
struct LobbyUpdateState : public CLobbyPackToPropagate struct LobbyUpdateState : public CLobbyPackToPropagate
{ {
LobbyState state; LobbyState state;
bool hostChanged; // Used on client-side only bool hostChanged = false; // Used on client-side only
LobbyUpdateState() : hostChanged(false) {}
bool applyOnLobbyHandler(CServerHandler * handler); bool applyOnLobbyHandler(CServerHandler * handler);
void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler); void applyOnLobbyScreen(CLobbyScreen * lobby, CServerHandler * handler);
@ -228,7 +219,6 @@ struct LobbySetCampaign : public CLobbyPackToServer
{ {
std::shared_ptr<CCampaignState> ourCampaign; std::shared_ptr<CCampaignState> ourCampaign;
LobbySetCampaign() {}
bool applyOnServer(CVCMIServer * srv); bool applyOnServer(CVCMIServer * srv);
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
@ -239,9 +229,8 @@ struct LobbySetCampaign : public CLobbyPackToServer
struct LobbySetCampaignMap : public CLobbyPackToServer struct LobbySetCampaignMap : public CLobbyPackToServer
{ {
int mapId; int mapId = -1;
LobbySetCampaignMap() : mapId(-1) {}
bool applyOnServer(CVCMIServer * srv); bool applyOnServer(CVCMIServer * srv);
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
@ -252,9 +241,8 @@ struct LobbySetCampaignMap : public CLobbyPackToServer
struct LobbySetCampaignBonus : public CLobbyPackToServer struct LobbySetCampaignBonus : public CLobbyPackToServer
{ {
int bonusId; int bonusId = -1;
LobbySetCampaignBonus() : bonusId(-1) {}
bool applyOnServer(CVCMIServer * srv); bool applyOnServer(CVCMIServer * srv);
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
@ -266,11 +254,10 @@ struct LobbySetCampaignBonus : public CLobbyPackToServer
struct LobbyChangePlayerOption : public CLobbyPackToServer struct LobbyChangePlayerOption : public CLobbyPackToServer
{ {
enum EWhat : ui8 {UNKNOWN, TOWN, HERO, BONUS}; enum EWhat : ui8 {UNKNOWN, TOWN, HERO, BONUS};
ui8 what; ui8 what = UNKNOWN;
si8 direction; //-1 or +1 si8 direction = 0; //-1 or +1
PlayerColor color; PlayerColor color = PlayerColor::CANNOT_DETERMINE;
LobbyChangePlayerOption() : what(UNKNOWN), direction(0), color(PlayerColor::CANNOT_DETERMINE) {}
bool checkClientPermissions(CVCMIServer * srv) const; bool checkClientPermissions(CVCMIServer * srv) const;
bool applyOnServer(CVCMIServer * srv); bool applyOnServer(CVCMIServer * srv);
@ -284,9 +271,8 @@ struct LobbyChangePlayerOption : public CLobbyPackToServer
struct LobbySetPlayer : public CLobbyPackToServer struct LobbySetPlayer : public CLobbyPackToServer
{ {
PlayerColor clickedColor; PlayerColor clickedColor = PlayerColor::CANNOT_DETERMINE;
LobbySetPlayer() : clickedColor(PlayerColor::CANNOT_DETERMINE){}
bool applyOnServer(CVCMIServer * srv); bool applyOnServer(CVCMIServer * srv);
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
@ -297,9 +283,8 @@ struct LobbySetPlayer : public CLobbyPackToServer
struct LobbySetTurnTime : public CLobbyPackToServer struct LobbySetTurnTime : public CLobbyPackToServer
{ {
ui8 turnTime; ui8 turnTime = 0;
LobbySetTurnTime() : turnTime(0) {}
bool applyOnServer(CVCMIServer * srv); bool applyOnServer(CVCMIServer * srv);
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
@ -310,9 +295,8 @@ struct LobbySetTurnTime : public CLobbyPackToServer
struct LobbySetDifficulty : public CLobbyPackToServer struct LobbySetDifficulty : public CLobbyPackToServer
{ {
ui8 difficulty; ui8 difficulty = 0;
LobbySetDifficulty() : difficulty(0) {}
bool applyOnServer(CVCMIServer * srv); bool applyOnServer(CVCMIServer * srv);
template <typename Handler> void serialize(Handler &h, const int version) template <typename Handler> void serialize(Handler &h, const int version)
@ -323,10 +307,9 @@ struct LobbySetDifficulty : public CLobbyPackToServer
struct LobbyForceSetPlayer : public CLobbyPackToServer struct LobbyForceSetPlayer : public CLobbyPackToServer
{ {
ui8 targetConnectedPlayer; ui8 targetConnectedPlayer = -1;
PlayerColor targetPlayerColor; PlayerColor targetPlayerColor = PlayerColor::CANNOT_DETERMINE;
LobbyForceSetPlayer() : targetConnectedPlayer(-1), targetPlayerColor(PlayerColor::CANNOT_DETERMINE) {}
bool applyOnServer(CVCMIServer * srv); bool applyOnServer(CVCMIServer * srv);
template <typename Handler> void serialize(Handler & h, const int version) template <typename Handler> void serialize(Handler & h, const int version)