Skip to content

Commit

Permalink
Merge pull request #614 from Wargus/animation_parse_flags
Browse files Browse the repository at this point in the history
Simplify `ParseAnimFlags`.
  • Loading branch information
Jarod42 authored Feb 29, 2024
2 parents 342201e + 818f429 commit 35337a1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 80 deletions.
13 changes: 0 additions & 13 deletions src/animation/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,19 +343,6 @@ int ParseAnimInt(const CUnit &unit, const std::string_view s)
return to_number(s);
}

/**
** Parse flags list in animation frame.
**
** @param unit Unit of the animation.
** @param parseflag Flag list to parse.
**
** @return The parsed value.
*/
int ParseAnimFlags(const CUnit &unit, std::string_view parseflag)
{
return unit.Anim.Anim->ParseAnimFlags(parseflag);
}

/**
** Show unit animation.
**
Expand Down
69 changes: 34 additions & 35 deletions src/animation/animation_spawnmissile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,41 @@ enum SpawnMissile_Flags
/// is bigger than unit's attack range
SM_SetDirection = 32 /// Missile takes the same direction as spawner
};

SpawnMissile_Flags ParseAnimFlags(const std::string_view &parseflag)
{
std::uint32_t flags = 0;
auto cur = parseflag;
auto beg = 0;

while (beg < parseflag.size()) {
const auto end = std::min(parseflag.find('.', beg), parseflag.size());
cur = parseflag.substr(beg, end - beg);
beg = end + 1;

if (cur == "none") {
return SM_None;
} else if (cur == "damage") {
flags |= SM_Damage;
} else if (cur == "totarget") {
flags |= SM_ToTarget;
} else if (cur == "pixel") {
flags |= SM_Pixel;
} else if (cur == "reltarget") {
flags |= SM_RelTarget;
} else if (cur == "ranged") {
flags |= SM_Ranged;
} else if (cur == "setdirection") {
flags |= SM_SetDirection;
} else {
ErrorPrint("Unknown animation flag: %s\n", cur.data());
ExitFatal(1);
}
}
return static_cast<SpawnMissile_Flags>(flags);
}

} // namespace

void CAnimation_SpawnMissile::Action(CUnit &unit, int &/*move*/, int /*scale*/) const /* override */
{
Expand All @@ -73,7 +106,7 @@ void CAnimation_SpawnMissile::Action(CUnit &unit, int &/*move*/, int /*scale*/)
const int starty = ParseAnimInt(unit, this->startYStr);
const int destx = ParseAnimInt(unit, this->destXStr);
const int desty = ParseAnimInt(unit, this->destYStr);
const SpawnMissile_Flags flags = (SpawnMissile_Flags)(::ParseAnimFlags(unit, this->flagsStr));
const SpawnMissile_Flags flags = ParseAnimFlags(this->flagsStr);
const int offsetnum = ParseAnimInt(unit, this->offsetNumStr);
const CUnit *goal = flags & SM_RelTarget ? unit.CurrentOrder()->GetGoal() : &unit;
if (!goal || goal->Destroyed) {
Expand Down Expand Up @@ -171,38 +204,4 @@ void CAnimation_SpawnMissile::Init(std::string_view s, lua_State *) /* override
>> this->destYStr >> this->flagsStr >> this->offsetNumStr;
}

std::uint32_t
CAnimation_SpawnMissile::ParseAnimFlags(const std::string_view &parseflag) const /* override */
{
std::uint32_t flags = 0;
auto cur = parseflag;
auto beg = 0;

while (beg < parseflag.size()) {
const auto end = std::min(parseflag.find('.', beg), parseflag.size());
cur = parseflag.substr(beg, end - beg);
beg = end + 1;

if (cur == "none") {
return SM_None;
} else if (cur == "damage") {
flags |= SM_Damage;
} else if (cur == "totarget") {
flags |= SM_ToTarget;
} else if (cur == "pixel") {
flags |= SM_Pixel;
} else if (cur == "reltarget") {
flags |= SM_RelTarget;
} else if (cur == "ranged") {
flags |= SM_Ranged;
} else if (cur == "setdirection") {
flags |= SM_SetDirection;
} else {
ErrorPrint("Unknown animation flag: %s\n", cur.data());
ExitFatal(1);
}
}
return flags;
}

//@}
55 changes: 27 additions & 28 deletions src/animation/animation_spawnunit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,43 @@ enum SpawnUnit_Flags
SU_Summoned = 1, /// Unit is marked as "summoned"
SU_JoinToAIForce = 2 /// Unit is included into spawner's AI force, if available
};

SpawnUnit_Flags ParseAnimFlags(const std::string_view &parseflag)
{
std::uint32_t flags = 0;
auto cur = parseflag;
auto beg = 0;

while (beg < parseflag.size()) {
const auto end = std::min(parseflag.find('.', beg), parseflag.size());
cur = parseflag.substr(beg, end - beg);
beg = end + 1;

if (cur == "none") {
return SU_None;
} else if (cur == "summoned") {
flags |= SU_Summoned;
} else if (cur == "jointoai") {
flags |= SU_JoinToAIForce;
} else {
ErrorPrint("Unknown animation flag: %s\n", cur.data());
ExitFatal(1);
}
}
return static_cast<SpawnUnit_Flags>(flags);
}

} // namespace

void CAnimation_SpawnUnit::Action(CUnit &unit, int &/*move*/, int /*scale*/) const /* override */
void CAnimation_SpawnUnit::Action(CUnit &unit, int & /*move*/, int /*scale*/) const /* override */
{
Assert(unit.Anim.Anim == this);

const int offX = ParseAnimInt(unit, this->offXStr);
const int offY = ParseAnimInt(unit, this->offYStr);
const int range = ParseAnimInt(unit, this->rangeStr);
const int playerId = ParseAnimInt(unit, this->playerStr);
const SpawnUnit_Flags flags = (SpawnUnit_Flags)(::ParseAnimFlags(unit, this->flagsStr));
const SpawnUnit_Flags flags = ParseAnimFlags(this->flagsStr);

CPlayer &player = Players[playerId];
const Vec2i pos(unit.tilePos.x + offX, unit.tilePos.y + offY);
Expand Down Expand Up @@ -104,30 +129,4 @@ void CAnimation_SpawnUnit::Init(std::string_view s, lua_State *) /* override */
>> this->flagsStr;
}

std::uint32_t
CAnimation_SpawnUnit::ParseAnimFlags(const std::string_view &parseflag) const /* override */
{
std::uint32_t flags = 0;
auto cur = parseflag;
auto beg = 0;

while (beg < parseflag.size()) {
const auto end = std::min(parseflag.find('.', beg), parseflag.size());
cur = parseflag.substr(beg, end - beg);
beg = end + 1;

if (cur == "none") {
return SU_None;
} else if (cur == "summoned") {
flags |= SU_Summoned;
} else if (cur == "jointoai") {
flags |= SU_JoinToAIForce;
} else {
ErrorPrint("Unknown animation flag: %s\n", cur.data());
ExitFatal(1);
}
}
return flags;
}

//@}
2 changes: 0 additions & 2 deletions src/include/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class CAnimation
virtual void Init(std::string_view s, lua_State *l = nullptr) {}
virtual void MapSound() {}
virtual std::optional<int> GetStillFrame(const CUnitType &type) { return std::nullopt; }
virtual std::uint32_t ParseAnimFlags(const std::string_view& parseflag) const { return 0; }

CAnimation *Next = nullptr;
};
Expand Down Expand Up @@ -156,7 +155,6 @@ extern int UnitShowAnimation(CUnit &unit, const CAnimation *anim);


extern int ParseAnimInt(const CUnit &unit, std::string_view parseint);
extern int ParseAnimFlags(const CUnit &unit, std::string_view parseflag);

extern void FindLabelLater(CAnimation **anim, std::string name);

Expand Down
1 change: 0 additions & 1 deletion src/include/animation/animation_spawnmissile.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class CAnimation_SpawnMissile : public CAnimation
public:
void Action(CUnit &unit, int &move, int scale) const override;
void Init(std::string_view s, lua_State *l) override;
std::uint32_t ParseAnimFlags(const std::string_view &parseflag) const override;

private:
std::string missileTypeStr;
Expand Down
1 change: 0 additions & 1 deletion src/include/animation/animation_spawnunit.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class CAnimation_SpawnUnit : public CAnimation
public:
void Action(CUnit &unit, int &move, int scale) const override;
void Init(std::string_view s, lua_State *l) override;
std::uint32_t ParseAnimFlags(const std::string_view &parseflag) const override;

private:
std::string unitTypeStr;
Expand Down

0 comments on commit 35337a1

Please sign in to comment.