// Presents: Christmas presents scattered on the island. Walk onto one to open it: a few random items and a coin sound.
// Opened presents come back elsewhere every few minutes. The present and the sound are not part of the game: they come
// from the s&box cloud (sbox.game packages facepunch.present_box and igrotronika.coin1), and players download them when
// they join. Any public model or sound package works: change Model and Sound in plugins/config/Presents.json.
// Admins: /presents spawn fills the island again, /presents clear removes them.
// Install: copy this file into the server's data folder, plugins/Presents.cs.
// Script engine rules: test objects with != null, no static fields that change, callbacks with one parameter at most.
using RustLegacy;
public class PresentLoot
{
public string Item { get; set; } = "";
public int Min { get; set; } = 1;
public int Max { get; set; } = 1;
}
public class PresentsConfig
{
/// Presents on the island at once.
public int Count { get; set; } = 12;
/// Minutes between two refills of the opened presents.
public float RefillMinutes { get; set; } = 10f;
/// How close a player must walk to open one, in meters.
public float PickupMeters { get; set; } = 1.5f;
/// Items in each present (2 picked at random).
public int ItemsPerPresent { get; set; } = 2;
public List Loot { get; set; } = new List
{
new PresentLoot { Item = "Large Medkit", Min = 1, Max = 2 },
new PresentLoot { Item = "Cooked Chicken Breast", Min = 3, Max = 6 },
new PresentLoot { Item = "Chocolate Bar", Min = 2, Max = 4 },
new PresentLoot { Item = "9mm Ammo", Min = 20, Max = 50 },
new PresentLoot { Item = "Arrow", Min = 10, Max = 20 },
new PresentLoot { Item = "Metal Fragments", Min = 50, Max = 150 },
new PresentLoot { Item = "Explosive Charge", Min = 1, Max = 1 },
};
/// Cloud package of the present's model (or a model path of the game).
public string Model { get; set; } = "facepunch.present_box";
/// Cloud package of the opening sound (or a .sound path of the game).
public string Sound { get; set; } = "igrotronika.coin1";
}
public class Presents : RustPlugin
{
PresentsConfig _config;
readonly List _boxes = new List();
public override void OnLoaded()
{
_config = LoadConfig();
UsePackage( _config.Model ); // mounted now: players download it while they join
UsePackage( _config.Sound );
AddCommand( "presents", OnCommand, "Christmas presents on the island", false, "[spawn | clear]" );
Fill();
Every( 1f, Check );
Every( _config.RefillMinutes * 60f, Fill );
}
void Prune()
{
for ( var i = _boxes.Count - 1; i >= 0; i-- )
if ( _boxes[i] == null || !_boxes[i].IsValid ) _boxes.RemoveAt( i );
}
/// Tops the island up to Count presents, at random spawn points (on the ground, out of the sea and radiation).
void Fill()
{
Prune();
while ( _boxes.Count < _config.Count )
{
var at = RustGameManager.FindSpawnPosition( Scene );
_boxes.Add( SpawnProp( _config.Model, at, Game.Random.Float( 0f, 360f ) ) );
}
}
void Check()
{
Prune();
var reach = _config.PickupMeters * 39.37f;
foreach ( var p in Players )
{
if ( p.IsDead ) continue;
for ( var i = _boxes.Count - 1; i >= 0; i-- )
{
var box = _boxes[i];
if ( box.WorldPosition.Distance( p.WorldPosition ) > reach ) continue;
_boxes.RemoveAt( i );
Open( p, box );
}
}
}
void Open( RustPlayer p, GameObject box )
{
PlaySound( _config.Sound, box.WorldPosition );
RemoveProp( box );
var got = new List();
for ( var n = 0; n < _config.ItemsPerPresent && _config.Loot.Count > 0; n++ )
{
var loot = _config.Loot[Game.Random.Int( 0, _config.Loot.Count - 1 )];
var amount = Game.Random.Int( loot.Min, Math.Max( loot.Min, loot.Max ) );
Give( p, loot.Item, amount );
p.NotifyInventoryGain( $"{amount} x {loot.Item}" );
got.Add( $"{amount} {loot.Item}" );
}
Reply( p, $"You opened a present: {string.Join( ", ", got )}. {_boxes.Count} left on the island." );
}
void OnCommand( ChatCommand cmd )
{
var p = cmd.Player;
Prune();
if ( cmd.Arg( 0 ) == "spawn" && IsAdmin( p ) ) { Fill(); Reply( p, $"{_boxes.Count} presents on the island." ); return; }
if ( cmd.Arg( 0 ) == "clear" && IsAdmin( p ) )
{
foreach ( var box in _boxes ) RemoveProp( box );
_boxes.Clear();
Reply( p, "Presents removed until the next refill." );
return;
}
Reply( p, $"{_boxes.Count} presents are hidden around the island. Walk onto one to open it." );
}
}