From edbef96f78f2226fbd98624005af7a5822f9ea74 Mon Sep 17 00:00:00 2001
From: Gerkiz <fs@nvfs.se>
Date: Sun, 12 Jul 2020 18:58:00 +0200
Subject: [PATCH] fixes

---
 antigrief.lua                      |  96 -----------------
 comfy_panel/player_list.lua        |  54 ++++++----
 maps/fish_defender/commands.lua    |  53 +++++++++
 maps/fish_defender/main.lua        |  34 ++++--
 maps/fish_defender/table.lua       |   3 +-
 maps/fish_defender/terrain.lua     | 165 +++++++++++++++--------------
 maps/mountain_fortress_v3/main.lua |   4 +-
 7 files changed, 196 insertions(+), 213 deletions(-)

diff --git a/antigrief.lua b/antigrief.lua
index 2596cbe9..a601343d 100644
--- a/antigrief.lua
+++ b/antigrief.lua
@@ -26,7 +26,6 @@ local this = {
     players_warned = {},
     log_tree_harvest = false,
     do_not_check_trusted = true,
-    protect_entities = true,
     enable_autokick = true,
     enable_autoban = false
 }
@@ -53,52 +52,6 @@ local ammo_names = {
     ['rocket'] = true
 }
 
-local protected = {
-    ['reactor'] = true,
-    ['roboport'] = true,
-    ['rocket-silo'] = true,
-    ['solar-panel'] = true,
-    ['generator'] = true,
-    ['splitter'] = true,
-    ['transport-belt'] = true,
-    ['underground-belt'] = true,
-    ['assembling-machine'] = true,
-    ['storage-tank'] = true,
-    ['pump'] = true,
-    ['mining-drill'] = true,
-    ['market'] = true,
-    ['accumulator'] = true,
-    ['ammo-turret'] = true,
-    ['artillery-turret'] = true,
-    ['artillery-wagon'] = true,
-    ['beacon'] = true,
-    ['boiler'] = true,
-    ['burner-generator'] = true,
-    ['car'] = true,
-    ['cargo-wagon'] = true,
-    ['constant-combinator'] = true,
-    ['straight-rail'] = true,
-    ['curved-rail'] = true,
-    ['decider-combinator'] = true,
-    ['electric-pole'] = true,
-    ['electric-turret'] = true,
-    ['fluid-turret'] = true,
-    ['fluid-wagon'] = true,
-    ['furnace'] = true,
-    ['gate'] = true,
-    ['heat-interface'] = true,
-    ['heat-pipe'] = true,
-    ['inserter'] = true,
-    ['lab'] = true,
-    ['lamp'] = true,
-    ['loader'] = true,
-    ['locomotive'] = true,
-    ['logistic-robot'] = true,
-    ['offshore-pump'] = true,
-    ['pipe-to-ground'] = true,
-    ['pipe'] = true
-}
-
 Global.register(
     this,
     function(t)
@@ -704,46 +657,6 @@ local function on_player_cancelled_crafting(event)
     end
 end
 
-local function protect_entities(event)
-    local entity = event.entity
-
-    local function is_protected(e)
-        if protected[e.type] then
-            return true
-        end
-
-        return false
-    end
-
-    if is_protected(entity) then
-        entity.health = entity.health + event.final_damage_amount
-    end
-end
-
---- Protect entities from the player force
----@param event
-local function on_entity_damaged(event)
-    if not this.protect_entities then
-        return
-    end
-
-    local entity = event.entity
-
-    if not entity or not entity.valid then
-        return
-    end
-
-    if event.force.index ~= 1 then
-        return
-    end
-
-    if entity.force.index ~= 1 then
-        return
-    end
-
-    protect_entities(event)
-end
-
 local function on_init()
     local branch_version = '0.18.35'
     local sub = string.sub
@@ -767,14 +680,6 @@ local function on_init()
     end
 end
 
---- Enabling this will protect all entities except for those in the not_protected table.
----@param boolean true/false
-function Public.protect_entities(value)
-    if value then
-        this.protect_entities = value
-    end
-end
-
 --- This will reset the table of this
 function Public.reset_tables()
     this.landfill_history = {}
@@ -833,6 +738,5 @@ Event.add(defines.events.on_player_used_capsule, on_player_used_capsule)
 Event.add(defines.events.on_player_cursor_stack_changed, on_player_cursor_stack_changed)
 Event.add(defines.events.on_player_cancelled_crafting, on_player_cancelled_crafting)
 Event.add(defines.events.on_player_joined_game, on_player_joined_game)
-Event.add(defines.events.on_entity_damaged, on_entity_damaged)
 
 return Public
diff --git a/comfy_panel/player_list.lua b/comfy_panel/player_list.lua
index a5428044..d2716a7b 100644
--- a/comfy_panel/player_list.lua
+++ b/comfy_panel/player_list.lua
@@ -13,7 +13,8 @@ to your scenario control.lua.
 Minor changes by ~~~Gerkiz~~~
 --]]
 local Event = require 'utils.event'
-local play_time = require 'utils.session_data'
+local Session = require 'utils.session_data'
+local Jailed = require 'utils.jail_data'
 local Tabs = require 'comfy_panel.main'
 
 local symbol_asc = '▲'
@@ -218,7 +219,7 @@ local function get_formatted_playtime(x)
 end
 
 local function get_rank(player)
-    local play_table = play_time.get_session_table()
+    local play_table = Session.get_session_table()
     local t = 0
     if play_table then
         if play_table[player.name] then
@@ -337,7 +338,7 @@ local function get_comparator(sort_by)
 end
 
 local function get_sorted_list(sort_by)
-    local play_table = play_time.get_session_table()
+    local play_table = Session.get_session_table()
     local player_list = {}
     for i, player in pairs(game.connected_players) do
         player_list[i] = {}
@@ -369,12 +370,14 @@ local function player_list_show(player, frame, sort_by)
     -- Frame management
     frame.clear()
     frame.style.padding = 8
-    local play_table = play_time.get_trusted_table()
+    local play_table = Session.get_trusted_table()
+    local jailed = Jailed.get_jailed_table()
 
     -- Header management
     local t = frame.add {type = 'table', name = 'player_list_panel_header_table', column_count = 5}
-    local column_widths = {tonumber(40), tonumber(240), tonumber(240), tonumber(150), tonumber(100)}
-    for _, w in ipairs(column_widths) do
+    local column_widths = {tonumber(40), tonumber(218), tonumber(220), tonumber(222), tonumber(50)}
+    local header_column_widths = {tonumber(40), tonumber(210), tonumber(220), tonumber(226), tonumber(50)}
+    for _, w in ipairs(header_column_widths) do
         local label = t.add {type = 'label', caption = ''}
         label.style.minimal_width = w
         label.style.maximal_width = w
@@ -427,14 +430,14 @@ local function player_list_show(player, frame, sort_by)
     header_modifier[sort_by](headers)
 
     for k, v in ipairs(headers) do
-        local label =
+        local header_label =
             t.add {
             type = 'label',
             name = 'player_list_panel_header_' .. k,
             caption = v
         }
-        label.style.font = 'default-bold'
-        label.style.font_color = {r = 0.98, g = 0.66, b = 0.22}
+        header_label.style.font = 'default-bold'
+        header_label.style.font_color = {r = 0.98, g = 0.66, b = 0.22}
     end
 
     -- special style on first header
@@ -476,6 +479,9 @@ local function player_list_show(player, frame, sort_by)
         if game.players[player_list[i].name].admin then
             trusted = '[color=red][A][/color]'
             tooltip = 'This player is an admin of this server.'
+        elseif jailed[player_list[i].name] then
+            trusted = '[color=orange][J][/color]'
+            tooltip = 'This player is currently jailed.'
         elseif play_table[player_list[i].name] then
             trusted = '[color=green][T][/color]'
             tooltip = 'This player is trusted.'
@@ -485,41 +491,42 @@ local function player_list_show(player, frame, sort_by)
         end
 
         -- Name
-        local label =
+        local name_label =
             player_list_panel_table.add {
             type = 'label',
             name = 'player_list_panel_player_names_' .. i,
             caption = player_list[i].name .. ' ' .. trusted,
             tooltip = tooltip
         }
-        label.style.font = 'default'
-        label.style.font_color = {
-            r = .4 + game.players[player_list[i].player_index].color.r * 0.6,
-            g = .4 + game.players[player_list[i].player_index].color.g * 0.6,
-            b = .4 + game.players[player_list[i].player_index].color.b * 0.6
+        local p_color = game.players[player_list[i].player_index]
+        name_label.style.font = 'default'
+        name_label.style.font_color = {
+            r = .4 + p_color.color.r * 0.6,
+            g = .4 + p_color.color.g * 0.6,
+            b = .4 + p_color.color.b * 0.6
         }
-        label.style.minimal_width = column_widths[2]
-        label.style.maximal_width = column_widths[2]
+        name_label.style.minimal_width = column_widths[2]
+        name_label.style.maximal_width = column_widths[2]
 
         -- Total time
-        local label =
+        local total_label =
             player_list_panel_table.add {
             type = 'label',
             name = 'player_list_panel_player_total_time_played_' .. i,
             caption = player_list[i].total_played_time
         }
-        label.style.minimal_width = column_widths[3]
-        label.style.maximal_width = column_widths[3]
+        total_label.style.minimal_width = column_widths[3]
+        total_label.style.maximal_width = column_widths[3]
 
         -- Current time
-        local label =
+        local current_label =
             player_list_panel_table.add {
             type = 'label',
             name = 'player_list_panel_player_time_played_' .. i,
             caption = player_list[i].played_time
         }
-        label.style.minimal_width = column_widths[4]
-        label.style.maximal_width = column_widths[4]
+        current_label.style.minimal_width = column_widths[4]
+        current_label.style.maximal_width = column_widths[4]
 
         -- Poke
         local flow = player_list_panel_table.add {type = 'flow', name = 'button_flow_' .. i, direction = 'horizontal'}
@@ -527,6 +534,7 @@ local function player_list_show(player, frame, sort_by)
         local button =
             flow.add {type = 'button', name = 'poke_player_' .. player_list[i].name, caption = player_list[i].pokes}
         button.style.font = 'default'
+        button.tooltip = 'Poke ' .. player_list[i].name .. ' with a random message!'
         label.style.font_color = {r = 0.83, g = 0.83, b = 0.83}
         button.style.minimal_height = 30
         button.style.minimal_width = 30
diff --git a/maps/fish_defender/commands.lua b/maps/fish_defender/commands.lua
index 439470f6..ad4dce2d 100644
--- a/maps/fish_defender/commands.lua
+++ b/maps/fish_defender/commands.lua
@@ -1,5 +1,6 @@
 local Server = require 'utils.server'
 local FDT = require 'maps.fish_defender.table'
+local Task = require 'utils.task'
 
 local mapkeeper = '[color=blue]Mapkeeper:[/color]'
 
@@ -93,3 +94,55 @@ commands.add_command(
         end
     end
 )
+
+commands.add_command(
+    'set_queue_speed',
+    'Usable only for admins - sets the queue speed of this map!',
+    function(cmd)
+        local p
+        local player = game.player
+        local param = tonumber(cmd.parameter)
+
+        if player then
+            if player ~= nil then
+                p = player.print
+                if not player.admin then
+                    p("[ERROR] You're not admin!", Color.fail)
+                    return
+                end
+                if not param then
+                    return
+                end
+                p('Queue speed set to: ' .. param)
+                Task.set_queue_speed(param)
+            else
+                p = log
+                p('Queue speed set to: ' .. param)
+                Task.set_queue_speed(param)
+            end
+        end
+    end
+)
+
+commands.add_command(
+    'get_queue_speed',
+    'Usable only for admins - gets the queue speed of this map!',
+    function()
+        local p
+        local player = game.player
+
+        if player then
+            if player ~= nil then
+                p = player.print
+                if not player.admin then
+                    p("[ERROR] You're not admin!", Color.fail)
+                    return
+                end
+                p(Task.get_queue_speed())
+            else
+                p = log
+                p(Task.get_queue_speed())
+            end
+        end
+    end
+)
diff --git a/maps/fish_defender/main.lua b/maps/fish_defender/main.lua
index 90fcb96f..050d6820 100644
--- a/maps/fish_defender/main.lua
+++ b/maps/fish_defender/main.lua
@@ -13,6 +13,7 @@ require 'modules.dangerous_goods'
 require 'modules.custom_death_messages'
 
 local Terrain = require 'maps.fish_defender.terrain'
+local Task = require 'utils.task'
 local Unit_health_booster = require 'modules.biter_health_booster'
 local Difficulty = require 'modules.difficulty_vote'
 local Map = require 'modules.map_info'
@@ -40,14 +41,11 @@ local starting_items = {
     ['stone'] = 12
 }
 
-local function shuffle(t)
-    local tbl = {}
-    for i = 1, #t do
-        tbl[i] = t[i]
-    end
-    for i = #tbl, 2, -1 do
-        local j = math.random(i)
-        tbl[i], tbl[j] = tbl[j], tbl[i]
+local function shuffle(tbl)
+    local size = #tbl
+    for i = size, 1, -1 do
+        local rand = math.random(size)
+        tbl[i], tbl[rand] = tbl[rand], tbl[i]
     end
     return tbl
 end
@@ -551,19 +549,30 @@ local function wake_up_the_biters(surface)
     )
 end
 
-local function damage_entity_outside_of_fence(e)
+local function damage_entity_outside_and_inside_of_fence(e)
     if not e.health then
         return
     end
+
     if e.force.name == 'neutral' then
         return
     end
+
     if e.type == 'unit' or e.type == 'unit-spawner' then
         return
     end
 
     e.surface.create_entity({name = 'water-splash', position = e.position})
 
+    if e.type == 'land-mine' then
+        e.health =
+            e.health - math_random(math.floor(e.prototype.max_health * 0.2), math.floor(e.prototype.max_health * 0.4))
+        if e.health <= 0 then
+            e.die('enemy')
+        end
+        return
+    end
+
     if e.type == 'entity-ghost' then
         e.destroy()
         return
@@ -646,8 +655,8 @@ local function biter_attack_wave()
     --	end
     --end
 
-    for _, e in pairs(surface.find_entities_filtered({area = {{160, -256}, {360, 256}}})) do
-        damage_entity_outside_of_fence(e)
+    for _, e in pairs(surface.find_entities_filtered({area = {{110, -256}, {360, 256}}})) do
+        damage_entity_outside_and_inside_of_fence(e)
     end
 
     local y_raffle = get_y_coord_raffle_table()
@@ -1432,6 +1441,9 @@ function Public.reset_game()
 
     Terrain.fish_eye(surface, {x = -2150, y = -300})
 
+    Task.get_task_queue(4)
+    Task.start_queue()
+
     game.map_settings.enemy_expansion.enabled = false
     game.map_settings.enemy_evolution.destroy_factor = 0
     game.map_settings.enemy_evolution.time_factor = 0
diff --git a/maps/fish_defender/table.lua b/maps/fish_defender/table.lua
index 2e051a15..7ae96d72 100644
--- a/maps/fish_defender/table.lua
+++ b/maps/fish_defender/table.lua
@@ -25,6 +25,7 @@ function Public.reset_table()
     this.game_reset = false
     this.spawn_area_generated = false
     this.results_sent = false
+
     this.explosive_bullets_unlocked = false
     this.bouncy_shells_unlocked = false
     this.trapped_capsules_unlocked = false
@@ -49,7 +50,7 @@ function Public.reset_table()
         ['laser-turret'] = {placed = 0, limit = 1, str = 'laser turret', slot_price = 300},
         ['artillery-turret'] = {placed = 0, limit = 1, str = 'artillery turret', slot_price = 500},
         ['flamethrower-turret'] = {placed = 0, limit = 0, str = 'flamethrower turret', slot_price = 50000},
-        ['land-mine'] = {placed = 0, limit = 1, str = 'mine', slot_price = 6}
+        ['land-mine'] = {placed = 0, limit = 1, str = 'mine', slot_price = 20}
     }
     this.difficulties_votes = {
         [1] = {wave_interval = 4500, amount_modifier = 0.52, strength_modifier = 0.40, boss_modifier = 3.0},
diff --git a/maps/fish_defender/terrain.lua b/maps/fish_defender/terrain.lua
index b0ac800b..48d81680 100644
--- a/maps/fish_defender/terrain.lua
+++ b/maps/fish_defender/terrain.lua
@@ -2,10 +2,14 @@ local Event = require 'utils.event'
 local map_functions = require 'tools.map_functions'
 local simplex_noise = require 'utils.simplex_noise'.d2
 local FDT = require 'maps.fish_defender.table'
+local Task = require 'utils.task'
+local Token = require 'utils.token'
 local math_random = math.random
 local math_abs = math.abs
 local math_floor = math.floor
 local math_sqrt = math.sqrt
+local tiles_per_call = 16
+local total_calls = math.ceil(1024 / tiles_per_call)
 
 local Public = {}
 
@@ -21,14 +25,11 @@ local rock_raffle = {
     'rock-huge'
 }
 
-local function shuffle(t)
-    local tbl = {}
-    for i = 1, #t do
-        tbl[i] = t[i]
-    end
-    for i = #tbl, 2, -1 do
-        local j = math.random(i)
-        tbl[i], tbl[j] = tbl[j], tbl[i]
+local function shuffle(tbl)
+    local size = #tbl
+    for i = size, 1, -1 do
+        local rand = math.random(size)
+        tbl[i], tbl[rand] = tbl[rand], tbl[i]
     end
     return tbl
 end
@@ -287,76 +288,6 @@ local function plankton_territory(surface, position, seed)
     return 'water'
 end
 
-local function process_chunk(left_top)
-    local this = FDT.get()
-    local surface = game.surfaces[this.active_surface_index]
-    if not surface or not surface.valid then
-        return
-    end
-
-    local seed = game.surfaces[1].map_gen_settings.seed
-
-    Public.generate_spawn_area(this, surface, left_top)
-    enemy_territory(surface, left_top)
-    fish_mouth(surface, left_top)
-
-    local tiles = {}
-
-    for x = 0, 31, 1 do
-        for y = 0, 31, 1 do
-            local pos = {x = left_top.x + x, y = left_top.y + y}
-            if is_out_of_map_tile(pos) then
-                --if not plankton_territory(surface, pos, seed) then surface.set_tiles({{name = "out-of-map", position = pos}}, true) end
-                local tile_to_set = plankton_territory(surface, pos, seed)
-                --local tile_to_set = "out-of-map"
-                tiles[#tiles + 1] = {name = tile_to_set, position = pos}
-            end
-        end
-    end
-
-    surface.set_tiles(tiles, true)
-
-    --if game.tick == 0 then return end
-    --if game.forces.player.is_chunk_charted(surface, {left_top.x / 32, left_top.y / 32}) then
-    game.forces.player.chart(surface, {{left_top.x, left_top.y}, {left_top.x + 31, left_top.y + 31}})
-    --end
-    if this.market and this.market.valid then
-        this.game_reset = false
-    end
-end
-
-local function process_chunk_queue()
-    local chunks = #global.chunk_queue
-    if chunks <= 0 then
-        return
-    end
-
-    for k, left_top in pairs(global.chunk_queue) do
-        process_chunk(left_top)
-        global.chunk_queue[k] = nil
-        return
-    end
-end
-
-local function on_chunk_generated(event)
-    local map_name = 'fish_defender'
-
-    if string.sub(event.surface.name, 0, #map_name) ~= map_name then
-        return
-    end
-    local left_top = event.area.left_top
-    local this = FDT.get()
-    if this.game_has_ended then
-        return
-    end
-
-    if game.tick == 0 or this.game_reset or this.force_chunk then
-        process_chunk(left_top)
-    else
-        global.chunk_queue[#global.chunk_queue + 1] = {x = left_top.x, y = left_top.y}
-    end
-end
-
 local function render_market_hp()
     local this = FDT.get()
     local surface = game.surfaces[this.active_surface_index]
@@ -391,7 +322,7 @@ local function render_market_hp()
     }
 end
 
-function Public.generate_spawn_area(this, surface)
+local function generate_spawn_area(this, surface)
     if this.spawn_area_generated then
         return
     end
@@ -541,6 +472,81 @@ function Public.generate_spawn_area(this, surface)
     this.spawn_area_generated = true
 end
 
+local function process_chunk(left_top)
+    local this = FDT.get()
+    local surface = game.surfaces[this.active_surface_index]
+    if not surface or not surface.valid then
+        return
+    end
+
+    local seed = game.surfaces[1].map_gen_settings.seed
+
+    generate_spawn_area(this, surface, left_top)
+    enemy_territory(surface, left_top)
+    fish_mouth(surface, left_top)
+
+    local tiles = {}
+
+    for x = 0, 31, 1 do
+        for y = 0, 31, 1 do
+            local pos = {x = left_top.x + x, y = left_top.y + y}
+            if is_out_of_map_tile(pos) then
+                --if not plankton_territory(surface, pos, seed) then surface.set_tiles({{name = "out-of-map", position = pos}}, true) end
+                local tile_to_set = plankton_territory(surface, pos, seed)
+                --local tile_to_set = "out-of-map"
+                tiles[#tiles + 1] = {name = tile_to_set, position = pos}
+            end
+        end
+    end
+
+    surface.set_tiles(tiles, true)
+
+    --if game.tick == 0 then return end
+    --if game.forces.player.is_chunk_charted(surface, {left_top.x / 32, left_top.y / 32}) then
+    game.forces.player.chart(surface, {{left_top.x, left_top.y}, {left_top.x + 31, left_top.y + 31}})
+    --end
+    if this.market and this.market.valid then
+        this.game_reset = false
+    end
+end
+
+local process_chunk_queue =
+    Token.register(
+    function(data)
+        local chunk_queue = data.chunk_queue
+
+        for i = 1, #chunk_queue do
+            local pos = {x = chunk_queue[i].x, y = chunk_queue[i].y}
+            process_chunk(pos)
+            chunk_queue[i] = nil
+        end
+    end
+)
+
+local function on_chunk_generated(event)
+    local map_name = 'fish_defender'
+
+    if string.sub(event.surface.name, 0, #map_name) ~= map_name then
+        return
+    end
+    local left_top = event.area.left_top
+    local this = FDT.get()
+    if this.game_has_ended then
+        return
+    end
+
+    if game.tick == 0 or this.game_reset or this.force_chunk then
+        process_chunk(left_top)
+    else
+        global.chunk_queue[#global.chunk_queue + 1] = {x = left_top.x, y = left_top.y}
+
+        local data = {
+            chunk_queue = global.chunk_queue
+        }
+        Task.set_timeout_in_ticks(total_calls, process_chunk_queue, data)
+    end
+end
+
 function Public.fish_eye(surface, position)
     surface.request_to_generate_chunks(position, 2)
     surface.force_generate_chunk_requests()
@@ -567,7 +573,6 @@ function Public.fish_eye(surface, position)
     end
 end
 
-Event.on_nth_tick(25, process_chunk_queue)
 Event.add(defines.events.on_chunk_generated, on_chunk_generated)
 
 return Public
diff --git a/maps/mountain_fortress_v3/main.lua b/maps/mountain_fortress_v3/main.lua
index e04ca01d..3170be18 100644
--- a/maps/mountain_fortress_v3/main.lua
+++ b/maps/mountain_fortress_v3/main.lua
@@ -265,7 +265,7 @@ function Public.reset_map()
     Collapse.set_direction('north')
     Collapse.start_now(false)
 
-    --[[ local x_value = rng(15, 25)
+    local x_value = rng(15, 25)
     local y_value = rng(50, 60)
 
     local data = {
@@ -274,7 +274,7 @@ function Public.reset_map()
         ['fluid-wagon'] = {left_top = {x = -x_value, y = 0}, right_bottom = {x = x_value, y = y_value}},
         ['locomotive'] = {left_top = {x = -x_value, y = 0}, right_bottom = {x = x_value, y = y_value}}
     }
-    ICT.set_wagon_area(data) ]]
+    ICT.set_wagon_area(data)
     this.locomotive_health = 10000
     this.locomotive_max_health = 10000