/
control.lua
117 lines (82 loc) · 2.83 KB
/
control.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
require "util"
require "perlin"
script.on_event(defines.events.on_built_entity, function(event)
if event.created_entity.name == "wind-generator" and event.created_entity.surface.name == "nauvis" then
if global.windGenerators == nil then
global.windGenerators = {}
end
table.insert(global.windGenerators, event.created_entity)
elseif event.created_entity.name == "tidal-generator" then
if global.tidalGenerators == nil then
global.tidalGenerators = {}
end
table.insert(global.tidalGenerators, event.created_entity)
end
end)
script.on_event(defines.events.on_robot_built_entity, function(event)
if event.created_entity.name == "wind-generator" and event.created_entity.surface.name == "nauvis" then
if global.windGenerators == nil then
global.windGenerators = {}
end
table.insert(global.windGenerators, event.created_entity)
elseif event.created_entity.name == "tidal-generator" then
if global.tidalGenerators == nil then
global.tidalGenerators = {}
end
table.insert(global.tidalGenerators, event.created_entity)
end
end)
local offset = 0.5051543
script.on_event(defines.events.on_tick, function(event)
local currentTick = event.tick
if global.windGenerators ~= nil then
local windGenerators = global.windGenerators
local fluid = {}
fluid.type = "wind"
local noise = perlin:noise(currentTick * 0.0001, currentTick * 0.0001, currentTick * 0.0001) + offset
if noise < 0 then
noise = 0
elseif noise > 1 then
noise = 1
end
local t1 = noise + 15
--local t2 = ((perlin:noise(currentTick * 0.0002, currentTick * 0.0002, currentTick * 0.0002) + offset) / divider * 90 - 20) / 2
--local t3 = ((perlin:noise(currentTick * 0.0004, currentTick * 0.0004, currentTick * 0.0004) + offset) / divider * 90 - 20) / 4
--local t4 = ((perlin:noise(currentTick * 0.001, currentTick * 0.001, currentTick * 0.001) + offset) / divider * 90 - 20 ) / 10
--fluid.temperature = (t1 + t2 + t3 + t4) / 1.85
fluid.temperature = t1
fluid.amount = noise * 100
for i=1, #windGenerators do
if windGenerators[i] ~= nil then
if windGenerators[i].valid then
windGenerators[i].fluidbox[1] = fluid
else
windGenerators[i] = nil
end
end
end
end
if global.tidalGenerators ~= nil then
local tidalGenerators = global.tidalGenerators
local fluid = {}
fluid.type = "tide"
local func = (math.sin(currentTick * 8 / 25000) + math.cos(currentTick * 2.1 * 8 / 25000)) / 2
if func < 0 then
func = 0
elseif func > 1 then
func = 1
end
local t1 = func
fluid.temperature = t1 + 14.87
fluid.amount = func * 100
for i=1, #tidalGenerators do
if tidalGenerators[i] ~= nil then
if tidalGenerators[i].valid then
tidalGenerators[i].fluidbox[1] = fluid
else
tidalGenerators[i] = nil
end
end
end
end
end)