Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 05-19-2015, 10:40 PM
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
Added support for more verbose move commands.

e.g. movewest, moveup, movedownright, movenorth, etc.

move[up|down|left|right|north|east|west|south|][up|down|left|right|north|east|west|south|]

You can also specify an amount of tiles to move, otherwise it just moves by 1 tile.

{"moveeast", 3}

PHP Code:
//#CLIENTSIDE
function onCreated() {
  if (!
this.ani_idlethis.ani_idle "idle";
  if (!
this.ani_walkthis.ani_walk "walk";
  if (!
this.speedthis.speed 6//Tiles Per Second.
  
if (this.can_stop) {
    
// Breaks up movement actions to allow NPC to
    // stop properly.
    
this.actions parseMovement(this.actions);
    
temp.subactions getstringkeys("this.action_");
    for (
temp.subactiontemp.subactions) {
      
this.("action_" temp.subaction) = parseMovement(this.("action_" temp.subaction));
    }
  }
  
this.initxy = {
    
this.xthis.y
  
};
  
this.actionpos 0;
  
doAction(0); 
}

function 
parseMovement(old_actions) {
  
temp.new_actions = {};
  for (
temp.actionold_actions) {
    
temp.data temp.action.tokenize(":");
    switch (
data[0]) {
      case 
"move":
        
temp.new_actions.addarray(calcMoves(data[1], data[2]));
        break;
      case 
"moveto":
        
temp.movex data[1] - (this.64);
        
temp.movey data[2] - (this.64);
        
temp.new_actions.addarray(calcMoves(movexmovey));
        break;
      default:
        if (
data[0].starts("move")) {
          
// Determine Direction
          
if (data[0].pos("east") >= || data[0].pos("right") >= 0temp.dx 1;
          else if (
data[0].pos("west") >= || data[0].pos("left") >= 0temp.dx = -1;
          if (
data[0].pos("north") >= || data[0].pos("up") >= 0temp.dy = -1;
          else if (
data[0].pos("south") >= || data[0].pos("down") >= 0temp.dy 1;
          
// Check for Valid Deltas
          
if (temp.dx != || temp.dy != 0) {
            
temp.dx *= data[1] == data[1];
            
temp.dy *= data[1] == data[1];
            
temp.new_actions.addarray(calcMoves(temp.dxtemp.dy));
          }
        } else {
          
temp.new_actions.add(temp.action);
        }
        break;
    }
  }
  return 
temp.new_actions;
}

function 
calcMoves(dxdy) {
  
temp.distance GetDistance(this.xthis.ythis.dxthis.dy);
  
temp.time distance this.speed;
  
temp.frames temp.time 0.05;
  
temp.ndx dx * (temp.frames);
  
temp.ndy dy * (temp.frames);
  
temp.nmove format("move:%s:%s"temp.ndxtemp.ndy);
  for (
temp.0temp.temp.framestemp.i++) {
    
temp.moves.add(temp.nmove);
  }
  return 
temp.moves;
}

function 
doAction(ind) {
  if (
ind == this.actions.size()) return;
  if (
this.is_stopped || this.waiting) return;
  
temp.action this.actions[ind];
  
temp.data action.tokenize(":");
  if (
data[0].starts("!")) {
    
this.actions.delete(ind);
    
this.actionpos--;
    
data[0] = data[0].substring(1);
  }
  switch (
data[0]) {
    case 
"move":
      
doMove(data[1], data[2]);
      break;
    case 
"moveto":
      
temp.movex data[1] - (this.64);
      
temp.movey data[2] - (this.64);
      
doMove(movexmovey);
      break;
    case 
"reversemove":
      
this.reversing true;
      
this.reversepos this.moves.size();
      
onMovementFinished();
      break;
    case 
"action":
      
temp.act this.("action_" data[1]);
      for (
temp.actionact) {
        
this.actions.insert(this.actionpos+1+temp.i"!" action);
        
temp.i++;
      }
      
onNextStep();
      break;
    case 
"randomaction":
      if (
random(0,1) <= data[1]) {
        
temp.act this.("action_" data[2]);
        for (
temp.actionact) {
          
this.actions.insert(this.actionpos+1+temp.i"!" action);
          
temp.i++;
        }
      }
      
onNextStep();
      break;
    case 
"setdir":
      
this.dir data[1];
      
onNextStep();
      break;
    case 
"setani":
      
setcharani(data[1], data[2]);
      
onNextStep();
      break;
    case 
"emote":
      
temp.emote data[1];
      
temp.time = (!data[2] ? data[2]);
      if (
temp.emote == "emoticon_Zzz_stay.mng") {
        
showimg(200temp.emotethis.2.5this.- (92/16) + 1);
      } else { 
        
showani(200this.xthis.y0"emoticon"temp.emote);
      }
      
this.scheduleevent(time"ClearEmote""");
      
onNextStep();
      break;
    case 
"levelvar":
      
level.(@data[1]) = data[2];
      
onNextStep();
      break;
    case 
"randomani":
      if (
random(0,1) <= data[1]) {
        
setcharani(data[2], data[3]);
      }
      
onNextStep();
      break;
    case 
"chat":
      
this.chat data[1];
      
temp.time = (!data[2] ? data[2]);
      
this.scheduleevent(time"ClearChat""");
      
onNextStep();
      break;
    case 
"randomchat":
      if (
random(0,1) <= data[1]) {
        
this.chat data[2];
        
temp.time = (!data[3] ? data[3]);
        
this.scheduleevent(time"ClearChat""");
      }
      
onNextStep();
      break;
    case 
"showimg":
      
showimg(data[1], data[2], data[3], data[4]);
      break;
    case 
"hideimg":
      
hideimg(data[1]);
      break;
    case 
"setvar":
      
this.(@data[1]) = data[2];
      break;
    case 
"wait":
      
this.waiting true;
      
this.scheduleevent(data[1], "NextStep"true);
      break;
    case 
"repeat":
      
this.actionpos 0;
      
doAction(this.actionpos);
      break;
    default:
      
// Check if starts with move
      // Support for variable move commands. e.g. moveright, moveup, movedownleft, movenortheast
      
if (data[0].starts("move")) {
        
// Determine Direction
        
if (data[0].pos("east") >= || data[0].pos("right") >= 0temp.dx 1;
        else if (
data[0].pos("west") >= || data[0].pos("left") >= 0temp.dx = -1;
        if (
data[0].pos("north") >= || data[0].pos("up") >= 0temp.dy = -1;
        else if (
data[0].pos("south") >= || data[0].pos("down") >= 0temp.dy 1;
        
// Check for Valid Deltas
        
if (temp.dx != || temp.dy != 0) {
          
temp.dx *= data[1] == data[1];
          
temp.dy *= data[1] == data[1];
          
doMove(temp.dxtemp.dy);
        }
      }
      break;
  }
}

// Movement Functions

function npc_stop() {
  
this.is_stopped true;
  
this.oani this.ani;
  
this.odir this.dir;
  
setcharani(this.ani_idle"");
}

function 
npc_continue() {
  
this.is_stopped false;
  
this.dir this.odir;
  
this.ani this.oani;
  
onMovementFinished();
}

function 
GetDistance(temp.x1temp.y1temp.x2temp.y2) {
  
temp.toreturn = (temp.x2 temp.x1) ^ + (temp.y2 temp.y1) ^ 2;
  return (
temp.toreturn) ^ .5;
}

function 
doMove(dxdy) {
  
temp.distance GetDistance(this.xthis.ythis.dxthis.dy);
  
temp.time distance this.speed;
  
this.dir getRealDir(dxdy);
  
move(dxdytemp.time8);
  if (
this.ani != this.ani_walksetcharani(this.ani_walk"");
  if (!
this.reversingthis.moves.add({dxdy});
}

function 
getRealDir(dxdy) {
  if (
dy && dx 0) return 3;
  if (
dy && dx 0) return 3;
  if (
dy && dx 0) return 1;
  if (
dy && dx 0) return 2;
  return 
getdir(dxdy);
}

function 
onMovementFinished() {
  if (
this.is_stopped || this.waiting) return;
  
temp.next_action this.actions[this.actionpos+1];
  if (
temp.next_action.pos("move") == -&& !this.reversing) {
    
setcharani(this.ani_idle"");
  }
  if (
this.reversing) {
    
this.reversepos--;
    if (
this.reversepos <= -1) {
      
this.reversing false;
      
this.moves "";
      
setcharani(this.ani_idle"");
      return 
onNextStep();
    }
    
temp.move this.moves[this.reversepos];
    return 
doMove(-temp.move[0], -temp.move[1]);
  }
  else 
onNextStep();
}

// Action Related Functions

function onClearChat() {
  
this.chat "";
}

function 
onClearEmote() {
  
hideimg(200);
}

function 
onNextStep(done_waiting) {
  if (
this.waiting && done_waiting) {
    
this.waiting false;
  }
  if (
this.is_stopped) return;
  
this.actionpos++;
  
doAction(this.actionpos);

__________________
Quote:
Reply With Quote
  #2  
Old 06-08-2015, 10:29 PM
Draenin Draenin is offline
Magnificent Bastard
Draenin's Avatar
Join Date: Dec 2004
Location: Bermuda Triangle
Posts: 6,790
Draenin has much to be proud ofDraenin has much to be proud ofDraenin has much to be proud ofDraenin has much to be proud ofDraenin has much to be proud ofDraenin has much to be proud of
Send a message via AIM to Draenin Send a message via MSN to Draenin Send a message via Yahoo to Draenin
Quote:
Originally Posted by fowlplay4 View Post
Added support for more verbose move commands.

e.g. movewest, moveup, movedownright, movenorth, etc.

move[up|down|left|right|north|east|west|south|][up|down|left|right|north|east|west|south|]

You can also specify an amount of tiles to move, otherwise it just moves by 1 tile.
I seriously cannot give you enough rep for this improvement. If people aren't using this script already, they definitely should be doing so right about now.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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:13 PM.


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