rt

package
v0.24.8 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package rt defines the runtime interfaces used to interact with the game world. We use interfaces rather than concrete implementations to decouple package dependencies.

Index

Constants

This section is empty.

Variables

View Source
var (
	True    = BoolOf(true)
	False   = BoolOf(false)
	Zero    = FloatOf(0.0)
	Nothing = StringOf("")
)

Functions

func IsNilRecord

func IsNilRecord(e error) bool

func IsUnknown

func IsUnknown(e error) bool

func UnknownField

func UnknownField(target, field string) error

func UnknownName

func UnknownName(name string) error

func UnknownObject

func UnknownObject(o string) error

func UnknownResponse

func UnknownResponse(v string) error

func UnknownVariable

func UnknownVariable(v string) error

Types

type Address added in v0.24.6

type Address interface {
	GetReference(Runtime) (Reference, error)
}

uniform access to objects and variables.

type Aspect

type Aspect struct {
	Name   string // matches the field name
	Traits []string
}

the traits of any aspects behave like separate boolean fields; ex. if the list of fields contains a "colour" aspect with traits "red", "blue", "green" then the returned kind will respond to "colour", "red", "blue", and "green".

func MakeAspects

func MakeAspects(ks Kinds, fields []Field) (ret []Aspect)

given a set of fields which may contain aspects generate a list of traits. relies on the passed kinds database having types which inherit from kindOf.Aspect the fields of those types are the traits.

type Assignment

type Assignment interface {
	GetAssignedValue(Runtime) (Value, error)
}

Assignment accesses values in a generic way.

func AssignValue added in v0.24.7

func AssignValue(v Value) Assignment

wrap an existing value as an assigned value

type BoolEval

type BoolEval interface {
	GetBool(Runtime) (Value, error)
}

BoolEval represents the result of some true or false expression.

type Cursor added in v0.24.6

type Cursor interface {
	CurrentValue() Value
	SetAtIndex(int, Value) error
	GetAtIndex(int) (Cursor, error)
	SetAtField(string, Value) error
	GetAtField(string) (Cursor, error)
}

a value within an object, record, or list.

type Dotted added in v0.24.6

type Dotted interface {
	// read a value from the contents of the passed target.
	Peek(target Cursor) (Cursor, error)
	// write into the contents of the passed target.
	Poke(target Cursor, newValue Value) error
}

a "pointer" into the contents of some as yet unspecified target: for example, a field for use with objects or records, or an index for use in lists.

type Execute

type Execute interface {
	Execute(Runtime) error
}

Execute runs a bit of code that has no return value.

type Field

type Field struct {
	Name     string          // name of the field, unique within the kind
	Affinity affine.Affinity // one of the built in data types
	Type     string          `json:",omitempty"` // subtype; ex. kind for text types
	Init     Assignment      `json:",omitempty"` // default initialization
}

member of a kind.

type Jump

type Jump int
const (
	// transitions immediately.
	JumpNow Jump = iota
	// transitions after processing the current rule set.
	JumpNext
	// transition only after processing the entire phase.
	JumpLater
)

func (Jump) String

func (i Jump) String() string

type Kind

type Kind struct {
	// indicates ancestry, the name of this kind at the start, parent kinds to the right.
	// can be nil if anonymous
	Path []string
	// holds the accumulated fields of all ancestors.
	// parent fields to the *left*, more derived fields to the right. ( the reverse of path. )
	// descendant kinds can have different initial assignments for the same named field.
	Fields []Field `json:",omitempty"`
	// a subset of fields for those containing traits
	// see MakeAspects.
	Aspects []Aspect `json:",omitempty"`
	// contains filtered or unexported fields
}

used for tapestry objects and records a kind is a collection of fields with optional inheritance.

func (*Kind) Ancestors

func (k *Kind) Ancestors() []string

ancestors of this kind, the name of this kind at the head of the list. the slice isn't a copy; it should be treated as read-only. nil for anonymous kinds.

func (*Kind) Aspect

func (k *Kind) Aspect(i int) (ret Aspect)

func (*Kind) AspectIndex

func (k *Kind) AspectIndex(aspect string) (ret int)

func (*Kind) Field

func (k *Kind) Field(i int) (ret Field)

return a description of an indexed field. panics if out of range

func (*Kind) FieldCount added in v0.24.6

func (k *Kind) FieldCount() int

number of fields contained by this ( and all parent kinds )

func (*Kind) FieldIndex

func (k *Kind) FieldIndex(n string) (ret int)

returns the index of the matching field; for traits, that's the index of its aspect. returns -1 if no matching field was found.

func (*Kind) FindAspectByTrait

func (k *Kind) FindAspectByTrait(trait string) int

return the index of the aspect containing the passed trait

func (*Kind) Implements

func (k *Kind) Implements(name string) bool

true if the kind has the named ancestor. ( this is a shortcut for testing the passed name in Path() )

func (*Kind) Name

func (k *Kind) Name() (ret string)

semi unique identifier for this kind returns the empty string for anonymous kinds

type Kinds

type Kinds interface {
	GetKindByName(n string) (*Kind, error)
}

Type database

type NilRecord

type NilRecord struct {
	Class string // name of the record type that the field wants
	Field int    // index of the field in its parent record
}

returned when trying to generate the zero value of a field containing a record record fields are nil by default so that types are able to refer to themselves in go, types can use pointers for self references ( ex. struct LinkedList { Link *LinkedList } )

func (NilRecord) Error

func (e NilRecord) Error() string

func (NilRecord) NoPanic

func (e NilRecord) NoPanic()

tell package errutil this is a non-critical error.

type Notifier added in v0.24.7

type Notifier struct {
	StartedScene    func(domains []string)
	EndedScene      func(domains []string)
	ChangedState    func(noun, aspect, oldState, newState string)
	ChangedRelative func(a, b, rel string)
}

Callbacks to listen for system level changes

type NumEval added in v0.24.6

type NumEval interface {
	GetNum(Runtime) (Value, error)
}

NumEval represents the result of some numeric expression.

type NumListEval

type NumListEval interface {
	GetNumList(Runtime) (Value, error)
}

NumListEval represents the computation of a series of numeric values.

type PanicValue

type PanicValue struct{}

PanicValue is a PanicValue where every method panics except type and affinity.

func (PanicValue) Affinity

func (n PanicValue) Affinity() affine.Affinity

Affinity returns a blank string.

func (PanicValue) Any

func (n PanicValue) Any() any

func (PanicValue) Appends

func (n PanicValue) Appends(Value) error

Append panics

func (PanicValue) Bool

func (n PanicValue) Bool() bool

Bool panics

func (PanicValue) FieldByName

func (n PanicValue) FieldByName(string) (Value, error)

FieldByName panics

func (PanicValue) Float

func (n PanicValue) Float() float64

Float panics

func (PanicValue) Floats

func (Floats PanicValue) Floats() []float64

Floats panics

func (PanicValue) Index

func (n PanicValue) Index(int) Value

Index panics

func (PanicValue) Int

func (n PanicValue) Int() int

Int panics

func (PanicValue) Len

func (n PanicValue) Len() int

Len panics

func (PanicValue) Record

func (n PanicValue) Record() *Record

Record panics

func (PanicValue) Records

func (n PanicValue) Records() []*Record

Records panics

func (PanicValue) SetFieldByName

func (n PanicValue) SetFieldByName(string, Value) error

SetFieldByName panics

func (PanicValue) SetIndex

func (n PanicValue) SetIndex(int, Value) error

SetIndex panics

func (PanicValue) Slice

func (n PanicValue) Slice(i, j int) (Value, error)

Slice panics

func (PanicValue) Splice

func (n PanicValue) Splice(start, end int, add Value, cutList *Value) error

Splice panics

func (PanicValue) String

func (n PanicValue) String() string

String panics

func (PanicValue) Strings

func (n PanicValue) Strings() []string

Strings panics

func (PanicValue) Type

func (n PanicValue) Type() string

Type returns a blank string.

type Record

type Record struct {
	*Kind
	// contains filtered or unexported fields
}

Record - provides access to named field/values pairs. The fields of a record are defined by its kind. Fields that are themselves records ( ie. sub-records ) start uninitialized. Trying to Get() an uninitialized record will result in a "NilRecord" error. safe.EnsureField() can use the Kinds database to create sub-records on demand.

func NewRecord

func NewRecord(k *Kind) *Record

func (*Record) GetIndexedField

func (d *Record) GetIndexedField(i int) (ret Value, err error)

GetIndexedField panics if out of range. note: traits are never indexed fields ( although their aspect is ) Can return a NilRecord error for uninitialized record fields.

func (*Record) GetNamedField

func (d *Record) GetNamedField(field string) (ret Value, err error)

GetNamedField picks a value or trait from this record. Can return a NilRecord error for uninitialized record fields.

func (*Record) HasValue

func (d *Record) HasValue(i int) (okay bool)

limited change detection ( a hack, basically, for determining whether patterns have written results )

func (*Record) SetIndexedField

func (d *Record) SetIndexedField(i int, val Value) (err error)

SetIndexedField - note this doesn't handle trait translation. Unlike the Value interface, this doesn't panic and it doesn't copy values. As a special case, passing a nil value will reset the field to its default.

func (*Record) SetNamedField

func (d *Record) SetNamedField(field string, val Value) (err error)

SetNamedField - pokes the passed value into the record. Unlike the Value interface, this doesn't panic and it doesn't copy values.

type RecordEval

type RecordEval interface {
	GetRecord(Runtime) (Value, error)
}

RecordEval yields access to a set of fields and their values.

type RecordListEval

type RecordListEval interface {
	GetRecordList(Runtime) (Value, error)
}

RecordListEval represents the computation of a series of a set of fields.

type Reference added in v0.24.6

type Reference interface {
	// set the value at the current cursor.
	SetValue(newValue Value) (err error)
	// read the value of the current cursor.
	GetValue() (ret Value, err error)
	// peeks into the current cursor at the requested field or index.
	Dot(next Dotted) (ret Reference, err error)
}

a value that might contain other values.

type Rule

type Rule struct {
	Name string
	// controls the style of transition:
	// when true, stops processing of any other rules; otherwise moves to the next phase.
	// a stop before the main rule is considered a canceled action;
	// and the result of the action in that case is false.
	Stop bool
	// controls when a transitions happens
	Jump Jump
	// whether the rule needs to be updated
	// before jumping to the next phase ( ex. for counters )
	Updates bool
	// a chain of if-else statements is considered an "optional" rule;
	// otherwise its considered a mandatory rule.
	// mandatory rules block other rules from running ( based on stop/stop )
	// optional rules only block other rules if some part of the if chain tested true.
	Exe []Execute
}

Rule triggers a named series of statements when its filters and phase are satisfied. stopping before the action happens is considered a cancel.

type Runtime

type Runtime interface {
	// ActivateDomain - objects are grouped into potentially hierarchical "domains".
	// de/activating makes those groups hidden/visible to the runtime.
	// Domain hierarchy is defined at assembly time.
	ActivateDomain(name string) error
	// GetKindByName - type description
	GetKindByName(name string) (*Kind, error)
	// Call - run the pattern defined by the passed record.
	// passes the expected return because patterns can be called in ambiguous context ( ex. template expressions )
	Call(name string, expectedReturn affine.Affinity, keys []string, vals []Value) (Value, error)
	// RelativesOf - returns a list, even for one-to-one relationships.
	RelativesOf(a, relation string) (Value, error)
	// ReciprocalsOf - returns a list, even for one-to-one relationships.
	ReciprocalsOf(b, relation string) (Value, error)
	// RelateTo - establish a new relation between nouns and and b.
	RelateTo(a, b, relation string) error
	// PushScope - modifies the behavior of Get/SetField meta.Variable
	// by adding a set of variables to the current namespace.
	// ex. loops add iterators
	PushScope(Scope)
	// PopScope - remove the most recently added set of variables from the internal stack.
	PopScope()
	// GetField - various runtime objects (ex. nouns, kinds, etc.) store data addressed by name.
	// the objects and their fields depend on implementation and context.
	// see package meta for a variety of common objects.
	GetField(object, field string) (Value, error)
	// SetField - store, or at least attempt to store, a *copy* of the value into the field of the named object.
	// it can return an error:
	// if the value is not of a compatible type,
	// if the field is considered to read-only,
	// or if there is no object or field of the indicated names.
	SetField(object, field string, value Value) error
	// PluralOf - turn single words into their plural variants, and vice-versa.
	// each plural word maps to a unique singular.
	// for example, if the singular of "people" is "person", it cant also be "personage".
	PluralOf(single string) string
	// note: one plural word can map to multiple single words.
	// in that case the returned singular word is arbitrary ( if theoretically consistent )
	// for example, "person" can have the plural "people" or "persons" and this could return either.
	SingularOf(plural string) string
	// Random - return a pseudo-random number.
	Random(inclusiveMin, exclusiveMax int) int
	// Writer which prints text to the player.
	// It's tempting to have this be Write() -- so callers can print directly to the runtime.
	// That doesn't work in practice because go interfaces aren't virtual functions.
	// Any runtime wrapper can get stripped off, and wind up writing to an unexpected implementation.
	Writer() io.Writer
	// SetWriter - Override the current writer.
	SetWriter(io.Writer) (prev io.Writer)
}

Runtime environment for an in-progress game.

type Scope

type Scope interface {
	// return g.Unknown if the named field/variable doesn't exist.
	FieldByName(field string) (Value, error)
	// set without copying
	SetFieldByName(field string, val Value) error
}

Scope - establishes a pool of local variables. Scopes are usually accessed via the runtime Set/GetField.

type Signal added in v0.24.8

type Signal int

error code for system commands used by play/step to switch take some action as a side-effect a command

const (
	SignalUnknown Signal = iota
	SignalQuit
	SignalSave
	SignalLoad
	SignalBreak
	SignalContinue
)

func (Signal) Error added in v0.24.8

func (s Signal) Error() string

func (Signal) String added in v0.24.8

func (i Signal) String() string

type TextEval

type TextEval interface {
	GetText(Runtime) (Value, error)
}

TextEval represents the result of some expression which creates a string.

type TextListEval

type TextListEval interface {
	GetTextList(Runtime) (Value, error)
}

TextListEval represents the computation of a series of strings.

type Unknown

type Unknown struct {
	Target, Field string
}

error for GetField, SetField

func (Unknown) Error

func (e Unknown) Error() (ret string)

func (Unknown) IsUnknownField

func (e Unknown) IsUnknownField() bool

type Value

type Value interface {
	// identifies how the passed value is represented internally.
	// ex. a number can be represented as float or as an int,
	// a record might be one of several different kinds,
	// text might represent an object id of a specific kind, an aspect, trait, or other value.
	Type() string
	// identifies the general category of the value.
	Affinity() affine.Affinity
	// return this value as a bool, or panic if the value isn't a bool.
	Bool() bool
	// return this value as a float, or panic if the value isn't a number.
	Float() float64
	// return this value as an int, or panic if the value isn't a number.
	Int() int
	// return this value as a string; it doesn't panic.
	// for non-string values, similar to package reflect, it returns a string of the form "<type value>".
	String() string
	// return this value as a record, or panic if the value isn't a record.
	Record() *Record
	// return this value as a slice of floats, or panic if this isn't a float slice.
	// note: while primitive values support both ints and floats, slices can only be floats.
	Floats() []float64
	// return this value as a slice of strings, or panic if this isn't a string slice.
	Strings() []string
	// return this value as a slice of records, or panic if not a record slice.
	// note: every value in the returned slice is expected to be record of this value's Type().
	Records() []*Record
	// return a value representing a field inside this record.
	// if the field holds a record or a slice, the returned value shares its memory with the named field.
	// errors if note a record, or the field doesn't exist.
	FieldByName(string) (Value, error)
	// writes a *copy* of the passed value into a record.
	// errors if not a record, the field doesn't exist, or if its affinity cant support the passed value.
	SetFieldByName(string, Value) error
	// the number of elements in the value.
	// panics if this cant have a length: isnt a slice or a string.
	Len() int
	// return the nth element of this value, where 0 is the first value.
	// panics if this isn't a slice.
	Index(int) Value
	// writes a *copy* of the passed value into a slice
	// panics if this isn't a slice, if the index is out of range, or if the affinities are mismatched.
	// errors if the types are mismatched
	SetIndex(int, Value) error
	// adds a *copy* of a value, or a copy of a slice of values, to the end of this slice.
	// panics if this value isn't an appropriate kind of slice; errors on subtype.
	// In golang, this is a package level function, presumably to mirror the built-in append()
	Appends(Value) error
	// return a *copy* of this slice and its values containing the first index up to (and not including) the second index.
	// panics if this value isn't a slice.
	Slice(i, j int) (Value, error)
	// cut elements out of this slice from start to end,
	// and add copies of the passed additional element(s) at the start of the cut point.
	// returns the cut element if cutList if non-nil.
	// errors if the start and end indices are bad.
	// panics if this value isn't a slice, or if additional element(s) are of an incompatible affinity.
	// 'add' can be a single value, a list of values, or nil.
	Splice(start, end int, add Value, cutList *Value) error
}

Value represents any one of Tapestry's built in types.

func BoolFrom

func BoolFrom(v bool, subtype string) Value

func BoolOf

func BoolOf(v bool) Value

func CopyValue

func CopyValue(val Value) (ret Value)

CopyValue: create a new value from a snapshot of the passed value panics on error because it assumes all values should be copyable

func FloatFrom

func FloatFrom(v float64, subtype string) Value

func FloatOf

func FloatOf(v float64) Value

func FloatsFrom

func FloatsFrom(vs []float64, subtype string) (ret Value)

func FloatsOf

func FloatsOf(vs []float64) Value

func IntFrom

func IntFrom(v int, subtype string) Value

func IntOf

func IntOf(v int) Value

func RecordOf

func RecordOf(v *Record) Value

func RecordsFrom

func RecordsFrom(vs []*Record, subtype string) (ret Value)

func StringFrom

func StringFrom(v string, subtype string) Value

func StringOf

func StringOf(v string) Value

func StringsFrom

func StringsFrom(vs []string, subtype string) (ret Value)

func StringsOf

func StringsOf(vs []string) Value

func ZeroField

func ZeroField(aff affine.Affinity, cls string, idx int) (ret Value, err error)

ZeroField generates a zero value for the specified data ( a field ) asking for a zero record returns a NilRecord error.

func ZeroValue

func ZeroValue(aff affine.Affinity) (ret Value, err error)

ZeroValue generates a zero value for the specified affinity; asking for a zero record returns an error.

Directories

Path Synopsis
package kindsOf defines a handful of built-in base types for tapestry kinds.
package kindsOf defines a handful of built-in base types for tapestry kinds.
Package meta provides string constants for rt.Runtime Get/SetField.
Package meta provides string constants for rt.Runtime Get/SetField.
Package pattern provides tools for creating and executing functions with guards.
Package pattern provides tools for creating and executing functions with guards.
Package print provides low level routines for styling output.
Package print provides low level routines for styling output.

Jump to

Keyboard shortcuts

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