C-Dawg
10-19-2006, 10:00 AM
I'm having some difficulty getting my wall-hugger to work. Here's the code:
--------------------------------------------------------------------
ffc script wall_hugger {
void run() {
// CONSTANTS (use to change FFC behavior without modifying code)
int speed = 1; // The wall-hugger's speed
int direction = 0; // The wall-hugger's direction
// 0 = Clockwise
// 1 = Counter-clockwise
// VARIABLES (used by the code; do not change)
int state = 0; // Determines the direction of travel
// 0 = North
// 1 = East
// 2 = South
// 3 = West
while(true){
// Check for changes in direction
if (state == 0){
if (direction == 0){ //Clockwise behavior
if (!canMove(this->X, this->Y - 16)){state = 3;}
if (canMove(this->X + 16, this->Y)){state = 1;}
}
else{ // Counter-clockwise behavior
if (!canMove(this->X, this->Y - 16)){state = 1;}
if (canMove(this->X - 16, this->Y)){state = 3;}
}
}
if (state == 1){
if (direction == 0){ //Clockwise behavior
if (!canMove(this->X + 16, this->Y)){state = 0;}
if (canMove(this->X, this->Y+16)){state = 2;}
}
else{ // Counter-clockwise behavior
if (!canMove(this->X + 16, this->Y)){state = 2;}
if (canMove(this->X, this->Y - 16)){state = 0;}
}
}
if (state == 2){
if (direction == 0){ //Clockwise behavior
if (!canMove(this->X, this->Y+16)){state = 1;}
if (canMove(this->X - 16, this->Y)){state = 3;}
}
else{ // Counter-clockwise behavior
if (!canMove(this->X, this->Y + 16)){state = 3;}
if (canMove(this->X + 16, this->Y)){state = 1;}
}
}
if (state == 3){
if (direction == 0){ //Clockwise behavior
if (!canMove(this->X - 16, this->Y)){state = 2;}
if (canMove(this->X, this->Y - 16)){state = 0;}
}
else{ // Counter-clockwise behavior
if (!canMove(this->X - 16, this->Y)){state = 0;}
if (canMove(this->X, this->Y + 16)){state = 2;}
}
}
// After determining direction, move.
if (state == 0) { this->Vy = -speed; }
if (state == 1) { this->Vy = speed; }
if (state == 2) { this->Vy = speed; }
if (state == 3) { this->Vx = -speed; }
Waitframe();
} //end of while loop
} //end of void run()
// Collision detection function
bool canMove(int x, int y){
if(x<0 || x>240 || y<0 || y>160)
return false;
return Screen->ComboS[y+(x>>4)]==0;
} // end of bool canMove
} // end of ffc script
-------------------------------------------------
Instead of acting like it should, the wall-hugger will act, well, unpredictably. If there is an unwalkable combo north of it, it will vibrate widely and then wander off the screen to the left. If there is not, it will vibrate and stay in one place. I can't figure out what's wrong with the logic in the code.
Essentially what the little guy is supposed to do is start facing north. Each tic he is supposed to first check if there is a block in front of him, and then check if there is an opening to his right or left, depending on whether he's going to prefer moving clockwise or counterclockwise. If there's a block in front of him, he'll go around it, but he'll prefer to go into openings that match his rotation.
Anyone see a problem here?
I
--------------------------------------------------------------------
ffc script wall_hugger {
void run() {
// CONSTANTS (use to change FFC behavior without modifying code)
int speed = 1; // The wall-hugger's speed
int direction = 0; // The wall-hugger's direction
// 0 = Clockwise
// 1 = Counter-clockwise
// VARIABLES (used by the code; do not change)
int state = 0; // Determines the direction of travel
// 0 = North
// 1 = East
// 2 = South
// 3 = West
while(true){
// Check for changes in direction
if (state == 0){
if (direction == 0){ //Clockwise behavior
if (!canMove(this->X, this->Y - 16)){state = 3;}
if (canMove(this->X + 16, this->Y)){state = 1;}
}
else{ // Counter-clockwise behavior
if (!canMove(this->X, this->Y - 16)){state = 1;}
if (canMove(this->X - 16, this->Y)){state = 3;}
}
}
if (state == 1){
if (direction == 0){ //Clockwise behavior
if (!canMove(this->X + 16, this->Y)){state = 0;}
if (canMove(this->X, this->Y+16)){state = 2;}
}
else{ // Counter-clockwise behavior
if (!canMove(this->X + 16, this->Y)){state = 2;}
if (canMove(this->X, this->Y - 16)){state = 0;}
}
}
if (state == 2){
if (direction == 0){ //Clockwise behavior
if (!canMove(this->X, this->Y+16)){state = 1;}
if (canMove(this->X - 16, this->Y)){state = 3;}
}
else{ // Counter-clockwise behavior
if (!canMove(this->X, this->Y + 16)){state = 3;}
if (canMove(this->X + 16, this->Y)){state = 1;}
}
}
if (state == 3){
if (direction == 0){ //Clockwise behavior
if (!canMove(this->X - 16, this->Y)){state = 2;}
if (canMove(this->X, this->Y - 16)){state = 0;}
}
else{ // Counter-clockwise behavior
if (!canMove(this->X - 16, this->Y)){state = 0;}
if (canMove(this->X, this->Y + 16)){state = 2;}
}
}
// After determining direction, move.
if (state == 0) { this->Vy = -speed; }
if (state == 1) { this->Vy = speed; }
if (state == 2) { this->Vy = speed; }
if (state == 3) { this->Vx = -speed; }
Waitframe();
} //end of while loop
} //end of void run()
// Collision detection function
bool canMove(int x, int y){
if(x<0 || x>240 || y<0 || y>160)
return false;
return Screen->ComboS[y+(x>>4)]==0;
} // end of bool canMove
} // end of ffc script
-------------------------------------------------
Instead of acting like it should, the wall-hugger will act, well, unpredictably. If there is an unwalkable combo north of it, it will vibrate widely and then wander off the screen to the left. If there is not, it will vibrate and stay in one place. I can't figure out what's wrong with the logic in the code.
Essentially what the little guy is supposed to do is start facing north. Each tic he is supposed to first check if there is a block in front of him, and then check if there is an opening to his right or left, depending on whether he's going to prefer moving clockwise or counterclockwise. If there's a block in front of him, he'll go around it, but he'll prefer to go into openings that match his rotation.
Anyone see a problem here?
I