aboutsummaryrefslogtreecommitdiff
path: root/gamemode/server
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2016-07-10 17:04:29 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2016-07-10 17:04:29 -0400
commit1de5f9ac6f038bfed2230cc1272b253794b2f41a (patch)
tree15bc9d515f1f48c036522afb7cc71f60243849a9 /gamemode/server
downloadartery-1de5f9ac6f038bfed2230cc1272b253794b2f41a.tar.gz
artery-1de5f9ac6f038bfed2230cc1272b253794b2f41a.tar.bz2
artery-1de5f9ac6f038bfed2230cc1272b253794b2f41a.zip
Initial commit
Diffstat (limited to 'gamemode/server')
-rw-r--r--gamemode/server/heatmap.lua141
-rw-r--r--gamemode/server/sv_config.lua19
-rw-r--r--gamemode/server/sv_database.lua155
-rw-r--r--gamemode/server/sv_loadplayer.lua15
-rw-r--r--gamemode/server/sv_mapconfig.lua19
-rw-r--r--gamemode/server/sv_pac.lua28
-rw-r--r--gamemode/server/sv_systems.lua21
-rw-r--r--gamemode/server/systems/health.lua12
-rw-r--r--gamemode/server/test/testfile.lua48
9 files changed, 458 insertions, 0 deletions
diff --git a/gamemode/server/heatmap.lua b/gamemode/server/heatmap.lua
new file mode 100644
index 0000000..7beb30f
--- /dev/null
+++ b/gamemode/server/heatmap.lua
@@ -0,0 +1,141 @@
+--[[
+ Some helper methods for useing heatmaps
+]]
+
+print("Hello from heatmap.lua!")
+local prs = pairs
+local iprs = ipairs
+local prnt = print
+local tblins,tbldel = table.insert, table.remove
+local pow,sqrt,max = math.pow, math.sqrt, math.max
+
+heatmap = {}
+
+local function VectorDistance(vec1,vec2)
+ if vec1.Distance then return vec1:Distance(vec2)
+ else
+ local dist = 0
+ for k,v in prs(vec1) do
+ local add = pow(vec1[k]-vec2[k],2)
+ dist = dist + add
+ end
+ dist = sqrt(dist)
+ return dist
+ end
+end
+
+local function VectorLength(vec)
+ if vec.Length then return vec:Length()
+ else
+ local len = 0
+ for k,v in prs do
+ local add = pow(v,2)
+ len = len + add
+ end
+ len = sqrt(len)
+ return len
+ end
+end
+
+
+local function RegisterEffect(self, func, position)
+ local stbl = {position,func}
+ tblins(self.heatpoints,#self.heatpoints+1,stbl)
+end
+
+local function CalculateFor(self, position)
+ local sh = self.heatpoints[1]
+ local total = sh[2](sh[1]-position,self.curtime)
+ for k=2,#self.heatpoints do
+ sh = self.heatpoints[k]
+ total, shouldremove = total + sh[2](sh[1]-position,self.curtime)
+ if(shouldremove) then tbldel(k) end
+ end
+ return total
+end
+
+--- Creates a heat map to keep track of effects.
+-- Effects must be structured as a function that takes a vector (position from origin of effect) and number(time) and returns a value
+-- @return a heatmap object
+function heatmap.CreateHeatMap()
+ local tbl = {}
+ tbl.heatpoints = {}
+ tbl.curtime = 0
+ tbl.RegisterEffect = RegisterEffect
+ tbl.CalculateFor = CalculateFor
+ return tbl
+end
+
+function heatmap.UniformInfiniteForever(field)
+ return function(vector, time)
+ return field, false
+ end
+end
+
+function heatmap.UniformInfiniteLinearDecay(field,decayrate)
+ return function(vector,time)
+ return heatmap.UniformInfiniteForever(field)-(time*decayrate), false
+ end
+end
+
+function heatmap.UniformInfiniteLinearDecayGrounded(field,decayrate)
+ local removetime = field/decayrate
+ return function(vector,time)
+ return max(heatmap.UniformInfiniteLinearDecay(field,decayrate),0), time < removetime
+ end
+end
+
+function heatmap.LinearInfiniteForever(field)
+ return function(vector, time)
+ return field - VectorLength(vector), false
+ end
+end
+
+function heatmap.LinearInfiniteForeverGrounded(field)
+ return function(vector,time)
+ return max(heatmap.LinearInfiniteForever(field),0), false
+ end
+end
+
+function heatmap.LinearInfiniteLinearDecay(field,decayrate)
+ return function(vector, time)
+ return field - VectorLength(vector) - (time*decayrate), false
+ end
+end
+
+function heatmap.LinearInfiniteLinearDecayGrounded(field,decayrate)
+ local removetime = field/decayrate
+ return function(vector, time)
+ return max(field-VectorLength(vector) - (time*decayrate),0), time < removetime
+ end
+end
+
+function heatmap.ParabolicInfiniteForever(field, power)
+ return function(vector, time)
+ return field - pow(VectorLength(vector),power)/pow(100,power), false
+ end
+end
+
+function heatmap.ParabolicInfiniteForeverGrounded(field, power)
+ return function(vector, time)
+ local pre = heatmap.ParabolicInfiniteForever(field, power)
+ --print("pre is:")
+ --print(pre)
+ return max(pre,0), false
+ end
+end
+
+function heatmap.ParabolicInfiniteLinearDecay(field,power,decayrate)
+ return function(vector, time)
+ return heatmap.ParabolicInfiniteForever(field, power) - (time*decayrate), false
+ end
+end
+
+function heatmap.ParabolicInfiniteLinearDecayGrounded(field,power,decayrate)
+ local removetime = field/decayrate
+ return function(vector,time)
+ return max(heatmap.ParabolicInfiniteLinearDecay(field,power,decayrate),0), time < removetime
+ end
+end
+
+return heatmap
diff --git a/gamemode/server/sv_config.lua b/gamemode/server/sv_config.lua
new file mode 100644
index 0000000..e4d43ab
--- /dev/null
+++ b/gamemode/server/sv_config.lua
@@ -0,0 +1,19 @@
+--[[
+ Some things that I might want to configure
+]]
+
+ART.defaults = {}
+ART.defaults.starting_inventory = '{"Backpacks":[[[[false,false,false,false,false],[false,false,false,false,false],[false,false,false,false,false],[false,false,false,false,false],[false,false,false,false,false]],[5,5],"Rucksack"]],"Equiped":{"Gloves":false,"Left":false,"Head":false,"Legs":false,"Right":false,"Body":false,"Boots":false}}'
+
+ART.defaults.starting_position = "185.690247 310.664398 515.031250"
+ART.defaults.starting_world = "0.0.0.0:0"
+ART.config = {}
+ART.config.server_world = game.GetIPAddress()
+
+local mysqlconfig = file.Read("artery/mysql.txt")
+for _,line in pairs(string.Explode("\n",mysqlconfig,false)) do
+ print(line)
+ local key, value = unpack(string.Explode("=",line,false))
+ print("setting",key,"to",value)
+ ART.config[key] = value
+end
diff --git a/gamemode/server/sv_database.lua b/gamemode/server/sv_database.lua
new file mode 100644
index 0000000..59b685f
--- /dev/null
+++ b/gamemode/server/sv_database.lua
@@ -0,0 +1,155 @@
+--One-type setup stuff
+print("Hello from database.lua!!")
+require("mysqloo")
+
+local createplayerquery
+local createplayerprepare = [[
+INSERT INTO playerdata (`SteamID`, `PlayerName`,`Inventory`,`WorldPosition`,`World`) VALUES(?, ?, ?, ?, ?)]]
+local createtablequery = [[
+create table if not exists playerdata(SteamID text, PlayerName text, Inventory json, WorldPosition text, World text)]]
+
+local function connectToDatabase()
+ if ART.database ~= nil then return end
+ ART.database = mysqloo.connect(
+ ART.config.dbhost,
+ ART.config.dbuser,
+ ART.config.dbpass,
+ ART.config.dbname,
+ ART.config.dbport
+ )
+ local db = ART.database
+
+ function db:onConnected()
+ print("Database connected successfully")
+ local q = self:query("SELECT 5+5")
+ function q:onSuccess(data)
+ print("Query successful")
+ PrintTable(data)
+ end
+ function q:onError(err, sql)
+ print("Query errored!")
+ print("Query",sql)
+ error(err)
+ end
+ q:start()
+ end
+
+ function db:onConnectionFailed( err )
+ print( "Connection to database failed!" )
+ print( "Error:",err )
+ end
+ print("Connecting to db")
+ db:connect()
+end
+
+connectToDatabase()
+
+local function checkDatabase()
+ local db = ART.database
+ print("Checking if playerdata table exists")
+ local q = db:query(createtablequery)
+ function q:onSuccess(data)
+ print("Got data:")
+ PrintTable(data)
+ end
+ function q:onError(err,sql)
+ print("Query error:")
+ print("Query",sql)
+ print("Error",err)
+ end
+ q:start()
+ print("Tried to query, something should be printed soon!")
+end
+
+checkDatabase()
+
+local function loadQueries()
+ local db = ART.database
+ print("Loading queries")
+ createplayerquery = db:prepare(createplayerprepare)
+ print("starting inventory:")
+ print(ART.defaults.starting_inventory)
+ createplayerquery:setString(3,ART.defaults.starting_inventory)
+ createplayerquery:setString(4,ART.defaults.starting_position)
+ createplayerquery:setString(5,ART.defaults.starting_world)
+ function createplayerquery:onSuccess(data)
+ print("Created player entry successfully!")
+ end
+ function createplayerquery:onError(err)
+ print("An error occured while createing a player entry: " .. err)
+ end
+ ART.DatabaseConnected = true
+end
+
+loadQueries()
+
+function ART.loadPlayerData(ply)
+ assert(ART.DatabaseConnected,"Player joined while setup was not complete!")
+ print("Attempting to load player data")
+ local steamid = ply:SteamID()
+ local db = ART.database
+ local q = db:query([[
+select Inventory, WorldPosition, World from playerdata where SteamID="]] .. steamid .. "\"")
+ function q:onSuccess(data)
+ if #data == 0 then --Player does not have an entry, make one!
+ print("Player does not have entry!")
+ if ART.config.server_world ~= ART.defaults.starting_world then
+ print("You didn't connect to the starting world, you are on:")
+ print(ART.config.server_world)
+ print("The starting world is")
+ print(ART.defaults.starting_world)
+ local ccmd = "connect " .. ART.defaults.starting_world
+ print("Running concommand:" .. ccmd)
+ --ply:ConCommand(ccmd)
+ else
+ print("New player without entry in database, and he's connected to the right place!")
+ createplayerquery:setString(1,steamid)
+ createplayerquery:setString(2,ply:Nick())
+ createplayerquery:start()
+ ply:ConCommand("retry") -- reconnect to the server to load player data normally
+ end
+ else
+ assert(#data == 1, "Two players with the same steamID? Something's gone horribly wrong!")
+ print("Loaded player data,")
+ --PrintTable(data)
+ local world = data[1].World
+ local inv = data[1].Inventory
+ local worldpos = data[1].WorldPosition
+ print("World:" .. world)
+ PrintTable(data)
+ if world ~= ART.config.server_world then
+ print("Joined the wrong world, redirecting!")
+ print("You are on :" .. ART.config.server_world .. " which is not " .. world)
+ local ccmd = "connect " .. world
+ --ply:ConCommand(ccmd)
+ return
+ end
+ --Otherwise, we're in the right world, load our data
+ ply:LoadInventory(inv)
+ local postbl = string.Explode(" ", worldpos)
+ local posvec = Vector(postbl[1],postbl[2],postbl[3])
+ print("Setting player pos to:")
+ print(posvec)
+ ply:SetPos(posvec)
+ end
+
+ end
+ function q:onError(err,sql)
+ print("Query error:")
+ print("Query",sql)
+ print("Error",err)
+ end
+ q:start()
+end
+
+--A randomly assigned model based on a player's UniqueID, so it should always stay the same.
+
+--[[
+hook.Add("PlayerInitialSpawn","ArteryPlayerInit",function(ply)
+ local modelnum = ply:UniqueID() % (#models)
+ print("Setting player model")
+ ply:SetModel(models[modelnum])
+ print("Loading data")
+ ART.loadPlayerData(ply)
+end)
+]]
diff --git a/gamemode/server/sv_loadplayer.lua b/gamemode/server/sv_loadplayer.lua
new file mode 100644
index 0000000..397c6cb
--- /dev/null
+++ b/gamemode/server/sv_loadplayer.lua
@@ -0,0 +1,15 @@
+
+local models = {}
+for k=1,9 do
+ models[#models+1] = "models/player/Group01/male_0" .. k .. ".mdl"
+ models[#models+1] = "models/player/Group02/Male_0" .. k .. ".mdl"
+end
+
+hook.Add("PlayerInitialSpawn","ArteryPlayerLoad",function(pl)
+ local modelnum = pl:UniqueID() % (#models)
+ ART.loadPlayerData(pl)
+ pl:SetModel(models[modelnum])
+ --pl:Give("weapon_pistol")
+ pl:Give("hands")
+ pl:SelectWeapon("hands")
+end)
diff --git a/gamemode/server/sv_mapconfig.lua b/gamemode/server/sv_mapconfig.lua
new file mode 100644
index 0000000..67a20ae
--- /dev/null
+++ b/gamemode/server/sv_mapconfig.lua
@@ -0,0 +1,19 @@
+--Loads map config form a file
+
+local chests = file.Read("artery/maps/" .. game.GetMap() .. "/chests.txt")
+local npcs = file.Read("artery/maps/" .. game.GetMap() .. "/npcs.txt")
+
+if chests == nil then return end
+if npcs == nil then return end
+
+for _,line in pairs(string.Explode("\n",chests,false)) do
+ local chest = util.JSONToTable(line)
+ local chestent = ents.Create("art_chest")
+ for k,v in pairs(chest.data) do
+ chestent[k] = v
+ end
+ for k,v in pairs(chest.procedures) do
+ chestent[k](unpack(v))
+ end
+ chestent:Spawn()
+end
diff --git a/gamemode/server/sv_pac.lua b/gamemode/server/sv_pac.lua
new file mode 100644
index 0000000..27b2ed9
--- /dev/null
+++ b/gamemode/server/sv_pac.lua
@@ -0,0 +1,28 @@
+hook.Add("PrePACConfigApply", "donators only", function(ply, outfit_data)
+ if not ply:IsSuperAdmin() then
+ return false, "give us money!"
+ end
+end)
+
+hook.Add( "PrePACEditorOpen", "RestrictToSuperadmin", function( ply )
+ if not ply:IsSuperAdmin( ) then
+ return false
+ end
+end )
+
+util.AddNetworkString("requestpac")
+util.AddNetworkString("responsepac")
+
+--be able to stream pac data to clients on-demand
+net.Receive("requestpac",function(ln,ply)
+ local name = net.ReadString()
+ local pdata = file.Read(string.format("artery/pac/%s.txt",name)) --make sure to have the .txt, so dot-dot-slash attacks can't read the mysql file.
+ net.Start("responsepac")
+ net.WriteString(pdata)
+ net.Send(ply)
+end)
+
+hook.Add("PlayerInitialSpawn", "AllowPacThings",function(ply)
+ --pac.SetupENT(ply, false)
+ PrintTable(pac)
+end)
diff --git a/gamemode/server/sv_systems.lua b/gamemode/server/sv_systems.lua
new file mode 100644
index 0000000..ad1b901
--- /dev/null
+++ b/gamemode/server/sv_systems.lua
@@ -0,0 +1,21 @@
+--[[
+ Calculates various needs for the player, displayed in /gamemode/client/cl_systems.lua
+ Each need has a min and a max (ex health, stamina ect.)
+
+ Provides:
+ ART.RegisterSystem(string_name, table_system)
+ Registers the system with all players. Two systems may not share the same name.
+]]
+
+local pmeta = FindMetaTable("Player")
+
+pmeta.Systems = {}
+
+function ART.RegisterSystem(tbl)
+ assert(tbl.Name ~= nil,"Attempted to register a system with a nil name")
+ pmeta.Systems[name] = tbl
+end
+
+concommand.Add("DisplaySystems",function()
+ PrintTable(pmeta.Systems)
+end)
diff --git a/gamemode/server/systems/health.lua b/gamemode/server/systems/health.lua
new file mode 100644
index 0000000..7ead999
--- /dev/null
+++ b/gamemode/server/systems/health.lua
@@ -0,0 +1,12 @@
+
+local SYSTEM = {}
+
+SYSTEM.onthink = function(ply)
+
+end
+
+SYSTEM.ontakedamage = function(ply)
+
+end
+
+return SYSTEM
diff --git a/gamemode/server/test/testfile.lua b/gamemode/server/test/testfile.lua
new file mode 100644
index 0000000..95e40fa
--- /dev/null
+++ b/gamemode/server/test/testfile.lua
@@ -0,0 +1,48 @@
+--print("This is a test file to see if it gets included! Test!")
+--Msg("Testing heatmap.lua\n")
+local hm = heatmap.CreateHeatMap()
+--PrintTable(hm)
+--PrintTable(heatmap)
+--local effect = heatmap.UniformInfiniteForever(5)
+--hm:RegisterEffect(effect,Vector(0,0,0))
+--PrintTable(hm)
+--local neweffect = heatmap.LinearInfiniteForeverGrounded(2000,Vector(0,0,0))
+--hm:RegisterEffect(neweffect,Vector(0,0,0))
+--PrintTable(hm)
+local decayeffect =
+heatmap.LinearInfiniteLinearDecayGrounded(2000,5)
+hm:RegisterEffect(decayeffect,Vector(4000,4000,0))
+local bubbleeffect = heatmap.ParabolicInfiniteForeverGrounded(1000,2.5)
+hm:RegisterEffect(bubbleeffect,Vector(0,0,0))
+
+function sendHeatMap(ply)
+ local points = {}
+ net.Start("showheatmap")
+ for i=0,10000,100 do
+ points[i] = {}
+ for j=0,10000,100 do
+ local offset = --[[ply:GetPos() + ]]Vector(i,j,0) + Vector(-5000,-5000,0)
+ points[i][j] = hm:CalculateFor(offset, 0)
+ --net.WriteVector(offset)
+ net.WriteFloat(points[i][j])
+ --print("at ("..i..","..j..") is ")
+ --print(points[i][j])
+ end
+ end
+ net.Send(ply)
+end
+
+function callloop(ply)
+ sendHeatMap(ply)
+ hm.curtime = hm.curtime + 0.5
+ timer.Simple(0.5,function() callloop(ply) end)
+end
+
+util.AddNetworkString("showheatmap")
+concommand.Add("ShowHeatMap",function(ply,cmd,args)
+ callloop(ply)
+end)
+
+concommand.Add("HeatMapAt",function(ply,cmd,args)
+ print(hm:CalculateFor(ply:GetPos(),0))
+end)