Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > New Scripting Engine (GS2)
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-01-2008, 03:47 PM
Robin Robin is offline
The secret of NIMH
Robin's Avatar
Join Date: Apr 2005
Location: Wales, UK
Posts: 515
Robin will become famous soon enough
Send a message via AIM to Robin
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:

Last edited by Robin; 05-01-2008 at 03:52 PM.. Reason: Added whole NPC Script
Reply With Quote
  #2  
Old 05-01-2008, 05:19 PM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
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 (to hard to read).
__________________
Do it with a DON!
Reply With Quote
  #3  
Old 05-01-2008, 06:34 PM
Robin Robin is offline
The secret of NIMH
Robin's Avatar
Join Date: Apr 2005
Location: Wales, UK
Posts: 515
Robin will become famous soon enough
Send a message via AIM to Robin
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

Reply With Quote
  #4  
Old 05-01-2008, 06:39 PM
zokemon zokemon is offline
That one guy...
zokemon's Avatar
Join Date: Mar 2001
Location: Sonoma County, California
Posts: 2,925
zokemon is a jewel in the roughzokemon is a jewel in the rough
Send a message via ICQ to zokemon Send a message via AIM to zokemon Send a message via MSN to zokemon Send a message via Yahoo to zokemon
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!
__________________
Do it with a DON!
Reply With Quote
  #5  
Old 05-01-2008, 08:19 PM
Robin Robin is offline
The secret of NIMH
Robin's Avatar
Join Date: Apr 2005
Location: Wales, UK
Posts: 515
Robin will become famous soon enough
Send a message via AIM to Robin
: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
Reply With Quote
  #6  
Old 05-02-2008, 01:02 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
You can know where all of the benches are, and then detect the distance to each?
__________________
Reply With Quote
  #7  
Old 05-02-2008, 02:10 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
distance formula: (a^2+b^2)^.5
Check yours.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #8  
Old 05-02-2008, 02:52 AM
Robin Robin is offline
The secret of NIMH
Robin's Avatar
Join Date: Apr 2005
Location: Wales, UK
Posts: 515
Robin will become famous soon enough
Send a message via AIM to Robin
Quote:
Originally Posted by Tolnaftate2004 View Post
distance formula: (a^2+b^2)^.5
Check yours.
Yes I fixed it in my latest version
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 01:45 AM.


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