// TODO: Move credits array to HealthComponent.
void HealthComponent::ScaleDamageAccounts(float healthRestored) {
	if (healthRestored <= 0.0f) return;

	// Get total damage account and remember relevant clients.
	float totalAccreditedDamage = 0.0f;
	std::vector<Entity*> relevantClients;
	ForEntities<ClientComponent>([&](Entity& other, ClientComponent& client) {
		float clientDamage = entity.oldEnt->credits[other.oldEnt->s.number].value;
		if (clientDamage > 0.0f) {
			totalAccreditedDamage += clientDamage;
			relevantClients.push_back(&other);
		}
	});

	if (relevantClients.empty()) return;

	// Calculate account scale factor.
	float scale;
	if (healthRestored < totalAccreditedDamage) {
		scale = (totalAccreditedDamage - healthRestored) / totalAccreditedDamage;

		healthLogger.Debug("Scaling damage accounts of %i client(s) by %.2f.",
		                   relevantClients.size(), scale);
	} else {
		// Clear all accounts.
		scale = 0.0f;

		healthLogger.Debug("Clearing damage accounts of %i client(s).", relevantClients.size());
	}

	// Scale down or clear damage accounts.
	for (Entity* other : relevantClients) {
		entity.oldEnt->credits[other->oldEnt->s.number].value *= scale;
	}
}
float ArmorComponent::GetLocationalDamageMod(float angle, float height) {
	class_t pcl = (class_t)entity.oldEnt->client->ps.stats[STAT_CLASS];

	bool crouching = (entity.oldEnt->client->ps.pm_flags & PMF_DUCKED);

	for (int regionNum = 0; regionNum < g_numDamageRegions[pcl]; regionNum++) {
		damageRegion_t *region = &g_damageRegions[pcl][regionNum];

		// Ignore the non-locational pseudo region.
		if (region->nonlocational) continue;

		// Crouch state must match.
		if (region->crouch != crouching) continue;

		// Height must be within given range.
		if (height < region->minHeight || height > region->maxHeight) continue;

		// Angle must be within given range.
		if ((region->minAngle <= region->maxAngle && (angle < region->minAngle || angle > region->maxAngle)) ||
		    (region->minAngle >  region->maxAngle && (angle > region->maxAngle && angle < region->minAngle))) {
			continue;
		}

		armorLogger.Debug("Locational damage modifier of %.2f found for angle %.2f and height %.2f (%s).",
		                  region->modifier, angle, height, region->name);

		return region->modifier;
	}

	armorLogger.Debug("Locational damage modifier for angle %.2f and height %.2f not found.",
	                  angle, height);

	return 1.0f;
}
void IgnitableComponent::ConsiderStop(int timeDelta) {
	if (!onFire) return;

	// Don't stop freshly (re-)ignited fires.
	if (igniteTime + MIN_BURN_TIME > level.time) {
		fireLogger.Debug("(Re-)Ignited %i ms ago, skipping stop check.", level.time - igniteTime);

		return;
	}

	float burnStopChance = STOP_CHANCE;

	// Lower burn stop chance if there are other burning entities nearby.
	ForEntities<IgnitableComponent>([&](Entity &other, IgnitableComponent &ignitable){
		if (&other == &entity) return;
		if (!ignitable.onFire) return;
		if (G_Distance(other.oldEnt, entity.oldEnt) > STOP_RADIUS) return;

		float frac = G_Distance(entity.oldEnt, other.oldEnt) / STOP_RADIUS;
		float mod  = frac * 1.0f + (1.0f - frac) * STOP_CHANCE;

		burnStopChance *= mod;
	});

	// Attempt to stop burning.
	if (random() < burnStopChance) {
		fireLogger.Debug("Stopped burning (chance was %.0f%%)", burnStopChance * 100.0f);

		entity.Extinguish(0);
		return;
	} else {
		fireLogger.Debug("Didn't stop burning (chance was %.0f%%)", burnStopChance * 100.0f);
	}
}
void IgnitableComponent::ConsiderSpread(int timeDelta) {
	if (!onFire) return;
	if (level.time < spreadAt) return;

	fireLogger.Notice("Trying to spread.");

	ForEntities<IgnitableComponent>([&](Entity &other, IgnitableComponent &ignitable){
		if (&other == &entity) return;

		// Don't re-ignite.
		if (ignitable.onFire) return;

		// TODO: Use LocationComponent.
		float distance = G_Distance(other.oldEnt, entity.oldEnt);

		if (distance > SPREAD_RADIUS) return;

		float distanceFrac = distance / SPREAD_RADIUS;
		float distanceMod  = 1.0f - distanceFrac;
		float spreadChance = distanceMod;

		if (random() < spreadChance) {
			if (G_LineOfSight(entity.oldEnt, other.oldEnt) && other.Ignite(fireStarter)) {
				fireLogger.Notice("Ignited a neighbour, chance to do so was %.0f%%.",
				                  spreadChance*100.0f);
			}
		}
	});

	// Don't spread again until re-ignited.
	spreadAt = INT_MAX;
}
void IgnitableComponent::HandleIgnite(gentity_t* fireStarter) {
	if (!fireStarter) {
		// TODO: Find out why this happens.
		fireLogger.Notice("Received ignite message with no fire starter.");
	}

	if (level.time < immuneUntil) {
		fireLogger.Debug("Not ignited: Immune against fire.");

		return;
	}

	// Refresh ignite time even if already burning.
	igniteTime = level.time;

	if (!onFire) {
		onFire = true;
		this->fireStarter = fireStarter;

		fireLogger.Debug("Ignited.");
	} else {
		if (alwaysOnFire && !this->fireStarter) {
			// HACK: Igniting an alwaysOnFire entity will initialize the fire starter.
			this->fireStarter = fireStarter;

			fireLogger.Debug("Firestarter initialized.");
		} else {
			fireLogger.Debug("Re-Ignited.");
		}
	}
}
bool TurretComponent::TargetValid(Entity& target, bool newTarget) {
	if (!target.Get<ClientComponent>() ||
	    target.Get<SpectatorComponent>() ||
	    Entities::IsDead(target) ||
	    (target.oldEnt->flags & FL_NOTARGET) ||
	    !Entities::OnOpposingTeams(entity, target) ||
	    G_Distance(entity.oldEnt, target.oldEnt) > range ||
	    !trap_InPVS(entity.oldEnt->s.origin, target.oldEnt->s.origin)) {

		if (!newTarget) {
			turretLogger.Verbose("Target lost: Out of range or eliminated.");
		}

		return false;
	}

	// New targets require a line of sight.
	if (G_LineOfFire(entity.oldEnt, target.oldEnt)) {
		lastLineOfSightToTarget = level.time;
	} else if (newTarget) {
		return false;
	}

	// Give up on an existing target if there was no line of sight for a while.
	if (lastLineOfSightToTarget + GIVEUP_TARGET_TIME <= level.time) {
		turretLogger.Verbose("Giving up on target: No line of sight for %d ms.",
			level.time - lastLineOfSightToTarget
		);

		return false;
	}

	return true;
}
void IgnitableComponent::ConsiderStop(int timeDelta) {
	if (!onFire) return;

	// Don't stop freshly (re-)ignited fires.
	if (igniteTime + MIN_BURN_TIME > level.time) {
		fireLogger.DoDebugCode([&]{
			int elapsed   = level.time - igniteTime;
			int remaining = MIN_BURN_TIME - elapsed;
			fireLogger.Debug("Burning for %.1fs, skipping stop check for another %.1fs.",
			                 (float)elapsed/1000.0f, (float)remaining/1000.0f);
		});

		return;
	}

	float averagePostMinBurnTime = BASE_AVERAGE_BURN_TIME - MIN_BURN_TIME;

	// Increase average burn time dynamically for burning entities in range.
	ForEntities<IgnitableComponent>([&](Entity &other, IgnitableComponent &ignitable){
		if (&other == &entity) return;
		if (!ignitable.onFire) return;

		// TODO: Use LocationComponent.
		float distance = G_Distance(other.oldEnt, entity.oldEnt);

		if (distance > EXTRA_BURN_TIME_RADIUS) return;

		float distanceFrac = distance / EXTRA_BURN_TIME_RADIUS;
		float distanceMod  = 1.0f - distanceFrac;

		averagePostMinBurnTime += EXTRA_AVERAGE_BURN_TIME * distanceMod;
	});

	// The burn stop chance follows an exponential distribution.
	float lambda = 1.0f / averagePostMinBurnTime;
	float burnStopChance = 1.0f - std::exp(-1.0f * lambda * (float)timeDelta);

	float averageTotalBurnTime = averagePostMinBurnTime + (float)MIN_BURN_TIME;

	// Attempt to stop burning.
	if (random() < burnStopChance) {
		fireLogger.Notice("Stopped burning after %.1fs, target average lifetime was %.1fs.",
		                  (float)(level.time - igniteTime) / 1000.0f, averageTotalBurnTime / 1000.0f);

		entity.Extinguish(0);
		return;
	} else {
		fireLogger.Debug("Burning for %.1fs, target average lifetime is %.1fs.",
		                 (float)(level.time - igniteTime) / 1000.0f, averageTotalBurnTime / 1000.0f);
	}
}
void TurretComponent::ResetDirection() {
	directionToTarget = baseDirection;

	turretLogger.Verbose("Target direction reset. New direction: %s.",
		Utility::Print(directionToTarget)
	);
}
void AlienBuildableComponent::HandleDie(gentity_t* killer, meansOfDeath_t meansOfDeath) {
	entity.oldEnt->powered = false;

	// Warn if in main base and there's an overmind.
	gentity_t *om;
	if ((om = G_ActiveOvermind()) && om != entity.oldEnt && level.time > om->warnTimer
			&& G_InsideBase(entity.oldEnt, true) && G_IsWarnableMOD(meansOfDeath)) {
		om->warnTimer = level.time + ATTACKWARN_NEARBY_PERIOD;
		G_BroadcastEvent(EV_WARN_ATTACK, 0, TEAM_ALIENS);
		Beacon::NewArea(BCT_DEFEND, entity.oldEnt->s.origin, entity.oldEnt->buildableTeam);
	}

	// Set blast timer.
	int blastDelay = 0;
	if (entity.oldEnt->spawned && GetBuildableComponent().GetHealthComponent().Health() /
			GetBuildableComponent().GetHealthComponent().MaxHealth() > -1.0f) {
		blastDelay += GetBlastDelay();
	}

	alienBuildableLogger.Debug("Alien buildable dies, will blast in %i ms.", blastDelay);

	GetBuildableComponent().SetState(BuildableComponent::PRE_BLAST);

	GetBuildableComponent().REGISTER_THINKER(Blast, ThinkingComponent::SCHEDULER_BEFORE, blastDelay);
}
void ResourceStorageComponent::HandleDie(gentity_t* killer, meansOfDeath_t meansOfDeath) {
	// TODO: Add TeamComponent and/or Utility::Team.
	team_t team           = entity.oldEnt->buildableTeam;
	float  storedFraction = GetStoredFraction();

	// Removes some of the owner's team's build points, proportional to the amount this structure
	// acquired and the amount of health lost (before deconstruction).
	float loss = (1.0f - entity.oldEnt->deconHealthFrac) * storedFraction *
	             level.team[team].buildPoints * g_buildPointLossFraction.value;

	resourceStorageLogger.Notice(
		"A resource storage died, removing %.0f BP from the team's pool "
		"(%.1f × %.0f%% stored × %.0f%% health lost × %.0f total).",
		loss, g_buildPointLossFraction.value, 100.0f * storedFraction,
		100.0f * (1.0f - entity.oldEnt->deconHealthFrac), level.team[team].buildPoints
	);

	G_ModifyBuildPoints(team, -loss);

	// Main structures keep their account of acquired build points across lifecycles, it's saved
	// in a per-team variable and copied over to the ResourceStorageComponent whenever it's modified.
	if (!entity.Get<MainBuildableComponent>()) {
		G_ModifyTotalBuildPointsAcquired(team, -acquiredBuildPoints);
	}

	acquiredBuildPoints = 0.0f;
}
Example #11
0
/**
 * @brief Predict the total efficiency gain for a team when a miner is constructed at a given point.
 * @return Predicted efficiency delta in percent points.
 * @todo Consider RGS set for deconstruction.
 */
float G_RGSPredictEfficiencyDelta(vec3_t origin, team_t team) {
	float delta = G_RGSPredictOwnEfficiency(origin);

	buildpointLogger.Debug("Predicted efficiency of new miner itself: %f.", delta);

	ForEntities<MiningComponent>([&] (Entity& miner, MiningComponent& miningComponent) {
		if (G_Team(miner.oldEnt) != team) return;

		delta += RGSPredictEfficiencyLoss(miner, origin);
	});

	buildpointLogger.Debug("Predicted efficiency delta: %f. Build point delta: %f.", delta,
	                       delta * g_buildPointBudgetPerMiner.value);

	return delta;
}
// TODO: Consider location as well as direction when both given.
void KnockbackComponent::HandleDamage(float amount, gentity_t* source, Util::optional<Vec3> location,
                                      Util::optional<Vec3> direction, int flags, meansOfDeath_t meansOfDeath) {
	if (!(flags & DAMAGE_KNOCKBACK)) return;
	if (amount <= 0.0f) return;

	if (!direction) {
		knockbackLogger.Warn("Received damage message with knockback flag set but no direction.");
		return;
	}

	if (Math::Length(direction.value()) == 0.0f) {
		knockbackLogger.Warn("Attempt to do knockback with null vector direction.");
		return;
	}

	// TODO: Remove dependency on client.
	gclient_t *client = entity.oldEnt->client;
	assert(client);

	// Check for immunity.
	if (client->noclip) return;
	if (client->sess.spectatorState != SPECTATOR_NOT) return;

	float mass = (float)BG_Class(client->ps.stats[ STAT_CLASS ])->mass;

	if (mass <= 0.0f) {
		knockbackLogger.Warn("Attempt to do knockback against target with no mass, assuming normal mass.");
		mass = KNOCKBACK_NORMAL_MASS;
	}

	float massMod  = Math::Clamp(KNOCKBACK_NORMAL_MASS / mass, KNOCKBACK_MIN_MASSMOD, KNOCKBACK_MAX_MASSMOD);
	float strength = amount * DAMAGE_TO_KNOCKBACK * massMod;

	// Change client velocity.
	Vec3 clientVelocity = Vec3::Load(client->ps.velocity);
	clientVelocity += Math::Normalize(direction.value()) * strength;
	clientVelocity.Store(client->ps.velocity);

	// Set pmove timer so that the client can't cancel out the movement immediately.
	if (!client->ps.pm_time) {
		client->ps.pm_time = KNOCKBACK_PMOVE_TIME;
		client->ps.pm_flags |= PMF_TIME_KNOCKBACK;
	}

	knockbackLogger.Debug("Knockback: client: %i, strength: %.1f (massMod: %.1f).",
	                      entity.oldEnt->s.number, strength, massMod);
}
void HealthComponent::SetHealth(float health) {
	Math::Clamp(health, FLT_EPSILON, maxHealth);

	healthLogger.Debug("Changing health: %3.1f → %3.1f.", this->health, health);

	ScaleDamageAccounts(health - this->health);
	HealthComponent::health = health;
}
float ArmorComponent::GetNonLocationalDamageMod() {
	class_t pcl = (class_t)entity.oldEnt->client->ps.stats[STAT_CLASS];

	for (int regionNum = 0; regionNum < g_numDamageRegions[pcl]; regionNum++) {
		damageRegion_t *region = &g_damageRegions[pcl][regionNum];

		if (!region->nonlocational) continue;

		armorLogger.Debug("Found non-locational damage modifier of %.2f.", region->modifier);

		return region->modifier;
	}

	armorLogger.Debug("No non-locational damage modifier found.");

	return 1.0f;
}
void TurretComponent::ResetPitch() {
	Vec3 targetRelativeAngles = relativeAimAngles;
	targetRelativeAngles[PITCH] = 0.0f;

	directionToTarget = RelativeAnglesToDirection(targetRelativeAngles);

	turretLogger.Debug("Target pitch reset. New direction: %s.", directionToTarget);
}
void HealthComponent::SetMaxHealth(float maxHealth, bool scaleHealth) {
	ASSERT_GT(maxHealth, 0.0f);

	healthLogger.Debug("Changing maximum health: %3.1f → %3.1f.", this->maxHealth, maxHealth);

	HealthComponent::maxHealth = maxHealth;
	if (scaleHealth) SetHealth(health * (this->maxHealth / maxHealth));
}
void TurretComponent::LowerPitch() {
	Vec3 targetRelativeAngles = relativeAimAngles;
	targetRelativeAngles[PITCH] = PITCH_CAP;

	directionToTarget = RelativeAnglesToDirection(targetRelativeAngles);

	turretLogger.Debug("Target pitch lowered. New direction: %s.", directionToTarget);
}
void IgnitableComponent::DamageSelf(int timeDelta) {
	if (!onFire) return;

	float damage = SELF_DAMAGE * timeDelta * 0.001f;

	if (entity.Damage(damage, fireStarter, {}, {}, 0, MOD_BURN)) {
		fireLogger.Debug("Self burn damage of %.1f (%.1f/s) was dealt.", damage, SELF_DAMAGE);
	}
}
void IgnitableComponent::DamageArea(int timeDelta) {
	if (!onFire) return;

	float damage = SPLASH_DAMAGE * timeDelta * 0.001f;

	if (G_SelectiveRadiusDamage(entity.oldEnt->s.origin, fireStarter, damage, SPLASH_DAMAGE_RADIUS,
			entity.oldEnt, MOD_BURN, TEAM_NONE)) {
		fireLogger.Debug("Area burn damage of %.1f (%.1f/s) was dealt.", damage, SPLASH_DAMAGE);
	}
}
void TurretComponent::RemoveTarget() {
	if (target) {
		// TODO: Decrease tracked-by counter for the target.

		turretLogger.Verbose("Target removed.");
	}

	target = nullptr;
	lastLineOfSightToTarget = 0;
}
Example #21
0
/**
 * @brief Predict the efficiecy loss of an existing miner if another one is constructed closeby.
 * @return Efficiency loss as negative value.
 */
static float RGSPredictEfficiencyLoss(Entity& miner, vec3_t newMinerOrigin) {
	float distance               = Distance(miner.oldEnt->s.origin, newMinerOrigin);
	float oldPredictedEfficiency = miner.Get<MiningComponent>()->Efficiency(true);
	float newPredictedEfficiency = oldPredictedEfficiency * MiningComponent::InterferenceMod(distance);
	float efficiencyLoss         = newPredictedEfficiency - oldPredictedEfficiency;

	buildpointLogger.Debug("Predicted efficiency loss of existing miner: %f - %f = %f.",
	                       oldPredictedEfficiency, newPredictedEfficiency, efficiencyLoss);

	return efficiencyLoss;
}
void IgnitableComponent::HandleExtinguish(int immunityTime) {
	if (!onFire) return;

	onFire = false;
	immuneUntil = level.time + immunityTime;

	if (alwaysOnFire) {
		entity.FreeAt(DeferredFreeingComponent::FREE_BEFORE_THINKING);
	}

	fireLogger.Debug("Extinguished.");
}
void IgnitableComponent::HandleIgnite(gentity_t* fireStarter) {
	if (!fireStarter) {
		// TODO: Find out why this happens.
		fireLogger.Notice("Received ignite message with no fire starter.");
	}

	if (level.time < immuneUntil) {
		fireLogger.Debug("Not ignited: Immune against fire.");

		return;
	}

	// Start burning on initial ignition.
	if (!onFire) {
		onFire = true;
		this->fireStarter = fireStarter;

		fireLogger.Notice("Ignited.");
	} else {
		if (alwaysOnFire && !this->fireStarter) {
			// HACK: Igniting an alwaysOnFire entity will initialize the fire starter.
			this->fireStarter = fireStarter;

			fireLogger.Debug("Firestarter set.");
		} else {
			fireLogger.Debug("Re-ignited.");
		}
	}

	// Refresh ignite time even if already burning.
	igniteTime = level.time;

	// The spread delay follows a normal distribution: More likely to spread early than late.
	int spreadTarget = level.time + (int)std::abs(normalDistribution(randomGenerator));

	// Allow re-ignition to update the spread delay to a lower value.
	if (spreadTarget < spreadAt) {
		fireLogger.DoNoticeCode([&]{
			int newDelay = spreadTarget - level.time;
			if (spreadAt == INT_MAX) {
				fireLogger.Notice("Spread delay set to %.1fs.", newDelay * 0.001f);
			} else {
				int oldDelay = spreadAt - level.time;
				fireLogger.Notice("Spread delay updated from %.1fs to %.1fs.",
				                  oldDelay * 0.001f, newDelay * 0.001f);
			}
		});
		spreadAt = spreadTarget;
	}
}
void IgnitableComponent::ConsiderSpread(int timeDelta) {
	if (!onFire) return;

	ForEntities<IgnitableComponent>([&](Entity &other, IgnitableComponent &ignitable){
		if (&other == &entity) return;

		// TODO: Use LocationComponent.
		float chance = 1.0f - G_Distance(entity.oldEnt, other.oldEnt) / SPREAD_RADIUS;

		if (chance <= 0.0f) return; // distance > spread radius

		if (random() < chance) {
			if (G_LineOfSight(entity.oldEnt, other.oldEnt) && other.Ignite(fireStarter)) {
				fireLogger.Debug("(Re-)Ignited a neighbour (chance was %.0f%%)", chance * 100.0f);
			} else {
				fireLogger.Debug("Tried to ignite a non-ignitable or non-LOS neighbour (chance was %.0f%%)",
								 chance * 100.0f);
			}
		} else {
			fireLogger.Debug("Didn't try to ignite a neighbour (chance was %.0f%%)", chance * 100.0f);
		}
	});
}
void TurretComponent::TrackEntityTarget() {
	if (!target) return;

	Vec3 oldDirectionToTarget = directionToTarget;

	Vec3 targetOrigin = Vec3::Load(target->s.origin);
	Vec3 muzzle       = Vec3::Load(entity.oldEnt->s.pos.trBase);

	directionToTarget = Math::Normalize(targetOrigin - muzzle);

	if (Math::DistanceSq(directionToTarget, oldDirectionToTarget) > 0.0f) {
		turretLogger.Debug("Following an entity target. New direction: %s.", directionToTarget);
	}
}
Example #26
0
void SpikerComponent::HandleDamage(float amount, gentity_t *source, Util::optional<Vec3> location,
                                   Util::optional<Vec3> direction, int flags, meansOfDeath_t meansOfDeath) {
	if (!GetAlienBuildableComponent().GetBuildableComponent().Active()) {
		return;
	}

	// Shoot if there is a viable target.
	if (lastExpectedDamage > 0.0f) {
		logger.Verbose("Spiker #%i was hurt while an enemy is close enough to also get hurt, so "
			"go eye for an eye.", entity.oldEnt->s.number);

		Fire();
	}
}
void HealthComponent::HandleHeal(float amount, gentity_t* source) {
	if (health <= 0.0f) return;
	if (health >= maxHealth) return;

	// Only heal up to maximum health.
	amount = std::min(amount, maxHealth - health);

	if (amount <= 0.0f) return;

	healthLogger.Debug("Healing: %3.1f (%3.1f → %3.1f)", amount, health, health + amount);

	health += amount;
	ScaleDamageAccounts(amount);
}
float ResourceStorageComponent::GetStoredFraction() {
	// TODO: Add TeamComponent and/or Utility::Team.
	team_t team = entity.oldEnt->buildableTeam;

	if (!level.team[team].acquiredBuildPoints) return 1.0f;

	// The stored fraction is equal to the acquired fraction.
	float storedFraction = acquiredBuildPoints / level.team[team].acquiredBuildPoints;

	if (storedFraction < 0.0f || storedFraction > 1.0f + LINE_DISTANCE_EPSILON) {
		resourceStorageLogger.Warn(
			"A resource storage stores an invalid fraction of all build points: %.1f", storedFraction
		);
	}

	return storedFraction;
}
// TODO: Move this to the client side.
void AlienBuildableComponent::CreepRecede(int timeDelta) {
	alienBuildableLogger.Debug("Starting creep recede.");

	G_AddEvent(entity.oldEnt, EV_BUILD_DESTROY, 0);

	if (entity.oldEnt->spawned) {
		entity.oldEnt->s.time = -level.time;
	} else {
		entity.oldEnt->s.time = -(level.time - (int)(
			(float)CREEP_SCALEDOWN_TIME *
			(1.0f - ((float)(level.time - entity.oldEnt->creationTime) /
					 (float)BG_Buildable(entity.oldEnt->s.modelindex)->buildTime)))
		);
	}

	// Remove buildable when done.
	GetBuildableComponent().REGISTER_THINKER(Remove, ThinkingComponent::SCHEDULER_AFTER, CREEP_SCALEDOWN_TIME);
	GetBuildableComponent().GetThinkingComponent().UnregisterActiveThinker();
}
Entity* TurretComponent::FindEntityTarget(std::function<bool(Entity&, Entity&)> CompareTargets) {
	// Delete old target.
	RemoveTarget();

	// Search best target.
	// TODO: Iterate over all valid targets, do not assume they have to be clients.
	ForEntities<ClientComponent>([&](Entity& candidate, ClientComponent& clientComponent) {
		if (TargetValid(candidate, true)) {
			if (!target || CompareTargets(candidate, *target->entity)) {
				target = candidate.oldEnt;
			}
		}
	});

	if (target) {
		// TODO: Increase tracked-by counter for a new target.

		turretLogger.Verbose("Target acquired.");
	}

	return target ? target->entity : nullptr;
}