-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from SladeThe/master
Fix bugs and some style issues in Go strategy
- Loading branch information
Showing
2 changed files
with
33 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
module aicup2019 | ||
module aicup2019 | ||
|
||
go 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,72 @@ | ||
package main | ||
|
||
import . "aicup2019/model" | ||
import "fmt" | ||
import ( | ||
"fmt" | ||
|
||
type MyStrategy struct {} | ||
. "aicup2019/model" | ||
) | ||
|
||
type MyStrategy struct{} | ||
|
||
func NewMyStrategy() MyStrategy { | ||
return MyStrategy {} | ||
return MyStrategy{} | ||
} | ||
|
||
func distanceSqr(a Vec2Float64, b Vec2Float64) float64 { | ||
return (a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y) | ||
return (a.X-b.X)*(a.X-b.X) + (a.Y-b.Y)*(a.Y-b.Y) | ||
} | ||
|
||
func (strategy MyStrategy) getAction(unit Unit, game Game, debug Debug) UnitAction { | ||
var nearestEnemy *Unit | ||
for _, other := range game.Units { | ||
for i, other := range game.Units { | ||
if other.PlayerId != unit.PlayerId { | ||
if nearestEnemy == nil || distanceSqr(unit.Position, other.Position) < distanceSqr(unit.Position, nearestEnemy.Position) { | ||
nearestEnemy = &other | ||
nearestEnemy = &game.Units[i] | ||
} | ||
} | ||
} | ||
var nearestWeapon *LootBox | ||
for _, lootBox := range game.LootBoxes { | ||
for i, lootBox := range game.LootBoxes { | ||
switch lootBox.Item.(type) { | ||
case *ItemWeapon: | ||
case ItemWeapon: | ||
if nearestWeapon == nil || distanceSqr(unit.Position, lootBox.Position) < distanceSqr(unit.Position, nearestWeapon.Position) { | ||
nearestWeapon = &lootBox | ||
nearestWeapon = &game.LootBoxes[i] | ||
} | ||
} | ||
} | ||
targetPos := unit.Position; | ||
targetPos := unit.Position | ||
if unit.Weapon == nil && nearestWeapon != nil { | ||
targetPos = nearestWeapon.Position | ||
} else if nearestEnemy != nil { | ||
targetPos = nearestEnemy.Position | ||
} | ||
debug.Draw(CustomDataLog { | ||
debug.Draw(CustomDataLog{ | ||
Text: fmt.Sprintf("Target pos: %v", targetPos), | ||
}); | ||
aim := Vec2Float64 { | ||
}) | ||
aim := Vec2Float64{ | ||
X: 0, | ||
Y: 0, | ||
} | ||
if nearestEnemy != nil { | ||
aim = Vec2Float64 { | ||
aim = Vec2Float64{ | ||
X: nearestEnemy.Position.X - unit.Position.X, | ||
Y: nearestEnemy.Position.Y - unit.Position.Y, | ||
} | ||
} | ||
jump := targetPos.Y > unit.Position.Y | ||
if targetPos.X > unit.Position.X && game.Level.Tiles[int(unit.Position.X + 1)][int(unit.Position.Y)] == TileWall { | ||
jump = true; | ||
if targetPos.X > unit.Position.X && game.Level.Tiles[int(unit.Position.X+1)][int(unit.Position.Y)] == TileWall { | ||
jump = true | ||
} | ||
if targetPos.X < unit.Position.X && game.Level.Tiles[int(unit.Position.X - 1)][int(unit.Position.Y)] == TileWall { | ||
jump = true; | ||
if targetPos.X < unit.Position.X && game.Level.Tiles[int(unit.Position.X-1)][int(unit.Position.Y)] == TileWall { | ||
jump = true | ||
} | ||
return UnitAction { | ||
Velocity: targetPos.X - unit.Position.X, | ||
Jump: jump, | ||
JumpDown: !jump, | ||
Aim: aim, | ||
return UnitAction{ | ||
Velocity: targetPos.X - unit.Position.X, | ||
Jump: jump, | ||
JumpDown: !jump, | ||
Aim: aim, | ||
SwapWeapon: false, | ||
PlantMine: false, | ||
PlantMine: false, | ||
Shoot: true, | ||
} | ||
} | ||
} |