loadmapblock: MACRO
	ld a, [de]
	inc de
	and a
	jr nz, .notMapBorderBlock\@
	ld a, [hMapBorderBlock]
.notMapBorderBlock\@
ENDM

nocarry: MACRO
	jr nc, .noCarry\@
	dec h
.noCarry\@
ENDM

addmapwidthoffset: MACRO
	ld a, [hMapWidthPlus6]
	add e
	ld e, a
	jr nc, .noCarry\@
	inc d
.noCarry\@
ENDM

_LoadMapPart::
	ld a, [wOverworldMapAnchor]
	ld e, a
	ld a, [wOverworldMapAnchor + 1]
	ld d, a
	ld a, [MapBorderBlock]
	ld [hMapBorderBlock], a
	ld a, [MapWidth]
	inc a
	ld [hMapWidthPlus6], a
	ld a, [wMetatileStandingY]
	add a
	ld b, a
	ld a, [wMetatileStandingX]
	or b
	ld b, a
	ld a, BANK(wDecompressedMetatiles)
	call StackCallInWRAMBankA

.Function:
	coord hl, 0, 0
	ld a, b
	and a
	jp z, .BlockY0BlockX0
	dec a
	jp z, .BlockY0BlockX1
	dec a
	jp z, .BlockY1BlockX0
; block y1, block x1

.BlockY1BlockX1:
	call .LoadOneQuarterBottomRightMetatile
	coord hl, 2, 0
	ld bc, SCREEN_WIDTH - 4
	call .LoadOneRowOf4BottomHalfBlocks
	coord hl, 18, 0
	call .LoadOneQuarterBottomLeftMetatile
	ld hl, hMapWidthPlus6
	dec [hl]
	addmapwidthoffset

	coord hl, 0, 2
	call .LoadOneHalfRightMetatile
	coord hl, 2, 2
	call .LoadOneRowOf4Blocks
	coord hl, 18, 2
	call .LoadOneHalfLeftMetatile
	addmapwidthoffset

	coord hl, 0, 6
	call .LoadOneHalfRightMetatile
	coord hl, 2, 6
	call .LoadOneRowOf4Blocks
	coord hl, 18, 6
	call .LoadOneHalfLeftMetatile
	addmapwidthoffset
	
	coord hl, 0, 10
	call .LoadOneHalfRightMetatile
	coord hl, 2, 10
	call .LoadOneRowOf4Blocks
	coord hl, 18, 10
	call .LoadOneHalfLeftMetatile
	addmapwidthoffset
	
	coord hl, 0, 14
	call .LoadOneHalfRightMetatile
	coord hl, 2, 14
	call .LoadOneRowOf4Blocks
	coord hl, 18, 14

.LoadOneHalfLeftMetatile:
	ld a, [de]
	inc de
	and a
	jr nz, .notMapBorderBlock2
	ld a, [hMapBorderBlock]
.notMapBorderBlock2
	ld bc, SCREEN_WIDTH - 1
	push de
	swap a
	ld d, a
	and $f0
.LoadOneHalfRightMetatile_Entrypoint
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a

	rept 3
	ld a, [de]
	inc e
	ld [hli], a
	ld a, [de]
	inc e
	inc e
	inc e
	ld [hl], a
	add hl, bc
	endr

	ld a, [de]
	inc e
	ld [hli], a
	ld a, [de]
	ld [hl], a
	pop de
	ret

.LoadOneRowOf5Blocks:
	ld bc, SCREEN_WIDTH - 4

.LoadOneRowOf5Blocks_NoBCLoad:
	call ._LoadOneRowOf5Blocks
	ld a, [hMapWidthPlus6]
	add e
	ld e, a
	ret nc
	inc d
	ret

.LoadOneHalfRightMetatile:
	ld a, [de]
	inc de
	and a
	jr nz, .notMapBorderBlock1
	ld a, [hMapBorderBlock]
.notMapBorderBlock1
	ld bc, SCREEN_WIDTH - 1
	push de
	swap a
	ld d, a
	and $f0
	inc a
	inc a
	jr .LoadOneHalfRightMetatile_Entrypoint

.BlockY1BlockX0:
	call .LoadOneHalfRowOf5LowerHalfBlocks
	addmapwidthoffset
	coord hl, 0, 2
	call .LoadOneRowOf5Blocks_NoBCLoad
	coord hl, 0, 6
	call .LoadOneRowOf5Blocks_NoBCLoad
	coord hl, 0, 10
	call .LoadOneRowOf5Blocks_NoBCLoad
	coord hl, 0, 14
	jr .LoadOneRowOf5Blocks_NoBCLoad

.BlockY0BlockX0:
	call .LoadOneRowOf5Blocks
	coord hl, 0, 4
	call .LoadOneRowOf5Blocks_NoBCLoad
	coord hl, 0, 8
	call .LoadOneRowOf5Blocks_NoBCLoad
	coord hl, 0, 12
	call .LoadOneRowOf5Blocks_NoBCLoad
	coord hl, 0, 16
; fallthrough

.LoadOneRowOf5UpperHalfBlocks:
	ld a, [de]
	inc de
	and a
	jr nz, .notMapBorderBlock7
	ld a, [hMapBorderBlock]
.notMapBorderBlock7
	push de
	swap a
	ld d, a
	and $f0
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a
	; copy the 4x4 metatile

; row 1
	rept 4
		ld a, [de]
		ld [hli], a
		inc e
	endr
	add hl, bc
; row 2
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	; Next metatile
	ld a, l
	sub (SCREEN_WIDTH * 1) - 1
	ld l, a
	nocarry
.LoadOneRowOf4UpperHalfBlocks:
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; row 1
	rept 4
		ld a, [de]
		ld [hli], a
		inc e
	endr
	add hl, bc
; row 2
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	; Next metatile
	ld a, l
	sub (SCREEN_WIDTH * 1) - 1
	ld l, a
	nocarry
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; row 1
	rept 4
		ld a, [de]
		ld [hli], a
		inc e
	endr
	add hl, bc
; row 2
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	; Next metatile
	ld a, l
	sub (SCREEN_WIDTH * 1) - 1
	ld l, a
	nocarry
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; row 1
	rept 4
		ld a, [de]
		ld [hli], a
		inc e
	endr
	add hl, bc
; row 2
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	; Next metatile
	ld a, l
	sub (SCREEN_WIDTH * 1) - 1
	ld l, a
	nocarry
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; row 1
	rept 4
		ld a, [de]
		ld [hli], a
		inc e
	endr
	add hl, bc
; row 2
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	ret

.LoadOneRowOf4Blocks:
	ld bc, SCREEN_WIDTH - 4
	jr .LoadOneRowOf4Blocks_Entrypoint

._LoadOneRowOf5Blocks:
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; rows 1 - 3
	rept 3
		rept 4
			ld a, [de]
			ld [hli], a
			inc e
		endr
		add hl, bc		
	endr
; row 4
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	ld a, l
	sub (SCREEN_WIDTH * 3) - 1
	ld l, a
	nocarry
.LoadOneRowOf4Blocks_Entrypoint:
; Next metatile
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; rows 1 - 3
	rept 3
		rept 4
			ld a, [de]
			ld [hli], a
			inc e
		endr
		add hl, bc		
	endr
; row 4
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	ld a, l
	sub (SCREEN_WIDTH * 3) - 1
	ld l, a
	nocarry
; Next metatile
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; rows 1 - 3
	rept 3
		rept 4
			ld a, [de]
			ld [hli], a
			inc e
		endr
		add hl, bc		
	endr
; row 4
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	ld a, l
	sub (SCREEN_WIDTH * 3) - 1
	ld l, a
	nocarry
; Next metatile
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; rows 1 - 3
	rept 3
		rept 4
			ld a, [de]
			ld [hli], a
			inc e
		endr
		add hl, bc		
	endr
; row 4
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	ld a, l
	sub (SCREEN_WIDTH * 3) - 1
	ld l, a
	nocarry
	loadmapblock
; Next metatile
	push de
	swap a
	ld d, a
	and $f0
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; rows 1 - 3
	rept 3
		rept 4
			ld a, [de]
			ld [hli], a
			inc e
		endr
		add hl, bc		
	endr
; row 4
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	ret

.LoadOneHalfRowOf5LowerHalfBlocks:
	ld bc, SCREEN_WIDTH - 4
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	add 8
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; row 1
	rept 4
		ld a, [de]
		ld [hli], a
		inc e
	endr
	add hl, bc
; row 2
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	; Next metatile

	ld a, l
	sub (SCREEN_WIDTH * 1) - 1
	ld l, a
	nocarry
.LoadOneRowOf4BottomHalfBlocks:
	ld bc, SCREEN_WIDTH - 4
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	add 8
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; row 1
	rept 4
		ld a, [de]
		ld [hli], a
		inc e
	endr
	add hl, bc
; row 2
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	; Next metatile

	ld a, l
	sub (SCREEN_WIDTH * 1) - 1
	ld l, a
	nocarry
	ld bc, SCREEN_WIDTH - 4
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	add 8
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; row 1
	rept 4
		ld a, [de]
		ld [hli], a
		inc e
	endr
	add hl, bc
; row 2
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	; Next metatile

	ld a, l
	sub (SCREEN_WIDTH * 1) - 1
	ld l, a
	nocarry
	ld bc, SCREEN_WIDTH - 4
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	add 8
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; row 1
	rept 4
		ld a, [de]
		ld [hli], a
		inc e
	endr
	add hl, bc
; row 2
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	; Next metatile

	ld a, l
	sub (SCREEN_WIDTH * 1) - 1
	ld l, a
	nocarry
	ld bc, SCREEN_WIDTH - 4
	loadmapblock
	push de
	swap a
	ld d, a
	and $f0
	add 8
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a	
	; copy the 4x4 metatile

; row 1
	rept 4
		ld a, [de]
		ld [hli], a
		inc e
	endr
	add hl, bc
; row 2
	rept 3
		ld a, [de]
		ld [hli], a
		inc e
	endr
	ld a, [de]
	ld [hl], a
	pop de
	; Next metatile
	ret

.BlockY0BlockX1:
	call .LoadOneHalfRightMetatile
	coord hl, 2, 0
	call .LoadOneRowOf4Blocks
	coord hl, 18, 0
	call .LoadOneHalfLeftMetatile
	ld hl, hMapWidthPlus6
	dec [hl]
	addmapwidthoffset

	coord hl, 0, 4
	call .LoadOneHalfRightMetatile
	coord hl, 2, 4
	call .LoadOneRowOf4Blocks
	coord hl, 18, 4
	call .LoadOneHalfLeftMetatile
	addmapwidthoffset

	coord hl, 0, 8
	call .LoadOneHalfRightMetatile
	coord hl, 2, 8
	call .LoadOneRowOf4Blocks
	coord hl, 18, 8
	call .LoadOneHalfLeftMetatile
	addmapwidthoffset
	
	coord hl, 0, 12
	call .LoadOneHalfRightMetatile
	coord hl, 2, 12
	call .LoadOneRowOf4Blocks
	coord hl, 18, 12
	call .LoadOneHalfLeftMetatile
	addmapwidthoffset
	
	coord hl, 0, 16
	call .LoadOneQuarterUpperRightMetatile
	coord hl, 2, 16
	ld bc, SCREEN_WIDTH - 4
	call .LoadOneRowOf4UpperHalfBlocks
	coord hl, 18, 16
	
.LoadOneQuarterUpperLeftMetatile:
	ld a, [de]
	inc de
	and a
	jr nz, .notMapBorderBlock3
	ld a, [hMapBorderBlock]
.notMapBorderBlock3
	push de
	swap a
	ld d, a
	and $f0
.LoadOneQuarterMetatile_Entrypoint
	ld e, a
	ld a, d
	and $f
	add (wDecompressedMetatiles >> 8)
	ld d, a

	ld a, [de]
	inc e
	ld [hli], a
	ld a, [de]
	inc e
	inc e
	inc e
	ld [hl], a
	ld a, SCREEN_WIDTH - 1
	add l
	ld l, a
	jr nc, .noCarry
	inc h
.noCarry
	ld a, [de]
	inc e
	ld [hli], a
	ld a, [de]
	ld [hl], a
	pop de
	ret

.LoadOneQuarterUpperRightMetatile:
	ld a, [de]
	inc de
	and a
	jr nz, .notMapBorderBlock4
	ld a, [hMapBorderBlock]
.notMapBorderBlock4
	push de
	swap a
	ld d, a
	and $f0
	inc a
	inc a
	jr .LoadOneQuarterMetatile_Entrypoint

.LoadOneQuarterBottomLeftMetatile:
	ld a, [de]
	inc de
	and a
	jr nz, .notMapBorderBlock5
	ld a, [hMapBorderBlock]
.notMapBorderBlock5
	push de
	swap a
	ld d, a
	and $f0
	add 8
	jr .LoadOneQuarterMetatile_Entrypoint
	
.LoadOneQuarterBottomRightMetatile:
	ld a, [de]
	inc de
	and a
	jr nz, .notMapBorderBlock6
	ld a, [hMapBorderBlock]
.notMapBorderBlock6
	push de
	swap a
	ld d, a
	and $f0
	add 10
	jr .LoadOneQuarterMetatile_Entrypoint