|
| 1 | +package coregame |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/42core-team/go-client-lib/game" |
| 5 | + "github.com/42core-team/go-client-lib/internal" |
| 6 | +) |
| 7 | + |
| 8 | +func (b *Bot) CreateUnit(unitType game.UnitType) { |
| 9 | + b.connection.ActionQueue().Add(internal.Action{ |
| 10 | + Type: internal.ActionCreate, |
| 11 | + UnitType: unitType, |
| 12 | + }) |
| 13 | +} |
| 14 | + |
| 15 | +func (b *Bot) Move(unit *game.Object, pos game.Position) { |
| 16 | + if unit == nil { |
| 17 | + return |
| 18 | + } |
| 19 | + b.connection.ActionQueue().Add(internal.Action{ |
| 20 | + Type: internal.ActionMove, |
| 21 | + UnitID: unit.ID, |
| 22 | + TargetPos: pos, |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +func (b *Bot) Attack(attacker, target *game.Object) { |
| 27 | + if attacker == nil || target == nil { |
| 28 | + return |
| 29 | + } |
| 30 | + b.connection.ActionQueue().Add(internal.Action{ |
| 31 | + Type: internal.ActionAttack, |
| 32 | + UnitID: attacker.ID, |
| 33 | + TargetID: target.ID, |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +func (b *Bot) TransferGems(source *game.Object, targetPos game.Position, amount uint) { |
| 38 | + if source == nil { |
| 39 | + return |
| 40 | + } |
| 41 | + b.connection.ActionQueue().Add(internal.Action{ |
| 42 | + Type: internal.ActionTransfer, |
| 43 | + UnitID: source.ID, |
| 44 | + TargetPos: targetPos, |
| 45 | + Amount: amount, |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func (b *Bot) Build(builder *game.Object, pos game.Position) { |
| 50 | + if builder == nil { |
| 51 | + return |
| 52 | + } |
| 53 | + b.connection.ActionQueue().Add(internal.Action{ |
| 54 | + Type: internal.ActionBuild, |
| 55 | + UnitID: builder.ID, |
| 56 | + TargetPos: pos, |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +func (b *Bot) SimplePathfind(unit *game.Object, targetPos game.Position) game.Position { |
| 61 | + if unit == nil || unit.Type != game.ObjectUnit { |
| 62 | + return game.Position{} |
| 63 | + } |
| 64 | + if unit.Pos.X == targetPos.X && unit.Pos.Y == targetPos.Y { |
| 65 | + return unit.Pos |
| 66 | + } |
| 67 | + |
| 68 | + data := unit.GetUnitData() |
| 69 | + if data == nil || (data.ActionCooldown != nil && *data.ActionCooldown > 0) { |
| 70 | + return unit.Pos |
| 71 | + } |
| 72 | + |
| 73 | + posOptionY := game.Position{X: unit.Pos.X, Y: unit.Pos.Y} |
| 74 | + if targetPos.Y != unit.Pos.Y { |
| 75 | + if targetPos.Y > unit.Pos.Y { |
| 76 | + posOptionY.Y = unit.Pos.Y + 1 |
| 77 | + } else { |
| 78 | + posOptionY.Y = unit.Pos.Y - 1 |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + posOptionX := game.Position{X: unit.Pos.X, Y: unit.Pos.Y} |
| 83 | + if targetPos.X != unit.Pos.X { |
| 84 | + if targetPos.X > unit.Pos.X { |
| 85 | + posOptionX.X = unit.Pos.X + 1 |
| 86 | + } else { |
| 87 | + posOptionX.X = unit.Pos.X - 1 |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + priorityX := 0 |
| 92 | + priorityY := 0 |
| 93 | + |
| 94 | + if !b.game.IsPosValid(posOptionY) || posOptionY.Y == unit.Pos.Y { |
| 95 | + priorityY += 500 |
| 96 | + } |
| 97 | + if !b.game.IsPosValid(posOptionX) || posOptionX.X == unit.Pos.X { |
| 98 | + priorityX += 500 |
| 99 | + } |
| 100 | + |
| 101 | + dx := abs(int(targetPos.X) - int(unit.Pos.X)) |
| 102 | + dy := abs(int(targetPos.Y) - int(unit.Pos.Y)) |
| 103 | + if dx > dy { |
| 104 | + priorityY++ |
| 105 | + } else { |
| 106 | + priorityX++ |
| 107 | + } |
| 108 | + |
| 109 | + objAtX := b.game.ObjectAtPos(posOptionX) |
| 110 | + objAtY := b.game.ObjectAtPos(posOptionY) |
| 111 | + |
| 112 | + if objAtX != nil { |
| 113 | + priorityX += 50 |
| 114 | + } |
| 115 | + if objAtY != nil { |
| 116 | + priorityY += 50 |
| 117 | + } |
| 118 | + |
| 119 | + if objAtX != nil && objAtX.IsFriendly(b.game.MyTeamID) { |
| 120 | + priorityX += 100 |
| 121 | + } |
| 122 | + if objAtY != nil && objAtY.IsFriendly(b.game.MyTeamID) { |
| 123 | + priorityY += 100 |
| 124 | + } |
| 125 | + |
| 126 | + if priorityX < 250 && priorityX < priorityY { |
| 127 | + if objAtX != nil && !objAtX.IsFriendly(b.game.MyTeamID) { |
| 128 | + b.Attack(unit, objAtX) |
| 129 | + } else if objAtX == nil { |
| 130 | + return posOptionX |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if priorityY < 250 { |
| 135 | + if objAtY != nil && !objAtY.IsFriendly(b.game.MyTeamID) { |
| 136 | + b.Attack(unit, objAtY) |
| 137 | + } else if objAtY == nil { |
| 138 | + return posOptionY |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return unit.Pos |
| 143 | +} |
| 144 | + |
| 145 | +func abs(x int) int { |
| 146 | + if x < 0 { |
| 147 | + return -x |
| 148 | + } |
| 149 | + return x |
| 150 | +} |
0 commit comments