From f4bdf569fa62f1e6128dd2aacb9dee357d2beac7 Mon Sep 17 00:00:00 2001 From: bmgjet <50484759+bmgjet@users.noreply.github.com> Date: Wed, 7 Sep 2022 18:26:05 +1200 Subject: [PATCH] Spawns trains and wagons --- LazyRail.cs | 119 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 113 insertions(+), 6 deletions(-) diff --git a/LazyRail.cs b/LazyRail.cs index 73e4cd3..b891101 100644 --- a/LazyRail.cs +++ b/LazyRail.cs @@ -1,18 +1,30 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + namespace Oxide.Plugins { - [Info("LazyRail", "bmgjet", "0.0.2")] - [Description("Help Above ground rails")] + [Info("LazyRail", "bmgjet", "0.0.3")] + [Description("Help above ground rails function on custom maps")] class LazyRail : RustPlugin { - #region Variables public bool DisablePlayerViolationsWhenOnTrain = true; public bool DisablePlayerSuicideWhenOnTrain = true; public bool DisableDecayOnTrain = true; public bool TrainUnlimitedFuel = true; + public int WagonAmount = 60; + public int WagonsPerTrain = 10; + public int ScanDelaySeconds = 300; + public int SidingMinLength = 20; + public int SidingMaxLength = 50; public bool ShowDebug = false; - #endregion + public List wagons = new List() { "assets/content/vehicles/train/trainwagonunloadable.entity.prefab", "assets/content/vehicles/train/trainwagonunloadablefuel.entity.prefab", "assets/content/vehicles/train/trainwagonunloadableloot.entity.prefab", }; + public List trains = new List(); + private List Threads = new List(); + private List railnodes = new List(); + private int Spawned = 0; - #region Hooks private object OnPlayerViolation(BasePlayer player, AntiHackType type, float amount) { if (DisablePlayerViolationsWhenOnTrain && player != null) @@ -29,6 +41,85 @@ namespace Oxide.Plugins return null; } + void OnServerInitialized() { if (WagonAmount != 0) { Threads.Add(ServerMgr.Instance.StartCoroutine(TrainChecker())); } } + + private void Unload() + { + if (Threads != null && Threads.Count != 0) + { + foreach (Coroutine co in Threads) { if (co != null) ServerMgr.Instance.StopCoroutine(co); } + Threads = null; + } + foreach (TrainCar t in trains) + { + if (!t.IsDestroyed) { t.Kill(); } + } + } + + private IEnumerator TrainChecker() + { + int checks = 0; + var _instruction = ConVar.FPS.limit > 80 ? CoroutineEx.waitForSeconds(0.01f) : null; + foreach (PathList pathList in World.GetPaths("Rail").AsEnumerable()) + { + if (pathList.Path.Points.Count() < SidingMaxLength && pathList.Path.Points.Count() > SidingMinLength) + { + for (int i = 10; i < pathList.Path.Points.Count() - 10; i++) + { + if (++checks >= 1000) + { + checks = 0; + yield return _instruction; + } + railnodes.Add(pathList.Path.Points[i]); + } + } + } + if (railnodes.Count > 30) { Puts("Found " + railnodes.Count + " Side rails"); } + else + { + Puts("Not enough Side rails found"); + yield break; + } + trains.Clear(); + while (true) + { + for (int i = 0; i < trains.Count; i++) + { + try + { + if (trains[i] == null || trains[i].IsDestroyed) + { + if (ShowDebug) { Puts("Removed Dead wagon reference"); } + trains.RemoveAt(i); + } + } + catch { } + } + if (trains.Count < WagonAmount) + { + foreach (BaseEntity bn in BaseEntity.serverEntities.entityList.Values) + { + if (bn == null) continue; + if (bn is TrainCar) { if (!trains.Contains(bn as TrainCar)) { trains.Add(bn as TrainCar); } } + if (++checks >= 500) + { + checks = 0; + yield return _instruction; + } + } + } + for (int i2 = 0; i2 < WagonAmount - trains.Count; i2++) { SpawnEntity(railnodes.GetRandom()); } + yield return CoroutineEx.waitForSeconds(ScanDelaySeconds); + } + } + + [ChatCommand("showsides")] + private void ShowsideCmd(BasePlayer player) { if (!player.IsAdmin) { return; } foreach (Vector3 v in railnodes) { if (Vector3.Distance(v, player.transform.position) < 500) { player.SendConsoleCommand("ddraw.sphere", 8f, Color.blue, v, 1f); } } } + + [ChatCommand("showwagons")] + private void ShowwagonCmd(BasePlayer player) { if (!player.IsAdmin) { return; } foreach (TrainCar v in trains) { if (v != null) { player.SendConsoleCommand("ddraw.sphere", 8f, Color.blue, v.transform.position, 1f); } } } + private object OnEntityTakeDamage(BasePlayer player, HitInfo hitInfo) { if (DisablePlayerSuicideWhenOnTrain && player != null || hitInfo != null) @@ -41,6 +132,23 @@ namespace Oxide.Plugins return null; } + private bool SpawnEntity(Vector3 position) + { + string prefab; + if (Spawned >= WagonsPerTrain) { prefab = "assets/content/vehicles/locomotive/locomotive.entity.prefab"; Spawned = 0; } + else { prefab = wagons.GetRandom(); } + TrainCar trainCar = GameManager.server.CreateEntity(prefab, position) as TrainCar; + trainCar.enableSaving = false; + trainCar.platformParentTrigger.ParentNPCPlayers = true; + trainCar.Spawn(); + trainCar.transform.position = position; + if (trainCar == null || trainCar.IsDestroyed) { return false; } + if (ShowDebug) { Puts("Spawned wagon @ " + trainCar.transform.position); } + trains.Add(trainCar); + Spawned++; + return true; + } + private void OnEntitySpawned(BaseEntity baseEntity) { TrainCar _trainCar = baseEntity as TrainCar; @@ -71,6 +179,5 @@ namespace Oxide.Plugins } } } - #endregion } } \ No newline at end of file