Commit ec56470b authored by ceddral's avatar ceddral
Browse files

board: MapStructure refactor, extract function listing adjacent structure parts

detecting adjacent parts of a structure and lack thereof will be used
to tell whether a structure is completed
parent 6de251f9
......@@ -127,6 +127,44 @@ type MapCursor struct {
id byte
}
func (b *Board) GetNeighbourStructures(c MapCursor, includeMissing bool) (res []MapCursor) {
res = make([]MapCursor, 0)
place, _ := b.Get(c.x, c.y)
nCs := getAdjacentCoords(c.x, c.y)
for _, nCoord := range nCs {
nPlace, nOk := b.Get(nCoord.x, nCoord.y)
dirx := nCoord.x - c.x
diry := nCoord.y - c.y
fmidx := 2 + (2 * dirx)
fmidy := 1 + diry
nfmidx := 2 - (2 * dirx)
nfmidy := 1 - diry
// iterate left middle right edge fields
for _, lmr := range []int{-1, 0, 1} {
fx := fmidx + lmr * diry
fy := fmidy + lmr * dirx
nfx := nfmidx + lmr * diry
nfy := nfmidy + lmr * dirx
// check if this edge field is part of our structure
if place.piece.f[place.rot][fy][fx].id != c.id {
continue
}
nc := MapCursor{nCoord.x, nCoord.y, 0}
if !nOk {
// cursor with 0 id represents missing neighbour / open structure edge
if includeMissing {
res = append(res, nc)
}
break
}
nid := nPlace.piece.f[nPlace.rot][nfy][nfx].id
nc.id = nid
res = append(res, nc)
}
}
return
}
func (b *Board) MapStructure(f func(interface{}, MapCursor)interface{}, acc interface{}, x, y int, fieldId byte) interface{} {
visited := make(map[MapCursor]struct{})
queue := []MapCursor{MapCursor{x, y, fieldId}}
......@@ -138,32 +176,9 @@ func (b *Board) MapStructure(f func(interface{}, MapCursor)interface{}, acc inte
}
acc = f(acc, c)
visited[c] = struct{}{}
place, _ := b.Get(c.x, c.y)
nCs, nPs := b.GetNeighbours(c.x, c.y)
for i, nCoord := range nCs {
nPlace := nPs[i]
dirx := nCoord.x - c.x
diry := nCoord.y - c.y
fmidx := 2 + (2 * dirx)
fmidy := 1 + diry
nfmidx := 2 - (2 * dirx)
nfmidy := 1 - diry
// iterate left middle right edge fields
for _, lmr := range []int{-1, 0, 1} {
fx := fmidx + lmr * diry
fy := fmidy + lmr * dirx
nfx := nfmidx + lmr * diry
nfy := nfmidy + lmr * dirx
// check if this edge field is part of our structure
if place.piece.f[place.rot][fy][fx].id != c.id {
continue
}
nid := nPlace.piece.f[nPlace.rot][nfy][nfx].id
nc := MapCursor{nCoord.x, nCoord.y, nid}
if _, ok := visited[nc]; ok {
continue
}
queue = append(queue, nc)
for _, n := range b.GetNeighbourStructures(c, false) {
if _, ok := visited[n]; !ok {
queue = append(queue, n)
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment