Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   NPC Movement Help. (https://forums.graalonline.com/forums/showthread.php?t=134257621)

Sage_Shadowbane 01-11-2010 05:31 AM

NPC Movement Help.
 
So yeah, I'm creating my first attempt at NPC Movement, obviously the code itself isn't going to be too great since I'm not that great but learning, so don't make fun of me. XP But yeah, i'm having problem regarding my onwall check, oddly I added an onwall check, and now the NPC isn't moving at all. Could anyone tell me why? Also, I know I could use move(), but i'm going to try with just adjusting the x/y first so any advice regarding a way to do it better than my current would be appreciated.

Here's the code:

PHP Code:

function onPlayerchats() 
{
  if ( 
player.chat == "/destroy" ) {
  
    
destroy();
  }
}

function 
onCreated() 
{
  
setshape(13248);
  
initializeCharacter();
}

function 
initializeCharacter()
{
  
setcharAni("idle"nil);
  
  
this.charDirection 2;
  
this.decisionRan int(random(02));
  if ( 
this.decisionRan == this.decisionAction "Walk";
  if ( 
this.decisionRan == this.decisionAction "Idle";
  if ( 
this.decisionRan == this.decisionAction "Animate";
  
this.decisionTimer 0;
  
this.decisionMax int(random(25));
  
  
onTimeout();
}

function 
onTimeout()
{
  
introduceDecisions();
  
introduceLife();
  
setTimer(0.01);
}

function 
introduceDecisions()
{
  if ( 
this.decisionTimer ) {
  
    
this.decisionTimer += .05;
  }
  if ( 
this.decisionTimer >= this.decisionMax ) {
  
    
this.decisionRan int(random(03));
    if ( 
this.decisionRan == this.decisionAction "Walk";
    if ( 
this.decisionRan == this.decisionAction "Idle";
    if ( 
this.decisionRan == this.decisionAction "Animate";
    
this.decisionTimer 0;
    
this.charAni false;
    
this.decisionMax int(random(14));
  }
}

function 
introduceLife()
{
  if ( 
this.decisionAction == "Walk" characterMove();
  if ( 
this.decisionAction == "Idle" characterIdle();
  if ( 
this.decisionAction == "Animate" characterAnimate();
  
dir this.charDirection;
}

function 
characterMove()
{
  if ( 
this.dirTimer this.dirTimer += .05;
  if ( 
this.dirTimer >= ) {
  
    
this.xRan int(random(03));
    
this.yRan int(random(03));
    
this.dirTimer 0;
  }
  if ( 
this.xRan == ) {
    
this.xMove .45;
  }
  if ( 
this.xRan == ) {
    
this.xMove = -.45;
  }
  if ( 
this.xRan == this.xMove 0;
  if ( 
this.yRan == ) {
    
this.yMove .45;
  }
  if ( 
this.yRan == ) {
    
this.yMove = -.45;
  }
  if ( 
this.yRan == this.yMove 0
  if ( 
this.xRan == this.charDirection 3;
  else
  if ( 
this.xRan == this.charDirection 1;
  else
  if ( 
this.yRan == this.charDirection 2;
  else 
  if ( 
this.yRan == this.charDirection 0
  
this.charMoving true;
  if ( 
this.charMoving ) {
  
    
this.charPos = {this.1.5 vecx(this.charDirection), 
                    
this.vecy(this.charDirection)};
    
chat this.charPos;
    if ( !
onwall(this.charPos[0], this.charPos[1]) ) {  
    
      
this.xMove;
      
this.yMove;
      if ( !
this.charAni ) {
    
        
setcharAni("walk"nil);
        
this.charAni true;
      }
    }
  }
}

function 
characterIdle()
{

  
this.charMoving false;
  
setcharAni("idle"nil);
}

function 
characterAnimate() {

  
this.charMoving false;
  if ( !
this.charAni ) {
  
    
setcharAni("lift"nil);
    
this.charAni true;
  }



fowlplay4 01-11-2010 05:56 AM

First things first.. setTimer(0.01), the maximum is 0.1 on the server-side.

Also Incrementing X, Y looks kinda ugly and if you use the wall detection bit-flag with move you get built-in wall detection, and there's not much point re-inventing the wheel but from what I can tell you have the right idea. You check if the next movement is on the wall, and handle it accordingly.

I wrote a class, that has some movement functions using move in it. Feel free to take a look at it:

Movement Class

DustyPorViva 01-11-2010 06:09 AM

When I did my serverside movement with move(), I used my own wall detection by running a loop beforehand and determining how far the NPC should move instead of relying on a constant wall detection. However, this isn't very good for NPCs that are moving constantly, but very useful for NPCs that are moving predefined, but random movements(AKA randomly left or right or so on) between pauses.


All times are GMT +2. The time now is 08:25 PM.

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