Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mf_mod_name = "moon-eneas"
- -- ============================================================================
- -- STATIC TRANSMISSION CLEANUP
- -- The key idea: we never call script.on_nth_tick() at runtime.
- -- Instead we store the cleanup tick in `storage` (which is saved and synced),
- -- and check it inside this handler that is ALWAYS registered at load time.
- -- ============================================================================
- script.on_nth_tick(61, function()
- -- If there is no pending cleanup, do nothing.
- if not storage.static_transmission_cleanup then return end
- local pending = storage.static_transmission_cleanup
- -- Check if enough time has passed
- if game.tick < pending.cleanup_tick then return end
- -- Time is up: find and destroy the dummy entity
- -- We look it up by position rather than storing the entity reference directly,
- -- because entity references (LuaEntity objects) cannot be stored in `storage`.
- -- Only plain Lua values (numbers, strings, tables, booleans) can go in storage.
- local nauvis = game.surfaces["nauvis"]
- if nauvis and nauvis.valid then
- local dummies = nauvis.find_entities_filtered{
- position = pending.position,
- radius = 5,
- name = "unit-05-dummy"
- }
- for _, entity in pairs(dummies) do
- entity.destroy()
- end
- end
- -- Clear the pending cleanup so we don't run this again next tick
- storage.static_transmission_cleanup = nil
- end)
- -- ============================================================================
- -- SHOW STATIC TRANSMISSION
- -- This function is safe to call at runtime because it only writes to `storage`
- -- and does not register any new event handlers.
- -- ============================================================================
- function ShowStaticTransmission(message, duration, animation)
- local nauvis = game.surfaces["nauvis"]
- local STATIC_POSITION = {x = 100000, y = 100000}
- -- Clean up any old dummy entity from a previous transmission
- local old_dummies = nauvis.find_entities_filtered{
- position = STATIC_POSITION,
- radius = 5,
- name = "unit-05-dummy"
- }
- for _, entity in pairs(old_dummies) do
- entity.destroy()
- end
- -- Create the new dummy entity
- local static_entity = nauvis.create_entity{
- name = "unit-05-dummy",
- position = STATIC_POSITION,
- force = "player"
- }
- -- Draw the animation on the dummy
- if animation and static_entity and static_entity.valid then
- rendering.draw_animation{
- animation = animation,
- target = {
- entity = static_entity,
- offset = util.by_pixel_hr(0, 0),
- },
- surface = static_entity.surface,
- render_layer = "object",
- time_to_live = 1200,
- animation_speed = 0.2,
- }
- end
- ShowTransmission(static_entity, "[color=yellow]Transmission[/color]", duration)
- -- Instead of registering a new on_nth_tick handler here (which would cause
- -- the desync), we write the cleanup information into `storage`.
- -- `storage` is part of the save file, so a player joining mid-game will
- -- load it and the already-registered on_nth_tick(61) above will find it.
- storage.static_transmission_cleanup = {
- cleanup_tick = game.tick + ((duration or 8) + 1) * 60,
- position = STATIC_POSITION,
- }
- end
- function ShowTransmission(entity, message, duration)
- duration = duration or 8
- CreateCameraForConnectedPlayers(
- entity,
- nil,
- message,
- 225,
- duration,
- 1.2
- )
- end
- script.on_event(defines.events.on_tick, function(event)
- cam_on_tick(event)
- end)
- script.on_event(defines.events.on_gui_click, function(event)
- cam_on_gui_click(event)
- end)
- commands.add_command("test-transmission", "Test the camera transmission system", function(command)
- local player = command.player_index and game.get_player(command.player_index)
- if player then
- ShowStaticTransmission("Eneas: ...can you hear me?...", 10, "eye-1")
- player.print("Transmission test triggered!")
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment