|
@@ -0,0 +1,112 @@
|
|
1
|
+local Fluid
|
|
2
|
+do
|
|
3
|
+ local _class_0
|
|
4
|
+ local _base_0 = {
|
|
5
|
+ update = function(self, dt)
|
|
6
|
+ local leak = 0
|
|
7
|
+ local _list_0 = self.breaches
|
|
8
|
+ for _index_0 = 1, #_list_0 do
|
|
9
|
+ local breach = _list_0[_index_0]
|
|
10
|
+ local rate = (self.pressure - breach.pressure) * (breach.size / self.volume) * dt
|
|
11
|
+ leak = leak + rate
|
|
12
|
+ breach.pressure = breach.pressure + (rate * (self.volume / breach.volume))
|
|
13
|
+ if breach.pressure < 0 then
|
|
14
|
+ breach.pressure = 0
|
|
15
|
+ end
|
|
16
|
+ local amount = breach.pressure * breach.volume
|
|
17
|
+ local modifier = 1 - self.volume * rate / amount
|
|
18
|
+ for item, percentage in pairs(breach.contents) do
|
|
19
|
+ breach.contents[item] = percentage * modifier
|
|
20
|
+ end
|
|
21
|
+ for item, percentage in pairs(self.contents) do
|
|
22
|
+ percentage = (self.volume * rate * percentage) / amount
|
|
23
|
+ if breach.contents[item] then
|
|
24
|
+ breach.contents[item] = breach.contents[item] + percentage
|
|
25
|
+ else
|
|
26
|
+ breach.contents[item] = percentage
|
|
27
|
+ end
|
|
28
|
+ end
|
|
29
|
+ end
|
|
30
|
+ self.pressure = self.pressure - leak
|
|
31
|
+ for i = 1, #self.breaches do
|
|
32
|
+ if self.breaches[i].pressure > self.pressure then
|
|
33
|
+ local other = table.remove(self.breaches, i)
|
|
34
|
+ other:breach(self, other.size)
|
|
35
|
+ i = i - 1
|
|
36
|
+ end
|
|
37
|
+ end
|
|
38
|
+ end,
|
|
39
|
+ amount = function(self, key)
|
|
40
|
+ if key then
|
|
41
|
+ if self.contents[key] then
|
|
42
|
+ return self.volume * self.pressure * self.contents[key]
|
|
43
|
+ else
|
|
44
|
+ return 0
|
|
45
|
+ end
|
|
46
|
+ else
|
|
47
|
+ return self.volume * self.pressure
|
|
48
|
+ end
|
|
49
|
+ end,
|
|
50
|
+ remove = function(self, key, amount)
|
|
51
|
+ if amount == nil then
|
|
52
|
+ amount = 1
|
|
53
|
+ end
|
|
54
|
+ local total = self:amount(key)
|
|
55
|
+ if total >= amount then
|
|
56
|
+ local percentage = amount / total
|
|
57
|
+ self.contents[key] = self.contents[key] - (self.contents[key] * percentage)
|
|
58
|
+ percentage = amount / self:amount()
|
|
59
|
+ self.pressure = self.pressure - (self.pressure * percentage)
|
|
60
|
+ for item, p in pairs(self.contents) do
|
|
61
|
+ self.contents[item] = self.contents[item] + (p * percentage)
|
|
62
|
+ end
|
|
63
|
+ return true
|
|
64
|
+ else
|
|
65
|
+ return false
|
|
66
|
+ end
|
|
67
|
+ end,
|
|
68
|
+ breach = function(self, other, size)
|
|
69
|
+ if other.pressure > self.pressure then
|
|
70
|
+ return other:breach(self, size)
|
|
71
|
+ else
|
|
72
|
+ other.size = size or 1
|
|
73
|
+ return table.insert(self.breaches, other)
|
|
74
|
+ end
|
|
75
|
+ end,
|
|
76
|
+ print = function(self)
|
|
77
|
+ print("Volume", self.volume, "Pressure", self.pressure, "#", self:amount())
|
|
78
|
+ local percentage, amount = 0, 0
|
|
79
|
+ for k, v in pairs(self.contents) do
|
|
80
|
+ local x = self:amount(k)
|
|
81
|
+ print(k, v, x)
|
|
82
|
+ percentage = percentage + v
|
|
83
|
+ amount = amount + x
|
|
84
|
+ end
|
|
85
|
+ return print("", "Total:", percentage, amount)
|
|
86
|
+ end
|
|
87
|
+ }
|
|
88
|
+ _base_0.__index = _base_0
|
|
89
|
+ _class_0 = setmetatable({
|
|
90
|
+ __init = function(self, opts)
|
|
91
|
+ if opts == nil then
|
|
92
|
+ opts = { }
|
|
93
|
+ end
|
|
94
|
+ self.volume = opts.volume or 1
|
|
95
|
+ self.pressure = opts.pressure or 1
|
|
96
|
+ self.contents = opts.contents or { }
|
|
97
|
+ self.breaches = { }
|
|
98
|
+ end,
|
|
99
|
+ __base = _base_0,
|
|
100
|
+ __name = "Fluid"
|
|
101
|
+ }, {
|
|
102
|
+ __index = _base_0,
|
|
103
|
+ __call = function(cls, ...)
|
|
104
|
+ local _self_0 = setmetatable({}, _base_0)
|
|
105
|
+ cls.__init(_self_0, ...)
|
|
106
|
+ return _self_0
|
|
107
|
+ end
|
|
108
|
+ })
|
|
109
|
+ _base_0.__class = _class_0
|
|
110
|
+ Fluid = _class_0
|
|
111
|
+ return _class_0
|
|
112
|
+end
|