pokecrystal/home/map_objects.asm

631 lines
8.1 KiB
NASM
Raw Normal View History

; Functions handling map objects.
2018-06-24 14:09:41 +00:00
GetSpritePalette::
push hl
push de
push bc
ld c, a
2015-11-14 19:41:47 +00:00
2017-12-24 17:47:30 +00:00
farcall _GetSpritePalette
2015-11-14 19:41:47 +00:00
ld a, c
pop bc
pop de
pop hl
ret
2018-06-24 14:09:41 +00:00
GetSpriteVTile::
push hl
push bc
2018-01-23 22:39:09 +00:00
ld hl, wUsedSprites + 2
ld c, SPRITE_GFX_LIST_CAPACITY - 1
ld b, a
ldh a, [hMapObjectIndexBuffer]
cp 0
jr z, .nope
ld a, b
.loop
cp [hl]
jr z, .found
inc hl
inc hl
dec c
jr nz, .loop
2018-01-23 22:39:09 +00:00
ld a, [wUsedSprites + 1]
scf
jr .done
.nope
2018-01-23 22:39:09 +00:00
ld a, [wUsedSprites + 1]
jr .done
.found
inc hl
xor a
ld a, [hl]
.done
pop bc
pop hl
ret
2018-06-24 14:09:41 +00:00
DoesSpriteHaveFacings::
push de
push hl
ld b, a
ldh a, [hROMBank]
push af
2015-11-05 01:20:14 +00:00
ld a, BANK(_DoesSpriteHaveFacings)
rst Bankswitch
ld a, b
2015-11-05 01:20:14 +00:00
call _DoesSpriteHaveFacings
ld c, a
pop de
ld a, d
rst Bankswitch
pop hl
pop de
ret
2018-06-24 14:09:41 +00:00
GetPlayerStandingTile::
2018-01-23 22:39:09 +00:00
ld a, [wPlayerStandingTile]
call GetTileCollision
ld b, a
ret
2018-06-24 14:09:41 +00:00
CheckOnWater::
2018-01-23 22:39:09 +00:00
ld a, [wPlayerStandingTile]
call GetTileCollision
sub WATERTILE
ret z
and a
ret
2018-06-24 14:09:41 +00:00
GetTileCollision::
; Get the collision type of tile a.
push de
push hl
ld hl, TileCollisionTable
ld e, a
ld d, 0
add hl, de
ldh a, [hROMBank]
push af
ld a, BANK(TileCollisionTable)
rst Bankswitch
ld e, [hl]
pop af
rst Bankswitch
ld a, e
and $f ; lo nybble only
pop hl
pop de
ret
2018-06-24 14:09:41 +00:00
CheckGrassTile::
ld d, a
and $f0
2017-12-12 21:15:58 +00:00
cp HI_NYBBLE_TALL_GRASS
jr z, .grass
cp HI_NYBBLE_WATER
jr z, .water
scf
ret
2017-12-12 21:15:58 +00:00
.grass
ld a, d
2017-12-12 21:15:58 +00:00
and LO_NYBBLE_GRASS
ret z
scf
ret
; For some reason, the above code is duplicated down here.
2017-12-12 21:15:58 +00:00
.water
ld a, d
2017-12-12 21:15:58 +00:00
and LO_NYBBLE_GRASS
ret z
scf
ret
2018-06-24 14:09:41 +00:00
CheckSuperTallGrassTile::
2017-12-12 21:15:58 +00:00
cp COLL_LONG_GRASS
ret z
2017-12-12 21:15:58 +00:00
cp COLL_LONG_GRASS_1C
ret
2018-06-24 14:09:41 +00:00
CheckCutTreeTile::
2017-12-12 21:15:58 +00:00
cp COLL_CUT_TREE
ret z
2017-12-12 21:15:58 +00:00
cp COLL_CUT_TREE_1A
ret
2018-06-24 14:09:41 +00:00
CheckHeadbuttTreeTile::
2017-12-12 21:15:58 +00:00
cp COLL_HEADBUTT_TREE
ret z
2017-12-12 21:15:58 +00:00
cp COLL_HEADBUTT_TREE_1D
ret
2018-06-24 14:09:41 +00:00
CheckCounterTile::
2017-12-12 21:15:58 +00:00
cp COLL_COUNTER
ret z
2017-12-12 21:15:58 +00:00
cp COLL_COUNTER_98
ret
2018-06-24 14:09:41 +00:00
CheckPitTile::
2017-12-12 21:15:58 +00:00
cp COLL_PIT
ret z
2017-12-12 21:15:58 +00:00
cp COLL_PIT_68
ret
2018-06-24 14:09:41 +00:00
CheckIceTile::
2017-12-12 21:15:58 +00:00
cp COLL_ICE
ret z
2017-12-12 21:15:58 +00:00
cp COLL_ICE_2B
ret z
scf
ret
2018-06-24 14:09:41 +00:00
CheckWhirlpoolTile::
nop
2017-12-12 21:15:58 +00:00
cp COLL_WHIRLPOOL
ret z
2017-12-12 21:15:58 +00:00
cp COLL_WHIRLPOOL_2C
ret z
scf
ret
2018-06-24 14:09:41 +00:00
CheckWaterfallTile::
2017-12-12 21:15:58 +00:00
cp COLL_WATERFALL
ret z
2017-12-12 21:15:58 +00:00
cp COLL_CURRENT_DOWN
ret
2018-06-24 14:09:41 +00:00
CheckStandingOnEntrance::
2018-01-23 22:39:09 +00:00
ld a, [wPlayerStandingTile]
2017-12-12 21:15:58 +00:00
cp COLL_DOOR
ret z
2017-12-12 21:15:58 +00:00
cp COLL_DOOR_79
ret z
2017-12-12 21:15:58 +00:00
cp COLL_STAIRCASE
ret z
2017-12-12 21:15:58 +00:00
cp COLL_CAVE
ret
2018-06-24 14:09:41 +00:00
GetMapObject::
; Return the location of map object a in bc.
2018-01-23 22:39:09 +00:00
ld hl, wMapObjects
ld bc, MAPOBJECT_LENGTH
call AddNTimes
ld b, h
ld c, l
ret
2018-06-24 14:09:41 +00:00
CheckObjectVisibility::
; Sets carry if the object is not visible on the screen.
ldh [hMapObjectIndexBuffer], a
call GetMapObject
ld hl, MAPOBJECT_OBJECT_STRUCT_ID
add hl, bc
ld a, [hl]
cp -1
jr z, .not_visible
ldh [hObjectStructIndexBuffer], a
call GetObjectStruct
and a
ret
.not_visible
scf
ret
2018-06-24 14:09:41 +00:00
CheckObjectTime::
ld hl, MAPOBJECT_HOUR
add hl, bc
ld a, [hl]
cp -1
jr nz, .check_hour
ld hl, MAPOBJECT_TIMEOFDAY
add hl, bc
ld a, [hl]
cp -1
jr z, .timeofday_always
ld hl, .TimeOfDayValues_191e
2018-01-23 22:39:09 +00:00
ld a, [wTimeOfDay]
add l
ld l, a
jr nc, .ok
inc h
.ok
ld a, [hl]
ld hl, MAPOBJECT_TIMEOFDAY
add hl, bc
and [hl]
jr nz, .timeofday_always
scf
ret
.timeofday_always
and a
ret
.TimeOfDayValues_191e:
; entries correspond to TimeOfDay values
db MORN
db DAY
db NITE
.check_hour
ld hl, MAPOBJECT_HOUR
add hl, bc
ld d, [hl]
ld hl, MAPOBJECT_TIMEOFDAY
add hl, bc
ld e, [hl]
ld hl, hHours
ld a, d
cp e
jr z, .yes
jr c, .check_timeofday
ld a, [hl]
cp d
jr nc, .yes
cp e
jr c, .yes
jr z, .yes
jr .no
.check_timeofday
ld a, e
cp [hl]
jr c, .no
ld a, [hl]
cp d
jr nc, .yes
jr .no
.yes
and a
ret
.no
scf
ret
; unused
ldh [hMapObjectIndexBuffer], a
call GetMapObject
call CopyObjectStruct
ret
2018-06-24 14:09:41 +00:00
_CopyObjectStruct::
ldh [hMapObjectIndexBuffer], a
2015-11-02 02:17:46 +00:00
call UnmaskObject
ldh a, [hMapObjectIndexBuffer]
call GetMapObject
2017-12-24 17:47:30 +00:00
farcall CopyObjectStruct
ret
2018-06-24 14:09:41 +00:00
ApplyDeletionToMapObject::
ldh [hMapObjectIndexBuffer], a
call GetMapObject
ld hl, MAPOBJECT_OBJECT_STRUCT_ID
add hl, bc
ld a, [hl]
cp -1
2015-11-02 16:15:32 +00:00
ret z ; already hidden
ld [hl], -1
push af
2016-01-11 07:36:39 +00:00
call .CheckStopFollow
pop af
call GetObjectStruct
2017-12-24 17:47:30 +00:00
farcall DeleteMapObject
ret
.CheckStopFollow:
2015-11-02 16:15:32 +00:00
ld hl, wObjectFollow_Leader
cp [hl]
jr z, .ok
2015-11-02 16:15:32 +00:00
ld hl, wObjectFollow_Follower
cp [hl]
ret nz
.ok
2017-12-24 17:47:30 +00:00
farcall StopFollow
ld a, -1
2015-11-02 16:15:32 +00:00
ld [wObjectFollow_Leader], a
ld [wObjectFollow_Follower], a
ret
2018-06-24 14:09:41 +00:00
DeleteObjectStruct::
2016-01-11 07:36:39 +00:00
call ApplyDeletionToMapObject
2015-11-02 02:17:46 +00:00
call MaskObject
ret
2018-06-24 14:09:41 +00:00
CopyPlayerObjectTemplate::
push hl
call GetMapObject
ld d, b
ld e, c
ld a, -1
ld [de], a
inc de
pop hl
ld bc, MAPOBJECT_LENGTH - 1
call CopyBytes
ret
Unreferenced_Function19b8:
call GetMapObject
ld hl, MAPOBJECT_OBJECT_STRUCT_ID
add hl, bc
ld a, [hl]
push af
ld [hl], -1
inc hl
ld bc, MAPOBJECT_LENGTH - 1
xor a
call ByteFill
pop af
cp -1
ret z
cp $d
ret nc
ld b, a
2015-11-02 16:15:32 +00:00
ld a, [wObjectFollow_Leader]
cp b
jr nz, .ok
ld a, -1
2015-11-02 16:15:32 +00:00
ld [wObjectFollow_Leader], a
.ok
ld a, b
call GetObjectStruct
2017-12-24 17:47:30 +00:00
farcall DeleteMapObject
ret
2018-06-24 14:09:41 +00:00
LoadMovementDataPointer::
; Load the movement data pointer for object a.
ld [wMovementObject], a
ldh a, [hROMBank]
2019-04-19 15:35:27 +00:00
ld [wMovementDataBank], a
ld a, l
2019-04-19 15:35:27 +00:00
ld [wMovementDataAddress], a
ld a, h
2019-04-19 15:35:27 +00:00
ld [wMovementDataAddress + 1], a
ld a, [wMovementObject]
2015-11-02 16:15:32 +00:00
call CheckObjectVisibility
ret c
2015-11-12 04:38:57 +00:00
2015-11-02 16:15:32 +00:00
ld hl, OBJECT_MOVEMENTTYPE
add hl, bc
2015-11-06 21:42:38 +00:00
ld [hl], SPRITEMOVEDATA_SCRIPTED
2015-11-12 04:38:57 +00:00
ld hl, OBJECT_STEP_TYPE
add hl, bc
2015-12-29 18:15:58 +00:00
ld [hl], STEP_TYPE_00
2015-11-12 04:38:57 +00:00
2018-01-23 22:39:09 +00:00
ld hl, wVramState
set 7, [hl]
and a
ret
2018-06-24 14:09:41 +00:00
FindFirstEmptyObjectStruct::
2015-11-01 01:05:02 +00:00
; Returns the index of the first empty object struct in A and its address in HL, then sets carry.
; If all object structs are occupied, A = 0 and Z is set.
; Preserves BC and DE.
push bc
push de
2018-01-23 22:39:09 +00:00
ld hl, wObjectStructs
ld de, OBJECT_LENGTH
ld c, NUM_OBJECT_STRUCTS
.loop
ld a, [hl]
and a
2015-11-01 01:05:02 +00:00
jr z, .break
add hl, de
dec c
jr nz, .loop
xor a
jr .done
2015-11-01 01:05:02 +00:00
.break
ld a, NUM_OBJECT_STRUCTS
sub c
scf
.done
pop de
pop bc
ret
2018-06-24 14:09:41 +00:00
GetSpriteMovementFunction::
2015-11-02 16:15:32 +00:00
ld hl, OBJECT_MOVEMENTTYPE
add hl, bc
ld a, [hl]
2015-11-06 21:42:38 +00:00
cp NUM_SPRITEMOVEDATA
jr c, .ok
xor a
.ok
ld hl, SpriteMovementData + SPRITEMOVEATTR_MOVEMENT
ld e, a
ld d, 0
rept NUM_SPRITEMOVEDATA_FIELDS
add hl, de
endr
ld a, [hl]
ret
2018-06-24 14:09:41 +00:00
GetInitialFacing::
push bc
push de
ld e, a
ld d, 0
ld hl, SpriteMovementData + SPRITEMOVEATTR_FACING
rept NUM_SPRITEMOVEDATA_FIELDS
add hl, de
endr
2015-11-06 21:42:38 +00:00
ld a, BANK(SpriteMovementData)
call GetFarByte
add a
add a
maskbits NUM_DIRECTIONS, 2
pop de
pop bc
ret
2018-06-24 14:09:41 +00:00
CopySpriteMovementData::
ld l, a
ldh a, [hROMBank]
push af
2015-11-06 21:42:38 +00:00
ld a, BANK(SpriteMovementData)
rst Bankswitch
ld a, l
push bc
2015-11-16 05:52:53 +00:00
call .CopyData
pop bc
pop af
rst Bankswitch
ret
2018-06-24 14:09:41 +00:00
.CopyData:
2015-11-02 16:15:32 +00:00
ld hl, OBJECT_MOVEMENTTYPE
add hl, de
ld [hl], a
2015-11-16 05:52:53 +00:00
push de
ld e, a
ld d, 0
ld hl, SpriteMovementData + SPRITEMOVEATTR_FACING
rept NUM_SPRITEMOVEDATA_FIELDS
2015-07-29 22:22:23 +00:00
add hl, de
endr
ld b, h
ld c, l
pop de
2015-11-16 05:52:53 +00:00
ld a, [bc]
inc bc
rlca
rlca
maskbits NUM_DIRECTIONS, 2
ld hl, OBJECT_FACING
add hl, de
ld [hl], a
2015-11-16 05:52:53 +00:00
ld a, [bc]
inc bc
2015-11-14 19:41:47 +00:00
ld hl, OBJECT_ACTION
add hl, de
ld [hl], a
2015-11-16 05:52:53 +00:00
ld a, [bc]
inc bc
ld hl, OBJECT_FLAGS1
add hl, de
ld [hl], a
2015-11-16 05:52:53 +00:00
ld a, [bc]
inc bc
ld hl, OBJECT_FLAGS2
add hl, de
ld [hl], a
2015-11-16 05:52:53 +00:00
ld a, [bc]
inc bc
ld hl, OBJECT_PALETTE
add hl, de
ld [hl], a
ret
2018-06-24 14:09:41 +00:00
_GetMovementByte::
2015-11-12 04:38:57 +00:00
; Switch to the movement data bank
ldh a, [hROMBank]
push af
ld a, [hli]
rst Bankswitch
2015-11-12 04:38:57 +00:00
; Load the current script byte as given by OBJECT_MOVEMENT_BYTE_INDEX, and increment OBJECT_MOVEMENT_BYTE_INDEX
ld a, [hli]
ld d, [hl]
2015-11-12 04:38:57 +00:00
ld hl, OBJECT_MOVEMENT_BYTE_INDEX
add hl, bc
add [hl]
ld e, a
ld a, d
adc 0
ld d, a
inc [hl]
ld a, [de]
ld h, a
pop af
rst Bankswitch
ld a, h
ret
2018-06-24 14:09:41 +00:00
SetVramState_Bit0::
2018-01-23 22:39:09 +00:00
ld hl, wVramState
set 0, [hl]
ret
2018-06-24 14:09:41 +00:00
ResetVramState_Bit0::
2018-01-23 22:39:09 +00:00
ld hl, wVramState
res 0, [hl]
ret
2018-06-24 14:09:41 +00:00
UpdateSprites::
2018-01-23 22:39:09 +00:00
ld a, [wVramState]
bit 0, a
ret z
2015-11-16 05:52:53 +00:00
2017-12-24 17:47:30 +00:00
farcall Function55e0
farcall _UpdateSprites
ret
2018-06-24 14:09:41 +00:00
GetObjectStruct::
ld bc, OBJECT_LENGTH
2018-01-23 22:39:09 +00:00
ld hl, wObjectStructs
call AddNTimes
ld b, h
ld c, l
ret
2018-06-24 14:09:41 +00:00
DoesObjectHaveASprite::
ld hl, OBJECT_SPRITE
add hl, bc
ld a, [hl]
and a
ret
2018-06-24 14:09:41 +00:00
SetSpriteDirection::
2015-11-14 19:41:47 +00:00
; preserves other flags
push af
ld hl, OBJECT_FACING
add hl, bc
ld a, [hl]
and %11110011
ld e, a
pop af
maskbits NUM_DIRECTIONS, 2
or e
ld [hl], a
ret
2018-06-24 14:09:41 +00:00
GetSpriteDirection::
ld hl, OBJECT_FACING
add hl, bc
ld a, [hl]
maskbits NUM_DIRECTIONS, 2
ret