NPC shit

[code]if (created) {
showcharacter;
setcharprop #1,sword1.png;
setcharprop #2,shield1.png;
setcharprop #3,head0.png;
setcharprop #8,body.png;
setcharprop #C0,orange;
setcharprop #C1,white;
setcharprop #C2,blue;
setcharprop #C3,red;
setcharprop #C4,black;
shieldpower = 1;
swordpower = 1;
hearts = 3;
dir = 2;
dirT = 3;
hurtc = 0;
hit = 0;
t = 0.05;
deadtime = 10;
this.speed = 0.25;
timeout = t;
}

if (washit && mode != 4) {
setcharani hurt,;
hearts -= playerswordpower/2;
if (hearts == 0) mode = 4;
else {
mode = 3;
hurtdx = dx/dist;
hurtdy = dy/dist;
hurtc = 0.35;
}
}

if (timeout) {
dx = playerx - x;
dy = playery - y;
dist = (dx^2 + dy^2)^0.5;
hit = (hit > 0) ? hit-t:0;
if (hurtc > 0) {
mode = 3;
hurtc -= t;
}
if (mode == 0) {
setcharani idle,;
idle += t;
if (idle == 3) {
mode = 1;
idle = 0;
turn();
}
search();
} else if (mode == 1) {
setcharani walk,;
dirS += t;
if (dirS == dirT) turn();
if (!onwall(x+(dir==1?0.25:2.5)+(vecx(dir)*this.speed),y+2)) x += vecx(dir)*this.speed;
else turn();
if (!onwall(x+1.5,y+(dir==0?0.75:3)+(vecy(dir)*this.speed))) y += vecy(dir)*this.speed;
else turn();
search();

} else if (mode == 2) {
setcharani walk,;
idle = 0;
dir = getdir(dx,dy);
if (!onwall(x+(dir==1?0.25:2.5)+vecx(dx>0?3:1)*this.speed,y+2)) x += (dx/dist)*this.speed;
if (!onwall(x+1.5,y+(dir==0?0.75:3)+vecy(dy>0?2:0)*this.speed)) y += (dy/dist)*this.speed;
search();

} else if (mode == 3) {
if (!onwall(x+(dir==1?0.25:2.5)+vecx(hurtdx>0?3:1)*this.speed,y+2)) x -= hurtdx;
if (!onwall(x+1.5,y+(dir==0?0.75:3)+vecy(hurtdy>0?2:0)*this.speed)) y -= hurtdy;
if (hurtc == 0) search();

} else if (mode == 4) {
setcharani dead,;
hearts = 3;
sleep deadtime;
mode = 0;
}
timeout = t;
}

function search() {
if (dist < 3 && hit == 0) {
setcharani sword,;
sleep 0.1;
hit = 0.3;
} else if (dist > 10) mode = (mode == 0) ? 0:1;
else mode = 2;
}

function turn() {
dirS = 0;
dirT = int(random(2,5));
dir = int(random(-0.5,3.49)+0.5);
}
[/code]

Problem is that the NPC can slide through walls while chasing. Example: there is a wall between the player and NPC so the NPC is running at the wall, if there is a wall above it as well it can slide through it while still facing the direction of the player.

Which mode is the chasing mode?
Plus have you forgot to add turn() to mode 2?

If you can’t read it I don’t want your help.

The lazy guy hates lazy people.

Thing is, you’re only checking one tile, that’s pretty much a no go.

In pretty much any baddy I make, I have to resort to an onwall2 equivalent.
For example:

function getChunk(){//returns a XxY tile chunk reading based off character size
  this.waterCount = 0; this.wallCount = 0;
  for (this.c = 0; this.c < this.charSize[0]*this.charSize[1]; this.c++){
    this.cCheck = {this.checkC[0]+(this.c%this.charSize[0]),this.checkC[1]+int(this.c/this.charSize[0])};
    this.waterCount += onwater(this.cCheck[0],this.cCheck[1]);
    this.wallCount  += onwall(this.cCheck[0],this.cCheck[1]);
  }

  this.canMove = (this.wallCount == 0 && ((this.canSwim == false && this.waterCount == 0) || (this.canSwim == true && this.waterCount >= 0)));
}

Implement such a thing into your code and you’ve got yourself a fix.

Nerd

I suspected as much. Was just hoping that someone could think up a quick fix with what I have. xD I’ll just simulate onwall2 then. Thanks.

Tricxta, in your snippet, what is this.checkC?

An array holding on wall or on water check data

this.checkC is the top left of the tile coordinate block you’d like to check.

Thanks.