1 /** 2 * Copyright: © 2012-2014 Anton Gushcha 3 * License: Subject to the terms of the MIT license, as written in the included LICENSE file. 4 * Authors: NCrashed <ncrashed@gmail.com>, 5 * LeMarwin <lemarwin42@gmail.com>, 6 * Nazgull09 <nazgull90@gmail.com> 7 */ 8 module main; 9 10 import std.stdio; 11 import std.process; 12 import std.conv; 13 import std.getopt; 14 15 import core.time, core.thread; 16 17 import devol.compiler; 18 19 import ant.progtype; 20 import ant.world; 21 import ant.app; 22 23 import derelict.sdl2.sdl; 24 25 alias Population!( getDefChars, Ant ) AntPopulation; 26 27 class MyCompilaton : GameCompilation 28 { 29 bool stopCond(ref int x, IndAbstract ind, WorldAbstract world) // stopCond 30 { 31 auto ai = cast(Ant)(ind); 32 auto aw = cast(AntWorld)(world); 33 34 if (ai.prevFoodCount < ai.FoodCount) 35 x = 0; 36 ai.prevFoodCount = ai.FoodCount; 37 return x > 5 || aw.Food <= ai.FoodCount; 38 } 39 40 void drawStep(IndAbstract ind, WorldAbstract world) // draw step 41 { 42 version(NoGraphicsOutput) 43 { 44 45 } else 46 { 47 auto ai = cast(Ant)(ind); 48 auto aw = cast(AntWorld)(world); 49 auto app = App.getSingleton(); 50 51 app.clear(); 52 53 foreach(uint j,ref l; aw.getMap() ) 54 { 55 foreach(uint i,ref c; l) 56 { 57 if ( j == ai.x && i == ai.y ) 58 { 59 app.draw(app.ants[ai.Direction], (j+1)*32u, (i+1)*32u, 32u, 32u); 60 } 61 else if (c) 62 app.draw(app.food, (j+1)*32u , (i+1)*32u, 32u, 32u); 63 else 64 app.draw(app.empty, (j+1)*32u , (i+1)*32u, 32u, 32u); 65 } 66 } 67 68 foreach( i; 0..aw.size+2) 69 app.draw(app.wall, i*32u, 0, 32u, 32u); 70 foreach( i; 0..aw.size+2) 71 app.draw(app.wall, i*32u, (aw.size+1)*32u, 32u, 32u); 72 foreach( i; 0..aw.size+2) 73 app.draw(app.wall, 0, i*32u, 32u, 32u); 74 foreach( i; 0..aw.size+2) 75 app.draw(app.wall, (aw.size+1)*32u, i*32u, 32u, 32u); 76 77 app.present(); 78 } 79 } 80 81 void drawFinal(PopAbstract pop, WorldAbstract world) 82 { 83 // auto ap = cast(AntPopulation)(pop); 84 // auto aw = cast(AntWorld)(world); 85 } 86 87 int roundsPerInd() 88 { 89 return 1; 90 } 91 } 92 93 alias Compiler!( 94 MyCompilaton, 95 Evolutor, 96 AntProgType, 97 AntPopulation, 98 AntWorld) 99 AntCompiler; 100 101 void main(string[] args) 102 { 103 string savedPop; 104 getopt(args, 105 "saved", &savedPop 106 ); 107 108 auto app = new App; 109 scope(exit) destroy(app); 110 111 auto comp = new AntCompiler(new MyCompilaton, new AntProgType); 112 auto tmng = TypeMng.getSingleton(); 113 auto opmng = OperatorMng.getSingleton(); 114 115 AntPopulation pop; 116 if(savedPop == "") 117 { 118 pop = comp.addPop(5); 119 } else 120 { 121 pop = comp.loadPopulation(savedPop); 122 } 123 124 while(!app.shouldExit) {comp.envolveGeneration(() => app.shouldExit, "saves", (d){}, () => false);} 125 }