@pkmn/engine 0.1.0-dev.fb5bf955 → 0.1.0-dev.fb6653d6

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.
Files changed (85) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +115 -76
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +44 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +97 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +53 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +27 -5
  13. package/build/pkg/addon.js +42 -73
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data/ids.json +12 -12
  16. package/build/pkg/data/layout.json +40 -5
  17. package/build/pkg/data/protocol.json +84 -13
  18. package/build/pkg/data.d.ts +27 -5
  19. package/build/pkg/data.js.map +1 -1
  20. package/build/pkg/gen1.d.ts +16 -19
  21. package/build/pkg/gen1.js +54 -42
  22. package/build/pkg/gen1.js.map +1 -1
  23. package/build/pkg/index.d.ts +55 -103
  24. package/build/pkg/index.js +6 -63
  25. package/build/pkg/index.js.map +1 -1
  26. package/build/pkg/protocol.d.ts +24 -3
  27. package/build/pkg/protocol.js +127 -104
  28. package/build/pkg/protocol.js.map +1 -1
  29. package/build/tools/debug.js +36 -69
  30. package/build/tools/debug.js.map +1 -1
  31. package/build/tools/display/debug.jsx +141 -0
  32. package/build/tools/display/debug.jsx.map +1 -0
  33. package/build/tools/display/index.js +142 -0
  34. package/build/tools/display/index.js.map +1 -0
  35. package/build/tools/display/ui.jsx +349 -0
  36. package/build/tools/display/ui.jsx.map +1 -0
  37. package/build/tools/display/util.js +18 -0
  38. package/build/tools/display/util.js.map +1 -0
  39. package/build.zig +373 -152
  40. package/package.json +68 -120
  41. package/src/bin/install-pkmn-engine +41 -26
  42. package/src/bin/pkmn-debug +29 -6
  43. package/src/lib/bindings/node.zig +121 -0
  44. package/src/lib/bindings/wasm.zig +58 -0
  45. package/src/lib/common/array.zig +77 -0
  46. package/src/lib/common/battle.zig +17 -0
  47. package/src/lib/common/data.zig +19 -31
  48. package/src/lib/common/debug.zig +58 -17
  49. package/src/lib/common/js.zig +120 -0
  50. package/src/lib/common/optional.zig +71 -0
  51. package/src/lib/common/options.zig +18 -3
  52. package/src/lib/common/protocol.zig +947 -407
  53. package/src/lib/common/rational.zig +449 -0
  54. package/src/lib/common/rng.zig +31 -24
  55. package/src/lib/common/util.zig +61 -0
  56. package/src/lib/gen1/calc.zig +953 -0
  57. package/src/lib/gen1/chance.zig +1185 -0
  58. package/src/lib/gen1/data/moves.zig +203 -193
  59. package/src/lib/gen1/data/species.zig +6 -8
  60. package/src/lib/gen1/data/types.zig +11 -12
  61. package/src/lib/gen1/data.zig +100 -89
  62. package/src/lib/gen1/helpers.zig +139 -50
  63. package/src/lib/gen1/mechanics.zig +1344 -907
  64. package/src/lib/gen2/calc.zig +153 -0
  65. package/src/lib/gen2/chance.zig +902 -0
  66. package/src/lib/gen2/data/items.zig +15 -23
  67. package/src/lib/gen2/data/moves.zig +665 -573
  68. package/src/lib/gen2/data/species.zig +3 -5
  69. package/src/lib/gen2/data/types.zig +138 -36
  70. package/src/lib/gen2/data.zig +134 -99
  71. package/src/lib/gen2/generated.zig +801 -0
  72. package/src/lib/gen2/helpers.zig +59 -44
  73. package/src/lib/gen2/mechanics.zig +3582 -30
  74. package/src/lib/node.zig +18 -0
  75. package/src/lib/pkmn.zig +93 -12
  76. package/src/lib/wasm.zig +17 -0
  77. package/src/tools/display/dom.ts +96 -0
  78. package/src/tools/display/ui.css +354 -0
  79. package/build/test/display.js +0 -382
  80. package/build/test/display.js.map +0 -1
  81. package/src/lib/binding/node.zig +0 -229
  82. package/src/lib/binding/wasm.zig +0 -10
  83. package/src/lib/gen1/README.md +0 -784
  84. package/src/lib/gen2/README.md +0 -309
  85. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -0,0 +1,153 @@
1
+ const chance = @import("chance.zig");
2
+ const common = @import("../common/data.zig");
3
+ const Optional = @import("../common/optional.zig").Optional;
4
+ const pkmn = @import("../pkmn.zig");
5
+ const std = @import("std");
6
+ const util = @import("../common/util.zig");
7
+
8
+ const Action = chance.Action;
9
+ const Actions = chance.Actions;
10
+ const assert = std.debug.assert;
11
+ const Criticals = chance.Criticals;
12
+ const Damages = chance.Damages;
13
+ const enabled = pkmn.options.calc;
14
+ const isPointerTo = util.isPointerTo;
15
+ const Player = common.Player;
16
+ const PointerType = util.PointerType;
17
+
18
+ const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
19
+ const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
20
+
21
+ /// Information relevant to damage calculation that occured during a Generation II battle `update`.
22
+ pub const Summaries = extern struct {
23
+ /// Relevant information for Player 1.
24
+ p1: Summary = .{},
25
+ /// Relevant information for Player 2.
26
+ p2: Summary = .{},
27
+
28
+ comptime {
29
+ assert(@sizeOf(Summaries) == 12);
30
+ }
31
+
32
+ /// Returns the `Summary` for the given `player`.
33
+ pub fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Summary) {
34
+ assert(isPointerTo(self, Summaries));
35
+ return if (player == .P1) &self.p1 else &self.p2;
36
+ }
37
+ };
38
+
39
+ /// Information relevant to damage calculation that occured during a Generation II battle `update`
40
+ /// for a single player.
41
+ pub const Summary = extern struct {
42
+ /// The computed raw damage values.
43
+ damage: Damage = .{},
44
+
45
+ /// Intermediate raw damage values computed during a calculation.
46
+ pub const Damage = extern struct {
47
+ /// The base computed damage before the damage roll is applied.
48
+ base: u16 = 0,
49
+ /// The final computed damage that gets applied to the Pokémon. May exceed the target's HP
50
+ // (to determine the *actual* damage done compare the target's stored HP before and after).
51
+ final: u16 = 0,
52
+ /// Whether higher damage will saturate / result in the same outcome (e.g. additional damage
53
+ /// gets ignored due to it already breaking a Substitute or causing the target to faint).
54
+ capped: bool = false,
55
+
56
+ // NOTE: 15 bits padding
57
+
58
+ comptime {
59
+ assert(@sizeOf(Damage) == 6);
60
+ }
61
+ };
62
+
63
+ comptime {
64
+ assert(@sizeOf(Summary) == 6);
65
+ }
66
+ };
67
+
68
+ /// Allows for forcing the value of specific RNG events during a Generation II battle `update` via
69
+ /// `overrides` and tracks `summaries` of information relevant to damage calculation.
70
+ pub const Calc = struct {
71
+ /// Overrides the normal behavior of the RNG during an `update` to force specific outcomes.
72
+ overrides: Actions = .{},
73
+ /// Information relevant to damage calculation.
74
+ summaries: Summaries = .{},
75
+
76
+ pub fn overridden(
77
+ self: Calc,
78
+ player: Player,
79
+ hit: u3,
80
+ comptime field: Action.Field,
81
+ ) ReturnType(field) {
82
+ if (!enabled) return null;
83
+
84
+ const val = switch (field) {
85
+ .damages => Damages.get(self.overrides.get(player).damages, hit),
86
+ .critical_hits => Criticals.get(self.overrides.get(player).critical_hits, hit),
87
+ else => val: {
88
+ assert(hit == 0);
89
+ break :val @field(self.overrides.get(player), @tagName(field));
90
+ },
91
+ };
92
+
93
+ return if (switch (@typeInfo(@TypeOf(val))) {
94
+ Enum => val != .None,
95
+ Int => val != 0,
96
+ else => unreachable,
97
+ }) val else null;
98
+ }
99
+
100
+ pub fn base(self: *Calc, player: Player, val: u16) void {
101
+ if (!enabled) return;
102
+
103
+ self.summaries.get(player).damage.base = val;
104
+ }
105
+
106
+ pub fn final(self: *Calc, player: Player, val: u16) void {
107
+ if (!enabled) return;
108
+
109
+ self.summaries.get(player).damage.final = val;
110
+ }
111
+
112
+ pub fn capped(self: *Calc, player: Player) void {
113
+ if (!enabled) return;
114
+
115
+ self.summaries.get(player).damage.capped = true;
116
+ }
117
+ };
118
+
119
+ /// Null object pattern implementation of Generation II `Calc` which does nothing, though damage
120
+ /// calculator support should additionally be turned off entirely via `options.calc`.
121
+ pub const NULL: Null = .{};
122
+
123
+ const Null = struct {
124
+ pub fn overridden(
125
+ self: Null,
126
+ player: Player,
127
+ hit: u3,
128
+ comptime field: Action.Field,
129
+ ) ReturnType(field) {
130
+ _ = .{ self, player, hit };
131
+ return null;
132
+ }
133
+
134
+ pub fn base(self: Null, player: Player, val: u16) void {
135
+ _ = .{ self, player, val };
136
+ }
137
+
138
+ pub fn final(self: Null, player: Player, val: u16) void {
139
+ _ = .{ self, player, val };
140
+ }
141
+
142
+ pub fn capped(self: Null, player: Player) void {
143
+ _ = .{ self, player };
144
+ }
145
+ };
146
+
147
+ fn ReturnType(comptime field: Action.Field) type {
148
+ return ?(switch (field) {
149
+ .damages => u8,
150
+ .critical_hits => Optional(bool),
151
+ else => std.meta.FieldType(Action, field),
152
+ });
153
+ }