game

package
v0.0.0-...-d091464 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ActionResultNoChange ActionResult = 0
	ActionResultPending               = 1 << iota
	ActionResultChangeRegions
	ActionResultClearAnimations
	ActionResultClearGraphics
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	ActionType              ActionType
	TickDelay               uint
	ServerMessageAction     *ServerMessageAction
	MoveInventoryItemAction *MoveInventoryItemAction
	TakeGroundItem          *TakeGroundItemAction
	DropInventoryItemAction *DropInventoryItemAction
	EquipItemAction         *EquipItemAction
	UnequipItemAction       *UnequipItemAction
	ShowInterfaceAction     *ShowInterfaceAction
	DoInterfaceAction       *DoInterfaceAction
	TeleportPlayerAction    *TeleportPlayerAction
	CastSpellOnItemAction   *CastSpellOnItemAction
	ExperienceGrantAction   *ExperienceGrantAction
	PlayMusicAction         *PlayMusicAction
	PlayerChangeEventAction *PlayerChangeEventAction
	SendSkillsAction        *SendSkillsAction
}

Action is an action that will be performed after a number of game ticks have elapsed.

type ActionPriority

type ActionPriority int

ActionPriority enumerates the priority of an action when it's deferred.

const (
	// ActionPriorityNormal is an action scheduled after all currently deferred actions.
	ActionPriorityNormal ActionPriority = iota
	// ActionPriorityHigh is an action scheduled ahead of all currently deferred actions.
	ActionPriorityHigh
)

type ActionResult

type ActionResult int

ActionResult is a bitmask describing the mutations resulting from an action.

type ActionType

type ActionType int

ActionType enumerates deferred actions that a player can take.

const (
	ActionMoveInventoryItem ActionType = iota
	ActionSendServerMessage
	ActionSendSkills
	ActionSendModes
	ActionSendFriendList
	ActionSendIgnoreList
	ActionSendEquipment
	ActionSendInventory
	ActionTakeGroundItem
	ActionDropInventoryItem
	ActionEquipItem
	ActionUnequipItem
	ActionShowInterface
	ActionHideInterfaces
	ActionDoInterfaceAction
	ActionTeleportPlayer
	ActionCastSpellOnItem
	ActionExperienceGrant
	ActionSendRunEnergy
	ActionSendChangeEvent
	ActionSendWeight
	ActionPlayMusic
	ActionDelayCurrent
)

type CastSpellOnItemAction

type CastSpellOnItemAction struct {
	SlotID               int
	ItemID               int
	InventoryInterfaceID int
	SpellInterfaceID     int
}

CastSpellOnItemAction is an action to cast a spell on an inventory item.

type ChatCommand

type ChatCommand struct {
	Type          ChatCommandType
	Pos           model.Vector3D
	SpawnItem     *ChatCommandSpawnItemParams
	ShowInterface *ChatCommandShowInterfaceParams
	Animate       *ChatCommandAnimateParams
}

ChatCommand is a game command embedded in a player chat message.

func ParseChatCommand

func ParseChatCommand(text string) *ChatCommand

ParseChatCommand attempts to parse a chat command from a string of text. If no recognized command is found, then nil is returned instead.

type ChatCommandAnimateParams

type ChatCommandAnimateParams struct {
	ID    int
	Delay int
}

ChatCommandAnimateParams contains parameters for an animation.

type ChatCommandShowInterfaceParams

type ChatCommandShowInterfaceParams struct {
	InterfaceID int
}

ChatCommandShowInterfaceParams contains parameters for showing an interface.

type ChatCommandSpawnItemParams

type ChatCommandSpawnItemParams struct {
	ItemID             int
	Amount             int
	DespawnTimeSeconds *int
}

ChatCommandSpawnItemParams contains parameters for a chat command that spawns a ground Item.

type ChatCommandType

type ChatCommandType int

ChatCommandType enumerates possible chat commands recognized by the server.

const (
	ChatCommandTypeSpawnItem ChatCommandType = iota
	ChatCommandTypeClearTile
	ChatCommandTypePosition
	ChatCommandTeleport
	ChatCommandTeleportRelative
	ChatCommandShowInterface
	ChatCommandHideInterfaces
	ChatCommandCharacterDesigner
	ChatCommandReloadScripts
	ChatCommandAnimate
)

type DoInterfaceAction

type DoInterfaceAction struct {
	Parent *model.Interface
	Actor  *model.Interface
}

DoInterfaceAction is an action taken on an interface.

type DropInventoryItemAction

type DropInventoryItemAction struct {
	InterfaceID       int
	Item              *model.Item
	SecondaryActionID int
}

DropInventoryItemAction is an action to drop an inventory item.

type EquipItemAction

type EquipItemAction struct {
	InterfaceID int
	Item        *model.Item
}

EquipItemAction is an action to equip an item from the player's inventory

type Event

type Event struct {
	Type         EventType
	Schedule     time.Time
	InstanceUUID uuid.UUID
	GlobalPos    model.Vector3D
}

Event is an action that the game server should take at a specified time.

func NewEventWithType

func NewEventWithType(eventType EventType, when time.Time) *Event

NewEventWithType creates an event with a specific type that should be processed at the provided time.

type EventType

type EventType int
const (
	// EventRemoveExpiredGroundItem removes a ground Item on a tile after it has expired.
	EventRemoveExpiredGroundItem EventType = iota
)

type ExperienceGrantAction

type ExperienceGrantAction struct {
	SkillType  model.SkillType
	Experience float64
}

ExperienceGrantAction is an action to grant the player experience after a delay.

type Game

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

Game is the game engine and representation of the game world.

func NewGame

func NewGame(opts Options) (*Game, error)

NewGame creates a new game engine using the given configuration.

func (*Game) AddFriend

func (g *Game) AddFriend(p *model.Player, username string)

AddFriend attempts to add another player to the player's friends list.

func (*Game) AddIgnored

func (g *Game) AddIgnored(p *model.Player, username string)

AddIgnored attempts to add another player to the player's ignored list.

func (*Game) AddPlayer

func (g *Game) AddPlayer(p *model.Player, lowMemory bool, writer *network.ProtocolWriter)

AddPlayer joins a player to the world and handles ongoing game events and network interactions. The lowMemory flag indicates if the player opted to play in low-memory mode on the client.

func (*Game) DoAttackNPC

func (g *Game) DoAttackNPC(p *model.Player, targetID int)

DoAttackNPC handles a player requesting to attack an NPC.

func (*Game) DoCastSpellOnItem

func (g *Game) DoCastSpellOnItem(p *model.Player, slotID, itemID, inventoryInterfaceID, spellInterfaceID int)

DoCastSpellOnItem handles a player casting a spell on one of their inventory items.

func (*Game) DoDropInventoryItem

func (g *Game) DoDropInventoryItem(p *model.Player, itemID, interfaceID, secondaryActionID int)

DoDropInventoryItem handles a player's request to drop an inventory item.

func (*Game) DoEquipItem

func (g *Game) DoEquipItem(p *model.Player, itemID, interfaceID, secondaryActionID int)

DoEquipItem handles a player's request to equip an item.

func (*Game) DoInteractWithNPC

func (g *Game) DoInteractWithNPC(p *model.Player, actionIndex, targetID int)

DoInteractWithNPC handles a player requesting to interact with an NPC.

func (*Game) DoInteractWithObject

func (g *Game) DoInteractWithObject(p *model.Player, action int, globalPos model.Vector2D)

DoInteractWithObject handles a player interaction with an object on the map.

func (*Game) DoInterfaceAction

func (g *Game) DoInterfaceAction(p *model.Player, interfaceID int)

DoInterfaceAction processes an action that a player performed on an interface.

func (*Game) DoPlayerChat

func (g *Game) DoPlayerChat(p *model.Player, effect model.ChatEffect, color model.ChatColor, text string)

DoPlayerChat broadcasts a player's chat message to nearby players.

func (*Game) DoPlayerChatCommand

func (g *Game) DoPlayerChatCommand(p *model.Player, text string)

DoPlayerChatCommand handles a chat command sent by a player.

func (*Game) DoPlayerPrivateChat

func (g *Game) DoPlayerPrivateChat(p *model.Player, recipient string, text string)

DoPlayerPrivateChat sends a private chat message from one player to a recipient player.

func (*Game) DoSetPlayerDesign

func (g *Game) DoSetPlayerDesign(p *model.Player, gender model.EntityGender, base model.EntityBase, bodyColors []int)

DoSetPlayerDesign handles updating a player's character design.

func (*Game) DoSwapInventoryItem

func (g *Game) DoSwapInventoryItem(p *model.Player, fromSlot int, toSlot int, mode int)

DoSwapInventoryItem handles a player's request to move an item in their inventory to another slot.

func (*Game) DoTakeGroundItem

func (g *Game) DoTakeGroundItem(p *model.Player, itemID int, globalPos model.Vector2D)

DoTakeGroundItem handles a player's request to pick up a ground item at a position, in global coordinates.

func (*Game) DoUnequipItem

func (g *Game) DoUnequipItem(p *model.Player, itemID, interfaceID int, slotType model.EquipmentSlotType)

DoUnequipItem handles a player's request to unequip an item.

func (*Game) DoUseInventoryItem

func (g *Game) DoUseInventoryItem(p *model.Player, sourceItemID, sourceInterfaceID, sourceSlotID,
	targetItemID, targetInterfaceID, targetSlotID int)

DoUseInventoryItem handles a player's request to use an inventory item on another item.

func (*Game) DoUseItem

func (g *Game) DoUseItem(p *model.Player, itemID, interfaceID, actionID int)

DoUseItem handles a player's request to use an item.

func (*Game) MarkPlayerActive

func (g *Game) MarkPlayerActive(p *model.Player)

MarkPlayerActive updates a player's last activity tracker and prevents them from becoming idle.

func (*Game) MarkPlayerInactive

func (g *Game) MarkPlayerInactive(p *model.Player)

MarkPlayerInactive flags that a player's client reported them as being idle.

func (*Game) ProcessAbuseReport

func (g *Game) ProcessAbuseReport(p *model.Player, username string, reason int, mute bool)

ProcessAbuseReport handles an abuse report sent by a player.

func (*Game) RemoveFriend

func (g *Game) RemoveFriend(p *model.Player, username string)

RemoveFriend removes another player from the player's friends list.

func (*Game) RemoveIgnored

func (g *Game) RemoveIgnored(p *model.Player, username string)

RemoveIgnored removes another player from the player's ignored list.

func (*Game) RemovePlayer

func (g *Game) RemovePlayer(p *model.Player)

RemovePlayer removes a previously joined player from the world.

func (*Game) Run

func (g *Game) Run()

Run starts the game loop and begins processing events. You need to call this method before players can connect and interact with the game.

func (*Game) SetPlayerModes

func (g *Game) SetPlayerModes(p *model.Player, publicChat model.ChatMode, privateChat model.ChatMode, interaction model.InteractionMode)

SetPlayerModes updates the chat and interaction modes for a player.

func (*Game) Stop

func (g *Game) Stop()

Stop gracefully terminates the game loop.

func (*Game) ValidatePlayer

func (g *Game) ValidatePlayer(p *model.Player) ValidationResult

ValidatePlayer checks if a player can be added to the game.

func (*Game) WalkPlayer

func (g *Game) WalkPlayer(p *model.Player, start model.Vector2D, waypoints []model.Vector2D)

WalkPlayer starts moving the player to a destination from a start position then following a set of waypoints. The slice of waypoints are deltas relative to start.

type MapManager

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

MapManager is responsible for managing the state of the entire world map.

func NewMapManager

func NewMapManager(m *model.Map) *MapManager

NewMapManager creates a new manager for a world map.

func (*MapManager) AddGroundItem

func (m *MapManager) AddGroundItem(itemID, amount int, stackable bool, timeoutSeconds *int, globalPos model.Vector3D)

AddGroundItem adds a ground Item to the top of a tile with an optional timeout (in seconds) when that Item should automatically be removed. Stackable items will be added to an existing stackable with the same Item ID, if one exists, or they will be placed as new items on the tile.

func (*MapManager) AddNPC

func (m *MapManager) AddNPC(ne *npcEntity, regionGlobal model.Vector3D)

AddNPC adds an NPC to the world map at the region whose coordinates correspond to regionGlobal.

func (*MapManager) AddPlayer

func (m *MapManager) AddPlayer(pe *playerEntity, regionGlobal model.Vector3D)

AddPlayer adds a player to the world map at the region whose coordinates correspond to regionGlobal.

func (*MapManager) ClearGroundItems

func (m *MapManager) ClearGroundItems(globalPos model.Vector3D)

ClearGroundItems removes all ground items on a tile.

func (*MapManager) FindSpectators

func (m *MapManager) FindSpectators(pe *playerEntity) (map[int]*playerEntity, map[int]*npcEntity)

FindSpectators returns a tuple of players and NPCs that are within visual distance to a player. The keys of the map are the player's IDs and NPC IDs, respectively.

func (*MapManager) Reconcile

func (m *MapManager) Reconcile() map[model.Vector3D][]response.Response

Reconcile validates the current state of the entire world map and recomputes its state if a change has occurred.

func (*MapManager) RegionsWithNPCs

func (m *MapManager) RegionsWithNPCs() map[int]*npcEntity

RegionsWithNPCs returns a map of NPC IDs to their entities that are found on the map.

func (*MapManager) RemoveGroundItem

func (m *MapManager) RemoveGroundItem(itemID int, globalPos model.Vector3D) *model.TileGroundItem

RemoveGroundItem attempts to remove a ground Item with the given ID at a position, in global coordinates. If the Item was found and removed, a pointer to its model.TileGroundItem model will be returned.

func (*MapManager) RemoveNPC

func (m *MapManager) RemoveNPC(ne *npcEntity, regionGlobal model.Vector3D)

RemoveNPC removes an NPC from the world map from the region specified by regionGlobal.

func (*MapManager) RemovePlayer

func (m *MapManager) RemovePlayer(pe *playerEntity, regionGlobal model.Vector3D)

RemovePlayer removes a player from the world map from the region specified by regionGlobal.

func (*MapManager) Start

func (m *MapManager) Start()

Start begins background routines that monitor for changes to the state of the map. When cleaning up, Stop() should be called to gracefully terminate this process.

func (*MapManager) State

func (m *MapManager) State(origin model.Vector3D, trim model.Boundary) []response.Response

State returns the last computed state of a 2D region. The origin should be the region origin in global coordinates, and the z-coordinate will be used to determine which plane of a region to return. If no region exists at this origin, nil will be returned instead.

func (*MapManager) Stop

func (m *MapManager) Stop()

Stop terminates scheduled events and stops the management of the map.

func (*MapManager) WarmUp

func (m *MapManager) WarmUp()

WarmUp computes the initial state of the world map. This should generally be called only once before the game state begins changing.

type MoveInventoryItemAction

type MoveInventoryItemAction struct {
	FromSlot int
	ToSlot   int
}

MoveInventoryItemAction is an action to move or swap the position of an inventory item.

type Options

type Options struct {
	Config         *config.Config
	ItemAttributes []*model.ItemAttributes
	Telemetry      telemetry.Telemetry
}

Options are parameters that configure how the game engine behaves.

type PlayMusicAction

type PlayMusicAction struct {
	MusicID int
}

PlayMusicAction is an action to instruct the player's client to play a song.

type PlayerChangeEventAction

type PlayerChangeEventAction struct {
	Event model.PlayerChangeEvent
}

PlayerChangeEventAction is an action to handle a change to a player's attributes.

type RegionManager

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

RegionManager is responsible for tracking the state of a single, 2D region on the world map. A region is defined by a square of size util.Region3D centered about an origin, plus additional tiles on each boundary equal to util.Chunk2D * 2. Therefore, the entire span of tiles for a RegionManager is util.Area2D.

func NewRegionManager

func NewRegionManager(origin model.Vector3D, m *model.Map) *RegionManager

NewRegionManager creates a manager for a 2D region of the map centered around an origin in global coordinates. The z-coordinate will be used to determine which plane of the region this manager will be responsible for.

func (*RegionManager) AddNPC

func (r *RegionManager) AddNPC(ne *npcEntity)

AddNPC adds an NPC to the region.

func (*RegionManager) AddPlayer

func (r *RegionManager) AddPlayer(pe *playerEntity)

AddPlayer adds a player to the region.

func (*RegionManager) Contains

func (r *RegionManager) Contains(globalPos model.Vector3D) bool

Contains returns true if a position, in global coordinates, is managed by this manager.

func (*RegionManager) FindNearbyNPCs

func (r *RegionManager) FindNearbyNPCs(pe *playerEntity) []*npcEntity

FindNearbyNPCs returns a slice of NPCs that are within visual distance of a given player.

func (*RegionManager) FindNearbyPlayers

func (r *RegionManager) FindNearbyPlayers(pe *playerEntity) []*playerEntity

FindNearbyPlayers returns a slice of players that are within visual distance of a given player.

func (*RegionManager) MarkGroundItemAdded

func (r *RegionManager) MarkGroundItemAdded(itemID, amount int, globalPos model.Vector3D)

MarkGroundItemAdded informs the region manager that a ground Item with a stack amount was placed on a tile.

func (*RegionManager) MarkGroundItemUpdated

func (r *RegionManager) MarkGroundItemUpdated(itemID, oldAmount, newAmount int, globalPos model.Vector3D)

MarkGroundItemUpdated informs the region manager that a ground Item's stack amount was updated.

func (*RegionManager) MarkGroundItemsCleared

func (r *RegionManager) MarkGroundItemsCleared(itemIDs []int, globalPos model.Vector3D)

MarkGroundItemsCleared informs the region manager that all ground items on a tile have been removed.

func (*RegionManager) Reconcile

func (r *RegionManager) Reconcile() []response.Response

Reconcile validates the current state of the region and recomputes its state if a change has occurred. A slice of messages will be returned that should be dispatched to players in the region.

func (*RegionManager) RemoveNPC

func (r *RegionManager) RemoveNPC(ne *npcEntity)

RemoveNPC removes an NPC from the region.

func (*RegionManager) RemovePlayer

func (r *RegionManager) RemovePlayer(pe *playerEntity)

RemovePlayer removes a player from the region.

func (*RegionManager) State

func (r *RegionManager) State(trim model.Boundary) []response.Response

State returns the last computed state of the region, described as a slice of response.Response messages that can be sent to a player's client.

type Scheduler

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

Scheduler provides a thread-safe queue of events that can be planned on a time basis.

func NewScheduler

func NewScheduler(changeChan chan bool) *Scheduler

NewScheduler creates a new event scheduler that will write to the changeChan when new events are scheduled.

func (*Scheduler) Next

func (s *Scheduler) Next() *Event

Next returns the next event that should be processed. If there are no events, nil will be returned instead.

func (*Scheduler) Plan

func (s *Scheduler) Plan(e *Event)

Plan schedules an event for later processing.

func (*Scheduler) TimeUntil

func (s *Scheduler) TimeUntil() time.Duration

TimeUntil returns the time left until the next event should be processed. If there are no events, then a time duration in the far future will be returned.

type ScriptHandler

type ScriptHandler interface {
	// contains filtered or unexported methods
}

ScriptHandler is the interface between the game engine and the script system.

type ScriptManager

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

ScriptManager manages game server scripts.

func NewScriptManager

func NewScriptManager(baseDir string, handler ScriptHandler) *ScriptManager

NewScriptManager creates a new script manager that manages scripts in a baseDir directory.

func (*ScriptManager) DoCastSpellOnItem

func (s *ScriptManager) DoCastSpellOnItem(pe *playerEntity, item *model.Item, slotID int, inventory, spellBook, spell *model.Interface) (bool, error)

DoCastSpellOnItem executes a script to handle a player casting a spell on an inventory items. If the spell has no further deferred actions, true will be returned. Otherwise, false will be returned to indicate that a deferred action has been planned that needs to completed before others can.

func (*ScriptManager) DoInterface

func (s *ScriptManager) DoInterface(pe *playerEntity, parent, actor *model.Interface, opCode int) error

DoInterface executes an interface script for an action performed by the player.

func (*ScriptManager) DoNPCOnTick

func (s *ScriptManager) DoNPCOnTick(ne *npcEntity) error

DoNPCOnTick executes an NPC's per-tick logic script.

func (*ScriptManager) DoOnEquipItem

func (s *ScriptManager) DoOnEquipItem(pe *playerEntity, item *model.Item) error

DoOnEquipItem executes a script to handle a player (un)equipping an item.

func (*ScriptManager) DoOnUnequipItem

func (s *ScriptManager) DoOnUnequipItem(pe *playerEntity, item *model.Item) error

DoOnUnequipItem executes a script to handle a player unequipping an item.

func (*ScriptManager) DoPlayerChangeEvent

func (s *ScriptManager) DoPlayerChangeEvent(event model.PlayerChangeEvent, pe *playerEntity) error

DoPlayerChangeEvent executes a script to handle a change on a player's attributes.

func (*ScriptManager) DoPlayerInit

func (s *ScriptManager) DoPlayerInit(pe *playerEntity) error

DoPlayerInit executes a script to initialize a player when they join the game.

func (*ScriptManager) Load

func (s *ScriptManager) Load() (int, error)

Load parses and loads all script files located in the base directory, returning the number of scripts on success. If any script fails to load, an error will be returned. If this method is called after the initial script load is done, a full reload of all scripts will be performed. This will destroy the current script manager state, so you need to ensure that no consumers are currently awaiting a result from a script.

type SendSkillsAction

type SendSkillsAction struct {
	SkillTypes []model.SkillType
}

SendSkillsAction is an action that sends skill information to the player's client.

type ServerMessageAction

type ServerMessageAction struct {
	Message string
}

ServerMessageAction is an action to send the player a server message.

type ShowInterfaceAction

type ShowInterfaceAction struct {
	InterfaceID int
}

ShowInterfaceAction is an action to show an interface.

type TakeGroundItemAction

type TakeGroundItemAction struct {
	GlobalPos model.Vector3D
	Item      *model.Item
}

TakeGroundItemAction is an action to pick up a ground item that should occur at a position.

type TeleportPlayerAction

type TeleportPlayerAction struct {
	GlobalPos model.Vector3D
}

TeleportPlayerAction is an action to teleport a player.

type UnequipItemAction

type UnequipItemAction struct {
	InterfaceID int
	Item        *model.Item
	SlotType    model.EquipmentSlotType
}

UnequipItemAction is an action to unequip an item from the player's equipment

type ValidationResult

type ValidationResult int

ValidationResult enumerates the errors that can result from game engine player validation.

const (
	// ValidationResultSuccess indicates no error was found during player validation.
	ValidationResultSuccess ValidationResult = iota
	// ValidationResultAlreadyLoggedIn indicates a player already has an existing session.
	ValidationResultAlreadyLoggedIn
	// ValidationResultNoCapacity indicates the server cannot accommodate more players.
	ValidationResultNoCapacity
)

Jump to

Keyboard shortcuts

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