-
-
Save captncraig/10e8884775774ef4b36e3e2b2172b18f to your computer and use it in GitHub Desktop.
ghetto loaders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "defines" | |
script.on_init(function() | |
if global.timer == nil then | |
global.timer = 0 | |
end | |
end) | |
script.on_load(function() | |
if global.timer == nil then | |
global.timer = 0 | |
end | |
end) | |
script.on_event(defines.events.on_tick, function(event) | |
if global.timer > 9 then | |
global.timer = 0 | |
voidTanks() | |
creativeTanks() | |
else | |
global.timer = global.timer + 1 | |
end | |
creativeChests() | |
voidChests() | |
end) | |
script.on_event(defines.events.on_built_entity, function(event) | |
if event.created_entity.name == "c-inv_creativeChest" then | |
if global.creativeChests == nil then global.creativeChests = {} end | |
table.insert(global.creativeChests, event.created_entity) | |
elseif event.created_entity.name == "c-inv_creativeTank" then | |
if global.creativeTanks == nil then global.creativeTanks = {} end | |
table.insert(global.creativeTanks, event.created_entity) | |
elseif event.created_entity.name == "c-inv_voidChest" then | |
if global.voidChests == nil then global.voidChests = {} end | |
table.insert(global.voidChests, event.created_entity) | |
elseif event.created_entity.name == "c-inv_voidTank" then | |
if global.voidTanks == nil then global.voidTanks = {} end | |
table.insert(global.voidTanks, event.created_entity) | |
end | |
end) | |
function voidChests() | |
if global.voidChests ~= nil then | |
for i = 1, #global.voidChests do | |
if global.voidChests[i].valid then | |
global.voidChests[i].clear_items_inside() | |
emptyNeighborBelts(global.voidChests[i]) | |
else | |
table.remove(global.voidChests, i) | |
break | |
end | |
end | |
end | |
end | |
function voidTanks() | |
if global.voidTanks ~= nil then | |
for i = 1, #global.voidTanks do | |
if global.voidTanks[i].valid then | |
global.voidTanks[i].fluidbox[1] = nil | |
else | |
table.remove(global.voidTanks, i) | |
break | |
end | |
end | |
end | |
end | |
function creativeTanks() | |
if global.creativeTanks ~= nil then | |
for i = 1, #global.creativeTanks do | |
if global.creativeTanks[i].valid then | |
if global.creativeTanks[i].fluidbox[1] ~= nil then | |
local tank = global.creativeTanks[i].fluidbox[1] | |
tank.amount = 2500 | |
global.creativeTanks[i].fluidbox[1] = tank | |
end | |
else | |
table.remove(global.creativeTanks, i) | |
break | |
end | |
end | |
end | |
end | |
function creativeChests() | |
if global.creativeChests ~= nil then | |
for i =1, #global.creativeChests do | |
if global.creativeChests[i].valid then | |
local chest = global.creativeChests[i].get_inventory(1) | |
if chest[1].valid_for_read then | |
local a = 1 | |
if chest[1].prototype.stack_size < 5 then a = 20 else a = 100 end | |
if global.creativeChests[i].can_insert(chest[1]) and chest.get_item_count(chest[1].name) < a then | |
global.creativeChests[i].insert({name = chest[1].name, count = a - chest.get_item_count(chest[1].name)}) | |
end | |
fillNeighborBelts(global.creativeChests[i], chest[1]) | |
end | |
else | |
table.remove(global.creativeChests, i) | |
break | |
end | |
end | |
end | |
end | |
local function log(fmt, ...) | |
for i, p in ipairs(game.players) do | |
p.print(string.format(fmt,unpack({...}))) | |
end | |
end | |
function fillNeighborBelts(ent,item) | |
local find = function(px,py,dir) | |
local ents = ent.surface.find_entities({{px, py}, {px, py}}) | |
for i,e in ipairs(ents) do | |
if e.direction == dir and e.type == "transport-belt" then | |
local left = e.get_transport_line(1) | |
local right = e.get_transport_line(2) | |
local si = {} | |
si.name = item.name | |
si.count = 1 | |
if left.can_insert_at_back() then | |
left.insert_at_back(si) | |
end | |
if right.can_insert_at_back() then | |
right.insert_at_back(si) | |
end | |
end | |
end | |
end | |
local x = ent.position.x | |
local y = ent.position.y | |
find(x,y+1, defines.direction.south) | |
find(x,y-1, defines.direction.north) | |
find(x-1,y, defines.direction.west) | |
find(x+1,y, defines.direction.east) | |
end | |
function emptyNeighborBelts(ent) | |
local find = function(px,py,dir) | |
local ents = ent.surface.find_entities({{px, py}, {px, py}}) | |
for i,e in ipairs(ents) do | |
if e.direction == dir and e.type == "transport-belt" then | |
local left = e.get_transport_line(1) | |
local right = e.get_transport_line(2) | |
left.clear() | |
right.clear() | |
end | |
end | |
end | |
local x = ent.position.x | |
local y = ent.position.y | |
find(x,y+1, defines.direction.north) | |
find(x,y-1, defines.direction.south) | |
find(x-1,y, defines.direction.east) | |
find(x+1,y, defines.direction.west) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment