#include <fstream>
#include "CMap.hpp"
#include "CMain.hpp"
#include "CPlayer.hpp"
#include "CNpc.hpp"
#include "Quests.hpp"

CNpc::CNpc()
{
    dir = 1;
    frame = 0;
    id = 0;
    
    steps = 0;
    done = false;
    
    xoff=yoff=moving=0;
    target = 0;
    
    hp=mana=gold=level=ac=exp=0;
    range = 1;
    dam = 1; // prevents divide by 0 errors
    name = "";
}


CNpcInfo::CNpcInfo()
{
  id = 1;
  name = "";
  text = "";
  yesno = "";
  yestext = "";
  notext = "";

  quest_idnum = 0;
  set_quest_idnum = 0;
  quest_text = "";
}


void CNpc::Move(CMap &map, int dir)
{
  if(moving == 0)
 	switch(dir)
	{
	case NORTH:
	//	facing = NORTH;
		if( !map.Collision(x,y-1) && !map.Collision(x+16-1,y-1) && !map.CheckPlayer(x, y-1) && !map.CheckNpcs(x, y-16))
		{
         yoff = 16;
         moving = 1;
        }
		break;
	case SOUTH:
	//	facing = SOUTH;
		if(!map.Collision(x,y+16) && !map.Collision(x+16-1,y+16) && !map.CheckPlayer(x, y+1) && !map.CheckNpcs(x, y+16) )
		{
         yoff = -16;
         moving = 1;
        }
		break;
	case EAST:
		//facing = EAST;
		if( !map.Collision(x+16, y) && !map.Collision(x+16, y+16-1) && !map.CheckPlayer(x+1, y) && !map.CheckNpcs(x+16, y))
		{
         xoff = -16;
         moving = 1;
        }
		break;
	case WEST:
		//facing = WEST;
		if( !map.Collision(x-1, y) && !map.Collision(x-1, y+16-1) && !map.CheckPlayer(x-1, y)  && !map.CheckNpcs(x-16, y))
		{
         xoff = 16;
         moving = 1;
        }
		break;
	}
}


void CNpc::UpdateMovement()
{
//     if(moving == 1)
     {

        if(xoff > 0)
        {
            x--;
            xoff--;
        }
		else if(xoff < 0)
		{
          x++;
          xoff++;
        }
       else if(yoff > 0)
        {
            y--;
            yoff--;
        }
        else if(yoff < 0)
        {
            y++;
            yoff++;
        }

		if( xoff == 0 && yoff == 0)
			moving = 0;

		if( y > screen->h/2 &&  y < map.GetHeight()*map.sizeh - screen->h/2) map.npccam.y = y - screen->h/2-1;
		if( y < screen->h/2) map.npccam.y = 0;
		if( y > map.GetHeight()*map.sizeh - screen->h/2) map.npccam.y = map.GetHeight()*map.sizeh - screen->h-1;
     	if( x > screen->w/2 && x < map.GetWidth()*map.sizew - screen->w/2) map.npccam.x = x - screen->w/2;
		if( x < screen->w/2) map.npccam.x = 0;
		if( x > map.GetWidth()*map.sizew - screen->w/2) map.npccam.x = map.GetWidth()*map.sizew - screen->w;
     }
}


void CNpc::UpdateTargets()
{
     if( CHECK_BIT(quest.quest_bit, QUEST_RECRUIT_FEMALE) )
     {
         target = rand()%(1-0+1);
         if(target >= 2) target = 1;
     }

}

void CNpc::LoadStats()
{
     switch(id)
     {
       // Test Character
       case 1:
            dam = 2;
            hp = 25;
            mana = 0;
            gold = 0;
            level = 1;
            exp = 10;
            range = 1;
            name = "Robert";
            break;

     
       case 2:
            dam = 2;
            hp = 2;
            mana = 0;
            gold = 0;
            level = 1;
            exp = 2;
            range = 1;
            name = "A goblin";
            break;
            
     case 3:
            dam = 3;
            hp = 8;
            mana = 0;
            gold = 0;
            level = 1;
            exp = 3;
            range = 1;
            name = "An ugly hybrid-goblin";
            break;

    case 4:
            dam = 1;
            hp = 5;
            mana = 0;
            gold = 3;
            level = 1;
            exp = 1;
            range = 1;
            name = "Goblin Warrior";
            break;
            
    case 5:
            dam = 2;
            hp = 4;
            mana = 0;
            gold = 2;
            level = 1;
            exp = 2;
            range = 1;
            name = "Goblin Shaman";
            break;

    case 6:
            dam = 1;
            hp = 10;
            mana = 0;
            gold = 2;
            level = 1;
            exp = 3;
            range = 1;
            name = "Flaming Kurtis Soldi";
            break;
            
    case 7:
            dam = 3;
            hp = 11;
            mana = 0;
            gold = 5;
            level = 2;
            exp = 5;
            range = 1;
            name = "Flaming Kurtis General";
            break;

    case 8:
            dam = 3;
            hp = 8;
            mana = 0;
            gold = 1;
            level = 1;
            exp = 6;
            range = 1;
            name = "Skeletal Warrior";
            break;
            
    case 9:
            dam = 1;
            hp = 3;
            mana = 0;
            gold = 2;
            level = 1;
            exp = 1;
            range = 3;
            name = "Skeletal Archer";
            break;
            
    case 10:
            dam = 3;
            hp = 4;
            mana = 0;
            gold = 1;
            level = 1;
            exp = 2;
            range = 1;
            name = "Blue Slime";
            break;
            
   case 11:
            dam = 4;
            hp = 5;
            mana = 0;
            gold = 4;
            level = 1;
            exp = 10;
            range = 1;
            name = "Thantillian Footperson";
        break;
        
   case 12:
            dam = 4;
            hp = 7;
            mana = 0;
            gold = 7;
            level = 1;
            exp = 14;
            range = 1;
            name = "Thantillian Guard";
        break;
        
   case 13:
            dam = 3;
            hp = 8;
            mana = 0;
            gold = 4;
            level = 1;
            exp = 12;
            range = 4;
            name = "Thantillian Archer";
        break;
        
   case 14:
            dam = 7;
            hp = 3;
            mana = 0;
            gold = 3;
            level = 1;
            exp = 10;
            range = 1;
            name = "Thug";
            
   case 15:
            dam = 5;
            hp = 10;
            mana = 0;
            gold = 4;
            level = 1;
            exp = 20;
            range = 4;
            name = "Thug Mudslinger";

   case 16:
            dam = 4;
            hp = 12;
            mana = 0;
            gold = 1;
            level = 1;
            exp = 13;
            range = 1;
            name = "A dirty thief";
        break;
        
   case 17:
            dam = 5;
            hp = 8;
            mana = 0;
            gold = 8;
            level = 1;
            exp = 19;
            range = 2;
            name = "Flaming Kurtis - NightMage";
        break;
        
   case 18:
            dam = 6;
            hp = 10;
            mana = 0;
            gold = 8;
            level = 1;
            exp = 15;
            range = 1;
            name = "Thantillian Berserker";
        break;


   case 19:
            dam = 4;
            hp = 10;
            mana = 0;
            gold = 9;
            level = 1;
            exp = 13;
            range = 2;
            name = "Thantillian Apprentice Mage";
        break;
        
   case 20:
            dam = 1;
            hp = 20;
            mana = 0;
            gold = 0;
            level = 1;
            exp = 15;
            range = 1;
            name = "A raging harpy";
        break;
        
   case 21:
           dam = 6;
           hp = 3;
           mana = 3;
           gold = 8;
           level = 1;
           exp = 20;
           range = 1;
           name = "A goblin snappa' cracka' ";
        break;
        
  case 22:
           dam = 6;
           hp = 28;
           mana = 0;
           gold = 8;
           level = 1;
           exp = 15;
           range = 1;
           name = "A Thantillian Berserker";
        break;

  case 23:
           dam = 7;
           hp = 12;
           mana = 0;
           gold = 5;
           level = 1;
           exp = 15;
           range = 1;
           name = "A Thantillian Soldier";
        break;

  case 24:
           dam = 10;
           hp = 8;
           mana = 0;
           gold = 10;
           level = 1;
           exp = 10;
           range = 1;
           name = "A Thantillian Lancer";
        break;
        
  case 25:
           dam = 5;
           hp = 5;
           mana = 0;
           gold = 5;
           level = 1;
           exp = 8;
           range = 3;
           name = "A skilled archer";
        break;


  case 26:
           dam = 10;
           hp = 35;
           mana = 0;
           gold = 10;
           level = 1;
           exp = 45;
           range = 1;
           name = "Thantil";
        break;
        

 case 27:
           dam = 10;
           hp = 25;
           mana = 0;
           gold = 20;
           level = 1;
           exp = 40;
           range = 1;
           name = "Commander Hildolf";
        break;

 case 28:
           dam = 8;
           hp = 20;
           mana = 0;
           gold = 10;
           level = 1;
           exp = 20;
           range = 3;
           name = "Elite Thantillian Archer";
        break;


 case 29:
           dam = 10;
           hp = 22;
           mana = 0;
           gold = 8;
           level = 1;
           exp = 15;
           range = 1;
           name = "Elite Thantillian Berserker";
        break;
        

case 30:
           dam = 10;
           hp = 18;
           mana = 0;
           gold = 10;
           level = 1;
           exp = 20;
           range = 1;
           name = "Thantillian Fanatic Cleric";
        break;

     }
}
