// Jump jets! Just like I'm not doing for a certain mech mod that I thought I might get around to! Haha! Kill me now.
// By Zhs2.

#library "RussianOverkillIsStealingAllTheGoodIdeasButThatIsForTheBestBecauseThisScriptAuthorIsALazyFuckingSod"
#include "zcommon.acs"

script "JetTracker" ENTER // Count for a while, then thrust! Just so they don't kick in immediately, that is. I'll save that for a mech.
{
	int jetcounter; // had less use than I anticipated
	int todefuel; // or not defuel, that is the question (refuel instead if not!)
	int refuelcounter, defuelcounter; // checks the delays for fueling
	int playmovex = 0, playmovey = 0; // Player movement
	int thrustx, thrusty; // The total thrust in a given direction
	int angle = GetActorAngle(0); // less typing
	int gravityamount; // Depends on the class
	int playerx, playery, playerz; // for spawning effects
	
	playerx = GetActorX(0);
	playery = GetActorY(0);
	playerz = GetActorZ(0); // if this isn't at the player's feet then I hate everything
	
	if(CheckInventory("PowerForkGodman")) //So you won't be doing jetboots stuff while being under forkgod effect.
	{
	delay(1);
	restart;
	}
	else
	if(CheckInventory("YuriTrigger")) { gravityamount = 0.7; } // robutt
	else if(CheckInventory("AloszaTrigger")) { gravityamount = 0.55; } // commmando
	else if(CheckInventory("MartyTrigger")) { gravityamount = 0.4; } // trees
	else { gravityamount = 0.6; } // a safe standard
	
	if(GetPlayerInput(-1, MODINPUT_BUTTONS) & BT_JUMP) // We are currently holding jump.
	{
		if(GetPlayerInput(-1, MODINPUT_OLDBUTTONS) & BT_JUMP) // The player keeps holding jump
		{
			jetcounter++;
			if(jetcounter > 10 && CheckInventory("JetThruster")) // Fly!
			{
				if (!todefuel) { SetActorVelocity(0, 0, 0, 2.2, 1, 0); } // Initial thrust
				todefuel = 1; // Big Amerikan Spender hier
				SetActorProperty(0, APROP_Gravity, gravityamount); // For ease of movement
				if (GetPlayerInput(-1, INPUT_FORWARDMOVE) >= 6000) { playmovex = 1; } // No modularity in this solution ;(
				else if (GetPlayerInput(-1, INPUT_FORWARDMOVE) <= -6000) { playmovex = -1; } // RIP in piece my dignity
				if (GetPlayerInput(-1, INPUT_SIDEMOVE) >= 6000) { playmovey = 1; } // I hate life
				else if (GetPlayerInput(-1, INPUT_SIDEMOVE) <= -6000) { playmovey = -1; } // Just trust me on this one
				SetActorVelocity(0, 0, 0, (jetcounter - 10) * 0.2, 1, 0); // I thought I'd use this for horizontal velocities too until a kind soul mentioned...
				ThrustThing((angle>>8),playmovex,0,0); // So yeah, fuck SAV
				ThrustThing((angle>>8)+192,playmovey,0,0); // But I still wish for a compromise
				SpawnForced("JumpJetActive", playerx, playery, playerz, 0, 0); // Effects spawn here
				PlaySound(0, "Jumpjet/Thrust", 7, 0.65, 0, 1.0); // Play the sound
			}
		}
		else // Release for one frame to no longer be effected
		{
			if(jetcounter > 10) { jetcounter = 10; }
			// Hold jump again to continue flying if the counter's already been built
			// If you touch ground again in that time, i.e. fuck around with the jump holding too much then nope
			// Theoretically this lets you build off falls
		}
	}
	else if(jetcounter>=10 && CheckInventory("JetThruster")) // An improvement for movement while still mid-air
	{
		if (GetPlayerInput(-1, INPUT_FORWARDMOVE) >= 6000) { playmovex = 1; } // See above for how ugly this is
		else if (GetPlayerInput(-1, INPUT_FORWARDMOVE) <= -6000) { playmovex = -1; }
		if (GetPlayerInput(-1, INPUT_SIDEMOVE) >= 6000) { playmovey = 1; }
		else if (GetPlayerInput(-1, INPUT_SIDEMOVE) <= -6000) { playmovey = -1; }
		ThrustThing((angle>>8),playmovex,0,0); // No thrust, but still add some sideways movement
		ThrustThing((angle>>8)+192,playmovey,0,0);
		SpawnForced("JumpJetActive2", playerx, playery, playerz, 0, 0); // Alternative effect
		StopSound(0, 7); // go away bibels
	}
	
	/* Fun old (funnelled) debug stuff
	hudmessage(i:playmovex; HUDMSG_PLAIN, 9001, CR_GREEN, 0.2, 0.2, 0.1);
	hudmessage(i:playmovey; HUDMSG_PLAIN, 9002, CR_GREEN, 0.2, 0.25, 0.1);
	hudmessage(f:angle; HUDMSG_PLAIN, 9005, CR_GREEN, 0.2, 0.3, 0.1);
	*/
	
	if(jetcounter > 13) { jetcounter = 13; } // Clamping
	if(GetActorZ(0)==GetActorFloorZ(0))
	{
		jetcounter = 0; // Touching ground resets the counter and the need to start charging jump jets again
		SetActorProperty(0, APROP_Gravity, 1.0); // le default
		todefuel = 0; // do it faggot
		StopSound(0, 7); // Just in case the player doesn't stop holding jump
	}
	
	// Here we grant the player eternal life or suffering depending on which he currently wants
	if(todefuel)
	{
		refuelcounter = 0; // Reset!
		defuelcounter++;
		if(defuelcounter>=(35)) // two seconds
		{
			TakeInventory("JetThruster", 1); // No free-flying zone!
			defuelcounter = 0;
		}
	}
	else
	{
		defuelcounter = 0; // Can't be having that disgusting thing stealing your jets faster!
		refuelcounter++;
		if(refuelcounter>=(35*2)) // four seconds
		{
			GiveInventory("JetThruster", 1); // Thanks doc!
			refuelcounter = 0;
		}
	}
	
	delay(2); // I wonder about whether I should not use such a fine delay for the sake of ease, hmm, too late
	restart;
}