Prev: 44258 Up: Map Next: 44311
44260: Start Berk Script
Used by the routine at Update_Berk_State.
Pick a Berk script row from the table at HL and enter ScriptInstruction.
Input
HL Base of the Berk script table to sample
A Number of candidate rows to skip / try
Start_Berk_Script 44260 LD (44258),HL Save the script-table base at Pointer_BerkScriptTableBase.
44263 LD (44291),A Remember the original row count at 44291.
Pick a random Berk script-table row from the candidate base.
Start_Berk_Script_PickRandom 44266 PUSH AF Stash AF on the stack.
44267 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random offset and keep it in C.
44270 LD C,A
44271 LD B,0 Clear B so BC is the random offset alone.
44273 ADD HL,BC Advance HL to the randomly chosen table row.
44274 POP AF Restore AF from the stack.
Claim a free script row, or step on / reload the table when the candidate is busy.
Start_Berk_Script_TestRowInUse 44275 BIT 7,(HL) Is this script row already marked in-use (bit 7)? If not, take it at Start_Berk_Script_ClaimRow.
44277 JR Z,Start_Berk_Script_ClaimRow
44279 INC HL Step to the flags byte of the next candidate.
44280 BIT 6,(HL) Is this candidate free (bit 6 clear)? If so, restart from the saved base at Start_Berk_Script_CountTries.
44282 JR Z,Start_Berk_Script_CountTries
44284 LD HL,(44258) Reload the script-table base from Pointer_BerkScriptTableBase.
Count down remaining script-row tries; when exhausted, clear in-use bits and retry.
Start_Berk_Script_CountTries 44287 DEC A Count down the remaining tries, then loop to Start_Berk_Script_TestRowInUse while tries remain.
44288 JR NZ,Start_Berk_Script_TestRowInUse
44290 LD B,1 Only one clear-pass remains, then continue into the in-use-bit reset at Start_Berk_Script_Branch.
44292 LD HL,(44258) Reload the script-table base from Pointer_BerkScriptTableBase.
44295 PUSH HL Stash HL on the stack.
Clear every script-row in-use flag, then retry a fresh random pick.
Start_Berk_Script_Branch 44296 RES 7,(HL) Clear the script-row in-use flag (bit 7 of the Berk script table entry).
44298 INC HL Advance the current pointer by one byte.
44299 DJNZ Start_Berk_Script_Branch Decrease the loop counter in B and jump to Start_Berk_Script_Branch if more iterations remain.
44301 POP HL Restore HL from the stack.
44302 LD A,(44291) Reload the original row-count from the self-modifying byte at 44291.
44305 JR Start_Berk_Script_PickRandom Jump to Start_Berk_Script_PickRandom to try another Berk script row.
Claim this script row by setting its in-use flag and return the selected byte.
Start_Berk_Script_ClaimRow 44307 LD A,(HL) Fetch the next byte from the current pointer.
44308 SET 7,(HL) Set the script-row in-use flag (bit 7 of the Berk script table entry).
44310 RET Return.
Prev: 44258 Up: Map Next: 44311