diff --git a/client/CSpellWindow.cpp b/client/CSpellWindow.cpp index 8c43b9dae..1f88df9e2 100644 --- a/client/CSpellWindow.cpp +++ b/client/CSpellWindow.cpp @@ -744,23 +744,26 @@ void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState) if (h->getSpellSchoolLevel(CGI->spellh->objects[spell]) < 2) //not advanced or expert - teleport to nearest available city { - int nearest = -1; //nearest town's ID - double dist = -1; - for (int g=0; gcb->getTown((*nearest)->id)->pos.dist2dSQ(h->pos); + + for (auto i = nearest + 1; i != Towns.cend(); ++i) { - const CGTownInstance * dest = LOCPLINT->cb->getTown(Towns[g]->id); - double curDist = dest->pos.dist2d(h->pos); - if (nearest == -1 || curDist < dist) + const CGTownInstance * dest = LOCPLINT->cb->getTown((*i)->id); + si32 curDist = dest->pos.dist2dSQ(h->pos); + + if (curDist < dist) { - nearest = g; + nearest = i; dist = curDist; } } - if ( Towns[nearest]->visitingHero ) + + if ((*nearest)->visitingHero) LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[123]); else { - const CGTownInstance * town = LOCPLINT->cb->getTown(Towns[nearest]->id); + const CGTownInstance * town = LOCPLINT->cb->getTown((*nearest)->id); LOCPLINT->cb->castSpell(h, spell, town->visitablePos());// - town->getVisitableOffset()); } } diff --git a/lib/CGameState.cpp b/lib/CGameState.cpp index 137852662..3e9165e0d 100644 --- a/lib/CGameState.cpp +++ b/lib/CGameState.cpp @@ -1032,7 +1032,7 @@ void CGameState::initGrailPosition() && !t.visitable && t.terType != ETerrainType::WATER && t.terType != ETerrainType::ROCK - && map->grailPos.dist2d(int3(i,j,k)) <= map->grailRadious) + && map->grailPos.dist2dSQ(int3(i, j, k)) <= (map->grailRadious * map->grailRadious)) allowedPos.push_back(int3(i,j,k)); } } diff --git a/lib/CObjectHandler.cpp b/lib/CObjectHandler.cpp index 8e4a38861..75d2ce506 100644 --- a/lib/CObjectHandler.cpp +++ b/lib/CObjectHandler.cpp @@ -4173,13 +4173,13 @@ void CGTeleport::postInit() //matches subterranean gates into pairs const CGObjectInstance *cur = gatesSplit[0][i]; //find nearest underground exit - std::pair best(-1,150000); //pair + std::pair best(-1, 0x7FFFFFFF); //pair for(int j = 0; j < gatesSplit[1].size(); j++) { const CGObjectInstance *checked = gatesSplit[1][j]; if(!checked) continue; - double hlp = checked->pos.dist2d(cur->pos); + si32 hlp = checked->pos.dist2dSQ(cur->pos); if(hlp < best.second) { best.first = j; @@ -4193,9 +4193,7 @@ void CGTeleport::postInit() //matches subterranean gates into pairs gatesSplit[1][best.first] = nullptr; } else - { gates.push_back(std::make_pair(cur->id, ObjectInstanceID())); - } } objs.erase(Obj::SUBTERRANEAN_GATE); } diff --git a/lib/mapping/CMap.cpp b/lib/mapping/CMap.cpp index bd97fd117..2231fd5f2 100644 --- a/lib/mapping/CMap.cpp +++ b/lib/mapping/CMap.cpp @@ -424,7 +424,7 @@ const CGObjectInstance * CMap::getObjectiveObjectFrom(int3 pos, Obj::EObj type) bestMatch = object; else { - if (object->pos.dist2d(pos) < bestMatch->pos.dist2d(pos)) + if (object->pos.dist2dSQ(pos) < bestMatch->pos.dist2dSQ(pos)) bestMatch = object;// closer than one we already found } } diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 3e833a7eb..30d709c33 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -5592,11 +5592,11 @@ bool CGameHandler::castSpell(const CGHeroInstance *h, SpellID spellID, const int if (h->getSpellSchoolLevel(s) < 2) { - double dist = town->pos.dist2d(h->pos); + si32 dist = town->pos.dist2dSQ(h->pos); ObjectInstanceID nearest = town->id; //nearest town's ID for(const CGTownInstance * currTown : gs->getPlayer(h->tempOwner)->towns) { - double curDist = currTown->pos.dist2d(h->pos); + si32 curDist = currTown->pos.dist2dSQ(h->pos); if (nearest == ObjectInstanceID() || curDist < dist) { nearest = town->id;