Skip to content

Out of Bounds don't Crash the Game

sabra55 edited this page Jan 23, 2024 · 5 revisions

Code by Rangi42 (in Red Star/Blue Star).

In vanilla Gen 1, due to map connections, if you leave a map where there are no defined map connections (or any map connections, in indoor maps), the game will crash. This is just because of the fact that it's attempting to switch to map $FF, an invalid map, which has an invalid map script and thusly crashes. This tutorial will make it so it won't crash the game, by adding a check to prevent the game from loading map $FF.

CheckMapConnections in home\overworld.asm

...
; if no matching warp was found
CheckMapConnections::
.checkWestMap
	ld a, [wXCoord]
	cp $ff
	jr nz, .checkEastMap
	ld a, [wWestConnectedMap]
+	cp $ff ; is the west connection set to $FF?
+	jr z, .checkEastMap ; if so, branch
	ld [wCurMap], a
	ld a, [wWestConnectedMapXAlignment] ; new X coordinate upon entering west map
	ld [wXCoord], a
...
...
	cp b
	jr nz, .checkNorthMap
	ld a, [wEastConnectedMap]
+	cp $ff ; is the east connection set to $FF?
+	jr z, .checkNorthMap ; if so, branch
	ld [wCurMap], a
	ld a, [wEastConnectedMapXAlignment] ; new X coordinate upon entering east map
	ld [wXCoord], a
...
...
	cp $ff
	jr nz, .checkSouthMap
	ld a, [wNorthConnectedMap]
+	cp $ff ; is the north connection set to $FF?
+	jr z, .checkSouthMap ; if so, branch
	ld [wCurMap], a
	ld a, [wNorthConnectedMapYAlignment] ; new Y coordinate upon entering north map
	ld [wYCoord], a
...
...
	cp b
	jr nz, .didNotEnterConnectedMap
	ld a, [wSouthConnectedMap]
+	cp $ff ; is the south connection set to $FF?
+	jr z, .didNotEnterConnectedMap ; if so, it's not worth crashing the game, so branch
	ld [wCurMap], a
	ld a, [wSouthConnectedMapYAlignment] ; new Y coordinate upon entering south map
	ld [wYCoord], a
...
Clone this wiki locally