Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-08-2007, 10:13 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Smarter AI?

Well, I know this is going to be a bit disappointing, but this script is not scripted in gs2, nor is it prepped for online use. Sorry, but I have access to neither of the two so I can't say much for that. But this isn't really for giving a complete free script, but to provide a base script for the idea. Everything here can easily be converted to gs2, and probably enhanced. And lastly, it's sort of buggy, not much, but not ready for public use. It does work most of the time though.

Anyways, I posted in the other thread suggesting that baddies track your movement. Well I thought I'd try it out and this is what I came up with. The baddy will act like a normal while you're in site, but if you run around an obstacle it will track your movement instead. I remember when I used to play I could easily get around the NPCs by just walking around a wall or such, this will follow you though, making them a bit smarter than just running around and stopping if they can't see you. He doesn't attack though, but that should be easy enough to add. Onwall detection isn't perfect, but eh... it does the job.

Anyways, like I said it's meant as a base to hopefully give ideas. Take it and make what you want of it, convert it, take ideas. Hopefully it'll help, because in the end, the concept is still the same. I can't say it's the most efficient or best script around(especially since gs1 is outdated), but I haven't scripted in ages.
Attached Files
File Type: nw smartbaddy1.nw (12.5 KB, 198 views)
Reply With Quote
  #2  
Old 03-08-2007, 10:14 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
And for those who want just the code:
PHP Code:
//NPC made by Dusty
if (created) {
  
this.speed=.4// Customize the NPCs speed

  // MODE 0 : idle
  // MODE 1 : aimless walking
  // MODE 2 : chasing via player-npc
  // MODE 3 : chasing via tracking players movements
  
this.mode=0;
  
setstring this.animations,idle,walkslow,walk,walk;
  
showcharacter;

  
timeout=0.05;
}

if (
timeout) {
  if (
this.mode in {0,1}) {
    
this.delta={(playerx+2)-(x+1.5),(playery+2)-(y+2),((this.delta[0]^2)+(this.delta[1]^2))^.5};

    if (
this.mode==0) {
      if (
random(0,100)<3) {
        
dir=int(random(0,4));
        
this.mode=1;
      }
    }
    if (
this.mode=1) {
      if (
onwall(x+1.5+vecx(dir)*2,y+2+vecy(dir)*2)) dir=(dir+2)%4;
      else {
        
x+=vecx(dir)*(this.speed/2);
        
y+=vecy(dir)*(this.speed/2);
      }
      if (
random(0,100)<6dir=int(random(0,4));
      if (
random(0,100)<2this.mode=0;
    }
    
this.checkdir=getdir(this.delta[0],this.delta[1]);
    if (
this.checkdir==dir && this.delta[2]<15checkbetween();
  } else {
    
calculatetracking();
    
dir=getdir(this.delta[0],this.delta[1]);
  }

  if (
this.mode==2) {
    
this.delta={(playerx+2)-(x+1.5),(playery+2)-(y+2),((this.delta[0]^2)+(this.delta[1]^2))^.5};
    if (
this.delta[2]>3) {
      
checkbetween();
      
this.addx=(this.delta[0]/this.delta[2])*this.speed;
      
this.addy=(this.delta[1]/this.delta[2])*this.speed;
      
x+=this.addx;
      
y+=this.addy;
    }
  }
  if (
this.mode==3) {
    
this.delta={(this.trail_x[this.step]+2)-(x+1.5),(this.trail_y[this.step]+2)-(y+2),((this.delta[0]^2)+(this.delta[1]^2))^.5};
    
this.addx=(this.delta[0]/this.delta[2])*this.speed;
    
this.addy=(this.delta[1]/this.delta[2])*this.speed;
    
x+=this.addx;
    
y+=this.addy;
    if (
this.delta[2]=<.5) {
      
this.step++;
      
checkbetween();
    }
  }
  
setcharani #I(this.animations,this.mode),;

  
timeout=0.05;
}

function 
calculatetracking() {
  
this.timer=(this.timer+.5)%2;
  if (
this.timer==1) {
    if (
playerx!=this.trail_x[arraylen(this.trail_x)-1]||playery!=this.trail_y[arraylen(this.trail_y)-1]) {
      
setarray this.trail_x,arraylen(this.trail_x)+1;
      
setarray this.trail_y,arraylen(this.trail_y)+1;
      
this.trail_x[arraylen(this.trail_x)-1]=playerx;
      
this.trail_y[arraylen(this.trail_y)-1]=playery;
    }
  }
}

function 
trackplayer() {
  
this.trackdistance=100;
  for (
i=0;i<arraylen(this.trail_x);i++) {
    
this.delta={(this.trail_x[i]+2)-(x+1.5),(this.trail_y[i]+2)-(y+2),((this.delta[0]^2)+(this.delta[1]^2))^.5};
    if (
this.delta[2]<this.trackdistance) {
      for (
w=0;w<int(this.delta[2]);w++) {
        
this.check={x+1.5+(this.delta[0]/this.delta[2])*w,y+2+(this.delta[1]/this.delta[2])*w};
        if (!
onwall(this.check[0],this.check[1])) {
          
this.trackdistance=this.delta[2];
          
this.step=i+1;
        }
      }
    }
  }
  
this.mode=3;
}

function 
checkbetween() {
  
this.checkdelta={(playerx+2)-(x+1.5),(playery+2)-(y+2),((this.checkdelta[0]^2)+(this.checkdelta[1]^2))^.5};
  
this.checkingwall=0;
  for (
i=0;i<int(this.checkdelta[2]);i++) {
    
this.check={x+1.5+(this.checkdelta[0]/this.checkdelta[2])*i,y+2+(this.checkdelta[1]/this.checkdelta[2])*i};
    if (
testplayer(this.check[0],this.check[1])<0this.checkingwall+=onwall(this.check[0],this.check[1]);
  }
  if (
this.checkingwall>0) {
    if (
this.mode==2trackplayer();
  } else {
    if (
this.mode==3) {
      
this.mode=2;
      
setarray this.trail_x,0;
      
setarray this.trail_y,0;
    }
    if (
this.mode<2this.mode=2;
  }

Again, sorry it's not GS2/serverside compatible.
Reply With Quote
  #3  
Old 03-12-2007, 02:46 AM
Angel_Light Angel_Light is offline
Varia Developer
Angel_Light's Avatar
Join Date: Nov 2005
Location: Knoxville, TN
Posts: 1,684
Angel_Light is on a distinguished road
Send a message via AIM to Angel_Light Send a message via MSN to Angel_Light
awesome
__________________
Deep into the Darkness peering...
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 06:19 AM.


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