Try using this code:
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
Trace(state);
Trace(direction);
Trace(1000);
if (state == 0){
Trace(1100);
if (direction == 0){ //Clockwise behavior
Trace(1110);
if (!canMove(this->X, this->Y - 16)){Trace(1111); state = 3;}
Trace(1120);
if (canMove(this->X + 16, this->Y)){Trace(1121); state = 1;}
}
else{ // Counter-clockwise behavior
Trace(1210);
if (!canMove(this->X, this->Y - 16)){Trace(1211); state = 1;}
Trace(1220);
if (canMove(this->X - 16, this->Y)){Trace(1221); state = 3;}
}
}
Trace(state);
Trace(direction);
Trace(2000);
if (state == 1){
Trace(2100);
if (direction == 0){ //Clockwise behavior
Trace(2110);
if (!canMove(this->X + 16, this->Y)){Trace(2111); state = 0;}
Trace(1120);
if (canMove(this->X, this->Y+16)){Trace(2121); state = 2;}
}
else{ // Counter-clockwise behavior
Trace(2210);
if (!canMove(this->X + 16, this->Y)){Trace(2211); state = 2;}
Trace(2220);
if (canMove(this->X, this->Y - 16)){Trace(2221); state = 0;}
}
}
Trace(state);
Trace(direction);
Trace(3000);
if (state == 2){
Trace(3100);
if (direction == 0){ //Clockwise behavior
Trace(3110);
if (!canMove(this->X, this->Y+16)){Trace(3111); state = 1;}
Trace(3120);
if (canMove(this->X - 16, this->Y)){Trace(3121); state = 3;}
}
else{ // Counter-clockwise behavior
Trace(3210);
if (!canMove(this->X, this->Y + 16)){Trace(3211); state = 3;}
Trace(3220);
if (canMove(this->X + 16, this->Y)){Trace(3221); state = 1;}
}
}
Trace(state);
Trace(direction);
Trace(4000);
if (state == 3){
Trace(4100);
if (direction == 0){ //Clockwise behavior
Trace(4110);
if (!canMove(this->X - 16, this->Y)){Trace(4111); state = 2;}
Trace(4120);
if (canMove(this->X, this->Y - 16)){Trace(4121); state = 0;}
}
else{ // Counter-clockwise behavior
Trace(4210);
if (!canMove(this->X - 16, this->Y)){Trace(4211); state = 0;}
Trace(4220);
if (canMove(this->X, this->Y + 16)){Trace(4221); state = 2;}
}
}
Trace(state);
Trace(direction);
Trace(5000);
// 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; }
Trace(6000);
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
It's the same code you had except with a bunch of trace statements in there so you can follow the flow of the code. I noticed that on my test, the state basically went through this sequence: 0, 1, 2, waitframe, 3, 0, waitframe, 1, 2, waitframe, 3, 0, waitframe, etc. Hope that helps.