Maze /Classes/Object/ObjectSearcher.cs

Language C# Lines 152
MD5 Hash d09208dc6af50bcb2628ef691f52a8b6 Estimated Cost $2,585 (why?)
Repository git://github.com/Concliff/Maze.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maze.Forms;

namespace Maze.Classes
{
    /// <summary>
    /// Provides static methods to search the <see cref="Object"/>s and its derived classes in <see cref="ObjectContainer"/>.
    /// </summary>
    public static class ObjectSearcher
    {
        //
        // Within Range Methods:
        //

        public static List<Object> GetObjectsWithinRange(Object searcher, int rangeDistance)
        {
            List<Object> objects = GetObjectsInArea(searcher.Position, rangeDistance);

            // exclude itself
            objects.Remove(searcher);

            return objects;
        }

        public static List<GridObject> GetGridObjectsWithinRange(Object searcher, int rangeDistance)
        {
            List<GridObject> gridObjects = GetGridObjectsInArea(searcher.Position, rangeDistance);

            // exclude itself
            if (searcher.ObjectType == ObjectTypes.GridObject)
                gridObjects.Remove((GridObject)searcher);

            return gridObjects;
        }

        public static List<Unit> GetUnitsWithinRange(Object searcher, int rangeDistance)
        {
            return GetUnitsWithinRange(searcher, rangeDistance, true, true);
        }

        public static List<Unit> GetUnitsWithinRange(Object searcher, int rangeDistance, bool isVisibleOnly, bool isAliveOnly)
        {
            List<Unit> units = GetUnitsInArea(searcher.Position, rangeDistance, isVisibleOnly, isAliveOnly);

            // exclude itself
            if (searcher.ObjectType == ObjectTypes.Unit || searcher.ObjectType == ObjectTypes.Slug)
                units.Remove((Unit)searcher);

            return units;
        }

        //
        // In Area Methods:
        //

        public static List<GridObject> GetGridObjectsInArea(GPS centralPosition, int radius)
        {
            List<GridLocation> locations = GetMatchedLocations(centralPosition, radius);
            List<GridObject> objects = new List<GridObject>();

            foreach (GridLocation location in locations)
            {
                List<GridObject> foundObject = ObjectContainer.Instance.GetGridObjects(location);
                if (foundObject.Count == 0)
                    continue;
                foundObject = FilterInRangeObjects<GridObject>(foundObject, centralPosition, radius);
                if (foundObject.Count > 0)
                    objects.AddRange(foundObject);
            }

            return objects;
        }

        public static List<Unit> GetUnitsInArea(GPS centralPosition, int radius, bool isVisibleOnly, bool isAliveOnly)
        {
            List<GridLocation> locations = GetMatchedLocations(centralPosition, radius);
            List<Unit> units = new List<Unit>();

            foreach (GridLocation location in locations)
            {
                List<Unit> foundUnits = ObjectContainer.Instance.GetUnits(location);
                if (foundUnits.Count == 0)
                    continue;
                foundUnits = FilterInRangeObjects<Unit>(foundUnits, centralPosition, radius);
                if (foundUnits.Count > 0)
                    foreach (Unit unit in foundUnits)
                        if ((!isVisibleOnly || (isVisibleOnly && unit.IsVisible)) && (!isAliveOnly || (isAliveOnly && unit.IsAlive)))
                            units.Add(unit);
            }

            return units;
        }

        public static List<Object> GetObjectsInArea(GPS centralPosition, int radius)
        {
            List<GridLocation> locations = GetMatchedLocations(centralPosition, radius);
            List<Object> objects = new List<Object>();

            foreach (GridLocation location in locations)
            {
                List<Object> foundObjects = ObjectContainer.Instance.GetObjects(location);
                if (foundObjects.Count == 0)
                    continue;
                foundObjects = FilterInRangeObjects<Object>(foundObjects, centralPosition, radius);
                if (foundObjects.Count > 0)
                    objects.AddRange(foundObjects);
            }

            return objects;
        }

        //
        // Helper Methods:
        //

        private static List<GridLocation> GetMatchedLocations(GPS position, int radius)
        {
            List<GridLocation> locations = new List<GridLocation>();
            // How much cells use for search
            int cellsToNorth = (int)Math.Ceiling(Math.Abs(position.Location.Y - radius) * 1d / GlobalConstants.CELL_HEIGHT);
            int cellsToSouth = (int)Math.Floor(Math.Abs(position.Location.Y + radius) * 1d / GlobalConstants.CELL_HEIGHT);
            int cellsToWest = (int)Math.Ceiling(Math.Abs(position.Location.X - radius) * 1d / GlobalConstants.CELL_WIDTH);
            int cellsToEast = (int)Math.Floor(Math.Abs(position.Location.X + radius) * 1d / GlobalConstants.CELL_WIDTH);
            GridLocation searchLocation = position.Location;

            for (int width = position.Location.X - cellsToWest; width <= position.Location.X + cellsToEast; ++width)
                for (int height = position.Location.Y - cellsToNorth; height <= position.Location.Y + cellsToSouth; ++height)
                {
                    searchLocation.X = width;
                    searchLocation.Y = height;
                    locations.Add(searchLocation);
                }

            return locations;
        }

        private static List<T> FilterInRangeObjects<T>(List<T> objects, GPS position, int range) where T : Object
        {
            List<T> matchedObjects = new List<T>();
            foreach (T obj in objects)
                if (obj.Position.GetDistance(position) <= range)
                    matchedObjects.Add(obj);

            return matchedObjects;
        }

    }
}
Back to Top