Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Huge refactor #57

Merged
merged 2 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ func main() {
ctx := context.TODO()

encoder := encoder.NewEncoder()

propRepository, _ := taleslabrepositories.NewPropRepository()
biomeRepository, _ := taleslabrepositories.NewBiomeRepository()
sliceGenerator := taleslabservices.NewSlabSliceGenerator(biomeRepository, propRepository)
slabGenerator := taleslabservices.NewSlabGenerator(sliceGenerator)

mapService := taleslabservices.NewMapService(biomeRepository, propRepository, encoder)
mapService := taleslabservices.NewMapService(slabGenerator, encoder)

inputMap := &taleslabdto.MapDtoRequest{
Biome: biometype.Beach,
Expand Down
16 changes: 8 additions & 8 deletions cmd/elevations/danielabeach/data.txt

Large diffs are not rendered by default.

142 changes: 31 additions & 111 deletions cmd/elevations/danielabeach/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,155 +3,75 @@ package main
import (
"context"
"fmt"
"math"

"github.com/johnfercher/talescoder/pkg/encoder"
"github.com/johnfercher/taleslab/pkg/file"
"github.com/johnfercher/taleslab/pkg/grid"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabdomain/taleslabconsts/biometype"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabdomain/taleslabconsts/elementtype"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabdomain/taleslabentities"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabdto"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabmappers"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabrepositories"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabservices"
"github.com/johnfercher/tessadem-sdk/pkg/tessadem"
"log"
)

func main() {
ctx := context.TODO()

fileReader := tessadem.NewFileReader()

areaResponse, err := fileReader.ReadArea(ctx, "data/elevation/daniela.json")
worldMatrix, err := generateWorldFromTessadem()
if err != nil {
fmt.Println(err.Error())
log.Fatal(err.Error())
return
}

worldMatrix := BuildNormalizedElevationMap(areaResponse)
maxWidth := len(worldMatrix)
maxLength := len(worldMatrix[0])
squareSize := 50

fmt.Println(len(worldMatrix), len(worldMatrix[0]))

biome := biometype.Beach

worldMatrixSlices := grid.SliceTerrain(worldMatrix, squareSize)

encoder := encoder.NewEncoder()
propRepository, _ := taleslabrepositories.NewPropRepository()
biomeRepository, _ := taleslabrepositories.NewBiomeRepository()
sliceGenerator := taleslabservices.NewSlabSliceGenerator(biomeRepository, propRepository)
slabGenerator := taleslabservices.NewSlabGenerator(sliceGenerator)

response := &taleslabdto.MapDtoResponse{
SlabVersion: "v2",
slabDto := &taleslabdto.SlabDto{
SliceSize: 50,
Biomes: []biometype.BiomeType{biometype.Beach},
World: worldMatrix,
}

currentX := 0
currentY := 0
for _, worldMatrix := range worldMatrixSlices {
sliceCode := []string{}
for _, slice := range worldMatrix {
assetsGenerator := taleslabservices.NewAssetsGenerator(biomeRepository, propRepository, maxWidth, maxLength).
SetBiome(biome)

worldAssets, err := assetsGenerator.Generate(slice, currentX, currentY)
if err != nil {
fmt.Println(err.Error())
return
}

slab := taleslabmappers.TaleSpireSlabFromAssets(worldAssets)
slabs, err := slabGenerator.Generate(slabDto)
if err != nil {
log.Fatal(err.Error())
return
}

base64, err := encoder.Encode(slab)
var sliceCodes [][]string
for _, slab := range slabs {
var codes []string
for _, line := range slab {
talespireSlab := taleslabmappers.TaleSpireSlabFromSlab(line)
base64, err := encoder.Encode(talespireSlab)
if err != nil {
fmt.Println(err.Error())
return
}

sliceCode = append(sliceCode, base64)
response.Size += len(base64) / 1024
currentY += squareSize
codes = append(codes, base64)
}

response.Codes = append(response.Codes, sliceCode)
currentY = 0
currentX += squareSize
sliceCodes = append(sliceCodes, codes)
}

err = file.SaveCodes(response.Codes, "cmd/elevations/danielabeach/data.txt")
err = file.SaveCodes(sliceCodes, "cmd/elevations/danielabeach/data.txt")
if err != nil {
fmt.Println(err.Error())
return
}
}

func BuildNormalizedElevationMap(response *tessadem.AreaResponse) [][]taleslabentities.Element {
hasOcean := false

min, _ := getMinMax(response)

if min <= 0 {
hasOcean = true
for i := 0; i < len(response.Results); i++ {
for j := 0; j < len(response.Results[i]); j++ {
response.Results[i][j].Elevation += math.Abs(min)
}
}
}

min, _ = getMinMax(response)

elevation := [][]taleslabentities.Element{}

for i := 0; i < len(response.Results); i++ {
array := []taleslabentities.Element{}
for j := 0; j < len(response.Results[i]); j++ {
elevation := int(response.Results[i][j].Elevation - min)
element := taleslabentities.Element{
elevation,
getBaseGroundType(hasOcean, elevation),
}

array = append(array, element)
}
elevation = append(elevation, array)
}

return elevation
}

func getBaseGroundType(hasOcean bool, elevation int) elementtype.ElementType {
if hasOcean && elevation <= 1 {
return elementtype.Water
}

if elevation <= 3 {
return elementtype.BaseGround
}

if elevation <= 10 {
return elementtype.Ground
}

return elementtype.Mountain
}

func getMinMax(response *tessadem.AreaResponse) (float64, float64) {
min := math.MaxFloat64
max := 0.0
func generateWorldFromTessadem() ([][]taleslabentities.Element, error) {
ctx := context.TODO()
fileReader := tessadem.NewFileReader()

for i := 0; i < len(response.Results); i++ {
for j := 0; j < len(response.Results[i]); j++ {
elevation := response.Results[i][j].Elevation
if elevation < min {
min = elevation
} else if elevation > max {
max = elevation
}
}
areaResponse, err := fileReader.ReadArea(ctx, "data/elevation/daniela.json")
if err != nil {
return nil, err
}

return min, max
geoGenerator := taleslabservices.NewGeoReferencingGridGenerator()
return geoGenerator.Generate(areaResponse), nil
}
16 changes: 8 additions & 8 deletions cmd/elevations/petropolis/data.txt

Large diffs are not rendered by default.

141 changes: 31 additions & 110 deletions cmd/elevations/petropolis/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,160 +3,81 @@ package main
import (
"context"
"fmt"
"math"

"github.com/johnfercher/talescoder/pkg/encoder"
"github.com/johnfercher/taleslab/pkg/file"
"github.com/johnfercher/taleslab/pkg/grid"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabdomain/taleslabconsts/biometype"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabdomain/taleslabconsts/elementtype"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabdomain/taleslabentities"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabdto"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabmappers"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabrepositories"
"github.com/johnfercher/taleslab/pkg/taleslab/taleslabservices"
"github.com/johnfercher/tessadem-sdk/pkg/tessadem"
"log"
)

func main() {
ctx := context.TODO()

fileReader := tessadem.NewFileReader()

areaResponse, err := fileReader.ReadArea(ctx, "data/elevation/petropolis.json")
worldMatrix, err := generateWorldFromTessadem()
if err != nil {
fmt.Println(err.Error())
log.Fatal(err.Error())
return
}

worldMatrix := BuildNormalizedElevationMap(areaResponse)
maxWidth := len(worldMatrix)
maxLength := len(worldMatrix[0])
squareSize := 50

river := &grid.River{
HeightCutThreshold: 2,
}
worldMatrix = grid.DigRiver(worldMatrix, river)

fmt.Println(len(worldMatrix), len(worldMatrix[0]))

biome := biometype.TemperateForest

worldMatrixSlices := grid.SliceTerrain(worldMatrix, squareSize)

encoder := encoder.NewEncoder()
propRepository, _ := taleslabrepositories.NewPropRepository()
biomeRepository, _ := taleslabrepositories.NewBiomeRepository()
sliceGenerator := taleslabservices.NewSlabSliceGenerator(biomeRepository, propRepository)
slabGenerator := taleslabservices.NewSlabGenerator(sliceGenerator)

response := &taleslabdto.MapDtoResponse{
SlabVersion: "v2",
slabDto := &taleslabdto.SlabDto{
SliceSize: 50,
Biomes: []biometype.BiomeType{biometype.TemperateForest},
World: worldMatrix,
}

currentX := 0
currentY := 0
for _, worldMatrix := range worldMatrixSlices {
sliceCode := []string{}
for _, slice := range worldMatrix {
assetsGenerator := taleslabservices.NewAssetsGenerator(biomeRepository, propRepository, maxWidth, maxLength).
SetBiome(biome)
slabs, err := slabGenerator.Generate(slabDto)
if err != nil {
log.Fatal(err.Error())
return
}

worldAssets, err := assetsGenerator.Generate(slice, currentX, currentY)
var sliceCodes [][]string
for _, slab := range slabs {
var codes []string
for _, line := range slab {
talespireSlab := taleslabmappers.TaleSpireSlabFromSlab(line)
base64, err := encoder.Encode(talespireSlab)
if err != nil {
fmt.Println(err.Error())
return
}

slab := taleslabmappers.TaleSpireSlabFromAssets(worldAssets)

base64, encodeError := encoder.Encode(slab)
if err != nil {
fmt.Println(encodeError.Error())
return
}

sliceCode = append(sliceCode, base64)
response.Size += len(base64) / 1024
currentY += squareSize
codes = append(codes, base64)
}

response.Codes = append(response.Codes, sliceCode)
currentY = 0
currentX += squareSize
sliceCodes = append(sliceCodes, codes)
}

err = file.SaveCodes(response.Codes, "cmd/elevations/petropolis/data.txt")
err = file.SaveCodes(sliceCodes, "cmd/elevations/petropolis/data.txt")
if err != nil {
fmt.Println(err.Error())
return
}
}

func BuildNormalizedElevationMap(response *tessadem.AreaResponse) [][]taleslabentities.Element {
hasOcean := false

min, _ := getMinMax(response)

if min <= 0 {
hasOcean = true
for i := 0; i < len(response.Results); i++ {
for j := 0; j < len(response.Results[i]); j++ {
response.Results[i][j].Elevation += math.Abs(min)
}
}
}

min, _ = getMinMax(response)

elevation := [][]taleslabentities.Element{}

for i := 0; i < len(response.Results); i++ {
array := []taleslabentities.Element{}
for j := 0; j < len(response.Results[i]); j++ {
elevation := int(response.Results[i][j].Elevation - min)
element := taleslabentities.Element{
elevation,
getBaseGroundType(hasOcean, elevation),
}

array = append(array, element)
}
elevation = append(elevation, array)
}

return elevation
}

func getBaseGroundType(hasOcean bool, elevation int) elementtype.ElementType {
if hasOcean && elevation <= 1 {
return elementtype.Water
}

if elevation <= 3 {
return elementtype.BaseGround
}

if elevation <= 10 {
return elementtype.Ground
}

return elementtype.Mountain
}

func getMinMax(response *tessadem.AreaResponse) (float64, float64) {
min := math.MaxFloat64
max := 0.0
func generateWorldFromTessadem() ([][]taleslabentities.Element, error) {
ctx := context.TODO()
fileReader := tessadem.NewFileReader()

for i := 0; i < len(response.Results); i++ {
for j := 0; j < len(response.Results[i]); j++ {
elevation := response.Results[i][j].Elevation
if elevation < min {
min = elevation
} else if elevation > max {
max = elevation
}
}
areaResponse, err := fileReader.ReadArea(ctx, "data/elevation/petropolis.json")
if err != nil {
return nil, err
}

return min, max
geoGenerator := taleslabservices.NewGeoReferencingGridGenerator()
return geoGenerator.Generate(areaResponse), nil
}
Loading
Loading