Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

SAI Wander State

SAI Wander State Version: 1.01
By: Bishop Myers ("Sailerius")

Introduction

This script alters the behavior of the Random movement to appear more natural instead of the artificial-seeming default movement. It requires no special configuration to run.

Screenshots

It's random movement; screenshots won't really convey what it looks like.

Demo

http://www.megaupload.com/?d=C39Z9PVG

Script

Code:
#===============================================================================

# ** SAI Random Wander State

#-------------------------------------------------------------------------------

# Author: Bishop Myers ("Sailerius")

# Version: 1.01

# Date: 2011.06.15

# SDK Version: 2.4

#===============================================================================

# Instructions

# ------------------------------------------------------------------------------

# Just paste this script in below the SDK and all events using Random movement 

# will automatically begin using the wander state.

# ------------------------------------------------------------------------------

# What It Does

# ------------------------------------------------------------------------------

# The default Random movement looks very artificial. Instead of choosing a 

# random adjacent tile to move to, the events will not select a random

# destination and walk to it, making it appear as if they're moving with

# purpose. Once they reach their new destination, the events will select a new

# point to wander to.

#===============================================================================

#-------------------------------------------------------------------------------

# * SDK Log

#-------------------------------------------------------------------------------

SDK.log('SAI Wander State', 'Sailerius', 1.0, '2011.06.13')

SDK.log_overwrite(:Game_Character, :move_random)

 

#===============================================================================

# ** Game_Character

#===============================================================================

class Game_Character

  #-----------------------------------------------------------------------------

  # * Alias Listings

  #-----------------------------------------------------------------------------

  alias_method :sailerius_saiwander_gamecharacter_initialize,  :initialize

  alias_method :sailerius_saiwander_gamecharacter_movetyperandom,  :move_type_random

  

  #-----------------------------------------------------------------------------

  # * Public Instance Variables

  #-----------------------------------------------------------------------------

  attr_reader   :wander_x          # map x-coordinate to pursue

  attr_reader   :wander_y          # map y-coordinate to pursue

  

  #--------------------------------------------------------------------------

  # * Object Initialization

  #--------------------------------------------------------------------------

  def initialize

    sailerius_saiwander_gamecharacter_initialize

    set_wander_destination    

  end  

  

  #--------------------------------------------------------------------------

  # * Move Type : Random

  #--------------------------------------------------------------------------

  def move_type_random      

    # Check if reached destination

    if (@wander_x == @x and @wander_y == @y)

      set_wander_destination

    end

    sailerius_saiwander_gamecharacter_movetyperandom

  end

  

  #--------------------------------------------------------------------------

  # * Move at Random

  #--------------------------------------------------------------------------

  def move_random    

    # Get distance from destination

    dx = @x - @wander_x

    dy = @y - @wander_y    

    # Move horizontally

    unless dx == 0

      if dx < 0

        # Go somewhere else if impassable

        if not passable?(@x + 1, @y, 0)

          set_wander_destination

          return

        end

        move_right(false)

        return

      else

        # Go somewhere else if impassable

        if not passable?(@x - 1, @y, 0)

          set_wander_destination

          return

        end

        move_left(false)

        return

      end

    end    

    # Move vertically

    unless dy == 0

      if dy < 0

        # Go somewhere else if impassable

        if not passable?(@x, @y + 1, 0)

          set_wander_destination

          return

        end

        move_down(false)

        return

      else

        # Go somewhere else if impassable

        if not passable?(@x, @y - 1, 0)

          set_wander_destination

          return

        end

        move_up(false)

        return

      end

    end    

  end

  

  #--------------------------------------------------------------------------

  # * Determine New Wander Destination

  #--------------------------------------------------------------------------

  def set_wander_destination

    @wander_x = @x + rand(40) - 20

    @wander_y = @y + rand(40) - 20

  end

end


Instructions

Just paste it in like you would any other script. This is designed to work with the SDK and should be pasted below it.

FAQ

Q: How does this differ from the default behavior?
A: All events using the Random movement will now select a random destination to seek out. Once they reach this destination, they will choose a new one, making it feel as if their movement is purposeful.

Compatibility

This script is SDK compliant and is designed for version 2.4.

Terms and Conditions

This script is free for all use, including commercial. Please give credit to Bishop Myers ("Sailerius") if you use this script.
 
I ran the demo. The npcs stayed in the one corner of the map and never moved more then a screen away. When I spread them out they still walked up to the corner of the map or got stuck on the way. They also like to stand in doorways, which at first I thought would be annoying, but if you made the sprite disappear it would look like the actually went inside. Just as long as they didn't get in your way when you're trying to go somewhere.
 
coyotecraft":1hqxb7th said:
I ran the demo. The npcs stayed in the one corner of the map and never moved more then a screen away. When I spread them out they still walked up to the corner of the map or got stuck on the way. They also like to stand in doorways, which at first I thought would be annoying, but if you made the sprite disappear it would look like the actually went inside. Just as long as they didn't get in your way when you're trying to go somewhere.
Really? That's strange. When I run the demo, they don't cluster around a specific area. Also, the NPCs are set to avoid obstacles (e.g., you) so there's no risk of them blocking your way.
 

MarkR

Member

Great concept!
This is a lot better than the random movement rmxp uses. :D
I will probably use this for one of my own scripts. :)

coyotecraft":22eztsq5 said:
I ran the demo. The npcs stayed in the one corner of the map and never moved more then a screen away. When I spread them out they still walked up to the corner of the map or got stuck on the way. They also like to stand in doorways, which at first I thought would be annoying, but if you made the sprite disappear it would look like the actually went inside. Just as long as they didn't get in your way when you're trying to go somewhere.
I think that's because these lines (in the set_wander_destination method):
    @wander_x = 0
    @wander_y = 0

Should be:
    @wander_x = @x
    @wander_y = @y

Because otherwise the wander x and y destination wil always be between -20 and 20.


Btw: You can shorten this code:
    # Assign x-coordinate
    case rand(1)
    when 0
      @wander_x += rand(20)
    when 1
      @wander_x -= rand(20)
    end
    # Assign y-coordinate
    case rand(1)
    when 0
      @wander_y += rand(20)
    when 1
      @wander_y -= rand(20)
    end

By using this code instead:
    @wander_x += rand(40) - 20
    @wander_y += rand(40) - 20
 
I moved them down to the right corner of the map. A soon as it starts they all walk neatly to left. Branch off a bit when they hit the left side of the map and start walking up. Some get stuck in the tent doorways and never move again. Then they just cluster in the top left corner. Sometimes walking into tents and then out again.

path.png
 
I applied the changes MarkR suggested and it works. Sometimes they get stuck walking back and forth between things like the edge of the water and the map. I guess you have to tailor the map with lots of openings other wise they'll just keep bumping into things until they have a destination they can actually walk to.
On a map like this they had a low probability of making it to the top-right. They hit the water start walking somewhere else instead.
 
Ah, thanks for the suggestions! This is my first nontrivial script, so I appreciate the feedback. I'm updating the first post with the changes.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top