@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.
Files changed (40) hide show
  1. package/README.md +55 -44
  2. package/build/pkg/addon.js +3 -2
  3. package/build/pkg/addon.js.map +1 -1
  4. package/build/pkg/data/layout.json +24 -2
  5. package/build/pkg/gen1.js +13 -16
  6. package/build/pkg/gen1.js.map +1 -1
  7. package/build/pkg/index.js.map +1 -1
  8. package/build.zig +86 -62
  9. package/package.json +25 -16
  10. package/src/bin/install-pkmn-engine +10 -5
  11. package/src/lib/binding/node.zig +45 -140
  12. package/src/lib/binding/wasm.zig +4 -2
  13. package/src/lib/common/battle.zig +17 -0
  14. package/src/lib/common/data.zig +14 -26
  15. package/src/lib/common/debug.zig +20 -11
  16. package/src/lib/common/js.zig +118 -0
  17. package/src/lib/common/optional.zig +60 -0
  18. package/src/lib/common/options.zig +9 -0
  19. package/src/lib/common/protocol.zig +277 -159
  20. package/src/lib/common/rational.zig +353 -0
  21. package/src/lib/common/rng.zig +11 -11
  22. package/src/lib/common/util.zig +12 -0
  23. package/src/lib/gen1/README.md +259 -177
  24. package/src/lib/gen1/calc.zig +426 -0
  25. package/src/lib/gen1/chance.zig +1036 -0
  26. package/src/lib/gen1/data/moves.zig +178 -180
  27. package/src/lib/gen1/data/species.zig +2 -2
  28. package/src/lib/gen1/data/types.zig +3 -3
  29. package/src/lib/gen1/data.zig +66 -54
  30. package/src/lib/gen1/helpers.zig +589 -18
  31. package/src/lib/gen1/mechanics.zig +791 -508
  32. package/src/lib/gen2/README.md +87 -13
  33. package/src/lib/gen2/data/items.zig +3 -3
  34. package/src/lib/gen2/data/moves.zig +624 -563
  35. package/src/lib/gen2/data/species.zig +1 -1
  36. package/src/lib/gen2/data/types.zig +3 -3
  37. package/src/lib/gen2/data.zig +100 -70
  38. package/src/lib/gen2/helpers.zig +16 -22
  39. package/src/lib/gen2/mechanics.zig +714 -10
  40. package/src/lib/pkmn.zig +61 -2
@@ -54,13 +54,12 @@ The following information is required to simulate a Generation II Pokémon battl
54
54
  | | | |
55
55
  | `active.last_used_move` | `PlayerUsedMove` | `side.lastMove` |
56
56
 
57
- - Pokémon Showdown does not implement the correct Generation II RNG and as such its `seed` is
57
+ - Pokémon Showdown doesn't implement the correct Generation II RNG and as such its `seed` is
58
58
  different
59
- - `battle.turn` only needs to be tracked in order to be compatible with the Pokémon Showdown
60
- protocol
59
+ - `battle.turn` only needs to be tracked to be compatible with the Pokémon Showdown protocol
61
60
  - Battle results (win, lose, draw) or request states are communicated via the return code of
62
61
  `Battle.update`
63
- - Nicknames (`BattleMonNick`/`pokemon.name`) are not handled by the pkmn engine as they are expected
62
+ - Nicknames (`BattleMonNick`/`pokemon.name`) aren't handled by the pkmn engine as they're expected
64
63
  to be handled by driver code if required
65
64
 
66
65
  ==**TODO**==
@@ -77,9 +76,9 @@ between the data structure for the "active" Pokémon and its party members (see
77
76
 
78
77
  ### `Pokemon` / `ActivePokemon`
79
78
 
80
- Similar to the cartridge, in order to save space different information is stored depending on
81
- whether a [Pokémon](https://pkmn.cc/bulba/Pok%c3%a9mon_data_structure_%28Generation_II%29) is
82
- actively participating in battle vs. is switched out (pret's [`battle_struct` vs.
79
+ Similar to the cartridge, to save space different information is stored depending on whether a
80
+ [Pokémon](https://pkmn.cc/bulba/Pok%c3%a9mon_data_structure_%28Generation_II%29) is actively
81
+ participating in battle vs. is switched out (pret's [`battle_struct` vs.
83
82
  `party_struct`](https://pkmn.cc/pokecrystal/macros/ram.asm)). In Pokémon Showdown, all the Pokemon
84
83
  are represented by the same
85
84
  [`Pokemon`](https://github.com/smogon/pokemon-showdown/blob/master/sim/pokemon.ts) class, and static
@@ -93,14 +92,14 @@ A `MoveSlot` is a data-type for a `(move, current pp)` pair. A pared down versio
93
92
  Showdown's `Pokemon#moveSlot`, it also stores data from the cartridge's `battle_struct::Move` macro
94
93
  and can be used to replace the `PlayerMove*` data. Move PP is stored as a full byte instead of how
95
94
  the cartridge (`battle_struct::PP`) stores it (6 bits for current PP and the remaining 2 bits used
96
- to store the number of applied PP Ups). PP Up bits do not actually need to be stored on move slot
95
+ to store the number of applied PP Ups). PP Up bits don't actually need to be stored on move slot
97
96
  as max PP is never relevant in Generation II (the Mystery Berry always increases PP from 0 -> 5).
98
97
 
99
98
  #### `Status`
100
99
 
101
100
  Bitfield representation of a Pokémon's major [status
102
- condition](https://pkmn.cc/bulba/Status_condition), mirroring how it is stored on the cartridge. A
103
- value of `0x00` means that the Pokémon is not affected by any major status, otherwise the lower 3
101
+ condition](https://pkmn.cc/bulba/Status_condition), mirroring how it's stored on the cartridge. A
102
+ value of `0x00` means that the Pokémon isn't affected by any major status, otherwise the lower 3
104
103
  bits represent the remaining duration for Sleep. Other status are denoted by the presence of
105
104
  individual bits - at most one status should be set at any given time.
106
105
 
@@ -161,7 +160,7 @@ boosts should always range from `-6`...`6` instead of `1`...`13` as on the cartr
161
160
 
162
161
  `Moves` serves as an identifier for a unique [Pokémon move](https://pkmn.cc/bulba/Move) that can be
163
162
  used to retrieve a `Move` with information regarding base power, accuracy and type. As covered
164
- above, PP information isn't strictly necessary in Generation I, but fits neatly into the 4 bits of
163
+ earlier, PP information isn't strictly necessary in Generation I, but fits neatly into the 4 bits of
165
164
  padding after noticing that all PP values are multiples of 5. `Moves.None` exists as a special
166
165
  sentinel value to indicate `null`.
167
166
 
@@ -215,11 +214,86 @@ entropy](https://en.wikipedia.org/wiki/Entropy_(information_theory))) is as foll
215
214
  | **wrap** | 0...5 | 3 | | **perish song** | 0...4 | 3 |
216
215
  | **item** | TODO | 5? | | **gender** | 0..1 | 1 |
217
216
 
218
- From this we can determine the minimum bits required to store each data structure to determine how
219
- much overhead the representations above have after taking into consideration [alignment &
217
+ From this one can determine the minimum bits required to store each data structure to determine how
218
+ much overhead the preceding representations have after taking into consideration [alignment &
220
219
  padding](https://en.wikipedia.org/wiki/Data_structure_alignment) and
221
220
  [denormalization](https://en.wikipedia.org/wiki/Denormalization):
222
221
 
222
+ ## Details
223
+
224
+ | pkmn | Pokémon Crystal (pret) |
225
+ | ------------------------- | ------------------------------------------------- |
226
+ | `start` | `DoBattle` |
227
+ | `update` | `BattleTurn` |
228
+ | `selectMove` / `saveMove` | `CheckPlayerLockedIn` / `ParsePlayerAction` |
229
+ | `switchIn` | `SwitchPlayerMon` / `SendOutPlayerMon` |
230
+ | `turnOrder` | `DetermineMoveOrder` |
231
+ | `doTurn` | `DoTurn` |
232
+ | `executeMove` | `DoMove` |
233
+ | `beforeMove` | `CheckTurn` |
234
+ | `canMove` | |
235
+ | `decrementPP` | |
236
+ | `doMove` | |
237
+ | `checkCriticalHit` | `BattleCommand_Critical` |
238
+ | `calcDamage` | `BattleCommand_DamageCalc` / `PlayerAttackDamage` |
239
+ | `adjustDamage` | `BattleCommand_Stab` |
240
+ | `randomizeDamage` | `BattleCommand_DamageVariation` |
241
+ | `specialDamage` | `BattleCommand_ConstantDamage` |
242
+ | `applyDamage` | `BattleCommand_ApplyDamage` |
243
+ | `checkHit` | `BattleCommand_CheckHit` |
244
+ | `checkFaint` | `CheckFaint` |
245
+ | `handleResidual` | `ResidualDamage` |
246
+ | `endTurn` | `EndTurn` |
247
+
248
+ DoBattle
249
+ BattleTurn
250
+ - HandleBerserkGene
251
+ - CheckPlayerLockedIn
252
+ - BattleMenu
253
+ - ParsePlayerAction
254
+ - DetermineMoveOrder
255
+ - CompareMovePriority
256
+ - GetMovePriority
257
+ - Battle_*
258
+ - Handle*Switch
259
+ - SwitchPlayerMon
260
+ - BreakAttraction
261
+ - SendOutPlayerMon
262
+ - SpikesDamage
263
+ - *Turn_EndOpponentProtectEndureDestinyBond
264
+ - EndUserDestinyBond
265
+ - Do*Turn
266
+ - EndOpponentProtectEndureDestinyBond
267
+ - ResidualDamage
268
+ - CheckFaint_*
269
+ - HandleBetweenTurnEffects
270
+ - HandleFutureSight
271
+ - HandleWeather
272
+ - HandleWrap
273
+ - HandlePerishSong
274
+ - HandleLeftovers
275
+ - HandleMysteryBerry
276
+ - HandleDefrost
277
+ - HandleSafeguard
278
+ - HandleScreens
279
+ - HandleStatBoostingHeldItems
280
+ - HandleHealingItems
281
+ - HandleEncore
282
+
283
+ DoTurn
284
+ DoMove = executeMove
285
+ CheckTurn = beforeMove
286
+ BattleCommand_Critical = checkCriticalHit
287
+ BattleCommand_Stab = adjustDamage
288
+ BattleCommand_DamageVariation = randomizeDamage
289
+ BattleCommand_CheckHit = checkHit
290
+ BattleCommand_ApplyDamage = applyDamage
291
+ BattleCommand_CheckFaint
292
+ BattleCommand_BuildOpponentRage = buildRage
293
+ BattleCommand_DamageCalc (PlayerAttackDamage / EnemyAttackDamage) = calcDamage
294
+ BattleCommand_ConstantDamage = specialDamage
295
+ DoEnemyDamage / DoPlayerDamage / DoSubstituteDamage
296
+
223
297
  ==**TODO**==
224
298
 
225
299
  ## Protocol
@@ -219,18 +219,18 @@ pub const Item = enum(u8) {
219
219
  pub inline fn boost(item: Item) ?Type {
220
220
  assert(item != .None);
221
221
  if (item == .PolkadotBow) return .Normal;
222
- return if (@enumToInt(item) <= 18) @intToEnum(Type, @enumToInt(item) - 1) else null;
222
+ return if (@intFromEnum(item) <= 18) @enumFromInt(@intFromEnum(item) - 1) else null;
223
223
  }
224
224
 
225
225
  /// Whether or not this item is a Berry.
226
226
  pub inline fn berry(item: Item) bool {
227
227
  assert(item != .None);
228
- return @enumToInt(item) > 30 and @enumToInt(item) <= 40;
228
+ return @intFromEnum(item) > 30 and @intFromEnum(item) <= 40;
229
229
  }
230
230
 
231
231
  /// Whether or not this item is Mail.
232
232
  pub inline fn mail(item: Item) bool {
233
233
  assert(item != .None);
234
- return @enumToInt(item) > 60 and @enumToInt(item) <= 70;
234
+ return @intFromEnum(item) > 60 and @intFromEnum(item) <= 70;
235
235
  }
236
236
  };