protected virtual bool FindNearestTarget() { bool[] aiInRange = new bool[0];// TODO: save this value on unit. and only run updates every few frames if (AnyAiInRange(transform.position, detectionRange, ref aiInRange)) { Ai targetAi = GlobalAi.ChoseNearest(transform, ai.alliance, "!", aiInRange); if (targetAi) { this.target = targetAi.transform; return(true); } } return(false); }
public override NodeStatus Tick() { //if (!data.ContainsKey("target") || (data.ContainsKey("target") && (Transform)data["target"] == null)) { Ai objective = GlobalAi.ChoseNearest((Transform)data["transform"], (string)data["alliance"], "!", data.ContainsKey("filtered") == false ? null : (bool[])data["filtered"]); //GlobalAi.ChoseTargetByPriorityImportance((string)data["alliance"], "!", data.ContainsKey("filtered") == false ? null : (bool[])data["filtered"]); data["target"] = null; if (objective != null) { data["target"] = objective.transform; } //} if (((Transform)data["target"]) != null) { data["targetPos"] = ((Transform)data["target"]).position; } return(data["target"] == null ? NodeStatus.FAILURE : NodeStatus.SUCCESS); }
/// <summary> /// /// </summary> /// <param name="transform"></param> /// <param name="alliance"></param> /// <param name="operation">! skip allied, = only allied</param> /// <param name="filteredAi"></param> /// <returns></returns> internal static Ai ChoseNearest(Transform transform, string alliance, string operation = "*", bool[] filteredAi = null) { int best = -1; GlobalAi.NullClear(); for (int i = 0; i < allAi.Count; i++) { if (filteredAi != null && filteredAi[i] == false) { continue; } bool passFilter = true; if (operation == "!") { passFilter = alliance != allAi[i].alliance; // filter by alliances } else if (operation == "=") { passFilter = alliance == allAi[i].alliance; // filter by alliances } if (passFilter) { try { // if (allAi[i] == null) { allAi = allAi.NullCleared(); } if (best == -1) { best = i; continue; } if (Vector3.SqrMagnitude(allAi[i].transform.position - transform.position) < Vector3.SqrMagnitude(allAi[best].transform.position - transform.position)) { best = i; } } catch (MissingReferenceException) { Debug.LogError(allAi[i] + " " + allAi[best] + " " + transform); throw; } } } return(best == -1 ? null : allAi[best]); }