@pkmn/engine 0.1.0-dev.d716e940 → 0.1.0-dev.dd5b9081
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 +55 -44
- package/build/pkg/addon.js +3 -2
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/layout.json +24 -2
- package/build/pkg/gen1.js +13 -16
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.js.map +1 -1
- package/build.zig +86 -62
- package/package.json +25 -16
- package/src/bin/install-pkmn-engine +10 -5
- package/src/lib/binding/node.zig +45 -140
- package/src/lib/binding/wasm.zig +4 -2
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/data.zig +14 -26
- package/src/lib/common/debug.zig +20 -11
- package/src/lib/common/js.zig +118 -0
- package/src/lib/common/optional.zig +60 -0
- package/src/lib/common/options.zig +9 -0
- package/src/lib/common/protocol.zig +277 -159
- package/src/lib/common/rational.zig +353 -0
- package/src/lib/common/rng.zig +11 -11
- package/src/lib/common/util.zig +12 -0
- package/src/lib/gen1/README.md +259 -177
- package/src/lib/gen1/calc.zig +426 -0
- package/src/lib/gen1/chance.zig +1036 -0
- package/src/lib/gen1/data/moves.zig +178 -180
- package/src/lib/gen1/data/species.zig +2 -2
- package/src/lib/gen1/data/types.zig +3 -3
- package/src/lib/gen1/data.zig +66 -54
- package/src/lib/gen1/helpers.zig +589 -18
- package/src/lib/gen1/mechanics.zig +791 -508
- package/src/lib/gen2/README.md +87 -13
- package/src/lib/gen2/data/items.zig +3 -3
- package/src/lib/gen2/data/moves.zig +624 -563
- package/src/lib/gen2/data/species.zig +1 -1
- package/src/lib/gen2/data/types.zig +3 -3
- package/src/lib/gen2/data.zig +100 -70
- package/src/lib/gen2/helpers.zig +16 -22
- package/src/lib/gen2/mechanics.zig +714 -10
- package/src/lib/pkmn.zig +61 -2
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
const std = @import("std");
|
|
2
2
|
|
|
3
|
+
const pkmn = @import("../pkmn.zig");
|
|
4
|
+
|
|
3
5
|
const common = @import("../common/data.zig");
|
|
4
6
|
const DEBUG = @import("../common/debug.zig").print;
|
|
5
|
-
const options = @import("../common/options.zig");
|
|
6
7
|
const protocol = @import("../common/protocol.zig");
|
|
7
8
|
const rng = @import("../common/rng.zig");
|
|
8
9
|
|
|
@@ -17,7 +18,7 @@ const ID = common.ID;
|
|
|
17
18
|
const Player = common.Player;
|
|
18
19
|
const Result = common.Result;
|
|
19
20
|
|
|
20
|
-
const showdown = options.showdown;
|
|
21
|
+
const showdown = pkmn.options.showdown;
|
|
21
22
|
|
|
22
23
|
const Damage = protocol.Damage;
|
|
23
24
|
const Heal = protocol.Heal;
|
|
@@ -34,20 +35,723 @@ const Species = data.Species;
|
|
|
34
35
|
const Stats = data.Stats;
|
|
35
36
|
const Status = data.Status;
|
|
36
37
|
|
|
37
|
-
|
|
38
|
+
// zig fmt: off
|
|
39
|
+
const STAT_BOOSTS = &[_][2]u8{
|
|
40
|
+
.{ 25, 100 }, // -6
|
|
41
|
+
.{ 28, 100 }, // -5
|
|
42
|
+
.{ 33, 100 }, // -4
|
|
43
|
+
.{ 40, 100 }, // -3
|
|
44
|
+
.{ 50, 100 }, // -2
|
|
45
|
+
.{ 66, 100 }, // -1
|
|
46
|
+
.{ 1, 1 }, // 0
|
|
47
|
+
.{ 15, 10 }, // +1
|
|
48
|
+
.{ 2, 1 }, // +2
|
|
49
|
+
.{ 25, 10 }, // +3
|
|
50
|
+
.{ 3, 1 }, // +4
|
|
51
|
+
.{ 35, 10 }, // +5
|
|
52
|
+
.{ 4, 1 }, // +6
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const ACCURACY_BOOSTS = &[_][2]u8{
|
|
56
|
+
.{ 33, 100 }, // -6
|
|
57
|
+
.{ 36, 100 }, // -5
|
|
58
|
+
.{ 43, 100 }, // -4
|
|
59
|
+
.{ 50, 100 }, // -3
|
|
60
|
+
.{ 60, 100 }, // -2
|
|
61
|
+
.{ 75, 100 }, // -1
|
|
62
|
+
.{ 1, 1 }, // 0
|
|
63
|
+
.{ 133, 100 }, // +1
|
|
64
|
+
.{ 166, 100 }, // +2
|
|
65
|
+
.{ 2, 1 }, // +3
|
|
66
|
+
.{ 233, 100 }, // +4
|
|
67
|
+
.{ 133, 50 }, // +5
|
|
68
|
+
.{ 3, 1 }, // +6
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const CRITICAL_HITS = &[_]u8{
|
|
72
|
+
256 / 15, // 0
|
|
73
|
+
256 / 8, // +1
|
|
74
|
+
256 / 4, // +2
|
|
75
|
+
256 / 3, // +3
|
|
76
|
+
256 / 2, // +4
|
|
77
|
+
256 / 2, // +5
|
|
78
|
+
256 / 2, // +6
|
|
79
|
+
};
|
|
80
|
+
// zig fmt: on
|
|
81
|
+
|
|
82
|
+
const MAX_STAT_VALUE = 999;
|
|
83
|
+
|
|
84
|
+
const DISTRIBUTION = [_]u3{ 2, 2, 2, 3, 3, 3, 4, 5 };
|
|
85
|
+
|
|
86
|
+
pub fn update(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Result {
|
|
87
|
+
assert(c1.type != .Pass or c2.type != .Pass or battle.turn == 0);
|
|
88
|
+
if (battle.turn == 0) return start(battle, options);
|
|
89
|
+
|
|
90
|
+
if (try turnOrder(battle, c1, c2, options) == .P1) {
|
|
91
|
+
if (try doTurn(battle, .P1, c1, .P2, c2, options)) |r| return r;
|
|
92
|
+
} else {
|
|
93
|
+
if (try doTurn(battle, .P2, c2, .P1, c1, options)) |r| return r;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return endTurn(battle, options);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
fn start(battle: anytype, options: anytype) !Result {
|
|
100
|
+
const p1 = battle.side(.P1);
|
|
101
|
+
const p2 = battle.side(.P2);
|
|
102
|
+
|
|
103
|
+
var p1_slot = findFirstAlive(p1);
|
|
104
|
+
assert(!showdown or p1_slot == 1);
|
|
105
|
+
if (p1_slot == 0) return if (findFirstAlive(p2) == 0) Result.Tie else Result.Lose;
|
|
106
|
+
|
|
107
|
+
var p2_slot = findFirstAlive(p2);
|
|
108
|
+
assert(!showdown or p2_slot == 1);
|
|
109
|
+
if (p2_slot == 0) return Result.Win;
|
|
110
|
+
|
|
111
|
+
try switchIn(battle, .P1, p1_slot, true, options);
|
|
112
|
+
try switchIn(battle, .P2, p2_slot, true, options);
|
|
113
|
+
|
|
114
|
+
return endTurn(battle, options);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
fn findFirstAlive(side: *const Side) u8 {
|
|
118
|
+
for (side.pokemon, 0..) |pokemon, i| if (pokemon.hp > 0) return side.pokemon[i].position;
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
fn selectMove(battle: anytype, player: Player, choice: Choice) void {
|
|
123
|
+
if (choice.type == .Pass) return; // FIXME BATON PASS
|
|
124
|
+
|
|
125
|
+
var side = battle.side(player);
|
|
126
|
+
var volatiles = &side.active.volatiles;
|
|
127
|
+
|
|
128
|
+
// pre-battle menu
|
|
129
|
+
const forced = forced: {
|
|
130
|
+
if (volatiles.Recharging) break :forced true;
|
|
131
|
+
// Pokémon Showdown removes Flinch at the end-of-turn in its residual handler
|
|
132
|
+
if (!showdown) {
|
|
133
|
+
volatiles.Flinch = false;
|
|
134
|
+
battle.foe(player).active.volatiles.Flinch = false;
|
|
135
|
+
}
|
|
136
|
+
break :forced (volatiles.Thrashing or volatiles.Charging or volatiles.Rollout);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// battle menu
|
|
140
|
+
if (choice.type == .Switch) return;
|
|
141
|
+
assert(choice.type == .Move and (!forced or choice.data == @intFromBool(showdown)));
|
|
142
|
+
|
|
143
|
+
done: {
|
|
144
|
+
if (forced) break :done;
|
|
145
|
+
// pre-move select
|
|
146
|
+
if (volatiles.Encore) {
|
|
147
|
+
// TODO wCurPlayerMove = side.lastMove(true);
|
|
148
|
+
// assert(wCurPlayerMove != .None);
|
|
149
|
+
volatiles.Bide = false; // FIXME wPlayerCharging vs. SUBSTATUS_BIDE?
|
|
150
|
+
const effect = Move.get(.Pound).effect; // TODO
|
|
151
|
+
if (effect != .FuryCutter) volatiles.fury_cutter = false;
|
|
152
|
+
if (effect != .Rage) {
|
|
153
|
+
volatiles.Rage = false;
|
|
154
|
+
volatiles.rage = 0;
|
|
155
|
+
}
|
|
156
|
+
if (effect != .Protect and effect != .Endure) volatiles.protect = 0;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (volatiles.Bide) break :done;
|
|
160
|
+
// move select
|
|
161
|
+
if (choice.data == 0) {
|
|
162
|
+
const struggle = ok: {
|
|
163
|
+
for (side.active.moves, 0..) |move, i| {
|
|
164
|
+
if (move.pp > 0 and volatiles.disabled_move != i + 1) break :ok false;
|
|
165
|
+
}
|
|
166
|
+
break :ok true;
|
|
167
|
+
};
|
|
168
|
+
assert(struggle);
|
|
169
|
+
// TODO wCurPlayerMove = .Struggle;
|
|
170
|
+
} else {
|
|
171
|
+
assert(showdown or side.active.volatiles.disabled_move != choice.data);
|
|
172
|
+
const move = side.active.move(choice.data);
|
|
173
|
+
// You cannot *select* a move with 0 PP
|
|
174
|
+
assert(move.pp != 0);
|
|
175
|
+
// TODO wCurPlayerMove = move.id;
|
|
176
|
+
// battle.lastMove(player).index = @intCast(c.data);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
volatiles.Rage = false;
|
|
181
|
+
volatiles.rage = 0;
|
|
182
|
+
volatiles.fury_cutter = 0;
|
|
183
|
+
volatiles.protect = 0;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
fn switchIn(battle: anytype, player: Player, slot: u8, initial: bool, options: anytype) !void {
|
|
187
|
+
var side = battle.side(player);
|
|
188
|
+
var foe = battle.foe(player);
|
|
189
|
+
|
|
190
|
+
var active = &side.active;
|
|
191
|
+
const incoming = side.get(slot);
|
|
192
|
+
|
|
193
|
+
assert(incoming.hp != 0);
|
|
194
|
+
assert(slot != 1 or initial);
|
|
195
|
+
|
|
196
|
+
const out = side.pokemon[0].position;
|
|
197
|
+
side.pokemon[0].position = side.pokemon[slot - 1].position;
|
|
198
|
+
side.pokemon[slot - 1].position = out;
|
|
199
|
+
|
|
200
|
+
side.last_used_move = .None;
|
|
201
|
+
foe.active.volatiles.dirty = true;
|
|
202
|
+
|
|
203
|
+
active.volatiles = .{};
|
|
204
|
+
active.stats = incoming.stats;
|
|
205
|
+
active.moves = incoming.moves;
|
|
206
|
+
active.boosts = .{};
|
|
207
|
+
active.types = incoming.types;
|
|
208
|
+
active.species = incoming.species;
|
|
209
|
+
|
|
210
|
+
statusModify(incoming.status, &active.stats);
|
|
211
|
+
|
|
212
|
+
try options.log.switched(battle.active(player), incoming);
|
|
213
|
+
options.chance.switched(player, side.pokemon[0].position, out);
|
|
214
|
+
|
|
215
|
+
if (side.conditions.Spikes and !active.types.includes(.Flying)) {
|
|
216
|
+
incoming.hp -|= @max(incoming.stats.hp / 8, 1);
|
|
217
|
+
try options.log.damage(battle.active(player), incoming, .Spikes);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
fn turnOrder(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Player {
|
|
222
|
+
assert(c1.type != .Pass or c2.type != .Pass);
|
|
223
|
+
|
|
224
|
+
if (c1.type == .Pass) return .P2;
|
|
225
|
+
if (c2.type == .Pass) return .P1;
|
|
226
|
+
|
|
227
|
+
// Pokémon Showdown always rolls for Quick Claw every turn, even if no Pokémon hold Quick Claw
|
|
228
|
+
// TODO: add to pending and only commit roll on Pokémon Showdown if roll is used
|
|
229
|
+
const qkc = showdown and try Rolls.quickClaw(battle, .P1, options);
|
|
230
|
+
|
|
231
|
+
if ((c1.type == .Switch) != (c2.type == .Switch)) return if (c1.type == .Switch) .P1 else .P2;
|
|
232
|
+
|
|
233
|
+
// https://www.smogon.com/forums/threads/adv-switch-priority.3622189/
|
|
234
|
+
// > In Gen 1 it's irrelevant [which player switches first] because switches happen instantly on
|
|
235
|
+
// > your own screen without waiting for the other player's choice (and their choice will appear
|
|
236
|
+
// > to happen first for them too, unless they attacked in which case your switch happens first)
|
|
237
|
+
// A cartridge-compatible implemention must not advance the RNG so we simply default to P1
|
|
238
|
+
const double_switch = c1.type == .Switch and c2.type == .Switch;
|
|
239
|
+
if (!showdown and double_switch) return .P1;
|
|
240
|
+
|
|
241
|
+
const m1 = .Pound; // TODO battle.side(.P1).last_selected_move;
|
|
242
|
+
const m2 = .Pound; // TODO battle.side(.P2).last_selected_move;
|
|
243
|
+
if (!showdown or !double_switch) {
|
|
244
|
+
const pri1 = Move.get(m1).priority;
|
|
245
|
+
const pri2 = Move.get(m2).priority;
|
|
246
|
+
if (pri1 != pri2) return if (pri1 > pri2) .P1 else .P2;
|
|
247
|
+
|
|
248
|
+
const qkc1 = battle.side(.P1).stored().item == .QuickClaw;
|
|
249
|
+
const qkc2 = battle.side(.P2).stored().item == .QuickClaw;
|
|
250
|
+
if (!showdown and qkc1 and qkc2) {
|
|
251
|
+
// If both Pokémon have Quick Claw the cartridge checks them in order and exits early if
|
|
252
|
+
// the first one procs, meaning the host has a slightly higher chance of going first
|
|
253
|
+
if (try Rolls.quickClaw(battle, .P1, options)) return .P1;
|
|
254
|
+
if (try Rolls.quickClaw(battle, .P2, options)) return .P2;
|
|
255
|
+
} else if (qkc1) {
|
|
256
|
+
const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P1, options);
|
|
257
|
+
if (proc) return .P1;
|
|
258
|
+
} else if (qkc2) {
|
|
259
|
+
const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P2, options);
|
|
260
|
+
if (proc) return .P2;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const spe1 = battle.side(.P1).active.stats.spe;
|
|
265
|
+
const spe2 = battle.side(.P2).active.stats.spe;
|
|
266
|
+
if (spe1 == spe2) {
|
|
267
|
+
// Pokémon Showdown's beforeTurnCallback shenanigans
|
|
268
|
+
if (showdown and !double_switch and hasCallback(m1) and hasCallback(m2)) {
|
|
269
|
+
battle.rng.advance(1);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const p1 = try Rolls.speedTie(battle, options);
|
|
273
|
+
if (!showdown) return if (p1) .P1 else .P2;
|
|
274
|
+
|
|
275
|
+
// Pokémon Showdown's "lockedmove" volatile's onBeforeTurn uses BattleQueue#changeAction,
|
|
276
|
+
// meaning that if a side is locked into a thrashing move and wins the speed tie, it
|
|
277
|
+
// actually uses its priority to simply insert its actual changed action into the queue,
|
|
278
|
+
// causing it to then execute *after* the side which should go second...
|
|
279
|
+
const t1 = battle.side(.P1).active.volatiles.Thrashing;
|
|
280
|
+
const t2 = battle.side(.P2).active.volatiles.Thrashing;
|
|
281
|
+
// If *both* sides are thrashing it really should be another speed tie, but we've patched
|
|
282
|
+
// that out and enforce host ordering of events, so P1 just goes first regardless of who
|
|
283
|
+
// won the original coin flip
|
|
284
|
+
if (t1 and t2) return .P1;
|
|
285
|
+
return if (p1) if (t1 and !t2) .P2 else .P1 else if (t2 and !t1) .P1 else .P2;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return if (spe1 > spe2) .P1 else .P2;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
fn hasCallback(m: Move) bool {
|
|
292
|
+
return switch (m) {
|
|
293
|
+
.Counter, .MirrorCoat, .Pursuit => true,
|
|
294
|
+
else => false,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
fn doTurn(
|
|
299
|
+
battle: anytype,
|
|
300
|
+
player: Player,
|
|
301
|
+
player_choice: Choice,
|
|
302
|
+
foe_player: Player,
|
|
303
|
+
foe_choice: Choice,
|
|
304
|
+
options: anytype,
|
|
305
|
+
) !?Result {
|
|
306
|
+
assert(player_choice.type != .Pass);
|
|
307
|
+
|
|
308
|
+
const volatiles = &battle.side(player).active.volatiles;
|
|
309
|
+
const foe_volatiles = &battle.foe(player).active.volatiles;
|
|
310
|
+
|
|
311
|
+
volatiles.DestinyBond = false;
|
|
312
|
+
if (try executeMove(battle, player, player_choice, options)) |r| return r;
|
|
313
|
+
foe_volatiles.Protect = false;
|
|
314
|
+
foe_volatiles.Endure = false;
|
|
315
|
+
foe_volatiles.DestinyBond = false;
|
|
316
|
+
|
|
317
|
+
// foe_volatiles.DestinyBond = false;
|
|
318
|
+
if (try executeMove(battle, foe_player, foe_choice, options)) |r| return r;
|
|
319
|
+
volatiles.Protect = false;
|
|
320
|
+
volatiles.Endure = false;
|
|
321
|
+
volatiles.DestinyBond = false;
|
|
322
|
+
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
fn executeMove(battle: anytype, player: Player, choice: Choice, options: anytype) !?Result {
|
|
327
|
+
if (choice.type == .Switch) {
|
|
328
|
+
try switchIn(battle, player, choice.data, false, options);
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
assert(choice.type == .Move);
|
|
333
|
+
var mslot: u4 = @intCast(choice.data);
|
|
334
|
+
|
|
335
|
+
// const move = Move.get(side.last_selected_move);
|
|
336
|
+
|
|
337
|
+
// CheckTurn
|
|
338
|
+
|
|
339
|
+
return doMove(battle, player, options, mslot);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
fn beforeMove(battle: anytype, player: Player, options: anytype) !void {
|
|
38
343
|
_ = battle;
|
|
39
|
-
_ =
|
|
40
|
-
_ =
|
|
41
|
-
|
|
344
|
+
_ = player;
|
|
345
|
+
_ = options;
|
|
346
|
+
}
|
|
42
347
|
|
|
43
|
-
|
|
348
|
+
fn canMove(battle: anytype, player: Player, options: anytype) !bool {
|
|
349
|
+
_ = battle;
|
|
350
|
+
_ = player;
|
|
351
|
+
_ = options;
|
|
352
|
+
return false;
|
|
44
353
|
}
|
|
45
354
|
|
|
46
|
-
|
|
355
|
+
fn decrementPP(side: *Side, mslot: u4) void {
|
|
356
|
+
var active = &side.active;
|
|
357
|
+
const volatiles = &active.volatiles;
|
|
358
|
+
|
|
359
|
+
// TODO assert(side.last_selected_move != .Struggle);
|
|
360
|
+
assert(!volatiles.Charging and !volatiles.BeatUp and !volatiles.Thrashing and !volatiles.Bide);
|
|
361
|
+
|
|
362
|
+
assert(active.move(mslot).pp > 0);
|
|
363
|
+
active.move(mslot).pp = @as(u6, @intCast(active.move(mslot).pp)) -% 1;
|
|
364
|
+
if (volatiles.Transform) return;
|
|
365
|
+
|
|
366
|
+
// FIXME mimic
|
|
367
|
+
|
|
368
|
+
assert(side.stored().move(mslot).pp > 0);
|
|
369
|
+
side.stored().move(mslot).pp = @as(u6, @intCast(side.stored().move(mslot).pp)) -% 1;
|
|
370
|
+
assert(active.move(mslot).pp == side.stored().move(mslot).pp);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
fn doMove(battle: anytype, player: Player, options: anytype, mslot: u4) !?Result {
|
|
374
|
+
_ = options;
|
|
375
|
+
var side = battle.side(player);
|
|
376
|
+
const move = Move.get(.Teleport); // side.last_selected_move
|
|
377
|
+
switch (move.effect) {
|
|
378
|
+
.Teleport => {
|
|
379
|
+
// side.useMove();
|
|
380
|
+
decrementPP(side, mslot);
|
|
381
|
+
},
|
|
382
|
+
else => @panic("TODO"),
|
|
383
|
+
}
|
|
384
|
+
return null;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
fn checkCriticalHit(battle: anytype, player: Player, move: Move.Data, options: anytype) !bool {
|
|
388
|
+
_ = battle;
|
|
389
|
+
_ = player;
|
|
390
|
+
_ = move;
|
|
391
|
+
_ = options;
|
|
392
|
+
return false;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
fn calcDamage(
|
|
396
|
+
battle: anytype,
|
|
397
|
+
player: Player,
|
|
398
|
+
target_player: Player,
|
|
399
|
+
m: ?Move.Data,
|
|
400
|
+
crit: bool,
|
|
401
|
+
) u16 {
|
|
402
|
+
_ = battle;
|
|
403
|
+
_ = player;
|
|
404
|
+
_ = target_player;
|
|
405
|
+
_ = m;
|
|
406
|
+
_ = crit;
|
|
407
|
+
return 0;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
fn adjustDamage(battle: anytype, player: Player) u16 {
|
|
47
411
|
_ = battle;
|
|
48
412
|
_ = player;
|
|
49
|
-
|
|
50
|
-
|
|
413
|
+
return 0;
|
|
414
|
+
}
|
|
51
415
|
|
|
416
|
+
fn randomizeDamage(battle: anytype, player: Player, options: anytype) !u16 {
|
|
417
|
+
_ = battle;
|
|
418
|
+
_ = player;
|
|
419
|
+
_ = options;
|
|
52
420
|
return 0;
|
|
53
421
|
}
|
|
422
|
+
|
|
423
|
+
fn applyDamage(
|
|
424
|
+
battle: anytype,
|
|
425
|
+
player: Player,
|
|
426
|
+
options: anytype,
|
|
427
|
+
) !bool {
|
|
428
|
+
_ = battle;
|
|
429
|
+
_ = player;
|
|
430
|
+
_ = options;
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
fn checkHit(battle: anytype, player: Player, move: Move.Data, options: anytype) !bool {
|
|
435
|
+
if (moveHit(battle, player, move, options)) return true;
|
|
436
|
+
|
|
437
|
+
try options.chance.commit(player, .miss);
|
|
438
|
+
try options.log.lastmiss();
|
|
439
|
+
try options.log.miss(battle.active(player));
|
|
440
|
+
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
fn moveHit(
|
|
445
|
+
battle: anytype,
|
|
446
|
+
player: Player,
|
|
447
|
+
move: Move.Data,
|
|
448
|
+
options: anytype,
|
|
449
|
+
) bool {
|
|
450
|
+
var side = battle.side(player);
|
|
451
|
+
const foe = battle.foe(player);
|
|
452
|
+
_ = move;
|
|
453
|
+
_ = side;
|
|
454
|
+
_ = foe;
|
|
455
|
+
_ = options;
|
|
456
|
+
return false;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
fn checkFaint(
|
|
460
|
+
battle: anytype,
|
|
461
|
+
player: Player,
|
|
462
|
+
options: anytype,
|
|
463
|
+
) @TypeOf(options.log).Error!?Result {
|
|
464
|
+
_ = battle;
|
|
465
|
+
_ = player;
|
|
466
|
+
return null;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
fn faint(battle: anytype, player: Player, done: bool, options: anytype) !?Result {
|
|
470
|
+
_ = battle;
|
|
471
|
+
_ = player;
|
|
472
|
+
_ = done;
|
|
473
|
+
_ = options;
|
|
474
|
+
return null;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
fn handleResidual(battle: anytype, player: Player, options: anytype) !void {
|
|
478
|
+
var side = battle.side(player);
|
|
479
|
+
var stored = side.stored();
|
|
480
|
+
const ident = battle.active(player);
|
|
481
|
+
var volatiles = &side.active.volatiles;
|
|
482
|
+
|
|
483
|
+
// TODO if (stored.hp == 0) return;
|
|
484
|
+
assert(stored.hp > 0);
|
|
485
|
+
|
|
486
|
+
const brn = Status.is(stored.status, .BRN);
|
|
487
|
+
if (brn or Status.is(stored.status, .PSN)) {
|
|
488
|
+
var damage: u16 = undefined;
|
|
489
|
+
if (volatiles.Toxic) {
|
|
490
|
+
volatiles.toxic += 1;
|
|
491
|
+
damage = @max(stored.stats.hp / 16, 1) * volatiles.toxic;
|
|
492
|
+
} else {
|
|
493
|
+
damage = @max(stored.stats.hp / 8, 1);
|
|
494
|
+
}
|
|
495
|
+
damage = @min(damage, stored.hp);
|
|
496
|
+
|
|
497
|
+
if (!showdown or damage > 0) {
|
|
498
|
+
stored.hp -= damage;
|
|
499
|
+
// Pokémon Showdown uses damageOf here but its not relevant in Generation II
|
|
500
|
+
try options.log.damage(ident, stored, if (brn) Damage.Burn else Damage.Poison);
|
|
501
|
+
if (stored.hp == 0) return;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
if (volatiles.LeechSeed) {
|
|
506
|
+
var foe = battle.foe(player);
|
|
507
|
+
var foe_stored = foe.stored();
|
|
508
|
+
const foe_ident = battle.active(player.foe());
|
|
509
|
+
|
|
510
|
+
// if (foe_stored.hp == 0) {
|
|
511
|
+
// assert(showdown);
|
|
512
|
+
// return;
|
|
513
|
+
// }
|
|
514
|
+
|
|
515
|
+
const damage = @min(@max(stored.stats.hp / 8, 1), stored.hp);
|
|
516
|
+
stored.hp -= damage;
|
|
517
|
+
// As above, Pokémon Showdown uses damageOf but its not relevant
|
|
518
|
+
if (damage > 0) try options.log.damage(ident, stored, .LeechSeed);
|
|
519
|
+
|
|
520
|
+
const before = foe_stored.hp;
|
|
521
|
+
foe_stored.hp = @min(foe_stored.hp + damage, foe_stored.stats.hp);
|
|
522
|
+
// Pokémon Showdown uses the less specific heal here instead of drain... because reasons?
|
|
523
|
+
if (foe_stored.hp > before) try options.log.heal(foe_ident, foe_stored, .Silent);
|
|
524
|
+
if (stored.hp == 0) return;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (volatiles.Nightmare) {
|
|
528
|
+
const damage = @min(@max(stored.stats.hp / 4, 1), stored.hp);
|
|
529
|
+
if (!showdown or damage > 0) {
|
|
530
|
+
stored.hp -= damage;
|
|
531
|
+
// ibid
|
|
532
|
+
try options.log.damage(ident, stored, Damage.Nightmare);
|
|
533
|
+
if (stored.hp == 0) return;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if (volatiles.Curse) {
|
|
538
|
+
const damage = @min(@max(stored.stats.hp / 4, 1), stored.hp);
|
|
539
|
+
if (!showdown or damage > 0) {
|
|
540
|
+
stored.hp -= damage;
|
|
541
|
+
// ibid
|
|
542
|
+
try options.log.damage(ident, stored, Damage.Curse);
|
|
543
|
+
if (stored.hp == 0) return;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
fn betweenTurn(battle: anytype, options: anytype) !void {
|
|
549
|
+
var p1 = battle.side(.P1);
|
|
550
|
+
var p2 = battle.side(.P2);
|
|
551
|
+
|
|
552
|
+
_ = p1;
|
|
553
|
+
_ = p2;
|
|
554
|
+
_ = options;
|
|
555
|
+
|
|
556
|
+
// TODO checkFaintThen(p1, p2);
|
|
557
|
+
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
fn endTurn(battle: anytype, options: anytype) @TypeOf(options.log).Error!Result {
|
|
561
|
+
battle.turn += 1;
|
|
562
|
+
|
|
563
|
+
if (showdown and battle.turn >= 1000) {
|
|
564
|
+
try options.log.tie();
|
|
565
|
+
return Result.Tie;
|
|
566
|
+
} else if (battle.turn >= 65535) {
|
|
567
|
+
return Result.Error;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
try options.log.turn(battle.turn);
|
|
571
|
+
|
|
572
|
+
return Result.Default;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
pub const Effects = struct {
|
|
576
|
+
fn protect(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
|
|
577
|
+
var side = battle.side(player);
|
|
578
|
+
var volatiles = &side.active.volatiles;
|
|
579
|
+
|
|
580
|
+
// TODO or if we didn't go first
|
|
581
|
+
if (volatiles.Substitute) return try options.log.fail(battle.active(player), .None);
|
|
582
|
+
|
|
583
|
+
// TODO
|
|
584
|
+
|
|
585
|
+
if (move.effect == .Endure) {
|
|
586
|
+
volatiles.Endure = true;
|
|
587
|
+
} else {
|
|
588
|
+
volatiles.Protect = true;
|
|
589
|
+
}
|
|
590
|
+
try options.log.singleturn(battle.active(player), move.id);
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
fn statusModify(status: u8, stats: *Stats(u16)) void {
|
|
595
|
+
if (Status.is(status, .PAR)) {
|
|
596
|
+
stats.spe = @max(stats.spe / 4, 1);
|
|
597
|
+
} else if (Status.is(status, .BRN)) {
|
|
598
|
+
stats.atk = @max(stats.atk / 2, 1);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
inline fn isForced(active: anytype) bool {
|
|
603
|
+
return active.volatiles.Recharging or active.volatiles.Rage or
|
|
604
|
+
active.volatiles.Thrashing or active.volatiles.Charging or
|
|
605
|
+
// FIXME TODO Underground or Flying?
|
|
606
|
+
active.volatiles.Underground or active.volatiles.Flying or
|
|
607
|
+
active.volatiles.Rollout;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
pub const Rolls = struct {
|
|
611
|
+
inline fn speedTie(battle: anytype, options: anytype) !bool {
|
|
612
|
+
const p1 = if (options.calc.overridden(.P1, .speed_tie)) |player|
|
|
613
|
+
player == .P1
|
|
614
|
+
else if (showdown)
|
|
615
|
+
battle.rng.range(u8, 0, 2) == 0
|
|
616
|
+
else
|
|
617
|
+
battle.rng.next() < Gen12.percent(50) + 1;
|
|
618
|
+
|
|
619
|
+
try options.chance.speedTie(p1);
|
|
620
|
+
return p1;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
inline fn quickClaw(battle: anytype, player: Player, options: anytype) !bool {
|
|
624
|
+
const qkc = if (options.calc.overridden(player, .quick_claw)) |val|
|
|
625
|
+
val == .true
|
|
626
|
+
else if (showdown)
|
|
627
|
+
battle.rng.chance(u8, 60, 256)
|
|
628
|
+
else
|
|
629
|
+
battle.rng.next() < 60;
|
|
630
|
+
|
|
631
|
+
try options.chance.quickClaw(player, qkc);
|
|
632
|
+
return qkc;
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
test "RNG agreement" {
|
|
637
|
+
if (!showdown) return;
|
|
638
|
+
var expected: [256]u32 = undefined;
|
|
639
|
+
for (0..expected.len) |i| {
|
|
640
|
+
expected[i] = @intCast(i * 0x1000000);
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
var spe = rng.FixedRNG(2, expected.len){ .rolls = expected };
|
|
644
|
+
var qkc = rng.FixedRNG(2, expected.len){ .rolls = expected };
|
|
645
|
+
|
|
646
|
+
for (0..expected.len) |i| {
|
|
647
|
+
try expectEqual(spe.range(u8, 0, 2) == 0, i < Gen12.percent(50) + 1);
|
|
648
|
+
try expectEqual(qkc.chance(u8, 60, 256), i < 60);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Choice) u8 {
|
|
653
|
+
var n: u8 = 0;
|
|
654
|
+
switch (request) {
|
|
655
|
+
.Pass => {
|
|
656
|
+
out[n] = .{};
|
|
657
|
+
n += 1;
|
|
658
|
+
},
|
|
659
|
+
.Switch => {
|
|
660
|
+
const side = battle.side(player);
|
|
661
|
+
var slot: u4 = 2;
|
|
662
|
+
while (slot <= 6) : (slot += 1) {
|
|
663
|
+
const id = side.order[slot - 1];
|
|
664
|
+
if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
|
|
665
|
+
out[n] = .{ .type = .Switch, .data = slot };
|
|
666
|
+
n += 1;
|
|
667
|
+
}
|
|
668
|
+
if (n == 0) {
|
|
669
|
+
out[n] = .{};
|
|
670
|
+
n += 1;
|
|
671
|
+
}
|
|
672
|
+
},
|
|
673
|
+
.Move => {
|
|
674
|
+
const side = battle.side(player);
|
|
675
|
+
|
|
676
|
+
var active = &side.active;
|
|
677
|
+
const stored = side.stored();
|
|
678
|
+
|
|
679
|
+
// While players are not given any input options on the cartridge in these cases,
|
|
680
|
+
// Pokémon Showdown instead produces a list with a single move that must be chosen.
|
|
681
|
+
//
|
|
682
|
+
// Given that no input is allowed on the cartridge 'Pass' seems like it would be logical
|
|
683
|
+
// here when not in compatibility mode, but the engine needs to be able to differentiate
|
|
684
|
+
// between passing while waiting for an opponent's forced switch after fainting and
|
|
685
|
+
// passing due to being forced into using a move. Instead of introducing another option
|
|
686
|
+
// we simply repurpose Move with no move slot, even though pedantically this is not
|
|
687
|
+
// strictly correct as the player would not have been presented the option to move or
|
|
688
|
+
// switch at all.
|
|
689
|
+
if (isForced(active)) {
|
|
690
|
+
out[n] = .{ .type = .Move, .data = @intFromBool(showdown) };
|
|
691
|
+
n += 1;
|
|
692
|
+
return n;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
var slot: u4 = 2;
|
|
696
|
+
while (!active.volatiles.trapped and slot <= 6) : (slot += 1) {
|
|
697
|
+
const id = side.order[slot - 1];
|
|
698
|
+
if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
|
|
699
|
+
out[n] = .{ .type = .Switch, .data = slot };
|
|
700
|
+
n += 1;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
const limited = active.volatiles.Bide or active.volatiles.bind > 0;
|
|
704
|
+
// On the cartridge, all of these happen after "FIGHT" (indicating you are not
|
|
705
|
+
// switching) but before you are allowed to select a move. Pokémon Showdown instead
|
|
706
|
+
// either disables all other moves in the case of limited or requires you to select a
|
|
707
|
+
// move normally if sleeping/frozen/bound.
|
|
708
|
+
if (!showdown and (limited or
|
|
709
|
+
Status.is(stored.status, .FRZ) or Status.is(stored.status, .SLP)))
|
|
710
|
+
{
|
|
711
|
+
out[n] = .{ .type = .Move, .data = 0 };
|
|
712
|
+
n += 1;
|
|
713
|
+
return n;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
slot = 1;
|
|
717
|
+
// Pokémon Showdown handles Bide and Binding moves by checking if the move in question
|
|
718
|
+
// is present in the Pokémon's moveset (which means moves called via Metronome / Mirror
|
|
719
|
+
// Move will not result in forcing the subsequent use unless the user also had the
|
|
720
|
+
// proc-ed move in their moveset) and disabling all other moves.
|
|
721
|
+
if (limited) {
|
|
722
|
+
assert(showdown);
|
|
723
|
+
assert(side.last_selected_move != .None);
|
|
724
|
+
while (slot <= 4) : (slot += 1) {
|
|
725
|
+
const m = active.moves[slot - 1];
|
|
726
|
+
if (m.id == .None) break;
|
|
727
|
+
if (m.id == if (active.volatiles.Bide) .Bide else side.last_selected_move) {
|
|
728
|
+
// Pokémon Showdown displays Struggle if limited to Bide but unable to pick
|
|
729
|
+
const struggle =
|
|
730
|
+
m.id == .Bide and (m.pp == 0 or active.volatiles.disabled_move == slot);
|
|
731
|
+
const s = if (struggle) 0 else slot;
|
|
732
|
+
out[n] = .{ .type = .Move, .data = s };
|
|
733
|
+
n += 1;
|
|
734
|
+
return n;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
const before = n;
|
|
740
|
+
slot = 1;
|
|
741
|
+
while (slot <= 4) : (slot += 1) {
|
|
742
|
+
const m = active.moves[slot - 1];
|
|
743
|
+
if (m.id == .None) break;
|
|
744
|
+
if (m.pp == 0) continue;
|
|
745
|
+
if (active.volatiles.disabled_move == slot) continue;
|
|
746
|
+
out[n] = .{ .type = .Move, .data = slot };
|
|
747
|
+
n += 1;
|
|
748
|
+
}
|
|
749
|
+
// Struggle (Pokémon Showdown would use 'move 1' here)
|
|
750
|
+
if (n == before) {
|
|
751
|
+
out[n] = .{ .type = .Move, .data = 0 };
|
|
752
|
+
n += 1;
|
|
753
|
+
}
|
|
754
|
+
},
|
|
755
|
+
}
|
|
756
|
+
return n;
|
|
757
|
+
}
|