-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMazeManipulation.c
220 lines (178 loc) · 4.84 KB
/
MazeManipulation.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//Contains functions to manipulate the Maze Array. Reads and writes cell visits and cell walls. Set which robot mapped a particular cell. Reads and writes current robot coordinates and robot heading
#include "MazeManipulation.h"
#include "adc.h"
#include "Config.h"
#include "led.h"
#include "initPorts.h"
extern unsigned int Maze [MAZEY][MAZEX];
extern unsigned int Mouse; //contains heading and xy position of mouse
void AddWall(unsigned char dir, unsigned char x, unsigned char y )
{
Maze[y][x] |= dir;
//if (x>0) Maze[y][x-1] |= (dir & NORTH); //N
//if (y<(MAZEY-1)) Maze[y+1][x] |= (dir & EAST ); //E
//if (x<(MAZEX-1)) Maze[y][x+1] |= (dir & SOUTH); //S
//if (y>0) Maze[y-1][x] |= (dir & WEST); //W
}
void ClearMaze (void)
{
unsigned char x = 0;
unsigned char y = 0;
//clear out old maze and set all to zero
while (y < MAZEY)
{
while (x < MAZEX)
{
Maze[y][x] = 0;
x++;
}
y++;
x=0;
}
/* // Add outside walls around maze
for (x=0, y=0; x<MAZEX ; x++) //Top Walls
{
Maze[y][x] |= NORTH;
}
for (x=0, y=0; y<MAZEY ; y++) //Left Walls
{
Maze[y][x] |= WEST;
}
for (x=(MAZEX-1), y=0; y<MAZEY ; y++) //Right Walls
{
Maze[y][x] |= EAST;
}
for (x=0, y=(MAZEX-1); x<MAZEX ; x++) //Bottom Walls
{
Maze[y][x] |= SOUTH;
}
*/
}
unsigned char GetMouseX( void )
{
return ( (unsigned char)(Mouse & (0xF)) );
}
unsigned char GetMouseY( void )
{
return ( (unsigned char)( (Mouse>>4)&(0xF)) );
}
unsigned char GetMouseHeading( void )
{
return ( (unsigned char)( (Mouse>>8)&(0xF)) );
}
unsigned char GetVisits( unsigned char MouseX, unsigned char MouseY )
{
return ( (unsigned char)((Maze[MouseY][MouseX]>>12)&(0xF)) );
}
unsigned char GetWalls( unsigned char MouseX, unsigned char MouseY )
{
return ( (unsigned char)((Maze[MouseY][MouseX])&(0xF)) );
}
unsigned char GetExits( unsigned char MouseX, unsigned char MouseY )
{
return ( (unsigned char)((~(Maze[MouseY][MouseX]))&(0xF)) );
}
void SetMouseX( unsigned char pos )
{
Mouse &= 0xFF0;
Mouse |= (pos & 0xF);
}
void SetMouseY( unsigned char pos )
{
Mouse &= 0xF0F;
Mouse |= ((pos & 0xF)<<4);
}
void SetMouseHeading( unsigned char pos )
{
Mouse &= 0x0FF;
Mouse |= ((pos & 0xF)<<8);
}
void SetVisits( unsigned char MouseX, unsigned char MouseY ,unsigned char visits )
{
Maze[MouseY][MouseX] &= 0x0FFF;
Maze[MouseY][MouseX] |= ((visits & 0xF) << 12);
}
void SetWalls( unsigned char MouseX, unsigned char MouseY ,unsigned char Walls )
{
Maze[MouseY][MouseX] &= 0xFFF0;
Maze[MouseY][MouseX] |= (Walls & 0xF);
}
unsigned char ShiftLeftWrapNibble ( unsigned char data)
{
unsigned char temp=0;
temp = ((unsigned char)((data & 0xF)));
temp <<=1;
temp |= ( (unsigned char)(temp >>4) );
temp &= (0xF);
return temp;
}
unsigned char ShiftRightWrapNibble ( unsigned char data)
{
unsigned char temp=0;
temp = ((unsigned char)((data & 0xF)<<4));
temp >>=1;
temp |= ( (unsigned char)(temp <<4) );
temp >>=4;
return temp;
}
unsigned char IsWallLeft()
{
unsigned char MinDist = 40;
unsigned char MaxDist = 80;
unsigned char dist = GetDistLeft();
if ( (dist >= MinDist) && (dist <= MaxDist ) ) return 1;
else return 0;
}
unsigned char IsWallRight()
{
unsigned char MinDist = 40;
unsigned char MaxDist = 80;
unsigned char dist = GetDistRight();
if ( (dist >= MinDist) && (dist <= MaxDist ) ) return 1;
else return 0;
}
unsigned char IsWallFront()
{
unsigned char MinDist = 40;
unsigned char MaxDist = 60;
unsigned char dist = GetDistFront();
if ( (dist >= MinDist) && (dist <= MaxDist ) ) return 1;
else return 0;
}
unsigned char IsWallBack()
{
/*unsigned char MinDist = 40;
unsigned char MaxDist = 70;
unsigned char dist = GetDistBack();
if ( (dist >= MinDist) && (dist <= MaxDist ) ) return 1;
else */return 0;
}
unsigned char GetCellMappedBy(unsigned char x, unsigned char y)
{
unsigned char temp = (Maze[y][x] >> 4);
temp &= 0x0f;
return temp;
}
void SetCellMappedByThisMouse(unsigned char x, unsigned char y)
{
unsigned int number = (MOUSENUMBER << 4); //move mousenumber to correct position
number &= 0x00F0; // make sure that mousenumber does not affect any other bits
Maze[y][x] &= 0xFF0F; // clear mousenumber bits
Maze[y][x] |= number;
}
void SetCellMappedBy(unsigned char x, unsigned char y, unsigned char MouseNumber)
{
unsigned int number = (MouseNumber << 4); //move mousenumber to correct position
number &= 0x00F0; // make sure that mousenumber does not affect any other bits
Maze[y][x] &= 0xFF0F; // clear mousenumber bits
Maze[y][x] |= number;
}
void SetThisCellMappedByThisMouse(void)
{
unsigned int number = (MOUSENUMBER << 4); //move mousenumber to correct position
unsigned char MouseX = GetMouseX();
unsigned char MouseY = GetMouseY();
number &= 0x00F0; // make sure that mousenumber does not affect any other bits
Maze[MouseY][MouseX] &= 0xFF0F; // clear mousenumber bits
Maze[MouseY][MouseX] |= number;
}