local explosionRadius = 2 local displaceDirt = true local throwRocksIntoWater = true local throwRocks = true local maximumRockThrowDistance = explosionRadius + 8 local bombs local replaceableTiles = { ["water"] = "grass-1", ["deepwater"] = "grass-1", ["water-green"] = "grass-1", ["deepwater-green"] = "grass-1" } script.on_configuration_changed(function(data) if global.waterbombs ~= nil then script.on_event(defines.events.on_tick, tickBombs) end end) script.on_load(function(event) if global.waterbombs ~= nil then script.on_event(defines.events.on_tick, tickBombs) end end) script.on_event(defines.events.on_built_entity, function(event) if event.created_entity.name == "waterbomb" then global.waterbombs = {} for _, surface in pairs(game.surfaces) do for _, entity in pairs(surface.find_entities_filtered{name="waterbomb"}) do table.insert(global.waterbombs, entity) end end script.on_event(defines.events.on_tick, tickBombs) end end) function tickBombs(event) if event.tick % 20 == 0 then for k,e in pairs(global.waterbombs) do if not e.valid then table.remove(global.waterbombs, k) elseif e.energy > 49000000 then local x, y, distX, distY, surface x = e.position.x y = e.position.y surface = e.surface -- create explosions for xx = x - explosionRadius, x + explosionRadius, 2 do for yy = y - explosionRadius, y + explosionRadius, 2 do distX = math.abs(x - xx) distY = math.abs(y - yy) if math.floor(math.sqrt((distX * distX) + (distY * distY))) <= explosionRadius then e.surface.create_entity({name = "medium-explosion", position = {x = xx, y = yy}}) end end end -- big detonation e.surface.create_entity({name = "waterbomb-detonation", position = e.position, target = e, speed = 1}) -- random stones for a = 1, explosionRadius * 10 do createRandomStone(e.position, surface) createRandomStone(e.position, surface) end -- bomb explodes e.destroy() table.remove(global.waterbombs, k) -- water is created createWater(x, y, explosionRadius, surface) throwDirt(x, y, explosionRadius, surface) end end -- nothing more to be exploded. Stop ticking. if #global.waterbombs == 0 then global.waterbombs = nil script.on_event(defines.events.on_tick, nil) end end end function createWater(x, y, size, surface) -- Setting tiles to water where players are standing deletes the player and sets them to god mode. local players = surface.find_entities_filtered({area = {{x - size - 1, y - size - 1}, {x + size + 1, y + size + 1}}, type="player"}) if #players ~= 0 then return end local waterTiles = {} x = math.floor(x) y = math.floor(y) for wx = x - size, x + size, 1 do for wy = y - size, y + size, 1 do table.insert(waterTiles, {name="water", position={wx, wy}}) end end surface.set_tiles(waterTiles) waterTiles = {} table.insert(waterTiles, {name="deepwater", position={x, y}}) surface.set_tiles(waterTiles) end function throwDirt(x, y, size, surface) local dirtTiles = {} local tileName local floor = math.floor local distX,distY if displaceDirt == true then x = floor(x) y = floor(y) for xx = x - (size + 1), x + (size + 1), 1 do for yy = y - (size + 1), y + (size + 1), 1 do distX = math.abs(x - xx) distY = math.abs(y - yy) if floor(math.sqrt((distX * distX) + (distY * distY))) >= 2 then table.insert(dirtTiles, {name = "grass-1", position = {xx, yy}}) end end end if #dirtTiles ~= 0 then surface.set_tiles(dirtTiles) end end end function createRandomStone(position, surface) local x,y local tileName local floor = math.floor local random = math.random if throwRocks == true then x = position.x y = position.y if random() > 0.5 then x = x - floor(random(2, maximumRockThrowDistance)) else x = x + floor(random(2, maximumRockThrowDistance)) end if random() < 0.5 then y = y - floor(random(2, maximumRockThrowDistance)) else y = y + floor(random(2, maximumRockThrowDistance)) end tileName = surface.get_tile(floor(x), floor(y)).name if replaceableTiles[tileName] then if throwRocksIntoWater == true then surface.set_tiles({{name=replaceableTiles[tileName], position={floor(x), floor(y)}}}) end else if floor(random(1, 4)) > 2 then surface.create_entity({name = "stone", position = {x, y}}).amount = floor(random(13, 27)) surface.create_entity({name = "explosion", position = {x, y}}) end end end end