aboutsummaryrefslogtreecommitdiff
path: root/gamemode/server/heatmap.lua
blob: 1e955903cc4a8b1dcb6fb6b8197224c542d99414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
--[[
    Some helper methods for useing heatmaps
]]
do return end
print("Hello from heatmap.lua!")
local prs = pairs
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