Skip to content

Commit

Permalink
Merge pull request #1078 from grilledham/container_dump
Browse files Browse the repository at this point in the history
Container dump
  • Loading branch information
grilledham authored Sep 3, 2020
2 parents 38a5948 + 3cb3809 commit 7be4c70
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
7 changes: 6 additions & 1 deletion features/player_stats.lua
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ local function player_mined_item(event)
end

local function player_crafted_item(event)
change_for_player(event.player_index, player_items_crafted_name, event.item_stack.count)
local item_stack = event.item_stack
if not item_stack or not item_stack.valid_for_read then
return
end

change_for_player(event.player_index, player_items_crafted_name, item_stack.count)
end

local function player_console_chat(event)
Expand Down
89 changes: 89 additions & 0 deletions map_gen/maps/danger_ores/modules/container_dump.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
local Event = require 'utils.event'
local Global = require 'utils.global'
local math = require 'utils.math'

local floor = math.floor
local max = math.max

local chest = defines.inventory.chest

local died_entities = {}

Global.register(
died_entities,
function(tbl)
died_entities = tbl
end
)

return function(config)
local entity_name = config.entity_name
Event.add(
defines.events.on_entity_died,
function(event)
local entity = event.entity

if not entity.valid then
return
end

local type = entity.type
if type ~= 'container' and type ~= 'logistic-container' then
return
end

local inventory = entity.get_inventory(chest)
if not inventory or not inventory.valid then
return
end

local count = inventory.get_item_count()
if count == 0 then
return
end

local area = entity.bounding_box
local left_top, right_bottom = area.left_top, area.right_bottom
local x1, y1 = floor(left_top.x), floor(left_top.y)
local x2, y2 = floor(right_bottom.x), floor(right_bottom.y)

local size_x = x2 - x1 + 1
local size_y = y2 - y1 + 1
local amount = floor(count / (size_x * size_y))
amount = max(amount, 1)

local create_entity = entity.surface.create_entity

for x = x1, x2 do
for y = y1, y2 do
create_entity({name = entity_name, position = {x, y}, amount = amount})
end
end

died_entities[entity.unit_number] = true
end
)

Event.add(
defines.events.on_post_entity_died,
function(event)
local unit_number = event.unit_number
if not unit_number then
return
end

if not died_entities[unit_number] then
return
end

died_entities[unit_number] = nil

local ghost = event.ghost
if not ghost or not ghost.valid then
return
end

ghost.destroy()
end
)
end
3 changes: 3 additions & 0 deletions map_gen/maps/danger_ores/presets/danger_bobangels_ores.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ rocket_launched(
shared_globals
)

local container_dump = require 'map_gen.maps.danger_ores.modules.container_dump'
container_dump({entity_name = 'coal'})

local config = {
spawn_shape = b.circle(80),
start_ore_shape = b.circle(86),
Expand Down
3 changes: 3 additions & 0 deletions map_gen/maps/danger_ores/presets/danger_bobs_ores.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ rocket_launched(
shared_globals
)

local container_dump = require 'map_gen.maps.danger_ores.modules.container_dump'
container_dump({entity_name = 'coal'})

local config = {
spawn_shape = b.circle(80),
start_ore_shape = b.circle(86),
Expand Down
3 changes: 3 additions & 0 deletions map_gen/maps/danger_ores/presets/danger_ores.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ Event.on_init(
end
)

local container_dump = require 'map_gen.maps.danger_ores.modules.container_dump'
container_dump({entity_name = 'coal'})

local config = {
spawn_shape = b.circle(64),
start_ore_shape = b.circle(68),
Expand Down
6 changes: 6 additions & 0 deletions map_gen/maps/danger_ores/presets/terraforming_danger_ore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ ScenarioInfo.set_new_info(
2019-03-27:
- Ore arranged into quadrants to allow for more controlled resource gathering.
2020-09-02
- Destroyed chests dump their content as coal ore.
]]
)

Expand Down Expand Up @@ -129,6 +132,9 @@ rocket_launched(
shared_globals
)

local container_dump = require 'map_gen.maps.danger_ores.modules.container_dump'
container_dump({entity_name = 'coal'})

local config = {
spawn_shape = b.circle(64),
start_ore_shape = b.circle(68),
Expand Down

0 comments on commit 7be4c70

Please sign in to comment.