pokecrystal-board/home/map.asm

2280 lines
33 KiB
NASM
Raw Normal View History

; Functions dealing with rendering and interacting with maps.
2019-04-19 08:35:27 -07:00
ClearUnusedMapBuffer::
ld hl, wUnusedMapBuffer
ld bc, wUnusedMapBufferEnd - wUnusedMapBuffer
2018-01-17 22:28:51 -08:00
ld a, 0
call ByteFill
ret
2018-06-24 07:09:41 -07:00
CheckScenes::
2018-09-09 12:09:51 -07:00
; Checks wCurMapSceneScriptPointer. If it's empty, returns -1 in a. Otherwise, returns the active scene ID in a.
push hl
2018-09-09 12:09:51 -07:00
ld hl, wCurMapSceneScriptPointer
ld a, [hli]
ld h, [hl]
ld l, a
or h
ld a, [hl]
jr nz, .scene_exists
ld a, -1
.scene_exists
pop hl
ret
2018-06-24 07:09:41 -07:00
GetCurrentMapSceneID::
2018-09-09 12:09:51 -07:00
; Grabs the wram map scene script pointer for the current map and loads it into wCurMapSceneScriptPointer.
; If there is no scene, both bytes of wCurMapSceneScriptPointer are wiped clean.
; Copy the current map group and number into bc. This is needed for GetMapSceneID.
2018-01-23 14:39:09 -08:00
ld a, [wMapGroup]
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wMapNumber]
ld c, a
2018-09-09 12:09:51 -07:00
; Blank out wCurMapSceneScriptPointer; this is the default scenario.
xor a
2018-09-09 12:09:51 -07:00
ld [wCurMapSceneScriptPointer], a
ld [wCurMapSceneScriptPointer + 1], a
call GetMapSceneID
ret c ; The map is not in the scene script table
2018-09-09 12:09:51 -07:00
; Load the scene script pointer from de into wCurMapSceneScriptPointer
ld a, e
2018-09-09 12:09:51 -07:00
ld [wCurMapSceneScriptPointer], a
ld a, d
2018-09-09 12:09:51 -07:00
ld [wCurMapSceneScriptPointer + 1], a
xor a
ret
2018-06-24 07:09:41 -07:00
GetMapSceneID::
2019-03-03 11:19:16 -08:00
; Searches the scene_var table for the map group and number loaded in bc, and returns the wram pointer in de.
; If the map is not in the scene_var table, returns carry.
push bc
ldh a, [hROMBank]
push af
ld a, BANK(MapScenes)
rst Bankswitch
ld hl, MapScenes
.loop
push hl
ld a, [hli] ; map group, or terminator
cp -1
2019-03-03 11:19:16 -08:00
jr z, .end ; the current map is not in the scene_var table
cp b
jr nz, .next ; map group did not match
ld a, [hli] ; map number
cp c
jr nz, .next ; map number did not match
jr .found ; we found our map
.next
pop hl
2019-03-03 11:19:16 -08:00
ld de, 4 ; scene_var size
add hl, de
jr .loop
.end
scf
jr .done
.found
ld e, [hl]
inc hl
ld d, [hl]
.done
pop hl
pop bc
ld a, b
rst Bankswitch
pop bc
ret
2018-06-24 07:09:41 -07:00
OverworldTextModeSwitch::
2015-11-05 11:06:03 -08:00
call LoadMapPart
call FarCallSwapTextboxPalettes
ret
2018-06-24 07:09:41 -07:00
LoadMapPart::
ldh a, [hROMBank]
push af
2018-01-23 14:39:09 -08:00
ld a, [wTilesetBlocksBank]
rst Bankswitch
2015-11-05 11:06:03 -08:00
call LoadMetatiles
ld a, "■"
hlcoord 0, 0
2015-11-02 09:54:27 -08:00
ld bc, SCREEN_WIDTH * SCREEN_HEIGHT
call ByteFill
2015-12-14 08:12:18 -08:00
ld a, BANK(_LoadMapPart)
rst Bankswitch
2015-12-14 08:12:18 -08:00
call _LoadMapPart
pop af
rst Bankswitch
ret
2018-06-24 07:09:41 -07:00
LoadMetatiles::
2015-12-14 08:12:18 -08:00
; de <- wOverworldMapAnchor
2015-12-13 17:33:56 -08:00
ld a, [wOverworldMapAnchor]
ld e, a
2015-12-13 17:33:56 -08:00
ld a, [wOverworldMapAnchor + 1]
ld d, a
ld hl, wSurroundingTiles
ld b, SURROUNDING_HEIGHT / METATILE_WIDTH ; 5
2015-12-14 08:12:18 -08:00
.row
push de
push hl
ld c, SURROUNDING_WIDTH / METATILE_WIDTH ; 6
2015-12-14 08:12:18 -08:00
.col
push de
push hl
2015-12-14 08:12:18 -08:00
; Load the current map block.
; If the current map block is a border block, load the border block.
ld a, [de]
and a
2015-11-05 11:06:03 -08:00
jr nz, .ok
2018-01-23 14:39:09 -08:00
ld a, [wMapBorderBlock]
2015-11-05 11:06:03 -08:00
.ok
; Load the current wSurroundingTiles address into de.
ld e, l
ld d, h
2018-01-23 14:39:09 -08:00
; Set hl to the address of the current metatile data ([wTilesetBlocksAddress] + (a) tiles).
2017-12-12 20:05:30 -08:00
; This is buggy; it wraps around past 128 blocks.
; To fix, uncomment the line below.
add a ; Comment or delete this line to fix the above bug.
ld l, a
ld h, 0
2017-12-12 20:05:30 -08:00
; add hl, hl
2015-12-14 08:12:18 -08:00
add hl, hl
add hl, hl
add hl, hl
2018-01-23 14:39:09 -08:00
ld a, [wTilesetBlocksAddress]
add l
ld l, a
2018-01-23 14:39:09 -08:00
ld a, [wTilesetBlocksAddress + 1]
adc h
ld h, a
2015-12-14 08:12:18 -08:00
; copy the 4x4 metatile
rept METATILE_WIDTH + -1
rept METATILE_WIDTH
ld a, [hli]
ld [de], a
inc de
endr
ld a, e
add SURROUNDING_WIDTH - METATILE_WIDTH
ld e, a
jr nc, .next\@
inc d
.next\@
endr
rept METATILE_WIDTH
ld a, [hli]
ld [de], a
inc de
endr
2015-12-14 08:12:18 -08:00
; Next metatile
pop hl
ld de, METATILE_WIDTH
add hl, de
pop de
inc de
dec c
2015-12-14 08:12:18 -08:00
jp nz, .col
; Next metarow
pop hl
ld de, SURROUNDING_WIDTH * METATILE_WIDTH
add hl, de
pop de
2018-01-23 14:39:09 -08:00
ld a, [wMapWidth]
2015-11-05 11:06:03 -08:00
add 6
add e
ld e, a
2015-11-05 11:06:03 -08:00
jr nc, .ok2
inc d
2015-11-05 11:06:03 -08:00
.ok2
dec b
2015-12-14 08:12:18 -08:00
jp nz, .row
ret
2018-06-24 07:09:41 -07:00
ReturnToMapFromSubmenu::
2015-11-11 20:38:57 -08:00
ld a, MAPSETUP_SUBMENU
ldh [hMapEntryMethod], a
2017-12-24 09:47:30 -08:00
farcall RunMapSetupScript
xor a
ldh [hMapEntryMethod], a
ret
2018-06-24 07:09:41 -07:00
CheckWarpTile::
2015-11-12 08:10:19 -08:00
call GetDestinationWarpNumber
ret nc
2015-11-12 08:10:19 -08:00
push bc
2017-12-24 09:47:30 -08:00
farcall CheckDirectionalWarp
pop bc
ret nc
2015-11-12 08:10:19 -08:00
call CopyWarpData
scf
ret
2018-06-24 07:09:41 -07:00
WarpCheck::
2015-11-12 08:10:19 -08:00
call GetDestinationWarpNumber
ret nc
2015-11-12 08:10:19 -08:00
call CopyWarpData
ret
2018-06-24 07:09:41 -07:00
GetDestinationWarpNumber::
2017-12-24 09:47:30 -08:00
farcall CheckWarpCollision
ret nc
ldh a, [hROMBank]
push af
call SwitchToMapScriptsBank
2015-11-12 08:10:19 -08:00
call .GetDestinationWarpNumber
pop de
ld a, d
rst Bankswitch
ret
2018-06-24 07:09:41 -07:00
.GetDestinationWarpNumber:
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapY]
sub 4
ld e, a
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapX]
sub 4
ld d, a
2018-09-09 12:09:51 -07:00
ld a, [wCurMapWarpCount]
and a
ret z
2015-11-12 08:10:19 -08:00
ld c, a
2018-09-09 12:09:51 -07:00
ld hl, wCurMapWarpsPointer
ld a, [hli]
ld h, [hl]
ld l, a
2015-11-12 08:10:19 -08:00
.loop
push hl
ld a, [hli]
cp e
2015-11-12 08:10:19 -08:00
jr nz, .next
ld a, [hli]
cp d
2015-11-12 08:10:19 -08:00
jr nz, .next
jr .found_warp
2015-11-12 08:10:19 -08:00
.next
pop hl
2019-03-03 11:19:16 -08:00
ld a, WARP_EVENT_SIZE
add l
ld l, a
2015-11-12 08:10:19 -08:00
jr nc, .okay
inc h
2015-11-12 08:10:19 -08:00
.okay
dec c
2015-11-12 08:10:19 -08:00
jr nz, .loop
xor a
ret
2015-11-12 08:10:19 -08:00
.found_warp
pop hl
2015-11-12 08:10:19 -08:00
call .IncreaseHLTwice
ret nc ; never encountered
2018-09-09 12:09:51 -07:00
ld a, [wCurMapWarpCount]
inc a
sub c
ld c, a
scf
ret
.IncreaseHLTwice:
inc hl
2016-01-06 04:44:50 -08:00
inc hl
scf
ret
2018-06-24 07:09:41 -07:00
CopyWarpData::
ldh a, [hROMBank]
push af
call SwitchToMapScriptsBank
2015-11-12 08:10:19 -08:00
call .CopyWarpData
pop af
rst Bankswitch
scf
ret
2018-06-24 07:09:41 -07:00
.CopyWarpData:
push bc
2018-09-09 12:09:51 -07:00
ld hl, wCurMapWarpsPointer
ld a, [hli]
ld h, [hl]
ld l, a
ld a, c
dec a
2019-03-03 11:19:16 -08:00
ld bc, WARP_EVENT_SIZE
call AddNTimes
2019-03-03 11:19:16 -08:00
ld bc, 2 ; warp number
add hl, bc
ld a, [hli]
cp $ff
2015-11-12 08:10:19 -08:00
jr nz, .skip
2018-01-23 14:39:09 -08:00
ld hl, wBackupWarpNumber
ld a, [hli]
2015-11-12 08:10:19 -08:00
.skip
pop bc
2015-11-12 08:10:19 -08:00
ld [wNextWarp], a
ld a, [hli]
2015-11-12 08:10:19 -08:00
ld [wNextMapGroup], a
ld a, [hli]
2015-11-12 08:10:19 -08:00
ld [wNextMapNumber], a
ld a, c
2015-11-12 08:10:19 -08:00
ld [wPrevWarp], a
2018-01-23 14:39:09 -08:00
ld a, [wMapGroup]
2015-11-12 08:10:19 -08:00
ld [wPrevMapGroup], a
2018-01-23 14:39:09 -08:00
ld a, [wMapNumber]
2015-11-12 08:10:19 -08:00
ld [wPrevMapNumber], a
scf
ret
2018-06-24 07:09:41 -07:00
CheckOutdoorMap::
cp ROUTE
ret z
cp TOWN
ret
2018-06-24 07:09:41 -07:00
CheckIndoorMap::
cp INDOOR
ret z
cp CAVE
ret z
cp DUNGEON
ret z
cp GATE
ret
; unused
cp INDOOR
ret z
cp GATE
ret z
cp ENVIRONMENT_5
ret
2018-06-24 07:09:41 -07:00
LoadMapAttributes::
call CopyMapPartialAndAttributes
call SwitchToMapScriptsBank
2015-11-12 08:10:19 -08:00
call ReadMapScripts
xor a ; do not skip object_events
call ReadMapEvents
ret
2018-06-24 07:09:41 -07:00
LoadMapAttributes_SkipPeople::
call CopyMapPartialAndAttributes
call SwitchToMapScriptsBank
2015-11-12 08:10:19 -08:00
call ReadMapScripts
ld a, TRUE ; skip object events
call ReadMapEvents
ret
2018-06-24 07:09:41 -07:00
CopyMapPartialAndAttributes::
call CopyMapPartial
call SwitchToMapAttributesBank
call GetMapAttributesPointer
call CopyMapAttributes
2015-11-12 08:10:19 -08:00
call GetMapConnections
ret
2018-06-24 07:09:41 -07:00
ReadMapEvents::
push af
2018-01-23 14:39:09 -08:00
ld hl, wMapEventsPointer
ld a, [hli]
ld h, [hl]
ld l, a
inc hl
2016-01-06 04:44:50 -08:00
inc hl
2015-07-26 00:11:33 -07:00
call ReadWarps
call ReadCoordEvents
call ReadBGEvents
2015-11-12 08:10:19 -08:00
pop af
and a ; skip object events?
ret nz
2015-11-12 08:10:19 -08:00
2015-07-26 00:11:33 -07:00
call ReadObjectEvents
ret
2018-06-24 07:09:41 -07:00
ReadMapScripts::
2018-01-23 14:39:09 -08:00
ld hl, wMapScriptsPointer
ld a, [hli]
ld h, [hl]
ld l, a
call ReadMapSceneScripts
2015-10-24 16:49:19 -07:00
call ReadMapCallbacks
ret
2018-06-24 07:09:41 -07:00
CopyMapAttributes::
ld de, wMapAttributes
ld c, wMapAttributesEnd - wMapAttributes
2015-11-12 08:10:19 -08:00
.loop
ld a, [hli]
ld [de], a
inc de
dec c
2015-11-12 08:10:19 -08:00
jr nz, .loop
ret
2018-06-24 07:09:41 -07:00
GetMapConnections::
ld a, $ff
2018-01-23 14:39:09 -08:00
ld [wNorthConnectedMapGroup], a
ld [wSouthConnectedMapGroup], a
ld [wWestConnectedMapGroup], a
ld [wEastConnectedMapGroup], a
2018-01-23 14:39:09 -08:00
ld a, [wMapConnections]
ld b, a
2015-11-12 08:10:19 -08:00
bit NORTH_F, b
jr z, .no_north
2018-01-23 14:39:09 -08:00
ld de, wNorthMapConnection
call GetMapConnection
2015-11-12 08:10:19 -08:00
.no_north
2015-11-12 08:10:19 -08:00
bit SOUTH_F, b
jr z, .no_south
2018-01-23 14:39:09 -08:00
ld de, wSouthMapConnection
call GetMapConnection
2015-11-12 08:10:19 -08:00
.no_south
2015-11-12 08:10:19 -08:00
bit WEST_F, b
jr z, .no_west
2018-01-23 14:39:09 -08:00
ld de, wWestMapConnection
call GetMapConnection
2015-11-12 08:10:19 -08:00
.no_west
2015-11-12 08:10:19 -08:00
bit EAST_F, b
jr z, .no_east
2018-01-23 14:39:09 -08:00
ld de, wEastMapConnection
call GetMapConnection
2015-11-12 08:10:19 -08:00
.no_east
ret
2018-06-24 07:09:41 -07:00
GetMapConnection::
; Load map connection struct at hl into de.
2018-01-23 14:39:09 -08:00
ld c, wSouthMapConnection - wNorthMapConnection
.loop
ld a, [hli]
ld [de], a
inc de
dec c
jr nz, .loop
ret
2018-06-24 07:09:41 -07:00
ReadMapSceneScripts::
2019-03-03 11:19:16 -08:00
ld a, [hli] ; scene_script count
ld c, a
2018-09-09 12:15:33 -07:00
ld [wCurMapSceneScriptCount], a
ld a, l
2018-09-09 12:15:33 -07:00
ld [wCurMapSceneScriptsPointer], a
ld a, h
2018-09-09 12:09:51 -07:00
ld [wCurMapSceneScriptsPointer + 1], a
ld a, c
and a
ret z
2015-10-24 07:34:19 -07:00
2019-03-03 11:19:16 -08:00
ld bc, SCENE_SCRIPT_SIZE
call AddNTimes
ret
2018-06-24 07:09:41 -07:00
ReadMapCallbacks::
ld a, [hli]
ld c, a
2018-09-09 12:09:51 -07:00
ld [wCurMapCallbackCount], a
ld a, l
2018-09-09 12:09:51 -07:00
ld [wCurMapCallbacksPointer], a
ld a, h
2018-09-09 12:09:51 -07:00
ld [wCurMapCallbacksPointer + 1], a
ld a, c
and a
ret z
2015-10-24 07:34:19 -07:00
2019-03-03 11:19:16 -08:00
ld bc, CALLBACK_SIZE
call AddNTimes
ret
2018-06-24 07:09:41 -07:00
ReadWarps::
ld a, [hli]
ld c, a
2018-09-09 12:09:51 -07:00
ld [wCurMapWarpCount], a
ld a, l
2018-09-09 12:09:51 -07:00
ld [wCurMapWarpsPointer], a
ld a, h
2018-09-09 12:09:51 -07:00
ld [wCurMapWarpsPointer + 1], a
ld a, c
and a
ret z
2019-03-03 11:19:16 -08:00
ld bc, WARP_EVENT_SIZE
call AddNTimes
ret
2018-06-24 07:09:41 -07:00
ReadCoordEvents::
ld a, [hli]
ld c, a
2018-09-09 12:09:51 -07:00
ld [wCurMapCoordEventCount], a
ld a, l
2018-09-09 12:09:51 -07:00
ld [wCurMapCoordEventsPointer], a
ld a, h
2018-09-09 12:09:51 -07:00
ld [wCurMapCoordEventsPointer + 1], a
2015-11-12 08:10:19 -08:00
ld a, c
and a
ret z
2015-11-12 08:10:19 -08:00
2019-03-03 11:19:16 -08:00
ld bc, COORD_EVENT_SIZE
call AddNTimes
ret
2018-06-24 07:09:41 -07:00
ReadBGEvents::
ld a, [hli]
ld c, a
2018-09-09 12:09:51 -07:00
ld [wCurMapBGEventCount], a
ld a, l
2018-09-09 12:09:51 -07:00
ld [wCurMapBGEventsPointer], a
ld a, h
2018-09-09 12:09:51 -07:00
ld [wCurMapBGEventsPointer + 1], a
2015-11-12 08:10:19 -08:00
ld a, c
and a
ret z
2015-11-12 08:10:19 -08:00
2019-03-03 11:19:16 -08:00
ld bc, BG_EVENT_SIZE
call AddNTimes
ret
2018-06-24 07:09:41 -07:00
ReadObjectEvents::
push hl
2015-11-12 08:10:19 -08:00
call ClearObjectStructs
pop de
2018-01-23 14:39:09 -08:00
ld hl, wMap1Object
ld a, [de]
inc de
2018-09-09 12:09:51 -07:00
ld [wCurMapObjectEventCount], a
ld a, e
2018-09-09 12:09:51 -07:00
ld [wCurMapObjectEventsPointer], a
ld a, d
2018-09-09 12:09:51 -07:00
ld [wCurMapObjectEventsPointer + 1], a
2015-11-12 08:10:19 -08:00
2018-09-09 12:09:51 -07:00
ld a, [wCurMapObjectEventCount]
call CopyMapObjectEvents
2015-11-12 08:10:19 -08:00
2018-09-09 12:09:51 -07:00
; get NUM_OBJECTS - [wCurMapObjectEventCount]
ld a, [wCurMapObjectEventCount]
ld c, a
2015-11-12 08:10:19 -08:00
ld a, NUM_OBJECTS ; - 1
sub c
2015-11-12 08:10:19 -08:00
jr z, .skip
; jr c, .skip
; stupid waste of time and space
ld bc, 1
add hl, bc
2015-11-12 08:10:19 -08:00
; Fill the remaining sprite IDs and y coords with 0 and -1, respectively.
; Bleeds into wObjectMasks due to a bug. Uncomment the above subtraction
; to fix.
ld bc, OBJECT_LENGTH
.loop
ld [hl], 0
inc hl
2015-11-12 08:10:19 -08:00
ld [hl], -1
dec hl
add hl, bc
dec a
2015-11-12 08:10:19 -08:00
jr nz, .loop
2015-11-12 08:10:19 -08:00
.skip
ld h, d
ld l, e
ret
2018-06-24 07:09:41 -07:00
CopyMapObjectEvents::
and a
ret z
2015-11-12 08:10:19 -08:00
ld c, a
2015-11-12 08:10:19 -08:00
.loop
push bc
push hl
ld a, $ff
ld [hli], a
2019-03-03 11:19:16 -08:00
ld b, OBJECT_EVENT_SIZE
2015-11-12 08:10:19 -08:00
.loop2
ld a, [de]
inc de
ld [hli], a
dec b
2015-11-12 08:10:19 -08:00
jr nz, .loop2
pop hl
2015-11-12 08:10:19 -08:00
ld bc, OBJECT_LENGTH
add hl, bc
pop bc
dec c
2015-11-12 08:10:19 -08:00
jr nz, .loop
ret
2018-06-24 07:09:41 -07:00
ClearObjectStructs::
2018-01-23 14:39:09 -08:00
ld hl, wObject1Struct
2015-07-26 00:11:33 -07:00
ld bc, OBJECT_STRUCT_LENGTH * (NUM_OBJECT_STRUCTS - 1)
xor a
call ByteFill
2015-11-12 08:10:19 -08:00
; Just to make sure (this is rather pointless)
2018-01-23 14:39:09 -08:00
ld hl, wObject1Struct
2015-07-26 00:11:33 -07:00
ld de, OBJECT_STRUCT_LENGTH
ld c, NUM_OBJECT_STRUCTS - 1
xor a
2015-11-12 08:10:19 -08:00
.loop
ld [hl], a
add hl, de
dec c
2015-11-12 08:10:19 -08:00
jr nz, .loop
ret
2018-06-24 07:09:41 -07:00
RestoreFacingAfterWarp::
call GetMapScriptsBank
rst Bankswitch
2018-01-23 14:39:09 -08:00
ld hl, wMapEventsPointer
ld a, [hli]
ld h, [hl]
ld l, a
2019-03-03 11:19:16 -08:00
rept 3 ; get to the warp coords
inc hl
endr
2018-01-23 14:39:09 -08:00
ld a, [wWarpNumber]
dec a
ld c, a
2015-11-12 08:10:19 -08:00
ld b, 0
2019-03-03 11:19:16 -08:00
ld a, WARP_EVENT_SIZE
call AddNTimes
ld a, [hli]
2018-01-23 14:39:09 -08:00
ld [wYCoord], a
ld a, [hli]
2018-01-23 14:39:09 -08:00
ld [wXCoord], a
2015-11-12 08:10:19 -08:00
; destination warp number
ld a, [hli]
cp $ff
2015-11-12 08:10:19 -08:00
jr nz, .skip
call .backup
2015-11-12 08:10:19 -08:00
.skip
2017-12-24 09:47:30 -08:00
farcall GetCoordOfUpperLeftCorner
ret
2015-11-12 08:10:19 -08:00
.backup
ld a, [wPrevWarp]
2018-01-23 14:39:09 -08:00
ld [wBackupWarpNumber], a
2015-11-12 08:10:19 -08:00
ld a, [wPrevMapGroup]
2018-01-23 14:39:09 -08:00
ld [wBackupMapGroup], a
2015-11-12 08:10:19 -08:00
ld a, [wPrevMapNumber]
2018-01-23 14:39:09 -08:00
ld [wBackupMapNumber], a
ret
2018-06-24 07:09:41 -07:00
LoadBlockData::
ld hl, wOverworldMapBlocks
ld bc, wOverworldMapBlocksEnd - wOverworldMapBlocks
ld a, 0
call ByteFill
call ChangeMap
call FillMapConnections
2016-01-06 04:44:50 -08:00
ld a, MAPCALLBACK_TILES
call RunMapCallback
ret
2018-06-24 07:09:41 -07:00
ChangeMap::
ldh a, [hROMBank]
push af
ld hl, wOverworldMapBlocks
2018-01-23 14:39:09 -08:00
ld a, [wMapWidth]
ldh [hConnectedMapWidth], a
add $6
ldh [hConnectionStripLength], a
ld c, a
ld b, 0
add hl, bc
add hl, bc
add hl, bc
ld c, 3
add hl, bc
2018-01-23 14:39:09 -08:00
ld a, [wMapBlocksBank]
rst Bankswitch
2018-01-23 14:39:09 -08:00
ld a, [wMapBlocksPointer]
ld e, a
2018-01-23 14:39:09 -08:00
ld a, [wMapBlocksPointer + 1]
ld d, a
2018-01-23 14:39:09 -08:00
ld a, [wMapHeight]
ld b, a
2016-01-12 09:46:18 -08:00
.row
push hl
ldh a, [hConnectedMapWidth]
ld c, a
2016-01-12 09:46:18 -08:00
.col
ld a, [de]
inc de
ld [hli], a
dec c
2016-01-12 09:46:18 -08:00
jr nz, .col
pop hl
ldh a, [hConnectionStripLength]
add l
ld l, a
2016-01-12 09:46:18 -08:00
jr nc, .okay
inc h
2016-01-12 09:46:18 -08:00
.okay
dec b
2016-01-12 09:46:18 -08:00
jr nz, .row
pop af
rst Bankswitch
ret
2018-06-24 07:09:41 -07:00
FillMapConnections::
; North
2018-01-23 14:39:09 -08:00
ld a, [wNorthConnectedMapGroup]
cp $ff
jr z, .South
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wNorthConnectedMapNumber]
ld c, a
call GetAnyMapBlocksBank
2018-01-23 14:39:09 -08:00
ld a, [wNorthConnectionStripPointer]
ld l, a
2018-01-23 14:39:09 -08:00
ld a, [wNorthConnectionStripPointer + 1]
ld h, a
2018-01-23 14:39:09 -08:00
ld a, [wNorthConnectionStripLocation]
ld e, a
2018-01-23 14:39:09 -08:00
ld a, [wNorthConnectionStripLocation + 1]
ld d, a
2018-01-23 14:39:09 -08:00
ld a, [wNorthConnectionStripLength]
ldh [hConnectionStripLength], a
2018-01-23 14:39:09 -08:00
ld a, [wNorthConnectedMapWidth]
ldh [hConnectedMapWidth], a
call FillNorthConnectionStrip
.South:
2018-01-23 14:39:09 -08:00
ld a, [wSouthConnectedMapGroup]
cp $ff
jr z, .West
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wSouthConnectedMapNumber]
ld c, a
call GetAnyMapBlocksBank
2018-01-23 14:39:09 -08:00
ld a, [wSouthConnectionStripPointer]
ld l, a
2018-01-23 14:39:09 -08:00
ld a, [wSouthConnectionStripPointer + 1]
ld h, a
2018-01-23 14:39:09 -08:00
ld a, [wSouthConnectionStripLocation]
ld e, a
2018-01-23 14:39:09 -08:00
ld a, [wSouthConnectionStripLocation + 1]
ld d, a
2018-01-23 14:39:09 -08:00
ld a, [wSouthConnectionStripLength]
ldh [hConnectionStripLength], a
2018-01-23 14:39:09 -08:00
ld a, [wSouthConnectedMapWidth]
ldh [hConnectedMapWidth], a
call FillSouthConnectionStrip
.West:
2018-01-23 14:39:09 -08:00
ld a, [wWestConnectedMapGroup]
cp $ff
jr z, .East
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wWestConnectedMapNumber]
ld c, a
call GetAnyMapBlocksBank
2018-01-23 14:39:09 -08:00
ld a, [wWestConnectionStripPointer]
ld l, a
2018-01-23 14:39:09 -08:00
ld a, [wWestConnectionStripPointer + 1]
ld h, a
2018-01-23 14:39:09 -08:00
ld a, [wWestConnectionStripLocation]
ld e, a
2018-01-23 14:39:09 -08:00
ld a, [wWestConnectionStripLocation + 1]
ld d, a
2018-01-23 14:39:09 -08:00
ld a, [wWestConnectionStripLength]
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wWestConnectedMapWidth]
ldh [hConnectionStripLength], a
call FillWestConnectionStrip
.East:
2018-01-23 14:39:09 -08:00
ld a, [wEastConnectedMapGroup]
cp $ff
jr z, .Done
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wEastConnectedMapNumber]
ld c, a
call GetAnyMapBlocksBank
2018-01-23 14:39:09 -08:00
ld a, [wEastConnectionStripPointer]
ld l, a
2018-01-23 14:39:09 -08:00
ld a, [wEastConnectionStripPointer + 1]
ld h, a
2018-01-23 14:39:09 -08:00
ld a, [wEastConnectionStripLocation]
ld e, a
2018-01-23 14:39:09 -08:00
ld a, [wEastConnectionStripLocation + 1]
ld d, a
2018-01-23 14:39:09 -08:00
ld a, [wEastConnectionStripLength]
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wEastConnectedMapWidth]
ldh [hConnectionStripLength], a
call FillEastConnectionStrip
.Done:
ret
FillNorthConnectionStrip::
2018-06-24 07:09:41 -07:00
FillSouthConnectionStrip::
ld c, 3
.y
push de
push hl
ldh a, [hConnectionStripLength]
ld b, a
.x
ld a, [hli]
ld [de], a
inc de
dec b
jr nz, .x
pop hl
ldh a, [hConnectedMapWidth]
ld e, a
ld d, 0
add hl, de
pop de
2018-01-23 14:39:09 -08:00
ld a, [wMapWidth]
add 6
add e
ld e, a
2016-01-12 09:46:18 -08:00
jr nc, .okay
inc d
2016-01-12 09:46:18 -08:00
.okay
dec c
jr nz, .y
ret
FillWestConnectionStrip::
2018-06-24 07:09:41 -07:00
FillEastConnectionStrip::
2016-01-12 09:46:18 -08:00
.loop
2018-01-23 14:39:09 -08:00
ld a, [wMapWidth]
add 6
ldh [hConnectedMapWidth], a
push de
push hl
ld a, [hli]
ld [de], a
inc de
ld a, [hli]
ld [de], a
inc de
ld a, [hli]
ld [de], a
inc de
pop hl
ldh a, [hConnectionStripLength]
ld e, a
ld d, 0
add hl, de
pop de
ldh a, [hConnectedMapWidth]
add e
ld e, a
2016-01-12 09:46:18 -08:00
jr nc, .okay
inc d
2016-01-12 09:46:18 -08:00
.okay
dec b
2016-01-12 09:46:18 -08:00
jr nz, .loop
ret
2018-06-24 07:09:41 -07:00
LoadMapStatus::
2018-01-23 14:39:09 -08:00
ld [wMapStatus], a
ret
2018-06-24 07:09:41 -07:00
CallScript::
; Call a script at a:hl.
2018-01-23 14:39:09 -08:00
ld [wScriptBank], a
ld a, l
2018-01-23 14:39:09 -08:00
ld [wScriptPos], a
ld a, h
2018-01-23 14:39:09 -08:00
ld [wScriptPos + 1], a
2015-11-05 12:08:00 -08:00
ld a, PLAYEREVENT_MAPSCRIPT
2018-01-23 14:39:09 -08:00
ld [wScriptRunning], a
scf
ret
2018-06-24 07:09:41 -07:00
CallMapScript::
2015-11-05 12:08:00 -08:00
; Call a script at hl in the current bank if there isn't already a script running
2018-01-23 14:39:09 -08:00
ld a, [wScriptRunning]
and a
ret nz
call GetMapScriptsBank
jr CallScript
2018-06-24 07:09:41 -07:00
RunMapCallback::
; Will run the first callback found with execution index equal to a.
ld b, a
ldh a, [hROMBank]
push af
call SwitchToMapScriptsBank
2015-11-05 12:08:00 -08:00
call .FindCallback
jr nc, .done
call GetMapScriptsBank
ld b, a
ld d, h
ld e, l
call ExecuteCallbackScript
.done
pop af
rst Bankswitch
ret
2018-06-24 07:09:41 -07:00
.FindCallback:
2018-09-09 12:09:51 -07:00
ld a, [wCurMapCallbackCount]
ld c, a
and a
ret z
2018-09-09 12:09:51 -07:00
ld hl, wCurMapCallbacksPointer
ld a, [hli]
ld h, [hl]
ld l, a
or h
ret z
2019-03-03 11:19:16 -08:00
ld de, CALLBACK_SIZE
.loop
ld a, [hl]
cp b
2015-11-05 12:08:00 -08:00
jr z, .found
add hl, de
dec c
jr nz, .loop
xor a
ret
2015-11-05 12:08:00 -08:00
.found
inc hl
ld a, [hli]
ld h, [hl]
ld l, a
scf
ret
2018-06-24 07:09:41 -07:00
ExecuteCallbackScript::
2015-11-05 12:08:00 -08:00
; Do map callback de and return to script bank b.
2017-12-24 09:47:30 -08:00
farcall CallCallback
2018-01-23 14:39:09 -08:00
ld a, [wScriptMode]
push af
2018-01-23 14:39:09 -08:00
ld hl, wScriptFlags
ld a, [hl]
push af
set 1, [hl]
2017-12-24 09:47:30 -08:00
farcall EnableScriptMode
farcall ScriptEvents
pop af
2018-01-23 14:39:09 -08:00
ld [wScriptFlags], a
pop af
2018-01-23 14:39:09 -08:00
ld [wScriptMode], a
ret
2018-06-24 07:09:41 -07:00
MapTextbox::
ldh a, [hROMBank]
push af
ld a, b
rst Bankswitch
push hl
2019-04-08 05:15:10 -07:00
call SpeechTextbox
call SafeUpdateSprites
ld a, 1
ldh [hOAMUpdate], a
2015-11-25 07:16:29 -08:00
call ApplyTilemap
pop hl
2019-04-08 05:15:10 -07:00
call PrintTextboxText
xor a
ldh [hOAMUpdate], a
pop af
rst Bankswitch
ret
2018-06-24 07:09:41 -07:00
Call_a_de::
; Call a:de.
ldh [hBuffer], a
ldh a, [hROMBank]
push af
ldh a, [hBuffer]
rst Bankswitch
call .de
pop af
rst Bankswitch
ret
.de
push de
ret
2018-06-24 07:09:41 -07:00
GetMovementData::
; Initialize the movement data for object c at b:hl
ldh a, [hROMBank]
push af
ld a, b
rst Bankswitch
ld a, c
2015-11-11 20:38:57 -08:00
call LoadMovementDataPointer
pop hl
ld a, h
rst Bankswitch
ret
GetScriptByte::
2018-01-23 14:39:09 -08:00
; Return byte at wScriptBank:wScriptPos in a.
push hl
push bc
ldh a, [hROMBank]
push af
2018-01-23 14:39:09 -08:00
ld a, [wScriptBank]
rst Bankswitch
2018-01-23 14:39:09 -08:00
ld hl, wScriptPos
ld c, [hl]
inc hl
ld b, [hl]
ld a, [bc]
inc bc
ld [hl], b
dec hl
ld [hl], c
ld b, a
pop af
rst Bankswitch
ld a, b
pop bc
pop hl
ret
ObjectEvent::
jumptextfaceplayer ObjectEventText
ObjectEventText::
text_far _ObjectEventText
text_end
2018-06-24 07:09:41 -07:00
BGEvent::
jumptext BGEventText
2018-06-24 07:09:41 -07:00
BGEventText::
text_far UnknownText_0x1c46fc
text_end
2018-06-24 07:09:41 -07:00
CoordinatesEvent::
jumptext CoordinatesEventText
2018-06-24 07:09:41 -07:00
CoordinatesEventText::
text_far UnknownText_0x1c4706
text_end
2018-06-24 07:09:41 -07:00
CheckObjectMask::
ldh a, [hMapObjectIndexBuffer]
ld e, a
ld d, $0
2015-11-01 18:17:46 -08:00
ld hl, wObjectMasks
add hl, de
ld a, [hl]
ret
2018-06-24 07:09:41 -07:00
MaskObject::
ldh a, [hMapObjectIndexBuffer]
ld e, a
ld d, $0
2015-11-01 18:17:46 -08:00
ld hl, wObjectMasks
add hl, de
2016-01-06 04:44:50 -08:00
ld [hl], -1 ; , masked
ret
2018-06-24 07:09:41 -07:00
UnmaskObject::
ldh a, [hMapObjectIndexBuffer]
ld e, a
ld d, $0
2015-11-01 18:17:46 -08:00
ld hl, wObjectMasks
add hl, de
2015-11-01 18:17:46 -08:00
ld [hl], 0 ; unmasked
ret
2019-06-06 14:59:13 -07:00
ScrollMapUp::
hlcoord 0, 0
2018-01-23 14:39:09 -08:00
ld de, wBGMapBuffer
2015-11-12 21:49:27 -08:00
call BackupBGMapRow
ld c, 2 * SCREEN_WIDTH
call FarCallScrollBGMapPalettes
ld a, [wBGMapAnchor]
ld e, a
ld a, [wBGMapAnchor + 1]
ld d, a
2015-11-12 21:49:27 -08:00
call UpdateBGMapRow
ld a, $1
ldh [hBGMapUpdate], a
ret
2019-06-06 14:59:13 -07:00
ScrollMapDown::
2015-11-12 21:49:27 -08:00
hlcoord 0, SCREEN_HEIGHT - 2
2018-01-23 14:39:09 -08:00
ld de, wBGMapBuffer
2015-11-12 21:49:27 -08:00
call BackupBGMapRow
ld c, 2 * SCREEN_WIDTH
call FarCallScrollBGMapPalettes
ld a, [wBGMapAnchor]
ld l, a
ld a, [wBGMapAnchor + 1]
ld h, a
2018-07-03 23:42:11 -07:00
ld bc, BG_MAP_WIDTH tiles
add hl, bc
2017-12-28 04:32:33 -08:00
; cap d at HIGH(vBGMap0)
ld a, h
2015-11-12 21:49:27 -08:00
and %00000011
2017-12-28 04:32:33 -08:00
or HIGH(vBGMap0)
ld e, l
ld d, a
2015-11-12 21:49:27 -08:00
call UpdateBGMapRow
ld a, $1
ldh [hBGMapUpdate], a
ret
2019-06-06 14:59:13 -07:00
ScrollMapLeft::
hlcoord 0, 0
2018-01-23 14:39:09 -08:00
ld de, wBGMapBuffer
2015-11-12 21:49:27 -08:00
call BackupBGMapColumn
ld c, 2 * SCREEN_HEIGHT
call FarCallScrollBGMapPalettes
ld a, [wBGMapAnchor]
ld e, a
ld a, [wBGMapAnchor + 1]
ld d, a
2015-11-12 21:49:27 -08:00
call UpdateBGMapColumn
ld a, $1
ldh [hBGMapUpdate], a
ret
2019-06-06 14:59:13 -07:00
ScrollMapRight::
2015-11-12 21:49:27 -08:00
hlcoord SCREEN_WIDTH - 2, 0
2018-01-23 14:39:09 -08:00
ld de, wBGMapBuffer
2015-11-12 21:49:27 -08:00
call BackupBGMapColumn
ld c, 2 * SCREEN_HEIGHT
call FarCallScrollBGMapPalettes
ld a, [wBGMapAnchor]
ld e, a
2015-11-12 21:49:27 -08:00
and %11100000
ld b, a
ld a, e
2015-11-12 21:49:27 -08:00
add SCREEN_HEIGHT
and %00011111
or b
ld e, a
ld a, [wBGMapAnchor + 1]
ld d, a
2015-11-12 21:49:27 -08:00
call UpdateBGMapColumn
ld a, $1
ldh [hBGMapUpdate], a
ret
2018-06-24 07:09:41 -07:00
BackupBGMapRow::
2015-11-12 21:49:27 -08:00
ld c, 2 * SCREEN_WIDTH
.loop
ld a, [hli]
ld [de], a
inc de
dec c
2015-11-12 21:49:27 -08:00
jr nz, .loop
ret
2018-06-24 07:09:41 -07:00
BackupBGMapColumn::
2015-11-12 21:49:27 -08:00
ld c, SCREEN_HEIGHT
.loop
ld a, [hli]
ld [de], a
inc de
ld a, [hl]
ld [de], a
inc de
2015-11-12 21:49:27 -08:00
ld a, SCREEN_WIDTH - 1
add l
ld l, a
2015-11-12 21:49:27 -08:00
jr nc, .skip
inc h
2015-11-12 21:49:27 -08:00
.skip
dec c
2015-11-12 21:49:27 -08:00
jr nz, .loop
ret
2018-06-24 07:09:41 -07:00
UpdateBGMapRow::
2018-01-23 14:39:09 -08:00
ld hl, wBGMapBufferPtrs
push de
2015-11-12 21:49:27 -08:00
call .iteration
pop de
ld a, BG_MAP_WIDTH
add e
ld e, a
2015-11-12 21:49:27 -08:00
.iteration
ld c, 10
.loop
ld a, e
ld [hli], a
ld a, d
ld [hli], a
ld a, e
inc a
2016-01-06 04:44:50 -08:00
inc a
and $1f
ld b, a
ld a, e
and $e0
or b
ld e, a
dec c
2015-11-12 21:49:27 -08:00
jr nz, .loop
ld a, SCREEN_WIDTH
ldh [hBGMapTileCount], a
ret
2018-06-24 07:09:41 -07:00
UpdateBGMapColumn::
2018-01-23 14:39:09 -08:00
ld hl, wBGMapBufferPtrs
2015-11-12 21:49:27 -08:00
ld c, SCREEN_HEIGHT
.loop
ld a, e
ld [hli], a
ld a, d
ld [hli], a
ld a, BG_MAP_WIDTH
add e
ld e, a
2015-11-12 21:49:27 -08:00
jr nc, .skip
inc d
2017-12-28 04:32:33 -08:00
; cap d at HIGH(vBGMap0)
ld a, d
2018-01-11 09:00:01 -08:00
and %11
2017-12-28 04:32:33 -08:00
or HIGH(vBGMap0)
ld d, a
2015-11-12 21:49:27 -08:00
.skip
dec c
2015-11-12 21:49:27 -08:00
jr nz, .loop
ld a, SCREEN_HEIGHT
ldh [hBGMapTileCount], a
ret
2018-01-02 07:04:21 -08:00
Unreferenced_Function2816::
2018-01-23 14:39:09 -08:00
ld hl, wBGMapBuffer
ld bc, wBGMapBufferEnd - wBGMapBuffer
xor a
call ByteFill
ret
2018-06-24 07:09:41 -07:00
LoadTilesetGFX::
2018-01-23 14:39:09 -08:00
ld hl, wTilesetAddress
ld a, [hli]
ld h, [hl]
ld l, a
2018-01-23 14:39:09 -08:00
ld a, [wTilesetBank]
ld e, a
2015-11-12 21:49:27 -08:00
ldh a, [rSVBK]
push af
ld a, BANK(wDecompressScratch)
ldh [rSVBK], a
2015-11-12 21:49:27 -08:00
ld a, e
ld de, wDecompressScratch
call FarDecompress
2015-11-12 21:49:27 -08:00
ld hl, wDecompressScratch
2017-12-28 04:32:33 -08:00
ld de, vTiles2
2015-11-05 16:13:09 -08:00
ld bc, $60 tiles
call CopyBytes
2015-11-12 21:49:27 -08:00
ldh a, [rVBK]
push af
ld a, BANK(vTiles5)
ldh [rVBK], a
2015-11-12 21:49:27 -08:00
2017-12-09 11:22:58 -08:00
ld hl, wDecompressScratch + $60 tiles
ld de, vTiles5
2015-11-05 16:13:09 -08:00
ld bc, $60 tiles
call CopyBytes
2015-11-12 21:49:27 -08:00
pop af
ldh [rVBK], a
2015-11-12 21:49:27 -08:00
pop af
ldh [rSVBK], a
; These tilesets support dynamic per-mapgroup roof tiles.
2018-01-23 14:39:09 -08:00
ld a, [wMapTileset]
2018-01-17 11:50:34 -08:00
cp TILESET_JOHTO
2015-11-12 21:49:27 -08:00
jr z, .load_roof
2018-01-17 11:50:34 -08:00
cp TILESET_JOHTO_MODERN
2015-11-12 21:49:27 -08:00
jr z, .load_roof
cp TILESET_BATTLE_TOWER_OUTSIDE
jr z, .load_roof
jr .skip_roof
.load_roof
2017-12-24 09:47:30 -08:00
farcall LoadMapGroupRoof
2015-11-12 21:49:27 -08:00
.skip_roof
xor a
ldh [hTileAnimFrame], a
ret
2018-06-24 07:09:41 -07:00
BufferScreen::
2015-12-13 17:33:56 -08:00
ld hl, wOverworldMapAnchor
ld a, [hli]
ld h, [hl]
ld l, a
2015-12-14 08:12:18 -08:00
ld de, wScreenSave
ld c, SCREEN_META_HEIGHT
ld b, SCREEN_META_WIDTH
2016-01-12 09:46:18 -08:00
.row
push bc
push hl
2016-01-12 09:46:18 -08:00
.col
ld a, [hli]
ld [de], a
inc de
dec b
2016-01-12 09:46:18 -08:00
jr nz, .col
pop hl
2018-01-23 14:39:09 -08:00
ld a, [wMapWidth]
add 6
ld c, a
ld b, 0
add hl, bc
pop bc
dec c
2016-01-12 09:46:18 -08:00
jr nz, .row
ret
2018-06-24 07:09:41 -07:00
SaveScreen::
2015-12-13 17:33:56 -08:00
ld hl, wOverworldMapAnchor
ld a, [hli]
ld h, [hl]
ld l, a
2015-12-14 08:12:18 -08:00
ld de, wScreenSave
2018-01-23 14:39:09 -08:00
ld a, [wMapWidth]
2015-12-14 08:12:18 -08:00
add 6
ldh [hMapObjectIndexBuffer], a
ld a, [wPlayerStepDirection]
and a
2015-11-12 21:49:27 -08:00
jr z, .down
cp UP
jr z, .up
cp LEFT
jr z, .left
cp RIGHT
jr z, .right
ret
2015-11-12 21:49:27 -08:00
.up
ld de, wScreenSave + SCREEN_META_WIDTH
ldh a, [hMapObjectIndexBuffer]
ld c, a
ld b, 0
add hl, bc
2015-11-12 21:49:27 -08:00
jr .vertical
2015-11-12 21:49:27 -08:00
.down
2015-12-14 08:12:18 -08:00
ld de, wScreenSave
2015-11-12 21:49:27 -08:00
.vertical
ld b, SCREEN_META_WIDTH
ld c, SCREEN_META_HEIGHT - 1
2015-12-14 08:12:18 -08:00
jr SaveScreen_LoadNeighbor
2015-11-12 21:49:27 -08:00
.left
2015-12-14 08:12:18 -08:00
ld de, wScreenSave + 1
inc hl
2015-11-12 21:49:27 -08:00
jr .horizontal
2015-11-12 21:49:27 -08:00
.right
2015-12-14 08:12:18 -08:00
ld de, wScreenSave
2015-11-12 21:49:27 -08:00
.horizontal
ld b, SCREEN_META_WIDTH - 1
ld c, SCREEN_META_HEIGHT
2015-12-14 08:12:18 -08:00
jr SaveScreen_LoadNeighbor
2014-01-26 19:07:19 -08:00
2018-06-24 07:09:41 -07:00
LoadNeighboringBlockData::
2015-12-13 17:33:56 -08:00
ld hl, wOverworldMapAnchor
ld a, [hli]
ld h, [hl]
ld l, a
2018-01-23 14:39:09 -08:00
ld a, [wMapWidth]
2015-12-14 08:12:18 -08:00
add 6
ldh [hConnectionStripLength], a
2015-12-14 08:12:18 -08:00
ld de, wScreenSave
ld b, SCREEN_META_WIDTH
ld c, SCREEN_META_HEIGHT
2018-06-24 07:09:41 -07:00
SaveScreen_LoadNeighbor::
2015-12-14 08:12:18 -08:00
.row
push bc
push hl
push de
2015-12-14 08:12:18 -08:00
.col
ld a, [de]
inc de
ld [hli], a
dec b
2015-12-14 08:12:18 -08:00
jr nz, .col
pop de
ld a, e
2015-12-14 08:12:18 -08:00
add 6
ld e, a
2015-11-12 21:49:27 -08:00
jr nc, .okay
inc d
2015-11-12 21:49:27 -08:00
.okay
pop hl
ldh a, [hConnectionStripLength]
ld c, a
2015-12-14 08:12:18 -08:00
ld b, 0
add hl, bc
pop bc
dec c
2015-12-14 08:12:18 -08:00
jr nz, .row
ret
2018-06-24 07:09:41 -07:00
GetMovementPermissions::
xor a
2018-01-23 14:39:09 -08:00
ld [wTilePermissions], a
2016-01-06 04:44:50 -08:00
call .LeftRight
call .UpDown
2015-11-12 21:49:27 -08:00
; get coords of current tile
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapX]
ld d, a
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapY]
ld e, a
2015-11-12 21:49:27 -08:00
call GetCoordTile
2018-01-23 14:39:09 -08:00
ld [wPlayerStandingTile], a
2016-01-06 04:44:50 -08:00
call .CheckHiNybble
ret nz
2015-11-12 21:49:27 -08:00
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingTile]
and 7
2016-01-06 04:44:50 -08:00
ld hl, .MovementPermissionsData
add l
ld l, a
ld a, 0
adc h
ld h, a
ld a, [hl]
2018-01-23 14:39:09 -08:00
ld hl, wTilePermissions
or [hl]
ld [hl], a
ret
2018-06-24 07:09:41 -07:00
.MovementPermissionsData:
db DOWN_MASK
db UP_MASK
db LEFT_MASK
db RIGHT_MASK
db DOWN_MASK | RIGHT_MASK
db UP_MASK | RIGHT_MASK
db DOWN_MASK | LEFT_MASK
db UP_MASK | LEFT_MASK
.UpDown:
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapX]
ld d, a
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapY]
ld e, a
2015-11-12 21:49:27 -08:00
push de
inc e
2015-11-12 21:49:27 -08:00
call GetCoordTile
2018-01-23 14:39:09 -08:00
ld [wTileDown], a
2016-01-06 04:44:50 -08:00
call .Down
2015-11-12 21:49:27 -08:00
pop de
dec e
2015-11-12 21:49:27 -08:00
call GetCoordTile
2018-01-23 14:39:09 -08:00
ld [wTileUp], a
2016-01-06 04:44:50 -08:00
call .Up
ret
.LeftRight:
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapX]
ld d, a
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapY]
ld e, a
2015-11-12 21:49:27 -08:00
push de
dec d
2015-11-12 21:49:27 -08:00
call GetCoordTile
2018-01-23 14:39:09 -08:00
ld [wTileLeft], a
2016-01-06 04:44:50 -08:00
call .Left
2015-11-12 21:49:27 -08:00
pop de
inc d
2015-11-12 21:49:27 -08:00
call GetCoordTile
2018-01-23 14:39:09 -08:00
ld [wTileRight], a
2016-01-06 04:44:50 -08:00
call .Right
ret
.Down:
2016-01-06 04:44:50 -08:00
call .CheckHiNybble
ret nz
2018-01-23 14:39:09 -08:00
ld a, [wTileDown]
and %111
cp COLL_UP_WALL & %111 ; COLL_UP_BUOY & %111
2016-01-06 04:44:50 -08:00
jr z, .ok_down
cp COLL_UP_RIGHT_WALL & %111 ; COLL_UP_RIGHT_BUOY & %111
2016-01-06 04:44:50 -08:00
jr z, .ok_down
cp COLL_UP_LEFT_WALL & %111 ; COLL_UP_LEFT_BUOY & %111
ret nz
2016-01-06 04:44:50 -08:00
.ok_down
2018-01-23 14:39:09 -08:00
ld a, [wTilePermissions]
2015-11-12 21:49:27 -08:00
or FACE_DOWN
2018-01-23 14:39:09 -08:00
ld [wTilePermissions], a
ret
.Up:
2016-01-06 04:44:50 -08:00
call .CheckHiNybble
ret nz
2018-01-23 14:39:09 -08:00
ld a, [wTileUp]
and %111
cp COLL_DOWN_WALL & %111 ; COLL_DOWN_BUOY & %111
2016-01-06 04:44:50 -08:00
jr z, .ok_up
cp COLL_DOWN_RIGHT_WALL & %111 ; COLL_DOWN_RIGHT_BUOY & %111
2016-01-06 04:44:50 -08:00
jr z, .ok_up
cp COLL_DOWN_LEFT_WALL & %111 ; COLL_DOWN_LEFT_BUOY & %111
ret nz
2016-01-06 04:44:50 -08:00
.ok_up
2018-01-23 14:39:09 -08:00
ld a, [wTilePermissions]
2015-11-12 21:49:27 -08:00
or FACE_UP
2018-01-23 14:39:09 -08:00
ld [wTilePermissions], a
ret
.Right:
2016-01-06 04:44:50 -08:00
call .CheckHiNybble
ret nz
2018-01-23 14:39:09 -08:00
ld a, [wTileRight]
and %111
cp COLL_LEFT_WALL & %111 ; COLL_LEFT_BUOY & %111
2016-01-06 04:44:50 -08:00
jr z, .ok_right
cp COLL_DOWN_LEFT_WALL & %111 ; COLL_DOWN_LEFT_BUOY & %111
2016-01-06 04:44:50 -08:00
jr z, .ok_right
cp COLL_UP_LEFT_WALL & %111 ; COLL_UP_LEFT_BUOY & %111
ret nz
2016-01-06 04:44:50 -08:00
.ok_right
2018-01-23 14:39:09 -08:00
ld a, [wTilePermissions]
2015-11-12 21:49:27 -08:00
or FACE_RIGHT
2018-01-23 14:39:09 -08:00
ld [wTilePermissions], a
ret
.Left:
2016-01-06 04:44:50 -08:00
call .CheckHiNybble
ret nz
2018-01-23 14:39:09 -08:00
ld a, [wTileLeft]
and %111
cp COLL_RIGHT_WALL & %111 ; COLL_RIGHT_BUOY & %111
2016-01-06 04:44:50 -08:00
jr z, .ok_left
cp COLL_DOWN_RIGHT_WALL & %111 ; COLL_DOWN_RIGHT_BUOY & %111
2016-01-06 04:44:50 -08:00
jr z, .ok_left
cp COLL_UP_RIGHT_WALL & %111 ; COLL_UP_RIGHT_BUOY & %111
ret nz
2016-01-06 04:44:50 -08:00
.ok_left
2018-01-23 14:39:09 -08:00
ld a, [wTilePermissions]
2015-11-12 21:49:27 -08:00
or FACE_LEFT
2018-01-23 14:39:09 -08:00
ld [wTilePermissions], a
ret
.CheckHiNybble:
and $f0
2018-06-28 19:35:29 -07:00
cp HI_NYBBLE_SIDE_WALLS
ret z
cp HI_NYBBLE_SIDE_BUOYS
ret
2018-06-24 07:09:41 -07:00
GetFacingTileCoord::
; Return map coordinates in (d, e) and tile id in a
; of the tile the player is facing.
2018-01-23 14:39:09 -08:00
ld a, [wPlayerDirection]
and %1100
srl a
srl a
ld l, a
ld h, 0
2016-01-06 04:44:50 -08:00
add hl, hl
add hl, hl
ld de, .Directions
add hl, de
ld d, [hl]
inc hl
ld e, [hl]
inc hl
ld a, [hli]
ld h, [hl]
ld l, a
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapX]
add d
ld d, a
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapY]
add e
ld e, a
ld a, [hl]
ret
.Directions:
; x, y
db 0, 1
2018-01-23 14:39:09 -08:00
dw wTileDown
db 0, -1
2018-01-23 14:39:09 -08:00
dw wTileUp
db -1, 0
2018-01-23 14:39:09 -08:00
dw wTileLeft
db 1, 0
2018-01-23 14:39:09 -08:00
dw wTileRight
2018-06-24 07:09:41 -07:00
GetCoordTile::
2015-11-12 21:49:27 -08:00
; Get the collision byte for tile d, e
call GetBlockLocation
ld a, [hl]
and a
jr z, .nope
ld l, a
ld h, $0
2016-01-06 04:44:50 -08:00
add hl, hl
add hl, hl
2018-01-23 14:39:09 -08:00
ld a, [wTilesetCollisionAddress]
ld c, a
2018-01-23 14:39:09 -08:00
ld a, [wTilesetCollisionAddress + 1]
ld b, a
add hl, bc
rr d
jr nc, .nocarry
inc hl
.nocarry
rr e
jr nc, .nocarry2
inc hl
2016-01-06 04:44:50 -08:00
inc hl
.nocarry2
2018-01-23 14:39:09 -08:00
ld a, [wTilesetCollisionBank]
call GetFarByte
ret
.nope
ld a, -1
ret
2018-06-24 07:09:41 -07:00
GetBlockLocation::
2018-01-23 14:39:09 -08:00
ld a, [wMapWidth]
add 6
ld c, a
ld b, 0
ld hl, wOverworldMapBlocks + 1
add hl, bc
ld a, e
srl a
jr z, .nope
and a
.loop
srl a
jr nc, .ok
add hl, bc
.ok
sla c
rl b
and a
jr nz, .loop
.nope
ld c, d
srl c
ld b, 0
add hl, bc
ret
2018-06-24 07:09:41 -07:00
CheckFacingBGEvent::
call GetFacingTileCoord
; Load facing into b.
ld b, a
; Convert the coordinates at de to within-boundaries coordinates.
ld a, d
sub 4
ld d, a
ld a, e
sub 4
ld e, a
; If there are no BG events, we don't need to be here.
2018-09-09 12:09:51 -07:00
ld a, [wCurMapBGEventCount]
and a
ret z
2015-11-12 08:10:19 -08:00
ld c, a
ldh a, [hROMBank]
push af
call SwitchToMapScriptsBank
call CheckIfFacingTileCoordIsBGEvent
pop hl
ld a, h
rst Bankswitch
ret
2018-06-24 07:09:41 -07:00
CheckIfFacingTileCoordIsBGEvent::
; Checks to see if you are facing a BG event. If so, copies it into wCurBGEvent and sets carry.
2018-09-09 12:09:51 -07:00
ld hl, wCurMapBGEventsPointer
ld a, [hli]
ld h, [hl]
ld l, a
.loop
push hl
ld a, [hli]
cp e
jr nz, .next
ld a, [hli]
cp d
jr nz, .next
jr .copysign
.next
pop hl
2019-03-03 11:19:16 -08:00
ld a, BG_EVENT_SIZE
add l
ld l, a
jr nc, .nocarry
inc h
.nocarry
dec c
jr nz, .loop
xor a
ret
.copysign
pop hl
ld de, wCurBGEvent
2019-03-03 11:19:16 -08:00
ld bc, BG_EVENT_SIZE
call CopyBytes
scf
ret
2018-06-24 07:09:41 -07:00
CheckCurrentMapCoordEvents::
; If there are no coord events, we don't need to be here.
2018-09-09 12:09:51 -07:00
ld a, [wCurMapCoordEventCount]
and a
ret z
; Copy the coord event count into c.
ld c, a
ldh a, [hROMBank]
push af
call SwitchToMapScriptsBank
call .CoordEventCheck
pop hl
ld a, h
rst Bankswitch
ret
.CoordEventCheck:
; Checks to see if you are standing on a coord event. If yes, copies the event to wCurCoordEvent and sets carry.
2018-09-09 12:09:51 -07:00
ld hl, wCurMapCoordEventsPointer
ld a, [hli]
ld h, [hl]
ld l, a
; Load the active scene ID into b
call CheckScenes
ld b, a
; Load your current coordinates into de. This will be used to check if your position is in the coord event table for the current map.
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapX]
sub 4
ld d, a
2018-01-23 14:39:09 -08:00
ld a, [wPlayerStandingMapY]
sub 4
ld e, a
.loop
push hl
ld a, [hli]
cp b
jr z, .got_id
cp -1
jr nz, .next
.got_id
ld a, [hli]
cp e
jr nz, .next
ld a, [hli]
cp d
jr nz, .next
jr .copy_coord_event
.next
pop hl
2019-03-03 11:19:16 -08:00
ld a, COORD_EVENT_SIZE
add l
ld l, a
jr nc, .nocarry
inc h
.nocarry
dec c
jr nz, .loop
xor a
ret
.copy_coord_event
pop hl
ld de, wCurCoordEvent
2019-03-03 11:19:16 -08:00
ld bc, COORD_EVENT_SIZE
call CopyBytes
scf
ret
2018-06-24 07:09:41 -07:00
FadeToMenu::
xor a
ldh [hBGMapMode], a
call LoadStandardMenuHeader
farcall FadeOutPalettes
call ClearSprites
2015-10-24 07:34:19 -07:00
call DisableSpriteUpdates
ret
2018-06-24 07:09:41 -07:00
CloseSubmenu::
2015-11-11 20:38:57 -08:00
call ClearBGPalettes
2015-12-18 17:07:09 -08:00
call ReloadTilesetAndPalettes
call UpdateSprites
2015-09-09 16:27:07 -07:00
call Call_ExitMenu
2015-12-09 08:38:40 -08:00
call ret_d90
2016-01-06 04:44:50 -08:00
jr FinishExitMenu
2018-06-24 07:09:41 -07:00
ExitAllMenus::
2015-11-11 20:38:57 -08:00
call ClearBGPalettes
2015-09-09 16:27:07 -07:00
call Call_ExitMenu
2015-12-18 17:07:09 -08:00
call ReloadTilesetAndPalettes
call UpdateSprites
2015-12-09 08:38:40 -08:00
call ret_d90
2018-06-24 07:09:41 -07:00
FinishExitMenu::
ld b, SCGB_MAPPALS
call GetSGBLayout
2017-12-24 09:47:30 -08:00
farcall LoadOW_BGPal7
2015-12-11 13:59:40 -08:00
call WaitBGMap2
farcall FadeInPalettes
2015-10-24 07:34:19 -07:00
call EnableSpriteUpdates
ret
ReturnToMapWithSpeechTextbox::
push af
ld a, $1
2015-11-25 07:16:29 -08:00
ld [wSpriteUpdatesEnabled], a
2015-11-11 20:38:57 -08:00
call ClearBGPalettes
call ClearSprites
2015-12-18 17:07:09 -08:00
call ReloadTilesetAndPalettes
hlcoord 0, 12
2015-11-05 11:06:03 -08:00
lb bc, 4, 18
2019-04-08 05:15:10 -07:00
call Textbox
2018-01-23 14:39:09 -08:00
ld hl, wVramState
set 0, [hl]
call UpdateSprites
2015-12-11 13:59:40 -08:00
call WaitBGMap2
ld b, SCGB_MAPPALS
call GetSGBLayout
2017-12-24 09:47:30 -08:00
farcall LoadOW_BGPal7
call UpdateTimePals
call DelayFrame
ld a, $1
ldh [hMapAnims], a
pop af
ret
2018-06-24 07:09:41 -07:00
ReloadTilesetAndPalettes::
call DisableLCD
call ClearSprites
farcall RefreshSprites
2015-11-04 13:14:27 -08:00
call LoadStandardFont
call LoadFontsExtra
ldh a, [hROMBank]
push af
2018-01-23 14:39:09 -08:00
ld a, [wMapGroup]
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wMapNumber]
ld c, a
call SwitchToAnyMapAttributesBank
2017-12-24 09:47:30 -08:00
farcall UpdateTimeOfDayPal
2015-11-11 11:12:46 -08:00
call OverworldTextModeSwitch
call LoadTilesetGFX
2014-05-21 13:21:46 -07:00
ld a, 9
call SkipMusic
pop af
rst Bankswitch
call EnableLCD
ret
2018-06-24 07:09:41 -07:00
GetMapPointer::
2018-01-23 14:39:09 -08:00
ld a, [wMapGroup]
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wMapNumber]
ld c, a
GetAnyMapPointer::
; Prior to calling this function, you must have switched banks so that
; MapGroupPointers is visible.
; inputs:
; b = map group, c = map number
; outputs:
; hl points to the map within its group
push bc ; save map number for later
; get pointer to map group
dec b
ld c, b
ld b, 0
ld hl, MapGroupPointers
add hl, bc
2016-01-06 04:44:50 -08:00
add hl, bc
ld a, [hli]
ld h, [hl]
ld l, a
pop bc ; restore map number
; find the cth map within the group
dec c
ld b, 0
ld a, 9
call AddNTimes
ret
GetMapField::
; Extract data from the current map's group entry.
; inputs:
; de = offset of desired data within the map (a MAP_* constant)
; outputs:
; bc = data from the current map's field
; (e.g., de = MAP_TILESET would return a pointer to the tileset id)
2018-01-23 14:39:09 -08:00
ld a, [wMapGroup]
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wMapNumber]
ld c, a
GetAnyMapField::
; bankswitch
ldh a, [hROMBank]
push af
ld a, BANK(MapGroupPointers)
rst Bankswitch
call GetAnyMapPointer
add hl, de
ld c, [hl]
inc hl
ld b, [hl]
; bankswitch back
pop af
rst Bankswitch
ret
2018-06-24 07:09:41 -07:00
SwitchToMapAttributesBank::
2018-01-23 14:39:09 -08:00
ld a, [wMapGroup]
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wMapNumber]
ld c, a
2018-06-24 07:09:41 -07:00
SwitchToAnyMapAttributesBank::
call GetAnyMapAttributesBank
rst Bankswitch
ret
2018-06-24 07:09:41 -07:00
GetMapAttributesBank::
2018-01-23 14:39:09 -08:00
ld a, [wMapGroup]
ld b, a
2018-01-23 14:39:09 -08:00
ld a, [wMapNumber]
ld c, a
2018-06-24 07:09:41 -07:00
GetAnyMapAttributesBank::
push hl
push de
ld de, MAP_MAPATTRIBUTES_BANK
call GetAnyMapField
ld a, c
pop de
pop hl
ret
2018-06-24 07:09:41 -07:00
CopyMapPartial::
; Copy map data bank, tileset, environment, and map data address
; from the current map's entry within its group.
ldh a, [hROMBank]
push af
2014-12-26 15:31:48 -08:00
ld a, BANK(MapGroupPointers)
rst Bankswitch
2015-11-12 08:10:19 -08:00
call GetMapPointer
ld de, wMapPartial
ld bc, wMapPartialEnd - wMapPartial
call CopyBytes
2015-11-12 08:10:19 -08:00
pop af
rst Bankswitch
ret
2018-06-24 07:09:41 -07:00
SwitchToMapScriptsBank::
2018-01-23 14:39:09 -08:00
ld a, [wMapScriptsBank]
rst Bankswitch
ret
2018-06-24 07:09:41 -07:00
GetMapScriptsBank::
2018-01-23 14:39:09 -08:00
ld a, [wMapScriptsBank]
ret
2018-06-24 07:09:41 -07:00
GetAnyMapBlocksBank::
; Return the blockdata bank for group b map c.
push hl
push de
push bc
push bc
ld de, MAP_MAPATTRIBUTES
call GetAnyMapField
ld l, c
ld h, b
pop bc
push hl
ld de, MAP_MAPATTRIBUTES_BANK
call GetAnyMapField
pop hl
ld de, MAP_MAPATTRIBUTES ; blockdata bank
add hl, de
ld a, c
call GetFarByte
rst Bankswitch
pop bc
pop de
pop hl
ret
GetMapAttributesPointer::
; returns the current map's data pointer in hl.
push bc
push de
ld de, MAP_MAPATTRIBUTES
call GetMapField
ld l, c
ld h, b
pop de
pop bc
ret
2018-06-24 07:09:41 -07:00
GetMapEnvironment::
push hl
push de
push bc
ld de, MAP_ENVIRONMENT
call GetMapField
ld a, c
pop bc
pop de
pop hl
ret
ret ; unused
2018-06-24 07:09:41 -07:00
GetAnyMapEnvironment::
push hl
push de
push bc
ld de, MAP_ENVIRONMENT
call GetAnyMapField
ld a, c
pop bc
pop de
pop hl
ret
2018-06-24 07:09:41 -07:00
GetAnyMapTileset::
ld de, MAP_TILESET
call GetAnyMapField
ld a, c
ret
GetWorldMapLocation::
; given a map group/id in bc, return its location on the Pokégear map.
push hl
push de
push bc
2015-11-12 08:10:19 -08:00
ld de, MAP_LOCATION
call GetAnyMapField
ld a, c
2015-11-12 08:10:19 -08:00
pop bc
pop de
pop hl
ret
2018-06-24 07:09:41 -07:00
GetMapMusic::
push hl
push bc
ld de, MAP_MUSIC
call GetMapField
ld a, c
2014-05-21 13:21:46 -07:00
cp MUSIC_MAHOGANY_MART
jr z, .mahoganymart
2017-12-10 17:50:08 -08:00
bit RADIO_TOWER_MUSIC_F, c
2014-05-21 13:21:46 -07:00
jr nz, .radiotower
2017-12-24 09:47:30 -08:00
farcall Function8b342
ld e, c
2014-05-21 13:21:46 -07:00
ld d, 0
.done
pop bc
pop hl
ret
2014-05-21 13:21:46 -07:00
.radiotower
ld a, [wStatusFlags2]
2018-01-22 12:40:43 -08:00
bit STATUSFLAGS2_ROCKETS_IN_RADIO_TOWER_F, a
2014-05-21 13:21:46 -07:00
jr z, .clearedradiotower
ld de, MUSIC_ROCKET_OVERTURE
jr .done
2014-05-21 13:21:46 -07:00
.clearedradiotower
; the rest of the byte
ld a, c
2017-12-10 17:50:08 -08:00
and RADIO_TOWER_MUSIC - 1
ld e, a
2014-05-21 13:21:46 -07:00
ld d, 0
jr .done
2014-05-21 13:21:46 -07:00
.mahoganymart
ld a, [wStatusFlags2]
2018-01-22 12:40:43 -08:00
bit STATUSFLAGS2_ROCKETS_IN_MAHOGANY_F, a
2014-05-21 13:21:46 -07:00
jr z, .clearedmahogany
ld de, MUSIC_ROCKET_HIDEOUT
jr .done
2014-05-21 13:21:46 -07:00
.clearedmahogany
ld de, MUSIC_CHERRYGROVE_CITY
jr .done
2018-06-24 07:09:41 -07:00
GetMapTimeOfDay::
call GetPhoneServiceTimeOfDayByte
and $f
ret
2018-06-24 07:09:41 -07:00
GetMapPhoneService::
call GetPhoneServiceTimeOfDayByte
and $f0
swap a
ret
2018-06-24 07:09:41 -07:00
GetPhoneServiceTimeOfDayByte::
push hl
push bc
2015-11-12 08:10:19 -08:00
ld de, MAP_PALETTE
call GetMapField
ld a, c
2015-11-12 08:10:19 -08:00
pop bc
pop hl
ret
2018-06-24 07:09:41 -07:00
GetFishingGroup::
push de
push hl
push bc
2015-11-12 08:10:19 -08:00
ld de, MAP_FISHGROUP
call GetMapField
ld a, c
2015-11-12 08:10:19 -08:00
pop bc
pop hl
pop de
ret
2018-06-24 07:09:41 -07:00
LoadTileset::
push hl
push bc
2013-12-11 14:23:33 -08:00
ld hl, Tilesets
2018-01-23 14:39:09 -08:00
ld bc, wTilesetEnd - wTileset
ld a, [wMapTileset]
call AddNTimes
2013-12-11 14:23:33 -08:00
2018-01-23 14:39:09 -08:00
ld de, wTilesetBank
ld bc, wTilesetEnd - wTileset
2013-12-11 14:23:33 -08:00
ld a, BANK(Tilesets)
call FarCopyBytes
2013-12-11 14:23:33 -08:00
pop bc
pop hl
ret