place blocks

This commit is contained in:
2022-02-24 20:20:33 +08:00
parent 9195dd7c3f
commit 32b06810e2
17 changed files with 309 additions and 55 deletions

View File

@@ -1,48 +1,55 @@
package world
import "edgaru089.ml/go/gl01/internal/util/itype"
import (
"edgaru089.ml/go/gl01/internal/util/itype"
)
// CastViewRay
func (w *World) CastViewRay(from, dir itype.Vec3d, maxlen float64) (ok bool, blockcoord itype.Vec3i, face itype.Direction, where itype.Vec3d, dist float64) {
func (w *World) castworker(from, dir itype.Vec3d, maxlen float64, current itype.Vec3i, skipdir itype.Direction) (ok bool, blockcoord itype.Vec3i, face itype.Direction, where itype.Vec3d, dist float64) {
bfrom := from.Floor()
bfromdir := itype.Direction(-1)
for bfrom.ToFloat64().Addv(0.5, 0.5, 0.5).Add(from.Negative()).Length() < maxlen {
for todir := itype.Direction(0); todir < 6; todir++ {
if todir == bfromdir {
continue
// Length test
if current.ToFloat64().Addv(0.5, 0.5, 0.5).Add(from.Negative()).Length() > maxlen+1 {
return
}
// Loose intersect test
if ok, _, _, _ = (itype.Boxd{
OffX: float64(current[0]),
OffY: float64(current[1]),
OffZ: float64(current[2]),
SizeX: 1, SizeY: 1, SizeZ: 1,
}).IntersectRay(from, dir, maxlen); !ok {
return
}
// Go through the bounding boxes
ba := w.Block(current).Appearance(current)
if !ba.NotSolid {
for _, b := range ba.Lookbox {
if ok, face, where, dist = b.Offset(current.ToFloat64()).IntersectRay(from, dir, maxlen); ok {
blockcoord = current
return
}
}
}
bto := bfrom.Add(itype.DirectionVeci[todir])
block := w.Block(bto)
// Test the directions
for i := itype.Direction(0); i < 6; i++ {
if i == skipdir {
continue
}
if block.Id != 0 {
outbox := itype.Boxd{
OffX: float64(bto[0]),
OffY: float64(bto[1]),
OffZ: float64(bto[2]),
SizeX: 1,
SizeY: 1,
SizeZ: 1,
}
var outface itype.Direction
if ok, outface, _, _ = outbox.IntersectRay(from, dir, maxlen); ok {
app := block.Appearance(bto)
for _, lb := range app.Lookbox {
if ok, face, where, dist = lb.IntersectRay(from, dir, maxlen); ok {
blockcoord = bto
return
}
}
}
bfromdir = outface.Opposite()
bfrom = bto
break
}
ok, blockcoord, face, where, dist = w.castworker(from, dir, maxlen, current.Add(itype.DirectionVeci[i]), i.Opposite())
if ok {
return
}
}
ok = false
return
}
// CastViewRay
func (w *World) CastViewRay(from, dir itype.Vec3d, maxlen float64) (ok bool, blockcoord itype.Vec3i, face itype.Direction, where itype.Vec3d, dist float64) {
return w.castworker(from, dir, maxlen, from.Floor(), -1)
}