// Airdrop: shows where the supply drops are. When a plane drops a crate, every player gets a chat line with its distance // and direction, a marker follows the crate on screen and on the map (G), and the HUD shows the nearest one. // Each player picks what they see, kept in plugins/data/Airdrop.json: // /airdrop the crates on the island and your settings // /airdrop settings a menu to switch each part on or off // /airdrop chat|hud|marker on|off one part; /airdrop on|off all of them // Legacy had none of this: players watched the plane and the smoke. Install it only if you want that race shorter. // Install: copy this file into the server's data folder, plugins/Airdrop.cs. Server settings: plugins/config/Airdrop.json. // Script engine rules: test objects with != null, no static fields that change, callbacks with one parameter at most. using RustLegacy; /// What the server allows (a part switched off here is off for everyone). public class AirdropConfig { /// A chat line when a crate is dropped. public bool ChatOnDrop { get; set; } = true; /// A marker on each crate, on screen and on the map. public bool Markers { get; set; } = true; /// The nearest crate in the top right corner of the HUD. public bool HudText { get; set; } = true; /// Seconds between two updates of the markers and the HUD. public float UpdateSeconds { get; set; } = 1f; } /// What one player wants to see. public class AirdropPrefs { public bool Chat { get; set; } = true; public bool Hud { get; set; } = true; public bool Marker { get; set; } = true; } public class AirdropData { /// SteamID → settings of the players who changed them (the others see everything). public Dictionary Players { get; set; } = new Dictionary(); /// Older version of this file: SteamIDs who typed /airdrop off. Moved into Players when read. public List Off { get; set; } = new List(); } public class Airdrop : RustPlugin { const int MaxMarkers = 10; AirdropConfig _config; AirdropData _data; readonly List _crates = new List(); public override void OnLoaded() { _config = LoadConfig(); _data = LoadData(); foreach ( var id in _data.Off ) _data.Players[id] = new AirdropPrefs { Chat = false, Hud = false, Marker = false }; _data.Off.Clear(); AddCommand( "airdrop", OnCommand, "Where the airdrops are, and what you see of them", false, "[settings | on | off | chat|hud|marker on|off]" ); foreach ( var crate in Scene.GetAllComponents() ) _crates.Add( crate ); // dropped before a restart of the plugins Every( _config.UpdateSeconds, Refresh ); } AirdropPrefs Prefs( RustPlayer p ) => _data.Players.ContainsKey( p.SteamId ) ? _data.Players[p.SteamId] : new AirdropPrefs(); /// The player's settings, created on first change and saved at once: they survive restarts and wipes. void Change( RustPlayer p, string part, bool on ) { var prefs = Prefs( p ); if ( part == "chat" || part == "all" ) prefs.Chat = on; if ( part == "hud" || part == "all" ) prefs.Hud = on; if ( part == "marker" || part == "all" ) prefs.Marker = on; _data.Players[p.SteamId] = prefs; SaveData( _data ); Refresh(); } public override void OnAirdrop( SupplyCrate crate ) { _crates.Add( crate ); if ( !_config.ChatOnDrop ) return; foreach ( var p in Players ) if ( Prefs( p ).Chat ) Reply( p, $"A supply drop is falling {Meters( p.WorldPosition, crate.WorldPosition )} m {Compass( p.WorldPosition, crate.WorldPosition )} of you (map {MapCell( crate.WorldPosition )})." ); } void OnCommand( ChatCommand cmd ) { var p = cmd.Player; var a = cmd.Arg( 0 ); var b = cmd.Arg( 1 ); if ( a == "on" || a == "off" ) { Change( p, "all", a == "on" ); Reply( p, a == "on" ? "Airdrops shown again." : "Airdrops hidden. /airdrop on to see them again." ); return; } if ( (a == "chat" || a == "hud" || a == "marker") && (b == "on" || b == "off") ) { Change( p, a, b == "on" ); Settings( p ); return; } if ( a == "settings" ) { Settings( p ); return; } Prune(); if ( _crates.Count == 0 ) Reply( p, "No supply drop on the island right now." ); foreach ( var crate in _crates ) Reply( p, $"Supply drop {Meters( p.WorldPosition, crate.WorldPosition )} m {Compass( p.WorldPosition, crate.WorldPosition )} (map {MapCell( crate.WorldPosition )})" ); var prefs = Prefs( p ); Reply( p, $"You see: chat {OnOff( prefs.Chat )}, HUD {OnOff( prefs.Hud )}, markers {OnOff( prefs.Marker )}. /airdrop settings to change." ); } static string OnOff( bool on ) => on ? "on" : "off"; /// A menu of three switches; a click flips one and shows the menu again. void Settings( RustPlayer p ) { var prefs = Prefs( p ); var menu = new PluginMenu { Title = "Airdrop", Text = "What you see when a supply drop falls. Saved for your next visits." }; menu.Buttons.Add( Switch( "Chat message", "chat", prefs.Chat, _config.ChatOnDrop ) ); menu.Buttons.Add( Switch( "Top right of the HUD", "hud", prefs.Hud, _config.HudText ) ); menu.Buttons.Add( Switch( "Markers (screen and map)", "marker", prefs.Marker, _config.Markers ) ); ShowMenu( p, menu ); } static PluginMenuButton Switch( string label, string part, bool on, bool allowed ) => allowed ? new PluginMenuButton { Label = $"{label}: {(on ? "ON" : "OFF")}", Command = $"/airdrop {part} {(on ? "off" : "on")}", Selected = on } : new PluginMenuButton { Label = $"{label}: off on this server" }; /// Crates that despawned or were destroyed leave the list (plain loop: RemoveAll takes a Predicate the script /// engine does not pass). void Prune() { for ( var i = _crates.Count - 1; i >= 0; i-- ) if ( _crates[i] == null || !_crates[i].IsValid ) _crates.RemoveAt( i ); } void Refresh() { Prune(); foreach ( var p in Players ) { var prefs = Prefs( p ); var markers = _config.Markers && prefs.Marker; for ( var i = 0; i < MaxMarkers; i++ ) { if ( i >= _crates.Count || !markers ) RemoveMarker( p, $"crate{i}" ); else SetMarker( p, $"crate{i}", _crates[i].WorldPosition, "Supply drop" ); } SupplyCrate nearest = null; foreach ( var crate in _crates ) if ( nearest == null || Meters( p.WorldPosition, crate.WorldPosition ) < Meters( p.WorldPosition, nearest.WorldPosition ) ) nearest = crate; if ( nearest == null || !_config.HudText || !prefs.Hud ) RemoveHudText( p, "nearest" ); else SetHudText( p, "nearest", $"Supply drop {Meters( p.WorldPosition, nearest.WorldPosition )} m {Compass( p.WorldPosition, nearest.WorldPosition )} ({MapCell( nearest.WorldPosition )})" ); } } }