// For world events, such as things feeling pain or dying.
class GC_WorldEventHandler : EventHandler
{
	// Arrays for powerups to extend.
	static const string GunzerkPowerups [] = {
	"PowerGunzerking",		// The main powerup.
	"PowerGunzerkNotDamage",// Damage resistance.
	"PowerAmmoRegen"		// Ammo regeneration, soon to be removed.
	};
	
	static const string BerserkPowerups [] = {
	"PowerBerserkDamage",	// The main damage booster.
	"PowerSpeeding",		// Speed boost.
	"PowerJumping",			// Jump boost.
	"PowerBerserkNotDamage"	// Damage resistance.
	};
	
	static const string AmplifyPowerups [] = {
	"PowerTomeEffect"		// Just one powerup for spells stronk.
	};
	
	static const string QuadDamagePowerups [] = {
	"PowerQuadDamage"		// One Quad to rule them all.
	};
	
	// Arrays full of damagetypes to check to ensure extending is valid.
	static const string GunzerkDamageTypes [] = {
	"Hitscan",				// Bullets/pellets.
	"Shrapnel",				// Ironblast.
	"Matriarch",			// Explosives.
	"Spirytus",				// Spiritcannon.
	"LightningBlue",		// Augur rails.
	"Tiberium",				// Dreadful balls.
	"BlueTiberium"			// Dreadful empowered.
	};
	
	static const string BerserkDamageTypes [] = {
	"SuperCloseCombat",		// Pawnch.
	"SuperMidasCloseCombat",// Pawnch, Midas Edition.
	"SuperShoelaces",		// Kick.
	"SuperMidasShoelaces",	// Kick, Midas Edition.
	"Swordsman"				// Sord.
	};
	
	static const string AmplifyDamageTypes [] = {
	"Fire",					// Firebreath, Skullfire, Flamewave.
	"Lightning",			// Thunderstruck.
	"Ice",					// Frostbite, Chillgrasp.
	"Acid",					// Gumpop.
	"Breath",				// Scourgebreath, Apocalypse.
	"Flesh",				// Corpseblast.
	"Crow",					// Trap Ravens.
	"Crow2",				// Normal Ravens.
	"Manuel",				// Threadcutter.
	"UndeadGoHome",			// Hammersmite.
	"Meteorfist",			// Meteorfist impact.
	"Annihilation",			// Annihilation.
	"Baaah",				// Sheepshield.
	"Spirytus",				// Spiritcannon.
	"Dark"					// Darkpath.
	};
	
	static const string QuadDamageTypes [] = {
	"Normal",
	"Hitscan",
	"Shrapnel",
	"Matriarch",
	"Spirytus",	
	"LightningBlue",
	"Tiberium",	
	"BlueTiberium",
	"CloseCombat",
	"Shoelaces",
	"SuperCloseCombat",
	"SuperMidasCloseCombat",
	"SuperShoelaces",
	"SuperMidasShoelaces",
	"Fire",
	"Lightning",
	"Ice",
	"Acid",
	"Breath",
	"Flesh",
	"Crow",
	"Crow2",
	"Manuel",
	"UndeadGoHome",
	"Meteorfist",
	"Annihilation",
	"Baaah",
	"Spirytus",
	"Dark"
	};
	
	// Types of money items, for fun and fancy reasons
	static const string GoldTypes [] = {
	"Dosh1",				// 1 gold.
	"Dosh5",				// 5 gold.
	"Dosh10",				// 10 gold.
	"Dosh25",				// 25 gold.
	"Dosh35",				// 35 gold.
	"Amber",				// 50 gold.
	"Ruby",					// 100 gold.
	"Emerald",				// 250 gold.
	"Sapphire",				// 400 gold.
	"Diamond",				// 20000 gold.
	// Midas coins.
	/*"SmallMidasCoin",
	"BigMidasCoin",*/
	"SmallMidasCoinStationary",
	"BigMidasCoinStationary"
	};
	
	override void WorldThingSpawned(WorldEvent e)
	{
		// Add spawned monsters to the monster list.
		// This includes monsters from level start.
		let Things = e.Thing;
		let gvars = GC_GlobalVariables.Get();
		if(Things && Things.bIsMonster)
		{
			gvars.MonsterList.Push(Things);
		}
		
		// Add money items to a money list.
		for(int i = 0; i < GoldTypes.Size(); i++)
		{
			if(Things && Things.GetClassName() == GoldTypes[i])
			{
				gvars.MoneyList.Push(Things);
				break;
			}
		}
	}
	
	override void WorldThingRevived(WorldEvent e)
	{
		// Add anything revived to the monster list as well.
		let Things = e.Thing;
		let gvars = GC_GlobalVariables.Get();
		if(Things && Things.bIsMonster)
		{
			gvars.MonsterList.Push(Things);
		}
		
		// And probably remove it from the corpse list.
		for(int i = 0; i < gvars.CorpseList.Size(); i++)
		{
			if(Things == gvars.CorpseList[i])
			{
				gvars.CorpseList.Delete(i);
			}
		}
	}

	override void WorldThingDied(WorldEvent e)
	{
		let victim = e.Thing;
		let killer = e.Thing.Target;
		let gvars = GC_GlobalVariables.Get();
		string DamageTypeTaken = victim.DamageTypeReceived;
		int i; // Iterator.
		
		// Remove the actor from the monster list, it is dead.
		for(i = 0; i < gvars.MonsterList.Size(); i++)
		{
			if(victim == gvars.MonsterList[i])
			{
				gvars.MonsterList.Delete(i);
			}
		}
		
		// It's dead, add it to the corpse list.
		if(victim.bIsMonster)
		{
			gvars.CorpseList.Push(victim);
		}
		
		// Only run the following for players vs. monsters.
		if(killer && killer is "PlayerPawn" && victim.bIsMonster && !victim.bFriendly)
		{
			// Croesus blessing.
			if(killer.CountInv("PowerMidasMode") && killer.CountInv("CroesusBlessing"))
			{
				victim.A_DropItem("MidasSphere", 1, 3*killer.CountInv("CroesusBlessing"));
			}
			
			//Console.Printf("%s was killed by damagetype %s", victim.GetClassName(), DamageTypeTaken);
			
			// Carnage collection.
			if(killer.CountInv("CarnageCollector"))
			{
				// Extending Gunzerking time.
				if(killer.CountInv(GunzerkPowerups[0]))
				{
					for(i = 0; i < GunzerkDamageTypes.Size(); i++)
					{
						if(DamageTypeTaken ~== GunzerkDamageTypes[i])
						{
							for(i = 0; i < GunzerkPowerups.Size(); i++)
							{
								let CurrentPowerup = Powerup(killer.FindInventory(GunzerkPowerups[i]));
								if(CurrentPowerup) // If the powerup exists, extend it.
								CurrentPowerup.EffectTics += 20 * killer.CountInv("CarnageCollector");
							}
							break;
						}
					}
				}
				
				// Extending Berserking time.
				if(killer.CountInv(BerserkPowerups[0]))
				{
					for(i = 0; i < BerserkDamageTypes.Size(); i++)
					{
						if(DamageTypeTaken ~== BerserkDamageTypes[i])
						{
							for(i = 0; i < BerserkPowerups.Size(); i++)
							{
								let CurrentPowerup = Powerup(killer.FindInventory(BerserkPowerups[i]));
								if(CurrentPowerup) // If the powerup exists, extend it.
								CurrentPowerup.EffectTics += 20 * killer.CountInv("CarnageCollector");
							}
							break;
						}
					}
				}
				
				// Extending Amplifier time.
				if(killer.CountInv(AmplifyPowerups[0]))
				{
					for(i = 0; i < AmplifyDamageTypes.Size(); i++)
					{
						if(DamageTypeTaken ~== AmplifyDamageTypes[i])
						{
							for(i = 0; i < AmplifyPowerups.Size(); i++)
							{
								let CurrentPowerup = Powerup(killer.FindInventory(AmplifyPowerups[i]));
								if(CurrentPowerup) // If the powerup exists, extend it.
								CurrentPowerup.EffectTics += 20 * killer.CountInv("CarnageCollector");
							}
							break;
						}
					}
				}
				
				// Extending Quad Damage time.
				if(killer.CountInv(QuadDamagePowerups[0]))
				{
					for(i = 0; i < QuadDamageTypes.Size(); i++)
					{
						if(DamageTypeTaken ~== QuadDamageTypes[i])
						{
							for(i = 0; i < QuadDamagePowerups.Size(); i++)
							{
								let CurrentPowerup = Powerup(killer.FindInventory(QuadDamagePowerups[i]));
								if(CurrentPowerup) // If the powerup exists, extend it.
								CurrentPowerup.EffectTics += 20 * killer.CountInv("CarnageCollector");
							}
							break;
						}
					}
				}
			}
		}
	}
	
	override void WorldThingDestroyed(WorldEvent e)
	{
		let Things = e.Thing;
		let gvars = GC_GlobalVariables.Get();
		int i;
		
		// If a monster was somehow destroyed before it died,
		// also remove it from the monster list.
		for(i = 0; i < gvars.MonsterList.Size(); i++)
		{
			if(Things == gvars.MonsterList[i])
			{
				gvars.MonsterList.Delete(i);
			}
		}
		
		// If it was a gold item picked up, remove it from the
		// money list.
		for(int i = 0; i < gvars.MoneyList.Size(); i++)
		{
			if(Things == gvars.MoneyList[i])
			{
					gvars.MoneyList.Delete(i);
					break;
			}
		}
		
		// Corpses that are destroyed should be removed from
		// that list as well.
		for(int i = 0; i < gvars.CorpseList.Size(); i++)
		{
			if(Things == gvars.CorpseList[i])
			{
				gvars.CorpseList.Delete(i);
			}
		}
	}
}

// A global variables class for data that pertains to individual maps.
class GC_GlobalVariables : Thinker
{
	// Monster list. Many uses, such as checking for nearest to a point.
	Array<Actor> MonsterList;
	// Corpse list, for corpseblasting.
	Array<Actor> CorpseList;
	// Money list, primarily for the money magnet and locator.
	Array<Actor> MoneyList;
	
	GC_GlobalVariables Init()
	{
		ChangeStatNum(STAT_INFO);
		return self;
	}
	
	static GC_GlobalVariables Get()
	{
		ThinkerIterator ti = ThinkerIterator.Create("GC_GlobalVariables", STAT_INFO);
		let gvars = GC_GlobalVariables(ti.Next());
		if(!gvars)
		{
			gvars = new("GC_GlobalVariables").Init();
		}
		return gvars;
	}
}

// A global data class for information meant to be pulled from above the game state.
class GC_GlobalDataHandler : StaticEventHandler
{
	// Ammo regeneration types and tics for Gunzerking.
	Array<String> AmmoRegenTypes;
	Array<Int> AmmoRegenTics;
	// Artifacts for valid chest drops.
	Array<String> ChestArtifactList;
	
	static GC_GlobalDataHandler Get()
	{
		return GC_GlobalDataHandler(Find("GC_GlobalDataHandler"));
	}
	
	override void OnRegister()
	{
		BuildAmmoRegenLists();
		Console.Printf("\c[Green]Global Gunzerk ammo regen list created.\c-");
		BuildArtifactList();
		Console.Printf("\c[Green]Global chest artifact list created.\c-");
	}
	
	void BuildAmmoRegenLists()
	{
		//Ammo regen ratios.
		AmmoRegenTypes.Push("LonghornAmmo");
		AmmoRegenTics.Push(73);
		AmmoRegenTypes.Push("ShotgunAmmo");
		AmmoRegenTics.Push(21);
		AmmoRegenTypes.Push("PulverizerAmmo");
		AmmoRegenTics.Push(8);
		AmmoRegenTypes.Push("MatriarchAmmo");
		AmmoRegenTics.Push(90);
		AmmoRegenTypes.Push("AugurAmmo");
		AmmoRegenTics.Push(350);
		AmmoRegenTypes.Push("TiberiumAmmo");
		AmmoRegenTics.Push(700);
		AmmoRegenTypes.Push("TNTAmmo");
		AmmoRegenTics.Push(350);
		
		//Hexen ammo.
		AmmoRegenTypes.Push("BlueMana");
		AmmoRegenTics.Push(25);
		AmmoRegenTypes.Push("GreenMana");
		AmmoRegenTics.Push(25);
	}
	
	void BuildArtifactList()
	{
		// List of chest artifacts to be dropped.
		ChestArtifactList.Push("CroesusBlessing");
		ChestArtifactList.Push("VoidwalkersTouch");
		ChestArtifactList.Push("TravelersJournal");
		ChestArtifactList.Push("CarnageCollector");
		ChestArtifactList.Push("ArbiterModule");
		ChestArtifactList.Push("HeavyDutyStompers");
		ChestArtifactList.Push("PhilosophersTome");
		ChestArtifactList.Push("ManashieldTome");
		ChestArtifactList.Push("GladiusBarrel");
		ChestArtifactList.Push("DraughTome");
		ChestArtifactList.Push("TibCore");
		ChestArtifactList.Push("AlchemyLab");
		ChestArtifactList.Push("MastersOfPain");
		ChestArtifactList.Push("SupermagJohnson");
		ChestArtifactList.Push("SupermagTheta");
		ChestArtifactList.Push("StridersLantern");
		ChestArtifactList.Push("SplitterShield");
		ChestArtifactList.Push("PowerAutoCannon");
	}
}