Scriptname RenVacuum:RenVacuumQuestScript extends Quest
{Quest for vacuum.}

Formlist Property CA_JunkItems Auto

Ammo Property RenVacuumAmmo Auto

Weapon Property JunkJet Auto

ObjectMod Property RenVacuumObjMod01 Auto

MiscObject Property RenVacuumMiscMod Auto

int prevvacammo = 0

ObjectReference[] junkarray

bool vacuumonce = false

bool firing = false

Event OnInit()
	if (Game.GetPlayer().GetItemCount(RenVacuumAmmo) <= 0)
		Game.GetPlayer().AddItem(RenVacuumAmmo, 10)
		Game.GetPlayer().AddItem(RenVacuumMiscMod, 1)
		if (Game.GetPlayer().GetItemCount(JunkJet) <= 0)
			Game.GetPlayer().AddItem(JunkJet)
		endif
	endif
	firing = false
	Self.RegisterForAnimationEvent(Game.GetPlayer(), "weaponCharge")
	Self.StartTimer(1.0, 0)
endEvent

Event OnAnimationEvent(ObjectReference akSource, string asEventName)
	if (Game.GetPlayer().GetEquippedWeapon(0) == JunkJet)
		if (akSource == Game.GetPlayer() && asEventName == "weaponCharge")
			firing = true
		endif
	endif
endEvent

Event OnTimer(int timerid)

	if (Game.GetPlayer().GetItemCount(RenVacuumAmmo) < prevvacammo)
		Game.GetPlayer().AddItem(RenVacuumAmmo, prevvacammo - Game.GetPlayer().GetItemCount(RenVacuumAmmo))
		firing = true
		debug.trace("Vacuum firing")
	endif
	
	if (firing == true)
		debug.trace("Vacuum finding nearby junk")
		junkarray = Game.GetPlayer().FindAllReferencesOfType(CA_JunkItems, 2000.0)
		firing = false
		debug.notification("Junkarray size: " + junkarray.length)		
		
		int itr = 0
		
		while (itr < junkarray.length)
			if (junkarray[itr].Is3DLoaded() == true)
				junkarray[itr].SetMotionType(junkarray[itr].Motion_Dynamic, true)				
			else
				junkarray[itr] = none
			endif
			itr += 1
		endWhile
	endif
	
	int itr = 0
	while (itr < junkarray.length)			
		if (junkarray[itr] != none && Game.GetPlayer().GetHeadingAngle(junkarray[itr]) >= -70 && Game.GetPlayer().GetHeadingAngle(junkarray[itr]) <= 70)		
			
			junkarray[itr].ApplyHavokImpulse(0.0, 0.0, 1.0, 1.0)

			float x = junkarray[itr].GetPositionX() + (Game.GetPlayer().X - junkarray[itr].GetPositionX()) * Math.cos(junkarray[itr].GetHeadingAngle(Game.GetPlayer()))
			float y = junkarray[itr].GetPositionX() + (Game.GetPlayer().Y - junkarray[itr].GetPositionY()) * Math.sin(junkarray[itr].GetHeadingAngle(Game.GetPlayer()))
			
			;junkarray[itr].SetPosition(x, y, junkarray[itr].Z)
			
			junkarray[itr].SplineTranslateToRef(Game.GetPlayer(), 1.0, 1.0)
			
			if (junkarray[itr].GetDistance(Game.GetPlayer()) <= 200)
				if (!Game.GetPlayer().WouldBeStealing(junkarray[itr]))
					junkarray[itr].Activate(Game.GetPlayer())
				else
					;junkarray[itr].StopTranslation()
					junkarray[itr].ApplyHavokImpulse(0.0, 0.0, -1.0, 1.0)				
				endif
				junkarray[itr] = none
			endif
			
		else
		
			if (junkarray[itr] != none && junkarray[itr].Is3DLoaded() == true)
				;junkarray[itr].StopTranslation()
				;junkarray[itr].ApplyHavokImpulse(0.0, 0.0, -1.0, 1.0)
				;junkarray[itr] = none
			endif
			
		endif
		itr += 1
	endWhile
	
	prevvacammo	= Game.GetPlayer().GetItemCount(RenVacuumAmmo)
	
	Self.StartTimer(0.1, 0)
endEvent