aboutsummaryrefslogtreecommitdiff
path: root/go/game/permanent.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-02-25 23:08:47 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-08-20 15:57:06 +0200
commit3d36d1d0254b8aeecc889a2a49cbcced458cbe9c (patch)
tree38dd0851d8eaa6535d05b97f29f65fc35655a5ef /go/game/permanent.go
parent19fb1a7e5c5883f49e944b6499fa1ced207a3e39 (diff)
downloadmuhqs-game-3d36d1d0254b8aeecc889a2a49cbcced458cbe9c.tar.gz
muhqs-game-3d36d1d0254b8aeecc889a2a49cbcced458cbe9c.zip
intermediate commit
* Implement move artifact action * Fix widget update memory leak * update the highlights not during on each frame * do not update the unchanged label of a button * Allow to exit the game by pressing 'q' * Implement some cards from the magic set * Improve permanent formatting * Some tweaks to UnitAI code
Diffstat (limited to 'go/game/permanent.go')
-rw-r--r--go/game/permanent.go79
1 files changed, 61 insertions, 18 deletions
diff --git a/go/game/permanent.go b/go/game/permanent.go
index ae94a657..dfa275a6 100644
--- a/go/game/permanent.go
+++ b/go/game/permanent.go
@@ -7,6 +7,8 @@ import (
"strings"
"golang.org/x/exp/slices"
+
+ "muhq.space/muhqs-game/go/utils"
)
type Permanent interface {
@@ -33,7 +35,9 @@ type Permanent interface {
IsAvailableTile(*Tile) bool
fight(p Permanent)
- addDamage(damage int)
+ adjustDamage(damage int)
+ addTmpEffect(tmpEffect string)
+ removeTmpEffect(tmpEffect string)
setContainingPerm(p Permanent)
addPermanentToPile(p Permanent)
clearPile()
@@ -77,13 +81,33 @@ 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 {
- if p.Tile() != nil {
- return fmt.Sprintf("%s's %s@%v",
- p.Controller().Name, p.Card().Name, p.Tile().Position)
+func FmtPermanent(p Permanent) string {
+ typeIndicator := strings.ToUpper(p.Card().Type.String()[:1])
+
+ containing := p.ContainingPerm()
+ if containing == nil {
+ return fmt.Sprintf("%s%v", typeIndicator, p.Tile().Position)
+ }
+
+ for i, p := range containing.Pile() {
+ if p == p {
+ return fmt.Sprintf("%s%v[%d]", typeIndicator, p.Tile().Position, i+1)
+ }
+ }
+
+ log.Panicf("Permanent piled at %v not found in the pile", TileOrContainingPermTile(p).Position)
+ return ""
+}
+
+func FindPermanent(s *State, desc string) Permanent {
+ var pos Position
+ _, err := fmt.Sscanf(desc[1:strings.Index(desc, ")")], POSITION_FMT, &pos.X, &pos.Y)
+ if err != nil {
+ log.Panicf("desc %q not a valid permanent description", desc)
}
- return fmt.Sprintf("%s's %s", p.Controller().Name, p.Card().Name)
+ p := s.Map.TileAt(pos).Permanent
+ return p
}
func DealDamage(src, dest Permanent, damage int) {
@@ -97,11 +121,24 @@ func DealDamage(src, dest Permanent, damage int) {
actualDamage = 0
}
}
- dest.addDamage(actualDamage)
+ dest.adjustDamage(actualDamage)
+}
+
+func (p *permanentBase) adjustDamage(damage int) {
+ p.damage += damage
+ if p.damage < 0 {
+ p.damage = 0
+ }
}
+func (p *permanentBase) fight(Permanent) {}
-func (p *permanentBase) addDamage(damage int) { p.damage += damage }
-func (p *permanentBase) fight(Permanent) {}
+func (p *permanentBase) addTmpEffect(tmpEffect string) {
+ p.tmpEffects = append(p.tmpEffects, tmpEffect)
+}
+
+func (p *permanentBase) removeTmpEffect(tmpEffect string) {
+ p.tmpEffects = utils.RemoveFromUnorderedSlice[string](p.tmpEffects, tmpEffect)
+}
func (p *permanentBase) Effects() []string {
return append(p.card.getEffects(), p.tmpEffects...)
@@ -158,15 +195,7 @@ func (p *permanentBase) addPermanentToPile(a Permanent) { p.pile = appen
func (p *permanentBase) clearPile() { p.pile = nil }
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
- }
+ p.pile = utils.RemoveFromUnorderedSlice[Permanent](p.pile, rm)
}
func (b *permanentBase) onPile(containing Permanent) { b.Card().Impl.onPile(containing) }
@@ -188,6 +217,20 @@ func removePermanentFromPile(containing, rm Permanent) {
rm.setContainingPerm(nil)
}
+func movePermanent(p Permanent, t *Tile) {
+ if !p.IsAvailableTile(t) {
+ log.Panicf("moving %v to not available tile %v", p, t)
+ }
+
+ if p.ContainingPerm() != nil {
+ removePermanentFromPile(p.ContainingPerm(), p)
+ } else {
+ leaveTile(p)
+ }
+
+ enterTileOrPile(p, t)
+}
+
func enterTile(p Permanent, t *Tile) {
p.SetTile(t)
t.entering(p)