-
Notifications
You must be signed in to change notification settings - Fork 2
/
transactions.go
802 lines (762 loc) · 19.2 KB
/
transactions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
package pokemonbattlelib
//go:generate go run ./scripts/transaction_marshall/gen_transaction_marshall.go
// Transactions describes a change to battle state.
// A sequence of transactions should be able to describe an entire battle.
type Transaction interface {
Mutate(b *Battle) // Modifies the battle to apply the transaction. Can also queue additional transactions via b.QueueTransaction().
}
type UseMoveTransaction struct {
User target
Target target
Move *Move
}
func (t UseMoveTransaction) Mutate(b *Battle) {
user := b.GetPokemon(t.User)
// Struggle conditions
if t.Move.CurrentPP == 0 {
if b.ruleset&BattleRuleStruggle == 0 {
blog.Println("Struggle is disabled - Pokemon did not use any move.")
return
}
// Ensure that struggle is forced (TODO: move to FightTurn validation)
for _, m := range user.Moves {
if m != nil && m.CurrentPP > 0 {
panic(ErrorNoPP)
}
}
b.QueueTransaction(UseMoveTransaction{
User: t.User,
Target: t.Target,
Move: GetMove(MoveStruggle),
})
// 1/4 of max HP dealt as recoil damage
b.QueueTransaction(DamageTransaction{
Target: t.User,
Damage: user.MaxHP() / 4,
})
return
}
receiver := b.GetPokemon(t.Target)
accuracy := CalcAccuracy(b.Weather, user, receiver, t.Move)
b.QueueTransaction(PPTransaction{
Move: t.Move,
Amount: -1,
})
if t.Move.Accuracy() != 0 && !b.rng.Roll(int(accuracy), 100) {
b.QueueTransaction(MoveFailTransaction{
User: t.User,
Reason: FailMiss,
})
return
}
// See: https://github.com/StevensSEC/pokemonbattlelib/wiki/Requirements#fight-using-a-move
// Status Moves
if t.Move.Category() == MoveCategoryStatus {
switch t.Move.Id {
case MoveSpite:
if m := receiver.metadata[MetaLastMove]; m != nil {
b.QueueTransaction(PPTransaction{
Move: m.(*Move),
Amount: -4,
})
}
case MoveAttract:
g1, g2 := user.Gender, receiver.Gender
// Only applies when Pokemon are opposite gender
if g1 != GenderGenderless && g2 != GenderGenderless && g1 != g2 {
b.QueueTransaction(InflictStatusTransaction{
Target: t.Target,
StatusEffect: StatusInfatuation,
})
if receiver.HeldItem == ItemDestinyKnot {
b.QueueTransaction(InflictStatusTransaction{
Target: t.User,
StatusEffect: StatusInfatuation,
})
}
}
case MoveRainDance:
turns := 5
if user.HeldItem == ItemDampRock {
turns = 8
}
b.QueueTransaction(WeatherTransaction{
Weather: WeatherRain,
Turns: turns,
})
case MoveSunnyDay:
turns := 5
if user.HeldItem == ItemHeatRock {
turns = 8
}
b.QueueTransaction(WeatherTransaction{
Weather: WeatherHarshSunlight,
Turns: turns,
})
case MoveHail:
turns := 5
if user.HeldItem == ItemIcyRock {
turns = 8
}
b.QueueTransaction(WeatherTransaction{
Weather: WeatherHail,
Turns: turns,
})
case MoveSandstorm:
turns := 5
if user.HeldItem == ItemSmoothRock {
turns = 8
}
b.QueueTransaction(WeatherTransaction{
Weather: WeatherSandstorm,
Turns: turns,
})
case MoveSplash:
b.QueueTransaction(MoveFailTransaction{
User: t.User,
Reason: FailOther,
})
case MoveDefog:
if b.Weather == WeatherFog {
b.QueueTransaction(WeatherTransaction{
Weather: WeatherClearSkies,
})
}
case MoveMoonlight, MoveSynthesis, MoveMorningSun:
if b.Weather == WeatherFog {
b.QueueTransaction(HealTransaction{
Target: t.User,
Amount: user.MaxHP() / 4,
})
}
default:
if t.Move.Ailment() != StatusNone {
b.QueueTransaction(InflictStatusTransaction{
Target: t.Target,
StatusEffect: t.Move.Ailment(),
})
} else if t.Move.StatStages() != 0 {
if t.Move.Targets() == MoveTargetUser {
b.QueueTransaction(ModifyStatTransaction{
Target: t.User,
SelfInflicted: true,
Stat: int(t.Move.AffectedStat()),
Stages: int(t.Move.StatStages()),
})
} else if t.Move.Targets() == MoveTargetSelected || t.Move.Targets() == MoveTargetAllOpponents {
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: int(t.Move.AffectedStat()),
Stages: int(t.Move.StatStages()),
})
} else {
blog.Printf("Unknown target for stat modifying move: %s: %v", t.Move.Name(), t.Move.Targets())
}
} else {
blog.Printf("Unimplemented status move: %s", t.Move.Name())
}
}
} else {
// Physical/Special Moves
damage := CalcMoveDamage(b.Weather, user, receiver, t.Move)
var crit uint = 1
if b.rng.Roll(1, user.CritChance()) {
crit = 2
}
// Target effects
if receiver.HeldItem != ItemNone {
switch receiver.HeldItem {
case ItemStickyBarb:
b.QueueTransaction(DamageTransaction{
Target: t.User,
Damage: user.MaxHP() / 8,
})
if t.Move.Flags()&FlagContact != 0 && user.HeldItem == ItemNone {
b.QueueTransaction(
GiveItemTransaction{
Target: t.User,
Item: receiver.HeldItem,
},
GiveItemTransaction{
Target: t.Target,
Item: ItemNone,
},
)
}
}
}
damage *= crit
if t.Move.Id == MoveSonicBoom {
// always deals 20 damage, no matter what
damage = 20
}
b.QueueTransaction(DamageTransaction{
Target: t.Target,
Move: t.Move,
Damage: uint(damage),
})
// Handle draining moves (Absorb, Mega Drain, Giga Drain, Drain Punch, etc.)
// However, if Drain is negative, it's actually recoil damage.
if t.Move.Drain() != 0 {
drain := int(damage) * t.Move.Drain() / 100
if drain > 0 {
// These multiplers only apply to draining moves, not recoil moves
if user.HeldItem == ItemBigRoot {
drain = drain * 130 / 100 // 30% more HP than normal
}
}
if drain == 0 {
// Min 1 HP drain
drain = 1
}
if drain > 0 {
b.QueueTransaction(HealTransaction{
Target: t.User,
Amount: uint(drain),
})
} else {
// recoil damage
b.QueueTransaction(DamageTransaction{
Target: t.User,
Damage: uint(-drain),
})
}
}
if t.Move.FlinchChance() > 0 && b.rng.Roll(t.Move.FlinchChance(), 100) {
b.QueueTransaction(InflictStatusTransaction{
Target: t.Target,
StatusEffect: StatusFlinch,
})
}
if t.Move.AilmentChance() > 0 && b.rng.Roll(t.Move.AilmentChance(), 100) {
b.QueueTransaction(InflictStatusTransaction{
Target: t.Target,
StatusEffect: t.Move.Ailment(),
})
}
// Other item effects in battle
switch user.HeldItem {
case ItemKingsRock, ItemRazorFang:
// King's Rock makes non-flinching moves have a 10% to cause flinch
// TODO: ensure only certain moves are affected -> https://bulbapedia.bulbagarden.net/wiki/King%27s_Rock
if t.Move.FlinchChance() == 0 && b.rng.Roll(1, 10) {
b.QueueTransaction(InflictStatusTransaction{
Target: t.Target,
StatusEffect: StatusFlinch,
})
}
case ItemLifeOrb:
b.QueueTransaction(DamageTransaction{
Target: t.User,
Damage: user.MaxHP() / 10,
})
case ItemShellBell:
b.QueueTransaction(DamageTransaction{
Target: t.User,
Damage: uint(damage / 8),
})
}
}
user.metadata[MetaLastMove] = t.Move
}
// A transaction to deal damage to an opponent Pokemon.
type DamageTransaction struct {
Target target
Move *Move
Damage uint
StatusEffect StatusCondition
}
func (t DamageTransaction) Mutate(b *Battle) {
// Minimum 1HP attack
if t.Damage == 0 {
t.Damage = 1
}
receiver := b.GetPokemon(t.Target)
if receiver.CurrentHP >= t.Damage {
receiver.CurrentHP -= t.Damage
} else {
// prevent underflow
receiver.CurrentHP = 0
}
if receiver.CurrentHP == 0 {
// Prevent OHKO with Focus Sash
if receiver.HeldItem == ItemFocusSash {
receiver.CurrentHP = 1
b.QueueTransaction(ItemTransaction{
Target: t.Target,
IsHeld: true,
Item: receiver.HeldItem,
})
return
}
// pokemon has fainted
b.QueueTransaction(FaintTransaction{
Target: t.Target,
})
}
}
// A transaction to change the friendship level of a Pokemon.
type FriendshipTransaction struct {
Target target // The target Pokemon
Amount int // The amount of friendship to increase/decrease
}
func (t FriendshipTransaction) Mutate(b *Battle) {
pkmn := b.GetPokemon(t.Target)
pkmn.Friendship += t.Amount
}
// A transaction to change the EVs of a Pokemon.
type EVTransaction struct {
Target target
Stat int
Amount uint8
}
func (t EVTransaction) Mutate(b *Battle) {
pkmn := b.GetPokemon(t.Target)
pkmn.EVs[t.Stat] += t.Amount
}
// A transaction to use and possibly consume an item.
type ItemTransaction struct {
Target target
IsHeld bool
Item Item
Move *Move
}
func (t ItemTransaction) Mutate(b *Battle) {
receiver := b.GetPokemon(t.Target)
if t.Item.Flags()&FlagConsumable > 0 {
if t.IsHeld {
t.Item = receiver.HeldItem // auto-correct if the value is not present or does not match
receiver.metadata[MetaLastItem] = receiver.HeldItem
receiver.HeldItem = ItemNone
}
// TODO: remove consumed item from party's inventory
}
switch t.Item.Category() {
case ItemCategoryFlutes:
b.QueueTransaction(CureStatusTransaction{
Target: t.Target,
StatusEffect: battleItemFlutes[t.Item],
})
case ItemCategoryStatBoost:
if t.Item == ItemDireHit {
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: StatCritChance,
Stages: +2,
})
} else if t.Item == ItemGuardSpec {
receiver.metadata[MetaStatChangeImmune] = 5
} else {
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: battleItemStats[t.Item],
Stages: +1,
})
}
b.QueueTransaction(FriendshipTransaction{
Target: t.Target,
Amount: [3]int{1, 1, 0}[receiver.Friendship/100],
})
case ItemCategoryHealing, ItemCategoryRevival, ItemCategoryStatusCures:
if t.Item.Category() == ItemCategoryRevival && receiver.CurrentHP != 0 {
return
}
fIndex := receiver.Friendship / 100
data, ok := medicineData[t.Item]
if ok {
if data.Heal > 0 {
b.QueueTransaction(HealTransaction{
Target: t.Target,
Amount: data.Heal,
})
}
if amount := data.Friendship[fIndex]; amount != 0 {
b.QueueTransaction(FriendshipTransaction{
Target: t.Target,
Amount: amount,
})
}
if data.Cure != StatusNone {
b.QueueTransaction(CureStatusTransaction{
Target: t.Target,
StatusEffect: data.Cure,
})
}
}
// Other medicine item effects
switch t.Item {
case ItemSacredAsh:
for slot := range b.parties[t.Target.party].pokemon() {
pkmnTarget := target{t.Target.party, uint(slot)}
pkmn := b.GetPokemon(pkmnTarget)
if pkmn.CurrentHP == 0 {
b.QueueTransaction(HealTransaction{
Target: pkmnTarget,
Amount: pkmn.MaxHP(),
})
}
}
case ItemFullRestore, ItemMaxPotion, ItemMaxRevive, ItemRevivalHerb:
b.QueueTransaction(HealTransaction{
Target: t.Target,
Amount: receiver.MaxHP(),
})
case ItemRevive:
b.QueueTransaction(HealTransaction{
Target: t.Target,
Amount: receiver.MaxHP() / 2,
})
}
}
switch t.Item {
// ItemCategoryPPRecovery
case ItemElixir:
for _, m := range receiver.Moves {
if m == nil {
continue
}
b.QueueTransaction(PPTransaction{
Move: m,
Amount: 10,
})
}
case ItemEther:
b.QueueTransaction(PPTransaction{
Move: t.Move,
Amount: 10,
})
case ItemMaxElixir:
for _, m := range receiver.Moves {
if m == nil {
continue
}
b.QueueTransaction(PPTransaction{
Move: m,
Amount: int8(m.MaxPP),
})
}
case ItemMaxEther:
b.QueueTransaction(PPTransaction{
Move: t.Move,
Amount: int8(t.Move.MaxPP),
})
// ItemCategoryInAPinch
case ItemApicotBerry:
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: StatSpDef,
Stages: 1,
})
case ItemCustapBerry:
// TODO: Force pokemon to go first
case ItemGanlonBerry:
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: StatDef,
Stages: 1,
})
case ItemLansatBerry:
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: StatCritChance,
Stages: 2,
})
case ItemLiechiBerry:
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: StatAtk,
Stages: 1,
})
case ItemMicleBerry:
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: StatAccuracy,
Stages: 1,
})
case ItemPetayaBerry:
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: StatSpAtk,
Stages: 1,
})
case ItemSalacBerry:
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: StatSpeed,
Stages: 1,
})
case ItemStarfBerry:
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: b.rng.Get(StatAtk, StatSpeed),
Stages: 2,
})
case ItemBlackSludge:
if receiver.Type&TypePoison != 0 {
b.QueueTransaction(HealTransaction{
Target: t.Target,
Amount: receiver.MaxHP() / 16,
})
} else {
b.QueueTransaction(DamageTransaction{
Target: t.Target,
Damage: receiver.MaxHP() / 8,
})
}
case ItemLeftovers:
b.QueueTransaction(HealTransaction{
Target: t.Target,
Amount: receiver.MaxHP() / 16,
})
case ItemMentalHerb:
b.QueueTransaction(CureStatusTransaction{
Target: t.Target,
StatusEffect: StatusInfatuation,
})
case ItemWhiteHerb:
for stat, stages := range receiver.StatModifiers {
if stages < 0 {
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: stat,
Stages: -stages,
})
}
}
// ItemCategoryBadHeldItems
case ItemFlameOrb:
b.QueueTransaction(InflictStatusTransaction{
Target: t.Target,
StatusEffect: StatusBurn,
})
case ItemStickyBarb:
b.QueueTransaction(DamageTransaction{
Target: t.Target,
Damage: receiver.MaxHP() / 8,
})
case ItemToxicOrb:
b.QueueTransaction(InflictStatusTransaction{
Target: t.Target,
StatusEffect: StatusBadlyPoison,
})
}
// In a pinch consumption
if receiver.HeldItem.Category() == ItemCategoryInAPinch && receiver.CurrentHP <= receiver.MaxHP()/4 {
b.QueueTransaction(ItemTransaction{
Target: t.Target,
IsHeld: true,
Item: receiver.HeldItem,
})
}
}
// A transaction to change the PP of a move.
type PPTransaction struct {
Move *Move
Amount int8
}
func (t PPTransaction) Mutate(b *Battle) {
t.Move.CurrentPP += uint8(t.Amount)
if t.Move.CurrentPP >= t.Move.MaxPP {
if t.Amount > 0 {
t.Move.CurrentPP = t.Move.MaxPP
} else {
t.Move.CurrentPP = 0
}
}
}
// A transaction to change the held item of a Pokemon
type GiveItemTransaction struct {
Target target
Item Item
}
func (t GiveItemTransaction) Mutate(b *Battle) {
pkmn := b.GetPokemon(t.Target)
pkmn.HeldItem = t.Item
}
// A transaction to restore HP to a Pokemon.
type HealTransaction struct {
Target target
Amount uint
}
func (t HealTransaction) Mutate(b *Battle) {
pkmn := b.GetPokemon(t.Target)
pkmn.CurrentHP += t.Amount
if pkmn.CurrentHP > pkmn.MaxHP() {
pkmn.CurrentHP = pkmn.MaxHP()
}
}
// A transaction to apply a status effect to a Pokemon.
type InflictStatusTransaction struct {
Target target
StatusEffect StatusCondition
}
func (t InflictStatusTransaction) Mutate(b *Battle) {
pkmn := b.GetPokemon(t.Target)
pkmn.StatusEffects.apply(t.StatusEffect)
if t.StatusEffect.check(StatusSleep) {
pkmn.metadata[MetaSleepTime] = b.rng.Get(1, 5)
}
if pkmn.Ability == AbilitySteadfast && t.StatusEffect.check(StatusFlinch) {
b.QueueTransaction(ModifyStatTransaction{
Target: t.Target,
Stat: StatSpeed,
Stages: 1,
})
}
}
type CureStatusTransaction struct {
Target target
StatusEffect StatusCondition
}
func (t CureStatusTransaction) Mutate(b *Battle) {
pkmn := b.GetPokemon(t.Target)
pkmn.StatusEffects.clear(t.StatusEffect)
if t.StatusEffect.check(StatusSleep) {
delete(pkmn.metadata, MetaSleepTime)
}
}
// A transaction that makes a pokemon faint, and returns the pokemon to the pokeball.
type FaintTransaction struct {
Target target
}
func (t FaintTransaction) Mutate(b *Battle) {
pkmn := b.GetPokemon(t.Target)
if b.ruleset&BattleRuleFaint == 0 {
blog.Println("Fainting is disabled - Pokemon HP fully restored")
pkmn.CurrentHP = pkmn.MaxHP()
return
}
// EVs are gained based on EV yield of defeated Pokemon
evGain := pkmn.GetEVYield()
for _, opponent := range b.getOpponents(b.parties[t.Target.party]) {
// Friendship is lowered based on level difference
opponentPkmn := b.GetPokemon(opponent)
levelGap := opponentPkmn.Level - pkmn.Level
loss := -1
if levelGap >= 30 {
if pkmn.Friendship < 200 {
loss = -5
} else {
loss = -10
}
}
b.QueueTransaction(FriendshipTransaction{
Target: t.Target,
Amount: loss,
})
for stat, amount := range evGain {
if amount == 0 {
continue
}
b.QueueTransaction(EVTransaction{
Target: opponent,
Stat: stat,
Amount: uint8(amount),
})
}
}
p := b.parties[t.Target.party]
p.SetInactive(t.Target.slot)
anyAlive := false
for slot, pkmn := range p.pokemon() {
if pkmn.CurrentHP > 0 {
anyAlive = true
// TODO: prompt Agent for which pokemon to send out next
// auto send out next pokemon
b.QueueTransaction(SendOutTransaction{
Target: target{t.Target.party, uint(slot)},
})
break
}
}
if !anyAlive {
// cause the battle to end by knockout
b.QueueTransaction(EndBattleTransaction{
Reason: EndKnockout,
Winner: (p.team + 1) % 2, // HACK: because there is always 2 teams in a battle
})
}
}
// A transaction that makes a party send out a pokemon.
type SendOutTransaction struct {
Target target
}
func (t SendOutTransaction) Mutate(b *Battle) {
p := b.parties[t.Target.party]
p.SetActive(t.Target.slot)
}
// Changes the current weather in a battle
type WeatherTransaction struct {
Weather Weather
Turns int
}
func (t WeatherTransaction) Mutate(b *Battle) {
b.Weather = t.Weather
b.metadata[MetaWeatherTurns] = t.Turns
}
// A transaction that ends the battle.
type EndReason int
const (
EndKnockout EndReason = iota
EndForfeit
EndFlee
)
type EndBattleTransaction struct {
Reason EndReason
Winner int
}
func (t EndBattleTransaction) Mutate(b *Battle) {
b.State = BattleEnd
b.results.Winner = t.Winner
for _, p := range b.parties {
b.results.Parties = append(b.results.Parties, p.Party)
}
}
// Handles pre-turn status checks. (Paralysis, Sleeping, etc.)
type ImmobilizeTransaction struct {
Target target
StatusEffect StatusCondition
}
func (t ImmobilizeTransaction) Mutate(b *Battle) {
receiver := b.GetPokemon(t.Target)
if t.StatusEffect.check(StatusSleep) {
receiver.metadata[MetaSleepTime] = receiver.metadata[MetaSleepTime].(int) - 1
}
}
// Handles evasion, misses, dodging, etc. when using moves
type MoveFailTransaction struct {
User target
Reason MoveFailReason
}
func (t MoveFailTransaction) Mutate(b *Battle) {
// currently a no-op.
}
// Modifies a stat's stages in the interval [-6, 6]
type ModifyStatTransaction struct {
Target target
SelfInflicted bool
Stat int
Stages int
}
func (t ModifyStatTransaction) Mutate(b *Battle) {
pkmn := b.GetPokemon(t.Target)
_, immune := pkmn.metadata[MetaStatChangeImmune]
if immune && t.Stages < 0 && !t.SelfInflicted {
return
}
stage := pkmn.StatModifiers[t.Stat] + t.Stages
min := MinStatModifier
max := MaxStatModifier
// Bounds for crit chance are [0, 4]
if t.Stat == StatCritChance {
min = 0
max = len(CritChances) - 1
}
if stage < min {
stage = min
}
if stage > max {
stage = max
}
pkmn.StatModifiers[t.Stat] = stage
}