@pkmn/engine 0.1.0-dev.eaae4aa6 → 0.1.0-dev.f3aab252

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 (41) hide show
  1. package/LICENSE +0 -2
  2. package/README.md +15 -11
  3. package/build/pkg/addon.js +6 -7
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data.d.ts +2 -3
  6. package/build/pkg/index.d.ts +1 -1
  7. package/build/pkg/index.js.map +1 -1
  8. package/build/pkg/protocol.js +4 -4
  9. package/build/pkg/protocol.js.map +1 -1
  10. package/build/test/display.js +8 -3
  11. package/build/test/display.js.map +1 -1
  12. package/build/tools/debug.js +2 -3
  13. package/build/tools/debug.js.map +1 -1
  14. package/build.zig +187 -72
  15. package/package.json +18 -16
  16. package/src/bin/install-pkmn-engine +17 -7
  17. package/src/lib/common/array.zig +77 -0
  18. package/src/lib/common/data.zig +8 -8
  19. package/src/lib/common/debug.zig +2 -2
  20. package/src/lib/common/js.zig +4 -2
  21. package/src/lib/common/optional.zig +13 -9
  22. package/src/lib/common/options.zig +2 -0
  23. package/src/lib/common/protocol.zig +51 -44
  24. package/src/lib/common/rational.zig +114 -24
  25. package/src/lib/common/rng.zig +10 -10
  26. package/src/lib/common/util.zig +50 -1
  27. package/src/lib/gen1/calc.zig +506 -128
  28. package/src/lib/gen1/chance.zig +383 -276
  29. package/src/lib/gen1/data/species.zig +1 -1
  30. package/src/lib/gen1/data.zig +31 -30
  31. package/src/lib/gen1/helpers.zig +95 -538
  32. package/src/lib/gen1/mechanics.zig +265 -162
  33. package/src/lib/gen2/calc.zig +34 -16
  34. package/src/lib/gen2/chance.zig +90 -502
  35. package/src/lib/gen2/data.zig +15 -14
  36. package/src/lib/gen2/helpers.zig +41 -13
  37. package/src/lib/gen2/mechanics.zig +158 -103
  38. package/src/lib/pkmn.zig +10 -16
  39. package/src/test/display/pkmn.html.tmpl +5 -5
  40. package/src/lib/gen1/README.md +0 -894
  41. package/src/lib/gen2/README.md +0 -329
@@ -5,6 +5,8 @@ const pkmn = @import("../pkmn.zig");
5
5
  const common = @import("../common/data.zig");
6
6
  const util = @import("../common/util.zig");
7
7
 
8
+ const Optional = @import("../common/optional.zig").Optional;
9
+
8
10
  const chance = @import("chance.zig");
9
11
 
10
12
  const assert = std.debug.assert;
@@ -14,9 +16,15 @@ const enabled = pkmn.options.calc;
14
16
  const Player = common.Player;
15
17
 
16
18
  const PointerType = util.PointerType;
19
+ const isPointerTo = util.isPointerTo;
17
20
 
18
21
  const Actions = chance.Actions;
19
22
  const Action = chance.Action;
23
+ const Criticals = chance.Criticals;
24
+ const Damages = chance.Damages;
25
+
26
+ const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
27
+ const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
20
28
 
21
29
  /// Information relevant to damage calculation that occured during a Generation II battle `update`.
22
30
  pub const Summaries = extern struct {
@@ -31,7 +39,7 @@ pub const Summaries = extern struct {
31
39
 
32
40
  /// Returns the `Summary` for the given `player`.
33
41
  pub fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Summary) {
34
- assert(@typeInfo(@TypeOf(self)).Pointer.child == Summaries);
42
+ assert(isPointerTo(self, Summaries));
35
43
  return if (player == .P1) &self.p1 else &self.p2;
36
44
  }
37
45
  };
@@ -65,33 +73,34 @@ pub const Summary = extern struct {
65
73
  }
66
74
  };
67
75
 
68
- /// TODO
69
- pub const Overrides = extern struct {
70
- /// TODO
71
- actions: Actions = .{},
72
- };
73
-
74
76
  /// Allows for forcing the value of specific RNG events during a Generation II battle `update` via
75
77
  /// `overrides` and tracks `summaries` of information relevant to damage calculation.
76
78
  pub const Calc = struct {
77
79
  /// Overrides the normal behavior of the RNG during an `update` to force specific outcomes.
78
- overrides: Overrides = .{},
80
+ overrides: Actions = .{},
79
81
  /// Information relevant to damage calculation.
80
82
  summaries: Summaries = .{},
81
83
 
82
84
  pub fn overridden(
83
85
  self: Calc,
84
86
  player: Player,
87
+ hit: u3,
85
88
  comptime field: Action.Field,
86
- ) ?std.meta.FieldType(Action, field) {
89
+ ) ReturnType(field) {
87
90
  if (!enabled) return null;
88
91
 
89
- const overrides =
90
- if (player == .P1) self.overrides.actions.p1 else self.overrides.actions.p2;
91
- const val = @field(overrides, @tagName(field));
92
+ const val = switch (field) {
93
+ .damages => Damages.get(self.overrides.get(player).damages, hit),
94
+ .critical_hits => Criticals.get(self.overrides.get(player).critical_hits, hit),
95
+ else => val: {
96
+ assert(hit == 0);
97
+ break :val @field(self.overrides.get(player), @tagName(field));
98
+ },
99
+ };
100
+
92
101
  return if (switch (@typeInfo(@TypeOf(val))) {
93
- .Enum => val != .None,
94
- .Int => val != 0,
102
+ Enum => val != .None,
103
+ Int => val != 0,
95
104
  else => unreachable,
96
105
  }) val else null;
97
106
  }
@@ -123,9 +132,10 @@ const Null = struct {
123
132
  pub fn overridden(
124
133
  self: Null,
125
134
  player: Player,
135
+ hit: u3,
126
136
  comptime field: Action.Field,
127
- ) ?std.meta.FieldType(Action, field) {
128
- _ = .{ self, player };
137
+ ) ReturnType(field) {
138
+ _ = .{ self, player, hit };
129
139
  return null;
130
140
  }
131
141
 
@@ -141,3 +151,11 @@ const Null = struct {
141
151
  _ = .{ self, player };
142
152
  }
143
153
  };
154
+
155
+ fn ReturnType(comptime field: Action.Field) type {
156
+ return ?(switch (field) {
157
+ .damages => u8,
158
+ .critical_hits => Optional(bool),
159
+ else => std.meta.FieldType(Action, field),
160
+ });
161
+ }