Prev: AF3F Up: Map Next: B055
AF79: Handle Valve
The handler for a valve lying about in a room (objects 60 to 63). If the room has a socket of the same shape the valve moves towards it one step at a time along the x and y axes, and is pulled down by gravity. When it sits on the socket it is placed: the object number goes up by four (64 to 67), the screen colours cycle sixteen times with a sparkle, the chamber ink is set to white, and one is added to ChambersActivated. If that makes 24 the game is completed.
A valve which is not on its way to a socket is just moved with gravity, and stopped and redrawn if it has been dropped.
Input
IX The address of the object
Look for a matching socket.
HandleValve AF79 CALL SetPixelAdjustments_Valve Call SetPixelAdjustments_Valve.
AF7C LD HL,$0470 Set HL to 0470.
AF7F CALL FindObject Call FindObject.
AF82 JR NC,HandleValve_Fall Jump to HandleValve_Fall if there is no socket.
AF84 LD A,(IY+$00) Fetch the sprite number of the socket.
AF87 AND %00000011 Keep bits 0 and 1.
AF89 LD C,A Keep it in C.
AF8A LD A,(IX+$00) Fetch the object number of the valve.
AF8D AND %00000011 Keep bits 0 and 1.
AF8F CP C Jump to HandleValve_Fall if the shapes are different.
AF90 JR NZ,HandleValve_Fall
Set the speed towards the socket.
AF92 LD A,(IX+$01) Subtract the socket x from the valve x.
AF95 SUB (IY+$01)
AF98 JR Z,HandleValve_SetSpeedX Jump to HandleValve_SetSpeedX if they are the same.
AF9A LD A,$01 Set the speed to 01.
AF9C JP M,HandleValve_SetSpeedX Jump to HandleValve_SetSpeedX if the valve is west of the socket.
AF9F NEG Make the speed negative.
HandleValve_SetSpeedX AFA1 LD (IX+$09),A Set the speed along the x axis.
AFA4 LD A,(IX+$02) Subtract the socket y from the valve y.
AFA7 SUB (IY+$02)
AFAA JR Z,HandleValve_SetSpeedY Jump to HandleValve_SetSpeedY if they are the same.
AFAC LD A,$01 Set the speed to 01.
AFAE JP M,HandleValve_SetSpeedY Jump to HandleValve_SetSpeedY if the valve is south of the socket.
AFB1 NEG Make the speed negative.
HandleValve_SetSpeedY AFB3 LD (IX+$0A),A Set the speed along the y axis.
Place the valve if it is on the socket.
AFB6 LD A,(IX+$01) Compare the x positions.
AFB9 CP (IY+$01)
AFBC JR NZ,HandleValve_Fall Jump to HandleValve_Fall if they differ.
AFBE LD A,(IX+$02) Compare the y positions.
AFC1 CP (IY+$02)
AFC4 JR NZ,HandleValve_Fall Jump to HandleValve_Fall if they differ.
AFC6 LD A,(IY+$03) Fetch the height of the socket.
AFC9 ADD A,$0C Add 0C.
AFCB CP (IX+$03) Compare it with the height of the valve.
AFCE JR Z,HandleValve_Arrived Jump to HandleValve_Arrived if the valve is on the socket.
Move the valve if it is not placed yet.
HandleValve_Fall AFD0 CALL ApplyGravityAndMove Call ApplyGravityAndMove.
AFD3 BIT 0,(IX+$0D) Jump to HandleValve_Dropped if the valve has been dropped.
AFD7 JR NZ,HandleValve_Dropped
AFD9 CALL TestObjectMoving Return if the valve is not moving.
AFDC RET Z
HandleValve_Dropped AFDD RES 0,(IX+$0D) Reset bit 0 of the state.
AFE1 CALL ClearSpeed Call ClearSpeed.
AFE4 JP PlaySoundAndRedraw Jump to PlaySoundAndRedraw.
Place the valve on the socket.
HandleValve_Arrived AFE7 LD A,(IX+$00) Fetch the object number of the valve.
AFEA XOR (IY+$00) Combine it with that of the socket.
AFED AND %00000011 Keep bits 0 and 1.
AFEF JR Z,HandleValve_PlaceValve Jump to HandleValve_PlaceValve if the shapes are the same.
AFF1 LD (IX+$00),$40 Write 40 as the sprite number.
AFF5 JP SetWipeAndDrawFlags Jump to SetWipeAndDrawFlags.
HandleValve_PlaceValve AFF8 LD A,(IX+$00) Fetch the object number of the valve.
AFFB OR %00000100 Set bit 2.
AFFD LD (IX+$00),A Store the object number.
B000 CALL PlaySoundAndRedraw Call PlaySoundAndRedraw.
B003 LD D,$10 Set a counter of 10.
Cycle the screen colours.
HandleValve_CycleColours B005 LD HL,$5800 Point HL at 5800 (in the attribute buffer).
B008 LD BC,$0300 Set BC to 0300.
HandleValve_CycleAttribute B00B LD A,(HL) Fetch an attribute.
B00C AND %11111000 Keep bits 3 to 7.
B00E LD E,A Keep it in E.
B00F LD A,(HL) Fetch the attribute and add one.
B010 INC A
B011 AND %00000111 Keep bits 0 to 2.
B013 OR E Combine it with the rest of the attribute.
B014 LD (HL),A Store the new attribute.
B015 INC HL Move on to the next attribute.
B016 DEC BC Count down the attributes.
B017 LD A,B Jump to HandleValve_CycleAttribute until all of them have been changed.
B018 OR C
B019 JR NZ,HandleValve_CycleAttribute
B01B CALL PlaySparkleSound Call PlaySparkleSound.
B01E LD BC,$0200 Set BC to 0200.
HandleValve_Delay B021 DEC BC Jump to HandleValve_Delay until the delay is done.
B022 LD A,B
B023 OR C
B024 JR NZ,HandleValve_Delay
B026 DEC D Jump to HandleValve_CycleColours until all of the colours have been cycled.
B027 JR NZ,HandleValve_CycleColours
B029 LD HL,($5B34) Fetch ChamberAddress.
B02C LD A,(HL) Fetch the colour of the room.
B02D OR %00000111 Set bits 0 to 2.
B02F LD (HL),A Store the colour of the chamber.
B030 AND %00000111 Keep bits 0 to 2.
B032 LD ($5B0D),A Set RoomColour.
B035 CALL UpdatePanel Call UpdatePanel.
B038 LD HL,$5B40 Point HL at ChambersActivated.
B03B LD A,(HL) Add one to ChambersActivated.
B03C ADD A,$01
B03E DAA
B03F LD (HL),A
B040 CP $24 Jump to HandleValve_Finish unless it is 24.
B042 JR NZ,HandleValve_Finish
B044 LD A,$01 Write 01 to GameCompletedFlag.
B046 LD ($5B23),A
B049 JP GameOver Jump to GameOver.
HandleValve_Finish B04C CALL PrintActivatedChambers Call PrintActivatedChambers.
B04F LD BC,$1820 Set the pixel position to 20, 18.
B052 JP CopyAreaToScreen Jump to CopyAreaToScreen.
Prev: AF3F Up: Map Next: B055