// TRADER MENU SYSTEM
#library "TRADMENU"
#import "traderlist.acs"
#include "zcommon.acs"

// Sell box
#define LEFTBOX_MINX 55.0 				// Left X
#define LEFTBOX_MAXX 230.0 			// Right X
#define LEFTBOX_MINY 83.0 				// Top Y
#define LEFTBOX_MAXY 544.0 			// Bottom Y

// Upgrade box
#define UPGRADEBOX_MINX 391.0 				// Left X
#define UPGRADEBOX_MAXX 744.0 			// Right X
#define UPGRADEBOX_MINY 83.0 				// Top Y
#define UPGRADEBOX_MAXY 464.0 			// Bottom Y

#define ALIGN_CENTER 0.4
#define ALIGN_CENTER_Y 0.0
#define ALIGN_LEFT 0.1
#define ALIGN_RIGHT 0.2

//===========================================
// PERK BUTTONS
//===========================================

#define PERK_IDBASE			5
#define PERK_XSTART			571.0				// Perk X
#define PERK_YSTART			52.0				// Perk Y

#define PERK_WIDTH			24.0				// How big a button is
#define PERK_HEIGHT			27.0				// How big a button is

#define TOTAL_PERKS			5

#define PD_DISPLAY			0
#define PD_ITEM				1
#define PD_ICON				2

str perkData[TOTAL_PERKS][3] = {
	{"Juggernaut", "Perk_Juggernaut", "TRADICO1"},
	{"Commando", "Perk_Commando", "TRADICO2"},
	{"Demolitionist","Perk_Demo", "TRADICO3"},
	{"Rifleman","Perk_Rifleman", "TRADICO4"},
	{"Pyromaniac","Perk_Pyro", "TRADICO5"},
};

#define PERKBUTTON_FRAMES	3
str perkButtonImages[PERKBUTTON_FRAMES] = {
	"PERKBUT1",
	"PERKBUT2",
	"PERKBUT3",
};

//===========================================

// Purchase box
#define RIGHTBOX_MINX 570.0
#define RIGHTBOX_MAXX 745.0
#define RIGHTBOX_MINY 83.0
#define RIGHTBOX_MAXY 544.0

// Left button
#define BUTTON1_MINX 246.0
#define BUTTON1_MAXX 332.0
#define BUTTON1_MINY 508.0
#define BUTTON1_MAXY 538.0

// Middle button
#define BUTTON2_MINX 360.0
#define BUTTON2_MAXX 445.0
#define BUTTON2_MINY 508.0
#define BUTTON2_MAXY 538.0

// Right button
#define BUTTON3_MINX 470.0
#define BUTTON3_MAXX 555.0
#define BUTTON3_MINY 508.0
#define BUTTON3_MAXY 538.0

// Left button - Cat2
#define CBUTTON1_MINX 426.0
#define CBUTTON1_MAXX 512.0
#define CBUTTON1_MINY 508.0
#define CBUTTON1_MAXY 538.0

// Middle button - Cat2
#define CBUTTON2_MINX 540.0
#define CBUTTON2_MAXX 625.0
#define CBUTTON2_MINY 508.0
#define CBUTTON2_MAXY 538.0

// Right button - Cat2
#define CBUTTON3_MINX 650.0
#define CBUTTON3_MAXX 736.0
#define CBUTTON3_MINY 508.0
#define CBUTTON3_MAXY 538.0

// Top Category Button
#define CATEGORY1_MINX 750.0
#define CATEGORY1_MAXX 782.0
#define CATEGORY1_MINY 453.0
#define CATEGORY1_MAXY 500.0

// Bottom Category Button
#define CATEGORY2_MINX 750.0
#define CATEGORY2_MAXX 782.0
#define CATEGORY2_MINY 500.0
#define CATEGORY2_MAXY 546.0

function int abs (int x)
{
    if (x < 0)
        return -x;

    return x;
}

// PUKED WHEN USING TRADER //
script 250 (int mode)
{
	switch (mode)
	{
		case 0:
		if (!CheckInventory("MenuActive"))
		{
		SetActorProperty(0,APROP_Invulnerable,1);
		SetPlayerProperty(0,1,4);
		SetPlayerProperty(0,1,1);
		SetPlayerProperty(0,1,7);
		GiveInventory("WepToApply",1);
		ACS_ExecuteAlways(701,0,0,0,0);
		GiveInventory("MenuActive",1);
		GiveInventory("MenuWeapon",1);
		SetWeapon("MenuWeapon");
		}
		break;

		case 1:
		SetActorProperty(0,APROP_Invulnerable,0);
		SetPlayerProperty(0,0,4);
		SetPlayerProperty(0,0,1);
		SetPlayerProperty(0,0,7);
		TakeInventory("MenuActive",1);
		TakeInventory("TraderCategory",2);
		TakeInventory("WepToApply",99);
		TakeInventory("MenuWeapon",1);
		break;
	}
}

#define TOTALWEPS 13

#define PURCHASE_DISPLAYNAME		0
#define PURCHASE_ACTOR				1
#define PURCHASE_SHOWPRICE			2
#define PURCHASE_BUYPIC				3
#define PURCHASE_DESCRIPTION		4
#define PURCHASE_DISCOUNT			7

#define LEVEL_DISCOUNT_AMOUNT		100

str ShopWeapons[TOTALWEPS][8] = {

//		{
//		Display Name
//		Actor Name
//		Price (Display only)
//		Buypic
//		Description
//		Weapon type
//		Silenced - Yes/no (Silencer actor name, "None" if nothing)
//		The perk the weapon requires (discounts)
//		}

///////////////////////////////
// -- WEAPON TYPES: -- //
//////////////////////////////

		{
		"MP5 Submachine Gun",
		"MP5 Submachine Gun",
		"800",
		"BUYPIC03",
		"The Heckler & Koch MP5 is one of the\nmost widely used submachine guns in the\nworld, having been adopted by 40 nations and\nnumerous military, law enforcement, \nintelligence, and security organizations.",
		"SMG",
		"None",
		"Perk_Commando",
		},

		{
		"Pump Shotgun",
		"Pump Shotgun",
		"1300",
		"BUYPIC04",
		"A standard-issue pump shotgun, equipped\nwith a side holder for extra shells. It\nlooks like one wielded by a well-known\nbadass.",
		"Shotgun",
		"Dragon",
		"Perk_Juggernaut",
		},

		{
		"The Annihilator",
		"The Annihilator",
		"2000",
		"BUYPIC02",
		"\cGSCRAP THIS SHIT",
		"Rocketlauncher",
		"None",
		"Perk_Demo",
		},

		{
		"Zed's Lobotomizer",
		"Zed's Lobotomizer",
		"1800",
		"BUYPIC05",
		"The Lobotomizer, perfect for the sniper who\ndreams of becoming a doctor.\n\n\cGPerfect for brain surgery.",
		"Sniper",
		"Thermal",
		"Perk_Rifleman",
		},

		{
		"PPSH-41",
		"PPSH-41",
		"600",
		"BUYPIC00",
		"The PPSh-41 is a Soviet submachine gun designed \nby Georgi Shpagin as a cheap, simplified\nalternative to the PPD-40.",
		"SMG",
		"None",
		"Perk_Commando",
		},

		{
		"Sten Mk. II",
		"Sten MK II",
		"700",
		"BUYPIC01",
		"The STEN was a family of British 9mm\nsubmachine guns used extensively by British\nand  Commonwealth forces throughout World \nWar II and the Korean War, the Mark II \nbeing the most common.",
		"SMG",
		"Silencer",
		"Perk_Commando"
		},

		{
		"Splayer SSG",
		"Splayer SSG",
		"2000",
		"BUYPIC04",
		"A double-barreled shotgun with a different\ndesign, pushing the barrels up rather than\nsimply breaking open. Looks like something that\nwould be good to combat imps from\na hellish realm.",
		"Shotgun",
		"None",
		"Perk_Juggernaut",
		},

		{
		"AA12 Shotgun",
		"AA12 Shotgun",
		"2400",
		"BUYPIC06",
		"One of the most well-known and powerful shotguns on the \nmarket today, the AA12 is great for clearing out rooms of \nhostile terrorists. Or zombies.",
		"Shotgun",
		"None",
		"Perk_Juggernaut",
		},
		
		{
		"M16A4 Rifle",
		"M16A4 Rifle",
		"2400",
		"BUYPIC07",
		"The M16A4 Rifle is one of the more powerful military assault\nrifles in existence. On top of this, it looks stylish. Great\nfor taking out single targets or several.",
		"None",
		"None",
		"Perk_Commando",
		},
		
		{
		"M79 Grenade Launcher",
		"M79 Grenade Launcher",
		"5600",
		"BUYPIC08",
		"The M79 Grenade Launcher, or Thumper, is a classic single-shot\ngrenade launcher that has been around for ages. Not only\ndoes it look nice, but it packs a punch too.",
		"Rocketlauncher",
		"None",
		"Perk_Demo",
		},
		
		{
		"C4 Kit",
		"C4 Kit",
		"2000",
		"BUYPIC09",
		"C4 is always a fun thing to have around. The Demolitionist's C4\nkit is a great tool for making explosions from a distance,\nor setting traps. Throw, click, boom.",
		"None",
		"None",
		"Perk_Demo",
		},
		
		{
		"M2 Flamethrower",
		"Flamethrower",
		"5500",
		"BUYPIC10",
		"Whether or not owning a flamethrower is legal, the time of\nzombie apocalypses calls for an obligatory M2 Flamethrower in\nyour arsenal. \n\n\cIStart a disco inferno.",
		"None",
		"None",
		"Perk_Pyro",
		},
		
		{
		"Maschinenpistole 40",
		"MP40",
		"1500",
		"BUYPIC11",
		"The Maschinenpistole 40 is an SMG of German design, small yet\npowerful for its size. It is a common weapon amongst German\nforces and is also popular around the world.",
		"SMG",
		"None",
		"Perk_Commando",
		},
									};

//=========================================================================
// WEAPON UPGRADES
//=========================================================================

#define SU_NAME				0
#define SU_SUFFIX			1
#define SU_ACTOR			2
#define SU_BUYPIC			3
#define SU_INFO				4
#define SU_PREVIEW			5

#define SHOP_UPGRADECOUNT 5

str ShopUpgrades[SHOP_UPGRADECOUNT][6] = {

//		{
//		Display Name
//		Switcher (Weapon name + switcher = item)
//		Weapon actor this attachment goes on
//		Buypic
//		Description
//		Image preview
//		}

		// STEN - SILENCER
		{
		"Silencer",
		"Silencer",
		"Sten MK II",
		"BUYPIC03",
		"Silencers are good for keeping your guns quiet.",
		"ZTSCREN2",
		},

		// LOBOTOMIZER - THERMAL SCOPE
		{
		"Thermal Scope",
		"Thermal",
		"Zed's Lobotomizer",
		"BUYPIC04",
		"Thermal scopes are good for seeing things in the dark.",
		"ZTSCREN3",
		},

		// PUMP SHOTGUN - DRAGON'S BREATH
		{
		"Dragon's Breath",
		"Dragon",
		"Pump Shotgun",
		"BUYPIC04",
		"Dragon's Breath Ammo is good for fucking enemies up.",
		"ZTSCREN4",
		},
		
		// M79 - INCENDIARY AMMO
		{
		"Incendiary Grenades",
		"Incendiary",
		"M79 Grenade Launcher",
		"BUYPIC04",
		"Incendiary grenades can cause dangerous, large fires from\na great distance. Especially shot from an M79.",
		"ZTSCREN5",
		},
		
		// M79 - INCENDIARY AMMO
		{
		"Buckshot Grenades",
		"Shotgun",
		"M79 Grenade Launcher",
		"BUYPIC04",
		"Buckshot grenades can be used to turn the M79 into a\nfull-on shotgun. Functions like a normal shotgun would.",
		"ZTSCREN6",
		},
};

int ShopPrices[TOTALWEPS] = { 800, 1300, 2000, 1800, 600, 700, 2000, 2400, 2400, 5600, 2000, 5500, 1500 };

#define SP_PRICE					0
#define SP_WEP						1

int ShopUpgradePrices[SHOP_UPGRADECOUNT] = { 
	400, 		// Silencer 			(Sten)
	600, 		// Thermal Scope 		(Lobotomizer)
	1000, 		// Dragon's Breath		(Pump Shotgun)
	1500,		// Incendiary Nades		(M79)
	1500,		// Shotgun nades		(M79)
};
str TradButImages[4] = { "TRADBUT1", "TRADBUT2", "TRADBUT3", "TRADBUT4" };
str BackgroundImages[2] = { "ZTTRADER", "ZTUPGRAD" };
str CategoryImages[3] = { "CATEG1", "CATEG3", "CATEG2" };

script 879 (void)
{
	Print(s:"availableWeapons is ",d:availableWeapons);
}

// Actual menu script
script 701 (void)
{

	int cursor;
	int mouse_x;
	int mousex_var;
	int mouse_y;
	int mousey_var;
	int buttons;
	int wait;
	int IsSelling;
	int IsBuying;
	int colortodraw;
	int buttonimg;
	int IsUpgrading;
	int weptoapply;
	
	int currentTab = 0;
	int currentTabHover = -1;
	
	int discountAmount = 0;

	int tradercategory;

	str moneycolor;

	moneycolor = "\cJ$";

	buttons = GetPlayerInput(-1, INPUT_BUTTONS);

	wait=CheckInventory("Waiting");
	colortodraw = CR_BLACK;

	int hover;

	cursor=CheckInventory("MenuCursor");
	mouse_x = CheckInventory("MouseX");
	mouse_y = CheckInventory("MouseY");
	
	LocalAmbientSound("trader/hello",128);

	int categoryicon;

	// if (!CheckInventory("WasGivenPerk"))
	// {

		while (CheckInventory("MenuActive") )
		{
			// Perk discount
			discountAmount = LEVEL_DISCOUNT_AMOUNT*CheckInventory("PerkLevel");
			
			SetHudSize(800,600,1);
			SetFont(BackgroundImages[CheckInventory("TraderCategory")]);
			HudMessage(s:"a"; HUDMSG_PLAIN, 620, CR_WHITE, 400.4, 300.4, 0.0);

			// PERFORM CATEGORY CHECKS
			if (CursorIsOverX(CATEGORY1_MINX, CATEGORY1_MAXX) && CursorIsOverY(CATEGORY1_MINY, CATEGORY1_MAXY))
			{
				categoryicon=1;
			}
			else if (CursorIsOverX(CATEGORY2_MINX, CATEGORY2_MAXX) && CursorIsOverY(CATEGORY2_MINY, CATEGORY2_MAXY))
			{
				categoryicon=2;
			}
			else
			{
				categoryicon=0;
			}

			SetFont(CategoryImages[categoryicon]);
			HudMessage(s:"a"; HUDMSG_PLAIN, 619, CR_WHITE, 765.4, 500.4, 0.0);

			SetFont("SMALLFONT");

			// Do menu item shit
			cursor=0;

			//TakeInventory("MouseX",800);
			//TakeInventory("MouseY",800);

			buttons = GetPlayerInput(-1, INPUT_BUTTONS);

			///////////////////////////////////////////////////////////////////////
			// -- CONTROL THE MENU BASED ON THE CATEGORY ITEM -- //
			///////////////////////////////////////////////////////////////////////

			switch (CheckInventory("TraderCategory"))
			{
				//--BUYING WEAPONS----------------------------------------
				case 0:
				
				// DRAW PERK BUTTONS
				int currentFrame = 0;
				int tabSelector = -1;
				
				for (int pp = 0; pp<TOTAL_PERKS; pp++)
				{
					currentFrame = 0;
					
					if (currentTab == pp)
						currentFrame = 2;
						
					// CHECK TO SEE IF WE'VE MOUSED OVER THIS TAB
					if (CursorIsOverX(PERK_XSTART + (pp*PERK_WIDTH), PERK_XSTART + (pp*PERK_WIDTH) + PERK_WIDTH) && CursorIsOverY(PERK_YSTART,PERK_YSTART+PERK_HEIGHT))
					{
						tabSelector = pp;
						currentFrame = 1;
					}
						
					SetFont(perkButtonImages[currentFrame]);
					HudMessage(s:"a"; HUDMSG_PLAIN, (PERK_IDBASE*2)+pp, CR_YELLOW, PERK_XSTART + (pp*PERK_WIDTH) + ALIGN_LEFT, PERK_YSTART + ALIGN_LEFT, 0);
					SetFont(perkData[pp][PD_ICON]);
					HudMessage(s:"a"; HUDMSG_PLAIN, PERK_IDBASE+pp, CR_YELLOW, PERK_XSTART + (pp*PERK_WIDTH) + ALIGN_LEFT, (PERK_YSTART + ALIGN_LEFT)-20.0, 0);
				}
				
				currentTabHover = tabSelector;
				
				SetFont("SMALLFONT");
				
				int sellChecker = "";
				int sellDisplay = "";
				
				// -- HANDLES DRAWING THE WEAPON NAMES: LEFT -- //
				for (int i = 0; i <= availableWeapons; i ++)
				{
					sellChecker = traderActorName[i];
					sellDisplay = traderDisplayName[i];
					
					// DUALIES EXCEPTION
					if (traderDualVariant[i] != "None")
					{
						if (CheckInventory(traderDualVariant[i]))
						{
							// Name check
							if (traderDualName[i] == "DEFAULT")
								sellDisplay = StrParam(s:"Dual ",s:traderDisplayName[i],s:"s");
							else
								sellDisplay = traderDualName[i];
								
							sellChecker = traderDualVariant[i];
						}
						else
							sellChecker = traderSingleVariant[i];
					}

					if (CursorIsOverX(LEFTBOX_MINX, LEFTBOX_MAXX) && CursorIsOverY(LEFTBOX_MINY, LEFTBOX_MAXY))
					{
						IsSelling=1;
						if (CursorIsOverY(LEFTBOX_MINY + (i*12.0)+11.0, LEFTBOX_MINY + (i*12.0)+24.0))
						{
							cursor=i;
							HudMessage(s:"\cD$",d:traderPrice[i],s:" \c-",s:sellDisplay; HUDMSG_PLAIN, 300+i, CR_YELLOW, 65.1, 100.0+(i*12.0), 0);
						}
						else
						{
							if (CheckInventory(sellChecker))
								HudMessage(s:"\cD$",d:traderPrice[i],s:" \c-",s:sellDisplay; HUDMSG_PLAIN, 300+i, CR_WHITE, 65.1, 100.0+(i*12.0), 0);
							else
								HudMessage(s:"\cD$",d:traderPrice[i],s:" \c-",s:sellDisplay; HUDMSG_PLAIN, 300+i, CR_BLACK, 65.1, 100.0+(i*12.0), 0);
						}
					}
					else
					{
						IsSelling=0;
						if (CheckInventory(sellChecker))
								HudMessage(s:sellDisplay; HUDMSG_PLAIN, 300+i, CR_WHITE, 65.1, 100.0+(i*12.0), 0);
							else
								HudMessage(s:sellDisplay; HUDMSG_PLAIN, 300+i, CR_BLACK, 65.1, 100.0+(i*12.0), 0);
					}
				}
				
				int drawPrice = 0;
				int hasDiscount = 0;
				
				int baseListY = 100.0;
				int baseListY2 = 0.0;
				int baseCounter = -1;
				
				int nameToDraw = "";
				int itemChecker = "";
				int isDualies = 0;

				// -- HANDLES DRAWING THE WEAPON NAMES: RIGHT -- //
				for (int j = 0; j <= availableWeapons; j ++)
				{
					nameToDraw = traderDisplayName[j];
					itemChecker = traderActorName[j];
					
					//===========================================================
					// PREP THIS WEAPON FOR DUALIES
					//===========================================================
					
					if (traderDualVariant[j] != "None" && (CheckInventory(traderSingleVariant[j]) || CheckInventory(traderDualVariant[j])))
					{
						isDualies = 1;
						itemChecker = traderDualVariant[j];
						
						if (traderDualName[j] == "DEFAULT")
							nameToDraw = StrParam(s:"Dual ",s:nameToDraw,s:"s");
						else
							nameToDraw = traderDualName[j];
					}
					
					//===========================================================
					
					if (traderDiscountActor[j] == perkData[currentTab][PD_ITEM])
					{
						baseCounter ++;
						baseListY2 = baseListY + (baseCounter*12.0);
						
						// APPLY DISCOUNT
						if (CheckInventory(traderDiscountActor[j]) > 0)
						{
							hasDiscount = 1;
							drawPrice = traderPrice[j]-discountAmount;
							if (drawPrice < 5)
								drawPrice = 5;
						}
						else
						{
							hasDiscount = 0;
							drawPrice = traderPrice[j];
						}

						if (CursorIsOverX(RIGHTBOX_MINX, RIGHTBOX_MAXX) && CursorIsOverY(RIGHTBOX_MINY, RIGHTBOX_MAXY))
						{
							IsBuying=1;
							if (CursorIsOverY(baseListY2-6.0, baseListY2+6.0))
							{
								cursor=j;
								// Price to show when you're mousing over an item
								if (CheckInventory("Money") >= traderPrice[j])
								{
									colortodraw=CR_YELLOW;
									if (CheckInventory(itemChecker))
									{
										colortodraw = CR_RED;
										moneycolor = "\cG$";
									}
									else
										moneycolor="\cK$";
										
									if (hasDiscount)
									{
										moneycolor = "\cK$";
										colortodraw = CR_ORANGE;
									}
								}
								else
								{
									moneycolor="\cG$";
									colortodraw=CR_RED;
								}

								HudMessage(s:moneycolor,d:drawPrice,s:" \c-",s:nameToDraw; HUDMSG_PLAIN, 400+j, colortodraw, 580.1, baseListY2, 0);
							}
							else
							{
									int C2D = CR_WHITE;
									
									//===========================================================================
									// IF WE HAVE ENOUGH MONEY FOR THIS
									// Draws the money amount
									//===========================================================================
									if (hasDiscount)
										moneycolor ="\cK$";
									else
									{
										if (CheckInventory("Money") >= drawPrice)
										{
											moneycolor="\cD$";
										}
										else
										{
											C2D = CR_BLACK;
											moneycolor="\cG$";
										}
									}
									
									if (CheckInventory(itemChecker))
									{
										moneycolor = "\cG$";
										C2D = CR_BLACK;
									}
									
									HudMessage(s:moneycolor,d:drawPrice,s:" \c-",s:nameToDraw; HUDMSG_PLAIN, 400+j, C2D, 580.1, baseListY2, 0);
							}
						}
						else
						{
							IsBuying=0;
							//===========================================================================
							// IF WE HAVE ENOUGH MONEY FOR THIS
							//===========================================================================
							if (CheckInventory("Money") >= drawPrice)
							{
									if (CheckInventory(itemChecker))
											HudMessage(s:nameToDraw; HUDMSG_PLAIN, 400+j, CR_BLACK, 580.1, baseListY2, 0);
										else
											HudMessage(s:nameToDraw; HUDMSG_PLAIN, 400+j, CR_WHITE, 580.1, baseListY2, 0);
							}
								else
									HudMessage(s:nameToDraw; HUDMSG_PLAIN, 400+j, CR_BLACK, 580.1, baseListY2, 0);
						}
					}
					
					else
						HudMessage(s:""; HUDMSG_PLAIN, 400+j, colortodraw, 580.1, baseListY2, 0);
				}

				if (IsSelling || IsBuying)
				{
				SetFont(traderBuyPicture[cursor]);
				HudMessage(s:"a"; HUDMSG_PLAIN, 470, CR_WHITE, 512.5, 330.4, 0);

				// DRAW DISPLAY PRICE ON THE BOTTOM
				int bottomPrice = traderPrice[cursor];
				
				if (CheckInventory(traderDiscountActor[cursor]) > 0)
				{
					bottomPrice = traderPrice[cursor] - discountAmount;
					if (bottomPrice < 5)
						bottomPrice = 5;
				}
				
				
				SetFont("BIGFONT");
				HudMessage(s:traderDisplayName[cursor],s:" - $",d:bottomPrice; HUDMSG_PLAIN, 471, CR_WHITE, 400.4, 390.4, 0);

				SetFont("SMALLFONT");
				HudMessage(s:traderDescription[cursor]; HUDMSG_PLAIN, 472, CR_WHITE, 400.4, 430.4, 0);
				}
				
				// MONEY ON TOP
				SetFont("TYPEWRIT");
				HudMessage(s:"\cD$",i:CheckInventory("Money"); HUDMSG_PLAIN, 473, CR_WHITE, 415.1, 145.4, 0);

				///////////////////////////////////////////////////////
				// HANDLE IMAGES FOR THE BOTTOM BUTTONS //
				///////////////////////////////////////////////////////

				if (CursorIsOverX(BUTTON1_MINX, BUTTON1_MAXX) && CursorIsOverY(BUTTON1_MINY, BUTTON1_MAXY))
					buttonimg=1;
				else if (CursorIsOverX(BUTTON2_MINX, BUTTON2_MAXX) && CursorIsOverY(BUTTON2_MINY, BUTTON2_MAXY))
					buttonimg=2;
				else if (CursorIsOverX(BUTTON3_MINX, BUTTON3_MAXX) && CursorIsOverY(BUTTON3_MINY, BUTTON3_MAXY))
					buttonimg=3;
				else buttonimg = 0;

				SetFont(TradButImages[buttonimg]);
				HudMessage(s:"a"; HUDMSG_PLAIN, 474, CR_WHITE, 400.4, 525.4, 0);

				break;
				//=====================================================================================================
				//
				// UPGRADE MODE
				//
				//=====================================================================================================
				case 1:
					int wepNum = 0;
					
					if (CursorIsOverX(CBUTTON1_MINX, CBUTTON1_MAXX) && CursorIsOverY(CBUTTON1_MINY, CBUTTON1_MAXY))
					buttonimg=1;
					else if (CursorIsOverX(CBUTTON2_MINX, CBUTTON2_MAXX) && CursorIsOverY(CBUTTON2_MINY, CBUTTON2_MAXY))
						buttonimg=2;
					else if (CursorIsOverX(CBUTTON3_MINX, CBUTTON3_MAXX) && CursorIsOverY(CBUTTON3_MINY, CBUTTON3_MAXY))
						buttonimg=3;
					else buttonimg = 0;

					SetFont(TradButImages[buttonimg]);
					HudMessage(s:"a"; HUDMSG_PLAIN, 474, CR_WHITE, 583.4, 524.4, 0);

					// HANDLES DRAWING UPGRADE NAMES
					for (int m = 0; m < SHOP_UPGRADECOUNT; m ++)
					{
						for (int q=0; q<availableWeapons; q++)
						{
							if (ShopUpgrades[m][SU_ACTOR] == ShopWeapons[q][PURCHASE_ACTOR])
								wepNum = q;
						}
						
						SetFont("SMALLFONT");

						if (CursorIsOverX(UPGRADEBOX_MINX, UPGRADEBOX_MAXX) && CursorIsOverY(UPGRADEBOX_MINY, UPGRADEBOX_MAXY))
						{
							IsUpgrading=1;
							if (CursorIsOverY(UPGRADEBOX_MINY + (m*12.0)+11.0, UPGRADEBOX_MINY + (m*12.0)+24.0))
							{
								cursor=m+1;
								HudMessage(s:"\cD$",d:ShopUpgradePrices[m],s:" \cK",s:ShopWeapons[wepNum][PURCHASE_DISPLAYNAME],s:" \cJ - \cK",s:ShopUpgrades[m][SU_NAME]; HUDMSG_PLAIN, 300+m, CR_YELLOW, 398.1, 100.0+(m*12.0), 0);
							}
							else
							{
								HudMessage(s:"\cD$",d:ShopUpgradePrices[m],s:" \cD",s:ShopWeapons[wepNum][PURCHASE_DISPLAYNAME],s:" \cJ - ",s:ShopUpgrades[m][SU_NAME]; HUDMSG_PLAIN, 300+m, CR_WHITE, 398.1, 100.0+(m*12.0), 0);
							}
						}
						else
						{
							cursor=0;
							IsUpgrading=0;
							HudMessage(s:"\cD",s:ShopWeapons[wepNum][PURCHASE_DISPLAYNAME],s:" \cJ - ",s:ShopUpgrades[m][SU_NAME]; HUDMSG_PLAIN, 300+m, CR_BLACK, 398.1, 100.0+(m*12.0), 0);
						}
					}

					if (cursor>0)
					{
					// UPGRADE DESCRIPTION
					
					int upNum = 0;
					for (int p=0; p<availableWeapons; p++)
					{
						if (ShopUpgrades[cursor-1][SU_ACTOR] == ShopWeapons[p][PURCHASE_ACTOR])
							upNum = p;
					}
					
					SetFont("SMALLFONT");
					HudMessage(s:ShopUpgrades[cursor-1][SU_INFO],s:"\n\n\cDApplied to: \cJ",s:ShopWeapons[upNum][PURCHASE_DISPLAYNAME]; HUDMSG_PLAIN, 472, CR_WHITE, 61.1, 470.1, 0);
	
					// UPGRADE NAME
					SetFont("BIGFONT");
					HudMessage(s:ShopUpgrades[cursor-1][SU_NAME]; HUDMSG_PLAIN, 471, CR_WHITE, 218.4, 439.4, 0);

					// Whatever this is
					SetFont("TEXTOVER");
					HudMessage(s:"a"; HUDMSG_PLAIN, 473, CR_WHITE, 222.4, 439.4, 0);

					SetFont(ShopUpgrades[cursor-1][SU_PREVIEW]);
					HudMessage(s:"a"; HUDMSG_PLAIN, 618, CR_WHITE, 221.4, 349.4, 0.0);

					}
					else
					{
					HudMessage(s:""; HUDMSG_PLAIN, 472, CR_WHITE, 61.1, 470.1, 0);
					HudMessage(s:""; HUDMSG_PLAIN, 471, CR_WHITE, 61.1, 470.1, 0);
					HudMessage(s:""; HUDMSG_PLAIN, 473, CR_WHITE, 61.1, 470.1, 0);
					HudMessage(s:""; HUDMSG_PLAIN, 618, CR_WHITE, 61.1, 470.1, 0);
					}

					weptoapply = CheckInventory("WepToApply");
					if (weptoapply<1)
					{
						TakeInventory("WepToApply",99);
						GiveInventory("WepToApply",availableWeapons);
						weptoapply = availableWeapons;
					}
					if (weptoapply >= availableWeapons+1)
					{
						TakeInventory("WepToApply",99);
						GiveInventory("WepToApply",1);
						weptoapply = 1;
					}

					// WEAPON THIS WILL BE APPLIED TO
					//SetFont("SMALLFONT");
					//HudMessage(s:"\cJApplied to:\n\cK",s:ShopWeapons[weptoapply-1][PURCHASE_NAME]; HUDMSG_PLAIN, 470, CR_WHITE, 569.4, 448.4, 0);

				break;
			}

			RedrawCursor();

			if (wait == 0)
			{
				if (buttons & BT_MOVERIGHT && CheckInventory("TraderCategory") > 0)
				{
					LocalAmbientSound("menu/change",127);
					GiveInventory("WepToApply",1);
				}

				else if (buttons & BT_MOVELEFT && CheckInventory("TraderCategory") > 0)
				{
					LocalAmbientSound("menu/change",127);
					TakeInventory("WepToApply",1);
				}

				else if (buttons & BT_ATTACK)
				{
					LocalAmbientSound("sten/fire",127);

					SetFont("SMALLFONT");
					//====================================================================
					// SELL A WEAPON
					//====================================================================
					if (IsSelling)
					{
						int SSchecker = traderActorName[cursor];
						int sellName = traderDisplayName[cursor];
						
						if (traderDualVariant[cursor] != "None")
						{
							// See if we've got the dual version
							if (CheckInventory(traderDualVariant[cursor]))
							{
								if (traderDualName[cursor] == "DEFAULT")
									sellName = StrParam(s:"Dual ",s:traderDisplayName[cursor],s:"s");
								else
									sellName = traderDualName[cursor];
									
								SSchecker = traderDualVariant[cursor];
							}
							else
								SSchecker = traderSingleVariant[cursor];
						}
						
						if (CheckInventory(SSchecker))
						{
						TakeInventory(traderActorName[cursor],1);
						Log(s:"You sold your ",s:sellName,s:" and got $",d:traderPrice[cursor],s:" in return!");
						GiveInventory("Money",traderPrice[cursor]);
						//============================================
						// DUALIES EXCEPTION
						//============================================
							if (traderSingleVariant[cursor] != "None")
							{
								if (CheckInventory(traderDualVariant[cursor]))
								{
								TakeInventory(traderDualVariant[cursor],1);
								GiveInventory(traderSingleVariant[cursor],1);
								}
								else
								{
								TakeInventory(traderSingleVariant[cursor],1);
								}
							}
						}
						else
						{
						Log(s:"You don't have this item!");
						}
					}

					//====================================================================
					// PURCHASE A WEAPON
					//====================================================================
					else if (IsBuying)
					{
						// Figure out our discount
						// discountAmount
						int subDiscount = traderPrice[cursor];
						
							if (CheckInventory(traderDiscountActor[cursor]) > 0)
							{
								subDiscount = subDiscount - discountAmount;
								
								if (subDiscount < 5)
									subDiscount = 5;
							}
						
						int showName = StrParam(s:"a ",s:traderDisplayName[cursor]);
						int itemChecker2 = traderActorName[cursor];
						
						if (traderDualVariant[cursor] != "None")
						{
							if (CheckInventory(traderSingleVariant[cursor]))
								itemChecker2 = traderDualVariant[cursor];
						}
						
						// IF WE DON'T HAVE THE WEAPON
						if (!CheckInventory(itemChecker2))
						{
							// IF WE HAVE ENOUGH MONEY
							if (CheckInventory("Money") >= subDiscount)
							{
							TakeInventory("Money",subDiscount);
								//============================================
								// DUALIES EXCEPTION
								//============================================
								if (traderDualVariant[cursor] != "None")
								{
									if (CheckInventory(traderSingleVariant[cursor]))
									{
										//-CONTROL SHOW NAME---------
										if (traderDualName[cursor] == "DEFAULT")
											showName = StrParam(s:"the Dual ",s:traderDisplayName[cursor],s:"s");
										else
											showName = StrParam(s:"the ",s:traderDualName[cursor]);
										//---------------------------
										
									GiveInventory(traderDualVariant[cursor],1);
									TakeInventory(traderActorName[cursor],1);
									}
									
									else
									{
									GiveInventory(traderActorName[cursor],1);
									}
								}
								else
								{
								GiveInventory(traderActorName[cursor],1);
								}
								
							Log(s:"You bought a ",s:showName,s:" for $",d:subDiscount,s:"!");							
							}
							else
							{
							Log(s:"You don't have enough money!");
							}
						}
						else
						{
						Log(s:"You already have this item!");
						}
						
						//============================================
					}
					//===========================================================================
					// 
					// UPGRADE PURCHASING
					//
					//===========================================================================
					else if (IsUpgrading)
					{
						int tempNum = 0;
						// Find the number
						for (int u=0; u<availableWeapons; u++)
						{
							if (ShopUpgrades[cursor-1][SU_ACTOR] == ShopWeapons[u][PURCHASE_ACTOR])
								tempNum = u;
						}
								//===========================================================================
								// If we have the weapon in our inventory
								//===========================================================================
								if (CheckInventory(ShopUpgrades[cursor-1][SU_ACTOR]))
								{
									// ===========================================================================
									// If we don't already have the silencer attachment
									// ===========================================================================
									if (!CheckInventory(StrParam(s:ShopUpgrades[cursor-1][SU_ACTOR],s:" ",s:ShopWeapons[weptoapply-1][6])))
									{
											// ===========================================================================
											// If we have enough money
											// ===========================================================================
											if (CheckInventory("Money") >= ShopUpgradePrices[cursor-1])
											{
													GiveInventory(StrParam(s:ShopWeapons[tempNum][PURCHASE_ACTOR],s:" ",s:ShopUpgrades[cursor-1][SU_SUFFIX]), 1);
													TakeInventory("Money",ShopUpgradePrices[cursor-1]);
													Log(s:"You bought a ",s:ShopUpgrades[cursor-1][SU_NAME],s:" for your ",s:ShopWeapons[tempNum][PURCHASE_DISPLAYNAME],s:".");
											}
											// ===========================================================================
											// Not enough money
											// ===========================================================================
											else
											{
												Log(s:"You do not have enough money for this modification.");
											}
									}
									// ===========================================================================
									// Already have the upgrade
									// ===========================================================================
									else
									{
										Log(s:"You have already applied that modification to this weapon.");
									}
								}
								// ===========================================================================
								// Not compatible
								// ===========================================================================
								else
								{
								Log(s:"You do not own this weapon!");
								}
					}
					//===========================================================================

					// IF WE CLICK ON THE BUTTONS
					else
					{
						// IF WE CLICK ONE OF THE THREE BUTTONS
						if (buttonimg == 1)
						{
							if (CheckInventory("Money") >= 500)
							{
								TakeInventory("Money",500);
								GiveInventory("Backpack",20);
								Log(s:"You bought some ammo for $500.");
							}
							else
							{
								Log(s:"You don't have enough.");
							}
						}
						else if (buttonimg == 2)
						{
							if (CheckInventory("Money") >= 200)
							{
								TakeInventory("Money",200);
								GiveInventory("GreenArmor",1);
								Log(s:"You bought some armor for $200.");
							}
							else
							{
								Log(s:"You don't have enough.");
							}
						}
						else if (buttonimg == 3)
						{
							TakeInventory("MenuActive",1);
						}
						
						// IF WE ARE USING TRADER CATEGORIES
						if (categoryicon == 1)
						{
							TakeInventory("TraderCategory",2);
							ClearMainTraderMessages();
						}
						else if (categoryicon==2)
						{
							GiveInventory("TraderCategory",1);
							ClearMainTraderMessages();
						}
						
						// CHECK TO SEE IF WE'RE OVER A PERK BUTTON
						if (CursorIsOverX(PERK_XSTART,PERK_XSTART+((TOTAL_PERKS+1)*PERK_WIDTH)) && CursorIsOverY(PERK_YSTART,PERK_YSTART+PERK_HEIGHT) && currentTabHover > -1)
						{
							currentTab = currentTabHover;
						}
					}

				}
				// REDRAW CURSOR
			}

		   //do
		   //{
			 GiveInventory("Waiting",1);
			 wait++;

			 if (buttons & BT_ATTACK && wait > 10)
			{
				TakeInventory("Waiting",200);
				wait=0;
			}

			if (buttons & BT_MOVELEFT && wait > 3)
			{
				TakeInventory("Waiting",200);
				wait=0;
			}

			if (buttons & BT_MOVERIGHT && wait > 3)
			{
				TakeInventory("Waiting",200);
				wait=0;
			}

			if (GetCVar ("cl_zombiedebugger") == 1)
			{
			Setfont("SMALLFONT");
			print(s:"Wait is ",d:wait,s:"\nCursor pos: ",d:GetMouseX(),s:" ; ",d:GetMouseY(),s:"\nCursor is ",d:cursor,s:"\nSelling: ",d:IsSelling);
			}

			delay(1);
		}

		// Clear all hud messages

		ClearTraderMessages();
		// Deactivate the menu
		// GiveInventory("WasGivenPerk",1);
		ACS_ExecuteAlways(250,0,1,0,0);

	// }
	// else
	// {
	// ACS_Execute(808, 0, 1, 0, 0);
	// }
}

function void ClearTraderMessages (void)
{
	for (int k=0; k<=availableWeapons; k++)
		{
			HudMessage(s:""; HUDMSG_PLAIN, 300+k, CR_WHITE, 0, 0, 0);
			HudMessage(s:""; HUDMSG_PLAIN, 400+k, CR_WHITE, 0, 0, 0);
		}

		HudMessage(s:""; HUDMSG_PLAIN, 4, CR_WHITE, 0, 0, 0);

		HudMessage(s:""; HUDMSG_PLAIN, 618, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 619, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 620, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 470, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 471, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 472, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 473, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 474, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 250, CR_WHITE, 0, 0, 0);
		
		for (int qq=0; qq<TOTAL_PERKS; qq++)
		{
		HudMessage(s:""; HUDMSG_PLAIN, (PERK_IDBASE*2)+qq, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, PERK_IDBASE+qq, CR_WHITE, 0, 0, 0);
		}
}

function void ClearMainTraderMessages (void)
{
	for (int k=0; k<=availableWeapons; k++)
		{
			HudMessage(s:""; HUDMSG_PLAIN, 300+k, CR_WHITE, 0, 0, 0);
			HudMessage(s:""; HUDMSG_PLAIN, 400+k, CR_WHITE, 0, 0, 0);
		}


		HudMessage(s:""; HUDMSG_PLAIN, 4, CR_WHITE, 0, 0, 0);
		
		HudMessage(s:""; HUDMSG_PLAIN, 618, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 470, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 471, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 472, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 473, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, 474, CR_WHITE, 0, 0, 0);
		
		for (int qq=0; qq<TOTAL_PERKS; qq++)
		{
		HudMessage(s:""; HUDMSG_PLAIN, (PERK_IDBASE*2)+qq, CR_WHITE, 0, 0, 0);
		HudMessage(s:""; HUDMSG_PLAIN, PERK_IDBASE+qq, CR_WHITE, 0, 0, 0);
		}
}

function void RedrawCursor (void)
{
		int mousex_var;
		int mousey_var;
		int mouse_x;
		int mouse_y;

		mousex_var = GetPlayerInput(-1, INPUT_YAW) / 30;
		mousey_var = GetPlayerInput(-1, INPUT_PITCH) / 20;

		mouse_x = CheckInventory("MouseX") * 1.0;
		mouse_y = CheckInventory("MouseY") * 1.0;

				if (mousex_var > 0)
					TakeInventory("MouseX",abs(mousex_var));
				else
					GiveInventory("MouseX",abs(mousex_var));

				if (mousey_var > 0)
					TakeInventory("MouseY",abs(mousey_var));
				else
					GiveInventory("MouseY",abs(mousey_var));

				SetFont("cursorp3");
				HudMessage(s:"a"; HUDMSG_PLAIN, 4, CR_WHITE, mouse_x+0.1, mouse_y+0.1, 0);
}

function int CursorIsOverX (int minx, int maxx)
{
	int cursor_xpos;
	int value;
	cursor_xpos = CheckInventory("MouseX") * 1.0;


	if (cursor_xpos > minx && cursor_xpos < maxx)
		value = 1;
	else
		value = 0;

	return value;
}

function int CursorIsOverY (int miny, int maxy)
{
	int value;
	int cursor_ypos;
	cursor_ypos = CheckInventory("MouseY") * 1.0;

	if (cursor_ypos > miny && cursor_ypos < maxy)
		value = 1;
	else
		value = 0;

	return value;
}

function int GetMouseX (void)
{
	int pos;

	pos = CheckInventory("MouseX");
	return pos;
}

function int GetMouseY (void)
{
	int pos;

	pos = CheckInventory("MouseY");
	return pos;
}