1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2024-12-30 23:17:53 +02:00

desync fixes

This commit is contained in:
Gerkiz 2020-07-10 13:52:55 +02:00
parent 78cba7f311
commit 473f75f7a6
4 changed files with 102 additions and 88 deletions

View File

@ -7,6 +7,14 @@ local AntiGrief = require 'antigrief'
local lower = string.lower
local function admin_only_message(str)
for _, player in pairs(game.connected_players) do
if player.admin == true then
player.print('Admins-only-message: ' .. str, {r = 0.88, g = 0.88, b = 0.88})
end
end
end
local function jail(player, source_player)
if player.name == source_player.name then
return player.print("You can't select yourself!", {r = 1, g = 0.5, b = 0.1})

View File

@ -40,17 +40,20 @@ local starting_items = {
['stone'] = 12
}
local biter_count_limit = 1024 --maximum biters on the east side of the map, next wave will be delayed if the maximum has been reached
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]
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]
end
return tbl
end
local biter_count_limit = 1024 --maximum biters on the east side of the map, next wave will be delayed if the maximum has been reached
local function create_wave_gui(player)
if player.gui.top['fish_defense_waves'] then
player.gui.top['fish_defense_waves'].destroy()
@ -362,7 +365,7 @@ local function get_y_coord_raffle_table()
for y = -96, 96, 8 do
t[#t + 1] = y
end
table.shuffle_table(t)
shuffle(t)
return t
end
@ -662,7 +665,7 @@ local function biter_attack_wave()
local biter_pool = get_biter_pool()
--local spawners = surface.find_entities_filtered({type = "unit-spawner", area = {{160, -196},{512, 196}}})
--table.shuffle_table(spawners)
--shuffle(spawners)
while this.attack_wave_threat > 0 do
for i = 1, #unit_groups, 1 do
@ -751,6 +754,10 @@ end
local function is_game_lost()
local this = FDT.get()
if not this.game_has_ended then
return
end
for _, player in pairs(game.connected_players) do
if player.gui.left['fish_defense_game_lost'] then
return
@ -774,22 +781,24 @@ local function is_game_lost()
local market_age_label
if this.market_age >= 216000 then
market_age_label =
t.add(
{
type = 'label',
caption = math.floor(((this.market_age / 60) / 60) / 60) ..
' hours ' .. math.ceil((this.market_age % 216000 / 60) / 60) .. ' minutes'
}
)
market_age_label.style.font = 'default-bold'
market_age_label.style.font_color = {r = 0.33, g = 0.66, b = 0.9}
else
market_age_label =
t.add({type = 'label', caption = math.ceil((this.market_age % 216000 / 60) / 60) .. ' minutes'})
market_age_label.style.font = 'default-bold'
market_age_label.style.font_color = {r = 0.33, g = 0.66, b = 0.9}
if this.market_age then
if this.market_age >= 216000 then
market_age_label =
t.add(
{
type = 'label',
caption = math.floor(((this.market_age / 60) / 60) / 60) ..
' hours ' .. math.ceil((this.market_age % 216000 / 60) / 60) .. ' minutes'
}
)
market_age_label.style.font = 'default-bold'
market_age_label.style.font_color = {r = 0.33, g = 0.66, b = 0.9}
else
market_age_label =
t.add({type = 'label', caption = math.ceil((this.market_age % 216000 / 60) / 60) .. ' minutes'})
market_age_label.style.font = 'default-bold'
market_age_label.style.font_color = {r = 0.33, g = 0.66, b = 0.9}
end
end
local mvp = get_mvps()
@ -1230,6 +1239,7 @@ local function set_objective_health(final_damage_amount)
this.market.die()
this.market = nil
this.market_age = game.tick - this.last_reset
this.game_has_ended = true
is_game_lost()
return
end
@ -1338,7 +1348,6 @@ local function has_the_game_ended()
if this.game_restart_timer % 1800 == 0 then
if this.game_restart_timer > 0 then
this.game_reset = true
this.game_has_ended = true
game.print(
'Game will ' .. cause_msg .. ' in ' .. this.game_restart_timer / 60 .. ' seconds!',
{r = 0.22, g = 0.88, b = 0.22}
@ -1346,7 +1355,7 @@ local function has_the_game_ended()
end
if this.soft_reset and this.game_restart_timer == 0 then
this.game_reset_tick = nil
Public.on_init()
Public.reset_game()
return
end
if this.restart and this.game_restart_timer == 0 then
@ -1373,7 +1382,7 @@ local function has_the_game_ended()
end
end
function Public.on_init()
function Public.reset_game()
FDT.reset_table()
Poll.reset()
local this = FDT.get()
@ -1387,9 +1396,13 @@ function Public.on_init()
for i = 1, #players do
local player = players[i]
Score.init_player_table(player)
if player.gui.left['fish_defense_game_lost'] then
player.gui.left['fish_defense_game_lost'].destroy()
end
end
local map_gen_settings = {}
map_gen_settings.seed = math_random(10000, 99999)
map_gen_settings.height = 2048
map_gen_settings.water = 0.10
map_gen_settings.terrain_segmentation = 3
@ -1407,11 +1420,9 @@ function Public.on_init()
if not this.active_surface_index then
this.active_surface_index = game.create_surface('fish_defender', map_gen_settings).index
this.active_surface = game.surfaces[this.active_surface_index]
else
this.active_surface_index =
Reset.soft_reset_map(game.surfaces[this.active_surface_index], map_gen_settings, starting_items).index
this.active_surface = game.surfaces[this.active_surface_index]
end
local surface = game.surfaces[this.active_surface_index]
@ -1435,13 +1446,12 @@ function Public.on_init()
if not game.forces.decoratives then
game.create_force('decoratives')
end
game.forces['decoratives'].set_cease_fire('enemy', true)
game.forces['enemy'].set_cease_fire('decoratives', true)
game.forces['player'].set_cease_fire('decoratives', true)
game.remove_offline_players()
Terrain.fish_eye(surface, {x = -2150, y = -300})
game.map_settings.enemy_expansion.enabled = false
game.forces['player'].technologies['artillery'].researched = false
@ -1453,18 +1463,16 @@ function Public.on_init()
this.market_health = 500
this.market_max_health = 500
end
function Public.on_init()
Public.reset_game()
local T = Map.Pop_info()
T.localised_category = 'fish_defender'
T.main_caption_color = {r = 0.11, g = 0.8, b = 0.44}
T.sub_caption_color = {r = 0.33, g = 0.66, b = 0.9}
for _, player in pairs(game.connected_players) do
if player.gui.left['fish_defense_game_lost'] then
player.gui.left['fish_defense_game_lost'].destroy()
end
end
local mgs = game.surfaces['nauvis'].map_gen_settings
mgs.width = 16
mgs.height = 16
@ -1525,7 +1533,7 @@ Event.add(defines.events.on_robot_mined_entity, on_robot_mined_entity)
Event.add(defines.events.on_tick, on_tick)
Event.on_init(on_init)
--[[ local gmeta = getmetatable(_ENV)
local gmeta = getmetatable(_ENV)
if not gmeta then
gmeta = {}
setmetatable(_ENV, gmeta)
@ -1536,5 +1544,6 @@ gmeta.__newindex = function(_, n, v)
end
gmeta.__index = function(_, n)
return global[n]
end ]]
end
return Public

View File

@ -19,6 +19,7 @@ function Public.reset_table()
this.restart = false
this.shutdown = false
this.announced_message = false
this.force_chunk = false
-- @end
this.game_has_ended = false
this.game_reset = false
@ -40,6 +41,7 @@ function Public.reset_table()
this.last_reset = game.tick
this.wave_interval = 3600
this.wave_grace_period = game.tick + 3600 * 20
-- this.wave_grace_period = game.tick + 3600
this.boss_biters = {}
this.acid_lines_delay = {}
this.entity_limits = {

View File

@ -21,10 +21,22 @@ 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]
end
return tbl
end
local function get_replacement_tile(surface, position)
for i = 1, 128, 1 do
local vectors = {{0, i}, {0, i * -1}, {i, 0}, {i * -1, 0}}
table.shuffle_table(vectors)
shuffle(vectors)
for k, v in pairs(vectors) do
local tile = surface.get_tile(position.x + v[1], position.y + v[2])
if not tile.collides_with('resource-layer') then
@ -132,9 +144,6 @@ local function place_fish_market(surface, position)
end
local function enemy_territory(surface, left_top)
--surface.request_to_generate_chunks({x = 256, y = 0}, 16)
--surface.force_generate_chunk_requests()
if left_top.x < 160 then
return
end
@ -150,12 +159,6 @@ local function enemy_territory(surface, left_top)
local area = {{left_top.x, left_top.y}, {left_top.x + 32, left_top.y + 32}}
--local area = {{160, -512},{750, 512}}
--for _, tile in pairs(surface.find_tiles_filtered({area = area})) do
-- if is_enemy_territory(tile.position) then
-- surface.set_tiles({{name = "water-mud", position = {tile.position.x, tile.position.y}}}, true)
-- end
--end
if left_top.x > 256 then
for x = 0, 31, 1 do
for y = 0, 31, 1 do
@ -179,15 +182,6 @@ local function enemy_territory(surface, left_top)
entity.destructible = false
end
end
--if pos.x % 32 == 0 and pos.y % 32 == 0 then
-- local decorative_names = {}
-- for k,v in pairs(game.decorative_prototypes) do
-- if v.autoplace_specification then
-- decorative_names[#decorative_names+1] = k
-- end
-- end
-- surface.regenerate_decorative(decorative_names, {{x=math_floor(pos.x/32),y=math_floor(pos.y/32)}})
--end
end
end
end
@ -296,18 +290,13 @@ 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 market = FDT.get('market')
if not surface or not surface.valid then
return
end
local seed = game.surfaces[1].map_gen_settings.seed
Public.generate_spawn_area(surface)
Public.generate_spawn_area(this, surface, left_top)
enemy_territory(surface, left_top)
fish_mouth(surface, left_top)
@ -320,7 +309,7 @@ local function process_chunk(left_top)
--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"
table.insert(tiles, {name = tile_to_set, position = pos})
tiles[#tiles + 1] = {name = tile_to_set, position = pos}
end
end
end
@ -331,12 +320,16 @@ local function process_chunk(left_top)
--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 market and market.valid then
FDT.get().game_reset = false
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
@ -351,15 +344,14 @@ local function on_chunk_generated(event)
return
end
local left_top = event.area.left_top
local game_reset = FDT.get('game_reset')
local game_has_ended = FDT.get('game_has_ended')
local this = FDT.get()
if this.game_has_ended then
return
end
if game.tick == 0 or game_reset then
if game.tick == 0 or this.game_reset or this.force_chunk then
process_chunk(left_top)
else
if game_has_ended then
return
end
global.chunk_queue[#global.chunk_queue + 1] = {x = left_top.x, y = left_top.y}
end
end
@ -398,21 +390,21 @@ local function render_market_hp()
}
end
function Public.generate_spawn_area(surface)
function Public.generate_spawn_area(this, surface)
if this.spawn_area_generated then
return
end
surface.request_to_generate_chunks({x = 0, y = 0}, 7)
surface.request_to_generate_chunks({x = 160, y = 0}, 4)
--surface.force_generate_chunk_requests()
local spawn_area_generated = FDT.get('spawn_area_generated')
if spawn_area_generated then
return
end
if not surface.is_chunk_generated({-7, 0}) then
return
end
if not surface.is_chunk_generated({5, 0}) then
return
end
FDT.get().spawn_area_generated = true
local spawn_position_x = -128
@ -475,7 +467,7 @@ function Public.generate_spawn_area(surface)
{x = spawn_position_x - 52, y = y * -0.5},
{x = spawn_position_x - 52, y = y * -1}
}
table.shuffle_table(ore_positions)
shuffle(ore_positions)
map_functions.draw_smoothed_out_ore_circle(ore_positions[1], 'copper-ore', surface, 15, 2500)
map_functions.draw_smoothed_out_ore_circle(ore_positions[2], 'iron-ore', surface, 15, 2500)
map_functions.draw_smoothed_out_ore_circle(ore_positions[3], 'coal', surface, 15, 1500)
@ -484,25 +476,28 @@ function Public.generate_spawn_area(surface)
map_functions.draw_oil_circle(ore_positions[5], 'crude-oil', surface, 8, 200000)
local pos = surface.find_non_colliding_position('market', {spawn_position_x, 0}, 50, 1)
FDT.get().market = place_fish_market(surface, pos)
local market = FDT.get('market')
this.market = place_fish_market(surface, pos)
render_market_hp()
Public.fish_eye(surface, {x = -2150, y = -300})
local r = 16
for _, entity in pairs(
surface.find_entities_filtered(
{
area = {
{market.position.x - r, market.position.y - r},
{market.position.x + r, market.position.y + r}
{this.market.position.x - r, this.market.position.y - r},
{this.market.position.x + r, this.market.position.y + r}
},
type = 'tree'
}
)
) do
local distance_to_center =
math_sqrt((entity.position.x - market.position.x) ^ 2 + (entity.position.y - market.position.y) ^ 2)
math_sqrt(
(entity.position.x - this.market.position.x) ^ 2 + (entity.position.y - this.market.position.y) ^ 2
)
if distance_to_center < r then
if math_random(1, r) > distance_to_center then
entity.destroy()
@ -516,7 +511,7 @@ function Public.generate_spawn_area(surface)
for x = -20, 20, 1 do
for y = -20, 20, 1 do
local pos = {x = market.position.x + x, y = market.position.y + y}
local pos = {x = this.market.position.x + x, y = this.market.position.y + y}
--local distance_to_center = math_sqrt(x^2 + y^2)
--if distance_to_center > 8 and distance_to_center < 15 then
local distance_to_center = x ^ 2 + y ^ 2
@ -544,11 +539,11 @@ function Public.generate_spawn_area(surface)
local pos = surface.find_non_colliding_position('character', {spawn_position_x + 1, 4}, 50, 1)
player.teleport(pos, surface)
end
this.spawn_area_generated = true
end
function Public.fish_eye(surface, position)
surface.request_to_generate_chunks(position, 2)
surface.force_generate_chunk_requests()
for x = -48, 48, 1 do
for y = -48, 48, 1 do
local p = {x = position.x + x, y = position.y + y}