Prev: 53356 Up: Map Next: 53587
53364: Script Routine: (62) 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 53364 LD E,255 Load E with 255.
53366 LD IY,36767 Point IY at an inactive complex-entity slot (StateData_Complex_13).
53370 LD B,3 Load B with 3 as a loop counter.
Scan nearby type-11 slots for the closest X neighbour within 40 of the vat.
ScriptRoutine_VatScanNearbySlots_ScanEntitySlot 53372 PUSH BC Stash BC on the stack.
53373 LD A,(IY+0) Load A with the referenced entity's entity type.
53376 CP 11 Is referenced entity's entity type equal to 11? If not, jump to ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot.
53378 JR NZ,ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot
53380 BIT 7,(IY+1) Is bit 7 of the referenced entity's flags byte 1 set? If so, jump to ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot.
53384 JR NZ,ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot
53386 LD A,(IY+5) Load A with the referenced entity's y-coordinate.
53389 CP 29 Is referenced entity's y-coordinate less than 29? If so, jump to ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot.
53391 JR C,ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot
53393 LD A,(IX+7) Load A with the current entity's x-coordinate.
53396 SUB (IY+7) Subtract the referenced entity's x-coordinate from A.
53399 LD D,A Copy the signed x-delta into D (bit 7 = sign).
53400 JR NC,ScriptRoutine_VatScanNearbySlots_TrackBestAbsDeltaX Jump to ScriptRoutine_VatScanNearbySlots_TrackBestAbsDeltaX if the subtraction did not underflow.
53402 NEG Negate A to get abs(x-delta).
Keep the nearest in-range candidate in E / TemporaryEntityPointer_Misc.
ScriptRoutine_VatScanNearbySlots_TrackBestAbsDeltaX 53404 CP 40 Is abs(x-delta) less than 40? If not, jump to ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot.
53406 JR NC,ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot
53408 CP E Is abs(x-delta) greater than or equal to the best-so-far in E? If so, jump to ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot.
53409 JR NC,ScriptRoutine_VatScanNearbySlots_AdvanceEntitySlot
53411 LD E,A Store this abs(x-delta) as the new best in E.
53412 LD (53045),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 53416 LD BC,14 Each entity record is 0014 bytes long, then load that length into BC.
53419 ADD IY,BC Add the offset in BC to the entity pointer in IY.
53421 POP BC Restore BC from the stack.
53422 DJNZ ScriptRoutine_VatScanNearbySlots_ScanEntitySlot Decrease the loop counter in B and jump to ScriptRoutine_VatScanNearbySlots_ScanEntitySlot if more iterations remain.
53424 LD A,40 Load A with 40 (no-candidate sentinel matching the initial E).
53426 CP E Was no nearer entity found (E still 40)? If carry set, jump to ScriptRoutine_VatScanNearbySlots_NoCandidateNearby.
53427 JP C,ScriptRoutine_VatScanNearbySlots_NoCandidateNearby
53430 LD IY,(53045) Load IY from TemporaryEntityPointer_Misc.
53434 LD A,(IX+4) Load A with the current entity's room index.
53437 CP (IY+4) Does the candidate share this entity's room (IY+4)? If so, jump to ScriptRoutine_VatScanNearbySlots_SameRoomCheckY.
53440 JR Z,ScriptRoutine_VatScanNearbySlots_SameRoomCheckY
53442 JR C,ScriptRoutine_VatScanNearbySlots_SetDirBit2 Jump to ScriptRoutine_VatScanNearbySlots_SetDirBit2 if the result underflowed.
53444 SET 3,(IX+9) Set bit 3 of the current entity's direction / state.
53448 JR ScriptRoutine_VatScanNearbySlots_DeltaSignPlus Jump to ScriptRoutine_VatScanNearbySlots_DeltaSignPlus to continue this routine.
Candidate is in a lower room: set direction bit 2 before choosing a chase script.
ScriptRoutine_VatScanNearbySlots_SetDirBit2 53450 SET 2,(IX+9) Set bit 2 of the current entity's direction / state.
53454 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 53456 LD A,E Copy the best abs(x-delta) from E into A.
53457 OR A Is the best abs(x-delta) zero? If not, jump to ScriptRoutine_VatScanNearbySlots_DeltaSignPlus.
53458 JR NZ,ScriptRoutine_VatScanNearbySlots_DeltaSignPlus
53460 LD A,(IX+5) Load A with the current entity's y-coordinate.
53463 CP (IY+5) Does this entity's y match the candidate's y (IY+5)? If not, jump to ScriptRoutine_VatScanNearbySlots_YMismatchSetBit6.
53466 JR NZ,ScriptRoutine_VatScanNearbySlots_YMismatchSetBit6
53468 LD (IY+0),0 Write 0 to the referenced entity's entity type.
53472 LD HL,53246 Point HL at Drutt script data 53246.
53475 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Same-room Y mismatch: set direction bit 6 and run the lift/absorb script.
ScriptRoutine_VatScanNearbySlots_YMismatchSetBit6 53478 SET 6,(IX+9) Set bit 6 of the current entity's direction / state.
53482 LD HL,53180 Point HL at Drutt script data 53180.
53485 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 53488 BIT 7,D Is bit 7 of the signed x-delta in D set? If so, jump to ScriptRoutine_VatScanNearbySlots_DeltaSignMinus.
53490 JR NZ,ScriptRoutine_VatScanNearbySlots_DeltaSignMinus
53492 LD A,2 Load A with 2 as the PseudoRandom random maximum.
53494 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
53497 OR A Is A zero? If so, jump to ScriptRoutine_VatScanNearbySlots_RunScanScript.
53498 JR Z,ScriptRoutine_VatScanNearbySlots_RunScanScript
53500 LD HL,53128 Point HL at Drutt script data DruttScript_Copy.
53503 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Run the vat-scan candidate script at DruttScript_Scan.
ScriptRoutine_VatScanNearbySlots_RunScanScript 53506 LD HL,53162 Point HL at Drutt script data DruttScript_Scan.
53509 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 53512 LD A,2 Load A with 2 as the PseudoRandom random maximum.
53514 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
53517 OR A Is A zero? If so, jump to ScriptRoutine_VatScanNearbySlots_RunAltScript.
53518 JR Z,ScriptRoutine_VatScanNearbySlots_RunAltScript
53520 LD HL,53076 Point HL at Drutt script data DruttScript_Near.
53523 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 53526 LD HL,53110 Point HL at Drutt script data DruttScript_Alt.
53529 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
53532 LD D,H Copy the high byte of the pointer into D.
53533 RST 8 Call the restart routine.
53534 ADC A,B Add B into A, including the previous success marker.
53535 RST 8 Call the restart routine.
53536 JP C,18127 Jump to 18127 (screen buffer) if the result underflowed.
53539 RST 8 Call the restart routine.
No nearby candidate: rarely run a positional script, else index the random vat table.
ScriptRoutine_VatScanNearbySlots_NoCandidateNearby 53540 LD A,5 Load A with 5 as the PseudoRandom random maximum.
53542 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
53545 OR A Is A zero? If not, jump to ScriptRoutine_VatScanNearbySlots_IndexTable.
53546 JR NZ,ScriptRoutine_VatScanNearbySlots_IndexTable
53548 LD A,(IX+7) Load A with the current entity's x-coordinate.
53551 CP 112 Is the current entity's x-coordinate less than 112? If so, jump to ScriptRoutine_VatScanNearbySlots_RunNearScript.
53553 JR C,ScriptRoutine_VatScanNearbySlots_RunNearScript
53555 LD HL,53162 Point HL at Drutt script data DruttScript_Scan.
53558 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 112.
ScriptRoutine_VatScanNearbySlots_RunNearScript 53561 LD HL,53076 Point HL at Drutt script data DruttScript_Near.
53564 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Pick a random word from the Drutt script table at 53532 and execute it.
ScriptRoutine_VatScanNearbySlots_IndexTable 53567 LD A,4 Load A with 4 as the PseudoRandom random maximum.
53569 CALL PseudoRandom Call PseudoRandom to pick a pseudo-random value.
53572 ADD A,A Double A to form a word index into the table at 53532.
53573 LD E,A Copy that word index into E.
53574 LD D,0 Clear D (high byte of a pointer/offset).
53576 LD HL,53532 Point HL at Drutt routine data 53532.
53579 ADD HL,DE Add the offset in DE to the pointer in HL.
53580 LD E,(HL) Fetch the word at HL into DE.
53581 INC HL
53582 LD D,(HL)
53583 EX DE,HL Swap the destination and source pointers in DE/HL.
53584 JP ScriptInstruction Jump to ScriptInstruction to execute the script instruction at HL.
Prev: 53356 Up: Map Next: 53587