OK, here are a bunch of skeleton movement scripts I cooked up on the way here on the metro; note that these are UNTESTED and incomplete, but these algorithms are hopefully enough to get you started. A couple of points:
1. For each of these you need to write and include a function canMove(), which can be more or less complicated depending on the fidelity of walkability detection you want to use. For no detection, make the function always return true.
2. In these scripts, if trying to move in a random direction fails, a new random direction is chosen, until movement succeeds. Note that this is approximative and not particularly clever; I couldn't think of a simple way to generate permutations without more advanced data structures or excessive pain. As a consequence, when choosing a new direction, I've put in code to prevent the script from hanging if the FFC is completely stuck; set NUMTRIES as desired. Higher values require more CPU time, but increase the probability of a legal direction being chosen, if one exists. Lower values take less time but increase the probability of the FFC doing nothing for several frames when it hits an unwalkable boundary.
Since this computer has a tendency to crash unexpectedly, I'll be entering these one at a time; please patientez.
1. A random walk in 2 dimensions. Very erratic movement. speed is the number of pixels the FFC is to move each frame.
Code:
ffc script rwalk2d {
void run(int speed) {
int x=this->X;
int y=this->Y;
int safety=NUMTRIES;
while(true) {
if(safety == 0) {
safety = NUMTRIES;
WaitFrame();
}
int deg=Rand(360);
int newx=x+speed*Sin(deg);
int newy=y+speed*Cos(deg);
if(canMove(newx,newy)) {
x=newx;
this->X=x;
y=newy;
this->Y=y;
safety=NUMTRIES;
WaitFrame();
}
else safety--;
}
}
}
2. Two simultaneous random walks in 1 dimension. Very erratic movement. Speed is the number of pixels per frame the FFC moves in each direction.
Code:
ffc script rwalk2x1d {
void run(int speed) {
int x=this->X;
int y=this->Y;
int safety=NUMTRIES;
while(true) {
if(safety == 0) {
safety = NUMTRIES;
WaitFrame();
}
int dx=2*Rand(2)-1;
int dy=2*Rand(2)-1;
int newx=x+speed*dx;
int newy=y+speed*dy;
if(canMove(newx,newy)) {
x=newx;
this->X=x;
y=newy;
this->Y=y;
safety=NUMTRIES;
WaitFrame();
}
else safety--;
}
}
}
3. The FFC continues in a straight line along a cardinal direction until it hits a wall, at which time it chooses a new direction. Speed is the number of pixels to move per frame.
Code:
ffc script wall2wallrect {
void run(int speed) {
int x=this->X;
int y=this->Y;
int safety=NUMTRIES;
int dir=Rand(4);
while(true) {
if(safety == 0) {
safety = NUMTRIES;
WaitFrame();
}
int newx=x;
int newy=y;
if(dir==0) newx+=speed;
if(dir==1) newx-=speed;
if(dir==2) newy+=speed;
if(dir==3) newy-=speed;
if(canMove(newx,newy)) {
x=newx;
this->X=x;
y=newy;
this->Y=y;
safety=NUMTRIES;
WaitFrame();
}
else {
dir=Rand(4);
safety--;
}
}
}
}
4. The FFC starts moving in a cardinal direction, and every few frames, there is a chance the FFC will choose a new random direction. Speed is the number of pixels the FFC moves per frame, interval is the number of frames that pass before the FFC has a chance to change direction, and prob is the percent chance, from 0 to 100, of the FFC changing direction at that time.
Code:
ffc script randrect {
void run(int speed, int interval, int prob) {
int x=this->X;
int y=this->Y;
int safety=NUMTRIES;
int dir=Rand(4);
int counter=0;
while(true) {
if(safety == 0) {
safety = NUMTRIES;
WaitFrame();
}
int newx=x;
int newy=y;
if(counter==0) {
int p = Rand(100);
if(p<prob) dir=Rand(4);
}
if(dir==0) newx+=speed;
if(dir==1) newx-=speed;
if(dir==2) newy+=speed;
if(dir==3) newy-=speed;
if(canMove(newx,newy)) {
x=newx;
this->X=x;
y=newy;
this->Y=y;
safety=NUMTRIES;
counter=(counter+1)%interval;
WaitFrame();
}
else {
dir=Rand(4);
safety--;
}
}
}
}
5. The FFC bounces around the screen; when it hits the wall, it chooses a new random angle. Speed is the number of pixels the FFC moves per frame.
Code:
ffc script wall2wall2d {
void run(int speed) {
int x=this->X;
int y=this->Y;
int safety=NUMTRIES;
int ang=Rand(360);
while(true) {
if(safety == 0) {
safety = NUMTRIES;
WaitFrame();
}
int newx=x+speed*Sin(ang);
int newy=y+speed*Cos(ang);
if(canMove(newx,newy)) {
x=newx;
this->X=x;
y=newy;
this->Y=y;
safety=NUMTRIES;
WaitFrame();
}
else {
ang=Rand(360);
safety--;
}
}
}
}
6. As script 4, but the FFC is not restricted to the cardinal directions.
Code:
ffc script rand2d {
void run(int speed, int interval, int prob) {
int x=this->X;
int y=this->Y;
int safety=NUMTRIES;
int ang=Rand(360);
int counter=0;
while(true) {
if(safety == 0) {
safety = NUMTRIES;
WaitFrame();
}
if(counter==0) {
int p = Rand(100);
if(p<prob) dir=Rand(360);
}
int newx=x+speed*Sin(ang);
int newy=y+speed*Cos(ang);
if(canMove(newx,newy)) {
x=newx;
this->X=x;
y=newy;
this->Y=y;
safety=NUMTRIES;
counter=(counter+1)%interval;
WaitFrame();
}
else {
ang=Rand(360);
safety--;
}
}
}
}
7. The FFC constantly accelerates towards link, until it hits a solid obstacle. This FFC should be fairly easy to dodge from far away. Initv is the initial velocity of the FFC, acc is the magnitude of the FFC's acceleration each frame.
Code:
ffc script seekacc {
void run(int initv, int acc) {
int dx = Link->X-this->X;
int dy = Link->Y-this->Y;
int scale = initv/Sqrt(dx*dx+dy*dy);
this->Vx = dx*scale;
this->Vy = dy*scale;
while(true) {
if(!canMove(this->X, this->Y)) {
this->Vx=0;
this->Vy=0;
this->Ax=0;
this->Ay=0;
Quit();
}
dx = Link->X-this->X;
dy = Link->Y-this->Y;
scale = acc/Sqrt(dx*dx+dy*dy);
this->Ax = dx*scale;
this->Ay = dy*scale;
WaitFrame();
}
}
}
8. This FFC homes in on Link while keeping constant velocity; this script is HIGHLY UNTESTED as the geometrical derivation was somewhat complicated. Initv is again the initial velocity, in pixels per frame, and maxacc is the FFC's maximum turning acceleration. The higher this acceleration, the better the homing.
Code:
ffc script seekconst {
void run(int initv, int maxacc) {
int dx = Link->X-this->X;
int dy = Link->Y-this->Y;
int scale = initv/Sqrt(dx*dx+dy*dy);
this->Vx = dx*scale;
this->Vy = dy*scale;
while(true) {
if(!canMove(this->X, this->Y)) {
this->Vx=0;
this->Vy=0;
this->Ax=0;
this->Ay=0;
Quit();
}
dx = Link->X-this->X;
dy = Link->Y-this->Y;
int distsqr = dx*dx+dy*dy;
int speed = Sqrt(this->Vx*this->Vx+this->Vy*this->Vy);
int acc = 2*speed*(this->Vx*dy-this->Vy*dx)/distsqr;
if(Abs(acc) > maxacc) {
if(acc>0) acc=maxacc;
else acc = -maxacc;
}
this->Ax = -this->Vy*acc/speed;
this->Ay = this->Vx*acc/speed;
WaitFrame();
}
}
}
Feel free to post enhancements or bug fixes to these scripts, or movement scripts of your own.