board.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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, width, height 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() + int64(startY+startY)))
  34. return newPartial(startX, startY, width, height, totalWidth, totalHeight, r)
  35. }
  36. func NewRemote(width, height int) *Board {
  37. b := newBlank(width, height)
  38. log.Printf("Start creating board %v x %v", width, height)
  39. const regionWidth = 32
  40. const regionHeight = 32
  41. regionWidthNum := (width + (regionWidth - 1)) / regionWidth
  42. regionHeightNum := (height + (regionHeight - 1)) / regionHeight
  43. returnChan := make(chan response)
  44. for i := 0; i < regionWidthNum; i++ {
  45. for j := 0; j < regionHeightNum; j++ {
  46. var partialWidth, partialHeight int
  47. x := i * regionWidth
  48. y := j * regionHeight
  49. if x+regionWidth > b.Width {
  50. partialWidth = b.Width - x
  51. } else {
  52. partialWidth = regionWidth
  53. }
  54. if y+regionHeight > b.Height {
  55. partialHeight = b.Height - y
  56. } else {
  57. partialHeight = regionHeight
  58. }
  59. region := req{
  60. x: x,
  61. y: y,
  62. width: partialWidth,
  63. height: partialHeight,
  64. }
  65. go b.genRegion(region, returnChan)
  66. }
  67. }
  68. answers := 0
  69. for regionWidthNum*regionHeightNum > answers {
  70. result := <-returnChan
  71. log.Printf("Received partial: x=%v, y=%v, width=%v, height=%v\n", result.x, result.y, result.width, result.height)
  72. for ry := 0; ry < result.height; ry++ {
  73. i, _ := b.xyToIndex(result.x, result.y+ry)
  74. result.boardTile.Read(b.data[i : i+byteIndex(result.width)])
  75. }
  76. answers++
  77. }
  78. b.placeIglo()
  79. log.Printf("Finished creating board %v x %v\n", width, height)
  80. return b
  81. }
  82. func (b *Board) placeIglo() {
  83. b.Finish.X = b.Width - 2
  84. b.Finish.Y = rand.Intn(b.Height-2) + 1
  85. b.Set(b.Finish.X, b.Finish.Y, Iglo)
  86. b.Set(b.Finish.X, b.Finish.Y-1, Water)
  87. b.Set(b.Finish.X, b.Finish.Y+1, Water)
  88. b.Set(b.Finish.X-1, b.Finish.Y, Water)
  89. b.Set(b.Finish.X+1, b.Finish.Y, Water)
  90. }
  91. func (b *Board) genRegion(region req, responseChan chan response) {
  92. 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)
  93. log.Printf("Requested generation of tile with url: %v\n", requestUrl)
  94. resp, _ := http.Get(requestUrl)
  95. result := response{
  96. x: region.x,
  97. y: region.y,
  98. width: region.width,
  99. height: region.height,
  100. boardTile: resp.Body,
  101. }
  102. responseChan <- result
  103. }
  104. func (b *Board) Set(x, y int, t TileType) {
  105. i, p := b.xyToIndex(x, y)
  106. b.data[i] = (b.data[i] & setMask[p]) | byte(t)<<(p<<1)
  107. }
  108. func (b *Board) Get(x, y int) TileType {
  109. i, p := b.xyToIndex(x, y)
  110. return TileType((b.data[i] & getMask[p]) >> uint(p<<1))
  111. }
  112. // Returns the index and offset inside the byte.
  113. func (b *Board) xyToIndex(x, y int) (int, uint) {
  114. width := byteIndex(b.Width) * 4
  115. index := y*width + x
  116. i := index / 4
  117. p := index % 4
  118. return i, uint(p)
  119. }
  120. func byteIndex(x int) int {
  121. return (x + 3) / 4
  122. }
  123. func (b *Board) WriteData(w io.Writer) {
  124. n, err := w.Write(b.data)
  125. if err != nil {
  126. fmt.Errorf("Error writing board after %v bytes: %v", n, err)
  127. }
  128. }
  129. func (b *Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
  130. sc, sr, cols, rows := sanitizeViewPort(b, startCol, startRow, cols, rows)
  131. fmt.Fprintf(w, "{\"x\":%v,\"y\":%v,\"rows\":%v,\"cols\":%v,\"tiles\":[", sc, sr, cols, rows)
  132. for y := startRow; y < sr+rows; y++ {
  133. for x := startCol; x < sc+cols; x++ {
  134. if !(x == startCol && y == startRow) {
  135. w.Write([]byte{','})
  136. }
  137. t := &jsonTile{x, y, b.Get(x, y).Name(), ""}
  138. bs, _ := json.Marshal(t)
  139. w.Write(bs)
  140. }
  141. }
  142. fmt.Fprintf(w, "]}")
  143. }
  144. func (b *Board) String() string {
  145. var buffer bytes.Buffer
  146. for y := 0; y < b.Height; y++ {
  147. for x := 0; x < b.Width; x++ {
  148. buffer.WriteString(b.Get(x, y).String())
  149. }
  150. buffer.WriteString(fmt.Sprintln())
  151. }
  152. return buffer.String()
  153. }
  154. func newBlank(width, height int) *Board {
  155. return &Board{
  156. Width: width,
  157. Height: height,
  158. data: make([]byte, byteIndex(width)*height),
  159. }
  160. }
  161. func new(width, height int, r *rand.Rand) *Board {
  162. return newPartial(0, 0, width, height, width, height, r)
  163. }
  164. func newPartial(startX, startY, width, height, totalWidth, totalHeight int, r *rand.Rand) *Board {
  165. b := newBlank(width, height)
  166. leftLimit := (totalWidth-maxIceWidth)/2 - startX
  167. rightLimit := totalWidth/2 + maxIceWidth/2 - startX
  168. mid := totalWidth/2 - startX
  169. for y := 0; y < height; y++ {
  170. for x := 0; x < width; x++ {
  171. switch {
  172. case r.Intn(10) > 7:
  173. b.Set(x, y, Rock)
  174. case x > leftLimit && x < rightLimit && r.Intn(maxIceWidth) >= abs(mid-x):
  175. b.Set(x, y, Ice)
  176. default:
  177. b.Set(x, y, Water)
  178. }
  179. }
  180. }
  181. return b
  182. }
  183. func sanitizeViewPort(b *Board, startCol, startRow, cols, rows int) (int, int, int, int) {
  184. if startCol > b.Width {
  185. startCol = b.Width
  186. }
  187. if startCol < 0 {
  188. cols += startCol
  189. startCol = 0
  190. }
  191. if startRow > b.Height {
  192. startRow = b.Height
  193. }
  194. if startRow < 0 {
  195. rows += startRow
  196. startRow = 0
  197. }
  198. if startCol+cols > b.Width {
  199. cols = b.Width - startCol
  200. }
  201. if startRow+rows > b.Height {
  202. rows = b.Height - startRow
  203. }
  204. if cols < 0 {
  205. cols = 0
  206. }
  207. if rows < 0 {
  208. rows = 0
  209. }
  210. return startCol, startRow, cols, rows
  211. }
  212. func abs(a int) int {
  213. if a < 0 {
  214. return -a
  215. }
  216. return a
  217. }