gamesheet

package
v0.0.0-...-127c1ca Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: Apache-2.0 Imports: 2 Imported by: 2

README

gamesheet: Simple agent character sheet

This package defines a struct that can be used to store and manage a character's status and attributes.

I try to be as conservative as possible with the size of the struct to allow for a lot of instances without filling up the memory.

Current scope

  • Leveling / XP requirement calculations
  • Skill point generation
  • AP / HP leveling
  • AP / HP regeneration
  • Status (hunger, thirst, exhaustion, stress)
    • Rudimentary support
    • Increase over time (on tick)
  • Handle death (through exhaustion, injury)

Planned

  • Provide means to reduce status values (hunger, thirst, etc.)
  • Handle injury (causes stress and damage)
  • Adjust limits of Status values based on resilience, etc.

TODO

States

Add a "state", which will influence the growth factor for each status. This could be a struct that can be defined externally, which would allow for a lot of flexibility wherever this is being used. New states like "swimming" or "flying" could be added easily without the need to modify the gamesheet package.

Instead of making the "rate" part of the Status struct, the rate could be part of the state struct, and instead of individually changing each status's rate, we simply refer to the respective property of the state.

Alternative, state could be a number of states, which are summed up to the rates of each status.

For example:

StatusAwake:  (exhaustion:  0.5, hunger: 0.05,  thirst: 0.1)
StatusAsleep: (exhaustion: -1.0, hunger: 0.025, thirst: 0.05, stress: -0.5)
StatusAfraid: (exhaustion:  0.1, stress: 0.2)
StatusRunning:(exhaustion:  0.5, hunger: 0.01,  thirst: 0.1,  stress: 0.1)
StatusCombat: (exhaustion:  0.5, hunger: 0.1,   thirst: 0.2,  stress: 0.1)

So, if the state is []{StatusAwake, StatusAfraid}, the resulting rates would be:

exhaustion:  0.5 + 0.1   = 0.6
hunger:      0.05        = 0.05
thirst:      0.1         = 0.1
stress:      0.2         = 0.2

If the state is []{StatusAsleep}, the resulting rates would be:

exhaustion: -1.0
hunger:      0.025
thirst:      0.05
stress:     -0.5
State duration.

Some states should only be active for a set duration or have an exit condition. Sleeping, for example, should end when we are fully rested or after a set number of time.

Should this be handled by the AI/Player?

Documentation

Overview

Package gamesheet provides a minimal character sheet for agents, or players.

Index

Constants

This section is empty.

Variables

View Source
var (
	StateAwake = &State{
		Name:       "awake",
		Exhaustion: 100.0 / (4 * dayToSecond),
		Hunger:     100.0 / (10 * dayToSecond),
		Thirst:     100.0 / (3 * dayToSecond),
		Stress:     100.0 / (2 * dayToSecond),
	}
	StateAsleep = &State{
		Name:       "asleep",
		Exhaustion: -100.0 / (8 * hourToSecond),
		Hunger:     100.0 / (20 * dayToSecond),
		Thirst:     100.0 / (20 * dayToSecond),
		Stress:     -100.0 / (8 * hourToSecond),
	}
)

Some constants related to stats.

TODO: This should be on a per-creature basis. - A camel needs less water than a human. - A humpback whale survives 6 MONTHS without food!

TODO: There should also be the recovery rate.

  • After 7 hours of sleep, the exhaustion stat should be reduced by a day's worth of exhaustion.
  • One hour of rest should reduce stress significantly.
  • While sleeping, hunger and thirst should increase much slower.
  • During strenuous activity, hunger, thirst, and exhaustion should increase much faster.
  • When in combat and in danger, stress should increase.

Functions

This section is empty.

Types

type Attribute

type Attribute byte

Attribute represents a character attribute.

func (*Attribute) Add

func (a *Attribute) Add(val int)

Add adds the given value to the attribute.

type CharacterSheet

type CharacterSheet struct {
	CurrentXP   uint16 // Collected XP for the current level.
	Level       byte   // Current level.
	SkillPoints byte   // Skill points to distribute.
	BaseHP      byte   // Level 0 HP, will be used to calculate leveled HP.
	BaseAP      byte   // Level 0 AP, will be used to calculate leveled AP.
	HP          Slider // Hit points.
	AP          Slider // Action points.
	Dead        bool   // Is the character dead?

	// Active states.
	States []*State

	// Physical stats.
	StatExhaustion Status
	StatHunger     Status
	StatThirst     Status
	StatStress     Status

	// Physical attributes.
	AttrStrength     Attribute
	AttrIntelligence Attribute
	AttrDexterity    Attribute
	AttrResilience   Attribute

	Messages []string // Messages to display.
	// contains filtered or unexported fields
}

CharacterSheet represents a character sheet.

TODO:

  • Add conditions like poisoned, blinded, etc.
  • Find a better way to handle max level (100).
  • Handle stats.

NOTE TO SELF: Do we allow the attribute values to change? If so, will retroactively the HP and AP increase or decrease? Would that even matter?

func New

func New(baseHP, baseAP, level, str, itl, dex, res byte) *CharacterSheet

New returns a new character sheet with the given base HP and AP.

NOTE: The base HP and AP are the unleveled values. Depending on the character's stats, the HP and AP will increase as the character levels up or if a starting level > 0 has been set.

func (*CharacterSheet) AddExperience

func (c *CharacterSheet) AddExperience(xp uint16)

AddExperience adds experience to the character sheet.

func (*CharacterSheet) Heal

func (c *CharacterSheet) Heal(hp int)

Heal heals the character for the given amount of hit points. NOTE: This is only for experimentation and will be removed or refactored

func (*CharacterSheet) Log

func (c *CharacterSheet) Log()

func (*CharacterSheet) NextLevelXP

func (c *CharacterSheet) NextLevelXP() uint16

NextLevelXP returns the XP required for the next level.

NOTE: The required XP increases exponentially up to about 65100 XP for level 100. This way we make the best use of the unsigned 16 bit integer.

Of course we could make the formula a bit more complicated so it doesn't irritate our more math-savvy players.

See: https://pavcreations.com/level-systems-and-character-growth-in-rpg-games/

func (*CharacterSheet) RestoreAP

func (c *CharacterSheet) RestoreAP(ap int)

RestoreAP restores the given amount of action points to the AP. NOTE: This is only for experimentation and will be removed or refactored

func (*CharacterSheet) SetState

func (c *CharacterSheet) SetState(state *State)

SetState sets the states to this single state. NOTE: This is only for experimentation and will be removed or refactored

func (*CharacterSheet) SetStates

func (c *CharacterSheet) SetStates(states []*State)

SetStates applies the given states to the statuses.

func (*CharacterSheet) TakeAction

func (c *CharacterSheet) TakeAction(ap int) bool

TakeAction deducts the given action points from the APs and returns true on success. NOTE: This is only for experimentation and will be removed or refactored

func (*CharacterSheet) TakeDamage

func (c *CharacterSheet) TakeDamage(hp int) bool

TakeDamage removes the given amount of hit points from the HP. Return true if the character is dead. NOTE: This is only for experimentation and will be removed or refactored.

func (*CharacterSheet) Tick

func (c *CharacterSheet) Tick(delta float64)

Advance the simulation by a step.

func (*CharacterSheet) Update

func (c *CharacterSheet) Update()

Update updates the character sheet.

func (*CharacterSheet) UpdatePoints

func (c *CharacterSheet) UpdatePoints()

UpdatePoints recalculates stats like HP and AP based on the current level and attributes.

Call this function if any of the attributes change to update the stats.

func (*CharacterSheet) UpdateStates

func (c *CharacterSheet) UpdateStates()

type Slider

type Slider [2]uint16

Slider represents a variable value with a variable upper bound. Example: Health, mana, etc.

func NewSlider

func NewSlider(max uint16) Slider

NewSlider returns a new slider with the given value and maximum.

func (*Slider) Add

func (s *Slider) Add(val int)

Add adds the given value to the slider.

func (*Slider) Max

func (s *Slider) Max() uint16

Max returns the slider's maximum value.

func (*Slider) SetMax

func (s *Slider) SetMax(val uint16)

SetMax sets the slider's maximum value.

func (*Slider) SetValue

func (s *Slider) SetValue(val int)

SetValue sets the slider's value.

func (*Slider) Value

func (s *Slider) Value() uint16

Value returns the slider's value.

type State

type State struct {
	Name       string
	Exhaustion float32
	Hunger     float32
	Thirst     float32
	Stress     float32
}

State represents a specific physical state (awake, asleep, ...)

type Status

type Status struct {
	Val  float32 // Current value.
	Rate float32 // Increase / decrease per second.
}

Status is a character sheet status value that changes at a set rate. TODO: Reduce the memory footprint of this struct.

func NewStatus

func NewStatus() Status

NewStatus returns a new status with the given rate and limit.

func (*Status) Add

func (s *Status) Add(val float32)

Add adds the given value to the status.

func (*Status) Max

func (s *Status) Max() bool

Max returns if the value has reached its upper limit.

func (*Status) Tick

func (s *Status) Tick(delta float64) bool

Tick advances the stat simulation by 'delta' (fraction of seconds) and returns true if it reaches the set limit.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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