-- This file is part of thesixthroc's Pirate Ship softmod, licensed under GPLv3 and stored at https://github.com/ComfyFactory/ComfyFactorio and https://github.com/danielmartin0/ComfyFactorio-Pirates.

local Memory = require('maps.pirates.memory')
local Math = require('maps.pirates.math')
-- local Balance = require 'maps.pirates.balance'
local Boats = require('maps.pirates.structures.boats.boats')
local Common = require('maps.pirates.common')
local CoreData = require('maps.pirates.coredata')
local Utils = require('maps.pirates.utils_local')
local _inspect = require('utils.inspect').inspect

local Public = {}

Public.StartingBoats = {
	{
		type = Boats.enum.SLOOP,
		position = { x = Boats[Boats.enum.SLOOP].Data.width - 65, y = -1 },
		surface_name = CoreData.lobby_surface_name,
		force_name = 'crew-001',
	},
	{
		type = Boats.enum.SLOOP,
		position = {
			x = Boats[Boats.enum.SLOOP].Data.width - 65,
			y = -1 + (23 + Boats[Boats.enum.SLOOP].Data.height / 2),
		},
		surface_name = CoreData.lobby_surface_name,
		force_name = 'crew-002',
	},
	{
		type = Boats.enum.SLOOP,
		position = {
			x = Boats[Boats.enum.SLOOP].Data.width - 65,
			y = -1 - (23 + Boats[Boats.enum.SLOOP].Data.height / 2),
		},
		surface_name = CoreData.lobby_surface_name,
		force_name = 'crew-003',
	},
	{
		type = Boats.enum.SLOOP,
		position = {
			x = Boats[Boats.enum.SLOOP].Data.width - 65,
			y = -1 + 2 * (23 + Boats[Boats.enum.SLOOP].Data.height / 2),
		},
		surface_name = CoreData.lobby_surface_name,
		force_name = 'crew-004',
	},
	{
		type = Boats.enum.SLOOP,
		position = {
			x = Boats[Boats.enum.SLOOP].Data.width - 65,
			y = -1 - 2 * (23 + Boats[Boats.enum.SLOOP].Data.height / 2),
		},
		surface_name = CoreData.lobby_surface_name,
		force_name = 'crew-005',
	},
	{
		type = Boats.enum.SLOOP,
		position = {
			x = Boats[Boats.enum.SLOOP].Data.width - 65,
			y = -1 + 3 * (23 + Boats[Boats.enum.SLOOP].Data.height / 2),
		},
		surface_name = CoreData.lobby_surface_name,
		force_name = 'crew-006',
	},
	{
		type = Boats.enum.SLOOP,
		position = {
			x = Boats[Boats.enum.SLOOP].Data.width - 65,
			y = -1 - 3 * (23 + Boats[Boats.enum.SLOOP].Data.height / 2),
		},
		surface_name = CoreData.lobby_surface_name,
		force_name = 'crew-007',
	},
	{
		type = Boats.enum.SLOOP,
		position = {
			x = Boats[Boats.enum.SLOOP].Data.width - 65,
			y = -1 + 4 * (23 + Boats[Boats.enum.SLOOP].Data.height / 2),
		},
		surface_name = CoreData.lobby_surface_name,
		force_name = 'crew-008',
	},
	{
		type = Boats.enum.SLOOP,
		position = {
			x = Boats[Boats.enum.SLOOP].Data.width - 65,
			y = -1 - 4 * (23 + Boats[Boats.enum.SLOOP].Data.height / 2),
		},
		surface_name = CoreData.lobby_surface_name,
		force_name = 'crew-009',
	},
	-- {
	-- 	type = Boats.enum.CUTTER,
	-- 	position = {x = Boats[Boats.enum.CUTTER].Data.width - 56, y = (70.5 + Boats[Boats.enum.CUTTER].Data.height/2)},
	-- 	surface_name = CoreData.lobby_surface_name,
	-- 	force_name = 'environment',
	-- 	speedticker1 = 0,
	-- 	speedticker2 = 1/3 * Common.boat_steps_at_a_time,
	-- 	speedticker3 = 2/3 * Common.boat_steps_at_a_time,
	-- },
}

Public.Data = {}
Public.Data.display_name = { 'pirates.location_displayname_lobby_1' }
Public.Data.width = 224
Public.Data.height = 384
-- Public.Data.noiseparams = {
-- 	land = {
-- 		type = 'simplex_2d',
-- 		normalised = false,
-- 		params = {
-- 			{wavelength = 128, amplitude = 10/100},
-- 			{wavelength = 64, amplitude = 10/100},
-- 			{wavelength = 32, amplitude = 5/100},
-- 			{wavelength = 12, amplitude = 5/100},
-- 		},
-- 	}
-- }

Public.Data.iconized_map_width = 4
Public.Data.iconized_map_height = 20

function Public.terrain(args)
	local x, y = args.p.x, args.p.y

	if Math.distance(args.p, { x = -316, y = 0 }) < 230 then
		args.tiles[#args.tiles + 1] = { name = 'dirt-3', position = args.p }
		if x <= -80 and (y >= 10 or y <= -10) and math.random() < 0.1 then
			local tree_name = 'tree-05'
			if math.random() < 0.2 then
				tree_name = 'tree-07'
			end
			args.entities[#args.entities + 1] = { name = tree_name, position = args.p }
		elseif x <= -80 and y < 3 and y > -5 then
			args.tiles[#args.tiles + 1] = { name = 'stone-path', position = args.p }
		end
	elseif Math.distance(args.p, { x = -264, y = 0 }) < 180 then
		args.tiles[#args.tiles + 1] = { name = 'water-shallow', position = args.p }
	elseif Math.abs(Common.lobby_spawnpoint.x - x) < 3 and Math.abs(Common.lobby_spawnpoint.y - y) < 3 then
		args.tiles[#args.tiles + 1] = { name = CoreData.walkway_tile, position = args.p }
	else
		args.tiles[#args.tiles + 1] = { name = 'water', position = args.p }
		if math.random(1, 300) == 1 then
			args.entities[#args.entities + 1] = { name = 'fish', position = args.p }
		end
	end
end

function Public.chunk_structures()
	return nil
end

function Public.create_starting_dock_surface()
	-- local memory = Memory.get_crew_memory()

	local starting_dock_name = CoreData.lobby_surface_name

	local width = Public.Data.width
	local height = Public.Data.height
	local map_gen_settings = Common.default_map_gen_settings(width, height)

	local surface = game.create_surface(starting_dock_name, map_gen_settings)
	surface.freeze_daytime = true
	surface.daytime = 0
end

function Public.place_starting_dock_showboat(id)
	local global_memory = Memory.get_global_memory()

	Memory.set_working_id(id)

	local boat = Utils.deepcopy(Public.StartingBoats[id])

	global_memory.lobby_boats[id] = boat

	Boats.place_boat(boat, CoreData.static_boat_floor, true, true, false)
	-- Boats.place_boat(boat, CoreData.static_boat_floor, true, true, false, true) --this final argument is to enable the comfy graphic to be placed on the boat, but that argument is disabled for now, so luacheck forbids this line
end

function Public.place_lobby_jetty_and_boats()
	local surface = game.surfaces[CoreData.lobby_surface_name]

	for id, _ in pairs(Public.StartingBoats) do
		Public.place_starting_dock_showboat(id)
	end

	local offset = { x = 38, y = 127 }

	local tiles = {}
	for _, p in pairs(Common.tile_positions_from_blueprint(Public.Data.jetty_bp, offset)) do
		tiles[#tiles + 1] = { name = CoreData.walkway_tile, position = p, force = 'environment' }
	end

	surface.set_tiles(tiles, true)

	-- for y = -3.5, 3.5 do
	-- 	local e = surface.create_entity{name = 'stone-wall', position = {x = -68.5, y = y}, force = 'environment'}
	-- 	e.destructible = false
	-- 	e.minable = false
	-- 	e.rotatable = false
	-- 	e.operable = false
	-- end

	local entities =
		Common.build_from_blueprint(Public.Data.jetty_objects_bp, surface, { x = -61, y = -1 }, 'environment')
	for _, e in pairs(entities) do
		if e and e.valid then
			e.destructible = false
			e.minable = false
			e.rotatable = false
		end
	end
end

Public.Data.jetty_bp =
	[[[encoded data, 25301 characters]]]

Public.Data.jetty_objects_bp =
	[[0eNqlnPlu2zgQh99FfzML8SbzKkUQKKm2NeoLttNuUeTdVzx8LNY/UzNCgAxpSx9J8ZjhDOU/3dv6Y9wfVttT9/ynW73vtsfu+cuf7rj6th3W6bPtsBm75+7Xbvd13D69fx+Pp+5TdKvt1/Gf7ll+vohu3J5Wp9VY7syZ36/bj83beJguEHcJotvvjtNNu20qYwI9efeXFd3vKSXVlPz8FP9jKQ7L3mfpC2szfl19bJ7G9fh+Oqzen/a79XiPqa5Mc59pOPXT91mWWj9nmkx3YR43w3r9tB42+3sk3SR5au2sbTIDlWnaTzHOa7Fp10721Opp34ZKKlSFNlTNa7SaUT/yLJGxDTVkaN+G2nmNnlE9x5jFqdl3YZ4DA0uWDLNhtg2LCxZABFX9vF7wso3iKA4J+lQpxpPrA4BpRs16D2AcndEj/WgX9CmEOs6zQx3BmQ+9BLDAb25E/RGpIxiRdE9vauwBizEZInhqWvGfWgBTQmu+tQKZhmqtQJLlWyuQ6fjWCmR6qrUCSYFvrEBm5NsqiGl6qqkCSZJvqUCm4hsqkKmJdgoEmQXTGCxZZoH+8KiejrqgQhJDd3jUUoYp5YCNZxZYUohpGcrDAUvKMpSHA8rDLlAeDig3y7CnLGoreVa4KxPs6u3MrYWzTRJja2EVYDFmg0W+BrK6sKHJnLv1bpIceedtYpNJVhembzJn7rt1m0Q2p7RsMskzQ6kmc+bMkG0S2ZySuslkzBADdI+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