| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- package board
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "io"
- "log"
- "math/rand"
- "net/http"
- "time"
- )
- var setMask [4]byte = [4]byte{0xFC, 0xF3, 0xCF, 0x3F}
- var getMask [4]byte = [4]byte{0x03, 0x0C, 0x30, 0xC0}
- type Board struct {
- Width int `json:"cols"`
- Height int `json:"rows"`
- Finish Coordinate `json:"finish"`
- data []byte `json:"-"`
- }
- type req struct {
- x, y, width, height int
- }
- type response struct {
- boardTile io.ReadCloser
- x, y, width, height int
- }
- const maxIceWidth = 26
- // Create a new randomly generated board.
- func New(width, height int) *Board {
- return NewPartial(0, 0, width, height, width, height)
- }
- func NewPartial(startX, startY, width, height, totalWidth, totalHeight int) *Board {
- r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(startY+startY)))
- return newPartial(startX, startY, width, height, totalWidth, totalHeight, r)
- }
- func NewRemote(width, height int) *Board {
- b := newBlank(width, height)
- log.Printf("Start creating board %v x %v", width, height)
- const regionWidth = 32
- const regionHeight = 32
- regionWidthNum := (width + (regionWidth - 1)) / regionWidth
- regionHeightNum := (height + (regionHeight - 1)) / regionHeight
- returnChan := make(chan response)
- for i := 0; i < regionWidthNum; i++ {
- for j := 0; j < regionHeightNum; j++ {
- var partialWidth, partialHeight int
- x := i * regionWidth
- y := j * regionHeight
- if x+regionWidth > b.Width {
- partialWidth = b.Width - x
- } else {
- partialWidth = regionWidth
- }
- if y+regionHeight > b.Height {
- partialHeight = b.Height - y
- } else {
- partialHeight = regionHeight
- }
- region := req{
- x: x,
- y: y,
- width: partialWidth,
- height: partialHeight,
- }
- go b.genRegion(region, returnChan)
- }
- }
- answers := 0
- for regionWidthNum*regionHeightNum > answers {
- result := <-returnChan
- log.Printf("Received partial: x=%v, y=%v, width=%v, height=%v\n", result.x, result.y, result.width, result.height)
- for ry := 0; ry < result.height; ry++ {
- i, _ := b.xyToIndex(result.x, result.y+ry)
- result.boardTile.Read(b.data[i : i+byteIndex(result.width)])
- }
- answers++
- }
- b.placeIglo()
- log.Printf("Finished creating board %v x %v\n", width, height)
- return b
- }
- func (b *Board) placeIglo() {
- b.Finish.X = b.Width - 2
- b.Finish.Y = rand.Intn(b.Height-2) + 1
- b.Set(b.Finish.X, b.Finish.Y, Iglo)
- b.Set(b.Finish.X, b.Finish.Y-1, Water)
- b.Set(b.Finish.X, b.Finish.Y+1, Water)
- b.Set(b.Finish.X-1, b.Finish.Y, Water)
- b.Set(b.Finish.X+1, b.Finish.Y, Water)
- }
- func (b *Board) genRegion(region req, responseChan chan response) {
- 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)
- log.Printf("Requested generation of tile with url: %v\n", requestUrl)
- resp, _ := http.Get(requestUrl)
- result := response{
- x: region.x,
- y: region.y,
- width: region.width,
- height: region.height,
- boardTile: resp.Body,
- }
- responseChan <- result
- }
- func (b *Board) Set(x, y int, t TileType) {
- i, p := b.xyToIndex(x, y)
- b.data[i] = (b.data[i] & setMask[p]) | byte(t)<<(p<<1)
- }
- func (b *Board) Get(x, y int) TileType {
- i, p := b.xyToIndex(x, y)
- return TileType((b.data[i] & getMask[p]) >> uint(p<<1))
- }
- // Returns the index and offset inside the byte.
- func (b *Board) xyToIndex(x, y int) (int, uint) {
- width := byteIndex(b.Width) * 4
- index := y*width + x
- i := index / 4
- p := index % 4
- return i, uint(p)
- }
- func byteIndex(x int) int {
- return (x + 3) / 4
- }
- func (b *Board) WriteData(w io.Writer) {
- n, err := w.Write(b.data)
- if err != nil {
- fmt.Errorf("Error writing board after %v bytes: %v", n, err)
- }
- }
- func (b *Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
- sc, sr, cols, rows := sanitizeViewPort(b, startCol, startRow, cols, rows)
- fmt.Fprintf(w, "{\"x\":%v,\"y\":%v,\"rows\":%v,\"cols\":%v,\"tiles\":[", sc, sr, cols, rows)
- for y := startRow; y < sr+rows; y++ {
- for x := startCol; x < sc+cols; x++ {
- if !(x == startCol && y == startRow) {
- w.Write([]byte{','})
- }
- t := &jsonTile{x, y, b.Get(x, y).Name(), ""}
- bs, _ := json.Marshal(t)
- w.Write(bs)
- }
- }
- fmt.Fprintf(w, "]}")
- }
- func (b *Board) String() string {
- var buffer bytes.Buffer
- for y := 0; y < b.Height; y++ {
- for x := 0; x < b.Width; x++ {
- buffer.WriteString(b.Get(x, y).String())
- }
- buffer.WriteString(fmt.Sprintln())
- }
- return buffer.String()
- }
- func newBlank(width, height int) *Board {
- return &Board{
- Width: width,
- Height: height,
- data: make([]byte, byteIndex(width)*height),
- }
- }
- func new(width, height int, r *rand.Rand) *Board {
- return newPartial(0, 0, width, height, width, height, r)
- }
- func newPartial(startX, startY, width, height, totalWidth, totalHeight int, r *rand.Rand) *Board {
- b := newBlank(width, height)
- leftLimit := (totalWidth-maxIceWidth)/2 - startX
- rightLimit := totalWidth/2 + maxIceWidth/2 - startX
- mid := totalWidth/2 - startX
- for y := 0; y < height; y++ {
- for x := 0; x < width; x++ {
- switch {
- case r.Intn(10) > 7:
- b.Set(x, y, Rock)
- case x > leftLimit && x < rightLimit && r.Intn(maxIceWidth) >= abs(mid-x):
- b.Set(x, y, Ice)
- default:
- b.Set(x, y, Water)
- }
- }
- }
- return b
- }
- func sanitizeViewPort(b *Board, startCol, startRow, cols, rows int) (int, int, int, int) {
- if startCol > b.Width {
- startCol = b.Width
- }
- if startCol < 0 {
- cols += startCol
- startCol = 0
- }
- if startRow > b.Height {
- startRow = b.Height
- }
- if startRow < 0 {
- rows += startRow
- startRow = 0
- }
- if startCol+cols > b.Width {
- cols = b.Width - startCol
- }
- if startRow+rows > b.Height {
- rows = b.Height - startRow
- }
- if cols < 0 {
- cols = 0
- }
- if rows < 0 {
- rows = 0
- }
- return startCol, startRow, cols, rows
- }
- func abs(a int) int {
- if a < 0 {
- return -a
- }
- return a
- }
|