Prev: D45B Up: Map Next: D562
D48E: Collect Entity
Used by the routine at Update_Collectibles.
Collect Entity against the complex entity table at StateData_Boney.
Input
IX Address of complex state data for an entity
Awards score and clears a collectible when Berk touches it.
Collect_Entity D48E LD DE,($72FB) Load DE with the camera room-origin pointer (Pointer_CameraRoomOrigin).
D492 LD L,C Copy the low byte from C into L (build HL).
D493 LD H,B Copy the high byte from B into H (build HL).
D494 LD A,($72B3) Load A with the game event flags (GameEventFlags).
D497 LD B,A Keep game event flags in B for later.
D498 LD A,(IX+$00) Fetch the current entity's type for the type test.
D49B BIT 7,A Is bit 07 of the current entity's entity type set? If so, return.
D49D RET NZ
D49E OR A Is the current entity's entity type zero? If so, jump to Collect_Entity_TypeZeroBit5.
D49F JR Z,Collect_Entity_TypeZeroBit5
D4A1 CP $0B Is the current entity's entity type equal to 0B? If so, jump to Collect_Entity_AsHeld.
D4A3 JR Z,Collect_Entity_AsHeld
D4A5 BIT 1,B Is bit 01 of the loop/flags byte in B set? If so, jump to AdvanceScriptInstruction.
D4A7 JP NZ,AdvanceScriptInstruction
D4AA LD (IX+$00),$00 Write 00 to the current entity's entity type.
D4AE RET Return.
Empty slot with bit 05 set: under the event latch, become type 0A and clear that bit.
Collect_Entity_TypeZeroBit5 D4AF BIT 5,(IX+$03) Is bit 05 of flags byte 3 (IX+03) set? If not, jump to Collect_Entity_MaybeSetBit5.
D4B3 JP Z,Collect_Entity_MaybeSetBit5
D4B6 BIT 1,B Is bit 01 of the loop/flags byte in B set? If not, return.
D4B8 RET Z
D4B9 LD (IX+$00),$0A Write 0A to the current entity's entity type.
D4BD RES 5,(IX+$03) Reset bit 05 of flags byte 3 (IX+03).
D4C1 RET Return.
Type-0B collectible: score it, branch on flags, or tick its counter / wander script.
Collect_Entity_AsHeld D4C2 LD A,(IX+$01) Load A with the current entity's flags byte 1.
D4C5 BIT 6,A Is bit 06 of the current entity's flags byte 1 set? If so, jump to Collect_Entity_RunScoreScript.
D4C7 JR NZ,Collect_Entity_RunScoreScript
D4C9 AND %10100000 Keep only bits 07 and 05 of that flags byte (mask A0).
D4CB JR NZ,Collect_Entity_Branch If the result was non-zero, jump to Collect_Entity_Branch.
D4CD LD A,(IX+$06) Load A with the current entity's y-extent.
D4D0 CP $23 Is the current entity's y-extent less than 23? If not, jump to Collect_Entity_TestCounterBit7.
D4D2 JR NC,Collect_Entity_TestCounterBit7
Run the collectible score script at D3C8.
Collect_Entity_RunScoreScript D4D4 LD HL,$D3C8 Point HL at collectible/score table data (D3C8).
D4D7 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Flagged type-0B path: clear the counter and run the alternate score script at D416.
Collect_Entity_Branch D4DA XOR A Clear the collectible counter.
D4DB LD ($72BC),A Set the collectible counter (CollectibleCounter).
D4DE LD HL,$D416 Point HL at collectible/score table data (D416).
D4E1 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
High collectibles skip scripting while counter bit 07 is set.
Collect_Entity_TestCounterBit7 D4E4 LD A,($72BC) Load A with the collectible counter (CollectibleCounter).
D4E7 BIT 7,A Is bit 07 of collectible counter set? If so, jump to AdvanceScriptInstruction.
D4E9 JP NZ,AdvanceScriptInstruction
Tick the collectible counter down, or reseed it and launch a wander script when it hits zero.
Collect_Entity_DecCounterOrScript D4EC OR A Is collectible counter zero? If so, jump to Collect_Entity_RandomiseCounterScript.
D4ED JR Z,Collect_Entity_RandomiseCounterScript
D4EF DEC A Step collectible counter back by one.
D4F0 LD ($72BC),A Set the collectible counter (CollectibleCounter).
D4F3 JP AdvanceScriptInstruction Jump to AdvanceScriptInstruction to run the next script instruction.
Reseed the counter to 84 plus a random and run the wander script at D3CE.
Collect_Entity_RandomiseCounterScript D4F6 LD A,$06 Load A with 06 as the PseudoRandom random maximum.
D4F8 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
D4FB ADD A,$84 Add 84 to A (build a score/message digit address).
D4FD LD ($72BC),A Set the collectible counter (CollectibleCounter).
D500 LD HL,$D3CE Point HL at collectible/score table data (D3CE).
D503 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
D506 INC B Advance the collectible-slot counter in B.
D507 LD L,L Keep L unchanged (alignment byte in the compiled routine).
D508 INC H Advance the high byte of the pointer in HL.
D509 LD (BC),A Write the computed score/digit byte through BC.
D50A LD HL,($05D4) Load HL from 05D4.
D50D LD L,L Keep L unchanged (alignment byte in the compiled routine).
D50E DAA Decimal-adjust A after adding to a score digit.
D50F INC BC Advance the counter/pointer in BC by one.
D510 INC E Advance the low byte of the pointer/offset in DE.
D511 CALL NC,$6B00 If not below, call PrimaryDisplayBuffer_0.
D514 DEC H Step the high byte of the score/table pointer back by one.
D515 INC BC Advance the counter/pointer in BC by one.
D516 JR C,Collect_Entity_DecCounterOrScript If a hit/borrow was signaled, jump to Collect_Entity_DecCounterOrScript.
Idle empty slot: rarely raise bit 05 and copy spawn coordinates from table D506.
Collect_Entity_MaybeSetBit5 D518 BIT 1,B Is bit 01 of the loop/flags byte in B set? If so, return.
D51A RET NZ
D51B LD A,$0A Load A with 0A.
D51D CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
D520 OR A Is 0A zero? If not, return.
D521 RET NZ
D522 SET 5,(IX+$03) Set bit 05 of flags byte 3 (IX+03).
D526 LD A,$03 Load A with 03.
D528 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
D52B ADD A,A Double 03 (word index).
D52C LD C,A Keep 03 in C for later.
D52D ADD A,A Double 03 (word index).
D52E ADD A,C Add the saved count in C to A.
D52F LD C,A Keep 03 in C for later.
D530 LD B,$00 Load B with 00 as a loop counter.
D532 LD IY,$D506 Point IY at collectible storage (D506).
D536 ADD IY,BC Add the offset in BC to the entity pointer in IY.
D538 LD A,(IY+$00) Load A with the referenced entity's entity type.
D53B OR A Is referenced entity's entity type zero? If so, jump to Collect_Entity_CopyIYCoordsToIX.
D53C JR Z,Collect_Entity_CopyIYCoordsToIX
D53E CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
Copy the chosen spawn coords / room from IY into this slot and launch its script.
Collect_Entity_CopyIYCoordsToIX D541 ADD A,(IY+$01) Add the referenced entity's flags byte 1 to A.
D544 LD (IX+$07),A Set the current entity's x-coordinate.
D547 LD (IX+$08),A Set the current entity's x-extent.
D54A LD A,(IY+$02) Load A with the referenced entity's flags byte 2.
D54D LD (IX+$05),A Set the current entity's y-coordinate.
D550 LD (IX+$06),A Set the current entity's y-extent.
D553 LD A,(IY+$03) Load A with the referenced entity's flags byte 3.
D556 LD (IX+$04),A Set the current entity's room index.
D559 LD L,(IY+$04) Load L with the referenced entity's room index.
D55C LD H,(IY+$05) Load H with the referenced entity's y-coordinate.
D55F JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Prev: D45B Up: Map Next: D562