Skip to content

Commit

Permalink
Merge pull request #5 from s0rg/release/v1.1.0
Browse files Browse the repository at this point in the history
code and workflows grouming, go version up, Bresenhams line
  • Loading branch information
s0rg committed Jul 29, 2023
2 parents 5f68586 + 7c8e6ae commit d0eded0
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 46 deletions.
19 changes: 19 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# dependabot settings
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
target-branch: "develop"
labels:
- "gomod dependencies"
schedule:
interval: "weekly"
day: "friday"
- package-ecosystem: "github-actions"
directory: "/"
target-branch: "develop"
labels:
- "github-actions dependencies"
schedule:
interval: "weekly"
day: "friday"
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ jobs:
- name: checkout
uses: actions/checkout@v3
- name: setup golang
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ^1.19
go-version: ^1.20
check-latest: true
cache: true
- name: golangci-lint
Expand All @@ -33,13 +33,13 @@ jobs:
- name: checkout
uses: actions/checkout@v3
- name: setup golang
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ^1.19
go-version: ^1.20
check-latest: true
cache: true
- name: test-coverage
uses: paambaati/codeclimate-action@v3.2.0
uses: paambaati/codeclimate-action@v5.0.0
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
with:
Expand All @@ -55,9 +55,9 @@ jobs:
- name: checkout
uses: actions/checkout@v3
- name: setup golang
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ^1.19
go-version: ^1.20
check-latest: true
cache: true
- name: init codeql
Expand Down
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ linters:
- nonamedreturns
- exhaustruct
- testpackage
- structcheck
- varnamelen
- depguard
- ireturn
- gofumpt

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Generic 2D grid
- [Ray-based line of sight](https://en.wikipedia.org/wiki/Line_of_sight_(video_games))
- [Recursive ShadowCasting](http://www.roguebasin.com/index.php/Shadow_casting)
- [Dijkstra maps](http://www.roguebasin.com/index.php/Dijkstra_Maps_Visualized)
- [Bresenham's lines](https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm)
- 100% test cover

# usage
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/s0rg/grid

go 1.19
go 1.20

require (
github.com/s0rg/array2d v1.0.0
github.com/s0rg/set v1.0.0
github.com/s0rg/vec2d v1.0.2
github.com/s0rg/vec2d v1.1.0
)

require golang.org/x/exp v0.0.0-20221114191408-850992195362 // indirect
require golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 // indirect
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ github.com/s0rg/array2d v1.0.0 h1:RqlhvPCq035CCEUDsm5nv/6NH6J0vRuU48gNG+6za2Q=
github.com/s0rg/array2d v1.0.0/go.mod h1:WY9vy75JDWyNnE2Qo3Gn8lMqiJYlgWCvK1Zauv7QiuU=
github.com/s0rg/set v1.0.0 h1:/MAmKEQ3Ltodli7/+e8iSTKNwhzmvxkmSDZO8BMTFbE=
github.com/s0rg/set v1.0.0/go.mod h1:n2TtovR+7TrBIztw+6muV9ZLRuDIimsd8YyTN+QBmHQ=
github.com/s0rg/vec2d v1.0.2 h1:HgjUL3VzZE5cizn6+MW9Snw7maxAGFO8nIFGuNRf11g=
github.com/s0rg/vec2d v1.0.2/go.mod h1:qnuPg+psBJLq7ICMqF2YJ6WW5L9sbY6Zy9xrEPNA/Hw=
golang.org/x/exp v0.0.0-20221114191408-850992195362 h1:NoHlPRbyl1VFI6FjwHtPQCN7wAMXI6cKcqrmXhOOfBQ=
golang.org/x/exp v0.0.0-20221114191408-850992195362/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
github.com/s0rg/vec2d v1.1.0 h1:2En34hYZFgNJwkRdzNt9ZCDVqlhCzRvsGRuNlPulFJw=
github.com/s0rg/vec2d v1.1.0/go.mod h1:/0pgb+2ax3mWfBZ1gb9LMMFawvmGyWpgCjk8a/ImCgg=
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090 h1:Di6/M8l0O2lCLc6VVRWhgCiApHV8MnQurBnFSHsQtNY=
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
129 changes: 97 additions & 32 deletions grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (m *Map[T]) Fill(filler func() T) {
func (m *Map[T]) Neighbours(
src image.Point,
dirs []image.Point,
walk Iter[T],
iter Iter[T],
) {
var (
cur image.Point
Expand All @@ -95,7 +95,7 @@ func (m *Map[T]) Neighbours(
continue
}

if !walk(cur, val) {
if !iter(cur, val) {
break
}
}
Expand All @@ -109,19 +109,19 @@ func (m *Map[T]) Path(
cost Cost[T],
) (rv []image.Point, ok bool) {
if !src.In(m.rc) {
return
return rv, false
}

var val T

if val, ok = m.cells.Get(dst.X, dst.Y); !ok {
return
return rv, false
}

tdist := dist(dst, src)

if _, ok = cost(dst, tdist, val); !ok {
return
return rv, false
}

var (
Expand Down Expand Up @@ -164,7 +164,7 @@ func (m *Map[T]) Path(
func (m *Map[T]) LineOfSight(
src image.Point,
distMax float64,
view Cast[T],
cast Cast[T],
) {
if !src.In(m.rc) {
return
Expand All @@ -173,7 +173,7 @@ func (m *Map[T]) LineOfSight(
const maxDegrees = 360.0

for t := float64(0); t < maxDegrees; t++ {
m.CastRay(src, t, distMax, view)
m.CastRay(src, t, distMax, cast)
}
}

Expand Down Expand Up @@ -250,7 +250,7 @@ func (m *Map[T]) CastRay(
func (m *Map[T]) CastShadow(
src image.Point,
distMax float64,
view Cast[T],
cast Cast[T],
) {
const (
octetMin = 1
Expand All @@ -262,18 +262,93 @@ func (m *Map[T]) CastShadow(
return
}

view(src, 0, val)
cast(src, 0, val)

for oct := octetMin; oct <= octetMax; oct++ {
m.emitShadow(src, oct, one, distMax, 0.0, one, view)
m.emitShadow(src, oct, one, distMax, 0.0, one, cast)
}
}

// DijkstraMap calculates 'Dijkstra' map for given points.
func (m *Map[T]) DijkstraMap(
targets []image.Point,
iter Iter[T],
) (rv *DijkstraMap) {
rv = &DijkstraMap{
ranks: array2d.New[uint16](m.cells.Bounds()),
}

rv.update(targets, func(p image.Point) (ok bool) {
val, _ := m.cells.Get(p.X, p.Y)

return iter(p, val)
})

return rv
}

// Line by Bresenham's algorithm.
func (m *Map[T]) LineBresenham(
src, dst image.Point,
iter Iter[T],
) {
if !src.In(m.rc) {
return
}

const two = 2

var (
sx, sy = 1, 1
dx, dy = abs(dst.X - src.X), -abs(dst.Y - src.Y)
e1 = dx + dy
e2 int
val T
ok bool
)

if src.X > dst.X {
sx = -1
}

if src.Y > dst.Y {
sy = -1
}

cur := src

for {
if val, ok = m.cells.Get(cur.X, cur.Y); !ok {
break
}

if !iter(cur, val) {
break
}

if cur.Eq(dst) {
break
}

e2 = e1 * two

if e2 >= dy {
cur.X += sx
e1 += dy
}

if e2 <= dx {
cur.Y += sy
e1 += dx
}
}
}

func (m *Map[T]) emitShadow(
src image.Point,
oct int,
dist, distMax, slopeLow, slopeHigh float64,
view Cast[T],
cast Cast[T],
) {
if dist > distMax {
return
Expand Down Expand Up @@ -302,35 +377,17 @@ func (m *Map[T]) emitShadow(
}

switch {
case view(pt, pdist, val):
case cast(pt, pdist, val):
gap = true
case gap:
m.emitShadow(src, oct, dist+1, distMax, slopeLow, (h-half)/dist, view)
m.emitShadow(src, oct, dist+1, distMax, slopeLow, (h-half)/dist, cast)

slopeLow = (h + half) / dist
gap = false
}
}

m.emitShadow(src, oct, dist+1, distMax, slopeLow, slopeHigh, view)
}

// DijkstraMap calculates 'Dijkstra' map for given points.
func (m *Map[T]) DijkstraMap(
targets []image.Point,
walk Iter[T],
) (rv *DijkstraMap) {
rv = &DijkstraMap{
ranks: array2d.New[uint16](m.cells.Bounds()),
}

rv.update(targets, func(p image.Point) (ok bool) {
val, _ := m.cells.Get(p.X, p.Y)

return walk(p, val)
})

return rv
m.emitShadow(src, oct, dist+1, distMax, slopeLow, slopeHigh, cast)
}

func radians(v float64) (d float64) {
Expand All @@ -356,3 +413,11 @@ func octantPoint(p image.Point, oct, d, h int) (rv image.Point) {

return p.Add(rv)
}

func abs(v int) int {
if v < 0 {
return -v
}

return v
}
Loading

0 comments on commit d0eded0

Please sign in to comment.