board.go 5.9 KB

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