public int DeltaCost(Polygon polygon, Vector2 position, int amount)
        {
            Point tlg, tll, brg, brl;
            float xMin, yMin, xMax, yMax;
            polygon.GetExtents(out xMin, out yMin, out xMax, out yMax);
            xMin += position.x; yMin += position.y;
            xMax += position.x; yMax += position.y;
            tlg = WorldToChunk(new Vector2(xMin, yMin), out tll);
            brg = WorldToChunk(new Vector2(xMax - 1, yMax - 1), out brl);

            int sum = 0;

            for (int cx = tlg.X; cx <= brg.X; ++cx) {
                for (int cy = tlg.Y; cy <= brg.Y; ++cy) {
                    var chunk = RequireChunk(new Point(cx, cy));
                    var tl = new Point(cx == tlg.X ? tll.X : 0, cy == tlg.Y ? tll.Y : 0);
                    var br = new Point(cx == brg.X ? brl.X : ChunkSize - 1, cy == brg.Y ? brl.Y : ChunkSize - 1);
                    for (int lx = tl.X; lx <= br.X; ++lx) {
                        for (int ly = tl.Y; ly <= br.Y; ++ly) {
                            var newValue = (byte)(chunk.Costs[lx, ly] + amount);
                            chunk.Costs[lx, ly] = newValue;
                            sum += newValue;
                        }
                    }
                }
            }

            return sum;
        }
 void Start()
 {
     Simulation = new Simulation.SimWorld();
     Simulation.PathingMap = GameObject.FindObjectOfType<Simulation.PathingMap>();
     var entities = GameObject.FindObjectsOfType<Entity>();
     for (int e = 0; e < entities.Length; ++e) {
         var entity = entities[e];
         var simEntity = new Simulation.SimEntity(e);
         simEntity.Player = entity.Player;
         simEntity.Position.Value = entity.Position;
         entity.Bind(simEntity);
         foreach (var component in entity.GetComponentsInChildren<MonoBehaviour>()) {
             var mapping = (SimulationMappingAttribute[])component.GetType().GetCustomAttributes(typeof(SimulationMappingAttribute), true);
             if (mapping != null && mapping.Length > 0) {
                 var simType = mapping[0].SimulationType;
                 var simCmp = (Simulation.SimComponent)Activator.CreateInstance(simType);
                 foreach (var field in component.GetType().GetFields()) {
                     if (field.DeclaringType.IsAssignableFrom(typeof(MonoBehaviour))) continue;
                     var simFld = simCmp.GetType().GetField(field.Name);
                     if (simFld == null) continue;
                     simFld.SetValue(simCmp, field.GetValue(component));
                 }
                 if (component is PathEntity) {
                     var pathEntity = (PathEntity)component;
                     var simPEntity = (Simulation.SCPathEntity)simCmp;
                     var boxC = pathEntity.BoundCollider;
                     if (boxC != null) {
                         var size = Vector3.Scale(boxC.size, entity.transform.localScale);
                         var polygon = new Simulation.Polygon(
                             new Vector2(boxC.center.x - size.x / 2, boxC.center.z - size.z / 2),
                             new Vector2(boxC.center.x + size.x / 2, boxC.center.z - size.z / 2),
                             new Vector2(boxC.center.x + size.x / 2, boxC.center.z + size.z / 2),
                             new Vector2(boxC.center.x - size.x / 2, boxC.center.z + size.z / 2)
                         );
                         simEntity.Bounds.Value = polygon;
                     }
                 }
                 simEntity.Components.Add(simCmp);
             }
         }
         Simulation.Entities.Add(simEntity);
         entity.SendMessage("SetIsCurrentTeam", Instance.CurrentPlayer == entity.Player);
     }
     Simulation.Begin();
     ServerSimulation = new Simulation.SimWorld();
     ServerSimulation.CloneFrom(Simulation);
 }
 public void RemoveCost(Polygon polygon, Vector2 position)
 {
     DeltaCost(polygon, position, -1);
 }
 public bool IsClear(Polygon polygon, Vector2 position)
 {
     return DeltaCost(polygon, position, 0) > 0;
 }
 public void AddCost(Polygon polygon, Vector2 position)
 {
     int count = DeltaCost(polygon, position, 1);
     if (count == 0) Debug.LogWarning("Unable to add cost! " + polygon);
     //else Debug.Log("Adding cost " + rect + " added " + count);
 }