nbattle

package module
v0.0.0-...-963a065 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 9 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

NBattle (Go)

By Nathan Ogden

Go library for managing RPG combat. It allows for the creation of combatants with stats and effects that can be applied to them. Effects are how combatants interact with one another, this can range from basic attacks to long lived status effects. Effects can be written in either Go or Lua.

This is a work in progress and is currently largely untested.

Lua Effects

Effects can be defined via Lua scripting.

Usage

To add a Lua effect use the NewEffect function and provide the NBattle context and a reader containing the Lua script.

import (
	nbattle "github.com/chompy/nbattle_go"
)

func main() {
    ctx := nbattle.New()
    f, err := os.Open("script.lua")
    if err != nil {
        panic(err)
    }
    defer f.Close()
    effectDef, err := ctx.NewLuaEffect(ctx, f)
    // ...
}
Scripts

The Lua script at minimum should contain a Name function that returns the name of the effect.

function Name()
    return "attack"
end

All variable and argument types...

  • GLOBALS

    • ctx - Context
    • STAT_* - StatDef - Every stat definition will get a global value that is 'STAT_` combined with the stat definition name in all uppercase letters.
    • FLAG_* - number - Every flag will get a global value that is 'FLAG_' combined with the flag name in all uppercase letters.
  • Object - Value representing an object. It can be the actual object, the ID number of the object, or the name string of the object.

  • Context

    • getTick() <number> - Function that returns the current tick.
    • getCombatants() <[]Combatant> - Function that returns a list of all combatants.
    • getObject(object <Object>) <Object> - Function that retrieve an object.
    • emitTrigger(name <string>, source <Object>) - Function that emits a trigger event.
  • StatDef

    • type <number> - Type number of object.
    • name <string> - Name of the stat definition.
    • min <number> - Minimum value of the stat.
    • max <number> - Maximum value of the stat.
  • Stat

    • def <StatDef> - Stat definition for this stat.
    • getBase() <number> - Function that returns the current base value of this stat.
    • setBase(value <number>) - Function that sets the base value of this stat.
    • addBase(value <number>) - Function that adds the given value to the base value of this stat.
    • subBase(value <number>) - Function that subtracts the given value to the base value of this stat.
    • getValue() <number> - Function that returns the current value of this stat with modifications.
    • setMod(source <Object>, value <number>) - Function that applies a modification to the stat. Modifications are mapped to an object. Modification value is added to the base value. A value of zero removes the mod.
    • getCombatant() <Combatant> - Function that returns the combatant this stat is applied to.
  • Combatant

    • getStat(statDef <Object>) - Retrieve a combatant stat from the stat definition.
    • setEffect(effectDef <Object>, potency <number>, sourceObject <Object>) - Apply effect to combatant.
    • removeEffect(effectDef <Object>) - Remove effect from combatant. Same as calling setEffect with zero potency.
    • hasEffect(effectDef <Object>) - Return true if combatant has given effect applied.
    • setFlag(flag <number>) - Apply a flag to the combatant.
    • hasFlag(flag <number>) - Return true if combatant has the given flag.
  • EffectCtx

    • target <Combatant> - The combatant that is the target of the effect.

    • source <Object> - The object that is the source of the effect. This can be nil.

    • effect <string> - The name of the effect.

    • potency <number> - The potency of the effect.

    • remove() - Function that removes this effect. Same as calling combatant.setEffect with zero potency.

  • TickEvent

    • tick <number> - The current tick.
  • CombatantUpdateEvent

    • combatant <Combatant> - The combatant who received the update.
    • active <bool> - Whether or not the combatant is currently active.
    • flags <number> - The combatant flag value.
  • CombatantStatBaseEvent

    • combatant <Combatant> - The combatant whose base stat changed.
    • statDef <StatDef> - The stat definition of the stat that changed.
    • value <number> - The new value of the stat.
    • setValue(value <number>) - Function that allows the base stat value to be altered as part of the same event.
  • CombatantStatModEvent

    • combatant <Combatant> - The combatant whose stat has been modified.
    • statDef <StatDef> - The stat definition of the stat that has been modified.
    • value <number> - The modification amount.
    • setValue(value <number>) - Function that allows the stat modification value to be altered as part of the same event.
  • CombatantEffectEvent

    • target <Combatant> - The combatant whom the effect is being applied to.
    • effect <string> - The name of the effect.
    • potency <number> - The potency of the effect. If this is zero then the effect is being removed.
    • source <Object> - The source of the effect, can be nil.
  • TriggerEvent

    • trigger <string> - The name of the trigger.
    • source <Object> - The source of the trigger.

Event handling functions...

  • OnAdd(ctx <Context>, effectCtx <EffectCtx>) - Called when the effect is applied to a combatant.
  • OnRemove(ctx <Context>, effectCtx <EffectCtx>) - Called when the effect is removed from a combatant.
  • OnTick(ctx <Context>, effectCtx <EffectCtx>, evt <TickEvent>) - Called whenever combatant advances a tick.
  • OnCombatantUpdate(ctx <Context>, effectCtx <EffectCtx>, evt <CombatantUpdateEvent>) - Called when a new combatant is added or an existing combatant is updated.
  • OnCombatantStatBase(ctx <Context>, effectCtx <EffectCtx>, evt <CombatantStatBaseEvent>) - Called when a stat applied to a combatant has a base value change.
  • OnCombatantStatMod(ctx <Context>, effectCtx <EffectCtx>, evt <CombatantStatModEvent>) - Called when a stat applied to a combatant has a mod value change.
  • OnCombatantEffect(ctx <Context>, effectCtx <EffectCtx>, evt <CombatantEffectEvent>) - Called when an effect is added or removed from a combatant.
  • OnTrigger(ctx <Context>, effectCtx <EffectCtx>, evt <TriggerEvent>) - Called when an effect emits a trigger event.

Documentation

Index

Constants

View Source
const (
	ObjectTypeStatDef = iota
	ObjectTypeEffectDef
	ObjectTypeTriggerDef
	ObjectTypeCombatant
	ObjectTypeUnknown
	ObjectTypeError
)

Variables

View Source
var (
	ErrObjectNotFound          = errors.New("object not found")
	ErrUnexpectedObjectType    = errors.New("unexpected object type")
	ErrNilObject               = errors.New("object is nil")
	ErrUnexpectedLuaFuncReturn = errors.New("unexpected return value/type from lua function call")
)

Functions

This section is empty.

Types

type BaseObject

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

BaseObject is a base struct for all objects.

func (*BaseObject) GetID

func (o *BaseObject) GetID() int

Retrieve the object ID.

type Combatant

type Combatant struct {
	BaseObject
	// contains filtered or unexported fields
}

Combatant is an entity that can interact with other combatants.

func (*Combatant) GetFlags

func (c *Combatant) GetFlags() uint64

GetFlags returns the current flags value.

func (*Combatant) GetStat

func (c *Combatant) GetStat(statDefObj any) (*Stat, error)

GetStat retrieves the combatant's stat from a stat definition object.

func (*Combatant) GetStats

func (c *Combatant) GetStats() []*Stat

GetStats retrieves all the combatant's stats.

func (*Combatant) GetType

func (c *Combatant) GetType() ObjectType

func (*Combatant) HasEffect

func (c *Combatant) HasEffect(effectDefObj any) bool

HasEffect returns true if the combatant has the given effect active.

func (*Combatant) HasFlag

func (c *Combatant) HasFlag(flag any) bool

HasFlag returns true if the given flag is set.

func (*Combatant) IsActive

func (c *Combatant) IsActive() bool

IsActive returns true if the combatant is active.

func (*Combatant) Reset

func (c *Combatant) Reset()

Reset resets the combatant's stats to their base values.

func (*Combatant) SetActive

func (c *Combatant) SetActive(active bool)

SetActive sets the combatant's active state.

func (*Combatant) SetEffect

func (c *Combatant) SetEffect(effectDefObj any, sourceObj any, potency int) error

SetEffect adds an effect to the combatant. The same effect can be applied multiple times from different sources. Potency is the power of the effect. A potency of zero removes the effect.

func (*Combatant) SetFlag

func (c *Combatant) SetFlag(flag any, on bool)

SetFlag sets the given flag to true or false.

func (*Combatant) String

func (c *Combatant) String() string

type Context

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

Context is the main object of NBattle.

func New

func New() *Context

New creates a new NBattle context.

func (*Context) EmitEvent

func (c *Context) EmitEvent(event event.Event) error

EmitEvent sends an event to all hooks and active combatant effects.

func (*Context) EmitTrigger

func (c *Context) EmitTrigger(triggerDefObj any, sourceObj any) error

EmitTrigger emits a trigger event from the given source.

func (*Context) GetCombatant

func (c *Context) GetCombatant(obj any) (*Combatant, error)

GetCombatant retrieves a combatant from an unknown variable.

func (*Context) GetCombatantWithStat

func (c *Context) GetCombatantWithStat(stat *Stat) (*Combatant, error)

GetCombatantWithStat retrieves a combatant by a stat that has been assigned to it.

func (*Context) GetCombatants

func (c *Context) GetCombatants() []*Combatant

GetCombatants retrieves all combatants.

func (*Context) GetEffectDef

func (c *Context) GetEffectDef(obj any) (*EffectDef, error)

GetEffectDef retrieves an effect definition from an unknown variable.

func (*Context) GetFlagByName

func (c *Context) GetFlagByName(name string) uint64

GetFlagByName retrieves a flag by its name.

func (*Context) GetFlags

func (c *Context) GetFlags() map[string]uint64

GetFlags retrieves all flags.

func (*Context) GetObject

func (c *Context) GetObject(obj any) (Object, error)

GetObject retrieves an object from an unknown type value (ID, name, or map containing ID).

func (*Context) GetObjectByID

func (c *Context) GetObjectByID(ID int) (Object, error)

GetObjectByID retrieves an object by its ID.

func (*Context) GetObjectByIDAndType

func (c *Context) GetObjectByIDAndType(ID int, objType ObjectType) (Object, error)

GetObjecGetObjectByIDAndTypetByID retrieves an object by its ID and type.

func (*Context) GetObjectByName

func (c *Context) GetObjectByName(name string) (Object, error)

GetObjectByName retrieves an object by its name.

func (*Context) GetObjectByNameAndType

func (c *Context) GetObjectByNameAndType(name string, objType ObjectType) (Object, error)

GetObjectByNameAndType retrieves an object by its name and type.

func (*Context) GetObjectByType

func (c *Context) GetObjectByType(obj any, objType ObjectType) (Object, error)

GetObject retrieves an object from an unknown type value (ID, name, or map containing ID).

func (*Context) GetStatDef

func (c *Context) GetStatDef(obj any) (*StatDef, error)

GetStatDef retrieves a stat definition from an unknown variable.

func (*Context) GetStatDefs

func (c *Context) GetStatDefs() []*StatDef

GetStatDefs retrieves all stat definitions.

func (*Context) GetTick

func (c *Context) GetTick() int

GetTick retrieves the current tick.

func (*Context) GetTriggerDef

func (c *Context) GetTriggerDef(obj any) (*TriggerDef, error)

GetTriggerDef retrieves a trigger definition from an unknown variable.

func (*Context) HookEvents

func (c *Context) HookEvents(hook event.Hook)

HookEvents adds a new event hook.

func (*Context) NewCombatant

func (c *Context) NewCombatant() *Combatant

NewCombatant creates a new combatant.

func (*Context) NewEffectDef

func (c *Context) NewEffectDef(name string, create func() Effect) *EffectDef

NewEffectDef creates a new effect definition.

func (*Context) NewFlag

func (c *Context) NewFlag(name string) uint64

NewFlag creates a new flag.

func (*Context) NewLuaEffect

func (c *Context) NewLuaEffect(script io.Reader) (*EffectDef, error)

NewEffect creates a new EffectDef from a Lua script.

func (*Context) NewStatDef

func (c *Context) NewStatDef(name string, min, max int) *StatDef

NewStatDef creates a new stat definition.

func (*Context) NewTriggerDef

func (c *Context) NewTriggerDef(name string) *TriggerDef

NewTriggerDef creates a new trigger definition.

func (*Context) ProcessEvent

func (c *Context) ProcessEvent(evt event.Event) error

ProcessEvent processes an event from another context.

func (*Context) Tick

func (c *Context) Tick() int

Tick advances the tick counter and emit tick event.

type Effect

type Effect interface {
	OnAdd(ctx *Context, effect *EffectContext)                      // OnAdd is called when the effect is first applied to a combatant.
	OnRemove(ctx *Context, effect *EffectContext)                   // OnRemove is called when the effect is removed from a combatant.
	OnEvent(ctx *Context, effect *EffectContext, event event.Event) // OnEvent is called whenever an outside event is emitted.
}

Effect is an interface for effects.

type EffectContext

type EffectContext struct {
	Def     *EffectDef
	Target  *Combatant
	Source  Object
	Potency int
}

EffectCtx is the context for an effect that has been applied to a specific combatant.

func (*EffectContext) String

func (e *EffectContext) String() string

type EffectDef

type EffectDef struct {
	BaseObject
	// contains filtered or unexported fields
}

EffectDef is a definition of an effect. An effect can be applied to combatants and used to dynamically alter the combatant's stats.

func (*EffectDef) GetName

func (d *EffectDef) GetName() string

func (*EffectDef) GetType

func (d *EffectDef) GetType() ObjectType

func (*EffectDef) String

func (e *EffectDef) String() string

type Object

type Object interface {
	GetID() int          // Get the object's unique ID.
	GetType() ObjectType // Get the object's type.
	String() string      // Get the string representation of the object.
}

Object is an interface for all objects managed by NBattle.

type ObjectType

type ObjectType int

func (ObjectType) String

func (o ObjectType) String() string

type Stat

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

Stat represents a single integer value that can be applied to a combatant. It represents the combatant's combat ability in one specific way (such as strength or intelegence). A stat can have mods applied to it that are temporary alterations applied by effects. Mods are a mapping of the effect definition ID that applied it and the ammount to offset the base stat value by.

func (*Stat) AddBase

func (s *Stat) AddBase(value int)

AddBase adds the given amount to the base stat.

func (*Stat) GetBase

func (s *Stat) GetBase() int

GetBase retrieves the base value of the stat without any mods applied.

func (*Stat) GetDef

func (s *Stat) GetDef() *StatDef

GetDef retrieves the stat's stat definition.

func (*Stat) GetValue

func (s *Stat) GetValue() int

GetValue returns the current value of the stat with mods applied.

func (*Stat) Reset

func (s *Stat) Reset()

Reset removes all mods.

func (*Stat) SetBase

func (s *Stat) SetBase(value int)

SetBase sets the base value of the stat.

func (*Stat) SetMod

func (s *Stat) SetMod(source any, value int) error

SetMod applies a mod from a given source.

func (*Stat) SubBase

func (s *Stat) SubBase(value int)

SubBase subtracts the given amount from the base stat.

type StatDef

type StatDef struct {
	BaseObject
	// contains filtered or unexported fields
}

StatDef is a definition of a stat that can be applied to a combatant.

func (*StatDef) GetMax

func (d *StatDef) GetMax() int

GetMax is the maximum value of the stat.

func (*StatDef) GetMin

func (d *StatDef) GetMin() int

GetMin is the minimum value of the stat.

func (*StatDef) GetName

func (d *StatDef) GetName() string

func (*StatDef) GetType

func (s *StatDef) GetType() ObjectType

func (*StatDef) String

func (s *StatDef) String() string

type TriggerDef

type TriggerDef struct {
	BaseObject
	// contains filtered or unexported fields
}

TriggerDef is a definition of a custom event that can be triggered by an effect.

func (*TriggerDef) GetName

func (d *TriggerDef) GetName() string

func (*TriggerDef) GetType

func (d *TriggerDef) GetType() ObjectType

func (*TriggerDef) String

func (s *TriggerDef) String() string

Directories

Path Synopsis
lua module

Jump to

Keyboard shortcuts

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