Omnimaga
Calculator Community => TI Calculators => Calculator C => Topic started by: fb39ca4 on July 28, 2010, 04:44:57 pm
-
I've been working on a 3d maze program that uses bwang's ncaster. Right now, I have finished the code to generate the maze, but it is crashing my calc. Right now, it is just placed inside main(), to test that it doesn't result in any infinite loops.
Here's the code: (WARNING: VERY unoptimmized)
void generate() {
int a, b;
short int maze[11][11];
for (a = 0; a == 10; a++) {
for (b = 0; b == 10; b++) {
maze[a][b] = 15;
if (a == 0)
maze[a][b] += 16;
if (a == 10)
maze[a][b] += 64;
if (b == 0)
maze[a][b] += 32;
if (b == 10)
maze[a][b] += 128;
}
}
int success = 0;
int x = 1;
int y = 1;
char new[4];
while (!isKeyPressed(KEY_NSPIRE_ESC)) {
if (!(maze[x][y] & 0x0010)) {
if ((maze[x][y] & 0x000F) == 15){
new[0] = 1;
}
}
if (!(maze[x][y] & 0x0020)) {
if ((maze[x][y] & 0x000F) == 15){
new[1] = 1;
}
}
if (!(maze[x][y] & 0x0040)) {
if ((maze[x][y] & 0x000F) == 15){
new[2] = 1;
}
}
if (!(maze[x][y] & 0x0080)) {
if ((maze[x][y] & 0x000F) == 15){
new[3] = 1;
}
}
if (new[0] || new[1] || new[2] || new[3]) {
while (!success) {
a = random();
if (new[a]) {
success = 1;
new[0] = new[1] = new[2] = new[3] = 0;
}
}
}
else
if ((maze[x][y] & 0xF000) == 0) {
break;
if ((maze[x][y] & 0x1000)) {
y--;
}
if ((maze[x][y] & 0x2000)) {
x++;
}
if ((maze[x][y] & 0x4000)) {
y++;
}
if ((maze[x][y] & 0x8000)) {
y--;
}
continue;
}
if (a == 0) {
maze[x][y] = ~(maze[x][y] & 0x0001);
y++;
maze[x][y] = ~(maze[x][y] & 0x0004);
maze[x][y] = ~(maze[x][y] & 0x1000);
}
if (a == 1) {
maze[x][y] = ~(maze[x][y] & 0x0002);
x++;
maze[x][y] = ~(maze[x][y] & 0x0008);
maze[x][y] = ~(maze[x][y] & 0x2000);
}
if (a == 2) {
maze[x][y] = ~(maze[x][y] & 0x0004);
y--;
maze[x][y] = ~(maze[x][y] & 0x0001);
maze[x][y] = ~(maze[x][y] & 0x4000);
}
if (a == 3) {
maze[x][y] = ~(maze[x][y] & 0x0008);
x--;
maze[x][y] = ~(maze[x][y] & 0x0002);
maze[x][y] = ~(maze[x][y] & 0x8000);
}
}
}
-
Why is your for loop for (a = 0; a == 10; a++)? Shouldn't it be for (a = 0; a <= 10; a++)?
-
I didn't think it would be a difference. Even so, the calc still crashes with <=.
-
Do you have a link to the algorithm you're using? The only thing I can think of at the moment would be x, y going out of bounds.
-
In this code:
int a, b;
short int maze[11][11];
for (a = 0; a == 10; a++) {
for (b = 0; b == 10; b++) {
maze[a][b] = 15;
if (a == 0)
maze[a][b] += 16;
if (a == 10)
maze[a][b] += 64;
if (b == 0)
maze[a][b] += 32;
if (b == 10)
maze[a][b] += 128;
}
}
Could it be that you could be adding to a value that does not yet exist? I have never used anything like this in C (the values might initilize to 0, I am not sure), but if you are adding, say 16, to an empty variable, it might crash. If this is the case, just add:
int x,y;
for(x=0;x<11;x++) {
for(y=0;y<11;y++) {
maze[x][y]=0;
}
}
I will try to look at the code a little close later if this does not fix the problem. Good luck! :)
-
First off check your brackets and make sure they are ending in the right places. I havent had a chance to look at your code. Next thing check the bounds and make sure your not accessing something your not supposed too. If you could try and break it down in parts to make sure this for loop really is being populated correctly etc. Keep doing that until you pinpoint the problem. I'll take a look at your code later tonight if you still dont have it solved.
-
Sorry, I misread your code with this thought to be problem. My syntax (with spacing) is usually different for what I thought was the error. Sorry for any inconvenience.
-
Provided you are calling this method within main, everything looks good at a glance. I dont see anything out of place within your brackets. Break it down like I said to pinpoint the problem.
for (a = 0; a == 10; a++) {
for (b = 0; b == 10; b++) {
maze[a][b] = 15;
if (a == 0)
maze[a][b] += 16;
if (a == 10)
maze[a][b] += 64;
if (b == 0)
maze[a][b] += 32;
if (b == 10)
maze[a][b] += 128;
}
}
Also it looks like your overwriting the value immediately, though this may be your intention, I'm not sure.
-
I've been using visual studio to debug it, and have some errors with the random() function.
Also, if ypu want to see what this is supposed to do, look at this (http://www.mazeworks.com/mazegen/mazetut/index.htm) site.
-
The Nspire has no random() function; the one in Ndless is called rand().
That could be a possible reason for your crashes. I've experienced crashes when calling non-existent functions before.
-
I defined random() myself, because I was having trouble with the current rand() function.
-
Could you post the code to your random() function?
-
I've got it working now, after a few hours of debugging, and doing all the bitwise operations with macros, so now all I have to do is export the maze to ncaster format.
-
Yay! Now I hope Ncaster can handle the map...the current algorithm doesn't handle mazes well; it was more intended for open areas with structures that extend upward.
-
So will this mean a performance loss? Or is graphical errors? I've been getting some, though I hope it's just my own code.
EDIT:
I've done some more tinkering, and it seems that when the map is this: (also, height is the same, and alt is filled with zeroes)
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
grahical errors occur. (namely, nothing gets drawn but the sprites)
-
What is your texture list?
-
The one already there.
-
Post your code.
-
My generate function does not make changes to anything but its internal variables. Aside from calling generate once, the only other change I made was to testlevel.c, as I described above.
-
Post testlevel.c.
-
Here it is.
-
Your height array is incorrect, I think. The rows have been shifted a bit, so now the spaces on your map are getting the wrong height data.
-
I fixed that, and now I can see the blocks, but the height of the camera looks wrong, and I can't move around. I'm attaching the whole source directory.
-
I messed around with your code, and discovered that you are doing something really weird in maze.c, which messes up the external variables in testlevel.c. The Nspire's support for global variables is pretty delicate at the moment, so if something doesn't work, try moving the contents of testlevel.c into main().
You probably want to have generate() take paramenters, so it should look something like:
void generate(int* maze)
where maze is the array you are writing to.
Oh, and random thought: I feel slightly happier about Ncaster now. With a map that dense, I was expecting it to be unbearably slow, but apparently that's not happening :).