diff options
Diffstat (limited to 'gamemode/server/heatmap.lua')
| -rw-r--r-- | gamemode/server/heatmap.lua | 141 |
1 files changed, 141 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 |
