Prev: 43894 Up: Map Next: 44258
43925: 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 43925 LD A,(29359) 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.
43928 OR A
43929 JR NZ,Update_Berk_State_TickCounter
43931 LD A,(29363) Load A with the game event flags (GameEventFlags).
43934 BIT 1,A Is bit 1 of the game event flags set? If so, jump to Update_Berk_State_TickCounter.
43936 JR NZ,Update_Berk_State_TickCounter
43938 LD A,(29384) Load A with Berk's action lock (BerkActionLock).
43941 OR A Is Berk's action lock zero? If not, jump to Update_Berk_State_ByTaskIndex.
43942 JR NZ,Update_Berk_State_ByTaskIndex
43944 INC A Set A to 1 ready to store as the task phase.
43945 JR Update_Berk_State_SetTaskPhase Jump to Update_Berk_State_SetTaskPhase to store the task phase.
Branch on the current task index: finish phase 5, tick when idle, or start an idle Berk script.
Update_Berk_State_ByTaskIndex 43947 LD A,(29358) Load A with the current task index (CurrentTaskIndex).
43950 CP 5 Is the current task index equal to 5? If so, jump to Update_Berk_State_SetTaskPhase.
43952 JR Z,Update_Berk_State_SetTaskPhase
43954 OR A Is the current task index zero? If so, jump to Update_Berk_State_TickCounter.
43955 JR Z,Update_Berk_State_TickCounter
43957 LD HL,43889 Point HL at Berk's idle script selectors (Data_BerkIdleScriptSelectors).
43960 LD A,4 Load A with 4 as the candidate-row count.
43962 CALL Start_Berk_Script Call Start_Berk_Script to start Berk's script.
Store the chosen task phase into CurrentTaskPhase.
Update_Berk_State_SetTaskPhase 43965 LD (29359),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 43968 LD HL,29426 Point HL at the game tick counter (GameTickCounter).
43971 LD A,(HL) Fetch the game tick counter into A.
43972 OR A Is the game tick counter zero? If so, jump to Update_Berk_State_Branch.
43973 JR Z,Update_Berk_State_Branch
43975 DEC (HL) Decrease the game tick counter.
43976 RET Return.
With no active task, either run Berk's action path or pick a random idle-script selector.
Update_Berk_State_Branch 43977 LD A,(29358) Load A with the current task index (CurrentTaskIndex).
43980 OR A Is the current task index zero? If not, jump to Update_Berk_State_ActiveTaskFrameTick.
43981 JR NZ,Update_Berk_State_ActiveTaskFrameTick
43983 LD A,(29364) Load A with the task status flags (TaskStatusFlags).
43986 BIT 0,A Is bit 0 of the task status flags set? If so, jump to Update_Berk_State_ActionAndMaybeRestart.
43988 JR NZ,Update_Berk_State_ActionAndMaybeRestart
43990 LD HL,29385 Point HL at TaskSelectionTable.
43993 LD A,(29384) Load A with Berk's action lock (BerkActionLock).
43996 OR A Is Berk's action lock zero? If not, jump to Update_Berk_State_RandomActionPick.
43997 JR NZ,Update_Berk_State_RandomActionPick
43999 INC A Advance Berk's action lock by one.
44000 LD (29384),A
44003 JR Update_Berk_State_MarkTaskSlotUsed Jump to Update_Berk_State_MarkTaskSlotUsed with task 1 in A.
Pick a random task-selection slot and prepare to try up to 4 candidates.
Update_Berk_State_RandomActionPick 44005 LD A,4 Load A with 4.
44007 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random offset and keep it in C.
44010 LD C,A
44011 LD B,0 Clear B so BC is the random offset alone.
44013 ADD HL,BC Advance HL by the random offset in BC.
44014 LD A,4 Load A with 4 as the try counter.
Skip used task slots; free slots fall through, used ones try the next candidate.
Update_Berk_State_TrySelectorRow 44016 BIT 7,(HL) Is this task slot already used (bit 7)? If not, jump to Update_Berk_State_TakeSelectorByte.
44018 JR Z,Update_Berk_State_TakeSelectorByte
44020 INC HL Advance the pointer in HL by one byte.
44021 BIT 6,(HL) Is the wrap marker (bit 6) clear? If so, jump to Update_Berk_State_DecTryCounter.
44023 JR Z,Update_Berk_State_DecTryCounter
44025 LD HL,29385 Point HL at TaskSelectionTable.
Exhaust selector tries, then either run Berk's action or force task index 5 after resetting defaults.
Update_Berk_State_DecTryCounter 44028 DEC A Decrease the try counter in A.
44029 JR NZ,Update_Berk_State_TrySelectorRow Jump to Update_Berk_State_TrySelectorRow while tries remain.
44031 LD A,(29360) Load A with the dialogue line index (DialogueLineIndex).
44034 CP 4 Is the dialogue line index less than 4? If so, jump to Update_Berk_State_ActionAndMaybeRestart.
44036 JP C,Update_Berk_State_ActionAndMaybeRestart
44039 LD A,(29353) Load A with the Learner Berk mode flag (Flag_LearnerBerkMode).
44042 OR A Is the Learner Berk mode flag zero? If not, jump to Update_Berk_State_ActionAndMaybeRestart.
44043 JR NZ,Update_Berk_State_ActionAndMaybeRestart
44045 CALL Initialise_Entity_Defaults Call Initialise_Entity_Defaults to initialise entity defaults.
44048 LD A,5 Load A with 5.
44050 JR Update_Berk_State_SetTaskIndexAndAnnounce Jump to Update_Berk_State_SetTaskIndexAndAnnounce to store task index 5.
Take the free selector byte as the new task index.
Update_Berk_State_TakeSelectorByte 44052 LD A,(HL) Fetch the selected byte through HL into A.
Mark the selected task slot as used with bit 7.
Update_Berk_State_MarkTaskSlotUsed 44053 SET 7,(HL) Mark the selected task slot as used (bit 7).
Announce the new task, reset the text cursor, seed the held-entity countdown and refresh the room display.
Update_Berk_State_SetTaskIndexAndAnnounce 44055 LD (29358),A Set the current task index (CurrentTaskIndex).
44058 CALL Print_MessageByIndex Call Print_MessageByIndex to print a message by index.
44061 CALL Reset_Text_Position Call Reset_Text_Position to reset the text position.
44064 LD A,160 Load A with 160.
44066 LD (29403),A Set the held-entity pointer (Pointer_BerkHeldEntity).
44069 CALL Initialise_Room_Display Call Initialise_Room_Display to initialise the room display.
44072 RET Return.
Run Berk's action handler; if task status bit 5 is set, refresh the room display and act again before restart.
Update_Berk_State_ActionAndMaybeRestart 44073 CALL Handle_Berk_Action Call Handle_Berk_Action to handle Berk's action.
44076 CALL Clear_Messaging_Attributes Call Clear_Messaging_Attributes to clear messaging attributes.
44079 LD A,(29364) Load A with the task status flags (TaskStatusFlags).
44082 BIT 5,A Is bit 5 of the task status flags set? If not, jump to Update_Berk_State_PrintStatusAndRestart.
44084 JR Z,Update_Berk_State_PrintStatusAndRestart
44086 LD A,200 Load A with 200.
44088 LD (29403),A Set the held-entity pointer (Pointer_BerkHeldEntity).
44091 CALL Initialise_Room_Display Call Initialise_Room_Display to initialise the room display.
44094 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 44097 CALL Print_StatusMessages Call Print_StatusMessages to print status messages.
44100 POP BC Restore BC from the stack.
44101 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 44104 LD A,(29390) Load A with the timed event latch (TimedEventLatch).
44107 OR A Is the timed event latch zero? If not, return.
44108 RET NZ
44109 LD A,(29382) Load A with the frame counter (FrameCounter).
44112 DEC A Step the frame counter back by one.
44113 LD (29382),A
44116 RET NZ Return unless the frame counter is now zero.
44117 LD A,(29353) Load A with the Learner Berk mode flag (Flag_LearnerBerkMode).
44120 OR A Is the Learner Berk mode flag zero? If so, jump to Update_Berk_State_ReloadFrameCounter.
44121 JR Z,Update_Berk_State_ReloadFrameCounter
44123 LD A,5 Load A with 5.
Reload the frame counter, advance the task timer, and either paint the attr strip or play the task-fanfare path.
Update_Berk_State_ReloadFrameCounter 44125 ADD A,15 Add 15 to A.
44127 LD (29382),A Set the frame counter (FrameCounter).
44130 LD A,(29424) Load A with the task timer (TaskTimer).
44133 INC A Advance the task timer by one.
44134 LD (29424),A
44137 CP 160 Is the task timer less than 160? If so, jump to Update_Berk_State_AttrStripFromSeed.
44139 JR C,Update_Berk_State_AttrStripFromSeed
44141 LD A,11 Load A with sound index 11.
44143 CALL Queue_Sound Call Queue_Sound to queue a sound.
44146 CALL Play_QueuedSound Call Play_QueuedSound to play the queued sound.
44149 LD A,(29358) Load A with the current task index (CurrentTaskIndex).
44152 CP 5 Is the current task index equal to 5? If so, jump to Update_Berk_State_PrintStatusAndRestart.
44154 JP Z,Update_Berk_State_PrintStatusAndRestart
44157 LD A,(29353) Load A with the Learner Berk mode flag (Flag_LearnerBerkMode).
44160 OR A Is the Learner Berk mode flag zero? If so, jump to Update_Berk_State_PrintStatusAndRestart.
44161 JP Z,Update_Berk_State_PrintStatusAndRestart
44164 XOR A Clear A.
44165 LD (29358),A Set the current task index (CurrentTaskIndex).
44168 LD A,(29359) Load A with the current task phase (CurrentTaskPhase).
44171 CP 1 Is the current task phase equal to 1? If not, jump to Update_Berk_State_TooLateFeedback.
44173 JR NZ,Update_Berk_State_TooLateFeedback
44175 DEC A Step the current task phase back by one.
44176 LD (29359),A
Print too-late feedback, handle Berk's action, and reload the game tick counter.
Update_Berk_State_TooLateFeedback 44179 CALL Print_TooLateTaskFeedback Call Print_TooLateTaskFeedback to print too-late task feedback.
44182 CALL Reset_Text_Position Call Reset_Text_Position to reset the text position.
44185 CALL Handle_Berk_Action Call Handle_Berk_Action to handle Berk's action.
44188 LD A,70 Load A with 70 as the game tick reload value.
44190 LD (29426),A Set the game tick counter (GameTickCounter).
44193 RET Return.
Choose an attribute value from the task timer (bright white, or divide to map into the ink range).
Update_Berk_State_AttrStripFromSeed 44194 LD A,(29424) Load A with the task timer (TaskTimer).
44197 CP 156 Is the task timer less than 156? If so, jump to Update_Berk_State_DivModSetup.
44199 JR C,Update_Berk_State_DivModSetup
44201 LD A,199 Load A with 199.
44203 JR Update_Berk_State_Hit Jump to Update_Berk_State_Hit to paint the attribute strip.
Set up divisor 23 to map the task timer into an attribute ink.
Update_Berk_State_DivModSetup 44205 LD BC,23 Load BC with 0023.
Divide the task timer by 23 and bias the quotient into the attribute ink range.
Update_Berk_State_DivModLoop 44208 INC B Increment the quotient counter in B.
44209 SUB C Subtract C from A.
44210 JR NC,Update_Berk_State_DivModLoop Jump to Update_Berk_State_DivModLoop while A remains at least C.
44212 LD A,B Copy the quotient from B into A.
44213 ADD A,64 Add 64 to A.
Point at the status attribute strip (23204 (attribute buffer)) ready to paint.
Update_Berk_State_Hit 44215 LD HL,23204 Point HL at attribute-file address 23204 (attribute buffer).
44218 LD B,20 Load B with 20 as a loop counter.
Fill the status attribute strip, then prepare to scan the matching screen row.
Update_Berk_State_Exit 44220 LD (HL),A Store A at the address in HL.
44221 INC HL Advance HL by one byte.
44222 DJNZ Update_Berk_State_Exit Decrease the loop counter in B and jump to Update_Berk_State_Exit while bytes remain.
44224 LD HL,20900 Point HL at screen address 20900 (screen buffer).
44227 LD B,19 Load B with 19 as a loop counter.
Scan the status screen row for a non-255 cell to rotate.
Update_Berk_State_PartA 44229 LD A,(HL) Fetch the next byte through HL into A.
44230 CP 255 Is the fetched byte equal to 255? If not, jump to Update_Berk_State_PartB.
44232 JR NZ,Update_Berk_State_PartB
44234 INC HL Advance HL by one byte.
44235 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 44237 LD B,6 Load B with 6 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 44239 SCF Signal success.
44240 RR (HL) Rotate right the byte at HL.
44242 INC H Advance the high byte of the pointer in HL.
44243 DJNZ Update_Berk_State_PartC Decrease the loop counter in B and jump to Update_Berk_State_PartC while rows remain.
44245 LD HL,(29403) Load HL with the held-entity pointer (Pointer_BerkHeldEntity).
44248 LD A,H Copy the high byte of the pointer into A.
44249 OR L Merge the low byte of the pointer into A.
44250 RET Z Return if the held-entity pointer is zero.
44251 DEC HL Step the held-entity pointer back by one.
44252 LD (29403),HL Store HL in the held-entity pointer (Pointer_BerkHeldEntity).
44255 JP Initialise_Room_Display_HeldEntity Jump to Initialise_Room_Display_HeldEntity to reprint the bonus.
Prev: 43894 Up: Map Next: 44258