Explorar el Código

Small micro-optimizations for code that gets called A LOT

Ivan Savenko hace 11 meses
padre
commit
4f80ccd648
Se han modificado 3 ficheros con 10 adiciones y 5 borrados
  1. 5 2
      lib/IHandlerBase.h
  2. 4 2
      lib/battle/CBattleInfoCallback.cpp
  3. 1 1
      lib/constants/EntityIdentifiers.cpp

+ 5 - 2
lib/IHandlerBase.h

@@ -48,12 +48,15 @@ template <class _ObjectID, class _ObjectBase, class _Object, class _ServiceBase>
 {
 	const _Object * getObjectImpl(const int32_t index) const
 	{
-		if(index < 0 || index >= objects.size())
+		try
+		{
+			return objects.at(index).get();
+		}
+		catch (const std::out_of_range&)
 		{
 			logMod->error("%s id %d is invalid", getTypeNames()[0], index);
 			throw std::runtime_error("Attempt to access invalid index " + std::to_string(index) + " of type " + getTypeNames().front());
 		}
-		return objects[index].get();
 	}
 
 public:

+ 4 - 2
lib/battle/CBattleInfoCallback.cpp

@@ -711,6 +711,9 @@ bool CBattleInfoCallback::battleCanShoot(const battle::Unit * attacker) const
 	if (attacker->creatureIndex() == CreatureID::CATAPULT) //catapult cannot attack creatures
 		return false;
 
+	if (!attacker->canShoot())
+		return false;
+
 	//forgetfulness
 	TConstBonusListPtr forgetfulList = attacker->getBonuses(Selector::type()(BonusType::FORGETFULL));
 	if(!forgetfulList->empty())
@@ -722,8 +725,7 @@ bool CBattleInfoCallback::battleCanShoot(const battle::Unit * attacker) const
 			return false;
 	}
 
-	return attacker->canShoot()	&& (!battleIsUnitBlocked(attacker)
-			|| attacker->hasBonusOfType(BonusType::FREE_SHOOTING));
+	return !battleIsUnitBlocked(attacker) || attacker->hasBonusOfType(BonusType::FREE_SHOOTING);
 }
 
 bool CBattleInfoCallback::battleCanTargetEmptyHex(const battle::Unit * attacker) const

+ 1 - 1
lib/constants/EntityIdentifiers.cpp

@@ -325,7 +325,7 @@ const Skill * SecondarySkill::toEntity(const Services * services) const
 
 const CCreature * CreatureIDBase::toCreature() const
 {
-	return dynamic_cast<const CCreature *>(toEntity(VLC));
+	return (*VLC->creh)[num];
 }
 
 const Creature * CreatureIDBase::toEntity(const Services * services) const