1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-30 04:30:58 +02:00

added support for hidden tiles

This commit is contained in:
grilledham 2018-11-11 15:08:35 +00:00
parent bac8b0bc87
commit 7fa2173e70
3 changed files with 154 additions and 14 deletions

View File

@ -1,4 +1,4 @@
local math = require "utils.math"
local math = require 'utils.math'
-- helpers
local inv_pi = 1 / math.pi
@ -1267,17 +1267,80 @@ function Builders.change_tile(shape, old_tile, new_tile)
end
end
local path_tiles = {
['concrete'] = true,
['hazard-concrete-left'] = true,
['hazard-concrete-right'] = true,
['stone-path'] = true,
['refined-concrete'] = true,
['refined-hazard-concrete-left'] = true,
['refined-hazard-concrete-right'] = true
}
function Builders.set_hidden_tile(shape, hidden_tile)
return function(x, y, world)
local tile = shape(x, y, world)
if type(tile) == 'table' and path_tiles[tile.tile] then
tile.hidden_tile = hidden_tile
elseif path_tiles[tile] then
tile = {tile = tile, hidden_tile = hidden_tile}
end
return tile
end
end
local collision_map = {
['concrete'] = 'ground-tile',
['deepwater-green'] = 'water-tile',
['deepwater'] = 'water-tile',
['dirt-1'] = 'ground-tile',
['dirt-2'] = 'ground-tile',
['dirt-3'] = 'ground-tile',
['dirt-4'] = 'ground-tile',
['dirt-5'] = 'ground-tile',
['dirt-6'] = 'ground-tile',
['dirt-7'] = 'ground-tile',
['dry-dirt'] = 'ground-tile',
['grass-1'] = 'ground-tile',
['grass-2'] = 'ground-tile',
['grass-3'] = 'ground-tile',
['grass-4'] = 'ground-tile',
['hazard-concrete-left'] = 'ground-tile',
['hazard-concrete-right'] = 'ground-tile',
['lab-dark-1'] = 'ground-tile',
['lab-dark-2'] = 'ground-tile',
['lab-white'] = 'ground-tile',
['out-of-map'] = false,
['red-desert-0'] = 'ground-tile',
['red-desert-1'] = 'ground-tile',
['red-desert-2'] = 'ground-tile',
['red-desert-3'] = 'ground-tile',
['sand-1'] = 'ground-tile',
['sand-2'] = 'ground-tile',
['sand-3'] = 'ground-tile',
['stone-path'] = 'ground-tile',
['water-green'] = 'water-tile',
['water'] = 'water-tile',
['refined-concrete'] = 'ground-tile',
['refined-hazard-concrete-left'] = 'ground-tile',
['refined-hazard-concrete-right'] = 'ground-tile'
}
function Builders.change_collision_tile(shape, collides, new_tile)
return function(x, y, world)
local tile = shape(x, y, world)
if type(tile) == 'table' then
if tile.tile.collides_with(collides) then
if collision_map[tile.tile] == collides then
tile.tile = new_tile
return tile
end
else
if tile.collides_with(collides) then
if collision_map[tile] == collides then
return new_tile
end
end
@ -1311,6 +1374,27 @@ function Builders.change_map_gen_tile(shape, old_tile, new_tile)
end
end
function Builders.change_map_gen_hidden_tile(shape, old_tile, hidden_tile)
return function(x, y, world)
local function is_collides()
local gen_tile = world.surface.get_tile(world.x, world.y)
return gen_tile.name == old_tile
end
local tile = shape(x, y, world)
if type(tile) == 'table' then
if path_tiles[tile.tile] and is_collides() then
tile.hidden_tile = hidden_tile
end
elseif path_tiles[tile] and is_collides() then
tile = {tile = tile, hidden_tile = hidden_tile}
end
return tile
end
end
-- only changes tiles made by the factorio map generator.
function Builders.change_map_gen_collision_tile(shape, collides, new_tile)
return function(x, y, world)
@ -1336,6 +1420,27 @@ function Builders.change_map_gen_collision_tile(shape, collides, new_tile)
end
end
function Builders.change_map_gen_collision_hidden_tile(shape, collides, hidden_tile)
return function(x, y, world)
local function is_collides()
local gen_tile = world.surface.get_tile(world.x, world.y)
return gen_tile.collides_with(collides)
end
local tile = shape(x, y, world)
if type(tile) == 'table' then
if path_tiles[tile.tile] and is_collides() then
tile.hidden_tile = hidden_tile
end
elseif path_tiles[tile] and is_collides() then
tile = {tile = tile, hidden_tile = hidden_tile}
end
return tile
end
end
local bad_tiles = {
['out-of-map'] = true,
['water'] = true,

View File

@ -2,6 +2,8 @@ local Task = require 'utils.Task'
local Token = require 'utils.global_token'
local Event = require 'utils.event'
local insert = table.insert
local tiles_per_tick
local regen_decoratives
local surfaces
@ -11,9 +13,9 @@ local total_calls
local function do_tile(y, x, data, shape)
local function do_tile_inner(tile, pos)
if not tile then
table.insert(data.tiles, {name = 'out-of-map', position = pos})
insert(data.tiles, {name = 'out-of-map', position = pos})
elseif type(tile) == 'string' then
table.insert(data.tiles, {name = tile, position = pos})
insert(data.tiles, {name = tile, position = pos})
end
end
@ -25,20 +27,25 @@ local function do_tile(y, x, data, shape)
if type(tile) == 'table' then
do_tile_inner(tile.tile, pos)
local hidden_tile = tile.hidden_tile
if hidden_tile then
insert(data.hidden_tiles, {tile = hidden_tile, position = pos})
end
local entities = tile.entities
if entities then
for _, entity in ipairs(entities) do
if not entity.position then
entity.position = pos
end
table.insert(data.entities, entity)
insert(data.entities, entity)
end
end
local decoratives = tile.decoratives
if decoratives then
for _, decorative in ipairs(decoratives) do
table.insert(data.decoratives, decorative)
insert(data.decoratives, decorative)
end
end
else
@ -50,6 +57,13 @@ local function do_place_tiles(data)
data.surface.set_tiles(data.tiles, true)
end
local function do_place_hidden_tiles(data)
local surface = data.surface
for _, t in ipairs(data.hidden_tiles) do
surface.set_hidden_tile(t.position, t.tile)
end
end
local decoratives = {
'brown-asterisk',
'brown-carpet-grass',
@ -149,14 +163,18 @@ local function map_gen_action(data)
data.y = 33
return true
elseif state == 33 then
do_place_entities(data)
do_place_hidden_tiles(data)
data.y = 34
return true
elseif state == 34 then
do_place_decoratives(data)
do_place_entities(data)
data.y = 35
return true
elseif state == 35 then
do_place_decoratives(data)
data.y = 36
return true
elseif state == 36 then
run_chart_update(data)
return false
end
@ -182,6 +200,7 @@ local function on_chunk(event)
top_y = area.left_top.y,
surface = surface,
tiles = {},
hidden_tiles = {},
entities = {},
decoratives = {}
}
@ -194,7 +213,7 @@ local function init(args)
regen_decoratives = args.regen_decoratives or false
surfaces = args.surfaces or {}
total_calls = math.ceil(1024 / tiles_per_tick) + 4
total_calls = math.ceil(1024 / tiles_per_tick) + 5
Event.add(defines.events.on_chunk_generated, on_chunk)
end

View File

@ -1,15 +1,17 @@
local Token = require 'utils.global_token'
local Event = require 'utils.event'
local insert = table.insert
local regen_decoratives
local surfaces
local function do_row(row, data, shape)
local function do_tile(tile, pos)
if not tile then
table.insert(data.tiles, {name = 'out-of-map', position = pos})
insert(data.tiles, {name = 'out-of-map', position = pos})
elseif type(tile) == 'string' then
table.insert(data.tiles, {name = tile, position = pos})
insert(data.tiles, {name = tile, position = pos})
end
end
@ -28,20 +30,25 @@ local function do_row(row, data, shape)
if type(tile) == 'table' then
do_tile(tile.tile, pos)
local hidden_tile = tile.hidden_tile
if hidden_tile then
insert(data.hidden_tiles, {tile = hidden_tile, position = pos})
end
local entities = tile.entities
if entities then
for _, entity in ipairs(entities) do
if not entity.position then
entity.position = pos
end
table.insert(data.entities, entity)
insert(data.entities, entity)
end
end
local decoratives = tile.decoratives
if decoratives then
for _, decorative in ipairs(decoratives) do
table.insert(data.decoratives, decorative)
insert(data.decoratives, decorative)
end
end
else
@ -54,6 +61,13 @@ local function do_place_tiles(data)
data.surface.set_tiles(data.tiles, true)
end
local function do_place_hidden_tiles(data)
local surface = data.surface
for _, t in ipairs(data.hidden_tiles) do
surface.set_hidden_tile(t.position, t.tile)
end
end
local decoratives = {
'brown-asterisk',
'brown-carpet-grass',
@ -112,6 +126,7 @@ local function on_chunk(event)
top_y = area.left_top.y,
surface = surface,
tiles = {},
hidden_tiles = {},
entities = {},
decoratives = {}
}
@ -121,6 +136,7 @@ local function on_chunk(event)
end
do_place_tiles(data)
do_place_hidden_tiles(data)
do_place_entities(data)
do_place_decoratives(data)
end