NPC Maze solver

Hey so I wrote this thing to solve a simple maze. the only problem is that it only decides which why to go when it hits a dead end. it will completely ignore a direction if it can still go the same way its going. anyway, i need some thoughts on how to detect when there are multiple ways to go. ill include maze file too so you can see what im talking about. also, can i dynamically allocate arrays? not being able to do that is hurting my “stack” that could expand to be big for large mazes.

if (created) {
  // Initialize the attributes
  showcharacter;
  setcharprop #3,head0.png;
  setcharprop #C0,orange;
  setcharprop #C1,white;
  setcharprop #C2,blue;
  setcharprop #C3,red;
  setcharprop #C4,black;
  setcharprop #2,shield1.gif;
  shieldpower = 1;
  dir = 0;
  this.speed = .5;
  this.count = 0;
  this.pdir = 0;

  setcharani walk,;

  x=16.5;
}
if (playerenters) {
  this.i=0;
  this.j=0;
  this.backtrack = 0;
  stack = {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,0,0,0,0,0,0,0,0,0,0,0,0}
  timeout = .05;
}
if (timeout)
  {
  x=x+vecx(dir)*this.speed;
  y=y+vecy(dir)*this.speed;
  message #v(stack[this.i]) #v(stack[this.i+1]) #v(stack[this.i+2]);
  if (onwall(x+vecx(dir)*this.speed+1.5+vecx(dir),y+vecy(dir)*this.speed+2+vecy(dir)))
    {
    this.i+=3;
    stack[this.i] = x;
    stack[this.i+1] = y;
    this.pdir = dir;

    while ((onwall(x+vecx(dir)*this.speed+1.5+vecx(dir),y+vecy(dir)*this.speed+2+vecy(dir))) && this.backtrack==0)
      {
      if (stack[this.i+2]<4)
        {
        if (stack[this.i+2] != (this.pdir+2)%4) dir = stack[this.i+2];
        stack[this.i+2]++;
      }
      else {
        dir = (this.pdir+2)%4;
        this.backtrack = 1;
        this.i-=3;
        break;

      }
    }
    while (this.backtrack==1)
      {
      if (x==stack[this.i] && y==stack[this.i+1])
        {
        this.backtrack=0;
        break;
      }
    }
  }
  timeout = .05;
}

lol genious, how come your coding so much now? Why dont you make an awesome server? Also google A*, theres some really good tutorials out there :slight_smile:

I was trying to come up with a maze solver but yer I cant think well enough lol, and I cant really hold a train of thought over a long time. If i cant think up a whole solution on the spot chances are I cant do it lol.

gscript can use pointers?!

anyway i was thinking of having a virtual direction that will constantly be rotating from 0-3 and do the onwall checks with that. that way i can keep the current movement system thing i got going for me.

Nice man. Although sometimes it just walks out of the maze lol.

dont you solve a maze by walking out of it??? um

You walk out of the end of the maze, not the middle of it.

it does that when the stack runs out of room to hold coordinates. this shit is hard in gscript.

Its looking like im gonna have to rewrite this and split the maze up into a grid or something. I dunno

Sent from my MB860 using Tapatalk

Just a tip, instead of declaring the stack array as you did just use setarray stack,size;
size can only hold 64*64 elements

ok. ill get this thing working tomorrow. then ill make a quest out of it. also finish that stupid ogre thing. the gscript is flowing back to me :slight_smile:

hey bro. You forgot colon after stack = {balls,e,taint}

Weird. It didn’t break it.

Sent from my MB860 using Tapatalk

Why do you switch between block and traditional styles?

Traditional

if (playerenters) {
  this.vagina++;
  message #v(this.vagina%4);
}

Block

if (playerenters)
{
  tokenize "I really hate block format.","You should",stop;
  message #t(0) #t(1) #t(2);
}

cause i do what i want. i honestly never knew the difference between traditional and block till now. are you talking about where i put the braces?

Yes.

then i guess i usually do block

___Merged doublepost__________________

ok so A* is for path finding. not pointers, haha.

arg. i might give up. gscript is pissing me off. anyway, i got it to solve the maze, but when it needs to backtrack, it will bug out. feel free to tell me wtf im doing wrong. here is updated script and level.

// Graal2002 NPC by Hosler
if (created || playerenters){
  showcharacter;
  setcharprop #3,head0.png;
  setcharprop #C0,orange;
  setcharprop #C1,white;
  setcharprop #C2,blue;
  setcharprop #C3,red;
  setcharprop #C4,black;
  setcharprop #2,shield1.gif;
  shieldpower = 1;
  setcharani walk,;
  dir = 0;
  this.speed = .5;
  this.vdir = 0;
  this.backtrack = 0;
  this.i = 0;
  this.count = 0;
  this.directions = 0;
  this.deciding = 0;
  setarray stackX,64;
  setarray stackY,64;
  setarray stackDir,64;
  x=16.5;
  timeout = .05;
}
if (timeout){
  //Make NPC Move
  if (!onwall(x+vecx(dir)*this.speed+1.5+vecx(dir),y+vecy(dir)*this.speed+2+vecy(dir)) && this.deciding == 0){
    x=x+vecx(dir)*this.speed;
    y=y+vecy(dir)*this.speed;
  }
  this.directions = 0;
  this.count++;
  checkArea();
  //If possible directions found, throw on stack
  if (this.directions>0 && this.deciding == 0){
    this.i++;
    stackX[this.i] = x;
    stacky[this.i] = y;
    this.deciding = 1;
  }
  while (this.deciding == 1 && this.backtrack == 0){
message #v(stackDir[this.i]);
    if (stackDir[this.i]<4){
      this.vdir = stackDir[this.i];

      if ((onwall(x+vecx(this.vdir)*this.speed+1.5+vecx(this.vdir),y+vecy(this.vdir)*this.speed+2+vecy(this.vdir))) || stackDir[this.i] == (dir+2)%4){
        stackDir[this.i]++;
      }
      else {
        dir = stackDir[this.i];this.deciding = 0;

      }
    }
    else {
      dir = (dir+2)%4;
      this.backtrack = 1;
      this.deciding = 0;
      this.i--;
    }
  }
  while (this.backtrack==1){
message backtracking;
    if (x==stackX[this.i] && y==stackY[this.i])
      {
      this.backtrack=0;
    }
  }
  timeout = .05;
}

function checkArea(){
  if (this.count == 5 && this.backtrack == 0){
    this.count = 0;
    for (this.vdir = 0;this.vdir<4;this.vdir++){
      if (!onwall(x+vecx(this.vdir)*this.speed+1.5+vecx(this.vdir),y+vecy(this.vdir)*this.speed+2+vecy(this.vdir))){
        this.directions++;
      }
      // message #v(this.directions);
    }
  }
}

i should have just made that ogre thing awesome instead of this stupid shit.

fixed typo. still buggy.

___Merged doublepost__________________

well its adding locations to the stack when it shouldnt be.

I love this guys idea…when he can’t find a way out, he becomes smart and just walks over the fucking wall xD

Don’t have much experience with these, but have it check the two directions that it isn’t going for onwall? O.o Then it’ll turn O.o

Its so broke.

Sent from my MB860 using Tapatalk