/
com/planet_ink/coffee_mud/Abilities/Common/
com/planet_ink/coffee_mud/Abilities/Diseases/
com/planet_ink/coffee_mud/Abilities/Druid/
com/planet_ink/coffee_mud/Abilities/Fighter/
com/planet_ink/coffee_mud/Abilities/Languages/
com/planet_ink/coffee_mud/Abilities/Misc/
com/planet_ink/coffee_mud/Abilities/Prayers/
com/planet_ink/coffee_mud/Abilities/Properties/
com/planet_ink/coffee_mud/Abilities/Skills/
com/planet_ink/coffee_mud/Abilities/Songs/
com/planet_ink/coffee_mud/Abilities/Spells/
com/planet_ink/coffee_mud/Abilities/Thief/
com/planet_ink/coffee_mud/Abilities/Traps/
com/planet_ink/coffee_mud/Behaviors/
com/planet_ink/coffee_mud/CharClasses/interfaces/
com/planet_ink/coffee_mud/Commands/
com/planet_ink/coffee_mud/Commands/interfaces/
com/planet_ink/coffee_mud/Common/
com/planet_ink/coffee_mud/Common/interfaces/
com/planet_ink/coffee_mud/Exits/interfaces/
com/planet_ink/coffee_mud/Items/Armor/
com/planet_ink/coffee_mud/Items/Basic/
com/planet_ink/coffee_mud/Items/CompTech/
com/planet_ink/coffee_mud/Items/MiscMagic/
com/planet_ink/coffee_mud/Items/Weapons/
com/planet_ink/coffee_mud/Items/interfaces/
com/planet_ink/coffee_mud/Libraries/
com/planet_ink/coffee_mud/Libraries/interfaces/
com/planet_ink/coffee_mud/Locales/
com/planet_ink/coffee_mud/MOBS/
com/planet_ink/coffee_mud/Races/
com/planet_ink/coffee_mud/Races/interfaces/
com/planet_ink/coffee_mud/WebMacros/
com/planet_ink/coffee_mud/WebMacros/interfaces/
com/planet_ink/coffee_mud/core/
com/planet_ink/coffee_mud/core/collections/
com/planet_ink/coffee_mud/core/interfaces/
com/planet_ink/coffee_mud/core/intermud/
com/planet_ink/coffee_mud/core/intermud/i3/
com/planet_ink/coffee_web/server/
com/planet_ink/siplet/applet/
lib/
resources/factions/
resources/fakedb/
resources/progs/autoplayer/
resources/quests/holidays/
web/
web/admin.templates/
web/admin/grinder/
web/admin/images/
web/clan.templates/
web/pub.templates/
web/pub/images/mxp/
web/pub/sounds/
web/pub/textedit/
package com.planet_ink.coffee_mud.Abilities.Common;
import com.planet_ink.coffee_mud.core.interfaces.*;
import com.planet_ink.coffee_mud.core.*;
import com.planet_ink.coffee_mud.core.collections.*;
import com.planet_ink.coffee_mud.Abilities.Common.CraftingSkill.CraftingActivity;
import com.planet_ink.coffee_mud.Abilities.interfaces.*;
import com.planet_ink.coffee_mud.Areas.interfaces.*;
import com.planet_ink.coffee_mud.Behaviors.interfaces.*;
import com.planet_ink.coffee_mud.CharClasses.interfaces.*;
import com.planet_ink.coffee_mud.Commands.interfaces.*;
import com.planet_ink.coffee_mud.Common.interfaces.*;
import com.planet_ink.coffee_mud.Exits.interfaces.*;
import com.planet_ink.coffee_mud.Items.interfaces.*;
import com.planet_ink.coffee_mud.Libraries.interfaces.*;
import com.planet_ink.coffee_mud.Locales.interfaces.*;
import com.planet_ink.coffee_mud.MOBS.interfaces.*;
import com.planet_ink.coffee_mud.Races.interfaces.*;

import java.util.*;


/*
   Copyright 2003-2016 Bo Zimmerman

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/


public class LockSmith extends CraftingSkill
{
	@Override public String ID() { return "LockSmith"; }
	private final static String localizedName = CMLib.lang().L("Locksmithing");
	@Override public String name() { return localizedName; }
	private static final String[] triggerStrings =I(new String[] {"LOCKSMITH","LOCKSMITHING"});
	@Override public String[] triggerStrings(){return triggerStrings;}
	@Override public String supportedResourceString(){return "METAL|MITHRIL";}

	private String keyCode="";
	protected Physical workingOn=null;
	protected boolean boltlock=false;
	private boolean delock=false;

	@Override
	public void unInvoke()
	{
		if(canBeUninvoked())
		{
			if(affected instanceof MOB)
			{
				final MOB mob=(MOB)affected;
				if((buildingI!=null)&&(!aborted))
				{
					if(messedUp)
						commonTell(mob,L("You've ruined @x1!",buildingI.name(mob)));
					else
					if(!delock)
					{
						dropAWinner(mob,buildingI);
						CMLib.achievements().possiblyBumpAchievement(mob, AchievementLibrary.Event.CRAFTING, 1, this);
					}
				}
				buildingI=null;
			}
		}
		super.unInvoke();
	}

	public Item getBuilding(Environmental target)
	{
		final Item newbuilding=CMClass.getItem("GenKey");
		if((workingOn instanceof Exit)
		&&((Exit)workingOn).hasALock()
		&&(((Exit)workingOn).keyName().length()>0))
			keyCode=((Exit)workingOn).keyName();
		if((workingOn instanceof Container)
		&&(((Container)workingOn).hasALock())
		&&(((Container)workingOn).keyName().length()>0))
			keyCode=((Container)workingOn).keyName();
		((DoorKey)newbuilding).setKey(keyCode);
		return newbuilding;
	}

	@Override
	public int classificationCode()
	{
		return Ability.ACODE_COMMON_SKILL | Ability.DOMAIN_BUILDINGSKILL;
	}

	@Override
	public boolean tick(Tickable ticking, int tickID)
	{
		if((affected!=null)&&(affected instanceof MOB)&&(tickID==Tickable.TICKID_MOB))
		{
			final MOB mob=(MOB)affected;
			if((workingOn instanceof Container) && (mob.location()!=CMLib.map().roomLocation(workingOn)))
			{
				commonTell(mob,L("You've stopped @x1.",verb));
				buildingI=null;
				unInvoke();
				return super.tick(ticking, tickID);
			}
			if(tickDown<=1)
			{
				if(buildingI==null)
					buildingI=getBuilding(workingOn);
				if((workingOn!=null)&&(mob.location()!=null)&&(!aborted))
				{
					if(workingOn instanceof Exit)
					{
						if((delock)||(!((Exit)workingOn).hasALock()))
						{
							int dir=-1;
							for(final int d : Directions.CODES())
								if(mob.location().getExitInDir(d)==workingOn)
								{
									dir=d;
									break;
								}
							if((messedUp)||(dir<0))
							{
								if(delock)
									commonTell(mob,L("You've failed to remove the lock."));
								else
									commonTell(mob,L("You've ruined the lock."));
								buildingI=null;
								unInvoke();
							}
							else
							{
								final Exit exit2=mob.location().getPairedExit(dir);
								final Room room2=mob.location().getRoomInDir(dir);
								((Exit)workingOn).basePhyStats().setLevel(xlevel(mob));
								((Exit)workingOn).recoverPhyStats();
								((Exit)workingOn).setDoorsNLocks(true,false,true,!delock,!delock,!delock);
								if(buildingI instanceof DoorKey)
								{
									if(((DoorKey)buildingI).getKey().length()==0)
										((DoorKey)buildingI).setKey(keyCode);
									((Exit)workingOn).setKeyName(((DoorKey)buildingI).getKey());
								}
								if(CMLib.map().getRoom(mob.location().roomID())==mob.location()) // ensures not an instance or ship
									CMLib.database().DBUpdateExits(mob.location());
								if((exit2!=null)
								&&(!boltlock)
								&&(exit2.hasADoor())
								&&(exit2.isGeneric())
								&&(room2!=null))
								{
									exit2.basePhyStats().setLevel(xlevel(mob));
									exit2.setDoorsNLocks(true,false,true,!delock,!delock,!delock);
									if(buildingI instanceof DoorKey)
										exit2.setKeyName(((DoorKey)buildingI).getKey());
									if(CMLib.map().getRoom(room2.roomID())==room2) // ensures not an instance or ship
										CMLib.database().DBUpdateExits(room2);
								}
							}
						}
					}
					else
					if(workingOn instanceof Container)
					{
						if(delock||(!((Container)workingOn).hasALock()))
						{
							if(messedUp)
							{
								if(delock)
									commonTell(mob,L("You've failed to remove the lock."));
								else
									commonTell(mob,L("You've ruined the lock."));
								buildingI=null;
								unInvoke();
							}
							else
							{
								((Container)workingOn).setDoorsNLocks(true,false,((Container)workingOn).defaultsClosed(),!delock,!delock,!delock);
								if(buildingI instanceof DoorKey)
								{
									if(((DoorKey)buildingI).getKey().length()==0)
										((DoorKey)buildingI).setKey(keyCode);
									((Container)workingOn).setKeyName(((DoorKey)buildingI).getKey());
								}
							}
						}
					}
				}
			}
		}
		return super.tick(ticking,tickID);
	}

	@Override
	public boolean invoke(MOB mob, List<String> commands, Physical givenTarget, boolean auto, int asLevel)
	{
		if(super.checkStop(mob, commands))
			return true;
		if((commands.size()==0)
		||(CMParms.combine(commands,0).equalsIgnoreCase("list")))
		{
			commonTell(mob,L("Locksmith what or where? Enter the name of a container or door direction. Put the word \"boltlock\" in front of the door direction to make a one-way lock.  Put the word \"delock\" in front of the door direction to remove the locks."));
			return false;
		}
		keyCode=""+Math.random();
		String startStr=null;
		int duration=8;
		activity = CraftingActivity.CRAFTING;
		buildingI=null;
		boolean keyFlag=false;
		workingOn=null;
		messedUp=false;
		int woodRequired=1;
		boolean lboltlock=false;
		if((commands.size()>0)&&("BOLTLOCK".startsWith((commands.get(0)).toUpperCase())))
		{
			lboltlock=true;
			commands.remove(0);
		}
		boolean ldelock=false;
		if((commands.size()>0)&&("DELOCK".startsWith((commands.get(0)).toUpperCase())))
		{
			ldelock=true;
			commands.remove(0);
		}
		final String recipeName=CMParms.combine(commands,0);
		final int dir=Directions.getGoodDirectionCode(recipeName);
		if(dir<0)
			workingOn=mob.location().fetchFromMOBRoomFavorsItems(mob,null,recipeName,Wearable.FILTER_UNWORNONLY);
		else
			workingOn=mob.location().getExitInDir(dir);

		if((workingOn==null)||(!CMLib.flags().canBeSeenBy(workingOn,mob)))
		{
			commonTell(mob,L("You don't see a '@x1' here.",recipeName));
			return false;
		}
		if(workingOn instanceof Exit)
		{
			if(!((Exit)workingOn).hasADoor())
			{
				commonTell(mob,L("There is no door in that direction."));
				return false;
			}
			if(!workingOn.isGeneric())
			{
				commonTell(mob,L("That door isn't built right -- it can't be modified."));
				return false;
			}
			if(!ldelock)
			{
				if(((Exit)workingOn).hasALock())
					keyFlag=true;
				else
					woodRequired=5;
			}

			final Room otherRoom=(dir>=0)?mob.location().getRoomInDir(dir):null;
			if((!CMLib.law().doesOwnThisProperty(mob,mob.location()))
			&&((otherRoom==null)
				||(!CMLib.law().doesOwnThisProperty(mob,otherRoom))))
			{
				commonTell(mob,L("You'll need the permission of the owner to do that."));
				return false;
			}
		}
		else
		if(workingOn instanceof Container)
		{
			if(!((Container)workingOn).hasADoor())
			{
				commonTell(mob,L("That doesn't have a lid."));
				return false;
			}
			if(!workingOn.isGeneric())
			{
				commonTell(mob,L("That just isn't built right -- it can't be modified."));
				return false;
			}
			if(!ldelock)
			{
				if(((Container)workingOn).hasALock())
					keyFlag=true;
				else
					woodRequired=3;
			}
			if((((Container)workingOn).owner() instanceof Room)
			&&(!CMLib.flags().isGettable((Container)workingOn))
			&&(!CMLib.law().doesHavePriviledgesHere(mob,mob.location())))
			{
				commonTell(mob,L("You'll need the permission of the owner of this place to do that."));
				return false;
			}
		}
		else
		{
			commonTell(mob,L("You can't put a lock on that."));
			return false;
		}

		String itemName=null;
		int makeResource=-1;
		if(ldelock)
		{
			itemName="a broken lock";
			keyFlag=false;
			if(!super.invoke(mob,commands,givenTarget,auto,asLevel))
				return false;
		}
		else
		{
			final int[] pm={RawMaterial.MATERIAL_METAL,RawMaterial.MATERIAL_MITHRIL};
			final int[][] data=fetchFoundResourceData(mob,
												woodRequired,"metal",pm,
												0,null,null,
												false,
												0,
												null);
			if(data==null)
				return false;
			woodRequired=data[0][FOUND_AMT];
			if(!super.invoke(mob,commands,givenTarget,auto,asLevel))
				return false;
			CMLib.materials().destroyResourcesValue(mob.location(),woodRequired,data[0][FOUND_CODE],0,null);
			itemName=(RawMaterial.CODES.NAME(data[0][FOUND_CODE])+" key").toLowerCase();
			itemName=CMLib.english().startWithAorAn(itemName);
			makeResource=data[0][FOUND_CODE];
		}
		buildingI=getBuilding(workingOn);
		if(buildingI==null)
		{
			commonTell(mob,L("There's no such thing as a GenKey!!!"));
			return false;
		}
		if((makeResource>=0)&&(buildingI!=null))
			buildingI.setMaterial(makeResource);
		duration=getDuration(25,mob,workingOn.phyStats().level(),8);
		if(keyFlag)
			duration=duration/2;
		buildingI.setName(itemName);
		startStr=L("<S-NAME> start(s) working on @x1@x2.",(keyFlag?"a key for ":""),workingOn.name());
		displayText=L("You are working on @x1@x2",(keyFlag?"a key for ":""),workingOn.name());
		verb=L("working on @x1@x2",(keyFlag?"a key for ":""),workingOn.name());
		playSound="drill.wav";
		buildingI.setDisplayText(L("@x1 lies here",itemName));
		buildingI.setDescription(itemName+". ");
		buildingI.basePhyStats().setWeight(woodRequired);
		buildingI.setBaseValue(1);
		buildingI.basePhyStats().setLevel(1);
		buildingI.setSecretIdentity(getBrand(mob));
		buildingI.recoverPhyStats();
		buildingI.text();
		buildingI.recoverPhyStats();

		int proficiencyAddition=0;
		if(workingOn.phyStats().level()>xlevel(mob))
			proficiencyAddition=workingOn.phyStats().level()-xlevel(mob);
		messedUp=!proficiencyCheck(mob,proficiencyAddition*5,auto);

		final CMMsg msg=CMClass.getMsg(mob,null,this,getActivityMessageType(),startStr);
		if(mob.location().okMessage(mob,msg))
		{
			mob.location().send(mob,msg);
			boltlock=lboltlock;
			delock=ldelock;
			beneficialAffect(mob,mob,asLevel,duration);
		}
		return true;
	}
}