Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   New Scripting Engine (GS2) (https://forums.graalonline.com/forums/forumdisplay.php?f=153)
-   -   A little bit of bench (https://forums.graalonline.com/forums/showthread.php?t=79586)

Robin 05-01-2008 03:47 PM

A little bit of bench
 
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.this.pos[1]; // Current Player.x
  
this.point.this.pos[2]; // Current Player.y
  
temp.min 400// arbitrary big number number
  
this.seat = {};
  for (
temp.int(this.point.x-20); temp.int(this.point.x+20); temp.x++;) {
    for (
temp.int(this.point.y-20); temp.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[1in {"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.this.pos[1];
  
this.point.this.pos[2];
  
temp.min 400;
  
this.seat = {};
  for (
temp.int(this.point.x-20); temp.int(this.point.x+20); temp.x++;)
  {
    for (
temp.int(this.point.y-20); temp.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+2in {681,682,697,698})
    {
      
this.setcharani("sit""");
    }
    else
    {
      
this.setcharani("walk""");
    }
  }
  else
  {
    if (
tiles[this.x+1.5,this.y+2in {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:

zokemon 05-01-2008 05:19 PM

Might help somewhat if you put a stopping case into your iteration (a break;). Didn't really look into it much more then that as you seem to be using way more variables then you need to x_x (to hard to read).

Robin 05-01-2008 06:34 PM

Why would I put a break in? It iterates to find the smallest distance, Zero.

Having worked with professional development teams I can honestly say this is the cleanest bit of code I've written in months x.x I am not proud of this fact.

Some psuedocode:
PHP Code:

public int findSmallestIndex(int[] values){
  
initialize variable to hold index of smallest value ( -1 might be a good number here)
  
initialize variable to hold value of smallest value (Integer.MAX-VALUE might be a good number here)
  for 
length of values
    
if i value smallest value
      smallest value 
i value
      smallest index 
i   
 
  
return i



zokemon 05-01-2008 06:39 PM

Oh your right, I didn't notice the smallest distance thing. I was thinking you were trying to find any bench for some reason, my bad! :)

Robin 05-01-2008 08:19 PM

:P

I fixed the movement problems and tidied up the code a bit, going to see if I can get the bench stuff working.

Might be cool to have npcs which wander about your server randomly getting in the way sitting in your chairs eating your FOODS.

Yeah I don't know. I'm getting into the swing of things again.
I can definitely see some poor choices in design @ GUI stuff x-x

cbk1994 05-02-2008 01:02 AM

You can know where all of the benches are, and then detect the distance to each?

Tolnaftate2004 05-02-2008 02:10 AM

distance formula: (a^2+b^2)^.5
Check yours. ;)

Robin 05-02-2008 02:52 AM

Quote:

Originally Posted by Tolnaftate2004 (Post 1388548)
distance formula: (a^2+b^2)^.5
Check yours. ;)

Yes I fixed it in my latest version :D


All times are GMT +2. The time now is 03:27 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.