class Object

Public Instance Methods

cmd_list_commands(player, input) click to toggle source

List the commands available to the player TODO: Pretty this output up a little bit - even columns would be nice

# File lib/skirmish/commands.rb, line 59
def cmd_list_commands(player, input)
  commands = $commands.keys

  print_line commands.join(" ")
  print_line
end
cmd_list_exits(player, input) click to toggle source

List the available exits from the room the player is currently in

# File lib/skirmish/commands.rb, line 45
def cmd_list_exits(player, input)
  exits = $world.get_exits player.location

  exits_list = "[ Exits: "
  until exits.empty? do
    exits_list << "#{exits.shift} "
  end
  exits_list << "]\n"

  print_line exits_list, :cyan
end
cmd_look(player, input) click to toggle source

For the Player's current location, display the room name, description, and any other characters who are in the room with the player

# File lib/skirmish/commands.rb, line 68
def cmd_look(player, input)

  look_at = input.split[1..-1]

  # If the player has specified a target / keyword, try to find it in the list
  # of keywords on characters in the room
  # TODO: Look at room keywords as well (i.e. 'look sign', etc.)
  unless look_at.nil? || look_at.first.nil?
    matches = $world.get_room_characters(player.location)
              .select { |c| c.keywords =~ /\A#{Regexp.escape(look_at.first)}/i }
    unless matches.first.nil?
      print_line matches.first.description + "\n"
    else
      print_line "There is nothing to look at with that name.\n"
    end
  # No target specified - just show them the room name / description, any other
  # characters who might be present, and the available exits
  # TODO: Get the '...is standing here' description from the character
  else
    print_line $world.get_room_name(player.location), :cyan
    print_line $world.get_room_description(player.location)
    $world.get_room_characters(player.location).each do | char |
      unless char.name == player.name
        print_line char.name + " is standing here.", :white
      end
    end
    cmd_list_exits player, nil
  end
end
cmd_move_character(character, direction) click to toggle source

Move the character in the specified direction, if possible

# File lib/skirmish/commands.rb, line 15
def cmd_move_character(character, direction)
  # Ask the world if there's a room in the direction we want to move
  new_location = $world.get_destination(character.location,
        case direction
                when /\An/i then 0
                when /\Ae/i then 1
                when /\As/i then 2
                when /\Aw/i then 3
                when /\Au/i then 4
                when /\Ad/i then 5
        end
        )
  # If there's a room to go to, tell the world where the character is going, and
  # let the character track it as well. If character is a player, either display
  # their new location or a message telling them they are unable to move in that
  # direction
  unless new_location.nil?
    $world.move_character character, character.location, new_location
    character.location = new_location
    if character.is_a?(Player)
      cmd_look character, ""
    end
  else
    if character.is_a?(Player)
      print_line "You can't go that way!\n"
    end
  end
end
cmd_quit(player, input) click to toggle source

Quit the game. Make sure the player is sure! We don't want to quit on an accidental 'q', for example TODO: Save player / world state

# File lib/skirmish/commands.rb, line 123
def cmd_quit(player, input)
  unless input =~ /quit/i
    print_line "You must type the entire word 'quit' to quit.\n"
  else
    print_line "Until next time..."
    $win.refresh
    sleep 3
    $win.close
    exit
  end
end
cmd_stats(player, input) click to toggle source

Print the player's statistics to the screen

# File lib/skirmish/commands.rb, line 99
def cmd_stats(player, input)

  # Height is stored in inches, but it's nice to have something like 5'10"
  feet = player.height / 12
  inches = player.height % 12

  print_line("Your name is %s. You are %d'%d\" tall and you weigh %d lbs." %
  [player.name, feet, inches, player.weight])
  print_line("You are level %d and have %d experience points." %
  [player.level, player.xp])
  print_line("============================================================")
  print_line("Hitpoints:   %6d / %d" % [player.hp, player.maxhp])
  print_line("Attack:      %6d - %2d" % [player.level, player.level * 10])
  print_line("Armour:           %6d" % player.armour)
  print_line("============================================================")
  print_line("Strength:    %6d                     Charisma:    %6d" % [player.str, player.cha])
  print_line("Constitution:%6d                     Wisdom:      %6d" % [player.con, player.wis])
  print_line("Dexterity:   %6d                     Intelligence:%6d" % [player.dex, player.int])
  print_line
end
get_input() click to toggle source

Look for input from the player, and return it once they have pressed 'enter'. Otherwise, return nil

# File lib/skirmish/utilities.rb, line 50
def get_input
  case char = $win.getch
    # Backspace: Remove the last character from the input buffer
    when Curses::KEY_BACKSPACE
      $input_buffer.pop
      return nil

    # Up: Look for the previous command in the command history. If something
    # exists, clear the input buffer and replace it with the last command
    when Curses::KEY_UP
      previous_command = $command_history.get_previous
      unless previous_command.nil?
        $input_buffer.clear
        previous_command.each_char { |chr|  $input_buffer.push chr }
      end
      return nil

    # Down: Look for the next command in the command history. If something
    # exists, clear the input buffer and replace it with the next command
    when Curses::KEY_DOWN
      next_command = $command_history.get_next
      unless next_command.nil?
        $input_buffer.clear
        next_command.each_char { |chr|  $input_buffer.push chr }
      end
      return nil

    # Tab: See if there's a command that will match to what's currently in the
    # input buffer. If there is, clear the buffer and replace with the command.
    # If not, do nothing
    when 9
      possible_commands = $commands.select { |c| c =~ /\A#{Regexp.escape($input_buffer.join)}/i }
      # TODO: Look for a space in the buffer instead of this horribe hack.
      # We don't want to tab-complete the argument to a command
      unless possible_commands.nil? or possible_commands.first.nil?
        $input_buffer.clear
        possible_commands.first.first.each_char { |chr|  $input_buffer.push chr }
      end
      return nil

    # Enter: Add the input buffer to the command history, and return the input
    # as a string
    when 10 , 13
      input = $input_buffer.join
      $input_buffer.clear
      $command_history.add_command input
      return input

    # Any printable character: Add the character to the input buffer
    when /[[:print:]]/
      $input_buffer << char
      return nil
  end
end
handle_input(input) click to toggle source

Find a command to invoke based on input from a character.

# File lib/skirmish/utilities.rb, line 130
def handle_input(input)
  return if input.nil?

  input.chomp!

  # Print functions clear the current line, so if we're handling input, we need
  # to move down a few lines so we can see what we entered on our screen.
  $win.addstr("\n\n")

  if input.length == 0
    print_line("Please enter a valid command. A list of commands is available by typing 'commands'.\n")
    return nil
  end

  # See if we can find a command key that matches the first word of the input
  matches = $commands.select { |c| c =~ /\A#{Regexp.escape(input.split.first)}/i }
  command = matches.first

  unless command.nil?
    # We have a match! Call the method associated with the command
    return command[1].call($player, input)
  else
    print_line("\"#{input}\" is not a valid command. A list of commands is available by typing 'commands'.\n")
  end
end
print_line(line = "", color = :green) click to toggle source

Print a line to the screen.

roll_dice(number_of_dice, size_of_dice) click to toggle source

Roll number_of_dice with size_of_dice sides, and return the result

# File lib/skirmish/utilities.rb, line 184
def roll_dice(number_of_dice, size_of_dice)
  (1..number_of_dice).inject(0){ | sum, _ | sum + rand(1..size_of_dice) }
end
setup_screen() click to toggle source

Curses screen setup and color pair definitions

# File lib/skirmish/utilities.rb, line 106
def setup_screen
  Curses.init_screen
  Curses.start_color
  Curses.cbreak
  Curses.noecho
  Curses.nl

  # Color pairs. Arguments: color_number, foreground color, background color
  Curses.init_pair 1, Curses::COLOR_GREEN, Curses::COLOR_BLACK
  Curses.init_pair 2, Curses::COLOR_CYAN, Curses::COLOR_BLACK
  Curses.init_pair 3, Curses::COLOR_WHITE, Curses::COLOR_BLACK

  # Set up the window to fill the whole terminal
  $win = Curses::Window.new(0, 0, 0, 0)
  # Set the initial color to green on black
  $win.color_set(1)
  # Allow the screen to scroll, and allow the terminal to scroll as well
  $win.scrollok true
  $win.idlok true
  # Allow capture of non-alpha key inputs (arrows, enter, etc.)
  $win.keypad = true
end
show_prompt(prompt=">") click to toggle source

Show a prompt to the player

# File lib/skirmish/utilities.rb, line 157
def show_prompt(prompt=">")
  $win.color_set(3)
  $win.setpos($win.cury, 0)
  $win.deleteln
  $win.addstr(prompt + " " + $input_buffer.join)
  $win.color_set(1)
end