RCRPG/Go: Difference between revisions

Content added Content deleted
m (Typo)
(library changes)
Line 2: Line 2:


import (
import (
"container/vector"
"fmt"
"fmt"
"io"
"io"
"math/rand"
"os"
"os"
"rand"
"sort"
"sort"
"strings"
"strings"
Line 482: Line 481:
name := strings.ToLower(args[0])
name := strings.ToLower(args[0])
switch name {
switch name {
case "north", "south", "east", "west", "up", "down", "attack", "drop", "take", "inventory", "name", "equip", "alias":
case "north", "south", "east", "west", "up", "down", "attack",
"drop", "take", "inventory", "name", "equip", "alias":
fmt.Printf("Can't overwrite the %q command.\n", name)
fmt.Printf("Can't overwrite the %q command.\n", name)
return
return
Line 544: Line 544:
// Resolves aliases and dispatches to the correct handler
// Resolves aliases and dispatches to the correct handler
func (g *Game) Dispatch(args []string) {
func (g *Game) Dispatch(args []string) {
argv := vector.StringVector(args)
seen := make(map[string]bool)
seen := make(map[string]bool)
name := argv.At(0)
name := args[0]
for {
seen[name] = true
seen[name] = true
prefix, ok := g.Aliases[name]
prefix, ok := g.Aliases[name]
for ok {
if !ok {
argv.Cut(0, 1)
break
prefixv := vector.StringVector(prefix)
}
argv.InsertVector(0, &prefixv)
args = append(prefix, args[1:]...)
name = argv.At(0)
name = args[0]
if seen[name] {
if seen[name] {
fmt.Print("Recursive alias.\n")
fmt.Print("Recursive alias.\n")
return
return
}
}
seen[name] = true
prefix, ok = g.Aliases[name]
}
}
cb := g.Commands[name]
cb := g.Commands[name]
Line 566: Line 564:
return
return
}
}
cb(argv[1:])
cb(args[1:])
return
return
}
}
Line 627: Line 625:
// Read a single line of input
// Read a single line of input
func readLine(in io.Reader) (string, bool) {
func readLine(in io.Reader) (string, bool) {
var line []string
line := new(vector.StringVector)
buf := []byte{0}
buf := []byte{0}
_, err := in.Read(buf)
_, err := in.Read(buf)
for err == nil && buf[0] != '\n' && buf[0] != '\r' {
for err == nil && buf[0] != '\n' && buf[0] != '\r' {
line.Push(string(buf))
line = append(line, string(buf))
_, err = in.Read(buf)
_, err = in.Read(buf)
}
}
Line 637: Line 635:
in.Read(buf)
in.Read(buf)
}
}
return strings.Join(line.Copy(), ""), err == nil
return strings.Join(line, ""), err == nil
}
}


// Returns a sorted array of command and alias names
// Returns a sorted array of command and alias names
func (g *Game) GetCommands() []string {
func (g *Game) GetCommands() []string {
var commandNames []string
commandNames := new(vector.StringVector)
for name := range g.Commands {
for name := range g.Commands {
commandNames.Push(name)
commandNames = append(commandNames, name)
}
}
for name := range g.Aliases {
for name := range g.Aliases {
commandNames.Push(name)
commandNames = append(commandNames, name)
}
}
sort.Sort(commandNames)
sort.Strings(commandNames)
return commandNames.Copy()
return commandNames
}
}


// Main game loop
// Main game loop
func (g *Game) Run() {
func (g *Game) Run() {
fmt.Printf("Welcome to RC Minimalist RPG, Go version %d.%d.\n", VERSION_MAJOR, VERSION_MINOR)
fmt.Printf("Welcome to RC Minimalist RPG, Go version %d.%d.\n",
VERSION_MAJOR, VERSION_MINOR)
fmt.Print("You start in room 0,0,0. Your goal is at 1,1,5. Good luck!\n")
fmt.Print("You start in room 0,0,0. Your goal is at 1,1,5. Good luck!\n")
fmt.Printf("Commands: %s\n", strings.Join(g.GetCommands(), ", "))
fmt.Printf("Commands: %s\n", strings.Join(g.GetCommands(), ", "))
Line 666: Line 665:
}
}
for ok {
for ok {
tokens := strings.Split(line, " ", -1)
tokens := strings.Split(line, " ")
g.Dispatch(tokens)
g.Dispatch(tokens)
fmt.Print("\n> ")
fmt.Print("\n> ")