engine.gno
4.19 Kb · 204 lines
1// Package engine is the deterministic battle core of Cage Match. Two
2// fighters trade actions on a small grid; the same two fighters always
3// produce the same recap, so any recorded fight can be replayed and
4// verified by anyone, on or off chain.
5package engine
6
7const (
8 GridSize = 9
9 MaxHP = 100
10 MaxRounds = 60
11 MeleeDamage = 12
12 ChargeDamage = 24
13)
14
15// Pos is a tile on the grid. (0,0) is the top-left corner.
16type Pos struct {
17 X, Y int
18}
19
20// Dir is a cardinal direction.
21type Dir int
22
23const (
24 North Dir = iota
25 East
26 South
27 West
28)
29
30// Delta returns the tile one step in direction d from p.
31func (p Pos) Delta(d Dir) Pos {
32 switch d {
33 case North:
34 return Pos{p.X, p.Y - 1}
35 case East:
36 return Pos{p.X + 1, p.Y}
37 case South:
38 return Pos{p.X, p.Y + 1}
39 default:
40 return Pos{p.X - 1, p.Y}
41 }
42}
43
44func (p Pos) clamp() Pos {
45 if p.X < 0 {
46 p.X = 0
47 }
48 if p.Y < 0 {
49 p.Y = 0
50 }
51 if p.X >= GridSize {
52 p.X = GridSize - 1
53 }
54 if p.Y >= GridSize {
55 p.Y = GridSize - 1
56 }
57 return p
58}
59
60// ActionKind is what a fighter does in a round.
61type ActionKind int
62
63const (
64 Idle ActionKind = iota
65 Move
66 Attack
67 Charge
68 Defend
69)
70
71// Action is one round's decision. Dir is used by Move and Attack.
72type Action struct {
73 Kind ActionKind
74 Dir Dir
75}
76
77// FighterState is the public state of one fighter.
78type FighterState struct {
79 P Pos
80 HP int
81 Charged bool // next Attack deals ChargeDamage, then clears
82 Defending bool // incoming damage halved this round
83}
84
85// View is what a fighter sees at the start of a round.
86type View struct {
87 Round int
88 Self FighterState
89 Enemy FighterState
90}
91
92// Fighter is a battle program. Step must be deterministic.
93type Fighter interface {
94 Name() string
95 Step(View) Action
96}
97
98// Frame is one resolved round of a fight.
99type Frame struct {
100 Round int
101 APos, BPos Pos
102 AHP, BHP int
103 AAct, BAct Action
104 ADmg, BDmg int // damage dealt to the enemy this round
105}
106
107// Recap is the full record of a fight. Winner is 0 for A, 1 for B, -1 draw.
108type Recap struct {
109 A, B string
110 Winner int
111 Rounds int
112 Frames []Frame
113}
114
115// Run resolves a whole fight and returns its recap.
116func Run(a, b Fighter) Recap {
117 sa := FighterState{P: Pos{0, GridSize / 2}, HP: MaxHP}
118 sb := FighterState{P: Pos{GridSize - 1, GridSize / 2}, HP: MaxHP}
119 r := Recap{A: a.Name(), B: b.Name(), Winner: -1}
120 for round := 1; round <= MaxRounds; round++ {
121 aa := a.Step(View{Round: round, Self: sa, Enemy: sb})
122 ab := b.Step(View{Round: round, Self: sb, Enemy: sa})
123 frame := Frame{Round: round, AAct: aa, BAct: ab}
124 sa.Defending = aa.Kind == Defend
125 sb.Defending = ab.Kind == Defend
126
127 // 1. Moves resolve in order: A first, then B. A move onto a tile
128 // the enemy currently occupies fails (the fighter stays put).
129 // Documented and deterministic: initiative always belongs to A.
130 if aa.Kind == Move {
131 p := sa.P.Delta(aa.Dir).clamp()
132 if p != sb.P {
133 sa.P = p
134 }
135 }
136 if ab.Kind == Move {
137 p := sb.P.Delta(ab.Dir).clamp()
138 if p != sa.P {
139 sb.P = p
140 }
141 }
142
143 // 2. Charges arm before attacks resolve.
144 if aa.Kind == Charge {
145 sa.Charged = true
146 }
147 if ab.Kind == Charge {
148 sb.Charged = true
149 }
150
151 // 3. Attacks. A strike hits if its target tile holds the enemy.
152 if aa.Kind == Attack && sa.P.Delta(aa.Dir) == sb.P {
153 frame.ADmg = strike(&sa, sb.Defending)
154 sb.HP -= frame.ADmg
155 }
156 if ab.Kind == Attack && sb.P.Delta(ab.Dir) == sa.P {
157 frame.BDmg = strike(&sb, sa.Defending)
158 sa.HP -= frame.BDmg
159 }
160 if sb.HP < 0 {
161 sb.HP = 0
162 }
163 if sa.HP < 0 {
164 sa.HP = 0
165 }
166
167 frame.APos, frame.BPos, frame.AHP, frame.BHP = sa.P, sb.P, sa.HP, sb.HP
168 r.Frames = append(r.Frames, frame)
169 r.Rounds = round
170
171 if sa.HP == 0 || sb.HP == 0 {
172 switch {
173 case sa.HP == 0 && sb.HP == 0:
174 r.Winner = -1 // double knockout
175 case sb.HP == 0:
176 r.Winner = 0
177 default:
178 r.Winner = 1
179 }
180 return r
181 }
182 }
183 // Time limit: higher HP wins; equal HP is a draw.
184 switch {
185 case sa.HP > sb.HP:
186 r.Winner = 0
187 case sb.HP > sa.HP:
188 r.Winner = 1
189 }
190 return r
191}
192
193// strike computes attack damage, consuming charge and applying defense.
194func strike(att *FighterState, defending bool) int {
195 dmg := MeleeDamage
196 if att.Charged {
197 dmg = ChargeDamage
198 att.Charged = false
199 }
200 if defending {
201 dmg /= 2
202 }
203 return dmg
204}