Beispiel #1
0
        // Checks for collision between two nodes and their children
        public static bool CheckCollision(Node n1, Node n2, NodeProvider provider, SpaceState state, bool noisy = false, int deep = 0)
        {
            if (deep > 5)
                return false;
            int x = (n1.x + n2.x) / 2;
            int y = (n1.y + n2.y) / 2;
            int t = (n1.t + n2.t) / 2;
            Node n3 = provider.GetNode (t, x, y);

            // Noisy calculation
            if (state.enemies != null && ((Cell)n3.cell).noisy) {
                foreach (Enemy enemy in state.enemies) {
                    Vector3 dupe = enemy.positions [t];
                    dupe.x = (dupe.x - state.floorMin.x) / state.tileSize.x;
                    dupe.y = n3.t;
                    dupe.z = (dupe.z - state.floorMin.z) / state.tileSize.y;

                    // This distance is in number of cells size radius i.e. a 10 tilesize circle around the point
                    if (Vector3.Distance (dupe, n3.GetVector3 ()) < 10)
                        return true;
                }
            }

            return !(n3.cell.safe || !(n3.cell.blocked || n3.cell.seen)) || CheckCollision (n1, n3, provider, state, noisy, deep + 1) || CheckCollision (n2, n3, provider, state, noisy, deep + 1);
        }
Beispiel #2
0
 public static bool SmoothNode(Node n, NodeProvider provider, SpaceState state, bool noisy = false)
 {
     if (n.parent != null && n.parent.parent != null) {
         if (CheckCollision (n, n.parent.parent, provider, state, noisy))
             return false;
         else {
             n.parent = n.parent.parent;
             return true;
         }
     } else
         return false;
 }
Beispiel #3
0
 static SpaceState()
 {
     editor = new SpaceState ();
     running = new SpaceState ();
 }
Beispiel #4
0
    // Precompute a timestamps number of maps in the future by simulating the enemies movement across the map
    // Stores withiin [populate] the variables [fullMap, enemies, tileSize] with the computed data
    public Cell[][][] PrecomputeMaps(SpaceState populate, Vector3 floorMin, Vector3 floorMax, int cellsX, int cellsZ, int timestamps, float stepSize, int ticksBehind = 0, Cell[][] baseMap = null)
    {
        // Initial computation
        ComputeTileSize (populate, floorMin, floorMax, cellsX, cellsZ);

        // Compute the fixed obstacle map
        if (baseMap == null)
            baseMap = ComputeObstacles ();

        // Prepare the dataholders (used afterwards by the callers)
        GameObject[] en = GameObject.FindGameObjectsWithTag ("Enemy") as GameObject[];
        Enemy[] enemies = new Enemy[en.Length];
        for (int i = 0; i < en.Length; i++) {
            enemies [i] = en [i].GetComponent<Enemy> ();
            enemies [i].positions = new Vector3[timestamps];
            enemies [i].forwards = new Vector3[timestamps];
            enemies [i].rotations = new Quaternion[timestamps];
            enemies [i].cells = new Vector2[timestamps][];
        }
        Cell[][][] fullMap = new Cell[timestamps][][];

        List<List<Vector2>> cells = new List<List<Vector2>> ();
        // Prepare the cells by enemy
        for (int i = 0; i < enemies.Length; i++) {
            cells.Add (new List<Vector2> ());
        }

        // Foreach period time, we advance a stepsize into the future and compute the map for it
        for (int counter = 0; counter < timestamps; counter++) {
            // Simulate and store the values for future use
            foreach (Enemy e in enemies) {
                e.Simulate (stepSize);
                e.positions [counter] = e.GetSimulationPosition ();
                e.forwards [counter] = e.GetSimulatedForward ();
                e.rotations [counter] = e.GetSimulatedRotation ();
            }

            fullMap [counter] = ComputeMap (baseMap, enemies, cells);

            // Store the seen cells in the enemy class
            List<Vector2>[] arr = cells.ToArray ();
            for (int i = 0; i < enemies.Length; i++) {
                enemies [i].cells [counter] = arr [i].ToArray ();
                arr [i].Clear ();
            }
        }

        // From the last time to the first, pick a cell and look back in time to see if it was seen previously
        if (ticksBehind > 0)
            for (int counter = timestamps-1; counter >= 0; counter--)
                for (int ticks = 1; ticks <= ticksBehind && counter - ticks > 0; ticks++)
                    foreach (Enemy e in enemies)
                        foreach (Vector2 v in e.cells[counter - ticks])
                            if (fullMap [counter - ticks] [(int)v.x] [(int)v.y].seen) {
                                fullMap [counter] [(int)v.x] [(int)v.y].seen = true;
                            }

        foreach (Enemy e in enemies)
            e.ComputeSeenCells(fullMap);

        populate.enemies = enemies;
        populate.fullMap = fullMap;

        return fullMap;
    }
Beispiel #5
0
    public void ComputeTileSize(SpaceState populate, Vector3 floorMin, Vector3 floorMax, int cellsX, int cellsZ)
    {
        // Initial computation
        float dx = Mathf.Abs (floorMin.x - floorMax.x);
        float dz = Mathf.Abs (floorMin.z - floorMax.z);
        tileSizeX = dx / cellsX;
        tileSizeZ = dz / cellsZ;

        this.minX = floorMin.x;
        this.minZ = floorMin.z;
        this.cellsX = cellsX;
        this.cellsZ = cellsZ;

        populate.tileSize = new Vector2 (tileSizeX, tileSizeZ);
        populate.floorMin = floorMin;
    }
Beispiel #6
0
 static SpaceState()
 {
     editor  = new SpaceState();
     running = new SpaceState();
 }
Beispiel #7
0
 public PathML(SpaceState state)
 {
     this.state = state;
 }