Advertisement
charok4400

modded/custom TTTweaponplacer.lua

Aug 6th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.71 KB | None | 0 0
  1.  
  2. TOOL.Category = "Trouble in Terrorist Town"
  3. TOOL.Name = "TTT Weapon Placer"
  4. TOOL.Command = nil
  5. TOOL.ConfigName = ""
  6.  
  7. TOOL.ClientConVar["weapon"] = "weapon_zm_pistol"
  8. TOOL.ClientConVar["frozen"] = "0"
  9. TOOL.ClientConVar["replacespawns"] = "0"
  10.  
  11.  
  12. cleanup.Register("ttt_weapons")
  13.  
  14.  
  15. if CLIENT then
  16.    language.Add( "Tool_tttweaponplacer_name", "TTT Weapon Placer" )
  17.    language.Add( "Tool_tttweaponplacer_desc", "Spawn TTT weapon dummies and export their placement" )
  18.    language.Add( "Tool_tttweaponplacer_0", "Left click to spawn entity. Right click for matching ammo." )
  19.    language.Add("Cleanup_ttt_weapons", "TTT Dummy Weapons/ammo/spawns")
  20. end
  21.  
  22. local weps = {
  23.    weapon_zm_pistol = {name="Pistol", snd="item_ammo_pistol_ttt"},
  24.    weapon_zm_shotgun = {name="Shotgun", snd="item_box_buckshot_ttt"},
  25.    weapon_zm_mac10 = {name="MAC10", snd="item_ammo_smg1_ttt"},
  26.    weapon_zm_revolver = {name="Deagle", snd="item_ammo_revolver_ttt"},
  27.    weapon_zm_rifle = {name="Rifle", snd="item_ammo_357_ttt"},
  28.    weapon_zm_sledge = {name="HUGE249", snd=nil},
  29.    weapon_zm_molotov = {name="Fire nade", snd=nil},
  30.    
  31.    weapon_ttt_awp = {name="awp", snd="item_ammo_357_ttt"},
  32.    weapon_ttt_confgrenade = {name="Discombobulator", snd=nil},
  33.    weapon_ttt_smokegrenade = {name="Smoke nade", snd=nil},
  34.    weapon_ttt_m16 = {name="M16", snd="item_ammo_pistol_ttt"},
  35.    weapon_ttt_mp5 = {name="P90", snd="item_ammo_smg1_ttt"},
  36.    weapon_ttt_glock = {name="Glock", snd="item_ammo_pistol_ttt"},
  37.    weapon_ttt_p90 = {name="P90", snd="item_ammo_smg1_ttt"},
  38.    weapon_ttt_tmp = {name="TMP", snd="item_ammo_smg1_ttt"},
  39.    
  40.  
  41.    ttt_random_weapon = {name="Random weapon", snd=nil},
  42.    ttt_random_ammo = {name="Random ammo", snd=nil},
  43.  
  44.    ttt_playerspawn = {name="Player spawn", snd=nil}
  45. }
  46.  
  47. local mdls = {
  48.    weapon_zm_pistol = "models/weapons/w_pist_fiveseven.mdl",
  49.    weapon_zm_shotgun = "models/weapons/w_shot_xm1014.mdl",
  50.    weapon_zm_mac10 = "models/weapons/w_smg_mac10.mdl",
  51.    weapon_zm_revolver = "models/weapons/w_pist_deagle.mdl",
  52.    weapon_zm_rifle = "models/weapons/w_snip_scout.mdl",
  53.    weapon_zm_sledge = "models/weapons/w_mach_m249para.mdl",
  54.    weapon_zm_molotov = "models/weapons/w_eq_flashbang.mdl",
  55.    
  56.    weapon_ttt_confgrenade = "models/weapons/w_eq_fraggrenade.mdl",
  57.    weapon_ttt_smokegrenade = "models/weapons/w_eq_smokegrenade.mdl",
  58.    weapon_ttt_m16 = "models/weapons/w_rif_m4a1.mdl",
  59.    weapon_ttt_glock = "models/weapons/w_pist_glock18.mdl",
  60.    weapon_ttt_mp5 = "models/weapons/w_smg_mp5.mdl",
  61.    weapon_ttt_p90 = "models/weapons/w_smg_p90.mdl",
  62.    weapon_ttt_tmp = "models/weapons/w_smg_mp5.mdl",
  63.    weapon_ttt_awp = "models/weapons/w_snip_awp.mdl",
  64.  
  65.    ttt_random_weapon = "models/weapons/w_shotgun.mdl",
  66.    ttt_random_ammo = "models/Items/battery.mdl",
  67.  
  68.    item_ammo_pistol_ttt= "models/items/boxsrounds.mdl",
  69.    item_ammo_smg1_ttt= "models/items/boxmrounds.mdl",
  70.    item_ammo_revolver_ttt = "models/items/357ammo.mdl",
  71.    item_ammo_357_ttt = "models/items/357ammo.mdl",
  72.    item_box_buckshot_ttt = "models/items/boxbuckshot.mdl",
  73.  
  74.    ttt_playerspawn = "models/player.mdl"
  75. };
  76.  
  77. -- special colours for certain ents
  78. local colors = {
  79.    ttt_random_weapon = Color(255, 255, 0),
  80.    ttt_random_ammo = Color(0, 255, 0),
  81.    item_ammo_revolver_ttt = Color(255, 100, 100),
  82.    ttt_playerspawn = Color(0, 255, 0)
  83. };
  84.  
  85. local function DummyInit(s)
  86.    if colors[s:GetClass()] then
  87.       local c = colors[s:GetClass()]
  88.       s:SetColor(c.r, c.g, c.b, 255)
  89.    end
  90.  
  91.    s:SetCollisionGroup(COLLISION_GROUP_WEAPON)
  92.    s:SetSolid(SOLID_VPHYSICS)
  93.    s:SetMoveType(MOVETYPE_VPHYSICS)
  94.  
  95.    if s:GetClass() == "ttt_playerspawn" then
  96.       s:PhysicsInitBox(Vector(-18, -18, 0), Vector(18, 18, 66))
  97.    else
  98.       s:PhysicsInit(SOLID_VPHYSICS)
  99.    end
  100.    
  101.    s:SetModel(s.Model)
  102. end
  103.  
  104. for cls, mdl in pairs(mdls) do
  105.    local tbl = {
  106.       Type = "anim",
  107.       Model = Model(mdl),
  108.       Initialize = DummyInit
  109.    };
  110.  
  111.    scripted_ents.Register(tbl, cls, false)
  112. end
  113.  
  114. function TOOL:SpawnEntity(cls, trace)
  115.    local mdl = mdls[cls]
  116.  
  117.    if not cls or not mdl then return end
  118.  
  119.    local ent = ents.Create(cls)
  120.    ent:SetModel(mdl)
  121.    ent:SetPos(trace.HitPos)
  122.  
  123.    local tr = util.TraceEntity({start=trace.StartPos, endpos=trace.HitPos, filter=self:GetOwner()}, ent)
  124.    if tr.Hit then
  125.       ent:SetPos(tr.HitPos)
  126.    end
  127.  
  128.    ent:Spawn()
  129.  
  130.    ent:PhysWake()
  131.  
  132.    undo.Create("TTTWeapon")
  133.    undo.AddEntity(ent)
  134.    undo.SetPlayer(self:GetOwner())
  135.    undo.Finish()
  136.  
  137.    self:GetOwner():AddCleanup("ttt_weapons", ent)
  138. end
  139.  
  140. function TOOL:LeftClick( trace )
  141.    local cls = self:GetClientInfo("weapon")
  142.  
  143.    self:SpawnEntity(cls, trace)
  144. end
  145.  
  146. function TOOL:RightClick( trace )
  147.    local cls = self:GetClientInfo("weapon")
  148.    local info = weps[cls]
  149.    if not info then return end
  150.  
  151.    local ammo = info.snd
  152.    if not ammo then
  153.       self:GetOwner():ChatPrint("No matching ammo for this type!")
  154.       return
  155.    end
  156.  
  157.    self:SpawnEntity(info.snd, trace)
  158. end
  159.  
  160. function TOOL.BuildCPanel(panel) -- note that this is not a method, REAL NICE
  161.    panel:AddControl( "Header", { Text = "#Tool_tttweaponplacer_name", Description = "#Tool_tttweaponplacer_desc"})
  162.  
  163.    local opts = {}
  164.    for w, info in pairs(weps) do
  165.       opts[info.name] = {tttweaponplacer_weapon = w}
  166.    end
  167.  
  168.    panel:AddControl("ListBox", { Label = "Weapons", Height = "200", Options = opts } )
  169.  
  170.    panel:AddControl("Button", {Label="Report counts", Command="tttweaponplacer_count", Text="Count"})
  171.  
  172.    panel:AddControl("Label", {Text="Export", Description="Export weapon placements"})
  173.  
  174.    panel:AddControl("CheckBox", {Label="Replace existing player spawnpoints", Command="tttweaponplacer_replacespawns", Text="Replace spawns"})
  175.  
  176.    panel:AddControl( "Button",  { Label = "Export to file", Command = "tttweaponplacer_queryexport", Text = "Export"})
  177.  
  178.    panel:AddControl("Label", {Text="Import", Description="Import weapon placements"})
  179.  
  180.    panel:AddControl( "Button",  { Label = "Import from file", Command = "tttweaponplacer_queryimport", Text = "Import"})
  181.  
  182.    panel:AddControl("Button", {Label="Convert HL2 entities", Command = "tttweaponplacer_replacehl2", Text="Convert"})
  183.  
  184.    panel:AddControl("Button", {Label="Remove all existing weapon/ammo", Command = "tttweaponplacer_removeall", Text="Remove all existing items"})
  185. end
  186.  
  187. -- STOOLs not being loaded on client = headache bonanza
  188. if CLIENT then
  189.    function QueryFileExists()
  190.  
  191.       local map = string.lower(game.GetMap())
  192.       if not map then return end
  193.  
  194.       local fname = "ttt/maps/" .. map .. "_ttt.txt"
  195.  
  196.       if file.Exists(fname) then
  197.          Derma_StringRequest("File exists", "The file \"" .. fname .. "\" already exists. Save under a different filename? Leave unchanged to overwrite.",
  198.                              fname,
  199.                              function(txt)
  200.                                 RunConsoleCommand("tttweaponplacer_export", txt)
  201.                              end)
  202.       else
  203.          RunConsoleCommand("tttweaponplacer_export")
  204.       end
  205.    end
  206.  
  207.    function QueryImportName()
  208.       local map = string.lower(game.GetMap())
  209.       if not map then return end
  210.  
  211.       local fname = "ttt/maps/" .. map .. "_ttt.txt"
  212.  
  213.       Derma_StringRequest("Import", "What file do you want to import? Note that files meant for other maps will result in crazy things happening.",
  214.                           fname,
  215.                           function(txt)
  216.                              RunConsoleCommand("tttweaponplacer_import", txt)
  217.                           end)
  218.      
  219.    end
  220. else
  221.    -- again, hilarious things happen when this shit is used in mp
  222.    concommand.Add("tttweaponplacer_queryexport", function() BroadcastLua("QueryFileExists()") end)
  223.    concommand.Add("tttweaponplacer_queryimport", function() BroadcastLua("QueryImportName()") end)
  224. end
  225.  
  226. WEAPON_PISTOL = 1
  227. WEAPON_HEAVY = 2
  228. WEAPON_NADE = 3
  229. WEAPON_RANDOM = 4
  230.  
  231. PLAYERSPAWN = 5
  232.  
  233. local enttypes = {
  234.    weapon_zm_pistol = WEAPON_PISTOL,
  235.    weapon_zm_revolver = WEAPON_PISTOL,
  236.    weapon_ttt_glock = WEAPON_PISTOL,
  237.  
  238.    weapon_zm_mac10 = WEAPON_HEAVY,
  239.    weapon_zm_shotgun = WEAPON_HEAVY,
  240.    weapon_zm_rifle = WEAPON_HEAVY,
  241.    weapon_zm_sledge = WEAPON_HEAVY,
  242.    weapon_ttt_m16 = WEAPON_HEAVY,
  243.    weapon_ttt_awp = WEAPON_HEAVY,
  244.    weapon_ttt_mp5 = WEAPON_HEAVY,
  245.    weapon_ttt_p90 = WEAPON_HEAVY,
  246.    weapon_ttt_tmp = WEAPON_HEAVY,
  247.  
  248.    weapon_zm_molotov = WEAPON_NADE,
  249.    weapon_ttt_smokegrenade = WEAPON_NADE,
  250.    weapon_ttt_confgrenade = WEAPON_NADE,
  251.  
  252.    ttt_random_weapon = WEAPON_RANDOM,
  253.    
  254.    ttt_playerspawn = PLAYERSPAWN
  255. };
  256.  
  257. local function PrintCount(ply)
  258.    local count = {
  259.       [WEAPON_PISTOL] = 0,
  260.       [WEAPON_HEAVY] = 0,
  261.       [WEAPON_NADE] = 0,
  262.       [WEAPON_RANDOM] = 0,
  263.       [PLAYERSPAWN] = 0
  264.    };
  265.  
  266.    for cls, t in pairs(enttypes) do
  267.       for _, ent in pairs(ents.FindByClass(cls)) do
  268.          count[t] = count[t] + 1
  269.       end
  270.    end
  271.  
  272.    ply:ChatPrint("Entity count (use report_entities in console for more detail):")
  273.    ply:ChatPrint("Primary weapons: " .. count[WEAPON_HEAVY])
  274.    ply:ChatPrint("Secondary weapons: " .. count[WEAPON_PISTOL])
  275.    ply:ChatPrint("Grenades: " .. count[WEAPON_NADE])
  276.    ply:ChatPrint("Random weapons: " .. count[WEAPON_RANDOM])
  277.    ply:ChatPrint("Player spawns: " .. count[PLAYERSPAWN])
  278. end
  279. concommand.Add("tttweaponplacer_count", PrintCount)
  280.  
  281. -- This shit will break terribly in MP
  282. if SERVER or CLIENT then
  283.    -- Could just do a GLON dump, but it's nice if the "scripts" are sort of
  284.    -- human-readable so it's easy to go in and delete all pistols or something.
  285.    local function Export(ply, cmd, args)
  286.       if not IsValid(ply) then return end
  287.  
  288.       local map = string.lower(game.GetMap())
  289.  
  290.       if not map then return end
  291.  
  292.       --local frozen_only = GetConVar("tttweaponplacer_frozen"):GetBool()
  293.       local frozen_only = false
  294.  
  295.       -- Nice header, # is comment
  296.       local buf =  "# Trouble in Terrorist Town weapon/ammo placement overrides\n"
  297.       buf = buf .. "# For map: " .. map .. "\n"
  298.       buf = buf .. "# Exported by: " .. ply:Nick() .. "\n"
  299.  
  300.       -- Write settings ("setting: <name> <value>")
  301.       local rspwns = GetConVar("tttweaponplacer_replacespawns"):GetBool() and "1" or "0"
  302.       buf = buf .. "setting:\treplacespawns " .. rspwns .. "\n"
  303.  
  304.       local num = 0
  305.       for cls, mdl in pairs(mdls) do
  306.          for _, ent in pairs(ents.FindByClass(cls)) do
  307.             if IsValid(ent) then
  308.                if not frozen_only or not ent:GetPhysicsObject():IsMoveable() then
  309.                   num = num + 1
  310.                   buf = buf .. Format("%s\t%s\t%s\n", cls, tostring(ent:GetPos()), tostring(ent:GetAngles()))
  311.                end
  312.             end
  313.          end
  314.       end
  315.  
  316.       local fname = "ttt/maps/" .. map .. "_ttt.txt"
  317.  
  318.       if args[1] then
  319.          fname = args[1]
  320.       end
  321.      
  322.       file.Write(fname, buf)
  323.  
  324.       if not file.Exists(fname) then
  325.          ErrorNoHalt("Exported file not found. Bug?\n")
  326.       end
  327.  
  328.       ply:ChatPrint(num .. " placements saved to /garrysmod/data/" .. fname)
  329.    end
  330.    concommand.Add("tttweaponplacer_export", Export)
  331.  
  332.    local function SpawnDummyEnt(cls, pos, ang)
  333.       if not cls or not pos or not ang then return false end
  334.  
  335.       local mdl = mdls[cls]
  336.       if not mdl then return end
  337.  
  338.       local ent = ents.Create(cls)
  339.       ent:SetModel(mdl)
  340.       ent:SetPos(pos)
  341.       ent:SetAngles(ang)
  342.       ent:SetCollisionGroup(COLLISION_GROUP_WEAPON)
  343.       ent:SetSolid(SOLID_VPHYSICS)
  344.       ent:SetMoveType(MOVETYPE_VPHYSICS)
  345.       ent:PhysicsInit(SOLID_VPHYSICS)
  346.      
  347.       ent:Spawn()
  348.  
  349.       local phys = ent:GetPhysicsObject()
  350.       if IsValid(phys) then
  351.          phys:SetAngle(ang)
  352.       end
  353.    end
  354.  
  355.  
  356.    local function Import(ply, cmd, args)
  357.       if not IsValid(ply) then return end
  358.       local map = string.lower(game.GetMap())
  359.       if not map then return end
  360.  
  361.       local fname = "ttt/maps/" .. map .. "_ttt.txt"
  362.  
  363.       if args[1] then
  364.          fname = args[1]
  365.       end
  366.  
  367.       if not file.Exists(fname) then
  368.          ply:ChatPrint(fname .. " not found!")
  369.          return
  370.       end
  371.  
  372.       local buf = file.Read(fname)
  373.       local lines = string.Explode("\n", buf)
  374.       local num = 0
  375.       for k, line in ipairs(lines) do
  376.          if not string.match(line, "^#") and line != "" then
  377.             local data = string.Explode("\t", line)
  378.  
  379.             local fail = true -- pessimism
  380.  
  381.             if #data > 0 then
  382.                if data[1] == "setting:" and tostring(data[2]) then
  383.                   local raw = string.Explode(" ", data[2])
  384.                   RunConsoleCommand("tttweaponplacer_" .. raw[1], tonumber(raw[2]))
  385.  
  386.                   fail = false
  387.                   num = num - 1
  388.                elseif #data == 3 then
  389.                   local cls = data[1]
  390.                   local ang = nil
  391.                   local pos = nil
  392.                  
  393.                   local posraw = string.Explode(" ", data[2])
  394.                   pos = Vector(tonumber(posraw[1]), tonumber(posraw[2]), tonumber(posraw[3]))
  395.  
  396.                   local angraw = string.Explode(" ", data[3])
  397.                   ang = Angle(tonumber(angraw[1]), tonumber(angraw[2]), tonumber(angraw[3]))
  398.  
  399.                   fail = SpawnDummyEnt(cls, pos, ang)
  400.                end
  401.             end
  402.  
  403.             if fail then
  404.                ErrorNoHalt("Invalid line " .. k .. " in " .. fname .. "\n")
  405.             else
  406.                num = num + 1
  407.             end
  408.          end
  409.       end
  410.  
  411.       ply:ChatPrint("Spawned " .. num .. " dummy ents")
  412.    end
  413.    concommand.Add("tttweaponplacer_import", Import)
  414.  
  415.    local function RemoveAll(ply, cmd, args)
  416.       if not IsValid(ply) then return end
  417.  
  418.       local num = 0
  419.       local delete = function(ent)
  420.                         if not IsValid(ent) then return end
  421.                         print("\tRemoving", ent, ent:GetClass())
  422.                         ent:Remove()
  423.                         num = num + 1
  424.                      end
  425.  
  426.       print("Removing ammo...")
  427.       for k, ent in pairs(ents.FindByClass("item_*")) do
  428.          delete(ent)
  429.       end
  430.  
  431.       print("Removing weapons...")
  432.       for k, ent in pairs(ents.FindByClass("weapon_*")) do
  433.          delete(ent)
  434.       end
  435.  
  436.       ply:ChatPrint("Removed " .. num .. " weapon/ammo ents")
  437.    end
  438.    concommand.Add("tttweaponplacer_removeall", RemoveAll)
  439.  
  440.    local hl2_replace = {
  441.       ["item_ammo_pistol"] = "item_ammo_pistol_ttt",
  442.       ["item_box_buckshot"] = "item_box_buckshot_ttt",
  443.       ["item_ammo_smg1"] = "item_ammo_smg1_ttt",
  444.       ["item_ammo_357"] = "item_ammo_357_ttt",
  445.       ["item_ammo_357_large"] = "item_ammo_357_ttt",
  446.       ["item_ammo_revolver"] = "item_ammo_revolver_ttt", -- zm
  447.       ["item_ammo_ar2"] = "item_ammo_pistol_ttt",
  448.       ["item_ammo_ar2_large"] = "item_ammo_smg1_ttt",
  449.       ["item_ammo_smg1_grenade"] = "weapon_zm_pistol",
  450.       ["item_battery"] = "item_ammo_357_ttt",
  451.       ["item_healthkit"] = "weapon_zm_shotgun",
  452.       ["item_suitcharger"] = "weapon_zm_mac10",
  453.       ["item_ammo_ar2_altfire"] = "weapon_zm_mac10",
  454.       ["item_rpg_round"] = "item_ammo_357_ttt",
  455.       ["item_ammo_crossbow"] = "item_box_buckshot_ttt",
  456.       ["item_healthvial"] = "weapon_zm_molotov",
  457.       ["item_healthcharger"] = "item_ammo_revolver_ttt",
  458.       ["item_ammo_crate"] = "weapon_ttt_confgrenade",
  459.       ["item_item_crate"] = "ttt_random_ammo",
  460.       ["weapon_smg1"] = "weapon_zm_mac10",
  461.       ["weapon_shotgun"] = "weapon_zm_shotgun",
  462.       ["weapon_ar2"] = "weapon_ttt_m16",
  463.       ["weapon_357"] = "weapon_zm_rifle",
  464.       ["weapon_crossbow"] = "weapon_zm_pistol",
  465.       ["weapon_rpg"] = "weapon_zm_sledge",
  466.       ["weapon_slam"] = "item_ammo_pistol_ttt",
  467.       ["weapon_frag"] = "weapon_zm_revolver",
  468.       ["weapon_crowbar"] = "weapon_zm_molotov"
  469.    };
  470.  
  471.    local function ReplaceSingle(ent, newname)
  472.       if ent:GetPos() == vector_origin then
  473.          return false
  474.       end
  475.  
  476.       if ent:IsWeapon() and IsValid(ent:GetOwner()) and ent:GetOwner():IsPlayer() then
  477.          return false
  478.       end
  479.  
  480.       ent:SetSolid(SOLID_NONE)
  481.  
  482.       local rent = ents.Create(newname)
  483.       rent:SetModel(mdls[newname])
  484.       rent:SetPos(ent:GetPos())
  485.       rent:SetAngles(ent:GetAngles())
  486.       rent:Spawn()
  487.  
  488.       rent:Activate()
  489.       rent:PhysWake()
  490.  
  491.       ent:Remove()
  492.       return true
  493.    end
  494.  
  495.    local function ReplaceHL2Ents(ply, cmd, args)
  496.       if not IsValid(ply) then return end
  497.  
  498.       local c = 0
  499.       for _, ent in pairs(ents.GetAll()) do
  500.          local rpl = hl2_replace[ent:GetClass()]
  501.          if rpl then
  502.             local success = ReplaceSingle(ent, rpl)
  503.             if success then
  504.                c = c + 1
  505.             end
  506.          end
  507.       end
  508.  
  509.       ply:ChatPrint("Replaced " .. c .. " HL2 entities with TTT versions.")
  510.    end
  511.    concommand.Add("tttweaponplacer_replacehl2", ReplaceHL2Ents)
  512. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement