瀏覽代碼

next bot version

Ronald Peterson 10 年之前
父節點
當前提交
549aea89b6
共有 2 個文件被更改,包括 97 次插入46 次删除
  1. 95 46
      battlecamp-go-bot/main.go
  2. 2 0
      battlecamp-go-gameserver/gameserver/urlrouter.go

+ 95 - 46
battlecamp-go-bot/main.go

@@ -7,6 +7,7 @@ import (
 	"io/ioutil"
 	"log"
 	"math"
+	"math/rand"
 	"net/http"
 	"os"
 	"strconv"
@@ -43,7 +44,7 @@ func subscribeToGame(pu chan *events.PlayerUpdate) {
 		json.Unmarshal(announcement.Body, &gs)
 		fmt.Printf("announcement type: %v for game %v\n", gs.Type, strconv.FormatInt(gs.GameId, 10))
 		if "GAME_START" == gs.Type {
-			names := []string{"Zeus", "Joost", "Henk", "Klaas"}
+			names := []string{"Zeus"}
 			tmp := make([]chan bool, 0, len(names))
 			gameEndChannels[int(gs.GameId)] = &tmp
 			for _, name := range names {
@@ -94,6 +95,7 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate,
 	resp, _ := http.Get(gameUrl)
 	g := new(game.Game)
 	b, _ := ioutil.ReadAll(resp.Body)
+	resp.Body.Close()
 	json.Unmarshal(b, &g)
 	boardSummery := g.Board
 	fmt.Printf("Finish x=%v y=%v\n", boardSummery.Finish.X, boardSummery.Finish.Y)
@@ -115,25 +117,28 @@ func joinGame(gameId int64, gameEndChan chan bool, pu chan *events.PlayerUpdate,
 	fmt.Printf("My start position x %v y %v\n", p.Pos.X, p.Pos.Y)
 	resp.Body.Close()
 
+	move(gameId, p, players, boardSummery)
+
 	for {
 		select {
 		case <-gameEndChan:
 			fmt.Println("Game ended returning")
+			close(gameEndChan)
 			return
 		case playerUpdate := <-pu:
 			if playerUpdate.GameId == gameId {
-		/*		players = playerUpdate.Players // TODO fix multi client
+				players = playerUpdate.Players // TODO fix multi client
 				for _, pup := range players {
 					if pup.Id == p.Id {
 						p.Pos.X = pup.Pos.X
 						p.Pos.Y = pup.Pos.Y
-						fmt.Printf("Set position to x=%v y=%v\n", pup.Pos.X, pup.Pos.Y)
+						fmt.Printf("Set my position to x=%v y=%v\n", pup.Pos.X, pup.Pos.Y)
+						time.Sleep(25 * time.Millisecond)
+						fmt.Println("Allowed to make a new move")
+						move(gameId, p, players, boardSummery)
 					}
-				}*/
+				}
 			}
-		default:
-			move(gameId, p, players, boardSummery)
-			time.Sleep(100 * time.Millisecond)
 		}
 	}
 }
@@ -146,15 +151,11 @@ type viewport struct {
 func getViewPort(x, y, width, height int, gameId int64, bs *board.Board) *viewport {
 	x, y, width, height = board.SanitizeViewPort(bs, x, y, width, height)
 
-	/*max(x, 0)
-	x = min(x, bs.Width)
-	y = max(y, 0)
-	y = min(y, bs.Height)*/
-
 	url := "http://localhost:8080/games/board/" + strconv.FormatInt(gameId, 10) + "?x=" + strconv.Itoa(x) + "&y=" + strconv.Itoa(y) + "&rows=" + strconv.Itoa(height) + "&cols=" + strconv.Itoa(width)
 	fmt.Println("getViewPort:>", url)
 	resp, _ := http.Get(url)
 	board := board.ReadJSON(x, y, resp.Body)
+	resp.Body.Close()
 	return &viewport{
 		x:      x,
 		y:      y,
@@ -168,8 +169,8 @@ func (v viewport) Get(x, y int) board.TileType {
 	return v.Board.Get(x-v.x, y-v.y)
 }
 
-const viewPortWidth = 1024
-const viewPortHeight = 1024
+const viewPortWidth = 128
+const viewPortHeight = 128
 
 func max(a, b int) int {
 	if a >= b {
@@ -185,17 +186,18 @@ func min(a, b int) int {
 	return b
 }
 
-func move(gameId int64, p *player.Player, players []*player.Player, bs *board.Board) {
+func move(gameId int64, p *player.Player, players []*player.Player, bs *board.Board) bool {
+	fmt.Println("Lets move")
+	r := rand.New(rand.NewSource(time.Now().UnixNano()))
 	if bs.Finish.X == p.Pos.X && p.Pos.Y == bs.Finish.Y {
 		fmt.Println("HELP i'm finished stop me!!")
-		time.Sleep(1 * time.Second)
-		return
+		return false
 	}
 	//determine move
 	viewPortX := 0
 	viewPortY := 0
 	if viewPortHeight <= bs.Height || viewPortWidth <= bs.Width {
-		viewPortX = p.Pos.X - 1
+		viewPortX = p.Pos.X - viewPortWidth/2
 		viewPortY = p.Pos.Y - viewPortHeight/2
 	}
 	viewPort := getViewPort(viewPortX, viewPortY, viewPortWidth, viewPortHeight, gameId, bs)
@@ -204,70 +206,116 @@ func move(gameId int64, p *player.Player, players []*player.Player, bs *board.Bo
 
 	direction := "E"
 
-	igloY := bs.Finish.Y
+	igloY := (bs.Finish.Y - viewPort.y)-1
 	if bs.Finish.Y < viewPort.y {
 		//iglo ten noorden van viewport
-		fmt.Println("iglo boven")
+		fmt.Println("iglo Y boven")
 		igloY = 0
 		/*for  viewPort.Get(viewPort.x+viewPortWidth, igloY) == board.Rock && igloY <= viewPort.y+viewPortHeight {
 			igloY++
 		}*/
 	} else if bs.Finish.Y > viewPort.y+viewPort.Board.Height {
 		//iglo ten zuiden van viewport
-		fmt.Println("iglo onder")
+		fmt.Println("iglo Y onder")
 		igloY = viewPort.Board.Height - 1
 		/*for  viewPort.Get(viewPort.x+viewPortWidth, igloY) == board.Rock  && igloY >= viewPort.y+viewPortHeight {
 			igloY--
 		}*/
 	}
 
-	igloX := viewPort.Board.Width
-	if viewPort.x < bs.Finish.X && viewPort.x+viewPort.Board.Width > bs.Finish.X {
-		igloX = bs.Finish.X
+	igloX := viewPort.Board.Width - 1
+	
+	if bs.Finish.X < viewPort.x {
+		//iglo links
+		fmt.Println("iglo X links")
+		igloX = 0
+	} else if bs.Finish.X > viewPort.x+viewPort.Board.Width {
+		//iglo rechts
+		fmt.Println("iglo X rechts")
+		igloX = viewPort.Board.Width - 1
+	} else if viewPort.x < bs.Finish.X && viewPort.x+viewPort.Board.Width > bs.Finish.X {
+		// iglo in range
+		igloX = (bs.Finish.X - viewPort.x)
 	}
 
-	//fmt.Printf("Virtuele iglo geplaast op x %v y %v\n", igloX, igloY)
+	fmt.Printf("Virtuele iglo geplaast op x %v y %v\n", igloX, igloY)
 
 	dist := calcDist(igloX, igloY, viewPort)
+	for igloX > 100 && dist[toIndex(p.Pos.X-viewPort.x, p.Pos.Y-viewPort.y, viewPort.Board.Width)] == 0 {
+		igloX--
+		fmt.Printf("replaced iglo to %v x %v\n", igloX, igloY)
+		dist = calcDist(igloX, igloY, viewPort)
 
-	/*for i := 0;i< viewPort.Board.Height;i++ {
-		for j := 0;j< viewPort.Board.Width;j++ {
-			if(j == p.Pos.X && i == p.Pos.Y) {
-				fmt.Printf("X")
-			}
-			if(j == bs.Finish.X && i == bs.Finish.Y) {
-				fmt.Printf("I")
+/*
+		for i := 0; i < viewPort.Board.Height; i++ {
+			for j := 0; j < viewPort.Board.Width; j++ {
+				if j == p.Pos.X-viewPort.x && i == p.Pos.Y-viewPort.y {
+					fmt.Printf("X")
+				}
+				if j == igloX && i == igloY {
+					fmt.Printf("I")
+				}
+				if dist[(i*viewPort.Board.Width)+j] < 10 {
+					fmt.Printf("0")
+				}
+				fmt.Printf("%v ", dist[(i*viewPort.Board.Width)+j])
 			}
-			fmt.Printf("%v ",  dist[(i*viewPort.Board.Width)+j])
+			fmt.Printf("\n")
 		}
-		fmt.Printf("\n")
-	}*/
+		
+		time.Sleep(1 * time.Second)*/
+	}
 
 	smallestDist := math.MaxInt32
 
-	if p.Pos.X+1 < viewPort.width && dist[toIndex(p.Pos.X+1, p.Pos.Y, viewPort.Board.Width)] != 0 {
+	if p.Pos.X+1-viewPort.x < viewPort.width && dist[toIndex(p.Pos.X+1-viewPort.x, p.Pos.Y-viewPort.y, viewPort.Board.Width)] != 0 {
 		direction = "E"
-		smallestDist = dist[toIndex(p.Pos.X+1, p.Pos.Y, viewPort.Board.Width)]
+		smallestDist = dist[toIndex(p.Pos.X+1-viewPort.x, p.Pos.Y-viewPort.y, viewPort.Board.Width)]
 	}
-	if p.Pos.X-1 >= 0 && dist[toIndex(p.Pos.X-1, p.Pos.Y, viewPort.Board.Width)] != 0 && smallestDist > dist[toIndex(p.Pos.X-1, p.Pos.Y, viewPort.Board.Width)] {
+	if p.Pos.X-1-viewPort.x >= 0 && dist[toIndex(p.Pos.X-1-viewPort.x, p.Pos.Y-viewPort.y, viewPort.Board.Width)] != 0 && smallestDist > dist[toIndex(p.Pos.X-1-viewPort.x, p.Pos.Y-viewPort.y, viewPort.Board.Width)] {
 		direction = "W"
-		smallestDist = dist[toIndex(p.Pos.X-1, p.Pos.Y, viewPort.Board.Width)]
+		smallestDist = dist[toIndex(p.Pos.X-1-viewPort.x, p.Pos.Y-viewPort.y, viewPort.Board.Width)]
 	}
-	if p.Pos.Y+1 < viewPort.height && dist[toIndex(p.Pos.X, p.Pos.Y+1, viewPort.Board.Width)] != 0 && smallestDist > dist[toIndex(p.Pos.X, p.Pos.Y+1, viewPort.Board.Width)] {
+	if p.Pos.Y+1-viewPort.y < viewPort.height && dist[toIndex(p.Pos.X-viewPort.x, p.Pos.Y+1-viewPort.y, viewPort.Board.Width)] != 0 && smallestDist > dist[toIndex(p.Pos.X-viewPort.x, p.Pos.Y+1-viewPort.y, viewPort.Board.Width)] {
 		direction = "S"
-		smallestDist = dist[toIndex(p.Pos.X, p.Pos.Y+1, viewPort.Board.Width)]
+		smallestDist = dist[toIndex(p.Pos.X-viewPort.x, p.Pos.Y+1-viewPort.y, viewPort.Board.Width)]
 	}
 
-	if p.Pos.Y-1 >= 0 && dist[toIndex(p.Pos.X, p.Pos.Y-1, viewPort.Board.Width)] != 0 && smallestDist > dist[toIndex(p.Pos.X, p.Pos.Y-1, viewPort.Board.Width)] {
+	if p.Pos.Y-1-viewPort.y >= 0 && dist[toIndex(p.Pos.X-viewPort.x, p.Pos.Y-1-viewPort.y, viewPort.Board.Width)] != 0 && smallestDist > dist[toIndex(p.Pos.X-viewPort.x, p.Pos.Y-1-viewPort.y, viewPort.Board.Width)] {
 		direction = "N"
-		smallestDist = dist[toIndex(p.Pos.X, p.Pos.Y-1, viewPort.Board.Width)]
+		smallestDist = dist[toIndex(p.Pos.X-viewPort.x, p.Pos.Y-1-viewPort.y, viewPort.Board.Width)]
+	}
+
+	if smallestDist == math.MaxInt32 {
+		validMoves := make([]string, 0)
+		if p.Pos.X+1-viewPort.x < viewPort.width && viewPort.Board.Get(p.Pos.X+1-viewPort.x, p.Pos.Y-viewPort.y) != board.Rock {
+			validMoves = append(validMoves, "E")
+			validMoves = append(validMoves, "E")
+		}
+		if p.Pos.X-1-viewPort.x >= 0 && viewPort.Board.Get(p.Pos.X-1-viewPort.x, p.Pos.Y-viewPort.y) != board.Rock {
+			validMoves = append(validMoves, "W")
+		}
+		if p.Pos.Y+1-viewPort.y < viewPort.height && viewPort.Board.Get(p.Pos.X-viewPort.x, p.Pos.Y+1-viewPort.y) != board.Rock {
+			validMoves = append(validMoves, "S")
+			validMoves = append(validMoves, "S")
+			validMoves = append(validMoves, "S")
+		}
+		if p.Pos.Y-1-viewPort.y >= 0 && viewPort.Board.Get(p.Pos.X-viewPort.x, p.Pos.Y-1-viewPort.y) != board.Rock {
+			validMoves = append(validMoves, "N")
+			validMoves = append(validMoves, "N")
+			validMoves = append(validMoves, "N")
+		}
+		rInt := r.Intn(len(validMoves))
+		direction = validMoves[rInt]
 	}
 
 	//send move
 	url := "http://localhost:8080/games/" + strconv.FormatInt(gameId, 10) + "/move/" + p.Id + "/" + direction
-	fmt.Println("move:>", url, smallestDist)
+	fmt.Println("move:>", url, smallestDist, p.Pos.X, p.Pos.Y)
+
+	resp, _ := http.Post(url, "text/plain", nil)
 
-	http.Post(url, "text/plain", nil)
+	resp.Body.Close()
 
 	//update x,y
 	switch direction {
@@ -280,7 +328,8 @@ func move(gameId int64, p *player.Player, players []*player.Player, bs *board.Bo
 	case "W":
 		p.Pos.X--
 	}
-	//fmt.Printf("new pos x=%v y=%v\n", p.Pos.X, p.Pos.Y)
+	fmt.Printf("new pos x=%v y=%v\n", p.Pos.X, p.Pos.Y)
+	return true
 }
 
 func toXy(index, boardWidth int) (x, y int) {

+ 2 - 0
battlecamp-go-gameserver/gameserver/urlrouter.go

@@ -141,10 +141,12 @@ func showBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
 	b := gameServer.GetGame(id).Board
 
 	if xString != "" && yString != "" && widthString != "" && heightString != "" {
+		log.Printf("showboard x %v y %v width %v height %v", )
 		x, _ := strconv.Atoi(xString)
 		y, _ := strconv.Atoi(yString)
 		width, _ := strconv.Atoi(widthString)
 		height, _ := strconv.Atoi(heightString)
+		log.Printf("showboard x %v y %v width %v height %v", x, y, width, height)
 		b.WriteJSON(w, x, y, width, height)
 	} else if xString != "" || yString != "" || widthString != "" || heightString != "" {
 		http.Error(w, "Bad request", http.StatusBadRequest)