User Tag List

Page 2 of 2 FirstFirst 1 2
Results 11 to 17 of 17

Thread: Randomly moving around

  1. #11
    Octorok
    Join Date
    Mar 2006
    Posts
    271
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    1,625
    Level
    13
    vBActivity - Bars
    Lv. Percent
    55.24%

    Re: Randomly moving around

    Just, moving in any direction randomly at all. Like, it chooses a random number of frames (out of a set of intergers from 1 to 20 or something) and a random direction from North, North-North West, North West, West North West, West, North South West et cetera, and then changes direction when it hits the edge of a combo of ANY solidity.

  2. #12
    Lynel Revfan9's Avatar
    Join Date
    Jun 2005
    Location
    In front of a screen. I never leave.
    Age
    31
    Posts
    1,160
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    3,391
    Level
    18
    vBActivity - Bars
    Lv. Percent
    71.34%

    Re: Randomly moving around

    That's what my script did, except I need the Zasm equivilent of CANMOVE. Aside from that, it should work fine (it changes a random direction every so often, and starts going the other way when it hits a solid object).

  3. #13
    Octorok
    Join Date
    Mar 2006
    Posts
    271
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    1,625
    Level
    13
    vBActivity - Bars
    Lv. Percent
    55.24%

    Re: Randomly moving around

    Also, BTW, I need it to not go off the screen.

  4. #14
    Administrator DarkDragon's Avatar
    Join Date
    Oct 2001
    Posts
    6,228
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    11,029
    Level
    31
    vBActivity - Bars
    Lv. Percent
    8.65%

    Re: Randomly moving around

    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.

  5. #15
    Octorok
    Join Date
    Mar 2006
    Posts
    271
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    1,625
    Level
    13
    vBActivity - Bars
    Lv. Percent
    55.24%

    Re: Randomly moving around

    I tried number 1 and it said it could not parse instruction one.

  6. #16
    Administrator DarkDragon's Avatar
    Join Date
    Oct 2001
    Posts
    6,228
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    11,029
    Level
    31
    vBActivity - Bars
    Lv. Percent
    8.65%

    Re: Randomly moving around

    Are you sure you're compiling it correctly? Save it to a text file and then import it from the main ZScript dialog.

    Also, it won't work without a canMove implementation. I'll write one for you but I'll need some time to sit down and work out how to do so correctly; maybe someone else will be able to help you sooner.

  7. #17
    Octorok
    Join Date
    Mar 2006
    Posts
    271
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    vBActivity - Stats
    Points
    1,625
    Level
    13
    vBActivity - Bars
    Lv. Percent
    55.24%

    Re: Randomly moving around

    People, I don't know if this is gravedigging or not, but please can someone help?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Armageddon Games is a game development group founded in 1997. We are extremely passionate about our work and our inspirations are mostly drawn from games of the 8-bit and 16-bit era.
Social