CargoSystem.Shuttle.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using Content.Server.Cargo.Components;
  2. using Content.Shared.Stacks;
  3. using Content.Shared.Cargo;
  4. using Content.Shared.Cargo.BUI;
  5. using Content.Shared.Cargo.Components;
  6. using Content.Shared.Cargo.Events;
  7. using Content.Shared.GameTicking;
  8. using Robust.Shared.Map;
  9. using Robust.Shared.Random;
  10. using Robust.Shared.Audio;
  11. namespace Content.Server.Cargo.Systems;
  12. public sealed partial class CargoSystem
  13. {
  14. /*
  15. * Handles cargo shuttle / trade mechanics.
  16. */
  17. private static readonly SoundPathSpecifier ApproveSound = new("/Audio/Effects/Cargo/ping.ogg");
  18. private void InitializeShuttle()
  19. {
  20. SubscribeLocalEvent<TradeStationComponent, GridSplitEvent>(OnTradeSplit);
  21. SubscribeLocalEvent<CargoShuttleConsoleComponent, ComponentStartup>(OnCargoShuttleConsoleStartup);
  22. SubscribeLocalEvent<CargoPalletConsoleComponent, CargoPalletSellMessage>(OnPalletSale);
  23. SubscribeLocalEvent<CargoPalletConsoleComponent, CargoPalletAppraiseMessage>(OnPalletAppraise);
  24. SubscribeLocalEvent<CargoPalletConsoleComponent, BoundUIOpenedEvent>(OnPalletUIOpen);
  25. SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestart);
  26. }
  27. #region Console
  28. private void UpdateCargoShuttleConsoles(EntityUid shuttleUid, CargoShuttleComponent _)
  29. {
  30. // Update pilot consoles that are already open.
  31. _console.RefreshDroneConsoles();
  32. // Update order consoles.
  33. var shuttleConsoleQuery = AllEntityQuery<CargoShuttleConsoleComponent>();
  34. while (shuttleConsoleQuery.MoveNext(out var uid, out var _))
  35. {
  36. var stationUid = _station.GetOwningStation(uid);
  37. if (stationUid != shuttleUid)
  38. continue;
  39. UpdateShuttleState(uid, stationUid);
  40. }
  41. }
  42. private void UpdatePalletConsoleInterface(EntityUid uid)
  43. {
  44. if (Transform(uid).GridUid is not EntityUid gridUid)
  45. {
  46. _uiSystem.SetUiState(uid, CargoPalletConsoleUiKey.Sale,
  47. new CargoPalletConsoleInterfaceState(0, 0, false));
  48. return;
  49. }
  50. GetPalletGoods(gridUid, out var toSell, out var amount);
  51. _uiSystem.SetUiState(uid, CargoPalletConsoleUiKey.Sale,
  52. new CargoPalletConsoleInterfaceState((int) amount, toSell.Count, true));
  53. }
  54. private void OnPalletUIOpen(EntityUid uid, CargoPalletConsoleComponent component, BoundUIOpenedEvent args)
  55. {
  56. UpdatePalletConsoleInterface(uid);
  57. }
  58. /// <summary>
  59. /// Ok so this is just the same thing as opening the UI, its a refresh button.
  60. /// I know this would probably feel better if it were like predicted and dynamic as pallet contents change
  61. /// However.
  62. /// I dont want it to explode if cargo uses a conveyor to move 8000 pineapple slices or whatever, they are
  63. /// known for their entity spam i wouldnt put it past them
  64. /// </summary>
  65. private void OnPalletAppraise(EntityUid uid, CargoPalletConsoleComponent component, CargoPalletAppraiseMessage args)
  66. {
  67. UpdatePalletConsoleInterface(uid);
  68. }
  69. private void OnCargoShuttleConsoleStartup(EntityUid uid, CargoShuttleConsoleComponent component, ComponentStartup args)
  70. {
  71. var station = _station.GetOwningStation(uid);
  72. UpdateShuttleState(uid, station);
  73. }
  74. private void UpdateShuttleState(EntityUid uid, EntityUid? station = null)
  75. {
  76. TryComp<StationCargoOrderDatabaseComponent>(station, out var orderDatabase);
  77. TryComp<CargoShuttleComponent>(orderDatabase?.Shuttle, out var shuttle);
  78. var orders = GetProjectedOrders(station ?? EntityUid.Invalid, orderDatabase, shuttle);
  79. var shuttleName = orderDatabase?.Shuttle != null ? MetaData(orderDatabase.Shuttle.Value).EntityName : string.Empty;
  80. if (_uiSystem.HasUi(uid, CargoConsoleUiKey.Shuttle))
  81. _uiSystem.SetUiState(uid, CargoConsoleUiKey.Shuttle, new CargoShuttleConsoleBoundUserInterfaceState(
  82. station != null ? MetaData(station.Value).EntityName : Loc.GetString("cargo-shuttle-console-station-unknown"),
  83. string.IsNullOrEmpty(shuttleName) ? Loc.GetString("cargo-shuttle-console-shuttle-not-found") : shuttleName,
  84. orders
  85. ));
  86. }
  87. #endregion
  88. private void OnTradeSplit(EntityUid uid, TradeStationComponent component, ref GridSplitEvent args)
  89. {
  90. // If the trade station gets bombed it's still a trade station.
  91. foreach (var gridUid in args.NewGrids)
  92. {
  93. EnsureComp<TradeStationComponent>(gridUid);
  94. }
  95. }
  96. #region Shuttle
  97. /// <summary>
  98. /// Returns the orders that can fit on the cargo shuttle.
  99. /// </summary>
  100. private List<CargoOrderData> GetProjectedOrders(
  101. EntityUid shuttleUid,
  102. StationCargoOrderDatabaseComponent? component = null,
  103. CargoShuttleComponent? shuttle = null)
  104. {
  105. var orders = new List<CargoOrderData>();
  106. if (component == null || shuttle == null || component.Orders.Count == 0)
  107. return orders;
  108. var spaceRemaining = GetCargoSpace(shuttleUid);
  109. for (var i = 0; i < component.Orders.Count && spaceRemaining > 0; i++)
  110. {
  111. var order = component.Orders[i];
  112. if (order.Approved)
  113. {
  114. var numToShip = order.OrderQuantity - order.NumDispatched;
  115. if (numToShip > spaceRemaining)
  116. {
  117. // We won't be able to fit the whole order on, so make one
  118. // which represents the space we do have left:
  119. var reducedOrder = new CargoOrderData(order.OrderId,
  120. order.ProductId, order.ProductName, order.Price, spaceRemaining, order.Requester, order.Reason);
  121. orders.Add(reducedOrder);
  122. }
  123. else
  124. {
  125. orders.Add(order);
  126. }
  127. spaceRemaining -= numToShip;
  128. }
  129. }
  130. return orders;
  131. }
  132. /// <summary>
  133. /// Get the amount of space the cargo shuttle can fit for orders.
  134. /// </summary>
  135. private int GetCargoSpace(EntityUid gridUid)
  136. {
  137. var space = GetCargoPallets(gridUid, BuySellType.Buy).Count;
  138. return space;
  139. }
  140. /// GetCargoPallets(gridUid, BuySellType.Sell) to return only Sell pads
  141. /// GetCargoPallets(gridUid, BuySellType.Buy) to return only Buy pads
  142. private List<(EntityUid Entity, CargoPalletComponent Component, TransformComponent PalletXform)> GetCargoPallets(EntityUid gridUid, BuySellType requestType = BuySellType.All)
  143. {
  144. _pads.Clear();
  145. var query = AllEntityQuery<CargoPalletComponent, TransformComponent>();
  146. while (query.MoveNext(out var uid, out var comp, out var compXform))
  147. {
  148. if (compXform.ParentUid != gridUid ||
  149. !compXform.Anchored)
  150. {
  151. continue;
  152. }
  153. if ((requestType & comp.PalletType) == 0)
  154. {
  155. continue;
  156. }
  157. _pads.Add((uid, comp, compXform));
  158. }
  159. return _pads;
  160. }
  161. private List<(EntityUid Entity, CargoPalletComponent Component, TransformComponent Transform)>
  162. GetFreeCargoPallets(EntityUid gridUid,
  163. List<(EntityUid Entity, CargoPalletComponent Component, TransformComponent Transform)> pallets)
  164. {
  165. _setEnts.Clear();
  166. List<(EntityUid Entity, CargoPalletComponent Component, TransformComponent Transform)> outList = new();
  167. foreach (var pallet in pallets)
  168. {
  169. var aabb = _lookup.GetAABBNoContainer(pallet.Entity, pallet.Transform.LocalPosition, pallet.Transform.LocalRotation);
  170. if (_lookup.AnyLocalEntitiesIntersecting(gridUid, aabb, LookupFlags.Dynamic))
  171. continue;
  172. outList.Add(pallet);
  173. }
  174. return outList;
  175. }
  176. #endregion
  177. #region Station
  178. private bool SellPallets(EntityUid gridUid, out double amount)
  179. {
  180. GetPalletGoods(gridUid, out var toSell, out amount);
  181. Log.Debug($"Cargo sold {toSell.Count} entities for {amount}");
  182. if (toSell.Count == 0)
  183. return false;
  184. var ev = new EntitySoldEvent(toSell);
  185. RaiseLocalEvent(ref ev);
  186. foreach (var ent in toSell)
  187. {
  188. Del(ent);
  189. }
  190. return true;
  191. }
  192. private void GetPalletGoods(EntityUid gridUid, out HashSet<EntityUid> toSell, out double amount)
  193. {
  194. amount = 0;
  195. toSell = new HashSet<EntityUid>();
  196. foreach (var (palletUid, _, _) in GetCargoPallets(gridUid, BuySellType.Sell))
  197. {
  198. // Containers should already get the sell price of their children so can skip those.
  199. _setEnts.Clear();
  200. _lookup.GetEntitiesIntersecting(palletUid, _setEnts,
  201. LookupFlags.Dynamic | LookupFlags.Sundries);
  202. foreach (var ent in _setEnts)
  203. {
  204. // Dont sell:
  205. // - anything already being sold
  206. // - anything anchored (e.g. light fixtures)
  207. // - anything blacklisted (e.g. players).
  208. if (toSell.Contains(ent) ||
  209. _xformQuery.TryGetComponent(ent, out var xform) &&
  210. (xform.Anchored || !CanSell(ent, xform)))
  211. {
  212. continue;
  213. }
  214. if (_blacklistQuery.HasComponent(ent))
  215. continue;
  216. var price = _pricing.GetPrice(ent);
  217. if (price == 0)
  218. continue;
  219. toSell.Add(ent);
  220. amount += price;
  221. }
  222. }
  223. }
  224. private bool CanSell(EntityUid uid, TransformComponent xform)
  225. {
  226. if (_mobQuery.HasComponent(uid))
  227. {
  228. return false;
  229. }
  230. var complete = IsBountyComplete(uid, out var bountyEntities);
  231. // Recursively check for mobs at any point.
  232. var children = xform.ChildEnumerator;
  233. while (children.MoveNext(out var child))
  234. {
  235. if (complete && bountyEntities.Contains(child))
  236. continue;
  237. if (!CanSell(child, _xformQuery.GetComponent(child)))
  238. return false;
  239. }
  240. return true;
  241. }
  242. private void OnPalletSale(EntityUid uid, CargoPalletConsoleComponent component, CargoPalletSellMessage args)
  243. {
  244. var xform = Transform(uid);
  245. if (xform.GridUid is not EntityUid gridUid)
  246. {
  247. _uiSystem.SetUiState(uid, CargoPalletConsoleUiKey.Sale,
  248. new CargoPalletConsoleInterfaceState(0, 0, false));
  249. return;
  250. }
  251. if (!SellPallets(gridUid, out var price))
  252. return;
  253. var stackPrototype = _protoMan.Index<StackPrototype>(component.CashType);
  254. _stack.Spawn((int) price, stackPrototype, xform.Coordinates);
  255. _audio.PlayPvs(ApproveSound, uid);
  256. UpdatePalletConsoleInterface(uid);
  257. }
  258. #endregion
  259. private void OnRoundRestart(RoundRestartCleanupEvent ev)
  260. {
  261. Reset();
  262. }
  263. }
  264. /// <summary>
  265. /// Event broadcast raised by-ref before it is sold and
  266. /// deleted but after the price has been calculated.
  267. /// </summary>
  268. [ByRefEvent]
  269. public readonly record struct EntitySoldEvent(HashSet<EntityUid> Sold);