|
| 1 | +#include "Body.h" |
| 2 | + |
| 3 | +AITriggerTypeExt::ExtContainer AITriggerTypeExt::ExtMap; |
| 4 | + |
| 5 | +// ============================= |
| 6 | +// load / save |
| 7 | + |
| 8 | +void AITriggerTypeExt::ExtData::LoadFromStream(PhobosStreamReader& Stm) |
| 9 | +{ |
| 10 | + // Nothing yet |
| 11 | +} |
| 12 | + |
| 13 | +void AITriggerTypeExt::ExtData::SaveToStream(PhobosStreamWriter& Stm) |
| 14 | +{ |
| 15 | + // Nothing yet |
| 16 | +} |
| 17 | + |
| 18 | +// container |
| 19 | + |
| 20 | +AITriggerTypeExt::ExtContainer::ExtContainer() : Container("AITriggerTypeClass") |
| 21 | +{ } |
| 22 | + |
| 23 | +AITriggerTypeExt::ExtContainer::~ExtContainer() = default; |
| 24 | + |
| 25 | +int AITriggerTypeExt::CheckConditions(AITriggerTypeClass* pThis, HouseClass* pOwner, HouseClass* pEnemy) |
| 26 | +{ |
| 27 | + int condition = (int)pThis->ConditionType; |
| 28 | + |
| 29 | + if (condition < -1) // Invalid, bail out early. |
| 30 | + return 0; |
| 31 | + else if (condition < (int)PhobosAITriggerConditions::NumberOfTechBuildingsExist) // Not Phobos condition |
| 32 | + return -1; |
| 33 | + |
| 34 | + bool success = false; |
| 35 | + |
| 36 | + switch ((PhobosAITriggerConditions)condition) |
| 37 | + { |
| 38 | + case PhobosAITriggerConditions::NumberOfTechBuildingsExist: |
| 39 | + success = AITriggerTypeExt::NumberOfTechBuildingsExist(pThis, pOwner); |
| 40 | + break; |
| 41 | + case PhobosAITriggerConditions::NumberOfBridgeRepairHutsExist: |
| 42 | + success = AITriggerTypeExt::NumberOfBridgeRepairHutsExist(pThis); |
| 43 | + break; |
| 44 | + } |
| 45 | + |
| 46 | + return success; |
| 47 | +} |
| 48 | + |
| 49 | +bool AITriggerTypeExt::GetComparatorResult(int operand1, AITriggerConditionComparatorType operatorType, int operand2) |
| 50 | +{ |
| 51 | + switch (operatorType) |
| 52 | + { |
| 53 | + case AITriggerConditionComparatorType::Less: |
| 54 | + return operand1 < operand2; |
| 55 | + break; |
| 56 | + case AITriggerConditionComparatorType::LessOrEqual: |
| 57 | + return operand1 <= operand2; |
| 58 | + break; |
| 59 | + case AITriggerConditionComparatorType::Equal: |
| 60 | + return operand1 == operand2; |
| 61 | + break; |
| 62 | + case AITriggerConditionComparatorType::GreaterOrEqual: |
| 63 | + return operand1 >= operand2; |
| 64 | + break; |
| 65 | + case AITriggerConditionComparatorType::Greater: |
| 66 | + return operand1 > operand2; |
| 67 | + break; |
| 68 | + case AITriggerConditionComparatorType::NotEqual: |
| 69 | + return operand1 != operand2; |
| 70 | + break; |
| 71 | + default: |
| 72 | + return false; |
| 73 | + break; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +bool AITriggerTypeExt::NumberOfTechBuildingsExist(AITriggerTypeClass* pThis, HouseClass* pOwner) |
| 78 | +{ |
| 79 | + int count = 0; |
| 80 | + |
| 81 | + for (auto const pHouse : HouseClass::Array) |
| 82 | + { |
| 83 | + if (pHouse->IsAlliedWith(pOwner)) |
| 84 | + continue; |
| 85 | + |
| 86 | + // Could possibly be optimized with bespoke tracking but |
| 87 | + // it didn't seem to make much of a difference in testing. |
| 88 | + for (auto const pBuilding : pHouse->Buildings) |
| 89 | + { |
| 90 | + if (!pBuilding->IsAlive || pBuilding->InLimbo) |
| 91 | + continue; |
| 92 | + |
| 93 | + auto const pType = pBuilding->Type; |
| 94 | + |
| 95 | + if (pType->NeedsEngineer && pType->Capturable) |
| 96 | + count++; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return AITriggerTypeExt::GetComparatorResult(count, pThis->Conditions[0].ComparatorType, pThis->Conditions[0].ComparatorOperand); |
| 101 | +} |
| 102 | + |
| 103 | +bool AITriggerTypeExt::NumberOfBridgeRepairHutsExist(AITriggerTypeClass* pThis) |
| 104 | +{ |
| 105 | + int count = 0; |
| 106 | + auto const pHouse = HouseClass::FindCivilianSide(); |
| 107 | + |
| 108 | + for (auto const pBuilding : pHouse->Buildings) |
| 109 | + { |
| 110 | + if (!pBuilding->IsAlive || pBuilding->InLimbo) |
| 111 | + continue; |
| 112 | + |
| 113 | + auto const pType = pBuilding->Type; |
| 114 | + |
| 115 | + if (pType->BridgeRepairHut && MapClass::Instance.IsLinkedBridgeDestroyed(pBuilding->GetMapCoords())) |
| 116 | + count++; |
| 117 | + } |
| 118 | + |
| 119 | + return AITriggerTypeExt::GetComparatorResult(count, pThis->Conditions[0].ComparatorType, pThis->Conditions[0].ComparatorOperand); |
| 120 | +} |
0 commit comments