1 #include "Ghost3.h"
2 #include "time.h"
3 #include "math.h"
4 #include <string>
5 
6 class Ghost3;
7 
Ghost3(char * filename,int x,int y,int speed,AnimationFactory * af)8 Ghost3::Ghost3(char* filename, int x, int y, int speed, AnimationFactory* af) : Ghost(filename,x,y,speed, af){
9 
10 	this->speed = speed;
11 	this->startx = x;
12 	this->starty = y;
13 
14 		direction = 2;
15 		xpos = 50 + (x*SIZET);
16 		ypos = 50 + (y*SIZET);
17 		nextypos = ypos;
18 		nextxpos = xpos;
19 		// annoyingly must currently be a divisor of 30
20 		speed = 5;
21 
22 		int n; int j;
23 		for(j=0;j<SIZEY;j++){
24 			for(n=0; n<SIZEX;n++){
25 				this->lev.map[n][j] = lev.map[n][j];
26 			}
27 		}
28 
29 		//printf("%c\n",this->map[1][1]);
30 
31 
32 };
33 
34 //Ghost3::~Ghost3(){
35 //	int i;
36 //	for(i=0; i<frames.size(); i++){
37 //		SDL_FreeSurface(frames[i]);
38 //	}
39 //};
40 
41 // x & y are pacman's current grid positions
move(int x,int y)42 void Ghost3::move(int x, int y){
43 	if(timeleft>0) timeleft--; else effect = ' ';
44 	if(respawntime>0){
45 		respawntime--;
46 	} else {
47 	// pacman's X / Y
48 	int pacx = (x / SIZET) - 1;
49 	int pacy = (y / SIZET) - 1;
50 	// ghost's X / Y
51 	int ax = (xpos / SIZET) - 1;
52 	int ay = (ypos / SIZET) - 1;
53 
54 	bool infront = false; // pac is above
55 	bool left = false; // pac is left
56 
57 	// determine which direction pacman is in
58 	switch(direction){
59 		case 1: if(pacy < ay){
60 							infront = true;
61 						} else {
62 							infront = false;
63 						}
64 						if(pacx < ax){
65 							left = true;
66 						} else {
67 							left = false;
68 						} break;
69 		case 2: if(pacy < ay){
70 							left = true;
71 						} else {
72 							left = false;
73 						}
74 						if(pacx < ax){
75 							infront = false;
76 						} else {
77 							infront = true;
78 						} break;
79 		case 3:	if(pacy < ay){
80 							infront = false;
81 						} else {
82 							infront = true;
83 						}
84 						if(pacx < ax){
85 							left = false;
86 						} else {
87 							left = true;
88 						} break;
89 		case 4: if(pacy < ay){
90 							left = false;
91 						} else {
92 							left = true;
93 						}
94 						if(pacx < ax){
95 							infront = true;
96 						} else {
97 							infront = false;
98 						} break;
99 	};
100 
101 
102 	// need to ensure it keeps going forwards unless interrupted
103 
104 	if(nextxpos == xpos && nextypos == ypos){
105 		// actual position matches wanted position, so
106 		// pacman is in square, so move
107 		int lft = 0;
108 		int rit = 0;
109 		int back = 0;
110 		lft = direction - 1;
111 		if(lft == 0) lft = 4;
112 		rit = direction + 1;
113 		if(rit == 5) rit = 1;
114 		back = direction - 2;
115 		if(back < 1) back = (0 - back) + (2 * direction);
116 
117 		int newdirection = 0; // test left right and set as newdirection
118 		// temporarily. Then test if straight on. If straight on is ok,
119 		// take 50-50 chance between straight on and a turn.
120 
121 		//if(testdirection(direction)==false){
122 			// cannot go forwards, try left and right...
123 			if(testdirection(lft) && !testdirection(rit)){
124 				// left is ok, right is blocked
125 				newdirection = lft;
126 			} else if(!testdirection(lft) && testdirection(rit)){
127 				// right is ok, left is blocked
128 				newdirection = rit;
129 			} else if(!testdirection(lft) && !testdirection(rit)){
130 				// left and right are blocked
131 				// ...go backwards
132 				newdirection = back;
133 			} else if(testdirection(lft) && testdirection(rit)){
134 				// left / right both ok. pick one at random.
135 				// int rn = rand()%8;
136 				// random between 0 and 1 inclusive
137 
138 				int biaseddirection = 0;
139 				int unbiaseddirection = 0;
140 				if(left){ biaseddirection = lft; unbiaseddirection = rit; }
141 				if(!left){ biaseddirection = rit; unbiaseddirection = lft; }
142 
143 				/* This Ghost will always choose the best route, so ignore
144 					the random numbers
145 				if(rn==0){
146 					newdirection = unbiaseddirection;
147 				} else {
148 					newdirection = biaseddirection;
149 				} */
150 
151 				newdirection = biaseddirection;
152 			}
153 
154 		if(!testdirection(direction)){
155 			direction = newdirection;
156 		} else if(testdirection(direction) && !testdirection(rit)
157 					&& !testdirection(lft)){
158 			// only forward and back are available, go forwards.
159 			direction = direction;
160 		} else {
161 			// 50% move forwards, 50% turn
162 			// int rn = rand()%8;
163 
164 			int biaseddirection = 0;
165 			int unbiaseddirection = 0;
166 			if(infront){ biaseddirection = direction; unbiaseddirection = newdirection; }
167 			if(!infront){ biaseddirection = newdirection; unbiaseddirection = direction; }
168 
169 			/* Again, ignore the random number and choose the best direction
170 			if(rn==0){
171 				direction = unbiaseddirection;
172 			} else {
173 				direction = biaseddirection;
174 			}*/
175 
176 			direction = biaseddirection;
177 		}
178 
179 		switch(direction){
180 			case 1:	nextypos = lev.map[ax][ay-1].t; break;
181 			case 2: nextxpos = lev.map[ax+1][ay].l; break;
182 			case 3: nextypos = lev.map[ax][ay+1].t; break;
183 			case 4: nextxpos = lev.map[ax-1][ay].l; break;
184 		};
185 
186 	} else if(nextxpos!=xpos){
187 		switch(direction){
188 			case 4: xpos = xpos - speed; break;
189 			case 2: xpos = xpos + speed; break;
190 		};
191 	} else if(nextypos!=ypos){
192 		switch(direction){
193 			case 1: ypos = ypos - speed; break;
194 			case 3: ypos = ypos + speed; break;
195 		};
196 	}
197 }
198 };
199 
200