@pkmn/engine 0.1.0-dev.6ef582f1 → 0.1.0-dev.736f7a8b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -3
- package/build.zig +31 -15
- package/package.json +11 -11
- package/src/bin/install-pkmn-engine +1 -1
- package/src/lib/common/protocol.zig +79 -25
- package/src/lib/gen1/README.md +83 -3
- package/src/lib/gen1/calc.zig +0 -1
- package/src/lib/gen1/chance.zig +3 -3
- package/src/lib/gen1/helpers.zig +228 -75
- package/src/lib/gen1/mechanics.zig +7 -0
- package/src/lib/gen2/README.md +75 -0
- package/src/lib/gen2/data/moves.zig +15 -2
- package/src/lib/gen2/data.zig +30 -15
- package/src/lib/gen2/helpers.zig +8 -7
- package/src/lib/gen2/mechanics.zig +348 -8
package/src/lib/gen2/data.zig
CHANGED
|
@@ -5,6 +5,7 @@ const pkmn = @import("../pkmn.zig");
|
|
|
5
5
|
|
|
6
6
|
const data = @import("../common/data.zig");
|
|
7
7
|
const DEBUG = @import("../common/debug.zig").print;
|
|
8
|
+
const protocol = @import("../common/protocol.zig");
|
|
8
9
|
const rng = @import("../common/rng.zig");
|
|
9
10
|
const util = @import("../common/util.zig");
|
|
10
11
|
|
|
@@ -15,6 +16,8 @@ const types = @import("data/types.zig");
|
|
|
15
16
|
|
|
16
17
|
const gen1 = @import("../gen1/data.zig");
|
|
17
18
|
|
|
19
|
+
const calc = @import("calc.zig");
|
|
20
|
+
const chance = @import("chance.zig");
|
|
18
21
|
const mechanics = @import("mechanics.zig");
|
|
19
22
|
|
|
20
23
|
const assert = std.debug.assert;
|
|
@@ -33,6 +36,9 @@ pub const LOGS_SIZE = if (builtin.mode == .ReleaseSmall)
|
|
|
33
36
|
else
|
|
34
37
|
std.math.ceilPowerOfTwo(usize, MAX_LOGS) catch unreachable;
|
|
35
38
|
|
|
39
|
+
/// Null object pattern implementation of pkmn.battle.Options for Generation II.
|
|
40
|
+
pub const NULL = pkmn.battle.options(protocol.NULL, chance.NULL, calc.NULL);
|
|
41
|
+
|
|
36
42
|
const Choice = data.Choice;
|
|
37
43
|
const ID = data.ID;
|
|
38
44
|
const Player = data.Player;
|
|
@@ -82,7 +88,7 @@ test Battle {
|
|
|
82
88
|
try expectEqual(496, @sizeOf(Battle(PRNG)));
|
|
83
89
|
}
|
|
84
90
|
|
|
85
|
-
const Field = packed struct {
|
|
91
|
+
pub const Field = packed struct {
|
|
86
92
|
weather: Weather = .None,
|
|
87
93
|
weather_duration: u4 = 0,
|
|
88
94
|
|
|
@@ -91,19 +97,21 @@ const Field = packed struct {
|
|
|
91
97
|
}
|
|
92
98
|
};
|
|
93
99
|
|
|
94
|
-
const Weather = enum(u4) {
|
|
100
|
+
pub const Weather = enum(u4) {
|
|
95
101
|
None,
|
|
96
102
|
Rain,
|
|
97
103
|
Sun,
|
|
98
104
|
Sandstorm,
|
|
99
105
|
};
|
|
100
106
|
|
|
101
|
-
const Side = extern struct {
|
|
107
|
+
pub const Side = extern struct {
|
|
102
108
|
pokemon: [6]Pokemon = [_]Pokemon{.{}} ** 6,
|
|
103
109
|
active: ActivePokemon = .{},
|
|
104
|
-
conditions: Conditions,
|
|
110
|
+
conditions: Conditions = .{},
|
|
111
|
+
// NB: wCurPlayerMove / wCurEnemyMove
|
|
112
|
+
last_selected_move: Move = .None,
|
|
113
|
+
// NB: wLastPlayerCounterMove / wLastEnemyCounterMove (wLastPlayerMove / wLastEnemyMove)
|
|
105
114
|
last_used_move: Move = .None,
|
|
106
|
-
_: u8 = 0,
|
|
107
115
|
|
|
108
116
|
const Conditions = packed struct {
|
|
109
117
|
Spikes: bool = false,
|
|
@@ -136,18 +144,23 @@ const Side = extern struct {
|
|
|
136
144
|
assert(@typeInfo(@TypeOf(self)).Pointer.child == Side);
|
|
137
145
|
return self.get(1);
|
|
138
146
|
}
|
|
147
|
+
|
|
148
|
+
pub inline fn lastMove(self: anytype, encore: bool) Move {
|
|
149
|
+
assert(self.last_used_move != .None or !self.active.volatiles.dirty);
|
|
150
|
+
if (encore) return self.last_used_move;
|
|
151
|
+
return if (self.active.volatiles.dirty) .None else self.last_used_move;
|
|
152
|
+
}
|
|
139
153
|
};
|
|
140
154
|
|
|
141
155
|
// NOTE: DVs (Gender & Hidden Power) and Happiness are stored only in Pokemon
|
|
142
|
-
const ActivePokemon = extern struct {
|
|
143
|
-
volatiles:
|
|
156
|
+
pub const ActivePokemon = extern struct {
|
|
157
|
+
volatiles: Volatiles align(4) = .{},
|
|
144
158
|
stats: Stats(u16) = .{},
|
|
145
159
|
moves: [4]MoveSlot = [_]MoveSlot{.{}} ** 4,
|
|
146
160
|
boosts: Boosts = .{},
|
|
161
|
+
types: Types = .{},
|
|
147
162
|
species: Species = .None,
|
|
148
163
|
item: Item = .None,
|
|
149
|
-
last_move: Move = .None,
|
|
150
|
-
_: u8 = 0,
|
|
151
164
|
|
|
152
165
|
comptime {
|
|
153
166
|
assert(@sizeOf(ActivePokemon) == 44);
|
|
@@ -161,7 +174,7 @@ const ActivePokemon = extern struct {
|
|
|
161
174
|
}
|
|
162
175
|
};
|
|
163
176
|
|
|
164
|
-
const Pokemon = extern struct {
|
|
177
|
+
pub const Pokemon = extern struct {
|
|
165
178
|
stats: Stats(u16) = .{},
|
|
166
179
|
moves: [4]MoveSlot = [_]MoveSlot{.{}} ** 4,
|
|
167
180
|
types: Types = .{},
|
|
@@ -192,7 +205,7 @@ pub const Gender = enum(u2) {
|
|
|
192
205
|
Unknown,
|
|
193
206
|
};
|
|
194
207
|
|
|
195
|
-
const DVs = packed struct {
|
|
208
|
+
pub const DVs = packed struct {
|
|
196
209
|
gender: Gender = .Male,
|
|
197
210
|
pow: u6 = 40,
|
|
198
211
|
type: Type = .Dark,
|
|
@@ -248,7 +261,7 @@ test DVs {
|
|
|
248
261
|
try expectEqual(Gender.Female, dvs.gender);
|
|
249
262
|
}
|
|
250
263
|
|
|
251
|
-
const MoveSlot = packed struct {
|
|
264
|
+
pub const MoveSlot = packed struct {
|
|
252
265
|
id: Move = .None,
|
|
253
266
|
pp_ups: u2 = 3,
|
|
254
267
|
pp: u6 = 0,
|
|
@@ -260,7 +273,7 @@ const MoveSlot = packed struct {
|
|
|
260
273
|
|
|
261
274
|
pub const Status = gen1.Status;
|
|
262
275
|
|
|
263
|
-
const
|
|
276
|
+
pub const Volatiles = packed struct {
|
|
264
277
|
Bide: bool = false,
|
|
265
278
|
Thrashing: bool = false,
|
|
266
279
|
Flinch: bool = false,
|
|
@@ -294,8 +307,9 @@ const Volatile = packed struct {
|
|
|
294
307
|
trapped: bool = false,
|
|
295
308
|
switched: bool = false,
|
|
296
309
|
switching: bool = false,
|
|
310
|
+
dirty: bool = false,
|
|
297
311
|
|
|
298
|
-
_:
|
|
312
|
+
_: u4 = 0,
|
|
299
313
|
|
|
300
314
|
bind: u4 = 0,
|
|
301
315
|
future_sight: FutureSight = .{},
|
|
@@ -322,7 +336,7 @@ const Volatile = packed struct {
|
|
|
322
336
|
};
|
|
323
337
|
|
|
324
338
|
comptime {
|
|
325
|
-
assert(@sizeOf(
|
|
339
|
+
assert(@sizeOf(Volatiles) == 16);
|
|
326
340
|
}
|
|
327
341
|
};
|
|
328
342
|
|
|
@@ -406,6 +420,7 @@ test Move {
|
|
|
406
420
|
try expectEqual(Move.Effect.ConfusionChance, move.effect);
|
|
407
421
|
try expectEqual(@as(u8, 50), move.accuracy);
|
|
408
422
|
try expectEqual(@as(u8, 5), move.pp);
|
|
423
|
+
try expectEqual(@as(i8, -1), Move.get(.Counter).priority);
|
|
409
424
|
}
|
|
410
425
|
|
|
411
426
|
pub const Species = species.Species;
|
package/src/lib/gen2/helpers.zig
CHANGED
|
@@ -48,7 +48,7 @@ pub const Battle = struct {
|
|
|
48
48
|
comptime rolls: anytype,
|
|
49
49
|
p1: []const Pokemon,
|
|
50
50
|
p2: []const Pokemon,
|
|
51
|
-
) data.Battle(rng.FixedRNG(
|
|
51
|
+
) data.Battle(rng.FixedRNG(2, rolls.len)) {
|
|
52
52
|
return .{
|
|
53
53
|
.rng = .{ .rolls = rolls },
|
|
54
54
|
.sides = .{ Side.init(p1), Side.init(p2) },
|
|
@@ -87,7 +87,7 @@ pub const Side = struct {
|
|
|
87
87
|
|
|
88
88
|
for (0..ps.len) |i| {
|
|
89
89
|
side.pokemon[i] = Pokemon.init(ps[i]);
|
|
90
|
-
|
|
90
|
+
side.pokemon[i].position = @as(u4, @intCast(i)) + 1;
|
|
91
91
|
}
|
|
92
92
|
return side;
|
|
93
93
|
}
|
|
@@ -98,7 +98,7 @@ pub const Side = struct {
|
|
|
98
98
|
|
|
99
99
|
for (0..n) |i| {
|
|
100
100
|
side.pokemon[i] = Pokemon.random(rand, opt);
|
|
101
|
-
side.pokemon[i].position = i + 1;
|
|
101
|
+
side.pokemon[i].position = @as(u4, @intCast(i)) + 1;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
return side;
|
|
@@ -139,7 +139,7 @@ pub const Pokemon = struct {
|
|
|
139
139
|
.id = m,
|
|
140
140
|
.pp_ups = 3,
|
|
141
141
|
// NB: PP can be at most 61 legally (though can overflow to 63)
|
|
142
|
-
.pp = @intCast(@min(Move.
|
|
142
|
+
.pp = @intCast(@min(Move.get(m).pp / 5 * 8, 61)),
|
|
143
143
|
};
|
|
144
144
|
}
|
|
145
145
|
pokemon.item = p.item;
|
|
@@ -151,7 +151,7 @@ pub const Pokemon = struct {
|
|
|
151
151
|
pokemon.status = p.status;
|
|
152
152
|
pokemon.types = species.types;
|
|
153
153
|
pokemon.level = p.level;
|
|
154
|
-
pokemon.dvs = DVs.from(p.dvs);
|
|
154
|
+
pokemon.dvs = DVs.from(p.species, p.dvs);
|
|
155
155
|
return pokemon;
|
|
156
156
|
}
|
|
157
157
|
|
|
@@ -189,7 +189,8 @@ pub const Pokemon = struct {
|
|
|
189
189
|
const pp_ups: u8 =
|
|
190
190
|
if (!opt.cleric and rand.chance(u8, 1, 10)) rand.range(u2, 0, 2 + 1) else 3;
|
|
191
191
|
// NB: PP can be at most 61 legally (though can overflow to 63)
|
|
192
|
-
|
|
192
|
+
var base = Move.get(m).pp;
|
|
193
|
+
const max_pp: u8 = @intCast(base + pp_ups * @min(base / 5, 7));
|
|
193
194
|
ms[i] = .{
|
|
194
195
|
.id = m,
|
|
195
196
|
.pp_ups = pp_ups,
|
|
@@ -199,7 +200,7 @@ pub const Pokemon = struct {
|
|
|
199
200
|
return .{
|
|
200
201
|
.species = s,
|
|
201
202
|
.types = species.types,
|
|
202
|
-
.dvs = DVs.from(dvs),
|
|
203
|
+
.dvs = DVs.from(s, dvs),
|
|
203
204
|
.level = lvl,
|
|
204
205
|
.stats = stats,
|
|
205
206
|
.hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
|
|
@@ -36,19 +36,359 @@ const Stats = data.Stats;
|
|
|
36
36
|
const Status = data.Status;
|
|
37
37
|
|
|
38
38
|
pub fn update(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Result {
|
|
39
|
+
assert(c1.type != .Pass or c2.type != .Pass or battle.turn == 0);
|
|
40
|
+
if (battle.turn == 0) return start(battle, options);
|
|
41
|
+
|
|
42
|
+
if (try turnOrder(battle, c1, c2, options) == .P1) {
|
|
43
|
+
if (try doTurn(battle, .P1, c1, .P2, c2, options)) |r| return r;
|
|
44
|
+
} else {
|
|
45
|
+
if (try doTurn(battle, .P2, c2, .P1, c1, options)) |r| return r;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return endTurn(battle, options);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
fn start(battle: anytype, options: anytype) !Result {
|
|
52
|
+
const p1 = battle.side(.P1);
|
|
53
|
+
const p2 = battle.side(.P2);
|
|
54
|
+
|
|
55
|
+
var p1_slot = findFirstAlive(p1);
|
|
56
|
+
assert(!showdown or p1_slot == 1);
|
|
57
|
+
if (p1_slot == 0) return if (findFirstAlive(p2) == 0) Result.Tie else Result.Lose;
|
|
58
|
+
|
|
59
|
+
var p2_slot = findFirstAlive(p2);
|
|
60
|
+
assert(!showdown or p2_slot == 1);
|
|
61
|
+
if (p2_slot == 0) return Result.Win;
|
|
62
|
+
|
|
63
|
+
try switchIn(battle, .P1, p1_slot, true, options);
|
|
64
|
+
try switchIn(battle, .P2, p2_slot, true, options);
|
|
65
|
+
|
|
66
|
+
return endTurn(battle, options);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
fn findFirstAlive(side: *const Side) u8 {
|
|
70
|
+
for (side.pokemon, 0..) |pokemon, i| if (pokemon.hp > 0) return side.pokemon[i].position;
|
|
71
|
+
return 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
fn selectMove(battle: anytype) ?Result {
|
|
39
75
|
_ = battle;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
fn saveMove(battle: anytype) void {
|
|
80
|
+
_ = battle;
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fn switchIn(battle: anytype, player: Player, slot: u8, initial: bool, options: anytype) !void {
|
|
85
|
+
var side = battle.side(player);
|
|
86
|
+
var foe = battle.foe(player);
|
|
87
|
+
|
|
88
|
+
var active = &side.active;
|
|
89
|
+
const incoming = side.get(slot);
|
|
90
|
+
|
|
91
|
+
assert(incoming.hp != 0);
|
|
92
|
+
assert(slot != 1 or initial);
|
|
93
|
+
|
|
94
|
+
const out = side.pokemon[0].position;
|
|
95
|
+
side.pokemon[0].position = side.pokemon[slot - 1].position;
|
|
96
|
+
side.pokemon[slot - 1].position = out;
|
|
97
|
+
|
|
98
|
+
side.last_used_move = .None;
|
|
99
|
+
foe.active.volatiles.dirty = true;
|
|
100
|
+
|
|
101
|
+
active.stats = incoming.stats;
|
|
102
|
+
active.species = incoming.species;
|
|
103
|
+
active.types = incoming.types;
|
|
104
|
+
active.boosts = .{};
|
|
105
|
+
active.volatiles = .{};
|
|
106
|
+
active.moves = incoming.moves;
|
|
107
|
+
|
|
108
|
+
statusModify(incoming.status, &active.stats);
|
|
109
|
+
|
|
110
|
+
try options.log.switched(battle.active(player), incoming);
|
|
111
|
+
// TODO: options.chance.switched(player, side.order[0], out);
|
|
112
|
+
|
|
113
|
+
if (side.conditions.Spikes and !active.types.includes(.Flying)) {
|
|
114
|
+
incoming.hp -|= @max(incoming.stats.hp / 8, 1);
|
|
115
|
+
try options.log.damage(battle.active(player), incoming, .Spikes);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
fn turnOrder(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Player {
|
|
120
|
+
assert(c1.type != .Pass or c2.type != .Pass);
|
|
121
|
+
|
|
122
|
+
if (c1.type == .Pass) return .P2;
|
|
123
|
+
if (c2.type == .Pass) return .P1;
|
|
124
|
+
|
|
125
|
+
// Pokémon Showdown always rolls for Quick Claw every turn, even if no Pokémon hold Quick Claw
|
|
126
|
+
const qkc = showdown and try Rolls.quickClaw(battle, .P1, options);
|
|
127
|
+
|
|
128
|
+
if ((c1.type == .Switch) != (c2.type == .Switch)) return if (c1.type == .Switch) .P1 else .P2;
|
|
129
|
+
|
|
130
|
+
// https://www.smogon.com/forums/threads/adv-switch-priority.3622189/
|
|
131
|
+
// > In Gen 1 it's irrelevant [which player switches first] because switches happen instantly on
|
|
132
|
+
// > your own screen without waiting for the other player's choice (and their choice will appear
|
|
133
|
+
// > to happen first for them too, unless they attacked in which case your switch happens first)
|
|
134
|
+
// A cartridge-compatible implemention must not advance the RNG so we simply default to P1
|
|
135
|
+
const double_switch = c1.type == .Switch and c2.type == .Switch;
|
|
136
|
+
if (!showdown and double_switch) return .P1;
|
|
137
|
+
|
|
138
|
+
const m1 = battle.side(.P1).last_selected_move;
|
|
139
|
+
const m2 = battle.side(.P2).last_selected_move;
|
|
140
|
+
if (!showdown or !double_switch) {
|
|
141
|
+
const pri1 = Move.get(m1).priority;
|
|
142
|
+
const pri2 = Move.get(m2).priority;
|
|
143
|
+
if (pri1 != pri2) return if (pri1 > pri2) .P1 else .P2;
|
|
144
|
+
|
|
145
|
+
const qkc1 = battle.side(.P1).active.item == .QuickClaw;
|
|
146
|
+
const qkc2 = battle.side(.P2).active.item == .QuickClaw;
|
|
147
|
+
if (!showdown and qkc1 and qkc2) {
|
|
148
|
+
// If both Pokémon have Quick Claw the cartridge checks them in order and exits early if
|
|
149
|
+
// the first one procs, meaning the host has a slightly higher chance of going first
|
|
150
|
+
if (try Rolls.quickClaw(battle, .P1, options)) return .P1;
|
|
151
|
+
if (try Rolls.quickClaw(battle, .P2, options)) return .P2;
|
|
152
|
+
} else if (qkc1) {
|
|
153
|
+
const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P1, options);
|
|
154
|
+
if (proc) return .P1;
|
|
155
|
+
} else if (qkc2) {
|
|
156
|
+
const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P2, options);
|
|
157
|
+
if (proc) return .P2;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const spe1 = battle.side(.P1).active.stats.spe;
|
|
162
|
+
const spe2 = battle.side(.P2).active.stats.spe;
|
|
163
|
+
if (spe1 == spe2) {
|
|
164
|
+
// Pokémon Showdown's beforeTurnCallback shenanigans
|
|
165
|
+
if (showdown and !double_switch and hasCallback(m1) and hasCallback(m2)) {
|
|
166
|
+
battle.rng.advance(1);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const p1 = try Rolls.speedTie(battle, options);
|
|
170
|
+
if (!showdown) return if (p1) .P1 else .P2;
|
|
171
|
+
|
|
172
|
+
// Pokémon Showdown's "lockedmove" volatile's onBeforeTurn uses BattleQueue#changeAction,
|
|
173
|
+
// meaning that if a side is locked into a thrashing move and wins the speed tie, it
|
|
174
|
+
// actually uses its priority to simply insert its actual changed action into the queue,
|
|
175
|
+
// causing it to then execute *after* the side which should go second...
|
|
176
|
+
const t1 = battle.side(.P1).active.volatiles.Thrashing;
|
|
177
|
+
const t2 = battle.side(.P2).active.volatiles.Thrashing;
|
|
178
|
+
// If *both* sides are thrashing it really should be another speed tie, but we've patched
|
|
179
|
+
// that out and enforce host ordering of events, so P1 just goes first regardless of who
|
|
180
|
+
// won the original coin flip
|
|
181
|
+
if (t1 and t2) return .P1;
|
|
182
|
+
return if (p1) if (t1 and !t2) .P2 else .P1 else if (t2 and !t1) .P1 else .P2;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return if (spe1 > spe2) .P1 else .P2;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
fn hasCallback(m: Move) bool {
|
|
189
|
+
return switch (m) {
|
|
190
|
+
.Counter, .MirrorCoat, .Pursuit => true,
|
|
191
|
+
else => false,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
fn doTurn(
|
|
196
|
+
battle: anytype,
|
|
197
|
+
player: Player,
|
|
198
|
+
player_choice: Choice,
|
|
199
|
+
foe_player: Player,
|
|
200
|
+
foe_choice: Choice,
|
|
201
|
+
options: anytype,
|
|
202
|
+
) !?Result {
|
|
203
|
+
if (try executeMove(battle, player, player_choice, options)) |r| return r;
|
|
204
|
+
if (try executeMove(battle, foe_player, foe_choice, options)) |r| return r;
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
fn executeMove(battle: anytype, player: Player, choice: Choice, options: anytype) !?Result {
|
|
209
|
+
if (choice.type == .Switch) {
|
|
210
|
+
try switchIn(battle, player, choice.data, false, options);
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
@panic("TODO");
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
fn endTurn(battle: anytype, options: anytype) @TypeOf(options.log).Error!Result {
|
|
218
|
+
battle.turn += 1;
|
|
219
|
+
|
|
220
|
+
if (showdown and battle.turn >= 1000) {
|
|
221
|
+
try options.log.tie();
|
|
222
|
+
return Result.Tie;
|
|
223
|
+
} else if (battle.turn >= 65535) {
|
|
224
|
+
return Result.Error;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
try options.log.turn(battle.turn);
|
|
43
228
|
|
|
44
229
|
return Result.Default;
|
|
45
230
|
}
|
|
46
231
|
|
|
232
|
+
fn statusModify(status: u8, stats: *Stats(u16)) void {
|
|
233
|
+
if (Status.is(status, .PAR)) {
|
|
234
|
+
stats.spe = @max(stats.spe / 4, 1);
|
|
235
|
+
} else if (Status.is(status, .BRN)) {
|
|
236
|
+
stats.atk = @max(stats.atk / 2, 1);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
inline fn isForced(active: anytype) bool {
|
|
241
|
+
return active.volatiles.Recharging or active.volatiles.Rage or
|
|
242
|
+
active.volatiles.Thrashing or active.volatiles.Charging or
|
|
243
|
+
active.volatiles.Underground or active.volatiles.Flying or
|
|
244
|
+
active.volatiles.Rollout;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
pub const Rolls = struct {
|
|
248
|
+
inline fn speedTie(battle: anytype, options: anytype) !bool {
|
|
249
|
+
const p1 = if (options.calc.overridden(.P1, .speed_tie)) |player|
|
|
250
|
+
player == .P1
|
|
251
|
+
else if (showdown)
|
|
252
|
+
battle.rng.range(u8, 0, 2) == 0
|
|
253
|
+
else
|
|
254
|
+
battle.rng.next() < Gen12.percent(50) + 1;
|
|
255
|
+
|
|
256
|
+
try options.chance.speedTie(p1);
|
|
257
|
+
return p1;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
inline fn quickClaw(battle: anytype, player: Player, options: anytype) !bool {
|
|
261
|
+
const qkc = if (options.calc.overridden(player, .quick_claw)) |val|
|
|
262
|
+
val == .true
|
|
263
|
+
else if (showdown)
|
|
264
|
+
battle.rng.chance(u8, 60, 256)
|
|
265
|
+
else
|
|
266
|
+
battle.rng.next() < 60;
|
|
267
|
+
|
|
268
|
+
try options.chance.quickClaw(player, qkc);
|
|
269
|
+
return qkc;
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
test "RNG agreement" {
|
|
274
|
+
if (!showdown) return;
|
|
275
|
+
var expected: [256]u32 = undefined;
|
|
276
|
+
for (0..expected.len) |i| {
|
|
277
|
+
expected[i] = @intCast(i * 0x1000000);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
var spe = rng.FixedRNG(2, expected.len){ .rolls = expected };
|
|
281
|
+
var qkc = rng.FixedRNG(2, expected.len){ .rolls = expected };
|
|
282
|
+
|
|
283
|
+
for (0..expected.len) |i| {
|
|
284
|
+
try expectEqual(spe.range(u8, 0, 2) == 0, i < Gen12.percent(50) + 1);
|
|
285
|
+
try expectEqual(qkc.chance(u8, 60, 256), i < 60);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
47
289
|
pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Choice) u8 {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
290
|
+
var n: u8 = 0;
|
|
291
|
+
switch (request) {
|
|
292
|
+
.Pass => {
|
|
293
|
+
out[n] = .{};
|
|
294
|
+
n += 1;
|
|
295
|
+
},
|
|
296
|
+
.Switch => {
|
|
297
|
+
const side = battle.side(player);
|
|
298
|
+
var slot: u4 = 2;
|
|
299
|
+
while (slot <= 6) : (slot += 1) {
|
|
300
|
+
const id = side.order[slot - 1];
|
|
301
|
+
if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
|
|
302
|
+
out[n] = .{ .type = .Switch, .data = slot };
|
|
303
|
+
n += 1;
|
|
304
|
+
}
|
|
305
|
+
if (n == 0) {
|
|
306
|
+
out[n] = .{};
|
|
307
|
+
n += 1;
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
.Move => {
|
|
311
|
+
const side = battle.side(player);
|
|
52
312
|
|
|
53
|
-
|
|
313
|
+
var active = &side.active;
|
|
314
|
+
const stored = side.stored();
|
|
315
|
+
|
|
316
|
+
// While players are not given any input options on the cartridge in these cases,
|
|
317
|
+
// Pokémon Showdown instead produces a list with a single move that must be chosen.
|
|
318
|
+
//
|
|
319
|
+
// Given that no input is allowed on the cartridge 'Pass' seems like it would be logical
|
|
320
|
+
// here when not in compatibility mode, but the engine needs to be able to differentiate
|
|
321
|
+
// between passing while waiting for an opponent's forced switch after fainting and
|
|
322
|
+
// passing due to being forced into using a move. Instead of introducing another option
|
|
323
|
+
// we simply repurpose Move with no move slot, even though pedantically this is not
|
|
324
|
+
// strictly correct as the player would not have been presented the option to move or
|
|
325
|
+
// switch at all.
|
|
326
|
+
if (isForced(active)) {
|
|
327
|
+
out[n] = .{ .type = .Move, .data = @intFromBool(showdown) };
|
|
328
|
+
n += 1;
|
|
329
|
+
return n;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
var slot: u4 = 2;
|
|
333
|
+
while (!active.volatiles.trapped and slot <= 6) : (slot += 1) {
|
|
334
|
+
const id = side.order[slot - 1];
|
|
335
|
+
if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
|
|
336
|
+
out[n] = .{ .type = .Switch, .data = slot };
|
|
337
|
+
n += 1;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const limited = active.volatiles.Bide or active.volatiles.bind > 0;
|
|
341
|
+
// On the cartridge, all of these happen after "FIGHT" (indicating you are not
|
|
342
|
+
// switching) but before you are allowed to select a move. Pokémon Showdown instead
|
|
343
|
+
// either disables all other moves in the case of limited or requires you to select a
|
|
344
|
+
// move normally if sleeping/frozen/bound.
|
|
345
|
+
if (!showdown and (limited or
|
|
346
|
+
Status.is(stored.status, .FRZ) or Status.is(stored.status, .SLP)))
|
|
347
|
+
{
|
|
348
|
+
out[n] = .{ .type = .Move, .data = 0 };
|
|
349
|
+
n += 1;
|
|
350
|
+
return n;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
slot = 1;
|
|
354
|
+
// Pokémon Showdown handles Bide and Binding moves by checking if the move in question
|
|
355
|
+
// is present in the Pokémon's moveset (which means moves called via Metronome / Mirror
|
|
356
|
+
// Move will not result in forcing the subsequent use unless the user also had the
|
|
357
|
+
// proc-ed move in their moveset) and disabling all other moves.
|
|
358
|
+
if (limited) {
|
|
359
|
+
assert(showdown);
|
|
360
|
+
assert(side.last_selected_move != .None);
|
|
361
|
+
while (slot <= 4) : (slot += 1) {
|
|
362
|
+
const m = active.moves[slot - 1];
|
|
363
|
+
if (m.id == .None) break;
|
|
364
|
+
if (m.id == if (active.volatiles.Bide) .Bide else side.last_selected_move) {
|
|
365
|
+
// Pokémon Showdown displays Struggle if limited to Bide but unable to pick
|
|
366
|
+
const struggle =
|
|
367
|
+
m.id == .Bide and (m.pp == 0 or active.volatiles.disabled_move == slot);
|
|
368
|
+
const s = if (struggle) 0 else slot;
|
|
369
|
+
out[n] = .{ .type = .Move, .data = s };
|
|
370
|
+
n += 1;
|
|
371
|
+
return n;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const before = n;
|
|
377
|
+
slot = 1;
|
|
378
|
+
while (slot <= 4) : (slot += 1) {
|
|
379
|
+
const m = active.moves[slot - 1];
|
|
380
|
+
if (m.id == .None) break;
|
|
381
|
+
if (m.pp == 0) continue;
|
|
382
|
+
if (active.volatiles.disabled_move == slot) continue;
|
|
383
|
+
out[n] = .{ .type = .Move, .data = slot };
|
|
384
|
+
n += 1;
|
|
385
|
+
}
|
|
386
|
+
// Struggle (Pokémon Showdown would use 'move 1' here)
|
|
387
|
+
if (n == before) {
|
|
388
|
+
out[n] = .{ .type = .Move, .data = 0 };
|
|
389
|
+
n += 1;
|
|
390
|
+
}
|
|
391
|
+
},
|
|
392
|
+
}
|
|
393
|
+
return n;
|
|
54
394
|
}
|