Files
wwdpublic/Content.Server/Salvage/SalvageSystem.Magnet.cs
DEATHB4DEFEAT 47b10a01b0 Catch-Up Cherry Pick 2 (#944)
# Description

Picked 400 commits (and skipped many, many more) from WizDen since #540.
Stopped at commit 332f54a3aebe669f6e50d26e7b047f0bdc28e0fb (Lobby
Refactor).

---

# TODO

- [x] Pick
- [x] Compile
- [x] Fix runtime errors
- [ ] Fix up humanoid profile editor
- [ ] Test everything

---

# Changelog

🆑
- add: Merged 400 WizDen PRs. Happy testing!

---------

Co-authored-by: Plykiya <58439124+Plykiya@users.noreply.github.com>
Co-authored-by: FungiFellow <151778459+FungiFellow@users.noreply.github.com>
Co-authored-by: osjarw <62134478+osjarw@users.noreply.github.com>
Co-authored-by: Ubaser <134914314+UbaserB@users.noreply.github.com>
Co-authored-by: beck-thompson <107373427+beck-thompson@users.noreply.github.com>
Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Co-authored-by: Magnus Larsen <i.am.larsenml@gmail.com>
Co-authored-by: Hanz <41141796+Hanzdegloker@users.noreply.github.com>
Co-authored-by: Kukutis96513 <146854220+Kukutis96513@users.noreply.github.com>
Co-authored-by: potato1234_x <79580518+potato1234x@users.noreply.github.com>
Co-authored-by: Gotimanga <127038462+Gotimanga@users.noreply.github.com>
Co-authored-by: Mangohydra <156087924+Mangohydra@users.noreply.github.com>
Co-authored-by: TsjipTsjip <19798667+TsjipTsjip@users.noreply.github.com>
Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com>
Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>
Co-authored-by: Morb <14136326+Morb0@users.noreply.github.com>
Co-authored-by: MilenVolf <63782763+MilenVolf@users.noreply.github.com>
Co-authored-by: KrasnoshchekovPavel <119816022+KrasnoshchekovPavel@users.noreply.github.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
Co-authored-by: DrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com>
Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
Co-authored-by: KittenColony <149278380+KittenColony@users.noreply.github.com>
Co-authored-by: ShadowCommander <shadowjjt@gmail.com>
Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com>
Co-authored-by: T-Stalker <43253663+DogZeroX@users.noreply.github.com>
Co-authored-by: ERROR404 <100093430+ERORR404V1@users.noreply.github.com>
Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com>
Co-authored-by: Jezithyr <jezithyr@gmail.com>
Co-authored-by: Psychpsyo <60073468+Psychpsyo@users.noreply.github.com>
Co-authored-by: no <165581243+pissdemon@users.noreply.github.com>
Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com>
Co-authored-by: Ciac32 <aknoxlor@gmail.com>
Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: NotSoDana <75203942+NotSoDana@users.noreply.github.com>
Co-authored-by: Simon <63975668+Simyon264@users.noreply.github.com>
Co-authored-by: Repo <47093363+Titian3@users.noreply.github.com>
Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
Co-authored-by: nao fujiwara <awkwarddryad@gmail.com>
Co-authored-by: Michael <107807667+Doc-Michael@users.noreply.github.com>
Co-authored-by: Vasilis <vasilis@pikachu.systems>
Co-authored-by: Lamrr <96937466+Lamrr@users.noreply.github.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Co-authored-by: Jay <67732946+DuskyJay@users.noreply.github.com>
Co-authored-by: Just-a-Unity-Dev <67359748+Just-a-Unity-Dev@users.noreply.github.com>
Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com>
Co-authored-by: Flareguy <78941145+Flareguy@users.noreply.github.com>
Co-authored-by: Tyzemol <85772526+Tyzemol@users.noreply.github.com>
Co-authored-by: Alzore <140123969+Blackern5000@users.noreply.github.com>
Co-authored-by: Pok <113675512+Pok27@users.noreply.github.com>
Co-authored-by: RumiTiger <154005209+RumiTiger@users.noreply.github.com>
Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
Co-authored-by: Killerqu00 <47712032+Killerqu00@users.noreply.github.com>
Co-authored-by: Ty Ashley <42426760+TyAshley@users.noreply.github.com>
Co-authored-by: exincore <me@exin.xyz>
Co-authored-by: 0x6273 <0x40@keemail.me>
Co-authored-by: Kara <lunarautomaton6@gmail.com>
Co-authored-by: Ygg01 <y.laughing.man.y@gmail.com>
Co-authored-by: Łukasz Mędrek <lukasz@lukaszm.xyz>
Co-authored-by: Hannah Giovanna Dawson <karakkaraz@gmail.com>
Co-authored-by: TurboTracker <130304754+TurboTrackerss14@users.noreply.github.com>
Co-authored-by: OnsenCapy <101037138+LGRuthes@users.noreply.github.com>
Co-authored-by: pigeonpeas <147350443+pigeonpeas@users.noreply.github.com>
Co-authored-by: Cojoke <83733158+Cojoke-dot@users.noreply.github.com>
Co-authored-by: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com>
Co-authored-by: Rio <110139251+Riolume@users.noreply.github.com>
Co-authored-by: vorkathbruh <152932728+vorkathbruh@users.noreply.github.com>
Co-authored-by: Sphiral <145869023+SphiraI@users.noreply.github.com>
Co-authored-by: PrPleGoo <PrPleGoo@users.noreply.github.com>
Co-authored-by: Moomoobeef <62638182+Moomoobeef@users.noreply.github.com>
Co-authored-by: username <113782077+whateverusername0@users.noreply.github.com>
Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com>
Co-authored-by: Джексон Миссиссиппи <tripwiregamer@gmail.com>
Co-authored-by: Brandon Li <48413902+aspiringLich@users.noreply.github.com>
Co-authored-by: Jajsha <101492056+Zap527@users.noreply.github.com>
Co-authored-by: RiceMar1244 <138547931+RiceMar1244@users.noreply.github.com>
Co-authored-by: IProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com>
Co-authored-by: youtissoum <51883137+youtissoum@users.noreply.github.com>
Co-authored-by: ike709 <ike709@users.noreply.github.com>
Co-authored-by: icekot8 <93311212+icekot8@users.noreply.github.com>
Co-authored-by: keronshb <54602815+keronshb@users.noreply.github.com>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>
Co-authored-by: Geekyhobo <66805063+Geekyhobo@users.noreply.github.com>
Co-authored-by: FoxxoTrystan <45297731+FoxxoTrystan@users.noreply.github.com>
# Conflicts:
#	Content.Client/Input/ContentContexts.cs
#	Content.Client/Lobby/LobbyState.cs
#	Content.Client/Lobby/UI/HumanoidProfileEditor.xaml
#	Content.Client/Lobby/UI/LobbyGui.xaml
#	Content.Client/Lobby/UI/LobbyGui.xaml.cs
#	Content.Client/Preferences/UI/CharacterSetupGui.xaml.cs
#	Content.Client/UserInterface/Systems/MenuBar/Widgets/GameTopMenuBar.xaml
#	Content.Server/Disposal/Unit/EntitySystems/DisposalUnitSystem.cs
#	Content.Server/Fluids/EntitySystems/PuddleSystem.Spillable.cs
#	Content.Server/GameTicking/GameTicker.Spawning.cs
#	Content.Shared/Alert/AlertType.cs
#	Content.Shared/Input/ContentKeyFunctions.cs
#	Content.Shared/Preferences/HumanoidCharacterProfile.cs
#	Content.Shared/Weapons/Melee/MeleeWeaponComponent.cs
#	Resources/ConfigPresets/EinsteinEngines/default.toml
#	Resources/Prototypes/Alerts/alerts.yml
#	Resources/Prototypes/Entities/Clothing/OuterClothing/coats.yml
#	Resources/Prototypes/Entities/Clothing/OuterClothing/hardsuits.yml
#	Resources/Prototypes/Entities/Clothing/Uniforms/jumpskirts.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Melee/e_sword.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Melee/sword.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/improvised/makeshiftstunprod.yml
#	Resources/Prototypes/Voice/speech_emotes.yml
#	Resources/keybinds.yml
2024-10-19 14:53:37 +07:00

429 lines
15 KiB
C#

using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using Content.Server.Salvage.Magnet;
using Content.Shared.Humanoid;
using Content.Shared.Mobs.Components;
using Content.Shared.Radio;
using Content.Shared.Salvage.Magnet;
using Robust.Server.Maps;
using Robust.Shared.Map;
namespace Content.Server.Salvage;
public sealed partial class SalvageSystem
{
[ValidatePrototypeId<RadioChannelPrototype>]
private const string MagnetChannel = "Supply";
private EntityQuery<SalvageMobRestrictionsComponent> _salvMobQuery;
private List<(Entity<TransformComponent> Entity, EntityUid MapUid, Vector2 LocalPosition)> _detachEnts = new();
private void InitializeMagnet()
{
_salvMobQuery = GetEntityQuery<SalvageMobRestrictionsComponent>();
SubscribeLocalEvent<SalvageMagnetDataComponent, MapInitEvent>(OnMagnetDataMapInit);
SubscribeLocalEvent<SalvageMagnetTargetComponent, GridSplitEvent>(OnMagnetTargetSplit);
SubscribeLocalEvent<SalvageMagnetComponent, MagnetClaimOfferEvent>(OnMagnetClaim);
SubscribeLocalEvent<SalvageMagnetComponent, ComponentStartup>(OnMagnetStartup);
SubscribeLocalEvent<SalvageMagnetComponent, AnchorStateChangedEvent>(OnMagnetAnchored);
}
private void OnMagnetClaim(EntityUid uid, SalvageMagnetComponent component, ref MagnetClaimOfferEvent args)
{
var station = _station.GetOwningStation(uid);
if (!TryComp(station, out SalvageMagnetDataComponent? dataComp) ||
dataComp.EndTime != null)
{
return;
}
TakeMagnetOffer((station.Value, dataComp), args.Index, (uid, component));
}
private void OnMagnetStartup(EntityUid uid, SalvageMagnetComponent component, ComponentStartup args)
{
UpdateMagnetUI((uid, component), Transform(uid));
}
private void OnMagnetAnchored(EntityUid uid, SalvageMagnetComponent component, ref AnchorStateChangedEvent args)
{
if (!args.Anchored)
return;
UpdateMagnetUI((uid, component), args.Transform);
}
private void OnMagnetDataMapInit(EntityUid uid, SalvageMagnetDataComponent component, ref MapInitEvent args)
{
CreateMagnetOffers((uid, component));
}
private void OnMagnetTargetSplit(EntityUid uid, SalvageMagnetTargetComponent component, ref GridSplitEvent args)
{
// Don't think I'm not onto you people splitting to make new grids.
if (TryComp(component.DataTarget, out SalvageMagnetDataComponent? dataComp))
{
foreach (var gridUid in args.NewGrids)
{
dataComp.ActiveEntities?.Add(gridUid);
}
}
}
private void UpdateMagnet()
{
var dataQuery = EntityQueryEnumerator<SalvageMagnetDataComponent>();
var curTime = _timing.CurTime;
while (dataQuery.MoveNext(out var uid, out var magnetData))
{
// Magnet currently active.
if (magnetData.EndTime != null)
{
if (magnetData.EndTime.Value < curTime)
{
EndMagnet((uid, magnetData));
}
else if (!magnetData.Announced && (magnetData.EndTime.Value - curTime).TotalSeconds < 31)
{
var magnet = GetMagnet((uid, magnetData));
if (magnet != null)
{
Report(magnet.Value.Owner, MagnetChannel,
"salvage-system-announcement-losing",
("timeLeft", (magnetData.EndTime.Value - curTime).Seconds));
}
magnetData.Announced = true;
}
}
if (magnetData.NextOffer < curTime)
{
CreateMagnetOffers((uid, magnetData));
}
}
}
/// <summary>
/// Ends the magnet attachment and deletes the relevant grids.
/// </summary>
private void EndMagnet(Entity<SalvageMagnetDataComponent> data)
{
if (data.Comp.ActiveEntities != null)
{
// Handle mobrestrictions getting deleted
var query = AllEntityQuery<SalvageMobRestrictionsComponent>();
while (query.MoveNext(out var salvUid, out var salvMob))
{
if (data.Comp.ActiveEntities.Contains(salvMob.LinkedEntity))
{
QueueDel(salvUid);
}
}
// Uhh yeah don't delete mobs or whatever
var mobQuery = AllEntityQuery<HumanoidAppearanceComponent, MobStateComponent, TransformComponent>();
_detachEnts.Clear();
while (mobQuery.MoveNext(out var mobUid, out _, out _, out var xform))
{
if (xform.GridUid == null || !data.Comp.ActiveEntities.Contains(xform.GridUid.Value) || xform.MapUid == null)
continue;
// Can't parent directly to map as it runs grid traversal.
_detachEnts.Add(((mobUid, xform), xform.MapUid.Value, _transform.GetWorldPosition(xform)));
_transform.DetachParentToNull(mobUid, xform);
}
// Go and cleanup the active ents.
foreach (var ent in data.Comp.ActiveEntities)
{
Del(ent);
}
foreach (var entity in _detachEnts)
{
_transform.SetCoordinates(entity.Entity.Owner, new EntityCoordinates(entity.MapUid, entity.LocalPosition));
}
data.Comp.ActiveEntities = null;
}
data.Comp.EndTime = null;
UpdateMagnetUIs(data);
}
private void CreateMagnetOffers(Entity<SalvageMagnetDataComponent> data)
{
data.Comp.Offered.Clear();
for (var i = 0; i < data.Comp.OfferCount; i++)
{
var seed = _random.Next();
// Fuck with the seed to mix wrecks and asteroids.
seed = (int) (seed / 10f) * 10;
if (i >= data.Comp.OfferCount / 2)
{
seed++;
}
data.Comp.Offered.Add(seed);
}
data.Comp.NextOffer = _timing.CurTime + data.Comp.OfferCooldown;
UpdateMagnetUIs(data);
}
// Just need something to announce.
private Entity<SalvageMagnetComponent>? GetMagnet(Entity<SalvageMagnetDataComponent> data)
{
var query = AllEntityQuery<SalvageMagnetComponent, TransformComponent>();
while (query.MoveNext(out var magnetUid, out var magnet, out var xform))
{
var stationUid = _station.GetOwningStation(magnetUid, xform);
if (stationUid != data.Owner)
continue;
return (magnetUid, magnet);
}
return null;
}
private void UpdateMagnetUI(Entity<SalvageMagnetComponent> entity, TransformComponent xform)
{
var station = _station.GetOwningStation(entity, xform);
if (!TryComp(station, out SalvageMagnetDataComponent? dataComp))
return;
_ui.SetUiState(entity.Owner, SalvageMagnetUiKey.Key,
new SalvageMagnetBoundUserInterfaceState(dataComp.Offered)
{
Cooldown = dataComp.OfferCooldown,
Duration = dataComp.ActiveTime,
EndTime = dataComp.EndTime,
NextOffer = dataComp.NextOffer,
ActiveSeed = dataComp.ActiveSeed,
});
}
private void UpdateMagnetUIs(Entity<SalvageMagnetDataComponent> data)
{
var query = AllEntityQuery<SalvageMagnetComponent, TransformComponent>();
while (query.MoveNext(out var magnetUid, out var magnet, out var xform))
{
var station = _station.GetOwningStation(magnetUid, xform);
if (station != data.Owner)
continue;
_ui.SetUiState(magnetUid, SalvageMagnetUiKey.Key,
new SalvageMagnetBoundUserInterfaceState(data.Comp.Offered)
{
Cooldown = data.Comp.OfferCooldown,
Duration = data.Comp.ActiveTime,
EndTime = data.Comp.EndTime,
NextOffer = data.Comp.NextOffer,
ActiveSeed = data.Comp.ActiveSeed,
});
}
}
private async Task TakeMagnetOffer(Entity<SalvageMagnetDataComponent> data, int index, Entity<SalvageMagnetComponent> magnet)
{
var seed = data.Comp.Offered[index];
var offering = GetSalvageOffering(seed);
var salvMap = _mapManager.CreateMap();
// Set values while awaiting asteroid dungeon if relevant so we can't double-take offers.
data.Comp.ActiveSeed = seed;
data.Comp.EndTime = _timing.CurTime + data.Comp.ActiveTime;
data.Comp.NextOffer = data.Comp.EndTime.Value;
UpdateMagnetUIs(data);
switch (offering)
{
case AsteroidOffering asteroid:
var grid = _mapManager.CreateGrid(salvMap);
await _dungeon.GenerateDungeonAsync(asteroid.DungeonConfig, grid.Owner, grid, Vector2i.Zero, seed);
break;
case SalvageOffering wreck:
var salvageProto = wreck.SalvageMap;
var opts = new MapLoadOptions
{
Offset = new Vector2(0, 0)
};
if (!_map.TryLoad(salvMap, salvageProto.MapPath.ToString(), out var roots, opts))
{
Report(magnet, MagnetChannel, "salvage-system-announcement-spawn-debris-disintegrated");
_mapManager.DeleteMap(salvMap);
return;
}
break;
default:
throw new ArgumentOutOfRangeException();
}
Box2? bounds = null;
var mapXform = _xformQuery.GetComponent(_mapManager.GetMapEntityId(salvMap));
if (mapXform.ChildCount == 0)
{
Report(magnet.Owner, MagnetChannel, "salvage-system-announcement-spawn-no-debris-available");
return;
}
var mapChildren = mapXform.ChildEnumerator;
while (mapChildren.MoveNext(out var mapChild))
{
// If something went awry in dungen.
if (!_gridQuery.TryGetComponent(mapChild, out var childGrid))
continue;
var childAABB = _transform.GetWorldMatrix(mapChild).TransformBox(childGrid.LocalAABB);
bounds = bounds?.Union(childAABB) ?? childAABB;
// Update mass scanner names as relevant.
if (offering is AsteroidOffering)
{
_metaData.SetEntityName(mapChild, Loc.GetString("salvage-asteroid-name"));
_gravity.EnableGravity(mapChild);
}
}
var magnetXform = _xformQuery.GetComponent(magnet.Owner);
var magnetGridUid = magnetXform.GridUid;
var attachedBounds = new Box2Rotated();
var mapId = MapId.Nullspace;
Angle worldAngle;
if (magnetGridUid != null)
{
var magnetGridXform = _xformQuery.GetComponent(magnetGridUid.Value);
var (gridPos, gridRot) = _transform.GetWorldPositionRotation(magnetGridXform);
var gridAABB = _gridQuery.GetComponent(magnetGridUid.Value).LocalAABB;
attachedBounds = new Box2Rotated(gridAABB.Translated(gridPos), gridRot, gridPos);
worldAngle = (gridRot + magnetXform.LocalRotation) - MathF.PI / 2;
mapId = magnetGridXform.MapID;
}
else
{
worldAngle = _random.NextAngle();
}
if (!TryGetSalvagePlacementLocation(mapId, attachedBounds, bounds!.Value, worldAngle, out var spawnLocation, out var spawnAngle))
{
Report(magnet.Owner, MagnetChannel, "salvage-system-announcement-spawn-no-debris-available");
_mapManager.DeleteMap(salvMap);
return;
}
data.Comp.ActiveEntities = null;
mapChildren = mapXform.ChildEnumerator;
// It worked, move it into position and cleanup values.
while (mapChildren.MoveNext(out var mapChild))
{
var salvXForm = _xformQuery.GetComponent(mapChild);
var localPos = salvXForm.LocalPosition;
_transform.SetParent(mapChild, salvXForm, _mapManager.GetMapEntityId(spawnLocation.MapId));
_transform.SetWorldPositionRotation(mapChild, spawnLocation.Position + localPos, spawnAngle, salvXForm);
data.Comp.ActiveEntities ??= new List<EntityUid>();
data.Comp.ActiveEntities?.Add(mapChild);
// Handle mob restrictions
var children = salvXForm.ChildEnumerator;
while (children.MoveNext(out var child))
{
if (!_salvMobQuery.TryGetComponent(child, out var salvMob))
continue;
salvMob.LinkedEntity = mapChild;
}
}
Report(magnet.Owner, MagnetChannel, "salvage-system-announcement-arrived", ("timeLeft", data.Comp.ActiveTime.TotalSeconds));
_mapManager.DeleteMap(salvMap);
data.Comp.Announced = false;
var active = new SalvageMagnetActivatedEvent()
{
Magnet = magnet,
};
RaiseLocalEvent(ref active);
}
private bool TryGetSalvagePlacementLocation(MapId mapId, Box2Rotated attachedBounds, Box2 bounds, Angle worldAngle, out MapCoordinates coords, out Angle angle)
{
// Grid intersection only does AABB atm.
var attachedAABB = attachedBounds.CalcBoundingBox();
var minDistance = (attachedAABB.Height < attachedAABB.Width ? attachedAABB.Width : attachedAABB.Height) / 2f;
var minActualDistance = bounds.Height < bounds.Width ? minDistance + bounds.Width / 2f : minDistance + bounds.Height / 2f;
var attachedCenter = attachedAABB.Center;
var fraction = 0.25f;
// Thanks 20kdc
for (var i = 0; i < 20; i++)
{
var randomPos = attachedCenter +
worldAngle.ToVec() * (minActualDistance * fraction);
var finalCoords = new MapCoordinates(randomPos, mapId);
angle = _random.NextAngle();
var box2 = Box2.CenteredAround(finalCoords.Position, bounds.Size);
var box2Rot = new Box2Rotated(box2, angle, finalCoords.Position);
// This doesn't stop it from spawning on top of random things in space
// Might be better like this, ghosts could stop it before
if (_mapManager.FindGridsIntersecting(finalCoords.MapId, box2Rot).Any())
{
// Bump it further and further just in case.
fraction += 0.25f;
continue;
}
coords = finalCoords;
return true;
}
angle = Angle.Zero;
coords = MapCoordinates.Nullspace;
return false;
}
}
[ByRefEvent]
public record struct SalvageMagnetActivatedEvent
{
public EntityUid Magnet;
}