Prev: AB76 Up: Map Next: ACE2
AB95: Update Berk State
Used by the routine at GameStart.
Update Berk State for the player character using complex state at StateData_Boney.
Checks the current task phase/index, Berk action lock and timed events before driving Berk scripts.
Update_Berk_State AB95 LD A,($72AF) Load A with the current task phase (CurrentTaskPhase). Is the current task phase inactive (zero)? If not, skip to the timed-event checks at Update_Berk_State_TickCounter.
AB98 OR A
AB99 JR NZ,Update_Berk_State_TickCounter
AB9B LD A,($72B3) Load A with the game event flags (GameEventFlags).
AB9E BIT 1,A Is bit 01 of the game event flags set? If so, jump to Update_Berk_State_TickCounter.
ABA0 JR NZ,Update_Berk_State_TickCounter
ABA2 LD A,($72C8) Load A with Berk's action lock (BerkActionLock).
ABA5 OR A Is Berk's action lock zero? If not, jump to Update_Berk_State_ByTaskIndex.
ABA6 JR NZ,Update_Berk_State_ByTaskIndex
ABA8 INC A Set A to 01 ready to store as the task phase.
ABA9 JR Update_Berk_State_SetTaskPhase Jump to Update_Berk_State_SetTaskPhase to store the task phase.
Branch on the current task index: finish phase 05, tick when idle, or start an idle Berk script.
Update_Berk_State_ByTaskIndex ABAB LD A,($72AE) Load A with the current task index (CurrentTaskIndex).
ABAE CP $05 Is the current task index equal to 05? If so, jump to Update_Berk_State_SetTaskPhase.
ABB0 JR Z,Update_Berk_State_SetTaskPhase
ABB2 OR A Is the current task index zero? If so, jump to Update_Berk_State_TickCounter.
ABB3 JR Z,Update_Berk_State_TickCounter
ABB5 LD HL,$AB71 Point HL at Berk's idle script selectors (Data_BerkIdleScriptSelectors).
ABB8 LD A,$04 Load A with 04 as the candidate-row count.
ABBA CALL Start_Berk_Script Call Start_Berk_Script to start Berk's script.
Store the chosen task phase into CurrentTaskPhase.
Update_Berk_State_SetTaskPhase ABBD LD ($72AF),A Set the current task phase (CurrentTaskPhase).
Count down the game tick; when it hits zero, continue into the idle/task branch.
Update_Berk_State_TickCounter ABC0 LD HL,$72F2 Point HL at the game tick counter (GameTickCounter).
ABC3 LD A,(HL) Fetch the game tick counter into A.
ABC4 OR A Is the game tick counter zero? If so, jump to Update_Berk_State_Branch.
ABC5 JR Z,Update_Berk_State_Branch
ABC7 DEC (HL) Decrease the game tick counter.
ABC8 RET Return.
With no active task, either run Berk's action path or pick a random idle-script selector.
Update_Berk_State_Branch ABC9 LD A,($72AE) Load A with the current task index (CurrentTaskIndex).
ABCC OR A Is the current task index zero? If not, jump to Update_Berk_State_ActiveTaskFrameTick.
ABCD JR NZ,Update_Berk_State_ActiveTaskFrameTick
ABCF LD A,($72B4) Load A with the task status flags (TaskStatusFlags).
ABD2 BIT 0,A Is bit 00 of the task status flags set? If so, jump to Update_Berk_State_ActionAndMaybeRestart.
ABD4 JR NZ,Update_Berk_State_ActionAndMaybeRestart
ABD6 LD HL,$72C9 Point HL at TaskSelectionTable.
ABD9 LD A,($72C8) Load A with Berk's action lock (BerkActionLock).
ABDC OR A Is Berk's action lock zero? If not, jump to Update_Berk_State_RandomActionPick.
ABDD JR NZ,Update_Berk_State_RandomActionPick
ABDF INC A Advance Berk's action lock by one.
ABE0 LD ($72C8),A
ABE3 JR Update_Berk_State_MarkTaskSlotUsed Jump to Update_Berk_State_MarkTaskSlotUsed with task 01 in A.
Pick a random task-selection slot and prepare to try up to 04 candidates.
Update_Berk_State_RandomActionPick ABE5 LD A,$04 Load A with 04.
ABE7 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random offset and keep it in C.
ABEA LD C,A
ABEB LD B,$00 Clear B so BC is the random offset alone.
ABED ADD HL,BC Advance HL by the random offset in BC.
ABEE LD A,$04 Load A with 04 as the try counter.
Skip used task slots; free slots fall through, used ones try the next candidate.
Update_Berk_State_TrySelectorRow ABF0 BIT 7,(HL) Is this task slot already used (bit 07)? If not, jump to Update_Berk_State_TakeSelectorByte.
ABF2 JR Z,Update_Berk_State_TakeSelectorByte
ABF4 INC HL Advance the pointer in HL by one byte.
ABF5 BIT 6,(HL) Is the wrap marker (bit 06) clear? If so, jump to Update_Berk_State_DecTryCounter.
ABF7 JR Z,Update_Berk_State_DecTryCounter
ABF9 LD HL,$72C9 Point HL at TaskSelectionTable.
Exhaust selector tries, then either run Berk's action or force task index 05 after resetting defaults.
Update_Berk_State_DecTryCounter ABFC DEC A Decrease the try counter in A.
ABFD JR NZ,Update_Berk_State_TrySelectorRow Jump to Update_Berk_State_TrySelectorRow while tries remain.
ABFF LD A,($72B0) Load A with the dialogue line index (DialogueLineIndex).
AC02 CP $04 Is the dialogue line index less than 04? If so, jump to Update_Berk_State_ActionAndMaybeRestart.
AC04 JP C,Update_Berk_State_ActionAndMaybeRestart
AC07 LD A,($72A9) Load A with the Learner Berk mode flag (Flag_LearnerBerkMode).
AC0A OR A Is the Learner Berk mode flag zero? If not, jump to Update_Berk_State_ActionAndMaybeRestart.
AC0B JR NZ,Update_Berk_State_ActionAndMaybeRestart
AC0D CALL Initialise_Entity_Defaults Call Initialise_Entity_Defaults to initialise entity defaults.
AC10 LD A,$05 Load A with 05.
AC12 JR Update_Berk_State_SetTaskIndexAndAnnounce Jump to Update_Berk_State_SetTaskIndexAndAnnounce to store task index 05.
Take the free selector byte as the new task index.
Update_Berk_State_TakeSelectorByte AC14 LD A,(HL) Fetch the selected byte through HL into A.
Mark the selected task slot as used with bit 07.
Update_Berk_State_MarkTaskSlotUsed AC15 SET 7,(HL) Mark the selected task slot as used (bit 07).
Announce the new task, reset the text cursor, seed the held-entity countdown and refresh the room display.
Update_Berk_State_SetTaskIndexAndAnnounce AC17 LD ($72AE),A Set the current task index (CurrentTaskIndex).
AC1A CALL Print_MessageByIndex Call Print_MessageByIndex to print a message by index.
AC1D CALL Reset_Text_Position Call Reset_Text_Position to reset the text position.
AC20 LD A,$A0 Load A with A0.
AC22 LD ($72DB),A Set the held-entity pointer (Pointer_BerkHeldEntity).
AC25 CALL Initialise_Room_Display Call Initialise_Room_Display to initialise the room display.
AC28 RET Return.
Run Berk's action handler; if task status bit 05 is set, refresh the room display and act again before restart.
Update_Berk_State_ActionAndMaybeRestart AC29 CALL Handle_Berk_Action Call Handle_Berk_Action to handle Berk's action.
AC2C CALL Clear_Messaging_Attributes Call Clear_Messaging_Attributes to clear messaging attributes.
AC2F LD A,($72B4) Load A with the task status flags (TaskStatusFlags).
AC32 BIT 5,A Is bit 05 of the task status flags set? If not, jump to Update_Berk_State_PrintStatusAndRestart.
AC34 JR Z,Update_Berk_State_PrintStatusAndRestart
AC36 LD A,$C8 Load A with C8.
AC38 LD ($72DB),A Set the held-entity pointer (Pointer_BerkHeldEntity).
AC3B CALL Initialise_Room_Display Call Initialise_Room_Display to initialise the room display.
AC3E CALL Handle_Berk_Action Call Handle_Berk_Action to handle Berk's action.
Print status/rating messages and restart the game at GameRestart.
Update_Berk_State_PrintStatusAndRestart AC41 CALL Print_StatusMessages Call Print_StatusMessages to print status messages.
AC44 POP BC Restore BC from the stack.
AC45 JP GameRestart Jump to GameRestart to restart the game.
While a timed event is quiet, tick the frame counter and fall through when it expires.
Update_Berk_State_ActiveTaskFrameTick AC48 LD A,($72CE) Load A with the timed event latch (TimedEventLatch).
AC4B OR A Is the timed event latch zero? If not, return.
AC4C RET NZ
AC4D LD A,($72C6) Load A with the frame counter (FrameCounter).
AC50 DEC A Step the frame counter back by one.
AC51 LD ($72C6),A
AC54 RET NZ Return unless the frame counter is now zero.
AC55 LD A,($72A9) Load A with the Learner Berk mode flag (Flag_LearnerBerkMode).
AC58 OR A Is the Learner Berk mode flag zero? If so, jump to Update_Berk_State_ReloadFrameCounter.
AC59 JR Z,Update_Berk_State_ReloadFrameCounter
AC5B LD A,$05 Load A with 05.
Reload the frame counter, advance the task timer, and either paint the attr strip or play the task-fanfare path.
Update_Berk_State_ReloadFrameCounter AC5D ADD A,$0F Add 0F to A.
AC5F LD ($72C6),A Set the frame counter (FrameCounter).
AC62 LD A,($72F0) Load A with the task timer (TaskTimer).
AC65 INC A Advance the task timer by one.
AC66 LD ($72F0),A
AC69 CP $A0 Is the task timer less than A0? If so, jump to Update_Berk_State_AttrStripFromSeed.
AC6B JR C,Update_Berk_State_AttrStripFromSeed
AC6D LD A,$0B Load A with sound index 0B.
AC6F CALL Queue_Sound Call Queue_Sound to queue a sound.
AC72 CALL Play_QueuedSound Call Play_QueuedSound to play the queued sound.
AC75 LD A,($72AE) Load A with the current task index (CurrentTaskIndex).
AC78 CP $05 Is the current task index equal to 05? If so, jump to Update_Berk_State_PrintStatusAndRestart.
AC7A JP Z,Update_Berk_State_PrintStatusAndRestart
AC7D LD A,($72A9) Load A with the Learner Berk mode flag (Flag_LearnerBerkMode).
AC80 OR A Is the Learner Berk mode flag zero? If so, jump to Update_Berk_State_PrintStatusAndRestart.
AC81 JP Z,Update_Berk_State_PrintStatusAndRestart
AC84 XOR A Clear A.
AC85 LD ($72AE),A Set the current task index (CurrentTaskIndex).
AC88 LD A,($72AF) Load A with the current task phase (CurrentTaskPhase).
AC8B CP $01 Is the current task phase equal to 01? If not, jump to Update_Berk_State_TooLateFeedback.
AC8D JR NZ,Update_Berk_State_TooLateFeedback
AC8F DEC A Step the current task phase back by one.
AC90 LD ($72AF),A
Print too-late feedback, handle Berk's action, and reload the game tick counter.
Update_Berk_State_TooLateFeedback AC93 CALL Print_TooLateTaskFeedback Call Print_TooLateTaskFeedback to print too-late task feedback.
AC96 CALL Reset_Text_Position Call Reset_Text_Position to reset the text position.
AC99 CALL Handle_Berk_Action Call Handle_Berk_Action to handle Berk's action.
AC9C LD A,$46 Load A with 46 as the game tick reload value.
AC9E LD ($72F2),A Set the game tick counter (GameTickCounter).
ACA1 RET Return.
Choose an attribute value from the task timer (bright white, or divide to map into the ink range).
Update_Berk_State_AttrStripFromSeed ACA2 LD A,($72F0) Load A with the task timer (TaskTimer).
ACA5 CP $9C Is the task timer less than 9C? If so, jump to Update_Berk_State_DivModSetup.
ACA7 JR C,Update_Berk_State_DivModSetup
ACA9 LD A,$C7 Load A with C7.
ACAB JR Update_Berk_State_Hit Jump to Update_Berk_State_Hit to paint the attribute strip.
Set up divisor 17 to map the task timer into an attribute ink.
Update_Berk_State_DivModSetup ACAD LD BC,$0017 Load BC with 0017.
Divide the task timer by 17 and bias the quotient into the attribute ink range.
Update_Berk_State_DivModLoop ACB0 INC B Increment the quotient counter in B.
ACB1 SUB C Subtract C from A.
ACB2 JR NC,Update_Berk_State_DivModLoop Jump to Update_Berk_State_DivModLoop while A remains at least C.
ACB4 LD A,B Copy the quotient from B into A.
ACB5 ADD A,$40 Add 40 to A.
Point at the status attribute strip (5AA4 (attribute buffer)) ready to paint.
Update_Berk_State_Hit ACB7 LD HL,$5AA4 Point HL at attribute-file address 5AA4 (attribute buffer).
ACBA LD B,$14 Load B with 14 as a loop counter.
Fill the status attribute strip, then prepare to scan the matching screen row.
Update_Berk_State_Exit ACBC LD (HL),A Store A at the address in HL.
ACBD INC HL Advance HL by one byte.
ACBE DJNZ Update_Berk_State_Exit Decrease the loop counter in B and jump to Update_Berk_State_Exit while bytes remain.
ACC0 LD HL,$51A4 Point HL at screen address 51A4 (screen buffer).
ACC3 LD B,$13 Load B with 13 as a loop counter.
Scan the status screen row for a non-FF cell to rotate.
Update_Berk_State_PartA ACC5 LD A,(HL) Fetch the next byte through HL into A.
ACC6 CP $FF Is the fetched byte equal to FF? If not, jump to Update_Berk_State_PartB.
ACC8 JR NZ,Update_Berk_State_PartB
ACCA INC HL Advance HL by one byte.
ACCB DJNZ Update_Berk_State_PartA Decrease the loop counter in B and jump to Update_Berk_State_PartA while bytes remain.
Prepare to rotate six pixel rows of the found status glyph.
Update_Berk_State_PartB ACCD LD B,$06 Load B with 06 as a loop counter.
Rotate the status glyph down the screen rows, then count down the held-entity pointer if set.
Update_Berk_State_PartC ACCF SCF Signal success.
ACD0 RR (HL) Rotate right the byte at HL.
ACD2 INC H Advance the high byte of the pointer in HL.
ACD3 DJNZ Update_Berk_State_PartC Decrease the loop counter in B and jump to Update_Berk_State_PartC while rows remain.
ACD5 LD HL,($72DB) Load HL with the held-entity pointer (Pointer_BerkHeldEntity).
ACD8 LD A,H Copy the high byte of the pointer into A.
ACD9 OR L Merge the low byte of the pointer into A.
ACDA RET Z Return if the held-entity pointer is zero.
ACDB DEC HL Step the held-entity pointer back by one.
ACDC LD ($72DB),HL Store HL in the held-entity pointer (Pointer_BerkHeldEntity).
ACDF JP Initialise_Room_Display_HeldEntity Jump to Initialise_Room_Display_HeldEntity to reprint the bonus.
Prev: AB76 Up: Map Next: ACE2