func power(p)
  return p*p
endfunc

func abs(n)
  if(n>0)
    return n
  else
    return -n
  endif
endfunc

# 全マスの所有者を調べる
func calc_Territory()
  local i j tmp territory
  i = 0
  while i < width
    j = 0
    while j < height
      tmp = gb_hexel_owner(i, j)
      hexels[i][j] = tmp
      if tmp == 0
        territory[0] = territory[0] + 1
      elif tmp == 1
        territory[1] = territory[1] + 1
      elif tmp == 2
        territory[2] = territory[2] + 1
      elif tmp == 3
        territory[3] = territory[3] + 1
      endif
      j = j+1
    endwhile
    i = i+1
  endwhile
  tratio = 100 * territory[team] / (territory[0]+territory[1]+territory[2]+territory[3])
  return territory
endfunc

# 全マスの所有者を調べる
func calc_Territory2()
  local i j tmp territory
  i = 0
  while i < width
    j = 0
    while j < height
      tmp = gb_hexel_owner(i, j)
      hexels[i][j] = tmp
      if (i == 0 | ((j&1)==1)&(i == width) | ((j&1)==0)&(i==(width-1)) | j == 0 | j == height ) # 壁際
        if tmp == 0
          territory[0] = territory[0] + 2
        elif tmp == 1
          territory[1] = territory[1] + 2
        elif tmp == 2
          territory[2] = territory[2] + 2
        elif tmp == 3
          territory[3] = territory[3] + 2
        endif
      else
        if tmp == 0
          territory[0] = territory[0] + 1
        elif tmp == 1
          territory[1] = territory[1] + 1
        elif tmp == 2
          territory[2] = territory[2] + 1
        elif tmp == 3
          territory[3] = territory[3] + 1
        endif
      endif
      j = j+1
    endwhile
    i = i+1
  endwhile
  tratio = 100 * territory[team] / (territory[0]+territory[1]+territory[2]+territory[3])
  return territory
endfunc

# トップチームのIDを返す
func calc_Topteam(team)
  local i top
  i = 0
  if team == 0
    top = 1
  else
    top = 0
  endif
  while i < 4
    if (i != team)
      if (territory[top] < territory[i]) #自チーム以外
        top = i
      endif
    endif
  i = i+1
  endwhile
  return top
endfunc

# 最下位チームのIDを返す
func calc_Bottomteam(team)
  local i bottom
  i = 0
  if team == 0
    bottom = 1
  else
    bottom = 0
  endif
  while i < 4
    if (i != team)
      if (territory[bottom] > territory[i]) #自チーム以外
        bottom = i
      endif
    endif
  i = i+1
  endwhile
  return bottom
endfunc

func countMyHex(x, y)
  local count offset
  count = 0

  if (y&1) == 0
    offset = 0
  else
    offset = -1
  endif
  
  if(hexels[x+offset][y-1] == team)
    count = count + 1
  endif
  if(hexels[x+1+offset][y-1] == team)
    count = count + 1
  endif
  if(hexels[x+1][y] == team)
    count = count + 1
  endif
  if(hexels[x+1+offset][y+1] == team)
    count = count + 1
  endif
  if(hexels[x+offset][y+1] == team)
    count = count + 1
  endif
  if(hexels[x-1][y] == team)
    count = count + 1
  endif

  return count
endfunc

# A座標 から B座標 への移動
func fromAtoB(ax, ay, bx, by)
  local diffx diffy
  diffx = ax - bx
  diffy = ay - by
  
  if diffx < 0
    if diffy < 0
      return 3
    elif diffy > 0
      return 1
    elif diffy == 0
      return 2
    endif
  endif
  
  if diffx > 0
    if diffy > 0
      return 0
    elif diffy < 0
      return 4
    elif diffy == 0
      return 5
    endif
  endif

  if diffx == 0
    if diffy > 0
      if (a%2)==0
        return 0
      else
        return 1
      endif
    elif diffy < 0
      if (a%2)==0
        return 4
      else
        return 3
      endif
    endif
  endif
    
endfunc

# A座標 から B座標 への移動
func fromAtoB2(ax, ay, bx, by)
  local diffx diffy up down left right
  diffx = ax - bx
  diffy = ay - by
  
  up = 0
  down = 0
  left = 0
  right = 0
  
  if(ay == 1)
    up = 1
  elif (ay == height - 1)
    down = 1
  endif

  if diffx < 0
    if diffy < 0
      if down == 1
        return 2
      else
        return 3
      endif
    elif diffy > 0
      if up == 1
        return 2
      else
        return 1
      endif
    elif diffy == 0
      return 2
    endif
  endif
  
  if diffx > 0
    if diffy > 0
      if up == 1
        return 5
      else
        return 0
      endif
    elif diffy < 0
      if down == 1
        return 5
      else
        return 4
      endif
    elif diffy == 0
      return 5
    endif
  endif

  if diffx == 0
    if diffy > 0
      if (a%2)==0
        if up == 1
          return 5
        else
          return 0
        endif
      else
        if up == 1
          return 2
        else
          return 1
        endif
      endif
    elif diffy < 0
      if (a%2)==0
        if down == 1
          return 5
        else
          return 4
        endif
      else
        if down == 1
          return 2
        else
          return 3
        endif
      endif
    endif
  endif
    
endfunc


# 相手エージェントの居る周囲2マスを塗りつぶしたい
# sokonuke関数追加
func calc_fillHexels()
  local i j
  i = 0
  while i < 4
    j = 0
    while j < 4
      fill(i, j)
      if(i != team)
        #sokonuke(i, j)
      endif
      j = j+1
    endwhile
  i = i+1
  endwhile
endfunc

# (i,j)の周囲マスを塗る
func fill(i, j)
  local k l val offset # k-x l-y 対応
  cx = pos[i][j][0]
  cy = pos[i][j][1]
  
  if (i == team)
     val = -1 # 自チームは-1
  else
    if (j == 0)
      val = 3 # 敵チームの犬
    else
      val = 1 # 敵チームの侍
    endif
  endif
  
  if (cy&1) == 0
    offset = 0
  else
    offset = -1
  endif
  
  # target から -2 行
  k = -1
  while k < 2
    filled[cx + k][cy - 2] = filled[cx + k][cy - 2] + val
    k = k+1
  endwhile

  # target から -1 行
  k = -1
  while k < 3
    filled[cx + k + offset][cy - 1] = filled[cx + k + offset][cy - 1] + val
    k = k+1
  endwhile

  # target から 0 行
  k = -2
  while k < 3
    filled[cx + k][cy] = filled[cx + k][cy] + val
    k = k+1
  endwhile

  #target から 1 行
  k = -1
  while k < 3
    filled[cx + k + offset][cy + 1] = filled[cx + k + offset][cy + 1] + val
    k = k+1
  endwhile
  
  #target から 2 行
  k = -1
  while k < 2
    filled[cx + k][cy + 2] = filled[cx + k][cy + 2] + val
    k = k+1
  endwhile

endfunc

# 敵から遠いほどよい
func sokonuke(i, j)
  local k l val x y n # k-x l-y 対応
  cx = pos[i][j][0]
  cy = pos[i][j][1]
  
  n = 5

  k = -n
  while(k <= 0)
    l = -n
    while (l <= 0)
      x = cx + k
      y = cy + l
      if (x > 0) & (x < width) & (y > 0) & (y < height)
        potential[x][y] = potential[x][y] + k + l
      endif
    l = l + 1
    endwhile
  k = k + 1
  endwhile

  k = -n
  while(k < 0)
    l = n
    while (l > 0)
      x = cx + k
      y = cy + l
      if (x > 0) & (x < width) & (y > 0) & (y < height)
        potential[x][y] = potential[x][y] + k - l
      endif
    l = l - 1
    endwhile
  k = k + 1
  endwhile

  k = n
  while(k > 0)
    l = -n
    while (l < 0)
      x = cx + k
      y = cy + l
      if (x > 0) & (x < width) & (y > 0) & (y < height)
        potential[x][y] = potential[x][y] - k + l
      endif
    l = l + 1
    endwhile
  k = k - 1
  endwhile

  k = n
  while(k >= 0)
    l = n
    while (l >= 0)
      x = cx + k
      y = cy + l
      if (x > 0) & (x < width) & (y > 0) & (y < height)
        potential[x][y] = potential[x][y] - k - l
      endif
    l = l - 1
    endwhile
  k = k - 1
  endwhile

endfunc



# 離れ小島をつなげたい
func calc_Sandwich()
  local i j offset tmp cnt

  i = 1
  while i < width -1
    j = 1
    while j < height -1
      tmp = 0
      cnt = countMyHex(i,j)
      
      if(cnt == 2)     
        if (j&1) == 0
          offset = 0
        else
          offset = -1
        endif

        if (hexels[i-1][j] == team)&(hexels[i][j] != team)&(hexels[i+1][j] == team)
          tmp = tmp + 1
        endif # side
        if(hexels[i+offset][j-1] == team)&(hexels[i][j] != team)&(hexels[i+1+offset][j+1] == team)
          tmp = tmp + 1
        endif # left-up -> right-down
        if(hexels[i+1+offset][j-1] == team)&(hexels[i][j] !=team)&(hexels[i+offset][j+1] == team)
          tmp = tmp + 1
        endif # right-up -> left-down

        if(hexels[i+offset][j-1] == team)&(hexels[i][j] !=team)&(hexels[i+1][j] == team)
          tmp = tmp + 1
        endif
        if(hexels[i+1+offset][j-1] == team)&(hexels[i][j] !=team)&(hexels[i+1+offset][j+1] == team)
          tmp = tmp + 1
        endif
        if(hexels[i+1][j] == team)&(hexels[i][j] !=team)&(hexels[i+offset][j+1] == team)
          tmp = tmp + 1
        endif
        if(hexels[i+1+offset][j+1] == team)&(hexels[i][j] !=team)&(hexels[i-1][j] == team)
          tmp = tmp + 1
        endif
        if(hexels[i+offset][j+1] == team)&(hexels[i][j] !=team)&(hexels[i+offset][j-1] == team)
          tmp = tmp + 1
        endif
        if(hexels[i-1][j] == team)&(hexels[i][j] !=team)&(hexels[i+1+offset][j-1] == team)
          tmp = tmp + 1
        endif
      endif

      if(tmp == 1)
        sand[i][j] = 1
      endif
    
    j = j+1
    endwhile
  i = i+1
  endwhile
endfunc


# 離れたところをつなげたい 2マスの飛び
func calc_Sandwich2()
  local i j offset tmp

  if (j&1) == 0
    offset = 0
  else
    offset = -1
  endif
  
  # 横方向
  i = 1
  while i < width-2
    j = 1
    while j < height-1

    if ( (countMyHex(i,j) == 1) & (countMyHex(i+1,j) == 1) )
      if(hexels[i+offset][j-1] == team)|(hexels[i-1][j] == team)|(hexels[i+offset][j+1] == team)
        if(hexels[i+2+offset][j-1] == team)|(hexels[i+2][j] == team)|(hexels[i+2+offset][j+1] == team)
          if(hexels[i+1+offset][j-1] != team)&(hexels[i][j] != team)&(hexels[i+1][j] != team)&(hexels[i+1+offset][j+1] != team)
            sand2[i][j] = 1
            sand2[i+1][j] = 1
          endif
        endif
      endif
    endif

    j = j+1
    endwhile
  i = i+1
  endwhile

  # 右肩さがり
  i = 1
  while i < width - 2
    j = 1
    while j < height - 2

    if ( (countMyHex(i,j) == 1) & (countMyHex(i+1+offset,j+1) == 1) )
      if(hexels[i-1][j] == team)|(hexels[i+offset][j-1] == team)|(hexels[i+1+offset][j-1] == team)
        if(hexels[i][j+2] == team)|(hexels[i+1][j+2] == team)|(hexels[i+2+offset][j+1] == team)
          if(hexels[i][j] != team)&(hexels[i+1][j] != team)&(hexels[i+offset][j+1] != team)&(hexels[i+1+offset][j+1] != team)
            sand2[i][j] = 1
            sand2[i+1+offset][j+1] = 1
          endif
        endif
      endif
    endif

    j = j+1
    endwhile
  i = i+1
  endwhile

  # 右肩上がり
  i = 2
  while i < width - 1
    j = 1
    while j < height - 2

    if ( (countMyHex(i,j) == 1) & (countMyHex(i+offset,j+1) == 1) )
      if(hexels[i+offset][j-1] == team)|(hexels[i+1+offset][j-1] == team)|(hexels[i+1][j] == team)
        if(hexels[i-1+offset][j+1] == team)|(hexels[i-1][j+2] == team)|(hexels[i][j+2] == team)
          if(hexels[i-1][j] != team)&(hexels[i][j] != team)&(hexels[i+offset][j+1] != team)&(hexels[i+1+offset][j+1] != team)
            sand2[i][j] = 1
            sand2[i+offset][j+1] = 1
          endif
        endif
      endif
    endif

    j = j+1
    endwhile
  i = i+1
  endwhile

endfunc


# 離れたところをつなげたい 3マスの飛び
func calc_Sandwich3()
  local i j offset tmp

  if (j&1) == 0
    offset = 0
  else
    offset = -1
  endif
  
  # 横方向
  i = 1
  while i < width-3
    j = 1
    while j < height-1

    if ( (countMyHex(i,j) == 1) & (countMyHex(i+2,j) == 1) )
      if(hexels[i+offset][j-1] == team)|(hexels[i-1][j] == team)|(hexels[i+offset][j+1] == team)
        if(hexels[i+3+offset][j-1] == team)|(hexels[i+3][j] == team)|(hexels[i+3+offset][j+1] == team)
          if(hexels[i+1+offset][j-1] != team)&(hexels[i][j] != team)&(hexels[i+1][j] != team)&(hexels[i+1+offset][j+1] != team)
            if(hexels[i+2+offset][j-1] != team)&(hexels[i+2][j] != team)&(hexels[i+2+offset][j+1] != team)
              sand3[i][j] = 1
              sand3[i+1][j] = 1
              sand3[i+2][j] = 1
            endif
          endif
        endif
      endif
    endif

    j = j+1
    endwhile
  i = i+1
  endwhile

  # 右肩さがり
  i = 1
  while i < width-2
    j = 1
    while j < height-3

    if ( (countMyHex(i,j) == 1) & (countMyHex(i+1,j+2) == 1) )
      if(hexels[i-1][j] == team)|(hexels[i+offset][j-1] == team)|(hexels[i+1+offset][j-1] == team)
        if(hexels[i+1+offset][j+3] == team)|(hexels[i+2+offset][j+3] == team)|(hexels[i+2][j+2] == team)
          if(hexels[i][j] != team)&(hexels[i+1][j] != team)&(hexels[i+offset][j+1] != team)&(hexels[i+1+offset][j+1] != team)
            if(hexels[i][j+2] != team)&(hexels[i+1][j+2] != team)&(hexels[i+2+offset][j+1] != team)
              sand3[i][j] = 1
              sand3[i+1+offset][j+1] = 1
              sand3[i+1][j+2] = 1
            endif
          endif
        endif
      endif
    endif

    j = j+1
    endwhile
  i = i+1
  endwhile

  # 右肩上がり
  i = 2
  while i < width-2
    j = 1
    while j < height-3

    if ( (countMyHex(i,j) == 1) & (countMyHex(i-1,j+2) == 1) )
      if(hexels[i+offset][j-1] == team)|(hexels[i+1+offset][j-1] == team)|(hexels[i+1][j] == team)
        if(hexels[i-2][j+2] == team)|(hexels[i-1+offset][j+3] == team)|(hexels[i+offset][j+3] == team)
          if(hexels[i-1][j] != team)&(hexels[i][j] != team)&(hexels[i+offset][j+1] != team)&(hexels[i+1+offset][j+1] != team)
             if(hexels[i-1+offset][j+1] != team)&(hexels[i-1][j+2] != team)&(hexels[i][j+2] != team)
              sand3[i][j] = 1
              sand3[i+offset][j+1] = 1
              sand3[i-1][j+2] = 1
            endif
          endif
        endif
      endif
    endif

    j = j+1
    endwhile
  i = i+1
  endwhile

endfunc

# 評価関数の中身
func eval(x, y)
  local k l tmp result
  tmp = hexels[x][y]

  # ここから評価 高いの優先
  # 領地系
  #if (x == 0 | x == width | y == 0 | y == height | ((y&1)==0)&(x==(width-1))) # 壁際
  if (x == 0 | ((y&1)==1)&(x==width) | ((y&1)==0)&(x==(width-1)) | y == 0 | y == height ) # 壁際
    if(gb_remaining_turns() > 300 | tratio > 35)
      result = result + 15 #15?
    else
      result = result + 0 #10
    endif
  endif
  if (tmp == topteam) #トップチームの領地
    result = result + 3
  endif
  if (tmp != topteam & tmp != team & tmp != -1) # 2位、3位の領地
    result = result + 2 #4
  endif
  if (tmp == team) # 自チームの領地
    result = result - 5 #6
  endif

  # レイヤー系
  result = result - filled[x][y] * 2 # 敵エージェントが近くに居るとか

  # シージ用？
  if(sand[x][y] == 1) # 離れ小島をつなぐ
    result = result + 15 #10
  endif

  if(sand2[x][y] == 1)
    result = result + 6
  endif
 
  if(sand3[x][y] == 1)
    result = result + 6
  endif


  # エージェント系 pos[teamid][agentid][width_or_height]
  k = 0
  while k < 4
    l = 0
    while l < 4
      if pos[k][l][0] == x & pos[k][l][1] == y #エージェントを検索
        if k == team
          result = result - 26  # 自チーム
        else
          if l == 0
            result = result - 200 # 敵チームのdog 200
          else
            result = result - 150 # 敵チームのsamurai 150
          endif
        endif
      endif
    l = l+1
    endwhile
  k = k+1
  endwhile

  return result
endfunc

func evalSiege(x, y)
  local result

  # シージ用？
  if(sand[x][y] == 1)
    result = result + 30
  endif
  
  if(sand[x][y] == 2) # 離れ小島をつなぐ
    #result = result + 10
  endif

  if(sand2[x][y] == 1)
    #result = result + 30
  endif

  return result
endfunc

# 点(x,y)の範囲1マスの評価を返す
func eval1hex(x,y)
  local point offset

  if (y&1) == 0
    offset = 0
  else
    offset = -1
  endif

  point = eval(x, y) 
  point = point + eval(x + offset, y - 1) #0
  point = point + eval(x + 1 + offset, y -1) #1
  point = point + eval(x + 1, y) #2
  point = point + eval(x + 1 + offset, y + 1) #3
  point = point + eval(x + offset, y + 1) #4
  point = point + eval(x - 1, y) #5
 
  return point
endfunc


# 点(x,y)の範囲2マスの評価を返す
func eval2hex(x, y)
  local k point offset

  if (y&1) == 0
    offset = 0
  else
    offset = -1
  endif

  # target から -2 行
  k = -1
  while k < 2
    point = point + eval(x + k, y - 2)
    k = k+1
  endwhile

  # target から -1 行
  k = -1
  while k < 3
    point = point + eval(x + k + offset, y - 1)
    k = k+1
  endwhile

  # target から 0 行
  k = -2
  while k < 3
    point = point + eval(x + k, y)
    k = k+1
  endwhile

  #target から 1 行
  k = -1
  while k < 3
    point = point + eval(x + k + offset, y + 1)
    k = k+1
  endwhile
  
  #target から 2 行
  k = -1
  while k < 2
    point = point + eval(x + k, y + 2)
    k = k+1
  endwhile
  
  return point
endfunc


#対象の点(x,y)の"1ヘックス移動先"と”2マス移動先の周辺2マス”の評価を行った後，高い順にソートする
func sortMove(x, y)
  local tmp prior_dir hoge i j swap min offset h1 h2
  if (y&1) == 0
    offset = 0
  else
    offset = -1
  endif

  h1 = 2
  h2 = 7
  h3 = 0
  
  tmp[0] = h2 * eval(x+offset, y-1)   + h1 * eval1hex(x+offset, y-1)   + eval2hex(x-1, y-2)  #0方向
  tmp[1] = h2 * eval(x+1+offset, y-1) + h1 * eval1hex(x+1+offset, y-1) + eval2hex(x+1, y-2)  #1方向
  tmp[2] = h2 * eval(x+1, y)          + h1 * eval1hex(x+1, y)          + eval2hex(x+2, y)    #2方向
  tmp[3] = h2 * eval(x+1+offset, y+1) + h1 * eval1hex(x+1+offset, y+1) + eval2hex(x+1, y+2)  #3方向
  tmp[4] = h2 * eval(x+offset, y+1)   + h1 * eval1hex(x+offset, y+1)   + eval2hex(x+1, y+2)  #4方向
  tmp[5] = h2 * eval(x-1, y)          + h1 * eval1hex(x-1, y)          + eval2hex(x-2, y)    #5方向

  # Siege
  #tmp[0] = tmp[0] + h3 * evalSiege(x+offset, y-1)
  #tmp[1] = tmp[1] + h3 * evalSiege(x+1+offset, y-1)
  #tmp[2] = tmp[2] + h3 * evalSiege(x+1, y)
  #tmp[3] = tmp[3] + h3 * evalSiege(x+1+offset, y+1)
  #tmp[4] = tmp[4] + h3 * evalSiege(x+offset, y+1)
  #tmp[5] = tmp[5] + h3 * evalSiege(x-1, y)


  # 各移動方向のスコアに順位づけ
  i = 0
  while i < 6
    j = 0
    prior_dir[i] = 0
    while j < 6
      if(tmp[i] < tmp[j])
        prior_dir[i] = prior_dir[i] + 1
      endif
      j = j+1
    endwhile
    i = i+1
  endwhile

  #swapに移動方向を優先順にソートして格納
  i = 0
  while i < 6
    j = 0
    min = 7
    while j < 6
      if (min > prior_dir[j])
        min = prior_dir[j]
        swap[i] = j
        hoge = j
      endif
    j = j+1
    endwhile
    prior_dir[hoge] = 8
  i = i+1
  endwhile

  return swap
endfunc
  

# Pがフィールドの内外かを調べる 外なら1
func outOfField(p)
  local x y
  x = p[0]
  y = p[1]
  if (x < 0) | (x >= width) | ((x >= width - 1) & ((y & 1) == 0))
    return 1
  endif
  if (y < 0) | (y >= height)
    return 1
  endif
  return 0
endfunc

# ランダムに移動
func randomMove(agnt)
  local dest current rand k cont
  if gb_agent_status(team, agnt)
    return -1
  else
    current = pos[team][agnt]
    k = 0
    cont = 1
    rand = gb_random_value(6)

    while cont
      dest = neighbor(current, (rand+k)%6)
      if (outOfField(dest) == 0)
        cont = 0
        return (rand+k)%6
      endif
      k = k+1
    endwhile
  endif
endfunc

# 近い敵に突撃
func nearestSearch_Dog(pos)
  local k l myx myy tmp nearest min diffx diffy

  if gb_agent_status(team, agnt)
    return -1
  endif
 
  myx = pos[team][0][0]
  myy = pos[team][0][1]
  min = 100000
  k = 0

  while k < 4
    l = 1
    if k != team & k != bottomteam
      while l < 4
        diffx = pos[k][l][0] - myx
        diffy = pos[k][l][1] - myy
        tmp = diffx + diffy
        if k == topteam
          tmp = tmp * 2/5 
        endif
        if tmp < min
          min = tmp
          nearest = pos[k][l]
        endif
      l = l+1 
      endwhile
    endif
  k = k+1
  endwhile

  return fromAtoB(myx, myy, nearest[0], nearest[1])
endfunc


# 移動先のマスの座標を計算
func neighbor(p, k)
  local x y ret offset
  x = p[0]
  y = p[1]
  if (y&1) == 0
    offset = 0
  else
    offset = -1
  endif
  if k == 0
    ret[0] = x + offset
    ret[1] = y - 1
  elif k == 1
    ret[0] = x + 1 + offset
    ret[1] = y - 1
  elif k == 2
    ret[0] = x + 1
    ret[1] = y
  elif k == 3
    ret[0] = x + 1 + offset
    ret[1] = y + 1
  elif k == 4
    ret[0] = x + offset
    ret[1] = y + 1
  elif k == 5
    ret[0] = x - 1
    ret[1] = y
  endif
  return ret
endfunc


# 侍の移動先が被らないようにする
func exclusive(dest, agnt)
  local i
  i = 1
  while i < agnt
    if(dest[0] == premove[i][0] & dest[1] == premove[i][1])
      return 1
    else
      i = i + 1
    endif
  endwhile
  return 0
endfunc


# 護持判定 護持しているチームIDを返す,されていなかったら-1
func isGuard(agnt)
  local i j k tmp1 tmp2 flag
  flag = 0
  current = pos[team][agnt]
  i = 1
  while i < 6
    tmp1 = neighbor(current, i-1)
    tmp2 = neighbor(current, i)
    if( hexels[tmp1[0]][tmp1[1]] == hexels[tmp2[0]][tmp2[1]] & hexels[tmp1[0]][tmp1[1]] != team )
      i = i + 1
    else
      return -1
    endif
  endwhile
  return tmp1
endfunc


# 護持から逃げる
func exitGuard(guard, agnt)
  local i j x y tmp nx ny min diffx diffy offset
 
  myx = pos[team][0][0]
  myy = pos[team][0][1]
  min = 100000
  
  k = 0
  while k < width
    l = 0
    while l < height
      if (hexels[k][l] != (guard | -2))
        diffx = k - myx
        diffy = l - myy
        if (diffy & 1)
          diffx = diffx - 1
        endif
        tmp = diffx + diffy
        if tmp < min
          min = tmp
          nx = k
          ny = l
        endif
      endif
      l = l+1
    endwhile
  k = k+1
  endwhile
  
  return fromAtoB(myx, myy, ny, ny)
endfunc


# 移動先の決定
func decideMove(agnt)
  local current k cont dest gates atgate mp guard
  if (gb_agent_status(team, agnt))
    return -1
  endif
  current = pos[team][agnt]
  atgate = 0
  k = 0
  while k != $gates 
    if gates[k][0] == current[0] & gates[k][1] == current[1]
      atgate = 1
    endif
    k = k+1
  endwhile

  cont = 1
  guard = isGuard(agnt)
  if(guard != -1)
    dest = neighbor(current, exitGuard(guard, agnt))
    if(outOfField(dest) == 0) & (exclusive(dest, agnt) == 0)
      cont = 0
      premove[agnt] = dest
      return exitGuard(guard, agnt)
    else
      randomMove(agnt)
    endif
  endif

  mp = sortMove(current[0], current[1]) 
  k = 0
  cont = 1
  while cont
    dest = neighbor(current, mp[k])
    if (atgate | outOfField(dest) == 0) & (hexels[dest[0]][dest[1]] != team) & (exclusive(dest, agnt) == 0)
      cont = 0
      premove[agnt] = dest
      return mp[k]
    else
      k = k+1
      if k == 6
        cont = 0
      endif
    endif
  endwhile
  return randomMove(agnt)
endfunc

#フィールド変数とか

team = gb_team_id()
remain = gb_remaining_turns()
size = gb_field_size()
width = size[0]
height = size[1]
gates = gb_gate_positions()
pos = gb_agent_positions()

hexels = 0
filled = 0
potential = 0
calc_fillHexels()
tratio = 0
territory = calc_Territory()
topteam = calc_Topteam(team)
bottomteam = calc_Bottomteam(team)

sand = 0
calc_Sandwich()
sand2 = 0
calc_Sandwich2()
sand3 = 0
calc_Sandwich3()

premove = 0

moves[0] = nearestSearch_Dog(pos)
moves[1] = decideMove(1)
moves[2] = decideMove(2)
moves[3] = decideMove(3)
gb_move(moves)
