Пример #1
0
func NewPlayer(myStage *stage.PlayerStage, balance money.PlayerBalance) *Player {
	AStar := astar.NewAStar(stage.Spawn_Size+stage.Grass_Rows+1, stage.Grass_Cols)

	astar_source := []astar.Point{astar.Point{Row: stage.Spawn_Size - 1, Col: stage.Grass_Cols / 2}}
	astar_target := make([]astar.Point, stage.Grass_Cols)
	for i := 0; i < stage.Grass_Cols; i++ {
		astar_target[i].Row = stage.Spawn_Size + stage.Grass_Rows + 1
		astar_target[i].Col = i
	}

	p := &Player{
		myStage: myStage,

		astar_source: astar_source,
		astar_target: astar_target,

		AStar:  AStar,
		Towers: make(map[pathing.Loc]*tower.Tower),
		Units:  make(map[int]*unit.Unit),

		// TODO: make this a proper variable
		lives: 50,

		Money: balance,
	}
	p.Repath()

	return p
}
Пример #2
0
func showGrid() {
	cols, rows := termbox.Size()
	a := astar.NewAStar(rows, cols)
	seed := time.Now().UnixNano()
	r := rand.New(rand.NewSource(seed))

	termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)

	source := astar.Point{Col: r.Intn(cols), Row: r.Intn(rows)}
	target := astar.Point{Col: r.Intn(cols), Row: r.Intn(rows)}

	for i := 0; i < 400; i++ {
		wall := astar.Point{Col: r.Intn(cols), Row: r.Intn(rows)}
		a.FillTile(wall, -1)
		termbox.SetCell(wall.Col, wall.Row, '#', termbox.ColorDefault, termbox.ColorDefault)
	}

	path := a.FindPath(astar.NewPointToPoint(), []astar.Point{source}, []astar.Point{target})

	for path != nil {
		path = path.Parent
		termbox.SetCell(path.Col, path.Row, '.', termbox.ColorDefault, termbox.ColorDefault)
		path = nil //path.Parent
	}

	termbox.SetCell(source.Col, source.Row, 's', termbox.ColorDefault, termbox.ColorDefault)
	termbox.SetCell(target.Col, target.Row, 't', termbox.ColorDefault, termbox.ColorDefault)
	termbox.Flush()
	termbox.PollEvent()
}
Пример #3
0
func main() {

	var start_t int64
	var end_t int64

	var seed int64 = 0

	// Setup the aStar structs
	ast := astar.NewAStar(50, 50)

	p2p := astar.NewPointToPoint()
	p2l := astar.NewListToPoint(true)

	// Generate a random map
	grid, source, target, me := GenerateRandomMap(ast, seed, 50, 600, 24, 100000)
	PrintGrid(grid)

	// Route from source to target (point to point)
	start_t = time.Now().UnixNano()
	end := ast.FindPath(p2p, source, target)
	end_t = time.Now().UnixNano()

	first_path_t := float64(end_t-start_t) / float64(time.Millisecond)

	DrawPath(grid, end, "*")
	PrintGrid(grid)

	// record path as array so it can be used in the next search
	p := end
	path := make([]astar.Point, 0)
	for p != nil {
		path = append(path, p.Point)
		p = p.Parent
	}

	start_t = time.Now().UnixNano()
	end = ast.FindPath(p2l, path, me)
	end_t = time.Now().UnixNano()
	second_path_t := float64(end_t-start_t) / float64(time.Millisecond)

	DrawPath(grid, end, ".")
	PrintGrid(grid)

	fmt.Println("me", me)
	fmt.Println("end", end)
	fmt.Println("end_grid", grid[end.Row][end.Col])
	fmt.Println(first_path_t)
	fmt.Println(second_path_t)
}