tile walker


#1

im trying to get this guy to follow tiles, and he does it well for the most part. at some point something gets off and i cant friggin figure it out. anyone wanna take a look. the level i attached will make it clear. here is the code for criticism:
[php]
// The walker
if (created||playerenters) {
// 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;
setcharani walk,;
this.xoffset=1;
this.yoffset=2;
this.tile = 1760;
this.speed = .5;
this.trigger = 1;

timeout = .05;
}
if (timeout) {
message #v(this.x) #v(this.y);
if (this.trigger<0)
{
dir = this.dir;
this.trigger = 1;
}
else this.trigger–;
this.x = x+this.xoffset;
this.y = y+this.yoffset;
this.ntile = tiles[this.x+vecx(dir),this.y+vecy(dir)];
for (this.i=3;this.i<8;this.i++) {
this.dir = (dir+this.i)%4;
if (tiles[this.x+vecx(this.dir),this.y+vecy(this.dir)] == this.tile) break;
}
if (this.tile==this.ntile) {
x=x+vecx(dir)*this.speed;
y=y+vecy(dir)*this.speed;
}

timeout = .05;
}
[/php]


#2

turnage priority is right, same direction, left, then backwards in that order. thats what that for loop does. xy offset variables are so tiles are beneath feet instead of npc xy. trigger prevents npc from changing directions too soon after doing it once.


#3

At a glance, vecx(dir)*this.speed to move your guy would result probably in .5 moved. Whereas your checks use this.x+vecx(dir) without using speed.
So they’d be checking 1 away or a future area.


#4

oh wow didnt even think of that. ill try it out


#5

Also not sure what purpose
for (this.i=3;this.i<8;this.i++) {
this.dir = (dir+this.i)%4;
if (tiles[this.x+vecx(this.dir),this.y+vecy(this.dir)] == this.tile) break;
}

Is doing? Think it was maybe to check if all 4 directions match to exit the script. But depending on how break works, it might just leave the for loop.
Plus it seems to break after finding even 1 of the directions matching it. So doesn’t “if (this.tile==this.ntile) {” end up doing the exact same job in the end?


#6

holy shit. way better now. he still gets off a little, but wow thanks for that input. attached is updated dude.


#7

right. that for loop first looks to go right, if it can then it breaks and goes right. does the same for all directions in this order relative to npc dir: right, straight, left, down. if anyone of those is good to go then it breaks and turns.

the loop is required to test all 4 directions.


#8

adding xoffset to equal 1.5 instead of 1 makes it a little better. thanks beholder. uhhh he works pretty good. 4 way intersections fuck him up tho. here is my current copy. i think im done with this. anyone can use this guy for stuff.

my plan was to create line rider for GR. i was gonna have certain tiles speed him up/slow him down. maybe ill still try that, but i dunno.

here is v3. ready for production! lol


#9

making AI for NPCs is such a chore. It’s part of the reason I scrapped the idea for RPG servers despite knowing how to go about it.

This might be quite useful actually. Do we still have that NPC gallery running? I suggest you drop it there.

edit: this guy is pretty neat. But he gets broken by patterns like this kinda easy.


#10

Oh that makes sense, so he’ll turn. Out of curiosity why is the x/y offset different? Unless you use a custom map type like isometric hex grid.
Kinda assume that moving in an X or Y direction you’d want it to be a consistent 1 or .5. Otherwise he’d move/check the tiles farther away from himself in the Y directions.

Unless it’s a weird graal related thing. Like measuring the players hitbox as his body height instead of just a point.


#11

the x,y for the npc isnt the name x,y as the tile when he stand on it. beholder called it collision hit box or whatever. he gave me a sweet pep talk on how i was doing it all wrong.