Merge pull request from mdiluz/improved-world-gen

Noise based world gen
This commit is contained in:
Marc Di Luzio 2020-07-09 00:11:46 +01:00 committed by GitHub
commit 84be8bff05
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 31 deletions

1
go.mod
View file

@ -8,6 +8,7 @@ require (
github.com/google/uuid v1.1.1
github.com/grpc-ecosystem/grpc-gateway v1.14.6
github.com/kr/pretty v0.1.0 // indirect
github.com/ojrac/opensimplex-go v1.0.1
github.com/robfig/cron v1.2.0
github.com/stretchr/testify v1.6.0
golang.org/x/net v0.0.0-20200602114024-627f9648deb9

2
go.sum
View file

@ -50,6 +50,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/ojrac/opensimplex-go v1.0.1 h1:XslvpLP6XqQSATUtsOnGBYtFPw7FQ6h6y0ihjVeOLHo=
github.com/ojrac/opensimplex-go v1.0.1/go.mod h1:MoSgj04tZpH8U0RefZabnHV2AbLgv/2mo3hLJtWqSEs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=

View file

@ -7,6 +7,7 @@ import (
"github.com/mdiluz/rove/pkg/maths"
"github.com/mdiluz/rove/pkg/objects"
"github.com/mdiluz/rove/pkg/vector"
"github.com/ojrac/opensimplex-go"
)
// Tile describes the type of terrain
@ -17,10 +18,13 @@ const (
TileNone = Tile(0)
// TileRock is solid rock ground
TileRock = Tile('.')
TileRock = Tile('-')
// TileGravel is loose rocks
TileGravel = Tile(':')
// TileSand is sand
TileSand = Tile(',')
TileSand = Tile('~')
)
// Chunk represents a fixed square grid of tiles
@ -47,19 +51,33 @@ type Atlas struct {
// ChunkSize is the x/y dimensions of each square chunk
ChunkSize int `json:"chunksize"`
// terrainNoise describes the noise function for the terrain
terrainNoise opensimplex.Noise
// terrainNoise describes the noise function for the terrain
objectNoise opensimplex.Noise
}
const (
noiseSeed = 1024
terrainNoiseScale = 6
objectNoiseScale = 3
)
// NewAtlas creates a new empty atlas
func NewAtlas(chunkSize int) Atlas {
// Start up with one chunk
a := Atlas{
ChunkSize: chunkSize,
Chunks: make([]Chunk, 1),
LowerBound: vector.Vector{X: 0, Y: 0},
UpperBound: vector.Vector{X: chunkSize, Y: chunkSize},
ChunkSize: chunkSize,
Chunks: make([]Chunk, 1),
LowerBound: vector.Vector{X: 0, Y: 0},
UpperBound: vector.Vector{X: chunkSize, Y: chunkSize},
terrainNoise: opensimplex.New(noiseSeed),
objectNoise: opensimplex.New(noiseSeed),
}
// Initialise the first chunk
a.Chunks[0].populate(chunkSize)
a.populate(0)
return a
}
@ -81,11 +99,8 @@ func (a *Atlas) SetObject(v vector.Vector, obj objects.Object) {
func (a *Atlas) QueryPosition(v vector.Vector) (byte, objects.Object) {
c := a.worldSpaceToChunkWithGrow(v)
local := a.worldSpaceToChunkLocal(v)
a.populate(c)
chunk := a.Chunks[c]
if chunk.Tiles == nil {
chunk.populate(a.ChunkSize)
a.Chunks[c] = chunk
}
i := a.chunkTileIndex(local)
return chunk.Tiles[i], chunk.Objects[i]
}
@ -96,16 +111,44 @@ func (a *Atlas) chunkTileIndex(local vector.Vector) int {
}
// populate will fill a chunk with data
func (c *Chunk) populate(size int) {
c.Tiles = make([]byte, size*size)
func (a *Atlas) populate(chunk int) {
c := a.Chunks[chunk]
if c.Tiles != nil {
return
}
c.Tiles = make([]byte, a.ChunkSize*a.ChunkSize)
c.Objects = make(map[int]objects.Object)
// Set up the tiles
for i := 0; i < len(c.Tiles); i++ {
if rand.Intn(3) == 0 {
c.Tiles[i] = byte(TileRock)
} else {
c.Tiles[i] = byte(TileSand)
origin := a.chunkOriginInWorldSpace(chunk)
for i := 0; i < a.ChunkSize; i++ {
for j := 0; j < a.ChunkSize; j++ {
// Get the terrain noise value for this location
t := a.terrainNoise.Eval2(float64(origin.X+i)/terrainNoiseScale, float64(origin.Y+j)/terrainNoiseScale)
var tile Tile
switch {
case t > 0.5:
tile = TileGravel
case t > 0.05:
tile = TileSand
default:
tile = TileRock
}
c.Tiles[j*a.ChunkSize+i] = byte(tile)
// Get the object noise value for this location
o := a.objectNoise.Eval2(float64(origin.X+i)/objectNoiseScale, float64(origin.Y+j)/objectNoiseScale)
var obj = objects.None
switch {
case o > 0.6:
obj = objects.LargeRock
case o > 0.5:
obj = objects.SmallRock
}
if obj != objects.None {
c.Objects[j*a.ChunkSize+i] = objects.Object{Type: obj}
}
}
}
@ -117,26 +160,23 @@ func (c *Chunk) populate(size int) {
c.Objects[i] = objects.Object{Type: objects.SmallRock}
}
}
a.Chunks[chunk] = c
}
// setTile sets a tile in a specific chunk
func (a *Atlas) setTile(chunk int, local vector.Vector, tile byte) {
a.populate(chunk)
c := a.Chunks[chunk]
if c.Tiles == nil {
c.populate(a.ChunkSize)
}
c.Tiles[a.chunkTileIndex(local)] = tile
a.Chunks[chunk] = c
}
// setObject sets an object in a specific chunk
func (a *Atlas) setObject(chunk int, local vector.Vector, object objects.Object) {
c := a.Chunks[chunk]
if c.Tiles == nil {
c.populate(a.ChunkSize)
}
a.populate(chunk)
c := a.Chunks[chunk]
i := a.chunkTileIndex(local)
if object.Type != objects.None {
c.Objects[i] = object
@ -214,10 +254,12 @@ func (a *Atlas) worldSpaceToChunkWithGrow(v vector.Vector) int {
// Create the new empty atlas
newAtlas := Atlas{
ChunkSize: a.ChunkSize,
LowerBound: lower,
UpperBound: upper,
Chunks: make([]Chunk, size.X*size.Y),
ChunkSize: a.ChunkSize,
LowerBound: lower,
UpperBound: upper,
Chunks: make([]Chunk, size.X*size.Y),
terrainNoise: a.terrainNoise,
objectNoise: a.objectNoise,
}
// Log that we're resizing

View file

@ -1,6 +1,7 @@
package atlas
import (
"fmt"
"testing"
"github.com/mdiluz/rove/pkg/objects"
@ -248,3 +249,26 @@ func TestAtlas_GetSetCorrect(t *testing.T) {
}
}
}
func TestAtlas_WorldGen(t *testing.T) {
a := NewAtlas(8)
// Spawn a large world
_, _ = a.QueryPosition(vector.Vector{X: 20, Y: 20})
// Print out the world for manual evaluation
num := 20
for j := num - 1; j >= 0; j-- {
for i := 0; i < num; i++ {
t, o := a.QueryPosition(vector.Vector{X: i, Y: j})
if o.Type != objects.None {
fmt.Printf("%c", o.Type)
} else if t != byte(TileNone) {
fmt.Printf("%c", t)
} else {
fmt.Printf(" ")
}
}
fmt.Print("\n")
}
}