forked from JumpyDuke/UniversalProcessKit_FS15
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc.lua
1087 lines (1015 loc) · 26.4 KB
/
c.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- by mor2000
upkModDirectory = g_currentModDirectory
_m=_G;_G=nil;_g=_G;_G=_m;
_m.mathrandom = math.random
_m.mathsqrt = math.sqrt
_m.mathlog = math.log
_m.mathmin = math.min
_m.mathmax = math.max
_m.mathfloor = math.floor
_m.mathceil = math.ceil
_m.mathabs = math.abs
_m.mathpi = math.pi
_m.mathsin = math.sin
_m.mathcos = math.cos
_g.UniversalProcessKit = {};
local UniversalProcessKit_mt = {
__index=function(t,k)
local wantFillType = string.match(k, "FILLTYPE_%S+")
if wantFillType~=nil and Fillable[k]~=nil then
rawset(UniversalProcessKit,k,Fillable[k])
return Fillable[k]
end
return nil
end
}
setmetatable(UniversalProcessKit,UniversalProcessKit_mt)
_g.UniversalProcessKitStorageBit={};
_g.UniversalProcessKitStorageController={};
----------------------------------
-- basic functions ---------------
----------------------------------
function emptyFunc() end
function UniversalProcessKit.InitEventClass(classObject,className)
if _g[className]~=classObject then
_g[className]=classObject
--print("Error: Can't assign eventId to "..tostring(className).." (object name conflict)",true)
--return
end
_g.InitEventClass(classObject,className)
if classObject.eventId==nil then
EventIds.assignEventObjectId(classObject,className,EventIds.eventIdNext)
end
end;
function length(t)
if t==nil or type(t)~="table" then
return 0
end
local len=0
for _ in pairs(t) do
len=len+1
end
return len
end;
function max(...)
tmpmax=-math.huge
arr=...
if type(arr)~="table" then
arr={...}
end
if length(arr)>0 then
for _,v in pairs(arr) do
if v>tmpmax then
tmpmax=v
end
end
return tmpmax
end
return nil
end;
function min(...)
tmpmin=math.huge
arr=...
if type(arr)~="table" then
arr={...}
end
if #arr>0 then
for _,v in pairs(arr) do
if v<tmpmin then
tmpmin=v
end
end
return tmpmin
end
return nil
end;
local times10 = {}
local times10_mt = {
__index = function(t,k)
local result=10^k
rawset(t,k,result)
return result
end
}
setmetatable(times10, times10_mt)
function round(nr, digits)
digits = digits or 0
local result=mathfloor(nr*times10[digits]+0.5)/times10[digits]
return result
end
function floor(nr, digits)
digits = digits or 0
local result=mathfloor(nr*times10[digits])/times10[digits]
return result
end
function gmatch(str, pattern)
local arr={}
if type(str)=="string" then
for v in string.gmatch(str,pattern) do
table.insert(arr,v)
end
end
return arr
end;
function tobool(val)
return not (val == nil or val == false or val == 0 or val == "0" or val == "false" )
end;
function strlen(...)
return utf8Strlen(...)
end
function getMinMaxKeys(t)
if t==nil or type(t)~="table" then
return nil, nil
end
if #t>0 then
local mink = math.huge
local maxk = -math.huge
for k,_ in pairs(t) do
mink=math.min(mink,k)
maxk=math.max(maxk,k)
end
return mink, maxk
end
return nil, nil
end;
function getVectorFromUserAttribute(nodeId, attribute, default)
if nodeId==nil then
print('Warning from getVectorFromUserAttribute(): nodeId is nil')
return default
end
local str=Utils.getNoNil(getUserAttribute(nodeId, attribute), default)
if type(str)=="string" then
return __c({Utils.getVectorFromString(str)})
end
return str
end;
function getNumberFromUserAttribute(nodeId, attribute, default, lowerBound, upperBound)
if nodeId==nil then
print('Warning from getNumberFromUserAttribute: nodeId is nil')
return default
end
local nr=tonumber(Utils.getNoNil(getUserAttribute(nodeId, attribute), default))
if lowerBound~=nil and nr~=nil then
nr=mathmax(nr,lowerBound)
end
if upperBound~=nil and nr~=nil then
nr=mathmin(nr,upperBound)
end
return nr
end;
function getBoolFromUserAttribute(nodeId, attribute, default)
if nodeId==nil then
print('Warning from getBoolFromUserAttribute: nodeId is nil')
return default
end
local bool=tobool(Utils.getNoNil(getUserAttribute(nodeId, attribute), default))
return bool
end;
--[[
function _m.getUserAttribute(nodeId, attribute)
if nodeId==0 or nodeId==nil or attribute=="" or attribute==nil then
print('Warning: wanted to get UserAttribute "'..tostring(attribute)..'" from node '..tostring(nodeId)..' but failed')
return nil
end
return _g.getUserAttribute(nodeId, attribute)
end
--]]
function getStringFromUserAttribute(nodeId, attribute, default)
if nodeId==nil then
print('Warning from getStringFromUserAttribute: nodeId is nil')
return default
end
local str=Utils.getNoNil(getUserAttribute(nodeId, attribute), default)
if str~=nil then
str=tostring(str)
end
return str
end;
function getArrayFromUserAttribute(nodeId, attribute, default)
if nodeId==nil then
print('Warning from getArrayFromUserAttribute: nodeId is nil')
return default
end
local str=getStringFromUserAttribute(nodeId, attribute)
if str==nil then
return default or {}
end
local arr=gmatch(str, "%S+")
return arr
end;
function removeValueFromTable(tbl, value, all)
local index={}
if type(tbl)=="table" and value~=nil then
for k,v in pairs(tbl) do
if v==value then
table.insert(index,k)
if all~=true then
break
end
end
end
table.sort(index, function(a, b) return a<b end)
for i=#index,1,-1 do
tbl[index[i]]=nil
end
return #index
end
return 0
end;
function isInTable(t,e)
if type(t)=="table" then
for _,v in pairs(t) do
if v==e then
return true
end
end
end
return false
end;
function returnNilIfEmptyString(str)
if str=="" then
return nil
end
return str
end;
function loopThruChildren(id,loopFunction,obj)
--print('loopThruChildren nodeId '..tostring(id))
if id==nil or id==0 or type(obj)~="table" or type(loopFunction)~="string" then
return false
end
local numChildren = getNumOfChildren(id)
--print('number of children of nodeId is '..tostring(numChildren))
if type(numChildren)=="number" and numChildren>0 then
for i=1,numChildren do
local childId = getChildAt(id, i-1)
if childId~=nil or childId~=0 then
if not obj[loopFunction](obj,childId) then
--print('abort loopThruChildren')
return true
end
end
end
end
return true
end;
function getChildrenRigidBodyTypeStatic(id)
local class={}
class.staticShapes={}
if getRigidBodyType(id)=="Static" then
table.insert(class.staticShapes,id)
end
function class:loopFunc(childId)
if getRigidBodyType(childId)=="Static" then
table.insert(self.staticShapes,childId)
end
loopThruChildren(childId,"loopFunc",self)
return true
end
loopThruChildren(id,"loopFunc",class)
return class.staticShapes
end;
function UniversalProcessKit.setTranslation(id,x,y,z)
--print('set translation of nodeId '..tostring(id)..' to '..tostring(x)..', '..tostring(x)..', '..tostring(z))
local staticShapes=getChildrenRigidBodyTypeStatic(id)
for _,v in pairs(staticShapes) do
setRigidBodyType(v,"Kinematic")
end
_g.setTranslation(id,x,y,z)
for _,v in pairs(staticShapes) do
setRigidBodyType(v,"Static")
end
end
function UniversalProcessKit.setWorldTranslation(id,x,y,z)
local staticShapes=getChildrenRigidBodyTypeStatic(id)
for _,v in pairs(staticShapes) do
setRigidBodyType(v,"Kinematic")
end
Utils.setWorldTranslation(id,x,y,z)
for _,v in pairs(staticShapes) do
setRigidBodyType(v,"Static")
end
end
function UniversalProcessKit.setRotation(id,rx,ry,rz)
local staticShapes=getChildrenRigidBodyTypeStatic(id)
for _,v in pairs(staticShapes) do
setRigidBodyType(v,"Kinematic")
end
_g.setRotation(id,rx,ry,rz)
for _,v in pairs(staticShapes) do
setRigidBodyType(v,"Static")
end
end
function UniversalProcessKit.adjustToTerrainHeight(id)
print('UniversalProcessKit.adjustToTerrainHeight')
local x,_,z=getWorldTranslation(id)
local y=getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x, 0, z)
UniversalProcessKit.setWorldTranslation(id, x, y, z)
end;
local secondNormalDistributedRandomNumber=false -- function below generates 2 normal distributed random numbers
function getNormalDistributedRandomNumber() -- see http://de.wikipedia.org/wiki/Polar-Methode
if secondNormalDistributedRandomNumber~=false then
local r=secondNormalDistributedRandomNumber
secondNormalDistributedRandomNumber=false
return r
end
local u,v,q,p
repeat
u=2*mathrandom()-1
v=2*mathrandom()-1
q=u*u+v*v
until 0<q and q<1
p=mathsqrt(-2 * mathlog(q)/q)
secondNormalDistributedRandomNumber=v*p
return u*p
end;
----------------------------------
-- classes and variables ---------
----------------------------------
_g.UPK_ActivatorTrigger={}
_g.UPK_Animator={}
_g.UPK_BaleSpawner={}
_g.UPK_BaleTrigger={}
_g.UPK_BalerTrigger={}
_g.UPK_Base={}
_g.UPK_BuyTrigger={}
_g.UPK_Conveyor={}
_g.UPK_DisplayTrigger={}
_g.UPK_DumpTrigger={}
_g.UPK_EmptyTrigger={}
_g.UPK_EntityTrigger={}
_g.UPK_FillTrigger={}
_g.UPK_GasStationTrigger={}
_g.UPK_LiquidManureFillTrigger={}
_g.UPK_Mover={}
_g.UPK_PalletTrigger={}
_g.UPK_ParkTrigger={}
_g.UPK_Processor={}
_g.UPK_Scaler={}
_g.UPK_SellTarget={}
_g.UPK_SprayerFillTrigger={}
_g.UPK_Storage={}
_g.UPK_Switcher={}
_g.UPK_PlayerSpawner={}
_g.UPK_TipTrigger={}
_g.UPK_TipTriggerObject={}
_g.UPK_TipTriggerActivatable={}
_g.UPK_WashTrigger={}
_g.UPK_WaterFillTrigger={}
_g.PlaceableUPK={}
_g.OnCreateUPK={}
UPK_Storage.SEPARATE=1
UPK_Storage.SINGLE=2
UPK_Storage.FIFO=3
UPK_Storage.FILO=4
_g.g_upkTipTrigger={}
_g.g_upkTrigger={}
----------------------------------
-- __c ---------------------------
----------------------------------
-- adds arithmetic operations to tables
-- functionality for c() like in R, and a little more
-- examples:
-- same length
-- c(1) = {1}
-- c({1,2,3}) + c({4,5,6}) = {5,7,9}
-- c({1,2,3}) - c({4,5,6}) = {-3,-3,-3}
-- c({1,2,3}) * c({4,5,6}) = {4,10,18}
-- c({1,2,3}) / c({4,5,6}) = {0.25,0.4,0.5}
-- c({1,2,3}):min() = 1
-- c({1,2,3}):max() = 3
-- different length + - * /
-- c({1,2,3}) + c({2,3}) = {3,5,5}
-- with keys left (same like above with keys) + - * /
-- c({a=1,b=2,c=3}) + c({2,3}) = {a=3,b=5,c=5}
-- keys left and right (same, only take matching keys into account) + - * /
-- c({a=1,b=2,c=3}) + c({b=2,d=4}) = {b=4}
local c_mt={
__index=function(arr,key)
if type(key)=="number" and key>1 then
return arr[(key-1) % length(arr) +1]
end
return nil
end,
__add = function(lhs,rhs)
local arr={}
if rhs~=nil and type(lhs)=="table" then
if type(rhs)=="number" then
for k,v in pairs(lhs) do
if type(v)=="number" then
arr[k]=v+rhs
end
end
elseif type(rhs)=="table" then
local i=1
for k,v in pairs(lhs) do
if type(v)=="number" then
if type(rhs[k])=="number" then
arr[k]=lhs[k]+rhs[k]
elseif type(rhs[i])=="number" then
arr[k]=lhs[k]+rhs[i]
i=i+1
end
end
end
end
end
return __c(arr)
end,
__sub = function(lhs,rhs)
local arr={}
if rhs~=nil and type(lhs)=="table" then
if type(rhs)=="number" then
for k,v in pairs(lhs) do
if type(v)=="number" then
arr[k]=v-rhs
end
end
elseif type(rhs)=="table" then
local i=1
for k,v in pairs(lhs) do
if type(v)=="number" then
if type(rhs[k])=="number" then
arr[k]=lhs[k]-rhs[k]
elseif type(rhs[i])=="number" then
arr[k]=lhs[k]-rhs[i]
i=i+1
end
end
end
end
end
return __c(arr)
end,
__mul = function(lhs,rhs)
local arr={}
if rhs~=nil and type(lhs)=="table" then
if type(rhs)=="number" then
for k,v in pairs(lhs) do
if type(v)=="number" then
arr[k]=v*rhs
end
end
elseif type(rhs)=="table" then
local i=1
for k,v in pairs(lhs) do
if type(v)=="number" then
if type(rhs[k])=="number" then
arr[k]=lhs[k]*rhs[k]
elseif type(rhs[i])=="number" then
arr[k]=lhs[k]*rhs[i]
i=i+1
end
end
end
end
end
return __c(arr)
end,
__div = function(lhs,rhs)
local arr={}
if rhs~=nil and type(lhs)=="table" then
if type(rhs)=="number" then
for k,v in pairs(lhs) do
if type(v)=="number" then
arr[k]=v/rhs
end
end
elseif type(rhs)=="table" then
local i=1
for k,v in pairs(lhs) do
if type(v)=="number" then
if type(rhs[k])=="number" then
arr[k]=lhs[k]/rhs[k]
elseif type(rhs[i])=="number" then
arr[k]=lhs[k]/rhs[i]
i=i+1
end
end
end
end
end
return __c(arr)
end,
__mod = function(lhs,rhs)
local arr={}
if rhs~=nil and type(lhs)=="table" then
if type(rhs)=="number" then
for k,v in pairs(lhs) do
if type(v)=="number" then
arr[k]=v%rhs
end
end
elseif type(rhs)=="table" then
local i=1
for k,v in pairs(lhs) do
if type(v)=="number" then
if type(rhs[k])=="number" then
arr[k]=lhs[k]%rhs[k]
elseif type(rhs[i])=="number" then
arr[k]=lhs[k]%rhs[i]
i=i+1
end
end
end
end
end
return __c(arr)
end,
__call = function(func, ...)
local t={}
local args=...
if type(args)~="table" then
args={...}
end
for k,v in pairs(args) do
table.insert(t,k,func[v])
end
return __c(t)
end,
__concat = function(lhs,rhs) -- not consistent logic yet
local arr=lhs
for i=1,length(rhs) do
table.insert(arr,rhs[i])
end
return __c(arr)
end,
__len=function(t)
return length(t)
end
}
function _g.__c(...)
local arr=...
if type(arr)~="table" then
arr={...}
end
setmetatable(arr,c_mt)
function arr:min()
local nr=math.huge
local len=length(self)
if len>0 then
for i=1,len do
nr=mathmin(nr,self[i])
end
return nr
elseif len==0 then
for k,v in pairs(self) do
if type(self[k])=="number" then -- exclude functions
nr=mathmin(nr,v)
end
end
return nr
end
return nil
end
function arr:max(returnKey)
local nr=-math.huge
local key
local len=length(self)
if len>0 then
for i=1,len do
nr=mathmax(nr,self[i])
key=i
end
elseif len==0 then
for k,v in pairs(self) do
if type(v)=="number" then -- exclude functions
nr=mathmax(nr,v)
key=k
end
end
end
if len>=0 then
if returnKey then
return key
else
return nr
end
end
return nil
end
function arr:getValuesOf(keys)
if type(keys)~="table" then
keys={keys}
end
local values={}
for i=1,length(keys) do
values[keys[i]]=self[keys[i]]
end
return values
end
function arr:zeroToNil()
local values=self
for i=1,length(self) do
if self[i]==0 then
--values
end
end
return values
end
function arr:getKeysAreTrue()
local r={}
for k,v in pairs(self) do
if type(v)~="function" and v then
table.insert(r,k)
end
end
return r
end
return arr
end;
----------------------------------
-- ClassUPK ----------------------
----------------------------------
function _g.ClassUPK(members, baseClass)
members = members or {}
if baseClass ~= nil then
setmetatable(members, {__index = baseClass})
end
local mt = {
__index = function(t,k)
if t.storageType==UPK_Storage.SEPARATE then
if k=="capacity" then
if t.interestedInFillType ~= nil then -- exception for dumptrigger
return t:getCapacity(t.interestedInFillType)
end
return math.huge
elseif k=="fillLevel" then
if t.interestedInFillType ~= nil then -- exception for dumptrigger
return t:getFillLevel(t.interestedInFillType)
end
return 0
elseif k=="fillType" then
if t.interestedInFillType ~= nil then -- exception for dumptrigger
return t.interestedInFillType
end
return Fillable.FILLTYPE_UNKNOWN
end
--print('asked for '..tostring(k))
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 members[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
local newFillType = lhs.fillTypesConversionMatrix[Fillable.FILLTYPE_UNKNOWN][rhs.fillType]
if UniversalProcessKit.isSpecialFillType(newFillType) then
added = UniversalProcessKitEnvironment.flbs[newFillType] + rhs.fillLevel
elseif lhs.storageType==UPK_Storage.SEPARATE then
if newFillType~=nil and lhs.p_flbs[newFillType]~=nil then
added = lhs.p_flbs[newFillType] + rhs
elseif lhs.parent ~= nil then
added = lhs.parent + rhs
end
elseif lhs.storageType==UPK_Storage.SINGLE then
added = lhs.p_flbs[1] + rhs
elseif lhs.storageType==UPK_Storage.FIFO then
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
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
local newFillType = lhs.fillTypesConversionMatrix[Fillable.FILLTYPE_UNKNOWN][rhs.fillType]
if newFillType~=nil and UniversalProcessKit.isSpecialFillType(newFillType) then
added = UniversalProcessKitEnvironment.flbs[newFillType] - rhs.fillLevel
elseif lhs.storageType==UPK_Storage.SEPARATE then
if newFillType~=nil and lhs.p_flbs[newFillType]~=nil then
added = lhs.p_flbs[newFillType] - rhs
elseif lhs.parent ~= nil then
added = lhs.parent - rhs
end
elseif lhs.storageType==UPK_Storage.SINGLE then
added = lhs.p_flbs[1] - rhs
elseif lhs.storageType==UPK_Storage.FIFO then
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
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
}
function members:class()
return members
end
function members:superClass()
return baseClass
end
function members:isa(other)
local ret = false
local curClass = members
while curClass ~= nil and ret == false do
if curClass == other then
ret = true
else
curClass = curClass:superClass()
end
end
return ret
end
return mt
end;
----------------------------------
-- debugMode ---------------------
----------------------------------
local debug_mt={
__index=function(obj,key)
local baseObj=rawget(obj,'baseObj')
local result=baseObj[key]
if type(result)=="function" then
local oldFunc=result
local function newFunc(...)
local argsStr=""
local args={...}
if #args>0 then
local mink, maxk = getMinMaxKeys(args)
for i=mink, maxk do
if(strlen(argsStr)>0)then -- strlen is utf8Strlen
argsStr=argsStr..', '
end
if args[i]==obj then
argsStr=argsStr..'self'
else
argsStr=argsStr..tostring(v)
end
end
end
obj:print('calling '..tostring(key)..'('..argsStr..'):')
local printLevel=rawget(obj,'printLevel') or 0
rawset(obj,'printLevel',printLevel+1)
local results = {oldFunc(...)}
rawset(obj,'printLevel',printLevel)
local returnsStr=""
if #results>0 then
local mink, maxk = getMinMaxKeys(results)
for i=mink, maxk do
if(strlen(returnsStr)>0)then -- strlen is utf8Strlen
returnsStr=returnsStr..', '
end
returnsStr=returnsStr..tostring(results[i])
end
end
if returnsStr=="" then
returnsStr="(nothing)"
end
obj:print('called '..tostring(key)..'() returns '..returnsStr)
return unpack(results)
end
return newFunc
end
obj:print('indexed '..tostring(key)..', got '..tostring(result))
return result
end,
__newindex=function(obj,key,val)
obj:print('set '..tostring(key)..' = '..tostring(val))
local baseObj=rawget(obj,'baseObj')
baseObj[key]=val
end,
__call=function(t, ...)
local baseObj=rawget(t,'baseObj')
return baseObj(...)
end,
__add=function(lhs,rhs)
local baseObj=rawget(lhs,'baseObj')
return baseObj + rhs
end,
__sub=function(lhs,rhs)
local baseObj=rawget(lhs,'baseObj')
return baseObj - rhs
end,
__mul=function(lhs,rhs)
local baseObj=rawget(lhs,'baseObj')
return baseObj * rhs
end,
__div=function(lhs,rhs)
local baseObj=rawget(lhs,'baseObj')
return baseObj / rhs
end,
__mod=function(lhs,rhs)
local baseObj=rawget(lhs,'baseObj')
return baseObj % rhs
end,
__pow=function(lhs,rhs)
local baseObj=rawget(lhs,'baseObj')
return baseObj ^ rhs
end,
__concat=function(lhs,rhs)
local baseObj=rawget(lhs,'baseObj')
return baseObj .. rhs
end,
__len=function(t)
local baseObj=rawget(t,'baseObj')
return #baseObj
end,
__gt=function(lhs,rhs)
local baseObj=rawget(lhs,'baseObj')
return baseObj > rhs
end,
__ge=function(lhs,rhs)
local baseObj=rawget(lhs,'baseObj')
return baseObj >= rhs
end,
__lt=function(lhs,rhs)
local baseObj=rawget(lhs,'baseObj')
return baseObj < rhs
end,
__le=function(lhs,rhs)
local baseObj=rawget(lhs,'baseObj')
return baseObj <= rhs
end
}
-- USAGE
-- object = Class:new(x,y)
-- object = debugObject(object)
-- then use like normal
function _g.debugObject(baseObj) -- _g may not be needed
local obj={}
obj.baseObj=baseObj
obj.printLevel=0
function obj.print(obj, ...)
local premsg=""
local printLevel=rawget(obj,'printLevel') or 0
for i=0,printLevel do
premsg='-'..premsg
end
local msg=premsg..' '..tostring(...)
local baseObj=rawget(obj,'baseObj')
if baseObj.print~=nil then
baseObj.print(baseObj,msg)
else
print(msg)