Skip to content

Commit

Permalink
Rewrite Position methods in constexpr
Browse files Browse the repository at this point in the history
  • Loading branch information
MillhioreBT committed Apr 24, 2024
1 parent a8a38e5 commit e6bd08e
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 266 deletions.
4 changes: 2 additions & 2 deletions src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,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 @@ -242,7 +242,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
34 changes: 10 additions & 24 deletions src/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,22 +722,15 @@ 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 @@ -919,22 +912,15 @@ 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 @@ -1236,8 +1222,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
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,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 @@ -338,8 +338,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 @@ -359,10 +359,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 @@ -478,7 +476,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;
if (getPlayer()) {
Expand All @@ -494,9 +492,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 @@ -548,7 +544,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 @@ -571,7 +567,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 @@ -1569,7 +1565,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 @@ -1581,7 +1577,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 @@ -1607,8 +1603,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
Loading

0 comments on commit e6bd08e

Please sign in to comment.