TheBiky88

camera.lua

Feb 25th, 2026
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.23 KB | None | 0 0
  1. mf_mod_name = "moon-eneas"
  2.  
  3. -- ============================================================================
  4. -- STATIC TRANSMISSION CLEANUP
  5. -- The key idea: we never call script.on_nth_tick() at runtime.
  6. -- Instead we store the cleanup tick in `storage` (which is saved and synced),
  7. -- and check it inside this handler that is ALWAYS registered at load time.
  8. -- ============================================================================
  9.  
  10. script.on_nth_tick(61, function()
  11.     -- If there is no pending cleanup, do nothing.
  12.     if not storage.static_transmission_cleanup then return end
  13.  
  14.     local pending = storage.static_transmission_cleanup
  15.  
  16.     -- Check if enough time has passed
  17.     if game.tick < pending.cleanup_tick then return end
  18.  
  19.     -- Time is up: find and destroy the dummy entity
  20.     -- We look it up by position rather than storing the entity reference directly,
  21.     -- because entity references (LuaEntity objects) cannot be stored in `storage`.
  22.     -- Only plain Lua values (numbers, strings, tables, booleans) can go in storage.
  23.     local nauvis = game.surfaces["nauvis"]
  24.     if nauvis and nauvis.valid then
  25.         local dummies = nauvis.find_entities_filtered{
  26.             position = pending.position,
  27.             radius = 5,
  28.             name = "unit-05-dummy"
  29.         }
  30.         for _, entity in pairs(dummies) do
  31.             entity.destroy()
  32.         end
  33.     end
  34.  
  35.     -- Clear the pending cleanup so we don't run this again next tick
  36.     storage.static_transmission_cleanup = nil
  37. end)
  38.  
  39. -- ============================================================================
  40. -- SHOW STATIC TRANSMISSION
  41. -- This function is safe to call at runtime because it only writes to `storage`
  42. -- and does not register any new event handlers.
  43. -- ============================================================================
  44.  
  45. function ShowStaticTransmission(message, duration, animation)
  46.     local nauvis = game.surfaces["nauvis"]
  47.     local STATIC_POSITION = {x = 100000, y = 100000}
  48.  
  49.     -- Clean up any old dummy entity from a previous transmission
  50.     local old_dummies = nauvis.find_entities_filtered{
  51.         position = STATIC_POSITION,
  52.         radius = 5,
  53.         name = "unit-05-dummy"
  54.     }
  55.     for _, entity in pairs(old_dummies) do
  56.         entity.destroy()
  57.     end
  58.  
  59.     -- Create the new dummy entity
  60.     local static_entity = nauvis.create_entity{
  61.         name = "unit-05-dummy",
  62.         position = STATIC_POSITION,
  63.         force = "player"
  64.     }
  65.  
  66.     -- Draw the animation on the dummy
  67.     if animation and static_entity and static_entity.valid then
  68.         rendering.draw_animation{
  69.             animation = animation,
  70.             target = {
  71.                 entity = static_entity,
  72.                 offset = util.by_pixel_hr(0, 0),
  73.             },
  74.             surface = static_entity.surface,
  75.             render_layer = "object",
  76.             time_to_live = 1200,
  77.             animation_speed = 0.2,
  78.         }
  79.     end
  80.  
  81.     ShowTransmission(static_entity, "[color=yellow]Transmission[/color]", duration)
  82.  
  83.     -- Instead of registering a new on_nth_tick handler here (which would cause
  84.     -- the desync), we write the cleanup information into `storage`.
  85.     -- `storage` is part of the save file, so a player joining mid-game will
  86.     -- load it and the already-registered on_nth_tick(61) above will find it.
  87.     storage.static_transmission_cleanup = {
  88.         cleanup_tick = game.tick + ((duration or 8) + 1) * 60,
  89.         position = STATIC_POSITION,
  90.     }
  91. end
  92.  
  93. function ShowTransmission(entity, message, duration)
  94.     duration = duration or 8
  95.     CreateCameraForConnectedPlayers(
  96.         entity,
  97.         nil,
  98.         message,
  99.         225,
  100.         duration,
  101.         1.2
  102.     )
  103. end
  104.  
  105. script.on_event(defines.events.on_tick, function(event)
  106.     cam_on_tick(event)
  107. end)
  108.  
  109. script.on_event(defines.events.on_gui_click, function(event)
  110.     cam_on_gui_click(event)
  111. end)
  112.  
  113. commands.add_command("test-transmission", "Test the camera transmission system", function(command)
  114.     local player = command.player_index and game.get_player(command.player_index)
  115.     if player then
  116.         ShowStaticTransmission("Eneas: ...can you hear me?...", 10, "eye-1")
  117.         player.print("Transmission test triggered!")
  118.     end
  119. end)
Advertisement
Add Comment
Please, Sign In to add comment