From c696829f9e37e9f3752fdb89ebaadd4b3b237bea Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sat, 2 Aug 2025 14:47:01 +0200 Subject: add support for flexible attacks --- go/game/unit.go | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'go/game/unit.go') diff --git a/go/game/unit.go b/go/game/unit.go index 463a1ad9..7032d464 100644 --- a/go/game/unit.go +++ b/go/game/unit.go @@ -122,6 +122,16 @@ func (u *Unit) MoveRangeTiles() []*Tile { return tiles } +func (u *Unit) AttackableTile(t *Tile) (bool, Attack) { + if flexAttack := u.Attack.flexAttack; flexAttack != nil { + if _, ok := flexAttack(u, t); ok { + return true, u.Attack + } + } + + return IsPositionInRange(u.Tile().Position, t.Position, u.Attack.MaxRange()), u.Attack +} + func (u *Unit) AttackableTiles() []*Tile { // Units not on the map can not attack if u.tile == nil { @@ -129,9 +139,14 @@ func (u *Unit) AttackableTiles() []*Tile { } m := u.controller.gameState.Map() - tilesInRange := TilesInRange(m, u, u.Attack.MaxRange()) - if u.Attack.MaxRange() == 1 { - return tilesInRange + var tilesInRange []*Tile + if flexAttack := u.Attack.flexAttack; flexAttack != nil { + tilesInRange = m.FilterTiles(func(t *Tile) bool { _, ok := flexAttack(u, t); return ok }) + } else { + tilesInRange = TilesInRange(m, u, u.Attack.MaxRange()) + if u.Attack.MaxRange() == 1 { + return tilesInRange + } } // House tiles are not attackable from ranges > 1 @@ -169,8 +184,10 @@ func (u *Unit) AttackableEnemyPermanents() []Permanent { } func (u *Unit) fight(p Permanent) { - r := DistanceBetweenPermanents(p, u) - DealDamage(u, p, u.Attack.DamageInRange(r)) + d, reachable := u.Attack.DamageForTile(u, p.Tile()) + if reachable { + DealDamage(u, p, d) + } } func (u *Unit) IsDestroyed() bool { @@ -202,7 +219,7 @@ func (u *Unit) AvailSlowActions() (actions []Action) { } m := u.Controller().gameState.Map() - if u.AvailAttackActions > 0 && !INVALID_ATTACK(u.Attack) && + if u.AvailAttackActions > 0 && u.Attack.Valid() && len(u.AttackableEnemyPermanents()) > 0 { actions = append(actions, NewAttackAction(u)) -- cgit v1.2.3