Advertisement
pogh10

Untitled

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