Advertisement
Xlonix

board.go

Dec 2nd, 2017
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.15 KB | None | 0 0
  1. //This package manages most functions for the board, tile, and ring structs
  2.  
  3. package main
  4.  
  5. import(
  6.     "github.com/gorilla/websocket"
  7. )
  8.  
  9. //Helper func to create new tile
  10. func newTile(gb *GameBoard, x, y, z uint8) *HexTile{
  11.     return &HexTile{[]uint8{x, y, z}, []bool{false, false, false, false, false, false}, "", gb}
  12. }
  13.  
  14. //Acts as the center tile for any board because the first ring is always 6 tiles
  15. var Center HexTile = HexTile{[]uint8{0, 0, 0}, []bool{true, true, true, true, true, true}, "NULL", nil}
  16.  
  17. /*
  18.     Helper to create new board
  19.    
  20.     NOTE: d is the diamater of
  21.     the board i.e. the number of rings
  22.     however, this does not include the
  23.     center piece, so gb.rings[0] will
  24.     have six tiles
  25. */
  26. func newBoard(d uint8) *GameBoard{
  27.     var gb *GameBoard = &GameBoard{make([]*BoardRing, d), 0, make([]Attacker, 3), 0, make([]Defender, 3), true}
  28.     for i := uint8(0); i < d; i++{
  29.         gb.rings[i] = &BoardRing{make([]*HexTile, (i+1)*6), false}
  30.         for j := uint8(0); j < (i+1)*6; j++{
  31.             gb.rings[i].tiles[j] = newTile(gb, i+1, j/(i+1), j%(i+1))
  32.         }
  33.         if i > d-1{
  34.             gb.rings[i].edge = true
  35.         }
  36.     }
  37.     return gb
  38. }
  39.  
  40. //Tile funcs
  41.  
  42. //Gets a tile adjacent to the given tile
  43. //the next tile is 0, and it goes clockwise from there
  44. func (t *HexTile) getAdjacent(side uint8) *HexTile{
  45.     if t.root == nil {
  46.         return t
  47.     }
  48.     x, y, z := t.pos[0], t.pos[1], t.pos[2]
  49.     switch side%6{
  50.         case 0:
  51.             if z < x-1 {
  52.                 z++
  53.             }else{
  54.                 y++
  55.                 z = 0
  56.             }
  57.         break
  58.         case 1:
  59.             if t.isOnEdge() == false {
  60.                 x++
  61.                 z++
  62.             }
  63.         break
  64.         case 2:
  65.             if t.isOnEdge() == false {
  66.                 x++
  67.             }
  68.         break
  69.         case 3:
  70.             if z > 0 {
  71.                 z--
  72.             }else if t.isOnEdge() == false{
  73.                 if y > 0 {
  74.                     y--
  75.                 }else {
  76.                     y = 5
  77.                 }
  78.                 z = x
  79.                 x++
  80.             }
  81.         break
  82.         case 4:
  83.             if z > 0 && x > 0 {
  84.                 x--
  85.                 z--
  86.             }else{
  87.                 if y > 0 {
  88.                     y--
  89.                 }else {
  90.                     y = 5
  91.                 }
  92.                 z = x-1
  93.             }
  94.         break
  95.         case 5:
  96.             if x > 0 {
  97.                 x--
  98.             }
  99.         break
  100.     }
  101.     return t.root.getTile(uint(x), uint(y), uint(z))
  102. }
  103.  
  104. //Determines if the tile is a corner (vertex)
  105. func (t *HexTile) isCorner() bool{
  106.     if t.pos[2] == 0 {
  107.         return true
  108.     }
  109.     return false
  110. }
  111.  
  112. //Determines if the tile is on the outer ring of the board
  113. func (t *HexTile) isOnEdge() bool{
  114.     return t.root.rings[t.pos[0]-1].edge
  115. }
  116.  
  117. /*
  118.     Gets a tile from a ring, vertex, and offset
  119.     ring 0 to d where d is the value used in NewBoard
  120.     vert 0 to 5 where 0 is the start of the ring and each addition of one donates d tiles
  121.     oset 0 to (d-1)
  122. */
  123. func (gb *GameBoard) getTile(ring, vert, oset uint) *HexTile{
  124.     if ring < 1{
  125.         return &Center
  126.     }else if ring <= uint(len(gb.rings)) && ring > oset {
  127.         t := oset+(ring*(vert%6))
  128.         return gb.rings[ring-1].tiles[t]
  129.     }
  130.     return &HexTile{[]uint8{0, 0, 0}, []bool{false, false, false, false, false, false}, "FALSE_TILE", nil}
  131. }
  132.  
  133. func (gb *GameBoard) addPlayer(socket *websocket.Conn, t bool) int8{
  134.     var i int8 = -1
  135.     if t && gb.numa < 3 {
  136.         i = gb.numa
  137.         gb.numa++
  138.         gb.pla[i] = Attacker{[]uint8{uint8(len(gb.rings)), uint8(3*i-1), 0}, socket, false}
  139.     }else if gb.numd < 3 {
  140.         i = gb.numd
  141.         gb.numd++
  142.         gb.pld[i] = Defender{uint8(i+1), socket, false}
  143.         i+=3
  144.     }
  145.     return i
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement