rpg

package module
v0.0.0-...-07d2fe9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2014 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package rpg provides a base API for a role playing game.

Example
package main

import (
	"fmt"
	"github.com/Rnoadm/rpg"
	"github.com/Rnoadm/rpg/history"
	"io"
	"io/ioutil"
	"os"
)

func printContainers(s *rpg.State) {
	for _, id := range s.ByComponent(rpg.ContainerType) {
		p := s.Get(id)
		for _, o := range p.Component(rpg.ContainerType).(*rpg.Container).Contents() {
			fmt.Println(p.Component(rpg.NameType), "has", o.Component(rpg.NameType))
		}
	}
}

func main() {
	f, err := ioutil.TempFile(os.TempDir(), "rnoadm")
	if err != nil {
		panic(err)
	}
	defer os.Remove(f.Name())
	defer f.Close()
	h := history.NewHistory(f)

	global := rpg.NewState()

	var personA, personB, itemA, itemB rpg.ObjectIndex
	if !global.Atomic(func(s *rpg.State) bool {
		var pa, pb, ia, ib *rpg.Object
		personA, pa = s.Create(rpg.NameFactory("person A"), rpg.ContainerFactory)
		personB, pb = s.Create(rpg.NameFactory("person B"), rpg.ContainerFactory)
		itemA, ia = s.Create(rpg.NameFactory("item A"))
		itemB, ib = s.Create(rpg.NameFactory("item B"))

		if !pa.Component(rpg.ContainerType).(*rpg.Container).Add(ia) {
			panic("unreachable")
		}
		if !pb.Component(rpg.ContainerType).(*rpg.Container).Add(ib) {
			panic("unreachable")
		}
		return true
	}) {
		panic("unreachable")
	}
	err = h.Append(global)
	if err != nil {
		panic(err)
	}

	printContainers(global)

	fmt.Println("Trade succeeded:", global.Atomic(func(s *rpg.State) bool {
		pa, pb, ia, ib := s.Get(personA), s.Get(personB), s.Get(itemA), s.Get(itemB)
		if !pa.Component(rpg.ContainerType).(*rpg.Container).Remove(ia) {
			return false
		}
		if !pb.Component(rpg.ContainerType).(*rpg.Container).Remove(ib) {
			return false
		}
		if !pa.Component(rpg.ContainerType).(*rpg.Container).Add(ib) {
			return false
		}
		if !pb.Component(rpg.ContainerType).(*rpg.Container).Add(ia) {
			return false
		}
		return true
	}))

	err = h.Append(global)
	if err != nil {
		panic(err)
	}

	printContainers(global)

	fmt.Println()
	h.Reset()
	for {
		s, err := h.Seek(1, history.SeekCur)
		if err == io.EOF {
			break
		}
		if err != nil {
			panic(err)
		}

		fmt.Println("Forward:", h.Tell())
		printContainers(s)
	}

	fmt.Println()
	h.Reset()
	for {
		s, err := h.Seek(-1, history.SeekCur)
		if err == io.EOF {
			break
		}
		if err != nil {
			panic(err)
		}

		fmt.Println("Reverse:", h.Tell())
		printContainers(s)
	}
}
Output:

person A has item A
person B has item B
Trade succeeded: true
person A has item B
person B has item A

Forward: 0
person A has item A
person B has item B
Forward: 1
person A has item B
person B has item A

Reverse: 1
person A has item B
person B has item A
Reverse: 0
person A has item A
person B has item B

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrStateParent         = errors.New("rpg: cannot encode a child State")
	ErrStateVersion        = errors.New("rpg: unrecognized State version")
	ErrObjectStateless     = errors.New("rpg: cannot decode Object directly")
	ErrObjectVersion       = errors.New("rpg: unrecognized Object version")
	ErrContainerVersion    = errors.New("rpg: unrecognized Container version")
	ErrContainerOutOfOrder = errors.New("rpg: Container is out of order")
	ErrResourcesVersion    = errors.New("rpg: unrecognized Resources version")
	ErrResourcesDuplicate  = errors.New("rpg: duplicate key in Resources")
	ErrLocationVersion     = errors.New("rpg: unrecognized Location version")
	ErrMessagesVersion     = errors.New("rpg: unrecognized Messages version")
)

ContainerType can be used with Object.Component to retrieve a Container.

LocationType can be used with Object.Component to retrieve a Location.

View Source
var MaxMessages = 200

Functions

func RegisterComponent

func RegisterComponent(f ComponentFactory) reflect.Type

RegisterComponent allows a ComponentFactory to be used with State.Create. It returns the reflect.Type of the Component which can be used in Object.Component.

Types

type Component

type Component interface {
	// Clone duplicates this Component and replaces the cloned Component's Object
	// with the given Object.
	Clone(*Object) Component
}

Component represents a feature of an Object.

func ContainerFactory

func ContainerFactory(o *Object) Component

ContainerFactory is a ComponentFactory.

func LocationFactory

func LocationFactory(o *Object) Component

LocationFactory is a ComponentFactory.

func MessagesFactory

func MessagesFactory(o *Object) Component

func ResourcesFactory

func ResourcesFactory(o *Object) Component

type ComponentFactory

type ComponentFactory func(*Object) Component

ComponentFactory constructs a Component for use with the given Object. The underlying type of the Component must be a pointer type. The Object is nil when the function is called from RegisterComponent.

func NameFactory

func NameFactory(name string) ComponentFactory

type Container

type Container struct {
	// contains filtered or unexported fields
}

Container is a Component that holds references to other Objects.

func (*Container) Add

func (c *Container) Add(v *Object) bool

Add adds v to c, returning false if v is already in c. If v has a Location, it is modified to the containing object's Location.

func (*Container) ByComponent

func (c *Container) ByComponent(t reflect.Type) []*Object

ByComponent returns the sorted set of Objects that have Component t in c.

func (*Container) Clone

func (c *Container) Clone(o *Object) Component

Clone implements Component.

func (*Container) Contents

func (c *Container) Contents() []*Object

Contents returns the sorted set of Objects in c.

func (*Container) GobDecode

func (c *Container) GobDecode(data []byte) (err error)

GobDecode implements gob.GobDecoder

func (*Container) GobEncode

func (c *Container) GobEncode() (data []byte, err error)

GobEncode implements gob.GobEncoder

func (*Container) Remove

func (c *Container) Remove(v *Object) bool

Remove removes v from c, returning false if v is not in c.

type Location

type Location struct {
	// contains filtered or unexported fields
}

Location represents the position of an Object.

func (*Location) Clone

func (l *Location) Clone(o *Object) Component

Clone implements Component.

func (*Location) Dist

func (l *Location) Dist(x, y, z int64) int64

Dist returns the distance squared between l and (x,y,z).

func (*Location) Equal

func (l *Location) Equal(o *Location) bool

Equal returns true if l and o have the same return values for Get.

func (*Location) Get

func (l *Location) Get() (x, y, z int64)

Get returns the position represented by l.

func (*Location) GobDecode

func (l *Location) GobDecode(data []byte) (err error)

GobDecode implements gob.GobDecoder

func (*Location) GobEncode

func (l *Location) GobEncode() (data []byte, err error)

GobEncode implements gob.GobEncoder

func (*Location) Set

func (l *Location) Set(x, y, z int64)

Set modifies l, along with any Location-having Objects inside a Container.

type Message

type Message struct {
	Source ObjectIndex
	Time   int64
	Text   string
	Kind   string
}

func (*Message) String

func (m *Message) String() string

type Messages

type Messages struct {
	// contains filtered or unexported fields
}

func (*Messages) Append

func (m *Messages) Append(msg Message)

func (*Messages) At

func (m *Messages) At(i int) Message

func (*Messages) Clone

func (m *Messages) Clone(o *Object) Component

func (*Messages) GobDecode

func (m *Messages) GobDecode(data []byte) (err error)

GobDecode implements gob.GobDecoder

func (*Messages) GobEncode

func (m *Messages) GobEncode() (data []byte, err error)

GobEncode implements gob.GobEncoder

func (*Messages) Len

func (m *Messages) Len() int

type Name

type Name string

func (*Name) Clone

func (n *Name) Clone(*Object) Component

func (*Name) String

func (n *Name) String() string

type Object

type Object struct {
	// contains filtered or unexported fields
}

Object represents a person, place, or thing in a State.

func (*Object) Component

func (o *Object) Component(t reflect.Type) Component

Component returns the Component of the given type if one exists in this Object.

func (*Object) ComponentAny

func (o *Object) ComponentAny(t reflect.Type) Component

ComponentAny returns the Component of the given type if one exists in this Object. If o.Parent is non-nil, ComponentAny will try to return the parent's component, recursively. The returned Component should not be modified.

func (*Object) Create

func (o *Object) Create(factories ...ComponentFactory) (ObjectIndex, *Object)

Create is the same as State.Create but the Object derives from o.

func (*Object) GobDecode

func (o *Object) GobDecode(data []byte) (err error)

GobDecode implements gob.GobDecoder

func (*Object) GobEncode

func (o *Object) GobEncode() (data []byte, err error)

GobEncode implements gob.GobEncoder

func (*Object) ID

func (o *Object) ID() ObjectIndex

ID returns the ID of this Object.

func (*Object) Modified

func (o *Object) Modified()

Modified notifies o that one of its Components has been modified. This is required for State.Atomic to function properly.

func (*Object) Parent

func (o *Object) Parent() *Object

ID returns the parent of this Object if it has one.

func (*Object) State

func (o *Object) State() *State

State returns the State this Object exists within.

type ObjectIndex

type ObjectIndex uint64

ObjectIndex is the ID of an Object in a State.

type Resources

type Resources struct {
	// contains filtered or unexported fields
}

func (*Resources) Clone

func (r *Resources) Clone(o *Object) Component

func (*Resources) Get

func (r *Resources) Get(id string) int64

func (*Resources) GobDecode

func (r *Resources) GobDecode(data []byte) (err error)

GobDecode implements gob.GobDecoder

func (*Resources) GobEncode

func (r *Resources) GobEncode() (data []byte, err error)

GobEncode implements gob.GobEncoder

func (*Resources) Set

func (r *Resources) Set(id string, v int64)

type State

type State struct {
	// contains filtered or unexported fields
}

State represents a set of Object that can be modified concurrently using compare-and-set.

func NewState

func NewState() *State

NewState initializes an empty State.

func (*State) Atomic

func (s *State) Atomic(f func(*State) bool) bool

Atomic calls f and tries to apply its changes. This is the only way a State should be modified. f may be called multiple times if other calls to Atomic are being processed at the same time. Returning false from f causes Atomic to return false without applying the changes.

This does not currently work recursively, but it may in the future.

func (*State) ByComponent

func (s *State) ByComponent(t reflect.Type) []ObjectIndex

ByComponent returns a sorted set of IDs of objects that have the given component type.

func (*State) Create

func (s *State) Create(factories ...ComponentFactory) (id ObjectIndex, o *Object)

Create initializes a new Object and returns it and its ObjectIndex. The factories must not be duplicate and must be pre-registered. The id is unique for all Objects in this State heirarchy.

func (*State) Delete

func (s *State) Delete(id ObjectIndex)

Delete removes an object from the State. Future calls to Get will return nil. If the object is referenced anywhere, Bad Things™ will happen.

func (*State) Get

func (s *State) Get(id ObjectIndex) *Object

Get returns the Object identified by id. The object is specific to this State.

func (*State) GobDecode

func (s *State) GobDecode(data []byte) (err error)

GobDecode implements gob.GobDecoder

func (*State) GobEncode

func (s *State) GobEncode() (data []byte, err error)

GobEncode implements gob.GobEncoder

func (*State) IDs

func (s *State) IDs() []ObjectIndex

IDs returns the set of ObjectIndex accessible from s in ascending order.

Directories

Path Synopsis
Package gui provides a text/graphical rendering system for tile-based games.
Package gui provides a text/graphical rendering system for tile-based games.
Package history provides a seekable sequence of rpg.State.
Package history provides a seekable sequence of rpg.State.
res

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL