Parcourir la source

The bot is able to read the viewport

Ronald Peterson il y a 10 ans
Parent
commit
c79a19f0cd
4 fichiers modifiés avec 121 ajouts et 19 suppressions
  1. 82 15
      battlecamp-go-bot/main.go
  2. 24 3
      board/board.go
  3. 1 1
      board/board_test.go
  4. 14 0
      board/tile.go

+ 82 - 15
battlecamp-go-bot/main.go

@@ -11,12 +11,12 @@ import (
 	"strconv"
 	"time"
 
+	"battlecamp-go-server/board"
 	"battlecamp-go-server/events"
 	"battlecamp-go-server/flag"
 	"battlecamp-go-server/game"
 	"battlecamp-go-server/player"
 	"battlecamp-go-server/stomp"
-	"battlecamp-go-server/board"
 )
 
 func main() {
@@ -73,20 +73,15 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
 
 	players := make([]*player.Player, 5)
 
-	//retrieve finishe
-
+	//retrieve finish
 	gameUrl := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10)
 	fmt.Println("getGame:>", gameUrl)
-
 	resp, _ := http.Get(gameUrl)
-
 	g := new(game.Game)
 	b, _ := ioutil.ReadAll(resp.Body)
 	json.Unmarshal(b, &g)
-
-	finish := g.Board.Finish
-	
-	fmt.Printf("Finish x=%v y=%v", finish.X, finish.Y)
+	boardSummery := g.Board
+	fmt.Printf("Finish x=%v y=%v\n", boardSummery.Finish.X, boardSummery.Finish.Y)
 
 	//join the game
 	url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/join"
@@ -94,7 +89,6 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
 	jsonStr, _ := json.Marshal(p)
 	req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
 	req.Header.Set("Content-Type", "application/json")
-	fmt.Println("URL:>", req)
 
 	client := &http.Client{}
 	resp, err := client.Do(req)
@@ -102,9 +96,16 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
 		panic(err)
 	}
 	v, _ := ioutil.ReadAll(resp.Body)
-	json.Unmarshal(v, p)
+	json.Unmarshal(v, p) //Set my location in the player
+	fmt.Printf("My start position x %v y %v\n", p.Pos.X, p.Pos.Y)
 	resp.Body.Close()
 
+	//determine to avoind north or south
+	north := true
+	if boardSummery.Finish.Y > p.Pos.Y {
+		north = false
+	}
+
 	for {
 		select {
 		case <-gameEndChan:
@@ -121,15 +122,81 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate)
 				}
 			}
 		default:
-			move(gameId, p, players, finish)
+			move(gameId, p, players, boardSummery, north)
 			time.Sleep(2 * time.Second)
 		}
 	}
 }
 
-func move(gameId int64, p *player.Player, players []*player.Player, finish board.Coordinate) {
+type viewport struct {
+	x,y,width,height int
+	Board *board.Board
+}
+
+func getViewPort(x, y, width, height int, gameId int64, bs *board.Board) *viewport {
+	x = max(x, 0)
+	x = min(x, bs.Width)
+	y = max(y, 0)
+	y = min(y, bs.Height)
+	if x + width > bs.Width {
+		width = bs.Width - x
+	}
+	if y + height > bs.Height {
+		height = bs.Height - x
+	}
+	url := "http://localhost:8080/games/board/" + strconv.FormatInt(gameId, 10) + "?x=" + strconv.Itoa(x) + "&y=" + strconv.Itoa(y) + "&rows=" + strconv.Itoa(width) + "&cols=" + strconv.Itoa(height)
+	fmt.Println("getViewPort:>", url)
+	resp, _ := http.Get(url)
+	board := board.ReadJSON(x, y, resp.Body)
+	return &viewport{
+		x:x,
+		y:y,
+		width:width,
+		height:height,
+		Board:board,
+	}
+}
+
+func (v viewport) Get(x, y int) board.TileType {
+	fmt.Printf("retrieving from viewport x %v y %v position in viewport x %v y %v", x, y, x-v.x, y-v.y)
+	return v.Board.Get(x-v.x,y-v.y)
+}
+
+const viewPortWidth = 32
+const viewPortHeight = 32
+
+func max(a, b int) int {
+	if a >= b {
+		return a
+	}
+	return b
+}
+
+func min(a, b int) int {
+	if a <= b {
+		return a
+	}
+	return b
+}
+
+func move(gameId int64, p *player.Player, players []*player.Player, bs *board.Board, north bool) {
 	//determine move
-	direction := "E"
+
+	viewPortX := p.Pos.X-1
+	viewPortY := p.Pos.Y-1
+	if north {
+		viewPortX = viewPortX-viewPortHeight
+	}
+
+	viewPort := getViewPort(viewPortX, viewPortY, viewPortWidth, viewPortHeight, gameId, bs)
+	
+	direction := "W"
+	
+	fmt.Printf("p.Pos.X+1 %v p.Pos.Y %v\n", p.Pos.X+1, p.Pos.Y)
+	
+	if viewPort.Get(p.Pos.X+1, p.Pos.Y) != board.Rock {
+		direction = "E"
+	}
 
 	//send move
 	url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/move/" + p.Id + "/" + direction
@@ -148,7 +215,7 @@ func move(gameId int64, p *player.Player, players []*player.Player, finish board
 	case "W":
 		p.Pos.Y++
 	}
-	fmt.Printf("new pos x=%v y=%v", p.Pos.X, p.Pos.Y)
+	fmt.Printf("new pos x=%v y=%v\n", p.Pos.X, p.Pos.Y)
 }
 
 func playerJoin(p *player.Player) {

+ 24 - 3
board/board.go

@@ -5,12 +5,12 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
+	"io/ioutil"
 	"log"
 	"math/rand"
 	"net/http"
 	"time"
 
-
 	"battlecamp-go-server/flag"
 )
 
@@ -131,7 +131,7 @@ func worker(workChan chan *work, responseChan chan *response) {
 			partialHeight = regionHeight
 		}
 
-		requestUrl := fmt.Sprintf("http://"+ *flag.BoardGenUrl + "/?totalWidth=%v&totalHeight=%v&width=%v&height=%v&x=%v&y=%v", w.width, w.height, partialWidth, partialHeight, w.x, w.y)
+		requestUrl := fmt.Sprintf("http://"+*flag.BoardGenUrl+"/?totalWidth=%v&totalHeight=%v&width=%v&height=%v&x=%v&y=%v", w.width, w.height, partialWidth, partialHeight, w.x, w.y)
 		log.Printf("Requested generation of tile with url: %v\n", requestUrl)
 
 		resp, err := http.Get(requestUrl)
@@ -165,6 +165,7 @@ func (b *Board) Set(x, y int, t TileType) {
 }
 
 func (b *Board) Get(x, y int) TileType {
+	fmt.Printf("get x %v y %v boardsize %v", x, y, len(b.data))
 	i, p := b.xyToIndex(x, y)
 	return TileType((b.data[i] & getMask[p]) >> uint(p<<1))
 }
@@ -208,6 +209,26 @@ func (b *Board) WriteJSON(w io.Writer, startCol, startRow, cols, rows int) {
 	fmt.Fprintf(w, "]}")
 }
 
+func ReadJSON(ofsetX, ofsetY int, reader io.Reader) *Board {
+	b, _ := ioutil.ReadAll(reader)
+	jsonBoard := new(boardJSON)
+	json.Unmarshal(b, &jsonBoard)
+	board := newBlank(jsonBoard.Width, jsonBoard.Height)
+	
+	for _, t := range jsonBoard.Tiles {
+		board.Set(t.X-ofsetX, t.Y-ofsetY, FromName(t.Type))
+	}
+	return board
+}
+
+type boardJSON struct {
+	X      int        `json:"x"`
+	Y      int        `json:"y"`
+	Height int        `json:"rows"`
+	Width  int        `json:"cols"`
+	Tiles  []jsonTile `json:"tiles"`
+}
+
 func (b *Board) String() string {
 	var buffer bytes.Buffer
 
@@ -229,7 +250,7 @@ func newBlank(width, height int) *Board {
 	}
 }
 
-func new(width, height int, r *rand.Rand) *Board {
+func newBoard(width, height int, r *rand.Rand) *Board {
 	return newRegion(0, 0, width, height, width, height, r)
 }
 

+ 1 - 1
board/board_test.go

@@ -45,7 +45,7 @@ var testBoard = `~~~~~~~~▲▲▲~▲▲~~*~*~~**~*▲****▲▲***▲~▲***~~
 
 func TestNewBoard(t *testing.T) {
 	r := rand.New(rand.NewSource(0))
-	b := new(56, 35, r)
+	b := newBoard(56, 35, r)
 
 	if testBoard != b.String() {
 		t.Fatalf("Incorrect board:\n %v", b)

+ 14 - 0
board/tile.go

@@ -18,6 +18,20 @@ const (
 	Iglo  TileType = 3
 )
 
+func FromName(name string) TileType {
+	switch name {
+		case "W":
+			return Water
+		case "R":
+			return Rock
+		case "I":
+			return Ice
+		case "H":
+			return Iglo
+	}
+	panic(fmt.Sprintf("Unknown tile type %v", name))
+}
+
 func (t TileType) String() string {
 	switch t {
 	case Water: