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 01-07-2011, 08:58 AM
Switch Switch is offline
o.o
Switch's Avatar
Join Date: Jan 2007
Location: Philadelphia
Posts: 3,038
Switch has a spectacular aura about
Send a message via MSN to Switch
Baddie Core

Back on Ruins I was attempting to make a baddie system. At the time I saved it to use/finish in the future but I doubt I'll get a chance to. The only thing missing are wall/water checks because I was having trouble with them, as well as dealing damage, taking damage, and healing since we didn't have a health or damage system on Ruins. Enjoy an almost complete baddie system.

Core:
PHP Code:
/*
  This is the core system of baddies.
  No scripts but those that are a baddie class
  should be linking to this.
  -Switch
*/

/*
  this.bname -> baddie name
  this.range
   [0] -> aggro
   [1] -> attack
  this.speed
  this.damage
  this.freeze -> freeze time after an attack
  this.health
   [0] -> current
   [1] -> max
*/

enum {
  
IDLE,
  
WALK,
  
ATTACK,
  
DEAD
}

function 
onCreated() {
  
cancelEvents("PlayerAggro");
  
this.spawn_coords = {this.level.namethis.xthis.y};
  
this.mode IDLE;
  
this.showCharacter();
  
this.dir 2;
  
this.nick this.bname;
  
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
  
  if (
findNearestPlayer(this.xthis.y) != null) {
    
//begin loop
    
onTimeout();
  }
}

function 
onPlayerEnters() {
  if (
this.mode == IDLE) {
    
//begin loop
    
onTimeout();
  }
}
function 
onTimeout() {  
  
temp.pl findNearestPlayer(this.xthis.y);
  
temp.dx temp.pl.x-this.x;
  
temp.dy temp.pl.y-this.y;
  
temp.dist = (temp.dx^2+temp.dy^2)^0.5;
  if (
temp.dist <= this.range[0]) {
    
this.dcount 0;
    
this.mode ATTACK;
    
onPlayerAggro(temp.pl);
    return;
  }

  if (
this.dcount == 20) {
    
temp.rand int(random(0100));
    if (
temp.rand in |080|) {
      
this.mode IDLE;
    }
    else if (
temp.rand in |81100|) {
      
this.mode WALK;
    }
  }
  if (
this.mode == IDLE) {
    
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
    if (
this.dcount == 20) {
      
this.dcount 0;
      
this.dir int(random(03));
    }
  }
  else if (
this.mode == WALK) {
    
setCharAni("baddie_" @lowercase(this.bname)@ "-walk"null);
    if (
this.dcount == 20) {
      
this.dcount 0;
      for (
temp.i=0temp.i<359temp.i++) {
        
temp.angle random(0pi*2);
        
temp.dx cos(temp.angle)*this.speed*20;
        
temp.dy = -sin(temp.angle)*this.speed*20;
        if (
this.x+temp.dx in |064| && this.y+temp.dy in |064|) {
          
this.dir = (temp.angle in |0pi*0.25|?3:(temp.angle in |pi*0.25pi*0.75|?0:(temp.angle in |pi*0.75pi*1.25|?1:(temp.angle in |pi*1.25pi*1.75|?2:3))));
          
move(temp.dx/20temp.dy/200.10);
          
this.wdx temp.dx/20;
          
this.wdy temp.dy/20;
          break;
        }
      }
    }
    else {
      
move(this.wdxthis.wdy0.10);
    }
  }
  
this.dcount++;
  
setTimer(0.1);
}

function 
onPlayerAggro(pl) {
  if (
this.mode != ATTACK) {
    
//stop walk ani
    
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
    
onTimeout();
    return;
  }
  
  
//get distance to player
  
temp.dx pl.x-this.x;
  
temp.dy pl.y-this.y;
  
temp.dist = (temp.dx^2+temp.dy^2)^0.5;
  
//in hurt range
  
if (temp.dist <= this.range[1]) {
    
//so that you can see the attack
    
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
    
attackPlayer(pl);
    return;
  }
  
//out of aggro range (plus a quarter of it, to make it harder to get out of)
  
if (temp.dist this.range[0]*1.25) {
    
//stop walk ani
    
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
    
onTimeout();
    return;
  }
  
//walk
  
setCharAni("baddie_" @lowercase(this.bname)@ "-walk"null);
  
//return dx and dy to a distance of 1, then add in its speed
  
temp.angle getangle(temp.dxtemp.dy);
  
temp.movex cos(temp.angle)*this.speed*2;
  
temp.movey = -sin(temp.angle)*this.speed*2;
  
//change dir
  
this.dir = (temp.angle in |0pi*0.25|?3:(temp.angle in |pi*0.25pi*0.75|?0:(temp.angle in |pi*0.75pi*1.25|?1:(temp.angle in |pi*1.25pi*1.75|?2:3))));
  
//move baddie towards player
  
move(temp.movextemp.movey0.10);
  
  
scheduleEvent(0.1"PlayerAggro"pl);
}
function 
attackPlayer(pl) {
  
setCharAni("baddie_" @lowercase(this.bname)@ "-attack"null);
  
//hurt stuff goes here
  
scheduleEvent(1.5"PlayerAggro"pl);
}

function 
onRespawn() {
  
cancelEvents("PlayerAggro");
  
setTimer(0);
  
warpto(this.spawn_coords[0], this.spawn_coords[1], this.spawn_coords[2]);
  
this.this.spawn_coords[1];
  
this.this.spawn_coords[2];
  
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
  
this.mode IDLE;
  if (
findNearestPlayer(this.xthis.y) != null) {
    
onTimeout();
  }
}

function 
onPlayerLeaves() {
  if (
findNearestPlayer(this.xthis.y) == null) {
    
this.mode IDLE;
    
//end loop
    
setTimer(0);
    
setCharAni("baddie_" @lowercase(this.bname)@ "-idle"null);
  }

Baddie Class (example):
PHP Code:
/*
  This is an example of a baddie class that
  would link to the baddie core.
*/

function onCreated() {
  
this.bname "Slug";
  
this.range = {51};
  
this.speed 0.4;
  
this.damage 9001;
  
this.freeze 0.3;
  
this.health = {90019001};
  
this.join("baddie-core");
}

// It's over 9000!!!!!! 
__________________
Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision.
But when I try to look at you, you scurry away.
Are you shy, squiggly line?
Why only when I ignore you, do you return to the center of my eye?
Oh, squiggly line, it's alright, you are forgiven.
Reply With Quote
  #2  
Old 01-07-2011, 11:24 PM
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 really shouldn't be using 0.1 second timeouts for a baddy, especially when it's just idling.
__________________
Reply With Quote
  #3  
Old 01-08-2011, 01:32 AM
Codein Codein is offline
jwd
Codein's Avatar
Join Date: Oct 2005
Location: Greater Manchester
Posts: 2,423
Codein has a spectacular aura aboutCodein has a spectacular aura about
Send a message via AIM to Codein Send a message via MSN to Codein
Is there any reason why this is not using the 'move()' function?
Reply With Quote
  #4  
Old 01-08-2011, 01:34 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Quote:
Originally Posted by Codein View Post
Is there any reason why this is not using the 'move()' function?
It is..
__________________
Quote:
Reply With Quote
  #5  
Old 01-08-2011, 02:04 AM
Codein Codein is offline
jwd
Codein's Avatar
Join Date: Oct 2005
Location: Greater Manchester
Posts: 2,423
Codein has a spectacular aura aboutCodein has a spectacular aura about
Send a message via AIM to Codein Send a message via MSN to Codein
Quote:
Originally Posted by fowlplay4 View Post
It is..
Oh aye, I couldn't find it in a wall of code inside a 0.1-length timeout loop, which seems pretty unnecessary for me.
Reply With Quote
  #6  
Old 01-08-2011, 12:22 PM
Tigairius Tigairius is offline
The Cat
Tigairius's Avatar
Join Date: Jan 2007
Location: Missouri, USA
Posts: 4,240
Tigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant futureTigairius has a brilliant future
Nice work, but like Chris said, for idling you can usually get away with a 0.25 or higher timeout and still achieve the same affect.
__________________


“Shoot for the moon. Even if you miss, you'll land among the stars.”
Reply With Quote
  #7  
Old 01-09-2011, 12:10 AM
Switch Switch is offline
o.o
Switch's Avatar
Join Date: Jan 2007
Location: Philadelphia
Posts: 3,038
Switch has a spectacular aura about
Send a message via MSN to Switch
Quote:
Originally Posted by cbk1994 View Post
You really shouldn't be using 0.1 second timeouts for a baddy, especially when it's just idling.
Quote:
Originally Posted by Tigairius View Post
Nice work, but like Chris said, for idling you can usually get away with a 0.25 or higher timeout and still achieve the same affect.
If a player walked in during any time between timeouts at 0.25 seconds it wouldn't see them if they then ran out of the area before the timeout.
__________________
Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision.
But when I try to look at you, you scurry away.
Are you shy, squiggly line?
Why only when I ignore you, do you return to the center of my eye?
Oh, squiggly line, it's alright, you are forgiven.
Reply With Quote
  #8  
Old 01-09-2011, 12:13 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Quote:
Originally Posted by Switch View Post
If a player walked in during any time between timeouts at 0.25 seconds it wouldn't see them if they then ran out of the area before the timeout.
Add code to onPlayerEnters something a long the lines..

PHP Code:
function onPlayerEnters() {
  if (
notaggroed) {
    
checkforplayer();
  }

__________________
Quote:
Reply With Quote
  #9  
Old 01-09-2011, 12:17 AM
Switch Switch is offline
o.o
Switch's Avatar
Join Date: Jan 2007
Location: Philadelphia
Posts: 3,038
Switch has a spectacular aura about
Send a message via MSN to Switch
Quote:
Originally Posted by fowlplay4 View Post
Add code to onPlayerEnters something a long the lines..

PHP Code:
function onPlayerEnters() {
  if (
notaggroed) {
    
checkforplayer();
  }

Would still require a loop.
Anyways, I haven't touched this script for a while except to change all the "ruins_" to "baddie_" for anis, nor do I plan to do anything to it. People can add/change/remove whatever they want to it.
__________________
Oh squiggly line in my eye fluid. I see you lurking there on the peripheral of my vision.
But when I try to look at you, you scurry away.
Are you shy, squiggly line?
Why only when I ignore you, do you return to the center of my eye?
Oh, squiggly line, it's alright, you are forgiven.
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 02:03 AM.


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