board.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package board
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "log"
  8. "math/rand"
  9. "net/http"
  10. "time"
  11. )
  12. var setMask [4]byte = [4]byte{0xFC, 0xF3, 0xCF, 0x3F}
  13. var getMask [4]byte = [4]byte{0x03, 0x0C, 0x30, 0xC0}
  14. type Board struct {
  15. Width int `json:"cols"`
  16. Height int `json:"rows"`
  17. Finish Coordinate `json:"finish"`
  18. data []byte `json:"-"`
  19. }
  20. type req struct {
  21. x, y, width, height int
  22. }
  23. type response struct {
  24. boardTile io.ReadCloser
  25. x, y int
  26. }
  27. const maxIceWidth = 26
  28. // Create a new randomly generated board.
  29. func New(width, height int) *Board {
  30. return NewPartial(0, 0, width, height, width, height)
  31. }
  32. func NewPartial(startX, startY, width, height, totalWidth, totalHeight int) *Board {
  33. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  34. return newPartial(startX, startY, width, height, totalWidth, totalHeight, r)
  35. }
  36. func NewRemote(width, height int) *Board {
  37. b := &Board{
  38. Width: width,
  39. Height: height,
  40. data: make([]byte, (width*height)/4+1),
  41. }
  42. log.Printf("Start creating board %v x %v", width, height)
  43. regionWidth := 16
  44. regionHeight := 16
  45. regionWidthNum := width / regionWidth
  46. regionHeightNum := height / regionHeight
  47. returnChan := make(chan response)
  48. for i := 0; i < regionWidthNum; i++ {
  49. for j := 0; j < regionHeightNum; j++ {
  50. x := i * regionWidth
  51. y := j * regionHeight
  52. region := req{
  53. x: x,
  54. y: y,
  55. width: regionWidth,
  56. height: regionHeight,
  57. }
  58. go b.genRegion(region, returnChan)
  59. }
  60. }
  61. answers := 0
  62. for regionWidthNum*regionHeightNum > answers {
  63. result := <-returnChan
  64. fmt.Printf("Received partial: x=%v, y=%v", result.x, result.y)
  65. for ry := 0; ry < regionHeight; ry++ {
  66. index := result.x + (result.y+ry)*b.Width
  67. fmt.Printf("index=%v\n", index)
  68. i := index / 4 // tiles per byte
  69. ba := make([]byte, regionWidth/4)
  70. result.boardTile.Read(ba)
  71. for _, v := range ba {
  72. fmt.Printf("%b ", v)
  73. }
  74. fmt.Println()
  75. copy(b.data[i:i+regionWidth/4], ba)
  76. }
  77. answers++
  78. }
  79. igloY := rand.Intn(height-2) + 1
  80. b.Set(width-2, igloY, Iglo)
  81. b.Finish.X = width - 2
  82. b.Finish.Y = igloY
  83. b.Set(width-2, igloY-1, Water)
  84. b.Set(width-2, igloY+1, Water)
  85. log.Printf("Finished creating board %v x %v\n", width, height)
  86. return b
  87. }
  88. func (b *Board) genRegion(region req, responseChan chan response) {
  89. 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)
  90. log.Printf("Requested generation of tile with url: %v\n", requestUrl)
  91. resp, _ := http.Get(requestUrl)
  92. result := response{
  93. x: region.x,
  94. y: region.y,
  95. boardTile: resp.Body,
  96. }
  97. responseChan <- result
  98. }
  99. func (b *Board) Set(x, y int, t TileType) {
  100. index := uint(y*b.Width + x)
  101. i := index / 4
  102. p := index % 4
  103. b.data[i] = (b.data[i] & setMask[p]) | byte(t)<<(p<<1)
  104. }
  105. func (b *Board) Get(x, y int) TileType {
  106. index := y*b.Width + x
  107. i := index / 4
  108. p := index % 4
  109. return TileType((b.data[i] & getMask[p]) >> uint(p<<1))
  110. }
  111. func (b *Board) WriteData(w io.Writer) {
  112. n, err := w.Write(b.data)
  113. if err != nil {
  114. fmt.Errorf("Error writing board after %v bytes: %v", n, err)
  115. }
  116. }
  117. func (b *Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
  118. sc, sr, cols, rows := sanitizeViewPort(b, startCol, startRow, cols, rows)
  119. fmt.Fprintf(w, "{\"x\":%v,\"y\":%v,\"rows\":%v,\"cols\":%v,\"tiles\":[", sc, sr, cols, rows)
  120. for y := startRow; y < sr+rows; y++ {
  121. for x := startCol; x < sc+cols; x++ {
  122. if !(x == startCol && y == startRow) {
  123. w.Write([]byte{','})
  124. }
  125. t := &jsonTile{x, y, b.Get(x, y).Name(), ""}
  126. bs, _ := json.Marshal(t)
  127. w.Write(bs)
  128. }
  129. }
  130. fmt.Fprintf(w, "]}")
  131. }
  132. func (b *Board) String() string {
  133. var buffer bytes.Buffer
  134. for y := 0; y < b.Height; y++ {
  135. for x := 0; x < b.Width; x++ {
  136. buffer.WriteString(b.Get(x, y).String())
  137. }
  138. buffer.WriteString(fmt.Sprintln())
  139. }
  140. return buffer.String()
  141. }
  142. func new(width, height int, r *rand.Rand) *Board {
  143. return newPartial(0, 0, width, height, width, height, r)
  144. }
  145. func newPartial(startX, startY, width, height, totalWidth, totalHeight int, r *rand.Rand) *Board {
  146. b := &Board{
  147. Width: width,
  148. Height: height,
  149. data: make([]byte, (width*height)/4+1),
  150. }
  151. leftLimit := (totalWidth-maxIceWidth)/2 - startX
  152. rightLimit := totalWidth/2 + maxIceWidth/2 - startX
  153. mid := totalWidth/2 - startX
  154. for y := 0; y < height; y++ {
  155. for x := 0; x < width; x++ {
  156. switch {
  157. case r.Intn(10) > 7:
  158. b.Set(x, y, Rock)
  159. case x > leftLimit && x < rightLimit && r.Intn(maxIceWidth) >= abs(mid-x):
  160. b.Set(x, y, Ice)
  161. default:
  162. b.Set(x, y, Water)
  163. }
  164. }
  165. }
  166. return b
  167. }
  168. func sanitizeViewPort(b *Board, startCol, startRow, cols, rows int) (int, int, int, int) {
  169. if startCol > b.Width {
  170. startCol = b.Width
  171. }
  172. if startCol < 0 {
  173. cols += startCol
  174. startCol = 0
  175. }
  176. if startRow > b.Height {
  177. startRow = b.Height
  178. }
  179. if startRow < 0 {
  180. rows += startRow
  181. startRow = 0
  182. }
  183. if startCol+cols > b.Width {
  184. cols = b.Width - startCol
  185. }
  186. if startRow+rows > b.Height {
  187. rows = b.Height - startRow
  188. }
  189. if cols < 0 {
  190. cols = 0
  191. }
  192. if rows < 0 {
  193. rows = 0
  194. }
  195. return startCol, startRow, cols, rows
  196. }
  197. func abs(a int) int {
  198. if a < 0 {
  199. return -a
  200. }
  201. return a
  202. }