2019-02-02 02:26:11 +02:00
|
|
|
-- This module prevents all but the allowed items from being built on top of resources
|
|
|
|
local RestrictEntities = require 'map_gen.shared.entity_placement_restriction'
|
2018-08-07 01:14:54 +02:00
|
|
|
local Event = require 'utils.event'
|
|
|
|
|
2019-02-02 02:26:11 +02:00
|
|
|
--- Items explicitly allowed on ores
|
|
|
|
RestrictEntities.add_allowed(
|
|
|
|
{
|
|
|
|
'transport-belt',
|
|
|
|
'fast-transport-belt',
|
|
|
|
'express-transport-belt',
|
|
|
|
'underground-belt',
|
|
|
|
'fast-underground-belt',
|
|
|
|
'express-underground-belt',
|
|
|
|
'small-electric-pole',
|
|
|
|
'medium-electric-pole',
|
|
|
|
'big-electric-pole',
|
|
|
|
'substation',
|
|
|
|
'electric-mining-drill',
|
|
|
|
'burner-mining-drill',
|
|
|
|
'pumpjack'
|
|
|
|
}
|
|
|
|
)
|
2018-08-07 01:14:54 +02:00
|
|
|
|
2019-02-02 02:26:11 +02:00
|
|
|
--- The logic for checking that there are resources under the entity's position
|
2019-02-02 20:36:25 +02:00
|
|
|
RestrictEntities.set_keep_alive_callback(
|
|
|
|
function(entity)
|
|
|
|
-- Some entities have a bounding_box area of zero, eg robots.
|
|
|
|
local area = entity.bounding_box
|
|
|
|
local left_top, right_bottom = area.left_top, area.right_bottom
|
|
|
|
if left_top.x == right_bottom.x and left_top.y == right_bottom.y then
|
2019-02-19 19:43:53 +02:00
|
|
|
return true
|
2019-02-02 20:36:25 +02:00
|
|
|
end
|
|
|
|
local count = entity.surface.count_entities_filtered {area = area, type = 'resource', limit = 1}
|
2018-08-07 01:14:54 +02:00
|
|
|
if count == 0 then
|
2019-02-02 02:26:11 +02:00
|
|
|
return true
|
2018-08-07 01:14:54 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
2019-02-02 02:26:11 +02:00
|
|
|
|
|
|
|
--- Warning for players when their entities are destroyed
|
|
|
|
local function on_destroy(event)
|
|
|
|
local p = event.player
|
|
|
|
if p and p.valid then
|
2019-02-02 20:36:25 +02:00
|
|
|
p.print('You cannot build that on top of ores, only belts, mining drills, and power poles are allowed.')
|
2019-02-02 02:26:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Event.add(RestrictEntities.events.on_restricted_entity_destroyed, on_destroy)
|