From 19fb1a7e5c5883f49e944b6499fa1ced207a3e39 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Fri, 24 Feb 2023 17:54:24 +0100 Subject: implement target disjunction and fix artifact constraint --- go/TODO | 3 +-- go/game/cardImplementations.go | 4 +--- go/game/targets.go | 44 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/go/TODO b/go/TODO index 1aa04869..24825cc8 100644 --- a/go/TODO +++ b/go/TODO @@ -6,9 +6,8 @@ * move * implement equipment actions * drop -* implement target disjunction * implement Pile UI hint -* implement spell target parsing / selection +* implement spell target parsing * implement triggers * implement target selection for triggers * implement draft diff --git a/go/game/cardImplementations.go b/go/game/cardImplementations.go index bc2f7414..1c4d07a5 100644 --- a/go/game/cardImplementations.go +++ b/go/game/cardImplementations.go @@ -5,7 +5,6 @@ import ( ) func adjustMelee(p Permanent, damage int) { - log.Println("adjusting melee") u, ok := p.(*Unit) if !ok { return @@ -99,8 +98,7 @@ func (*pioneerImpl) fullActions(u *Unit) []*FullAction { s := u.Controller().gameState a := newFullAction(u, resolvePrototype, "neutralize tile or artifact") - // TODO: add target disjunktion 'or' - a.targets = newTargets(newTarget(s, newTargetDesc("adjacent tile or artifcat"), a)) + a.targets = newTargets(newTarget(s, newTargetDesc("adjacent tile or adjacent artifact"), a)) return []*FullAction{a} } diff --git a/go/game/targets.go b/go/game/targets.go index 5bc6aa9f..1e80c06d 100644 --- a/go/game/targets.go +++ b/go/game/targets.go @@ -276,6 +276,20 @@ func noPreviousSelectionConstraint(targets *Targets, idx int) TargetConstraintFu } func targetConstraint(desc string, s *State, action Action) TargetConstraintFunc { + if !strings.Contains(desc, " or ") { + return singleTargetConstraint(desc, s, action) + } + + descs := strings.Split(desc, " or ") + constraints := make([]TargetConstraintFunc, 0, len(descs)) + for _, desc := range descs { + constraints = append(constraints, singleTargetConstraint(desc, s, action)) + } + + return disjunction(constraints...) +} + +func singleTargetConstraint(desc string, s *State, action Action) TargetConstraintFunc { constraints := []TargetConstraintFunc{} if strings.Contains(desc, "permanent") { constraints = append(constraints, parsePermanentTargetConstraint(desc, s, action)...) @@ -361,7 +375,7 @@ func rangeTargetConstraint(source interface{}, r int) TargetConstraintFunc { } } -func typeTargetConstrain[T any](typeDesc string) TargetConstraintFunc { +func typeTargetConstraint[T any](typeDesc string) TargetConstraintFunc { return func(t interface{}) (err error) { _, ok := t.(T) if !ok { @@ -371,12 +385,30 @@ func typeTargetConstrain[T any](typeDesc string) TargetConstraintFunc { } } +func cardTypeTargetConstraint(f func(CardType) bool) TargetConstraintFunc { + return func(t interface{}) (err error) { + p, ok := t.(Permanent) + if !ok { + err = fmt.Errorf("unexpected target type %T not Permanent", t) + return + } + + c := p.Card() + if !f(c.Type) { + err = fmt.Errorf("unexpected target card type %v", c.Type) + return + } + + return + } +} + var ( - permanentTargetConstraint TargetConstraintFunc = typeTargetConstrain[Permanent]("Permanent") - unitTargetConstraint TargetConstraintFunc = typeTargetConstrain[*Unit]("*Unit") - tileTargetConstraint TargetConstraintFunc = typeTargetConstrain[*Tile]("*Tile") - artifactTargetConstraint TargetConstraintFunc = typeTargetConstrain[*Artifact]("*Artifact") - cardTargetConstraint TargetConstraintFunc = typeTargetConstrain[*Card]("*Card") + permanentTargetConstraint TargetConstraintFunc = typeTargetConstraint[Permanent]("Permanent") + unitTargetConstraint TargetConstraintFunc = typeTargetConstraint[*Unit]("*Unit") + tileTargetConstraint TargetConstraintFunc = typeTargetConstraint[*Tile]("*Tile") + artifactTargetConstraint TargetConstraintFunc = cardTypeTargetConstraint(CardType.IsArtifact) + cardTargetConstraint TargetConstraintFunc = typeTargetConstraint[*Card]("*Card") ) func parsePermanentTargetConstraint(desc string, s *State, action Action) []TargetConstraintFunc { -- cgit v1.2.3