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.std.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 		//writeln("Appling GoForward");
41 		auto ind = cast(Ant)(individ);	
42 		auto Wrld = cast(AntWorld)(world);
43 		//writeln("Casting success: ", ind !is null, " " ,Wrld !is null);	
44 		final switch(ind.Direction)
45 		{
46 			case ind.Faces.SOUTH:
47 				if(ind.y<Wrld.size-1)
48 					ind.y+=1;
49 				break;
50 			case ind.Faces.NORTH:
51 				if(ind.y>1)
52 					ind.y-=1;
53 				break;
54 			case ind.Faces.WEST:
55 				if(ind.x>1)
56 					ind.x-=1;
57 				break;
58 			case ind.Faces.EAST:
59 				if(ind.x<Wrld.size-1)
60 					ind.x+=1;
61 				break;
62 		}
63 		//writeln("checking food");
64 		if(Wrld.checkForFood(ind.x, ind.y) && !ind.isCheater())
65 			{
66 				//writeln("eating");
67 				ind.FoodCount++;
68 				Wrld.removeFood(ind.x,ind.y);
69 			}
70 		//writeln("return");
71 		return voidtype.getNewArg();
72 	}
73 }
74 
75 class TurnLeft : Operator
76 {
77 	Type voidtype;
78 	this()
79 	{
80 		voidtype = TypeMng.getSingleton().getType("TypeVoid");
81 		mRetType = voidtype;
82 		super("LEFT", "Ant turns counterclockwise", ArgsStyle.NULAR_STYLE);
83 	}
84 	
85 	override Argument apply(IndAbstract individ, Line line, WorldAbstract world)
86 	{
87 		//writeln("Appling TurnLeft");
88 		auto ind = cast(Ant)(individ);
89 		//writeln("Casting success: ", ind !is null);
90 		final switch(ind.Direction)
91 		{
92 			case ind.Faces.NORTH:
93 				ind.Direction = ind.Faces.WEST;
94 				break;
95 			case ind.Faces.SOUTH:
96 				ind.Direction = ind.Faces.EAST;
97 				break;
98 			case ind.Faces.EAST:
99 				ind.Direction = ind.Faces.NORTH;
100 				break;
101 			case ind.Faces.WEST:
102 				ind.Direction = ind.Faces.SOUTH;
103 				break;
104 		}
105 		return voidtype.getNewArg();
106 	}
107 }
108 
109 class TurnRight : Operator
110 {
111 	Type voidtype;
112 	this()
113 	{
114 		voidtype = TypeMng.getSingleton().getType("TypeVoid");
115 		mRetType = voidtype;
116 		super("RIGHT", "Ant turns clockwise", ArgsStyle.NULAR_STYLE);
117 	}
118 	
119 	override Argument apply(IndAbstract individ, Line line, WorldAbstract world)
120 	{
121 		//writeln("Appling TurnRight");
122 		auto ind = cast(Ant)(individ);
123 		//writeln("Casting success: ", ind !is null);
124 		final switch(ind.Direction)
125 		{
126 			case ind.Faces.NORTH:
127 				ind.Direction = ind.Faces.EAST;
128 				break;
129 			case ind.Faces.SOUTH:
130 				ind.Direction = ind.Faces.WEST;
131 				break;
132 			case ind.Faces.EAST:
133 				ind.Direction = ind.Faces.SOUTH;
134 				break;
135 			case ind.Faces.WEST:
136 				ind.Direction = ind.Faces.NORTH;
137 				break;
138 		}
139 		return voidtype.getNewArg();
140 	}
141 }
142 
143 
144 
145