|
|
@@ -5,7 +5,9 @@ import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
+ "log"
|
|
|
"math/rand"
|
|
|
+ "net/http"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
@@ -19,6 +21,15 @@ type Board struct {
|
|
|
data []byte `json:"-"`
|
|
|
}
|
|
|
|
|
|
+type req struct {
|
|
|
+ x, y, width, height int
|
|
|
+}
|
|
|
+
|
|
|
+type response struct {
|
|
|
+ boardTile io.ReadCloser
|
|
|
+ x, y int
|
|
|
+}
|
|
|
+
|
|
|
const maxIceWidth = 26
|
|
|
|
|
|
// Create a new randomly generated board.
|
|
|
@@ -31,6 +42,82 @@ func NewPartial(startX, startY, width, height, totalWidth, totalHeight int) *Boa
|
|
|
return newPartial(startX, startY, width, height, totalWidth, totalHeight, r)
|
|
|
}
|
|
|
|
|
|
+func NewRemote(width, height int) *Board {
|
|
|
+ b := &Board{
|
|
|
+ Width: width,
|
|
|
+ Height: height,
|
|
|
+ data: make([]byte, (width*height)/4+1),
|
|
|
+ }
|
|
|
+
|
|
|
+ log.Printf("Start creating board %v x %v", width, height)
|
|
|
+
|
|
|
+ regionWidth := 8
|
|
|
+ regionHeight := 8
|
|
|
+
|
|
|
+ regionWidthNum := width / regionWidth
|
|
|
+ regionHeightNum := height / regionHeight
|
|
|
+
|
|
|
+ returnChan := make(chan response)
|
|
|
+
|
|
|
+ for i := 0; i < regionWidthNum; i++ {
|
|
|
+ for j := 0; j < regionHeightNum; j++ {
|
|
|
+ x := i * regionWidth
|
|
|
+ y := j * regionHeight
|
|
|
+
|
|
|
+ gbt := req{
|
|
|
+ x: x,
|
|
|
+ y: y,
|
|
|
+ width: regionWidth,
|
|
|
+ height: regionHeight,
|
|
|
+ }
|
|
|
+ go b.genRegion(gbt, returnChan)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ answers := 0
|
|
|
+ for regionWidthNum+regionHeightNum > answers {
|
|
|
+
|
|
|
+ result := <-returnChan
|
|
|
+
|
|
|
+ n := -1
|
|
|
+
|
|
|
+ for n != 0 {
|
|
|
+ index := result.x*b.Width + result.y
|
|
|
+ i := index / 4 // tiles per byte
|
|
|
+
|
|
|
+ // TODO
|
|
|
+ ba := b.data[i : i+regionWidth/4]
|
|
|
+ n, _ = result.boardTile.Read(ba)
|
|
|
+ }
|
|
|
+ answers++
|
|
|
+ }
|
|
|
+
|
|
|
+ igloY := rand.Intn(height-2) + 1
|
|
|
+
|
|
|
+ b.Set(width-2, igloY, Iglo)
|
|
|
+ b.Finish.X = width - 2
|
|
|
+ b.Finish.Y = igloY
|
|
|
+ b.Set(width-2, igloY-1, Water)
|
|
|
+ b.Set(width-2, igloY+1, Water)
|
|
|
+ log.Printf("Finished creating board %v x %v\n", width, height)
|
|
|
+
|
|
|
+ return b
|
|
|
+}
|
|
|
+
|
|
|
+func (b *Board) genRegion(req req, responseChan chan response) {
|
|
|
+ requestUrl := fmt.Sprintf("http://localhost:8081/?totalWidth=%v&totalHeight=%vwidth=%v&height=%v&x=%v&y=%v", b.Width, b.Height, req.width, req.height, req.x, req.y)
|
|
|
+ log.Printf("Requested generation of tile with url: %v\n", requestUrl)
|
|
|
+ resp, _ := http.Get(requestUrl)
|
|
|
+
|
|
|
+ result := response{
|
|
|
+ x: req.x,
|
|
|
+ y: req.y,
|
|
|
+ boardTile: resp.Body,
|
|
|
+ }
|
|
|
+
|
|
|
+ responseChan <- result
|
|
|
+}
|
|
|
+
|
|
|
func (b *Board) Set(x, y int, t TileType) {
|
|
|
index := uint(y*b.Width + x)
|
|
|
i := index / 4
|
|
|
@@ -92,7 +179,7 @@ func newPartial(startX, startY, width, height, totalWidth, totalHeight int, r *r
|
|
|
b := &Board{
|
|
|
Width: width,
|
|
|
Height: height,
|
|
|
- data: make([]byte, width*height),
|
|
|
+ data: make([]byte, (width*height)/4+1),
|
|
|
}
|
|
|
|
|
|
leftLimit := (totalWidth-maxIceWidth)/2 - startX
|