Documentation
¶
Overview ¶
The attr (attributes) package implements all of the functionality for objects in a WolfMUD world. All objects are instances of the Thing type. To these instances various attributes are added depending on the functionality required. The functionality of an object may be changed at runtime by adding or removing attributes.
A Thing can be searched for specific Attribute types using finders. All finders return a typed nil in the event of the specific Attribute type not being found. Returning a typed nil such as (*Alias)(nil) allows the finder to be chained to any other methods taking the a pointer to the Attribute type as a receiver. For example:
if attr.FindAlias(t).HasAlias("test") {
// do something...
}
If you need to check specifically if the finder returns nil, compare the returned value to the typed nil:
if a := attr.FindAlias(t); a == (*Alias)(nil) {
// do something...
}
Or if available use the Found method:
if !attr.FindAlias(t).Found() {
// do something...
}
All methods that take a pointer to an Attribute as a receiver are expected to be able to handle a nil receiver unless otherwise stated.
All typed nils should be of the same type as the default implementation for the type. For example the has.Alias interface has attr.Alias as the default implementation, therefore the typed nil should also be of type *Alias.
While it may seem unusual to return a typed nil it simplifies and reduces a lot of code in WolfMUD.
Index ¶
- Constants
- func DumpFmt(format string, args ...interface{}) string
- func FindAlias(t has.Thing) has.Alias
- func FindAllDescription(t has.Thing) (matches []has.Description)
- func FindCleanup(t has.Thing) has.Cleanup
- func FindDoor(t has.Thing) has.Door
- func FindExits(t has.Thing) has.Exits
- func FindInventory(t has.Thing) has.Inventory
- func FindLocate(t has.Thing) has.Locate
- func FindName(t has.Thing) has.Name
- func FindNarrative(t has.Thing) has.Narrative
- func FindPlayer(t has.Thing) has.Player
- func FindReset(t has.Thing) has.Reset
- func FindStart(t has.Thing) has.Start
- func FindVetoes(t has.Thing) has.Vetoes
- func FindWriting(t has.Thing) has.Writing
- func Return(direction byte) byte
- type Alias
- type Attribute
- type Cleanup
- type Description
- type Door
- func (d *Door) Check(cmd ...string) has.Veto
- func (d *Door) Close()
- func (d *Door) Closed() bool
- func (d *Door) Copy() has.Attribute
- func (d *Door) Description() string
- func (d *Door) Direction() byte
- func (d *Door) Dump() (buff []string)
- func (n *Door) Found() bool
- func (d *Door) Free()
- func (d *Door) Open()
- func (d *Door) Opened() bool
- func (d *Door) OtherSide()
- func (*Door) Unmarshal(data []byte) has.Attribute
- type Exits
- func (e *Exits) AutoLink(direction byte, to has.Inventory)
- func (e *Exits) AutoUnlink(direction byte)
- func (e *Exits) Copy() has.Attribute
- func (e *Exits) Dump() []string
- func (e *Exits) Found() bool
- func (e *Exits) Free()
- func (e *Exits) LeadsTo(direction byte) has.Inventory
- func (e *Exits) Link(direction byte, to has.Inventory)
- func (e *Exits) List() string
- func (*Exits) NormalizeDirection(name string) (direction byte, err error)
- func (e *Exits) Surrounding() []has.Inventory
- func (*Exits) ToName(direction byte) (name string)
- func (e *Exits) Unlink(direction byte)
- func (*Exits) Unmarshal(data []byte) has.Attribute
- func (e *Exits) Within(moves int) (locations [][]has.Inventory)
- type Inventory
- func (i *Inventory) Add(t has.Thing) has.Thing
- func (i *Inventory) Carried() bool
- func (i *Inventory) Contents() []has.Thing
- func (i *Inventory) Copy() has.Attribute
- func (i *Inventory) Crowded() (crowded bool)
- func (i *Inventory) Dump() (buff []string)
- func (i *Inventory) Empty() bool
- func (i *Inventory) Found() bool
- func (i *Inventory) Free()
- func (i *Inventory) List() string
- func (i *Inventory) Move(t has.Thing, to has.Inventory) has.Thing
- func (i *Inventory) Narratives() []has.Thing
- func (i *Inventory) Remove(t has.Thing) has.Thing
- func (i *Inventory) Search(alias string) has.Thing
- func (*Inventory) Unmarshal(data []byte) has.Attribute
- type Locate
- func (l *Locate) Copy() has.Attribute
- func (l *Locate) Dump() []string
- func (l *Locate) Found() bool
- func (l *Locate) Free()
- func (l *Locate) Origin() (origin has.Inventory)
- func (l *Locate) SetOrigin(i has.Inventory)
- func (l *Locate) SetWhere(i has.Inventory)
- func (*Locate) Unmarshal(data []byte) has.Attribute
- func (l *Locate) Where() (where has.Inventory)
- type Name
- type Narrative
- type Player
- func (p *Player) Copy() has.Attribute
- func (p *Player) Dump() []string
- func (p *Player) Found() bool
- func (p *Player) Free()
- func (p *Player) SetPromptStyle(new has.PromptStyle) (old has.PromptStyle)
- func (*Player) Unmarshal(data []byte) has.Attribute
- func (p *Player) Write(b []byte) (n int, err error)
- type Reset
- type Start
- type Thing
- func (t *Thing) Add(a ...has.Attribute)
- func (t *Thing) Attrs() []has.Attribute
- func (t *Thing) Copy() has.Thing
- func (t *Thing) Dispose()
- func (t *Thing) Dump() (buff []string)
- func (t *Thing) Free()
- func (t *Thing) Remove(a ...has.Attribute)
- func (t *Thing) SetOrigins()
- func (t *Thing) UID() string
- func (t *Thing) Unmarshal(recno int, record recordjar.Record)
- type Veto
- type Vetoes
- type Writing
- Bugs
Constants ¶
const ( North byte = iota Northeast East Southeast South Southwest West Northwest Up Down )
Constants for direction indexes. These can be used for the Link, AutoLink, Unlink and AutoUnlink methods. If these constants are modified probably need to update the Return function as well.
Variables ¶
This section is empty.
Functions ¶
func FindAlias ¶
FindAlias searches the attributes of the specified Thing for attributes that implement has.Alias returning the first match it finds or a *Alias typed nil otherwise.
func FindAllDescription ¶
func FindAllDescription(t has.Thing) (matches []has.Description)
FindAllDescription searches the attributes of the specified Thing for attributes that implement has.Description returning all that match. If no matches are found an empty slice will be returned.
func FindCleanup ¶ added in v0.0.5
FindCleanup searches the attributes of the specified Thing for attributes that implement has.Cleanup returning the first match it finds or a *Cleanup typed nil otherwise.
func FindDoor ¶ added in v0.0.4
FindDoor searches the attributes of the specified Thing for attributes that implement has.Door returning the first match it finds or a *Door typed nil otherwise.
func FindExits ¶
FindExits searches the attributes of the specified Thing for attributes that implement has.Exits returning the first match it finds or a *Exits typed nil otherwise.
func FindInventory ¶
FindInventory searches the attributes of the specified Thing for attributes that implement has.Inventory returning the first match it finds or a *Inventory typed nil otherwise.
func FindLocate ¶
FindLocate searches the attributes of the specified Thing for attributes that implement has.Locate returning the first match it finds or a *Locate typed nil otherwise.
func FindName ¶
FindName searches the attributes of the specified Thing for attributes that implement has.Name returning the first match it finds or a *Name typed nil otherwise.
func FindNarrative ¶
FindNarrative searches the attributes of the specified Thing for attributes that implement has.Narrative returning the first match it finds or a *Narrative typed nil otherwise.
func FindPlayer ¶
FindPlayer searches the attributes of the specified Thing for attributes that implement has.Player returning the first match it finds or a *Player typed nil otherwise.
func FindReset ¶ added in v0.0.5
FindReset searches the attributes of the specified Thing for attributes that implement has.Reset returning the first match it finds or a *Reset typed nil otherwise.
func FindStart ¶
FindStart searches the attributes of the specified Thing for attributes that implement has.Start returning the first match it finds or a *Start typed nil otherwise.
func FindVetoes ¶
FindVetoes searches the attributes of the specified Thing for attributes that implement has.Vetoes returning the first match it finds or a *Vetoes typed nil otherwise.
func FindWriting ¶
FindWriting searches the attributes of the specified Thing for attributes that implement has.Writing returning the first match it finds or a *Writing typed nil otherwise.
func Return ¶
Return calculates the opposite/return direction for the direction given. This is handy for calculating things like normal exits where if you go north you return by going back south. It is also useful for implementing ranged weapons, thrown weapons and spells. For example if you fire a bow west the person will see the arrow come from the east (from their perspective).
Types ¶
type Alias ¶
type Alias struct {
Attribute
// contains filtered or unexported fields
}
Alias implements an attribute for referring to a Thing. An alias is a single word used to refer to things. Things may have more than one alias. For example a sword may have the aliases 'SWORD' and 'SHORTSWORD'. Given these aliases a player may use commands such as:
GET SWORD EXAMINE SHORTSWORD DROP SHORTSWORD
Every Alias attribute that has a parent Thing set will have a unique ID equal to the result of calling Alias.Parent().UID(). Therefore a specific, unique Thing can be found by unique ID using, for example, Alias.HasAlias(Thing.UID()) or Inventory.Search(Thing.UID()).
NOTE: It is important to switch to the unique alias whenever possible, especially when scripting, so that the correct Thing is used for commands. This avoids picking the wrong Thing when a given alias identifies multiple Things. For example if we have a respawnable runestone and we get and drop the runestone it will be registered for cleanup. However if we just use the alias 'RUNESTONE' either the dropped or respawned runestone could be cleaned up. If the respawned runestone is cleaned up we could end up in a loop respawning and cleaning up the wrong runestone. Using the unique alias of the dropped runestone avoids this situation.
TODO: Need to implement alias prefixes. This would allow us to distinguish between two similar items with the same alias. For example if there are two coins, one copper and one silver, we could use either "GET COPPER COIN" or "GET SILVER COIN". If there is only one coin then "GET COIN" would be sufficient. See also BUG about aliases being single words.
func NewAlias ¶
NewAlias returns a new Alias attribute initialised with the specified aliases. The specified aliases are automatically uppercased when stored. A unique alias using the parent Thing.UID will be added automatically.
func (*Alias) Aliases ¶
Aliases returns a []string of all the aliases for an Alias attribute. If there are no aliases an empty slice will be returned.
func (*Alias) Copy ¶ added in v0.0.4
Copy returns a copy of the Alias receiver.
func (*Alias) Found ¶
Found returns false if the receiver is nil otherwise true.
func (*Alias) HasAlias ¶
HasAlias checks the passed string for a matching alias. Returns true if a match is found otherwise false.
func (*Alias) SetParent ¶ added in v0.0.5
SetParent overrides the default Attribute.SetParent in order to set a unique alias based on the parent Thing unique ID. The alias will be equal to the value returned by calling Alias.Parent().UID(). When the parent for the attribute changes the old unique identifier is removed (if there is one) and the new unique alias added before setting the new parent.
type Attribute ¶
type Attribute struct {
// contains filtered or unexported fields
}
Attribute implements a stub for other attributes. Any types providing attributes can embed this type instead of implementing their own Parent and SetParent methods.
NOTE: Attribute does NOT provide a default Copy implementation. Each attribute must implement its own Copy method. This is due to the fact that other attributes will know best how to create copies based on their own implementation.
func (*Attribute) Free ¶ added in v0.0.5
func (a *Attribute) Free()
Free makes sure references are nil'ed when the Attribute is freed. Other attributes should override Free to release their own references and resources. Attributes that implement their own Free method should also call Attribute.Free.
func (*Attribute) Marshal ¶
FOR DEVELOPMENT ONLY SO WE DON'T HAVE TO IMPLEMENT Marshal ON ALL THE ATTRIBUTES AT ONCE. REMOVE AS SOON AS ALL ATTRIBUTES UPDATED.
func (*Attribute) Parent ¶
Parent returns the Thing that the Attribute has been added to.
type Cleanup ¶ added in v0.0.5
Cleanup implements an Attribute for disposing of Things left laying around in the game world. When an item is dropped it will be cleaned up after a delay period has elapsed. Otherwise the world will get cluttered with items. The delay period is between Cleanup.after and Cleanup.after+Cleanup.jitter. If a Thing is being cleaned up and is in its delay period the Cleanup.Cancel channel will be non-nil and the clean up may be aborted by closing the channel or by calling Cleanup.Abort which will cancel clean up requests recursively for a Thing.
SPECIFICS ¶
If an item is put into any Inventory and the item ends up not being carried by a player - either in their Inventory or in a container in their Inventory - and the receiving inventory has no parent Inventory that are already scheduled for clean up then the item is scheduled for a clean up. If the item has an Inventory (a container) the contents do not need to be scheduled for clean up recursively as everything will be cleaned up when the item itself is cleaned up. This is also why we don't schedule a clean up when putting an item in an Inventory where a parent Inventory is scheduled for a clean up already.
If an item is removed from any Inventory any pending clean ups are cancelled. If the item has an Inventory its content - checked recursively - will have any pending clean ups cancelled. If we don't cancel pending clean ups recursively then putting an item into a container and then picking the container up would result in the item still being scheduled for a clean up resulting in the item disappearing from the container.
func NewCleanup ¶ added in v0.0.5
NewCleanup returns a new Cleanup attribute initialised with the passed after and jitter durations. The after and jitter Duration set the delay period to between after and after+jitter for when a Thing is cleaned up after being dropped.
func (*Cleanup) Abort ¶ added in v0.0.5
func (c *Cleanup) Abort()
Abort causes an outstanding clean up event to be cancelled for the parent Thing. If the Thing has an Inventory Abort is called the content recursively. If we don't do this putting an item into a container and then picking the container up would result in the item still being scheduled for a clean up and disappearing from the container.
func (*Cleanup) Active ¶ added in v0.0.5
Active returns true if any of the Inventories the parent Thing is in already have a clean up scheduled, otherwise false.
func (*Cleanup) Cleanup ¶ added in v0.0.5
func (c *Cleanup) Cleanup()
Cleanup schedules a clean up of the parent Thing. If there is already a clean up event pending it will be cancelled and a new one queued.
func (*Cleanup) Copy ¶ added in v0.0.5
Copy returns a copy of the Cleanup receiver. The copy will not inherit any pending clean up events.
func (*Cleanup) Found ¶ added in v0.0.5
Found returns false if the receiver is nil otherwise true.
type Description ¶
type Description struct {
Attribute
// contains filtered or unexported fields
}
Description implements an attribute for describing Things. Things can have multiple descriptions or other attributes that implement the has.Description interface to add additional information to descriptions.
func NewDescription ¶
func NewDescription(description string) *Description
NewDescription returns a new Description attribute initialised with the specified description.
func (*Description) Copy ¶ added in v0.0.4
func (d *Description) Copy() has.Attribute
Copy returns a copy of the Description receiver.
func (*Description) Description ¶
func (d *Description) Description() string
Description returns the descriptive string of the attribute.
func (*Description) Dump ¶
func (d *Description) Dump() []string
func (*Description) Found ¶
func (d *Description) Found() bool
Found returns false if the receiver is nil otherwise true.
func (*Description) Unmarshal ¶
func (*Description) Unmarshal(data []byte) has.Attribute
Unmarshal is used to turn the passed data into a new Description attribute.
type Door ¶ added in v0.0.4
type Door struct {
Attribute
// contains filtered or unexported fields
}
Door implements an attribute for blocking exits. Doors are the most common way of blocking an exit but this attribute may relate to gates, grills, bookcases and other such obstacles.
A complete working door consists of two Thing each with a Door attribute. One is the original door and the other is the 'other side'. The original Door is added to the location with the exit to be blocked, the 'other side' is added to the location the exit to be blocked leads to. Taking the tavern entrance in data/zones/zinara.wrj as an example:
_________________________________________
|L3 |L5 |
| Tavern # Between Tavern |
Entrance # & Bakery
#
(Door) # ('Other Side')
| # | # = a door
|__ __|__ __|
Here we have locations L3 (Tavern Entrance) and L5 (Between Tavern * Bakery). Between them is the Tavern door. It is defined as:
%%
Ref: L3N1
Narrative:
Name: the tavern door
Aliases: DOOR
Door: EXIT→E RESET→1m JITTER→1m
This is a sturdy wooden door with a simple latch.
%%
This adds a Thing representing the door to L3 and blocks the exit going east (EXIT→E) to L5. During zone loading and Unmarshaling OtherSide is called on the original door Thing. This creates another Thing used for the 'Other Side'. It is added to the location found by taking the exit the original door is blocking, in this case we are blocking the east exit which leads to L5. The 'Other Side' is added to L5 and is setup to block the returning exit, in this case west - back to L3. Now in L3 if we do 'EXAMINE DOOR' we are examining the original, in L5 we are examining the 'Other Side' which appears to be the same door. Because the original and 'Other Side' share state be can also issue 'OPEN DOOR' or 'CLOSE DOOR' in either L3 or L5.
When the door is not in it's initial state it will reset after a delay of between 1 and 2 minutes. That is, sometime between delay and delay+jitter.
If delay and jitter are both zero the door will not reset automatically.
NOTE: For now a Door attribute should only be added to a Thing with a Narrative attribute that is placed at a location. Adding a Door attribute to a location directly or to a moveable object will result in odd - possibly interesting - behaviour.
func NewDoor ¶ added in v0.0.4
NewDoor returns a new Door attribute. The direction is the direction the door blocks - specified as per attr.Exit constants. Open specifies whether the door is initially open (true) or closed (false). The reset is the duration to wait before resetting the door to its initial state - open or closed as specified by open. The jitter is a random amount of time to add to the reset delay. Adding jitter means the Door will reset with an actual delay of between delay and delay+jitter.
This actually only creates one side of a door. To create the 'other side' of the door Door.OtherSide should be called.
func (*Door) Check ¶ added in v0.0.4
Check will veto passing through a Door dynamically based on the command (direction) given and the current state of the Door - open or closed.
func (*Door) Close ¶ added in v0.0.4
func (d *Door) Close()
Close changes a Door state from open to closed. If there is a pending event to close the door it will be cancelled. If the door should automatically open again an event to "OPEN <door>" will be queued. If the door is already closed calling Close does nothing.
func (*Door) Closed ¶ added in v0.0.4
Closed returns true if the door is currently closed else false.
func (*Door) Copy ¶ added in v0.0.4
Copy returns a copy of the Door receiver. Copy will only copy a specific Door not an original and 'other side' pair - they have to be copied separately if required.
func (*Door) Direction ¶ added in v0.0.4
Direction returns the direction of the exit being blocked. The returned value matches the constants defined in attr.Exits.
func (*Door) Found ¶ added in v0.0.4
Found returns false if the receiver is nil otherwise true.
func (*Door) Free ¶ added in v0.0.5
func (d *Door) Free()
Free makes sure references are nil'ed and channels closed when the Door attribute is freed.
func (*Door) Open ¶ added in v0.0.4
func (d *Door) Open()
Open changes a Door state from closed to open. If there is a pending event to open the door it will be cancelled. If the door should automatically close again an event to "CLOSE <door>" will be queued. If the door is already open calling Open does nothing.
func (*Door) Opened ¶ added in v0.0.4
Opened returns true if the door is currently open else false.
func (*Door) OtherSide ¶ added in v0.0.4
func (d *Door) OtherSide()
OtherSide creates the 'other side' of a Door and places it in the World. The 'other side' will be placed in the Inventory found by following the exit that is being blocked by the original Door. Creating the 'other side' will fail if:
- The original Door attribute has not been added to a Thing
- The parent Thing of the original Door is not in an Inventory (e.g. location)
- The parent Thing of the Inventory the parent Thing of the Door is in has no Exits
- There is no exit in the direction the door is supposed to be blocking
For more details see the attr.Door type.
type Exits ¶
type Exits struct {
Attribute
// contains filtered or unexported fields
}
Exits implements an attribute describing exits for the eight compass points north, northeast, east, southeast, south, southwest, west and northwest as well as the directions up and down and where they lead to. Exits are usually in pairs, for example one north and one back south. You can have one way exits or return exits that do not lead back to where you came from.
func NewExits ¶
func NewExits() *Exits
NewExits returns a new Exits attribute with no exits set. Exits should be added to the attribute using the Link and AutoLink methods. The reason exits cannot be set during initialisation like most other attributes is that all 'locations' have to be setup before they can all be linked together.
func (*Exits) AutoLink ¶
AutoLink links the given exit, calculates the opposite return exit and links that automatically as well - as long as the parent Thing of the to Inventory has an Exits attribute.
func (*Exits) AutoUnlink ¶
AutoUnlink unlinks the given exit, calculates the opposite return exit and unlinks that automatically as well.
BUG(diddymus): Does not check that exit A links to B and B links back to A. For example a maze may have an exit going North from A to B but going South from B takes you to C instead of back to A as would be expected!
func (*Exits) Copy ¶ added in v0.0.4
Copy returns a copy of the Exits receiver.
func (*Exits) Found ¶
Found returns false if the receiver is nil otherwise true.
func (*Exits) Free ¶ added in v0.0.5
func (e *Exits) Free()
Free makes sure references are nil'ed when the Exits attribute is freed.
func (*Exits) LeadsTo ¶
LeadsTo returns the Inventory of the location found by taking a specific exit. If a particular direction leads nowhere nil will be returned.
func (*Exits) Link ¶
Link links the given exit direction to the given Inventory. If the given direction was already linked the exit will be overwritten - in effect the same as unlinking the exit first and then relinking it.
func (*Exits) List ¶
List will return a string listing the exits you can see. For example:
You can see exits east, southeast and south.
func (*Exits) NormalizeDirection ¶
NormalizeDirection takes a long or short variant of a direction name in any case and returns the direction.
So 'N', 'NORTH', 'n', 'north', 'North' and 'NoRtH' all return the constant NORTH which is 0.
If the direction name given cannot be normalized, maybe because it is invalid, a non-nil error will be returned.
func (*Exits) Surrounding ¶
Surrounding returns an Inventory slice of all locations immediatly reachable from the current location. The cuurent location is specified by the receiver. If there are no immediatly reachable locations an empty slice will be returned.
func (*Exits) ToName ¶
ToName returns the lowercased long name of a direction or an empty string if the direction is invalid.
func (*Exits) Unlink ¶
Unlink sets the exit for the given direction to nil. It does not matter if the given direction was not linked in the first place.
func (*Exits) Unmarshal ¶
Unmarshal is used to turn the passed data into a new Exits attribute.
func (*Exits) Within ¶
Within returns all of the locations within the given number of moves from the location specified by the receiver. It is 3D and will follow up and down exits as well. The locations are returned as a slice of Inventory slices. The first slice represents the cwnumber of moves. The second slice is all locations within that number of moves. The first slice, moves 0, is always the current location.
Assume the following map of the Tavern and surrounding locations:
____________________________________________________________
|1 |3 |5 |6 |
| Fireplace Entrance Between Bakery |
| Tavern/Bakery |
|__ __|__ __|______________|
|2 4 |7 |8 |
| Common Room Bar | Street outside Pawn Shop |
| | Pawn Shop |
|______________|______________|__ __|______________|
|10 |9 |
| Outside Fountain |
| Armourer Square |
|______________|_______________|
If we are at location 7 on the map then Within(2) will return all locations within 2 moves:
[][]has.Inventory{
[]has.Inventory{ 7 }, // Within 0 moves of location 7
[]has.Inventory{ 5, 8, 9 }, // Within 1 move of location 7
[]has.Inventory{ 6, 3, 10 }, // Within 2 moves of location 7
}
The above numbers e.g. 5,8,9 refer to the map locations. In reality they would actually be references to has.Inventory interface types.
See cmd/sneeze.go for an example of using the Within method.
type Inventory ¶
Inventory implements an attribute for container inventories. The most common container usage is for locations and rooms as well as actual containers like bags, boxes and inventories for mobiles. WolfMUD does not actually define a specific type for locations. Locations are simply Things that have an Exits attribute.
Any Thing added to an Inventory will automatically be assigned a Locate attribute. A locate attribute is simply a back reference to the Inventory a Thing is in. This enables a Thing to work out where it is.
NOTE: The contents slice is split into two parts. Things with a Narrative attribute are added to the beginning of the slice. All other Things are appended to the end of the slice. Which items are narrative and which are not is tracked by split:
narratives := contents[:split] other := contents[split:] countNarratives := split countOther := len(contents) - split
For a complete description of narratives see the Narrative attribute type.
TODO: A slice for contents is fine for convenience and simplicity but maybe a linked list would be better? This would possibly save reslicing in Remove.
BUG(diddymus): Inventory capacity is not implemented yet.
func NewInventory ¶
NewInventory returns a new Inventory attribute initialised with the specified Things as initial contents.
func (*Inventory) Add ¶
Add puts a Thing into an Inventory. If the Thing does not have a Locate attribute one will be added automatically, otherwise the existing Locate attribute will be updated. On success Add will return the Thing actually added to the inventory - which may not be the Thing passed in, it may be a copy. It is therefore important to use the Thing returned after calling Add. On failure Add returns nil.
func (*Inventory) Carried ¶ added in v0.0.5
Carried returns true if putting an item into the Inventory would result in it being carried by a player, otherwise false. The Inventory can be the player's actual Inventory or the Inventory of a container (checked recursively) in the player's inventory.
TODO: Need to check for players or mobiles
func (*Inventory) Contents ¶
Contents returns a 'copy' of the Inventory non-narrative contents. That is a copy of the slice containing has.Thing interface headers. Therefore the Inventory contents may be indirectly manipulated through the copy but changes to the actual slice are not possible - use the Add and Remove methods instead.
func (*Inventory) Copy ¶ added in v0.0.4
Copy returns a copy of the Inventory receiver. The copy will be made recursively copying the complete content of the Inventory as well.
NOTE: There are no checks made for cyclic references which could send us into infinite recursion. However cyclic references should be prevented by the zone loader. See zones.isParent function.
func (*Inventory) Empty ¶
Empty returns true if there are no non-Narrative items else false.
func (*Inventory) Found ¶
Found returns false if the receiver is nil otherwise true.
func (*Inventory) Free ¶ added in v0.0.5
func (i *Inventory) Free()
Free recursively calls Free on all of it's content when the Inventory attribute is freed.
func (*Inventory) List ¶
List returns a string describing the non-narrative contents of an Inventory. The layout of the dscription returned is dependant on the number of items. If the Inventory is empty and the Parent Thing has a narrative attribute we return nothing. Otherwise if the Inventory is empty we return:
It is empty.
A single item only we return:
It contains xxx.
For multiple items we return:
It contains: Item Item Item ...
If the inventory cannot be listed an empty string will be returned.
func (*Inventory) Move ¶ added in v0.0.5
Move removes a Thing from one Inventory and puts it into another Inventory. On success Move will return the Thing moved - which may not be the Thing passed in, it may be a copy. It is therefore important to use the Thing returned after calling Move. If Move fails it will return nil.
If the receiver is a *Inventory typed nil the Thing will only be added to an inventory. If the to Inventory is nil the Thing will only be removed from the reveiver Inventory. In both cases the Thing's Locate attribute will be updated or one added if missing.
func (*Inventory) Narratives ¶ added in v0.0.4
Narratives returns a 'copy' of the Inventory narrative contents. That is a copy of the slice containing has.Thing interface headers. Therefore the Inventory narratives may be indirectly manipulated through the copy but changes to the actual slice are not possible - use the Add and Remove methods instead.
func (*Inventory) Remove ¶
Remove takes a Thing from an Inventory. On success Remove will return the Thing actually removed from the inventory - which may not be the Thing passed in, it may be a copy. It is therefore important to use the Thing returned after calling Remove. If Remove fails it will return nil.
func (*Inventory) Search ¶
Search returns the first Inventory Thing that matches the alias passed. If no matches are found nil is returned.
type Locate ¶
type Locate struct {
Attribute
// contains filtered or unexported fields
}
Locate implements an attribute that refers to the Inventory of where something is. When a Thing is added to an Inventory a Locate attribute will be added automatically if the Thing does not already have one. When a Thing is added to or removed from an Inventory the Locate.SetWhere method is called to update the reference. See inventory.Add for more details. Locate also records the initial starting position or origin of a Thing.
func NewLocate ¶
NewLocate returns a new Locate attribute initialised to refer to the passed Inventory. Passing nil is a valid reference and is usually treated as being nowhere.
func (*Locate) Copy ¶ added in v0.0.4
Copy returns a copy of the Locate receiver.
func (*Locate) Found ¶
Found returns false if the receiver is nil otherwise true.
func (*Locate) Free ¶ added in v0.0.5
func (l *Locate) Free()
Free makes sure references are nil'ed when the Locate attribute is freed.
func (*Locate) Origin ¶ added in v0.0.5
Origin return the initial starting Inventory that a Thing is placed into.
func (*Locate) SetOrigin ¶ added in v0.0.5
SetOrigin is use to specify the initial starting Inventory that a Thing is placed into.
func (*Locate) SetWhere ¶
SetWhere is used to set the Inventory containing the parent Thing. Passing nil is a valid reference and is usually treated as being nowhere. The current reference can be retrieved by calling Where.
NOTE: This is called automatically by the Inventory Add and Remove methods.
func (*Locate) Unmarshal ¶
Unmarshal is used to turn the passed data into a new Locate attribute. At the moment Locate attributes are created internally so return an untyped nil so we get ignored.
type Name ¶
type Name struct {
Attribute
// contains filtered or unexported fields
}
func NewName ¶
Name implements an attribute for naming Things. It is used when referring to or listing Things. For example if there is a sword it could have the name of 'a sword'. Then manipulating it you could see the following messages:
You see a sword here. You pick up a sword. You examine a sword. You start to wield a sword.
Messages such as the examples would typically be general messages with a placeholder for the name of the Thing. For example:
You see %s here. You pick up %s. You examine %s. You start to wield %s.
It is therefore important to take this into consideration when choosing names for Things.
func (*Name) Copy ¶ added in v0.0.4
Copy returns a copy of the Name receiver.
func (*Name) Found ¶
Found returns false if the receiver is nil otherwise true.
func (*Name) Name ¶
Name returns the name stored in the attribute. If the receiver is nil or the name is an empty string the specified preset will be returned instead. This allows for a generic preset name such as someone, something or somewhere to be returned for things without names.
type Narrative ¶
type Narrative struct {
Attribute
}
Narrative implements an attribute to mark non-removable content. It allows creators to cater to the more discerning adventurer by providing content that is not spoon fed to them. Narrative content is usually mentioned or discoverable from text descriptions. For example:
You are in the corner of a common room in the Dragon's Breath tavern. There is a fire burning away merrily in an ornate fireplace giving comfort to weary travellers. Shadows flicker around the room, changing light to darkness and back again. To the south the common room extends and east the common room leads to the tavern entrance.
From such a description it would be reasonable for someone to want to example the fireplace although there would be no "You see a fireplace here." when listing the items at the location. Should someone try to examine the fireplace they are rewarded with:
This is a very ornate fireplace carved from marble. Either side a dragon curls downward until the head is below the fire looking upward, giving the impression that they are breathing fire.
While anything that can normally be put into an inventory can be put into a narrative, nothing should be directly removable. However everything in a narrative still works as expected - readable things are still readable and containers can have things put in them as well as removed. As an example consider this brief description:
You are standing next to a small fish pond. Paths lead off north, south and west deeper into the gardens.
Examining the pond - in this case a simple inventory - reveals its content:
This is a small fish pond. It contains a fish of gold.
Taking the fish from the pond and examining it reveals:
This is a small fish made from solid gold.
A much more satisfying reward for being curious :)
NOTE: At the moment narrative content should not be removeable for the simple reason that descriptions are mostly static - for now(?). So removing something would therefore invalidate the descriptive text.
func (*Narrative) Copy ¶ added in v0.0.4
Copy returns a copy of the Narrative receiver.
func (*Narrative) Found ¶
Found returns false if the receiver is nil otherwise true.
type Player ¶
type Player struct {
Attribute
io.Writer
has.PromptStyle
}
Player implements an attribute for associating a Thing with a Writer used to return data to the associated client.
func NewPlayer ¶
NewPlayer returns a new Player attribute initialised with the specified Writer which is used to send data back to the associated client.
func (*Player) Copy ¶ added in v0.0.4
Copy returns a copy of the Player receiver.
NOTE: The copy will use the same io.Writer as the original.
func (*Player) Found ¶
Found returns false if the receiver is nil otherwise true.
func (*Player) Free ¶ added in v0.0.5
func (p *Player) Free()
Free makes sure references are nil'ed when the Player attribute is freed.
func (*Player) SetPromptStyle ¶
func (p *Player) SetPromptStyle(new has.PromptStyle) (old has.PromptStyle)
SetPromptStyle is used to set the current prompt style and returns the previous prompt style. This is so the previous prompt style can be restored if required later on.
func (*Player) Unmarshal ¶
Unmarshal is used to turn the passed data into a new Player attribute. At the moment Player attributes are created internally so return an untyped nil so we get ignored.
type Reset ¶ added in v0.0.5
Reset implements an Attribute for resetting or respawning Things in the game world. When a Thing is disposed of in the game world it may need to be reset and placed back into the game world at it's initial starting position after a delay period has elapsed. Otherwise the world quickly becomes empty with little for players to do. Some Thing's may respawn instead of resetting. When a Thing respawns another copy of the Thing is placed into the game world after a period of time when the Thing is taken. For both cases the delay period will be between Reset.after and Reset.after+Reset.jitter. If a Thing is being reset/respawned and is in its delay period the Reset.Cancel channel will be non-nil and the reset/respawn may be aborted by closing the channel. If Reset.spawn is true the Thing is respawnable otherwise it is resettable. Items that should just be removed when disposed of should not have a Reset attribute.
func NewReset ¶ added in v0.0.5
Reset implements an attribute for resetting or respawning Things and putting them back into the game world. The after and jitter Duration set the delay period to between after and after+jitter for when a Thing is reset or respawned. If spawn is true the Thing will respawn otherwise it will reset.
func (*Reset) Copy ¶ added in v0.0.5
Copy returns a copy of the Reset receiver. The copy will not inherit any pending Reset events.
func (*Reset) Found ¶ added in v0.0.5
Found returns false if the receiver is nil otherwise true.
func (*Reset) Free ¶ added in v0.0.5
func (r *Reset) Free()
Free makes sure references are nil'ed and channels closed when the Reset attribute is freed.
func (*Reset) Reset ¶ added in v0.0.5
func (r *Reset) Reset()
Reset schedules a reset of the parent Thing. If there is already a reset event pending it will be cancelled and a new one queued.
func (*Reset) Spawn ¶ added in v0.0.5
Spawn returns a non-spawnable copy of a Thing and schedules the original Thing to respawn.spawn if Reset.spawn is true. Otherwise it returns nil. If there is already a respawn event pending it will be cancelled and a new one queued.
type Start ¶
type Start struct {
Attribute
}
Start implements an attribute for tagging a Thing as a starting location.
func NewStart ¶
func NewStart() *Start
NewStart returns a new Start attribute. When a new Start attribute is created it is also registered automatically.
TODO: Implement starting locations that are only usable by specific players. For example only dwarves should start in the dwarven home, or thieves in the thieves guild.
func (*Start) Copy ¶ added in v0.0.4
Copy returns a copy of the Start receiver.
func (*Start) Found ¶
Found returns false if the receiver is nil otherwise true.
func (*Start) Pick ¶
Pick returns the Inventory of a randomly selected starting location.
type Thing ¶
type Thing struct {
// contains filtered or unexported fields
}
Thing is a container for Attributes. Everything in WolfMUD is constructed by creating a Thing and then adding Attributes to it which implement specific functionality.
func NewThing ¶
NewThing returns a new Thing initialised with the specified Attributes. Attributes can also be dynamically modified using Add and Remove methods.
func (*Thing) Add ¶
Add is used to add the passed Attributes to a Thing. When an Attribute is added its parent is set to reference the Thing it was added to. This allows an Attribute to find and query the parent Thing about other Attributes the Thing may have.
func (*Thing) Attrs ¶
Attrs returns all of the Attributes a Thing has as a slice of has.Attribute. This is commonly used to range over all of the Attributes of a Thing instead of using a finder for a specific type of Attribute.
func (*Thing) Copy ¶ added in v0.0.4
Copy returns a copy of the Thing receiver. The copy will be made recursively copying all associated Attribute and Thing.
func (*Thing) Dispose ¶ added in v0.0.5
func (t *Thing) Dispose()
Dispose will either reset or discard a Thing in the game world when it is finished with. If the Thing has a Reset attribute the Thing will be scheduled for a reset. Otherwise all references to the Thing will be freed so that it can be garbage collected. If the Thing has an Inventory its content will be reset or discarded recursively.
func (*Thing) Free ¶ added in v0.0.5
func (t *Thing) Free()
Free is used to clean-up/release references to all Attribute for a Thing. When a Thing is finished with calling Free helps the garbage collector to reclaim objects. It can also help to break cyclic references that could prevent garbage collection.
func (*Thing) Remove ¶
Remove is used to remove the passed Attributes from a Thing. There is no indication if an Attribute cannot actually be removed. When an Attribute is removed and is no longer required its Free method should be called.
func (*Thing) SetOrigins ¶ added in v0.0.5
func (t *Thing) SetOrigins()
SetOrigins updates the origin for the Thing to its containing Inventory and recursivly sets the origins for the content of a Thing's Inventory if it has one.
func (*Thing) UID ¶ added in v0.0.5
UID returns the unique identifier for a specific Thing or an empty string if the unique ID is unavailable. The unique ID should be automatically assigned to any Thing created by calling NewThing or Copy.
func (*Thing) Unmarshal ¶
Unmarshal unmarshals a Thing from a recordjar record containing all of the Attribute to be added. The recno is the record number in the recordjar for this record. It is passed so that we can give informative messages if errors are found. If the record number is not known -1 should be passed instead.
type Veto ¶
type Veto struct {
// contains filtered or unexported fields
}
Veto implements a veto for a specific command. Veto need to be added to a Vetoes list using NewVetoes.
func NewVeto ¶
NewVeto returns a new Veto attribute initialised for the specified command with the specified message text. The command is a normal command such as GET and DROP and will automatically be uppercased. The message text should indicate why the command was vetoed such as "You can't drop the sword. It seems to be cursed". Referring to specific items - such as the sword in the example - is valid as a Veto is for a specific known Thing.
func (*Veto) Command ¶
Command returns the command associated with the Veto.
type Vetoes ¶
type Vetoes struct {
Attribute
// contains filtered or unexported fields
}
Vetoes implement an attribute for lists of Veto preventing commands for a Thing that would otherwise be valid. For example you could Veto the drop command if a very sticky item is picked up :)
func NewVetoes ¶
NewVetoes returns a new Vetoes attribute initialised with the specified Vetos.
func (*Vetoes) Check ¶
Check checks if any of the passed commands are vetoed. The first matching Veto found is returned otherwise nil is returned.
func (*Vetoes) Copy ¶ added in v0.0.4
Copy returns a copy of the Vetoes receiver.
func (*Vetoes) Found ¶
Found returns false if the receiver is nil otherwise true.
type Writing ¶
type Writing struct {
Attribute
// contains filtered or unexported fields
}
Writing implements an attribute that allows for writing to be put onto any Thing so that it can be read.
TODO: Writing currently assumes the text is written onto a Thing. However it could also be carved, burnt, painted, etc. onto a Thing. It also assumes the text is in a common language known to all. If language were implemented we could write in common, elvish, dwarfish, ancient runes, secret code or anything else with the text only being readable by those who know the relevant language. See also the Writing Description method.
func NewWriting ¶
NewWriting returns a new Writing attribute initialised with the specified writing/text.
func (*Writing) Copy ¶ added in v0.0.4
Copy returns a copy of the Writing receiver.
func (*Writing) Description ¶
Description automatically adds the specified text to the description of a Thing that has a has.Writing attribute.
FIXME: This should return a message based on the type of writing: runes, painting, carving etc. See also TODO for the Writing type.
func (*Writing) Found ¶
Found returns false if the receiver is nil otherwise true.
func (*Writing) Unmarshal ¶
Unmarshal is used to turn the passed data into a new Writing attribute.
Notes ¶
Bugs ¶
Aliases are expected be single words only, otherwise they probably won't work correctly and cause all sorts of weird problems and behaviour. See also TODO about prefixes.
Does not check that exit A links to B and B links back to A. For example a maze may have an exit going North from A to B but going South from B takes you to C instead of back to A as would be expected!
Inventory capacity is not implemented yet.
Source Files
¶
- alias.go
- attribute.go
- cleanup.go
- description.go
- doc.go
- door.go
- exits.go
- inventory.go
- locate.go
- name.go
- narrative.go
- player.go
- reset.go
- start.go
- thing.go
- vetoes.go
- writing.go