Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

engine.gno

// Package engine is the deterministic battle core of Cage Match. Two
// fighters trade actions on a small grid; the same two fighters always
// produce the same recap, so any recorded fight can be replayed and
// verified by anyone, on or off chain.
package engine

const (
	GridSize     = 9
	MaxHP        = 100
	MaxRounds    = 60
	MeleeDamage  = 12
	ChargeDamage = 24
)

// Pos is a tile on the grid. (0,0) is the top-left corner.
type Pos struct {
	X, Y int
}

// Dir is a cardinal direction.
type Dir int

const (
	North Dir = iota
	East
	South
	West
)

// Delta returns the tile one step in direction d from p.
func (p Pos) Delta(d Dir) Pos {
	switch d {
	case North:
		return Pos{p.X, p.Y - 1}
	case East:
		return Pos{p.X + 1, p.Y}
	case South:
		return Pos{p.X, p.Y + 1}
	default:
		return Pos{p.X - 1, p.Y}
	}
}

func (p Pos) clamp() Pos {
	if p.X < 0 {
		p.X = 0
	}
	if p.Y < 0 {
		p.Y = 0
	}
	if p.X >= GridSize {
		p.X = GridSize - 1
	}
	if p.Y >= GridSize {
		p.Y = GridSize - 1
	}
	return p
}

// ActionKind is what a fighter does in a round.
type ActionKind int

const (
	Idle ActionKind = iota
	Move
	Attack
	Charge
	Defend
)

// Action is one round's decision. Dir is used by Move and Attack.
type Action struct {
	Kind ActionKind
	Dir  Dir
}

// FighterState is the public state of one fighter.
type FighterState struct {
	P         Pos
	HP        int
	Charged   bool // next Attack deals ChargeDamage, then clears
	Defending bool // incoming damage halved this round
}

// View is what a fighter sees at the start of a round.
type View struct {
	Round int
	Self  FighterState
	Enemy FighterState
}

// Fighter is a battle program. Step must be deterministic.
type Fighter interface {
	Name() string
	Step(View) Action
}

// Frame is one resolved round of a fight.
type Frame struct {
	Round      int
	APos, BPos Pos
	AHP, BHP   int
	AAct, BAct Action
	ADmg, BDmg int // damage dealt to the enemy this round
}

// Recap is the full record of a fight. Winner is 0 for A, 1 for B, -1 draw.
type Recap struct {
	A, B   string
	Winner int
	Rounds int
	Frames []Frame
}

// Run resolves a whole fight and returns its recap.
func Run(a, b Fighter) Recap {
	sa := FighterState{P: Pos{0, GridSize / 2}, HP: MaxHP}
	sb := FighterState{P: Pos{GridSize - 1, GridSize / 2}, HP: MaxHP}
	r := Recap{A: a.Name(), B: b.Name(), Winner: -1}
	for round := 1; round <= MaxRounds; round++ {
		aa := a.Step(View{Round: round, Self: sa, Enemy: sb})
		ab := b.Step(View{Round: round, Self: sb, Enemy: sa})
		frame := Frame{Round: round, AAct: aa, BAct: ab}
		sa.Defending = aa.Kind == Defend
		sb.Defending = ab.Kind == Defend

		// 1. Moves resolve in order: A first, then B. A move onto a tile
		// the enemy currently occupies fails (the fighter stays put).
		// Documented and deterministic: initiative always belongs to A.
		if aa.Kind == Move {
			p := sa.P.Delta(aa.Dir).clamp()
			if p != sb.P {
				sa.P = p
			}
		}
		if ab.Kind == Move {
			p := sb.P.Delta(ab.Dir).clamp()
			if p != sa.P {
				sb.P = p
			}
		}

		// 2. Charges arm before attacks resolve.
		if aa.Kind == Charge {
			sa.Charged = true
		}
		if ab.Kind == Charge {
			sb.Charged = true
		}

		// 3. Attacks. A strike hits if its target tile holds the enemy.
		if aa.Kind == Attack && sa.P.Delta(aa.Dir) == sb.P {
			frame.ADmg = strike(&sa, sb.Defending)
			sb.HP -= frame.ADmg
		}
		if ab.Kind == Attack && sb.P.Delta(ab.Dir) == sa.P {
			frame.BDmg = strike(&sb, sa.Defending)
			sa.HP -= frame.BDmg
		}
		if sb.HP < 0 {
			sb.HP = 0
		}
		if sa.HP < 0 {
			sa.HP = 0
		}

		frame.APos, frame.BPos, frame.AHP, frame.BHP = sa.P, sb.P, sa.HP, sb.HP
		r.Frames = append(r.Frames, frame)
		r.Rounds = round

		if sa.HP == 0 || sb.HP == 0 {
			switch {
			case sa.HP == 0 && sb.HP == 0:
				r.Winner = -1 // double knockout
			case sb.HP == 0:
				r.Winner = 0
			default:
				r.Winner = 1
			}
			return r
		}
	}
	// Time limit: higher HP wins; equal HP is a draw.
	switch {
	case sa.HP > sb.HP:
		r.Winner = 0
	case sb.HP > sa.HP:
		r.Winner = 1
	}
	return r
}

// strike computes attack damage, consuming charge and applying defense.
func strike(att *FighterState, defending bool) int {
	dmg := MeleeDamage
	if att.Charged {
		dmg = ChargeDamage
		att.Charged = false
	}
	if defending {
		dmg /= 2
	}
	return dmg
}

gnomod.toml

module = "gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cagematch/engine"
gno = "0.9"

[addpkg]
  creator = "g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v"
  height = 1534431
  max_deposit = "100000000ugnot"