BorgSystem.Modules.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using System.Linq;
  2. using Content.Shared.Hands.Components;
  3. using Content.Shared.Interaction.Components;
  4. using Content.Shared.Silicons.Borgs.Components;
  5. using Content.Shared.Whitelist;
  6. using Robust.Shared.Containers;
  7. namespace Content.Server.Silicons.Borgs;
  8. /// <inheritdoc/>
  9. public sealed partial class BorgSystem
  10. {
  11. public void InitializeModules()
  12. {
  13. SubscribeLocalEvent<BorgModuleComponent, EntGotInsertedIntoContainerMessage>(OnModuleGotInserted);
  14. SubscribeLocalEvent<BorgModuleComponent, EntGotRemovedFromContainerMessage>(OnModuleGotRemoved);
  15. SubscribeLocalEvent<SelectableBorgModuleComponent, BorgModuleInstalledEvent>(OnSelectableInstalled);
  16. SubscribeLocalEvent<SelectableBorgModuleComponent, BorgModuleUninstalledEvent>(OnSelectableUninstalled);
  17. SubscribeLocalEvent<SelectableBorgModuleComponent, BorgModuleActionSelectedEvent>(OnSelectableAction);
  18. SubscribeLocalEvent<ItemBorgModuleComponent, ComponentStartup>(OnProvideItemStartup);
  19. SubscribeLocalEvent<ItemBorgModuleComponent, BorgModuleSelectedEvent>(OnItemModuleSelected);
  20. SubscribeLocalEvent<ItemBorgModuleComponent, BorgModuleUnselectedEvent>(OnItemModuleUnselected);
  21. }
  22. private void OnModuleGotInserted(EntityUid uid, BorgModuleComponent component, EntGotInsertedIntoContainerMessage args)
  23. {
  24. var chassis = args.Container.Owner;
  25. if (!TryComp<BorgChassisComponent>(chassis, out var chassisComp) ||
  26. args.Container != chassisComp.ModuleContainer ||
  27. !Toggle.IsActivated(chassis))
  28. return;
  29. if (!_powerCell.HasDrawCharge(uid))
  30. return;
  31. InstallModule(chassis, uid, chassisComp, component);
  32. }
  33. private void OnModuleGotRemoved(EntityUid uid, BorgModuleComponent component, EntGotRemovedFromContainerMessage args)
  34. {
  35. var chassis = args.Container.Owner;
  36. if (!TryComp<BorgChassisComponent>(chassis, out var chassisComp) ||
  37. args.Container != chassisComp.ModuleContainer)
  38. return;
  39. UninstallModule(chassis, uid, chassisComp, component);
  40. }
  41. private void OnProvideItemStartup(EntityUid uid, ItemBorgModuleComponent component, ComponentStartup args)
  42. {
  43. component.ProvidedContainer = Container.EnsureContainer<Container>(uid, component.ProvidedContainerId);
  44. }
  45. private void OnSelectableInstalled(EntityUid uid, SelectableBorgModuleComponent component, ref BorgModuleInstalledEvent args)
  46. {
  47. var chassis = args.ChassisEnt;
  48. if (_actions.AddAction(chassis, ref component.ModuleSwapActionEntity, out var action, component.ModuleSwapActionId, uid))
  49. {
  50. if(TryComp<BorgModuleIconComponent>(uid, out var moduleIconComp))
  51. {
  52. action.Icon = moduleIconComp.Icon;
  53. };
  54. action.EntityIcon = uid;
  55. Dirty(component.ModuleSwapActionEntity.Value, action);
  56. }
  57. if (!TryComp(chassis, out BorgChassisComponent? chassisComp))
  58. return;
  59. if (chassisComp.SelectedModule == null)
  60. SelectModule(chassis, uid, chassisComp, component);
  61. }
  62. private void OnSelectableUninstalled(EntityUid uid, SelectableBorgModuleComponent component, ref BorgModuleUninstalledEvent args)
  63. {
  64. var chassis = args.ChassisEnt;
  65. _actions.RemoveProvidedActions(chassis, uid);
  66. if (!TryComp(chassis, out BorgChassisComponent? chassisComp))
  67. return;
  68. if (chassisComp.SelectedModule == uid)
  69. UnselectModule(chassis, chassisComp);
  70. }
  71. private void OnSelectableAction(EntityUid uid, SelectableBorgModuleComponent component, BorgModuleActionSelectedEvent args)
  72. {
  73. var chassis = args.Performer;
  74. if (!TryComp<BorgChassisComponent>(chassis, out var chassisComp))
  75. return;
  76. var selected = chassisComp.SelectedModule;
  77. args.Handled = true;
  78. UnselectModule(chassis, chassisComp);
  79. if (selected != uid)
  80. {
  81. SelectModule(chassis, uid, chassisComp, component);
  82. }
  83. }
  84. /// <summary>
  85. /// Selects a module, enabling the borg to use its provided abilities.
  86. /// </summary>
  87. public void SelectModule(EntityUid chassis,
  88. EntityUid moduleUid,
  89. BorgChassisComponent? chassisComp = null,
  90. SelectableBorgModuleComponent? selectable = null,
  91. BorgModuleComponent? moduleComp = null)
  92. {
  93. if (LifeStage(chassis) >= EntityLifeStage.Terminating)
  94. return;
  95. if (!Resolve(chassis, ref chassisComp))
  96. return;
  97. if (!Resolve(moduleUid, ref moduleComp) || !moduleComp.Installed || moduleComp.InstalledEntity != chassis)
  98. {
  99. Log.Error($"{ToPrettyString(chassis)} attempted to select uninstalled module {ToPrettyString(moduleUid)}");
  100. return;
  101. }
  102. if (selectable == null && !HasComp<SelectableBorgModuleComponent>(moduleUid))
  103. {
  104. Log.Error($"{ToPrettyString(chassis)} attempted to select invalid module {ToPrettyString(moduleUid)}");
  105. return;
  106. }
  107. if (!chassisComp.ModuleContainer.Contains(moduleUid))
  108. {
  109. Log.Error($"{ToPrettyString(chassis)} does not contain the installed module {ToPrettyString(moduleUid)}");
  110. return;
  111. }
  112. if (chassisComp.SelectedModule != null)
  113. return;
  114. if (chassisComp.SelectedModule == moduleUid)
  115. return;
  116. UnselectModule(chassis, chassisComp);
  117. var ev = new BorgModuleSelectedEvent(chassis);
  118. RaiseLocalEvent(moduleUid, ref ev);
  119. chassisComp.SelectedModule = moduleUid;
  120. Dirty(chassis, chassisComp);
  121. }
  122. /// <summary>
  123. /// Unselects a module, removing its provided abilities
  124. /// </summary>
  125. public void UnselectModule(EntityUid chassis, BorgChassisComponent? chassisComp = null)
  126. {
  127. if (LifeStage(chassis) >= EntityLifeStage.Terminating)
  128. return;
  129. if (!Resolve(chassis, ref chassisComp))
  130. return;
  131. if (chassisComp.SelectedModule == null)
  132. return;
  133. var ev = new BorgModuleUnselectedEvent(chassis);
  134. RaiseLocalEvent(chassisComp.SelectedModule.Value, ref ev);
  135. chassisComp.SelectedModule = null;
  136. Dirty(chassis, chassisComp);
  137. }
  138. private void OnItemModuleSelected(EntityUid uid, ItemBorgModuleComponent component, ref BorgModuleSelectedEvent args)
  139. {
  140. ProvideItems(args.Chassis, uid, component: component);
  141. }
  142. private void OnItemModuleUnselected(EntityUid uid, ItemBorgModuleComponent component, ref BorgModuleUnselectedEvent args)
  143. {
  144. RemoveProvidedItems(args.Chassis, uid, component: component);
  145. }
  146. private void ProvideItems(EntityUid chassis, EntityUid uid, BorgChassisComponent? chassisComponent = null, ItemBorgModuleComponent? component = null)
  147. {
  148. if (!Resolve(chassis, ref chassisComponent) || !Resolve(uid, ref component))
  149. return;
  150. if (!TryComp<HandsComponent>(chassis, out var hands))
  151. return;
  152. var xform = Transform(chassis);
  153. foreach (var itemProto in component.Items)
  154. {
  155. EntityUid item;
  156. if (!component.ItemsCreated)
  157. {
  158. item = Spawn(itemProto, xform.Coordinates);
  159. }
  160. else
  161. {
  162. item = component.ProvidedContainer.ContainedEntities
  163. .FirstOrDefault(ent => Prototype(ent)?.ID == itemProto);
  164. if (!item.IsValid())
  165. {
  166. Log.Debug($"no items found: {component.ProvidedContainer.ContainedEntities.Count}");
  167. continue;
  168. }
  169. _container.Remove(item, component.ProvidedContainer, force: true);
  170. }
  171. if (!item.IsValid())
  172. {
  173. Log.Debug("no valid item");
  174. continue;
  175. }
  176. var handId = $"{uid}-item{component.HandCounter}";
  177. component.HandCounter++;
  178. _hands.AddHand(chassis, handId, HandLocation.Middle, hands);
  179. _hands.DoPickup(chassis, hands.Hands[handId], item, hands);
  180. EnsureComp<UnremoveableComponent>(item);
  181. component.ProvidedItems.Add(handId, item);
  182. }
  183. component.ItemsCreated = true;
  184. }
  185. private void RemoveProvidedItems(EntityUid chassis, EntityUid uid, BorgChassisComponent? chassisComponent = null, ItemBorgModuleComponent? component = null)
  186. {
  187. if (!Resolve(chassis, ref chassisComponent) || !Resolve(uid, ref component))
  188. return;
  189. if (!TryComp<HandsComponent>(chassis, out var hands))
  190. return;
  191. if (TerminatingOrDeleted(uid))
  192. {
  193. foreach (var (hand, item) in component.ProvidedItems)
  194. {
  195. QueueDel(item);
  196. _hands.RemoveHand(chassis, hand, hands);
  197. }
  198. component.ProvidedItems.Clear();
  199. return;
  200. }
  201. foreach (var (handId, item) in component.ProvidedItems)
  202. {
  203. if (LifeStage(item) <= EntityLifeStage.MapInitialized)
  204. {
  205. RemComp<UnremoveableComponent>(item);
  206. _container.Insert(item, component.ProvidedContainer);
  207. }
  208. _hands.RemoveHand(chassis, handId, hands);
  209. }
  210. component.ProvidedItems.Clear();
  211. }
  212. /// <summary>
  213. /// Checks if a given module can be inserted into a borg
  214. /// </summary>
  215. public bool CanInsertModule(EntityUid uid, EntityUid module, BorgChassisComponent? component = null, BorgModuleComponent? moduleComponent = null, EntityUid? user = null)
  216. {
  217. if (!Resolve(uid, ref component) || !Resolve(module, ref moduleComponent))
  218. return false;
  219. if (component.ModuleContainer.ContainedEntities.Count >= component.MaxModules)
  220. {
  221. if (user != null)
  222. Popup.PopupEntity(Loc.GetString("borg-module-too-many"), uid, user.Value);
  223. return false;
  224. }
  225. if (_whitelistSystem.IsWhitelistFail(component.ModuleWhitelist, module))
  226. {
  227. if (user != null)
  228. Popup.PopupEntity(Loc.GetString("borg-module-whitelist-deny"), uid, user.Value);
  229. return false;
  230. }
  231. if (TryComp<ItemBorgModuleComponent>(module, out var itemModuleComp))
  232. {
  233. foreach (var containedModuleUid in component.ModuleContainer.ContainedEntities)
  234. {
  235. if (!TryComp<ItemBorgModuleComponent>(containedModuleUid, out var containedItemModuleComp))
  236. continue;
  237. if (containedItemModuleComp.Items.Count == itemModuleComp.Items.Count &&
  238. containedItemModuleComp.Items.All(itemModuleComp.Items.Contains))
  239. {
  240. if (user != null)
  241. Popup.PopupEntity(Loc.GetString("borg-module-duplicate"), uid, user.Value);
  242. return false;
  243. }
  244. }
  245. }
  246. return true;
  247. }
  248. /// <summary>
  249. /// Check if a module can be removed from a borg.
  250. /// </summary>
  251. /// <param name="borg">The borg that the module is being removed from.</param>
  252. /// <param name="module">The module to remove from the borg.</param>
  253. /// <param name="user">The user attempting to remove the module.</param>
  254. /// <returns>True if the module can be removed.</returns>
  255. public bool CanRemoveModule(
  256. Entity<BorgChassisComponent> borg,
  257. Entity<BorgModuleComponent> module,
  258. EntityUid? user = null)
  259. {
  260. if (module.Comp.DefaultModule)
  261. return false;
  262. return true;
  263. }
  264. /// <summary>
  265. /// Installs and activates all modules currently inside the borg's module container
  266. /// </summary>
  267. public void InstallAllModules(EntityUid uid, BorgChassisComponent? component = null)
  268. {
  269. if (!Resolve(uid, ref component))
  270. return;
  271. var query = GetEntityQuery<BorgModuleComponent>();
  272. foreach (var moduleEnt in new List<EntityUid>(component.ModuleContainer.ContainedEntities))
  273. {
  274. if (!query.TryGetComponent(moduleEnt, out var moduleComp))
  275. continue;
  276. InstallModule(uid, moduleEnt, component, moduleComp);
  277. }
  278. }
  279. /// <summary>
  280. /// Deactivates all modules currently inside the borg's module container
  281. /// </summary>
  282. /// <param name="uid"></param>
  283. /// <param name="component"></param>
  284. public void DisableAllModules(EntityUid uid, BorgChassisComponent? component = null)
  285. {
  286. if (!Resolve(uid, ref component))
  287. return;
  288. var query = GetEntityQuery<BorgModuleComponent>();
  289. foreach (var moduleEnt in new List<EntityUid>(component.ModuleContainer.ContainedEntities))
  290. {
  291. if (!query.TryGetComponent(moduleEnt, out var moduleComp))
  292. continue;
  293. UninstallModule(uid, moduleEnt, component, moduleComp);
  294. }
  295. }
  296. /// <summary>
  297. /// Installs a single module into a borg.
  298. /// </summary>
  299. public void InstallModule(EntityUid uid, EntityUid module, BorgChassisComponent? component, BorgModuleComponent? moduleComponent = null)
  300. {
  301. if (!Resolve(uid, ref component) || !Resolve(module, ref moduleComponent))
  302. return;
  303. if (moduleComponent.Installed)
  304. return;
  305. moduleComponent.InstalledEntity = uid;
  306. var ev = new BorgModuleInstalledEvent(uid);
  307. RaiseLocalEvent(module, ref ev);
  308. }
  309. /// <summary>
  310. /// Uninstalls a single module from a borg.
  311. /// </summary>
  312. public void UninstallModule(EntityUid uid, EntityUid module, BorgChassisComponent? component, BorgModuleComponent? moduleComponent = null)
  313. {
  314. if (!Resolve(uid, ref component) || !Resolve(module, ref moduleComponent))
  315. return;
  316. if (!moduleComponent.Installed)
  317. return;
  318. moduleComponent.InstalledEntity = null;
  319. var ev = new BorgModuleUninstalledEvent(uid);
  320. RaiseLocalEvent(module, ref ev);
  321. }
  322. /// <summary>
  323. /// Sets <see cref="BorgChassisComponent.MaxModules"/>.
  324. /// </summary>
  325. /// <param name="ent">The borg to modify.</param>
  326. /// <param name="maxModules">The new max module count.</param>
  327. public void SetMaxModules(Entity<BorgChassisComponent> ent, int maxModules)
  328. {
  329. ent.Comp.MaxModules = maxModules;
  330. }
  331. /// <summary>
  332. /// Sets <see cref="BorgChassisComponent.ModuleWhitelist"/>.
  333. /// </summary>
  334. /// <param name="ent">The borg to modify.</param>
  335. /// <param name="whitelist">The new module whitelist.</param>
  336. public void SetModuleWhitelist(Entity<BorgChassisComponent> ent, EntityWhitelist? whitelist)
  337. {
  338. ent.Comp.ModuleWhitelist = whitelist;
  339. }
  340. }