Advertisement
pogh10

Untitled

May 23rd, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.26 KB | None | 0 0
  1. local Version = "1.0.7"
  2. --[[
  3.  
  4. THIS IS NOT A CONFIGURATION FILE, USE config.lua!
  5. You shouldn't need to edit this file unless you know what you are doing, or have contacted me.
  6. Changing something in here could break the entire Skill Point system.
  7.  
  8. Avaliable Functions:
  9. ST_ValidateAmt(amt) -- Validates the given points are valid.
  10. Player:ST_GetLevel() -- returns the players number.
  11. Player:ST_SetSkills() --Sets the clients skills (e.g setting their jump height on spawn), currently called with PlayerSpawn hook.
  12.  
  13. -- Player Networking --
  14. Player:ST_GetSkills() --Syncs the client with the server for their Levels/XP and Skill Points
  15.  
  16. -- Player XP Functions --
  17. Player:ST_GiveXP(amt) --Give Set amount of XP
  18. Player:ST_TakeXP(amt) --Take Set amount of XP
  19. Player:ST_CheckXP() --Checks if the players XP is enough to gain the next level
  20.  
  21. -- Player Skill Point Functions --
  22. Player:ST_GiveSkillPoints(amt) --Give set amount of Skill Points
  23. Player:ST_TakeSkillPoints(amt) --Take set amount of Skill Points
  24.  
  25. ]]--
  26.  
  27. include("config.lua")
  28.  
  29. local Player = FindMetaTable('Player')
  30.  
  31. local MenuKey = {}
  32. MenuKey["F1"] = "ShowHelp"
  33. MenuKey["F2"] = "ShowTeam"
  34. MenuKey["F3"] = "ShowSpare1"
  35. MenuKey["F4"] = "ShowSpare2"
  36.  
  37. local Skills = {
  38. { Name = "Jump", Value = 0, Icon = "STIcons/jump-32.png" },
  39. { Name = "Sprint", Value = 0, Icon = "STIcons/running-32.png"},
  40. { Name = "Health", Value = 0, Icon = "STIcons/heart-32.png"},
  41. { Name = "Armor", Value = 0, Icon = "STIcons/shield-32.png"},
  42. }
  43.  
  44. Level = {}
  45. for i=0, 100 do Level[i] = math.Round(30*math.pow(i,ST_DifMul)) end
  46.  
  47. --[[ Network Strings, needed and shouldn't be touched ]]--
  48. util.AddNetworkString('ST_Menu')
  49. util.AddNetworkString('ST_SkillPoints')
  50. util.AddNetworkString('ST_Skills')
  51. util.AddNetworkString('ST_SpentPoint')
  52. util.AddNetworkString('ST_RemovePoint')
  53. util.AddNetworkString('ST_SendAll')
  54. util.AddNetworkString('ST_Notification')
  55. util.AddNetworkString('ST_Ding')
  56.  
  57. hook.Add("PlayerInitialSpawn", "STInitialSpawn", function(ply)
  58. PlayerJumpHeight = ply:GetJumpPower()
  59. PlayerRunSpeed = ply:GetRunSpeed()
  60. ply:ST_GetSkills()
  61. ply:ST_GiveXPTime()
  62. SendAllLevels()
  63. end)
  64. hook.Add("PlayerConnect", "UpdateLevels", function() SendAllLevels() end)
  65. hook.Add(MenuKey[ST_MenuKey], "ST_Menu", function(ply) ply:ST_Menu() end)
  66. hook.Add("ST_LevelUp", "UpdateLevels", function(ply, level) print(ply, level) end)
  67. hook.Add("PlayerSpawn", "SetSkills", function(ply) ply:ST_SetSkills() end)
  68. if ST_TAKED then
  69. hook.Add("PlayerDeath", "TakeXPOnDeath", function(ply,inflictor,attacker) ply:ST_TakeXP(ST_DAMT) end)
  70. end
  71. --if ST_NPCK then
  72. -- hook.Add("OnNPCKilled", "TakeXPOnDeath", function(npc,ply,inflictor) ply:ST_GiveXP(ST_NPCKAMT) end)
  73. --end
  74.  
  75. local NPC_XPGain = {}
  76. NPC_XPGain["npc_dwarven_spider"] = 25
  77. NPC_XPGain["npc_draugr"] = 35
  78. NPC_XPGain["npc_mudcrab"] = 5
  79. NPC_XPGain["npc_dragon_paarthurnax"] = 100
  80. NPC_XPGain["npc_dwarven_centurion"] = 250
  81. NPC_XPGain["npc_dwarven_sphere"] = 105
  82. NPC_XPGain["npc_mudcrab_legendary"] = 75
  83. NPC_XPGain["npc_draugr_deathlord"] = 75
  84. --NPC_XPGain["npc_headcrab_black"] = 9
  85. --NPC_XPGain["npc_antlion"] = 7
  86. --NPC_XPGain["npc_antlionguard"] = 1500
  87. NPC_XPGain["default"] = 5
  88.  
  89. -- Start Killing NPC Exp Gain
  90. function EXPGainNpc(npc, killer, weapon)
  91. if killer:IsPlayer() and npc:IsNPC() then
  92. local NPCExpGained = NPC_XPGain[npc:GetClass()] or NPC_XPGain["default"] or 5
  93. killer:ST_GiveXP,killer:GetNWInt("EXP") + NPCExpGained)
  94. -- Exp Gain Complete
  95. end
  96. end
  97. hook.Add( "OnNPCKilled", "NpcExpGain", EXPGainNpc )
  98. -- End Killing NPC Exp Gain
  99.  
  100.  
  101. net.Receive('ST_RemovePoint', function(length, ply)
  102.  
  103. ply:ST_GiveSkillPoints(net.ReadInt(2))
  104. ply:ST_UpdateSkillTable(net.ReadInt(5), net.ReadInt(5))
  105.  
  106. end)
  107.  
  108. function Player:ST_Notification(msg, type, time)
  109.  
  110. net.Start('ST_Notification')
  111. net.WriteString(msg)
  112. net.WriteString(type)
  113. net.WriteInt(time, 32)
  114. net.Send(self)
  115.  
  116. end
  117.  
  118. --[[ Starts the net message to open the menu ]]--
  119. function Player:ST_Menu()
  120. net.Start('ST_Menu')
  121. net.Send(self)
  122. end
  123.  
  124.  
  125. --[[ Validates that the given points is a valid number, should be used anywhere when setting points ]]--
  126. function ST_ValidateAmt(amt)
  127.  
  128. if (tonumber(amt) == nil or amt < 0) then --We want to make sure it's a valid number and is not below 0
  129. amt = 0 --If it's invalid or below 0, we will just set the points to 0
  130. end
  131. return math.floor(amt) --Stops us having decimal numbers
  132.  
  133. end
  134.  
  135. function Player:ST_GetLevel()
  136.  
  137. return self.ST_Level
  138.  
  139. end
  140.  
  141.  
  142. --[[
  143.  
  144. XP System
  145.  
  146. ]]--
  147. function Player:ST_GiveXP(amt)
  148. self.ST_XPAmt = self.ST_XPAmt + ST_ValidateAmt(amt)
  149. self:SetPData("ST_XPAmt", self.ST_XPAmt) --Save the players skill points on the server db
  150. self:ST_UpdatePoints() --Update client with his new amount of skill points
  151. self:ST_CheckXP()
  152. self:ST_Notification("Gained "..amt.." XP!", "general", 3)
  153. end
  154.  
  155. function Player:ST_TakeXP(amt)
  156.  
  157. self.ST_XPAmt = self.ST_XPAmt - ST_ValidateAmt(amt)
  158. self:SetPData("ST_XPAmt", self.ST_XPAmt) --Save the players skill points on the server db
  159. self:ST_UpdatePoints() --Update client with his new amount of skill points
  160. self:ST_CheckXP()
  161. self:ST_Notification("Lost "..amt.." XP!", "general", 3)
  162.  
  163. end
  164.  
  165. function Player:ST_SetXP(amt)
  166.  
  167. self.ST_XPAmt = ST_ValidateAmt(amt)
  168. self:SetPData("ST_XPAmt", self.ST_XPAmt) --Save the players skill points on the server db
  169. self:ST_UpdatePoints() --Update client with his new amount of skill points
  170. self:ST_CheckXP()
  171.  
  172. end
  173.  
  174. function Player:ST_CheckXP()
  175.  
  176. if self.ST_XPAmt >= Level[tonumber(self.ST_Level)] then
  177. self:ST_GiveSkillPoints(1)
  178. if self.ST_XPAmt > Level[tonumber(self.ST_Level)] then
  179. self:ST_SetXP(self.ST_XPAmt - Level[tonumber(self.ST_Level)])
  180. else
  181. self:ST_SetXP(0)
  182. end
  183. self.ST_Level = self.ST_Level + 1
  184. self:SetPData("ST_Level", self.ST_Level + 1) --Save the players skill points on the server db
  185. self.ST_XPNeed = Level[tonumber(self.ST_Level)]
  186. net.Start('ST_Ding')
  187. net.Send(self)
  188. self:ST_UpdatePoints() --Update client with his new amount of skill points
  189. self:ST_Notification("Leveled Up!", "general", 3)
  190. hook.Call( "ST_LevelUp", nil, self:Nick(), self.ST_Level)
  191. elseif self.ST_XPAmt < 0 then
  192. if tonumber(self.ST_SkillPoints) > 0 then
  193. self:ST_TakeSkillPoints(1)
  194. end
  195. if tonumber(self.ST_Level) > 2 then
  196. self.ST_Level = self.ST_Level - 1
  197. self:SetPData("ST_Level", self.ST_Level - 1)
  198. self:ST_Notification("Lost A Level!", "general", 3)
  199. end
  200. self.ST_XPNeed = Level[tonumber(self.ST_Level)]
  201. self:ST_SetXP(self.ST_XPNeed + self.ST_XPAmt)
  202. hook.Call( "ST_LevelUp", nil, self:Nick(), self.ST_Level)
  203. end
  204.  
  205. end
  206.  
  207.  
  208. --[[
  209.  
  210. Skill Points
  211.  
  212. ]]--
  213. function Player:ST_GiveSkillPoints(amt)
  214.  
  215. self.ST_SkillPoints = self.ST_SkillPoints + ST_ValidateAmt(amt)
  216. self:SetPData("ST_SkillPoints", self.ST_SkillPoints) --Save the players skill points on the server db
  217. self:ST_UpdatePoints() --Update client with his new amount of skill points
  218. self:ST_Notification("Gained "..amt.." Skill Point(s)!", "general", 3)
  219.  
  220. end
  221.  
  222. function Player:ST_TakeSkillPoints(amt)
  223.  
  224. self.ST_SkillPoints = self.ST_SkillPoints - ST_ValidateAmt(amt)
  225. self:SetPData("ST_SkillPoints", self.ST_SkillPoints)
  226. self:ST_UpdatePoints()
  227. self:ST_Notification("Spent "..amt.." Skill Point(s)!", "general", 3)
  228.  
  229. end
  230.  
  231.  
  232. --[[
  233.  
  234. Networking for Skill Points, Levels and skills.
  235.  
  236. ]]--
  237. function Player:ST_UpdateSkillTable(skill, value)
  238.  
  239. self.ST_Skills[skill].Value = value
  240. self:SetPData("ST_Skills", util.TableToJSON(self.ST_Skills))
  241.  
  242. end
  243.  
  244. function Player:ST_UpdatePoints()
  245.  
  246. net.Start('ST_SkillPoints')
  247. net.WriteEntity(self)
  248. net.WriteInt(self.ST_SkillPoints, 32)
  249. net.WriteInt(self.ST_XPAmt, 32)
  250. net.WriteInt(self.ST_Level, 32)
  251. net.WriteInt(self.ST_XPNeed or 0, 32)
  252. net.Send(self)
  253.  
  254. SendAllLevels()
  255.  
  256. end
  257.  
  258. function Player:ST_UpdateSkills()
  259.  
  260. net.Start('ST_Skills')
  261. net.WriteEntity(self)
  262. net.WriteTable(self.ST_Skills)
  263. net.Send(self)
  264.  
  265. end
  266.  
  267. function SendAllLevels()
  268.  
  269. for k,ply in pairs(player.GetAll()) do
  270. ST_SendAll(ply)
  271. end
  272.  
  273. end
  274.  
  275. function ST_SendAll(ply)
  276.  
  277. net.Start('ST_SendAll')
  278. net.WriteEntity(ply)
  279. net.WriteInt(ply.ST_Level, 32)
  280. net.Broadcast()
  281.  
  282. end
  283.  
  284. function Player:ST_GiveXPTime()
  285. if ST_XPOnPlay then
  286. timer.Create(self:UniqueID(), ST_XPPlayTime * 60, 0, function() self:ST_GiveXP(ST_XPPlayAmt) end)
  287.  
  288. local function RemoveTimer(ply)
  289. timer.Remove(ply:UniqueID())
  290. end
  291. hook.Add("PlayerDisconnected", "RemoveTimerOnLeave", RemoveTimer)
  292. end
  293. end
  294.  
  295. function Player:ST_GetSkills()
  296.  
  297. if self:GetPData("ST_Skills", "nil") == "nil" then
  298. self:SetPData("ST_Skills", util.TableToJSON(Skills))
  299. end
  300. self.ST_SkillPoints = self:GetPData("ST_SkillPoints", 1)
  301. self.ST_Skills = util.JSONToTable(self:GetPData("ST_Skills"))
  302. self.ST_XPAmt = self:GetPData("ST_XPAmt", 0)
  303. self.ST_Level = self:GetPData("ST_Level", 1)
  304. self.ST_XPNeed = Level[tonumber(self.ST_Level)]
  305. self:ST_UpdatePoints()
  306. self:ST_UpdateSkills()
  307. SendAllLevels()
  308.  
  309. end
  310.  
  311. function Player:ST_SetSkills()
  312.  
  313. timer.Simple( 0.1,
  314. function()
  315. self:SetHealth(self:Health() + self.ST_Skills[3].Value * 10)
  316. self:SetRunSpeed(PlayerRunSpeed + self.ST_Skills[2].Value * 10)
  317. self:SetJumpPower(PlayerJumpHeight + self.ST_Skills[1].Value * 10)
  318. self:SetArmor(self:Armor() + self.ST_Skills[4].Value * 10)
  319. end)
  320.  
  321. end
  322.  
  323. function XPOnKill( victim, killer, weapon )
  324. if ST_XPOnKill and killer:IsPlayer() and killer != victim then
  325. killer:ST_GiveXP(ST_XPAmtOnKill)
  326. end
  327. end
  328. hook.Add( "PlayerDeath", "GiveXPOnKill", XPOnKill )
  329.  
  330.  
  331. local function CheckForGamemodes()
  332. if engine.ActiveGamemode() == "terrortown" then
  333. print("ST Detected Trouble in Terrorist Town!")
  334. hook.Add("TTTBeginRound", "SetSkills", function() for _,ply in pairs(player.GetAll()) do ply:ST_SetSkills() end end)
  335. function TTTXPOnWin(result)
  336. if ST_TTTWin then
  337. if result == WIN_TRAITOR then
  338. for k,ply in pairs(player.GetAll()) do
  339. if ply:IsTraitor() and ply:Alive() then
  340. ply:ST_GiveXP(ST_TTTAmt)
  341. end
  342. end
  343. elseif result == WIN_TIMELIMIT or result == WIN_INNOCENT then
  344. for k,ply in pairs(player.GetAll()) do
  345. if !(ply:IsTraitor() and ply:Alive()) then
  346. ply:ST_GiveXP(ST_TTTAmt)
  347. end
  348. end
  349. end
  350. end
  351. end
  352. hook.Add("TTTEndRound", "GiveXPOnWin", TTTXPOnWin)
  353.  
  354. elseif engine.ActiveGamemode() == "zombiesurvival" then
  355. print("ST Detected Zombie Survival!")
  356. local function GetWinner(winner)
  357. for _,ply in pairs(team.GetPlayers(winner)) do
  358. if ply:Alive() and ST_ZMWAMT != 0 then
  359. ply:ST_GiveXP(ST_ZMWAMT)
  360. end
  361. end
  362. end
  363. hook.Add( "PostEndRound", "GetWinner", GetWinner )
  364.  
  365. local function HumanKilledZombie(victim, attacker, dmginfo, assister, assistfrags, headshot)
  366. if ST_ZMHAMT != 0 then
  367. attacker:ST_GiveXP(ST_ZMHAMT)
  368. end
  369. end
  370. hook.Add( "PostHumanKilledZombie", "Human Killed a Zombie", HumanKilledZombie )
  371.  
  372. local function ZombieKilledHuman(victim, attacker, dmginfo, headshot, wassuicide)
  373. if ST_ZMZAMT != 0 then
  374. attacker:ST_GiveXP(ST_ZMZAMT)
  375. end
  376. end
  377. hook.Add( "PostZombieKilledHuman", "Zombie Killed a Human", ZombieKilledHuman )
  378.  
  379. local function PlayerRedeemed(ply)
  380. if ST_ZMRAMT != 0 then
  381. ply:ST_GiveXP(ST_ZMRAMT)
  382. end
  383. end
  384. hook.Add( "PlayerRedeemed", "Player Redeemed", PlayerRedeemed )
  385. elseif engine.ActiveGamemode() == "deathrun" then
  386. function GetWinner()
  387. if round == ROUND_ENDING then
  388. if winner != 123 then
  389. for _,ply in pairs(team.GetPlayers(winner)) do
  390. if ply:Alive() then
  391. ply:ST_GiveXP(ST_DRWAMT)
  392. end
  393. end
  394. end
  395. end
  396. end
  397. hook.Add( "OnRoundSet", "GetRoundWinner", GetWinner)
  398. end
  399. timer.Simple(5, function() ST_CheckVersion() end)
  400. end
  401. hook.Add( "Initialize", "Check for gamemodes", CheckForGamemodes )
  402.  
  403.  
  404.  
  405. --[[
  406.  
  407. This should only be ran to clear ALL saved stats, irriversable unless you have backed up sv.db
  408. This also only clears current connected players stats.
  409.  
  410. ]]--
  411. function ClearSkills(pl,command,args)
  412.  
  413. if pl:IsAdmin() then
  414. for k,ply in pairs(player.GetAll()) do
  415. ply:RemovePData("ST_SkillPoints")
  416. ply:RemovePData("ST_Skills")
  417. ply:RemovePData("ST_XPAmt")
  418. ply:RemovePData("ST_Level")
  419. ply:ST_GetSkills()
  420. end
  421. pl:SendLua( "MsgC( Color( 0, 255, 0 ), \"Cleared All Player Skills\\n\" )")
  422. MsgC(Color(255,0,0,255),pl:Nick() .. "("..pl:SteamID()..") Cleared ALL Player Skills")
  423. end
  424.  
  425. end
  426. concommand.Add("ST_ResetServerSkills",ClearSkills)
  427.  
  428.  
  429. function ST_ResetPlayer(pl,command,args)
  430.  
  431. if pl:IsAdmin() then
  432. if args[1] then
  433. for k,ply in pairs(player.GetAll()) do
  434. if args[1] == tostring(ply:SteamID()) then
  435. ply:RemovePData("ST_SkillPoints")
  436. ply:RemovePData("ST_Skills")
  437. ply:RemovePData("ST_XPAmt")
  438. ply:RemovePData("ST_Level")
  439. ply:ST_GetSkills()
  440. pl:SendLua( "MsgC( Color( 0, 255, 0 ), \"Cleared Players Skills\\n\" )")
  441. MsgC(Color(255,0,0,255), ply:Nick() .. "("..ply:SteamID()..") had his skills cleared by admin ".. pl:Nick() .. "("..pl:SteamID()..")")
  442. end
  443. end
  444. else
  445. pl:SendLua( "MsgC( Color( 255, 0, 0 ), \"SteamID Required!\\n\" )")
  446. end
  447. end
  448.  
  449. end
  450. concommand.Add("ST_ResetPlayerSkills",ST_ResetPlayer)
  451.  
  452. function GiveXP(pl,command,args)
  453.  
  454. if pl:IsAdmin() then
  455. for k,ply in pairs(player.GetAll()) do
  456. if args[1] == tostring(ply:SteamID()) then
  457. ply:ST_GiveXP(tonumber(args[2]))
  458. pl:ChatPrint("Gave " .. args[2] .. " XP to " .. ply:Nick())
  459. end
  460. end
  461. end
  462.  
  463. end
  464. concommand.Add("ST_GiveXP",GiveXP)
  465.  
  466. function TakeXP(pl,command,args)
  467.  
  468. if pl:IsAdmin() then
  469. for k,ply in pairs(player.GetAll()) do
  470. if args[1] == tostring(ply:SteamID()) then
  471. ply:ST_TakeXP(tonumber(args[2]))
  472. pl:ChatPrint("Took " .. args[2] .. " XP to " .. ply:Nick())
  473. end
  474. end
  475. end
  476.  
  477. end
  478. concommand.Add("ST_TakeXP",TakeXP)
  479.  
  480. function ST_GETGAMEMODE(ply,command,args)
  481. ply:ChatPrint(engine.ActiveGamemode())
  482. end
  483. concommand.Add("ST_Getgamemode",ST_GETGAMEMODE)
  484.  
  485. --Check for version of SkillTree
  486. function ST_CheckVersion()
  487. http.Fetch( "https://dl.dropboxusercontent.com/u/4331988/STVersion.html",
  488. function( body, len, headers, code )
  489. if body != Version then
  490. MsgC( Color( 255, 0, 0 ), "SkillTree Outdated! Newest Version is " .. body .."\n" )
  491. else
  492. MsgC( Color( 0, 255, 0 ), "SkillTree Up To Date!\n" )
  493. end
  494. end,
  495. function( error )
  496. print("Unable to fetch SkillTree Version!")
  497. end
  498. )
  499. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement