File: Ghost2.cc

package info (click to toggle)
hannah 1.0-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,712 kB
  • ctags: 281
  • sloc: cpp: 2,210; sh: 214; makefile: 117
file content (191 lines) | stat: -rw-r--r-- 4,572 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "Ghost2.h"
#include "time.h"
#include "math.h"
#include <string>

class Ghost2;

Ghost2::Ghost2(char* filename, int x, int y, int speed, AnimationFactory* af) : Ghost(filename,x,y,speed, af){

	this->speed = speed;
	this->startx = x;
	this->starty = y;
	direction = 2;
	xpos = 50 + (x*SIZET);
	ypos = 50 + (y*SIZET);
	nextypos = ypos;
	nextxpos = xpos;
	// annoyingly must currently be a divisor of 30
	//speed = 5;
	
	int n; int j;
	for(j=0;j<SIZEY;j++){
		for(n=0; n<SIZEX;n++){
			this->lev.map[n][j] = lev.map[n][j];
		}
	}

	//printf("%c\n",this->map[1][1]);

	
};

//Ghost2::~Ghost2(){
//	int i;
//	for(i=0; i<frames.size(); i++){
//		SDL_FreeSurface(frames[i]);
//	}
//};

// x & y are pacman's current grid positions
void Ghost2::move(int x, int y){
	if(timeleft>0) timeleft--; else effect = ' ';
	if(respawntime>0){
		respawntime--;
	} else {
	// pacman's X / Y
	int pacx = (x / SIZET) - 1;
	int pacy = (y / SIZET) - 1;
	// ghost's X / Y
	int ax = (xpos / SIZET) - 1;
	int ay = (ypos / SIZET) - 1;

	bool infront = false; // pac is above
	bool left = false; // pac is left

	// determine which direction pacman is in
	switch(direction){
		case 1: if(pacy < ay){
							infront = true;
						} else {
							infront = false;
						}
						if(pacx < ax){
							left = true;
						} else {
							left = false;
						} break;
		case 2: if(pacy < ay){
							left = true;
						} else {
							left = false;
						}
						if(pacx < ax){
							infront = false;
						} else {
							infront = true;
						} break;
		case 3:	if(pacy < ay){
							infront = false;
						} else {
							infront = true;
						}
						if(pacx < ax){
							left = false;
						} else {
							left = true;
						} break;
		case 4: if(pacy < ay){
							left = false;
						} else {
							left = true;
						}
						if(pacx < ax){
							infront = true;
						} else {
							infront = false;
						} break;
	};


	// need to ensure it keeps going forwards unless interrupted

	if(nextxpos == xpos && nextypos == ypos){
		// actual position matches wanted position, so 
		// pacman is in square, so move
		int lft = 0;
		int rit = 0;
		int back = 0;
		lft = direction - 1;		
		if(lft == 0) lft = 4;
		rit = direction + 1;
		if(rit == 5) rit = 1;
		back = direction - 2;
		if(back < 1) back = (0 - back) + (2 * direction);

		int newdirection = 0; // test left right and set as newdirection
		// temporarily. Then test if straight on. If straight on is ok,
		// take 50-50 chance between straight on and a turn.

		//if(testdirection(direction)==false){
			// cannot go forwards, try left and right...
			if(testdirection(lft) && !testdirection(rit)){
				// left is ok, right is blocked
				newdirection = lft;
			} else if(!testdirection(lft) && testdirection(rit)){
				// right is ok, left is blocked
				newdirection = rit;
			} else if(!testdirection(lft) && !testdirection(rit)){
				// left and right are blocked
				// ...go backwards
				newdirection = back;
			} else if(testdirection(lft) && testdirection(rit)){
				// left / right both ok. pick one at random.
				int rn = rand()%8;
				// random between 0 and 1 inclusive

				int biaseddirection = 0;
				int unbiaseddirection = 0;
				if(left){ biaseddirection = lft; unbiaseddirection = rit; }
				if(!left){ biaseddirection = rit; unbiaseddirection = lft; }

				if(rn==0){
					newdirection = unbiaseddirection;
				} else {
					newdirection = biaseddirection;
				}
			}

		if(!testdirection(direction)){
			direction = newdirection;
		} else if(testdirection(direction) && !testdirection(rit)  
					&& !testdirection(lft)){ 
			// only forward and back are available, go forwards.
			direction = direction;
		} else {
			// 50% move forwards, 50% turn
			int rn = rand()%8;
		
			int biaseddirection = 0;
			int unbiaseddirection = 0;
			if(infront){ biaseddirection = direction; unbiaseddirection = newdirection; }
			if(!infront){ biaseddirection = newdirection; unbiaseddirection = direction; }

			if(rn==0){
				direction = unbiaseddirection;
			} else {
				direction = biaseddirection;
			}
		}
	
		switch(direction){
			case 1:	nextypos = lev.map[ax][ay-1].t; break;
			case 2: nextxpos = lev.map[ax+1][ay].l; break;
			case 3: nextypos = lev.map[ax][ay+1].t; break;
			case 4: nextxpos = lev.map[ax-1][ay].l; break;
		};

	} else if(nextxpos!=xpos){
		switch(direction){
			case 4: xpos = xpos - speed; break;
			case 2: xpos = xpos + speed; break;
		};
	} else if(nextypos!=ypos){
		switch(direction){
			case 1: ypos = ypos - speed; break;
			case 3: ypos = ypos + speed; break;
		};
	}
}
};