2021-09-19 01:10:52 +02:00
|
|
|
package field
|
|
|
|
|
2021-09-19 01:14:58 +02:00
|
|
|
func (f Field) Get(x, y int) int {
|
|
|
|
if y >= f.y && y < f.y+f.ly {
|
|
|
|
l := f.lines[y-f.y]
|
|
|
|
if x >= l.x && x < l.x+l.l {
|
|
|
|
return l.columns[x-l.x]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ' '
|
|
|
|
}
|
|
|
|
|
2021-09-19 01:10:52 +02:00
|
|
|
func (f Field) isIn(x, y int) bool {
|
|
|
|
return x >= f.x && x < f.x+f.lx && y >= f.y && y < f.y+f.ly
|
|
|
|
}
|
2021-09-19 02:19:58 +02:00
|
|
|
|
|
|
|
func (f *Field) Set(x, y, v int) {
|
2021-09-26 19:29:00 +02:00
|
|
|
if v == ' ' {
|
|
|
|
f.Blank(x, y)
|
|
|
|
return
|
|
|
|
}
|
2021-09-19 02:19:58 +02:00
|
|
|
if y >= f.y {
|
|
|
|
if y < f.y+f.ly {
|
|
|
|
l := &f.lines[y-f.y]
|
|
|
|
if l.l == 0 {
|
|
|
|
// an empty line is a special case
|
|
|
|
l.x = x
|
|
|
|
l.l = 1
|
|
|
|
l.columns = append(l.columns, v)
|
|
|
|
} else if x >= l.x {
|
|
|
|
if x < l.x+l.l {
|
|
|
|
// just set the value
|
|
|
|
l.columns[x-l.x] = v
|
|
|
|
} else {
|
|
|
|
// append columns
|
2021-09-26 19:29:00 +02:00
|
|
|
newL := x - l.x + 1
|
2021-09-19 02:19:58 +02:00
|
|
|
for i := l.l; i < newL-1; i++ {
|
|
|
|
l.columns = append(l.columns, ' ')
|
|
|
|
}
|
|
|
|
l.columns = append(l.columns, v)
|
|
|
|
l.l = newL
|
2021-09-26 19:29:00 +02:00
|
|
|
if f.lx-f.x < l.l-l.x {
|
2021-09-19 02:19:58 +02:00
|
|
|
f.lx = l.l - l.x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// prepend columns
|
|
|
|
newL := l.l + l.x - x
|
|
|
|
c := make([]int, newL)
|
|
|
|
c[0] = v
|
2021-09-26 19:29:00 +02:00
|
|
|
for i := 1; i < l.x-x; i++ {
|
|
|
|
c[i] = ' '
|
2021-09-19 02:19:58 +02:00
|
|
|
}
|
2021-09-26 19:29:00 +02:00
|
|
|
for i := l.x - x; i < newL; i++ {
|
|
|
|
c[i] = l.columns[i-l.x+x]
|
2021-09-19 02:19:58 +02:00
|
|
|
}
|
|
|
|
l.columns = c
|
|
|
|
l.x = x
|
|
|
|
l.l = newL
|
|
|
|
if f.x > x {
|
2021-09-26 19:29:00 +02:00
|
|
|
f.lx = f.lx + f.x - x
|
2021-09-19 02:19:58 +02:00
|
|
|
f.x = x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// append lines
|
|
|
|
newLy := y - f.y + 1
|
|
|
|
for i := f.ly; i < newLy-1; i++ {
|
|
|
|
f.lines = append(f.lines, Line{})
|
|
|
|
}
|
|
|
|
f.lines = append(f.lines, Line{x: x, l: 1, columns: []int{v}})
|
|
|
|
f.ly = newLy
|
|
|
|
if f.x > x {
|
2021-09-26 19:29:00 +02:00
|
|
|
f.lx = f.lx + f.x - x
|
2021-09-19 02:19:58 +02:00
|
|
|
f.x = x
|
2021-09-26 19:29:00 +02:00
|
|
|
}
|
|
|
|
if f.lx-f.x < x {
|
2021-09-19 02:19:58 +02:00
|
|
|
f.lx = x - f.x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// prepend lines
|
|
|
|
newLy := f.ly + f.y - y
|
|
|
|
lines := make([]Line, newLy)
|
2021-09-26 19:29:00 +02:00
|
|
|
lines[0] = Line{x: x, l: 1, columns: []int{v}}
|
|
|
|
for j := f.y - y; j < newLy; j++ {
|
|
|
|
lines[j] = f.lines[j-f.y+y]
|
2021-09-19 02:19:58 +02:00
|
|
|
}
|
|
|
|
f.lines = lines
|
|
|
|
f.y = y
|
|
|
|
f.ly = newLy
|
|
|
|
if f.x > x {
|
2021-09-26 19:29:00 +02:00
|
|
|
f.lx = f.lx + f.x - x
|
2021-09-19 02:19:58 +02:00
|
|
|
f.x = x
|
2021-09-26 19:29:00 +02:00
|
|
|
}
|
|
|
|
if f.lx-f.x < x {
|
2021-09-19 02:19:58 +02:00
|
|
|
f.lx = x - f.x + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 01:26:49 +02:00
|
|
|
|
|
|
|
func (f Field) Dump() (int, int, int, int) {
|
|
|
|
return f.x, f.y, f.lx, f.ly
|
|
|
|
}
|