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

lag feista and tweaks

This commit is contained in:
Gerkiz 2020-11-04 17:14:30 +01:00
parent 221def8ef2
commit a76fafa944
9 changed files with 333 additions and 316 deletions

View File

@ -70,7 +70,7 @@ commands.add_command(
local function on_gui_click(event)
local player = game.players[event.player_index]
if not event.element.valid then
if not (event.element and event.element.valid) then
return
end

View File

@ -148,16 +148,42 @@ local function on_entity_removed(data)
end
end
local function check_health(this)
local m = this.locomotive_health / this.locomotive_max_health
if this.carriages then
for i = 1, #this.carriages do
local entity = this.carriages[i]
if not (entity and entity.valid) then
return
end
if entity.type == 'locomotive' then
entity.health = 1000 * m
else
entity.health = 600 * m
end
end
end
end
local function check_health_final_damage(this, final_damage_amount)
if this.carriages then
for i = 1, #this.carriages do
local entity = this.carriages[i]
if not (entity and entity.valid) then
return
end
entity.health = entity.health + final_damage_amount
end
end
end
local function set_objective_health(final_damage_amount)
local this = WPT.get()
if final_damage_amount == 0 then
return
end
if not this.locomotive then
return
end
if not this.locomotive.valid then
if not (this.locomotive and this.locomotive.valid) then
return
end
@ -178,7 +204,7 @@ local function set_objective_health(final_damage_amount)
end
if this.locomotive_health <= 0 then
this.locomotive.health = this.locomotive.health + final_damage_amount
check_health_final_damage(this, final_damage_amount)
return
end
@ -191,8 +217,7 @@ local function set_objective_health(final_damage_amount)
Public.loco_died()
end
local m = this.locomotive_health / this.locomotive_max_health
this.locomotive.health = 1000 * m
check_health(this)
rendering.set_text(this.health_text, 'HP: ' .. this.locomotive_health .. ' / ' .. this.locomotive_max_health)
end
@ -217,9 +242,23 @@ local function protect_entities(event)
return false
end
local function exists()
local t = {}
for i = 1, #this.carriages do
local e = this.carriages[i]
if not (e and e.valid) then
return
end
t[e.unit_number] = true
end
return t
end
local units = exists()
if is_protected(entity) then
if event.cause and event.cause.valid then
if event.cause.force.index == 2 and entity.unit_number == this.locomotive.unit_number then
if event.cause.force.index == 2 and units[entity.unit_number] then
set_objective_health(event.final_damage_amount)
elseif event.cause.force.index == 2 then
return
@ -991,8 +1030,7 @@ function Public.loco_died()
game.forces.enemy.set_friend('player', true)
game.forces.player.set_friend('enemy', true)
local fake_shooter =
surface.create_entity({name = 'character', position = this.locomotive.position, force = 'enemy'})
local fake_shooter = surface.create_entity({name = 'character', position = this.locomotive.position, force = 'enemy'})
surface.create_entity(
{
name = 'atomic-rocket',

View File

@ -367,8 +367,7 @@ Public.disable_minable_and_ICW_callback =
function(entity)
if entity and entity.valid then
entity.minable = false
local wagon = ICW.register_wagon(entity, true)
wagon.entity_count = 999
ICW.register_wagon(entity, true)
end
end
)
@ -454,8 +453,7 @@ Public.power_source_callback =
local power_sources = this.power_sources
local callback_data = data.callback_data
local power_source =
turret.surface.create_entity {name = 'hidden-electric-energy-interface', position = turret.position}
local power_source = turret.surface.create_entity {name = 'hidden-electric-energy-interface', position = turret.position}
power_source.electric_buffer_size = callback_data.buffer_size
power_source.power_production = callback_data.power_production
power_source.destructible = false

View File

@ -397,15 +397,17 @@ Gui.on_click(
discard_button_name,
function(event)
local player = event.player
if not player or not player.valid or not player.character then
return
end
local screen = player.gui.screen
local frame = screen[main_frame_name]
if not frame or not frame.valid then
return
end
local player_frame = frame[draw_add_player_frame_name]
if not player or not player.valid or not player.character then
return
end
if player_frame and player_frame.valid then
player_frame.destroy()
end
@ -416,12 +418,13 @@ Gui.on_click(
main_toolbar_name,
function(event)
local player = event.player
local screen = player.gui.screen
local frame = screen[main_frame_name]
if not player or not player.valid or not player.character then
return
end
local screen = player.gui.screen
local frame = screen[main_frame_name]
if frame and frame.valid then
frame.destroy()
else
@ -439,12 +442,13 @@ Event.add(
defines.events.on_gui_closed,
function(event)
local player = game.get_player(event.player_index)
local screen = player.gui.screen
local frame = screen[main_frame_name]
if not player or not player.valid or not player.character then
return
end
local screen = player.gui.screen
local frame = screen[main_frame_name]
if frame and frame.valid then
frame.destroy()
end

View File

@ -1,7 +1,7 @@
local Public = {}
local ICW = require 'maps.mountain_fortress_v3.icw.table'
local random = math.random
local WPT = require 'maps.mountain_fortress_v3.table'
local main_tile_name = 'black-refined-concrete'
function Public.request_reconstruction(icw)
@ -37,13 +37,7 @@ local function kick_players_from_surface(wagon)
for _, e in pairs(wagon.surface.find_entities_filtered({area = wagon.area})) do
if validate_entity(e) and e.name == 'character' and e.player then
e.player.teleport(
main_surface.find_non_colliding_position(
'character',
game.forces.player.get_spawn_position(main_surface),
3,
0,
5
),
main_surface.find_non_colliding_position('character', game.forces.player.get_spawn_position(main_surface), 3, 0, 5),
main_surface
)
end
@ -130,8 +124,7 @@ local function equal_fluid(source_tank, target_tank)
return
end
local inserted_amount =
target_tank.insert_fluid({name = source_fluid.name, amount = amount, temperature = source_fluid.temperature})
local inserted_amount = target_tank.insert_fluid({name = source_fluid.name, amount = amount, temperature = source_fluid.temperature})
if inserted_amount > 0 then
source_tank.remove_fluid({name = source_fluid.name, amount = inserted_amount})
end
@ -174,18 +167,18 @@ end
local function input_cargo(wagon, chest)
if not chest.request_from_buffers then
return
goto continue
end
local wagon_entity = wagon.entity
if not validate_entity(wagon_entity) then
wagon.transfer_entities = nil
return
goto continue
end
local wagon_inventory = wagon_entity.get_inventory(defines.inventory.cargo_wagon)
if wagon_inventory.is_empty() then
return
goto continue
end
local chest_inventory = chest.get_inventory(defines.inventory.chest)
@ -198,12 +191,12 @@ local function input_cargo(wagon, chest)
if chest.get_request_slot(1) then
input_filtered(wagon_inventory, chest, chest_inventory, free_slots)
return
goto continue
end
for i = 1, wagon_inventory.get_bar() - 1, 1 do
if free_slots <= 0 then
return
goto continue
end
if wagon_inventory[i].valid_for_read then
chest_inventory.insert(wagon_inventory[i])
@ -211,15 +204,17 @@ local function input_cargo(wagon, chest)
free_slots = free_slots - 1
end
end
::continue::
end
local function output_cargo(wagon, passive_chest)
if not validate_entity(wagon.entity) then
return
goto continue
end
if not passive_chest.valid then
return
goto continue
end
local chest1 = passive_chest.get_inventory(defines.inventory.chest)
local chest2 = wagon.entity.get_inventory(defines.inventory.cargo_wagon)
@ -232,6 +227,7 @@ local function output_cargo(wagon, passive_chest)
end
end
end
::continue::
end
local transfer_functions = {
@ -257,10 +253,7 @@ local function get_wagon_for_entity(icw, entity)
if wagon then
local left_top = wagon.area.left_top
local right_bottom = wagon.area.right_bottom
if
position.x >= left_top.x and position.y >= left_top.y and position.x <= right_bottom.x and
position.y <= right_bottom.y
then
if position.x >= left_top.x and position.y >= left_top.y and position.x <= right_bottom.x and position.y <= right_bottom.y then
return wagon
end
end
@ -429,13 +422,6 @@ function Public.create_wagon_room(icw, wagon)
end
end
--[[ for x = area.left_top.x, area.right_bottom.x - 1, 1 do
for y = area.left_top.y + 2, area.right_bottom.y - 3, 1 do
if random(1, 16) == 1 then
fishes[#fishes + 1] = {name = crash_site[random(1, size_of_crash)], position = {x, y}, force = 'player'}
end
end
end ]]
surface.set_tiles(tiles, true)
for _, fish in pairs(fishes) do
@ -633,8 +619,7 @@ function Public.create_wagon(icw, created_entity, delay_surface)
left_top = {x = wagon_area.left_top.x, y = wagon_area.left_top.y},
right_bottom = {x = wagon_area.right_bottom.x, y = wagon_area.right_bottom.y}
},
doors = {},
entity_count = 0
doors = {}
}
local wagon = icw.wagons[created_entity.unit_number]
@ -647,27 +632,6 @@ function Public.create_wagon(icw, created_entity, delay_surface)
return wagon
end
function Public.add_wagon_entity_count(icw, added_entity)
local wagon = get_wagon_for_entity(icw, added_entity)
if not wagon then
return
end
wagon.entity_count = wagon.entity_count + 1
wagon.entity.minable = false
end
function Public.subtract_wagon_entity_count(icw, removed_entity)
local wagon = get_wagon_for_entity(icw, removed_entity)
if not wagon then
return
end
wagon.entity_count = wagon.entity_count - 1
if wagon.entity_count > 0 then
return
end
wagon.entity.minable = true
end
function Public.use_cargo_wagon_door_with_entity(icw, player, door)
local player_data = get_player_data(icw, player)
if player_data.state then
@ -703,6 +667,9 @@ function Public.use_cargo_wagon_door_with_entity(icw, player, door)
if wagon.entity.surface.name ~= player.surface.name then
local surface = wagon.entity.surface
if not (surface and surface.valid) then
return
end
local x_vector = (door.position.x / math.abs(door.position.x)) * 2
local position = {wagon.entity.position.x + x_vector, wagon.entity.position.y}
local surface_position = surface.find_non_colliding_position('character', position, 128, 0.5)
@ -730,6 +697,9 @@ function Public.use_cargo_wagon_door_with_entity(icw, player, door)
player_data.surface = surface.index
else
local surface = wagon.surface
if not (surface and surface.valid) then
return
end
local area = wagon.area
local x_vector = door.position.x - player.position.x
local position
@ -816,9 +786,8 @@ local function move_room_to_train(icw, train, wagon)
end
end
local function get_connected_rolling_stock(entity, direction)
local function get_connected_rolling_stock(entity, direction, carriages)
--thanks Boskid
local carriages = entity.train.carriages
local first_stock, second_stock
for k, v in pairs(carriages) do
if v == entity then
@ -835,8 +804,7 @@ local function get_connected_rolling_stock(entity, direction)
end
local angle =
math.atan2(-(entity.position.x - first_stock.position.x), entity.position.y - first_stock.position.y) /
(2 * math.pi) -
math.atan2(-(entity.position.x - first_stock.position.x), entity.position.y - first_stock.position.y) / (2 * math.pi) -
entity.orientation
if direction == defines.rail_direction.back then
angle = angle + 0.5
@ -858,8 +826,7 @@ local function get_connected_rolling_stock(entity, direction)
end
angle =
math.atan2(-(connected_stock.position.x - entity.position.x), connected_stock.position.y - entity.position.y) /
(2 * math.pi) -
math.atan2(-(connected_stock.position.x - entity.position.x), connected_stock.position.y - entity.position.y) / (2 * math.pi) -
connected_stock.orientation
while angle < -0.5 do
angle = angle + 1
@ -876,17 +843,16 @@ local function get_connected_rolling_stock(entity, direction)
return connected_stock, joint_of_connected_stock
end
function Public.construct_train(icw, carriages)
local WPT = package.loaded['maps.mountain_fortress_v3.table']
local choochoo = WPT.get().locomotive
for i, carriage in pairs(carriages) do
if carriage == choochoo then
local stock = get_connected_rolling_stock(choochoo, defines.rail_direction.front)
if stock ~= carriages[i - 1] then
function Public.construct_train(icw, locomotive, carriages)
local locomotive_carriages = locomotive.train.carriages
for i, carriage in pairs(locomotive_carriages) do
if carriage == locomotive then
local stock = get_connected_rolling_stock(locomotive, defines.rail_direction.front, locomotive_carriages)
if stock ~= locomotive_carriages[i - 1] then
local n = 1
local m = #carriages
local m = #locomotive_carriages
while (n < m) do
carriages[n], carriages[m] = carriages[m], carriages[n]
locomotive_carriages[n], locomotive_carriages[m] = locomotive_carriages[m], locomotive_carriages[n]
n = n + 1
m = m - 1
end
@ -917,13 +883,21 @@ function Public.reconstruct_all_trains(icw)
return
end
local locomotive = WPT.get('locomotive')
if not (locomotive and locomotive.valid) then
return
end
if not wagon.surface then
wagon.surface = Public.create_room_surface(icw, unit_number)
Public.create_wagon_room(icw, wagon)
end
local carriages = wagon.entity.train.carriages
Public.construct_train(icw, carriages)
local locomotive_carriages = locomotive.train.carriages
WPT.set().carriages = locomotive_carriages
Public.construct_train(icw, locomotive, carriages)
end
delete_empty_surfaces(icw)
end
@ -935,17 +909,21 @@ function Public.item_transfer(icw)
end
if wagon.transfer_entities then
for k, e in pairs(wagon.transfer_entities) do
transfer_functions[e.name](wagon, e)
if validate_entity(e) then
transfer_functions[e.name](wagon, e)
end
end
end
end
end
function Public.draw_minimap(icw, player, surface, position)
if not (surface and surface.valid) then
return
end
local frame = player.gui.left.icw_main_frame
if not frame then
frame =
player.gui.left.add({type = 'frame', direction = 'vertical', name = 'icw_main_frame', caption = 'Minimap'})
frame = player.gui.left.add({type = 'frame', direction = 'vertical', name = 'icw_main_frame', caption = 'Minimap'})
end
local element = frame['icw_sub_frame']
if not element then

View File

@ -17,7 +17,6 @@ local function on_entity_died(event)
return
end
local icw = ICW.get()
Functions.subtract_wagon_entity_count(icw, entity)
Functions.kill_wagon(icw, entity)
end
@ -132,5 +131,13 @@ Event.add(defines.events.on_player_died, on_player_died)
Event.add(defines.events.on_gui_click, on_gui_click)
Event.add(defines.events.on_gui_closed, on_gui_closed)
Event.add(defines.events.on_gui_opened, on_gui_opened)
Event.add(
defines.events.on_built_entity,
function(event)
local created_entity = event.created_entity
local icw = ICW.get()
Functions.create_wagon(icw, created_entity)
end
)
return Public

View File

@ -149,9 +149,7 @@ local set_loco_tiles =
end
if surface.can_place_entity({name = 'wooden-chest', position = p[i]}) then
local e =
surface.create_entity(
{name = 'wooden-chest', position = p[i], force = 'player', create_build_effect_smoke = false}
)
surface.create_entity({name = 'wooden-chest', position = p[i], force = 'player', create_build_effect_smoke = false})
local inventory = e.get_inventory(defines.inventory.chest)
inventory.insert(cargo_boxes[i])
end
@ -321,10 +319,7 @@ local function property_boost(data)
return
end
if player.afk_time < 200 then
if
Math2D.bounding_box.contains_point(area, player.position) or
player.surface.index == locomotive_surface.index
then
if Math2D.bounding_box.contains_point(area, player.position) or player.surface.index == locomotive_surface.index then
Public.add_player_to_permission_group(player, 'locomotive')
local pos = player.position
Functions.gain_xp(player, 0.5 * (rpg[player.index].bonus + this.xp_points))
@ -437,15 +432,28 @@ local function set_locomotive_health()
return
end
if not this.locomotive.valid then
local function check_health()
local m = this.locomotive_health / this.locomotive_max_health
if this.carriages then
for i = 1, #this.carriages do
local entity = this.carriages[i]
if not (entity and entity.valid) then
return
end
if entity.type == 'locomotive' then
entity.health = 1000 * m
else
entity.health = 600 * m
end
end
end
end
if not (this.locomotive and this.locomotive.valid) then
return
end
local locomotive_health = WPT.get('locomotive_health')
local locomotive_max_health = WPT.get('locomotive_max_health')
local m = locomotive_health / locomotive_max_health
this.locomotive.health = 1000 * m
rendering.set_text(this.health_text, 'HP: ' .. locomotive_health .. ' / ' .. locomotive_max_health)
check_health()
end
local function validate_index()
@ -966,9 +974,7 @@ local function gui_click(event)
Alert.alert_all_players(5, message)
Server.to_discord_bold(
table.concat {
player.name ..
' has upgraded the teams pickaxe to tier ' ..
tier .. ' for ' .. format_number(item.price, true) .. ' coins.'
player.name .. ' has upgraded the teams pickaxe to tier ' .. tier .. ' for ' .. format_number(item.price, true) .. ' coins.'
}
)
@ -996,8 +1002,7 @@ local function gui_click(event)
Alert.alert_all_players(5, message)
Server.to_discord_bold(
table.concat {
player.name ..
' has bought the chest limit upgrade for ' .. format_number(item.price, true) .. ' coins.'
player.name .. ' has bought the chest limit upgrade for ' .. format_number(item.price, true) .. ' coins.'
}
)
this.chest_limit_outside_upgrades = this.chest_limit_outside_upgrades + item.stack
@ -1014,13 +1019,25 @@ local function gui_click(event)
Alert.alert_all_players(5, message)
Server.to_discord_bold(
table.concat {
player.name ..
' has bought the locomotive health modifier for ' .. format_number(item.price, true) .. ' coins.'
player.name .. ' has bought the locomotive health modifier for ' .. format_number(item.price, true) .. ' coins.'
}
)
this.locomotive_max_health = this.locomotive_max_health + 2500 * item.stack
local m = this.locomotive_health / this.locomotive_max_health
this.locomotive.health = 1000 * m
if this.carriages then
for i = 1, #this.carriages do
local entity = this.carriages[i]
if not (entity and entity.valid) then
return
end
if entity.type == 'locomotive' then
entity.health = 1000 * m
else
entity.health = 600 * m
end
end
end
this.train_upgrades = this.train_upgrades + item.stack
this.health_upgrades = this.health_upgrades + item.stack
@ -1039,8 +1056,7 @@ local function gui_click(event)
Alert.alert_all_players(5, message)
Server.to_discord_bold(
table.concat {
player.name ..
' has bought the locomotive xp aura modifier for ' .. format_number(item.price, true) .. ' coins.'
player.name .. ' has bought the locomotive xp aura modifier for ' .. format_number(item.price, true) .. ' coins.'
}
)
this.locomotive_xp_aura = this.locomotive_xp_aura + 5
@ -1098,8 +1114,7 @@ local function gui_click(event)
Alert.alert_all_players(5, message)
Server.to_discord_bold(
table.concat {
player.name ..
' has bought the explosive bullet modifier for ' .. format_number(item.price) .. ' coins.'
player.name .. ' has bought the explosive bullet modifier for ' .. format_number(item.price) .. ' coins.'
}
)
this.explosive_bullets = true
@ -1122,8 +1137,7 @@ local function gui_click(event)
Alert.alert_all_players(5, message)
Server.to_discord_bold(
table.concat {
player.name ..
' has bought a flamethrower-turret slot for ' .. format_number(item.price, true) .. ' coins.'
player.name .. ' has bought a flamethrower-turret slot for ' .. format_number(item.price, true) .. ' coins.'
}
)
else
@ -1138,9 +1152,7 @@ local function gui_click(event)
Server.to_discord_bold(
table.concat {
player.name ..
' has bought ' ..
item.stack ..
' flamethrower-turret slots for ' .. format_number(item.price, true) .. ' coins.'
' has bought ' .. item.stack .. ' flamethrower-turret slots for ' .. format_number(item.price, true) .. ' coins.'
}
)
end
@ -1169,8 +1181,7 @@ local function gui_click(event)
Server.to_discord_bold(
table.concat {
player.name ..
' has bought ' ..
item.stack .. ' landmine slots for ' .. format_number(item.price, true) .. ' coins.'
' has bought ' .. item.stack .. ' landmine slots for ' .. format_number(item.price, true) .. ' coins.'
}
)
end
@ -1302,9 +1313,7 @@ local function spawn_biter()
return
end
this.locomotive_biter =
loco_surface.create_entity(
{name = biters[random(1, 4)], position = position, force = 'player', create_build_effect_smoke = false}
)
loco_surface.create_entity({name = biters[random(1, 4)], position = position, force = 'player', create_build_effect_smoke = false})
this.locomotive_biter.ai_settings.allow_destroy_when_commands_fail = false
this.locomotive_biter.ai_settings.allow_try_return_to_spawner = false
@ -1339,9 +1348,7 @@ local function create_market(data, rebuild)
for _, entity in pairs(surface.find_entities_filtered {area = area, name = 'market'}) do
entity.destroy()
end
for _, entity in pairs(
surface.find_entities_filtered {area = area, type = 'item-entity', name = 'item-on-ground'}
) do
for _, entity in pairs(surface.find_entities_filtered {area = area, type = 'item-entity', name = 'item-on-ground'}) do
entity.destroy()
end
for _, entity in pairs(surface.find_entities_filtered {area = area, type = 'unit'}) do
@ -1386,11 +1393,7 @@ local function create_market(data, rebuild)
for x = center_position.x - 5, center_position.x + 5, 1 do
for y = center_position.y - 5, center_position.y + 5, 1 do
if random(1, 2) == 1 then
loco_surface.spill_item_stack(
{x + random(0, 9) * 0.1, y + random(0, 9) * 0.1},
{name = 'raw-fish', count = 1},
false
)
loco_surface.spill_item_stack({x + random(0, 9) * 0.1, y + random(0, 9) * 0.1}, {name = 'raw-fish', count = 1}, false)
end
loco_surface.set_tiles({{name = 'blue-refined-concrete', position = {x, y}}}, true)
end
@ -1398,11 +1401,7 @@ local function create_market(data, rebuild)
for x = center_position.x - 3, center_position.x + 3, 1 do
for y = center_position.y - 3, center_position.y + 3, 1 do
if random(1, 2) == 1 then
loco_surface.spill_item_stack(
{x + random(0, 9) * 0.1, y + random(0, 9) * 0.1},
{name = 'raw-fish', count = 1},
false
)
loco_surface.spill_item_stack({x + random(0, 9) * 0.1, y + random(0, 9) * 0.1}, {name = 'raw-fish', count = 1}, false)
end
loco_surface.set_tiles({{name = 'cyan-refined-concrete', position = {x, y}}}, true)
end
@ -1688,10 +1687,7 @@ local function on_player_changed_surface(event)
if not surface or not surface.valid then
return
end
player.teleport(
surface.find_non_colliding_position('character', game.forces.player.get_spawn_position(surface), 3, 0, 5),
surface
)
player.teleport(surface.find_non_colliding_position('character', game.forces.player.get_spawn_position(surface), 3, 0, 5), surface)
end
if string.sub(player.surface.name, 0, #map_name) ~= map_name then
@ -1829,17 +1825,13 @@ end
function Public.locomotive_spawn(surface, position)
local this = WPT.get()
for y = -6, 6, 2 do
surface.create_entity(
{name = 'straight-rail', position = {position.x, position.y + y}, force = 'player', direction = 0}
)
for y = -6, 14, 2 do
surface.create_entity({name = 'straight-rail', position = {position.x, position.y + y}, force = 'player', direction = 0})
end
this.locomotive =
surface.create_entity({name = 'locomotive', position = {position.x, position.y + -3}, force = 'player'})
this.locomotive = surface.create_entity({name = 'locomotive', position = {position.x, position.y + -3}, force = 'player'})
this.locomotive.get_inventory(defines.inventory.fuel).insert({name = 'wood', count = 100})
this.locomotive_cargo =
surface.create_entity({name = 'cargo-wagon', position = {position.x, position.y + 3}, force = 'player'})
this.locomotive_cargo = surface.create_entity({name = 'cargo-wagon', position = {position.x, position.y + 3}, force = 'player'})
this.locomotive_cargo.get_inventory(defines.inventory.cargo_wagon).insert({name = 'raw-fish', count = 8})
rendering.draw_light(
@ -1857,6 +1849,21 @@ function Public.locomotive_spawn(surface, position)
}
)
rendering.draw_light(
{
sprite = 'utility/light_medium',
scale = 5.5,
intensity = 1,
minimum_darkness = 0,
oriented = true,
color = {255, 255, 255},
target = this.locomotive_cargo,
surface = surface,
visible = true,
only_in_alt_mode = false
}
)
local data = {
surface = surface,
position = position
@ -1887,9 +1894,7 @@ function Public.locomotive_spawn(surface, position)
this.locomotive_cargo.operable = true
local locomotive = ICW.register_wagon(this.locomotive)
local wagon = ICW.register_wagon(this.locomotive_cargo)
locomotive.entity_count = 999
wagon.entity_count = 999
ICW.register_wagon(this.locomotive_cargo)
this.icw_locomotive = locomotive

View File

@ -232,6 +232,10 @@ local function open_inventory(source, target)
}
)
if not validate_object(frame) then
return
end
frame.auto_center = true
source.opened = frame
frame.style.minimal_width = 500

View File

@ -1,51 +1,33 @@
local table_sort = table.sort
local string_rep = string.rep
local string_format = string.format
local string_len = string.len
local string_sub = string.sub
local string_gsub = string.gsub
local debug_getinfo = debug.getinfo
-- Call
-- name (string)
-- calls (int)
-- profiler (LuaProfiler)
-- next (Array of Call)
local Profiler =
{
-- Call
CallTree = nil,
IsRunning = false,
local Profiler = {
-- Call
CallTree = nil,
IsRunning = false
}
local ignoredFunctions =
{
[debug.sethook] = true
local ignoredFunctions = {
[debug.sethook] = true
}
local namedSources =
{
["[string \"local n, v = \"serpent\", \"0.30\" -- (C) 2012-17...\"]"] = "serpent",
local namedSources = {
['[string "local n, v = "serpent", "0.30" -- (C) 2012-17..."]'] = 'serpent'
}
local function startCommand(command)
Profiler.Start(command.parameter ~= nil)
Profiler.Start(command.parameter ~= nil)
end
local function stopCommand(command)
Profiler.Stop(command.parameter ~= nil, nil)
Profiler.Stop(command.parameter ~= nil, nil)
end
ignoredFunctions[startCommand] = true
ignoredFunctions[stopCommand] = true
commands.add_command("startProfiler", "Starts profiling", startCommand)
commands.add_command("stopProfiler", "Stops profiling", stopCommand)
commands.add_command('startProfiler', 'Starts profiling', startCommand)
commands.add_command('stopProfiler', 'Stops profiling', stopCommand)
--local assert_raw = assert
--function assert(expr, ...)
@ -56,167 +38,168 @@ commands.add_command("stopProfiler", "Stops profiling", stopCommand)
--end
local error_raw = error
function error(...)
Profiler.Stop(false, "Error raised")
error_raw(...)
Profiler.Stop(false, 'Error raised')
error_raw(...)
end
function Profiler.Start(excludeCalledMs)
if Profiler.IsRunning then
return
end
if Profiler.IsRunning then
return
end
local create_profiler = game.create_profiler
local create_profiler = game.create_profiler
Profiler.IsRunning = true
Profiler.IsRunning = true
Profiler.CallTree =
{
name = "root",
calls = 0,
profiler = create_profiler(),
next = { },
}
Profiler.CallTree = {
name = 'root',
calls = 0,
profiler = create_profiler(),
next = {}
}
-- Array of Call
local stack = { [0] = Profiler.CallTree }
local stack_count = 0
-- Array of Call
local stack = {[0] = Profiler.CallTree}
local stack_count = 0
debug.sethook(function(event)
local info = debug_getinfo(2, "nSf")
debug.sethook(
function(event)
local info = debug_getinfo(2, 'nSf')
if ignoredFunctions[info.func] then
return
end
if ignoredFunctions[info.func] then
return
end
if event == "call" or event == "tail call" then
local prevCall = stack[stack_count]
if excludeCalledMs then
prevCall.profiler.stop()
end
if event == 'call' or event == 'tail call' then
local prevCall = stack[stack_count]
if excludeCalledMs then
prevCall.profiler.stop()
end
local what = info.what
local name
if what == "C" then
name = string_format("C function %q", info.name or "anonymous")
else
local source = info.short_src
local namedSource = namedSources[source]
if namedSource ~= nil then
source = namedSource
elseif string.sub(source, 1, 1) == "@" then
source = string.sub(source, 1)
end
name = string_format("%q in %q, line %d", info.name or "anonymous", source, info.linedefined)
end
local what = info.what
local name
if what == 'C' then
name = string_format('C function %q', info.name or 'anonymous')
else
local source = info.short_src
local namedSource = namedSources[source]
if namedSource ~= nil then
source = namedSource
elseif string.sub(source, 1, 1) == '@' then
source = string.sub(source, 1)
end
name = string_format('%q in %q, line %d', info.name or 'anonymous', source, info.linedefined)
end
local prevCall_next = prevCall.next
if prevCall_next == nil then
prevCall_next = { }
prevCall.next = prevCall_next
end
local prevCall_next = prevCall.next
if prevCall_next == nil then
prevCall_next = {}
prevCall.next = prevCall_next
end
local currCall = prevCall_next[name]
local profilerStartFunc
if currCall == nil then
local prof = create_profiler()
currCall =
{
name = name,
calls = 1,
profiler = prof,
}
prevCall_next[name] = currCall
profilerStartFunc = prof.reset
else
currCall.calls = currCall.calls + 1
profilerStartFunc = currCall.profiler.restart
end
local currCall = prevCall_next[name]
local profilerStartFunc
if currCall == nil then
local prof = create_profiler()
currCall = {
name = name,
calls = 1,
profiler = prof
}
prevCall_next[name] = currCall
profilerStartFunc = prof.reset
else
currCall.calls = currCall.calls + 1
profilerStartFunc = currCall.profiler.restart
end
stack_count = stack_count + 1
stack[stack_count] = currCall
stack_count = stack_count + 1
stack[stack_count] = currCall
profilerStartFunc()
end
profilerStartFunc()
end
if event == "return" or event == "tail call" then
if stack_count > 0 then
stack[stack_count].profiler.stop()
stack[stack_count] = nil
stack_count = stack_count - 1
if event == 'return' or event == 'tail call' then
if stack_count > 0 then
stack[stack_count].profiler.stop()
stack[stack_count] = nil
stack_count = stack_count - 1
if excludeCalledMs then
stack[stack_count].profiler.restart()
end
end
end
end, "cr")
if excludeCalledMs then
stack[stack_count].profiler.restart()
end
end
end
end,
'cr'
)
end
ignoredFunctions[Profiler.Start] = true
local function DumpTree(averageMs)
local function sort_Call(a, b)
return a.calls > b.calls
end
local fullStr = { "" }
local str = fullStr
local line = 1
local function sort_Call(a, b)
return a.calls > b.calls
end
local fullStr = {''}
local str = fullStr
local line = 1
local function recurse(curr, depth)
local function recurse(curr, depth)
local sort = {}
local i = 1
for k, v in pairs(curr) do
sort[i] = v
i = i + 1
end
table_sort(sort, sort_Call)
local sort = { }
local i = 1
for k, v in pairs(curr) do
sort[i] = v
i = i + 1
end
table_sort(sort, sort_Call)
for i = 1, #sort do
local call = sort[i]
for i = 1, #sort do
local call = sort[i]
if line >= 19 then --Localised string can only have up to 20 parameters
local newStr = {''} --So nest them!
str[line + 1] = newStr
str = newStr
line = 1
end
if line >= 19 then --Localised string can only have up to 20 parameters
local newStr = { "" } --So nest them!
str[line + 1] = newStr
str = newStr
line = 1
end
if averageMs then
call.profiler.divide(call.calls)
end
if averageMs then
call.profiler.divide(call.calls)
end
str[line + 1] =
string_format('\n%s%dx %s. %s ', string_rep('\t', depth), call.calls, call.name, averageMs and 'Average' or 'Total')
str[line + 2] = call.profiler
line = line + 2
str[line + 1] = string_format("\n%s%dx %s. %s ", string_rep("\t", depth), call.calls, call.name, averageMs and "Average" or "Total")
str[line + 2] = call.profiler
line = line + 2
local next = call.next
if next ~= nil then
recurse(next, depth + 1)
end
end
end
if Profiler.CallTree.next ~= nil then
recurse(Profiler.CallTree.next, 0)
return fullStr
end
return "No calls"
local next = call.next
if next ~= nil then
recurse(next, depth + 1)
end
end
end
if Profiler.CallTree.next ~= nil then
recurse(Profiler.CallTree.next, 0)
return fullStr
end
return 'No calls'
end
function Profiler.Stop(averageMs, message)
if not Profiler.IsRunning then
return
end
if not Profiler.IsRunning then
return
end
debug.sethook()
debug.sethook()
local text = { "", "\n\n----------PROFILER DUMP----------\n", DumpTree(averageMs), "\n\n----------PROFILER STOPPED----------\n" }
if message ~= nil then
text[#text + 1] = string.format("Reason: %s\n", message)
end
log(text)
Profiler.CallTree = nil
Profiler.IsRunning = false
local text = {'', '\n\n----------PROFILER DUMP----------\n', DumpTree(averageMs), '\n\n----------PROFILER STOPPED----------\n'}
if message ~= nil then
text[#text + 1] = string.format('Reason: %s\n', message)
end
log(text)
Profiler.CallTree = nil
Profiler.IsRunning = false
end
ignoredFunctions[Profiler.Stop] = true
return Profiler
return Profiler