shithub: pokecrystal

Download patch

ref: 9ab9088a9f264c8b032ecffe18389135714d280b
parent: 6220200f0f223a4077fcc716c105c01f3e1d3691
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Sun Nov 1 08:42:12 EST 2020

Identify wLinkBattleRNPreamble

--- a/constants/serial_constants.asm
+++ b/constants/serial_constants.asm
@@ -30,6 +30,8 @@
 SERIAL_PATCH_LIST_PART_TERMINATOR EQU $ff
 
 SERIAL_PREAMBLE_LENGTH EQU 6
+SERIAL_RN_PREAMBLE_LENGTH EQU 7
+SERIAL_RNS_LENGTH EQU 10
 
 ; timeout duration after exchanging a byte
 SERIAL_LINK_BYTE_TIMEOUT EQU $5000
--- a/engine/link/link.asm
+++ b/engine/link/link.asm
@@ -73,9 +73,9 @@
 	ldh [rIF], a
 	ld a, 1 << SERIAL
 	ldh [rIE], a
-	ld hl, wd1f3
+	ld hl, wLinkBattleRNPreamble
 	ld de, wEnemyMonSpecies
-	ld bc, $11
+	ld bc, SERIAL_RN_PREAMBLE_LENGTH + SERIAL_RNS_LENGTH
 	call Serial_ExchangeBytes
 	ld a, SERIAL_NO_DATA_BYTE
 	ld [de], a
@@ -87,7 +87,7 @@
 	ld [de], a
 	ld hl, wLink_c608
 	ld de, wTrademons
-	ld bc, wTrademons - wLink_c608
+	ld bc, 200
 	call Serial_ExchangeBytes
 	xor a
 	ldh [rIF], a
@@ -217,9 +217,9 @@
 	ldh [rIF], a
 	ld a, 1 << SERIAL
 	ldh [rIE], a
-	ld hl, wd1f3
+	ld hl, wLinkBattleRNPreamble
 	ld de, wEnemyMonSpecies
-	ld bc, $11
+	ld bc, SERIAL_RN_PREAMBLE_LENGTH + SERIAL_RNS_LENGTH
 	call Serial_ExchangeBytes
 	ld a, SERIAL_NO_DATA_BYTE
 	ld [de], a
@@ -231,7 +231,7 @@
 	ld [de], a
 	ld hl, wLink_c608
 	ld de, wTrademons
-	ld bc, wTrademons - wLink_c608
+	ld bc, 200
 	call Serial_ExchangeBytes
 	ld a, [wLinkMode]
 	cp LINK_TRADECENTER
@@ -561,36 +561,41 @@
 	ret
 
 FixDataForLinkTransfer:
-	ld hl, wd1f3
+	ld hl, wLinkBattleRNPreamble
 	ld a, SERIAL_PREAMBLE_BYTE
-	ld b, wLinkBattleRNs - wd1f3
-.loop1
+	ld b, SERIAL_RN_PREAMBLE_LENGTH
+.preamble_loop
 	ld [hli], a
 	dec b
-	jr nz, .loop1
-	ld b, wTempEnemyMonSpecies - wLinkBattleRNs
-.loop2
+	jr nz, .preamble_loop
+
+	assert wLinkBattleRNPreamble + SERIAL_RN_PREAMBLE_LENGTH == wLinkBattleRNs
+	ld b, SERIAL_RNS_LENGTH
+.rn_loop
 	call Random
 	cp SERIAL_PREAMBLE_BYTE
-	jr nc, .loop2
+	jr nc, .rn_loop
 	ld [hli], a
 	dec b
-	jr nz, .loop2
+	jr nz, .rn_loop
+
 	ld hl, wLink_c608
 	ld a, SERIAL_PREAMBLE_BYTE
 	ld [hli], a
 	ld [hli], a
 	ld [hli], a
-	ld b, $c8
+
+	ld b, 200
 	xor a
-.loop3
+.loop1
 	ld [hli], a
 	dec b
-	jr nz, .loop3
+	jr nz, .loop1
+
 	ld hl, wTimeCapsulePlayerData - 1 + PARTY_LENGTH
 	ld de, wc612
 	lb bc, 0, 0
-.loop4
+.loop2
 	inc c
 	ld a, c
 	cp SERIAL_PREAMBLE_BYTE
@@ -613,12 +618,12 @@
 	inc hl
 	ld a, [hl]
 	cp SERIAL_NO_DATA_BYTE
-	jr nz, .loop4
+	jr nz, .loop2
 	ld a, c
 	ld [de], a
 	inc de
 	ld [hl], SERIAL_PATCH_LIST_PART_TERMINATOR
-	jr .loop4
+	jr .loop2
 
 .next1
 	ld a, SERIAL_PATCH_LIST_PART_TERMINATOR
@@ -625,7 +630,7 @@
 	ld [de], a
 	inc de
 	lb bc, 1, 0
-	jr .loop4
+	jr .loop2
 
 .done
 	ld a, SERIAL_PATCH_LIST_PART_TERMINATOR
--- a/wram.asm
+++ b/wram.asm
@@ -2264,6 +2264,12 @@
 wEvolutionCanceled:: db
 
 NEXTU
+; link
+	ds 9
+wLinkBattleRNPreamble:: ds SERIAL_RN_PREAMBLE_LENGTH
+wLinkBattleRNs:: ds SERIAL_RNS_LENGTH
+
+NEXTU
 ; mobile
 	ds 2
 wd1ec:: ds 1
@@ -2273,7 +2279,7 @@
 wd1f0:: ds 1
 wd1f1:: ds 1
 wd1f2:: ds 1
-wd1f3:: ds 4
+wd1f3:: ds 1
 
 NEXTU
 ; miscellaneous
@@ -2283,10 +2289,6 @@
 	ds 3
 wCurEnemyItem:: db
 ENDU
-
-	ds 3
-
-wLinkBattleRNs:: ds 10
 
 wTempEnemyMonSpecies::  db
 wTempBattleMonSpecies:: db