Prev: D06C Up: Map Next: D153
D074: Script Routine: (3E) Vat action
Scan nearby entity slots for something the vat can interact with.
Input
IX Address of complex state data for an entity
ScriptRoutine_VatScanNearbySlots D074 LD E,$FF Load E with FF.
D076 LD IY,$8F9F Point IY at an inactive complex-entity slot (StateData_Complex_13).
D07A LD B,$03 Load B with 03 as a loop counter.
Scan nearby type-0B slots for the closest X neighbour within 28 of the vat.
ScriptRoutine_VatScanNearbySlots_ScanEntitySlot D07C PUSH BC Stash BC on the stack.
D07D LD A,(IY+$00) Load A with the referenced entity's entity type.
D080 CP $0B Is referenced entity's entity type equal to 0B? If not, jump to ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot.
D082 JR NZ,ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot
D084 BIT 7,(IY+$01) Is bit 07 of the referenced entity's flags byte 1 set? If so, jump to ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot.
D088 JR NZ,ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot
D08A LD A,(IY+$05) Load A with the referenced entity's y-coordinate.
D08D CP $1D Is referenced entity's y-coordinate less than 1D? If so, jump to ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot.
D08F JR C,ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot
D091 LD A,(IX+$07) Load A with the current entity's x-coordinate.
D094 SUB (IY+$07) Subtract the referenced entity's x-coordinate from A.
D097 LD D,A Copy the signed x-delta into D (bit 07 = sign).
D098 JR NC,ScriptRoutine_VatScanNearbySlots_TrackBestAbsDeltaX Jump to ScriptRoutine_VatScanNearbySlots_TrackBestAbsDeltaX if the subtraction did not underflow.
D09A NEG Negate A to get abs(x-delta).
Keep the nearest in-range candidate in E / TemporaryEntityPointer_Misc.
ScriptRoutine_VatScanNearbySlots_TrackBestAbsDeltaX D09C CP $28 Is abs(x-delta) less than 28? If not, jump to ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot.
D09E JR NC,ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot
D0A0 CP E Is abs(x-delta) greater than or equal to the best-so-far in E? If so, jump to ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot.
D0A1 JR NC,ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot
D0A3 LD E,A Store this abs(x-delta) as the new best in E.
D0A4 LD ($CF35),IY Store IY in the miscellaneous-entity temp pointer (TemporaryEntityPointer_Misc).
Advance to the next candidate slot and finish the scan when B hits zero.
ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot D0A8 LD BC,$000E Each entity record is 000E bytes long, then load that length into BC.
D0AB ADD IY,BC Add the offset in BC to the entity pointer in IY.
D0AD POP BC Restore BC from the stack.
D0AE DJNZ ScriptRoutine_VatScanNearbySlots_ScanEntitySlot Decrease the loop counter in B and jump to ScriptRoutine_VatScanNearbySlots_ScanEntitySlot if more iterations remain.
D0B0 LD A,$28 Load A with 28 (no-candidate sentinel matching the initial E).
D0B2 CP E Was no nearer entity found (E still 28)? If carry set, jump to ScriptRoutine_VatScanNearbySlots_NoCandidateNearby.
D0B3 JP C,ScriptRoutine_VatScanNearbySlots_NoCandidateNearby
D0B6 LD IY,($CF35) Load IY from TemporaryEntityPointer_Misc.
D0BA LD A,(IX+$04) Load A with the current entity's room index.
D0BD CP (IY+$04) Does the candidate share this entity's room (IY+04)? If so, jump to ScriptRoutine_VatScanNearbySlots_SameRoomCheckY.
D0C0 JR Z,ScriptRoutine_VatScanNearbySlots_SameRoomCheckY
D0C2 JR C,ScriptRoutine_VatScanNearbySlots_SetDirBit2 Jump to ScriptRoutine_VatScanNearbySlots_SetDirBit2 if the result underflowed.
D0C4 SET 3,(IX+$09) Set bit 03 of the current entity's direction / state.
D0C8 JR ScriptRoutine_VatScanNearbySlots_DeltaSignPlus Jump to ScriptRoutine_VatScanNearbySlots_DeltaSignPlus to continue this routine.
Candidate is in a lower room: set direction bit 02 before choosing a chase script.
ScriptRoutine_VatScanNearbySlots_SetDirBit2 D0CA SET 2,(IX+$09) Set bit 02 of the current entity's direction / state.
D0CE JR ScriptRoutine_VatScanNearbySlots_DeltaSignPlus Jump to ScriptRoutine_VatScanNearbySlots_DeltaSignPlus to continue this routine.
Same-room and aligned in X: clear the candidate type and run the capture script; else check Y.
ScriptRoutine_VatScanNearbySlots_SameRoomCheckY D0D0 LD A,E Copy the best abs(x-delta) from E into A.
D0D1 OR A Is the best abs(x-delta) zero? If not, jump to ScriptRoutine_VatScanNearbySlots_DeltaSignPlus.
D0D2 JR NZ,ScriptRoutine_VatScanNearbySlots_DeltaSignPlus
D0D4 LD A,(IX+$05) Load A with the current entity's y-coordinate.
D0D7 CP (IY+$05) Does this entity's y match the candidate's y (IY+05)? If not, jump to ScriptRoutine_VatScanNearbySlots_YMismatchSetBit6.
D0DA JR NZ,ScriptRoutine_VatScanNearbySlots_YMismatchSetBit6
D0DC LD (IY+$00),$00 Write 00 to the referenced entity's entity type.
D0E0 LD HL,$CFFE Point HL at Drutt script data CFFE.
D0E3 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Same-room Y mismatch: set direction bit 06 and run the lift/absorb script.
ScriptRoutine_VatScanNearbySlots_YMismatchSetBit6 D0E6 SET 6,(IX+$09) Set bit 06 of the current entity's direction / state.
D0EA LD HL,$CFBC Point HL at Drutt script data CFBC.
D0ED JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Positive X-delta: randomly pick the copy script (DruttScript_Copy) or the scan script (DruttScript_Scan).
ScriptRoutine_VatScanNearbySlots_DeltaSignPlus D0F0 BIT 7,D Is bit 07 of the signed x-delta in D set? If so, jump to ScriptRoutine_VatScanNearbySlots_DeltaSignMinus.
D0F2 JR NZ,ScriptRoutine_VatScanNearbySlots_DeltaSignMinus
D0F4 LD A,$02 Load A with 02 as the PseudoRandom random maximum.
D0F6 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
D0F9 OR A Is A zero? If so, jump to ScriptRoutine_VatScanNearbySlots_RunScanScript.
D0FA JR Z,ScriptRoutine_VatScanNearbySlots_RunScanScript
D0FC LD HL,$CF88 Point HL at Drutt script data DruttScript_Copy.
D0FF JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Run the vat-scan candidate script at DruttScript_Scan.
ScriptRoutine_VatScanNearbySlots_RunScanScript D102 LD HL,$CFAA Point HL at Drutt script data DruttScript_Scan.
D105 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Negative X-delta: randomly pick the near script (DruttScript_Near) or the alternate (DruttScript_Alt).
ScriptRoutine_VatScanNearbySlots_DeltaSignMinus D108 LD A,$02 Load A with 02 as the PseudoRandom random maximum.
D10A CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
D10D OR A Is A zero? If so, jump to ScriptRoutine_VatScanNearbySlots_RunAltScript.
D10E JR Z,ScriptRoutine_VatScanNearbySlots_RunAltScript
D110 LD HL,$CF54 Point HL at Drutt script data DruttScript_Near.
D113 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Run the alternate near-range Drutt script at DruttScript_Alt.
ScriptRoutine_VatScanNearbySlots_RunAltScript D116 LD HL,$CF76 Point HL at Drutt script data DruttScript_Alt.
D119 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
D11C LD D,H Copy the high byte of the pointer into D.
D11D RST $08 Call the restart routine.
D11E ADC A,B Add B into A, including the previous success marker.
D11F RST $08 Call the restart routine.
D120 JP C,$46CF Jump to 46CF (screen buffer) if the result underflowed.
D123 RST $08 Call the restart routine.
No nearby candidate: rarely run a positional script, else index the random vat table.
ScriptRoutine_VatScanNearbySlots_NoCandidateNearby D124 LD A,$05 Load A with 05 as the PseudoRandom random maximum.
D126 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
D129 OR A Is A zero? If not, jump to ScriptRoutine_VatScanNearbySlots_IndexTable.
D12A JR NZ,ScriptRoutine_VatScanNearbySlots_IndexTable
D12C LD A,(IX+$07) Load A with the current entity's x-coordinate.
D12F CP $70 Is the current entity's x-coordinate less than 70? If so, jump to ScriptRoutine_VatScanNearbySlots_RunNearScript.
D131 JR C,ScriptRoutine_VatScanNearbySlots_RunNearScript
D133 LD HL,$CFAA Point HL at Drutt script data DruttScript_Scan.
D136 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Run the near-range Drutt script at DruttScript_Near when the vat sits left of 70.
ScriptRoutine_VatScanNearbySlots_RunNearScript D139 LD HL,$CF54 Point HL at Drutt script data DruttScript_Near.
D13C JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Pick a random word from the Drutt script table at D11C and execute it.
ScriptRoutine_VatScanNearbySlots_IndexTable D13F LD A,$04 Load A with 04 as the PseudoRandom random maximum.
D141 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
D144 ADD A,A Double A to form a word index into the table at D11C.
D145 LD E,A Copy that word index into E.
D146 LD D,$00 Clear D (high byte of a pointer/offset).
D148 LD HL,$D11C Point HL at Drutt routine data D11C.
D14B ADD HL,DE Add the offset in DE to the pointer in HL.
D14C LD E,(HL) Fetch the word at HL into DE.
D14D INC HL
D14E LD D,(HL)
D14F EX DE,HL Swap the destination and source pointers in DE/HL.
D150 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Prev: D06C Up: Map Next: D153