1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

int3 dist2d microoptimization

This commit is contained in:
karol57 2014-05-25 20:42:25 +02:00
parent df8cce0d61
commit dd33fd51a8
5 changed files with 18 additions and 17 deletions

View File

@ -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; g<Towns.size(); ++g)
auto nearest = Towns.cbegin(); //nearest town's iterator
si32 dist = LOCPLINT->cb->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());
}
}

View File

@ -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));
}
}

View File

@ -4173,13 +4173,13 @@ void CGTeleport::postInit() //matches subterranean gates into pairs
const CGObjectInstance *cur = gatesSplit[0][i];
//find nearest underground exit
std::pair<int,double> best(-1,150000); //pair<pos_in_vector, distance>
std::pair<int, si32> best(-1, 0x7FFFFFFF); //pair<pos_in_vector, distance^2>
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,10 +4193,8 @@ 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);
}

View File

@ -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
}
}

View File

@ -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;