simnpcs

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: 5 Imported by: 1

README

SimNPCs

This package is a pretty messy collection of different ideas that I had for simulating NPC careers, experience, interactions, etc.

I'm not happy how it is implemented but I decided to publish the code so others can take inspiration or maybe suggest improvements. There are a few problems with the code like for example the lack of separation of the different logical parts of the code and whatnot.

Documentation

Index

Constants

View Source
const (
	DayTimeStart = 7
	DayTimeEnd   = 22
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AcquiredFact

type AcquiredFact struct {
	ID   uint64 // Unique ID
	Fact *Fact  // The fact itself
}

AcquiredFact is a fact that a character has acquired.

TODO: Add more information about how the fact was acquired. - Who / who else supplied information - When / during what interaction(s) - What was discussed - Who else knows?

Method of acquisition: - Casual knowledge - Education - Legend - First hand experience

type Career

type Career struct {
	ID           uint64      // Unique ID
	Start        int         // Started career at cycle
	Active       int         // Number of active cycles (experience)
	End          int         // Last cycle of activity
	Profession   *Profession // Type of profession
	Location     *Location   // Main career location
	Storage      *Inventory  // Produced items, resources...
	WorkingHours [7][24]bool // Active working hours

	// TODO: Change this to requested items list / orders.
	WorkingOn *ProductionTask // Task currently being worked on
}

Career is an instance of a profession associated with a person.

func (*Career) BuysItems

func (c *Career) BuysItems() []*Item

BuysItems returns all items that can be bought by this profession.

func (*Career) IsWorkTime

func (c *Career) IsWorkTime(day, hour int) bool

IsWorkTime returns true if the given day of the week and hour is active for this profession.

func (*Career) NeedsItems

func (c *Career) NeedsItems() []*Item

NeedsItems returns all items that are required to produce items for this profession.

func (*Career) SellsItems

func (c *Career) SellsItems() []*Item

SellsItems returns all items that can be sold by this profession.

func (*Career) Update

func (c *Career) Update()

type Character

type Character struct {
	ID         uint64          // Unique ID
	FirstName  string          // First name
	LastName   string          // Last name
	Title      string          // Title (Sir, Lady, ...), optional
	Exhaustion int             // Current exhaustion level (0-8)
	WakeAt     int             // Time of day to wake up
	SleepAt    int             // Time of day to go to sleep
	Status     CharacterStatus // Current status (idle, working, resting, sleeping, ...)

	aifiver.SmallModel // Personality

	Career      *Career     // Current career (maybe allow multiple careers?)
	Home        *Location   // Current home (where the character sleeps)
	Location    *Location   // Current location
	PastCareers []*Career   // TODO: Add reason for new career
	PastHomes   []*Location // TODO: Add reason for move

	// Social standing.
	// Superior / Underlings
	// Birthday / Gender
	// Passions
	// Hobby []*Career - Hobby gardening, chicken coop.
	// Beliefs
	// Affiliations
	// Social connections
	Knowledge map[*Topic][]AcquiredFact // Total knowledge
	Opinions  map[uint64]Opinion        // ID to opinion mapping
	Routines  [7][24]*Routine           // Fixed routines
	Sources   map[uint64][]*Location    // Where to find what
	Tasks                               // Current tasks
	Inventory *Inventory                // Personal inventory
}

func NewCharacter

func NewCharacter(id uint64, firstName, lastName string, p aifiver.SmallModel) *Character

NewCharacter creates a new character.

func (*Character) AddRoutine

func (c *Character) AddRoutine(r *Routine)

AddRoutine adds a specific routine for the character.

func (*Character) AddSources

func (c *Character) AddSources(item *Item, locs ...*Location)

AddSources adds a list of locations to the sources map so the character can find the item.

func (*Character) ChangeOpinion

func (c *Character) ChangeOpinion(id uint64, imp Impact) Opinion

Change opinion on an entity with the given ID.

func (*Character) DoYourThing

func (c *Character) DoYourThing(day int, hour int)

DoYourThing causes the character to do watever is expected for the given day and hour.

func (*Character) FindTopics

func (c *Character) FindTopics(ct *Character) []*Topic

Determine overlap in Topics.

func (*Character) GetRoutine

func (c *Character) GetRoutine(dayOfWeek int, hour int) *Routine

GetRoutine gets any routine for the given day and hour.

func (*Character) GoTo

func (c *Character) GoTo(loc *Location)

GoTo moves the character to the given location.

func (*Character) Idle

func (c *Character) Idle()

Idle sets the character to idle.

func (*Character) Interact

func (c *Character) Interact(ct *Character, loc *Location)

Interact with another character.

func (*Character) Name

func (c *Character) Name() string

Name of the character.

func (*Character) Plan

func (c *Character) Plan()

Plan creates a number of tasks for the character on the current day.

func (*Character) SetCareer

func (c *Character) SetCareer(car *Career)

SetCareer sets a new active career for the character.

func (*Character) Sleep

func (c *Character) Sleep()

Sleep puts the character to sleep.

func (*Character) WakeUp

func (c *Character) WakeUp()

WakeUp wakes up the character.

func (*Character) Work

func (c *Character) Work()

Work causes the character to work.

type CharacterStatus

type CharacterStatus int
const (
	CharStatIdle CharacterStatus = iota
	CharStatWorking
	CharStatResting
	CharStatSleeping
)

type DayOfWeek

type DayOfWeek int
const (
	DayMonday DayOfWeek = iota
	DayTuesday
	DayWednesday
	DayThursday
	DayFriday
	DaySaturday
	DaySunday
)

type Education

type Education struct {
	ID uint64 // Unique ID
}

Education is a record of a character's education.

type Fact

type Fact struct {
	ID    uint64
	Topic *Topic
}

type Impact

type Impact struct {
	Emotional   float64 // Positive / negative emotion
	Monitary    float64 // Monitary gain
	Information float64 // Value of information exchanged
}

Impact is a measure of the impact of an interaction.

type Index

type Index struct {
	Entries   []*Character // All NPCs
	Locations []*Location  // All locations
	Topics    *TopicPool   // All topics (shared between NPCs)
	TickCount uint64       // Current tick count
	IDCount   uint64       // Current ID count (for unique IDs)
}

Index is the central index for all NPCs.

func New

func New() *Index

New returns a new Index.

func (*Index) GetID

func (idx *Index) GetID() uint64

GetID returns a new unique ID.

func (*Index) NewLocation

func (idx *Index) NewLocation(name string, parent *Location, t LocationType, s LocationScale) *Location

NewLocation adds a new location to the index.

func (*Index) NewProfession

func (idx *Index) NewProfession(name string, req LocationType) *Profession

NewProfession adds a new profession to the index.

func (*Index) StartCareer

func (idx *Index) StartCareer(c *Character, p *Profession, l *Location)

StartCareer starts a new career for a character.

func (*Index) Tick

func (idx *Index) Tick()

Tick updates the index.

type Interaction

type Interaction struct {
	ID uint64 // Unique ID
}

Interaction is a record of an interaction between two characters. TODO: Add more information about the interaction. - Who was part of the interaction. - What was the type of interaction? - What was the outcome of the interaction - Was there an exchange of information

type Inventory

type Inventory struct {
	Items []*ItemInstance
}

Inventory represents a storage of items.

func (*Inventory) Add

func (inv *Inventory) Add(item *ItemInstance)

Add adds an item to the inventory.

func (*Inventory) Find

func (inv *Inventory) Find(item *Item) *ItemInstance

Find returns the first item instance of a given item in the inventory.

func (*Inventory) FindIndex

func (inv *Inventory) FindIndex(item *ItemInstance) int

FindIndex returns the index of the given item instance in the inventory.

func (*Inventory) List

func (inv *Inventory) List() []*ItemInstance

List returns a list of all items in the inventory.

func (*Inventory) Move

func (inv *Inventory) Move(item *ItemInstance, to *Inventory)

Move moves an item from one inventory to another.

func (*Inventory) Remove

func (inv *Inventory) Remove(idx int)

Remove removes an item instance with the given index from the inventory.

type Item

type Item struct {
	ID           uint64        // Unique id
	Name         string        // Name of the item
	Buy          int           // Purchase price
	Sell         int           // Sale price
	Durability   int           // Base durability in cycles
	Consumes     []*Item       // Items consumed to produce
	RequiresTime int           // Number of cycles required to produce
	Requires     *Skill        // Skill required to produce
	ProducedBy   []*Profession // Professions that can produce this item
}

Item represents an item that can be bought, sold, crafted, etc. TODO: Allow passive production (growing crops).

type ItemInstance

type ItemInstance struct {
	ID         uint64 // Unique id
	Item       *Item  // Base item
	Durability int    // Remaining durability
}

ItemInstance represents an instance of an item.

type Location

type Location struct {
	ID       uint64        // Unique ID
	Name     string        // Name of the location
	Type     LocationType  // Type of location
	Scale    LocationScale // Scale of the location
	Parent   *Location     // Parent location
	Children []*Location   // Child locations
	Host     *Character    // Host of the location (merchant, innkeeper, etc.)

}

TODO: Add features (farm plot, chicken coop) that allow passive production of goods... Features might require new routines (collecting eggs etc.)

func NewLocation

func NewLocation(id uint64, name string, t LocationType, s LocationScale) *Location

NewLocation creates a new location.

func (*Location) AssignChild

func (l *Location) AssignChild(c *Location)

AssignChild adds a child location to the location.

func (*Location) Visit

func (l *Location) Visit(p *Character)

Visit is called if a character visits a location.

type LocationScale

type LocationScale int

LocationScale determines the likelyhood of an encounter.

const (
	LocScaleHouse LocationScale = iota
	LocScaleMerchantShopTavern
	LocScaleSquare
	LocScaleDistrict
	LocScaleTown
	LocScaleCity
)

type LocationType

type LocationType int
const (
	LocTypeNone LocationType = iota
	LocTypeShop
	LocTypeSmith
	LocTypeFarm
	LocTypeMine
	LocTypeInn
	LocTypeTown
	LocTypeHome
)

type Opinion

type Opinion struct {
	Count  int    // Number of values in average
	Impact        // Recent impact
	Total  Impact // Total impact
}

Opinion is a measure of how a character feels about a subject.

func (*Opinion) Change

func (o *Opinion) Change(imp Impact)

Change the opinion of a character about a subject.

func (*Opinion) String

func (o *Opinion) String() string

String returns a string representation of the opinion.

type ProductionTask

type ProductionTask struct {
	*Item         // Item being produced
	Remaining int // Remaining cycles until completion
}

ProductionTask is a task that is being worked on.

type Profession

type Profession struct {
	ID            uint64       // Unique ID
	Name          string       // Name of the profession.
	Requires      LocationType // Requires location type.
	TypicalStart  int          // Typical time the works starts (hour)
	TypicalEnd    int          // Typical time the works ends (hour)
	TypicalDays   []DayOfWeek  // Typical days of the week the profession is performed.
	RequiresItems []*Item      // Required items (speer f. hunter, plow f. farmer, ...)
	Novice        int          // Requires number of (active) cycles to be "Novice"
	Skilled       int          // Requires number of (active) cycles to be "Skilled"
	Expert        int          // Requires number of (active) cycles to be "Expert"
	Skills        []*Skill     // Skills that can be learned by this profession.

}

Profession represents a profession like "smith", "farmer", "miner", etc.

func NewProfession

func NewProfession(id uint64, name string, req LocationType) *Profession

NewProfession creates a new profession.

func (*Profession) AddSkill

func (p *Profession) AddSkill(id uint64, name string, produce []*Item, minExp int)

AddSkill adds a new skill to the profession.

func (*Profession) CanCraft

func (p *Profession) CanCraft(activeTics int) []*Item

CanCraft returns all items that can be crafted by this profession at the given experience level.

type Routine

type Routine struct {
	DayOfWeek int        // Day of week the routine is performed.
	Hour      int        // Hour of day the routine is performed.
	Location  *Location  // Location of the routine.
	C         *Character // Character performing the routine.
}

Routine is a routine that a character performs.

type Skill

type Skill struct {
	ID            uint64      // Unique ID of the skill.
	Name          string      // Name of the skill.
	CanProduce    []*Item     // Items that can be produced by this skill.
	Requires      *Profession // Profession required.
	MinExperience int         // Minimum number of active cycles needed
}

Skill represents a skill that can be used to produce items.

type SpeechModel

type SpeechModel struct {
}

SpeechModel describes the factors that has an impact on how a Character expresses themselves.

type Task

type Task struct {
	ID   uint64   // Unique id
	Type TaskType // Type of task
	Item *Item    // Item to find/buy/sell/craft
}

Task represents a task.

func (*Task) String

func (t *Task) String() string

String returns the string representation of the task.

type TaskType

type TaskType int

TaskType represents the type of task.

const (
	TaskNone TaskType = iota
	TaskFind
	TaskBuy
	TaskSell
	TaskCraft
)

Task types.

func (*TaskType) String

func (t *TaskType) String() string

String returns the string representation of the task type.

type Tasks

type Tasks []*Task

Tasks represents a list of tasks.

func (*Tasks) AddTask

func (c *Tasks) AddTask(id uint64, item *Item, t TaskType)

AddTask adds a task to the list.

func (*Tasks) CompleteTask

func (c *Tasks) CompleteTask(t *Task)

CompleteTask completes a task.

func (Tasks) FindTask

func (c Tasks) FindTask(t *Task) int

FindTask finds a task in the list.

func (*Tasks) RemoveTask

func (c *Tasks) RemoveTask(s int)

RemoveTask removes a task from the list.

type Topic

type Topic struct {
	ID        uint64        // Unique id
	Name      string        // Name of the topic
	Facts     []*Fact       // Facts related to this topic
	RelatedTo TopicRelation // Topics related to this topic

}

Topic represents a topic of interest.

func NewTopic

func NewTopic(id uint64, name string) *Topic

NewTopic returns a new topic.

type TopicPool

type TopicPool struct {
	ByProfession map[*Profession][]Topic
	ByLocation   map[*Location][]Topic
	ByEducation  map[*Education][]Topic
}

TopicPool represents a pool of topics npc's can talk about.

func NewTopicPool

func NewTopicPool() *TopicPool

NewTopicPool returns a new, empty topic pool.

type TopicRelation

type TopicRelation struct {
	Professions []*Profession
	Characters  []*Character
	Locations   []*Location
}

TopicRelation represents locations, profession, etc. related to a topic.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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