|
|
@@ -21,16 +21,16 @@ type Board struct {
|
|
|
data []byte `json:"-"`
|
|
|
}
|
|
|
|
|
|
-type req struct {
|
|
|
- x, y, width, height int
|
|
|
-}
|
|
|
-
|
|
|
type response struct {
|
|
|
boardTile io.ReadCloser
|
|
|
x, y, width, height int
|
|
|
}
|
|
|
|
|
|
-const maxIceWidth = 26
|
|
|
+const (
|
|
|
+ maxIceWidth = 26
|
|
|
+ regionWidth = 32
|
|
|
+ regionHeight = 32
|
|
|
+)
|
|
|
|
|
|
// Create a new randomly generated board.
|
|
|
func New(width, height int) *Board {
|
|
|
@@ -43,60 +43,30 @@ func NewPartial(startX, startY, width, height, totalWidth, totalHeight int) *Boa
|
|
|
}
|
|
|
|
|
|
func NewRemote(width, height int) *Board {
|
|
|
- b := newBlank(width, height)
|
|
|
-
|
|
|
log.Printf("Start creating board %v x %v", width, height)
|
|
|
+ b := newBlank(width, height)
|
|
|
|
|
|
- const regionWidth = 32
|
|
|
- const regionHeight = 32
|
|
|
-
|
|
|
- regionWidthNum := (width + (regionWidth - 1)) / regionWidth
|
|
|
- regionHeightNum := (height + (regionHeight - 1)) / regionHeight
|
|
|
-
|
|
|
- returnChan := make(chan response)
|
|
|
-
|
|
|
- for i := 0; i < regionWidthNum; i++ {
|
|
|
- for j := 0; j < regionHeightNum; j++ {
|
|
|
- var partialWidth, partialHeight int
|
|
|
- x := i * regionWidth
|
|
|
- y := j * regionHeight
|
|
|
-
|
|
|
- if x+regionWidth > b.Width {
|
|
|
- partialWidth = b.Width - x
|
|
|
- } else {
|
|
|
- partialWidth = regionWidth
|
|
|
- }
|
|
|
-
|
|
|
- if y+regionHeight > b.Height {
|
|
|
- partialHeight = b.Height - y
|
|
|
- } else {
|
|
|
- partialHeight = regionHeight
|
|
|
- }
|
|
|
+ xRegions := (width + (regionWidth - 1)) / regionWidth
|
|
|
+ yRegions := (height + (regionHeight - 1)) / regionHeight
|
|
|
|
|
|
- region := req{
|
|
|
- x: x,
|
|
|
- y: y,
|
|
|
- width: partialWidth,
|
|
|
- height: partialHeight,
|
|
|
- }
|
|
|
- go b.genRegion(region, returnChan)
|
|
|
- }
|
|
|
- }
|
|
|
+ returnChan := make(chan response, 32)
|
|
|
+ go b.genRegions(xRegions, yRegions, returnChan)
|
|
|
|
|
|
- answers := 0
|
|
|
- for regionWidthNum*regionHeightNum > answers {
|
|
|
+ for answers := 0; xRegions*yRegions > answers; answers++ {
|
|
|
result := <-returnChan
|
|
|
log.Printf("Received partial: x=%v, y=%v, width=%v, height=%v\n", result.x, result.y, result.width, result.height)
|
|
|
for ry := 0; ry < result.height; ry++ {
|
|
|
i, _ := b.xyToIndex(result.x, result.y+ry)
|
|
|
- result.boardTile.Read(b.data[i : i+byteIndex(result.width)])
|
|
|
+ n, err := result.boardTile.Read(b.data[i : i+byteIndex(result.width)])
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("ERROR reading after %v bytes: %v", n, err)
|
|
|
+ }
|
|
|
}
|
|
|
- answers++
|
|
|
+ result.boardTile.Close()
|
|
|
}
|
|
|
|
|
|
b.placeIglo()
|
|
|
log.Printf("Finished creating board %v x %v\n", width, height)
|
|
|
-
|
|
|
return b
|
|
|
}
|
|
|
|
|
|
@@ -110,20 +80,47 @@ func (b *Board) placeIglo() {
|
|
|
b.Set(b.Finish.X+1, b.Finish.Y, Water)
|
|
|
}
|
|
|
|
|
|
-func (b *Board) genRegion(region req, responseChan chan response) {
|
|
|
- requestUrl := fmt.Sprintf("http://localhost:8081/?totalWidth=%v&totalHeight=%v&width=%v&height=%v&x=%v&y=%v", b.Width, b.Height, region.width, region.height, region.x, region.y)
|
|
|
- log.Printf("Requested generation of tile with url: %v\n", requestUrl)
|
|
|
- resp, _ := http.Get(requestUrl)
|
|
|
-
|
|
|
- result := response{
|
|
|
- x: region.x,
|
|
|
- y: region.y,
|
|
|
- width: region.width,
|
|
|
- height: region.height,
|
|
|
- boardTile: resp.Body,
|
|
|
- }
|
|
|
+func (b *Board) genRegions(xRegions, yRegions int, responseChan chan response) {
|
|
|
+
|
|
|
+ for i := 0; i < xRegions; i++ {
|
|
|
+ for j := 0; j < yRegions; j++ {
|
|
|
+ var partialWidth, partialHeight int
|
|
|
+ x := i * regionWidth
|
|
|
+ y := j * regionHeight
|
|
|
|
|
|
- responseChan <- result
|
|
|
+ if x+regionWidth > b.Width {
|
|
|
+ partialWidth = b.Width - x
|
|
|
+ } else {
|
|
|
+ partialWidth = regionWidth
|
|
|
+ }
|
|
|
+
|
|
|
+ if y+regionHeight > b.Height {
|
|
|
+ partialHeight = b.Height - y
|
|
|
+ } else {
|
|
|
+ partialHeight = regionHeight
|
|
|
+ }
|
|
|
+
|
|
|
+ requestUrl := fmt.Sprintf("http://localhost:8081/?totalWidth=%v&totalHeight=%v&width=%v&height=%v&x=%v&y=%v", b.Width, b.Height, partialWidth, partialHeight, x, y)
|
|
|
+ log.Printf("Requested generation of tile with url: %v\n", requestUrl)
|
|
|
+
|
|
|
+ resp, err := http.Get(requestUrl)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("ERROR requesting region: %v", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ result := response{
|
|
|
+ x: x,
|
|
|
+ y: y,
|
|
|
+ width: partialHeight,
|
|
|
+ height: partialHeight,
|
|
|
+ boardTile: resp.Body,
|
|
|
+ }
|
|
|
+
|
|
|
+ responseChan <- result
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func (b *Board) Set(x, y int, t TileType) {
|