#include "data/scripts/library/spawn.h"

//Script for objects and spawners
//Sometimes used by simple enemies

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

    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 + Limit + 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 layer(int layer)
{// Set's enemy's layer
    void self = getlocalvar("self");
    changeentityproperty(self, "setlayer", layer);
}

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

    killentity(self); //Suicide!
}

void beidle()
{// Go to IDLE animation!
    void self = getlocalvar("self");

    setidle(self, openborconstant("ANI_IDLE"));
}

void anichange(void Ani)
{ // Animation changer
    void self = getlocalvar("self");

    changeentityproperty(self, "animation", openborconstant(Ani)); //Change the animation
}

void attackE(void Ani, int Num)
{ // Animation changer if entity is absent
    void self = getlocalvar("self");
    void Entity = getentityvar(self, Num);

    if(Entity==NULL()){ // Entity is absent?
      performattack(self, openborconstant(Ani)); //Change the animation
    } else { // Entity is present
      int Health = getentityproperty(Entity, "health");

      if(Health<=0){ // Entity is dead?
        setentityvar(self, 0, NULL());
      }
    }
}

void mapchange(int Map)
{ // Animation changer
    void self = getlocalvar("self");

    changeentityproperty(self, "map", Map);
}

void clearE(int Num)
{ // Clears entity variable
    void self = getlocalvar("self");
    setentityvar(self, Num, NULL());
}

void Mspawn(void Name, float dx, float dy, int DevX, int DevY, int Limit)
{ // Spawn certain entity based on screen edge with same remap & health as spawner's
//Random effect is added
   void self = getlocalvar("self");
   int map = getentityproperty(self,"map");
   int SHealth = getentityproperty(self, "health");
   int y = getentityproperty(self,"a");
   void Spawn;
   int iR = rand()%30;
   int iDX;
   int iDY;

   if(DevX!=0){
     iDX = rand()%DevX;
   } else {
     iDX = 0;
   }

   if(DevY!=0){
     iDY = rand()%DevY;
   } else {
     iDY = 0;
   }

   if (iR > 0){ 
     Spawn = spawn02(Name, Limit-dx+iDX, dy+iDY+y, 0);
     changeentityproperty(Spawn, "direction", 0);
   } else {
     Spawn = spawn02(Name, dx+iDX, dy+iDY+y, 0);
     changeentityproperty(Spawn, "direction", 1);
   }
   changeentityproperty(Spawn, "map", map);
   changeentityproperty(Spawn, "health", SHealth);
}

void MspawnN(void Name, float dx, float dy, int DevX, int DevY)
{ // Spawn certain entity based on its location and respawns if it dies
//Random effect is added
   void self = getlocalvar("self");
   int map = getentityproperty(self,"map");
   int SHealth = getentityproperty(self, "maxhealth");
   void Entity = getentityvar(self, 0);

   if(Entity==NULL()){ // Entity is absent?
     void Spawn;
     int iDX;
     int iDY;

     if(DevX!=0){
       iDX = rand()%DevX;
     } else {
       iDX = 0;
     }

     if(DevY!=0){
       iDY = rand()%DevY;
     } else {
       iDY = 0;
     }

     Spawn = spawn01(Name, dx+iDX, dy+iDY, 0);
     changeentityproperty(Spawn, "map", map);
     changeentityproperty(Spawn, "health", SHealth);
     setentityvar(self, 0, Spawn);

   } else { // Entity is present
     int Health = getentityproperty(Entity, "health");

     if(Health<=0){ // Entity is dead?
       setentityvar(self, 0, NULL());
     }
   }
}

void MspawnR(void Name, float dx, float dz, int DevX, int DevZ, int Limit, int Side)
{ // Spawn certain entity based on screen edge and respawns if it dies
//Random effect is added
   void self = getlocalvar("self");
   int map = getentityproperty(self,"map");
//   void Name = getentityproperty(self,"name");
   int Dir = getentityproperty(self,"direction");
   int SHealth = getentityproperty(self, "health");
   int z = getentityproperty(self,"z");
   void Entity = getentityvar(self, 0);

   if(Entity==NULL()){ // Entity is absent?
     void Spawn;
     int iDX;
     int iDZ;

     if(DevX!=0){
       iDX = rand()%DevX;
     } else {
       iDX = 0;
     }

     if(DevZ!=0){
       iDZ = rand()%DevZ;
     } else {
       iDZ = 0;
     }

     if(Side==0){
       int iR = rand()%30;

       if (iR > 0){ 
         Spawn = spawn02(Name, Limit-dx+iDX, 0, dz+iDZ+z);
         changeentityproperty(Spawn, "direction", 0);
       } else {
         Spawn = spawn02(Name, dx+iDX, 0, dz+iDZ+z);
         changeentityproperty(Spawn, "direction", 1);
       }
     } else {
       int iR = rand()%30;

       if (Dir == 0){ 
         Spawn = spawn02(Name, Limit-dx+iDX, 0, dz+iDZ+z);
         changeentityproperty(Spawn, "direction", 0);
       } else {
         Spawn = spawn02(Name, dx+iDX, 0, dz+iDZ+z);
         changeentityproperty(Spawn, "direction", 1);
       }
     }

//     changeentityproperty(Spawn, "name", Name);
     changeentityproperty(Spawn, "map", map);
     changeentityproperty(Spawn, "health", SHealth);
     setentityvar(self, 0, Spawn);

   } else { // Entity is present
     int Health = getentityproperty(Entity, "health");

     if(Health<=0){ // Entity is dead?
       setentityvar(self, 0, NULL());
     }
   }
}

void Rspawn(void Name, float dx, float dz, int DevX, int DevZ, int Limit)
{ // Spawn certain entity based on screen edge with same remap & health as spawner's
//Random effect is added
//Spawning edge is based on facing direction
//For xz plane
   void self = getlocalvar("self");
   int map = getentityproperty(self,"map");
   int Dir = getentityproperty(self,"direction");
   int SHealth = getentityproperty(self, "health");
   int y = getentityproperty(self,"a");
   void Spawn;
   int iDX;
   int iDZ;

   if(DevX!=0){
     iDX = rand()%DevX;
   } else {
     iDX = 0;
   }

   if(DevZ!=0){
     iDZ = rand()%DevZ;
   } else {
     iDZ = 0;
   }

   if (Dir == 0){ 
     Spawn = spawn02(Name, Limit-dx+iDX, y, dz+iDZ);
     changeentityproperty(Spawn, "direction", 0);
   } else {
     Spawn = spawn02(Name, dx+iDX, y, dz+iDZ);
     changeentityproperty(Spawn, "direction", 1);
   }
   changeentityproperty(Spawn, "map", map);
   changeentityproperty(Spawn, "health", SHealth);
}

void Rspawn2(void Name, float dx, float dy, int DevX, int DevY, int Limit)
{ // Spawn certain entity based on screen edge with same remap & health as spawner's
//Random effect is added
//Spawning edge is based on facing direction
//For xy plane
   void self = getlocalvar("self");
   int map = getentityproperty(self,"map");
   int Dir = getentityproperty(self,"direction");
   int SHealth = getentityproperty(self, "health");
   int y = getentityproperty(self,"a");
   void Spawn;
   int iDX;
   int iDY;

   if(DevX!=0){
     iDX = rand()%DevX;
   } else {
     iDX = 0;
   }

   if(DevY!=0){
     iDY = rand()%DevY;
   } else {
     iDY = 0;
   }

   if (Dir == 0){ 
     Spawn = spawn02(Name, Limit-dx+iDX, dy+iDY+y, 0);
     changeentityproperty(Spawn, "direction", 0);
   } else {
     Spawn = spawn02(Name, dx+iDX, dy+iDY+y, 0);
     changeentityproperty(Spawn, "direction", 1);
   }
   changeentityproperty(Spawn, "map", map);
   changeentityproperty(Spawn, "health", SHealth);
}
