// Arena: a PvP training area, after the Legacy ArenaMiniGames plugin. Players type /arena join (or use the "Arena" button // of the inventory screen) to fight, respawn in the arena when they die, lose nothing, and get their own inventory and // position back with /arena leave. Three modes, picked by an admin (/arena mode): // ffa everyone gets the same kit; the first to KillsToWin kills wins the round // randomkit the kit changes at each death (next kit of RandomKits); the first to KillsToWin kills wins // gungame each kill moves you to the next weapon of GunGameLevels; the first past the last one wins // Admins place the spawn points where they stand with /arena addspawn. // Install: copy this file into the server's data folder, plugins/Arena.cs. Settings: plugins/config/Arena.json. // Script engine rules met here: write RustLegacy.DamageInfo (s&box has its own DamageInfo), test objects with != null // (x.IsValid() on a null x fails), no static fields that change, callbacks with one parameter at most. using RustLegacy; public class ArenaItem { public string Item { get; set; } = ""; public int Amount { get; set; } = 1; } public class ArenaKit { public string Name { get; set; } = ""; public List Items { get; set; } = new List(); } public class ArenaConfig { /// ffa, randomkit or gungame. public string Mode { get; set; } = "ffa"; /// Kills that win a round in ffa and randomkit; 0 = no rounds. public int KillsToWin { get; set; } = 15; /// Seconds between a death in the arena and the respawn there. public float RespawnDelay { get; set; } = 3f; /// Seconds a fighter cannot be hurt after appearing at a spawn point. public float SpawnProtection { get; set; } = 3f; /// Given with every kit (all modes). Item names as in the admin menu. public List Armor { get; set; } = new List { new ArenaItem { Item = "Kevlar Helmet" }, new ArenaItem { Item = "Kevlar Vest" }, new ArenaItem { Item = "Kevlar Pants" }, new ArenaItem { Item = "Kevlar Boots" }, new ArenaItem { Item = "Large Medkit", Amount = 3 }, }; /// The ffa kit. public ArenaKit Kit { get; set; } = Kit2( "M4 + P250", "M4", "556 Ammo", 250, "P250", "9mm Ammo", 100 ); /// randomkit: one after the other, the next at each death. public List RandomKits { get; set; } = new List { Kit2( "M4", "M4", "556 Ammo", 250, "", "", 0 ), Kit2( "MP5A4", "MP5A4", "9mm Ammo", 250, "", "", 0 ), Kit2( "Shotgun", "Shotgun", "Shotgun Shells", 64, "", "", 0 ), Kit2( "Bolt Action Rifle", "Bolt Action Rifle", "556 Ammo", 100, "", "", 0 ), Kit2( "Revolver", "Revolver", "9mm Ammo", 100, "", "", 0 ), }; /// gungame: a level per kill, in this order. public List GunGameLevels { get; set; } = new List { Kit2( "M4", "M4", "556 Ammo", 250, "", "", 0 ), Kit2( "MP5A4", "MP5A4", "9mm Ammo", 250, "", "", 0 ), Kit2( "Bolt Action Rifle", "Bolt Action Rifle", "556 Ammo", 100, "", "", 0 ), Kit2( "Shotgun", "Shotgun", "Shotgun Shells", 64, "", "", 0 ), Kit2( "P250", "P250", "9mm Ammo", 100, "", "", 0 ), Kit2( "Revolver", "Revolver", "9mm Ammo", 100, "", "", 0 ), Kit2( "9mm Pistol", "9mm Pistol", "9mm Ammo", 100, "", "", 0 ), Kit2( "Pipe Shotgun", "Pipe Shotgun", "Handmade Shell", 64, "", "", 0 ), Kit2( "Hunting Bow", "Hunting Bow", "Arrow", 64, "", "", 0 ), Kit2( "Rock", "Rock", "", 0, "", "", 0 ), }; static ArenaKit Kit2( string name, string gun, string ammo, int rounds, string gun2, string ammo2, int rounds2 ) { var kit = new ArenaKit { Name = name }; if ( gun != "" ) kit.Items.Add( new ArenaItem { Item = gun } ); if ( ammo != "" ) kit.Items.Add( new ArenaItem { Item = ammo, Amount = rounds } ); if ( gun2 != "" ) kit.Items.Add( new ArenaItem { Item = gun2 } ); if ( ammo2 != "" ) kit.Items.Add( new ArenaItem { Item = ammo2, Amount = rounds2 } ); return kit; } } /// A player in the arena: what to give back when they leave, and where they are in the round. public class ArenaFighter { public List Inventory { get; set; } = new List(); public Vector3 Return { get; set; } public int Kills { get; set; } public int Deaths { get; set; } /// randomkit: kit index; gungame: level. public int Step { get; set; } } public class ArenaData { public List Spawns { get; set; } = new List(); /// SteamID → fighter. Kept on disk so a server crash does not eat anyone's inventory. public Dictionary Fighters { get; set; } = new Dictionary(); /// SteamID → kills, all time. public Dictionary Kills { get; set; } = new Dictionary(); /// SteamID → rounds won, all time. public Dictionary Wins { get; set; } = new Dictionary(); public Dictionary Names { get; set; } = new Dictionary(); } public class Arena : RustPlugin { ArenaConfig _config; ArenaData _data; /// SteamID → RealTime of the last arrival at a spawn point (spawn protection). readonly Dictionary _spawnedAt = new Dictionary(); public override void OnLoaded() { _config = LoadConfig(); _data = LoadData(); AddCommand( "arena", OnArena, "The PvP training arena", false, "join | leave | top | menu" ); foreach ( var p in Players ) { SetButton( p, "menu", "Arena", "/arena menu", 3 ); SendBackIfStranded( p ); // fighters from before a restart or a reload of this plugin get their things back } } /// A reload or a stop sends everyone home, but keeps their copy on disk: when the server stops, the world may /// have been saved with the arena kit, and the copy is given back when they reconnect (OnPlayerConnected). After a reload, /// OnLoaded gives it back at once. (So remove Arena.cs while nobody is fighting, or they get the copy again later.) public override void OnUnloaded() { foreach ( var p in Players.Where( x => InArena( x ) ).ToList() ) Leave( p, false, false ); SaveData( _data ); } bool InArena( RustPlayer p ) => p != null && _data.Fighters.ContainsKey( p.SteamId ); string ModeTitle => _config.Mode == "gungame" ? "Gun game" : _config.Mode == "randomkit" ? "Random kit" : "Free for all"; int Count( Dictionary table, string id ) => table.ContainsKey( id ) ? table[id] : 0; void OnArena( ChatCommand cmd ) { var p = cmd.Player; var action = cmd.Arg( 0 ); if ( action == "join" ) Join( p ); else if ( action == "leave" ) { if ( InArena( p ) ) Leave( p, true, true ); else Reply( p, "You are not in the arena." ); } else if ( action == "top" ) Top( p ); else if ( action == "menu" ) Menu( p ); else if ( action == "mode" && IsAdmin( p ) ) SetMode( p, cmd.Arg( 1 ) ); else if ( action == "addspawn" && IsAdmin( p ) ) { _data.Spawns.Add( p.WorldPosition ); SaveData( _data ); Reply( p, $"Spawn point {_data.Spawns.Count} added here." ); } else if ( action == "clearspawns" && IsAdmin( p ) ) { _data.Spawns.Clear(); SaveData( _data ); Reply( p, "Spawn points removed." ); } else { Reply( p, $"{ModeTitle}, {_data.Fighters.Count} fighting. /arena join, /arena leave, /arena top" ); if ( IsAdmin( p ) ) Reply( p, $"Admin: /arena mode ffa|randomkit|gungame, /arena addspawn (where you stand), /arena clearspawns ({_data.Spawns.Count} now)" ); } } /// The "Arena" button: join or leave, the scores, and the modes for admins. void Menu( RustPlayer p ) { var goal = _config.Mode == "gungame" ? $"{_config.GunGameLevels.Count} weapons to go through" : _config.KillsToWin > 0 ? $"first to {_config.KillsToWin} kills" : "no limit"; var menu = new PluginMenu { Title = "Arena", Text = $"{ModeTitle}: {goal}. {_data.Fighters.Count} fighting. You lose nothing in the arena." }; menu.Buttons.Add( InArena( p ) ? new PluginMenuButton { Label = "Leave the arena", Command = "/arena leave" } : new PluginMenuButton { Label = "Join the arena", Command = "/arena join", Item = "M4" } ); menu.Buttons.Add( new PluginMenuButton { Label = "Best players", Command = "/arena top" } ); if ( IsAdmin( p ) ) { menu.Buttons.Add( new PluginMenuButton { Group = "Mode (admin)", Label = "Free for all", Command = "/arena mode ffa", Selected = _config.Mode == "ffa" } ); menu.Buttons.Add( new PluginMenuButton { Group = "Mode (admin)", Label = "Random kit", Command = "/arena mode randomkit", Selected = _config.Mode == "randomkit" } ); menu.Buttons.Add( new PluginMenuButton { Group = "Mode (admin)", Label = "Gun game", Command = "/arena mode gungame", Selected = _config.Mode == "gungame" } ); menu.Buttons.Add( new PluginMenuButton { Group = "Spawn points (admin)", Label = $"Add one here ({_data.Spawns.Count})", Command = "/arena addspawn" } ); menu.Buttons.Add( new PluginMenuButton { Group = "Spawn points (admin)", Label = "Remove all", Command = "/arena clearspawns" } ); } ShowMenu( p, menu ); } void SetMode( RustPlayer p, string mode ) { if ( mode != "ffa" && mode != "randomkit" && mode != "gungame" ) { Reply( p, "/arena mode ffa | randomkit | gungame" ); return; } _config.Mode = mode; SaveConfig( _config ); NewRound(); Broadcast( $"The arena is now {ModeTitle}." ); } void Join( RustPlayer p ) { if ( InArena( p ) ) { Reply( p, "You are already in the arena. /arena leave to go back." ); return; } if ( p.IsDead ) { Reply( p, "Respawn first." ); return; } if ( _data.Spawns.Count == 0 ) { Reply( p, "The arena has no spawn point yet: an admin must place them with /arena addspawn." ); return; } _data.Fighters[p.SteamId] = new ArenaFighter { Inventory = SaveInventory( p ), Return = p.WorldPosition }; _data.Names[p.SteamId] = p.DisplayName; SaveData( _data ); Equip( p ); SendToSpawn( p ); Broadcast( $"{p.DisplayName} entered the arena ({ModeTitle}, {_data.Fighters.Count} fighting). Type /arena join to fight." ); } void Leave( RustPlayer p, bool tell, bool forget ) { var fighter = _data.Fighters[p.SteamId]; if ( forget ) _data.Fighters.Remove( p.SteamId ); SaveData( _data ); if ( p.IsDead ) Respawn( p ); RestoreInventory( p, fighter.Inventory ); p.Teleport( fighter.Return ); if ( tell ) Reply( p, $"Back with your things. This round: {fighter.Kills} kills, {fighter.Deaths} deaths." ); } /// Someone listed as a fighter who is not really fighting (restart, crash): give their things back. void SendBackIfStranded( RustPlayer p ) { if ( InArena( p ) ) Leave( p, true, true ); } /// The fighter's kit for the mode (the ffa kit, their random kit or their gun game level), then the armor: /// the weapon takes the first belt slot. void Equip( RustPlayer p ) { p.Inventory.Clear(); var kit = CurrentKit( _data.Fighters[p.SteamId] ); if ( kit != null ) foreach ( var item in kit.Items ) Give( p, item.Item, item.Amount ); foreach ( var item in _config.Armor ) Give( p, item.Item, item.Amount ); } ArenaKit CurrentKit( ArenaFighter f ) { if ( _config.Mode == "gungame" ) return _config.GunGameLevels.Count == 0 ? null : _config.GunGameLevels[Math.Min( f.Step, _config.GunGameLevels.Count - 1 )]; if ( _config.Mode == "randomkit" ) return _config.RandomKits.Count == 0 ? null : _config.RandomKits[f.Step % _config.RandomKits.Count]; return _config.Kit; } Vector3 RandomSpawn() => _data.Spawns[Game.Random.Int( 0, _data.Spawns.Count - 1 )] + Vector3.Up * 10f; void SendToSpawn( RustPlayer p ) { p.Teleport( RandomSpawn() ); _spawnedAt[p.SteamId] = RealTime.Now; } /// Scores back to zero; everyone fighting gets a fresh kit at a spawn point. void NewRound() { foreach ( var f in _data.Fighters.Values ) { f.Kills = 0; f.Deaths = 0; f.Step = 0; } SaveData( _data ); foreach ( var p in Players.Where( x => InArena( x ) && !x.IsDead ).ToList() ) { Equip( p ); SendToSpawn( p ); } } void Won( RustPlayer winner ) { _data.Wins[winner.SteamId] = Count( _data.Wins, winner.SteamId ) + 1; Broadcast( $"{winner.DisplayName} wins the arena round ({ModeTitle}). A new round starts." ); NewRound(); } void Top( RustPlayer p ) { if ( _data.Kills.Count == 0 ) { Reply( p, "No arena kill yet." ); return; } // plain loops: the script engine does not infer LINQ lambdas over KeyValuePair var left = new List( _data.Kills.Keys ); for ( var rank = 1; rank <= 5 && left.Count > 0; rank++ ) { var best = left[0]; foreach ( var id in left ) if ( _data.Kills[id] > _data.Kills[best] ) best = id; left.Remove( best ); var name = _data.Names.ContainsKey( best ) ? _data.Names[best] : best; Reply( p, $"{rank}. {name}: {_data.Kills[best]} kills, {Count( _data.Wins, best )} rounds won" ); } } // ------------------------------------------------------------------ hooks public override void OnPlayerConnected( RustPlayer player ) { SetButton( player, "menu", "Arena", "/arena menu", 3 ); SendBackIfStranded( player ); } /// Leaving the server from the arena: the real inventory is put back before the server saves the sleeper. public override void OnPlayerDisconnected( RustPlayer player ) { if ( !InArena( player ) ) return; var fighter = _data.Fighters[player.SteamId]; _data.Fighters.Remove( player.SteamId ); SaveData( _data ); RestoreInventory( player, fighter.Inventory ); player.WorldPosition = fighter.Return; // the player's machine is gone: no Teleport, the host moves the body itself } /// Fighters only hurt each other: nobody outside can hit them, they cannot hit anyone outside, bots and animals /// leave them alone, and nobody is hurt in the first seconds at a spawn point. Fighting works even with rl_pvp 0. public override bool? CanTakeDamage( RustPlayer victim, RustLegacy.DamageInfo info ) { var attacker = info.Attacker?.Components.GetInAncestorsOrSelf(); var victimIn = InArena( victim ); var attackerIn = attacker != null && InArena( attacker ); if ( victimIn && _spawnedAt.ContainsKey( victim.SteamId ) && RealTime.Now - _spawnedAt[victim.SteamId] < _config.SpawnProtection ) return false; if ( attacker != null && attacker != victim ) return victimIn && attackerIn; if ( victimIn && info.Attacker != null && attacker != victim ) return false; // bot, animal, someone's spike wall return null; } public override bool? CanDropDeathBag( RustPlayer player ) => InArena( player ) ? false : null; public override void OnPlayerDeath( RustPlayer player, RustLegacy.DamageInfo info ) { if ( !InArena( player ) ) return; var victim = _data.Fighters[player.SteamId]; victim.Deaths++; if ( _config.Mode == "randomkit" ) victim.Step++; var killer = info.Attacker?.Components.GetInAncestorsOrSelf(); RustPlayer winner = null; if ( killer != null && killer != player && InArena( killer ) ) { var f = _data.Fighters[killer.SteamId]; f.Kills++; _data.Kills[killer.SteamId] = Count( _data.Kills, killer.SteamId ) + 1; _data.Names[killer.SteamId] = killer.DisplayName; foreach ( var x in Players.Where( x => InArena( x ) ) ) Reply( x, $"{killer.DisplayName} killed {player.DisplayName}" ); if ( _config.Mode == "gungame" ) { f.Step++; if ( f.Step >= _config.GunGameLevels.Count ) winner = killer; else if ( !killer.IsDead ) { Equip( killer ); Reply( killer, $"Level {f.Step + 1}/{_config.GunGameLevels.Count}: {CurrentKit( f ).Name}" ); } } else if ( _config.KillsToWin > 0 && f.Kills >= _config.KillsToWin ) winner = killer; } SaveData( _data ); if ( winner != null ) Won( winner ); After( _config.RespawnDelay, () => Respawn( player ) ); } public override Vector3? OnPlayerRespawn( RustPlayer player ) => InArena( player ) && _data.Spawns.Count > 0 ? RandomSpawn() : null; public override void OnPlayerRespawned( RustPlayer player ) { if ( !InArena( player ) ) return; Equip( player ); _spawnedAt[player.SteamId] = RealTime.Now; } }