summaryrefslogtreecommitdiff
path: root/home
diff options
context:
space:
mode:
Diffstat (limited to 'home')
-rwxr-xr-xhome/items.asm8
-rw-r--r--home/random.asm69
-rwxr-xr-xhome/tables.asm27
3 files changed, 100 insertions, 4 deletions
diff --git a/home/items.asm b/home/items.asm
index 496b685..0edd72b 100755
--- a/home/items.asm
+++ b/home/items.asm
@@ -10,17 +10,17 @@ AddItemToInventory:: ; 3259
; function to add an item (in varying quantities) to the player's bag or PC box
; INPUT:
; HL = address of inventory (either wNumBagItems or wNumBoxItems)
-; [wcd76] = item ID
+; [wCurItem] = item ID
; [wItemQuantity] = item quantity
; sets carry flag if successful, unsets carry flag if unsuccessful
push bc
ldh a, [hROMBank]
push af
- ld a, BANK(AddItemToInventory_)
+ ld a, BANK(_ReceiveItem)
call Bankswitch
push hl
push de
- call AddItemToInventory_
+ call _ReceiveItem
pop de
pop hl
pop bc
@@ -41,7 +41,7 @@ GiveItem::
; Return carry on success.
ld a, b
ld [wce37], a
- ld [wcd76], a
+ ld [wCurItem], a
ld a, c
ld [wItemQuantity], a
ld hl, wNumBagItems
diff --git a/home/random.asm b/home/random.asm
new file mode 100644
index 0000000..5ca7c78
--- /dev/null
+++ b/home/random.asm
@@ -0,0 +1,69 @@
+include "constants.asm"
+
+if DEBUG
+SECTION "Random Number Generation", ROM0 [$3270]
+else
+SECTION "Random Number Generation", ROM0 [$3234]
+endc
+
+Random::
+; A simple hardware-based random number generator (RNG).
+
+; Two random numbers are generated by adding and subtracting
+; the divider to the respective values every time it's called.
+
+; The divider is a register that increments at a rate of 16384Hz.
+; For comparison, the Game Boy operates at a clock speed of 4.2MHz.
+
+; This implementation also takes the current scanline as read from rLY
+; and adds it nybble-swapped to the value of the divider. The unswapped
+; value of rLY is also added to the value that's subtracted.
+
+; Additionally, an equivalent function is executed in VBlank.
+
+; This leaves a with the value in hRandomSub.
+
+ push bc
+
+ ldh a, [rLY]
+ ld c, a
+ swap a
+ ld b, a
+ ldh a, [rDIV]
+ adc b
+ ld b, a
+ ldh a, [hRandomAdd]
+ adc b
+ ldh [hRandomAdd], a
+
+ ldh a, [rLY]
+ swap a
+ ld b, a
+ ldh a, [rDIV]
+ adc b
+ adc c
+ ld b, a
+ ldh a, [hRandomSub]
+ sbc b
+ ldh [hRandomSub], a
+
+ pop bc
+ ret
+
+BattleRandom::
+; _BattleRandom lives in another bank.
+
+; It handles all RNG calls in the battle engine, allowing
+; link battles to remain in sync using a shared PRNG.
+ ldh a, [hROMBank]
+ push af
+ ld a, BANK(_BattleRandom)
+ call Bankswitch
+
+ call _BattleRandom
+
+ ld [wPredefHL + 1], a
+ pop af
+ call Bankswitch
+ ld a, [wPredefHL + 1]
+ ret
diff --git a/home/tables.asm b/home/tables.asm
new file mode 100755
index 0000000..dc9cebe
--- /dev/null
+++ b/home/tables.asm
@@ -0,0 +1,27 @@
+INCLUDE "constants.asm"
+
+SECTION "FindItemInTable", ROM0[$35F8]
+
+; find value a from table hl with row length de
+; returns carry and row index b if successful
+FindItemInTable: ; 00:35F8
+ ld b, 0
+ ld c, a
+
+.loop
+ ld a, [hl]
+ cp -1
+ jr z, .fail
+ cp c
+ jr z, .success
+ inc b
+ add hl, de
+ jr .loop
+
+.fail
+ and a
+ ret
+
+.success
+ scf
+ ret