CoverData FindAdvancingCover(Vector3 targetTransformPos, Transform transformToDefend) { var i = 0; var myPos = myTransform.position; CoverNode currentCoverNodeScript = null; //Will find closest cover that is nearer than the last one we have if possible. //If not, we'll move to the target. Vector3 posToAdvanceTo; posToAdvanceTo = transformToDefend ? transformToDefend.position : targetTransformPos; var distBetweenMeAndTarget = Vector3.SqrMagnitude(myPos - posToAdvanceTo) - minDistToAdvance; var closestDistBetweenMeAndCover = distBetweenMeAndTarget; for (i = 0; i < coverNodes.Length; i++) { if (!coverNodes[i].isOccupied()) { float sqrDistBetweenNodeAndTargetPos = Vector3.SqrMagnitude(coverNodes[i].GetPosition() - posToAdvanceTo); //Check if we'll be closer to target than we stand now if (sqrDistBetweenNodeAndTargetPos < distBetweenMeAndTarget) { //Check if this node is closest to us if (Vector3.SqrMagnitude(coverNodes[i].GetPosition() - myPos) < closestDistBetweenMeAndCover) { //Check if node is safe if (coverNodes[i].ValidCoverCheck(targetTransformPos)) { closestDistBetweenMeAndCover = sqrDistBetweenNodeAndTargetPos; currentCoverNodeScript = coverNodes[i]; } } } } } if (currentCoverNodeScript != null) { lastCoverPos = currentCoverNodeScript.GetPosition(); return(new CoverData(true, currentCoverNodeScript.GetPosition(), currentCoverNodeScript.GetSightNodePosition(), false, currentCoverNodeScript)); } //Dynamic advancing cover is NOT supported return(new CoverData()); }
CoverData FindRandomCover(Vector3 targetTransformPos, Transform transformToDefend) { var i = 0; CoverNode currentCoverNodeScript = null; var availableCoverNodeScripts = new List <CoverNode>(); //Fill a list with potential nodes for (i = 0; i < coverNodes.Length; i++) { if (!coverNodes[i].isOccupied()) { if (coverNodes[i].ValidCoverCheck(targetTransformPos) && (!transformToDefend || Vector3.SqrMagnitude(coverNodes[i].GetPosition() - transformToDefend.position) < defendingDistSquared)) { availableCoverNodeScripts.Add(coverNodes[i]); } } } if (availableCoverNodeScripts.Count > 0) { //Pick a random node currentCoverNodeScript = availableCoverNodeScripts[Random.Range(0, availableCoverNodeScripts.Count)]; lastCoverPos = currentCoverNodeScript.GetPosition(); return(new CoverData(true, currentCoverNodeScript.GetPosition(), currentCoverNodeScript.GetSightNodePosition(), false, currentCoverNodeScript)); } //Only bother with dynamic cover if we need it if (shouldUseDynamicCover && !AIController.Instance.usePerformanceMode) { return(FindDynamicCover(targetTransformPos, transformToDefend)); } return(new CoverData()); }
//Agent will try and find cover that is within a given range of the target. CoverData FindCoverWithinCombatRange(Vector3 targetTransformPos, Transform transformToDefend) { var i = 0; var myPos = myTransform.position; CoverNode currentCoverNode = null; var closestDistSquared = maxDistToCover; //We will take cover outside of the desired range if we can't find any within. var foundCoverWithinAcceptableRange = false; for (i = 0; i < coverNodes.Length; i++) { //Check if the node we are checking is occupied and within acceptable distances to key points if (!coverNodes[i].isOccupied() && coverNodeGroup == coverNodes[i].coverNodeGroup && Vector3.SqrMagnitude(coverNodes[i].GetPosition() - lastCoverPos) > minDistBetweenLastCoverSquared && (!transformToDefend || Vector3.SqrMagnitude(coverNodes[i].GetPosition() - transformToDefend.position) < defendingDistSquared)) { print("final?"); var distToTargetSquared = Vector3.SqrMagnitude(coverNodes[i].GetPosition() - targetTransformPos); var nodeCheckingNowDistSquared = Vector3.SqrMagnitude(myPos - coverNodes[i].GetPosition()); //Check for line of sight if (coverNodes[i].ValidCoverCheck(targetTransformPos)) { print("Check complete?"); //Prefer nodes within othe agent's combat range if (minCoverDistSqrd < distToTargetSquared && maxCoverDistSqrd > distToTargetSquared) { if (!foundCoverWithinAcceptableRange || (nodeCheckingNowDistSquared < closestDistSquared)) { closestDistSquared = nodeCheckingNowDistSquared; currentCoverNode = coverNodes[i]; foundCoverWithinAcceptableRange = true; } } //Check if this is the closest so far else if (!foundCoverWithinAcceptableRange && nodeCheckingNowDistSquared < closestDistSquared) { closestDistSquared = nodeCheckingNowDistSquared; currentCoverNode = coverNodes[i]; } } } } //pass the data to the script that asked for cover if (currentCoverNode != null) { lastCoverPos = currentCoverNode.GetPosition(); return(new CoverData(true, currentCoverNode.GetPosition(), currentCoverNode.GetSightNodePosition(), false, currentCoverNode)); } //Only bother with dynamic cover if we need it if (shouldUseDynamicCover && !AIController.Instance.usePerformanceMode) { return(FindDynamicCover(targetTransformPos, transformToDefend)); } return(new CoverData()); }