/
centrifuge.lua
177 lines (158 loc) · 5.41 KB
/
centrifuge.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
require "/objects/generic/centrifuge_recipes.lua"
require "/scripts/fu_storageutils.lua"
require "/scripts/kheAA/transferUtil.lua"
require '/scripts/fupower.lua'
function init()
if config.getParameter('powertype') then
power.init()
powered = true
else
powered = false
end
--storage.activeConsumption = storage.activeConsumption or false
self.requiredPower=config.getParameter("isn_requiredPower")
self.powered=(type(self.requiredPower)=="number") and (self.requiredPower>0)
self.centrifugeType = config.getParameter("centrifugeType") or error("centrifugeType is undefined in .object file") -- die horribly
self.itemChances = config.getParameter("itemChances")
self.inputSlot = config.getParameter("inputSlot",1)
self.initialCraftDelay = config.getParameter("craftDelay",1)
--script.setUpdateDelta(self.initialCraftDelay*60.0)
script.setUpdateDelta(1)
if self.powered then
self.effectiveRequiredPower=self.requiredPower*self.initialCraftDelay
end
--storage.craftDelay = storage.craftDelay or self.initialCraftDelay
storage.combsProcessed = storage.combsProcessed or { count = 0 }
--sb.logInfo("centrifuge: %s", storage.combsProcessed)
self.combsPerJar = 3 -- ref. recipes
self.recipeTable = getRecipes()
self.recipeTypes = self.recipeTable.recipeTypes[self.centrifugeType]
handleOnOff()
math.randomseed(util.seedTime())
object.setInteractive(true)
end
function deciding(item)
for i=#self.recipeTypes,1,-1 do
if self.recipeTable[self.recipeTypes[i]][item.name] then
return self.recipeTable[self.recipeTypes[i]][item.name]
end
end
return nil
end
function update(dt)
if not transferUtilDeltaTime or (transferUtilDeltaTime > 1) then
transferUtilDeltaTime=0
transferUtil.loadSelfContainer()
else
transferUtilDeltaTime=transferUtilDeltaTime+dt
end
if not storage.timer then storage.timer=self.initialCraftDelay end
if storage.timer > 0 then
storage.timer = math.max(storage.timer - dt,0)
elseif storage.timer == 0 then
if storage.activeConsumption then
storage.currentinput={ name = storage.input.name, count = 1}
stashHoney(storage.input.name)
storage.input = nil
storage.activeConsumption = false
local rnd = math.random()
for item, chancePair in pairs(storage.output) do
local chanceBase,chanceDivisor = table.unpack(chancePair)
local chance = self.itemChances[chanceBase] / chanceDivisor
local done=false
if rnd <= chance then
local throw={parameters={}, name=item, count=1}
local contSize=world.containerSize(entity.id())
for i=self.inputSlot,contSize-1 do
if throw then
throw = world.containerPutItemsAt(entity.id(),throw,i)
end
if not throw then
done=true
break
end
end
if throw then
world.spawnItem(throw, entity.position())
done=true
end
if done then
break
end
end
storage.currentinput=nil
rnd = rnd - chance
end
storage.output=nil
storage.timer=-1
end
local input
for i=0,self.inputSlot-1 do
input = world.containerItemAt(entity.id(),i)
if input then
local output = deciding(input)
if output then
if ((not self.powered) or (power.getTotalEnergy()>=self.effectiveRequiredPower)) and world.containerConsume(entity.id(), { name = input.name, count = 1, data={}}) and ((not self.powered) or power.consume(self.effectiveRequiredPower)) then
storage.output = output
storage.input = input
storage.activeConsumption = true
else
storage.activeConsumption = false
end
break
end
end
end
else
handleOnOff()
storage.timer = self.initialCraftDelay
end
if storage.combsProcessed and storage.combsProcessed.count > 0 then
-- discard the stash if unclaimed by a jarrer within a reasonable time (twice the craft delay)
storage.combsProcessed.stale = (storage.combsProcessed.stale or (self.initialCraftDelay * 2)) - dt
if storage.combsProcessed.stale == 0 then
drawHoney() -- effectively clear the stash, stopping the jarrer from getting it
end
end
if powered then
power.update(dt)
end
end
function handleOnOff()
animator.setAnimationState("centrifuge", storage.activeConsumption and "working" or "idle")
if object.outputNodeCount() > 0 then
object.setOutputNodeLevel(0,storage.activeConsumption)
end
end
function stashHoney(comb)
-- For any nearby jarrer (if this is an industrial centrifuge),
-- Record that we've processed a comb.
-- The stashed type is the jar object name for the comb type.
-- If the stashed type is different, reset the count.
local jar = honeyCheck and honeyCheck(comb)
if jar then
if storage.combsProcessed == nil then storage.combsProcessed = { count = 0 } end
if storage.combsProcessed.type == jar then
storage.combsProcessed.count = math.min(storage.combsProcessed.count + 1, self.combsPerJar) -- limit to one jar's worth in stash at any given time
storage.combsProcessed.stale = nil
else
storage.combsProcessed = { type = jar, count = 1 }
end
--sb.logInfo("STASH: %s %s", storage.combsProcessed.count,storage.combsProcessed.type)
end
end
-- Called by the honey jarrer
function drawHoney()
if not storage.combsProcessed or storage.combsProcessed.count == 0 then return nil end
local ret = storage.combsProcessed
storage.combsProcessed = { count = 0 }
--sb.logInfo("STASH: Withdrawing")
return ret
end
function die()
--safety for fringe cases
if storage.currentinput then
world.spawnItem(storage.currentinput,entity.position())
end
storage.currentinput=nil
end