terrain

package
v0.0.0-...-db4c50c Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2020 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Surfaces are the currently predefined surface types (the 'elevation zones' of the terrain)
	Surfaces = []Surface{
		{uuid.New(), "Water", color.RGBA{R: 116, G: 167, B: 235, A: 255}, false},
		{uuid.New(), "Grassland", color.RGBA{R: 96, G: 236, B: 133, A: 255}, true},
		{uuid.New(), "Forest", color.RGBA{R: 44, G: 139, B: 54, A: 255}, true},
		{uuid.New(), "Gravel", color.RGBA{R: 198, G: 198, B: 198, A: 255}, true},
		{uuid.New(), "Mountain", color.RGBA{R: 204, G: 153, B: 102, A: 255}, true},
		{uuid.New(), "Moutain Peak", color.RGBA{R: 240, G: 240, B: 240, A: 255}, false},
	}
)

Functions

func MutateValue

func MutateValue(parentAttribute, mutationRate float64, valueRange attributeRange) float64

MutateValue produces a new value from the parent value It uses a normal distribution with standard deviation of mutation rate and it does not overflow attribute range

func MutateValues

func MutateValues(value1, value2, mutationRate float64, valueRange attributeRange) float64

Mutate values produces a value between first two parameters with a standard deviation of mutation rate

func ParseHexColorFast

func ParseHexColorFast(s string) (c color.RGBA, err error)

ParseHexColorFast converts HEX color string to RGBA. All of this code was found on Stack Overflow. Thanks to @icza (https://stackoverflow.com/a/54200713)

Types

type RandomWorld

type RandomWorld struct {
	// TODO introduce MaxBeings based on world size / terrain
	Width, Height int
	TerrainImage  *image.Gray // TerrainImage holds the terrain surface image (like a DEM model)
	TerrainZones  *image.RGBA // TerrainZones is a colored version of TerrainImage (based on defined zones and ratios)
	TerrainSpots  [][]*Spot   // TerrainSpots holds data about each spot on the map (what surface, what object or being
	// occupies it)
	BeingList map[string]*GoWorld.Being // The list of world inhabitants
	FoodList  map[string]*GoWorld.Food  // List of all edible food
	// contains filtered or unexported fields
}

RandomWorld represents the world implementation using Perlin Noise as terrain

func (*RandomWorld) AdjustNeeds

func (w *RandomWorld) AdjustNeeds(b *GoWorld.Being)

AdjustNeeds increases the being needs for the current epoch Higher values lower need for food / drinks:

  • Durability

Higher values increase need for food / drinks:

  • Speed
  • Stress
  • Size

func (*RandomWorld) AdjustStressFor

func (w *RandomWorld) AdjustStressFor(b *GoWorld.Being)

AdjustStressFor updates the stress value for the being The highest stress (255) is achieved when all basic necessities (food, drinks, mating) are at 255 and being is outside its natural habitat zone. The basic necessities all give the same amount of stress (multiplier of 0.1667), being outside the natural habitat doubles the stress (0.3333 per necessity) Higher values of being Size lower stress

func (*RandomWorld) BeingsToJSON

func (w *RandomWorld) BeingsToJSON(fileName string)

BeingsToJSON stores the current living beings to a file

func (*RandomWorld) CalculateZoneLimits

func (w *RandomWorld) CalculateZoneLimits(hist []int, ratios ...float64) []uint8

CalculateZoneLimits returns the upper bound values for zones if ratios are given of how much area each zone covers The number of zones can vary but sum(ratios) must equal to 1.0

func (*RandomWorld) CreateCarnivores

func (w *RandomWorld) CreateCarnivores(quantity int)

CreateCarnivores generates instances of beings and fills them with random attributes Provide the number of beings to create Note that the beings are added to the world and previously created beings are kept

func (*RandomWorld) CreateFishies

func (w *RandomWorld) CreateFishies(quantity int)

CreateFishies generates random instances of beings that live in water

func (*RandomWorld) CreateFlyers

func (w *RandomWorld) CreateFlyers(quantity int)

CreateFlyers generates instances of random flying beings

func (*RandomWorld) CreateRandomCarnivore

func (w *RandomWorld) CreateRandomCarnivore() *GoWorld.Being

CreateRandomCarnivore returns a new being with random parameters (places it onto the map)

func (*RandomWorld) CreateRandomFish

func (w *RandomWorld) CreateRandomFish() *GoWorld.Being

CreateRandomFish generates an instance of a being that lives in water

func (*RandomWorld) CreateRandomFlyer

func (w *RandomWorld) CreateRandomFlyer() *GoWorld.Being

CreateRandomFlyer generate an instance of a being that can fly

func (*RandomWorld) DisperseSeeds

func (w *RandomWorld) DisperseSeeds(p *GoWorld.Food, seeds int) []uuid.UUID

DisperseSeeds plants seeds within some range from plant Returns UUIDs of newly planted plants

func (*RandomWorld) Distance

func (w *RandomWorld) Distance(from, to GoWorld.Location) float64

Distance returns the euclidean distance between two locations. To speed up we leave out the square root

func (*RandomWorld) GetBeingAt

func (w *RandomWorld) GetBeingAt(location GoWorld.Location) (uuid.UUID, error)

GetBeingAt returns the ID of the being at the provided location Returns uuid.Nil if no being present

func (*RandomWorld) GetBeingWithID

func (w *RandomWorld) GetBeingWithID(id uuid.UUID) *GoWorld.Being

func (*RandomWorld) GetBeings

func (w *RandomWorld) GetBeings() map[string]*GoWorld.Being

GetBeings is a getter for all living beings

func (*RandomWorld) GetFood

func (w *RandomWorld) GetFood() map[string]*GoWorld.Food

GetFood is a getter for all the edible food

func (*RandomWorld) GetFoodWithID

func (w *RandomWorld) GetFoodWithID(id uuid.UUID) *GoWorld.Food

GetFoodWithID returns an item from the food list with given ID. Returns nil if ID does not exist

func (*RandomWorld) GetSize

func (w *RandomWorld) GetSize() (int, int)

GetSize returns the world bounds (width and height)

func (*RandomWorld) GetSurfaceColorAtSpot

func (w *RandomWorld) GetSurfaceColorAtSpot(spot GoWorld.Location) color.RGBA

GetSurfaceColorAtSpot returns the color of the surface (aka the zone) at the desired location

func (*RandomWorld) GetSurfaceNameAt

func (w *RandomWorld) GetSurfaceNameAt(location GoWorld.Location) (string, error)

GetSurfaceNameAt returns the common name of the surface at the provided location Panics if location is out of bound

func (*RandomWorld) GetTerrainImage

func (w *RandomWorld) GetTerrainImage() *image.RGBA

GetTerrainImage is a getter for the colored terrain (zones)

func (*RandomWorld) IsHabitable

func (w *RandomWorld) IsHabitable(location GoWorld.Location) (bool, error)

IsHabitable returns if the provided spot allows movement and seeding plants

func (*RandomWorld) IsOutOfBounds

func (w *RandomWorld) IsOutOfBounds(location GoWorld.Location) bool

IsOutOfBounds check if a location is inside the terrain zone. Returns true if location outside the bounds.

func (*RandomWorld) LaunchPlant

func (w *RandomWorld) LaunchPlant(p *GoWorld.Food)

LaunchPlant randomly places a plant onto water (will become a water plant)

func (*RandomWorld) MateBeing

func (w *RandomWorld) MateBeing(b *GoWorld.Being) []uuid.UUID

MateBeing tries to mate two adjacent beings with opposite genders and produce offspring The mutation rate is taken from the initiator Returns IDs of children produced

func (*RandomWorld) MidpointCircleAt

func (w *RandomWorld) MidpointCircleAt(center GoWorld.Location, radius float64) []GoWorld.Location

MidpointCircleAt creates a circle with the provided coordinates as the middle point and the radius. Returns a list of locations for the filled circle (including midpoint). If circle extends over world edges, then those locations are filtered out

func (*RandomWorld) MoveBeingToLocation

func (w *RandomWorld) MoveBeingToLocation(b *GoWorld.Being, to GoWorld.Location) error

MoveBeingToLocation moves the being to the provided location

func (*RandomWorld) New

func (w *RandomWorld) New() error

New returns new terrain generated using Perlin noise

func (*RandomWorld) PlantsToJSON

func (w *RandomWorld) PlantsToJSON(fileName string)

PlantsToJSON stores the current edible plants in the world into a json file

func (*RandomWorld) ProvideFood

func (w *RandomWorld) ProvideFood(landPlants, waterPlants int)

Provide food generates random plants across the terrain

func (*RandomWorld) QuenchHunger

func (w *RandomWorld) QuenchHunger(b *GoWorld.Being, foodSpot GoWorld.Location) bool

QuenchHunger tries to eat food if being is located on top of or next to food For food selection see method RandomWorld.MoveBeingTo() Returns true if being ate

func (*RandomWorld) QuenchThirst

func (w *RandomWorld) QuenchThirst(b *GoWorld.Being) bool

QuenchThirst tries to drink water if being is located 1 field away from water Returns true when being was able to drink, otherwise returns false

func (*RandomWorld) RandomPlant

func (w *RandomWorld) RandomPlant(inWater bool) *GoWorld.Food

randomPlant returns a food object with random parameters

func (*RandomWorld) SenseActionFor

func (w *RandomWorld) SenseActionFor(b *GoWorld.Being) (string, GoWorld.Location)

SenseActionFor uses the sense range of the being to decide on its next action Rules:

  1. priorities are in this order: drinks, food, mating, stress
  2. if any value is above threshold prefer its action, in case many are above threshold follow the previous order
  3. if stress is above threshold and can not eat/drink or mate try to move to natural habitat
  4. if nothing in sensing range, or all need fulfilled (values at 0) move randomly

Returns action to do as string and the location it picked for the action

func (*RandomWorld) ThrowBeing

func (w *RandomWorld) ThrowBeing(b *GoWorld.Being)

ThrowBeing randomly places the a being onto the map (onto walkable surfaces) Use with caution as it adjusts the beings habitat to that spot

func (*RandomWorld) ThrowPlant

func (w *RandomWorld) ThrowPlant(p *GoWorld.Food)

ThrowPlant randomly places a plant (food) onto the map

func (*RandomWorld) UpdateBeing

func (w *RandomWorld) UpdateBeing(b *GoWorld.Being) (string, []uuid.UUID)

UpdateBeing executes the next action for the being Returns action done as string and UUIDs of objects affected by action

func (*RandomWorld) UpdatePlant

func (w *RandomWorld) UpdatePlant(p *GoWorld.Food) (string, []uuid.UUID)

UpdatePlant updates the attributes for plant. It can grow, produce seeds or wither Returns action done as string and list of UUIDs of objects affected by action

func (*RandomWorld) Wander

func (w *RandomWorld) Wander(b *GoWorld.Being) error

Wander moves a being similar to Brownian Motion Implementation reference: http://people.bu.edu/andasari/courses/stochasticmodeling/lecture5/stochasticlecture5.html I have adjusted the following parameters:

  • the time step (delta t) is the speed of each creature
  • the previous position is the current position of the being
  • the next position is recalculated until a valid one is found

type Spot

type Spot struct {
	Surface        *Surface  // The surface attributes
	Object         uuid.UUID // The UUID for the object (nil for nothing) visible on surface
	Being          uuid.UUID // The being on the spot (nil for noone)
	OccupyingPlant uuid.UUID // The plant using this spot for growth (see Food.Area) not necessarily visible on surface

}

Spot is a place on the map with a defined surface type. Optionally an object (e.g. food) and a being can be located in it (a being above the object, for example eating food)

type Surface

type Surface struct {
	ID         uuid.UUID // The UUID (e.g. '7d444840-9dc0-11d1-b245-5ffdce74fad2'
	CommonName string    // A common name for it (e.g. 'Forest')
	// TODO use textures instead of colors
	Color     color.RGBA // A color value for the appearance
	Habitable bool       // Whether a Being can move across this surface (e.g. Can't walk on moutain peaks or on

}

Surface represents the data about a certain zone

Jump to

Keyboard shortcuts

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