Difference between revisions of "Ruby"

From RogueBasin
Jump to navigation Jump to search
m (Use wiki formatting, not HTML. Made Python claim more accurate (Python has a JIT compiler too: it's called Psyco.).)
Line 3: Line 3:
Ruby is still coming of age in the roguelike field -- there are no roguelike libraries and only a few finished games in the language. That said, Ruby is very powerful, combining an elegant syntax with an incredibly dynamic object system. It's also incredibly fun to code with. But beware! Many dangers lurk within beneath its alluring facade!
Ruby is still coming of age in the roguelike field -- there are no roguelike libraries and only a few finished games in the language. That said, Ruby is very powerful, combining an elegant syntax with an incredibly dynamic object system. It's also incredibly fun to code with. But beware! Many dangers lurk within beneath its alluring facade!


In terms of speed, Ruby 1.8 measured far below Python in terms of performance. This is mostly due to Python having a bytecode compiler. However, Ruby 1.9 has JIT compilation and <i>out-performs</i> Python. It should be noted however that ruby does have a C API, and extending Ruby in C can result in very fast code (only a bit slower than C itself).
In terms of speed, Ruby 1.8 measured far below Python in terms of performance. This is mostly due to Python having a bytecode compiler. However, Ruby 1.9 has just-in-time compilation and ''out-performs'' CPython (the official Python implementation). It should be noted however that Ruby does have a C API, and extending Ruby in C can result in very fast code (only a bit slower than C itself).


Ruby is not well documented in relation to other languages, but this is getting better all the time. There are quite a few gotchas (like using <tt>setter=</tt> methods inside of classes).  
Ruby is not well documented in relation to other languages, but this is getting better all the time. There are quite a few gotchas (like using <tt>setter=</tt> methods inside of classes).  
Line 37: Line 37:
===Line of sight===
===Line of sight===


[[Bresenham's Line Algorithm|Here]] is a Bresenham's Line Algorithm implementation in Ruby
[[Bresenham's Line Algorithm|Here]] is a Bresenham's Line Algorithm implementation in Ruby.


===Field of View===
===Field of View===

Revision as of 17:49, 18 November 2010

Ruby

Ruby is still coming of age in the roguelike field -- there are no roguelike libraries and only a few finished games in the language. That said, Ruby is very powerful, combining an elegant syntax with an incredibly dynamic object system. It's also incredibly fun to code with. But beware! Many dangers lurk within beneath its alluring facade!

In terms of speed, Ruby 1.8 measured far below Python in terms of performance. This is mostly due to Python having a bytecode compiler. However, Ruby 1.9 has just-in-time compilation and out-performs CPython (the official Python implementation). It should be noted however that Ruby does have a C API, and extending Ruby in C can result in very fast code (only a bit slower than C itself).

Ruby is not well documented in relation to other languages, but this is getting better all the time. There are quite a few gotchas (like using setter= methods inside of classes).

Libraries

  • Chingu, a game framework that extends Gosu with higher level features
  • Gosu, a 2D game development library
  • Ncurses, cross-platform, true Curses support (built-in Curses support has no colours under Windows and is generally a mess)
  • Rubygame, multimedia library for entertainment software, scientific data visualisation programs, educational/training tools and interactive motion graphics


As people begin to use Ruby for RL development, code will surface. There are already a few algorithms implemented and put up at RogueBasin.

Okay, I'm going to use Ruby. What now?

You'll need to install a copy of Ruby from http://www.ruby-lang.org/ (I recommend the one-click installer) or your package manager (apt-get install ruby). Then acquaint yourself with the Ruby tools: irb, ri and rdoc. A good version of the standard library API can be found at http://www.noobkit.com/

ncurses-ruby

It is possible to use the built-in curses on linux and other platforms, and interface with the Win32 console via the Win32 API for Windows support. However, this is a relative pain. Instead, install the non-standard ncurses-ruby package!

On Windows:

  • Download ncurses-ruby1.8-0.9.1-i386-mswin32.zip from http://ncurses-ruby.berlios.de/
  • Copy lib/ncurses.rb and ncurses.so into your code directory (don't copy the lib directory itself!)
  • In your script, require 'ncurses'

That's it! You can now use ncurses (see the zdennis's ncurses examples at http://github.com/zdennis/ncurses_examples/tree/master or the C API, which is very similar). At some point an example roguelike using ncurses will be uploaded.

Line of sight

Here is a Bresenham's Line Algorithm implementation in Ruby.

Field of View

A few field of view algorithms have been implemented in Ruby

Bitfields

For certain intensive operations it may be prudent to use a Bitfield. One example is storing which map tiles have been visited by your player (and will be drawn).

Using this library, it is easy to add methods to your Map class for FOV calculation:

def initialize(map)
    ....
    @visited = BitField.new(@width * @height) # visited by player (drawn in grey)
    @lit = BitField.new(@width * @height) # seen by player this round (drawn in white)
end

# Return true if the square is lit (seen by player)
def lit?(x, y)
    @lit[y * @width + x] == 1
end

# Returns true if square has been visited by player
def visited?(x, y)
    @visited[y * @width + x] == 1
end

# Set lit status for square. Also visits the square
def light(x, y)
    idx = y * @width + x
    @lit[idx] = @visited[idx] = 1
end

# Unlight everything (call after doing FOV calc + map draw)
def reset_light
    @lit.clear
end