RPG text interpreter

I was just playing textual RPG, and I was wondering how exactly the command interpreters were implemented and is there a better way to implement something like this now? It would be simple enough to make a ton of if statements, but this seems cumbersome, especially considering most of pick up the goldit is the same as that pick up gold, which has the same effect as take gold. I am sure that this is a very deep question, I just would like to know the general idea of ​​how such interpreters were implemented. Or if there is an open source game with a decent and representative interpreter, that would be perfect.

Answers may be language independent, but try to keep them in something reasonable, not a prologue or golf script or anything else. I don’t know exactly how to put it.

+3
source share
2 answers

The usual name for this kind of game is text adventure or interactive fantasy if it's a single player, or MUD if it's multiplayer.

There are several special programming languages ​​for writing interactive fiction, such as Inform 6 , Inform 7 (a completely new language that compiles before Inform 6), TADS , Hugo , etc.

Inform 7, , , , :

"Example Game" by Brian Campbell

The Alley is a room. "You are in a small, dark alley." A bronze key is in the 
Alley. "A bronze key lies on the ground."

:

Example Game
An Interactive Fiction by Brian Campbell
Release 1 / Serial number 100823 / Inform 7 build 6E59 (I6/v6.31 lib 6/12N) SD

Alley
You are in a small, dark alley.

A bronze key lies on the ground.

>take key
Taken.

>drop key
Dropped.

>take the key
Taken.

>drop key
Dropped.

>pick up the bronze key
Taken.

>put down the bronze key
Dropped.

>

, , , , , MUD-.

, , . , Ruby ( ):

case input
  when /(?:take|pick +up)(?: +(?:the|a))? +(.*)/
    take_command(lookup_name($3))
  when /(?:drop|put +down)(?: +(?:the|a))? +(.*)/
    drop_command(lookup_name($3))
end

, . , , :

OPT_ART = "(?: +(?:the|a))?"  # shorthand for an optional article
case input
  when /(?:take|pick +up)#{OPT_ART} +(.*)/
    take_command(lookup_name($3))
  when /(?:drop|put +down)#{OPT_ART} +(.*)/
    drop_command(lookup_name($3))
end

, , . , , , .

lexers , , . , , ; .

, , Treetop, Ruby :

grammar Adventure
  rule command
    take / drop
  end

  rule take
    ('take' / 'pick' space 'up') article? space object {
      def command
        :take
      end
    }
  end

  rule drop
    ('drop' / 'put' space 'down') article? space object {
      def command
        :drop
      end
    }
  end

  rule space
    ' '+
  end

  rule article
    space ('a' / 'the')
  end

  rule object
    [a-zA-Z0-9 ]+
  end
end

:

require 'treetop'
Treetop.load 'adventure.tt'

parser = AdventureParser.new
tree = parser.parse('take the key')
tree.command            # => :take
tree.object.text_value  # => "key"
+6
+1

Source: https://habr.com/ru/post/1761070/


All Articles