
void () radar_precache = {

   precache_model2 ("progs/radar.mdl");
   precache_model2 ("progs/mid.spr");
   precache_model2 ("progs/down.spr");
   precache_model2 ("progs/up.spr");
   precache_sound2 ("radar/radar.wav");

};
float DOT_CENTER_X    = -25.200;
float DOT_CENTER_Y    = 13.700;
float DOT_MAX    = 7.000;
float PI    = 3.142;
float (entity org, entity targ) GetDirection = {

   local float organg;
   local float targang;
   local float dir;

   makevectors (org.v_angle);
   organg = vectoyaw (v_forward);
   targang = vectoyaw (normalize ((targ.origin - org.origin)));
   dir = anglemod ((organg - targang));
   return ( dir );

};
float /* Warning: Could not determine return type */ (float n) sq = {

   return ( (n * n) );

};
float /* Warning: Could not determine return type */ (float deg) sine = {

   local float x;
   local float x2;
   local float ans;
   local float neg;

   neg = FALSE;
   if ( (deg > 180.000) ) {

      neg = TRUE;
      deg = (360.000 - deg);

   }
   x = ((PI / 180.000) * deg);
   x2 = sq (x);
   ans = (x * (1.000 - ((x2 * (1.000 - ((x2 * (1.000 - (x2 / (6.000 * 7.000)))) / (4.000 * 5.000)))) / (2.000 * 3.000))));
   if ( !neg ) {

      return ( ans );

   } else {

      return ( (0.000 - ans) );

   }

};
float /* Warning: Could not determine return type */ (float deg) cosine = {

   if ( (deg <= 90.000) ) {

      return ( sine ((90.000 - deg)) );

   } else {

      return ( (0.000 - sine ((deg - 90.000))) );

   }

};
vector (float dir, entity org, entity targ) GetDotPos = {

   local vector pos;
   local float xpos;
   local float ypos;
   local float xdist;
   local float ydist;
   local float dist;

   dist = vlen ((targ.origin - org.origin));
   xdist = (sine (dir) * dist);
   ydist = (cosine (dir) * dist);
   xpos = (DOT_CENTER_X + ((xdist / 1000.000) * DOT_MAX));
   ypos = (DOT_CENTER_Y + ((ydist / 1000.000) * DOT_MAX));
   makevectors (self.owner.v_angle);
   pos = (((v_forward * 37.000) + (v_right * xpos)) + (v_up * ypos));
   return ( pos );

};

void () DotThink = {

   local vector pos;
   local float dist;
   local float dir;

   if ( !self.enemy ) {

      remove (self);
      return ;

   }
   if ( (((self.enemy.health <= 0.000) || (self.enemy.deadflag == DEAD_DEAD)) || (self.owner.radar_on == FALSE)) ) {

      self.enemy.dot_flag = (self.enemy.dot_flag - self.radar_id);
      remove (self);
      return ;

   }
   dist = vlen ((self.enemy.origin - self.owner.origin));
   if ( (dist > 1000.000) ) {

      self.enemy.dot_flag = (self.enemy.dot_flag - self.radar_id);
      remove (self);
      return ;

   }
   pos = (self.enemy.origin - self.owner.origin);
   if ( (pos_z > 80.000) ) {

      setmodel (self,"progs/up.spr");

   } else {

      if ( (pos_z < -80.000) ) {

         setmodel (self,"progs/down.spr");

      } else {

         setmodel (self,"progs/mid.spr");

      }

   }
   dir = GetDirection (self.owner,self.enemy);
   pos = GetDotPos (dir,self.owner,self.enemy);
   self.think = DotThink;
   self.nextthink = (time + 0.030);
   makevectors (self.owner.v_angle);
   setorigin (self,(self.owner.origin + pos));

};

void (entity dot_owner, entity dot_enemy, float id) CreateDot = {

   local entity radardot;

   radardot = spawn ();
   radardot.owner = dot_owner;
   radardot.enemy = dot_enemy;
   radardot.radar_id = id;
   dot_enemy.dot_flag = (dot_enemy.dot_flag | self.radar_id);
   radardot.touch = SUB_Null;
   radardot.solid = SOLID_NOT;
   radardot.movetype = MOVETYPE_NONE;
   radardot.angles = dot_owner.angles;
   radardot.classname = "radardot";
   radardot.think = DotThink;
   radardot.nextthink = (time + 0.030);
   setmodel (radardot,"progs/mid.spr");
   setsize (radardot,'0.000 0.000 0.000','0.000 0.000 0.000');
   setorigin (radardot,'0.000 0.000 0.000');

};

void () SearchNearBy = {

   local entity head;

   head = findradius (self.owner.origin,1000.000);
   while ( head ) {

      if ( (((head != self.owner) && (head.classname != "corpse")) && ((((head.flags & FL_CLIENT) || (head.flags & FL_MONSTER)) || (head.classname == "cujo")) || (head.classname == "zeus"))) ) {

         if ( !(head.dot_flag & self.radar_id) ) {

            CreateDot (self.owner,head,self.radar_id);

         }

      }
      head = head.chain;

   }

};

void () RadarThink = {

   if ( !self.owner ) {

      total_radar = (total_radar - 1.000);
      current_usedflag = (current_usedflag - self.radar_id);
      remove (self);
      return ;

   }
   if ( (((self.owner.health <= 0.000) || (self.owner.deadflag != DEAD_NO)) || !(self.owner.flags & FL_CLIENT)) ) {

      self.owner.radar_on = FALSE;
      total_radar = (total_radar - 1.000);
      current_usedflag = (current_usedflag - self.radar_id);
      remove (self);
      return ;

   }
   if ( (self.owner.classname != "player") ) {

      setmodel (self,"");
      self.owner.radar_on = FALSE;

   } else {

      if ( (self.owner.classname == "player") ) {

         if ( (self.model != "progs/radar.mdl") ) {

            setmodel (self,"progs/radar.mdl");

         }
         self.owner.radar_on = TRUE;

      }

   }
   if ( (self.attack_finished < time) ) {

      self.attack_finished = (time + 1.500);
      SearchNearBy ();
      sound (self.owner,CHAN_WEAPON,"radar/radar.wav",0.500,ATTN_NORM);

   }
   self.angles = self.owner.angles;
   self.think = RadarThink;
   self.nextthink = (time + 0.030);
   self.owner.attack_finished = (time + 0.200);
   makevectors (self.owner.v_angle);
   setorigin (self,(((self.owner.origin + (v_forward * 38.000)) + (v_right * -26.000)) + (v_up * 13.000)));

};
float () ObtainRadarID = {

   local entity head;
   local float temp;

   temp = current_usedflag;
   while ( (temp & current_usedflag) ) {

      temp = current_radarflag;
      current_radarflag = (current_radarflag * 2.000);
      if ( (current_radarflag > 4194304.000) ) {

         current_radarflag = 1.000;

      }

   }
   current_usedflag = (current_usedflag | temp);
   return ( temp );

};

void () ToggleRadar = {

   local entity radar;

   if ( (total_radar >= 20.000) ) {

      sprint (self,"Too many active radar, aborting\n");
      return ;

   }
   if ( self.radar_on ) {

      self.radar_on = FALSE;
      total_radar = (total_radar - 1.000);
      current_usedflag = (current_usedflag - self.myradar.radar_id);
      remove (self.myradar);
      return ;

   }
   radar = spawn ();
   radar.owner = self;
   self.myradar = radar;
   self.radar_on = TRUE;
   radar.touch = SUB_Null;
   radar.frame = 0.000;
   radar.solid = SOLID_BBOX;
   radar.flags = 0.000;
   radar.movetype = MOVETYPE_FLYMISSILE;
   radar.velocity = '0.000 0.000 0.000';
   radar.angles = self.angles;
   radar.classname = "radar";
   total_radar = (total_radar + 1.000);
   radar.radar_id = ObtainRadarID ();
   radar.think = RadarThink;
   radar.nextthink = (time + 0.030);
   radar.attack_finished = (time + 0.100);
   self.attack_finished = (time + 0.200);
   setmodel (radar,"progs/radar.mdl");
   setsize (radar,'0.000 0.000 0.000','0.000 0.000 0.000');
   setorigin (radar,'0.000 0.000 0.000');

};
