Preserve entered vehicle on inventory sync

Save vehicle the player is driving before downloading the inventory and
restore the player position to this vehicle if possible.
This commit is contained in:
Hornwitser committed 2022-06-19 19:51:22 +02:00
1 parent 430a64722e
commit 105f99ced3
2 files changed
+37 -12

No files matched your search

@@ -3,6 +3,32 @@ local ensure_character = require("modules/inventory_sync/ensure_character")
local serialize = require("modules/inventory_sync/serialize") local serialize = require("modules/inventory_sync/serialize")
local load_crafting_queue = require("modules/inventory_sync/load_crafting_queue") local load_crafting_queue = require("modules/inventory_sync/load_crafting_queue")
local can_enter_vehicle = {
[defines.controllers.character] = true,
[defines.controllers.god] = true,
[defines.controllers.editor] = true,
}
local function restore_position(record, player)
if record.vehicle and record.vehicle.valid and can_enter_vehicle[player.controller_type] then
player.teleport(record.vehicle.position, record.vehicle.surface)
player.driving = true
-- Teleport to safe location if unable to enter vehicle
if not player.driving and player.controller_type == defines.controllers.character then
local safe_position = record.vehicle.surface.find_non_colliding_position(
player.character.name, player.position, 32, 1/8
)
if safe_position then
player.teleport(safe_position, player.surface)
end
end
elseif record.surface and record.position then
player.teleport(record.position, record.surface)
end
end
local function download_inventory(player_name, data, number, total) local function download_inventory(player_name, data, number, total)
local player = game.get_player(player_name) local player = game.get_player(player_name)
if player == nil then if player == nil then
@@ -33,10 +59,8 @@ local function download_inventory(player_name, data, number, total)
-- Give the player a character and pretend that's the synced player data -- Give the player a character and pretend that's the synced player data
ensure_character(player) ensure_character(player)
-- Restore player position in case they moved while being spectator -- Restore player position and driving state
if record.surface and record.position then restore_position(record, player)
player.teleport(record.position, record.surface)
end
global.inventory_sync.active_downloads[player_name] = nil global.inventory_sync.active_downloads[player_name] = nil
player_record.dirty = player.connected player_record.dirty = player.connected
@@ -87,10 +111,8 @@ local function download_inventory(player_name, data, number, total)
local serialized_player = game.json_to_table(record.data) local serialized_player = game.json_to_table(record.data)
serialize.deserialize_player(player, serialized_player) serialize.deserialize_player(player, serialized_player)
-- Restore player position in case they moved while being spectator -- Restore player position and driving state
if record.surface and record.position then restore_position(record, player)
player.teleport(record.position, record.surface)
end
-- Load crafting queue -- Load crafting queue
if player.character then if player.character then
@@ -310,13 +310,16 @@ function inventory_sync.initiate_inventory_download(player, player_record, gener
-- If this is a synced player turn them into a spectator while the -- If this is a synced player turn them into a spectator while the
-- player data is downloading -- player data is downloading
if player_record.sync then if player_record.sync then
local character = player.character
player.set_controller({ type = defines.controllers.spectator })
if character ~= nil then character.destroy() end
-- Store original position to teleport back to -- Store original position to teleport back to
record.surface = player.surface record.surface = player.surface
record.position = player.position record.position = player.position
if player.driving then
record.vehicle = player.vehicle
end
local character = player.character
player.set_controller({ type = defines.controllers.spectator })
if character ~= nil then character.destroy() end
-- Indicate this state shouldn't be persisted -- Indicate this state shouldn't be persisted
player_record.dirty = false player_record.dirty = false