ソースを参照

int3 dist2d microoptimization

karol57 11 年 前
コミット
dd33fd51a8
5 ファイル変更18 行追加17 行削除
  1. 12 9
      client/CSpellWindow.cpp
  2. 1 1
      lib/CGameState.cpp
  3. 2 4
      lib/CObjectHandler.cpp
  4. 1 1
      lib/mapping/CMap.cpp
  5. 2 2
      server/CGameHandler.cpp

+ 12 - 9
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; 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());
 						}
 					}

+ 1 - 1
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));
 				}
 			}

+ 2 - 4
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<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,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);
 }

+ 1 - 1
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
 			}
 		}

+ 2 - 2
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;