From e2db00b0ea144ecdea0208d7d7ba9d319770fa58 Mon Sep 17 00:00:00 2001 From: Dadido3 Date: Wed, 5 Oct 2016 14:30:35 +0200 Subject: [PATCH] Init --- lua/autorun/cl_d3stats.lua | 5 ++ lua/d3stats/cl_hud.lua | 30 ++++++++++ lua/d3stats/cl_init.lua | 16 +++++ lua/d3stats/cl_network.lua | 29 +++++++++ lua/d3stats/init.lua | 108 ++++++++++++++++++++++++++++++++++ lua/d3stats/sh_concommand.lua | 7 +++ lua/d3stats/sh_level.lua | 47 +++++++++++++++ lua/d3stats/sh_settings.lua | 56 ++++++++++++++++++ lua/d3stats/sv_network.lua | 35 +++++++++++ lua/d3stats/vgui/overlay.lua | 46 +++++++++++++++ 10 files changed, 379 insertions(+) create mode 100644 lua/autorun/cl_d3stats.lua create mode 100644 lua/d3stats/cl_hud.lua create mode 100644 lua/d3stats/cl_init.lua create mode 100644 lua/d3stats/cl_network.lua create mode 100644 lua/d3stats/init.lua create mode 100644 lua/d3stats/sh_concommand.lua create mode 100644 lua/d3stats/sh_level.lua create mode 100644 lua/d3stats/sh_settings.lua create mode 100644 lua/d3stats/sv_network.lua create mode 100644 lua/d3stats/vgui/overlay.lua diff --git a/lua/autorun/cl_d3stats.lua b/lua/autorun/cl_d3stats.lua new file mode 100644 index 0000000..be3105b --- /dev/null +++ b/lua/autorun/cl_d3stats.lua @@ -0,0 +1,5 @@ +if (SERVER) then + include("d3stats/init.lua") +else + include("d3stats/cl_init.lua") +end \ No newline at end of file diff --git a/lua/d3stats/cl_hud.lua b/lua/d3stats/cl_hud.lua new file mode 100644 index 0000000..6acd240 --- /dev/null +++ b/lua/d3stats/cl_hud.lua @@ -0,0 +1,30 @@ +-- Delete HUD and redo if already existent. This will reset the values until the next update from the server +if d3stats.D3StatsOverlay then + d3stats.D3StatsOverlay:Remove() + d3stats.Overlay_Init() +end + +-- HUD on the left top of the screen +function d3stats.Overlay_Init() + local screenscale = BetterScreenScale() + + d3stats.D3StatsOverlay = vgui.Create( "D3StatsOverlay" ) + d3stats.D3StatsOverlay:SetPos( 0, screenscale * 80 ) + d3stats.D3StatsOverlay:SetSize( 350, 100 ) + +end + +-- Call this function from cl_targetid.lua in the gamemode +local colTemp = Color(255, 255, 255) +function d3stats.DrawTargetID( ent, fade, x, y ) + + colTemp.a = fade * 255 + --util.ColorCopy(COLOR_FRIENDLY, colTemp) + + if ent.D3Stats_Level and d3stats.Levels[ent.D3Stats_Level] then + draw.SimpleTextBlur("Level " .. tostring(ent.D3Stats_Level) .. " \"" .. d3stats.Levels[ent.D3Stats_Level].Name .. "\"", "ZSHUDFontTiny", x, y, colTemp, TEXT_ALIGN_CENTER) + y = y + draw.GetFontHeight("ZSHUDFontTiny") + 4 + end + + return x, y +end \ No newline at end of file diff --git a/lua/d3stats/cl_init.lua b/lua/d3stats/cl_init.lua new file mode 100644 index 0000000..324f172 --- /dev/null +++ b/lua/d3stats/cl_init.lua @@ -0,0 +1,16 @@ +d3stats = d3stats or {} + +-- Includes +include( "sh_settings.lua" ) +include( "sh_level.lua" ) +include( "sh_concommand.lua" ) + +include( "cl_network.lua" ) +include( "cl_hud.lua" ) + +include( "vgui/overlay.lua" ) + +-- Initialisation +hook.Add( "Initialize", "D3Stats_Init", function () + d3stats.Overlay_Init() +end ) diff --git a/lua/d3stats/cl_network.lua b/lua/d3stats/cl_network.lua new file mode 100644 index 0000000..7b014f7 --- /dev/null +++ b/lua/d3stats/cl_network.lua @@ -0,0 +1,29 @@ +net.Receive( "D3Stats_UpdateXP", function() + local XP = net.ReadUInt( 32 ) + + --print( "XP update: " .. tostring(XP)) + + if d3stats and d3stats.D3StatsOverlay then + d3stats.D3StatsOverlay:StatsUpdate( XP, d3stats.CalculateLevel( XP ) ) + end +end ) + +net.Receive( "D3Stats_UpdateLevels", function() + local players = net.ReadUInt( 16 ) + + for i = 1, players do + local ply = net.ReadEntity() + local Level = net.ReadUInt( 16 ) + ply.D3Stats_Level = Level + --print( "Level update: " .. tostring(ply) .." --> " .. tostring(Level)) + end +end ) + +net.Receive( "D3Stats_BroadcastLevel", function() + local ply = net.ReadEntity() + local Level = net.ReadUInt( 16 ) + + ply.D3Stats_Level = Level + + --print( "Level broadcast: " .. tostring(ply) .." --> " .. tostring(Level)) +end ) \ No newline at end of file diff --git a/lua/d3stats/init.lua b/lua/d3stats/init.lua new file mode 100644 index 0000000..ef6ef2f --- /dev/null +++ b/lua/d3stats/init.lua @@ -0,0 +1,108 @@ +--[[ + +XP and statistics addon for the "Zombie Survival" gamemode +by David Vogel (Dadido3) + +]] + +d3stats = d3stats or {} + +AddCSLuaFile( "sh_settings.lua" ) +AddCSLuaFile( "sh_level.lua" ) +AddCSLuaFile( "sh_concommand.lua" ) + +AddCSLuaFile( "cl_init.lua" ) +AddCSLuaFile( "cl_network.lua" ) +AddCSLuaFile( "cl_hud.lua" ) +AddCSLuaFile( "vgui/overlay.lua" ) + +include( "sh_settings.lua" ) +include( "sh_level.lua" ) +include( "sh_concommand.lua" ) + +include( "sv_network.lua" ) + +-- XP rewards for players getting points +hook.Add( "PlayerPointsAdded", "D3Stats_PlayerPointsAdded", function ( ply, points ) + if points <= d3stats.PlayerPointsAdded_Limit then + ply:D3Stats_AddXP( points ) + end +end ) + +-- XP rewards for zombies killing humans +hook.Add( "PostZombieKilledHuman", "D3Stats_PostZombieKilledHuman", function ( ply, attacker, dmginfo, headshot, wassuicide ) + local reward = d3stats.ZombieKilledHuman_Static + d3stats.ZombieKilledHuman_Fraction * ply:GetPoints() + + reward = math.Clamp( reward, d3stats.ZombieKilledHuman_Min, d3stats.ZombieKilledHuman_Max ) + + attacker:D3Stats_AddXP( reward ) +end ) + +hook.Add( "PlayerReady", "D3Stats_PlayerReady", function ( ply ) -- TODO: Find a better method than using the PlayerReady hook + -- Send all the levels of the other players to ply + ply:D3Stats_Net_UpdateLevels() +end ) + +hook.Add( "PlayerSpawn", "D3Stats_PlayerSpawn", function ( ply ) + -- Send its own XP + ply:D3Stats_Net_UpdateXP() + + -- Broadcast the own level to others + ply.D3Stats_Level = ply:D3Stats_GetLevel() + ply:D3Stats_Net_BroadcastLevel( ply.D3Stats_Level ) +end ) + +local meta = FindMetaTable( "Player" ) +if not meta then return end + +function meta:D3Stats_GetXP() + local XP = tonumber( self:GetPData( "D3Stats_XP", "0" ) ) + + if not XP then + XP = 0 + end + + return XP +end + +function meta:D3Stats_SetXP( XP ) + if not XP then + XP = 0 + end + + self:SetPData( "D3Stats_XP", tostring( XP ) ) + + self:D3Stats_Net_UpdateXP() + + -- Broadcast on level change + local Level = self:D3Stats_GetLevel() + if self.D3Stats_Level and self.D3Stats_Level ~= Level then + self.D3Stats_Level = Level + self:D3Stats_Net_BroadcastLevel( Level ) + self:CenterNotify( Color( 0, 255, 255 ), "You ascended to level " .. tostring( Level ) .. " \"" .. d3stats.Levels[Level].Name .. "\"") + end + self.D3Stats_Level = Level +end + +function meta:D3Stats_AddXP( XP ) + if not XP then + XP = 0 + end + + self:D3Stats_SetXP( self:D3Stats_GetXP() + XP ) +end + +function meta:D3Stats_GetLevel() + return d3stats.CalculateLevel( self:D3Stats_GetXP() ) +end + +-- Check if the players level has the permission +function meta:D3Stats_HasPermission( Permission ) + + -- If no one else has this permission, allow it + if d3stats.Permissions[Permission] and d3stats.Permissions[Permission].AllowIfLessThan and d3stats.Permissions[Permission].AllowIfLessThan > d3stats.CountPermissionPlayers( Permission ) then + return true + end + + return d3stats.LevelCheckPermission( self:D3Stats_GetLevel(), Permission ) +end \ No newline at end of file diff --git a/lua/d3stats/sh_concommand.lua b/lua/d3stats/sh_concommand.lua new file mode 100644 index 0000000..321a728 --- /dev/null +++ b/lua/d3stats/sh_concommand.lua @@ -0,0 +1,7 @@ +if SERVER then + + concommand.Add( "d3stats_clearall", function( ply, cmd, args ) + print( "Test asd asdasdasd!" ) + end ) + +end \ No newline at end of file diff --git a/lua/d3stats/sh_level.lua b/lua/d3stats/sh_level.lua new file mode 100644 index 0000000..db34b4c --- /dev/null +++ b/lua/d3stats/sh_level.lua @@ -0,0 +1,47 @@ +-- Calculate the level from the given XP +function d3stats.CalculateLevel( XP ) + local Level = 1 + + for key, value in pairs(d3stats.Levels) do + if value.XP_needed <= XP then + Level = key + else + break + end + end + + return Level +end + +-- Check if the level has the given permission +function d3stats.LevelCheckPermission( Level, Permission ) + local Granted = false + + for key, value in pairs(d3stats.Levels) do + if key <= Level then + if value.Permissions and value.Permissions[Permission] then + Granted = value.Permissions[Permission] + end + else + break + end + end + + return Granted +end + +-- Count the amount of online players who have the permission +if SERVER then + function d3stats.CountPermissionPlayers( Permission ) + local Counter = 0 + + local players = player.GetAll() + for key, ply in pairs( players ) do + if d3stats.LevelCheckPermission( ply:D3Stats_GetLevel(), Permission ) == true then + Counter = Counter + 1 + end + end + + return Counter + end +end \ No newline at end of file diff --git a/lua/d3stats/sh_settings.lua b/lua/d3stats/sh_settings.lua new file mode 100644 index 0000000..63ec3b5 --- /dev/null +++ b/lua/d3stats/sh_settings.lua @@ -0,0 +1,56 @@ +--[[ + +Settings and level definitions are stored in here + +]] + +-- Permissions +-- AllowIfLessThan: If the amount of players who have the permission is lower than this number, allow it anyways +d3stats.Permissions = { + ["Buy_Hammer"] = { AllowIfLessThan = 2 }, + ["Use_Hammer"] = { AllowIfLessThan = 2 }, +} + +-- Levels, please sort by XP +d3stats.Levels = { + { XP_needed = 0, Name = "Kleiner" }, + { XP_needed = 500, Name = "Lesser Kleiner" }, + { XP_needed = 1000, Name = "Dayfly" }, + { XP_needed = 2000, Name = "Apprentice", Permissions = { ["Buy_Hammer"] = true, ["Use_Hammer"] = true } }, + { XP_needed = 3000, Name = "Adventurer" }, + { XP_needed = 4000, Name = "Scout" }, + { XP_needed = 5000, Name = "Guardian" }, + { XP_needed = 6000, Name = "Fighter" }, + { XP_needed = 7000, Name = "Brawler" }, + { XP_needed = 8000, Name = "Scrapper" }, + { XP_needed = 9000, Name = "Skirmisher" }, + { XP_needed = 10000, Name = "Battler" }, + { XP_needed = 15000, Name = "Marauder" }, + { XP_needed = 20000, Name = "Slayer" }, + { XP_needed = 25000, Name = "Mercenary" }, + { XP_needed = 30000, Name = "Swordsman" }, + { XP_needed = 35000, Name = "Freelancer" }, + { XP_needed = 40000, Name = "Swashbuckler" }, + { XP_needed = 45000, Name = "Vanquisher" }, + { XP_needed = 50000, Name = "Exemplar" }, + { XP_needed = 60000, Name = "Conqueror" }, + { XP_needed = 70000, Name = "Specialist" }, + { XP_needed = 80000, Name = "Lieutenant" }, + { XP_needed = 90000, Name = "Captain" }, + { XP_needed = 100000, Name = "Major" }, + { XP_needed = 133333, Name = "Colonel" }, + { XP_needed = 166666, Name = "General" }, + { XP_needed = 200000, Name = "Champion" }, + { XP_needed = 250000, Name = "Hero" }, + { XP_needed = 300000, Name = "Legend" }, + { XP_needed = 500000, Name = "Demigod" }, + { XP_needed = 1000000, Name = "God" }, +} + +d3stats.PlayerPointsAdded_Limit = 200 -- Ignore all "PlayerPointsAdded" callbacks above this XP value + +-- Zombie reward is calculated as follows: Reward = math.clamp( Static + Fraction * Human_Points, Min, Max ) +d3stats.ZombieKilledHuman_Fraction = 1.0 -- Amount of XP a zombie gets of the killed humans points +d3stats.ZombieKilledHuman_Static = 100 -- Amount of XP a zombie gets for killing a human +d3stats.ZombieKilledHuman_Max = 1000 -- Upper XP reward clamp +d3stats.ZombieKilledHuman_Min = 0 -- Lower XP reward clamp \ No newline at end of file diff --git a/lua/d3stats/sv_network.lua b/lua/d3stats/sv_network.lua new file mode 100644 index 0000000..c967045 --- /dev/null +++ b/lua/d3stats/sv_network.lua @@ -0,0 +1,35 @@ +util.AddNetworkString( "D3Stats_UpdateXP" ) +util.AddNetworkString( "D3Stats_UpdateLevels" ) +util.AddNetworkString( "D3Stats_BroadcastLevel" ) + +local meta = FindMetaTable( "Player" ) +if not meta then return end + +-- Send the XP of this (self) player to itself +function meta:D3Stats_Net_UpdateXP() + net.Start( "D3Stats_UpdateXP" ) + net.WriteUInt( self:D3Stats_GetXP(), 32 ) + net.Send( self ) +end + +-- Send all player levels to this (self) player +function meta:D3Stats_Net_UpdateLevels() + net.Start( "D3Stats_UpdateLevels" ) + + local players = player.GetAll() + + net.WriteUInt( table.getn( players ), 16 ) + for k, ply in pairs( players ) do + net.WriteEntity( ply ) + net.WriteUInt( ply:D3Stats_GetLevel(), 16 ) + end + net.Send( self ) +end + +-- Send the current level of this (self) player to everyone +function meta:D3Stats_Net_BroadcastLevel( Level ) + net.Start( "D3Stats_BroadcastLevel" ) + net.WriteEntity( self ) + net.WriteUInt( Level, 16 ) + net.Broadcast() +end \ No newline at end of file diff --git a/lua/d3stats/vgui/overlay.lua b/lua/d3stats/vgui/overlay.lua new file mode 100644 index 0000000..d1543ec --- /dev/null +++ b/lua/d3stats/vgui/overlay.lua @@ -0,0 +1,46 @@ +local PANEL = {} + +function PANEL:Init() + + self.Progress = vgui.Create( "DProgress", self ) + self.Progress:SetPos( 10, 10 ) + self.Progress:SetSize( 200, 10 ) + + self.Label = vgui.Create( "DLabel", self ) + self.Label:SetFont("ZSHUDFontTiny") + self.Label:SetPos( 10, 25 ) -- Set the position of the label + self:StatsUpdate( 0, 1 ) + --self.Label:SetDark( 1 ) -- Set the colour of the text inside the label to a darker one + + --timer.Create( "D3StatsOverlay_Timer", 0.1, 0, function() self.Progress:SetFraction( math.random() ) end ) + + +end + +function PANEL:Paint( aWide, aTall ) + -- Nothing for now +end + +function PANEL:StatsUpdate( XP, Level ) + local Text + local Fraction + + if d3stats.Levels[Level+1] then + Text = "XP: " .. tostring( XP ) .. " / " .. d3stats.Levels[Level+1].XP_needed .. "\nLevel: " .. tostring( Level ) .. " \"" .. d3stats.Levels[Level].Name .. "\"" + Fraction = ( XP - d3stats.Levels[Level].XP_needed ) / ( d3stats.Levels[Level+1].XP_needed - d3stats.Levels[Level].XP_needed ) + else + Text = "XP: " .. tostring( XP ) .. "\nLevel: " .. tostring( Level ) .. " \"" .. d3stats.Levels[Level].Name .. "\"" + Fraction = 1 + end + + self.Label:SetText( Text ) -- Set the text of the label + self.Label:SizeToContents() -- Size the label to fit the text in it + + self.Progress:SetFraction( Fraction ) + +end + +function PANEL:SetText( aText ) self.Text = tostring( aText ) end +function PANEL:GetText() return self.Text or "" end + +vgui.Register( "D3StatsOverlay", PANEL, "DPanel" ) \ No newline at end of file