mtsatellite/common/area.go

103 lines
2.5 KiB
Go
Raw Normal View History

// Copyright 2014, 2015, 2017 by Sascha L. Teichmann
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package common
import (
"math"
)
2024-01-07 03:51:27 +01:00
type area struct {
X1, Z1 int16
X2, Z2 int16
}
2024-01-07 03:51:27 +01:00
func (a area) contains(x, z int16) bool {
return x >= a.X1 && x <= a.X2 && z >= a.Z1 && z <= a.Z2
}
2024-01-07 03:51:27 +01:00
func (a area) higher() bool {
return a.Z2-a.Z1 > a.X2-a.X1
}
2024-01-07 03:51:27 +01:00
func areasContain(areas []area, x, z int16) bool {
for _, r := range areas {
if r.contains(x, z) {
return true
}
}
return false
}
// recalculate implements a greedy algorithm to figure out
// a list of disjunct areas of free regions in the domain
// to the (x, z) block plane.
// oldAreas are searched and found free areas are appended
// to newAreas which ist return.
// This is useful to spatial query only blocks from db
// that are not below already rendered blocks.
2024-01-07 03:51:27 +01:00
func (a area) recalculate(r *Renderer, nareas []area) []area {
yM := r.yMin
const ex = 1
const ez = 2
nas := len(nareas)
2024-01-06 16:12:19 +01:00
for z := a.Z1; z <= a.Z2; z++ {
row := z * int16(r.width)
2024-01-06 16:12:19 +01:00
for x := a.X1; x <= a.X2; x++ {
// Uncovered and not in list of new areas?
if yM[row+x] > math.MinInt32 || areasContain(nareas[nas:], x, z) {
continue
}
2024-01-07 03:51:27 +01:00
ar := area{X1: x, Z1: z, X2: x, Z2: z}
// Try to extend the area in x and/or z till no further extension is possible.
ext:
for extend := ex | ez; extend != 0; {
// If we extending in both directions a the current area
// is higher than wide we gain more block if extend
// in the x direction first.
2024-01-06 16:12:19 +01:00
if (extend == ex|ez && ar.higher()) || extend&ex == ex { // check x
nx := ar.X2 + 1
if nx > a.X2 { // reached border of area
extend &= ^ex
continue
}
// Check column right of the current area if its fully free.
2024-01-06 16:12:19 +01:00
for nz := ar.Z1; nz <= ar.Z2; nz++ {
if yM[nz*int16(r.width)+nx] > math.MinInt32 ||
areasContain(nareas[nas:], nx, nz) {
extend &= ^ex
continue ext
}
}
// free -> extend
2024-01-06 16:12:19 +01:00
ar.X2 = nx
} else if extend&ez == ez { // check z
2024-01-06 16:12:19 +01:00
nz := ar.Z2 + 1
if nz > a.Z2 {
extend &= ^ez
continue
}
// Check line right below the current area if its free.
row2 := nz * int16(r.width)
2024-01-06 16:12:19 +01:00
for nx := ar.X1; nx <= ar.X2; nx++ {
if yM[row2+nx] > math.MinInt32 ||
areasContain(nareas[nas:], nx, nz) {
extend &= ^ez
continue ext
}
}
// free -> extend
2024-01-06 16:12:19 +01:00
ar.Z2 = nz
}
}
// At this point the area is extended to max.
2024-01-06 16:12:19 +01:00
nareas = append(nareas, ar)
}
}
return nareas
}