package game import ( "fmt" "log" "strconv" "strings" "golang.org/x/exp/slices" ) type Permanent interface { Card() *Card Tile() *Tile Damage() int Pile() []Permanent Controller() *Player Owner() *Player Effects() []string HasEffect(string) bool XEffect(string) (int, error) String() string Fight(p Permanent) AddDamage(damage int) IsDestroyed() bool Attackable() bool AvailActions() []Action IsAvailableTile(*Tile) bool SetContainingPerm(p Permanent) AddPermanentToPile(p Permanent) RemovePermanentFromPile(p Permanent) onPile(containing Permanent) onUnpile(containing Permanent) onDrop(containing Permanent) } type PermanentBase struct { card *Card tile *Tile damage int pile []Permanent containingPerm Permanent controller *Player owner *Player tmpEffects []string } func NewPermanentBase(card *Card, tile *Tile, owner *Player) PermanentBase { if tile != nil && tile.Permanent != nil { log.Fatalf("Tile at %v already occupied by %v", tile.Position, tile.Permanent) } return PermanentBase{ card: card, tile: tile, controller: owner, owner: owner, } } func (p *PermanentBase) Card() *Card { return p.card } func (p *PermanentBase) Tile() *Tile { return p.tile } func (p *PermanentBase) Damage() int { return p.damage } func (p *PermanentBase) Pile() []Permanent { return p.pile } func (p *PermanentBase) Controller() *Player { return p.controller } func (p *PermanentBase) Owner() *Player { return p.owner } func (p *PermanentBase) String() string { return fmt.Sprintf("%s's %s@%s", p.Controller().Name, p.Card().Name, p.Tile().Position.String()) } func DealDamage(src, dest Permanent, damage int) { actualDamage := damage shield, err := dest.XEffect("shield") if err != nil { // TODO: implement piercing actualDamage -= shield if actualDamage < 0 { actualDamage = 0 } } dest.AddDamage(actualDamage) } func (p *PermanentBase) AddDamage(damage int) { p.damage += damage } func (p *PermanentBase) Effects() []string { return append(p.card.getEffects(), p.tmpEffects...) } func (p *PermanentBase) HasEffect(effect string) bool { return slices.Contains(p.tmpEffects, effect) || p.card.hasEffect(effect) } func (p *PermanentBase) XEffect(effect string) (int, error) { x, err := p.card.getXEffect(effect) for _, e := range p.tmpEffects { if !strings.Contains(e, effect) { continue } tokens := strings.SplitN(e, " ", 2) tmpX, err := strconv.Atoi(tokens[1]) if err != nil { return 0, err } x += tmpX } return x, err } func (p *PermanentBase) IsAvailableTile(tile *Tile) bool { return tile.IsAvailableForCard(p.card) } func (p *PermanentBase) SetContainingPerm(containing Permanent) { p.containingPerm = containing } func (p *PermanentBase) AddPermanentToPile(a Permanent) { p.pile = append(p.pile, a) } func (p *PermanentBase) RemovePermanentFromPile(rm Permanent) { for i, piled := range p.pile { if piled != rm { continue } p.pile[i] = p.pile[len(p.pile)-1] p.pile = p.pile[:len(p.pile)-1] return } } func (b *PermanentBase) onPile(p Permanent) { p.Card().Impl.onPile(p) } func (b *PermanentBase) onUnpile(p Permanent) { p.Card().Impl.onUnpile(p) } func (b *PermanentBase) onDrop(p Permanent) { p.Card().Impl.onDrop(p) } func AddPermanentToPile(containing, p Permanent) { p.SetContainingPerm(containing) containing.AddPermanentToPile(p) p.onPile(p) } func RemovePermanentFromPile(containing, rm Permanent) { containing.RemovePermanentFromPile(rm) rm.onUnpile(containing) rm.SetContainingPerm(nil) }