1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00

refactor rail_grid

This commit is contained in:
grilledham 2019-09-28 14:23:57 +01:00
parent e96cbfa680
commit 781cd11178
4 changed files with 165 additions and 148 deletions

View File

@ -4,151 +4,6 @@
-- - We recommend playing with RSO to force expansion by rail -- - We recommend playing with RSO to force expansion by rail
-- - If playing with mods, do not use FARL, the rail placing mechanic will not work with this map -- - If playing with mods, do not use FARL, the rail placing mechanic will not work with this map
-- Dependencies require 'map_gen.maps.rail_grid.scenario_setup'
local b = require 'map_gen.shared.builders' require 'map_gen.maps.rail_grid.rail_grid_restrictions'
local Random = require 'map_gen.shared.random' return require 'map_gen.maps.rail_grid.map'
local table = require 'utils.table'
local Event = require 'utils.event'
local RS = require 'map_gen.shared.redmew_surface'
local MGSP = require 'resources.map_gen_settings'
local MSP = require 'resources.map_settings'
local degrees = require "utils.math".degrees
local ScenarioInfo = require 'features.gui.info'
-- Setup surface and map settings
RS.set_map_gen_settings(
{
MGSP.cliff_none,
MGSP.grass_disabled,
MGSP.enable_water,
}
)
ScenarioInfo.set_map_name('Rail Grid')
ScenarioInfo.set_map_description(
[[
Nauvis' factory planners have been disappointed with the recent trend towards
rail spaghetti. As such they have enacted rules to enforce neat grid shaped
rails and crossings.
]]
)
ScenarioInfo.add_map_extra_info(
[[
This map has green "city blocks" to enforce construction of rail in a grid
pattern.
You cannot place rail on any tile type except landfill. There is space at the
grid intersections for junctions and turnarounds. There is space for
two stations on each side of the grid.
]]
)
RS.set_spawn_position({x = 20,y = 20})
local function is_not_water_tile(x, y, world)
local gen_tile = world.surface.get_tile(world.x, world.y)
return not gen_tile.collides_with('water-tile')
end
local station_length = 40
local station = b.any{
b.rectangle(station_length,18),
b.translate(b.square_diamond(18),station_length/2,0), -- these just make it pretty
b.translate(b.square_diamond(18),station_length/-2,0) -- these just make it pretty
}
local grid_size = 224
local path = b.any{
b.square_diamond(40),
b.rectangle(grid_size,6),
b.rectangle(6,grid_size),
b.circular_pattern(b.rotate(station,degrees(90)), 4, grid_size/3)
}
path = b.change_tile(path, true, 'landfill') -- MUST be landfill or the rail removal event doesn't work.
local grid = b.single_grid_pattern(path, grid_size, grid_size)
local no_water_grid = b.choose(is_not_water_tile, grid, b.full_shape)
local map = b.if_else(no_water_grid, b.full_shape)
map = b.translate(map,1,1)
-- replace grass tiles with dirt so that the rail grid is much
local tile_map ={
['grass-1'] = 'dirt-1',
['grass-2'] = 'dirt-2',
['grass-3'] = 'dirt-3',
['grass-4'] = 'dirt-4',
}
map = b.change_map_gen_tiles(map, tile_map)
-- This event removes rail and curve rail entities and removes them unless they are placed on landfill
Event.add(
defines.events.on_built_entity,
function(event)
local entity = event.created_entity
if not entity or not entity.valid then
return
end
local name = entity.name
local ghost = false
if name == 'tile-ghost' or name == 'entity-ghost' then
ghost = true
ghost_name = entity.ghost_name
end
if (name ~= 'straight-rail') and (name ~= 'curved-rail') then
if not ghost then
return
elseif (ghost_name ~= 'straight-rail') and (ghost_name ~= 'curved-rail') then
return
end
end
-- Check the bounding box for the tile
local status = true
local area = entity.bounding_box
local left_top = area.left_top
local right_bottom = area.right_bottom
local p = game.get_player(event.player_index)
--check for sand under all tiles in bounding box
for x = math.floor(left_top.x), math.floor(right_bottom.x), 1 do
for y = math.floor(left_top.y), math.floor(right_bottom.y), 1 do
if (p.surface.get_tile(x, y).name ~= 'landfill')then
status = false
break
end
end
end
if status == true then
return
else
--destroy entity and return to player
if not p or not p.valid then
return
end
entity.destroy()
if not ghost then
p.insert(event.stack)
end
end
end
)
-- On player join print a notice explaining the rail mechanic
local function player_joined_game(event)
local player_index = event.player_index
local player = game.get_player(player_index)
if not player or not player.valid then
return
end
player.print("Welcome to RedMew's Rail Grids Map. Rails can only be built on green tiles.", {r=0, g=1, b=0, a=1})
end
Event.add(defines.events.on_player_joined_game, player_joined_game)
return map

View File

@ -0,0 +1,47 @@
local b = require 'map_gen.shared.builders'
local rad = math.rad
-- x and y must be even numbers else rail grid is misaligned.
local spawn_position = {x = 20, y = 20}
local function is_not_water_tile(x, y, world)
local gen_tile = world.surface.get_tile(world.x, world.y)
return not gen_tile.collides_with('water-tile')
end
local station_length = 40
local station =
b.any {
b.rectangle(station_length, 18),
b.translate(b.square_diamond(18), station_length / 2, 0), -- these just make it pretty
b.translate(b.square_diamond(18), station_length / -2, 0) -- these just make it pretty
}
local grid_size = 224
local path =
b.any {
b.square_diamond(40),
b.rectangle(grid_size, 6),
b.rectangle(6, grid_size),
b.circular_pattern(b.rotate(station, rad(90)), 4, grid_size / 3)
}
path = b.change_tile(path, true, 'landfill') -- MUST be landfill or the rail removal event doesn't work.
local grid = b.single_grid_pattern(path, grid_size, grid_size)
local no_water_grid = b.choose(is_not_water_tile, grid, b.full_shape)
local map = b.if_else(no_water_grid, b.full_shape)
-- replace grass tiles with dirt so that the rail grid is much
local tile_map = {
['grass-1'] = 'dirt-1',
['grass-2'] = 'dirt-2',
['grass-3'] = 'dirt-3',
['grass-4'] = 'dirt-4'
}
map = b.change_map_gen_tiles(map, tile_map)
map = b.translate(map, 1 - spawn_position.x, 1 - spawn_position.y)
return map

View File

@ -0,0 +1,84 @@
local Event = require 'utils.event'
local Global = require 'utils.global'
local RestrictEntities = require 'map_gen.shared.entity_placement_restriction'
local Popup = require 'features.gui.popup'
local floor = math.floor
local players_popuped = {}
Global.register(
players_popuped,
function(tbl)
players_popuped = tbl
end
)
local rail_entities = {
['straight-rail'] = true,
['curved-rail'] = true
}
local function all_on_landfill(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
for x = floor(left_top.x), floor(right_bottom.x) do
for y = floor(left_top.y), floor(right_bottom.y) do
if get_tile(x, y).name ~= 'landfill' then
return false
end
end
end
return true
end
RestrictEntities.set_keep_alive_callback(
function(entity)
local name = entity.name
if name == 'entity-ghost' then
name = entity.ghost_name
end
if not rail_entities[name] then
return true
end
return all_on_landfill(entity)
end
)
-- On first time player places rail entity on invalid tile, show popup explaining the rail mechanic.
local function restricted_entity_destroyed(event)
local p = event.player
if not p or not p.valid then
return
end
if players_popuped[p.index] then
return
end
Popup.player(p, 'Rails can only be built on green tiles.', nil, nil, 'rail_grid')
players_popuped[p.index] = true
end
-- On player join print a notice explaining the rail mechanic
local function player_joined_game(event)
local player_index = event.player_index
local player = game.get_player(player_index)
if not player or not player.valid then
return
end
player.print(
"Welcome to RedMew's Rail Grids Map. Rails can only be built on green tiles.",
{r = 0, g = 1, b = 0, a = 1}
)
end
Event.add(RestrictEntities.events.on_restricted_entity_destroyed, restricted_entity_destroyed)
Event.add(defines.events.on_player_joined_game, player_joined_game)

View File

@ -0,0 +1,31 @@
local ScenarioInfo = require 'features.gui.info'
local RS = require 'map_gen.shared.redmew_surface'
local MGSP = require 'resources.map_gen_settings'
-- Setup surface and map settings
RS.set_map_gen_settings(
{
MGSP.cliff_none,
MGSP.grass_disabled,
MGSP.enable_water
}
)
ScenarioInfo.set_map_name('Rail Grid')
ScenarioInfo.set_map_description(
[[
Nauvis' factory planners have been disappointed with the recent trend towards
rail spaghetti. As such they have enacted rules to enforce neat grid shaped
rails and crossings.
]]
)
ScenarioInfo.add_map_extra_info(
[[
This map has green "city blocks" to enforce construction of rail in a grid
pattern.
You cannot place rail on any tile type except landfill. There is space at the
grid intersections for junctions and turnarounds. There is space for
two stations on each side of the grid.
]]
)