Okay, so, long time, haven't done much coding since I've been gone, so prepare to be quite shocked at how much I really suck.
PHP Code:
function findSeat() {
this.point.x = this.pos[1]; // Current Player.x
this.point.y = this.pos[2]; // Current Player.y
temp.min = 400; // arbitrary big number number
this.seat = {};
for (temp.x = int(this.point.x-20); temp.x < int(this.point.x+20); temp.x++;) {
for (temp.y = int(this.point.y-20); temp.y < int(this.point.y+20); temp.y++;) {
if (tiles[temp.x,temp.y] == 681) { // Find top left hand corner of bench
temp.dx = temp.x-this.point.x;
temp.dy = temp.y-this.point.y;
temp.dist = ((temp.dx ^ 2) - (temp.dy ^ 2))^0.5; // Get distance
if (!(testnpc(temp.x,temp.y) > 0) && !(testplayer(temp.x,temp.y) > 0)) { // Only allow if there's no one already sitting there
if (temp.dist<temp.min) {
temp.min = temp.dist;
this.seat = {temp.x,temp.y};
}
}
}
}
}
if (temp.min == 400) { // If the criteria has not been met
return false;
} else {
return this.seat; // Otherwise return a seat!
}
}
So essentially I have an NPC walking around with me, when I stop, it finds a seat, and goes over to it and sits there.
The problem I'm having is (apart from move not working like I thought it would and hardly 'moving' at all, looks more like warping, and sometimes it moves smoothly :/) that when the NPC calls this command, it will return a different bench each time called without fail, it almost looks as if it's cycling through each bench and whichever is next in line, it warps/walks to.
Any ideas where I'm messing up?
EDIT: Whole NPC included for completion's sake.
PHP Code:
function onCreated()
{
this.setcharani("idle", "");
this.body = "body5.png";
this.head = "head26.png";
this.following = "Robin";
this.master = "Robin";
this.moving = false;
showcharacter();
setTimer(5);
}
function onTimeout() {
if (this.master == this.following)
{
this.chat = "";
}
else
{
this.chat = "I am following you for the pleasure of " @ this.master @ ", My master!";
}
temp.isloggedin = false;
with(findplayer(this.following)) {
this.pos = {player.level,player.x-vecx(player.dir)*3,player.y-vecy(player.dir)*3,player.dir};
}
with(findplayer(this.master)) {
temp.isloggedin = true;
if (player.chat.starts("slave!"))
temp.chat = player.chat;
}
if (temp.chat.length() > 0)
{
temp.tokens = temp.chat.tokenize();
if (temp.tokens[1] in {"go", "come"}) {
this.following = temp.tokens[3];
}
}
if (this.pos[0] == this.level)
{
if (this.pos[1] != this.oldpos[1] || this.pos[2] != this.oldpos[2] || this.pos[3] != this.oldpos[3])
{
temp.dx = this.pos[1]-this.x;
temp.dy = this.pos[2]-this.y;
temp.dist = ((temp.dx ^ 2) - (temp.dy ^ 2))^0.5;
temp.speed = temp.dist*0.1; /*temp.dist*0.1 > 4.5 ? 4.5 : temp.dist*0.1;*/
this.move(temp.dx,temp.dy,temp.speed,16+8);
this.moving = true;
}
else
{
temp.seat = findSeat();
if (temp.seat != false)
{
temp.dx = temp.seat[0]-0.5-this.x;
temp.dy = temp.seat[1]-0.5-this.y;
temp.dist = ((temp.dx ^ 2) - (temp.dy ^ 2))^0.5;
//this.chat = temp.dist;
temp.speed = temp.dist*0.1; /*temp.dist*0.1 > 4.5 ? 4.5 : temp.dist*0.1;*/
this.move(temp.dx,temp.dy,temp.speed,16+8);
this.moving = true;
}
}
}
else
{
if (temp.isloggedin == true)
{
this.warpto(this.pos[0],this.pos[1],this.pos[2]);
}
else
{
this.warpto("server.nw",30.5,30);
setTimer(300);
return;
}
}
attr[9] = this.moving;
this.oldpos = this.pos;
if (this.moving == false)
setTimer(5);
}
function onMovementFinished() {
this.moving = false;
attr[9] = this.moving;
setTimer(5);
}
function findSeat() {
this.point.x = this.pos[1];
this.point.y = this.pos[2];
temp.min = 400;
this.seat = {};
for (temp.x = int(this.point.x-20); temp.x < int(this.point.x+20); temp.x++;)
{
for (temp.y = int(this.point.y-20); temp.y < int(this.point.y+20); temp.y++;)
{
if (tiles[temp.x,temp.y] == 681)
{
temp.dx = temp.x-this.point.x;
temp.dy = temp.y-this.point.y;
temp.dist = ((temp.dx ^ 2) - (temp.dy ^ 2))^0.5;
if (!(testnpc(temp.x,temp.y) > 0) && !(testplayer(temp.x,temp.y) > 0))
{
if (temp.dist<temp.min)
{
temp.min = temp.dist;
this.seat = {temp.x,temp.y};
}
}
}
}
}
if (temp.min == 400)
{
return false;
}
else
{
return this.seat;
}
}
//#CLIENTSIDE
function onCreated() {
setTimer(0.05);
}
function onTimeout() {
this.moving = attr[9];
if (this.moving == true)
{
if (tiles[this.x+1.5,this.y+2] in {681,682,697,698})
{
this.setcharani("sit", "");
}
else
{
this.setcharani("walk", "");
}
}
else
{
if (tiles[this.x+1.5,this.y+2] in {681,682,697,698})
{
this.setcharani("sit", "");
}
else
{
this.setcharani("idle", "");
}
}
setTimer(0.05);
}
Yeah I know, No it's not for serious, just trying to readjust after not looking at this stuff for years D: