| 50 | } |
| 51 | |
| 52 | bool AI::runDecisionTree(Unit* unit) { |
| 53 | PROFILE("AI"_slice); |
| 54 | if (unit->getBehaviorTree()) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | Leaf* decisionTree = unit->getDecisionTree(); |
| 59 | if (!decisionTree) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | _self = unit; |
| 64 | |
| 65 | _nearestUnit = nullptr; |
| 66 | _nearestFriend = nullptr; |
| 67 | _nearestEnemy = nullptr; |
| 68 | _nearestNeutral = nullptr; |
| 69 | |
| 70 | float minUnitDistance = 0; |
| 71 | float minFriendDistance = 0; |
| 72 | float minEnemyDistance = 0; |
| 73 | float minNeutralDistance = 0; |
| 74 | |
| 75 | Sensor* detectSensor = unit->getDetectSensor(); |
| 76 | if (detectSensor) { |
| 77 | ARRAY_START(Body, body, detectSensor->getSensedBodies()) { |
| 78 | Unit* aroundUnit = DoraAs<Unit>(body->getOwner()); |
| 79 | if (!aroundUnit) continue; |
| 80 | |
| 81 | _detectedUnits->add(Value::alloc(aroundUnit)); |
| 82 | |
| 83 | float newDistance = unit->getPosition().distanceSquared(aroundUnit->getPosition()); |
| 84 | |
| 85 | if (!_nearestUnit || newDistance < minUnitDistance) { |
| 86 | minUnitDistance = newDistance; |
| 87 | _nearestUnit = aroundUnit; |
| 88 | } |
| 89 | Relation relation = SharedData.getRelation(_self, aroundUnit); |
| 90 | switch (relation) { |
| 91 | case Relation::Friend: |
| 92 | _friends->add(Value::alloc(aroundUnit)); |
| 93 | if (!_nearestFriend || newDistance < minFriendDistance) { |
| 94 | minFriendDistance = newDistance; |
| 95 | _nearestFriend = aroundUnit; |
| 96 | } |
| 97 | break; |
| 98 | case Relation::Enemy: |
| 99 | _enemies->add(Value::alloc(aroundUnit)); |
| 100 | if (!_nearestEnemy || newDistance < minEnemyDistance) { |
| 101 | minEnemyDistance = newDistance; |
| 102 | _nearestEnemy = aroundUnit; |
| 103 | } |
| 104 | break; |
| 105 | case Relation::Neutral: |
| 106 | _neutrals->add(Value::alloc(aroundUnit)); |
| 107 | if (!_nearestNeutral || newDistance < minNeutralDistance) { |
| 108 | minNeutralDistance = newDistance; |
| 109 | _nearestNeutral = aroundUnit; |
no test coverage detected