Index: source/simulation2/components/CCmpRangeManager.cpp
===================================================================
--- source/simulation2/components/CCmpRangeManager.cpp	(revision 12791)
+++ source/simulation2/components/CCmpRangeManager.cpp	(working copy)
@@ -211,6 +211,7 @@
 	tag_t m_QueryNext; // next allocated id
 	std::map<tag_t, Query> m_Queries;
 	std::map<entity_id_t, EntityData> m_EntityData;
+	std::vector<EntityData> m_EntityDataArray;
 	SpatialSubdivision<entity_id_t> m_Subdivision; // spatial index of m_EntityData
 
 	// LOS state:
@@ -285,6 +286,7 @@
 		serialize.NumberU32_Unbounded("query next", m_QueryNext);
 		SerializeMap<SerializeU32_Unbounded, SerializeQuery>()(serialize, "queries", m_Queries);
 		SerializeMap<SerializeU32_Unbounded, SerializeEntityData>()(serialize, "entity data", m_EntityData);
+		//SerializeVector<SerializeEntityData>(serialize, "entity data array", m_EntityDataArray);
 
 		SerializeMap<SerializeI32_Unbounded, SerializeBool>()(serialize, "los reveal all", m_LosRevealAll);
 		serialize.Bool("los circular", m_LosCircular);
@@ -336,6 +338,7 @@
 			// (any initialisation of those values will happen later), so we can just
 			// use the default-constructed EntityData here
 			EntityData entdata;
+			
 
 			// Store the LOS data, if any
 			CmpPtr<ICmpVision> cmpVision(GetSimContext(), ent);
@@ -347,6 +350,10 @@
 
 			// Remember this entity
 			m_EntityData.insert(std::make_pair(ent, entdata));
+			if (ent >= m_EntityDataArray.size()) {
+				m_EntityDataArray.resize(ent+1);
+			}
+			m_EntityDataArray[ent] = entdata;
 
 			break;
 		}
@@ -358,7 +365,7 @@
 			std::map<entity_id_t, EntityData>::iterator it = m_EntityData.find(ent);
 
 			// Ignore if we're not already tracking this entity
-			if (it == m_EntityData.end())
+			if (it == m_EntityData.end() || ent >= m_EntityDataArray.size())
 				break;
 
 			if (msgData.inWorld)
@@ -380,6 +387,11 @@
 				it->second.inWorld = 1;
 				it->second.x = msgData.x;
 				it->second.z = msgData.z;
+				
+				EntityData& entData = m_EntityDataArray[ent];
+				entData.inWorld = 1;
+				entData.x = msgData.x;
+				entData.z = msgData.z;
 			}
 			else
 			{
@@ -393,6 +405,11 @@
 				it->second.inWorld = 0;
 				it->second.x = entity_pos_t::Zero();
 				it->second.z = entity_pos_t::Zero();
+				
+				EntityData& entData = m_EntityDataArray[ent];
+				entData.inWorld = 0;
+				entData.x = entity_pos_t::Zero();
+				entData.z = entity_pos_t::Zero();
 			}
 
 			break;
@@ -417,6 +434,8 @@
 
 			ENSURE(-128 <= msgData.to && msgData.to <= 127);
 			it->second.owner = (i8)msgData.to;
+			
+			m_EntityDataArray[ent].owner = (i8)msgData.to;
 
 			break;
 		}
@@ -467,6 +486,7 @@
 				LosRemove(it->second.owner, oldRange, pos);
 
 			it->second.visionRange = newRange;
+			m_EntityDataArray[ent].visionRange = newRange;
 
 			if (it->second.inWorld)
 				LosAdd(it->second.owner, newRange, pos);
@@ -643,7 +663,9 @@
 			return r;
 		}
 
+		PROFILE_START("perform");
 		PerformQuery(q, r);
+		PROFILE_END("perform");
 
 		// Return the list sorted by distance from the entity
 		CFixedVector2D pos = cmpSourcePosition->GetPosition2D();
@@ -815,29 +837,37 @@
 		else
 		{
 			// Get a quick list of entities that are potentially in range
-			std::vector<entity_id_t> ents = m_Subdivision.GetNear(pos, q.maxRange);
+			std::vector<entity_id_t> ents;
+			PROFILE_START("GetNear");
+			ents = m_Subdivision.GetNear(pos, q.maxRange);
+			PROFILE_END("GetNear");
 
 			for (size_t i = 0; i < ents.size(); ++i)
 			{
-				std::map<entity_id_t, EntityData>::const_iterator it = m_EntityData.find(ents[i]);
-				ENSURE(it != m_EntityData.end());
+				//std::map<entity_id_t, EntityData>::const_iterator it = m_EntityData.find(ents[i]);
+				//ENSURE(it != m_EntityData.end());
+				const EntityData& entData = m_EntityDataArray[ents[i]];
+				
+				//ENSURE(entData.x == it->second.x);
+				
+				//if (entData.x != it->second.x) LOGWARNING(L"hi");
 
-				if (!TestEntityQuery(q, it->first, it->second))
+				if (!TestEntityQuery(q, ents[i], entData))
 					continue;
 
 				// Restrict based on precise distance
-				int distVsMax = (CFixedVector2D(it->second.x, it->second.z) - pos).CompareLength(q.maxRange);
+				int distVsMax = (CFixedVector2D(entData.x, entData.z) - pos).CompareLength(q.maxRange);
 				if (distVsMax > 0)
 					continue;
 
 				if (!q.minRange.IsZero())
 				{
-					int distVsMin = (CFixedVector2D(it->second.x, it->second.z) - pos).CompareLength(q.minRange);
+					int distVsMin = (CFixedVector2D(entData.x, entData.z) - pos).CompareLength(q.minRange);
 					if (distVsMin < 0)
 						continue;
 				}
 
-				r.push_back(it->first);
+				r.push_back(ents[i]);
 			}
 		}
 	}
