 if ( SERVER ) then 
   
 	AddCSLuaFile( "shared.lua" ) 
 	SWEP.HoldType = "pistol";
 
 end 

 
 SWEP.Base = "ts2_base";
   
 SWEP.Spawnable			= false 
 SWEP.AdminSpawnable		= false 
   


SWEP.WorldModel = "models/weapons/w_pistol.mdl";
SWEP.ViewModel = "models/weapons/v_pistol.mdl";

SWEP.PrintName = "Map Editing";
SWEP.TS2Desc = "Left click - Edit target door\nRight click - Copy settings from target door\nReload - Apply copied settings to target door";

 SWEP.Primary.Recoil			= .8
 SWEP.Primary.RecoilAdd			= .06
 SWEP.Primary.RecoilMin = .8
 SWEP.Primary.RecoilMax = 1.9
 SWEP.Primary.RecoverTime = .4

 SWEP.Primary.Sound = Sound( "weapons/pistol/pistol_fire3.wav" );

 SWEP.Primary.Damage			= 40 
 SWEP.Primary.NumShots		= 1 
 SWEP.Primary.ClipSize = 16;
 SWEP.Primary.DefaultClip = 32;
 SWEP.Primary.Ammo = "pistol";
 SWEP.Primary.Delay = .08;
 
 SWEP.TS2HoldType = "PISTOL";

 SWEP.Primary.SpreadCone = Vector( .04, .04, .04 );

 SWEP.Primary.IronSightPos = Vector( -5.4, 4.5, -2.0 );
 SWEP.Primary.IronSightAng = Vector( 0.0, 0.0, 0.0 );
 
   
SWEP.Primary.HolsteredPos = Vector( -6.4, -5.5, -12.0 );
   
   
SWEP.IconCamPos = Vector( 50, 139, 0 ) 
SWEP.IconLookAt = Vector( 1, 4, -1 ) 
SWEP.IconFOV = 6

function SWEP:SecondaryAttack()


	if( SERVER ) then
	
		local trace = { }
		trace.start = self.Owner:EyePos();
		trace.endpos = trace.start + self.Owner:GetAimVector() * 1024;
		trace.filter = self.Owner;
		
		local tr = util.TraceLine( trace );
		
		if( tr.Entity and tr.Entity:IsValid() and tr.Entity:IsDoor() ) then
		
			self.Owner:GetTable().MEDoorEntity = tr.Entity;
			self.Owner:GetTable().MEDoorName = tr.Entity:GetTable().DoorName or "";
			self.Owner:GetTable().MEDoorPrice = tr.Entity:GetTable().DoorPrice or 0;
			self.Owner:GetTable().MEDoorFlags = tr.Entity:GetTable().DoorFlags or "";
			self.Owner:GetTable().MEPropertyFamily = tr.Entity:GetTable().PropertyFamily or "";
			self.Owner:GetTable().MEStorageWidth = tr.Entity:GetTable().StorageWidth or 0;
			self.Owner:GetTable().MEStorageHeight = tr.Entity:GetTable().StorageHeight or 0;
			
			umsg.Start( "CDS", self.Owner );
				umsg.String( self.Owner:GetTable().MEDoorName );
				umsg.Short( self.Owner:GetTable().MEDoorPrice );
				umsg.String( self.Owner:GetTable().MEDoorFlags );
				umsg.String( self.Owner:GetTable().MEPropertyFamily );
			umsg.End();

		end
		
	end

end

function SWEP:PrimaryAttack()

	if( SERVER ) then
	
		local trace = { }
		trace.start = self.Owner:EyePos();
		trace.endpos = trace.start + self.Owner:GetAimVector() * 1024;
		trace.filter = self.Owner;
		
		local tr = util.TraceLine( trace );
		
		if( tr.Entity and tr.Entity:IsValid() and tr.Entity:IsDoor() ) then
		
			umsg.Start( "MEED", self.Owner );
				umsg.Entity( tr.Entity );
				umsg.String( tr.Entity:GetTable().DoorName or "" );
				umsg.Short( tr.Entity:GetTable().DoorPrice or 0 );
				umsg.String( tr.Entity:GetTable().DoorFlags or "" );
				umsg.String( tr.Entity:GetTable().PropertyFamily or "" );
				umsg.Short( tr.Entity:GetTable().StorageWidth or 0 );
				umsg.Short( tr.Entity:GetTable().StorageHeight or 0 );
			umsg.End();
		
		end
		
	end

end

function SWEP:Reload()

	if( SERVER ) then
	
		local trace = { }
		trace.start = self.Owner:EyePos();
		trace.endpos = trace.start + self.Owner:GetAimVector() * 1024;
		trace.filter = self.Owner;
		
		local tr = util.TraceLine( trace );
		
		if( tr.Entity and tr.Entity:IsValid() and tr.Entity:IsDoor() ) then
		
			local doorent = tr.Entity;
		
			doorent:GetTable().DoorName = self.Owner:GetTable().MEDoorName;
			doorent:GetTable().DoorPrice = self.Owner:GetTable().MEDoorPrice;
			doorent:GetTable().DoorFlags = self.Owner:GetTable().MEDoorFlags;
			doorent:GetTable().PropertyFamily = self.Owner:GetTable().MEPropertyFamily;
			doorent:GetTable().StorageWidth = self.Owner:GetTable().MEStorageWidth;
			doorent:GetTable().StorageHeight = self.Owner:GetTable().MEStorageHeight;
			
			if( not table.HasValueWithField( TS.MapDoors, "Door", doorent ) ) then
			
				table.insert( TS.MapDoors, { Door = doorent, Name = doorent:GetTable().DoorName, Price = doorent:GetTable().DoorPrice, Flags = doorent:GetTable().DoorFlags, PropertyFamily = doorent:GetTable().PropertyFamily, StorageWidth = doorent:GetTable().StorageWidth, StorageHeight = doorent:GetTable().StorageHeight  } );
			
			else
			
				for k, v in pairs( TS.MapDoors ) do
				
					if( v.Door == doorent ) then
					
						v.Name = doorent:GetTable().DoorName;
						v.Price = doorent:GetTable().DoorPrice;
						v.Flags = doorent:GetTable().DoorFlags;
						v.PropertyFamily = doorent:GetTable().PropertyFamily;
						v.StorageWidth = doorent:GetTable().StorageWidth;
						v.StorageHeight = doorent:GetTable().StorageHeight;
						
					end
				
				end
			
			end
			
			TS.BindPropertyAndDoor( doorent );
			TS.BindContainerAndDoor( doorent );
			
			local rec = RecipientFilter();
			
			for k, v in pairs( TS.MapEditors ) do
			
				rec:AddPlayer( v );
			
			end
			
			umsg.Start( "MEUD", rec );
				umsg.Entity( doorent );
				umsg.String( doorent:GetTable().DoorName or "" );
				umsg.Short( doorent:GetTable().DoorPrice or 0 );
				umsg.String( doorent:GetTable().DoorFlags or "" );
				umsg.String( doorent:GetTable().PropertyFamily or "" );
				umsg.Short( doorent:GetTable().StorageWidth or 0 );
				umsg.Short( doorent:GetTable().StorageHeight or 0 );
			umsg.End();
				
		end
		
	end

end


if( CLIENT ) then

	MEDoorName = "";
	MEDoorPrice = 0;
	MEDoorFlags = "";
	MEPropertyFamily = "";

	function SWEP:DrawHUD()
	
		local parent = "";
		local child = "";
		
		if( string.find( MEPropertyFamily, ":" ) ) then
		
			parent = string.sub( MEPropertyFamily, 1, string.find( MEPropertyFamily, ":" ) - 1 );
			child = string.sub( MEPropertyFamily, string.find( MEPropertyFamily, ":" ) + 1 );
			
		end
	
		draw.RoundedBox( 0, 10, ScrH() * .2 - 20, 200, 105, Color( 0, 0, 0, 200 ) );
		draw.DrawText( "Copied door settings", "NewChatFont", 14, ScrH() * .2 - 10, Color( 255, 100, 100, 200 ) );
		draw.DrawText( "Door name: " .. MEDoorName .. "\nDoor price: " .. MEDoorPrice .. "\nDoor flags: " .. MEDoorFlags .. "\nParent: " .. parent .. "\nChild: " .. child, "NewChatFont", 14, ScrH() * .2 + 5, Color( 255, 255, 255, 200 ) );
	
		local trace = { }
		trace.start = self.Owner:EyePos();
		trace.endpos = trace.start + self.Owner:GetAimVector() * 1024;
		trace.filter = self.Owner;
		
		local tr = util.TraceLine( trace );
		
		if( tr.Entity and tr.Entity:IsValid() ) then
		
			if( tr.Entity:IsDoor() ) then
				draw.DrawText( "o", "TargetID", ScrW() / 2, ScrH() / 2, Color( 255, 100, 100, 255 ), 1, 1 );
			else
				draw.DrawText( "x", "TargetID", ScrW() / 2, ScrH() / 2, Color( 255, 255, 255, 255 ), 1, 1 );
			end
			
		else
			draw.DrawText( "x", "TargetID", ScrW() / 2, ScrH() / 2, Color( 255, 255, 255, 255 ), 1, 1 );
		end
		
	end
	
	local function CopyDoorSettings( msg )
	
		MEDoorName = msg:ReadString();
		MEDoorPrice = msg:ReadShort();
		MEDoorFlags = msg:ReadString();
		MEPropertyFamily = msg:ReadString();
	
	end
	usermessage.Hook( "CDS", CopyDoorSettings );
	
end
	

   