1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-30 04:30:58 +02:00
This commit is contained in:
grilledham 2018-05-24 19:09:46 +01:00
commit 62ddc353a7
8 changed files with 851 additions and 1015 deletions

View File

@ -1,5 +1,6 @@
local Event = require 'utils.event' local Event = require 'utils.event'
local Task = require 'utils.Task' local Task = require 'utils.Task'
local Token = require 'utils.global_token'
local function on_init() local function on_init()
global.corpse_util_corpses = {} global.corpse_util_corpses = {}
@ -68,11 +69,14 @@ local function corpse_expired(event)
end end
end end
function corpse_util_mined_entity(data) local corpse_util_mined_entity =
if not data.entity.valid then Token.register(
remove_tag(data.position) function(data)
if not data.entity.valid then
remove_tag(data.position)
end
end end
end )
local function mined_entity(event) local function mined_entity(event)
local entity = event.entity local entity = event.entity
@ -80,7 +84,7 @@ local function mined_entity(event)
if entity and entity.valid and entity.name == 'character-corpse' then if entity and entity.valid and entity.name == 'character-corpse' then
-- The corpse may be mined but not removed (if player doesn't have inventory space) -- The corpse may be mined but not removed (if player doesn't have inventory space)
-- so we wait one tick to see if the corpse is gone. -- so we wait one tick to see if the corpse is gone.
Task.set_timeout_in_ticks(1, 'corpse_util_mined_entity', {entity = entity, position = entity.position}) Task.set_timeout_in_ticks(1, corpse_util_mined_entity, {entity = entity, position = entity.position})
end end
end end

View File

@ -1,16 +1,17 @@
local Task = require "utils.Task" local Task = require 'utils.Task'
local Event = require "utils.event" local Event = require 'utils.event'
local Token = require 'utils.global_token'
function player_print(str) function player_print(str)
if game.player then if game.player then
game.player.print(str) game.player.print(str)
else else
log(str) log(str)
end end
end end
function cant_run(name) function cant_run(name)
player_print("Can't run command (" .. name .. ") - insufficient permission.") player_print("Can't run command (" .. name .. ') - insufficient permission.')
end end
local function invoke(cmd) local function invoke(cmd)
@ -18,14 +19,14 @@ local function invoke(cmd)
cant_run(cmd.name) cant_run(cmd.name)
return return
end end
local target = cmd["parameter"] local target = cmd['parameter']
if target == nil or game.players[target] == nil then if target == nil or game.players[target] == nil then
player_print("Unknown player.") player_print('Unknown player.')
return return
end end
local pos = game.player.surface.find_non_colliding_position("player", game.player.position, 0, 1) local pos = game.player.surface.find_non_colliding_position('player', game.player.position, 0, 1)
game.players[target].teleport({pos.x, pos.y}, game.player.surface) game.players[target].teleport({pos.x, pos.y}, game.player.surface)
game.print(target .. ", get your ass over here!") game.print(target .. ', get your ass over here!')
end end
local function teleport_player(cmd) local function teleport_player(cmd)
@ -33,13 +34,13 @@ local function teleport_player(cmd)
cant_run(cmd.name) cant_run(cmd.name)
return return
end end
local target = cmd["parameter"] local target = cmd['parameter']
if target == nil or game.players[target] == nil then if target == nil or game.players[target] == nil then
player_print("Unknown player.") player_print('Unknown player.')
return return
end end
local surface = game.players[target].surface local surface = game.players[target].surface
local pos = surface.find_non_colliding_position("player", game.players[target].position, 0, 1) local pos = surface.find_non_colliding_position('player', game.players[target].position, 0, 1)
game.player.teleport(pos, surface) game.player.teleport(pos, surface)
game.print(target .. "! watcha doin'?!") game.print(target .. "! watcha doin'?!")
end end
@ -50,434 +51,541 @@ local function teleport_location(cmd)
return return
end end
if game.player.selected == nil then if game.player.selected == nil then
player_print("Nothing selected.") player_print('Nothing selected.')
return return
end end
local pos = game.player.surface.find_non_colliding_position("player", game.player.selected.position, 0, 1) local pos = game.player.surface.find_non_colliding_position('player', game.player.selected.position, 0, 1)
game.player.teleport(pos) game.player.teleport(pos)
end end
local function kill() local function kill()
if game.player and game.player.character then if game.player and game.player.character then
game.player.character.die() game.player.character.die()
end end
end end
global.walking = {} global.walking = {}
local custom_commands_return_player =
Token.register(
function(args)
global.walking[args.player.name:lower()] = false
args.player.character.destroy()
local character = args.player.surface.find_entity('player', args.position)
if character ~= nil and character.valid then
args.player.character = character
else
args.player.create_character()
end
args.player.force = args.force
args.player.teleport(args.position)
game.print(args.player.name .. ' came back from his walkabout.')
end
)
local function walkabout(cmd) local function walkabout(cmd)
if not ((not game.player) or game.player.admin or is_mod(game.player.name)) then if not ((not game.player) or game.player.admin or is_mod(game.player.name)) then
cant_run(cmd.name) cant_run(cmd.name)
return return
end end
local params = {} local params = {}
if cmd.parameter == nil then if cmd.parameter == nil then
player_print("Walkabout failed.") player_print('Walkabout failed.')
return return
end end
for param in string.gmatch(cmd.parameter, "%S+") do table.insert(params, param) end for param in string.gmatch(cmd.parameter, '%S+') do
local player_name = params[1] table.insert(params, param)
local duration = 60 end
if #params > 2 then local player_name = params[1]
player_print("Walkabout failed, check /help walkabout.") local duration = 60
return if #params > 2 then
elseif #params == 2 and tonumber(params[2]) == nil then player_print('Walkabout failed, check /help walkabout.')
player_print(params[2] .. " is not a number.") return
return elseif #params == 2 and tonumber(params[2]) == nil then
elseif #params == 2 and tonumber(params[2]) then player_print(params[2] .. ' is not a number.')
duration = tonumber(params[2]) return
end elseif #params == 2 and tonumber(params[2]) then
if duration < 15 then duration = 15 end duration = tonumber(params[2])
end
if duration < 15 then
duration = 15
end
local player = game.players[player_name] local player = game.players[player_name]
if type(player) ~= "table" or global.walking[player_name:lower()] then if type(player) ~= 'table' or global.walking[player_name:lower()] then
player_print(player_name .. " could not go on a walkabout.") player_print(player_name .. ' could not go on a walkabout.')
return return
end end
local chunks = {} local chunks = {}
for chunk in player.surface.get_chunks() do for chunk in player.surface.get_chunks() do
table.insert(chunks, chunk) table.insert(chunks, chunk)
end end
local chunk = chunks[math.random(#chunks)] local chunk = chunks[math.random(#chunks)]
if not chunk then if not chunk then
return return
end end
local pos = {x=chunk.x * 32, y=chunk.y * 32} local pos = {x = chunk.x * 32, y = chunk.y * 32}
local non_colliding_pos = player.surface.find_non_colliding_position("player", pos, 100, 1) local non_colliding_pos = player.surface.find_non_colliding_position('player', pos, 100, 1)
if non_colliding_pos then if non_colliding_pos then
game.print(player_name .. " went on a walkabout, to find himself.") game.print(player_name .. ' went on a walkabout, to find himself.')
Task.set_timeout(duration, "custom_commands_return_player", {player = player, force = player.force, position = {x = player.position.x, y = player.position.y}}) Task.set_timeout(
player.character = nil duration,
player.create_character() custom_commands_return_player,
player.teleport(non_colliding_pos) {player = player, force = player.force, position = {x = player.position.x, y = player.position.y}}
player.force = "enemy" )
global.walking[player_name:lower()] = true player.character = nil
else player.create_character()
player_print("Walkabout failed: count find non colliding position") player.teleport(non_colliding_pos)
end player.force = 'enemy'
end global.walking[player_name:lower()] = true
else
function custom_commands_return_player(args) player_print('Walkabout failed: count find non colliding position')
global.walking[args.player.name:lower()] = false end
args.player.character.destroy()
local character = args.player.surface.find_entity('player', args.position)
if character ~= nil and character.valid then
args.player.character = character
else
args.player.create_character()
end
args.player.force = args.force
args.player.teleport(args.position)
game.print(args.player.name .. " came back from his walkabout.")
end end
local function regular(cmd) local function regular(cmd)
if not ((not game.player) or game.player.admin or is_mod(game.player.name)) then if not ((not game.player) or game.player.admin or is_mod(game.player.name)) then
cant_run(cmd.name) cant_run(cmd.name)
return return
end end
if cmd.parameter == nil then if cmd.parameter == nil then
player_print("Command failed. Usage: /regular <promote, demote>, <player>") player_print('Command failed. Usage: /regular <promote, demote>, <player>')
return return
end end
local params = {} local params = {}
for param in string.gmatch(cmd.parameter, "%S+") do table.insert(params, param) end for param in string.gmatch(cmd.parameter, '%S+') do
if params[2] == nil then table.insert(params, param)
player_print("Command failed. Usage: /regular <promote, demote>, <player>") end
return if params[2] == nil then
elseif (params[1] == "promote") then player_print('Command failed. Usage: /regular <promote, demote>, <player>')
add_regular(params[2]) return
elseif (params[1] == "demote") then elseif (params[1] == 'promote') then
remove_regular(params[2]) add_regular(params[2])
else elseif (params[1] == 'demote') then
player_print("Command failed. Usage: /regular <promote, demote>, <player>") remove_regular(params[2])
end else
player_print('Command failed. Usage: /regular <promote, demote>, <player>')
end
end end
local function mod(cmd) local function mod(cmd)
if game.player and not game.player.admin then if game.player and not game.player.admin then
cant_run(cmd.name) cant_run(cmd.name)
return return
end end
if cmd.parameter == nil then if cmd.parameter == nil then
player_print("Command failed. Usage: /mod <promote, demote>, <player>") player_print('Command failed. Usage: /mod <promote, demote>, <player>')
return return
end end
local params = {} local params = {}
for param in string.gmatch(cmd.parameter, "%S+") do table.insert(params, param) end for param in string.gmatch(cmd.parameter, '%S+') do
if params[2] == nil then table.insert(params, param)
player_print("Command failed. Usage: /mod <promote, demote>, <player>") end
return if params[2] == nil then
elseif (params[1] == "promote") then player_print('Command failed. Usage: /mod <promote, demote>, <player>')
add_mod(params[2]) return
elseif (params[1] == "demote") then elseif (params[1] == 'promote') then
remove_mod(params[2]) add_mod(params[2])
else elseif (params[1] == 'demote') then
player_print("Command failed. Usage: /mod <promote, demote>, <player>") remove_mod(params[2])
end else
player_print('Command failed. Usage: /mod <promote, demote>, <player>')
end
end end
local function afk() local function afk()
for _,v in pairs (game.players) do for _, v in pairs(game.players) do
if v.afk_time > 300 then if v.afk_time > 300 then
local time = " " local time = ' '
if v.afk_time > 21600 then if v.afk_time > 21600 then
time = time .. math.floor(v.afk_time / 216000) .. " hours " time = time .. math.floor(v.afk_time / 216000) .. ' hours '
end end
if v.afk_time > 3600 then if v.afk_time > 3600 then
time = time .. math.floor(v.afk_time / 3600) % 60 .. " minutes and " time = time .. math.floor(v.afk_time / 3600) % 60 .. ' minutes and '
end end
time = time .. math.floor(v.afk_time / 60) % 60 .. " seconds." time = time .. math.floor(v.afk_time / 60) % 60 .. ' seconds.'
player_print(v.name .. " has been afk for" .. time) player_print(v.name .. ' has been afk for' .. time)
end
end end
end
end end
local function tag(cmd) local function tag(cmd)
if game.player and not game.player.admin then if game.player and not game.player.admin then
cant_run(cmd.name) cant_run(cmd.name)
return return
end end
if cmd.parameter ~= nil then if cmd.parameter ~= nil then
local params = {} local params = {}
for param in string.gmatch(cmd.parameter, "%S+") do table.insert(params, param) end for param in string.gmatch(cmd.parameter, '%S+') do
if #params < 2 then table.insert(params, param)
player_print("Usage: <player> <tag> Sets a players tag.") end
elseif game.players[params[1]] == nil then if #params < 2 then
player_print("Player does not exist.") player_print('Usage: <player> <tag> Sets a players tag.')
else elseif game.players[params[1]] == nil then
local tag = string.sub(cmd.parameter, params[1]:len() + 2) player_print('Player does not exist.')
game.players[params[1]].tag = "[" .. tag .. "]" else
game.print(params[1] .. " joined [" .. tag .. "].") local tag = string.sub(cmd.parameter, params[1]:len() + 2)
game.players[params[1]].tag = '[' .. tag .. ']'
game.print(params[1] .. ' joined [' .. tag .. '].')
end
else
player_print('Usage: /tag <player> <tag> Sets a players tag.')
end end
else
player_print('Usage: /tag <player> <tag> Sets a players tag.')
end
end end
local function follow(cmd) local function follow(cmd)
if not game.player then if not game.player then
log("<Server can't do that.") log("<Server can't do that.")
return return
end end
if cmd.parameter ~= nil and game.players[cmd.parameter] ~= nil then if cmd.parameter ~= nil and game.players[cmd.parameter] ~= nil then
global.follows[game.player.name] = cmd.parameter global.follows[game.player.name] = cmd.parameter
global.follows.n_entries = global.follows.n_entries + 1 global.follows.n_entries = global.follows.n_entries + 1
else else
player_print("Usage: /follow <player> makes you follow the player. Use /unfollow to stop following a player.") player_print('Usage: /follow <player> makes you follow the player. Use /unfollow to stop following a player.')
end end
end end
local function unfollow(cmd) local function unfollow(cmd)
if not game.player then if not game.player then
log("<Server can't do that.") log("<Server can't do that.")
return return
end end
if global.follows[game.player.name] ~= nil then if global.follows[game.player.name] ~= nil then
global.follows[game.player.name] = nil global.follows[game.player.name] = nil
global.follows.n_entries = global.follows.n_entries - 1 global.follows.n_entries = global.follows.n_entries - 1
end end
end end
global.tp_players = {} global.tp_players = {}
local function built_entity(event) local function built_entity(event)
local index = event.player_index local index = event.player_index
if global.tp_players[index] then if global.tp_players[index] then
local entity = event.created_entity local entity = event.created_entity
if entity.type ~= "entity-ghost" then return end if entity.type ~= 'entity-ghost' then
return
end
game.players[index].teleport(entity.position) game.players[index].teleport(entity.position)
entity.destroy() entity.destroy()
end end
end end
Event.add(defines.events.on_built_entity, built_entity) Event.add(defines.events.on_built_entity, built_entity)
local function toggle_tp_mode(cmd) local function toggle_tp_mode(cmd)
if not game.player or not (game.player.admin or is_mod(game.player.name)) then if not game.player or not (game.player.admin or is_mod(game.player.name)) then
cant_run(cmd.name) cant_run(cmd.name)
return return
end end
local index = game.player.index local index = game.player.index
local toggled = global.tp_players[index] local toggled = global.tp_players[index]
if toggled then if toggled then
global.tp_players[index] = nil global.tp_players[index] = nil
player_print("tp mode is now off") player_print('tp mode is now off')
else else
global.tp_players[index] = true global.tp_players[index] = true
player_print("tp mode is now on - place a ghost entity to teleport there.") player_print('tp mode is now on - place a ghost entity to teleport there.')
end end
end end
global.old_force = {} global.old_force = {}
global.force_toggle_init = true global.force_toggle_init = true
local function forcetoggle(cmd) local function forcetoggle(cmd)
if not game.player or not (game.player.admin or is_mod(game.player.name)) or (not game.player.character) then if not game.player or not (game.player.admin or is_mod(game.player.name)) or (not game.player.character) then
cant_run(cmd.name) cant_run(cmd.name)
return return
end end
if global.force_toggle_init then if global.force_toggle_init then
game.forces.enemy.research_all_technologies() --avoids losing logstics slot configuration game.forces.enemy.research_all_technologies() --avoids losing logstics slot configuration
global.force_toggle_init = false global.force_toggle_init = false
end end
-- save the logistics slots -- save the logistics slots
local slots = {} local slots = {}
local slot_counts = game.player.character.request_slot_count local slot_counts = game.player.character.request_slot_count
if game.player.character.request_slot_count > 0 then if game.player.character.request_slot_count > 0 then
for i = 1, slot_counts do for i = 1, slot_counts do
slot = game.player.character.get_request_slot(i) slot = game.player.character.get_request_slot(i)
if slot ~= nil then if slot ~= nil then
table.insert(slots, slot) table.insert(slots, slot)
end
end
end
if game.player.force.name == "enemy" then
local old_force = global.old_force[game.player.name]
if not old_force then
game.player.force = "player"
game.player.print("You're are now on the player force.")
else
if game.forces[old_force] then
game.player.force = old_force
else
game.player.force = "player"
end
end
else
--Put roboports into inventory
inv = game.player.get_inventory(defines.inventory.player_armor)
if inv[1].valid_for_read
then
local name = inv[1].name
if name:match("power") or name:match("modular") then
local equips = inv[1].grid.equipment
for _,equip in pairs(equips) do
if equip.name == "personal-roboport-equipment"
or equip.name == "personal-roboport-mk2-equipment"
or equip.name == "personal-laser-defense-equipment" then
if game.player.insert{name = equip.name} == 0 then
game.player.surface.spill_item_stack(game.player.position, {name = equip.name})
end
inv[1].grid.take(equip)
end
end end
end end
end end
global.old_force[game.player.name] = game.player.force.name if game.player.force.name == 'enemy' then
game.player.force = "enemy" local old_force = global.old_force[game.player.name]
end if not old_force then
game.player.print("You are now on the " .. game.player.force.name .. " force.") game.player.force = 'player'
game.player.print("You're are now on the player force.")
else
if game.forces[old_force] then
game.player.force = old_force
else
game.player.force = 'player'
end
end
else
--Put roboports into inventory
inv = game.player.get_inventory(defines.inventory.player_armor)
if inv[1].valid_for_read then
local name = inv[1].name
if name:match('power') or name:match('modular') then
local equips = inv[1].grid.equipment
for _, equip in pairs(equips) do
if
equip.name == 'personal-roboport-equipment' or equip.name == 'personal-roboport-mk2-equipment' or
equip.name == 'personal-laser-defense-equipment'
then
if game.player.insert {name = equip.name} == 0 then
game.player.surface.spill_item_stack(game.player.position, {name = equip.name})
end
inv[1].grid.take(equip)
end
end
end
end
-- Attempt to rebuild the request slots global.old_force[game.player.name] = game.player.force.name
slot_counts = game.player.character.request_slot_count game.player.force = 'enemy'
if game.player.character.request_slot_count > 0 then end
for _,slot in ipairs(slots) do game.player.print('You are now on the ' .. game.player.force.name .. ' force.')
game.player.character.set_request_slot(slot, _)
end -- Attempt to rebuild the request slots
end slot_counts = game.player.character.request_slot_count
if game.player.character.request_slot_count > 0 then
for _, slot in ipairs(slots) do
game.player.character.set_request_slot(slot, _)
end
end
end end
local function get_group() local function get_group()
local group = game.permissions.get_group("Banned") local group = game.permissions.get_group('Banned')
if not group then if not group then
game.permissions.create_group("Banned") game.permissions.create_group('Banned')
group = game.permissions.get_group("Banned") group = game.permissions.get_group('Banned')
if group then if group then
for i=2,174 do for i = 2, 174 do
group.set_allows_action(i, false) group.set_allows_action(i, false)
end
else
game.print(
'This would have nearly crashed the server, please consult the next best scenario dev (valansch or TWLtriston).'
)
end end
else
game.print("This would have nearly crashed the server, please consult the next best scenario dev (valansch or TWLtriston).")
end end
end return group
return group
end end
function custom_commands_untempban(param) local custom_commands_untempban =
game.print(param.name .. " is out of timeout.") Token.register(
game.permissions.get_group("Default").add_player(param.name) function(param)
end game.print(param.name .. ' is out of timeout.')
game.permissions.get_group('Default').add_player(param.name)
end
)
local function tempban(cmd) local function tempban(cmd)
if (not game.player) or not (game.player.admin or is_mod(game.player.name)) then if (not game.player) or not (game.player.admin or is_mod(game.player.name)) then
cant_run(cmd.name) cant_run(cmd.name)
return return
end
if cmd.parameter == nil then
player_print("Tempban failed. Usage: /tempban <player> <minutes> Temporarily bans a player.")
return
end end
local params = {} if cmd.parameter == nil then
for param in string.gmatch(cmd.parameter, "%S+") do table.insert(params, param) end player_print('Tempban failed. Usage: /tempban <player> <minutes> Temporarily bans a player.')
if #params < 2 or not tonumber(params[2]) then return
player_print("Tempban failed. Usage: /tempban <player> <minutes> Temporarily bans a player.") end
return local params = {}
end for param in string.gmatch(cmd.parameter, '%S+') do
if not game.players[params[1]] then table.insert(params, param)
player_print("Player doesn't exist.") end
return if #params < 2 or not tonumber(params[2]) then
end player_print('Tempban failed. Usage: /tempban <player> <minutes> Temporarily bans a player.')
local group = get_group() return
game.print(get_actor() .. " put " .. params[1] .. " in timeout for " .. params[2] .. " minutes.") end
if group then if not game.players[params[1]] then
group.add_player(params[1]) player_print("Player doesn't exist.")
if not tonumber(cmd.parameter) then return
Task.set_timeout(60 * tonumber(params[2]), "custom_commands_untempban", {name = params[1]}) end
local group = get_group()
game.print(get_actor() .. ' put ' .. params[1] .. ' in timeout for ' .. params[2] .. ' minutes.')
if group then
group.add_player(params[1])
if not tonumber(cmd.parameter) then
Task.set_timeout(60 * tonumber(params[2]), custom_commands_untempban, {name = params[1]})
end
end end
end
end end
local custom_commands_replace_ghosts =
function custom_commands_replace_ghosts(param) Token.register(
for _,ghost in pairs(param.ghosts) do function(param)
local new_ghost = game.surfaces[param.surface_index].create_entity{name = "entity-ghost", position = ghost.position, inner_name = ghost.ghost_name, expires = false, force = "enemy", direction = ghost.direction} for _, ghost in pairs(param.ghosts) do
new_ghost.last_user = ghost.last_user local new_ghost =
end game.surfaces[param.surface_index].create_entity {
end name = 'entity-ghost',
position = ghost.position,
inner_name = ghost.ghost_name,
expires = false,
force = 'enemy',
direction = ghost.direction
}
new_ghost.last_user = ghost.last_user
end
end
)
local function spyshot(cmd) local function spyshot(cmd)
if not cmd then return 0 end if not cmd then
local player_name = cmd.parameter return 0
if player_name and game.players[player_name] then end
for _,spy in pairs(global.spys) do local player_name = cmd.parameter
if game.players[spy] and game.players[spy].connected then if player_name and game.players[player_name] then
local pos = game.players[player_name].position for _, spy in pairs(global.spys) do
pseudo_ghosts = {} if game.players[spy] and game.players[spy].connected then
for _,ghost in pairs(game.players[player_name].surface.find_entities_filtered{area = {{pos.x - 60, pos.y - 35},{pos.x + 60, pos.y + 35}},name = "entity-ghost", force = enemy}) do local pos = game.players[player_name].position
local pseudo_ghost = {position = ghost.position, ghost_name = ghost.ghost_name, expires = false, force = "enemy", direction = ghost.direction, last_user = ghost.last_user} pseudo_ghosts = {}
table.insert(pseudo_ghosts, pseudo_ghost) for _, ghost in pairs(
ghost.destroy() game.players[player_name].surface.find_entities_filtered {
end area = {{pos.x - 60, pos.y - 35}, {pos.x + 60, pos.y + 35}},
game.take_screenshot{by_player = spy, position = pos, show_gui = false, show_entity_info = true, resolution = {1920, 1080}, anti_alias = true, zoom = 0.5, path ="spyshot.png"} name = 'entity-ghost',
game.players[spy].print("You just took a screenshot!") force = enemy
Task.set_timeout(2, "custom_commands_replace_ghosts", {ghosts = pseudo_ghosts, surface_index = game.players[player_name].surface.index}) --delay replacements for the screenshot to render }
return ) do
end local pseudo_ghost = {
position = ghost.position,
ghost_name = ghost.ghost_name,
expires = false,
force = 'enemy',
direction = ghost.direction,
last_user = ghost.last_user
}
table.insert(pseudo_ghosts, pseudo_ghost)
ghost.destroy()
end
game.take_screenshot {
by_player = spy,
position = pos,
show_gui = false,
show_entity_info = true,
resolution = {1920, 1080},
anti_alias = true,
zoom = 0.5,
path = 'spyshot.png'
}
game.players[spy].print('You just took a screenshot!')
Task.set_timeout(
2,
custom_commands_replace_ghosts,
{ghosts = pseudo_ghosts, surface_index = game.players[player_name].surface.index}
) --delay replacements for the screenshot to render
return
end
end
player_print('No spy online!')
end end
player_print("No spy online!")
end
end end
local function zoom(cmd) local function zoom(cmd)
if game.player and cmd and cmd.parameter and tonumber(cmd.parameter) then if game.player and cmd and cmd.parameter and tonumber(cmd.parameter) then
game.player.zoom = tonumber(cmd.parameter) game.player.zoom = tonumber(cmd.parameter)
end end
end end
local function pool() local function pool()
if game.player and game.player.admin then if game.player and game.player.admin then
local t = {} p = game.player.position local t = {}
for x = p.x - 3, p.x + 3 do p = game.player.position
for y = p.y + 2, p.y + 7 do for x = p.x - 3, p.x + 3 do
table.insert(t, {name="water", position={x,y}}) for y = p.y + 2, p.y + 7 do
end table.insert(t, {name = 'water', position = {x, y}})
end
end
game.player.surface.set_tiles(t)
game.player.surface.create_entity {name = 'fish', position = {p.x + 0.5, p.y + 5}}
end end
game.player.surface.set_tiles(t)
game.player.surface.create_entity{name = "fish", position = {p.x + 0.5, p.y + 5}}
end
end end
if not _DEBUG then if not _DEBUG then
local old_add_command = commands.add_command local old_add_command = commands.add_command
commands.add_command = function(name, desc, func) commands.add_command =
old_add_command(name, desc, function(cmd) function(name, desc, func)
local success, error = pcall(func, cmd) old_add_command(
if not success then name,
log(error) desc,
end function(cmd)
end) local success, error = pcall(func, cmd)
end if not success then
log(error)
end
end
)
end
end end
commands.add_command("kill", "Will kill you.", kill) commands.add_command('kill', 'Will kill you.', kill)
commands.add_command("tpplayer", "<player> - Teleports you to the player. (Admins and moderators)", teleport_player) commands.add_command('tpplayer', '<player> - Teleports you to the player. (Admins and moderators)', teleport_player)
commands.add_command("invoke", "<player> - Teleports the player to you. (Admins and moderators)", invoke) commands.add_command('invoke', '<player> - Teleports the player to you. (Admins and moderators)', invoke)
commands.add_command("tppos", "Teleports you to a selected entity. (Admins only)", teleport_location) commands.add_command('tppos', 'Teleports you to a selected entity. (Admins only)', teleport_location)
commands.add_command("walkabout", '<player> <duration> - Send someone on a walk. (Admins and moderators)', walkabout) commands.add_command('walkabout', '<player> <duration> - Send someone on a walk. (Admins and moderators)', walkabout)
commands.add_command("regulars", 'Prints a list of game regulars.', print_regulars) commands.add_command('regulars', 'Prints a list of game regulars.', print_regulars)
commands.add_command("regular", '<promote, demote>, <player> Change regular status of a player. (Admins and moderators)', regular) commands.add_command(
commands.add_command("mods", 'Prints a list of game mods.', print_mods) 'regular',
commands.add_command("mod", '<promote, demote>, <player> Changes moderator status of a player. (Admins only)', mod) '<promote, demote>, <player> Change regular status of a player. (Admins and moderators)',
commands.add_command("afk", 'Shows how long players have been afk.', afk) regular
)
commands.add_command('mods', 'Prints a list of game mods.', print_mods)
commands.add_command('mod', '<promote, demote>, <player> Changes moderator status of a player. (Admins only)', mod)
commands.add_command('afk', 'Shows how long players have been afk.', afk)
--commands.add_command("tag", '<player> <tag> Sets a players tag. (Admins only)', tag) --commands.add_command("tag", '<player> <tag> Sets a players tag. (Admins only)', tag)
commands.add_command("follow", '<player> makes you follow the player. Use /unfollow to stop following a player.', follow) commands.add_command(
commands.add_command("unfollow", 'stops following a player.', unfollow) 'follow',
commands.add_command("tpmode", "Toggles tp mode. When on place a ghost entity to teleport there (Admins and moderators)", toggle_tp_mode) '<player> makes you follow the player. Use /unfollow to stop following a player.',
commands.add_command("forcetoggle", "Toggles the players force between player and enemy (Admins and moderators)", forcetoggle) follow
commands.add_command("tempban", "<player> <minutes> Temporarily bans a player (Admins and moderators)", tempban) )
commands.add_command("spyshot", "<player> Sends a screenshot of player to discord. (If a host is online. If no host is online, you can become one yourself. Ask on discord :))", spyshot) commands.add_command('unfollow', 'stops following a player.', unfollow)
commands.add_command("zoom", "<number> Sets your zoom.", zoom) commands.add_command(
commands.add_command("all-tech", "researches all technologies", function() if game.player and game.player.admin then game.player.force.research_all_technologies() end end) 'tpmode',
commands.add_command("hax", "Toggles your hax", function() if game.player and game.player.admin then game.player.cheat_mode = not game.player.cheat_mode end end) 'Toggles tp mode. When on place a ghost entity to teleport there (Admins and moderators)',
commands.add_command("pool", "Spawns a pool", pool) toggle_tp_mode
)
commands.add_command(
'forcetoggle',
'Toggles the players force between player and enemy (Admins and moderators)',
forcetoggle
)
commands.add_command('tempban', '<player> <minutes> Temporarily bans a player (Admins and moderators)', tempban)
commands.add_command(
'spyshot',
'<player> Sends a screenshot of player to discord. (If a host is online. If no host is online, you can become one yourself. Ask on discord :))',
spyshot
)
commands.add_command('zoom', '<number> Sets your zoom.', zoom)
commands.add_command(
'all-tech',
'researches all technologies',
function()
if game.player and game.player.admin then
game.player.force.research_all_technologies()
end
end
)
commands.add_command(
'hax',
'Toggles your hax',
function()
if game.player and game.player.admin then
game.player.cheat_mode = not game.player.cheat_mode
end
end
)
commands.add_command('pool', 'Spawns a pool', pool)

View File

@ -2,10 +2,9 @@
-- !! ATTENTION !! -- !! ATTENTION !!
-- Use water only in starting area as map setting!!! -- Use water only in starting area as map setting!!!
local perlin = require 'map_gen.shared.perlin_noise' local perlin = require 'map_gen.shared.perlin_noise'
local Task = require 'utils.Task' local Token = require 'utils.global_token'
wreck_item_pool = {} local wreck_item_pool = {
wreck_item_pool = {
{name = 'iron-gear-wheel', count = 32}, {name = 'iron-gear-wheel', count = 32},
{name = 'iron-plate', count = 64}, {name = 'iron-plate', count = 64},
{name = 'rocket-control-unit', count = 1}, {name = 'rocket-control-unit', count = 1},
@ -35,586 +34,240 @@ wreck_item_pool = {
{name = 'explosive-rocket', count = 32} {name = 'explosive-rocket', count = 32}
} }
local function place_entities(surface, entity_list) local ship_callback =
local directions = { Token.register(
defines.direction.north, function(entity)
defines.direction.east, entity.health = math.random(entity.health)
defines.direction.south,
defines.direction.west
}
for _, entity in pairs(entity_list) do
local r = math.random(1, entity.chance)
if r == 1 then
if not entity.force then
entity.force = 'player'
end
local r = math.random(1, 4)
if
surface.can_place_entity {
name = entity.name,
position = entity.pos,
direction = directions[r],
force = entity.force
}
then
local e =
surface.create_entity {
name = entity.name,
position = entity.pos,
direction = directions[r],
force = entity.force
}
if entity.health then
if entity.health == 'low' then
e.health = ((e.health / 1000) * math.random(33, 330))
end
if entity.health == 'medium' then
e.health = ((e.health / 1000) * math.random(333, 666))
end
if entity.health == 'high' then
e.health = ((e.health / 1000) * math.random(666, 999))
end
if entity.health == 'random' then
e.health = ((e.health / 1000) * math.random(1, 1000))
end
end
return true, e
end
end
end
return false
end
local function find_tile_placement_spot_around_target_position(tilename, position, mode, density) entity.insert(wreck_item_pool[math.random(#wreck_item_pool)])
local x = position.x entity.insert(wreck_item_pool[math.random(#wreck_item_pool)])
local y = position.y entity.insert(wreck_item_pool[math.random(#wreck_item_pool)])
if not surface then
surface = game.surfaces[1]
end end
local scan_radius = 50 )
if not tilename then
return
end
if not mode then
mode = 'ball'
end
if not density then
density = 1
end
local cluster_tiles = {}
local auto_correct = true
local scanned_tile = surface.get_tile(x, y) local clear_types = {'simple-entity', 'tree'}
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
local i = 2 local function do_clear_entities(world)
local r = 1 local entities = world.surface.find_entities_filtered({area = world.area, type = clear_types})
for _, entity in ipairs(entities) do
if mode == 'ball' then entity.destroy()
if math.random(1, 2) == 1 then
density = density * -1
end
r = math.random(1, 4)
end
if mode == 'line' then
density = 1
r = math.random(1, 4)
end
if mode == 'line_down' then
density = density * -1
r = math.random(1, 4)
end
if mode == 'line_up' then
density = 1
r = math.random(1, 4)
end
if mode == 'block' then
r = 1
density = 1
end
if r == 1 then
--start placing at -1,-1
while i <= scan_radius do
y = y - density
x = x - density
for a = 1, i, 1 do
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
x = x + density
end
for a = 1, i, 1 do
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
y = y + density
end
for a = 1, i, 1 do
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
x = x - density
end
for a = 1, i, 1 do
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
y = y - density
end
i = i + 2
end
end
if r == 2 then
--start placing at 0,-1
while i <= scan_radius do
y = y - density
x = x - density
for a = 1, i, 1 do
x = x + density
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
end
for a = 1, i, 1 do
y = y + density
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
end
for a = 1, i, 1 do
x = x - density
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
end
for a = 1, i, 1 do
y = y - density
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
end
i = i + 2
end
end
if r == 3 then
--start placing at 1,-1
while i <= scan_radius do
y = y - density
x = x + density
for a = 1, i, 1 do
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
y = y + density
end
for a = 1, i, 1 do
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
x = x - density
end
for a = 1, i, 1 do
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
y = y - density
end
for a = 1, i, 1 do
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
x = x + density
end
i = i + 2
end
end
if r == 4 then
--start placing at 1,0
while i <= scan_radius do
y = y - density
x = x + density
for a = 1, i, 1 do
y = y + density
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
end
for a = 1, i, 1 do
x = x - density
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
end
for a = 1, i, 1 do
y = y - density
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
end
for a = 1, i, 1 do
x = x + density
local scanned_tile = surface.get_tile(x, y)
if scanned_tile.name ~= tilename then
table.insert(cluster_tiles, {name = tilename, position = {x, y}})
surface.set_tiles(cluster_tiles, auto_correct)
return true, x, y
end
end
i = i + 2
end
end
return false
end
local function create_tile_cluster(tilename, position, amount)
local mode = 'ball'
local cluster_tiles = {}
local surface = game.surfaces[1]
local pos = position
local x = pos.x
local y = pos.y
for i = 1, amount, 1 do
local b, x, y = find_tile_placement_spot_around_target_position(tilename, pos, mode)
if b == true then
if 1 == math.random(1, 2) then
pos.x = x
pos.y = y
end
end
if b == false then
return false, x, y
end
if i >= amount then
return true, x, y
end
end end
end end
function run_combined_module(event) local random_health =
local area = event.area Token.register(
local surface = event.surface function(e)
e.health = math.random(e.health)
end
)
local entities = surface.find_entities(area) local medium_health =
for _, entity in pairs(entities) do Token.register(
if entity.type == 'simple-entity' or entity.type == 'tree' then function(e)
if entity.name ~= 'dry-tree' then e.health = math.random(math.floor(e.health * 0.333), math.floor(e.health * 0.666))
entity.destroy() end
)
local low_health =
Token.register(
function(e)
e.health = math.random(math.floor(e.health * 0.033), math.floor(e.health * 0.330))
end
)
local turrent_callback =
Token.register(
function(e)
if math.random(1, 3) == 1 then
e.insert('piercing-rounds-magazine')
else
e.insert('firearm-magazine')
end
end
)
return function(x, y, world)
local entities = {}
local area = world.area
local surface = world.surface
if not world.island_resort_cleared then
world.island_resort_cleared = true
do_clear_entities(world)
end
local tile_to_insert = 'sand-1'
local seed_increment_number = 10000
local seed = surface.map_gen_settings.seed
local noise_borg_defense_1 = perlin:noise(((world.x + seed) / 100), ((world.y + seed) / 100), 0)
seed = seed + seed_increment_number
local noise_borg_defense_2 = perlin:noise(((world.x + seed) / 20), ((world.y + seed) / 20), 0)
seed = seed + seed_increment_number
local noise_borg_defense = noise_borg_defense_1 + noise_borg_defense_2 * 0.15
local noise_trees_1 = perlin:noise(((world.x + seed) / 50), ((world.y + seed) / 50), 0)
seed = seed + seed_increment_number
local noise_trees_2 = perlin:noise(((world.x + seed) / 15), ((world.y + seed) / 15), 0)
seed = seed + seed_increment_number
local noise_trees = noise_trees_1 + noise_trees_2 * 0.3
local noise_walls_1 = perlin:noise(((world.x + seed) / 150), ((world.y + seed) / 150), 0)
seed = seed + seed_increment_number
local noise_walls_2 = perlin:noise(((world.x + seed) / 50), ((world.y + seed) / 50), 0)
seed = seed + seed_increment_number
local noise_walls_3 = perlin:noise(((world.x + seed) / 20), ((world.y + seed) / 20), 0)
seed = seed + seed_increment_number
local noise_walls = noise_walls_1 + noise_walls_2 * 0.1 + noise_walls_3 * 0.03
if noise_borg_defense > 0.66 then
if math.random(25) == 1 then
table.insert(entities, {name = 'big-ship-wreck-1', force = 'player', callback = ship_callback})
elseif math.random(25) == 1 then
table.insert(entities, {name = 'big-ship-wreck-2', force = 'player', callback = ship_callback})
elseif math.random(25) == 1 then
table.insert(entities, {name = 'big-ship-wreck-3', force = 'player', callback = ship_callback})
end
end
if noise_trees > 0.17 then
tile_to_insert = 'sand-3'
end
if noise_borg_defense > 0.4 then
tile_to_insert = 'concrete'
end
if noise_borg_defense > 0.35 and noise_borg_defense < 0.4 then
tile_to_insert = 'stone-path'
end
if noise_borg_defense > 0.65 and noise_borg_defense < 0.66 then
table.insert(entities, {name = 'substation', force = 'enemy'})
end
if noise_borg_defense >= 0.54 and noise_borg_defense < 0.65 then
table.insert(entities, {name = 'solar-panel', force = 'enemy'})
end
if noise_borg_defense > 0.53 and noise_borg_defense < 0.54 then
table.insert(entities, {name = 'substation', force = 'enemy'})
end
if noise_borg_defense >= 0.51 and noise_borg_defense < 0.53 then
table.insert(entities, {name = 'accumulator', force = 'enemy'})
end
if noise_borg_defense >= 0.50 and noise_borg_defense < 0.51 then
table.insert(entities, {name = 'substation', force = 'enemy'})
end
if noise_borg_defense >= 0.487 and noise_borg_defense < 0.50 then
table.insert(entities, {name = 'laser-turret', force = 'enemy'})
end
if noise_borg_defense >= 0.485 and noise_borg_defense < 0.487 then
table.insert(entities, {name = 'substation', force = 'enemy'})
end
if noise_borg_defense >= 0.45 and noise_borg_defense < 0.484 then
table.insert(entities, {name = 'stone-wall', force = 'enemy'})
end
if noise_trees > 0.2 and tile_to_insert == 'sand-3' then
if math.random(1, 15) == 1 then
if math.random(1, 5) == 1 then
table.insert(entities, {name = 'dry-hairy-tree'})
else
table.insert(entities, {name = 'dry-tree'})
end end
end end
end end
for x = 0, 31, 1 do if math.random(35000) == 1 then
Task.queue_task('run_borg', {area = event.area, surface = event.surface, x = x}) table.insert(entities, {name = 'big-ship-wreck-1', force = 'player', callback = ship_callback})
elseif math.random(45000) == 1 then
table.insert(entities, {name = 'big-ship-wreck-2', force = 'player', callback = ship_callback})
elseif math.random(55000) == 1 then
table.insert(entities, {name = 'big-ship-wreck-3', force = 'player', callback = ship_callback})
elseif noise_walls > -0.03 and noise_walls < 0.03 and math.random(40) == 1 then
table.insert(entities, {name = 'gun-turret', force = 'enemy', callback = turrent_callback})
elseif noise_borg_defense > 0.41 and noise_borg_defense < 0.45 and math.random(15) == 1 then
table.insert(entities, {name = 'gun-turret', force = 'enemy', callback = turrent_callback})
elseif math.random(7500) == 1 then
table.insert(entities, {name = 'pipe-to-ground', force = 'enemy'})
elseif tile_to_insert ~= 'stone-path' and tile_to_insert ~= 'concrete' and math.random(1500) == 1 then
table.insert(entities, {name = 'dead-dry-hairy-tree'})
elseif tile_to_insert ~= 'stone-path' and tile_to_insert ~= 'concrete' and math.random(1500) == 1 then
table.insert(entities, {name = 'dead-grey-trunk'})
elseif math.random(25000) == 1 then
table.insert(entities, {name = 'medium-ship-wreck', force = 'player', callback = medium_health})
elseif math.random(15000) == 1 then
table.insert(entities, {name = 'small-ship-wreck', force = 'player', callback = medium_health})
elseif math.random(150000) == 1 then
table.insert(entities, {name = 'car', force = 'player', callback = low_health})
elseif math.random(100000) == 1 then
table.insert(entities, {name = 'laser-turret', force = 'enemy', callback = low_health})
elseif math.random(1000000) == 1 then
table.insert(entities, {name = 'nuclear-reactor', force = 'enemy', callback = medium_health})
end end
end
function run_borg(params) if noise_trees < -0.5 and (tile_to_insert == 'sand-3' or tile_to_insert == 'sand-1') and math.random(15) == 1 then
local tiles = {} table.insert(entities, {name = 'rock-big'})
local decoratives = {} end
local area = params.area local noise_water_1 = perlin:noise(((world.x + seed) / 200), ((world.y + seed) / 200), 0)
local surface = params.surface seed = seed + seed_increment_number
local noise_water_2 = perlin:noise(((world.x + seed) / 100), ((world.y + seed) / 100), 0)
seed = seed + seed_increment_number
local noise_water_3 = perlin:noise(((world.x + seed) / 25), ((world.y + seed) / 25), 0)
seed = seed + seed_increment_number
local noise_water_4 = perlin:noise(((world.x + seed) / 10), ((world.y + seed) / 10), 0)
seed = seed + seed_increment_number
local noise_water = noise_water_1 + noise_water_2 + noise_water_3 * 0.07 + noise_water_4 * 0.07
local x = params.x noise_water_1 = perlin:noise(((world.x + seed) / 200), ((world.y + seed) / 200), 0)
local pos_x = area.left_top.x + x seed = seed + seed_increment_number
noise_water_2 = perlin:noise(((world.x + seed) / 100), ((world.y + seed) / 100), 0)
seed = seed + seed_increment_number
noise_water_3 = perlin:noise(((world.x + seed) / 25), ((world.y + seed) / 25), 0)
seed = seed + seed_increment_number
noise_water_4 = perlin:noise(((world.x + seed) / 10), ((world.y + seed) / 10), 0)
seed = seed + seed_increment_number
noise_water_2 = noise_water_1 + noise_water_2 + noise_water_3 * 0.07 + noise_water_4 * 0.07
for y = 0, 31, 1 do if
local pos_y = area.left_top.y + y tile_to_insert ~= 'stone-path' and tile_to_insert ~= 'concrete' and noise_water > -0.15 and noise_water < 0.15 and
local pos = {x = pos_x, y = pos_y} noise_water_2 > 0.5
local tile = surface.get_tile(pos_x, pos_y) then
local tile_to_insert = 'sand-1' tile_to_insert = 'water-green'
local entity_placed = false end
local seed_increment_number = 10000 if noise_borg_defense <= 0.45 and tile_to_insert ~= 'water-green' then
local seed = surface.map_gen_settings.seed local a = -0.01
local b = 0.01
local noise_borg_defense_1 = perlin:noise(((pos_x + seed) / 100), ((pos_y + seed) / 100), 0) if noise_walls > a and noise_walls < b then
seed = seed + seed_increment_number table.insert(entities, {name = 'stone-wall', force = 'enemy'})
local noise_borg_defense_2 = perlin:noise(((pos_x + seed) / 20), ((pos_y + seed) / 20), 0)
seed = seed + seed_increment_number
local noise_borg_defense = noise_borg_defense_1 + noise_borg_defense_2 * 0.15
local noise_trees_1 = perlin:noise(((pos_x + seed) / 50), ((pos_y + seed) / 50), 0)
seed = seed + seed_increment_number
local noise_trees_2 = perlin:noise(((pos_x + seed) / 15), ((pos_y + seed) / 15), 0)
seed = seed + seed_increment_number
local noise_trees = noise_trees_1 + noise_trees_2 * 0.3
local noise_walls_1 = perlin:noise(((pos_x + seed) / 150), ((pos_y + seed) / 150), 0)
seed = seed + seed_increment_number
local noise_walls_2 = perlin:noise(((pos_x + seed) / 50), ((pos_y + seed) / 50), 0)
seed = seed + seed_increment_number
local noise_walls_3 = perlin:noise(((pos_x + seed) / 20), ((pos_y + seed) / 20), 0)
seed = seed + seed_increment_number
local noise_walls = noise_walls_1 + noise_walls_2 * 0.1 + noise_walls_3 * 0.03
if noise_borg_defense > 0.66 then
local entity_list = {}
table.insert(entity_list, {name = 'big-ship-wreck-1', pos = {pos_x, pos_y}, chance = 25})
table.insert(entity_list, {name = 'big-ship-wreck-2', pos = {pos_x, pos_y}, chance = 25})
table.insert(entity_list, {name = 'big-ship-wreck-3', pos = {pos_x, pos_y}, chance = 25})
local b, placed_entity = place_entities(surface, entity_list)
if b == true then
if
placed_entity.name == 'big-ship-wreck-1' or placed_entity.name == 'big-ship-wreck-2' or
placed_entity.name == 'big-ship-wreck-3'
then
placed_entity.insert(wreck_item_pool[math.random(1, #wreck_item_pool)])
placed_entity.insert(wreck_item_pool[math.random(1, #wreck_item_pool)])
placed_entity.insert(wreck_item_pool[math.random(1, #wreck_item_pool)])
end
end
end end
if noise_walls >= a and noise_walls <= b then
if noise_trees > 0.17 then
tile_to_insert = 'sand-3'
end
if noise_borg_defense > 0.4 then
tile_to_insert = 'concrete' tile_to_insert = 'concrete'
end end
if noise_borg_defense > 0.35 and noise_borg_defense < 0.4 then if noise_borg_defense < 0.40 then
tile_to_insert = 'stone-path' if noise_walls > b and noise_walls < b + 0.03 then
end tile_to_insert = 'stone-path'
if noise_borg_defense > 0.65 and noise_borg_defense < 0.66 then end
if surface.can_place_entity {name = 'substation', position = {pos_x, pos_y}, force = 'enemy'} then if noise_walls > a - 0.03 and noise_walls < a then
surface.create_entity {name = 'substation', position = {pos_x, pos_y}, force = 'enemy'} tile_to_insert = 'stone-path'
end end
end end
if noise_borg_defense >= 0.54 and noise_borg_defense < 0.65 then
if surface.can_place_entity {name = 'solar-panel', position = {pos_x, pos_y}, force = 'enemy'} then
surface.create_entity {name = 'solar-panel', position = {pos_x, pos_y}, force = 'enemy'}
end
end
if noise_borg_defense > 0.53 and noise_borg_defense < 0.54 then
if surface.can_place_entity {name = 'substation', position = {pos_x, pos_y}, force = 'enemy'} then
surface.create_entity {name = 'substation', position = {pos_x, pos_y}, force = 'enemy'}
end
end
if noise_borg_defense >= 0.51 and noise_borg_defense < 0.53 then
if surface.can_place_entity {name = 'accumulator', position = {pos_x, pos_y}, force = 'enemy'} then
surface.create_entity {name = 'accumulator', position = {pos_x, pos_y}, force = 'enemy'}
end
end
if noise_borg_defense >= 0.50 and noise_borg_defense < 0.51 then
if surface.can_place_entity {name = 'substation', position = {pos_x, pos_y}, force = 'enemy'} then
surface.create_entity {name = 'substation', position = {pos_x, pos_y}, force = 'enemy'}
end
end
if noise_borg_defense >= 0.487 and noise_borg_defense < 0.50 then
if surface.can_place_entity {name = 'laser-turret', position = {pos_x, pos_y}, force = 'enemy'} then
surface.create_entity {name = 'laser-turret', position = {pos_x, pos_y}, force = 'enemy'}
end
end
if noise_borg_defense >= 0.485 and noise_borg_defense < 0.487 then
if surface.can_place_entity {name = 'substation', position = {pos_x, pos_y}, force = 'enemy'} then
surface.create_entity {name = 'substation', position = {pos_x, pos_y}, force = 'enemy'}
end
end
if noise_borg_defense >= 0.45 and noise_borg_defense < 0.484 then
if surface.can_place_entity {name = 'stone-wall', position = {pos_x, pos_y}, force = 'enemy'} then
surface.create_entity {name = 'stone-wall', position = {pos_x, pos_y}, force = 'enemy'}
end
end
if noise_trees > 0.2 and tile_to_insert == 'sand-3' then
if math.random(1, 15) == 1 then
if math.random(1, 5) == 1 then
if surface.can_place_entity {name = 'dry-hairy-tree', position = {pos_x, pos_y}} then
surface.create_entity {name = 'dry-hairy-tree', position = {pos_x, pos_y}}
end
else
if surface.can_place_entity {name = 'dry-tree', position = {pos_x, pos_y}} then
surface.create_entity {name = 'dry-tree', position = {pos_x, pos_y}}
end
end
end
end
local entity_list = {}
table.insert(entity_list, {name = 'big-ship-wreck-1', pos = {pos_x, pos_y}, chance = 35000, health = 'random'})
table.insert(entity_list, {name = 'big-ship-wreck-2', pos = {pos_x, pos_y}, chance = 45000, health = 'random'})
table.insert(entity_list, {name = 'big-ship-wreck-3', pos = {pos_x, pos_y}, chance = 55000, health = 'random'})
if noise_walls > -0.03 and noise_walls < 0.03 then
table.insert(entity_list, {name = 'gun-turret', pos = {pos_x, pos_y}, force = 'enemy', chance = 40})
end
if noise_borg_defense > 0.41 and noise_borg_defense < 0.45 then
table.insert(entity_list, {name = 'gun-turret', pos = {pos_x, pos_y}, force = 'enemy', chance = 15})
end
table.insert(entity_list, {name = 'pipe-to-ground', pos = {pos_x, pos_y}, force = 'enemy', chance = 7500})
if tile_to_insert ~= 'stone-path' and tile_to_insert ~= 'concrete' then
table.insert(
entity_list,
{name = 'dead-dry-hairy-tree', pos = {pos_x, pos_y}, force = 'enemy', chance = 1500}
)
table.insert(entity_list, {name = 'dead-grey-trunk', pos = {pos_x, pos_y}, force = 'enemy', chance = 1500})
end
table.insert(entity_list, {name = 'medium-ship-wreck', pos = {pos_x, pos_y}, chance = 25000, health = 'medium'})
table.insert(entity_list, {name = 'small-ship-wreck', pos = {pos_x, pos_y}, chance = 15000, health = 'medium'})
table.insert(entity_list, {name = 'car', pos = {pos_x, pos_y}, chance = 150000, health = 'low'})
table.insert(
entity_list,
{name = 'laser-turret', pos = {pos_x, pos_y}, chance = 100000, force = 'enemy', health = 'low'}
)
table.insert(
entity_list,
{name = 'nuclear-reactor', pos = {pos_x, pos_y}, chance = 1000000, force = 'enemy', health = 'medium'}
)
local b, placed_entity = place_entities(surface, entity_list)
if b == true then
if
placed_entity.name == 'big-ship-wreck-1' or placed_entity.name == 'big-ship-wreck-2' or
placed_entity.name == 'big-ship-wreck-3'
then
placed_entity.insert(wreck_item_pool[math.random(1, #wreck_item_pool)])
placed_entity.insert(wreck_item_pool[math.random(1, #wreck_item_pool)])
placed_entity.insert(wreck_item_pool[math.random(1, #wreck_item_pool)])
end
if placed_entity.name == 'gun-turret' then
if math.random(1, 3) == 1 then
placed_entity.insert('piercing-rounds-magazine')
else
placed_entity.insert('firearm-magazine')
end
end
end
if noise_trees < -0.5 then
if tile_to_insert == 'sand-3' or tile_to_insert == 'sand-1' then
if math.random(1, 15) == 1 then
if surface.can_place_entity {name = 'rock-big', position = {pos_x, pos_y}} then
surface.create_entity {name = 'rock-big', position = {pos_x, pos_y}}
end
end
end
end
local noise_water_1 = perlin:noise(((pos_x + seed) / 200), ((pos_y + seed) / 200), 0)
seed = seed + seed_increment_number
local noise_water_2 = perlin:noise(((pos_x + seed) / 100), ((pos_y + seed) / 100), 0)
seed = seed + seed_increment_number
local noise_water_3 = perlin:noise(((pos_x + seed) / 25), ((pos_y + seed) / 25), 0)
seed = seed + seed_increment_number
local noise_water_4 = perlin:noise(((pos_x + seed) / 10), ((pos_y + seed) / 10), 0)
seed = seed + seed_increment_number
local noise_water = noise_water_1 + noise_water_2 + noise_water_3 * 0.07 + noise_water_4 * 0.07
local noise_water_1 = perlin:noise(((pos_x + seed) / 200), ((pos_y + seed) / 200), 0)
seed = seed + seed_increment_number
local noise_water_2 = perlin:noise(((pos_x + seed) / 100), ((pos_y + seed) / 100), 0)
seed = seed + seed_increment_number
local noise_water_3 = perlin:noise(((pos_x + seed) / 25), ((pos_y + seed) / 25), 0)
seed = seed + seed_increment_number
local noise_water_4 = perlin:noise(((pos_x + seed) / 10), ((pos_y + seed) / 10), 0)
seed = seed + seed_increment_number
local noise_water_2 = noise_water_1 + noise_water_2 + noise_water_3 * 0.07 + noise_water_4 * 0.07
if tile_to_insert ~= 'stone-path' and tile_to_insert ~= 'concrete' then
if noise_water > -0.15 and noise_water < 0.15 and noise_water_2 > 0.5 then
tile_to_insert = 'water-green'
local a = pos_x + 1
table.insert(tiles, {name = tile_to_insert, position = {a, pos_y}})
local a = pos_y + 1
table.insert(tiles, {name = tile_to_insert, position = {pos_x, a}})
local a = pos_x - 1
table.insert(tiles, {name = tile_to_insert, position = {a, pos_y}})
local a = pos_y - 1
table.insert(tiles, {name = tile_to_insert, position = {pos_x, a}})
table.insert(tiles, {name = tile_to_insert, position = {pos_x, pos_y}})
end
end
if noise_borg_defense <= 0.45 and tile_to_insert ~= 'water-green' then
local a = -0.01
local b = 0.01
if noise_walls > a and noise_walls < b then
if surface.can_place_entity {name = 'stone-wall', position = {pos_x, pos_y}, force = 'enemy'} then
surface.create_entity {name = 'stone-wall', position = {pos_x, pos_y}, force = 'enemy'}
end
end
if noise_walls >= a and noise_walls <= b then
tile_to_insert = 'concrete'
end
if noise_borg_defense < 0.40 then
if noise_walls > b and noise_walls < b + 0.03 then
tile_to_insert = 'stone-path'
end
if noise_walls > a - 0.03 and noise_walls < a then
tile_to_insert = 'stone-path'
end
end
end
local noise_decoratives_1 = perlin:noise(((pos_x + seed) / 50), ((pos_y + seed) / 50), 0)
seed = seed + seed_increment_number
local noise_decoratives_2 = perlin:noise(((pos_x + seed) / 15), ((pos_y + seed) / 15), 0)
seed = seed + seed_increment_number
local noise_decoratives = noise_decoratives_1 + noise_decoratives_2 * 0.3
if noise_decoratives > 0.3 and noise_decoratives < 0.5 then
if tile_to_insert ~= 'stone-path' and tile_to_insert ~= 'concrete' and tile_to_insert ~= 'water-green' then
if math.random(1, 10) == 1 then
table.insert(decoratives, {name = 'red-desert-bush', position = {pos_x, pos_y}, amount = 1})
end
end
end
table.insert(tiles, {name = tile_to_insert, position = {pos_x, pos_y}})
end end
surface.set_tiles(tiles, true)
for _, deco in pairs(decoratives) do local noise_decoratives_1 = perlin:noise(((world.x + seed) / 50), ((world.y + seed) / 50), 0)
surface.create_decoratives {check_collision = false, decoratives = {deco}} seed = seed + seed_increment_number
local noise_decoratives_2 = perlin:noise(((world.x + seed) / 15), ((world.y + seed) / 15), 0)
local noise_decoratives = noise_decoratives_1 + noise_decoratives_2 * 0.3
local decoratives
if noise_decoratives > 0.3 and noise_decoratives < 0.5 then
if
tile_to_insert ~= 'stone-path' and tile_to_insert ~= 'concrete' and tile_to_insert ~= 'water-green' and
math.random(10) == 1
then
decoratives = {name = 'red-desert-bush', amount = 1}
end
end end
return {tile = tile_to_insert, entities = entities, decoratives = decoratives}
end end

View File

@ -4,9 +4,23 @@ local perlin = require 'map_gen.shared.perlin_noise'
local radius = 129 local radius = 129
local radsquare = radius * radius local radsquare = radius * radius
local clear_types = {'simple-entity', 'resource', 'tree'}
local function do_clear_entities(world)
local entities = world.surface.find_entities_filtered({area = world.area, type = clear_types})
for _, entity in ipairs(entities) do
entity.destroy()
end
end
return function(x, y, world) return function(x, y, world)
local surface = world.surface local surface = world.surface
if not world.island_resort_cleared then
world.island_resort_cleared = true
do_clear_entities(world)
end
local entities = {} local entities = {}
local decoratives = {} local decoratives = {}

View File

@ -3,8 +3,11 @@ local Token = require 'utils.global_token'
local Event = require 'utils.event' local Event = require 'utils.event'
local shape local shape
local tiles_per_tick
local regen_decoratives local regen_decoratives
local total_calls
local function do_row(row, data) local function do_row(row, data)
local function do_tile(tile, pos) local function do_tile(tile, pos)
if not tile then if not tile then
@ -21,7 +24,7 @@ local function do_row(row, data)
for x = top_x, top_x + 31 do for x = top_x, top_x + 31 do
data.x = x data.x = x
local pos = {data.x, data.y} local pos = {x, y}
-- local coords need to be 'centered' to allow for correct rotation and scaling. -- local coords need to be 'centered' to allow for correct rotation and scaling.
local tile = shape(x + 0.5, y + 0.5, data) local tile = shape(x + 0.5, y + 0.5, data)
@ -51,6 +54,44 @@ local function do_row(row, data)
end end
end end
local function do_tile(y, x, data)
local function do_tile_inner(tile, pos)
if not tile then
table.insert(data.tiles, {name = 'out-of-map', position = pos})
elseif type(tile) == 'string' then
table.insert(data.tiles, {name = tile, position = pos})
end
end
local pos = {x, y}
-- local coords need to be 'centered' to allow for correct rotation and scaling.
local tile = shape(x + 0.5, y + 0.5, data)
if type(tile) == 'table' then
do_tile_inner(tile.tile, pos)
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)
end
end
local decoratives = tile.decoratives
if decoratives then
for _, decorative in ipairs(decoratives) do
table.insert(data.decoratives, decorative)
end
end
else
do_tile_inner(tile, pos)
end
end
local function do_place_tiles(data) local function do_place_tiles(data)
data.surface.set_tiles(data.tiles, true) data.surface.set_tiles(data.tiles, true)
end end
@ -79,7 +120,7 @@ local function do_place_decoratives(data)
end end
local dec = data.decoratives local dec = data.decoratives
if dec then if #dec > 0 then
data.surface.create_decoratives({check_collision = true, decoratives = dec}) data.surface.create_decoratives({check_collision = true, decoratives = dec})
end end
end end
@ -112,23 +153,49 @@ local function run_chart_update(data)
end end
end end
function map_gen_action(data) local function map_gen_action(data)
local state = data.state local state = data.y
if state < 32 then if state < 32 then
do_row(state, data) local count = tiles_per_tick
data.state = state + 1
local y = state + data.top_y
local x = data.x
local max_x = data.top_x + 32
data.y = y
repeat
count = count - 1
do_tile(y, x, data)
x = x + 1
if x == max_x then
y = y + 1
if y == data.top_y + 32 then
break
end
x = data.top_x
data.y = y
end
data.x = x
until count == 0
data.y = y - data.top_y
return true return true
elseif state == 32 then elseif state == 32 then
do_place_tiles(data) do_place_tiles(data)
data.state = 33 data.y = 33
return true return true
elseif state == 33 then elseif state == 33 then
do_place_entities(data) do_place_entities(data)
data.state = 34 data.y = 34
return true return true
elseif state == 34 then elseif state == 34 then
do_place_decoratives(data) do_place_decoratives(data)
data.state = 35 data.y = 35
return true return true
elseif state == 35 then elseif state == 35 then
run_chart_update(data) run_chart_update(data)
@ -136,11 +203,15 @@ function map_gen_action(data)
end end
end end
local map_gen_action_token = Token.register(map_gen_action)
local function on_chunk(event) local function on_chunk(event)
local area = event.area local area = event.area
local data = { local data = {
state = 0, y = 0,
x = area.left_top.x,
area = area,
top_x = area.left_top.x, top_x = area.left_top.x,
top_y = area.left_top.y, top_y = area.left_top.y,
surface = event.surface, surface = event.surface,
@ -148,13 +219,17 @@ local function on_chunk(event)
entities = {}, entities = {},
decoratives = {} decoratives = {}
} }
Task.queue_task('map_gen_action', data, 36)
Task.queue_task(map_gen_action_token, data, total_calls)
end end
local function init(args) local function init(args)
shape = args.shape shape = args.shape
tiles_per_tick = args.tiles_per_tick or 32
regen_decoratives = args.regen_decoratives or false regen_decoratives = args.regen_decoratives or false
total_calls = math.ceil(1024 / tiles_per_tick) + 4
Event.add(defines.events.on_chunk_generated, on_chunk) Event.add(defines.events.on_chunk_generated, on_chunk)
end end

View File

@ -78,7 +78,7 @@ local function do_place_decoratives(data)
end end
local dec = data.decoratives local dec = data.decoratives
if dec then if #dec > 0 then
data.surface.create_decoratives({check_collision = true, decoratives = dec}) data.surface.create_decoratives({check_collision = true, decoratives = dec})
end end
end end
@ -100,6 +100,7 @@ local function on_chunk(event)
local area = event.area local area = event.area
local data = { local data = {
area = area,
top_x = area.left_top.x, top_x = area.left_top.x,
top_y = area.left_top.y, top_y = area.left_top.y,
surface = event.surface, surface = event.surface,

View File

@ -4,15 +4,17 @@ You may choose up to one of each type shapes, terrain, ores and misc or one of t
If you want to add your own module, just add it to the others If you want to add your own module, just add it to the others
in this file and your run_*type*_module(event) function will be called. in this file and your run_*type*_module(event) function will be called.
--]] --]]
local Event = require "utils.event"
local b = require "map_gen.shared.builders" local b = require 'map_gen.shared.builders'
local shape = nil local shape = nil
local regen_decoratives = false
local tiles_per_tick = 32
--combined-- --combined--
--shape = require "map_gen.combined.island_resort" --shape = require "map_gen.combined.island_resort"
--require "map_gen.combined.red_planet_v2" --require "map_gen.combined.red_planet_v2"
--require "map_gen.combined.borg_planet_v2" --shape = require 'map_gen.combined.borg_planet_v2'
--require "map_gen.combined.dimensions" --require "map_gen.combined.dimensions"
--require "map_gen.combined.dagobah_swamp" --require "map_gen.combined.dagobah_swamp"
--require "map_gen.combined.meteor_strike" --unfinished --require "map_gen.combined.meteor_strike" --unfinished
@ -84,7 +86,6 @@ local shape = nil
--ores-- --ores--
--require "map_gen.ores.rso.rso_control" --require "map_gen.ores.rso.rso_control"
-- modules that only return max one entity per tile -- modules that only return max one entity per tile
local entity_modules = { local entity_modules = {
--require "map_gen.ores.glitter_ores", --require "map_gen.ores.glitter_ores",
@ -97,7 +98,7 @@ local entity_modules = {
--require "map_gen.ores.resource_clustertruck" --require "map_gen.ores.resource_clustertruck"
} }
local terrain_modules ={ local terrain_modules = {
--require "map_gen.misc.tris_chunk_grid", --require "map_gen.misc.tris_chunk_grid",
} }
@ -106,43 +107,21 @@ miscs = {}
--require "map_gen.misc.rusky_pvp" --require "map_gen.misc.rusky_pvp"
--table.insert(miscs, require("map_gen.misc.rail_grid")) -- used for map_gen.presets.UK --table.insert(miscs, require("map_gen.misc.rail_grid")) -- used for map_gen.presets.UK
local regen_decoratives = false
if #entity_modules > 0 then if #entity_modules > 0 then
shape = shape or b.full_shape shape = shape or b.full_shape
shape = b.apply_entities(shape, entity_modules) shape = b.apply_entities(shape, entity_modules)
end end
if #terrain_modules > 0 then if #terrain_modules > 0 then
shape = shape or b.full_shape shape = shape or b.full_shape
for _, m in ipairs(terrain_modules) do for _, m in ipairs(terrain_modules) do
shape = b.overlay_tile_land(shape, m) shape = b.overlay_tile_land(shape, m)
end end
end end
if shape then if shape then
require ("map_gen.shared.generate")({shape = shape, regen_decoratives = regen_decoratives}) require('map_gen.shared.generate')({shape = shape, regen_decoratives = regen_decoratives, tiles_per_tick = tiles_per_tick})
--require ("map_gen.shared.generate_not_threaded")({shape = shape, regen_decoratives = regen_decoratives}) --require ("map_gen.shared.generate_not_threaded")({shape = shape, regen_decoratives = regen_decoratives})
end end
--[[ local on_chunk_generated = function(event)
if run_combined_module ~= nil then
run_combined_module(event)
end
if run_shape_module ~= nil then
run_shape_module(event)
end
if run_terrain_module ~= nil then
run_terrain_module(event)
end
if run_ores_module ~= nil then
run_ores_module(event)
end
for _,v in pairs(miscs) do
v.on_chunk_generated(event)
end
end
Event.add(defines.events.on_chunk_generated, on_chunk_generated) ]]

View File

@ -4,9 +4,10 @@
-- github: https://github.com/Valansch/RedMew -- github: https://github.com/Valansch/RedMew
-- ======================================================= -- -- ======================================================= --
local Queue = require "utils.Queue" local Queue = require 'utils.Queue'
local PriorityQueue = require "utils.PriorityQueue" local PriorityQueue = require 'utils.PriorityQueue'
local Event = require "utils.event" local Event = require 'utils.event'
local Token = require 'utils.global_token'
local Task = {} local Task = {}
@ -17,64 +18,65 @@ global.total_task_weight = 0
global.task_queue_speed = 1 global.task_queue_speed = 1
local function comp(a, b) local function comp(a, b)
return a.time < b.time return a.time < b.time
end end
local function on_tick() local function on_tick()
local queue = global.task_queue local queue = global.task_queue
for i = 1, get_task_per_tick() do for i = 1, get_task_per_tick() do
local task = Queue.peek(queue) local task = Queue.peek(queue)
if task ~= nil then if task ~= nil then
local success, result = pcall(_G[task.func_name], task.params) -- result is error if not success else result is a boolean for if the task should stay in the queue. -- result is error if not success else result is a boolean for if the task should stay in the queue.
if not success then local success, result = pcall(Token.get(task.func_token), task.params)
log(result) if not success then
Queue.pop(queue) log(result)
global.total_task_weight = global.total_task_weight - task.weight Queue.pop(queue)
elseif not result then global.total_task_weight = global.total_task_weight - task.weight
Queue.pop(queue) elseif not result then
global.total_task_weight = global.total_task_weight - task.weight Queue.pop(queue)
end global.total_task_weight = global.total_task_weight - task.weight
end
end
end end
end
local callbacks = global.callbacks local callbacks = global.callbacks
local callback = PriorityQueue.peek(callbacks) local callback = PriorityQueue.peek(callbacks)
while callback ~= nil and game.tick >= callback.time do while callback ~= nil and game.tick >= callback.time do
local success, error = pcall(_G[callback.func_name], callback.params) local success, error = pcall(Token.get(callback.func_token), callback.params)
if not success then if not success then
log(error) log(error)
end
PriorityQueue.pop(callbacks, comp)
callback = PriorityQueue.peek(callbacks)
end end
PriorityQueue.pop(callbacks, comp)
callback = PriorityQueue.peek(callbacks)
end
end end
global.tpt = global.task_queue_speed global.tpt = global.task_queue_speed
function get_task_per_tick() function get_task_per_tick()
if game.tick % 300 == 0 then if game.tick % 300 == 0 then
local size = global.total_task_weight local size = global.total_task_weight
global.tpt = math.floor(math.log10(size + 1)) * global.task_queue_speed global.tpt = math.floor(math.log10(size + 1)) * global.task_queue_speed
if global.tpt < 1 then if global.tpt < 1 then
global.tpt = 1 global.tpt = 1
end
end end
end return global.tpt
return global.tpt
end end
function Task.set_timeout_in_ticks(ticks, func_name, params) function Task.set_timeout_in_ticks(ticks, func_token, params)
local time = game.tick + ticks local time = game.tick + ticks
local callback = {time = time, func_name = func_name, params = params} local callback = {time = time, func_token = func_token, params = params}
PriorityQueue.push(global.callbacks, callback, comp) PriorityQueue.push(global.callbacks, callback, comp)
end end
function Task.set_timeout(sec, func_name, params) function Task.set_timeout(sec, func_token, params)
Task.set_timeout_in_ticks(60 * sec, func_name, params) Task.set_timeout_in_ticks(60 * sec, func_token, params)
end end
function Task.queue_task(func_name, params, weight) function Task.queue_task(func_token, params, weight)
weight = weight or 1 weight = weight or 1
global.total_task_weight = global.total_task_weight + weight global.total_task_weight = global.total_task_weight + weight
Queue.push(global.task_queue, {func_name = func_name, params = params, weight = weight}) Queue.push(global.task_queue, {func_token = func_token, params = params, weight = weight})
end end
Event.add(defines.events.on_tick, on_tick) Event.add(defines.events.on_tick, on_tick)