Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.49 KB | None | 0 0
  1. --[[
  2.     ~ Fading Door STool ~
  3.     ~ Based on Conna's, but this time it works. ~
  4.     ~ Lexi ~
  5. --]]
  6.  
  7. --[[ Tool Related Settings ]]--
  8. TOOL.Category = "Lexical Tools";
  9. TOOL.Name = "#Fading Doors (ByB)";
  10.  
  11. TOOL.ClientConVar["key"] = "5"
  12. TOOL.ClientConVar["toggle"] = "0"
  13. TOOL.ClientConVar["reversed"] = "0"
  14.  
  15. local function checkTrace(tr)
  16.     return (tr.Entity and tr.Entity:IsValid() and not (tr.Entity:IsPlayer() or tr.Entity:IsNPC() or tr.Entity:IsVehicle() or tr.HitWorld));
  17. end
  18.  
  19. if (CLIENT) then
  20.     usermessage.Hook("FadingDoorHurrah!", function()
  21.         GAMEMODE:AddNotify("Fading door has been created!", NOTIFY_GENERIC, 10);
  22.         surface.PlaySound ("ambient/water/drip" .. math.random(1, 4) .. ".wav");
  23.     end);
  24.     language.Add("Tool_byb_fading_doors_name", "Fading Doors (ByB Edition)");
  25.     language.Add("Tool_byb_fading_doors_desc", "Makes anything into a fadable door");
  26.     language.Add("Tool_byb_fading_doors_0", "Click on something to make it a fading door. Reload to set it back to normal");
  27.     language.Add("Undone_byb_fading_door", "Undone Fading Door");
  28.    
  29.     function TOOL:BuildCPanel()
  30.         self:AddControl("Header",   {Text = "#Tool_byb_fading_doors_name", Description = "#Tool_byb_fading_doors_desc"});
  31.         self:AddControl("CheckBox", {Label = "Reversed (Starts invisible, becomes solid)", Command = "byb_fading_doors_reversed"});
  32.         self:AddControl("CheckBox", {Label = "Toggle Active", Command = "byb_fading_doors_toggle"});
  33.         self:AddControl("Numpad",   {Label = "Button", ButtonSize = "22", Command = "byb_fading_doors_key"});
  34.     end
  35.    
  36.     TOOL.LeftClick = checkTrace;
  37.    
  38.     return;
  39. end
  40.  
  41. --[[ Disable the numpad ]]--
  42. concommand.Remove("+gm_special")
  43. concommand.Remove("-gm_special")
  44. concommand.Add("+gm_special",function(ply)
  45.     ply:ChatPrint("Sorry, but using the numpad to activate things has been disabled on this server. Please use a button or keypad instead.");
  46.     ply:ChatPrint("You can still use the numpad to enter passwords on keypads very fast, and wired numpad outputs will still work as they did before.");
  47. end);
  48. concommand.Add("-gm_special",function() end);
  49. --umsg.PoolString("FadingDoorHurrah!");
  50.  
  51. local function fadeActivate(self)
  52.     if (self.fadeActive) then return; end
  53.     self.fadeActive = true;
  54.     self.fadeMaterial = self:GetMaterial();
  55.     self:SetMaterial("sprites/heatwave")
  56.     self:DrawShadow(false)
  57.     self:SetNotSolid(true)
  58.     --self:SetCollisionGroup(COLLISION_GROUP_WORLD)
  59.     local phys = self:GetPhysicsObject();
  60.     if (IsValid(phys)) then
  61.         self.fadeMoveable = phys:IsMoveable();
  62.         phys:EnableMotion(false);
  63.     end
  64.     if (WireLib) then
  65.         Wire_TriggerOutput(self,  "FadeActive",  1);
  66.     end
  67. end
  68.  
  69. local function fadeDeactivate(self)
  70.     if (not self.fadeActive) then return; end
  71.     self.fadeActive = false;
  72.     self:SetMaterial(self.fadeMaterial or "");
  73.     self:DrawShadow(true);
  74.     self:SetNotSolid(false);
  75.     --self:SetCollisionGroup(COLLISION_GROUP_NONE);
  76.     local phys = self:GetPhysicsObject();
  77.     if (IsValid(phys)) then
  78.         phys:EnableMotion(self.fadeMoveable or false);
  79.     end
  80.     if (WireLib) then
  81.         Wire_TriggerOutput(self,  "FadeActive",  0);
  82.     end
  83. end
  84.  
  85. local function fadeToggleActive(self)
  86.     if (self.fadeActive) then
  87.         self:fadeDeactivate();
  88.     else
  89.         self:fadeActivate();
  90.     end
  91. end
  92.  
  93. local function killTimer(ent)
  94.     if (not (IsValid(ent) and ent.fadeToggleActive)) then
  95.         return;
  96.     end
  97.     ent.fadeByB5SecTimer = false;
  98.     if (not (ent.fadeInputActive or ent.fadeToggle)) then
  99.         ent:fadeToggleActive();
  100.     end
  101. end
  102.  
  103. --[[
  104.     These are to prevent multiple concurrent on/off calls from glitching the door
  105. --]]
  106. local function fadeInputOn(self)
  107.     if (self.fadeInputActive or self.fadeByB5SecTimer) then
  108.         return;
  109.     end
  110.     self.fadeInputActive = true;
  111.     self.fadeByB5SecTimer = true;
  112.     timer.Simple(5,killTimer,self);
  113.     self:fadeToggleActive();
  114. end
  115.  
  116. local function fadeInputOff(self)
  117.     if (not self.fadeInputActive) then
  118.         return;
  119.     end
  120.     self.fadeInputActive = false;
  121.     if (self.fadeToggle or self.fadeByB5SecTimer) then
  122.         return;
  123.     end
  124.     self:fadeToggleActive();
  125. end
  126.  
  127. local function onDown(ply, ent)
  128.     if (not (ent:IsValid() and ent.fadeToggleActive)) then
  129.         return;
  130.     end
  131.     ent:fadeInputOn();
  132. end
  133. numpad.Register("ByB Fading Doors onDown", onDown);
  134.  
  135.  
  136. local function onUp(ply, ent)
  137.     if (not (ent:IsValid() and ent.fadeToggleActive)) then
  138.         return;
  139.     end
  140.     ent:fadeInputOff();
  141. end
  142. numpad.Register("ByB Fading Doors onUp", onUp);
  143.  
  144. -- Fuck you wire.
  145. local function doWireInputs(ent)
  146.     local inputs = ent.Inputs;
  147.     if (not inputs) then
  148.         Wire_CreateInputs(ent, {"Fade"});
  149.         return;
  150.     end
  151.     local names, types, descs = {}, {}, {};
  152.     local num;
  153.     for _, data in pairs(inputs) do
  154.         num = data.Num;
  155.         names[num] = data.Name;
  156.         types[num] = data.Type;
  157.         descs[num] = data.Desc;
  158.     end
  159.     table.insert(names, "Fade");
  160.     WireLib.AdjustSpecialInputs(ent, names, types, descs);
  161. end
  162.  
  163. local function doWireOutputs(ent)
  164.     local outputs = ent.Outputs;
  165.     if (not outputs) then
  166.         Wire_CreateOutputs(ent, {"FadeActive"});
  167.         return;
  168.     end
  169.     local names, types, descs = {}, {}, {};
  170.     local num;
  171.     for _, data in pairs(outputs) do
  172.         num = data.Num;
  173.         names[num] = data.Name;
  174.         types[num] = data.Type;
  175.         descs[num] = data.Desc;
  176.     end
  177.     table.insert(names, "FadeActive");
  178.     WireLib.AdjustSpecialOutputs(ent, names, types, descs);
  179. end
  180.  
  181. local function TriggerInput(self, name, value, ...)
  182.     if (name == "Fade") then
  183.         if (value == 0) then
  184.             if (self.fadePrevWireOn) then
  185.                 self.fadePrevWireOn = false;
  186.                 self:fadeInputOff();
  187.             end
  188.         else
  189.             if (not self.fadePrevWireOn) then
  190.                 self.fadePrevWireOn = true;
  191.                 self:fadeInputOn();
  192.             end
  193.         end
  194.     elseif (self.fadeTriggerInput) then
  195.         return self:fadeTriggerInput(name, value, ...);
  196.     end
  197. end
  198.  
  199. local function PreEntityCopy(self)
  200.     local info = WireLib.BuildDupeInfo(self)
  201.     if (info) then
  202.         duplicator.StoreEntityModifier(self, "WireDupeInfo", info);
  203.     end
  204.     if (self.wireSupportPreEntityCopy) then
  205.         self:wireSupportPreEntityCopy();
  206.     end
  207. end
  208.  
  209. local function PostEntityPaste(self, ply, ent, ents)
  210.     if (self.EntityMods and self.EntityMods.WireDupeInfo) then
  211.         WireLib.ApplyDupeInfo(ply, self, self.EntityMods.WireDupeInfo, function(id) return ents[id]; end);
  212.     end
  213.     if (self.wireSupportPostEntityPaste) then
  214.         self:wireSupportPostEntityPaste(ply, ent, ents);
  215.     end
  216. end
  217.    
  218.  
  219. local function onRemove(self)
  220.     numpad.Remove(self.fadeUpNum);
  221.     numpad.Remove(self.fadeDownNum);
  222. end
  223.  
  224. -- Fer Duplicator
  225. local function dooEet(ply, ent, stuff)
  226.     if (ent.isFadingDoor) then
  227.         ent:fadeDeactivate();
  228.         onRemove(ent)
  229.     else
  230.         ent.isFadingDoor = true;
  231.         ent.fadeActivate = fadeActivate;
  232.         ent.fadeDeactivate = fadeDeactivate;
  233.         ent.fadeToggleActive = fadeToggleActive;
  234.         ent.fadeInputOn = fadeInputOn;
  235.         ent.fadeInputOff = fadeInputOff;
  236.         ent:CallOnRemove("Fading Doors", onRemove);
  237.         if (WireLib) then
  238.             doWireInputs(ent);
  239.             doWireOutputs(ent);
  240.             ent.fadeTriggerInput = ent.fadeTriggerInput or ent.TriggerInput;
  241.             ent.TriggerInput = TriggerInput;
  242.             if (not (ent.IsWire or ent.addedWireSupport)) then -- Dupe Support
  243.                 ent.wireSupportPreEntityCopy = ent.PreEntityCopy;
  244.                 ent.PreEntityCopy = PreEntityCopy;
  245.                 ent.wireSupportPostEntityPaste = ent.PostEntityPaste;
  246.                 ent.PostEntityPaste = PostEntityPaste;
  247.                 ent.addedWireSupport = true
  248.             end            
  249.         end
  250.     end
  251.     ent.fadeUpNum = numpad.OnUp(ply, stuff.key, "ByB Fading Doors onUp", ent);
  252.     ent.fadeDownNum = numpad.OnDown(ply, stuff.key, "ByB Fading Doors onDown", ent);
  253.     ent.fadeToggle = stuff.toggle;
  254.     if (stuff.reversed) then
  255.         ent:fadeActivate();
  256.     end
  257.     duplicator.StoreEntityModifier(ent, "Fading Door", stuff);
  258.     return true;
  259. end
  260.  
  261. duplicator.RegisterEntityModifier("Fading Door", dooEet);
  262.  
  263. if (not FadingDoor) then
  264.     local function legacy(ply, ent, data)
  265.         return dooEet(ply, ent, {
  266.             key      = data.Key;
  267.             toggle   = data.Toggle;
  268.             reversed = data.Inverse;
  269.         });
  270.     end
  271.     duplicator.RegisterEntityModifier("FadingDoor", legacy);
  272. end
  273.  
  274. local function doUndo(undoData, ent)
  275.     if (IsValid(ent) and ent.isFadingDoor) then
  276.         onRemove(ent); -- Get rid of the numpad inputs.
  277.         ent:fadeDeactivate(); -- Ensure the doors are turned off when we remove 'em :D
  278.         ent.isFadingDoor = false; -- We don't actually delete the fading door functions, to save time.
  279.         duplicator.ClearEntityModifier(ent, "Fading Door"); -- Make sure we don't unexpectedly re-add removed doors when duped.
  280.         -- [[ 'Neatly' remove the Wire inputs/outputs without disturbing the pre-existing ones. ]] --
  281.         if (WireLib) then
  282.             ent.TriggerInput = ent.fadeTriggerInput; -- Purge our input checker
  283.             if (ent.Inputs) then
  284.                 Wire_Link_Clear(ent, "Fade");
  285.                 ent.Inputs['Fade'] = nil;
  286.                 WireLib._SetInputs(ent);
  287.             end if (ent.Outputs) then
  288.                 local port = ent.Outputs['FadeActive']
  289.                 if (port) then
  290.                     for i,inp in ipairs(port.Connected) do -- From WireLib.lua: -- fix by Syranide: unlinks wires of removed outputs
  291.                         if (inp.Entity:IsValid()) then
  292.                             Wire_Link_Clear(inp.Entity, inp.Name)
  293.                         end
  294.                     end
  295.                 end
  296.                 ent.Outputs['FadeActive'] = nil;
  297.                 WireLib._SetOutputs(ent);
  298.             end
  299.         end
  300.         return true;
  301.     end
  302.     return false;
  303. end
  304.  
  305. function TOOL:LeftClick(tr)
  306.     if (not checkTrace(tr)) then
  307.         return false;
  308.     end
  309.     local ent = tr.Entity;
  310.     local ply = self:GetOwner();
  311.     dooEet(ply, ent, {
  312.         key      = self:GetClientNumber("key");
  313.         toggle   = self:GetClientNumber("toggle") == 1;
  314.         reversed = self:GetClientNumber("reversed") == 1;
  315.     });
  316.     undo.Create("byb_fading_door");
  317.         undo.AddFunction(doUndo, ent);
  318.         undo.SetPlayer(ply);
  319.     undo.Finish();
  320.    
  321.     SendUserMessage("FadingDoorHurrah!", ply);
  322.     return true
  323. end
  324.  
  325. function TOOL:Reload(tr)
  326.     return doUndo(_,tr.Entity);
  327. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement