Scriptname wizRockAliasScript extends ReferenceAlias  

Actor Property PlayerREF Auto
GlobalVariable Property wizOptionDestroyRocksEnabled Auto
GlobalVariable Property wizDynamicThingsEnabled Auto
FormList Property wizPickAxes Auto
GlobalVariable Property wizCurrentRock Auto 
GlobalVariable Property wizCurrentRockHits Auto 
GlobalVariable Property wizLastBrokenRock Auto 
GlobalVariable Property wizOptionRocksStone Auto
GlobalVariable Property wizOptionMaxRockHits Auto
GlobalVariable Property wizTempRock Auto
Sound Property wizRockPickaxe Auto
wizDTCompatibility Property Compatibility Auto

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack,  bool abSneakAttack, bool abBashAttack, bool abHitBlocked)
    ;Debug.MessageBox("Aggressor:" + akAggressor + "Weapon:" + akSource + " HasForm:" + wizPickAxes.HasForm(akSource as form))
	;Debug.Notification("It's a hit")

	if wizDynamicThingsEnabled.GetValueInt() == 1 && wizOptionDestroyRocksEnabled.GetValueInt() == 1 && playerRef.IsWeaponDrawn() == TRUE

		if (akAggressor == PlayerREF)
			;if (akSource.HasKeyword(WeapTypeBattleAxe) || akSource.HasKeyword(WeapTypeWarAxe))
			if wizPickAxes.HasForm(akSource as form)

				;Need to see how many times we have his this tree
				int CurrentRock = wizCurrentRock.getvalue() as int
				ObjectReference ThisRockREF = self.getref()
				if ThisRockREF == NONE
					Return ;Reference or rock already gone
				endIF

				int thisRock = ThisRockREF.GetFormID()
				wizTempRock.setValueInt(thisRock)
				int tempRock = wizTempRock.GetValueInt()
			
				int CurrentHits = wizCurrentRockHits.getvalue() as int
				
				if CurrentRock == tempRock 
					currenthits=currenthits + 1
					wizCurrentRockHits.setvalue(currenthits)
				else
					wizCurrentRockHits.setvalue(1)
					wizCurrentRock.setvalue(thisRock)
					currenthits = 1
				endif

				wizRockPickaxe.play(playerRef)
				
				int StoneSize = ThisRockREF.getWidth() as int  * ThisRockREF.getLength() as int
				;How many hits to break?
				int HitsToBreak = (StoneSize/3000) + 1
				If HitsToBreak < 5
					HitsToBreak = 5
				EndIf
				if HitsToBreak > wizOptionMaxRockHits.GetValueInt()
					HitsToBreak = wizOptionMaxRockHits.GetValueInt()
				EndIf
				
				if CurrentHits >= HitsToBreak ;5
					ThisRockREF.disable()
					if ThisRockREF.IsDisabled()
						wizLastBrokenRock.setvalue(thisRock)
					
						if Compatibility.isHFLoaded && wizOptionRocksStone.getvalue() == 1
							MiscObject QuarriedStone = Game.GetFormFromFile(0x0100306c, "HearthFires.esm") as MiscObject ;Quarried Stone
							
							;debug.notification("Stone Size:" + StoneSize)
							
							int NumberStone
							
							if StoneSize <= 12000
								NumberStone = 3
							ElseIf StoneSize < 30000
								NumberStone = 4
							ElseIf StoneSize < 100000
								NumberStone = 5
							ElseIf StoneSize < 200000
								NumberStone = 6
							Else
								NumberStone = 7
							EndIf
							
							objectreference Stone
							While NumberStone > 0
								Stone = ThisRockREF.placeatme(QuarriedStone,1)
								;Stone.moveto(PlayerRef, 120.0 * Math.Sin(PlayerRef.GetAngleZ()), 120.0 * Math.Cos(PlayerRef.GetAngleZ()), PlayerRef.GetPositionZ() - Stone.getPositionZ() + 50)
								Stone.moveto(PlayerRef, 120.0 * Math.Sin(PlayerRef.GetAngleZ()), 120.0 * Math.Cos(PlayerRef.GetAngleZ()), 50)
								NumberStone = NumberStone - 1
							endWhile
							
							wizCurrentRockHits.setvalue(0)
							
						Else
							;Currently the rock just disappears without Hearthfire!
						endIf					
					Else
						debug.trace("Dynamic Things: The above parent error was the result of trying to break a rock that couldn't be disabled. The mod is running normally")
						debug.Notification("Despite my best efforts, this rock will not break.")
					EndIf
					

				
				endIf

			endIf 
		endif
	endIf
EndEvent