Otter
Packs
Otter
Upstream Contract & Behavioral Tests
game-entity Pack
Reusable entity and combat mechanics: Actor, InventoryItem, Cooldown, Inventory, Equipment, damage/healing, leveling, inventory containers, equipment bonuses, and state serialization.
Schema v2 Operation Contract (26 operations)
Scoping Prefix:_ge_
Schema v2 Pack exposing 26 structurally AST-verified Otter 1.0.1 operations with isolated prefix '_ge_'.
| Operation Signature | Returns | Side Effects | Error Mode |
|---|---|---|---|
createActor(name: text, hp: number, lvl: number) |
object |
none | returns-fallback |
createItem(itemName: text, itemKind: text, itemPower: number) |
object |
none | returns-fallback |
createStackableItem(itemName: text, itemKind: text, itemPower: number, countVal: number, maxStackVal: number) |
object |
none | returns-fallback |
applyDamage(actor: object, amount: number) |
number |
state:mutation | returns-fallback |
restoreHealth(actor: object, amount: number) |
number |
state:mutation | returns-fallback |
reviveActor(actor: object, hp: number) |
number |
state:mutation | returns-fallback |
levelUp(actor: object) |
number |
state:mutation | returns-fallback |
gainExperience(actor: object, amount: number, neededPerLevel: number) |
boolean |
state:mutation | returns-false |
createCooldown(name: text, cd: number) |
object |
none | returns-fallback |
isCooldownReady(cooldownObj: object, nowSeconds: number) |
boolean |
none | returns-false |
triggerCooldown(cooldownObj: object, nowSeconds: number) |
boolean |
state:mutation | returns-false |
createInventory(slots: number) |
object |
none | returns-fallback |
getInventoryCount(inv: object) |
number |
none | returns-fallback |
getInventoryCapacity(inv: object) |
number |
none | returns-fallback |
findItemInInventory(inv: object, targetName: text) |
object |
none | returns-gone |
addItemToInventory(inv: object, item: object) |
boolean |
state:mutation | returns-false |
removeItemFromInventory(inv: object, targetName: text) |
boolean |
state:mutation | returns-false |
consumeItemFromInventory(inv: object, targetName: text, amount: number) |
boolean |
state:mutation | returns-false |
createEquipment() |
object |
none | returns-fallback |
equipItem(equipment: object, item: object) |
boolean |
state:mutation | returns-false |
unequipSlot(equipment: object, slotName: text) |
object |
state:mutation | returns-gone |
getEquipmentAttackBonus(equipment: object) |
number |
none | returns-fallback |
getEquipmentDefenseBonus(equipment: object) |
number |
none | returns-fallback |
buildItemRecord(it: object) |
thing |
none | returns-fallback |
serializeGameState(actor: object, inv: object, equip: object) |
thing |
none | returns-fallback |
deserializeGameState(stateData: thing) |
thing |
none | returns-fallback |
Source Code (4 files)
# game-entity - Otter Reusable Game Actor & Combat Library
# Provides Actor, InventoryItem, Cooldown, Inventory, Equipment,
# combat mechanics, leveling, inventory management, and state persistence
# Internal variables use '_ge_' prefix to prevent collisions with caller/outer variables.
a Actor has
name
health
maxHealth
level
isAlive
attackBonus
defenseBonus
experience
.
a InventoryItem has
itemName
itemKind
itemPower
isStackable
itemCount
maxStack
.
a Cooldown has
skillName
cooldownSeconds
lastUsedSeconds
.
a Inventory has
capacity
currentCount
items
.
a Equipment has
weapon
armor
accessory
.
to createActor name hp lvl
_ge_actor is a Actor
name of _ge_actor is name
health of _ge_actor is hp
maxHealth of _ge_actor is hp
level of _ge_actor is lvl
isAlive of _ge_actor is true
attackBonus of _ge_actor is 0
defenseBonus of _ge_actor is 0
experience of _ge_actor is 0
return _ge_actor
.
to createItem itemName itemKind itemPower
_ge_item is a InventoryItem
itemName of _ge_item is itemName
itemKind of _ge_item is itemKind
itemPower of _ge_item is itemPower
isStackable of _ge_item is false
itemCount of _ge_item is 1
maxStack of _ge_item is 1
return _ge_item
.
to createStackableItem itemName itemKind itemPower countVal maxStackVal
_ge_item is a InventoryItem
itemName of _ge_item is itemName
itemKind of _ge_item is itemKind
itemPower of _ge_item is itemPower
isStackable of _ge_item is true
itemCount of _ge_item is countVal
maxStack of _ge_item is maxStackVal
return _ge_item
.
to applyDamage actor amount
if amount is at most 0
return health of actor
.
_ge_cur is health of actor
_ge_newHp is _ge_cur minus amount
if _ge_newHp is at most 0
health of actor is 0
isAlive of actor is false
return 0
.
health of actor is _ge_newHp
return _ge_newHp
.
to restoreHealth actor amount
if isAlive of actor is false
return 0
.
_ge_cur is health of actor
_ge_maxHp is maxHealth of actor
_ge_newHp is _ge_cur plus amount
if _ge_newHp is greater than _ge_maxHp
health of actor is _ge_maxHp
return _ge_maxHp
.
health of actor is _ge_newHp
return _ge_newHp
.
to reviveActor actor hp
if hp is at most 0
return 0
.
isAlive of actor is true
_ge_maxHp is maxHealth of actor
_ge_newHp is hp
if _ge_newHp is greater than _ge_maxHp
_ge_newHp is _ge_maxHp
.
health of actor is _ge_newHp
return _ge_newHp
.
to levelUp actor
_ge_curLvl is level of actor
level of actor is _ge_curLvl plus 1
_ge_curMax is maxHealth of actor
_ge_newMax is _ge_curMax plus 25
maxHealth of actor is _ge_newMax
health of actor is _ge_newMax
attackBonus of actor is attackBonus of actor plus 5
defenseBonus of actor is defenseBonus of actor plus 3
return level of actor
.
to gainExperience actor amount neededPerLevel
if neededPerLevel is at most 0
return false
.
_ge_curExp is experience of actor
experience of actor is _ge_curExp plus amount
_ge_leveled is false
while experience of actor is at least neededPerLevel
levelUp actor
_ge_remExp is experience of actor minus neededPerLevel
experience of actor is _ge_remExp
_ge_leveled is true
.
return _ge_leveled
.
to createCooldown name cd
_ge_cdObj is a Cooldown
skillName of _ge_cdObj is name
cooldownSeconds of _ge_cdObj is cd
lastUsedSeconds of _ge_cdObj is 0 minus 9999
return _ge_cdObj
.
to isCooldownReady cooldownObj nowSeconds
_ge_last is lastUsedSeconds of cooldownObj
_ge_dur is cooldownSeconds of cooldownObj
_ge_elapsed is nowSeconds minus _ge_last
if _ge_elapsed is at least _ge_dur
return true
.
return false
.
to triggerCooldown cooldownObj nowSeconds
_ge_check is isCooldownReady cooldownObj nowSeconds
if _ge_check is true
lastUsedSeconds of cooldownObj is nowSeconds
return true
.
return false
.
to createInventory slots
_ge_inv is a Inventory
capacity of _ge_inv is slots
currentCount of _ge_inv is 0
_ge_empty are empty
items of _ge_inv is _ge_empty
return _ge_inv
.
to getInventoryCount inv
return currentCount of inv
.
to getInventoryCapacity inv
return capacity of inv
.
to findItemInInventory inv targetName
_ge_itemList is items of inv
for each _ge_it in _ge_itemList
if itemName of _ge_it is targetName
return _ge_it
.
.
return gone
.
to addItemToInventory inv item
_ge_curCount is currentCount of inv
_ge_cap is capacity of inv
# Check if stackable with existing item
if isStackable of item is true
_ge_existing is findItemInInventory inv itemName of item
if _ge_existing is not gone
_ge_existingCount is itemCount of _ge_existing
_ge_addCount is itemCount of item
_ge_newCount is _ge_existingCount plus _ge_addCount
_ge_maxLimit is maxStack of _ge_existing
if _ge_newCount is at most _ge_maxLimit
itemCount of _ge_existing is _ge_newCount
return true
.
.
.
# Check capacity limit
if _ge_curCount is at least _ge_cap
return false
.
currentCount of inv is _ge_curCount plus 1
_ge_itemList is items of inv
add item to _ge_itemList
return true
.
to removeItemFromInventory inv targetName
_ge_itemList is items of inv
_ge_newItems are empty
_ge_removed is false
for each _ge_it in _ge_itemList
if not _ge_removed and itemName of _ge_it is targetName
_ge_removed is true
otherwise
add _ge_it to _ge_newItems
.
.
if _ge_removed is true
items of inv is _ge_newItems
currentCount of inv is currentCount of inv minus 1
return true
.
return false
.
to consumeItemFromInventory inv targetName amount
if amount is at most 0
return false
.
_ge_targetItem is findItemInInventory inv targetName
if _ge_targetItem is gone
return false
.
_ge_curQty is itemCount of _ge_targetItem
if _ge_curQty is less than amount
return false
.
_ge_remQty is _ge_curQty minus amount
if _ge_remQty is at most 0
removeItemFromInventory inv targetName
otherwise
itemCount of _ge_targetItem is _ge_remQty
.
return true
.
to createEquipment
_ge_newEq is a Equipment
weapon of _ge_newEq is gone
armor of _ge_newEq is gone
accessory of _ge_newEq is gone
return _ge_newEq
.
to equipItem equipment item
_ge_kind is itemKind of item
if _ge_kind is "weapon"
weapon of equipment is item
return true
.
if _ge_kind is "armor"
armor of equipment is item
return true
.
if _ge_kind is "accessory"
accessory of equipment is item
return true
.
if _ge_kind is "attack_accessory"
accessory of equipment is item
return true
.
if _ge_kind is "defense_accessory"
accessory of equipment is item
return true
.
return false
.
to unequipSlot equipment slotName
if slotName is "weapon"
_ge_prevW is weapon of equipment
weapon of equipment is gone
return _ge_prevW
.
if slotName is "armor"
_ge_prevA is armor of equipment
armor of equipment is gone
return _ge_prevA
.
if slotName is "accessory"
_ge_prevAcc is accessory of equipment
accessory of equipment is gone
return _ge_prevAcc
.
return gone
.
to getEquipmentAttackBonus equipment
_ge_bonus is 0
_ge_w is weapon of equipment
if _ge_w is not gone
_ge_bonus is _ge_bonus plus itemPower of _ge_w
.
_ge_acc is accessory of equipment
if _ge_acc is not gone
_ge_k is itemKind of _ge_acc
if _ge_k is "attack_accessory"
_ge_bonus is _ge_bonus plus itemPower of _ge_acc
otherwise if _ge_k is "accessory"
_ge_bonus is _ge_bonus plus itemPower of _ge_acc
.
.
return _ge_bonus
.
to getEquipmentDefenseBonus equipment
_ge_bonus is 0
_ge_arm is armor of equipment
if _ge_arm is not gone
_ge_bonus is _ge_bonus plus itemPower of _ge_arm
.
_ge_acc is accessory of equipment
if _ge_acc is not gone
_ge_k is itemKind of _ge_acc
if _ge_k is "defense_accessory"
_ge_bonus is _ge_bonus plus itemPower of _ge_acc
.
.
return _ge_bonus
.
to buildItemRecord it
_ge_itemData has
itemName is itemName of it
itemKind is itemKind of it
itemPower is itemPower of it
isStackable is isStackable of it
itemCount is itemCount of it
maxStack is maxStack of it
.
return _ge_itemData
.
to serializeGameState actor inv equip
_ge_rawItems are empty
for each _ge_it in items of inv
_ge_rec is buildItemRecord _ge_it
add _ge_rec to _ge_rawItems
.
_ge_equipData has
hasWeapon is false
hasArmor is false
hasAccessory is false
.
_ge_w is weapon of equip
if _ge_w is not gone
hasWeapon of _ge_equipData is true
weaponName of _ge_equipData is itemName of _ge_w
weaponPower of _ge_equipData is itemPower of _ge_w
.
_ge_arm is armor of equip
if _ge_arm is not gone
hasArmor of _ge_equipData is true
armorName of _ge_equipData is itemName of _ge_arm
armorPower of _ge_equipData is itemPower of _ge_arm
.
_ge_acc is accessory of equip
if _ge_acc is not gone
hasAccessory of _ge_equipData is true
accessoryName of _ge_equipData is itemName of _ge_acc
accessoryKind of _ge_equipData is itemKind of _ge_acc
accessoryPower of _ge_equipData is itemPower of _ge_acc
.
_ge_gameState has
name is name of actor
health is health of actor
maxHealth is maxHealth of actor
level is level of actor
isAlive is isAlive of actor
attackBonus is attackBonus of actor
defenseBonus is defenseBonus of actor
experience is experience of actor
inventoryCapacity is capacity of inv
inventoryCount is currentCount of inv
inventoryItems is _ge_rawItems
equipment is _ge_equipData
.
return _ge_gameState
.
to deserializeGameState stateData
# 1. Reconstruct Actor
_ge_restoredAct is createActor name of stateData health of stateData level of stateData
maxHealth of _ge_restoredAct is maxHealth of stateData
isAlive of _ge_restoredAct is isAlive of stateData
attackBonus of _ge_restoredAct is attackBonus of stateData
defenseBonus of _ge_restoredAct is defenseBonus of stateData
experience of _ge_restoredAct is experience of stateData
# 2. Reconstruct Inventory
_ge_restoredInv is createInventory inventoryCapacity of stateData
for each _ge_rawIt in inventoryItems of stateData
_ge_it is createItem itemName of _ge_rawIt itemKind of _ge_rawIt itemPower of _ge_rawIt
isStackable of _ge_it is isStackable of _ge_rawIt
itemCount of _ge_it is itemCount of _ge_rawIt
maxStack of _ge_it is maxStack of _ge_rawIt
addItemToInventory _ge_restoredInv _ge_it
.
# 3. Reconstruct Equipment
_ge_restoredEq is createEquipment
_ge_eqData is equipment of stateData
if hasWeapon of _ge_eqData is true
_ge_wItem is createItem weaponName of _ge_eqData "weapon" weaponPower of _ge_eqData
equipItem _ge_restoredEq _ge_wItem
.
if hasArmor of _ge_eqData is true
_ge_aItem is createItem armorName of _ge_eqData "armor" armorPower of _ge_eqData
equipItem _ge_restoredEq _ge_aItem
.
if hasAccessory of _ge_eqData is true
_ge_accObj is createItem accessoryName of _ge_eqData accessoryKind of _ge_eqData accessoryPower of _ge_eqData
equipItem _ge_restoredEq _ge_accObj
.
_ge_bundle has
actor is _ge_restoredAct
inventory is _ge_restoredInv
equipment is _ge_restoredEq
.
return _ge_bundle
.
{
"schemaVersion": 2,
"name": "game-entity",
"publisher": "otter-official",
"version": "1.1.0",
"kind": "pack",
"description": "Reusable entity and combat mechanics: Actor, InventoryItem, Cooldown, Inventory, Equipment, damage/healing, leveling, inventory containers, equipment bonuses, and state serialization.",
"entry": "src/main.ot",
"license": "MIT",
"requiresOtter": ">=1.0.1",
"platforms": [
"console",
"desktop",
"web"
],
"variablePrefix": "_ge_",
"provides": {
"capabilities": [
"game.entity",
"game.combat",
"game.inventory",
"game.equipment",
"game.serialization"
],
"types": [
{
"name": "Actor",
"fields": [
{ "name": "name", "type": "text" },
{ "name": "health", "type": "number" },
{ "name": "maxHealth", "type": "number" },
{ "name": "level", "type": "number" },
{ "name": "isAlive", "type": "boolean" },
{ "name": "attackBonus", "type": "number" },
{ "name": "defenseBonus", "type": "number" },
{ "name": "experience", "type": "number" }
]
},
{
"name": "InventoryItem",
"fields": [
{ "name": "itemName", "type": "text" },
{ "name": "itemKind", "type": "text" },
{ "name": "itemPower", "type": "number" },
{ "name": "isStackable", "type": "boolean" },
{ "name": "itemCount", "type": "number" },
{ "name": "maxStack", "type": "number" }
]
},
{
"name": "Cooldown",
"fields": [
{ "name": "skillName", "type": "text" },
{ "name": "cooldownSeconds", "type": "number" },
{ "name": "lastUsedSeconds", "type": "number" }
]
},
{
"name": "Inventory",
"fields": [
{ "name": "capacity", "type": "number" },
{ "name": "currentCount", "type": "number" },
{ "name": "items", "type": "list" }
]
},
{
"name": "Equipment",
"fields": [
{ "name": "weapon", "type": "object" },
{ "name": "armor", "type": "object" },
{ "name": "accessory", "type": "object" }
]
}
],
"operations": [
{
"name": "createActor",
"description": "Creates a new living Actor with the given name, starting/max health, and level.",
"parameters": [
{ "name": "name", "type": "text" },
{ "name": "hp", "type": "number" },
{ "name": "lvl", "type": "number" }
],
"returns": { "type": "object", "objectType": "Actor" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "createItem",
"description": "Creates a non-stackable InventoryItem with name, kind, and power.",
"parameters": [
{ "name": "itemName", "type": "text" },
{ "name": "itemKind", "type": "text" },
{ "name": "itemPower", "type": "number" }
],
"returns": { "type": "object", "objectType": "InventoryItem" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "createStackableItem",
"description": "Creates a stackable InventoryItem with initial stack count and maximum stack size.",
"parameters": [
{ "name": "itemName", "type": "text" },
{ "name": "itemKind", "type": "text" },
{ "name": "itemPower", "type": "number" },
{ "name": "countVal", "type": "number" },
{ "name": "maxStackVal", "type": "number" }
],
"returns": { "type": "object", "objectType": "InventoryItem" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "applyDamage",
"description": "Applies positive damage to an Actor, clamping health at 0 and setting isAlive to false on defeat.",
"parameters": [
{ "name": "actor", "type": "object", "objectType": "Actor" },
{ "name": "amount", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["state:mutation"],
"errorMode": "returns-fallback"
},
{
"name": "restoreHealth",
"description": "Heals a living Actor up to maxHealth; returns 0 without reviving if Actor is dead.",
"parameters": [
{ "name": "actor", "type": "object", "objectType": "Actor" },
{ "name": "amount", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["state:mutation"],
"errorMode": "returns-fallback"
},
{
"name": "reviveActor",
"description": "Revives an Actor with positive hp capped at maxHealth; returns 0 if hp is at most 0.",
"parameters": [
{ "name": "actor", "type": "object", "objectType": "Actor" },
{ "name": "hp", "type": "number" }
],
"returns": { "type": "number" },
"sideEffects": ["state:mutation"],
"errorMode": "returns-fallback"
},
{
"name": "levelUp",
"description": "Increments Actor level, boosts maxHealth/attack/defense, and refills health.",
"parameters": [
{ "name": "actor", "type": "object", "objectType": "Actor" }
],
"returns": { "type": "number" },
"sideEffects": ["state:mutation"],
"errorMode": "returns-fallback"
},
{
"name": "gainExperience",
"description": "Adds experience to an Actor and triggers one or more level-ups when thresholds are reached.",
"parameters": [
{ "name": "actor", "type": "object", "objectType": "Actor" },
{ "name": "amount", "type": "number" },
{ "name": "neededPerLevel", "type": "number" }
],
"returns": { "type": "boolean" },
"sideEffects": ["state:mutation"],
"errorMode": "returns-false"
},
{
"name": "createCooldown",
"description": "Creates a ready-to-use skill Cooldown tracker with duration cd seconds.",
"parameters": [
{ "name": "name", "type": "text" },
{ "name": "cd", "type": "number" }
],
"returns": { "type": "object", "objectType": "Cooldown" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "isCooldownReady",
"description": "Returns true if cooldownObj has elapsed at nowSeconds.",
"parameters": [
{ "name": "cooldownObj", "type": "object", "objectType": "Cooldown" },
{ "name": "nowSeconds", "type": "number" }
],
"returns": { "type": "boolean" },
"sideEffects": ["none"],
"errorMode": "returns-false"
},
{
"name": "triggerCooldown",
"description": "Activates cooldownObj at nowSeconds if ready, returning true on success or false if still cooling.",
"parameters": [
{ "name": "cooldownObj", "type": "object", "objectType": "Cooldown" },
{ "name": "nowSeconds", "type": "number" }
],
"returns": { "type": "boolean" },
"sideEffects": ["state:mutation"],
"errorMode": "returns-false"
},
{
"name": "createInventory",
"description": "Creates an empty Inventory container with the specified slot capacity.",
"parameters": [
{ "name": "slots", "type": "number" }
],
"returns": { "type": "object", "objectType": "Inventory" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "getInventoryCount",
"description": "Returns the number of currently occupied slots in inv.",
"parameters": [
{ "name": "inv", "type": "object", "objectType": "Inventory" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "getInventoryCapacity",
"description": "Returns the maximum slot capacity of inv.",
"parameters": [
{ "name": "inv", "type": "object", "objectType": "Inventory" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "findItemInInventory",
"description": "Finds an InventoryItem by name in inv, or returns gone if absent.",
"parameters": [
{ "name": "inv", "type": "object", "objectType": "Inventory" },
{ "name": "targetName", "type": "text" }
],
"returns": { "type": "object", "objectType": "InventoryItem", "nullable": true },
"sideEffects": ["none"],
"errorMode": "returns-gone"
},
{
"name": "addItemToInventory",
"description": "Adds an item to inv (merging into an existing stack if possible), returning false if capacity is full.",
"parameters": [
{ "name": "inv", "type": "object", "objectType": "Inventory" },
{ "name": "item", "type": "object", "objectType": "InventoryItem" }
],
"returns": { "type": "boolean" },
"sideEffects": ["state:mutation"],
"errorMode": "returns-false"
},
{
"name": "removeItemFromInventory",
"description": "Removes the first matching item named targetName from inv, returning true if removed.",
"parameters": [
{ "name": "inv", "type": "object", "objectType": "Inventory" },
{ "name": "targetName", "type": "text" }
],
"returns": { "type": "boolean" },
"sideEffects": ["state:mutation"],
"errorMode": "returns-false"
},
{
"name": "consumeItemFromInventory",
"description": "Consumes amount units of targetName from inv, removing the slot if depleted.",
"parameters": [
{ "name": "inv", "type": "object", "objectType": "Inventory" },
{ "name": "targetName", "type": "text" },
{ "name": "amount", "type": "number" }
],
"returns": { "type": "boolean" },
"sideEffects": ["state:mutation"],
"errorMode": "returns-false"
},
{
"name": "createEquipment",
"description": "Creates an Equipment loadout with empty weapon, armor, and accessory slots.",
"parameters": [],
"returns": { "type": "object", "objectType": "Equipment" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "equipItem",
"description": "Equips an InventoryItem into its matching equipment slot, returning false for unsupported kinds.",
"parameters": [
{ "name": "equipment", "type": "object", "objectType": "Equipment" },
{ "name": "item", "type": "object", "objectType": "InventoryItem" }
],
"returns": { "type": "boolean" },
"sideEffects": ["state:mutation"],
"errorMode": "returns-false"
},
{
"name": "unequipSlot",
"description": "Clears the named equipment slot and returns the previously equipped item or gone.",
"parameters": [
{ "name": "equipment", "type": "object", "objectType": "Equipment" },
{ "name": "slotName", "type": "text" }
],
"returns": { "type": "object", "objectType": "InventoryItem", "nullable": true },
"sideEffects": ["state:mutation"],
"errorMode": "returns-gone"
},
{
"name": "getEquipmentAttackBonus",
"description": "Computes total attack power bonus from equipped weapon and attack accessories.",
"parameters": [
{ "name": "equipment", "type": "object", "objectType": "Equipment" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "getEquipmentDefenseBonus",
"description": "Computes total defense power bonus from equipped armor and defense accessories.",
"parameters": [
{ "name": "equipment", "type": "object", "objectType": "Equipment" }
],
"returns": { "type": "number" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "buildItemRecord",
"description": "Converts an InventoryItem into a JSON-serializable thing record.",
"parameters": [
{ "name": "it", "type": "object", "objectType": "InventoryItem" }
],
"returns": { "type": "thing" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "serializeGameState",
"description": "Serializes Actor, Inventory, and Equipment state into a JSON-compatible thing snapshot.",
"parameters": [
{ "name": "actor", "type": "object", "objectType": "Actor" },
{ "name": "inv", "type": "object", "objectType": "Inventory" },
{ "name": "equip", "type": "object", "objectType": "Equipment" }
],
"returns": { "type": "thing" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "deserializeGameState",
"description": "Reconstructs Actor, Inventory, and Equipment objects from a serialized stateData thing.",
"parameters": [
{ "name": "stateData", "type": "thing" }
],
"returns": { "type": "thing" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
}
]
},
"requires": {
"capabilities": [],
"packages": {}
},
"capabilities": [],
"dependencies": {},
"tags": [
"game",
"rpg",
"entity",
"actor",
"health",
"combat",
"damage",
"level",
"inventory",
"equipment"
],
"examples": [
"examples/demo.ot"
]
}
# Tests for game-entity (Strict Behavioral Assertions)
use "../src/main.ot"
say "Running game-entity behavioral tests..."
# 1. Actor creation
hero is createActor "Sir Galahad" 100 1
if name of hero is not "Sir Galahad"
fail with "createActor failed: name mismatch"
.
if health of hero is not 100
fail with "createActor failed: health mismatch"
.
if isAlive of hero is not true
fail with "createActor failed: expected isAlive to be true"
.
# 2. Damage & Death State
applyDamage hero 40
if health of hero is not 60
fail with "applyDamage failed: expected 60 HP"
.
applyDamage hero 70
if health of hero is not 0
fail with "applyDamage death failed: expected 0 HP"
.
if isAlive of hero is not false
fail with "applyDamage death failed: expected isAlive to be false"
.
# 3. Dead Actor cannot be healed without revive
healedDead is restoreHealth hero 50
if healedDead is not 0
fail with "restoreHealth failed: dead actor was healed"
.
if isAlive of hero is not false
fail with "dead actor was unexpectedly revived by restoreHealth"
.
# 4. Revive Actor & Healing Cap
reviveActor hero 20
if isAlive of hero is not true
fail with "reviveActor failed: expected isAlive to be true"
.
if health of hero is not 20
fail with "reviveActor failed: expected 20 HP"
.
restoreHealth hero 200
if health of hero is not 100
fail with "restoreHealth cap failed: health exceeded maxHealth 100"
.
# 5. Experience & Leveling
leveledUp is gainExperience hero 150 100
if leveledUp is not true
fail with "gainExperience failed: expected level-up to trigger"
.
if level of hero is not 2
fail with "levelUp failed: expected level 2"
.
if maxHealth of hero is not 125
fail with "levelUp failed: expected maxHealth 125"
.
if attackBonus of hero is not 5
fail with "levelUp failed: expected attackBonus 5"
.
if defenseBonus of hero is not 3
fail with "levelUp failed: expected defenseBonus 3"
.
if experience of hero is not 50
fail with "gainExperience failed: expected 50 remaining experience"
.
# 6. Cooldowns
slashCd is createCooldown "Slash" 3
if isCooldownReady slashCd 10 is not true
fail with "isCooldownReady initial check failed"
.
triggerCooldown slashCd 10
if isCooldownReady slashCd 11 is not false
fail with "isCooldownReady check after 1s failed: should be cooling down"
.
if isCooldownReady slashCd 13 is not true
fail with "isCooldownReady check after 3s failed: should be ready"
.
# 7. Inventory & Capacity
bag is createInventory 2
potion1 is createItem "Health Potion" "consumable" 25
potion2 is createItem "Mana Potion" "consumable" 15
swordItem is createItem "Broadsword" "weapon" 12
ok1 is addItemToInventory bag potion1
if ok1 is not true
fail with "addItemToInventory 1 failed"
.
ok2 is addItemToInventory bag potion2
if ok2 is not true
fail with "addItemToInventory 2 failed"
.
# Exceeds capacity
ok3 is addItemToInventory bag swordItem
if ok3 is not false
fail with "addItemToInventory should fail when capacity is full"
.
if getInventoryCount bag is not 2
fail with "getInventoryCount mismatch"
.
# 8. Find and Remove Items
foundPot is findItemInInventory bag "Health Potion"
if foundPot is gone
fail with "findItemInInventory failed to find item"
.
remOk is removeItemFromInventory bag "Health Potion"
if remOk is not true
fail with "removeItemFromInventory failed to remove item"
.
if getInventoryCount bag is not 1
fail with "getInventoryCount mismatch after removal"
.
missingPot is findItemInInventory bag "Health Potion"
if missingPot is not gone
fail with "removed item is still found in inventory"
.
# 9. Stackable Items
arrow1 is createStackableItem "Iron Arrow" "ammo" 1 10 30
arrow2 is createStackableItem "Iron Arrow" "ammo" 1 15 30
addItemToInventory bag arrow1
addItemToInventory bag arrow2
stackedArrow is findItemInInventory bag "Iron Arrow"
if itemCount of stackedArrow is not 25
fail with "stackable item failed: expected stacked count 25"
.
# 10. Equipment System & Stat Bonuses
equip is createEquipment
weaponItem is createItem "Excalibur" "weapon" 20
armorItem is createItem "Dragon Plate" "armor" 15
equipItem equip weaponItem
equipItem equip armorItem
atkBonus is getEquipmentAttackBonus equip
if atkBonus is not 20
fail with "getEquipmentAttackBonus failed: expected 20"
.
defBonus is getEquipmentDefenseBonus equip
if defBonus is not 15
fail with "getEquipmentDefenseBonus failed: expected 15"
.
# 11. State Serialization & Deserialization
savedState is serializeGameState hero bag equip
if name of savedState is not "Sir Galahad"
fail with "serializeGameState failed: name mismatch"
.
# Restore into fresh objects
restoredBundle is deserializeGameState savedState
restoredHero is actor of restoredBundle
restoredBag is inventory of restoredBundle
restoredEquip is equipment of restoredBundle
if name of restoredHero is not "Sir Galahad"
fail with "deserializeGameState failed: restored name mismatch"
.
if level of restoredHero is not 2
fail with "deserializeGameState failed: restored level mismatch"
.
if getEquipmentAttackBonus restoredEquip is not 20
fail with "deserializeGameState failed: restored equipment attack mismatch"
.
if getEquipmentDefenseBonus restoredEquip is not 15
fail with "deserializeGameState failed: restored equipment defense mismatch"
.
# 12. Bug 5 Test: reviveActor with 0 HP must not revive actor
fallen is createActor "Fallen Knight" 50 1
applyDamage fallen 50
if isAlive of fallen is not false
fail with "fallen actor expected dead"
.
reviveZeroRes is reviveActor fallen 0
if reviveZeroRes is not 0
fail with "reviveActor with 0 HP must return 0"
.
if isAlive of fallen is not false
fail with "actor must remain dead when revived with 0 HP"
.
if health of fallen is not 0
fail with "dead actor HP must remain 0"
.
# 13. Bug 4 Test: Multi-leveling in gainExperience (500 XP at 100/level -> level 6)
novice is createActor "Novice Mage" 60 1
multiLvlOk is gainExperience novice 500 100
if multiLvlOk is not true
fail with "gainExperience failed to trigger multi-leveling"
.
if level of novice is not 6
fail with "multi-leveling expected level 6 from 500 XP at 100/level"
.
if experience of novice is not 0
fail with "multi-leveling expected 0 leftover experience"
.
# 14. Bug 2 & 3 Test: Accessory equipping, attack/defense bonuses, and savegame persistence
accEquip is createEquipment
fireRing is createItem "Ring of Fire" "attack_accessory" 12
wardPendant is createItem "Pendant of Warding" "defense_accessory" 8
equipFireOk is equipItem accEquip fireRing
if equipFireOk is not true
fail with "failed to equip attack_accessory into accessory slot"
.
atkWithAcc is getEquipmentAttackBonus accEquip
if atkWithAcc is not 12
fail with "attack_accessory bonus failed: expected 12"
.
equipWardOk is equipItem accEquip wardPendant
if equipWardOk is not true
fail with "failed to equip defense_accessory into accessory slot"
.
defWithAcc is getEquipmentDefenseBonus accEquip
if defWithAcc is not 8
fail with "defense_accessory bonus failed: expected 8"
.
# Save and restore accessory slot
accSavedState is serializeGameState novice bag accEquip
accRestoredBundle is deserializeGameState accSavedState
accRestoredEquip is equipment of accRestoredBundle
restoredDefAcc is getEquipmentDefenseBonus accRestoredEquip
if restoredDefAcc is not 8
fail with "restored accessory defense bonus mismatch: expected 8"
.
restoredAccItem is accessory of accRestoredEquip
if itemName of restoredAccItem is not "Pendant of Warding"
fail with "restored accessory itemName mismatch: expected Pendant of Warding"
.
if itemKind of restoredAccItem is not "defense_accessory"
fail with "restored accessory itemKind mismatch: expected defense_accessory"
.
# 15. Bug 1 Test: Runtime Scoping & Variable Clash Safety
# Callers can have variables named b, inv, eq, item without clashing with library internal variables
inv is "caller_inv_string"
eq is "caller_eq_string"
item is "caller_item_string"
b is createInventory 3
c is createInventory 4
if inv is not "caller_inv_string"
fail with "caller inv variable was corrupted by library call"
.
if eq is not "caller_eq_string"
fail with "caller eq variable was corrupted by library call"
.
if item is not "caller_item_string"
fail with "caller item variable was corrupted by library call"
.
if capacity of b is not 3
fail with "first created inventory capacity mismatch"
.
if capacity of c is not 4
fail with "second created inventory capacity mismatch"
.
say "All game-entity behavioral tests passed successfully!"
# game-entity Demo
use "../src/main.ot"
say "=== game-entity Demo ==="
player is createActor "Paladin" 120 1
say "Hero created:" name of player "HP:" health of player "/" maxHealth of player
bag is createInventory 6
potion is createItem "Elixir of Life" "consumable" 50
sword is createItem "Mithril Sword" "weapon" 25
shield is createItem "Kite Shield" "armor" 15
arrows is createStackableItem "Fire Arrow" "ammo" 5 10 20
addItemToInventory bag potion
addItemToInventory bag sword
addItemToInventory bag shield
addItemToInventory bag arrows
say "Inventory items count:" getInventoryCount bag
equip is createEquipment
equipItem equip sword
equipItem equip shield
say "Equipment Attack Bonus: +" getEquipmentAttackBonus equip
say "Equipment Defense Bonus: +" getEquipmentDefenseBonus equip
say "Engaging in combat..."
applyDamage player 35
say "HP after combat damage:" health of player "/" maxHealth of player
restoreHealth player 20
say "HP after recovery:" health of player "/" maxHealth of player
say "Awarding 120 experience..."
gainExperience player 120 100
say "Hero is now Level" level of player "with Max HP:" maxHealth of player
say "Saving full character and inventory state..."
savedState is serializeGameState player bag equip
say "State serialized for:" name of savedState "Level:" level of savedState
bundle is deserializeGameState savedState
heroRestored is actor of bundle
say "Restored hero name:" name of heroRestored "Level:" level of heroRestored
Installation & Usage
otter pack install otter-official/game-entity
otter pack verify --contracts
use ".otter/packages/otter-official/game-entity/1.1.0/src/main.ot" # Inspect exported operation contracts: otter pack contract otter-official/game-entity