diff options
| author | aaaaaa123456789 <aaaaaa123456789@acidch.at> | 2021-05-30 06:15:08 -0300 |
|---|---|---|
| committer | aaaaaa123456789 <aaaaaa123456789@acidch.at> | 2021-05-30 06:15:08 -0300 |
| commit | 44436ec88a6a8941d83ee1a813bde686ad4275a4 (patch) | |
| tree | 0896491d490d1f0a4f76414d166fe3ade0f48eab | |
| parent | 8ec6f6fb0e6e8a425f228b3fa010f245ce7ba0fc (diff) | |
Cleanup and code improvements
| -rw-r--r-- | Smashing-rocks-has-a-chance-to-contain-items.md | 21 |
1 files changed, 7 insertions, 14 deletions
diff --git a/Smashing-rocks-has-a-chance-to-contain-items.md b/Smashing-rocks-has-a-chance-to-contain-items.md index 8926824..53a25bd 100644 --- a/Smashing-rocks-has-a-chance-to-contain-items.md +++ b/Smashing-rocks-has-a-chance-to-contain-items.md @@ -55,14 +55,13 @@ We have to define `RockItemEncounter`, and a suitable place to put it would be i +.loop + sub [hl] + jr c, .ok -+rept 2 + inc hl -+endr ++ inc hl + jr .loop ++ +.ok + ld a, [hli] -+ cp $ff -+ ld a, NO_ITEM ++ inc a + jr z, .done + ld a, [hli] +.done @@ -80,21 +79,15 @@ We have to define `RockItemEncounter`, and a suitable place to put it would be i + db 24, SOFT_SAND + db 48, PEARL + db 64, BRICK_PIECE -+ db 48, NO_ITEM + db -1 + ``` -For starters, `RockItemEncounter` will load the `.RockItems` table into `hl`, and then call `Random`. `Random` is what the game uses to generate a lot of random statistics and variables, a good example would be the stats and specific Pokemon of a wild encounter. - -It rolls a chance to obtain an item, and if true, then `Random` will pick an item from the `.RockItems` table. The numerical values next to each item on the table is the percentage chance the player will receive said item, in this scenario a `MAX_REVIVE` has a chance of 1%. - -If `Random` rolls and lands on `NO_ITEM`(48%), then no item will be given, and the script will jump to `.done`, and back to executing the rest of the `RockSmashScript`. - -If `Random` rolls and lands on any item in the table, it loads it into memory and heads back to the `.no_battle` part of `RockSmashScript`, giving the player whatever item `Random` found. - - +For starters, `RockItemEncounter` will load the `.RockItems` table into `hl`, and then call `Random`, which will load a random byte (i.e., a value between 0 and 255) in `a`. With this random value generated, it performs comparisons against the probabilities encoded in the `.RockItems` table, picking an item with the listed odds (out of 256: in the example, there's a 1 in 256 chance of getting a Max Revive, and a 64 in 256 (or 1 in 4) chance of getting a Brick Piece). +Since the probabilities in the table add up to less than 256, the `db -1` line at the end indicates the end of the table. If this line is reached, the function will load a 0 (which represents `NO_ITEM`), indicating that the rocks didn't contain an item. If this is the case, the `iffalse .no_item` line in the script will skip giving an item to the player. Otherwise, the selected item, which will have already been loaded into the script variable (`wScriptVar`) by the function, will be awarded to the player by the script. And there you have it, the player can now find items within smashed rocks! You can add your own items and chances by editing the `.RockItems` table in `checkhiddenitems.asm`, or even add something more complex like the Fossils/Old Amber from RBY. +Note that the probabilities can only add up to 256 or less. (If they add up to 256 exactly, the final `db -1` is not needed, but it can be left in place just in case.) If they add up to more than 256, the last entries in the table will be inaccessible. + 
\ No newline at end of file |
