Forums Archived

The forums are being archived. See this topic for more information.

AI scripting 4 pokemon/FinalFantasy style rpg

Get help and support with Rubygame

AI scripting 4 pokemon/FinalFantasy style rpg

Postby HOMER-16 » Fri Apr 02, 2010 8:03 pm

How would I write a code that would randomize NPC movement? Also any ideas as to how to make attacks semi-random.

I would actually love to see a built-in module that gave people some of these tools already.
HOMER-16
 
Posts: 13
Joined: Sat Dec 19, 2009 6:02 am

Re: AI scripting 4 pokemon/FinalFantasy style rpg

Postby jacius » Sat Apr 03, 2010 2:52 am

It's pretty simple in Ruby to choose a random item from an Array of choices. For example, you can do this code to a define a new method called "rand" in the Array class:

Code: Select all
class Array
  def rand
    self[Kernel.rand(length)]
  end
end

Then you could choose a random direction or attack like this:

Code: Select all
direction = [:up, :down, :left, :right].rand
attack = [:sword, :jump, :fire, :ice].rand

Since it's so simple to do this, and other libraries already provide it (such as ActiveSupport), I don't think it makes sense for Rubygame to also provide it.
User avatar
jacius
Site Admin
 
Posts: 131
Joined: Fri Feb 06, 2009 11:13 pm

Re: AI scripting 4 pokemon/FinalFantasy style rpg

Postby HOMER-16 » Fri Apr 09, 2010 5:58 pm

Thanks jacius.

How do I blit an image to the screen without the white backround when I make something in paint?

Also, how do I write and read YAML files with ruby?
HOMER-16
 
Posts: 13
Joined: Sat Dec 19, 2009 6:02 am

Re: AI scripting 4 pokemon/FinalFantasy style rpg

Postby jacius » Fri Apr 09, 2010 11:38 pm

HOMER-16 wrote:How do I blit an image to the screen without the white backround when I make something in paint?

You'll need to set the image surface's "colorkey" to white. There's also a neat trick of reading the color of the top left pixel in the image, and using that for the colorkey. That way you can change the background color in the image, and the code will still work with no changes. You can see an example of that in the chimp.rb sample script

HOMER-16 wrote:Also, how do I write and read YAML files with ruby?

Ruby comes with a pretty nice YAML module. The methods you'll be most interested in are "dump" and "load", like in the example in those docs. Of course, you'll need to read and write the contents of the file itself, but I'm sure you can find plenty of tutorials about that online. ;)
User avatar
jacius
Site Admin
 
Posts: 131
Joined: Fri Feb 06, 2009 11:13 pm

Re: AI scripting 4 pokemon/FinalFantasy style rpg

Postby HOMER-16 » Sat Apr 10, 2010 8:25 pm

Ok, I've looked it up online but there is no detaled explanasion of YAML that I can read. :evil:

I need a way to take individual numbers and use them to put a certain image at a certain location.

I did this in TI Basic with a matrix of numbers and iterate over each one, with another program acting as a holder file.

basically the Yaml will look like this

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 4 4 0 0 0 2 2 2 2 0 1
1 0 0 0 4 4 4 0 0 0 2 2 2 2 1
1 3 0 2 2 4 4 4 0 0 2 2 2 0 1
1 3 3 2 0 0 5 5 5 0 2 2 2 2 1
1 1 1 1 1 1 1 4 4 4 1 1 1 1 1

I need to iterate over each number.
HOMER-16
 
Posts: 13
Joined: Sat Dec 19, 2009 6:02 am

Re: AI scripting 4 pokemon/FinalFantasy style rpg

Postby jacius » Sun Apr 11, 2010 2:22 am

Okay. If you're using YAML, you'll need to format the file something like this:

Code: Select all
---
tiles:
  - [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  - [1, 0, 0, 0, 4, 4, 0, 0, 0, 2, 2, 2, 2, 0, 1]
  - [1, 0, 0, 0, 4, 4, 4, 0, 0, 0, 2, 2, 2, 2, 1]
  - [1, 3, 0, 2, 2, 4, 4, 4, 0, 0, 2, 2, 2, 0, 1]
  - [1, 3, 3, 2, 0, 0, 5, 5, 5, 0, 2, 2, 2, 2, 1]
  - [1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 1, 1, 1, 1, 1]

Let's call that file "map1.yaml". To load and parse the file in Ruby, you can use code like this:

Code: Select all
require 'yaml'                    # Load the YAML module
mapfile = File.open("map1.yaml")  # Open the file
map1 = YAML.load(mapfile)         # Parse the file
mapfile.close                     # Close the file

Or, using the power of ruby, you can do the same thing in less code:

Code: Select all
require 'yaml'
map1 = File.open("map1.yaml"){ |mapfile|  YAML.load(mapfile)  }

Either way, the "map1" variable in ruby now holds the following Hash:

Code: Select all
{"tiles"=>
  [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
   [1, 0, 0, 0, 4, 4, 0, 0, 0, 2, 2, 2, 2, 0, 1],
   [1, 0, 0, 0, 4, 4, 4, 0, 0, 0, 2, 2, 2, 2, 1],
   [1, 3, 0, 2, 2, 4, 4, 4, 0, 0, 2, 2, 2, 0, 1],
   [1, 3, 3, 2, 0, 0, 5, 5, 5, 0, 2, 2, 2, 2, 1],
   [1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 1, 1, 1, 1, 1]]}

Then you could iterate over each tile like this:

Code: Select all
# Iterate over every row in the matrix
map1["tiles"].each_with_index { |row_of_tiles, r|

  # Iterate over every tile in each row
  row_of_tiles.each_with_index { |tile_type, c|

    # r = row number, c = column number
    # tile_type = the value from the matrix

    # For this example, let's print some text:
    puts "[#{r}][#{c}] == #{tile_type}"

  }
}

This example just prints some text, but you could use the "r" and "c" variables (row and column numbers) to calculate a location to draw, and "tile_type" variable to decide what image to draw at that location.
User avatar
jacius
Site Admin
 
Posts: 131
Joined: Fri Feb 06, 2009 11:13 pm

Re: AI scripting 4 pokemon/FinalFantasy style rpg

Postby HOMER-16 » Sun Apr 11, 2010 10:00 pm

Thanks, the format for maticies is familiar to me so I shouldn't have much of a problem with that! ;)
HOMER-16
 
Posts: 13
Joined: Sat Dec 19, 2009 6:02 am


Return to Help & Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron