
class ZTempInvActor : Actor
{
	string invty;
	string invon;
	string invactive;
	string invduration;
	string invretrieve;
	string loopsound;
	int loopsoundchannel;
	string offsound;
	int playerowner;
	int clock;
	int clocklimit;
	bool retrieve;

	void A_BeginItem(string iv, string ivon, string ivact, string ivdur, string ivret, int climit, string lsound, int lsoundc, string offs)
	{
		invty = iv;
		invon = ivon;
		invactive = ivact;
		invduration = ivdur;
		invretrieve = ivret;
		clocklimit = climit;
		loopsound = lsound;
		loopsoundchannel = lsoundc;
		offsound = offs;

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

	override void OnDestroy()
	{
		if (playeringame[playerowner] && players[playerowner].mo) {
			if(loopsound) players[playerowner].mo.A_StopSound(loopsoundchannel);
			if(offsound) players[playerowner].mo.A_PlaySound(offsound);
			players[playerowner].mo.A_SetInventory(invactive, 0);
			players[playerowner].mo.A_SetInventory(invretrieve, 0);

			if(!retrieve) {
				players[playerowner].mo.A_SetInventory(invduration, 0);
				players[playerowner].mo.A_SetInventory(invty, 0);
			}
		}

		super.OnDestroy();
	}

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

		if (playeringame[playerowner] && players[playerowner].mo) {
			if(!retrieve && players[playerowner].mo.health>0) {
				if(clock==0) {
					let inv = players[playerowner].mo.FindInventory(invty);
					if (inv!=null && inv.Amount>0) {
						if(loopsound) players[playerowner].mo.A_PlaySound(loopsound,loopsoundchannel|CHAN_NOSTOP,2,1);
						players[playerowner].mo.A_GiveInventory(invon, 1);
						players[playerowner].mo.A_TakeInventory(invty, 1);
						inv = players[playerowner].mo.FindInventory(invty);
						if (inv!=null) players[playerowner].mo.A_SetInventory(invduration, inv.Amount); else players[playerowner].mo.A_SetInventory(invduration, 0);
					} else if (inv==null || (inv!=null && inv.Amount<=0)) {
						Destroy();
					}
				}
				clock++;
				if(clock==clocklimit) clock=0;

				let invr = players[playerowner].mo.FindInventory(invretrieve);
				if (invr!=null && invr.Amount>0) {
					retrieve = true;
					Destroy();
				}
			}
			else if(players[playerowner].mo.health<1) {
				Destroy();
			}
		} else {
			Destroy();
		}
	}
}