Guest User

Untitled

a guest
Feb 21st, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 4.37 KB | None | 0 0
  1. /**
  2.  * Author: Bystroushaak (bystrousak@kitakitsune.org)
  3.  *
  4.  * TODO:
  5.  *   Procházení skrz okraje
  6.  *   Closest i diagonálně
  7. */
  8.  
  9.  
  10. module MyBot;
  11.  
  12. import ants;
  13. import std.stdio : stdout;
  14. import std.algorithm : remove;
  15. import std.string : indexOf;
  16.  
  17.  
  18. bool notIn(Loc strange_ant, const(Loc)[] ants){
  19.     foreach(ant; ants){
  20.         if (strange_ant == ant)
  21.             return false;
  22.     }
  23.    
  24.     return true;
  25. }
  26.  
  27.  
  28. class MyBot : IBot {
  29.     private Ants engine;
  30.     public Loc[] planed_steps;
  31.     public Direction[Loc] last_direction;
  32.    
  33.     /// vrati nejblizsi prvek z pole
  34.     Loc findClosestDest(Loc ant, const(Loc)[] locations){
  35.         Loc cl;
  36.         foreach(l; locations){
  37.             if (l == Loc()){
  38.                 cl = l;
  39.                 continue;
  40.             }
  41.        
  42.             if (engine.distance(l, ant) < engine.distance(cl, ant))
  43.                 cl = l;
  44.         }
  45.    
  46.         return cl;
  47.     }
  48.    
  49.     /// vrati nejblizsi jidlo ke kteremu ma mravenec cisty pristup
  50.     Loc findClosestClear(Loc ant, const(Loc)[] where){
  51.         Loc[] cf;
  52.         foreach(f; where){
  53.             if (hasClearRoad(ant, f))
  54.                 cf ~= f;
  55.         }
  56.        
  57.         return findClosestDest(ant, cf);
  58.     }
  59.     Loc findClosestEnemyClear(Loc ant, const(Loc)[] where){
  60.         Loc[] cf;
  61.         foreach(f; where){
  62.             if (hasClearRoad(ant, f))
  63.                 cf ~= f;
  64.         }
  65.        
  66.         return findClosestDest(ant, cf);
  67.     }
  68.    
  69.     bool hasClearRoad(Loc ant, Loc dest){
  70.         if (ant == dest)
  71.             return true;
  72.            
  73.         auto d = closestDirection(ant, dest);
  74.         Loc next = engine.destination(ant, d);
  75.        
  76.         return (engine.passable(next) && notIn(next, this.planed_steps) && notIn(next, this.engine.myAnts()) && hasClearRoad(next, dest));
  77.     }
  78.     bool hasEnemyClearRoad(Loc ant, Loc dest){
  79.         if (ant == dest)
  80.             return true;
  81.            
  82.         auto d = closestDirection(ant, dest);
  83.         Loc next = engine.destination(ant, d);
  84.        
  85.         bool no_enemy_around = true;
  86.         Loc around;
  87.         foreach(delta_row; [-1, 0, 1]){
  88.             around.row = next.row + delta_row;
  89.             foreach(delta_col; [-1, 0, 1]){
  90.                 around.col = next.col + delta_col;
  91.                 no_enemy_around = no_enemy_around && notIn(around, this.engine.enemyAnts());
  92.                 if (!no_enemy_around)
  93.                     break;
  94.             }
  95.             if (!no_enemy_around)
  96.                     break;
  97.         }
  98.        
  99.         return (engine.passable(next) && notIn(next, this.planed_steps) && notIn(next, this.engine.myAnts()) && no_enemy_around && hasClearRoad(next, dest));
  100.     }
  101.    
  102.     void moveTo(Loc ant, Direction d){
  103.         Loc next = engine.destination(ant, d);
  104.        
  105.         if (!hasClearRoad(ant, next))
  106.             return;
  107.            
  108.         engine.issueOrder(ant, d);
  109.         planed_steps ~= next;
  110.        
  111.         last_direction.remove(ant);
  112.         last_direction[next] = d;
  113.     }
  114.    
  115.     Direction closestDirection(const(Loc) ant, const(Loc) dest){
  116.         struct smer{
  117.             float l;
  118.             Direction d;
  119.         }
  120.        
  121.         smer s;
  122.         foreach(d; engine.direction(ant, dest)){
  123.             auto destination = engine.destination(ant, d);
  124.            
  125.             if (s == smer()){
  126.                 s.l =  engine.distance(ant, destination);
  127.                 s.d = d;
  128.                 continue;
  129.             }
  130.            
  131.             if (s.l > engine.distance(ant, destination)){
  132.                 s.l = engine.distance(ant, destination);
  133.                 s.d = d;
  134.             }
  135.         }
  136.        
  137.         return s.d;
  138.     }
  139.    
  140.     const(Loc)[] hillsToLoc(const(Hill)[] hills){
  141.         const(Loc)[] l_hills;
  142.        
  143.         foreach(h; hills){
  144.             l_hills ~= h.loc;
  145.         }
  146.        
  147.         return l_hills;
  148.     }
  149.    
  150.     void doTurn(Ants engine) {
  151.         this.engine = engine;
  152.         this.planed_steps = [];
  153.        
  154.         foreach(ant; engine.myAnts){
  155.             Loc food  = findClosestClear(ant, engine.food());
  156.             Loc enemy = findClosestClear(ant, engine.enemyAnts());
  157.             Loc enemyHill = findClosestEnemyClear(ant, hillsToLoc(engine.enemyHills));
  158.            
  159.             if (enemyHill != Loc()){
  160.                 this.moveTo(ant, closestDirection(ant, enemyHill));
  161.             }else if (food != Loc()){
  162.                 this.moveTo(ant, closestDirection(ant, food)); // mozna predelat na fci pro nejkratsi cestu?
  163.             }else if (engine.myAnts().length > engine.enemyAnts().length + 3 && engine.myAnts().length > 20 && enemy != Loc()){
  164.                 this.moveTo(ant, closestDirection(ant, enemy)); // mozna predelat na fci pro nejkratsi cestu?
  165.             }else{
  166.                 Direction new_d;
  167.                 if (ant in last_direction)
  168.                     new_d = last_direction[ant];
  169.                 else
  170.                     new_d = AIM[0];
  171.                    
  172.                 Loc antGoto = engine.destination(ant, new_d);
  173.                 int cnt = 0;
  174.                 while(!hasClearRoad(ant, antGoto) && cnt++ <= 4){
  175.                     new_d = AIM["wnes".indexOf(new_d.key)];
  176.                     antGoto = engine.destination(ant, new_d);
  177.                 }
  178.                
  179.                 moveTo(ant, new_d);
  180.             }
  181.         }
  182.     }
  183. }
  184.  
  185.  
  186.  
  187. void main() {
  188.     version(unittest) {
  189.         // We don't run the bot or wait for input in this case
  190.     } else {
  191.         Ants.run(new MyBot());
  192.     }
  193. }
Add Comment
Please, Sign In to add comment