forked from JumpyDuke/UniversalProcessKit_FS15
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFillLevelBubble.lua
371 lines (340 loc) · 11.3 KB
/
FillLevelBubble.lua
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
-- by mor2000
----------------------------------
-- fill level bubble -------------
----------------------------------
_g.FillLevelBubble = {}
_g.fillLevelBubble_mt = {
__index = function(t,k)
if k=="fillLevel" then
--print(tostring(t)..' fillLevel is '..tostring(t.p_fillLevel))
return t.p_fillLevel or 0
elseif k=="fillType" then
return t.p_fillType or UniversalProcessKit.FILLTYPE_UNKNOWN
elseif k=="capacity" then
return t.capacities[t.p_fillType] or math.huge
end
return FillLevelBubble[k]
end,
__newindex = function(t,k,v)
if k=="fillLevel" then
if v<=0.00000001 then
t.p_fillType = UniversalProcessKit.FILLTYPE_UNKNOWN
t.p_fillLevel = 0
else
--print(tostring(t)..' fillLevel set to '..tostring(v))
t.p_fillLevel = v
end
end
end,
__add = function(lhs,rhs)
if type(rhs)=="number" then
if rhs<0 then
return lhs - (-rhs)
end
local oldFillLevel = lhs.fillLevel
local newFillLevel = mathmax(mathmin(oldFillLevel + rhs, lhs.capacity), 0)
local diff = newFillLevel - oldFillLevel
lhs:onFillLevelChange(diff,newFillLevel,lhs.fillType)
lhs.fillLevel = newFillLevel
return diff
elseif type(rhs)=="table" then
if not rhs.isflb then
rhs = FillLevelBubble:new(rhs)
end
if rhs.fillLevel<0 then
return lhs - {-rhs.fillLevel, rhs.fillType}
end
local newFillType = lhs.fillTypesConversionMatrix[lhs.fillType][rhs.fillType]
print('lhs.fillType='..tostring(lhs.fillType)..', rhs.fillType='..tostring(rhs.fillType)..', newfilltype='..tostring(newFillType))
if newFillType~=nil then
lhs.p_fillType = newFillType
local diff = lhs + rhs.fillLevel
if rhs.isflb then
_ = rhs - diff
end
return diff
end
end
return 0
end,
__sub = function(lhs,rhs)
if type(rhs)=="number" then
if rhs<0 then
return lhs + (-rhs)
end
local oldFillLevel = lhs.fillLevel
local newFillLevel = mathmin(mathmax(oldFillLevel - rhs, 0), lhs.capacity)
local diff = newFillLevel - oldFillLevel
lhs:onFillLevelChange(diff,newFillLevel,lhs.fillType)
lhs.fillLevel = newFillLevel
return diff
elseif type(rhs)=="table" then
if not rhs.isflb then
rhs = FillLevelBubble:new(rhs)
end
if rhs.fillLevel<0 then
return lhs + {-rhs.fillLevel, rhs.fillType}
end
local newFillType = lhs.fillTypesConversionMatrix[lhs.fillType][rhs.fillType]
if newFillType~=nil then
lhs.p_fillType = newFillType
local diff = lhs - rhs.fillLevel
if rhs.isflb then
_ = rhs + diff
end
return diff
end
end
return 0
end
}
function FillLevelBubble:new(...)
local arr=...
if type(arr)~="table" then
arr={...}
end
local self={}
self.isflb = true
self.p_fillLevel = arr['fillLevel'] or arr[1] or 0
self.p_fillType = arr['fillType'] or arr[2] or UniversalProcessKit.FILLTYPE_UNKNOWN
self.capacities = arr['capacities'] or FillLevelBubbleCapacities:new()
self.fillTypesConversionMatrix = arr['fillTypesConversionMatrix'] or FillTypesConversionMatrix:new(self.p_fillType)
self.onFillLevelChangeFuncs = {}
setmetatable(self,fillLevelBubble_mt)
return self
end
function FillLevelBubble:onFillLevelChange(deltaFillLevel, newFillLevel, fillType)
--print(tostring(deltaFillLevel)..', '..tostring(newFillLevel)..', '..tostring(fillType))
if deltaFillLevel~=0 then
for obj,func in pairs(self.onFillLevelChangeFuncs) do
obj[func](obj, deltaFillLevel, newFillLevel, fillType)
end
end
end
function FillLevelBubble:registerOnFillLevelChangeFunc(obj,func)
--print('FillLevelBubble:registerOnFillLevelChangeFunc('..tostring(obj)..', '..tostring(func)..')')
if type(obj)=="table" and type(func)=="string" and type(obj[func])=="function" then
self.onFillLevelChangeFuncs[obj]=func
end
end
function FillLevelBubble:unregisterOnFillLevelChangeFunc(obj)
if type(obj)=="table" then
self.onFillLevelChangeFuncs[obj]=nil
end
end
----------------------------------
-- fill types conversion matrix --
----------------------------------
_g.FillTypesConversionMatrix = {}
_g.fillTypesConversionMatrix_mt = {
__index = function(t,k)
local newarr={}
newarr[k]=k
t[k]=newarr
return newarr
end,
__add = function(lhs,rhs)
if type(lhs)~="table" or type(rhs)~="table" then
return FillTypesConversionMatrix:new()
end
for k,v in pairs(rhs) do
for l,w in pairs(v) do
lhs[k][l] = w
end
end
return lhs
end,
__sub = function(lhs,rhs)
if type(lhs)~="table" or type(rhs)~="table" then
return FillTypesConversionMatrix:new()
end
for k,v in pairs(rhs) do
for l,_ in pairs(v) do
if k~=UniversalProcessKit.FILLTYPE_UNKNOWN and l~=UniversalProcessKit.FILLTYPE_UNKNOWN then
lhs[k][l] = nil
end
end
end
return lhs
end
}
function FillTypesConversionMatrix:new(...)
local arr=...
if type(arr)~="table" then
arr={...}
end
local self={}
setmetatable(self, fillTypesConversionMatrix_mt)
for _,name in pairs(UniversalProcessKit.specialFillTypes) do
local index = UniversalProcessKit.fillTypeNameToInt[name]
self[UniversalProcessKit.FILLTYPE_UNKNOWN][index] = index
end
self[UniversalProcessKit.FILLTYPE_UNKNOWN][UniversalProcessKit.FILLTYPE_UNKNOWN] = UniversalProcessKit.FILLTYPE_UNKNOWN
if #arr>=1 then
for i=1,#arr do
-- print('setting filltype '..tostring(arr[1])..' equal to '..tostring(arr[i]))
self[arr[1]][arr[i]] = arr[1]
self[UniversalProcessKit.FILLTYPE_UNKNOWN][arr[i]] = arr[1]
end
self[arr[1]][UniversalProcessKit.FILLTYPE_UNKNOWN] = arr[1]
end
return self
end
----------------------------------
-- fill level bubble capacities --
----------------------------------
_g.FillLevelBubbleCapacities = {}
_g.fillLevelBubbleCapacities_mt = {
__index = function(t,k)
return t.p_capacities[k] or t.p_defaultCapacity
end
}
function FillLevelBubbleCapacities:new(defaultCapacity, capacities) -- fill type bubble capacity
local self = {}
self.p_defaultCapacity = defaultCapacity or math.huge
self.p_capacities = capacities or {}
setmetatable(self, fillLevelBubbleCapacities_mt)
return self
end
----------------------------------
-- fill level bubbles shell ------
----------------------------------
-- might not work, changed/ fixed version in ClassUPK
_g.fillLevelBubbleShell_mt = {
__index = function(t,k)
if t.storageType==UPK_Storage.SEPARATE then
if k=="capacity" then
return nil
elseif k=="fillLevel" then
return 0
elseif k=="fillType" then
return UniversalProcessKit.FILLTYPE_UNKNOWN
end
elseif t.storageType==UPK_Storage.SINGLE then
if k=="capacity" then
return t.p_capacity
elseif k=="fillLevel" then
return t.p_flbs[1].fillLevel
elseif k=="fillType" then
return t.p_flbs[1].fillType
end
elseif t.storageType==UPK_Storage.FIFO or t.storageType==UPK_Storage.FILO then
if k=="capacity" then
return t.p_capacity
elseif k=="fillLevel" then
return t.p_totalFillLevel
elseif k=="fillType" then
return t.p_flbs[1].fillType
end
end
return UniversalProcessKit[k]
end,
__add = function(lhs,rhs)
local added = 0
if type(rhs)=="table" then
if not rhs.isflb then
rhs = FillLevelBubble:new(rhs)
end
if rhs.fillLevel<0 then
return lhs - {-rhs.fillLevel, rhs.fillType}
end
if UniversalProcessKit.isSpecialFillType(rhs.fillType) then
added = UniversalProcessKitEnvironment.flbs[rhs.fillType] + rhs
elseif lhs.storageType==UPK_Storage.SEPARATE then
added = (lhs.p_flbs[lhs.fillTypesConversionMatrix[UniversalProcessKit.FILLTYPE_UNKNOWN][rhs.fillType]] or lhs.parent or FillLevelBubble:new()) + rhs
elseif lhs.storageType==UPK_Storage.SINGLE then
added = lhs.p_flbs[1] + rhs
elseif lhs.storageType==UPK_Storage.FIFO then
local newFillType = lhs.p_flbs[lhs.p_flbs_fifo_lastkey].fillTypesConversionMatrix[lhs.p_flbs[lhs.p_flbs_fifo_lastkey].fillType][rhs.fillType]
if newFillType~=nil then
local newCapacity = lhs.p_capacity - lhs.p_totalFillLevel
lhs.capacities[newFillType] = newCapacity
added = lhs.p_flbs[lhs.p_flbs_fifo_lastkey] + rhs
end
if added==0 then
local flb = FillLevelBubble:new()
flb.capacities = lhs.capacities
flb.fillTypesConversionMatrix = lhs.fillTypesConversionMatrix
flb:registerOnFillLevelChangeFunc(lhs,"onFillLevelChange")
local newFillType = flb.fillTypesConversionMatrix[flb.fillType][rhs.fillType]
if newFillType~=nil then
local newCapacity = lhs.p_capacity - lhs.p_totalFillLevel
lhs.capacities[newFillType] = newCapacity
added = flb + rhs
end
if added>0 then
lhs.p_flbs_fifo_lastkey = lhs.p_flbs_fifo_lastkey + 1
table.insert(lhs.p_flbs,lhs.p_flbs_fifo_lastkey,flb)
end
end
lhs.p_totalFillLevel = lhs.p_totalFillLevel + added
elseif lhs.storageType==UPK_Storage.FILO then
local newFillType = lhs.p_flbs[1].fillTypesConversionMatrix[lhs.p_flbs[1].fillType][rhs.fillType]
if newFillType~=nil then
local newCapacity = lhs.p_capacity - lhs.p_totalFillLevel
lhs.capacities[newFillType] = newCapacity
added = lhs.p_flbs[1] + rhs
end
if added==0 then
local flb = FillLevelBubble:new()
flb.capacities = lhs.capacities
flb.fillTypesConversionMatrix = lhs.fillTypesConversionMatrix
flb:registerOnFillLevelChangeFunc(lhs,"onFillLevelChange")
local newFillType = flb.fillTypesConversionMatrix[flb.fillType][rhs.fillType]
if newFillType~=nil then
local newCapacity = lhs.p_capacity - lhs.p_totalFillLevel
lhs.capacities[newFillType] = newCapacity
added = flb + rhs
end
if added>0 then
table.insert(lhs.p_flbs,1,flb)
end
end
lhs.p_totalFillLevel = lhs.p_totalFillLevel + added
end
end
return added
end,
__sub = function(lhs,rhs)
local added = 0
if type(rhs)=="table" then
if not rhs.isflb then
rhs = FillLevelBubble:new(rhs)
end
if rhs.fillLevel<0 then
return lhs + {-rhs.fillLevel, rhs.fillType}
end
if UniversalProcessKit.isSpecialFillType(rhs.fillType) then
added = UniversalProcessKitEnvironment.flbs[rhs.fillType] - rhs
elseif lhs.storageType==UPK_Storage.SEPARATE then
added = (lhs.p_flbs[lhs.fillTypesConversionMatrix[UniversalProcessKit.FILLTYPE_UNKNOWN][rhs.fillType]] or lhs.parent or FillLevelBubble:new()) - rhs
elseif lhs.storageType==UPK_Storage.SINGLE then
added = lhs.p_flbs[1] - rhs
elseif lhs.storageType==UPK_Storage.FIFO then
local newFillType = lhs.p_flbs[1].fillTypesConversionMatrix[lhs.p_flbs[1].fillType][rhs.fillType]
if newFillType~=nil then
local newCapacity = lhs.p_capacity - lhs.p_totalFillLevel + lhs.p_flbs[1].fillLevel
lhs.capacities[newFillType] = newCapacity
added = lhs.p_flbs[1] - rhs
end
if added<0 and lhs.p_flbs[1].fillLevel==0 and lhs.p_flbs[2]~=nil then
table.remove(lhs.p_flbs,1)
lhs.p_flbs_fifo_lastkey = lhs.p_flbs_fifo_lastkey - 1
end
lhs.p_totalFillLevel = lhs.p_totalFillLevel + added
elseif lhs.storageType==UPK_Storage.FILO then
local newFillType = lhs.p_flbs[1].fillTypesConversionMatrix[lhs.p_flbs[1].fillType][rhs.fillType]
if newFillType~=nil then
local newCapacity = lhs.p_capacity - lhs.p_totalFillLevel + lhs.p_flbs[1].fillLevel
lhs.capacities[newFillType] = newCapacity
added = lhs.p_flbs[1] - rhs
end
if added<0 and lhs.p_flbs[1].fillLevel==0 and lhs.p_flbs[2]~=nil then
table.remove(lhs.p_flbs,1)
end
lhs.p_totalFillLevel = lhs.p_totalFillLevel + added
end
end
return added
end
}