Scriptname wizTreeAliasScript extends ReferenceAlias  

Actor Property PlayerREF Auto
wizDTCompatibility Property Compatibility Auto
Keyword Property WeapTypeBattleAxe Auto
Keyword Property WeapTypeWarAxe Auto
MiscObject Property Firewood01 Auto
Static Property TreePineForestCutStump02 Auto
Container Property wizWoodPileHugeContainer Auto
GlobalVariable Property wizDeadwoodCollected Auto
GlobalVariable Property wizDeadwoodFloor Auto
Globalvariable property GameHour auto
Globalvariable property GameDay auto
GlobalVariable Property wizTempTree Auto 
GlobalVariable Property wizCurrentTree Auto 
GlobalVariable Property wizCurrentTreeHits Auto 
;GlobalVariable Property wizOptionSwingsPerWood Auto 
;GlobalVariable Property wizOptionSwingsToFellTree Auto 
GlobalVariable Property wizOptionSpiderFallsFromTree Auto 
GlobalVariable Property wizOptionTimeToFellTree Auto 
GlobalVariable Property wizOptionPlayCutsceneSounds Auto 
GlobalVariable Property wizDynamicThingsEnabled Auto
GlobalVariable Property wizOptionHarvestWoodTreesEnabled Auto
ImageSpaceModifier Property wizFadeDown auto
ImageSpaceModifier Property wizFadeUp auto
ImageSpaceModifier Property wizBlack auto
Sound property NPCHumanWoodChop auto
Sound property wizTreeFall auto
Sound property wizTreeCreak Auto
Sound Property wizTreeBranchBreak Auto
ActorBase property EncFrostbiteSpider auto
FormList Property woodChoppingAxes Auto
FormList Property wizTreeRemovalMisc Auto
Weapon Property Axe01 Auto
Activator Property wizTreeMarker Auto

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack,  bool abSneakAttack, bool abBashAttack, bool abHitBlocked)
;    Debug.MessageBox("Aggressor:" + akAggressor + "Weapon:" + akSource + " HasForm:" + woodChoppingAxes.HasForm(akSource as form))

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

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

            ;Need to see how many times we have his this tree
            int CurrentTree = wizcurrenttree.getvalue() as int
			ObjectReference ThisTreeREF = self.getref()
			if thisTreeREF == NONE
				Return ;Reference or tree already gone
			endIF

            ;This is the only way for it to work on things other than skyrim.esm!
			int thisTree = ThisTreeREF.GetFormID() as int
			wizTempTree.setValueInt(thisTree)
			int tempTree = wizTempTree.GetValueInt()
			
            int CurrentHits = wizCurrentTreeHits.getvalueint() 

			;debug.notification("This Tree: " + tempTree + " Current Tree:" + CurrentTree)
			
            if CurrentTree == tempTree 
                currenthits=currenthits + 1
                wizCurrentTreeHits.setvalueint(currenthits)
            else
                wizCurrentTreeHits.setvalueint(1)
                wizCurrentTree.setvalueint(thisTree)
                currenthits = 1
            endif

            ;debug.notification("Hits:" + currenthits)            

            ;if(PlayerREF.IsSneaking() == 1)
            if currenthits == 15 ;wizOptionSwingsToFellTree.getvalue() as int

                ;Fade out
                wizFadeDown.Apply()
                utility.Wait(1)
                wizFadeDown.PopTo(wizBlack)
 		   
				if Compatibility.isFrostfall
					Compatibility.degradeAxe()
				endIf
                
				Game.DisablePlayerControls()

				;Place a stump at tree location
				ObjectReference TreeStump = ThisTreeREF.PlaceAtMe(TreePineForestCutStump02)
				TreeStump.MoveTo(TreeStump , 0, 0, -15)
				TreeStump.SetAngle(0.0, 0.0, PlayerREF.GetAngleZ() + 180)

				;Place a huge wood pile at player's feet
				ObjectReference WoodPile = PlayerREF.PlaceAtMe(wizWoodPileHugeContainer)

				;Move wood back from stump 20 units and 10 units lower
				WoodPile.MoveTo(WoodPile, -20.0 * Math.Sin(PlayerREF.GetAngleZ()), -20.0 * Math.Cos(PlayerREF.GetAngleZ()), -10)
				WoodPile.SetAngle(0.0, 0.0, PlayerREF.GetAngleZ() + 180)

				;Add fire wood to wood pile
				WoodPile.Additem(Firewood01,utility.RandomInt(15, 25))
	
				if Compatibility.isFrostfall
					woodPile.additem(compatibility._DE_DeadWood, utility.RandomInt(4,8))
					;debug.notification("Added deadwood")
				EndIf

				;Move player back 120 units from current position
				PlayerREF.MoveTo(PlayerREF, -120.0 * Math.Sin(PlayerREF.GetAngleZ()), -120.0 * Math.Cos(PlayerREF.GetAngleZ()), 0)

				;Disable tree
				ThisTreeREF.placeatme(wizTreeMarker)
				;ThisTreeREF.disable()

				;Advance time by 1 hour
				if GameHour.GetValue() < (24.0 - (wizOptionTimeToFellTree.getValue() as float/60 as float))
					;if GameHour.GetValue() < (24.0 - (60 as float/60 as float))
					GameHour.SetValue(GameHour.GetValue() as float + (wizOptionTimeToFellTree.getValue() as float/60 as float))
					;GameHour.SetValue(GameHour.GetValue() as float + (60 as float/60 as float))
				else
					GameHour.SetValue(GameHour.getvalue() as float + (wizOptionTimeToFellTree.getValue() as float/60 as float) - 24 as float)
					;GameHour.SetValue(GameHour.getvalue() as float + (60 as float/60 as float) - 24 as float)
					;Past midnight so advance GameDay as well
					GameDay.setValue(GameDay.GetValue() as int +1)
				endif

				;Play some sounds?
				if wizOptionPlayCutsceneSounds.getvalue() as int == 1
				;if 1 == 1
					wizTreeFall.Play(PlayerREF)
					utility.wait(6)
					NPCHumanWoodChop.Play(PlayerREF)
					utility.wait(1)
					NPCHumanWoodChop.Play(PlayerREF)
					utility.wait(1)
					NPCHumanWoodChop.Play(PlayerREF)
					utility.wait(1)
				endIf

				;Remove beehives and birdnests
				;bool remove = TRUE
				;while remove == TRUE
				;RemoveThis = NONE
				objectReference RemoveThis = Game.FindClosestReferenceOfAnyTypeInListFromRef(wizTreeRemovalMisc, ThisTreeREF, 2000.0)			
				;debug.messagebox(RemoveThis)
				if RemoveThis
					RemoveThis.disable()
				;else
					;remove = FALSE
				endIf
				;endWhile

				wizCurrentTreeHits.setvalueint(0)
				
				;Fade back up
				Game.EnablePlayerControls()
				wizBlack.PopTo(wizFadeUp)

            elseIf  currenthits >= 10 ;wizOptionSwingsToFellTree.getvalue() as int
                ;Do nothing
                ;Sometimes the player gets an extra hit in before the tree disappears. This will catch it and cause no ill effects.
				wizTreeCreak.play(PlayerREF)
				if currentHits > 18
					currenthits = 14
				endIf

            else            
                if(PlayerREF.IsSneaking() == 1)
                    ;No firewood if sneaking
                        if currentHits == 3
                            debug.notification("That swing really connected!")
                        elseIf CurrentHits == 6
                            debug.notification("I'm getting good at this")
                        elseIf CurrentHits == 9
                            debug.notification("This tree seems to be weakening!")
                        endIf
                else
                    ;Spider falls on head
                    If utility.RandomInt(1,100) <= wizOptionSpiderFallsFromTree.getValue() as int
                        objectReference Spider = PlayerREF.PlaceAtMe(EncFrostbiteSpider)
                        Spider.MoveTo(PlayerREF, 0, 0, 400)
                    endIf

                    ;A piece of wood every three hits
                    ;if ((currentHits as float / wizOptionSwingsPerWood.getvalue() as float)- math.floor(currentHits as float / wizOptionSwingsPerWood.getvalue() as float) == 0 as float) 
                    if ((currentHits as float / 3 as float)- math.floor(currentHits as float / 3 as float) == 0 as float) && currentHits <10

						wizTreeBranchBreak.play(playerRef)
						if Compatibility.isFrostfall
							akAggressor.additem(compatibility._DE_DeadWood, 1)
							wizDeadwoodCollected.setValue(wizDeadwoodCollected.getValueInt() + 1)
				
							;15 Collected = Degrade Axe
							if Math.Floor(wizDeadwoodCollected.getValueInt()/15) > wizDeadwoodFloor.getValueInt()
								wizDeadwoodFloor.SetValue(Math.Floor(wizDeadwoodCollected.getValueInt()/15))
								;Are we degrading the axe?
								GlobalVariable _DE_Setting_Tent = Game.GetFormFromFile(0x0200E5B9, "Chesko_Frostfall.esp") as GlobalVariable
								if _DE_Setting_Tent.GetvalueInt() == 2
									Compatibility.DegradeAxe()
								endif
							else
								;Nothing todo yet
							endIf

						else
							;One firewood regardless of size
							akAggressor.additem(Firewood01,1)
						endIf

                        
						if currentHits == 3
							debug.notification("I got a useful piece of wood!")
						elseIf CurrentHits == 6
							debug.notification("More wood! Maybe a few more hits.")
						elseIf CurrentHits == 9
							debug.notification("This tree seems to be weakening!")
						endIf
                    
					endIf
                endIf
            endif
        endif
    endif
endIf
EndEvent


