Creating good waypoints

There are times when the number of waypoints must be reduced at a certain point during the game or just for memory constraints. In this recipe, we will learn a technique called condensation that helps us deal with this problem, forcing the waypoints to compete with each other given their assigned value.

Getting ready

In this recipe, we will deal with static member functions. It is important that we understand the use and value of static functions.

How to do it…

We will create the Waypoint class and add the functions for condensing the set of waypoints.

  1. Create the Waypoint class, deriving not only from MonoBehaviour, but also from the IComparer interface:
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class Waypoint : MonoBehaviour, IComparer
    {
        public float value;
        public List<Waypoint> neighbours;
    }
  2. Implement the Compare function from the aforementioned interface:
    public int Compare(object a, object b)
    {
        Waypoint wa = (Waypoint)a;
        Waypoint wb = (Waypoint)b;
        if (wa.value == wb.value)
            return 0;
        if (wa.value < wb.value)
            return -1;
        return 1;
    }
  3. Implement the static function to compute whether an agent can move between two waypoints:
    public static bool CanMove(Waypoint a, Waypoint b)
    {
        // implement your own behaviour for
        // deciding whether an agent can move
        // easily between two waypoints
        return true;
    }
  4. Start declaring the function for condensing the waypoints:
    public static void CondenseWaypoints(List<Waypoint> waypoints, float distanceWeight)
    {
        // next steps
    }
  5. Initialize some variables and sort the waypoints in descending order:
    distanceWeight *= distanceWeight;
    waypoints.Sort();
    waypoints.Reverse();
    List<Waypoint> neighbours;
  6. Start the loop for computing each waypoint:
    foreach (Waypoint current in waypoints)
    {
        // next steps
    }
  7. Retrieve the waypoint neighbors, sort them, and start the loop to make them compete with each other:
    neighbours = new List<Waypoint>(current.neighbours);
    neighbours.Sort();
    foreach (Waypoint target in neighbours)
    {
        if (target.value > current.value)
            break;
        if (!CanMove(current, target))
            continue;
        // next steps
    }
  8. Compute the target's position:
    Vector3 deltaPos = current.transform.position;
    deltaPos -= target.transform.position;
    deltaPos = Vector3.Cross(deltaPos, deltaPos);
    deltaPos *= distanceWeight;
  9. Compute the target's overall value and decide whether to keep it or throw it:
    float deltaVal = current.value - target.value;
    deltaVal *= deltaVal;
    if (deltaVal < distanceWeight)
    {
        neighbours.Remove(target);
        waypoints.Remove(target);
    }

How it works…

The waypoints are ordered according to their relevance (such as height to be used as a sniping or advantage location) and then their neighbors are checked to see which ones are going to be dismissed from the condensation. Naturally, the less valuable waypoints are kept to the end of the computation cycle. In the next recipe, we will learn how to analyze waypoints.

See also

Refer to the the following recipes:

  • Analyzing waypoints by height
  • Analyzing waypoints by cover and visibility
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.163.13