1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-02-09 13:37:05 +02:00

Fixed upvalues

This commit is contained in:
SimonFlapse 2019-10-05 17:00:36 +02:00
parent 9b6a9784ba
commit 712d6bd006
3 changed files with 72 additions and 46 deletions

View File

@ -2,6 +2,8 @@ local floor = math.floor
local RestrictEntities = require 'map_gen.shared.entity_placement_restriction'
local Event = require 'utils.event'
local b = require 'map_gen.shared.builders'
local Token = require 'utils.token'
local Color = require 'resources.color_presets'
local redmew_config = global.config
@ -93,7 +95,7 @@ local entity_tiers = {
}
--Creates rich text icons of the tiered entities
local tier_2_items = ""
local tier_2_items = ''
local tier_3_items = ''
local tier_2_counter = 0
@ -108,7 +110,6 @@ for k, v in pairs(entity_tiers) do
tier_2_counter = tier_2_counter + 1
end
if tier_3_counter > 14 then
tier_3_counter = 0
tier_3_items = tier_3_items .. '\n'
@ -118,7 +119,7 @@ for k, v in pairs(entity_tiers) do
end
end
local tile_tiers_entities = "You may only build the factory on:\n"
local tile_tiers_entities = 'You may only build the factory on:\n'
local tile_tiers_entities_counter = 0
for k, _ in pairs(tile_tiers) do
@ -131,8 +132,8 @@ for k, _ in pairs(tile_tiers) do
end
ScenarioInfo.add_map_extra_info(
tile_tiers_entities ..
[[
tile_tiers_entities ..
[[
Exceptions:
@ -144,7 +145,7 @@ but some require better ground support!
Ground support minimum concrete:
]] ..
tier_2_items .. [[
tier_2_items .. [[
Ground support minimum refined concrete:
]] .. tier_3_items
@ -152,44 +153,62 @@ Ground support minimum refined concrete:
--- The logic for checking that there are the correct ground support under the entity's position
RestrictEntities.set_keep_alive_callback(
function(entity)
local get_tile = entity.surface.get_tile
local area = entity.bounding_box
local left_top = area.left_top
local right_bottom = area.right_bottom
local name = entity.name
Token.register(
function(entity)
local get_tile = entity.surface.get_tile
local area = entity.bounding_box
local left_top = area.left_top
local right_bottom = area.right_bottom
local name = entity.name
if name == 'entity-ghost' then
name = entity.ghost_name
end
if name == 'entity-ghost' then
name = entity.ghost_name
end
for x = floor(left_top.x), floor(right_bottom.x) do
for y = floor(left_top.y), floor(right_bottom.y) do
local tile_name = get_tile(x, y).name
local tier = tile_tiers[tile_name]
for x = floor(left_top.x), floor(right_bottom.x) do
for y = floor(left_top.y), floor(right_bottom.y) do
local tile_name = get_tile(x, y).name
local tier = tile_tiers[tile_name]
if not((entity_tiers[name] or 1) <= (tier or 0)) then
return false
if not ((entity_tiers[name] or 1) <= (tier or 0)) then
return false
end
end
end
return true
end
return true
end
)
)
local function print_floating_text(player, entity, text, color)
color = color or Color.white
local surface = player.surface
local position = entity.position
return surface.create_entity {
name = 'tutorial-flying-text',
color = color,
text = text,
position = position
}
end
--- Warning for players when their entities are destroyed (needs to be pre because of the stack)
local function on_destroy(event)
local p = game.get_player(event.player_index)
local name = event.stack.name
if p and p.valid then
if not (name == 'blueprint') then
local tier = 'stone path'
local entity = event.created_entity
local tier = '[tile=stone-path]'
if (entity_tiers[name] == 2) then
tier = 'concrete'
tier = '[tile=concrete]'
elseif (entity_tiers[name] == 3) then
tier = 'refined concrete'
tier = '[tile=refined-concrete]'
end
p.print('[color=yellow]This [/color][color=red]' .. name .. '[/color][color=yellow] cannot be placed here, it needs ground support of at least [/color][color=red]' .. tier .. '[/color]')
local text = 'Requires at least ' .. tier
--local text = '[color=yellow]This [/color][item=' .. name .. '][color=yellow] cannot be placed here, it needs ground support of at least [/color]' .. tier
print_floating_text(p, entity, text)
else
p.print('[color=yellow]Some parts of this [/color][color=red]blueprint[/color][color=yellow] cannot be placed here, they need better ground support![/color]')
end

View File

@ -1,6 +1,7 @@
-- This module prevents all but the allowed items from being built on top of resources
local RestrictEntities = require 'map_gen.shared.entity_placement_restriction'
local Event = require 'utils.event'
local Token = require 'utils.token'
--- Items explicitly allowed on ores
RestrictEntities.add_allowed(
@ -25,18 +26,20 @@ RestrictEntities.add_allowed(
--- The logic for checking that there are resources under the entity's position
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
return true
Token.register(
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
return true
end
local count = entity.surface.count_entities_filtered {area = area, type = 'resource', limit = 1}
if count == 0 then
return true
end
end
local count = entity.surface.count_entities_filtered {area = area, type = 'resource', limit = 1}
if count == 0 then
return true
end
end
)
)
--- Warning for players when their entities are destroyed

View File

@ -9,6 +9,8 @@
This means you can use any custom logic you want to determine whether an entity should be destroyed or not.
The callback function is supplied a valid LuaEntity as an argument.
A return of true indicates the entity should be kept alive, while false or nil indicate it should be destroyed.
This function must be a registered with the Token module and the keep_alive_callback function will take the Token-id as parameter
This is to prevent upvalue errors
Refunds for items that were placed can be toggled on or off via the enable and disable_refund functions
@ -24,16 +26,18 @@
-- The function provided does nothing but return nil
-- every entity will be destroyed except those on the allowed list
RestrictEntities.add_allowed({'transport-belt'})
RestrictEntities.set_keep_alive_callback(function() end)
RestrictEntities.set_keep_alive_callback(Token.register(function() end))
-- Danger ores (a lot of important code omitted for the sake of a brief example)
RestrictEntities.add_allowed({belts, power_poles, mining_drills, 'pumpjack'})
RestrictEntities.set_keep_alive_callback(
function(entity)
if entity.surface.count_entities_filtered {area = entity.bounding_box, type = 'resource', limit = 1} == 0 then
return true
Token.register(
function(entity)
if entity.surface.count_entities_filtered {area = entity.bounding_box, type = 'resource', limit = 1} == 0 then
return true
end
end
end
)
)
]]
local Event = require 'utils.event'
@ -132,7 +136,7 @@ local on_built_token =
-- destroy in these cases:
-- all banned ents
-- not banned and callback function and not saved by callback
if not banned_entities[name] and (not keep_alive_callback or keep_alive_callback(entity)) then
if not banned_entities[name] and (not keep_alive_callback or Token.get(keep_alive_callback)(entity)) then
return
end
@ -201,8 +205,8 @@ end
-- logic on what entities should and should not be destroyed.
-- @param keep_alive_callback <function>
function Public.set_keep_alive_callback(keep_alive_callback)
if type(keep_alive_callback) ~= 'function' then
error('Sending a non-function')
if type(keep_alive_callback) ~= 'number' then
error('Sending a non-token function')
end
primitives.keep_alive_callback = keep_alive_callback
check_event_status()