1 local explosionRadius = 2
3 local displaceDirt = true
4 local throwRocksIntoWater = true
5 local throwRocks = true
6 local maximumRockThrowDistance = explosionRadius + 8
8 local replaceableTiles = {
10 ["deepwater"] = "grass-1",
11 ["water-green"] = "grass-1",
12 ["deepwater-green"] = "grass-1"
16 script.on_configuration_changed(function(data)
17 if global.waterbombs ~= nil then
18 script.on_event(defines.events.on_tick, tickBombs)
23 script.on_load(function(event)
24 if global.waterbombs ~= nil then
25 script.on_event(defines.events.on_tick, tickBombs)
30 script.on_event(defines.events.on_built_entity, function(event)
31 if event.created_entity.name == "waterbomb" then
32 global.waterbombs = {}
33 for _, surface in pairs(game.surfaces) do
34 for _, entity in pairs(surface.find_entities_filtered{name="waterbomb"}) do
35 table.insert(global.waterbombs, entity)
38 script.on_event(defines.events.on_tick, tickBombs)
43 function tickBombs(event)
44 if event.tick % 20 == 0 then
45 for k,e in pairs(global.waterbombs) do
47 table.remove(global.waterbombs, k)
49 elseif e.energy > 49000000 then
50 local x, y, distX, distY, surface
56 for xx = x - explosionRadius, x + explosionRadius, 2 do
57 for yy = y - explosionRadius, y + explosionRadius, 2 do
58 distX = math.abs(x - xx)
59 distY = math.abs(y - yy)
61 if math.floor(math.sqrt((distX * distX) + (distY * distY))) <= explosionRadius then
62 e.surface.create_entity({name = "medium-explosion", position = {x = xx, y = yy}})
68 e.surface.create_entity({name = "waterbomb-detonation", position = e.position, target = e, speed = 1})
71 for a = 1, explosionRadius * 10 do
72 createRandomStone(e.position, surface)
73 createRandomStone(e.position, surface)
78 table.remove(global.waterbombs, k)
81 createWater(x, y, explosionRadius, surface)
82 throwDirt(x, y, explosionRadius, surface)
86 -- nothing more to be exploded. Stop ticking.
87 if #global.waterbombs == 0 then
88 global.waterbombs = nil
89 script.on_event(defines.events.on_tick, nil)
94 function createWater(x, y, size, surface)
95 -- Setting tiles to water where player characters are standing deletes the character and sets them to god mode. We dont want that.
96 local players = surface.find_entities_filtered({area = {{x - size - 1, y - size - 1}, {x + size + 1, y + size + 1}}, type="character"})
101 local waterTiles = {}
105 for wx = x - size, x + size, 1 do
106 for wy = y - size, y + size, 1 do
107 table.insert(waterTiles, {name="water", position={wx, wy}})
110 surface.set_tiles(waterTiles)
113 table.insert(waterTiles, {name="deepwater", position={x, y}})
114 surface.set_tiles(waterTiles)
118 function throwDirt(x, y, size, surface)
121 local floor = math.floor
124 if displaceDirt == true then
128 for xx = x - (size + 1), x + (size + 1), 1 do
129 for yy = y - (size + 1), y + (size + 1), 1 do
130 distX = math.abs(x - xx)
131 distY = math.abs(y - yy)
133 if floor(math.sqrt((distX * distX) + (distY * distY))) >= 2 then
134 table.insert(dirtTiles, {name = "grass-1", position = {xx, yy}})
139 if #dirtTiles ~= 0 then
140 surface.set_tiles(dirtTiles)
146 function createRandomStone(position, surface)
149 local floor = math.floor
150 local random = math.random
152 if throwRocks == true then
156 if random() > 0.5 then
157 x = x - floor(random(2, maximumRockThrowDistance))
159 x = x + floor(random(2, maximumRockThrowDistance))
162 if random() < 0.5 then
163 y = y - floor(random(2, maximumRockThrowDistance))
165 y = y + floor(random(2, maximumRockThrowDistance))
168 tileName = surface.get_tile(floor(x), floor(y)).name
170 if replaceableTiles[tileName] then
171 if throwRocksIntoWater == true then
172 surface.set_tiles({{name=replaceableTiles[tileName], position={floor(x), floor(y)}}})
175 if floor(random(1, 4)) > 2 then
176 surface.create_entity({name = "stone", position = {x, y}}).amount = floor(random(13, 27))
177 surface.create_entity({name = "explosion", position = {x, y}})