@pkmn/engine 0.1.0-dev.cf6f5d2c → 0.1.0-dev.d3e796d5
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 +25 -3
- package/build/pkg/gen1.d.ts +2 -1
- package/build/pkg/gen1.js +36 -18
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +4 -2
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +4 -2
- package/build/pkg/protocol.js +4 -2
- package/build/pkg/protocol.js.map +1 -1
- package/build/test/display.js +7 -3
- package/build/test/display.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 +11 -11
- 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 +10 -10
- package/src/lib/common/util.zig +12 -0
- package/src/lib/gen1/README.md +239 -151
- package/src/lib/gen1/calc.zig +426 -0
- package/src/lib/gen1/chance.zig +1044 -0
- package/src/lib/gen1/data/moves.zig +10 -10
- package/src/lib/gen1/data/species.zig +2 -2
- package/src/lib/gen1/data/types.zig +3 -3
- package/src/lib/gen1/data.zig +64 -38
- package/src/lib/gen1/helpers.zig +592 -35
- package/src/lib/gen1/mechanics.zig +804 -500
- package/src/lib/gen2/README.md +87 -13
- package/src/lib/gen2/data/items.zig +3 -3
- package/src/lib/gen2/data/moves.zig +16 -3
- package/src/lib/gen2/data/species.zig +1 -1
- package/src/lib/gen2/data/types.zig +3 -3
- package/src/lib/gen2/data.zig +73 -38
- package/src/lib/gen2/helpers.zig +15 -16
- package/src/lib/gen2/mechanics.zig +476 -11
- package/src/lib/pkmn.zig +61 -2
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) },
|
|
@@ -72,11 +72,9 @@ pub fn prng(rand: *PSRNG) data.PRNG {
|
|
|
72
72
|
rand.newSeed()
|
|
73
73
|
else
|
|
74
74
|
.{
|
|
75
|
-
rand.range(u8, 0, 256), rand.range(u8, 0, 256),
|
|
76
|
-
rand.range(u8, 0, 256), rand.range(u8, 0, 256),
|
|
77
|
-
rand.range(u8, 0, 256), rand.range(u8, 0, 256),
|
|
78
|
-
rand.range(u8, 0, 256), rand.range(u8, 0, 256),
|
|
79
|
-
rand.range(u8, 0, 256), rand.range(u8, 0, 256),
|
|
75
|
+
rand.range(u8, 0, 256), rand.range(u8, 0, 256), rand.range(u8, 0, 256),
|
|
76
|
+
rand.range(u8, 0, 256), rand.range(u8, 0, 256), rand.range(u8, 0, 256),
|
|
77
|
+
rand.range(u8, 0, 256), rand.range(u8, 0, 256), rand.range(u8, 0, 256),
|
|
80
78
|
},
|
|
81
79
|
},
|
|
82
80
|
};
|
|
@@ -89,7 +87,7 @@ pub const Side = struct {
|
|
|
89
87
|
|
|
90
88
|
for (0..ps.len) |i| {
|
|
91
89
|
side.pokemon[i] = Pokemon.init(ps[i]);
|
|
92
|
-
|
|
90
|
+
side.pokemon[i].position = @as(u4, @intCast(i)) + 1;
|
|
93
91
|
}
|
|
94
92
|
return side;
|
|
95
93
|
}
|
|
@@ -100,7 +98,7 @@ pub const Side = struct {
|
|
|
100
98
|
|
|
101
99
|
for (0..n) |i| {
|
|
102
100
|
side.pokemon[i] = Pokemon.random(rand, opt);
|
|
103
|
-
side.pokemon[i].position = i + 1;
|
|
101
|
+
side.pokemon[i].position = @as(u4, @intCast(i)) + 1;
|
|
104
102
|
}
|
|
105
103
|
|
|
106
104
|
return side;
|
|
@@ -141,7 +139,7 @@ pub const Pokemon = struct {
|
|
|
141
139
|
.id = m,
|
|
142
140
|
.pp_ups = 3,
|
|
143
141
|
// NB: PP can be at most 61 legally (though can overflow to 63)
|
|
144
|
-
.pp = @intCast(
|
|
142
|
+
.pp = @intCast(@min(Move.get(m).pp / 5 * 8, 61)),
|
|
145
143
|
};
|
|
146
144
|
}
|
|
147
145
|
pokemon.item = p.item;
|
|
@@ -153,16 +151,16 @@ pub const Pokemon = struct {
|
|
|
153
151
|
pokemon.status = p.status;
|
|
154
152
|
pokemon.types = species.types;
|
|
155
153
|
pokemon.level = p.level;
|
|
156
|
-
pokemon.dvs = DVs.from(p.dvs);
|
|
154
|
+
pokemon.dvs = DVs.from(p.species, p.dvs);
|
|
157
155
|
return pokemon;
|
|
158
156
|
}
|
|
159
157
|
|
|
160
158
|
pub fn random(rand: *PSRNG, opt: Options) data.Pokemon {
|
|
161
|
-
const s = @
|
|
159
|
+
const s: Species = @enumFromInt(rand.range(u8, 1, Species.size + 1));
|
|
162
160
|
const species = Species.get(s);
|
|
163
161
|
const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
|
|
164
162
|
// Pokémon Showdown does not support most items without in-battle held item effects
|
|
165
|
-
const max_item = @
|
|
163
|
+
const max_item = @intFromEnum(if (showdown) Item.FlowerMail else Item.TM50);
|
|
166
164
|
const item = if (rand.chance(u8, 1, 10)) 0 else rand.range(u8, 1, max_item + 1);
|
|
167
165
|
|
|
168
166
|
var stats: Stats(u16) = .{};
|
|
@@ -184,14 +182,15 @@ pub const Pokemon = struct {
|
|
|
184
182
|
for (0..n) |i| {
|
|
185
183
|
var m: Move = .None;
|
|
186
184
|
sample: while (true) {
|
|
187
|
-
m = @
|
|
185
|
+
m = @enumFromInt(rand.range(u8, 1, 164 + 1));
|
|
188
186
|
for (0..i) |j| if (ms[j].id == m) continue :sample;
|
|
189
187
|
break;
|
|
190
188
|
}
|
|
191
|
-
const pp_ups =
|
|
189
|
+
const pp_ups: u8 =
|
|
192
190
|
if (!opt.cleric and rand.chance(u8, 1, 10)) rand.range(u2, 0, 2 + 1) else 3;
|
|
193
191
|
// NB: PP can be at most 61 legally (though can overflow to 63)
|
|
194
|
-
|
|
192
|
+
var base = Move.get(m).pp;
|
|
193
|
+
const max_pp: u8 = @intCast(base + pp_ups * @min(base / 5, 7));
|
|
195
194
|
ms[i] = .{
|
|
196
195
|
.id = m,
|
|
197
196
|
.pp_ups = pp_ups,
|
|
@@ -201,7 +200,7 @@ pub const Pokemon = struct {
|
|
|
201
200
|
return .{
|
|
202
201
|
.species = s,
|
|
203
202
|
.types = species.types,
|
|
204
|
-
.dvs = DVs.from(dvs),
|
|
203
|
+
.dvs = DVs.from(s, dvs),
|
|
205
204
|
.level = lvl,
|
|
206
205
|
.stats = stats,
|
|
207
206
|
.hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
|
|
@@ -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,484 @@ const Species = data.Species;
|
|
|
34
35
|
const Stats = data.Stats;
|
|
35
36
|
const Status = data.Status;
|
|
36
37
|
|
|
37
|
-
pub fn update(battle: anytype, c1: Choice, c2: Choice,
|
|
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, player: Player, choice: Choice) void {
|
|
75
|
+
if (choice.type == .Pass) return; // FIXME BATON PASS
|
|
76
|
+
|
|
77
|
+
var side = battle.side(player);
|
|
78
|
+
var volatiles = &side.active.volatiles;
|
|
79
|
+
|
|
80
|
+
// pre-battle menu
|
|
81
|
+
const forced = forced: {
|
|
82
|
+
if (volatiles.Recharging) break :forced true;
|
|
83
|
+
// Pokémon Showdown removes Flinch at the end-of-turn in its residual handler
|
|
84
|
+
if (!showdown) {
|
|
85
|
+
volatiles.Flinch = false;
|
|
86
|
+
battle.foe(player).active.volatiles.Flinch = false;
|
|
87
|
+
}
|
|
88
|
+
break :forced (volatiles.Thrashing or volatiles.Charging or volatiles.Rollout);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// battle menu
|
|
92
|
+
if (choice.type == .Switch) return;
|
|
93
|
+
assert(choice.type == .Move and (!forced or choice.data == @intFromBool(showdown)));
|
|
94
|
+
|
|
95
|
+
done: {
|
|
96
|
+
if (forced) break :done;
|
|
97
|
+
// pre-move select
|
|
98
|
+
if (volatiles.Encore) {
|
|
99
|
+
// TODO wCurPlayerMove = side.lastMove(true);
|
|
100
|
+
// assert(wCurPlayerMove != .None);
|
|
101
|
+
volatiles.Bide = false; // FIXME wPlayerCharging vs. SUBSTATUS_BIDE?
|
|
102
|
+
const effect = Move.get(.Pound).effect; // TODO
|
|
103
|
+
if (effect != .FuryCutter) volatiles.fury_cutter = false;
|
|
104
|
+
if (effect != .Rage) {
|
|
105
|
+
volatiles.Rage = false;
|
|
106
|
+
volatiles.rage = 0;
|
|
107
|
+
}
|
|
108
|
+
if (effect != .Protect and effect != .Endure) volatiles.protect = 0;
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (volatiles.Bide) break :done;
|
|
112
|
+
// move select
|
|
113
|
+
if (choice.data == 0) {
|
|
114
|
+
const struggle = ok: {
|
|
115
|
+
for (side.active.moves, 0..) |move, i| {
|
|
116
|
+
if (move.pp > 0 and volatiles.disabled_move != i + 1) break :ok false;
|
|
117
|
+
}
|
|
118
|
+
break :ok true;
|
|
119
|
+
};
|
|
120
|
+
assert(struggle);
|
|
121
|
+
// TODO wCurPlayerMove = .Struggle;
|
|
122
|
+
} else {
|
|
123
|
+
assert(showdown or side.active.volatiles.disabled_move != choice.data);
|
|
124
|
+
const move = side.active.move(choice.data);
|
|
125
|
+
// You cannot *select* a move with 0 PP
|
|
126
|
+
assert(move.pp != 0);
|
|
127
|
+
// TODO wCurPlayerMove = move.id;
|
|
128
|
+
// battle.lastMove(player).index = @intCast(c.data);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
volatiles.Rage = false;
|
|
133
|
+
volatiles.rage = 0;
|
|
134
|
+
volatiles.fury_cutter = 0;
|
|
135
|
+
volatiles.protect = 0;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
fn saveMove(battle: anytype) void {
|
|
38
139
|
_ = battle;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
fn switchIn(battle: anytype, player: Player, slot: u8, initial: bool, options: anytype) !void {
|
|
144
|
+
var side = battle.side(player);
|
|
145
|
+
var foe = battle.foe(player);
|
|
146
|
+
|
|
147
|
+
var active = &side.active;
|
|
148
|
+
const incoming = side.get(slot);
|
|
149
|
+
|
|
150
|
+
assert(incoming.hp != 0);
|
|
151
|
+
assert(slot != 1 or initial);
|
|
152
|
+
|
|
153
|
+
const out = side.pokemon[0].position;
|
|
154
|
+
side.pokemon[0].position = side.pokemon[slot - 1].position;
|
|
155
|
+
side.pokemon[slot - 1].position = out;
|
|
156
|
+
|
|
157
|
+
side.last_used_move = .None;
|
|
158
|
+
foe.active.volatiles.dirty = true;
|
|
159
|
+
|
|
160
|
+
active.volatiles = .{};
|
|
161
|
+
active.stats = incoming.stats;
|
|
162
|
+
active.moves = incoming.moves;
|
|
163
|
+
active.boosts = .{};
|
|
164
|
+
active.types = incoming.types;
|
|
165
|
+
active.species = incoming.species;
|
|
166
|
+
|
|
167
|
+
statusModify(incoming.status, &active.stats);
|
|
168
|
+
|
|
169
|
+
try options.log.switched(battle.active(player), incoming);
|
|
170
|
+
// TODO: options.chance.switched(player, side.order[0], out);
|
|
171
|
+
|
|
172
|
+
if (side.conditions.Spikes and !active.types.includes(.Flying)) {
|
|
173
|
+
incoming.hp -|= @max(incoming.stats.hp / 8, 1);
|
|
174
|
+
try options.log.damage(battle.active(player), incoming, .Spikes);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
fn turnOrder(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Player {
|
|
179
|
+
assert(c1.type != .Pass or c2.type != .Pass);
|
|
180
|
+
|
|
181
|
+
if (c1.type == .Pass) return .P2;
|
|
182
|
+
if (c2.type == .Pass) return .P1;
|
|
183
|
+
|
|
184
|
+
// Pokémon Showdown always rolls for Quick Claw every turn, even if no Pokémon hold Quick Claw
|
|
185
|
+
// TODO: add to pending and only commit roll on Pokémon Showdown if roll is used
|
|
186
|
+
const qkc = showdown and try Rolls.quickClaw(battle, .P1, options);
|
|
187
|
+
|
|
188
|
+
if ((c1.type == .Switch) != (c2.type == .Switch)) return if (c1.type == .Switch) .P1 else .P2;
|
|
189
|
+
|
|
190
|
+
// https://www.smogon.com/forums/threads/adv-switch-priority.3622189/
|
|
191
|
+
// > In Gen 1 it's irrelevant [which player switches first] because switches happen instantly on
|
|
192
|
+
// > your own screen without waiting for the other player's choice (and their choice will appear
|
|
193
|
+
// > to happen first for them too, unless they attacked in which case your switch happens first)
|
|
194
|
+
// A cartridge-compatible implemention must not advance the RNG so we simply default to P1
|
|
195
|
+
const double_switch = c1.type == .Switch and c2.type == .Switch;
|
|
196
|
+
if (!showdown and double_switch) return .P1;
|
|
197
|
+
|
|
198
|
+
const m1 = .Pound; // TODO battle.side(.P1).last_selected_move;
|
|
199
|
+
const m2 = .Pound; // TODO battle.side(.P2).last_selected_move;
|
|
200
|
+
if (!showdown or !double_switch) {
|
|
201
|
+
const pri1 = Move.get(m1).priority;
|
|
202
|
+
const pri2 = Move.get(m2).priority;
|
|
203
|
+
if (pri1 != pri2) return if (pri1 > pri2) .P1 else .P2;
|
|
204
|
+
|
|
205
|
+
const qkc1 = battle.side(.P1).stored().item == .QuickClaw;
|
|
206
|
+
const qkc2 = battle.side(.P2).stored().item == .QuickClaw;
|
|
207
|
+
if (!showdown and qkc1 and qkc2) {
|
|
208
|
+
// If both Pokémon have Quick Claw the cartridge checks them in order and exits early if
|
|
209
|
+
// the first one procs, meaning the host has a slightly higher chance of going first
|
|
210
|
+
if (try Rolls.quickClaw(battle, .P1, options)) return .P1;
|
|
211
|
+
if (try Rolls.quickClaw(battle, .P2, options)) return .P2;
|
|
212
|
+
} else if (qkc1) {
|
|
213
|
+
const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P1, options);
|
|
214
|
+
if (proc) return .P1;
|
|
215
|
+
} else if (qkc2) {
|
|
216
|
+
const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P2, options);
|
|
217
|
+
if (proc) return .P2;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const spe1 = battle.side(.P1).active.stats.spe;
|
|
222
|
+
const spe2 = battle.side(.P2).active.stats.spe;
|
|
223
|
+
if (spe1 == spe2) {
|
|
224
|
+
// Pokémon Showdown's beforeTurnCallback shenanigans
|
|
225
|
+
if (showdown and !double_switch and hasCallback(m1) and hasCallback(m2)) {
|
|
226
|
+
battle.rng.advance(1);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const p1 = try Rolls.speedTie(battle, options);
|
|
230
|
+
if (!showdown) return if (p1) .P1 else .P2;
|
|
231
|
+
|
|
232
|
+
// Pokémon Showdown's "lockedmove" volatile's onBeforeTurn uses BattleQueue#changeAction,
|
|
233
|
+
// meaning that if a side is locked into a thrashing move and wins the speed tie, it
|
|
234
|
+
// actually uses its priority to simply insert its actual changed action into the queue,
|
|
235
|
+
// causing it to then execute *after* the side which should go second...
|
|
236
|
+
const t1 = battle.side(.P1).active.volatiles.Thrashing;
|
|
237
|
+
const t2 = battle.side(.P2).active.volatiles.Thrashing;
|
|
238
|
+
// If *both* sides are thrashing it really should be another speed tie, but we've patched
|
|
239
|
+
// that out and enforce host ordering of events, so P1 just goes first regardless of who
|
|
240
|
+
// won the original coin flip
|
|
241
|
+
if (t1 and t2) return .P1;
|
|
242
|
+
return if (p1) if (t1 and !t2) .P2 else .P1 else if (t2 and !t1) .P1 else .P2;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return if (spe1 > spe2) .P1 else .P2;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
fn hasCallback(m: Move) bool {
|
|
249
|
+
return switch (m) {
|
|
250
|
+
.Counter, .MirrorCoat, .Pursuit => true,
|
|
251
|
+
else => false,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
fn doTurn(
|
|
256
|
+
battle: anytype,
|
|
257
|
+
player: Player,
|
|
258
|
+
player_choice: Choice,
|
|
259
|
+
foe_player: Player,
|
|
260
|
+
foe_choice: Choice,
|
|
261
|
+
options: anytype,
|
|
262
|
+
) !?Result {
|
|
263
|
+
assert(player_choice.type != .Pass);
|
|
264
|
+
|
|
265
|
+
const volatiles = &battle.side(player).active.volatiles;
|
|
266
|
+
const foe_volatiles = &battle.foe(player).active.volatiles;
|
|
267
|
+
|
|
268
|
+
volatiles.DestinyBond = false;
|
|
269
|
+
if (try executeMove(battle, player, player_choice, options)) |r| return r;
|
|
270
|
+
foe_volatiles.Protect = false;
|
|
271
|
+
foe_volatiles.Endure = false;
|
|
272
|
+
foe_volatiles.DestinyBond = false;
|
|
273
|
+
|
|
274
|
+
// foe_volatiles.DestinyBond = false;
|
|
275
|
+
if (try executeMove(battle, foe_player, foe_choice, options)) |r| return r;
|
|
276
|
+
volatiles.Protect = false;
|
|
277
|
+
volatiles.Endure = false;
|
|
278
|
+
volatiles.DestinyBond = false;
|
|
279
|
+
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
fn executeMove(battle: anytype, player: Player, choice: Choice, options: anytype) !?Result {
|
|
284
|
+
if (choice.type == .Switch) {
|
|
285
|
+
try switchIn(battle, player, choice.data, false, options);
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
assert(choice.type == .Move);
|
|
290
|
+
var mslot: u4 = @intCast(choice.data);
|
|
291
|
+
|
|
292
|
+
// const move = Move.get(side.last_selected_move);
|
|
293
|
+
|
|
294
|
+
// CheckTurn
|
|
295
|
+
|
|
296
|
+
return doMove(battle, player, options, mslot);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
fn decrementPP(side: *Side, mslot: u4) void {
|
|
300
|
+
var active = &side.active;
|
|
301
|
+
const volatiles = &active.volatiles;
|
|
302
|
+
|
|
303
|
+
// TODO assert(side.last_selected_move != .Struggle);
|
|
304
|
+
assert(!volatiles.Charging and !volatiles.BeatUp and !volatiles.Thrashing and !volatiles.Bide);
|
|
305
|
+
|
|
306
|
+
assert(active.move(mslot).pp > 0);
|
|
307
|
+
active.move(mslot).pp = @as(u6, @intCast(active.move(mslot).pp)) -% 1;
|
|
308
|
+
if (volatiles.Transform) return;
|
|
309
|
+
|
|
310
|
+
// FIXME mimic
|
|
311
|
+
|
|
312
|
+
assert(side.stored().move(mslot).pp > 0);
|
|
313
|
+
side.stored().move(mslot).pp = @as(u6, @intCast(side.stored().move(mslot).pp)) -% 1;
|
|
314
|
+
assert(active.move(mslot).pp == side.stored().move(mslot).pp);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
fn doMove(battle: anytype, player: Player, options: anytype, mslot: u4) !?Result {
|
|
318
|
+
_ = options;
|
|
319
|
+
var side = battle.side(player);
|
|
320
|
+
const move = Move.get(.Teleport); // side.last_selected_move
|
|
321
|
+
switch (move.effect) {
|
|
322
|
+
.Teleport => {
|
|
323
|
+
// side.useMove();
|
|
324
|
+
decrementPP(side, mslot);
|
|
325
|
+
},
|
|
326
|
+
else => @panic("TODO"),
|
|
327
|
+
}
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
fn endTurn(battle: anytype, options: anytype) @TypeOf(options.log).Error!Result {
|
|
332
|
+
battle.turn += 1;
|
|
333
|
+
|
|
334
|
+
if (showdown and battle.turn >= 1000) {
|
|
335
|
+
try options.log.tie();
|
|
336
|
+
return Result.Tie;
|
|
337
|
+
} else if (battle.turn >= 65535) {
|
|
338
|
+
return Result.Error;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
try options.log.turn(battle.turn);
|
|
42
342
|
|
|
43
343
|
return Result.Default;
|
|
44
344
|
}
|
|
45
345
|
|
|
346
|
+
pub const Effects = struct {
|
|
347
|
+
fn teleport(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
|
|
348
|
+
_ = battle;
|
|
349
|
+
_ = player;
|
|
350
|
+
_ = move;
|
|
351
|
+
_ = options;
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
fn statusModify(status: u8, stats: *Stats(u16)) void {
|
|
356
|
+
if (Status.is(status, .PAR)) {
|
|
357
|
+
stats.spe = @max(stats.spe / 4, 1);
|
|
358
|
+
} else if (Status.is(status, .BRN)) {
|
|
359
|
+
stats.atk = @max(stats.atk / 2, 1);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
inline fn isForced(active: anytype) bool {
|
|
364
|
+
return active.volatiles.Recharging or active.volatiles.Rage or
|
|
365
|
+
active.volatiles.Thrashing or active.volatiles.Charging or
|
|
366
|
+
// FIXME TODO Underground or Flying?
|
|
367
|
+
active.volatiles.Underground or active.volatiles.Flying or
|
|
368
|
+
active.volatiles.Rollout;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
pub const Rolls = struct {
|
|
372
|
+
inline fn speedTie(battle: anytype, options: anytype) !bool {
|
|
373
|
+
const p1 = if (options.calc.overridden(.P1, .speed_tie)) |player|
|
|
374
|
+
player == .P1
|
|
375
|
+
else if (showdown)
|
|
376
|
+
battle.rng.range(u8, 0, 2) == 0
|
|
377
|
+
else
|
|
378
|
+
battle.rng.next() < Gen12.percent(50) + 1;
|
|
379
|
+
|
|
380
|
+
try options.chance.speedTie(p1);
|
|
381
|
+
return p1;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
inline fn quickClaw(battle: anytype, player: Player, options: anytype) !bool {
|
|
385
|
+
const qkc = if (options.calc.overridden(player, .quick_claw)) |val|
|
|
386
|
+
val == .true
|
|
387
|
+
else if (showdown)
|
|
388
|
+
battle.rng.chance(u8, 60, 256)
|
|
389
|
+
else
|
|
390
|
+
battle.rng.next() < 60;
|
|
391
|
+
|
|
392
|
+
try options.chance.quickClaw(player, qkc);
|
|
393
|
+
return qkc;
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
test "RNG agreement" {
|
|
398
|
+
if (!showdown) return;
|
|
399
|
+
var expected: [256]u32 = undefined;
|
|
400
|
+
for (0..expected.len) |i| {
|
|
401
|
+
expected[i] = @intCast(i * 0x1000000);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
var spe = rng.FixedRNG(2, expected.len){ .rolls = expected };
|
|
405
|
+
var qkc = rng.FixedRNG(2, expected.len){ .rolls = expected };
|
|
406
|
+
|
|
407
|
+
for (0..expected.len) |i| {
|
|
408
|
+
try expectEqual(spe.range(u8, 0, 2) == 0, i < Gen12.percent(50) + 1);
|
|
409
|
+
try expectEqual(qkc.chance(u8, 60, 256), i < 60);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
46
413
|
pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Choice) u8 {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
414
|
+
var n: u8 = 0;
|
|
415
|
+
switch (request) {
|
|
416
|
+
.Pass => {
|
|
417
|
+
out[n] = .{};
|
|
418
|
+
n += 1;
|
|
419
|
+
},
|
|
420
|
+
.Switch => {
|
|
421
|
+
const side = battle.side(player);
|
|
422
|
+
var slot: u4 = 2;
|
|
423
|
+
while (slot <= 6) : (slot += 1) {
|
|
424
|
+
const id = side.order[slot - 1];
|
|
425
|
+
if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
|
|
426
|
+
out[n] = .{ .type = .Switch, .data = slot };
|
|
427
|
+
n += 1;
|
|
428
|
+
}
|
|
429
|
+
if (n == 0) {
|
|
430
|
+
out[n] = .{};
|
|
431
|
+
n += 1;
|
|
432
|
+
}
|
|
433
|
+
},
|
|
434
|
+
.Move => {
|
|
435
|
+
const side = battle.side(player);
|
|
51
436
|
|
|
52
|
-
|
|
437
|
+
var active = &side.active;
|
|
438
|
+
const stored = side.stored();
|
|
439
|
+
|
|
440
|
+
// While players are not given any input options on the cartridge in these cases,
|
|
441
|
+
// Pokémon Showdown instead produces a list with a single move that must be chosen.
|
|
442
|
+
//
|
|
443
|
+
// Given that no input is allowed on the cartridge 'Pass' seems like it would be logical
|
|
444
|
+
// here when not in compatibility mode, but the engine needs to be able to differentiate
|
|
445
|
+
// between passing while waiting for an opponent's forced switch after fainting and
|
|
446
|
+
// passing due to being forced into using a move. Instead of introducing another option
|
|
447
|
+
// we simply repurpose Move with no move slot, even though pedantically this is not
|
|
448
|
+
// strictly correct as the player would not have been presented the option to move or
|
|
449
|
+
// switch at all.
|
|
450
|
+
if (isForced(active)) {
|
|
451
|
+
out[n] = .{ .type = .Move, .data = @intFromBool(showdown) };
|
|
452
|
+
n += 1;
|
|
453
|
+
return n;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
var slot: u4 = 2;
|
|
457
|
+
while (!active.volatiles.trapped and slot <= 6) : (slot += 1) {
|
|
458
|
+
const id = side.order[slot - 1];
|
|
459
|
+
if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
|
|
460
|
+
out[n] = .{ .type = .Switch, .data = slot };
|
|
461
|
+
n += 1;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const limited = active.volatiles.Bide or active.volatiles.bind > 0;
|
|
465
|
+
// On the cartridge, all of these happen after "FIGHT" (indicating you are not
|
|
466
|
+
// switching) but before you are allowed to select a move. Pokémon Showdown instead
|
|
467
|
+
// either disables all other moves in the case of limited or requires you to select a
|
|
468
|
+
// move normally if sleeping/frozen/bound.
|
|
469
|
+
if (!showdown and (limited or
|
|
470
|
+
Status.is(stored.status, .FRZ) or Status.is(stored.status, .SLP)))
|
|
471
|
+
{
|
|
472
|
+
out[n] = .{ .type = .Move, .data = 0 };
|
|
473
|
+
n += 1;
|
|
474
|
+
return n;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
slot = 1;
|
|
478
|
+
// Pokémon Showdown handles Bide and Binding moves by checking if the move in question
|
|
479
|
+
// is present in the Pokémon's moveset (which means moves called via Metronome / Mirror
|
|
480
|
+
// Move will not result in forcing the subsequent use unless the user also had the
|
|
481
|
+
// proc-ed move in their moveset) and disabling all other moves.
|
|
482
|
+
if (limited) {
|
|
483
|
+
assert(showdown);
|
|
484
|
+
assert(side.last_selected_move != .None);
|
|
485
|
+
while (slot <= 4) : (slot += 1) {
|
|
486
|
+
const m = active.moves[slot - 1];
|
|
487
|
+
if (m.id == .None) break;
|
|
488
|
+
if (m.id == if (active.volatiles.Bide) .Bide else side.last_selected_move) {
|
|
489
|
+
// Pokémon Showdown displays Struggle if limited to Bide but unable to pick
|
|
490
|
+
const struggle =
|
|
491
|
+
m.id == .Bide and (m.pp == 0 or active.volatiles.disabled_move == slot);
|
|
492
|
+
const s = if (struggle) 0 else slot;
|
|
493
|
+
out[n] = .{ .type = .Move, .data = s };
|
|
494
|
+
n += 1;
|
|
495
|
+
return n;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const before = n;
|
|
501
|
+
slot = 1;
|
|
502
|
+
while (slot <= 4) : (slot += 1) {
|
|
503
|
+
const m = active.moves[slot - 1];
|
|
504
|
+
if (m.id == .None) break;
|
|
505
|
+
if (m.pp == 0) continue;
|
|
506
|
+
if (active.volatiles.disabled_move == slot) continue;
|
|
507
|
+
out[n] = .{ .type = .Move, .data = slot };
|
|
508
|
+
n += 1;
|
|
509
|
+
}
|
|
510
|
+
// Struggle (Pokémon Showdown would use 'move 1' here)
|
|
511
|
+
if (n == before) {
|
|
512
|
+
out[n] = .{ .type = .Move, .data = 0 };
|
|
513
|
+
n += 1;
|
|
514
|
+
}
|
|
515
|
+
},
|
|
516
|
+
}
|
|
517
|
+
return n;
|
|
53
518
|
}
|
package/src/lib/pkmn.zig
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
///
|
|
1
|
+
/// Configured Options for the pkmn package.
|
|
2
2
|
pub const options = @import("common/options.zig");
|
|
3
3
|
/// Configures the behavior of the pkmn package.
|
|
4
4
|
pub const Options = options.Options;
|
|
@@ -46,9 +46,68 @@ pub const protocol = if (options.internal) struct {
|
|
|
46
46
|
pub const NULL = @import("common/protocol.zig").NULL;
|
|
47
47
|
};
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
pub usingnamespace @import("common/rational.zig");
|
|
50
|
+
|
|
51
|
+
/// Namespace for cross-generation battle-related types.
|
|
52
|
+
pub const battle = @import("common/battle.zig");
|
|
53
|
+
|
|
54
|
+
/// Namespace for Generation I Pokémon.
|
|
50
55
|
pub const gen1 = struct {
|
|
51
56
|
pub usingnamespace @import("gen1/data.zig");
|
|
57
|
+
/// Tracks chance actions and their associated probability during a
|
|
58
|
+
/// Generation I battle update when `options.chance` is enabled.
|
|
59
|
+
pub const Chance = @import("gen1/chance.zig").Chance;
|
|
60
|
+
/// Namespace for types associated with tracking Generation I Pokémon chance outcomes.
|
|
61
|
+
pub const chance = struct {
|
|
62
|
+
/// Actions taken by a hypothetical "chance player" that convey
|
|
63
|
+
/// information about which RNG events were observed during a Generation
|
|
64
|
+
/// I battle `update`. This can additionally be provided as input to the
|
|
65
|
+
/// `update` call via the `Calc` when `options.calc` is enabled to
|
|
66
|
+
/// override the normal behavior of the RNG in order to force specific
|
|
67
|
+
/// outcomes.
|
|
68
|
+
pub const Actions = @import("gen1/chance.zig").Actions;
|
|
69
|
+
/// Information about the RNG that was observed during a Generation I
|
|
70
|
+
/// battle `update` for a single player.
|
|
71
|
+
pub const Action = @import("gen1/chance.zig").Action;
|
|
72
|
+
/// TODO
|
|
73
|
+
pub const Durations = @import("gen1/chance.zig").Durations;
|
|
74
|
+
/// TODO
|
|
75
|
+
pub const Duration = @import("gen1/chance.zig").Duration;
|
|
76
|
+
/// Null object pattern implementation of Generation I `Chance` which
|
|
77
|
+
/// does nothing, though chance tracking should additionally be turned
|
|
78
|
+
/// off entirely via `options.chance`.
|
|
79
|
+
pub const NULL = @import("gen1/chance.zig").NULL;
|
|
80
|
+
};
|
|
81
|
+
/// Allows for forcing the value of specific RNG events during a Generation I battle `update`
|
|
82
|
+
/// via `overrides` and tracks `summaries` of information relevant to damage calculation.
|
|
83
|
+
pub const Calc = @import("gen1/calc.zig").Calc;
|
|
84
|
+
/// Namespace for types associated with supported Generation I Pokémon damage calc features.
|
|
85
|
+
pub const calc = if (options.internal) struct {
|
|
86
|
+
pub usingnamespace @import("gen1/calc.zig");
|
|
87
|
+
} else struct {
|
|
88
|
+
/// TODO
|
|
89
|
+
pub const Overrides = @import("gen1/calc.zig").Overrides;
|
|
90
|
+
/// Information relevant to damage calculation that occured during a Generation I
|
|
91
|
+
/// battle `update`.
|
|
92
|
+
pub const Summaries = @import("gen1/calc.zig").Summaries;
|
|
93
|
+
/// Information relevant to damage calculation that occured during a Generation I
|
|
94
|
+
/// battle `update` for a single player.
|
|
95
|
+
pub const Summary = @import("gen1/calc.zig").Summary;
|
|
96
|
+
/// Null object pattern implementation of Generation I `Calc` which does nothing,
|
|
97
|
+
/// though damage calculator support should additionally be turned off
|
|
98
|
+
/// entirely via `options.calc`.
|
|
99
|
+
pub const NULL = @import("gen1/calc.zig").NULL;
|
|
100
|
+
};
|
|
52
101
|
/// Provides helpers for initializing Generation I Pokémon battles.
|
|
53
102
|
pub const helpers = @import("gen1/helpers.zig");
|
|
54
103
|
};
|
|
104
|
+
|
|
105
|
+
// Internal APIs used by other pkmn libraries, not actually part of the public API.
|
|
106
|
+
// NOTE: `pub usingnamespace struct { ... }` here results in a (false?) dependency loop
|
|
107
|
+
pub const js = if (options.internal) struct {
|
|
108
|
+
pub usingnamespace @import("common/js.zig");
|
|
109
|
+
} else struct {};
|
|
110
|
+
pub const bindings = if (options.internal) struct {
|
|
111
|
+
pub const node = @import("binding/node.zig");
|
|
112
|
+
pub const wasm = @import("binding/wasm.zig");
|
|
113
|
+
} else struct {};
|