refactor: plotter bitmap
This commit is contained in:
+29
-40
@@ -1,12 +1,11 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
"github.com/iliadenisov/galaxy/pkg/generator/draw"
|
||||
"github.com/iliadenisov/galaxy/pkg/bitmap"
|
||||
)
|
||||
|
||||
type Map struct {
|
||||
@@ -33,8 +32,11 @@ type PlanetarySystem struct {
|
||||
}
|
||||
|
||||
type Plotter struct {
|
||||
factor float64
|
||||
sectors draw.Plane
|
||||
factor float64
|
||||
clearFn func()
|
||||
circleFn func(x, y int, r float64)
|
||||
freeCountFn func() int
|
||||
freeNumberToCoordFn func(int) (int, int)
|
||||
}
|
||||
|
||||
func Generate(param MapParameter) (result Map) {
|
||||
@@ -69,20 +71,16 @@ func Generate(param MapParameter) (result Map) {
|
||||
coord := result.newPlanet(float64(param.BigPlanets.MinDistanceHW))
|
||||
planet := pl(coord, param.BigPlanets)
|
||||
result.addPlanet(planet)
|
||||
// result.FreePlanets = append(result.FreePlanets, planet)
|
||||
}
|
||||
|
||||
for player := 0; player < int(param.Players); player++ {
|
||||
fmt.Println("generating HW #", player)
|
||||
coord := result.newPlanet(float64(param.HW_MinDistance))
|
||||
planet := Planet{Position: coord, Size: float64(param.HW_Size), Resources: float64(param.HW_Resources)}
|
||||
// fmt.Println("HW: ", planet)
|
||||
result.HomePlanets[player] = PlanetarySystem{HW: planet}
|
||||
}
|
||||
|
||||
fmt.Println("free sectors left: ", len(result.plotter.freeSectors()))
|
||||
|
||||
result.plotter.sectors.Clear()
|
||||
result.plotter.clearFn()
|
||||
|
||||
for _, hw := range result.HomePlanets {
|
||||
result.plotter.markNoGoZone(hw.HW.Position.X, hw.HW.Position.Y, 5)
|
||||
@@ -96,21 +94,16 @@ func Generate(param MapParameter) (result Map) {
|
||||
|
||||
}
|
||||
|
||||
fmt.Println("free sectors after reset: ", len(result.plotter.freeSectors()))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (m Map) newPlanet(deadZoneRaduis float64) Coordinate {
|
||||
fs := m.plotter.freeSectors()
|
||||
fmt.Println("free sectors: ", len(fs))
|
||||
fsCount := len(fs)
|
||||
fsCount := m.plotter.freeCountFn()
|
||||
if fsCount == 0 {
|
||||
panic("no more space for planets")
|
||||
}
|
||||
next := rand.Intn(fsCount)
|
||||
x := fs[next][0]
|
||||
y := fs[next][1]
|
||||
x, y := m.plotter.freeNumberToCoordFn(next)
|
||||
fmt.Println("planet on plot: x =", x, "y =", y)
|
||||
planetX := float64(x)*m.plotter.factor + rand.Float64()*m.plotter.factor
|
||||
planetY := float64(y)*m.plotter.factor + rand.Float64()*m.plotter.factor
|
||||
@@ -122,39 +115,35 @@ func (m *Map) addPlanet(planet Planet) {
|
||||
m.FreePlanets = append(m.FreePlanets, planet)
|
||||
}
|
||||
|
||||
func (p Plotter) freeSectors() (result [][]uint) {
|
||||
result = make([][]uint, 0)
|
||||
for x := uint(0); x < p.sectors.Width; x++ {
|
||||
for y := uint(0); y < p.sectors.Height; y++ {
|
||||
if !p.sectors.Marked(x, y) {
|
||||
result = append(result, []uint{x, y})
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p Plotter) markDeadZone(x, y int, radius float64) {
|
||||
p.sectors.Circle(x, y, radius)
|
||||
}
|
||||
|
||||
func (p Plotter) markNoGoZone(x, y float64, radius float64) { // TODO: test
|
||||
p.markDeadZone(int(x/p.factor), int(y/p.factor), radius)
|
||||
}
|
||||
|
||||
func (p Plotter) markDeadZone(x, y int, radius float64) {
|
||||
p.circleFn(x, y, radius)
|
||||
}
|
||||
|
||||
func NewMap(width, height, players uint) Map {
|
||||
var factor float64 = 1.0
|
||||
sectorsX := uint(float64(width) / factor)
|
||||
sectorsY := uint(float64(height) / factor)
|
||||
sectorsX := uint32(float64(width) / factor)
|
||||
sectorsY := uint32(float64(height) / factor)
|
||||
bm := bitmap.NewBitmap(sectorsX, sectorsY)
|
||||
return Map{
|
||||
Width: width,
|
||||
Height: height,
|
||||
HomePlanets: make([]PlanetarySystem, players),
|
||||
plotter: Plotter{
|
||||
factor: factor,
|
||||
sectors: draw.New(sectorsX, sectorsY)}}
|
||||
}
|
||||
|
||||
func errfmt(message string) error {
|
||||
return errors.New("generator: " + message)
|
||||
factor: factor,
|
||||
clearFn: bm.Clear,
|
||||
circleFn: func(x, y int, r float64) { bm.Circle(x, y, r, true) },
|
||||
freeCountFn: bm.FreeCount,
|
||||
freeNumberToCoordFn: func(n int) (x int, y int) {
|
||||
x, y, err := bm.GetFreeN(n)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user