pokecrystal-board/engine/overworld/events.asm

1348 lines
20 KiB
NASM
Raw Normal View History

SECTION "Events", ROMX
2018-06-24 07:09:41 -07:00
OverworldLoop::
2019-01-24 18:41:14 -08:00
xor a ; MAPSTATUS_START
2018-01-23 14:39:09 -08:00
ld [wMapStatus], a
2013-12-27 02:56:23 -08:00
.loop
2018-01-23 14:39:09 -08:00
ld a, [wMapStatus]
2021-03-17 08:38:01 -07:00
ld hl, .Jumptable
rst JumpTable
2018-01-23 14:39:09 -08:00
ld a, [wMapStatus]
2019-01-24 18:41:14 -08:00
cp MAPSTATUS_DONE
2013-12-27 02:56:23 -08:00
jr nz, .loop
.done
ret
2021-03-17 08:38:01 -07:00
.Jumptable:
2019-01-24 18:41:14 -08:00
; entries correspond to MAPSTATUS_* constants
2013-12-27 02:56:23 -08:00
dw StartMap
dw EnterMap
dw HandleMap
dw .done
2018-06-24 07:09:41 -07:00
DisableEvents:
xor a
ld [wScriptFlags2], a
ret
2018-06-24 07:09:41 -07:00
EnableEvents::
ld a, $ff
ld [wScriptFlags2], a
ret
CheckBit5_ScriptFlags2:
ld hl, wScriptFlags2
bit 5, [hl]
ret
DisableWarpsConnxns: ; unreferenced
ld hl, wScriptFlags2
res 2, [hl]
ret
DisableCoordEvents: ; unreferenced
ld hl, wScriptFlags2
res 1, [hl]
ret
DisableStepCount: ; unreferenced
ld hl, wScriptFlags2
res 0, [hl]
ret
DisableWildEncounters: ; unreferenced
ld hl, wScriptFlags2
res 4, [hl]
ret
EnableWarpsConnxns: ; unreferenced
ld hl, wScriptFlags2
set 2, [hl]
ret
EnableCoordEvents: ; unreferenced
ld hl, wScriptFlags2
set 1, [hl]
ret
EnableStepCount: ; unreferenced
ld hl, wScriptFlags2
set 0, [hl]
ret
2018-06-24 07:09:41 -07:00
EnableWildEncounters:
ld hl, wScriptFlags2
set 4, [hl]
ret
2018-06-24 07:09:41 -07:00
CheckWarpConnxnScriptFlag:
ld hl, wScriptFlags2
bit 2, [hl]
ret
2018-06-24 07:09:41 -07:00
CheckCoordEventScriptFlag:
ld hl, wScriptFlags2
bit 1, [hl]
ret
2018-06-24 07:09:41 -07:00
CheckStepCountScriptFlag:
ld hl, wScriptFlags2
bit 0, [hl]
ret
2018-06-24 07:09:41 -07:00
CheckWildEncountersScriptFlag:
ld hl, wScriptFlags2
bit 4, [hl]
ret
2018-06-24 07:09:41 -07:00
StartMap:
xor a
2018-01-23 14:39:09 -08:00
ld [wScriptVar], a
xor a
2018-01-23 14:39:09 -08:00
ld [wScriptRunning], a
ld hl, wMapStatus
ld bc, wMapStatusEnd - wMapStatus
call ByteFill
2017-12-24 09:47:30 -08:00
farcall InitCallReceiveDelay
call ClearJoypad
2018-06-24 07:09:41 -07:00
EnterMap:
xor a
2016-01-12 09:46:18 -08:00
ld [wXYComparePointer], a
ld [wXYComparePointer + 1], a
call SetUpFiveStepWildEncounterCooldown
2017-12-24 09:47:30 -08:00
farcall RunMapSetupScript
call DisableEvents
2013-12-27 02:56:23 -08:00
ldh a, [hMapEntryMethod]
2015-11-11 20:38:57 -08:00
cp MAPSETUP_CONNECTION
jr nz, .dont_enable
call EnableEvents
.dont_enable
2013-12-27 02:56:23 -08:00
ldh a, [hMapEntryMethod]
2015-10-24 16:49:19 -07:00
cp MAPSETUP_RELOADMAP
jr nz, .dontresetpoison
xor a
2018-01-23 14:39:09 -08:00
ld [wPoisonStepCount], a
.dontresetpoison
2013-12-27 02:56:23 -08:00
2015-10-24 16:49:19 -07:00
xor a ; end map entry
ldh [hMapEntryMethod], a
2019-01-24 18:41:14 -08:00
ld a, MAPSTATUS_HANDLE
2018-01-23 14:39:09 -08:00
ld [wMapStatus], a
ret
UnusedWait30Frames: ; unreferenced
ld c, 30
call DelayFrames
ret
2018-06-24 07:09:41 -07:00
HandleMap:
call ResetOverworldDelay
2015-12-17 12:14:51 -08:00
call HandleMapTimeAndJoypad
2017-12-24 09:47:30 -08:00
farcall HandleCmdQueue ; no need to farcall
2013-12-27 02:56:23 -08:00
call MapEvents
; Not immediately entering a connected map will cause problems.
2018-01-23 14:39:09 -08:00
ld a, [wMapStatus]
2019-01-24 18:41:14 -08:00
cp MAPSTATUS_HANDLE
ret nz
2013-12-27 02:56:23 -08:00
call HandleMapObjects
call NextOverworldFrame
call HandleMapBackground
call CheckPlayerState
ret
2018-06-24 07:09:41 -07:00
MapEvents:
2018-01-23 14:39:09 -08:00
ld a, [wMapEventStatus]
2021-03-17 08:38:01 -07:00
ld hl, .Jumptable
rst JumpTable
ret
2021-03-17 08:38:01 -07:00
.Jumptable:
2019-01-24 18:41:14 -08:00
; entries correspond to MAPEVENTS_* constants
2013-12-27 02:56:23 -08:00
dw .events
dw .no_events
2021-03-17 08:38:01 -07:00
.events:
call PlayerEvents
call DisableEvents
2017-12-24 09:47:30 -08:00
farcall ScriptEvents
ret
2021-03-17 08:38:01 -07:00
.no_events:
ret
2018-06-24 07:09:41 -07:00
MaxOverworldDelay:
db 2
2018-06-24 07:09:41 -07:00
ResetOverworldDelay:
ld a, [MaxOverworldDelay]
2018-01-23 14:39:09 -08:00
ld [wOverworldDelay], a
ret
2018-06-24 07:09:41 -07:00
NextOverworldFrame:
2018-01-23 14:39:09 -08:00
ld a, [wOverworldDelay]
and a
ret z
ld c, a
call DelayFrames
ret
2018-06-24 07:09:41 -07:00
HandleMapTimeAndJoypad:
2018-01-23 14:39:09 -08:00
ld a, [wMapEventStatus]
2019-01-24 18:41:14 -08:00
cp MAPEVENTS_OFF
ret z
2013-12-27 02:56:23 -08:00
call UpdateTime
call GetJoypad
call TimeOfDayPals
ret
2018-06-24 07:09:41 -07:00
HandleMapObjects:
2020-11-17 08:12:21 -08:00
farcall HandleNPCStep
2017-12-24 09:47:30 -08:00
farcall _HandlePlayerStep
call _CheckObjectEnteringVisibleRange
ret
2018-06-24 07:09:41 -07:00
HandleMapBackground:
2017-12-24 09:47:30 -08:00
farcall _UpdateSprites
farcall ScrollScreen
farcall PlaceMapNameSign
ret
2018-06-24 07:09:41 -07:00
CheckPlayerState:
ld a, [wPlayerStepFlags]
2019-01-24 18:41:14 -08:00
bit PLAYERSTEP_CONTINUE_F, a
jr z, .events
2019-01-24 18:41:14 -08:00
bit PLAYERSTEP_STOP_F, a
jr z, .noevents
2019-01-24 18:41:14 -08:00
bit PLAYERSTEP_MIDAIR_F, a
jr nz, .noevents
call EnableEvents
.events
2019-01-24 18:41:14 -08:00
ld a, MAPEVENTS_ON
2018-01-23 14:39:09 -08:00
ld [wMapEventStatus], a
ret
.noevents
2019-01-24 18:41:14 -08:00
ld a, MAPEVENTS_OFF
2018-01-23 14:39:09 -08:00
ld [wMapEventStatus], a
ret
2018-06-24 07:09:41 -07:00
_CheckObjectEnteringVisibleRange:
ld hl, wPlayerStepFlags
2019-01-24 18:41:14 -08:00
bit PLAYERSTEP_STOP_F, [hl]
ret z
2017-12-24 09:47:30 -08:00
farcall CheckObjectEnteringVisibleRange
ret
2018-06-24 07:09:41 -07:00
PlayerEvents:
xor a
2015-10-24 07:34:19 -07:00
; If there's already a player event, don't interrupt it.
2018-01-23 14:39:09 -08:00
ld a, [wScriptRunning]
and a
ret nz
call Dummy_CheckScriptFlags2Bit5 ; This is a waste of time
call CheckTrainerBattle_GetPlayerEvent
jr c, .ok
call CheckTileEvent
jr c, .ok
2015-10-24 07:34:19 -07:00
call RunMemScript
jr c, .ok
call RunSceneScript
jr c, .ok
2015-10-24 07:34:19 -07:00
call CheckTimeEvents
jr c, .ok
call OWPlayerInput
jr c, .ok
xor a
ret
.ok
push af
2017-12-24 09:47:30 -08:00
farcall EnableScriptMode
pop af
2018-01-23 14:39:09 -08:00
ld [wScriptRunning], a
2015-10-24 16:49:19 -07:00
call DoPlayerEvent
2018-01-23 14:39:09 -08:00
ld a, [wScriptRunning]
2015-11-11 20:38:57 -08:00
cp PLAYEREVENT_CONNECTION
jr z, .ok2
2015-11-11 20:38:57 -08:00
cp PLAYEREVENT_JOYCHANGEFACING
jr z, .ok2
xor a
ld [wLandmarkSignTimer], a
.ok2
scf
ret
CheckTrainerBattle_GetPlayerEvent:
nop
nop
call CheckTrainerBattle
jr nc, .nope
2013-12-27 02:56:23 -08:00
2015-11-12 08:10:19 -08:00
ld a, PLAYEREVENT_SEENBYTRAINER
scf
ret
.nope
xor a
ret
2018-06-24 07:09:41 -07:00
CheckTileEvent:
; Check for warps, coord events, or wild battles.
call CheckWarpConnxnScriptFlag
jr z, .connections_disabled
2017-12-24 09:47:30 -08:00
farcall CheckMovingOffEdgeOfMap
2015-11-12 08:10:19 -08:00
jr c, .map_connection
2015-10-24 16:49:19 -07:00
call CheckWarpTile
2015-11-12 08:10:19 -08:00
jr c, .warp_tile
.connections_disabled
call CheckCoordEventScriptFlag
jr z, .coord_events_disabled
call CheckCurrentMapCoordEvents
jr c, .coord_event
.coord_events_disabled
call CheckStepCountScriptFlag
jr z, .step_count_disabled
call CountStep
ret c
.step_count_disabled
call CheckWildEncountersScriptFlag
jr z, .ok
call RandomEncounter
ret c
jr .ok ; pointless
.ok
xor a
ret
2015-11-12 08:10:19 -08:00
.map_connection
ld a, PLAYEREVENT_CONNECTION
scf
ret
2015-11-12 08:10:19 -08:00
.warp_tile
ld a, [wPlayerTile]
call CheckPitTile
2015-11-12 08:10:19 -08:00
jr nz, .not_pit
ld a, PLAYEREVENT_FALL
scf
ret
2015-11-12 08:10:19 -08:00
.not_pit
ld a, PLAYEREVENT_WARP
scf
ret
.coord_event
2016-01-06 04:44:50 -08:00
ld hl, wCurCoordEventScriptAddr
ld a, [hli]
ld h, [hl]
ld l, a
call GetMapScriptsBank
call CallScript
ret
2018-06-24 07:09:41 -07:00
CheckWildEncounterCooldown::
ld hl, wWildEncounterCooldown
ld a, [hl]
and a
ret z
dec [hl]
ret z
scf
ret
2018-06-24 07:09:41 -07:00
SetUpFiveStepWildEncounterCooldown:
ld a, 5
ld [wWildEncounterCooldown], a
ret
2018-06-24 07:09:41 -07:00
SetMinTwoStepWildEncounterCooldown:
; dummied out
ret
ld a, [wWildEncounterCooldown]
cp 2
ret nc
ld a, 2
ld [wWildEncounterCooldown], a
ret
Dummy_CheckScriptFlags2Bit5:
call CheckBit5_ScriptFlags2
ret z
2020-10-04 16:43:28 -07:00
call SetXYCompareFlags
ret
2018-06-24 07:09:41 -07:00
RunSceneScript:
2018-09-09 12:09:51 -07:00
ld a, [wCurMapSceneScriptCount]
and a
jr z, .nope
ld c, a
call CheckScenes
cp c
jr nc, .nope
ld e, a
ld d, 0
2018-09-09 12:09:51 -07:00
ld hl, wCurMapSceneScriptsPointer
ld a, [hli]
ld h, [hl]
ld l, a
2019-03-03 11:19:16 -08:00
rept SCENE_SCRIPT_SIZE
2016-01-10 14:44:09 -08:00
add hl, de
endr
call GetMapScriptsBank
call GetFarWord
call GetMapScriptsBank
call CallScript
2018-01-23 14:39:09 -08:00
ld hl, wScriptFlags
res 3, [hl]
2017-12-24 09:47:30 -08:00
farcall EnableScriptMode
farcall ScriptEvents
2018-01-23 14:39:09 -08:00
ld hl, wScriptFlags
bit 3, [hl]
jr z, .nope
2021-06-08 17:35:04 -07:00
ld hl, wDeferredScriptAddr
ld a, [hli]
ld h, [hl]
ld l, a
2021-06-08 17:35:04 -07:00
ld a, [wDeferredScriptBank]
call CallScript
scf
ret
.nope
xor a
ret
2018-06-24 07:09:41 -07:00
CheckTimeEvents:
ld a, [wLinkMode]
and a
2014-07-24 20:19:56 -07:00
jr nz, .nothing
ld hl, wStatusFlags2
2018-01-22 12:40:43 -08:00
bit STATUSFLAGS2_BUG_CONTEST_TIMER_F, [hl]
2015-10-24 07:34:19 -07:00
jr z, .do_daily
2014-07-24 20:19:56 -07:00
2017-12-24 09:47:30 -08:00
farcall CheckBugContestTimer
2015-10-24 07:34:19 -07:00
jr c, .end_bug_contest
xor a
ret
2015-10-24 07:34:19 -07:00
.do_daily
2017-12-24 09:47:30 -08:00
farcall CheckDailyResetTimer
farcall CheckPokerusTick
farcall CheckPhoneCall
ret c
2014-07-24 20:19:56 -07:00
.nothing
xor a
ret
2015-10-24 07:34:19 -07:00
.end_bug_contest
ld a, BANK(BugCatchingContestOverScript)
ld hl, BugCatchingContestOverScript
call CallScript
scf
ret
.unused ; unreferenced
ld a, $8 ; ???
scf
ret
2018-06-24 07:09:41 -07:00
OWPlayerInput:
call PlayerMovement
ret c
and a
jr nz, .NoAction
; Can't perform button actions while sliding on ice.
2017-12-24 09:47:30 -08:00
farcall CheckStandingOnIce
jr c, .NoAction
call CheckAPressOW
jr c, .Action
call CheckMenuOW
jr c, .Action
.NoAction:
xor a
ret
.Action:
push af
2017-12-24 09:47:30 -08:00
farcall StopPlayerForEvent
pop af
scf
ret
2018-06-24 07:09:41 -07:00
CheckAPressOW:
ldh a, [hJoyPressed]
and A_BUTTON
ret z
call TryObjectEvent
ret c
call TryBGEvent
ret c
call TryTileCollisionEvent
ret c
xor a
ret
2018-06-24 07:09:41 -07:00
PlayTalkObject:
push de
ld de, SFX_READ_TEXT_2
call PlaySFX
pop de
ret
2018-06-24 07:09:41 -07:00
TryObjectEvent:
2017-12-24 09:47:30 -08:00
farcall CheckFacingObject
jr c, .IsObject
xor a
ret
.IsObject:
call PlayTalkObject
ldh a, [hObjectStructIndex]
call GetObjectStruct
ld hl, OBJECT_MAP_OBJECT_INDEX
add hl, bc
ld a, [hl]
ldh [hLastTalked], a
ldh a, [hLastTalked]
call GetMapObject
ld hl, MAPOBJECT_COLOR
add hl, bc
ld a, [hl]
and %00001111
; BUG: TryObjectEvent arbitrary code execution (see docs/bugs_and_glitches.md)
push bc
ld de, 3
2021-03-17 08:38:01 -07:00
ld hl, ObjectEventTypeArray
call IsInArray
2019-04-23 16:07:41 -07:00
jr nc, .nope
pop bc
inc hl
ld a, [hli]
ld h, [hl]
ld l, a
jp hl
2019-04-23 16:07:41 -07:00
.nope
xor a
ret
2021-03-17 08:38:01 -07:00
ObjectEventTypeArray:
table_width 3, ObjectEventTypeArray
dbw OBJECTTYPE_SCRIPT, .script
dbw OBJECTTYPE_ITEMBALL, .itemball
dbw OBJECTTYPE_TRAINER, .trainer
; the remaining four are dummy events
dbw OBJECTTYPE_3, .three
dbw OBJECTTYPE_4, .four
dbw OBJECTTYPE_5, .five
dbw OBJECTTYPE_6, .six
2021-03-17 08:38:01 -07:00
assert_table_length NUM_OBJECT_TYPES
db -1 ; end
2018-06-24 07:09:41 -07:00
.script
ld hl, MAPOBJECT_SCRIPT_POINTER
add hl, bc
ld a, [hli]
ld h, [hl]
ld l, a
call GetMapScriptsBank
call CallScript
ret
2018-06-24 07:09:41 -07:00
.itemball
ld hl, MAPOBJECT_SCRIPT_POINTER
add hl, bc
ld a, [hli]
ld h, [hl]
ld l, a
call GetMapScriptsBank
ld de, wItemBallData
ld bc, wItemBallDataEnd - wItemBallData
call FarCopyBytes
ld a, PLAYEREVENT_ITEMBALL
scf
ret
2018-06-24 07:09:41 -07:00
.trainer
call TalkToTrainer
ld a, PLAYEREVENT_TALKTOTRAINER
scf
ret
2018-06-24 07:09:41 -07:00
.three
xor a
ret
2018-06-24 07:09:41 -07:00
.four
xor a
ret
2018-06-24 07:09:41 -07:00
.five
xor a
ret
2018-06-24 07:09:41 -07:00
.six
xor a
ret
2018-06-24 07:09:41 -07:00
TryBGEvent:
call CheckFacingBGEvent
jr c, .is_bg_event
xor a
ret
.is_bg_event:
ld a, [wCurBGEventType]
2021-03-17 08:38:01 -07:00
ld hl, BGEventJumptable
rst JumpTable
ret
2021-03-17 08:38:01 -07:00
BGEventJumptable:
table_width 2, BGEventJumptable
dw .read
dw .up
dw .down
dw .right
dw .left
dw .ifset
dw .ifnotset
dw .itemifset
dw .copy
2021-03-17 08:38:01 -07:00
assert_table_length NUM_BGEVENTS
2021-03-17 08:38:01 -07:00
.up:
ld b, OW_UP
jr .checkdir
2021-03-17 08:38:01 -07:00
.down:
ld b, OW_DOWN
jr .checkdir
2021-03-17 08:38:01 -07:00
.right:
ld b, OW_RIGHT
jr .checkdir
2021-03-17 08:38:01 -07:00
.left:
ld b, OW_LEFT
jr .checkdir
2021-03-17 08:38:01 -07:00
.checkdir:
2018-01-23 14:39:09 -08:00
ld a, [wPlayerDirection]
and %1100
cp b
jp nz, .dontread
2021-03-17 08:38:01 -07:00
.read:
call PlayTalkObject
ld hl, wCurBGEventScriptAddr
ld a, [hli]
ld h, [hl]
ld l, a
call GetMapScriptsBank
call CallScript
scf
ret
2021-03-17 08:38:01 -07:00
.itemifset:
call CheckBGEventFlag
jp nz, .dontread
call PlayTalkObject
call GetMapScriptsBank
ld de, wHiddenItemData
ld bc, wHiddenItemDataEnd - wHiddenItemData
call FarCopyBytes
ld a, BANK(HiddenItemScript)
ld hl, HiddenItemScript
call CallScript
scf
ret
2021-03-17 08:38:01 -07:00
.copy:
call CheckBGEventFlag
jr nz, .dontread
call GetMapScriptsBank
ld de, wHiddenItemData
ld bc, wHiddenItemDataEnd - wHiddenItemData
call FarCopyBytes
jr .dontread
2021-03-17 08:38:01 -07:00
.ifset:
call CheckBGEventFlag
jr z, .dontread
jr .thenread
2021-03-17 08:38:01 -07:00
.ifnotset:
call CheckBGEventFlag
jr nz, .dontread
2021-03-17 08:38:01 -07:00
.thenread:
push hl
call PlayTalkObject
pop hl
inc hl
2015-12-26 18:59:03 -08:00
inc hl
call GetMapScriptsBank
call GetFarWord
call GetMapScriptsBank
call CallScript
scf
ret
2021-03-17 08:38:01 -07:00
.dontread:
xor a
ret
2018-06-24 07:09:41 -07:00
CheckBGEventFlag:
ld hl, wCurBGEventScriptAddr
ld a, [hli]
ld h, [hl]
ld l, a
push hl
call GetMapScriptsBank
call GetFarWord
ld e, l
ld d, h
2015-11-12 08:10:19 -08:00
ld b, CHECK_FLAG
call EventFlagAction
ld a, c
and a
pop hl
ret
2018-06-24 07:09:41 -07:00
PlayerMovement:
2017-12-24 09:47:30 -08:00
farcall DoPlayerMovement
ld a, c
2021-03-17 08:38:01 -07:00
ld hl, PlayerMovementPointers
rst JumpTable
ld a, c
ret
2021-03-17 08:38:01 -07:00
PlayerMovementPointers:
2019-11-03 11:30:33 -08:00
; entries correspond to PLAYERMOVEMENT_* constants
2021-03-17 08:38:01 -07:00
table_width 2, PlayerMovementPointers
2019-11-03 11:30:33 -08:00
dw .normal
dw .warp
dw .turn
dw .force_turn
dw .finish
dw .continue
dw .exit_water
dw .jump
2021-03-17 08:38:01 -07:00
assert_table_length NUM_PLAYER_MOVEMENTS
2019-11-03 11:30:33 -08:00
.normal:
.finish:
xor a
ld c, a
ret
2019-11-03 11:30:33 -08:00
.jump:
call SetMinTwoStepWildEncounterCooldown
xor a
ld c, a
ret
2019-11-03 11:30:33 -08:00
.warp:
ld a, PLAYEREVENT_WARP
ld c, a
scf
ret
2019-11-03 11:30:33 -08:00
.turn:
ld a, PLAYEREVENT_JOYCHANGEFACING
ld c, a
scf
ret
2019-11-03 11:30:33 -08:00
.force_turn:
; force the player to move in some direction
ld a, BANK(Script_ForcedMovement)
ld hl, Script_ForcedMovement
call CallScript
ld c, a
scf
ret
2019-11-03 11:30:33 -08:00
.continue:
.exit_water:
ld a, -1
ld c, a
and a
ret
2018-06-24 07:09:41 -07:00
CheckMenuOW:
xor a
ldh [hMenuReturn], a
ldh [hUnusedByte], a
ldh a, [hJoyPressed]
2017-12-30 09:35:50 -08:00
bit SELECT_F, a
jr nz, .Select
2017-12-30 09:35:50 -08:00
bit START_F, a
jr z, .NoMenu
ld a, BANK(StartMenuScript)
ld hl, StartMenuScript
call CallScript
scf
ret
.NoMenu:
xor a
ret
.Select:
call PlayTalkObject
ld a, BANK(SelectMenuScript)
ld hl, SelectMenuScript
call CallScript
scf
ret
2018-06-24 07:09:41 -07:00
StartMenuScript:
2015-01-20 00:01:23 -08:00
callasm StartMenu
sjump StartMenuCallback
2018-06-24 07:09:41 -07:00
SelectMenuScript:
2015-01-20 00:01:23 -08:00
callasm SelectMenu
sjump SelectMenuCallback
StartMenuCallback:
2018-06-24 07:09:41 -07:00
SelectMenuCallback:
readmem hMenuReturn
2018-02-02 18:09:17 -08:00
ifequal HMENURETURN_SCRIPT, .Script
ifequal HMENURETURN_ASM, .Asm
end
2018-06-24 07:09:41 -07:00
.Script:
memjump wQueuedScriptBank
2018-06-24 07:09:41 -07:00
.Asm:
memcallasm wQueuedScriptBank
end
2018-06-24 07:09:41 -07:00
CountStep:
2015-10-24 07:34:19 -07:00
; Don't count steps in link communication rooms.
ld a, [wLinkMode]
and a
jr nz, .done
2015-10-24 07:34:19 -07:00
; If there is a special phone call, don't count the step.
2017-12-24 09:47:30 -08:00
farcall CheckSpecialPhoneCall
2015-10-24 07:34:19 -07:00
jr c, .doscript
2015-10-24 07:34:19 -07:00
; If Repel wore off, don't count the step.
call DoRepelStep
jr c, .doscript
2015-10-24 07:34:19 -07:00
; Count the step for poison and total steps
2018-01-23 14:39:09 -08:00
ld hl, wPoisonStepCount
inc [hl]
2018-01-23 14:39:09 -08:00
ld hl, wStepCount
inc [hl]
2015-10-24 07:34:19 -07:00
; Every 256 steps, increase the happiness of all your Pokemon.
jr nz, .skip_happiness
2017-12-24 09:47:30 -08:00
farcall StepHappiness
2015-10-24 07:34:19 -07:00
.skip_happiness
; Every 256 steps, offset from the happiness incrementor by 128 steps,
; decrease the hatch counter of all your eggs until you reach the first
; one that is ready to hatch.
2018-01-23 14:39:09 -08:00
ld a, [wStepCount]
cp $80
2015-10-24 07:34:19 -07:00
jr nz, .skip_egg
2017-12-24 09:47:30 -08:00
farcall DoEggStep
2015-10-24 07:34:19 -07:00
jr nz, .hatch
2015-10-24 07:34:19 -07:00
.skip_egg
; Increase the EXP of (both) DayCare Pokemon by 1.
2017-12-24 09:47:30 -08:00
farcall DayCareStep
2020-11-17 08:09:02 -08:00
; Every 4 steps, deal damage to all poisoned Pokemon.
2018-01-23 14:39:09 -08:00
ld hl, wPoisonStepCount
ld a, [hl]
cp 4
2015-10-24 07:34:19 -07:00
jr c, .skip_poison
ld [hl], 0
2017-12-24 09:47:30 -08:00
farcall DoPoisonStep
2015-10-24 07:34:19 -07:00
jr c, .doscript
2015-10-24 07:34:19 -07:00
.skip_poison
2017-12-24 09:47:30 -08:00
farcall DoBikeStep
.done
xor a
ret
2015-10-24 07:34:19 -07:00
.doscript
ld a, -1
scf
ret
2015-10-24 07:34:19 -07:00
.hatch
2019-11-03 11:30:33 -08:00
ld a, PLAYEREVENT_HATCH
scf
ret
.whiteout ; unreferenced
2019-11-03 11:30:33 -08:00
ld a, PLAYEREVENT_WHITEOUT
scf
ret
2018-06-24 07:09:41 -07:00
DoRepelStep:
2015-10-24 07:34:19 -07:00
ld a, [wRepelEffect]
and a
ret z
2015-09-09 16:27:07 -07:00
dec a
2015-10-24 07:34:19 -07:00
ld [wRepelEffect], a
ret nz
2015-09-09 16:27:07 -07:00
ld a, BANK(RepelWoreOffScript)
ld hl, RepelWoreOffScript
call CallScript
scf
ret
2018-06-24 07:09:41 -07:00
DoPlayerEvent:
2018-01-23 14:39:09 -08:00
ld a, [wScriptRunning]
and a
ret z
2015-11-11 20:38:57 -08:00
2015-11-05 12:08:00 -08:00
cp PLAYEREVENT_MAPSCRIPT ; run script
ret z
2015-11-11 20:38:57 -08:00
2015-11-05 12:08:00 -08:00
cp NUM_PLAYER_EVENTS
ret nc
ld c, a
ld b, 0
2015-11-05 12:08:00 -08:00
ld hl, PlayerEventScriptPointers
2016-01-10 14:44:09 -08:00
add hl, bc
add hl, bc
add hl, bc
ld a, [hli]
2018-01-23 14:39:09 -08:00
ld [wScriptBank], a
ld a, [hli]
2018-01-23 14:39:09 -08:00
ld [wScriptPos], a
ld a, [hl]
2018-01-23 14:39:09 -08:00
ld [wScriptPos + 1], a
ret
2018-06-24 07:09:41 -07:00
PlayerEventScriptPointers:
; entries correspond to PLAYEREVENT_* constants
2021-03-17 08:38:01 -07:00
table_width 3, PlayerEventScriptPointers
2020-10-04 16:43:28 -07:00
dba InvalidEventScript ; PLAYEREVENT_NONE
dba SeenByTrainerScript ; PLAYEREVENT_SEENBYTRAINER
dba TalkToTrainerScript ; PLAYEREVENT_TALKTOTRAINER
dba FindItemInBallScript ; PLAYEREVENT_ITEMBALL
dba EdgeWarpScript ; PLAYEREVENT_CONNECTION
dba WarpToNewMapScript ; PLAYEREVENT_WARP
dba FallIntoMapScript ; PLAYEREVENT_FALL
dba OverworldWhiteoutScript ; PLAYEREVENT_WHITEOUT
dba HatchEggScript ; PLAYEREVENT_HATCH
dba ChangeDirectionScript ; PLAYEREVENT_JOYCHANGEFACING
2020-10-04 16:43:28 -07:00
dba InvalidEventScript ; (NUM_PLAYER_EVENTS)
2021-03-17 08:38:01 -07:00
assert_table_length NUM_PLAYER_EVENTS + 1
2020-10-04 16:43:28 -07:00
InvalidEventScript:
end
2020-10-04 16:43:28 -07:00
UnusedPlayerEventScript: ; unreferenced
end
2018-06-24 07:09:41 -07:00
HatchEggScript:
callasm OverworldHatchEgg
end
2018-06-24 07:09:41 -07:00
WarpToNewMapScript:
warpsound
2015-11-11 20:38:57 -08:00
newloadmap MAPSETUP_DOOR
end
2018-06-24 07:09:41 -07:00
FallIntoMapScript:
2015-11-11 20:38:57 -08:00
newloadmap MAPSETUP_FALL
playsound SFX_KINESIS
2020-10-04 16:43:28 -07:00
applymovement PLAYER, .SkyfallMovement
playsound SFX_STRENGTH
2015-10-24 16:49:19 -07:00
scall LandAfterPitfallScript
end
2020-10-04 16:43:28 -07:00
.SkyfallMovement:
skyfall
step_end
2018-06-24 07:09:41 -07:00
LandAfterPitfallScript:
earthquake 16
end
2020-10-04 16:43:28 -07:00
EdgeWarpScript:
reloadend MAPSETUP_CONNECTION
2020-10-04 16:43:28 -07:00
ChangeDirectionScript:
deactivatefacing 3
callasm EnableWildEncounters
end
INCLUDE "engine/overworld/scripting.asm"
2018-01-11 20:50:44 -08:00
WarpToSpawnPoint::
ld hl, wStatusFlags2
res STATUSFLAGS2_SAFARI_GAME_F, [hl]
res STATUSFLAGS2_BUG_CONTEST_TIMER_F, [hl]
ret
RunMemScript::
; If there is no script here, we don't need to be here.
ld a, [wMapReentryScriptQueueFlag]
and a
ret z
; Execute the script at (wMapReentryScriptBank):(wMapReentryScriptAddress).
ld hl, wMapReentryScriptAddress
ld a, [hli]
ld h, [hl]
ld l, a
ld a, [wMapReentryScriptBank]
call CallScript
scf
; Clear the buffer for the next script.
push af
xor a
ld hl, wMapReentryScriptQueueFlag
ld bc, 8
call ByteFill
pop af
ret
LoadScriptBDE::
; If there's already a script here, don't overwrite.
ld hl, wMapReentryScriptQueueFlag
ld a, [hl]
and a
ret nz
; Set the flag
ld [hl], 1
inc hl
; Load the script pointer b:de into (wMapReentryScriptBank):(wMapReentryScriptAddress)
ld [hl], b
inc hl
ld [hl], e
inc hl
ld [hl], d
scf
ret
TryTileCollisionEvent::
call GetFacingTileCoord
ld [wFacingTileID], a
ld c, a
; CheckFacingTileForStdScript preserves c, and
; farcall copies c back into a.
farcall CheckFacingTileForStdScript
jr c, .done
; CheckCutTreeTile expects a == [wFacingTileID], which
; it still is after the previous farcall.
call CheckCutTreeTile
jr nz, .whirlpool
farcall TryCutOW
jr .done
.whirlpool
ld a, [wFacingTileID]
call CheckWhirlpoolTile
jr nz, .waterfall
farcall TryWhirlpoolOW
jr .done
.waterfall
ld a, [wFacingTileID]
call CheckWaterfallTile
jr nz, .headbutt
farcall TryWaterfallOW
jr .done
.headbutt
ld a, [wFacingTileID]
call CheckHeadbuttTreeTile
jr nz, .surf
farcall TryHeadbuttOW
jr c, .done
jr .noevent
.surf
farcall TrySurfOW
jr nc, .noevent
jr .done
.noevent
xor a
ret
.done
call PlayClickSFX
ld a, $ff
scf
ret
RandomEncounter::
; Random encounter
call CheckWildEncounterCooldown
jr c, .nope
call CanUseSweetScent
jr nc, .nope
ld hl, wStatusFlags2
bit STATUSFLAGS2_BUG_CONTEST_TIMER_F, [hl]
jr nz, .bug_contest
farcall TryWildEncounter
jr nz, .nope
jr .ok
.bug_contest
call _TryWildEncounter_BugContest
jr nc, .nope
jr .ok_bug_contest
.nope
ld a, 1
and a
ret
.ok
ld a, BANK(WildBattleScript)
ld hl, WildBattleScript
jr .done
.ok_bug_contest
ld a, BANK(BugCatchingContestBattleScript)
ld hl, BugCatchingContestBattleScript
jr .done
.done
call CallScript
scf
ret
WildBattleScript:
randomwildmon
startbattle
reloadmapafterbattle
end
CanUseSweetScent::
ld hl, wStatusFlags
bit STATUSFLAGS_NO_WILD_ENCOUNTERS_F, [hl]
jr nz, .no
ld a, [wEnvironment]
cp CAVE
jr z, .ice_check
cp DUNGEON
jr z, .ice_check
farcall CheckGrassCollision
jr nc, .no
.ice_check
ld a, [wPlayerTile]
call CheckIceTile
jr z, .no
scf
ret
.no
and a
ret
_TryWildEncounter_BugContest:
call TryWildEncounter_BugContest
ret nc
call ChooseWildEncounter_BugContest
farcall CheckRepelEffect
ret
ChooseWildEncounter_BugContest::
; Pick a random mon out of ContestMons.
.loop
call Random
cp 100 << 1
jr nc, .loop
srl a
ld hl, ContestMons
ld de, 4
.CheckMon:
sub [hl]
jr c, .GotMon
add hl, de
jr .CheckMon
.GotMon:
inc hl
; Species
ld a, [hli]
ld [wTempWildMonSpecies], a
; Min level
ld a, [hli]
ld d, a
; Max level
ld a, [hl]
sub d
jr nz, .RandomLevel
; If min and max are the same.
ld a, d
jr .GotLevel
.RandomLevel:
; Get a random level between the min and max.
ld c, a
inc c
call Random
ldh a, [hRandomAdd]
call SimpleDivide
add d
.GotLevel:
ld [wCurPartyLevel], a
xor a
ret
TryWildEncounter_BugContest:
ld a, [wPlayerTile]
call CheckSuperTallGrassTile
ld b, 40 percent
jr z, .ok
ld b, 20 percent
.ok
farcall ApplyMusicEffectOnEncounterRate
farcall ApplyCleanseTagEffectOnEncounterRate
call Random
ldh a, [hRandomAdd]
cp b
ret c
ld a, 1
and a
ret
INCLUDE "data/wild/bug_contest_mons.asm"
DoBikeStep::
nop
nop
; If the bike shop owner doesn't have our number, or
; if we've already gotten the call, we don't have to
; be here.
ld hl, wStatusFlags2
bit STATUSFLAGS2_BIKE_SHOP_CALL_F, [hl]
jr z, .NoCall
; If we're not on the bike, we don't have to be here.
ld a, [wPlayerState]
cp PLAYER_BIKE
jr nz, .NoCall
; If we're not in an area of phone service, we don't
; have to be here.
call GetMapPhoneService
and a
jr nz, .NoCall
; Check the bike step count and check whether we've
; taken 65536 of them yet.
ld hl, wBikeStep
ld a, [hli]
ld d, a
ld e, [hl]
cp 255
jr nz, .increment
ld a, e
cp 255
jr z, .dont_increment
.increment
inc de
ld [hl], e
dec hl
ld [hl], d
.dont_increment
; If we've taken at least 1024 steps, have the bike
; shop owner try to call us.
ld a, d
cp HIGH(1024)
jr c, .NoCall
; If a call has already been queued, don't overwrite
; that call.
ld a, [wSpecialPhoneCallID]
and a
jr nz, .NoCall
; Queue the call.
ld a, SPECIALCALL_BIKESHOP
ld [wSpecialPhoneCallID], a
xor a
ld [wSpecialPhoneCallID + 1], a
ld hl, wStatusFlags2
res STATUSFLAGS2_BIKE_SHOP_CALL_F, [hl]
scf
ret
.NoCall:
xor a
ret
INCLUDE "engine/overworld/cmd_queue.asm"