SCN moXTrapEngine

; Trap for Doors and Containers.


; Switches

Short ControlXnoPROBE  ; On if No Probes in Inventory.
Short ControlXyesProbe ; On if at least one probe in Inventory
short XDisarmSwitch    ; On if Trap has been Disarmed or Triggered
short XDetected        ; On if Trap Has been Detected.


; Permanent Memory Stores

ref  XProbeID


; Dynamic Memory Stores


ref  XSpellID ; Stores SpellID of Trap Spell.
short XSpellIndeX ; Stores a random Number involved in determining Spell to cast.
Short Button  ; Stores Button Pressed in Menu.
Float Max	  ; Stores a 'semi-randomly generated' and 'semi Skill Dependent' value.
float Xstore  ; Stores a Semi-Random Number involved in Trap Disarming.
float Xstore2 ; Stores a Semi-Random Number involved in Trap Disarming.

Begin OnActivate

;------------------------  Trap Previously Disarmed or Triggered so open ---------------------- 	
	
	If XDisarmSwitch == 1
					
		Activate
		Return
	ENDIF
	
;-----------------------------------------------------------------------------------------------	

;-------------------------  Trap Effects -------------------------	

	
	Set XProbeID to "Lockpick"
	
	Set XSpellIndex to Rand 0 7
	
		If XSpellIndex == 1
			Set XSpellID to "TestFireball"
		ElseIf XSpellIndex == 2
			Set XSpellID to "TestShock"
		ElseIf XSpellIndex == 3
			Set XSpellID to "TestFrost"
		ElseIf XSpellIndex == 4
			Set XSpellID to "StandardDisintegrateWeapon3Journeyman"
		ElseIf XSpellIndex == 5
			Set XSpellID to "StandardSilence5Master"
		ElseIf XSpellIndex == 6
			Set XSpellID to "StandardParalyze3Journeyman"
		ENDIF
	
;---------------------------------------------------------------------------------	
	
	
; ---------------------------   Trap Detection -----------------------------------	
; PC's Security Skill and some random chance determines chance of Success
; At 100 Security there is a virtual 100% Success rate in detecting the trap
	
	
	If XDisarmSwitch == 0 && IsActionRef Player && XDetected ==0
		Set Max to ((100 - Player.GetAV Security)/10)

		If (rand 0 Max) <= 1
			Set XDetected to 1

		Else	
			Cast XSpellID Player
			Message "A Trap has been Triggered!"
			Set XDisarmSwitch to 1
			Set ControlXnoProbe to 0
			Set ControlXyesProbe to 0
			Return	

		ENDIF		
	ENDIF
	
;-----------------------------------------------------------------------------
	
	
;-------------------------------------- DisArming with Probes-----------------	
		
	

	If XDisarmSwitch == 0 && IsActionRef Player && Player.GetItemCount XProbeID >= 1  

; PC has at least one Probe, so Untrap and Open.

		MessageBox "You notice a Trap. You can attempt to Disarm it with your Probe.", "Use Probe", "Open without using Probe", "Do Nothing"
		Set ControlXyesProbe to 1
		SET ControlXnoProbe  to 0	



	ElseIf XDisarmSwitch == 0 && IsActionRef Player && Player.GetItemCount XProbeID < 1 

		; PC does not have a Probe so get confirmation for triggering trap.  

		MessageBox "You Notice a Trap, but you do not have a Probe to disarm it. Would you like to Trigger the Trap?", "Yes", "No"
		Set ControlXyesProbe to 0
		Set ControlXnoProbe to 1


	ENDIF


END

;-----------------------------------------------------------------


Begin GameMode

	If ( ControlXyesProbe == 0 ) && (ControlXnoProbe ==0)
		Return 								;  Nothing to do, so save on Processing.
	ENDIF



	If ( ControlXyesProbe == 1 ) || (ControlXnoProbe == 1)
		Set button to GetButtonPressed 				; A MenuBox has Opened so watch out for buttons pressed.

		If ( button == -1 )
			Return									; No Button Pressed this Frame, so Skip and Save on Processing



		ElseIf (( button == 0 ) && ControlXnoProbe == 1 && ControlXyesProbe == 0) || (( button == 1 ) && ControlXYesProbe == 1 && ControlXnoProbe == 0)

; PC has chosen to Trigger Trap so punish him\her and remove the Trap. Alternatively he didn't detect the trap so it got triggered.
			Cast XSpellID Player
			Message "Trap Triggered!"
			Set XDisarmSwitch to 1
			Set ControlXnoProbe to 0
			Set ControlXyesProbe to 0



		ElseIf (( button == 2 ) && ControlXnoProbe == 1 && ControlXyesProbe == 0) || (( button == 3 ) && ControlXYesProbe == 1 && ControlXnoProbe == 0)		

; PC has chosen to leave the Trapped Door alone, so do nothing.


			Set ControlXnoProbe to 0
			Set ControlXyesProbe to 0
			Return


		ElseIf (( button == 0 ) && ControlXYesProbe == 1 && ControlXnoProbe == 0)				

; PC has chosen to use the Probe to disarm the Trap, so do that.


		Set Max to ((100 - Player.GetAV Security)/10)

		Set XStore to (rand 0 Max) 

			If Xstore <= 2

			MessageBox "You have successfully Disarmed the Trap.", "Ok"
			Message "Trap successfully Disarmed!"
			Player.RemoveItem XProbeID 1
			Set ControlXnoProbe to 0
			Set ControlXyesProbe to 0
			Set XDisarmSwitch to 1
			Return	

			Else

			Set XStore2 to rand 0 4

			If Xstore2 <= 2

			MessageBox "You have failed to disarm this trap, though luckily you haven't set it off.", "Ok"
			Player.RemoveItem XProbeID 1
			Set ControlXnoProbe to 0
			Set ControlXyesProbe to 0
			Return		

			ElseIf Xstore2 > 2
						
			MessageBox "You have failed to disarm this trap, and have set it off while tampering with it.", "Ok"

			Cast XSpellID Player
			Message "Trap Triggered!"
			Set XDisarmSwitch to 1
			Set ControlXnoProbe to 0
			Set ControlXyesProbe to 0
			Player.RemoveItem XProbeID 1	
			Return		

			EndIF
				ENDIF



		ENDIF	
	ENDIF
END	