.float grenadetimer;
.float numminion;//init
.float numholo;//init
.float nextregen;

//prototypes
float() crandom;
void() GrenadeExplode;
void() BecomeExplosion;
void() CastLightning; //shambler.qc modified to change ligntning origin 

//Used to toss a weapon, ammo, or armor.
void(float ax) TossBackpack =
{
	local entity	item;
	if (self.flags & FL_OBSERVER) return;
	if (self.impulse==55) ax=0;
	else if (self.impulse==56) ax=1;
	else if (self.impulse==57) ax=32;
	if (ax==0) {
		if (self.weapon==IT_SUPER_SHOTGUN) ax=3;
		if (self.weapon==IT_NAILGUN) ax=4;
		if (self.weapon==IT_SUPER_NAILGUN) ax=5;
		if (self.weapon==IT_GRENADE_LAUNCHER) ax=6;
		if (self.weapon==IT_ROCKET_LAUNCHER) ax=7;
		if (self.weapon==IT_LIGHTNING) ax=8;
	}
	if (ax==1) {
		if (self.weapon==IT_SHOTGUN) ax=16;
		if (self.weapon==IT_SUPER_SHOTGUN) ax=16;
		if (self.weapon==IT_NAILGUN) ax=17;
		if (self.weapon==IT_SUPER_NAILGUN) ax=17;
		if (self.weapon==IT_GRENADE_LAUNCHER) ax=18;
		if (self.weapon==IT_ROCKET_LAUNCHER) ax=18;
		if (self.weapon==IT_LIGHTNING) ax=19;
	}
	if (ax==2) {
		if (!(self.ammo_shells+self.ammo_nails+self.ammo_rockets+self.ammo_cells))
			return;	// nothing in it
		item = spawn();
		item.owner = self;
		item.items = 0;
		setmodel (item, "progs/backpack.mdl");
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = BackpackTouch;
		item.ammo_shells = self.ammo_shells;
		item.ammo_nails = self.ammo_nails;
		item.ammo_rockets = self.ammo_rockets;
		item.ammo_cells = self.ammo_cells;
		item.classname="backpack";
		self.ammo_shells = 0;
		self.ammo_nails = 0;
		self.ammo_rockets = 0;
		self.ammo_cells = 0;	
	} else if (ax==3) {
		if (!(self.items & IT_SUPER_SHOTGUN)) return;
		item = spawn();
		item.owner = self;
		item.items = IT_SUPER_SHOTGUN;
		self.items = self.items - IT_SUPER_SHOTGUN;
		if (self.weapon==IT_SUPER_SHOTGUN) self.weapon=W_BestWeapon();
		setmodel (item, "progs/g_shot.mdl");
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = BackpackTouch;
		if (self.ammo_shells < 10)
		item.ammo_shells = self.ammo_shells;
		else	item.ammo_shells = 10;
		self.ammo_shells=self.ammo_shells-item.ammo_shells;
		item.classname="weapon_supershotgun";
	} else if (ax==4) {
		if (!(self.items & IT_NAILGUN)) return;
		item = spawn();
		item.owner = self;
		item.items = IT_NAILGUN;
		self.items = self.items - IT_NAILGUN;
		if (self.weapon==IT_NAILGUN) self.weapon=W_BestWeapon();
		setmodel (item, "progs/g_nail.mdl");
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = BackpackTouch;
		if (self.ammo_nails < 20)
			item.ammo_nails = self.ammo_nails;
		else	item.ammo_nails = 20;
		self.ammo_nails=self.ammo_nails-item.ammo_nails;
		item.classname="weapon_nailgun";
	} else if (ax==5) {
		if (!(self.items & IT_SUPER_NAILGUN)) return;
		item = spawn();
		item.owner = self;
		item.items = IT_SUPER_NAILGUN;
		self.items = self.items - IT_SUPER_NAILGUN;
		if (self.weapon==IT_SUPER_NAILGUN) self.weapon=W_BestWeapon();
		setmodel (item, "progs/g_nail2.mdl");
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = BackpackTouch;
		if (self.ammo_nails < 20) item.ammo_nails = self.ammo_nails;
		else	item.ammo_nails = 20;
		self.ammo_nails=self.ammo_nails-item.ammo_nails;
		item.classname="weapon_supernailgun";
	} else if (ax==6) {
		if (!(self.items & IT_GRENADE_LAUNCHER)) return;
		item = spawn();
		item.owner = self;
		item.items = IT_GRENADE_LAUNCHER;
		self.items = self.items - IT_GRENADE_LAUNCHER;
		if (self.weapon==IT_GRENADE_LAUNCHER) self.weapon=W_BestWeapon();
		setmodel (item, "progs/g_rock.mdl");
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = BackpackTouch;
		if (self.ammo_rockets < 10) item.ammo_rockets = self.ammo_rockets;
		else	item.ammo_rockets = 10;
		self.ammo_rockets=self.ammo_rockets-item.ammo_rockets;
		item.classname="weapon_grenadelauncher";
	} else if (ax==7) {
		if (!(self.items & IT_ROCKET_LAUNCHER)) return;
		item = spawn();
		item.owner = self;
		item.items = IT_ROCKET_LAUNCHER;
		self.items = self.items - IT_ROCKET_LAUNCHER;
		if (self.weapon==IT_ROCKET_LAUNCHER) self.weapon=W_BestWeapon();
		setmodel (item, "progs/g_rock2.mdl");
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = BackpackTouch;
		if (self.ammo_rockets < 10) item.ammo_rockets = self.ammo_rockets;
		else	item.ammo_rockets = 10;
		self.ammo_rockets=self.ammo_rockets-item.ammo_rockets;
		item.classname="weapon_rocketlauncher";
	} else if (ax==8) {
		if (!(self.items & IT_LIGHTNING)) return;
		item = spawn();
		item.owner = self;
		item.items = IT_LIGHTNING;
		self.items = self.items - IT_LIGHTNING;
		if (self.weapon==IT_LIGHTNING) self.weapon=W_BestWeapon();
		setmodel (item, "progs/g_light.mdl");
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = BackpackTouch;
		if (self.ammo_cells < 10) item.ammo_cells = self.ammo_cells;
		else	item.ammo_cells = 10;
		self.ammo_cells=self.ammo_cells-item.ammo_cells;
		item.classname="weapon_lightning";
	} else if (ax==16) {
		//shells
		if (self.ammo_shells==0) return;
		item = spawn();
		if (self.ammo_shells < 10) item.ammo_shells = self.ammo_shells;
		else	item.ammo_shells = 10;
		item.owner = self;
		self.ammo_shells=self.ammo_shells-item.ammo_shells;
		setmodel (item, "maps/b_shell1.bsp");
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = BackpackTouch;
		item.classname = "ammo_shells";
	} else if (ax==17) {
		//nails
		if (self.ammo_nails==0) return;
		item = spawn();
		if (self.ammo_nails < 20) item.ammo_nails = self.ammo_nails;
		else	item.ammo_nails = 20;
		item.owner = self;
		self.ammo_nails=self.ammo_nails-item.ammo_nails;
		setmodel (item, "maps/b_nail1.bsp");
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = BackpackTouch;
		item.classname = "ammo_nails";
	} else if (ax==18) {
		//rockets
		if (self.ammo_rockets==0) return;
		item = spawn();
		if (self.ammo_rockets < 10) item.ammo_rockets=self.ammo_rockets;
		else	item.ammo_rockets = 10;
		item.owner = self;
		self.ammo_rockets=self.ammo_rockets-item.ammo_rockets;
		setmodel (item, "maps/b_rock1.bsp");
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = BackpackTouch;
		item.classname = "ammo_rockets";
	} else if (ax==19) {
		//cells
		if (self.ammo_cells==0) return;
		item = spawn();
		if (self.ammo_cells < 10) item.ammo_cells = self.ammo_cells;
		else	item.ammo_cells = 10;
		item.owner = self;
		self.ammo_cells=self.ammo_cells-item.ammo_cells;
		setmodel (item, "maps/b_batt1.bsp");
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = BackpackTouch;
		item.classname = "ammo_cells";
	} else if (ax==32) {
		if (self.armorvalue<1) return;
		sound (self, CHAN_WEAPON, "weapons/grenade.wav", 1, ATTN_NORM);
		item = spawn();
		item.owner = self;
		item.armortype=self.armortype;
		item.armorvalue=self.armorvalue;
		self.armortype=0;
		self.armorvalue=0;
		setmodel (item, "progs/armor.mdl");
		if (item.armortype==0.3) {
			item.skin=0;
			self.items=self.items-IT_ARMOR1;
			item.items=IT_ARMOR1;
			item.classname="item_armor1";
		} else if (item.armortype==0.6) {
			item.skin=1;
			self.items=self.items-IT_ARMOR2;
			item.items=IT_ARMOR2;
			item.classname="item_armor2";
		} else if (item.armortype==0.8) {
			item.skin=2;
			self.items=self.items-IT_ARMOR3;
			item.items=IT_ARMOR3;
			item.classname="item_armorInv";
		}
		setsize (item, '-16 -16 0', '16 16 56');
		item.touch = armor_touch;
	} else return;
	sound (self, CHAN_WEAPON, "weapons/grenade.wav", 1, ATTN_NORM);
	W_SetCurrentAmmo();	//Draw new weapon on screen (in hand)
// set missile speed	
	makevectors (self.v_angle);
	if (self.v_angle_x)
		item.velocity = v_forward*600 + v_up * 200 +
		crandom()*v_right*10 + crandom()*v_up*10;
	else
	{
		item.velocity = aim(self, 10000);
		item.velocity = item.velocity * 600;
	}
	item.origin = self.origin + (item.velocity * 0.0); 
	item.movetype = MOVETYPE_TOSS;
//	so people cant pick it up immediately
	item.nextthink = time + 0.25;
	item.think = SUB_MakePickup;
};

//Detonate all pipebombs
void() DetPipeBombs =
{
        local entity    head;

        head = findradius (self.origin, 32000);
        while(head)
        {
                if((head.classname == "pipebomb") && (head.owner == self))
                        head.nextthink = time;
                head = head.chain;
        }
};

//bouncing pipe bombs
void() PipeBombTouch =
{
	if (other == self.owner)
		return;		// don't explode on owner
	sound (self, CHAN_WEAPON, "weapons/bounce.wav", 1, ATTN_NORM);	// bounce sound
	if (self.velocity == '0 0 0')
		self.avelocity = '0 0 0';
};

//fire a pipe bomb
void() W_FirePipeBomb =
{
	local	entity missile, mpuff;
	if(self.ammo_rockets < 1)	// have to have ammo
	  return;
	self.currentammo = self.ammo_rockets = self.ammo_rockets - 1;
	missile = spawn ();
	missile.owner = self;
	missile.movetype = MOVETYPE_BOUNCE;
	missile.solid = SOLID_BBOX;
	missile.classname = "pipebomb";
	makevectors (self.v_angle);
	if (self.v_angle_x) missile.velocity = 
	v_forward*600 + v_up * 200 + crandom()*v_right*10 + crandom()*v_up*10;
	else {
		missile.velocity = aim(self, 10000);
		missile.velocity = missile.velocity * 600;
		missile.velocity_z = 200;
	}
	missile.avelocity = '300 300 300';
	missile.angles = vectoangles(missile.velocity);
	missile.touch = PipeBombTouch;
	//pipe bombs explode after 5 mins by default
	missile.nextthink=time+300;
	missile.think = GrenadeExplode;
	setmodel (missile, "progs/grenade.mdl");
	setsize (missile, '0 0 0', '0 0 0');		
	setorigin (missile, self.origin);
};
// end pipebombs


entity() LightFindTarget =   //this routine based on function by VHOLD
{
	local entity head, selected;
	local float dist;
	dist = 100000;
	selected = world;
	head = findradius(self.origin, 100000);
	while(head)
    {                         //(head.team > 0) &&
        if (((coop) && (head.classname != "player")) || (!coop))
        if (((teamplay) &&  (head.team != self.owner.team))
             || (!teamplay))
        if ((head.health > 1) && (head != self) && (head != self.owner))
         {
			traceline(self.origin,head.origin,TRUE,self);
			if ( (trace_fraction == 1) && (vlen(head.origin - self.origin) < dist) )
			{
				selected = head;
				dist = vlen(head.origin - self.origin);
			}
         }    
       head = head.chain;
	}
	return selected;
};

//Minion AI
void() MinionThink =
{
    local vector dir, vtemp;
    local string s;
    if (self.owner.flags & FL_OBSERVER) self.ammo_cells=0;
    self.enemy = self.owner;
    if (self.enemy != world) 
	{
		vtemp = self.enemy.origin + '0 0 10';
        dir = normalize(vtemp - self.origin + '0 0 25');
        self.velocity = dir * 200;
		self.angles = vectoangles(self.velocity);
	}
    self.enemy=LightFindTarget();

    if (self.enemy)
    {
     if (self.ammo_cells > 1) {
	self.currentammo = self.ammo_cells =self.ammo_cells - 2; 
	CastLightning();
     }
     else
       { self.owner.numminion = 0; remove(self);return;}//self.think=SUB_Remove; self.nextthink=time; return;}
    }
    self.nextthink = time + 0.5;
    self.think=MinionThink;
};

//Create the minion
void(vector vec) Minion = 
{   sound (self, CHAN_WEAPON, "items/damage3.wav", 1, ATTN_NORM);
   
	vec = normalize(vec);
	newmis = spawn();
	newmis.owner = self;
  newmis.classname = "electricminion";
  newmis.movetype = MOVETYPE_NOCLIP;
  newmis.solid = SOLID_NOT;
	newmis.effects = EF_DIMLIGHT;
  newmis.ammo_cells = 25;
  newmis.items = IT_LIGHTNING;
  setmodel (newmis, "progs/quaddama.mdl");
  setsize (newmis, VEC_ORIGIN, VEC_ORIGIN);  
  setorigin (newmis, self.origin + '0 0 25');// v_forward*8 + '0 0 16');
  newmis.velocity = aim(self, 1000);
  newmis.velocity = newmis.velocity * 1;
  newmis.angles = vectoangles(newmis.velocity);
	newmis.enemy = world;
  newmis.nextthink = time + 2;
  newmis.think = MinionThink;

};

//Fire the minion with your gun
void() W_FireMinion =
{   if ((self.ammo_cells < 40) || self.numminion) return;

		sound (self, CHAN_AUTO, "weapons/lstart.wav", 1, ATTN_NORM);
    self.effects = self.effects | EF_MUZZLEFLASH;
    self.currentammo = self.ammo_cells = self.ammo_cells - 40;
    self.numminion = 1;
    Minion(self.enemy.origin - self.origin);
};

//after a minute, hologram disappears
void() Holothink = { 
  self.owner.numholo = self.owner.numholo - 1;
  sound (self, CHAN_WEAPON, "enforcer/enfire.wav", 1, ATTN_NORM);
  remove(self);
   
};

//Create a hologram
void(vector vec) Hologram = {    
	if (self.flags & FL_OBSERVER) return;
	sound (self, CHAN_WEAPON, "enforcer/enfire.wav", 1, ATTN_NORM); 
	vec = normalize(vec);
	
    newmis = spawn();
    newmis.owner = self;
    newmis.classname = "hologram";
    newmis.solid = SOLID_NOT;
    setmodel (newmis, self.model);
    newmis.skin=self.skin;
    newmis.frame=self.frame;
    newmis.colormap=self.colormap;
    newmis.modelindex=self.modelindex;

    setsize (newmis, VEC_ORIGIN, VEC_ORIGIN);
        
    setorigin (newmis, self.origin);
    newmis.velocity = aim(self, 1000);
    newmis.velocity = newmis.velocity * 1;
    newmis.angles = vectoangles(newmis.velocity);
    newmis.nextthink = time + 60;
    newmis.think = Holothink;
};

//Fire the Hologram
void() W_FireHolo =
{ 
    if ((self.ammo_cells < 12) || (self.numholo > 2)) return;

    self.effects = self.effects | EF_MUZZLEFLASH;
    self.currentammo = self.ammo_cells = self.ammo_cells - 12;
    self.numholo = self.numholo + 1;
    Hologram(self.enemy.origin - self.origin);
};


//Become an Observer
void() Observer =
{
	self.items = self.items | (IT_INVULNERABILITY + IT_INVISIBILITY); 
	self.solid = SOLID_NOT;
	centerprint(self,"OBSERVER MODE"); 
 	self.flags = self.flags | FL_OBSERVER;   
	self.movetype=MOVETYPE_NOCLIP;
	self.frags=self.frags - 1;
	// Detonate Pipe Bombs so person cant use them later
	DetPipeBombs();
	bprint("\n");
	bprint(self.netname); 
	bprint(" ");
	bprint("is now an observer.");   
	bprint("\n");
	setmodel(self,"");
};              // END OBSERVER FUNCTION

//set the grenade timer based on impulses 53 and 54
void ()SetGrenadeTimer = {
	local string s;
	local float iv; 
	if (self.impulse==53){
		if (self.grenadetimer==0.5) iv=0;
		else iv= -0.5;
		if (self.grenadetimer >5) iv = -1;
		if (self.grenadetimer >20) iv= -5;
		if (self.grenadetimer >60) iv= -15;
		if (self.grenadetimer >120) iv= -30;
	} else {
		iv=0.5;
		if (self.grenadetimer>=5) iv=1;
		if (self.grenadetimer>=15) iv=5;
		if (self.grenadetimer >=60) iv=15;
		if (self.grenadetimer >=120) iv=30;
		if (self.grenadetimer==300) iv=0;
	}
	self.grenadetimer=self.grenadetimer+iv;
	if (!self.grenadetimer) self.grenadetimer=0.5;
	sprint(self,"Grenade Timer: ");
	s=ftos(self.grenadetimer);
	sprint(self,s);
	sprint(self,"\n");
};

//If player has lightning gun and less than 50 cells, the gain a cell
void ()CheckPowerRegen = {
	if(self.nextregen <= time){
		if((self.items & IT_LIGHTNING) && (self.ammo_cells < 50)){
			self.ammo_cells=self.ammo_cells+1;
			if (self.weapon==IT_LIGHTNING) self.currentammo=self.ammo_cells;
		}
		self.nextregen=time+5;
	}	
};
