//Script for player's and enemy's projectiles
//Also used by entities with simple effects

void offscreenkill(float dx, int E)
{// Check position relative to screen. If enemy is offscreen, suicide!
//  dx : Distance to screen edge
//  E  : Edge selection, 0 = left, 1 = right

    void self = getlocalvar("self");
    int x = getentityproperty(self,"x"); //Get character's x coordinate
    int XPos = openborvariant("xpos"); //Get screen edge's position

    if( (x > XPos + 320 + dx) && (E == 1) ){ // Offscreen to the right?
      killentity(self); //Suicide!
    } else if( (x < XPos - dx) && (E == 0) ){ // Offscreen to the left?
      killentity(self); //Suicide!
    }
}

void platform(int Flag)
{// Turns subject's platform status
    void self = getlocalvar("self");
    changeentityproperty(self, "Subject_to_Platform", Flag);
}

void dirspeed()
{ // Match facing direction with x velocity
    void self = getlocalvar("self");
    float Vx = getentityproperty(self, "xdir");

    if (Vx < 0){
      changeentityproperty(self, "direction", 0); //Face left!
    } else if (Vx > 0){
      changeentityproperty(self, "direction", 1); //Face right!
    }
}

void groundkill()
{// Check altitude. If projectile is on ground, suicide!
    void self = getlocalvar("self");
    int x = getentityproperty(self,"x");
    int z = getentityproperty(self,"z");
    int y = getentityproperty(self,"a");

    int H = checkhole(x,z,y-100);

    if( y <= 1 && H != 1){ // On ground but not on hole?
      killentity(self); //Suicide!
    }
}

void groundchange(void Ani)
{// Check altitude. If projectile is on ground, change animation!
    void self = getlocalvar("self");
    int x = getentityproperty(self,"x");
    int z = getentityproperty(self,"z");
    int y = getentityproperty(self,"a");

    int H = checkhole(x,z,y-100);

    if( y <= 1 && H != 1){ // On ground but not on hole?
      changeentityproperty(self, "animation", openborconstant(Ani));
    }
}

void basechange(void Ani)
{// Check altitude. If bomb is on base, change animation!

    void self = getlocalvar("self");
    int y = getentityproperty(self,"a"); //Get bomb's altitude
    int b = getentityproperty(self, "base"); //Get base

    if( y <= 1+b ){ // On base?
      performattack(self, openborconstant(Ani)); //Change the animation
    }
}

void suicide()
{ // Suicide!!
    void self = getlocalvar("self");

    killentity(self); //Suicide!
}

void degravity(int Ratio)
{// Changes antigravity effect
    void self = getlocalvar("self");
    changeentityproperty(self, "antigravity", Ratio);
}

void givescore(int Add)
{// Gives parent score
    void self = getlocalvar("self");
    void target = getentityproperty(self, "opponent");

    if(target!=NULL()){
      void Def = getentityvar(target, 7);

      if(Def!=1){
        void Parent = getentityproperty(self,"parent");
        int iPIndex = getentityproperty(Parent,"playerindex");
        int PScore = getplayerproperty(iPIndex, "score");

        changeplayerproperty(iPIndex, "score", PScore+Add);
      }
    }
}

void dasher( float Vx, float Vy)
{// Dash with desired speed!
    void self = getlocalvar("self");
    int dir = getentityproperty(self,"direction");

    if(dir==0){ // Facing left?
      Vx = -Vx ;
    }

    changeentityproperty(self, "velocity", Vx, 0, Vy); //Move!
}

void stop()
{// Stop enemy's movement!
    void self = getlocalvar("self");
    changeentityproperty(self, "velocity", 0, 0, 0); //Stop moving!
}

void spawn01(void vName, float fX, float fY, float fZ)
{
	//spawn01 (Generic spawner)
	//Damon Vaughn Caskey
	//07/06/2007
	//
	//Spawns entity next to caller.
	//
	//vName: Model name of entity to be spawned in.
	//fX: X location adjustment.
	//fY: Y location adjustment.
      //fZ: Z location adjustment.

	void self = getlocalvar("self"); //Get calling entity.
	void vSpawn; //Spawn object.
	int  iDirection = getentityproperty(self, "direction");

	clearspawnentry(); //Clear current spawn entry.
      setspawnentry("name", vName); //Acquire spawn entity by name.

	if (iDirection == 0){ //Is entity facing left?                  
          fX = -fX; //Reverse X direction to match facing.
	}

      fX = fX + getentityproperty(self, "x"); //Get X location and add adjustment.
      fY = fY + getentityproperty(self, "a"); //Get Y location and add adjustment.
      fZ = fZ + getentityproperty(self, "z"); //Get Z location and add adjustment.
	
	vSpawn = spawn(); //Spawn in entity.

	changeentityproperty(vSpawn, "position", fX, fZ, fY); //Set spawn location.
	changeentityproperty(vSpawn, "direction", iDirection); //Set direction.
    
	return vSpawn; //Return spawn.
}

void shooter(void Shot, float dx, float dy, float Vx, float Vy)
{ // Shooting special projectile with speed control
   void self = getlocalvar("self");
   int Direction = getentityproperty(self, "direction");
   void vShot;

   if (Direction == 0){ //Is entity facing left?                  
      Vx = -Vx; //Reverse Vx direction to match facing
   }

   vShot = spawn01(Shot, dx, dy, 0);
   changeentityproperty(vShot, "velocity", Vx, 0, Vy);

   return vShot;
}

void shooterX(void Shot, float dx, float dy, float Vx, float Vy, int Dir)
{ // Shooting special projectile with speed and direction
//Projectile's direction is adjusted to current direction and current remap

   void self = getlocalvar("self");
   int Direction = getentityproperty(self, "direction");
   int Map = getentityproperty(self, "map");
   void vShot;
   int JDir;

   if (Direction == 0){ //Is entity facing left?                  
     Vx = -Vx; //Reverse Vx direction to match facing
   }

   if (Dir == 0){                 
     JDir = Direction;
   } else if (Dir == 1){
     if (Direction == 0){                 
       JDir = 1;
     } else { JDir = 0; }
   }

   vShot = shooter(Shot, dx, dy, Vx, Vy);
   changeentityproperty(vShot, "direction", JDir);
   changeentityproperty(vShot, "map", Map);
}

void tosser(void Bomb, float dx, float dy, float Vx, float Vy)
{ // Tossing special bomb with speed control
   void self = getlocalvar("self");
   int Direction = getentityproperty(self, "direction");
   void vShot;

   if (Direction == 0){ //Is entity facing left?                  
      Vx = -Vx; //Reverse Vx direction to match facing
   }

   vShot = spawn01(Bomb, dx, dy, 0);
   tossentity(vShot, Vy, Vx, 0);
   return vShot;
}

void revolver( float Ax, float Ay, int C )
{// Revolve with speed change
// For none type
    void self = getlocalvar("self");
    float Vx = getentityproperty(self,"xdir");
    float Vy = getentityproperty(self,"tossv");
    float Nx;
    float Ny;

    if( C==0 ){
      if(Vx > 0 && Vy == 0){
        Nx = Vx + Ax;
        Ny = 2*Vx + Ay;
      } else if(Vx > 0 && Vy > 0){
        Nx = 0;
        Ny = Vy + Ay;
      } else if(Vx == 0 && Vy > 0){
        Nx = -0.5*Vy - Ax;
        Ny = Vy + Ay;
      } else if(Vx < 0 && Vy > 0){
        Nx = Vx - Ax;
        Ny = 0;
      } else if(Vx < 0 && Vy == 0){
        Nx = Vx - Ax;
        Ny = 2*Vx - Ay;
      } else if(Vx < 0 && Vy < 0){
        Nx = 0;
        Ny = Vy - Ay;
      } else if(Vx == 0 && Vy < 0){
        Nx = -0.5*Vy + Ax;
        Ny = Vy - Ay;
      } else if(Vx > 0 && Vy < 0){
        Nx = Vx + Ax;
        Ny = 0;
      }
    } else if( C==1 ){
      if(Vx > 0 && Vy == 0){
        Nx = Vx + Ax;
        Ny = -2*Vx - Ay;
      } else if(Vx > 0 && Vy < 0){
        Nx = 0;
        Ny = Vy - Ay;
      } else if(Vx == 0 && Vy < 0){
        Nx = 0.5*Vy - Ax;
        Ny = Vy - Ay;
      } else if(Vx < 0 && Vy < 0){
        Nx = Vx - Ax;
        Ny = 0;
      } else if(Vx < 0 && Vy == 0){
        Nx = Vx - Ax;
        Ny = -2*Vx + Ay;
      } else if(Vx < 0 && Vy > 0){
        Nx = 0;
        Ny = Vy + Ay;
      } else if(Vx == 0 && Vy > 0){
        Nx = 0.5*Vy + Ax;
        Ny = Vy + Ay;
      } else if(Vx > 0 && Vy > 0){
        Nx = Vx + Ax;
        Ny = 0;
      }
    }

    changeentityproperty(self, "velocity", Nx, 0, Ny); //Revolve!
}

void revolverE( float Ax, float Ay, int C )
{// Revolve with speed change
// For enemy type
    void self = getlocalvar("self");
    float Vx = getentityproperty(self,"xdir");
    float Vy = getentityproperty(self,"tossv");
    float Nx;
    float Ny;

    if( C==0 ){
      if(Vx > 0 && Vy == 0){
        Nx = Vx + Ax;
        Ny = Vx + Ay;
      } else if(Vx > 0 && Vy > 0){
        Nx = 0;
        Ny = Vy + Ay;
      } else if(Vx == 0 && Vy > 0){
        Nx = -Vy - Ax;
        Ny = Vy + Ay;
      } else if(Vx < 0 && Vy > 0){
        Nx = Vx - Ax;
        Ny = 0;
      } else if(Vx < 0 && Vy == 0){
        Nx = Vx - Ax;
        Ny = Vx - Ay;
      } else if(Vx < 0 && Vy < 0){
        Nx = 0;
        Ny = Vy - Ay;
      } else if(Vx == 0 && Vy < 0){
        Nx = -Vy + Ax;
        Ny = Vy - Ay;
      } else if(Vx > 0 && Vy < 0){
        Nx = Vx + Ax;
        Ny = 0;
      }
    } else if( C==1 ){
      if(Vx > 0 && Vy == 0){
        Nx = Vx + Ax;
        Ny = -Vx - Ay;
      } else if(Vx > 0 && Vy < 0){
        Nx = 0;
        Ny = Vy - Ay;
      } else if(Vx == 0 && Vy < 0){
        Nx = Vy - Ax;
        Ny = Vy - Ay;
      } else if(Vx < 0 && Vy < 0){
        Nx = Vx - Ax;
        Ny = 0;
      } else if(Vx < 0 && Vy == 0){
        Nx = Vx - Ax;
        Ny = -Vx + Ay;
      } else if(Vx < 0 && Vy > 0){
        Nx = 0;
        Ny = Vy + Ay;
      } else if(Vx == 0 && Vy > 0){
        Nx = Vy + Ax;
        Ny = Vy + Ay;
      } else if(Vx > 0 && Vy > 0){
        Nx = Vx + Ax;
        Ny = 0;
      }
    }

    changeentityproperty(self, "velocity", Nx, 0, Ny); //Revolve!
}
