| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931 |
- #include "combat_system.h"
- #include "../core/component.h"
- #include "../core/event_manager.h"
- #include "../core/world.h"
- #include "../units/troop_config.h"
- #include "../visuals/team_colors.h"
- #include "arrow_system.h"
- #include "building_collision_registry.h"
- #include "command_service.h"
- #include "owner_registry.h"
- #include <algorithm>
- #include <cmath>
- #include <limits>
- #include <random>
- namespace Game::Systems {
- void CombatSystem::update(Engine::Core::World *world, float deltaTime) {
- processAttacks(world, deltaTime);
- processAutoEngagement(world, deltaTime);
- }
- void CombatSystem::processAttacks(Engine::Core::World *world, float deltaTime) {
- auto units = world->getEntitiesWith<Engine::Core::UnitComponent>();
- ArrowSystem *arrowSys = world->getSystem<ArrowSystem>();
- for (auto attacker : units) {
- if (attacker->hasComponent<Engine::Core::PendingRemovalComponent>()) {
- continue;
- }
- auto attackerUnit = attacker->getComponent<Engine::Core::UnitComponent>();
- auto attackerTransform =
- attacker->getComponent<Engine::Core::TransformComponent>();
- auto attackerAtk = attacker->getComponent<Engine::Core::AttackComponent>();
- if (!attackerUnit || !attackerTransform) {
- continue;
- }
- if (attackerUnit->health <= 0) {
- continue;
- }
- if (attackerAtk && attackerAtk->inMeleeLock) {
- auto *lockTarget = world->getEntity(attackerAtk->meleeLockTargetId);
- if (!lockTarget ||
- lockTarget->hasComponent<Engine::Core::PendingRemovalComponent>()) {
- attackerAtk->inMeleeLock = false;
- attackerAtk->meleeLockTargetId = 0;
- } else {
- auto *lockTargetUnit =
- lockTarget->getComponent<Engine::Core::UnitComponent>();
- if (!lockTargetUnit || lockTargetUnit->health <= 0) {
- attackerAtk->inMeleeLock = false;
- attackerAtk->meleeLockTargetId = 0;
- } else {
- auto *attT = attackerTransform;
- auto *tgtT =
- lockTarget->getComponent<Engine::Core::TransformComponent>();
- if (attT && tgtT) {
- float dx = tgtT->position.x - attT->position.x;
- float dz = tgtT->position.z - attT->position.z;
- float dist = std::sqrt(dx * dx + dz * dz);
- const float IDEAL_MELEE_DISTANCE = 0.6f;
- const float MAX_MELEE_SEPARATION = 0.9f;
- if (dist > MAX_MELEE_SEPARATION) {
- float pullAmount =
- (dist - IDEAL_MELEE_DISTANCE) * 0.3f * deltaTime * 5.0f;
- if (dist > 0.001f) {
- QVector3D direction(dx / dist, 0.0f, dz / dist);
- attT->position.x += direction.x() * pullAmount;
- attT->position.z += direction.z() * pullAmount;
- }
- }
- }
- }
- }
- }
- if (attackerAtk && attackerAtk->inMeleeLock &&
- attackerAtk->meleeLockTargetId != 0) {
- auto *lockTarget = world->getEntity(attackerAtk->meleeLockTargetId);
- if (lockTarget &&
- !lockTarget->hasComponent<Engine::Core::PendingRemovalComponent>()) {
- auto *attackTarget =
- attacker->getComponent<Engine::Core::AttackTargetComponent>();
- if (!attackTarget) {
- attackTarget =
- attacker->addComponent<Engine::Core::AttackTargetComponent>();
- }
- if (attackTarget) {
- attackTarget->targetId = attackerAtk->meleeLockTargetId;
- attackTarget->shouldChase = false;
- }
- }
- }
- float range = 2.0f;
- int damage = 10;
- float cooldown = 1.0f;
- float *tAccum = nullptr;
- float tmpAccum = 0.0f;
- if (attackerAtk) {
- updateCombatMode(attacker, world, attackerAtk);
- range = attackerAtk->getCurrentRange();
- damage = attackerAtk->getCurrentDamage();
- cooldown = attackerAtk->getCurrentCooldown();
- auto *holdMode =
- attacker->getComponent<Engine::Core::HoldModeComponent>();
- if (holdMode && holdMode->active) {
- if (attackerUnit->spawnType == Game::Units::SpawnType::Archer) {
- range *= 1.5f;
- damage = static_cast<int>(damage * 1.3f);
- } else if (attackerUnit->spawnType ==
- Game::Units::SpawnType::Spearman) {
- damage = static_cast<int>(damage * 1.4f);
- }
- }
- attackerAtk->timeSinceLast += deltaTime;
- tAccum = &attackerAtk->timeSinceLast;
- } else {
- tmpAccum += deltaTime;
- tAccum = &tmpAccum;
- }
- if (*tAccum < cooldown) {
- continue;
- }
- auto *attackTarget =
- attacker->getComponent<Engine::Core::AttackTargetComponent>();
- Engine::Core::Entity *bestTarget = nullptr;
- if (attackTarget && attackTarget->targetId != 0) {
- auto *target = world->getEntity(attackTarget->targetId);
- if (target &&
- !target->hasComponent<Engine::Core::PendingRemovalComponent>()) {
- auto *targetUnit = target->getComponent<Engine::Core::UnitComponent>();
- auto &ownerRegistry = Game::Systems::OwnerRegistry::instance();
- bool isAlly =
- ownerRegistry.areAllies(attackerUnit->ownerId, targetUnit->ownerId);
- if (targetUnit && targetUnit->health > 0 &&
- targetUnit->ownerId != attackerUnit->ownerId && !isAlly) {
- if (isInRange(attacker, target, range)) {
- bestTarget = target;
- bool isRangedUnit = false;
- if (attackerAtk && attackerAtk->canRanged &&
- attackerAtk->currentMode ==
- Engine::Core::AttackComponent::CombatMode::Ranged) {
- isRangedUnit = true;
- }
- if (isRangedUnit) {
- auto *movement =
- attacker->getComponent<Engine::Core::MovementComponent>();
- if (movement && movement->hasTarget) {
- movement->hasTarget = false;
- movement->vx = 0.0f;
- movement->vz = 0.0f;
- movement->path.clear();
- if (attackerTransform) {
- movement->targetX = attackerTransform->position.x;
- movement->targetY = attackerTransform->position.z;
- movement->goalX = attackerTransform->position.x;
- movement->goalY = attackerTransform->position.z;
- }
- }
- }
- if (auto *attT =
- attacker
- ->getComponent<Engine::Core::TransformComponent>()) {
- if (auto *tgtT =
- target
- ->getComponent<Engine::Core::TransformComponent>()) {
- float dx = tgtT->position.x - attT->position.x;
- float dz = tgtT->position.z - attT->position.z;
- float yaw = std::atan2(dx, dz) * 180.0f / 3.14159265f;
- attT->desiredYaw = yaw;
- attT->hasDesiredYaw = true;
- }
- }
- } else if (attackTarget->shouldChase) {
- auto *holdMode =
- attacker->getComponent<Engine::Core::HoldModeComponent>();
- if (holdMode && holdMode->active) {
- if (!isInRange(attacker, target, range)) {
- attacker
- ->removeComponent<Engine::Core::AttackTargetComponent>();
- }
- continue;
- }
- bool isRangedUnit = false;
- if (attackerAtk && attackerAtk->canRanged &&
- attackerAtk->currentMode ==
- Engine::Core::AttackComponent::CombatMode::Ranged) {
- isRangedUnit = true;
- }
- bool currentlyInRange = isInRange(attacker, target, range);
- if (isRangedUnit && currentlyInRange) {
- auto *movement =
- attacker->getComponent<Engine::Core::MovementComponent>();
- if (movement) {
- movement->hasTarget = false;
- movement->vx = 0.0f;
- movement->vz = 0.0f;
- movement->path.clear();
- auto *attackerTransformComponent =
- attacker->getComponent<Engine::Core::TransformComponent>();
- if (attackerTransformComponent) {
- movement->targetX = attackerTransformComponent->position.x;
- movement->targetY = attackerTransformComponent->position.z;
- movement->goalX = attackerTransformComponent->position.x;
- movement->goalY = attackerTransformComponent->position.z;
- }
- }
- bestTarget = target;
- } else {
- auto *targetTransform =
- target->getComponent<Engine::Core::TransformComponent>();
- auto *attackerTransformComponent =
- attacker->getComponent<Engine::Core::TransformComponent>();
- if (targetTransform && attackerTransformComponent) {
- QVector3D attackerPos(attackerTransformComponent->position.x,
- 0.0f,
- attackerTransformComponent->position.z);
- QVector3D targetPos(targetTransform->position.x, 0.0f,
- targetTransform->position.z);
- QVector3D desiredPos = targetPos;
- bool holdPosition = false;
- bool targetIsBuilding =
- target->hasComponent<Engine::Core::BuildingComponent>();
- if (targetIsBuilding) {
- float scaleX = targetTransform->scale.x;
- float scaleZ = targetTransform->scale.z;
- float targetRadius = std::max(scaleX, scaleZ) * 0.5f;
- QVector3D direction = targetPos - attackerPos;
- float distanceSq = direction.lengthSquared();
- if (distanceSq > 0.000001f) {
- float distance = std::sqrt(distanceSq);
- direction /= distance;
- float desiredDistance =
- targetRadius + std::max(range - 0.2f, 0.2f);
- if (distance > desiredDistance + 0.15f) {
- desiredPos = targetPos - direction * desiredDistance;
- } else {
- holdPosition = true;
- }
- }
- } else if (isRangedUnit) {
- QVector3D direction = targetPos - attackerPos;
- float distanceSq = direction.lengthSquared();
- if (distanceSq > 0.000001f) {
- float distance = std::sqrt(distanceSq);
- direction /= distance;
- float optimalRange = range * 0.85f;
- if (distance > optimalRange + 0.5f) {
- desiredPos = targetPos - direction * optimalRange;
- } else {
- holdPosition = true;
- }
- }
- }
- auto *movement =
- attacker->getComponent<Engine::Core::MovementComponent>();
- if (!movement) {
- movement =
- attacker->addComponent<Engine::Core::MovementComponent>();
- }
- if (movement) {
- if (holdPosition) {
- movement->hasTarget = false;
- movement->vx = 0.0f;
- movement->vz = 0.0f;
- movement->path.clear();
- if (attackerTransformComponent) {
- movement->targetX =
- attackerTransformComponent->position.x;
- movement->targetY =
- attackerTransformComponent->position.z;
- movement->goalX = attackerTransformComponent->position.x;
- movement->goalY = attackerTransformComponent->position.z;
- }
- } else {
- QVector3D plannedTarget(movement->targetX, 0.0f,
- movement->targetY);
- if (!movement->path.empty()) {
- const auto &finalNode = movement->path.back();
- plannedTarget =
- QVector3D(finalNode.first, 0.0f, finalNode.second);
- }
- float diffSq = (plannedTarget - desiredPos).lengthSquared();
- bool needNewCommand = !movement->pathPending;
- if (movement->hasTarget && diffSq <= 0.25f * 0.25f) {
- needNewCommand = false;
- }
- if (needNewCommand) {
- CommandService::MoveOptions options;
- options.clearAttackIntent = false;
- options.allowDirectFallback = true;
- std::vector<Engine::Core::EntityID> unitIds = {
- attacker->getId()};
- std::vector<QVector3D> moveTargets = {desiredPos};
- CommandService::moveUnits(*world, unitIds, moveTargets,
- options);
- }
- }
- }
- }
- if (isInRange(attacker, target, range)) {
- bestTarget = target;
- }
- }
- } else {
- attacker->removeComponent<Engine::Core::AttackTargetComponent>();
- }
- } else {
- attacker->removeComponent<Engine::Core::AttackTargetComponent>();
- }
- } else {
- attacker->removeComponent<Engine::Core::AttackTargetComponent>();
- }
- }
- if (!bestTarget && !attackTarget) {
- auto &ownerRegistry = Game::Systems::OwnerRegistry::instance();
- for (auto target : units) {
- if (target == attacker) {
- continue;
- }
- auto targetUnit = target->getComponent<Engine::Core::UnitComponent>();
- if (!targetUnit || targetUnit->health <= 0) {
- continue;
- }
- if (targetUnit->ownerId == attackerUnit->ownerId) {
- continue;
- }
- if (ownerRegistry.areAllies(attackerUnit->ownerId,
- targetUnit->ownerId)) {
- continue;
- }
- if (target->hasComponent<Engine::Core::BuildingComponent>()) {
- continue;
- }
- if (isInRange(attacker, target, range)) {
- bestTarget = target;
- break;
- }
- }
- }
- if (bestTarget) {
- auto *bestTargetUnit =
- bestTarget->getComponent<Engine::Core::UnitComponent>();
- if (!attacker->hasComponent<Engine::Core::AttackTargetComponent>()) {
- auto *newTarget =
- attacker->addComponent<Engine::Core::AttackTargetComponent>();
- newTarget->targetId = bestTarget->getId();
- newTarget->shouldChase = false;
- } else {
- auto *existingTarget =
- attacker->getComponent<Engine::Core::AttackTargetComponent>();
- if (existingTarget->targetId != bestTarget->getId()) {
- existingTarget->targetId = bestTarget->getId();
- existingTarget->shouldChase = false;
- }
- }
- bool isRangedUnit = false;
- if (attackerAtk && attackerAtk->canRanged &&
- attackerAtk->currentMode ==
- Engine::Core::AttackComponent::CombatMode::Ranged) {
- isRangedUnit = true;
- }
- if (isRangedUnit) {
- auto *movement =
- attacker->getComponent<Engine::Core::MovementComponent>();
- if (movement && movement->hasTarget) {
- movement->hasTarget = false;
- movement->vx = 0.0f;
- movement->vz = 0.0f;
- movement->path.clear();
- if (attackerTransform) {
- movement->targetX = attackerTransform->position.x;
- movement->targetY = attackerTransform->position.z;
- movement->goalX = attackerTransform->position.x;
- movement->goalY = attackerTransform->position.z;
- }
- }
- }
- if (auto *attT =
- attacker->getComponent<Engine::Core::TransformComponent>()) {
- if (auto *tgtT =
- bestTarget->getComponent<Engine::Core::TransformComponent>()) {
- float dx = tgtT->position.x - attT->position.x;
- float dz = tgtT->position.z - attT->position.z;
- float yaw = std::atan2(dx, dz) * 180.0f / 3.14159265f;
- attT->desiredYaw = yaw;
- attT->hasDesiredYaw = true;
- }
- }
- if (arrowSys) {
- auto attT = attacker->getComponent<Engine::Core::TransformComponent>();
- auto tgtT =
- bestTarget->getComponent<Engine::Core::TransformComponent>();
- auto attU = attacker->getComponent<Engine::Core::UnitComponent>();
- if (!attackerAtk ||
- attackerAtk->currentMode !=
- Engine::Core::AttackComponent::CombatMode::Melee) {
- QVector3D aPos(attT->position.x, attT->position.y, attT->position.z);
- QVector3D tPos(tgtT->position.x, tgtT->position.y, tgtT->position.z);
- QVector3D dir = (tPos - aPos).normalized();
- QVector3D color =
- attU ? Game::Visuals::teamColorForOwner(attU->ownerId)
- : QVector3D(0.8f, 0.9f, 1.0f);
- int arrowCount = 1;
- if (attU) {
- int troopSize =
- Game::Units::TroopConfig::instance().getIndividualsPerUnit(
- attU->spawnType);
- int maxArrows = std::max(1, troopSize / 3);
- static thread_local std::mt19937 gen(std::random_device{}());
- std::uniform_int_distribution<> dist(1, maxArrows);
- arrowCount = dist(gen);
- }
- for (int i = 0; i < arrowCount; ++i) {
- static thread_local std::mt19937 spreadGen(std::random_device{}());
- std::uniform_real_distribution<float> spreadDist(-0.15f, 0.15f);
- QVector3D perpendicular(-dir.z(), 0.0f, dir.x());
- QVector3D upVector(0.0f, 1.0f, 0.0f);
- float lateralOffset = spreadDist(spreadGen);
- float verticalOffset = spreadDist(spreadGen) * 1.5f;
- float depthOffset = spreadDist(spreadGen) * 1.3f;
- QVector3D startOffset =
- perpendicular * lateralOffset + upVector * verticalOffset;
- QVector3D endOffset = perpendicular * lateralOffset +
- upVector * verticalOffset + dir * depthOffset;
- QVector3D start =
- aPos + QVector3D(0.0f, 0.6f, 0.0f) + dir * 0.35f + startOffset;
- QVector3D end = tPos + QVector3D(0.5f, 0.5f, 0.0f) + endOffset;
- arrowSys->spawnArrow(start, end, color, 14.0f);
- }
- }
- }
- if (attackerAtk && attackerAtk->currentMode ==
- Engine::Core::AttackComponent::CombatMode::Melee) {
- attackerAtk->inMeleeLock = true;
- attackerAtk->meleeLockTargetId = bestTarget->getId();
- auto *targetAtk =
- bestTarget->getComponent<Engine::Core::AttackComponent>();
- if (targetAtk) {
- targetAtk->inMeleeLock = true;
- targetAtk->meleeLockTargetId = attacker->getId();
- }
- auto *attT = attacker->getComponent<Engine::Core::TransformComponent>();
- auto *tgtT =
- bestTarget->getComponent<Engine::Core::TransformComponent>();
- if (attT && tgtT) {
- float dx = tgtT->position.x - attT->position.x;
- float dz = tgtT->position.z - attT->position.z;
- float dist = std::sqrt(dx * dx + dz * dz);
- const float IDEAL_MELEE_DISTANCE = 0.6f;
- if (dist > IDEAL_MELEE_DISTANCE + 0.1f) {
- float moveAmount = (dist - IDEAL_MELEE_DISTANCE) * 0.5f;
- if (dist > 0.001f) {
- QVector3D direction(dx / dist, 0.0f, dz / dist);
- attT->position.x += direction.x() * moveAmount;
- attT->position.z += direction.z() * moveAmount;
- tgtT->position.x -= direction.x() * moveAmount;
- tgtT->position.z -= direction.z() * moveAmount;
- }
- }
- }
- }
- dealDamage(world, bestTarget, damage, attacker->getId());
- *tAccum = 0.0f;
- } else {
- if (!attackTarget &&
- attacker->hasComponent<Engine::Core::AttackTargetComponent>()) {
- attacker->removeComponent<Engine::Core::AttackTargetComponent>();
- }
- }
- }
- }
- bool CombatSystem::isInRange(Engine::Core::Entity *attacker,
- Engine::Core::Entity *target, float range) {
- auto attackerTransform =
- attacker->getComponent<Engine::Core::TransformComponent>();
- auto targetTransform =
- target->getComponent<Engine::Core::TransformComponent>();
- if (!attackerTransform || !targetTransform) {
- return false;
- }
- float dx = targetTransform->position.x - attackerTransform->position.x;
- float dz = targetTransform->position.z - attackerTransform->position.z;
- float dy = targetTransform->position.y - attackerTransform->position.y;
- float distanceSquared = dx * dx + dz * dz;
- float targetRadius = 0.0f;
- if (target->hasComponent<Engine::Core::BuildingComponent>()) {
- float scaleX = targetTransform->scale.x;
- float scaleZ = targetTransform->scale.z;
- targetRadius = std::max(scaleX, scaleZ) * 0.5f;
- } else {
- float scaleX = targetTransform->scale.x;
- float scaleZ = targetTransform->scale.z;
- targetRadius = std::max(scaleX, scaleZ) * 0.5f;
- }
- float effectiveRange = range + targetRadius;
- if (distanceSquared > effectiveRange * effectiveRange) {
- return false;
- }
- auto attackerAtk = attacker->getComponent<Engine::Core::AttackComponent>();
- if (attackerAtk && attackerAtk->currentMode ==
- Engine::Core::AttackComponent::CombatMode::Melee) {
- float heightDiff = std::abs(dy);
- if (heightDiff > attackerAtk->maxHeightDifference) {
- return false;
- }
- }
- return true;
- }
- void CombatSystem::dealDamage(Engine::Core::World *world,
- Engine::Core::Entity *target, int damage,
- Engine::Core::EntityID attackerId) {
- auto unit = target->getComponent<Engine::Core::UnitComponent>();
- if (unit) {
- unit->health = std::max(0, unit->health - damage);
- int attackerOwnerId = 0;
- if (attackerId != 0 && world) {
- auto *attacker = world->getEntity(attackerId);
- if (attacker) {
- auto *attackerUnit =
- attacker->getComponent<Engine::Core::UnitComponent>();
- if (attackerUnit) {
- attackerOwnerId = attackerUnit->ownerId;
- }
- }
- }
- if (target->hasComponent<Engine::Core::BuildingComponent>() &&
- unit->health > 0) {
- Engine::Core::EventManager::instance().publish(
- Engine::Core::BuildingAttackedEvent(target->getId(), unit->ownerId,
- unit->spawnType, attackerId,
- attackerOwnerId, damage));
- }
- if (unit->health <= 0) {
- int killerOwnerId = attackerOwnerId;
- Engine::Core::EventManager::instance().publish(
- Engine::Core::UnitDiedEvent(target->getId(), unit->ownerId,
- unit->spawnType, attackerId,
- killerOwnerId));
- auto *targetAtk = target->getComponent<Engine::Core::AttackComponent>();
- if (targetAtk && targetAtk->inMeleeLock &&
- targetAtk->meleeLockTargetId != 0) {
- if (world) {
- auto *lockPartner = world->getEntity(targetAtk->meleeLockTargetId);
- if (lockPartner &&
- !lockPartner
- ->hasComponent<Engine::Core::PendingRemovalComponent>()) {
- auto *partnerAtk =
- lockPartner->getComponent<Engine::Core::AttackComponent>();
- if (partnerAtk &&
- partnerAtk->meleeLockTargetId == target->getId()) {
- partnerAtk->inMeleeLock = false;
- partnerAtk->meleeLockTargetId = 0;
- }
- }
- }
- }
- if (target->hasComponent<Engine::Core::BuildingComponent>()) {
- BuildingCollisionRegistry::instance().unregisterBuilding(
- target->getId());
- }
- if (auto *r = target->getComponent<Engine::Core::RenderableComponent>()) {
- r->visible = false;
- }
- if (auto *movement =
- target->getComponent<Engine::Core::MovementComponent>()) {
- movement->hasTarget = false;
- movement->vx = 0.0f;
- movement->vz = 0.0f;
- movement->path.clear();
- movement->pathPending = false;
- }
- target->addComponent<Engine::Core::PendingRemovalComponent>();
- }
- }
- }
- void CombatSystem::updateCombatMode(Engine::Core::Entity *attacker,
- Engine::Core::World *world,
- Engine::Core::AttackComponent *attackComp) {
- if (!attackComp) {
- return;
- }
- if (attackComp->preferredMode !=
- Engine::Core::AttackComponent::CombatMode::Auto) {
- attackComp->currentMode = attackComp->preferredMode;
- return;
- }
- auto attackerTransform =
- attacker->getComponent<Engine::Core::TransformComponent>();
- if (!attackerTransform) {
- return;
- }
- auto attackerUnit = attacker->getComponent<Engine::Core::UnitComponent>();
- if (!attackerUnit) {
- return;
- }
- auto &ownerRegistry = Game::Systems::OwnerRegistry::instance();
- auto units = world->getEntitiesWith<Engine::Core::UnitComponent>();
- float closestEnemyDistSq = std::numeric_limits<float>::max();
- float closestHeightDiff = 0.0f;
- for (auto target : units) {
- if (target == attacker) {
- continue;
- }
- auto targetUnit = target->getComponent<Engine::Core::UnitComponent>();
- if (!targetUnit || targetUnit->health <= 0) {
- continue;
- }
- if (ownerRegistry.areAllies(attackerUnit->ownerId, targetUnit->ownerId)) {
- continue;
- }
- auto targetTransform =
- target->getComponent<Engine::Core::TransformComponent>();
- if (!targetTransform) {
- continue;
- }
- float dx = targetTransform->position.x - attackerTransform->position.x;
- float dz = targetTransform->position.z - attackerTransform->position.z;
- float dy = targetTransform->position.y - attackerTransform->position.y;
- float distSq = dx * dx + dz * dz;
- if (distSq < closestEnemyDistSq) {
- closestEnemyDistSq = distSq;
- closestHeightDiff = std::abs(dy);
- }
- }
- if (closestEnemyDistSq == std::numeric_limits<float>::max()) {
- if (attackComp->canRanged) {
- attackComp->currentMode =
- Engine::Core::AttackComponent::CombatMode::Ranged;
- } else {
- attackComp->currentMode =
- Engine::Core::AttackComponent::CombatMode::Melee;
- }
- return;
- }
- float closestDist = std::sqrt(closestEnemyDistSq);
- bool inMeleeRange =
- attackComp->isInMeleeRange(closestDist, closestHeightDiff);
- bool inRangedRange = attackComp->isInRangedRange(closestDist);
- if (inMeleeRange && attackComp->canMelee) {
- attackComp->currentMode = Engine::Core::AttackComponent::CombatMode::Melee;
- } else if (inRangedRange && attackComp->canRanged) {
- attackComp->currentMode = Engine::Core::AttackComponent::CombatMode::Ranged;
- } else if (attackComp->canRanged) {
- attackComp->currentMode = Engine::Core::AttackComponent::CombatMode::Ranged;
- } else {
- attackComp->currentMode = Engine::Core::AttackComponent::CombatMode::Melee;
- }
- }
- void CombatSystem::processAutoEngagement(Engine::Core::World *world,
- float deltaTime) {
- auto units = world->getEntitiesWith<Engine::Core::UnitComponent>();
- for (auto it = m_engagementCooldowns.begin();
- it != m_engagementCooldowns.end();) {
- it->second -= deltaTime;
- if (it->second <= 0.0f) {
- it = m_engagementCooldowns.erase(it);
- } else {
- ++it;
- }
- }
- for (auto unit : units) {
- if (unit->hasComponent<Engine::Core::PendingRemovalComponent>()) {
- continue;
- }
- auto unitComp = unit->getComponent<Engine::Core::UnitComponent>();
- if (!unitComp || unitComp->health <= 0) {
- continue;
- }
- if (unit->hasComponent<Engine::Core::BuildingComponent>()) {
- continue;
- }
- auto attackComp = unit->getComponent<Engine::Core::AttackComponent>();
- if (!attackComp || !attackComp->canMelee) {
- continue;
- }
- if (attackComp->canRanged &&
- attackComp->preferredMode !=
- Engine::Core::AttackComponent::CombatMode::Melee) {
- continue;
- }
- if (m_engagementCooldowns.find(unit->getId()) !=
- m_engagementCooldowns.end()) {
- continue;
- }
- if (!isUnitIdle(unit)) {
- continue;
- }
- float visionRange = unitComp->visionRange;
- auto *nearestEnemy = findNearestEnemy(unit, world, visionRange);
- if (nearestEnemy) {
- auto *attackTarget =
- unit->getComponent<Engine::Core::AttackTargetComponent>();
- if (!attackTarget) {
- attackTarget =
- unit->addComponent<Engine::Core::AttackTargetComponent>();
- }
- if (attackTarget) {
- attackTarget->targetId = nearestEnemy->getId();
- attackTarget->shouldChase = true;
- m_engagementCooldowns[unit->getId()] = ENGAGEMENT_COOLDOWN;
- }
- }
- }
- }
- bool CombatSystem::isUnitIdle(Engine::Core::Entity *unit) {
- auto *holdMode = unit->getComponent<Engine::Core::HoldModeComponent>();
- if (holdMode && holdMode->active) {
- return false;
- }
- auto *attackTarget =
- unit->getComponent<Engine::Core::AttackTargetComponent>();
- if (attackTarget && attackTarget->targetId != 0) {
- return false;
- }
- auto *movement = unit->getComponent<Engine::Core::MovementComponent>();
- if (movement && movement->hasTarget) {
- return false;
- }
- auto *attackComp = unit->getComponent<Engine::Core::AttackComponent>();
- if (attackComp && attackComp->inMeleeLock) {
- return false;
- }
- auto *patrol = unit->getComponent<Engine::Core::PatrolComponent>();
- if (patrol && patrol->patrolling) {
- return false;
- }
- return true;
- }
- Engine::Core::Entity *CombatSystem::findNearestEnemy(Engine::Core::Entity *unit,
- Engine::Core::World *world,
- float maxRange) {
- auto unitComp = unit->getComponent<Engine::Core::UnitComponent>();
- auto unitTransform = unit->getComponent<Engine::Core::TransformComponent>();
- if (!unitComp || !unitTransform) {
- return nullptr;
- }
- auto &ownerRegistry = Game::Systems::OwnerRegistry::instance();
- auto units = world->getEntitiesWith<Engine::Core::UnitComponent>();
- Engine::Core::Entity *nearestEnemy = nullptr;
- float nearestDistSq = maxRange * maxRange;
- for (auto target : units) {
- if (target == unit) {
- continue;
- }
- if (target->hasComponent<Engine::Core::PendingRemovalComponent>()) {
- continue;
- }
- auto targetUnit = target->getComponent<Engine::Core::UnitComponent>();
- if (!targetUnit || targetUnit->health <= 0) {
- continue;
- }
- if (targetUnit->ownerId == unitComp->ownerId) {
- continue;
- }
- if (ownerRegistry.areAllies(unitComp->ownerId, targetUnit->ownerId)) {
- continue;
- }
- if (target->hasComponent<Engine::Core::BuildingComponent>()) {
- continue;
- }
- auto targetTransform =
- target->getComponent<Engine::Core::TransformComponent>();
- if (!targetTransform) {
- continue;
- }
- float dx = targetTransform->position.x - unitTransform->position.x;
- float dz = targetTransform->position.z - unitTransform->position.z;
- float distSq = dx * dx + dz * dz;
- if (distSq < nearestDistSq) {
- nearestDistSq = distSq;
- nearestEnemy = target;
- }
- }
- return nearestEnemy;
- }
- } // namespace Game::Systems
|