#include "zcommon.acs"
#library "GUN_ACS"

str HealthGFX[11] =
{
	"H_DEAD_X",
	"H_HEAL_1",
	"H_HEAL_2",
	"H_HEAL_3",
	"H_HEAL_4",
	"H_HEAL_5",
	"H_HEAL_6",
	"H_HEAL_7",
	"H_HEAL_8",
	"H_HEAL_9",
	"H_HEAL_0",
};

str ArmorGFX[21] =
{
	"H_BLANK",
	"S_ARMR_1",
	"S_ARMR_2",
	"S_ARMR_3",
	"S_ARMR_4",
	"S_ARMR_5",
	"S_ARMR_6",
	"S_ARMR_7",
	"S_ARMR_8",
	"S_ARMR_9",
	"S_ARMR10",
	"S_ARMR11",
	"S_ARMR12",
	"S_ARMR13",
	"S_ARMR14",
	"S_ARMR15",
	"S_ARMR16",
	"S_ARMR17",
	"S_ARMR18",
	"S_ARMR19",
	"S_ARMR20",
};

str LanternGFX[21] =
{
	"H_BLANK",
	"S_LANT_1",
	"S_LANT_2",
	"S_LANT_3",
	"S_LANT_4",
	"S_LANT_5",
	"S_LANT_6",
	"S_LANT_7",
	"S_LANT_8",
	"S_LANT_9",
	"S_LANT10",
	"S_LANT11",
	"S_LANT12",
	"S_LANT13",
	"S_LANT14",
	"S_LANT15",
	"S_LANT16",
	"S_LANT17",
	"S_LANT18",
	"S_LANT19",
	"S_LANT20",
};

int hudup;

Script "gun_main" ENTER
{
	//Check screen size
	if (getcvar("screenblocks")<=10)
    {
        hudup = 1;
    }
    else 
    {
        hudup = 0;
    }
	
	//Check health function
	health_gauge(checkinventory("Health"));
	//Check armor function
	armor_gauge(checkinventory("Armor"));
	//Check lamp oil function
	lantern_gauge(checkinventory("LampOil"));
	
    delay(1);
    restart;
}

function void health_gauge(int health)
{
	sethudsize(640,480,1);
	setfont(healthGFX[(health+9)/10]);
	if (hudup == 1)
	{
	    hudmessage(s:"A"; 
        hudmsg_notwithfullmap, 900, cr_untranslated, 558.0, 417.0, 0);
	}
	else
	{
		hudmessage(s:""; 
        hudmsg_notwithfullmap, 900, cr_untranslated, 558.0, 417.0, 0);
	}
}

function void armor_gauge(int armor)
{
	sethudsize(640,480,1);
	setfont(ArmorGFX[(armor+9)/10]);
	
	if (hudup == 1)
	{
	    hudmessage(s:"A"; 
        hudmsg_notwithfullmap, 901, cr_untranslated, 470.0, 417.0, 0);
	}
	else
	{
		hudmessage(s:""; 
        hudmsg_notwithfullmap, 901, cr_untranslated, 470.0, 417.0, 0);
	}
}

Script "gun_heal" (void)
{
	if (CheckInventory("Health") < 100)
	{
		ambientsound("pickup_doctorbag",255);
		Until(Checkinventory("Health")==100)
		{
			takeinventory("DoctorBag",1);
			giveinventory("Health",10);
			If(CheckInventory("DoctorBag")==0)
			{terminate;}
			delay(1);
		}
		terminate;
	}
	else
	{
		terminate;
	}
}

int pjumping=FALSE;
int prunning=FALSE;
int gatlinguse=1;

Script "gun_stamina" ENTER
{
	While(TRUE)
	{
		While(pjumping | prunning == True)
		{
			Delay(1);
		}
		If(pjumping | prunning == FALSE && CheckInventory("Stamina")!=100)
		{
			Until(CheckInventory("Stamina")==100)
			{
				If(pjumping | prunning == True){restart;}
				GiveInventory("Stamina",1);
				Delay(1);
			}
		}
		Delay(1);
	}
}

Script "gun_tired" ENTER
{
	While(TRUE)
	{
		If(CheckInventory("Stamina")<=25)
		{
			Until(CheckInventory("Stamina")>=25)
			{
				If(CheckInventory("Health") <= 0)
				{terminate;}
				AmbientSound("player/breathing", 255);
				Delay(50);
			}
		}
		Delay(1);
	}
}


Script "gun_sprint" ENTER
{
	int runinput;
	ACS_NamedExecute("gun_speed",0,100/gatlinguse);
	While(TRUE)
	{
		While(CheckInventory("Stamina")!=0)
		{
			runinput = GetPlayerInput(-1, INPUT_BUTTONS);
			If(runinput & BT_SPEED)
			{
				If(GetActorVelX(0)+GetActorVelY(0)!=0)
				{
					ACS_NamedTerminate("gun_runcheck",0);
					prunning = TRUE;
					ACS_NamedExecute("gun_speed",0,300/gatlinguse);
					TakeInventory("Stamina",1);
					Delay(1);
					ACS_NamedExecute("gun_runcheck",0);
					If(CheckInventory("Stamina")==0){restart;}
					Delay(1);
				}
				Delay(1);
			}
			ACS_NamedExecute("gun_speed",0,100/gatlinguse);
			Delay(1);
		}
	Delay(1);
	ACS_NamedExecute("gun_speed",0,100/gatlinguse);
	}
}

Script "gun_speed" (int movespeed)
{
	SetActorProperty(0,APROP_SPEED,movespeed*65535/100);
}

Script "gun_runcheck" (Void)
{
	Delay(100);
	prunning = FALSE;
}

Script "gun_gatlingspeed" (int gatspeed)
{
	gatlinguse = gatspeed;
}


Script "gun_jump" ENTER
{
	int jumpinput;
	int plvelz0;
	int plvelz1;
	int jstamina;
	While(TRUE)
	{
		jumpinput = GetPlayerInput(-1, INPUT_BUTTONS);
		plvelz0 = GetActorVelZ(0);
		Delay(1);
		plvelz1 = GetActorVelz(0);
		If(plvelz0==plvelz1)
		{
			If(jumpinput & BT_JUMP)
			{
				ACS_NamedTerminate("gun_jumpcheck",0);
				pjumping = TRUE;
				jstamina = checkinventory("Stamina")/5+10;
				AmbientSound("player/jumping",255);
				ThrustThingZ(0, jstamina, 0, 0);
				TakeInventory("Stamina", 15);
//				pjumping=TRUE;
				Delay(10);
				ACS_NamedExecute("gun_jumpcheck",0);
			}
		}
	}
}

Script "gun_jumpcheck" (Void)
{
	Delay(100);
	pjumping = FALSE;
}

//int xhairon;
int crossx = 320.0;
int crossy = 240.0;
global int 1: xhairon;

Script "gun_crosstoggle" (Void)
{
	if(xhairon == 0)
	{
		xhairon = 1;
		ACS_NamedExecute("gun_crosshair",0);
		terminate;
	}
	if(xhairon == 1)
	{
		xhairon = 0;
		hudmessage(s:""; 
		hudmsg_notwithfullmap, 903, cr_untranslated, crossx, crossy, 0);
		terminate;
	}
}

Script "gun_crosshair" ENTER
{
	while(xhairon == 1)
	{
		sethudsize(640,480,0);
		setfont("X_CROSS");
		if (checkweapon("Dukes")||checkweapon("Rifle"))
		{
			if (checkweapon("Dukes"))
			{
				hudmessage(s:""; 
				hudmsg_notwithfullmap, 903, cr_untranslated, crossx, crossy, 0);
			}
			else if (checkweapon("Rifle"))
			{
				if (checkinventory("Zoomed") > 0)
				{
					hudmessage(s:""; 
					hudmsg_notwithfullmap, 903, cr_untranslated, crossx, crossy, 0);
				}
				else
				{
					hudmessage(s:"A"; 
					hudmsg_notwithfullmap, 903, cr_untranslated, crossx, crossy, 0);
				}
			}
		}
		else
		{
			hudmessage(s:"A"; 
			hudmsg_notwithfullmap, 903, cr_untranslated, crossx, crossy, 0);
		}
	delay(1);
	}
}

Script "gun_death" DEATH
{
	ambientsound("death_ring",255);
}

global int 2: lanternon;

Script "gun_lanterntoggle" (Void)
{
	If(lanternon == 0)
	{
		lanternon = 1;
		If(CheckInventory("LampOil")>0)
		{ 
			AmbientSound("lantern_light",255);
		}
		Terminate;
	}
	If(lanternon == 1)
	{
		lanternon = 0;
		Terminate;
	}
	lanternon = 1;
	Terminate;
}

Script "gun_lantern" ENTER
{
	While(TRUE)
	{
		If(lanternon == 1)
		{
			If(CheckInventory("LampOil")!=0)
			{
				Spawn("LanternLight", GetActorX(0),GetActorY(0),GetActorZ(0)+40*65536);
				TakeInventory("LampOil", 1);
				
			}
			If(CheckInventory("LampOil")==0)
			{ lanternon = 0; }
			Delay(1);
		}
		Delay(1);
	}
}

function void lantern_gauge(int light)
{
	sethudsize(640,480,1);
	setfont(LanternGFX[(light+49)/50]);
	if (hudup == 1)
	{
		if (lanternon == 1)
		{
			hudmessage(s:"A"; 
			hudmsg_notwithfullmap, 902, cr_untranslated, 400.0, 417.0, 0);
		}
		else
		{
			hudmessage(s:""; 
			hudmsg_notwithfullmap, 902, cr_untranslated, 400.0, 417.0, 0);
		}
	}
	else
	{
		hudmessage(s:""; 
        hudmsg_plain, 902, cr_untranslated, 400.0, 417.0, 0);
	}
}

int killlimit;
int killmax;
int killdisplay;
int bossawake;

Script "gun_boss_reset" ENTER
{
	bossawake = 0;
	killlimit = 0;
	killmax = 0;
	killdisplay = 0;
	terminate;
}

Script "gun_boss" (void)
{
	delay(180);
	killlimit = GetLevelInfo(LEVELINFO_KILLED_MONSTERS);
	if(GameSkill() == SKILL_EASY)
	{
		killmax = (GetLevelInfo(LEVELINFO_KILLED_MONSTERS)+30);
	}
	else if (GameSkill() == SKILL_NORMAL)
	{
		killmax = (GetLevelInfo(LEVELINFO_KILLED_MONSTERS)+40);
	}
	else
	{
		killmax = (GetLevelInfo(LEVELINFO_KILLED_MONSTERS)+50);
	}
	bossawake = 1;
}

Script "gun_boss_prep" (void)
{
	Thing_ChangeTID(0,1337);
}

Script "gun_counter" ENTER
{
	if(bossawake > 0)
	{
		print(s:"\cgSurvive the final wave!");
		SetFont("SMALLFONT");
		SetHudSize(320,200,0);
		while(killlimit != killmax)
		{
			killlimit = GetLevelInfo(LEVELINFO_KILLED_MONSTERS);
			killdisplay = (killmax - killlimit);
			if(killlimit != (killmax-1))
			{
				hudmessage(s:"\cg", d:killdisplay, s:" \ccmonsters left!";
				hudmsg_plain, 905, cr_untranslated, 160.0, 20.0, 0);
			}
			else
			{
				hudmessage(s:"\cg", d:killdisplay, s:" \ccmonster left!";
				hudmsg_plain, 905, cr_untranslated, 160.0, 20.0, 0);
			}
			delay(1);
		}
		hudmessage(s:"";
		hudmsg_plain, 905, cr_untranslated, 160.0, 20.0, 0);
		Thing_Destroy(0,1);
		Thing_Destroy(1337,0);
		terminate;
	}
	else
	{
	}
	delay(1);
	restart;
}

global int 3: hasscope;

Script "gun_getscope" (void)
{
	hasscope = 1;
}

Script "gun_backpack" (void)
{
	if(hasscope == 1)
	{
		SpawnForced("PowerupSpawner", GetActorX(0), GetActorY(0), GetActorZ(0),0,0);
	}
	else
	{
		SpawnForced("SniperScope", GetActorX(0), GetActorY(0), GetActorZ(0),0,0);
	}
}