eldhamud/boards/
eldhamud/clans/
eldhamud/classes/
eldhamud/councils/
eldhamud/deity/
eldhamud/doc/
eldhamud/doc/DIKU/
eldhamud/doc/MERC/
eldhamud/doc/mudprogs/
eldhamud/gods/
eldhamud/houses/
eldhamud/lockers/
eldhamud/player/a/
/****************************************************************************
 *   _______  _        ______            _______  _______           ______   *
 *  (  ____ \( \      (  __  \ |\     /|(  ___  )(       )|\     /|(  __  \  *
 *  | (    \/| (      | (  \  )| )   ( || (   ) || () () || )   ( || (  \  ) *
 *  | (__    | |      | |   ) || (___) || (___) || || || || |   | || |   ) | *
 *  |  __)   | |      | |   | ||  ___  ||  ___  || |(_)| || |   | || |   | | *
 *  | (      | |      | |   ) || (   ) || (   ) || |   | || |   | || |   ) | *
 *  | (____/\| (____/\| (__/  )| )   ( || )   ( || )   ( || (___) || (__/  ) *
 *  (_______/(_______/(______/ |/     \||/     \||/     \|(_______)(______/  *
 *              +-+-+-+  +-+-+-+-+-+-+-+  +-+-+-+-+-+-+-+-+-+-+              *
 *              |T|h|e|  |O|a|k|l|a|n|d|  |C|h|r|o|n|i|c|l|e|s|              *
 *              +-+-+-+  +-+-+-+-+-+-+-+  +-+-+-+-+-+-+-+-+-+-+              *
 * ------------------------------------------------------------------------- *
 * EldhaMUD code (C) 2003-2005 by Robert Powell (Tommi)                      *
 * EldhaMUD Team: Celest, Altere and Krelowyn                                *
 * ------------------------------------------------------------------------- *
 *                                                                           *
 ****************************************************************************/

#include <string.h>
#include "./Headers/mud.h"
#include "./Headers/metalworker.h"


void do_mining( CHAR_DATA *ch, char *argument )
{
  char arg [MAX_INPUT_LENGTH];
  OBJ_DATA *obj, *bonus;
  OBJ_INDEX_DATA *ore;
  int add_weight, vnum, random_number, mineIndex;
  int CHANCE = 100;


  if ( IS_NPC(ch) )
    {
      send_to_char( "Mobs cannot use this skill.\n\r", ch );
      return;
    }
  if ( IS_AFFECTED( ch, AFF_CHARM ) )
    {
      send_to_char( "You can't concentrate enough for that.\n\r", ch );
      return;
    }
  if ( ch->mount )
    {
      send_to_char( "You can't do that while mounted.\n\r", ch );
      return;
    }
  if( IS_PLR_FLAG( ch, PLR_ONMAP ) )
    {
      send_to_char( "You cannot mine out in the wilderness.\n\r", ch );
      return;
    }
/*  if(ch->in_room->sector_type == SECT_CITY)
    {
      send_to_char( "You will not find any ore inside a city.\n\r", ch );
      return;
    } */ 
  // commented out city sector untill all rooms are checked to make sure they are not city.  
  if ( ch->move < 20 )
    {
      send_to_char( "You do not have enough movement left to mine.\n\r", ch );
      return;
    }
  if (ch->carry_weight > can_carry_w(ch))
    {
      send_to_char("You look to be over loaded and cannot mine anymore",ch);
      return;
    }

  switch( ch->substate )
    {
    default:
      add_timer( ch, TIMER_DO_FUN, UMIN(skill_table[gsn_mine]->beats, 3), do_mining, 1);
      ch->alloc_ptr = str_dup( arg );
      send_to_char( "You begin mining...\n\r", ch );
      act( AT_PLAIN, "$n begins mining...", ch, NULL, NULL, TO_ROOM );
      return;

    case 1:
      if ( !ch->alloc_ptr )
        {
          send_to_char( "Your mining was interrupted!\n\r", ch );
          act( AT_PLAIN, "$n's mining was interrupted!", ch, NULL, NULL, TO_ROOM );
          bug( "do_mining: alloc_ptr NULL", 0 );
          return;
        }
      strcpy( arg, ch->alloc_ptr );
      DISPOSE( ch->alloc_ptr );
      break;

    case SUB_TIMER_DO_ABORT:
      DISPOSE( ch->alloc_ptr );
      ch->substate = SUB_NONE;
      send_to_char( "You stop mining...\n\r", ch );
      act( AT_PLAIN, "$n stops mining...", ch, NULL, NULL, TO_ROOM );
      return;
    }
  ch->substate = SUB_NONE;

  /* Gain less chance of dirt and rocks if using pick or miners helm*/
  if ( ( bonus = get_eq_char( ch, WEAR_HOLD ) ) != NULL )
    {
      if (bonus->pIndexData->vnum == OBJ_VNUM_QUEST_PICK)
        CHANCE -= 10;
    }
  if ( ( bonus = get_eq_char( ch, WEAR_HEAD ) ) != NULL )
    {
      if (bonus->pIndexData->vnum == OBJ_VNUM_QUEST_HELM)
        CHANCE -= 10;
    }

  for(mineIndex=0; mineIndex<42; mineIndex++)
    if(ch->in_room->vnum == ore_inroom_data[mineIndex].room_vnum)
      {
        break;
      }
  if(ch->in_room->vnum != ore_inroom_data[mineIndex].room_vnum)
    {
      send_to_char( "This room does not seem suitable to mine...\n\r", ch );
      return;
    }

  /* Roll to determine what ore you get*/
  random_number = number_range(0, CHANCE);
  if (random_number < 6)
    {
      vnum = ore_inroom_data[mineIndex].mine_ore_1;
    }
  else if (random_number < 16)
    {
      vnum = ore_inroom_data[mineIndex].mine_ore_2;
    }
  else if (random_number < 31)
    {
      vnum = ore_inroom_data[mineIndex].mine_ore_3;
    }
  else if (random_number < 56)
    {
      vnum = ore_inroom_data[mineIndex].mine_ore_4;
    }
  else if (random_number < 81)
    {
      vnum = ore_inroom_data[mineIndex].mine_ore_5;
    }
  else
    {
      vnum = OBJ_VNUM_ORE_DIRT;
    }
  add_weight = number_range(2, 5);
  ore = get_obj_index( vnum );
  if ( ore == NULL )
    {
      bug( "do_mining: Cannot locate item for vnum %d", vnum );
      send_to_char( "Oops. Slight bug here. The immortals have been notified.\n\r", ch );
      return;
    }
  if ( number_percent() < ch->pcdata->learned[gsn_mine] )
    {
      if ( ( obj = get_obj_vnum( ch, vnum ) ) != NULL ) //we have the object now we need to add the weight to it
        {
          obj->weight += add_weight;
          ch->carry_weight += add_weight;
          ch_printf( ch, "After some intense mining, you unearth %d pounds of %s!\n\r",add_weight, obj->short_descr );
        }
      if ( ( obj = get_obj_vnum( ch, vnum ) ) == NULL ) //we dont have the object better put one in the inventory
        {
          obj = create_object( ore, 1 );
          obj = obj_to_char( obj, ch );
          obj->weight = add_weight;
          ch->carry_weight += add_weight;
          ch_printf( ch, "After some intense mining, you unearth %d pounds of %s!\n\r",add_weight, obj->short_descr );
        }
      learn_from_success( ch, gsn_mine );
      ch->move -= 20; //take this much if your sucessfull
      if ( ch->move < 1 )
        ch->move = 0;
    }
  else
    {
      send_to_char("You fail to find anything usefull",ch);
      learn_from_success( ch, gsn_mine );
      ch->move -= 10; //take this much if your not sucessfull
      if ( ch->move < 1 )
        ch->move = 0;
    }
  return;
}

void applyAffectToObject(apply_data *theData, OBJ_DATA *theObject, CHAR_DATA *theChar)
{
  int applyAmmount = 0;
  AFFECT_DATA *affData = 0;

  if(!theData)
    return;

  if(theData->applyType == APPLY_NONE)
    return;

  if(!theObject)
    return;

  if(!theChar)
    return;

  if(theData->divisor)
    applyAmmount = (theChar->level / theData->divisor);

  applyAmmount+=theData->base + number_fuzzy(theData->noise);

  CREATE(affData, AFFECT_DATA, 1);
  affData->type = -1;
  affData->duration = -1;
  affData->location = theData->applyType;
  affData->modifier = applyAmmount;
  xCLEAR_BITS(affData->bitvector);
  LINK(affData, theObject->first_affect, theObject->last_affect, next, prev);
}

bool isForgePresent(CHAR_DATA *ch)
{
  CHAR_DATA *forge;

  bool found = FALSE;

  for ( forge = ch->in_room->first_person; forge; forge = forge->next_in_room )
    if ( IS_NPC(forge) && xIS_SET(forge->act, ACT_FORGE) )
      found = TRUE;
  return (found);
}

enum ore_types lookupOreIndexFromString(char* theString)
{
  enum ore_types oreIndex;
  enum ore_types result = -1;

  for(oreIndex=enFirstOre; oreIndex<=enLastOre; oreIndex++)
    if(!str_cmp(theString, ore_data[oreIndex].name))
      {
        result = oreIndex;
        break;
      }

  return (result);
}

enum armor_types lookupArmorIndexFromString(char* theString)
{
  enum armor_types armorIndex=0;
  enum armor_types result = -1;

  for (armorIndex=enFirstArmor; armorIndex<=enLastArmor; armorIndex++)
    if(!str_cmp(theString, armor_data[armorIndex].name))
      {
        result = armorIndex;
        break;
      }

  return (result);
}

enum weapon_types lookupWeaponIndexFromString(char* theString)
{
  enum weapon_types weaponIndex=0;
  enum weapon_types result = -1;

  for (weaponIndex=enFirstWeapon; weaponIndex<=enLastWeapon; weaponIndex++)
    if(!str_cmp(theString, weapon_data[weaponIndex].name))
      {
        result = weaponIndex;
        break;
      }

  return (result);
}

void listForgableTypes(CHAR_DATA *ch)
{
  enum armor_types armorIndex;
  enum weapon_types weaponIndex;
  char buf [30];


  send_to_char("It is possible to forge the following item types:\n\r", ch);

  send_to_char("Weapons:\n\r", ch);
  for (weaponIndex=enFirstWeapon; weaponIndex<=enLastWeapon; weaponIndex++)
    {
      sprintf(buf, "\t%s\n\r", weapon_data[weaponIndex].name);
      send_to_char(buf, ch);
    }
  send_to_char("Armors:\n\r", ch);
  for (armorIndex=enFirstArmor; armorIndex<=enLastArmor; armorIndex++)
    {
      sprintf(buf, "\t%s\n\r", armor_data[armorIndex].name);
      send_to_char(buf, ch);
    }
}

void do_forge(CHAR_DATA *ch, char *argument)
{
  OBJ_DATA *ore;
  OBJ_DATA *forged_item;
  OBJ_INDEX_DATA *pForgedItemIndex;
  //  CHAR_DATA *forge = NULL;

  char arg[MAX_INPUT_LENGTH];
  char arg1[MAX_INPUT_LENGTH];
  char buf[MAX_STRING_LENGTH];
  char obj_name[20];

  enum armor_types armorIndex;
  enum ore_types oreIndex;
  enum weapon_types weaponIndex;
  enum forge_types forgingType;

  bool foundForgingType = FALSE;

  apply_data* theData;
  //  int applyAmmount;

  argument = one_argument( argument, arg );
  argument = one_argument2( argument, arg1 );

  if (!str_cmp(arg, "list"))
    {
      listForgableTypes(ch);
      return;
    }

  if ( arg[0] == '\0' || arg1[0] == '\0' )
    {
      send_to_char("Syntax: forge  <ore> <item_type>\n\r",ch);
      send_to_char("        forge list\n\r", ch);
      send_to_char("Example: forge mithril sword\n\r",ch);
      send_to_char("Also see help forge for more details.\n\r",ch);
      return;
    }

  if(!isForgePresent(ch))
    {
      send_to_char("You require a blacksmith in order to forge\n\r", ch);
      return;
    }



  //determine if ch has ore and assign values to object based on ore type
  if ( ( ore = get_obj_carry( ch, arg ) ) == NULL )
    {
      sprintf(buf, "Sorry, you do not have any '%s' ore.\n\r", arg);
      send_to_char(buf,ch);
      return;
    }
  if ( ore->item_type != ITEM_ORE )
    {
      send_to_char("That is not real ore, you need to go and mine the real thing!!!\n\r",ch);
      return;
    }

  /*  Figure out what type of ore we are dealing with */
  if(str_cmp(arg, ore->name))
    {
      send_to_char( "That is not proper ore.  Please go and mine somewhere and leave the poor blacksmith alone.\n\r", ch );
      return;
    }

  oreIndex=lookupOreIndexFromString(arg);

  if(oreIndex == -1)
    {
      send_to_char( "Error.  Please file a note on the bug bulletin with the command you typed\n\r", ch);
      return;
    }

  if(!foundForgingType)
    {
      armorIndex=lookupArmorIndexFromString(arg1);
      if(armorIndex != -1)
        {
          forgingType = enForgeArmor;
          foundForgingType=TRUE;
          sprintf(obj_name, "%s", armor_data[armorIndex].name);
        }
    }

  if(!foundForgingType)
    {
      weaponIndex=lookupWeaponIndexFromString(arg1);
      if(weaponIndex != -1)
        {
          forgingType = enForgeWeapon;
          foundForgingType=TRUE;
          sprintf(obj_name, "%s", weapon_data[weaponIndex].name);
        }
    }

  if(!foundForgingType)
    {
      send_to_char( "You don't know how to forge that!\n\r", ch);
      send_to_char( "Type \"forge list\" for a list of objects you may forge.\n\r", ch);
      return;
    }

  if(forgingType == enForgeArmor)
    {
      if (armor_data[armorIndex].weight > ore->weight)
        {
          send_to_char( "You do not have enought ore to make that object\n\r", ch);
          return;
        }
      ore->weight -= armor_data[armorIndex].weight;

      forged_item = create_object(get_obj_index(OBJ_VNUM_FORGE_ARMOR), 0);
      forged_item->cost = ( ch->level ) * armor_data[armorIndex].costMod * number_fuzzy(10);

      SET_BIT(forged_item->wear_flags, armor_data[armorIndex].wearLocation);
      forged_item->weight = armor_data[armorIndex].weight;
      forged_item->value[0] = ((ch->level/3) + ore_data[oreIndex].modifier + armor_data[armorIndex].acMod);
      //sets the armor
      forged_item->value[3] = 12; //set the condition to perfect

      send_to_char("You work and work the ore, turning it into a piece of armor.\n\r", ch);
      act( AT_ACTION, "$n works and work the ore, turning it into a piece of armor.", ch, NULL, NULL, TO_ROOM );
    }
  if(forgingType == enForgeWeapon)
    {
      if (weapon_data[weaponIndex].weight > ore->weight)
        {
          send_to_char( "You do not have enought ore to make that object\n\r", ch);
          return;
        }
      ore->weight -= weapon_data[weaponIndex].weight;

      pForgedItemIndex=get_obj_index(OBJ_VNUM_FORGE_WEAPON);

      if(!pForgedItemIndex)
        {
          send_to_char("Please bug a coder to fix OBJ_VNUM_FORGE_WEAPON.\n\r", ch);
          return;
        }

      forged_item = create_object(pForgedItemIndex, ch->level);

      forged_item->cost = ( ch->level ) * weapon_data[weaponIndex].costMod * number_fuzzy(10);

      forged_item->value[0] = 13; //set the condition to perfect

      /*  num dice */
      forged_item->value[1] = 6 + ((ch->level / 8 ) * 4);

      /*  size of dice */
      forged_item->value[2] = ((ch->level/9) + ore_data[oreIndex].modifier ) * ((ch->level/9) + weapon_data[weaponIndex].dmgMod );
      if (forged_item->value[2] <= ( 3 * ch->level / 2 + 6 ))
        forged_item->value[2] = ( 3 * ch->level / 2 + 16 );

      forged_item->value[3] = weapon_data[weaponIndex].damageType;
      forged_item->value[4] = weapon_data[weaponIndex].weaponType;
      forged_item->weight = weapon_data[weaponIndex].weight;

      send_to_char("You work and work the ore, turning it into a new weapon.\n\r", ch);
      act( AT_ACTION, "$n works and work the ore, turning it into a new weapon.", ch, NULL, NULL, TO_ROOM );

    }

  if(ore->weight <=2)
    {
      separate_obj( ore );
      obj_from_char( ore );
    }

  forged_item->level = ch->level;


  /* right not lets set some effects on the eq based on differnt ore values */
  theData = &(ore_data[oreIndex].apply_1);

  applyAffectToObject(&(ore_data[oreIndex].apply_1), forged_item, ch);

  sprintf( buf, "%s %s", ore_data[oreIndex].name, obj_name );
  STRFREE( forged_item->name );
  forged_item->name = STRALLOC (buf);

  sprintf( buf, "A forged %s %s", ore_data[oreIndex].name, obj_name );
  STRFREE( forged_item->short_descr );
  forged_item->short_descr = STRALLOC (buf);

  sprintf( buf, "A %s %s forged by %s", ore_data[oreIndex].name, obj_name, ch->name );
  STRFREE( forged_item->description );
  forged_item->description = STRALLOC (buf);

  obj_to_char( forged_item, ch ); //finally you get the newly forged object

  learn_from_success(ch, gsn_forge);
  return;
}