
Const FAS_DukebotHealth = 300;

class Dukebot1Health : Ammo
{
	Default
	{
		+INVENTORY.IGNORESKILL;
		Inventory.Amount 1;
		Inventory.MaxAmount FAS_DukebotHealth;
	}
}

class Dukebot2Health : Ammo
{
	Default
	{
		+INVENTORY.IGNORESKILL;
		Inventory.Amount 1;
		Inventory.MaxAmount FAS_DukebotHealth;
	}
}

class Dukebot3Health : Ammo
{
	Default
	{
		+INVENTORY.IGNORESKILL;
		Inventory.Amount 1;
		Inventory.MaxAmount FAS_DukebotHealth;
	}
}

class Dukebot4Health : Ammo
{
	Default
	{
		+INVENTORY.IGNORESKILL;
		Inventory.Amount 1;
		Inventory.MaxAmount FAS_DukebotHealth;
	}
}

class ZDukebot : Actor
{
	string dukebothealth;
	string dukebotduration;
	string dukebotactive;
	string dukebotinventory;
	string dukebotretrieve;
	string dukebottype;
	int playerowner;
	int clock;
	int lasthealth;
	bool retrieve;
	int shrinkcount;

	override void PostBeginPlay()
	{
		Super.PostBeginPlay();

		dukebothealth = "";
		dukebotduration = "";
		dukebotactive = "";
		dukebotinventory = "";
		dukebotretrieve = "";
		dukebottype = "";
		playerowner = -1;
		clock = -25;
		lasthealth = -1;
		retrieve = false;
		shrinkcount = -1;
	}

	void A_BeginDukebot(string dbhealth, string dbduration, string dbactive, string dbinventory, string dbretrieve, string dbtype)
	{
		dukebothealth = dbhealth;
		dukebotduration = dbduration;
		dukebotactive = dbactive;
		dukebotinventory = dbinventory;
		dukebotretrieve = dbretrieve;
		dukebottype = dbtype;

		if(master) {
			playerowner = Playerpawn(master).PlayerNumber();
		}

		if (playeringame[playerowner] && players[playerowner].mo) {
			let invh = players[playerowner].mo.FindInventory(dukebothealth);
			if (invh!=null && invh.Amount>0) {
				self.health = invh.Amount;
			}
			lasthealth = self.health;
		}

		self.bINVULNERABLE=0; self.bNODAMAGE=0; self.bNOPAIN=0; self.bTHRUACTORS=0; self.bGHOST=0; self.bNORADIUSDMG=0; self.bDONTTHRUST=0; self.ThruBits=0;
	}

	void A_SwitchActorTransferHealth(string newactor, StateLabel beginstate)
	{
		Actor a = self;

		let anewactor = Spawn(newactor, a.pos);
		anewactor.health = a.health;
		anewactor.tracer = a.tracer;

		anewactor.SetStateLabel(beginstate);
	}

	void A_SetSizeHeight()
	{
		self.A_SetSize(-1, self.default.height);
	}

	void A_JumpIfLowerScale(StateLabel label)
	{
		if ((self.Scale.X < self.default.scale.Y) && (self.Scale.Y < self.default.scale.Y)) {self.SetStateLabel(label); return;}
	}

	void A_ShrinkBot(double sScale=0.2, double sHeight=16)
	{
		Actor a = self;

		a.A_SetScale(sScale);
		a.A_SetSize(-1, sHeight);
		a.health = max(a.health/5.0,1);
		a.speed = a.default.speed/2.0;
		a.bFORCEXYBILLBOARD=true;
		a.bSOLID=false;
		a.bFRIGHTENED=true;
		a.A_UnSetFloorClip();
		shrinkcount = 0;
	}

	void A_UnshrinkBot()
	{
		Actor a = self;

		a.A_SetScale(a.default.scale.x,a.default.scale.y);
		a.speed = a.default.speed;
		a.bFORCEXYBILLBOARD=false;
		a.bFRIGHTENED=false;
		a.A_SetFloorClip();
	}

	void A_MissileType(int mtype, StateLabel label)
	{
		if (playeringame[playerowner] && players[playerowner].mo) {
			let inv = players[playerowner].mo.FindInventory(dukebottype);
			if (inv!=null && inv.Amount>0) {
				if(inv.Amount == mtype) {self.SetStateLabel(label); return;}
			}
		}
	}

	override int DamageMobj(Actor inflictor, Actor source, int damage, Name mod, int flags, double angle)
    	{
		if(source && source!=self && source.species=="Dukes")
		{
			damage = 0;
		}
		else if ( inflictor && (mod == 'Fire' || mod == 'DukeAcid') )
		{
			if (inflictor.GetClassName()=="RadioactiveProjectile" || inflictor.GetClassName()=="BarrelExplosionAcidColumn") {
				damage = damage/2.0;
			} else	if (inflictor.GetClassName()=="DukeBurningFireDamage") {
				damage = damage/5.0;
			}
		}

		if(inflictor && inflictor.GetClass() is 'DukePlasmaExplosion') damage = 0;

	        return super.DamageMobj(inflictor, source, damage, mod, flags, angle);
	}

	override void Die(Actor source, Actor inflictor, int dmgflags)
	{
	    if (playerowner > -1) {
		if (playeringame[playerowner] && players[playerowner].mo) {
			players[playerowner].mo.A_TakeInventory(dukebotactive, 1);
			players[playerowner].mo.A_SetInventory(dukebotretrieve, 0);

			if(!retrieve) {
				players[playerowner].mo.A_TakeInventory(dukebotinventory, 1);
				players[playerowner].mo.A_SetInventory(dukebothealth, 0);
				players[playerowner].mo.A_SetInventory(dukebotduration, 0);
				players[playerowner].mo.A_SetInventory(dukebottype, 0);

				let inv1 = players[playerowner].mo.FindInventory("Dukebot1Active");
				let inv2 = players[playerowner].mo.FindInventory("Dukebot2Active");
				let inv3 = players[playerowner].mo.FindInventory("Dukebot3Active");
				let inv4 = players[playerowner].mo.FindInventory("Dukebot4Active");
				if ( (inv1==null || (inv1!=null && inv1.Amount==0)) && (inv2==null || (inv2!=null && inv2.Amount==0)) && (inv3==null || (inv3!=null && inv3.Amount==0)) && (inv4==null || (inv4!=null && inv4.Amount==0)) )
				{
					players[playerowner].mo.A_TakeInventory("Retrieve_Duke_Bots", 1);
				}
			}

			if(retrieve) {
				if(shrinkcount>=0) {
					int dbhealth = lasthealth*5.0;
					players[playerowner].mo.A_SetInventory(dukebothealth, dbhealth);
				}

				let inv = players[playerowner].mo.FindInventory(dukebotinventory);
				if (inv!=null && inv.Amount>0) {
					players[playerowner].mo.A_GiveInventory("Duke_Bot", 1);
				}
			}
		}
	    }

	    super.Die(source, inflictor, dmgflags);
	}

	override void Tick()
	{
		let invd = self.FindInventory("DukeDamagePickup");
		if (invd!=null && invd.Amount>0) {
			self.A_TakeInventory("DukeDamagePickup",1);
		}

        	Super.Tick();

		if(shrinkcount>=0 && self.health>0) {
			shrinkcount++;
			if(shrinkcount>=320) {
				shrinkcount = -1;
				self.health = self.health*5.0;
				self.SetStateLabel("Unshrink");
			}
		}

		String st = self.GetClassName();
		if (playerowner == -1 || self.bINVULNERABLE) {
			if (st.Left(7) == "Dukebot") {
				self.SetStateLabel("Spawn");
				return;
			} else {
				return;
			}
		}

		if (playeringame[playerowner] && players[playerowner].mo) {
			if(!retrieve && self.health>0) {
				let invh = players[playerowner].mo.FindInventory(dukebothealth);
				if (invh!=null && invh.Amount>lasthealth) {
					self.health = invh.Amount;
				} else {
					int dbhealth = self.health;
					if(dbhealth<0) dbhealth = 0;
					players[playerowner].mo.A_SetInventory(dukebothealth, dbhealth);
				}
				lasthealth = self.health;
				
				if(clock==0) {
					let inv = players[playerowner].mo.FindInventory(dukebotduration);
					if (inv!=null && inv.Amount>0) {
						inv.Amount--;
					} else if (inv!=null && inv.Amount<=0) {
						self.A_Die("Vanish");
					}
				}
				clock++;
				if(clock==100) clock=0;

				let invr = players[playerowner].mo.FindInventory(dukebotretrieve);
				if (invr!=null && invr.Amount>0) {
					self.bNOPAIN = 1;
					retrieve = true;
					self.A_Die("Vanish");
				}
			}
			else if(retrieve && self.health>0) {
				let inva = players[playerowner].mo.FindInventory(dukebotactive);
				if (inva!=null && inva.Amount>0) {
					self.bNOPAIN = 1;
					retrieve = true;
					self.A_Die("Vanish");
				} else {
					Destroy();
				}
			}
		} else {
			Destroy();
		}
	}

}