genvillage

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

README

Simple Village Generator

This package implements the simplest approach for generating a somewhat stable and self-sustaining settlement.

Principle

You instantiate a new BuildingPool, which will provide all available building types and add a number of types to it.

Then, you create a new settlement and give it a number of buildings you'd like to seed the economy with. After calling Solve(), the settlement should have a number of additional buildings needed to sustain itself... If there are still missing resources that the known building types cannot provide, these will have to be imported from another settlement.

Example

package main

import (
	"github.com/Flokey82/go_gens/genvillage"
	"log"
)

const (
	resFish   = "fish"
	resWorker = "worker"
	resGrain  = "grain"
	resFlour  = "flour"
	resBread  = "bread"
)

func main() {
	// Create a new building pool and register the known
	// building types.
	p := genvillage.NewBuildingPool()
	bFishery := genvillage.NewBuildingType("fishery")
	bFishery.Requires[resWorker] = 2
	bFishery.Provides[resFish] = 10
	p.AddType(bFishery)

	bHousing := genvillage.NewBuildingType("housing")
	bHousing.Requires[resBread] = 4
	bHousing.Provides[resWorker] = 4
	p.AddType(bHousing)

	bFarm := genvillage.NewBuildingType("farm")
	bFarm.Requires[resWorker] = 1
	bFarm.Provides[resGrain] = 10
	p.AddType(bFarm)

	bMill := genvillage.NewBuildingType("mill")
	bMill.Requires[resGrain] = 10
	bMill.Requires[resWorker] = 1
	bMill.Provides[resFlour] = 10
	p.AddType(bMill)

	bBakery := genvillage.NewBuildingType("bakery")
	bBakery.Requires[resFlour] = 2
	bBakery.Requires[resWorker] = 1
	bBakery.Provides[resBread] = 8
	p.AddType(bBakery)

	// Create a new settlement and add the fishery to seed
	// the economy.
	v := genvillage.NewSettlement(p)
	v.AddBuilding(bFishery.NewBuilding())

	// Resolve all resource dependencies if possible.
	v.Solve()

	// Print all buildings of the settlement.
	log.Println(v.Buildings)
}

Documentation

Overview

Package genvillage provides a very simple generator which attempts to generate a self-sustaining settlement economy given a number of buildings that consume and/or produce a number of resources.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Building

type Building struct {
	*BuildingType
}

Building represents an instance of BuildingType.

type BuildingPool

type BuildingPool struct {
	Types    []*BuildingType            // All known building types.
	Provides map[string][]*BuildingType // Maps resource ID to building types that provide it.
	Requires map[string][]*BuildingType // Maps resource ID to building types that require it.
}

BuildingPool represents all known / registered building types.

func NewBuildingPool

func NewBuildingPool() *BuildingPool

NewBuildingPool returns a new, empty building pool.

func (*BuildingPool) AddType

func (bp *BuildingPool) AddType(b *BuildingType)

AddType registers the given building type in the pool.

func (*BuildingPool) FindType

func (bp *BuildingPool) FindType(name string) *BuildingType

FindType returns the given building type from the pool.

func (*BuildingPool) Update

func (bp *BuildingPool) Update()

Update rebuilds the internal mappings for looking up building types by resource ID.

type BuildingType

type BuildingType struct {
	Name        string // Name of the building type.
	*Production        // Produces and requires which resources.
}

BuildingType represents a class of building requiring and/or providing specific resources.

func NewBuildingType

func NewBuildingType(name string) *BuildingType

NewBuildingType returns a new building type with the given name.

func (*BuildingType) NewBuilding

func (bt *BuildingType) NewBuilding() *Building

NewBuilding returns a new building instance of this type.

func (*BuildingType) String

func (bt *BuildingType) String() string

String implements the stringer function for this building type.

type Production

type Production struct {
	Requires map[string]int
	Provides map[string]int
}

Production provides information on transformation per cycle.

func NewProduction

func NewProduction() *Production

NewProduction returns a new production index. This represents somewhat a business or process that requires a number of resources and in turn provides other resources.

Example: A bakery requires water, flour, fire-wood, and one worker to produce bread.

func (*Production) GetExcess

func (p *Production) GetExcess() map[string]int

GetExcess returns all resources that are not needed but provided.

func (*Production) GetMissing

func (p *Production) GetMissing() map[string]int

GetMissing returns all resources that are needed and not provided.

type Settlement

type Settlement struct {
	Buildings     []*Building // All buildings in the settlement.
	*Production               // Production totals for the settlement.
	*BuildingPool             // All known building types.
}

Settlement represents a village with a number of buildings.

func NewSettlement

func NewSettlement(p *BuildingPool) *Settlement

NewSettlement returns a new settlement with the given BuildingPool.

func (*Settlement) AddBuilding

func (s *Settlement) AddBuilding(b *Building)

AddBuilding adds the given building to the settlement and updates the totals for each resource ID.

func (*Settlement) Solve

func (s *Settlement) Solve()

Solve attempts to add known building types to the settlements until the local economy is self-sustaining.

func (*Settlement) Update

func (s *Settlement) Update()

Update rebuilds the totals for each resource ID required and/or provided by the local economy.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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