Skip to content

Commit d2601ad

Browse files
committed
[bots/go] feat proper go softcore my-core-bot starter template
1 parent 737b32c commit d2601ad

File tree

6 files changed

+93
-37
lines changed

6 files changed

+93
-37
lines changed

bots/go/softcore/gridmaster/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ OBJDIR = build
22
EXEC = gridmaster
33

44
build:
5-
go build -o $(EXEC) main.go
5+
go build -o $(EXEC) *.go
66

77
run: build
88
./$(EXEC)

bots/go/softcore/gridmaster/main.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,23 @@ import (
1111

1212
const teamName = "Gridmaster"
1313

14-
func isEnemyCore(myTeamID uint) func(*game.Object) bool {
15-
return func(obj *game.Object) bool {
16-
if obj.Type != game.ObjectCore {
17-
return false
18-
}
19-
data := obj.GetCoreData()
20-
if data == nil {
21-
return false
22-
}
23-
return data.TeamID != myTeamID
14+
func moveToward(bot *coregame.Bot, unit *game.Object, targetPos game.Position) {
15+
nextPos := bot.SimplePathfind(unit, targetPos)
16+
if nextPos != unit.Pos {
17+
bot.Move(unit, nextPos)
2418
}
2519
}
2620

2721
func tick(g *game.Game, bot *coregame.Bot) {
2822
bot.CreateUnit(game.UnitWarrior)
2923

30-
enemyCore := g.NearestObject(game.Position{X: 0, Y: 0}, isEnemyCore(g.MyTeamID))
24+
enemyCore := g.EnemyCore()
3125
if enemyCore == nil {
3226
return
3327
}
3428

3529
for _, unit := range g.TeamUnits() {
36-
pos := bot.SimplePathfind(unit, enemyCore.Pos)
37-
bot.Move(unit, pos)
30+
moveToward(bot, unit, enemyCore.Pos)
3831
}
3932
}
4033

bots/go/softcore/my-core-bot/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ OBJDIR = build
22
EXEC = bot
33

44
build:
5-
go build -o $(EXEC) main.go
5+
go build -o $(EXEC) *.go
66

77
run: build
88
./$(EXEC)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import "github.com/42core-team/go-client-lib/game"
4+
5+
func ownCore(g *game.Game) *game.Object {
6+
return g.MyCore()
7+
}
8+
9+
func opponentCore(g *game.Game) *game.Object {
10+
return g.EnemyCore()
11+
}
12+
13+
func nearestDeposit(g *game.Game, pos game.Position) *game.Object {
14+
return g.NearestObject(pos, isDeposit)
15+
}
16+
17+
func nearestGemPile(g *game.Game, pos game.Position) *game.Object {
18+
return g.NearestObject(pos, isGemPile)
19+
}
20+
21+
func nearestDepositOrGemPile(g *game.Game, pos game.Position) *game.Object {
22+
return g.NearestObject(pos, isDepositOrGemPile)
23+
}
24+
25+
func ownUnits(g *game.Game) []*game.Object {
26+
return g.ObjectsFilter(isOwnUnit(g.MyTeamID))
27+
}
28+
29+
func opponentUnits(g *game.Game) []*game.Object {
30+
return g.ObjectsFilter(isOpponentUnit(g.MyTeamID))
31+
}
32+
33+
func nearestOpponentUnit(g *game.Game, pos game.Position) *game.Object {
34+
return g.NearestObject(pos, isOpponentUnit(g.MyTeamID))
35+
}
36+
37+
func isDeposit(obj *game.Object) bool {
38+
return obj != nil && obj.Type == game.ObjectDeposit
39+
}
40+
41+
func isGemPile(obj *game.Object) bool {
42+
return obj != nil && obj.Type == game.ObjectGemPile
43+
}
44+
45+
func isDepositOrGemPile(obj *game.Object) bool {
46+
return isDeposit(obj) || isGemPile(obj)
47+
}
48+
49+
func isOwnUnit(myTeamID uint) func(*game.Object) bool {
50+
return func(obj *game.Object) bool {
51+
if obj == nil || obj.Type != game.ObjectUnit {
52+
return false
53+
}
54+
data := obj.GetUnitData()
55+
return data != nil && data.TeamID == myTeamID
56+
}
57+
}
58+
59+
func isOpponentUnit(myTeamID uint) func(*game.Object) bool {
60+
return func(obj *game.Object) bool {
61+
if obj == nil || obj.Type != game.ObjectUnit {
62+
return false
63+
}
64+
data := obj.GetUnitData()
65+
return data != nil && data.TeamID != myTeamID
66+
}
67+
}

bots/go/softcore/my-core-bot/main.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,39 @@ import (
99
"github.com/42core-team/go-client-lib/game"
1010
)
1111

12-
const teamName = "Gridmaster"
12+
const teamName = "YOUR TEAM NAME HERE"
1313

14-
func isEnemyCore(myTeamID uint) func(*game.Object) bool {
15-
return func(obj *game.Object) bool {
16-
if obj.Type != game.ObjectCore {
17-
return false
18-
}
19-
data := obj.GetCoreData()
20-
if data == nil {
21-
return false
22-
}
23-
return data.TeamID != myTeamID
14+
func moveUnitIfNeeded(bot *coregame.Bot, unit *game.Object, nextPos game.Position) {
15+
if nextPos != unit.Pos {
16+
bot.Move(unit, nextPos)
2417
}
2518
}
2619

2720
func tick(g *game.Game, bot *coregame.Bot) {
28-
if g.MyCore().GetCoreData().Gems > g.Config.Units[game.UnitWarrior].Cost {
29-
bot.CreateUnit(game.UnitWarrior)
30-
}
21+
fmt.Printf("-----> [⚡️ TICK %d 🔥]\n", g.ElapsedTicks)
22+
23+
bot.CreateUnit(game.UnitWarrior)
3124

32-
enemyCore := g.NearestObject(game.Position{X: 0, Y: 0}, isEnemyCore(g.MyTeamID))
25+
enemyCore := opponentCore(g)
3326
if enemyCore == nil {
3427
return
3528
}
3629

37-
for _, unit := range g.TeamUnits() {
38-
pos := bot.SimplePathfind(unit, enemyCore.Pos)
39-
bot.AddObjectPathStep(unit, pos)
40-
if *unit.GetUnitData().ActionCooldown == 0 {
41-
bot.Move(unit, pos)
42-
}
30+
for _, unit := range ownUnits(g) {
31+
nextPos := bot.SimplePathfind(unit, enemyCore.Pos)
32+
bot.AddObjectPathStep(unit, nextPos)
33+
moveUnitIfNeeded(bot, unit, nextPos)
34+
bot.AddObjectInfo(unit, fmt.Sprintf(
35+
"I am a warrior! 🗡️ - I am heading for the opponent core at [%d,%d]! 🏰\n",
36+
enemyCore.Pos.X,
37+
enemyCore.Pos.Y,
38+
))
4339
}
4440
}
4541

4642
func main() {
4743
if len(os.Args) < 2 {
48-
fmt.Println("Usage: gridmaster <team-id>")
44+
fmt.Println("Usage: bot <team-id>")
4945
os.Exit(1)
5046
}
5147

-3.75 MB
Binary file not shown.

0 commit comments

Comments
 (0)