gamecs

package
v0.0.0-...-ce97658 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2024 License: Apache-2.0 Imports: 15 Imported by: 1

README

Practical use of different AI techniques to control agents (WIP)

This package currently only uses a minimal state machine setup.

Principle

I'll write something here some day when this is done.

IOU

Proper documentation whenever this is finished.

Pixel People!

alt text

TODO

  • Add in-game calendar
    • configurable time step
    • day / night cycle
  • AI
    • Refactor states
    • Add time based schedules
    • Proper pathfinding
  • Memory usage
    • Ensure flyweight implementation
    • AI logic should be shared between agents

Documentation

Index

Constants

View Source
const StateTypeAttack aistate.StateType = 2
View Source
const StateTypeFind aistate.StateType = 0
View Source
const StateTypeFlee aistate.StateType = 1
View Source
const StateTypeIdle aistate.StateType = 7
View Source
const StateTypeMunch aistate.StateType = 3
View Source
const StateTypeProfession aistate.StateType = 6
View Source
const StateTypeRest aistate.StateType = 4
View Source
const StateTypeStore aistate.StateType = 5

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionAttack

type ActionAttack struct {
	TargetFunc func() *Agent
	// contains filtered or unexported fields
}

func (*ActionAttack) Tick

func (l *ActionAttack) Tick() aitree.State

type ActionConsumeItem

type ActionConsumeItem struct {
	ItemFunc func() *Item
	// contains filtered or unexported fields
}

func (*ActionConsumeItem) Tick

func (l *ActionConsumeItem) Tick() aitree.State

type ActionIsTrue

type ActionIsTrue struct {
	Eval func() bool
	// contains filtered or unexported fields
}

func (*ActionIsTrue) Tick

func (l *ActionIsTrue) Tick() aitree.State

type ActionMoveTo

type ActionMoveTo struct {
	FailFunc func() bool
	PosFunc  func() vectors.Vec2
	// contains filtered or unexported fields
}

func (*ActionMoveTo) Tick

func (l *ActionMoveTo) Tick() aitree.State

type ActionPickUpItem

type ActionPickUpItem struct {
	ItemFunc func() *Item
	// contains filtered or unexported fields
}

func (*ActionPickUpItem) Tick

func (l *ActionPickUpItem) Tick() aitree.State

type ActionTransferItems

type ActionTransferItems struct {
	TargetFunc func() *CompInventory
	// contains filtered or unexported fields
}

func (*ActionTransferItems) Tick

func (l *ActionTransferItems) Tick() aitree.State

type ActionWander

type ActionWander struct {
	EndCondition func() bool
	// contains filtered or unexported fields
}

func (*ActionWander) Tick

func (l *ActionWander) Tick() aitree.State

type Agent

type Agent struct {
	*CompMovable
	*CompStatus
	*CompInventory
	*CompAi
	*Profession
	// contains filtered or unexported fields
}

Agent is an independent entity in the world.

func (*Agent) ID

func (c *Agent) ID() int

ID returns the unique identifier for this Agent.

func (*Agent) Injure

func (c *Agent) Injure(amount, srcID int)

Injure causes damage to the agent from a given source.

func (*Agent) SetProfession

func (c *Agent) SetProfession(w *World, p *ProfessionType)

SetProfession assigns a profession to the agent. NOTE: This is just for experimentation and will probably be refactored into a more generic function that allows the extension of the AI.

func (*Agent) Update

func (c *Agent) Update(delta float64)

Update updates the state of the agent.

type CAiMemory

type CAiMemory struct {
	Locations map[string]*Location
	Positions map[string]vectors.Vec2
	// contains filtered or unexported fields
}

func (*CAiMemory) GetLocation

func (c *CAiMemory) GetLocation(tag string) *Location

func (*CAiMemory) GetPosition

func (c *CAiMemory) GetPosition(tag string) vectors.Vec2

func (*CAiMemory) SetLocation

func (c *CAiMemory) SetLocation(tag string, pos *Location)

func (*CAiMemory) SetPosition

func (c *CAiMemory) SetPosition(tag string, pos vectors.Vec2)

func (*CAiMemory) Update

func (c *CAiMemory) Update(delta float64)

Update ticks the AI memory by delta.

type CAiPath

type CAiPath struct {
	Waypoints       []vectors.Vec2 // Current list of waypoints.
	WaypointCurrent int            // Current index in the waypoints array.
	Target          vectors.Vec2   // Our current target.
	// contains filtered or unexported fields
}

CAiPath is a path planning component.

func (*CAiPath) SetTarget

func (c *CAiPath) SetTarget(t vectors.Vec2)

SetTarget resets the current waypoints and sets the new target to move towards.

func (*CAiPath) Update

func (c *CAiPath) Update(m *CompMovable, delta float64)

Update ticks the AI path planner by delta.

type CAiPerception

type CAiPerception struct {
	Entities []*Agent
	Items    []*Item
	// contains filtered or unexported fields
}

func (*CAiPerception) CanSee

func (c *CAiPerception) CanSee(it *Item) bool

CanSee returns true if we can see the given item. TODO: Deduplicate with CanSeeEntity.

func (*CAiPerception) CanSeeEntity

func (c *CAiPerception) CanSeeEntity(it *Agent) bool

CanSeeEntity returns true if we can percieve the given entity (Agent). TODO: Deduplicate with CanSee.

func (*CAiPerception) Update

func (c *CAiPerception) Update(m *CompMovable, delta float64)

Update updates the list of visible items / entities.

type CAiScheduler

type CAiScheduler struct {
	*aistate.StateMachine
}

func (*CAiScheduler) Update

func (c *CAiScheduler) Update(delta float64)

type CAiStatus

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

func (*CAiStatus) Eat

func (c *CAiStatus) Eat()

func (*CAiStatus) HasFood

func (c *CAiStatus) HasFood() bool

func (*CAiStatus) Idle

func (c *CAiStatus) Idle() bool

func (*CAiStatus) IsFunc

func (c *CAiStatus) IsFunc(s string) func() bool

func (*CAiStatus) IsNotFunc

func (c *CAiStatus) IsNotFunc(s string) func() bool

func (*CAiStatus) NoFood

func (c *CAiStatus) NoFood() bool

func (*CAiStatus) Sleep

func (c *CAiStatus) Sleep()

func (*CAiStatus) Update

func (c *CAiStatus) Update(s *CompStatus, delta float64)

type CompAi

type CompAi struct {
	*CAiPerception
	*CAiScheduler
	*CAiStatus
	*CAiMemory
	*CAiPath
	aifiver.SmallModel
	// contains filtered or unexported fields
}

CompAi is the AI component.

func (*CompAi) Conflict

func (c *CompAi) Conflict() bool

Conflict returns true if the personality indicates low agreeableness.

func (*CompAi) Update

func (c *CompAi) Update(m *CompMovable, s *CompStatus, delta float64)

Update updates the AI state, performs calculations and magic.

type CompInventory

type CompInventory struct {
	Slots []*Item
	Size  int
	// contains filtered or unexported fields
}

CompInventory represents a storage for items with a specified capacity.

func (*CompInventory) Add

func (in *CompInventory) Add(it *Item) bool

Add adds the given item to the inventory and return success. NOTE: This will fail if the inventory is full.

func (*CompInventory) Drop

func (in *CompInventory) Drop(it *Item) bool

Drop drops the specified item from inventory and returns true on success.

func (*CompInventory) Find

func (in *CompInventory) Find(tag string) *Item

Find finds an item with a given tag in the inventory and returns the found item.

func (*CompInventory) Has

func (in *CompInventory) Has(itt *ItemType) bool

Has returns true if an item with the given ItemType is present in the inventory. TODO: Maybe return count instead of bool or introduce a second method that returns the count.

func (*CompInventory) IsFull

func (in *CompInventory) IsFull() bool

IsFull returns true if we have exceeded or have reached the inventory capacity

func (*CompInventory) RemoveID

func (in *CompInventory) RemoveID(id int) bool

RemoveID removes the item with the given id from inventory and returns true on success.

func (*CompInventory) TransferAll

func (in *CompInventory) TransferAll(to *CompInventory) bool

TransferAll transfers all items from the current inventory to the target inventory.

type CompMovable

type CompMovable struct {
	Pos   vectors.Vec2
	Speed vectors.Vec2
}

CompMovable is a movable component.

func (*CompMovable) Update

func (c *CompMovable) Update(delta float64)

Update moves the position in the component by the speed within the given time.

type CompStatus

type CompStatus struct {
	Sleeping bool
	// contains filtered or unexported fields
}

CompStatus is a component that handles the status or state of an entity (Health, stamina, hunger, thirst...).

func (*CompStatus) Dead

func (c *CompStatus) Dead() bool

Dead returns true if the entity is dead.

func (*CompStatus) Exhaustion

func (c *CompStatus) Exhaustion() float64

Exhaustion returns the current exhaustion of the entity.

func (*CompStatus) Health

func (c *CompStatus) Health() float64

Health returns the current health of the entity.

func (*CompStatus) Hunger

func (c *CompStatus) Hunger() float64

Hunger returns the current hunger of the entity.

func (*CompStatus) MaxHealth

func (c *CompStatus) MaxHealth() float64

MaxHealth returns the maximum health of the entity.

func (*CompStatus) Sleep

func (c *CompStatus) Sleep()

Sleep puts the entity to sleep.

func (*CompStatus) Stress

func (c *CompStatus) Stress() float64

Stress returns the current stress of the entity.

func (*CompStatus) Thirst

func (c *CompStatus) Thirst() float64

Thirst returns the current thirst of the entity.

func (*CompStatus) Update

func (c *CompStatus) Update(delta float64)

Update updates the status of the entity.

func (*CompStatus) Wake

func (c *CompStatus) Wake()

Wake wakes the entity up.

type Item

type Item struct {
	Location   ItemLocation // Type of location (world, inventory, ...)
	LocationID int          // ID of the entity if in inventory
	Pos        vectors.Vec2 // World position if in world
	*ItemType               // Base information
	// contains filtered or unexported fields
}

Item represents a movable item in the world.

func (*Item) ID

func (it *Item) ID() int

ID returns the unique identifier for this item.

type ItemLocation

type ItemLocation int

ItemLocation indicates the type of location where an item is located.

const (
	LocWorld ItemLocation = iota
	LocContainer
	LocInventory
)

type ItemType

type ItemType struct {
	Name       string
	Tags       []string       // Food, Weapon
	Properties map[string]int // Price, weight, damage, ...
	Requires   []*ItemType    // Requires items to craft
}

ItemType represents the base type of an item.

func NewItemType

func NewItemType(name string, tags ...string) *ItemType

NewItemType returns a new item type with the given name and tags.

func (*ItemType) New

func (i *ItemType) New(w *World, pos vectors.Vec2) *Item

New returns a new item of the current type.

type Location

type Location struct {
	Pos            vectors.Vec2 // Position on the map
	*CompInventory              // Location storage.
	// contains filtered or unexported fields
}

Location represents a destination with storage space. (e.g. building, hut, hiding place, ...)

func (*Location) ID

func (loc *Location) ID() int

ID returns the unique identifier for this location.

type Manager

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

Manager is a rudimentary entity manager. NOTE: This sucks. I really need to re-think and re-write this.

func (*Manager) Entities

func (m *Manager) Entities() []*Agent

Entities returns all registered entities.

func (*Manager) GetEntityFromID

func (m *Manager) GetEntityFromID(id int) *Agent

GetEntityFromID returns the agent with the given ID (if any). NOTE: This should be generic and not be typed as *Agent.

func (*Manager) Items

func (m *Manager) Items() []*Item

Items returns all registered items.

func (*Manager) Locations

func (m *Manager) Locations() []*Location

Locations returns all registered locations.

func (*Manager) NextID

func (m *Manager) NextID() int

NextID returns the next available unique identifier.

func (*Manager) RegisterEntity

func (m *Manager) RegisterEntity(e *Agent)

RegisterEntity registers the given agent in the manager. NOTE: This should be generic and not be typed as *Agent.

func (*Manager) RegisterItem

func (m *Manager) RegisterItem(it *Item)

RegisterItem registers the given item in the manager.

func (*Manager) RegisterLocation

func (m *Manager) RegisterLocation(loc *Location)

RegisterLocation registers a new location.

func (*Manager) RemoveEntity

func (m *Manager) RemoveEntity(e *Agent)

RemoveEntity removes the given agent from the manager. NOTE: This should be generic and not be typed as *Agent.

func (*Manager) RemoveItem

func (m *Manager) RemoveItem(it *Item)

RemoveItem removes the given item from the world.

func (*Manager) Reset

func (m *Manager) Reset()

Reset resets stuff.

type Profession

type Profession struct {
	*ProfessionType

	CurrentProject *Project // Current project? Would a queue be better?

	Missing []*ItemType
	// contains filtered or unexported fields
}

Profession represents a career of an individual and performs tasks related to the production of items. Implements aistate.State.

func (*Profession) OnEnter

func (s *Profession) OnEnter()

OnEnter is called when the state machine switches to this state.

func (*Profession) OnExit

func (s *Profession) OnExit()

OnExit is called when the state machine switches to another state.

func (*Profession) Tick

func (s *Profession) Tick(delta uint64)

Tick advances the tasks associated with the profession by the given time interval.

func (*Profession) Type

func (s *Profession) Type() aistate.StateType

TODO: Implement state that solely handles work activity.

type ProfessionType

type ProfessionType struct {
	Name     string
	CanCraft []*ItemType
}

ProfessionType is the general type of a profession. (e.g.: Baker, farmer, butcher, ...)

func NewProfessionType

func NewProfessionType(name string, canCraft ...*ItemType) *ProfessionType

NewProfessionType returns a new profession of a given type.

func (*ProfessionType) New

func (p *ProfessionType) New(w *World, a *Agent, workshop *Location) *Profession

type Project

type Project struct {
	Produce  *ItemType
	Progress uint64 // Amount of time invested
	Duration uint64
	Complete bool
}

Project represents a production task.

func (*Project) Tick

func (p *Project) Tick(delta uint64)

Tick advances the project by the given duration.

type StateAttack

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

StateAttack

func NewStateAttack

func NewStateAttack(ai *CompAi) *StateAttack

func (*StateAttack) OnEnter

func (s *StateAttack) OnEnter()

func (*StateAttack) OnExit

func (s *StateAttack) OnExit()

func (*StateAttack) Tick

func (s *StateAttack) Tick(delta uint64)

func (*StateAttack) Type

func (s *StateAttack) Type() aistate.StateType

type StateEatFood

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

StateEatFood will be active if the agent is hungry and we have food.

func NewStateEatFood

func NewStateEatFood(ai *CompAi) *StateEatFood

func (*StateEatFood) OnEnter

func (s *StateEatFood) OnEnter()

func (*StateEatFood) OnExit

func (s *StateEatFood) OnExit()

func (*StateEatFood) Tick

func (s *StateEatFood) Tick(delta uint64)

func (*StateEatFood) Type

func (s *StateEatFood) Type() aistate.StateType

type StateFindFood

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

func NewStateFindFood

func NewStateFindFood(ai *CompAi) *StateFindFood

func (*StateFindFood) OnEnter

func (s *StateFindFood) OnEnter()

func (*StateFindFood) OnExit

func (s *StateFindFood) OnExit()

func (*StateFindFood) Tick

func (s *StateFindFood) Tick(delta uint64)

func (*StateFindFood) Type

func (s *StateFindFood) Type() aistate.StateType

type StateFlee

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

StateFlee

func NewStateFlee

func NewStateFlee(ai *CompAi) *StateFlee

func (*StateFlee) OnEnter

func (s *StateFlee) OnEnter()

func (*StateFlee) OnExit

func (s *StateFlee) OnExit()

func (*StateFlee) Tick

func (s *StateFlee) Tick(delta uint64)

func (*StateFlee) Type

func (s *StateFlee) Type() aistate.StateType

type StateIdle

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

func NewStateIdle

func NewStateIdle(ai *CompAi) *StateIdle

func (*StateIdle) OnEnter

func (s *StateIdle) OnEnter()

func (*StateIdle) OnExit

func (s *StateIdle) OnExit()

func (*StateIdle) Tick

func (s *StateIdle) Tick(delta uint64)

func (*StateIdle) Type

func (s *StateIdle) Type() aistate.StateType

type StateRest

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

StateRest will be active once an agent is exhausted. In this state, the agent will attempt to return home to rest.

func NewStateRest

func NewStateRest(ai *CompAi) *StateRest

func (*StateRest) OnEnter

func (s *StateRest) OnEnter()

func (*StateRest) OnExit

func (s *StateRest) OnExit()

func (*StateRest) Tick

func (s *StateRest) Tick(delta uint64)

func (*StateRest) Type

func (s *StateRest) Type() aistate.StateType

type StateStoreFood

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

func NewStateStoreFood

func NewStateStoreFood(ai *CompAi) *StateStoreFood

func (*StateStoreFood) OnEnter

func (s *StateStoreFood) OnEnter()

func (*StateStoreFood) OnExit

func (s *StateStoreFood) OnExit()

func (*StateStoreFood) Tick

func (s *StateStoreFood) Tick(delta uint64)

func (*StateStoreFood) Type

func (s *StateStoreFood) Type() aistate.StateType

type World

type World struct {
	Width  int
	Height int
	// contains filtered or unexported fields
}

func New

func New() *World

func (*World) ExportGif

func (w *World) ExportGif(path string) error

Export all frames to a GIF under the given path.

func (*World) NewChar

func (w *World) NewChar() *Agent

NewChar adds a new Agent to the world and returns it.

func (*World) Update

func (w *World) Update(delta float64)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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