# 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. ```go 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. ```lua 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() ` - Function that returns the current tick. - `getCombatants() <[]Combatant>` - Function that returns a list of all combatants. - `getObject(object ) ` - Function that retrieve an object. - `emitTrigger(name , source )` - Function that emits a trigger event. - `StatDef` - `type ` - Type number of object. - `name ` - Name of the stat definition. - `min ` - Minimum value of the stat. - `max ` - Maximum value of the stat. - `Stat` - `def ` - Stat definition for this stat. - `getBase() ` - Function that returns the current base value of this stat. - `setBase(value )` - Function that sets the base value of this stat. - `addBase(value )` - Function that adds the given value to the base value of this stat. - `subBase(value )` - Function that subtracts the given value to the base value of this stat. - `getValue() ` - Function that returns the current value of this stat with modifications. - `setMod(source , value )` - 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() ` - Function that returns the combatant this stat is applied to. - `Combatant` - `getStat(statDef )` - Retrieve a combatant stat from the stat definition. - `setEffect(effectDef , potency , sourceObject )` - Apply effect to combatant. - `removeEffect(effectDef )` - Remove effect from combatant. Same as calling `setEffect` with zero potency. - `hasEffect(effectDef )` - Return true if combatant has given effect applied. - `setFlag(flag )` - Apply a flag to the combatant. - `hasFlag(flag )` - Return true if combatant has the given flag. - `EffectCtx` - `target ` - The combatant that is the target of the effect. - `source ` - The object that is the source of the effect. This can be nil. - `effect ` - The name of the effect. - `potency ` - The potency of the effect. - `remove()` - Function that removes this effect. Same as calling `combatant.setEffect` with zero potency. - `TickEvent` - `tick ` - The current tick. - `CombatantUpdateEvent` - `combatant ` - The combatant who received the update. - `active ` - Whether or not the combatant is currently active. - `flags ` - The combatant flag value. - `CombatantStatBaseEvent` - `combatant ` - The combatant whose base stat changed. - `statDef ` - The stat definition of the stat that changed. - `value ` - The new value of the stat. - `setValue(value )` - Function that allows the base stat value to be altered as part of the same event. - `CombatantStatModEvent` - `combatant ` - The combatant whose stat has been modified. - `statDef ` - The stat definition of the stat that has been modified. - `value ` - The modification amount. - `setValue(value )` - Function that allows the stat modification value to be altered as part of the same event. - `CombatantEffectEvent` - `target ` - The combatant whom the effect is being applied to. - `effect ` - The name of the effect. - `potency ` - The potency of the effect. If this is zero then the effect is being removed. - `source ` - The source of the effect, can be nil. - `TriggerEvent` - `trigger ` - The name of the trigger. - `source ` - The source of the trigger. *Event handling functions...* - `OnAdd(ctx , effectCtx )` - Called when the effect is applied to a combatant. - `OnRemove(ctx , effectCtx )` - Called when the effect is removed from a combatant. - `OnTick(ctx , effectCtx , evt )` - Called whenever combatant advances a tick. - `OnCombatantUpdate(ctx , effectCtx , evt )` - Called when a new combatant is added or an existing combatant is updated. - `OnCombatantStatBase(ctx , effectCtx , evt )` - Called when a stat applied to a combatant has a base value change. - `OnCombatantStatMod(ctx , effectCtx , evt )` - Called when a stat applied to a combatant has a mod value change. - `OnCombatantEffect(ctx , effectCtx , evt )` - Called when an effect is added or removed from a combatant. - `OnTrigger(ctx , effectCtx , evt )` - Called when an effect emits a trigger event.