Advertisement
Guest User

Untitled

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