Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Baddie Core (https://forums.graalonline.com/forums/showthread.php?t=134261588)

Switch 01-07-2011 08:58 AM

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!!!!!! 


cbk1994 01-07-2011 11:24 PM

You really shouldn't be using 0.1 second timeouts for a baddy, especially when it's just idling.

Codein 01-08-2011 01:32 AM

Is there any reason why this is not using the 'move()' function?

fowlplay4 01-08-2011 01:34 AM

Quote:

Originally Posted by Codein (Post 1621181)
Is there any reason why this is not using the 'move()' function?

It is..

Codein 01-08-2011 02:04 AM

Quote:

Originally Posted by fowlplay4 (Post 1621184)
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.

Tigairius 01-08-2011 12:22 PM

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.

Switch 01-09-2011 12:10 AM

Quote:

Originally Posted by cbk1994 (Post 1621130)
You really shouldn't be using 0.1 second timeouts for a baddy, especially when it's just idling.

Quote:

Originally Posted by Tigairius (Post 1621280)
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.

fowlplay4 01-09-2011 12:13 AM

Quote:

Originally Posted by Switch (Post 1621434)
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();
  }



Switch 01-09-2011 12:17 AM

Quote:

Originally Posted by fowlplay4 (Post 1621436)
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.


All times are GMT +2. The time now is 04:37 AM.

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