Skip to content

Commit

Permalink
Rewrite Position methods in constexpr (#4648)
Browse files Browse the repository at this point in the history
* Rewrite Position methods in constexpr

- Refactor static functions to member functions
- Drop unnecessary fast integer variants
- Remove redundant template parameter to std::max/min

* Add constexpr abs to workaround MSVC
  • Loading branch information
ranisalt committed Apr 17, 2024
1 parent 2cdf1de commit 45dc6f9
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 214 deletions.
4 changes: 2 additions & 2 deletions src/actions.cpp
Expand Up @@ -236,7 +236,7 @@ ReturnValue Actions::canUse(const Player* player, const Position& pos)
return playerPos.z > pos.z ? RETURNVALUE_FIRSTGOUPSTAIRS : RETURNVALUE_FIRSTGODOWNSTAIRS;
}

if (!Position::areInRange<1, 1>(playerPos, pos)) {
if (!playerPos.isInRange(pos, 1, 1)) {
return RETURNVALUE_TOOFARAWAY;
}
}
Expand All @@ -263,7 +263,7 @@ ReturnValue Actions::canUseFar(const Creature* creature, const Position& toPos,
return creaturePos.z > toPos.z ? RETURNVALUE_FIRSTGOUPSTAIRS : RETURNVALUE_FIRSTGODOWNSTAIRS;
}

if (!Position::areInRange<Map::maxClientViewportX - 1, Map::maxClientViewportY - 1>(toPos, creaturePos)) {
if (!toPos.isInRange(creaturePos, Map::maxClientViewportX - 1, Map::maxClientViewportY - 1)) {
return RETURNVALUE_TOOFARAWAY;
}

Expand Down
36 changes: 10 additions & 26 deletions src/combat.cpp
Expand Up @@ -717,22 +717,14 @@ void Combat::doCombat(Creature* caster, const Position& position) const
: getCombatArea(position, position, area.get());

SpectatorVec spectators;
uint32_t maxX = 0;
uint32_t maxY = 0;
int32_t maxX = 0;
int32_t maxY = 0;

// calculate the max viewable range
for (Tile* tile : tiles) {
const Position& tilePos = tile->getPosition();

uint32_t diff = Position::getDistanceX(tilePos, position);
if (diff > maxX) {
maxX = diff;
}

diff = Position::getDistanceY(tilePos, position);
if (diff > maxY) {
maxY = diff;
}
maxX = std::max(maxX, tilePos.getDistanceX(position));
maxY = std::max(maxY, tilePos.getDistanceY(position));
}

const int32_t rangeX = maxX + Map::maxViewportX;
Expand Down Expand Up @@ -914,22 +906,14 @@ void Combat::doAreaCombat(Creature* caster, const Position& position, const Area
}
}

uint32_t maxX = 0;
uint32_t maxY = 0;
int32_t maxX = 0;
int32_t maxY = 0;

// calculate the max viewable range
for (Tile* tile : tiles) {
const Position& tilePos = tile->getPosition();

uint32_t diff = Position::getDistanceX(tilePos, position);
if (diff > maxX) {
maxX = diff;
}

diff = Position::getDistanceY(tilePos, position);
if (diff > maxY) {
maxY = diff;
}
maxX = std::max(maxX, tilePos.getDistanceX(position));
maxY = std::max(maxY, tilePos.getDistanceY(position));
}

const int32_t rangeX = maxX + Map::maxViewportX;
Expand Down Expand Up @@ -1231,8 +1215,8 @@ void TargetCallback::onTargetCombat(Creature* creature, Creature* target) const

const MatrixArea& AreaCombat::getArea(const Position& centerPos, const Position& targetPos) const
{
int32_t dx = Position::getOffsetX(targetPos, centerPos);
int32_t dy = Position::getOffsetY(targetPos, centerPos);
int32_t dx = centerPos.getOffsetX(targetPos);
int32_t dy = centerPos.getOffsetY(targetPos);

Direction dir;
if (dx < 0) {
Expand Down
31 changes: 13 additions & 18 deletions src/creature.cpp
Expand Up @@ -54,12 +54,12 @@ bool Creature::canSee(const Position& myPos, const Position& pos, int32_t viewRa
}

// view is +/- 2 from the floor we stand on
if (Position::getDistanceZ(myPos, pos) > 2) {
if (myPos.getDistanceZ(pos) > 2) {
return false;
}
}

const int_fast32_t offsetz = myPos.getZ() - pos.getZ();
int32_t offsetz = myPos.getOffsetZ(pos);
return (pos.getX() >= myPos.getX() - viewRangeX + offsetz) && (pos.getX() <= myPos.getX() + viewRangeX + offsetz) &&
(pos.getY() >= myPos.getY() - viewRangeY + offsetz) && (pos.getY() <= myPos.getY() + viewRangeY + offsetz);
}
Expand Down Expand Up @@ -346,8 +346,8 @@ void Creature::updateTileCache(const Tile* tile, const Position& pos)
{
const Position& myPos = getPosition();
if (pos.z == myPos.z) {
int32_t dx = Position::getOffsetX(pos, myPos);
int32_t dy = Position::getOffsetY(pos, myPos);
int32_t dx = myPos.getOffsetX(pos);
int32_t dy = myPos.getOffsetY(pos);
updateTileCache(tile, dx, dy);
}
}
Expand All @@ -367,10 +367,8 @@ int32_t Creature::getWalkCache(const Position& pos) const
return 1;
}

int32_t dx = Position::getOffsetX(pos, myPos);
if (std::abs(dx) <= maxWalkCacheWidth) {
int32_t dy = Position::getOffsetY(pos, myPos);
if (std::abs(dy) <= maxWalkCacheHeight) {
if (int32_t dx = myPos.getOffsetX(pos); std::abs(dx) <= maxWalkCacheWidth) {
if (int32_t dy = myPos.getOffsetY(pos); std::abs(dy) <= maxWalkCacheHeight) {
if (localMapCache[maxWalkCacheHeight + dy][maxWalkCacheWidth + dx]) {
return 1;
}
Expand Down Expand Up @@ -482,7 +480,7 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos
if (oldPos.z != newPos.z) {
// floor change extra cost
lastStepCost = 2;
} else if (Position::getDistanceX(newPos, oldPos) >= 1 && Position::getDistanceY(newPos, oldPos) >= 1) {
} else if (newPos.getDistanceX(oldPos) >= 1 && oldPos.getDistanceY(newPos) >= 1) {
// diagonal extra cost
lastStepCost = 3;
}
Expand All @@ -495,9 +493,7 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos
std::forward_list<Creature*> despawnList;
for (Creature* summon : summons) {
const Position& pos = summon->getPosition();
if (Position::getDistanceZ(newPos, pos) > 2 ||
(std::max<int32_t>(Position::getDistanceX(newPos, pos), Position::getDistanceY(newPos, pos)) >
30)) {
if (newPos.getDistanceZ(pos) > 2 || std::max(newPos.getDistanceX(pos), pos.getDistanceY(newPos)) > 30) {
despawnList.push_front(summon);
}
}
Expand Down Expand Up @@ -549,7 +545,7 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos
// shift y west
int32_t starty = 0;
int32_t endy = mapWalkHeight - 1;
int32_t dy = Position::getDistanceY(oldPos, newPos);
int32_t dy = oldPos.getDistanceY(newPos);

if (dy < 0) {
endy += dy;
Expand All @@ -572,7 +568,7 @@ void Creature::onCreatureMove(Creature* creature, const Tile* newTile, const Pos
// shift y east
int32_t starty = 0;
int32_t endy = mapWalkHeight - 1;
int32_t dy = Position::getDistanceY(oldPos, newPos);
int32_t dy = oldPos.getDistanceY(newPos);

if (dy < 0) {
endy += dy;
Expand Down Expand Up @@ -1584,7 +1580,7 @@ bool FrozenPathingConditionCall::isInRange(const Position& startPos, const Posit
return false;
}
} else {
int_fast32_t dx = Position::getOffsetX(startPos, targetPos);
int32_t dx = targetPos.getOffsetX(startPos);

int32_t dxMax = (dx >= 0 ? fpp.maxTargetDist : 0);
if (testPos.x > targetPos.x + dxMax) {
Expand All @@ -1596,7 +1592,7 @@ bool FrozenPathingConditionCall::isInRange(const Position& startPos, const Posit
return false;
}

int_fast32_t dy = Position::getOffsetY(startPos, targetPos);
int32_t dy = targetPos.getOffsetY(startPos);

int32_t dyMax = (dy >= 0 ? fpp.maxTargetDist : 0);
if (testPos.y > targetPos.y + dyMax) {
Expand All @@ -1622,8 +1618,7 @@ bool FrozenPathingConditionCall::operator()(const Position& startPos, const Posi
return false;
}

int32_t testDist =
std::max<int32_t>(Position::getDistanceX(targetPos, testPos), Position::getDistanceY(targetPos, testPos));
int32_t testDist = std::max(targetPos.getDistanceX(testPos), testPos.getDistanceY(targetPos));
if (fpp.maxTargetDist == 1) {
if (testDist < fpp.minTargetDist || testDist > fpp.maxTargetDist) {
return false;
Expand Down

0 comments on commit 45dc6f9

Please sign in to comment.