forked from Blu3wolf/Treefarm-Caffeine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.lua
More file actions
279 lines (236 loc) · 8.64 KB
/
control.lua
File metadata and controls
279 lines (236 loc) · 8.64 KB
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
require "defines"
local BUFFTIME = 60
local DEBUFFTIME = 90
local BUFFMODIFIER = 1
local DEBUFFMODIFIER = -0.5
-- the (arbitrary) name of the seed, only used internally
local mySeedTypeName1 = "coffeeplant"
local mySeedTypeName2 = "teaplant"
-- the entity-names of the growing stages
-- here the plant will grow from "alien-seed" over "small-alien-plant" over "medium-alien-plant" to "mature-alien-plant"
local myGrowingStates1 =
{
"tf-coffee-seed",
"tf-small-coffee-plant",
"tf-medium-coffee-plant",
"tf-mature-coffee-plant"
}
local myGrowingStates2 =
{
"tf-tea-seed",
"tf-small-tea-bush",
"tf-medium-tea-bush",
"tf-mature-tea-bush"
}
-- the mining-result of the last growing state
local myOutput1 = {"tf-coffee-beans", 1}
local myOutput2 = {"tf-tea-leaves", 2}
-- defines the growing speed-modifier for different tiles
-- 1.00 means normal speed; 0.50 means half speed; 2.00 means double speed
local myTileEfficiency1 =
{
["grass"] = 1.00,
["grass-medium"] = 1.00,
["grass-dry"] = 0.90,
["dirt"] = 0.80,
["dirt-dark"] = 0.80,
["hills"] = 1.00,
["sand"] = 0.50,
["sand-dark"] = 0.50,
["other"] = 0.1
}
local myTileEfficiency2 =
{
["grass"] = 0.50,
["grass-medium"] = 0.60,
["grass-dry"] = 1.00,
["dirt"] = 1.00,
["dirt-dark"] = 1.00,
["hills"] = 1.00,
["sand"] = 0.50,
["sand-dark"] = 0.50,
["other"] = 0.1
}
-- defines the minimum amount of ticks that are needed to evolve into the next growing-state
local myBasicGrowingTime1 = 12500 * 0.9 -- approx 1/2 day +/- 10%
local myBasicGrowingTime2 = 25000 * 0.9 -- approx 1 day +/- 10%
-- defines the highest value that might be added to the basic growing time
-- in general the growing time is determined by basicGrowingTime + randomValue
-- randomValue is between 0 and randomGrowingTime
local myRandomGrowingTime1 = 12500 / 5
local myRandomGrowingTime2 = 25000 / 5
-- defines how big the impact of fertilizer is
-- the total growing efficiency is TileEfficiency + fertilizerBoost ( if fertilizer was applied)
-- e.g. total efficiency of an alient-plant on a grass-tile with fertilizer = 2, which means double growing speed
local myFertilizerBoost1 = 0.50
local myFertilizerBoost2 = 0.50
-- !!!do not modify!!!
local allInOne1 =
{
["name"] = mySeedTypeName1,
["states"] = myGrowingStates1,
["output"] = myOutput1,
["efficiency"] = myTileEfficiency1,
["basicGrowingTime"] = myBasicGrowingTime1,
["randomGrowingTime"] = myRandomGrowingTime1,
["fertilizerBoost"] = myFertilizerBoost1
}
local allInOne2 =
{
["name"] = mySeedTypeName2,
["states"] = myGrowingStates2,
["output"] = myOutput2,
["efficiency"] = myTileEfficiency2,
["basicGrowingTime"] = myBasicGrowingTime2,
["randomGrowingTime"] = myRandomGrowingTime2,
["fertilizerBoost"] = myFertilizerBoost2
}
script.on_init(function()
initTables()
-- this is used to "transfer" the needed information to the treefarm mod
if (remote.interfaces.treefarm_interface) and (remote.interfaces.treefarm_interface.addSeed) then
-- if something went wrong an error message is returned
-- if everything is ok the function returns nil
local errorMsg1 = remote.call("treefarm_interface", "addSeed", allInOne1)
if errorMsg2 ~= nil then
for _, player in ipairs(game.players) do
player.print(errorMsg)
end
return
end
local errorMsg2 = remote.call("treefarm_interface", "addSeed", allInOne2)
if errorMsg2 ~= nil then
for _, player in ipairs(game.players) do
player.print(errorMsg)
end
return
end
global.initDone = true
end
end)
script.on_event(defines.events.on_built_entity, function(event)
if global.initDone == true then
if event.created_entity.name == "tf-crafting-speed-booster" then
if global.blocking == nil then
global.blocking = {ent = event.created_entity, counter = 0}
else
global.blocking.ent.destroy()
global.blocking = {ent = event.created_entity, counter = 0}
end
if global.caffeineTimer.state == "nothing" then
global.caffeineTimer.running = true
global.caffeineTimer.state = "buff"
global.caffeineTimer.initialModifierValue = game.forces.player.manual_crafting_speed_modifier
game.forces.player.manual_crafting_speed_modifier = BUFFMODIFIER
global.caffeineTimer.buffValue = global.caffeineTimer.buffValue + BUFFTIME
global.caffeineTimer.debuffValue = global.caffeineTimer.debuffValue + DEBUFFTIME
global.caffeineTimer.amount = 1
elseif global.caffeineTimer.state == "buff" then
if (global.caffeineTimer.amount) and (global.caffeineTimer.amount >= 0) then
global.caffeineTimer.amount = global.caffeineTimer.amount + 1
else
global.caffeineTimer.amount = 1
end
global.caffeineTimer.buffValue = global.caffeineTimer.buffValue + math.floor(BUFFTIME / global.caffeineTimer.amount)
global.caffeineTimer.debuffValue = global.caffeineTimer.debuffValue + math.floor(DEBUFFTIME / global.caffeineTimer.amount)
elseif global.caffeineTimer.state == "debuff" then
--global.caffeineTimer.state = "buff"
--game.forces.player.manual_crafting_speed_modifier = BUFFMODIFIER
--global.caffeineTimer.buffValue = global.caffeineTimer.buffValue + BUFFTIME
global.caffeineTimer.debuffValue = global.caffeineTimer.debuffValue - math.floor(0.1 * global.caffeineTimer.debuffValue)
end
showGUI()
end
end
end)
script.on_event(defines.events.on_tick, function(event)
if (global.blocking ~= nil) then
global.blocking.counter = global.blocking.counter + 1
if global.blocking.counter >= 60 then
global.blocking.ent.destroy()
global.blocking = nil
end
end
if (game.tick % 60 == 0) and (global.caffeineTimer.running == true) and (global.initDone == true) then
if global.caffeineTimer.state == "buff" then
global.caffeineTimer.buffValue = global.caffeineTimer.buffValue - 1
if global.caffeineTimer.buffValue <= 0 then
global.caffeineTimer.buffValue = 0
global.caffeineTimer.state = "debuff"
game.forces.player.manual_crafting_speed_modifier = DEBUFFMODIFIER
end
elseif global.caffeineTimer.state == "debuff" then
global.caffeineTimer.debuffValue = global.caffeineTimer.debuffValue - 1
if global.caffeineTimer.debuffValue <= 0 then
global.caffeineTimer.debuffValue = 0
global.caffeineTimer.state = "nothing"
global.caffeineTimer.running = false
game.forces.player.manual_crafting_speed_modifier = global.caffeineTimer.initialModifierValue
for _, player in ipairs(game.players) do
if player.gui.left.caffeineRoot ~= nil then
player.gui.left.caffeineRoot.destroy()
end
end
end
end
updateGUI()
end
end)
script.on_event(defines.events.on_research_finished, function(event)
if event.research == "tf-caffeine" then
for _,player in pairs(game.players) do
if player.can_insert{name="tf-coffee-seed", count = 10} then
player.insert{name="tf-coffee-seed", count = 10}
else
game.get_surface("nauvis").create_entity{name = "item-on-ground", position = player.position, stack = {name="tf-coffee-seed", count = 10}}
end
if player.can_insert{name = "tf-tea-seed", count = 10} then
player.insert{name = "tf-tea-seed", count = 10}
else
game.get_surface("nauvis").create_entity{name = "item-on-ground", position = player.position, stack = {name="tf-tea-seed", count = 10}}
end
end
end
end)
function showGUI()
for _, player in ipairs(game.players) do
if player.gui.left.caffeineRoot == nil then
--create GUI
local text = "Increased for"
local timerValue = global.caffeineTimer.buffValue
local timerCaption = string.format("%s %d s", text, timerValue)
player.gui.left.add{type = "frame", name = "caffeineRoot", caption = {"caffeineGUICaption"}, direction = "vertical"}
player.gui.left.caffeineRoot.add{type = "label", name = "caffeineTimerLabel", caption = timerCaption}
end
end
end
function updateGUI()
local text
local timerValue
if global.caffeineTimer.state == "buff" then
text = "Increased for"
timerValue = global.caffeineTimer.buffValue
elseif global.caffeineTimer.state == "debuff" then
text = "Decreased for"
timerValue = global.caffeineTimer.debuffValue
end
for _, player in ipairs(game.players) do
if player.gui.left.caffeineRoot ~= nil then
local arguments = "%s %d s"
if timerValue > 600 then
timerValue = math.floor(timerValue / 60)
arguments = "%s %d min"
end
player.gui.left.caffeineRoot.caffeineTimerLabel.caption = string.format(arguments, text, timerValue)
end
end
end
function initTables()
global.caffeineTimer = {}
global.caffeineTimer.running = false
global.caffeineTimer.state = "nothing"
global.caffeineTimer.buffValue = 0
global.caffeineTimer.debuffValue = 0
global.caffeineTimer.initialModifierValue = game.forces.player.manual_crafting_speed_modifier
global.initDone = false
end