Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-11-2010, 05:31 AM
Sage_Shadowbane Sage_Shadowbane is offline
Graal Developer
Sage_Shadowbane's Avatar
Join Date: Mar 2004
Posts: 585
Sage_Shadowbane will become famous soon enough
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;
  }

Reply With Quote
  #2  
Old 01-11-2010, 05:56 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
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
__________________
Quote:
Reply With Quote
  #3  
Old 01-11-2010, 06:09 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
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.
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 04:17 PM.


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