board.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 answers < regionWidthNum*regionHeightNum {
  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. result.boardTile.Read(b.data[i:i+regionWidth/4])
  70. for _, v := range b.data[i:i+regionWidth/4] {
  71. fmt.Printf("%b ", v)
  72. }
  73. fmt.Println()
  74. }
  75. answers++
  76. }
  77. igloY := rand.Intn(height-2) + 1
  78. b.Set(width-2, igloY, Iglo)
  79. b.Finish.X = width - 2
  80. b.Finish.Y = igloY
  81. b.Set(width-2, igloY-1, Water)
  82. b.Set(width-2, igloY+1, Water)
  83. log.Printf("Finished creating board %v x %v\n", width, height)
  84. return b
  85. }
  86. func (b *Board) genRegion(region req, responseChan chan response) {
  87. 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)
  88. log.Printf("Requested generation of tile with url: %v\n", requestUrl)
  89. resp, _ := http.Get(requestUrl)
  90. result := response{
  91. x: region.x,
  92. y: region.y,
  93. boardTile: resp.Body,
  94. }
  95. responseChan <- result
  96. }
  97. func (b *Board) Set(x, y int, t TileType) {
  98. index := uint(y*b.Width + x)
  99. i := index / 4
  100. p := index % 4
  101. b.data[i] = (b.data[i] & setMask[p]) | byte(t)<<(p<<1)
  102. }
  103. func (b *Board) Get(x, y int) TileType {
  104. index := y*b.Width + x
  105. i := index / 4
  106. p := index % 4
  107. return TileType((b.data[i] & getMask[p]) >> uint(p<<1))
  108. }
  109. func (b *Board) WriteData(w io.Writer) {
  110. n, err := w.Write(b.data)
  111. if err != nil {
  112. fmt.Errorf("Error writing board after %v bytes: %v", n, err)
  113. }
  114. }
  115. func (b *Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
  116. sc, sr, cols, rows := sanitizeViewPort(b, startCol, startRow, cols, rows)
  117. fmt.Fprintf(w, "{\"x\":%v,\"y\":%v,\"rows\":%v,\"cols\":%v,\"tiles\":[", sc, sr, cols, rows)
  118. for y := startRow; y < sr+rows; y++ {
  119. for x := startCol; x < sc+cols; x++ {
  120. if !(x == startCol && y == startRow) {
  121. w.Write([]byte{','})
  122. }
  123. t := &jsonTile{x, y, b.Get(x, y).Name(), ""}
  124. bs, _ := json.Marshal(t)
  125. w.Write(bs)
  126. }
  127. }
  128. fmt.Fprintf(w, "]}")
  129. }
  130. func (b *Board) String() string {
  131. var buffer bytes.Buffer
  132. for y := 0; y < b.Height; y++ {
  133. for x := 0; x < b.Width; x++ {
  134. buffer.WriteString(b.Get(x, y).String())
  135. }
  136. buffer.WriteString(fmt.Sprintln())
  137. }
  138. return buffer.String()
  139. }
  140. func new(width, height int, r *rand.Rand) *Board {
  141. return newPartial(0, 0, width, height, width, height, r)
  142. }
  143. func newPartial(startX, startY, width, height, totalWidth, totalHeight int, r *rand.Rand) *Board {
  144. b := &Board{
  145. Width: width,
  146. Height: height,
  147. data: make([]byte, (width*height)/4+1),
  148. }
  149. leftLimit := (totalWidth-maxIceWidth)/2 - startX
  150. rightLimit := totalWidth/2 + maxIceWidth/2 - startX
  151. mid := totalWidth/2 - startX
  152. for y := 0; y < height; y++ {
  153. for x := 0; x < width; x++ {
  154. switch {
  155. case r.Intn(10) > 7:
  156. b.Set(x, y, Rock)
  157. case x > leftLimit && x < rightLimit && r.Intn(maxIceWidth) >= abs(mid-x):
  158. b.Set(x, y, Ice)
  159. default:
  160. b.Set(x, y, Water)
  161. }
  162. }
  163. }
  164. return b
  165. }
  166. func sanitizeViewPort(b *Board, startCol, startRow, cols, rows int) (int, int, int, int) {
  167. if startCol > b.Width {
  168. startCol = b.Width
  169. }
  170. if startCol < 0 {
  171. cols += startCol
  172. startCol = 0
  173. }
  174. if startRow > b.Height {
  175. startRow = b.Height
  176. }
  177. if startRow < 0 {
  178. rows += startRow
  179. startRow = 0
  180. }
  181. if startCol+cols > b.Width {
  182. cols = b.Width - startCol
  183. }
  184. if startRow+rows > b.Height {
  185. rows = b.Height - startRow
  186. }
  187. if cols < 0 {
  188. cols = 0
  189. }
  190. if rows < 0 {
  191. rows = 0
  192. }
  193. return startCol, startRow, cols, rows
  194. }
  195. func abs(a int) int {
  196. if a < 0 {
  197. return -a
  198. }
  199. return a
  200. }