Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Author: Bystroushaak (bystrousak@kitakitsune.org)
- *
- * TODO:
- * Procházení skrz okraje
- * Closest i diagonálně
- */
- module MyBot;
- import ants;
- import std.stdio : stdout;
- import std.algorithm : remove;
- import std.string : indexOf;
- bool notIn(Loc strange_ant, const(Loc)[] ants){
- foreach(ant; ants){
- if (strange_ant == ant)
- return false;
- }
- return true;
- }
- class MyBot : IBot {
- private Ants engine;
- public Loc[] planed_steps;
- public Direction[Loc] last_direction;
- /// vrati nejblizsi prvek z pole
- Loc findClosestDest(Loc ant, const(Loc)[] locations){
- Loc cl;
- foreach(l; locations){
- if (l == Loc()){
- cl = l;
- continue;
- }
- if (engine.distance(l, ant) < engine.distance(cl, ant))
- cl = l;
- }
- return cl;
- }
- /// vrati nejblizsi jidlo ke kteremu ma mravenec cisty pristup
- Loc findClosestClear(Loc ant, const(Loc)[] where){
- Loc[] cf;
- foreach(f; where){
- if (hasClearRoad(ant, f))
- cf ~= f;
- }
- return findClosestDest(ant, cf);
- }
- Loc findClosestEnemyClear(Loc ant, const(Loc)[] where){
- Loc[] cf;
- foreach(f; where){
- if (hasClearRoad(ant, f))
- cf ~= f;
- }
- return findClosestDest(ant, cf);
- }
- bool hasClearRoad(Loc ant, Loc dest){
- if (ant == dest)
- return true;
- auto d = closestDirection(ant, dest);
- Loc next = engine.destination(ant, d);
- return (engine.passable(next) && notIn(next, this.planed_steps) && notIn(next, this.engine.myAnts()) && hasClearRoad(next, dest));
- }
- bool hasEnemyClearRoad(Loc ant, Loc dest){
- if (ant == dest)
- return true;
- auto d = closestDirection(ant, dest);
- Loc next = engine.destination(ant, d);
- bool no_enemy_around = true;
- Loc around;
- foreach(delta_row; [-1, 0, 1]){
- around.row = next.row + delta_row;
- foreach(delta_col; [-1, 0, 1]){
- around.col = next.col + delta_col;
- no_enemy_around = no_enemy_around && notIn(around, this.engine.enemyAnts());
- if (!no_enemy_around)
- break;
- }
- if (!no_enemy_around)
- break;
- }
- return (engine.passable(next) && notIn(next, this.planed_steps) && notIn(next, this.engine.myAnts()) && no_enemy_around && hasClearRoad(next, dest));
- }
- void moveTo(Loc ant, Direction d){
- Loc next = engine.destination(ant, d);
- if (!hasClearRoad(ant, next))
- return;
- engine.issueOrder(ant, d);
- planed_steps ~= next;
- last_direction.remove(ant);
- last_direction[next] = d;
- }
- Direction closestDirection(const(Loc) ant, const(Loc) dest){
- struct smer{
- float l;
- Direction d;
- }
- smer s;
- foreach(d; engine.direction(ant, dest)){
- auto destination = engine.destination(ant, d);
- if (s == smer()){
- s.l = engine.distance(ant, destination);
- s.d = d;
- continue;
- }
- if (s.l > engine.distance(ant, destination)){
- s.l = engine.distance(ant, destination);
- s.d = d;
- }
- }
- return s.d;
- }
- const(Loc)[] hillsToLoc(const(Hill)[] hills){
- const(Loc)[] l_hills;
- foreach(h; hills){
- l_hills ~= h.loc;
- }
- return l_hills;
- }
- void doTurn(Ants engine) {
- this.engine = engine;
- this.planed_steps = [];
- foreach(ant; engine.myAnts){
- Loc food = findClosestClear(ant, engine.food());
- Loc enemy = findClosestClear(ant, engine.enemyAnts());
- Loc enemyHill = findClosestEnemyClear(ant, hillsToLoc(engine.enemyHills));
- if (enemyHill != Loc()){
- this.moveTo(ant, closestDirection(ant, enemyHill));
- }else if (food != Loc()){
- this.moveTo(ant, closestDirection(ant, food)); // mozna predelat na fci pro nejkratsi cestu?
- }else if (engine.myAnts().length > engine.enemyAnts().length + 3 && engine.myAnts().length > 20 && enemy != Loc()){
- this.moveTo(ant, closestDirection(ant, enemy)); // mozna predelat na fci pro nejkratsi cestu?
- }else{
- Direction new_d;
- if (ant in last_direction)
- new_d = last_direction[ant];
- else
- new_d = AIM[0];
- Loc antGoto = engine.destination(ant, new_d);
- int cnt = 0;
- while(!hasClearRoad(ant, antGoto) && cnt++ <= 4){
- new_d = AIM["wnes".indexOf(new_d.key)];
- antGoto = engine.destination(ant, new_d);
- }
- moveTo(ant, new_d);
- }
- }
- }
- }
- void main() {
- version(unittest) {
- // We don't run the bot or wait for input in this case
- } else {
- Ants.run(new MyBot());
- }
- }
Add Comment
Please, Sign In to add comment