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 ant.operators;
9 
10 import std.stdio;
11 
12 import ant.world;
13 import ant.progtype;
14 import devol.operator;
15 import devol.operatormng;
16 import devol.argument;
17 
18 import devol.individ;
19 import devol.line;
20 import devol.world;
21 
22 import devol.typemng;
23 
24 import devol.type;
25 import devol.std.typevoid;
26 import std.process;
27 
28 class GoForward : Operator
29 {
30 	Type voidtype;
31 	this()
32 	{
33 		voidtype = TypeMng.getSingleton().getType("TypeVoid");
34 		mRetType = voidtype;
35 		super("FORWARD","Moves ant 1 step forward", ArgsStyle.NULAR_STYLE);
36 	}
37 	
38 	override Argument apply(IndAbstract individ, Line line, WorldAbstract world)
39 	{
40 		version(linux)
41 		{system("beep");}
42 		writeln("Appling GoForward");
43 		auto ind = cast(Ant)(individ);	
44 		auto Wrld = cast(AntWorld)(world);
45 		writeln("Casting success: ", ind !is null, " " ,Wrld !is null);	
46 		final switch(ind.Direction)
47 		{
48 			case ind.Faces.SOUTH:
49 				if(ind.y<Wrld.size-1)
50 					ind.y+=1;
51 				break;
52 			case ind.Faces.NORTH:
53 				if(ind.y>1)
54 					ind.y-=1;
55 				break;
56 			case ind.Faces.WEST:
57 				if(ind.x>1)
58 					ind.x-=1;
59 				break;
60 			case ind.Faces.EAST:
61 				if(ind.x<Wrld.size-1)
62 					ind.x+=1;
63 				break;
64 		}
65 		writeln("checking food");
66 		if(Wrld.checkForFood(ind.x, ind.y) && !ind.isCheater())
67 			{
68 				writeln("eating");
69 				ind.FoodCount++;
70 				Wrld.removeFood(ind.x,ind.y);
71 			}
72 		writeln("return");
73 		return voidtype.getNewArg();
74 	}
75 }
76 
77 class TurnLeft : Operator
78 {
79 	Type voidtype;
80 	this()
81 	{
82 		voidtype = TypeMng.getSingleton().getType("TypeVoid");
83 		mRetType = voidtype;
84 		super("LEFT", "Ant turns counterclockwise", ArgsStyle.NULAR_STYLE);
85 	}
86 	
87 	override Argument apply(IndAbstract individ, Line line, WorldAbstract world)
88 	{
89 		writeln("Appling TurnLeft");
90 		auto ind = cast(Ant)(individ);
91 		writeln("Casting success: ", ind !is null);
92 		final switch(ind.Direction)
93 		{
94 			case ind.Faces.NORTH:
95 				ind.Direction = ind.Faces.WEST;
96 				break;
97 			case ind.Faces.SOUTH:
98 				ind.Direction = ind.Faces.EAST;
99 				break;
100 			case ind.Faces.EAST:
101 				ind.Direction = ind.Faces.NORTH;
102 				break;
103 			case ind.Faces.WEST:
104 				ind.Direction = ind.Faces.SOUTH;
105 				break;
106 		}
107 		return voidtype.getNewArg();
108 	}
109 }
110 
111 class TurnRight : Operator
112 {
113 	Type voidtype;
114 	this()
115 	{
116 		voidtype = TypeMng.getSingleton().getType("TypeVoid");
117 		mRetType = voidtype;
118 		super("RIGHT", "Ant turns clockwise", ArgsStyle.NULAR_STYLE);
119 	}
120 	
121 	override Argument apply(IndAbstract individ, Line line, WorldAbstract world)
122 	{
123 		writeln("Appling TurnRight");
124 		auto ind = cast(Ant)(individ);
125 		writeln("Casting success: ", ind !is null);
126 		final switch(ind.Direction)
127 		{
128 			case ind.Faces.NORTH:
129 				ind.Direction = ind.Faces.EAST;
130 				break;
131 			case ind.Faces.SOUTH:
132 				ind.Direction = ind.Faces.WEST;
133 				break;
134 			case ind.Faces.EAST:
135 				ind.Direction = ind.Faces.SOUTH;
136 				break;
137 			case ind.Faces.WEST:
138 				ind.Direction = ind.Faces.NORTH;
139 				break;
140 		}
141 		return voidtype.getNewArg();
142 	}
143 }
144 
145 
146 
147