Advertisement
charlesg

OptimalBarrierPath

Aug 8th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.01 KB | None | 0 0
  1. package com.godlinggame.mapmodel;
  2.  
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.Iterator;
  6. import java.util.Set;
  7.  
  8. import com.badlogic.gdx.utils.Array;
  9.  
  10.  
  11. public class OptimalBarrierPath extends OptimalPath
  12. {
  13. HashMap<MapLocation, Array<distance>> distances = null;
  14.  
  15.  
  16.   public OptimalBarrierPath( Map map )
  17.   {
  18.     super( map );
  19.     setMap( map );
  20.   }
  21.  
  22.  
  23.   @Override
  24.   public void setMap( Map map )
  25.   {
  26.     super.setMap( map );
  27.     distances = new HashMap<MapLocation, Array<distance>>();
  28.   }
  29.  
  30.  
  31.   @Override
  32.   public double stepCost( MapLocation ml, double barrierWeight )
  33.   {
  34.     if (null == distances) return Map.OutOfBounds;
  35.  
  36.   Array<distance> da = distances.get( ml );
  37.  
  38.     if (null == da || da.size < 1) return Map.OutOfBounds;
  39.  
  40.   double best = Map.OutOfBounds;
  41.  
  42.     for ( distance d : da )
  43.     {
  44.     double cost = d.cost( barrierWeight );
  45.  
  46.       if (best > cost)
  47.         best = cost;
  48.     }
  49.  
  50.     return best;
  51.   }
  52.  
  53.  
  54.   @Override
  55.   public void clearPath() { distances.clear(); }
  56.   @Override
  57.   public void addGoalPosition( MapLocation add )
  58.   {
  59.     addDistance( add, new distance( 0.0, 0.0 ));
  60.   }
  61.  
  62.  
  63.   @Override
  64.   public void recomputePath()
  65.   {
  66.     recomputePath( distances.keySet() );
  67.   }
  68.  
  69.  
  70.   protected void recomputePath( Set<MapLocation> start )
  71.   {
  72.   HashSet<MapLocation> newwork, work;
  73.  
  74.     work = new HashSet<MapLocation>();
  75.     newwork = new HashSet<MapLocation>();
  76.     work.addAll( start );
  77.  
  78.     while(!work.isEmpty())
  79.     {
  80.     Iterator<MapLocation> it = work.iterator();
  81.  
  82.       while( it.hasNext() )
  83.       {
  84.       MapLocation ml = it.next(), newloc;
  85.       Array<distance> d = getDistances( ml );
  86.  
  87.         newloc = ml.move( 1, 0 );
  88.         if (addPaths( d, newloc )) newwork.add( newloc );
  89.         newloc = ml.move( -1, 0 );
  90.         if (addPaths( d, newloc )) newwork.add( newloc );
  91.         newloc = ml.move( 0, 1 );
  92.         if (addPaths( d, newloc )) newwork.add( newloc );
  93.         newloc = ml.move( 0, -1 );
  94.         if (addPaths( d, newloc )) newwork.add( newloc );
  95.       }
  96.  
  97.       work = newwork;
  98.       newwork = new HashSet<MapLocation>();
  99.     }
  100.   }
  101.  
  102.  
  103.   private boolean addPaths( Array<distance> begin, MapLocation add )
  104.   {
  105.   boolean added = false;
  106.   double barrier = map.getBarrierAt( add );
  107.  
  108.     if (barrier != Map.OutOfBounds)
  109.       for( distance d : begin )
  110.       {
  111.         if (addDistance( add, d.add( 1.0, barrier )))
  112.           added = true;
  113.       }
  114.  
  115.     return added;
  116.   }
  117.  
  118.  
  119.   private Array<distance> getDistances( MapLocation ml )
  120.   {
  121.     return distances.get( ml );
  122.   }
  123.  
  124.  
  125.   private boolean addDistance( MapLocation ml, distance d )
  126.   {
  127.   Array<distance> alld = getDistances( ml );
  128.  
  129.     if (null == alld)
  130.     {
  131.       alld = new Array<distance>( false, 16 );
  132.       alld.add( d );
  133.       distances.put( ml,  alld );
  134.  
  135.       return true;
  136.     }
  137.  
  138.   boolean add = false;
  139.  
  140.     for (int i=0; i<alld.size; i++)
  141.     {
  142.     distance old = alld.get( i );
  143.  
  144.       if (old.move <= d.move && old.barrier <= d.barrier) return false;
  145.       if (old.move > d.move || old.barrier > d.barrier)
  146.       {
  147.         if (old.move >= d.move && old.barrier >= d.barrier)
  148.         {
  149.           alld.set( i, d );
  150.           return true;
  151.         }
  152.  
  153.         add = true;
  154.       }
  155.     }
  156.  
  157.     if (add)
  158.       alld.add( d );
  159.  
  160.     return add;
  161.   }
  162. }
  163.  
  164.  
  165. class distance
  166. {
  167. final double move, barrier;
  168.  
  169.  
  170.   distance( double move, double barrier )
  171.   {
  172.     this.move = move;
  173.     this.barrier = barrier;
  174.   }
  175.  
  176.  
  177.   double cost( double barrierWeight )
  178.   {
  179.     return move + barrier * barrierWeight;
  180.   }
  181.  
  182.  
  183.   distance add( double move, double barrier )
  184.   {
  185.     return new distance( move + this.move, barrier + this.barrier );
  186.   }
  187.  
  188.  
  189.   public boolean equals( Object o )
  190.   {
  191.     if (this == o) return true;
  192.  
  193.     if (o instanceof distance)
  194.     {
  195.     distance d = (distance) o;
  196.  
  197.       return move == d.move && barrier == d.barrier;
  198.     }
  199.  
  200.     return false;
  201.   }
  202.  
  203.  
  204.   public String toString() { return " d("+move+", "+barrier+")"; }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement