2020-07-10 16:54:43 +01:00
|
|
|
package atlas
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"github.com/mdiluz/rove/pkg/maths"
|
2020-07-19 12:26:57 +01:00
|
|
|
"github.com/mdiluz/rove/proto/roveapi"
|
2020-07-10 16:54:43 +01:00
|
|
|
"github.com/ojrac/opensimplex-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// chunk represents a fixed square grid of tiles
|
|
|
|
type chunk struct {
|
|
|
|
// Tiles represents the tiles within the chunk
|
|
|
|
Tiles []byte `json:"tiles"`
|
|
|
|
|
|
|
|
// Objects represents the objects within the chunk
|
|
|
|
// only one possible object per tile for now
|
2020-07-10 18:39:33 +01:00
|
|
|
Objects map[int]Object `json:"objects"`
|
2020-07-10 16:54:43 +01:00
|
|
|
}
|
|
|
|
|
2020-07-10 16:56:17 +01:00
|
|
|
// chunkBasedAtlas represents a grid of Chunks
|
|
|
|
type chunkBasedAtlas struct {
|
2020-07-10 16:54:43 +01:00
|
|
|
// Chunks represents all chunks in the world
|
|
|
|
// This is intentionally not a 2D array so it can be expanded in all directions
|
|
|
|
Chunks []chunk `json:"chunks"`
|
|
|
|
|
|
|
|
// LowerBound is the origin of the bottom left corner of the current chunks in world space (current chunks cover >= this value)
|
2020-07-10 18:22:59 +01:00
|
|
|
LowerBound maths.Vector `json:"lowerBound"`
|
2020-07-10 16:54:43 +01:00
|
|
|
|
|
|
|
// UpperBound is the top left corner of the current chunks (curent chunks cover < this value)
|
2020-07-10 18:22:59 +01:00
|
|
|
UpperBound maths.Vector `json:"upperBound"`
|
2020-07-10 16:54:43 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewChunkAtlas creates a new empty atlas
|
|
|
|
func NewChunkAtlas(chunkSize int) Atlas {
|
|
|
|
// Start up with one chunk
|
2020-07-10 16:56:17 +01:00
|
|
|
a := chunkBasedAtlas{
|
2020-07-10 16:54:43 +01:00
|
|
|
ChunkSize: chunkSize,
|
|
|
|
Chunks: make([]chunk, 1),
|
2020-07-10 18:22:59 +01:00
|
|
|
LowerBound: maths.Vector{X: 0, Y: 0},
|
|
|
|
UpperBound: maths.Vector{X: chunkSize, Y: chunkSize},
|
2020-07-10 16:54:43 +01:00
|
|
|
terrainNoise: opensimplex.New(noiseSeed),
|
|
|
|
objectNoise: opensimplex.New(noiseSeed),
|
|
|
|
}
|
|
|
|
// Initialise the first chunk
|
|
|
|
a.populate(0)
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTile sets an individual tile's kind
|
2020-07-19 12:26:57 +01:00
|
|
|
func (a *chunkBasedAtlas) SetTile(v maths.Vector, tile roveapi.Tile) {
|
2020-07-10 16:54:43 +01:00
|
|
|
c := a.worldSpaceToChunkWithGrow(v)
|
|
|
|
local := a.worldSpaceToChunkLocal(v)
|
|
|
|
a.setTile(c, local, byte(tile))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetObject sets the object on a tile
|
2020-07-10 18:39:33 +01:00
|
|
|
func (a *chunkBasedAtlas) SetObject(v maths.Vector, obj Object) {
|
2020-07-10 16:54:43 +01:00
|
|
|
c := a.worldSpaceToChunkWithGrow(v)
|
|
|
|
local := a.worldSpaceToChunkLocal(v)
|
|
|
|
a.setObject(c, local, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryPosition will return information for a specific position
|
2020-07-19 12:26:57 +01:00
|
|
|
func (a *chunkBasedAtlas) QueryPosition(v maths.Vector) (roveapi.Tile, Object) {
|
2020-07-10 16:54:43 +01:00
|
|
|
c := a.worldSpaceToChunkWithGrow(v)
|
|
|
|
local := a.worldSpaceToChunkLocal(v)
|
|
|
|
a.populate(c)
|
|
|
|
chunk := a.Chunks[c]
|
|
|
|
i := a.chunkTileIndex(local)
|
2020-07-19 12:26:57 +01:00
|
|
|
return roveapi.Tile(chunk.Tiles[i]), chunk.Objects[i]
|
2020-07-10 16:54:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// chunkTileID returns the tile index within a chunk
|
2020-07-10 18:22:59 +01:00
|
|
|
func (a *chunkBasedAtlas) chunkTileIndex(local maths.Vector) int {
|
2020-07-10 16:54:43 +01:00
|
|
|
return local.X + local.Y*a.ChunkSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// populate will fill a chunk with data
|
2020-07-10 16:56:17 +01:00
|
|
|
func (a *chunkBasedAtlas) populate(chunk int) {
|
2020-07-10 16:54:43 +01:00
|
|
|
c := a.Chunks[chunk]
|
|
|
|
if c.Tiles != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Tiles = make([]byte, a.ChunkSize*a.ChunkSize)
|
2020-07-10 18:39:33 +01:00
|
|
|
c.Objects = make(map[int]Object)
|
2020-07-10 16:54:43 +01:00
|
|
|
|
|
|
|
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)
|
2020-07-19 12:26:57 +01:00
|
|
|
var tile roveapi.Tile
|
2020-07-10 16:54:43 +01:00
|
|
|
switch {
|
|
|
|
case t > 0.5:
|
2020-07-19 12:26:57 +01:00
|
|
|
tile = roveapi.Tile_Gravel
|
2020-07-10 16:54:43 +01:00
|
|
|
case t > 0.05:
|
2020-07-19 12:26:57 +01:00
|
|
|
tile = roveapi.Tile_Sand
|
2020-07-10 16:54:43 +01:00
|
|
|
default:
|
2020-07-19 12:26:57 +01:00
|
|
|
tile = roveapi.Tile_Rock
|
2020-07-10 16:54:43 +01:00
|
|
|
}
|
|
|
|
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)
|
2020-07-19 12:26:57 +01:00
|
|
|
var obj = roveapi.Object_ObjectNone
|
2020-07-10 16:54:43 +01:00
|
|
|
switch {
|
|
|
|
case o > 0.6:
|
2020-07-19 12:26:57 +01:00
|
|
|
obj = roveapi.Object_RockLarge
|
2020-07-10 16:54:43 +01:00
|
|
|
case o > 0.5:
|
2020-07-19 12:26:57 +01:00
|
|
|
obj = roveapi.Object_RockSmall
|
2020-07-10 16:54:43 +01:00
|
|
|
}
|
2020-07-19 12:26:57 +01:00
|
|
|
if obj != roveapi.Object_ObjectNone {
|
|
|
|
c.Objects[j*a.ChunkSize+i] = Object{Type: roveapi.Object(obj)}
|
2020-07-10 16:54:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up any objects
|
|
|
|
for i := 0; i < len(c.Tiles); i++ {
|
|
|
|
if rand.Intn(16) == 0 {
|
2020-07-19 12:26:57 +01:00
|
|
|
c.Objects[i] = Object{Type: roveapi.Object_RockLarge}
|
2020-07-10 16:54:43 +01:00
|
|
|
} else if rand.Intn(32) == 0 {
|
2020-07-19 12:26:57 +01:00
|
|
|
c.Objects[i] = Object{Type: roveapi.Object_RockSmall}
|
2020-07-10 16:54:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Chunks[chunk] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
// setTile sets a tile in a specific chunk
|
2020-07-10 18:22:59 +01:00
|
|
|
func (a *chunkBasedAtlas) setTile(chunk int, local maths.Vector, tile byte) {
|
2020-07-10 16:54:43 +01:00
|
|
|
a.populate(chunk)
|
|
|
|
c := a.Chunks[chunk]
|
|
|
|
c.Tiles[a.chunkTileIndex(local)] = tile
|
|
|
|
a.Chunks[chunk] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
// setObject sets an object in a specific chunk
|
2020-07-10 18:39:33 +01:00
|
|
|
func (a *chunkBasedAtlas) setObject(chunk int, local maths.Vector, object Object) {
|
2020-07-10 16:54:43 +01:00
|
|
|
a.populate(chunk)
|
|
|
|
|
|
|
|
c := a.Chunks[chunk]
|
|
|
|
i := a.chunkTileIndex(local)
|
2020-07-19 12:26:57 +01:00
|
|
|
if object.Type != roveapi.Object_ObjectNone {
|
2020-07-10 16:54:43 +01:00
|
|
|
c.Objects[i] = object
|
|
|
|
} else {
|
|
|
|
delete(c.Objects, i)
|
|
|
|
}
|
|
|
|
a.Chunks[chunk] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
// worldSpaceToChunkLocal gets a chunk local coordinate for a tile
|
2020-07-10 18:22:59 +01:00
|
|
|
func (a *chunkBasedAtlas) worldSpaceToChunkLocal(v maths.Vector) maths.Vector {
|
|
|
|
return maths.Vector{X: maths.Pmod(v.X, a.ChunkSize), Y: maths.Pmod(v.Y, a.ChunkSize)}
|
2020-07-10 16:54:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// worldSpaceToChunkID gets the current chunk ID for a position in the world
|
2020-07-10 18:22:59 +01:00
|
|
|
func (a *chunkBasedAtlas) worldSpaceToChunkIndex(v maths.Vector) int {
|
2020-07-10 16:54:43 +01:00
|
|
|
// Shift the vector by our current min
|
|
|
|
v = v.Added(a.LowerBound.Negated())
|
|
|
|
|
|
|
|
// Divide by the current size and floor, to get chunk-scaled vector from the lower bound
|
|
|
|
v = v.DividedFloor(a.ChunkSize)
|
|
|
|
|
|
|
|
// Calculate the width
|
|
|
|
width := a.UpperBound.X - a.LowerBound.X
|
|
|
|
widthInChunks := width / a.ChunkSize
|
|
|
|
|
|
|
|
// Along the corridor and up the stairs
|
|
|
|
return (v.Y * widthInChunks) + v.X
|
|
|
|
}
|
|
|
|
|
|
|
|
// chunkOriginInWorldSpace returns the origin of the chunk in world space
|
2020-07-10 18:22:59 +01:00
|
|
|
func (a *chunkBasedAtlas) chunkOriginInWorldSpace(chunk int) maths.Vector {
|
2020-07-10 16:54:43 +01:00
|
|
|
// Calculate the width
|
|
|
|
width := a.UpperBound.X - a.LowerBound.X
|
|
|
|
widthInChunks := width / a.ChunkSize
|
|
|
|
|
|
|
|
// Reverse the along the corridor and up the stairs
|
2020-07-10 18:22:59 +01:00
|
|
|
v := maths.Vector{
|
2020-07-10 16:54:43 +01:00
|
|
|
X: chunk % widthInChunks,
|
|
|
|
Y: chunk / widthInChunks,
|
|
|
|
}
|
|
|
|
// Multiply up to world scale
|
|
|
|
v = v.Multiplied(a.ChunkSize)
|
|
|
|
// Shift by the lower bound
|
|
|
|
return v.Added(a.LowerBound)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNewBounds gets new lower and upper bounds for the world space given a vector
|
2020-07-10 18:22:59 +01:00
|
|
|
func (a *chunkBasedAtlas) getNewBounds(v maths.Vector) (lower maths.Vector, upper maths.Vector) {
|
|
|
|
lower = maths.Min2(v, a.LowerBound)
|
|
|
|
upper = maths.Max2(v.Added(maths.Vector{X: 1, Y: 1}), a.UpperBound)
|
2020-07-10 16:54:43 +01:00
|
|
|
|
2020-07-10 18:22:59 +01:00
|
|
|
lower = maths.Vector{
|
2020-07-10 16:54:43 +01:00
|
|
|
X: maths.RoundDown(lower.X, a.ChunkSize),
|
|
|
|
Y: maths.RoundDown(lower.Y, a.ChunkSize),
|
|
|
|
}
|
2020-07-10 18:22:59 +01:00
|
|
|
upper = maths.Vector{
|
2020-07-10 16:54:43 +01:00
|
|
|
X: maths.RoundUp(upper.X, a.ChunkSize),
|
|
|
|
Y: maths.RoundUp(upper.Y, a.ChunkSize),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// worldSpaceToTrunkWithGrow will expand the current atlas for a given world space position if needed
|
2020-07-10 18:22:59 +01:00
|
|
|
func (a *chunkBasedAtlas) worldSpaceToChunkWithGrow(v maths.Vector) int {
|
2020-07-10 16:54:43 +01:00
|
|
|
// If we're within bounds, just return the current chunk
|
|
|
|
if v.X >= a.LowerBound.X && v.Y >= a.LowerBound.Y && v.X < a.UpperBound.X && v.Y < a.UpperBound.Y {
|
|
|
|
return a.worldSpaceToChunkIndex(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the new bounds
|
|
|
|
lower, upper := a.getNewBounds(v)
|
|
|
|
size := upper.Added(lower.Negated())
|
|
|
|
size = size.Divided(a.ChunkSize)
|
|
|
|
|
|
|
|
// Create the new empty atlas
|
2020-07-10 16:56:17 +01:00
|
|
|
newAtlas := chunkBasedAtlas{
|
2020-07-10 16:54:43 +01:00
|
|
|
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
|
|
|
|
log.Printf("Re-allocating world, old: %+v,%+v new: %+v,%+v\n", a.LowerBound, a.UpperBound, newAtlas.LowerBound, newAtlas.UpperBound)
|
|
|
|
|
|
|
|
// Copy all old chunks into the new atlas
|
|
|
|
for chunk, chunkData := range a.Chunks {
|
|
|
|
|
|
|
|
// Calculate the chunk ID in the new atlas
|
|
|
|
origin := a.chunkOriginInWorldSpace(chunk)
|
|
|
|
newChunk := newAtlas.worldSpaceToChunkIndex(origin)
|
|
|
|
|
|
|
|
// Copy over the old chunk to the new atlas
|
|
|
|
newAtlas.Chunks[newChunk] = chunkData
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite the old atlas with this one
|
|
|
|
*a = newAtlas
|
|
|
|
|
|
|
|
return a.worldSpaceToChunkIndex(v)
|
|
|
|
}
|