2020-05-08 16:38:26 +00:00
|
|
|
package tins2020
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"opslag.de/schobers/geom/noise"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
Terrain *Map
|
|
|
|
Flowers []Flower
|
|
|
|
Bees []Bee
|
|
|
|
}
|
|
|
|
|
|
|
|
type Map struct {
|
2020-05-09 11:55:37 +00:00
|
|
|
Temp NoiseMap
|
|
|
|
Humid NoiseMap
|
|
|
|
Variant NoiseMap
|
|
|
|
PlaceX NoiseMap // displacement map of props
|
|
|
|
PlaceY NoiseMap
|
2020-05-08 16:38:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 11:55:37 +00:00
|
|
|
type NoiseMap interface {
|
|
|
|
Value(x, y int32) float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNoiseMap(seed int64) NoiseMap {
|
|
|
|
return &noiseMap{
|
|
|
|
noise: noise.NewPerlin(seed),
|
|
|
|
alpha: 2,
|
|
|
|
beta: 4,
|
|
|
|
harmonics: 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRandomNoiseMap(seed int64) NoiseMap {
|
|
|
|
return &randomNoiseMap{noise.NewPerlin(seed)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type noiseMap struct {
|
2020-05-08 16:38:26 +00:00
|
|
|
noise *noise.Perlin
|
|
|
|
alpha, beta float64
|
|
|
|
harmonics int
|
|
|
|
}
|
|
|
|
|
2020-05-09 11:55:37 +00:00
|
|
|
func clipNormalized(x float64) float64 {
|
|
|
|
if x < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if x > 1 {
|
|
|
|
return 1
|
2020-05-08 16:38:26 +00:00
|
|
|
}
|
2020-05-09 11:55:37 +00:00
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value generates the noise value for an x/y pair. The range of the output is approximately [-1.325825214,1.325825214] (for 4 harmonics).
|
|
|
|
func (m *noiseMap) Value(x, y int32) float64 {
|
|
|
|
value := m.noise.Noise2D(float64(x)*.01, float64(y)*.01, m.alpha, m.beta, m.harmonics)*.565 + .5
|
|
|
|
return clipNormalized(value)
|
2020-05-08 16:38:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 11:55:37 +00:00
|
|
|
type randomNoiseMap struct {
|
|
|
|
*noise.Perlin
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value generates the noise value for an x/y pair. The range of the output is approximately [-1.325825214,1.325825214] (for 4 harmonics).
|
|
|
|
func (m randomNoiseMap) Value(x, y int32) float64 {
|
|
|
|
value := m.Noise2D(float64(x)*.53, float64(y)*.53, 1.01, 2, 2)*.5 + .5
|
|
|
|
return clipNormalized(value)
|
2020-05-08 16:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGame() *Game {
|
|
|
|
terrain := &Map{
|
2020-05-09 11:55:37 +00:00
|
|
|
Temp: NewNoiseMap(rand.Int63()),
|
|
|
|
Humid: NewNoiseMap(rand.Int63()),
|
|
|
|
Variant: NewRandomNoiseMap(rand.Int63()),
|
|
|
|
PlaceX: NewRandomNoiseMap(rand.Int63()),
|
|
|
|
PlaceY: NewRandomNoiseMap(rand.Int63()),
|
2020-05-08 16:38:26 +00:00
|
|
|
}
|
|
|
|
return &Game{
|
|
|
|
Terrain: terrain,
|
|
|
|
Flowers: []Flower{},
|
|
|
|
Bees: []Bee{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Flower struct {
|
|
|
|
Location PointF
|
|
|
|
}
|
|
|
|
|
|
|
|
type Bee struct {
|
|
|
|
Location PointF
|
|
|
|
}
|