/////////////////
// make_blacksmith_items.src
//
// Use a smithy hammer to either repair arms & armor or
// to work with metal to create new items. You need to be
// skillful to work with the rarer metals.
//
// TODO: remove hardcoding of skill pointvalue
//
// Author: DreamWeaver:
//              original
//         Racalac:
//              colored items
// Updates: Dream Weaver 6/3/99 handled item quality an HP to new specs
//          Ignatius     99/06/10 added name of maker to exceptional items.		
//
// More Mods: DarkFyre 11/16/99 Added support for colored ores to be better/worse than normal 
//                     also added name of maker to normal items so people can laugh at the 
//                     really bad smiths ;)
////////////////

use uo;
use cfgfile;
use os;
use util;
use basicio;

include "util/repair";
include "include/client";
include "include/objtype";
include "include/yesno";

Global itemcfg   := ReadConfigFile(":combat:itemdesc");
Global smith_cfg := ReadConfigFile( "blacksmithy" ); 

// called when a player double-clicks a hammer or other blacksmith tool
program use_hammer( character, hammer )

    	SendSysmessage( character, "What would you like to use that on?" );
    	local use_on := Target( character);
    	
	if(!ReserveItem(use_on)) return; endif

    	if( !use_on )
	        SendSysmessage( character, "Canceled." );
	        return;
    	endif
	
    	if(ownitem(character.backpack, use_on)=0)
	        SendSysmessage( character, "That item has to be in your backpack." );
	        return;
    	endif
	    
    	if( is_weapon_armor(use_on.objtype) )
	        repair_item( character, use_on, SKILLID_BLACKSMITHY );
	        return;
    	endif
	    
	if( ((use_on.objtype < UOBJ_INGOTS_START) or  (use_on.objtype > UOBJ_INGOTS_END)) and ((use_on.objtype < UOBJ_INGOTS2_START) or (use_on.objtype > UOBJ_INGOTS2_END)))
	        SendSysMessage( character, "You can only use that on ingots!" );
	        return;
    	endif
	
    	if( !checkskillfordifferentmetals( character , use_on ) )
	        SendSysmessage( character, "You have no chance to successfully work with that metal." );
	        return;
    	endif
	
	//   **Anvil finding snippet.  Inserted by Ingot Head
     	local Near_Items := ListItemsNearLocation(character.x, character.y, character.z,3);
     	foreach item in Near_Items
          	if ((item.objtype = UOBJ_ANVIL1) or (item.objtype = UOBJ_ANVIL2))
               		makeblacksmithitems(character, use_on);
               		return;
          	endif
     	endforeach
	SendSysmessage( character, "You must be near an anvil to smith items!" );
	
endprogram
	
	
	
function is_weapon_armor(what)

    	local elem := FindConfigElem(itemcfg, what);
    	if (elem)
        	return 1;
    	else
        	return 0;
    	endif
    	
endfunction



function makeblacksmithitems( character, ingots )

    	if(!ReserveItem(ingots)) return 0; endif

    	local skill;          //skill needed to create the item
    	local element;        // entry in smith_config
    	local material;       //amount of material needed to create the item
    	local points;
    	local quality;
    	local itemname;
    	local orediff;
	local Mod := ModPoints ( ingots );
	local what;
    
    	if (NOT Accessible( character, ingots))
        	SendSysmessage( character, "You don't have any ingots!" );
        	return;
    	endif
    
    	if (!smith_cfg)
        	SendSysmessage (character, "Could not open blacksmith.cfg");
        	return;
    	endif
	what := SelectMenuItem2( character, "blacksmith" );
    
    	if (what.index<1) SendSysmessage( character, "Canceled." ); return; endif
    
    	element :=  smith_cfg[what.objtype ];
    
    	if (element)
        	material := element.material;
        	skill :=  element.skill;
        	itemname := element.Name;
        	points := element.points;
    	else
        	return; 
    	endif
    	
    	local x := character.x;
	local y := character.y;

	local loopit := YesNo(character,"Autoloop?");

	SendSysMessage(character,"You begin blacksmithing...");

	repeat

	//first, check to see that they have enough ingots
	if ( GetAmount(ingots) >= material )
        	PlaySoundEffect(character, SFX_ANVIL);
        	sleep (2);
        	PlaySoundEffect(character, SFX_ANVIL);
        	sleep (2);
        	PlaySoundEffect(character, SFX_ANVIL);
        	sleep (2);

	//local Mod := ModPoints ( ingots );

	//points := points * Mod;
	SendSysMessage(character,"Skill:" + skill);
	SendSysMessage(character,"Skill:" + points);

        	

        	if (CheckSkill( character, SKILLID_BLACKSMITHY, skill, points ))
			SendSysMessage(character,"Skill:" + skill);
			SendSysMessage(character,"Skill:" + points);
            		local theitem := CreateItemInBackpack( character, what, 1 );
            		if (theitem) //they sucessfully create the item
                		local tname;
                		case(ingots.objtype)
					UOBJ_IRON_INGOT:
						quality := 1;
						orediff := 10;
						theitem.quality := quality;
						tname :=   "Iron " + itemname;
						break;
						
					UOBJ_SPIKE_INGOT:
				    		quality := 1.05;	
						orediff := 25;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname :=   "Spike " + itemname;
						break;
		                     
					UOBJ_FRUITY_INGOT:
					    	quality := 1.05;	
						orediff := 30;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname :=   "Fruity " + itemname;
						break;
	                     
					UOBJ_GREYVIXEN_INGOT:
						quality := 1.10;
						orediff := 40;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname :=   "Ice Rock " + itemname;
						break;
		                     
					UOBJ_BLACKDWARF_INGOT:
					    	quality := 1.10;	
						orediff := 50;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname :=   "Black Dwarven " + itemname;
						break;
	                     	
                			UOBJ_BRONZE_INGOT:
					    	quality:= 1.10;
						orediff := 60;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Bronze " + itemname;
						break;
			                    
					UOBJ_DARKPAGAN_INGOT:
						quality := 1.15;
						orediff := 62;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Dark Pagan " + itemname;
						break;
		                    
					UOBJ_SILVERROCK_INGOT:
				      		quality := 1.15;
						orediff := 64;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Silver Rock " + itemname;
						break;
		                    
					UOBJ_PLATINUM_INGOT:
				      		quality := 1.15;
						orediff := 65;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Platinum " + itemname;
						break;
	                    
                			UOBJ_DULLCOPPER_INGOT:
					    	quality := 1.15;	
						orediff := 70;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Dull Copper " + itemname;
						break;
		                     
					UOBJ_MYSTIC_INGOT:
					    	quality := 1.20;
						orediff := 75;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Mystic " + itemname;
						break;
		                     
					UOBJ_COPPER_INGOT:
					    	quality := 1.20;
						orediff := 80;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Copper " + itemname;
						break;
		                     
                			UOBJ_SPECTRAL_INGOT:
						quality := 1.20;
						orediff := 83;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Spectral " + itemname;
						break;
		                     
					UOBJ_ONYX_INGOT:
					    	quality := 1.25;	
						orediff := 85;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Onyx " + itemname;
						break;
		                     
					UOBJ_OLDBRITAIN_INGOT:
					    	quality := 1.25;
						orediff := 87;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Old Britain " + itemname;
						break;
		                     
					UOBJ_REDELVEN_INGOT:
						quality := 1.25;
						orediff := 88;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Red Elven " + itemname;
						break;
		                     
					UOBJ_UNDEAD_INGOT:
					    	quality := 1.3;
						orediff := 89;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Undead " + itemname;
						break;
		                     
                			UOBJ_PYRITE_INGOT:
					    	quality := 1.3;
						orediff := 90;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Pyrite " + itemname;
						break;
		                     
                			UOBJ_VIRGINITY_INGOT:
						quality := 1.35;
						orediff := 93;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Virginity " + itemname;
						break;
		                     
                			UOBJ_MALACHITE_INGOT:
						quality := 1.4;
						orediff := 95;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Malachite " + itemname;
						break;
		                     
					UOBJ_LAVAROCK_INGOT:
					    	quality := 1.5;
						orediff := 97;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Lavarock " + itemname;
						break;
		                     
                			UOBJ_AZURITE_INGOT:
				      		quality := 1.7;
						orediff := 98;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Azurite " + itemname;
						break;
		                     
					UOBJ_ZULU_INGOT:
						quality := 1.8;
						orediff := 99;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "New Zulu " + itemname;
						break;
		                     
					UOBJ_DRIPSTONE_INGOT:
						quality := 1.81;
						orediff := 100;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Dripstone " + itemname;
						break;
		                     
					UOBJ_EXECUTOR_INGOT:
						quality := 1.82;
						orediff := 105;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Executor " + itemname;
						break;
		                     
					UOBJ_PEACHBLUE_INGOT:
						quality := 1.83;
						orediff := 105;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Peachblue " + itemname;
						break;
		                     
					UOBJ_DESTRUCTION_INGOT:
						quality := 1.84;
						orediff := 110;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Destruction " + itemname;
						break;
		                     
					UOBJ_ANRA_INGOT:
						quality := 1.85;
						orediff := 115;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Anra " + itemname;
						break;
		                     
					UOBJ_GODDESS_INGOT:
						quality := 1.86;
						orediff := 120;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Goddess " + itemname;
						break;
		                     
					UOBJ_CRYSTAL_INGOT:
						quality := 1.87;
						orediff := 125;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Crystal " + itemname;
						break;
		                     
					UOBJ_DOOM_INGOT:
						quality := 1.9;
						orediff := 129;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Doom " + itemname;
						break;
		                     
					UOBJ_SAPPHIRE_INGOT:
						quality := 2.2;
						orediff := 130;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Ebon Twilight Sapphire " + itemname;
						break;
		                     
					UOBJ_RUBY_INGOT:
						quality := 2.2;
						orediff := 130;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Dark Sable Ruby " + itemname;
						break;
		                     
					UOBJ_DIAMOND_INGOT:
						quality := 2.2;
						orediff := 130;
						theitem.quality := quality;
						theitem.hp := theitem.maxhp;
						tname := "Radiant Nimbus Diamond " + itemname;
						break;
                    		endcase
		                    	
				skill := skill + Cint(orediff / 2);
				if( skill < 90 )
					skill := 90;
				endif
				
                		if ( (randomint(100) < 10) and CheckSkill(character, SKILLID_BLACKSMITHY, skill, Cint(points / 2)) )
                			//Made something Exceptionnal
                			local armslore := GetSkill( character , SKILLID_ARMSLORE );
                			local multiplier := 105 + Cint(armslore / 10);
                			local newquality := Cint(quality * multiplier);
                			newquality := Cdbl(newquality);
                			newquality := newquality / 100;
                			theitem.quality := newquality;
                			theitem.hp := theitem.maxhp;
                			tname := "Exceptional " + tname + " [Crafted by " + character.name + "]";
	                    		SendSysmessage (character, "You make an exceptional item and put it in your pack");
                    		else
                    			//Made a normal item
	                    		SendSysmessage( character, "You create the item and place it in your pack." );
                    		endif
                    		
                    		theitem.color := ingots.color; //for colored ingots!
                		SubtractAmount( ingots, material );
                    		SetObjProperty( theitem , "type" , "b" );
                    		SetName( theitem , tname );
			endif
       
        	else
            		SubtractAmount( ingots, 1 );
            		SendSysmessage( character, "You destroy some ingots." );
        	endif
    
    	else 
        	SendSysmessage (character, "You don't have enough ingots!");
        	return;
	endif

	until ( (character.x != x) or (character.y !=y) or (!loopit));

	SendSysMessage(character,"You stop blacksmithing...");
  
endfunction



function checkskillfordifferentmetals( character , ingot )

    case( ingot.objtype )
 	UOBJ_IRON_INGOT: 	return (checkhisskill( character , 10 ));
	UOBJ_SPIKE_INGOT: 	return (checkhisskill( character , 25 ));
	UOBJ_FRUITY_INGOT: 	return (checkhisskill( character , 30 ));
	UOBJ_GREYVIXEN_INGOT: 	return (checkhisskill( character , 40 ));
	UOBJ_BLACKDWARF_INGOT: 	return (checkhisskill( character , 50 ));
      	UOBJ_BRONZE_INGOT: 	return (checkhisskill( character , 60 ));
	UOBJ_DARKPAGAN_INGOT: 	return (checkhisskill( character , 62 ));
	UOBJ_SILVERROCK_INGOT: 	return (checkhisskill( character , 64 ));
	UOBJ_PLATINUM_INGOT: 	return (checkhisskill( character , 65 ));
      	UOBJ_DULLCOPPER_INGOT: 	return (checkhisskill( character , 70 ));
	UOBJ_MYSTIC_INGOT: 	return (checkhisskill( character , 75 ));
      	UOBJ_COPPER_INGOT: 	return (checkhisskill( character , 80 ));
	UOBJ_SPECTRAL_INGOT: 	return (checkhisskill( character , 83 ));
      	UOBJ_ONYX_INGOT: 	return (checkhisskill( character , 85 ));
	UOBJ_OLDBRITAIN_INGOT: 	return (checkhisskill( character , 87 ));
	UOBJ_REDELVEN_INGOT: 	return (checkhisskill( character , 88 ));
	UOBJ_UNDEAD_INGOT: 	return (checkhisskill( character , 89 ));
      	UOBJ_PYRITE_INGOT: 	return (checkhisskill( character , 90 ));
	UOBJ_VIRGINITY_INGOT: 	return (checkhisskill( character , 93 ));
      	UOBJ_MALACHITE_INGOT: 	return (checkhisskill( character , 95 ));
	UOBJ_LAVAROCK_INGOT: 	return (checkhisskill( character , 97 ));
      	UOBJ_AZURITE_INGOT: 	return (checkhisskill( character , 98 ));
	UOBJ_ZULU_INGOT: 	return (checkhisskill( character , 99 ));
	UOBJ_DRIPSTONE_INGOT: 	return (checkhisskill( character , 100 ));
	UOBJ_EXECUTOR_INGOT: 	return (checkhisskill( character , 105 ));
	UOBJ_PEACHBLUE_INGOT: 	return (checkhisskill( character , 105 ));
	UOBJ_DESTRUCTION_INGOT: return (checkhisskill( character , 110 ));
	UOBJ_ANRA_INGOT: 	return (checkhisskill( character , 115 ));
	UOBJ_GODDESS_INGOT: 	return (checkhisskill( character , 120 ));
	UOBJ_CRYSTAL_INGOT: 	return (checkhisskill( character , 125 ));
	UOBJ_DOOM_INGOT: 	return (checkhisskill( character , 129 ));
	UOBJ_SAPPHIRE_INGOT: 	return (checkhisskill( character , 120 ));
	UOBJ_RUBY_INGOT: 	return (checkhisskill( character , 120 ));
	UOBJ_DIAMOND_INGOT: 	return (checkhisskill( character , 120 ));

        default: SendSysmessage( character , "You can't smelt that." );
    endcase
    
endfunction



function checkhisskill( character , skill )

    	local his_skill := GetSkill( character , SKILLID_BLACKSMITHY );

	if( his_skill >= skill )
        	return 1;
    	else
        	return 0;
    	endif
    
endfunction

function ModPoints( ingots )


	case(ingots.objtype)
			UOBJ_IRON_INGOT: return 1;
			UOBJ_SPIKE_INGOT: return 1.1;
			UOBJ_IRON_INGOT: return 1.2;
			UOBJ_SPIKE_INGOT: return 1.3;
			UOBJ_FRUITY_INGOT: return 1.4;
			UOBJ_GREYVIXEN_INGOT: return 1.5;
			UOBJ_BLACKDWARF_INGOT: return 1.6;
      			UOBJ_BRONZE_INGOT: return 1.7;
			UOBJ_DARKPAGAN_INGOT: return 1.8;
			UOBJ_SILVERROCK_INGOT: return 1.9;
			UOBJ_PLATINUM_INGOT: return 2.0;
      			UOBJ_DULLCOPPER_INGOT: return 2.1;
			UOBJ_MYSTIC_INGOT: return 2.2;
		      	UOBJ_COPPER_INGOT: return 2.3;
			UOBJ_SPECTRAL_INGOT: return 2.4;
      			UOBJ_ONYX_INGOT: return 2.5;
			UOBJ_OLDBRITAIN_INGOT: return 2.6;
			UOBJ_REDELVEN_INGOT: return 2.7;
			UOBJ_UNDEAD_INGOT: return 2.8;
      			UOBJ_PYRITE_INGOT: return 2.9;
			UOBJ_VIRGINITY_INGOT: return 3.0;
      			UOBJ_MALACHITE_INGOT: return 3.1;
			UOBJ_LAVAROCK_INGOT: return 3.2;
		      	UOBJ_AZURITE_INGOT: return 3.3;
			UOBJ_ZULU_INGOT: return 5.0;
			UOBJ_DRIPSTONE_INGOT: return 3.4;
			UOBJ_EXECUTOR_INGOT: return 3.5;
			UOBJ_PEACHBLUE_INGOT: return 3.6;
			UOBJ_DESTRUCTION_INGOT: return 3.7;
			UOBJ_ANRA_INGOT: return 3.8;
			UOBJ_GODDESS_INGOT: return 3.9;
			UOBJ_CRYSTAL_INGOT: return 4.0;
			UOBJ_DOOM_INGOT: return 4.5;
			UOBJ_SAPPHIRE_INGOT: return 5.0;
			UOBJ_RUBY_INGOT: return 5.0;
			UOBJ_DIAMOND_INGOT: return 5.0;
	
	default: return 1.0;

	endcase

endfunction

function ownitem( bpack ,item )

   	local backpack := EnumerateItemsInContainer ( bpack );
   	foreach bpitem in backpack
		if (bpitem.serial = item.serial)
			return 1;
		endif
   	endforeach

   	return 0;

endfunction
