This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
forked from new-frontiers-14/frontier-station-14
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from noverd/randshiuperrrul
Новое рандомное событие: RandomShipyardErrorRule
- Loading branch information
Showing
3 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
Content.Server/_NF/GameRule/Events/Components/RandomShipyardErrorRuleComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Content.Server._NF.GameRule.Events.Components; | ||
|
||
[RegisterComponent, Access(typeof(RandomShipyardErrorRule))] | ||
public sealed partial class RandomShipyardErrorRuleComponent : Component | ||
{ | ||
/// <summary> | ||
/// Min vessels count for spawning | ||
/// </summary> | ||
[DataField("minGridCount")] | ||
public int MinGridsCount = 2; | ||
/// <summary> | ||
/// Man vessels count for spawning | ||
/// </summary> | ||
[DataField("maxGridCount")] | ||
public int MaxGridsCount = 5; | ||
|
||
/// <summary> | ||
/// The grid in question, set after starting the event | ||
/// </summary> | ||
[DataField("gridUid")] | ||
public List<EntityUid?> GridUids = new(); | ||
} |
124 changes: 124 additions & 0 deletions
124
Content.Server/_NF/GameRule/Events/RandomShipyardErrorRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using System.Linq; | ||
using System.Numerics; | ||
using Content.Server._NF.GameRule.Events.Components; | ||
using Content.Server.Cargo.Components; | ||
using Content.Server.Cargo.Systems; | ||
using Robust.Server.GameObjects; | ||
using Robust.Server.Maps; | ||
using Robust.Shared.Map; | ||
using Content.Server.GameTicking.Rules.Components; | ||
using Content.Server.Shuttles.Components; | ||
using Content.Server.Shuttles.Systems; | ||
using Content.Server.StationEvents.Events; | ||
using Content.Shared.Humanoid; | ||
using Content.Shared.Mobs.Components; | ||
using Content.Shared.Shipyard.Prototypes; | ||
using Robust.Shared.Prototypes; | ||
using Robust.Shared.Random; | ||
|
||
namespace Content.Server._NF.GameRule.Events; | ||
|
||
public sealed class RandomShipyardErrorRule : StationEventSystem<RandomShipyardErrorRuleComponent> | ||
{ | ||
[Dependency] private readonly IMapManager _mapManager = default!; | ||
[Dependency] private readonly MapLoaderSystem _map = default!; | ||
[Dependency] private readonly ShuttleSystem _shuttle = default!; | ||
[Dependency] private readonly IRobustRandom _random = default!; | ||
[Dependency] private readonly SharedTransformSystem _transform = default!; | ||
[Dependency] private readonly PricingSystem _pricing = default!; | ||
[Dependency] private readonly CargoSystem _cargo = default!; | ||
[Dependency] private readonly IPrototypeManager _proto = default!; | ||
|
||
private List<(Entity<TransformComponent> Entity, EntityUid MapUid, Vector2 LocalPosition)> _playerMobs = new(); | ||
|
||
protected override void Started(EntityUid uid, RandomShipyardErrorRuleComponent component, | ||
GameRuleComponent gameRule, | ||
GameRuleStartedEvent args) | ||
{ | ||
base.Started(uid, component, gameRule, args); | ||
var vesselPrototypes = _proto.EnumeratePrototypes<VesselPrototype>(); | ||
var prototypes = vesselPrototypes as VesselPrototype[] ?? vesselPrototypes.ToArray(); | ||
var rnd = new Random(); | ||
var vesselCount = rnd.Next(component.MinGridsCount, component.MaxGridsCount); | ||
|
||
for (var i = 0; i < vesselCount; i++) | ||
{ | ||
var index = rnd.Next(0, prototypes.Count()); | ||
var gridPath = prototypes.ElementAt(index).ShuttlePath.ToString(); | ||
var shuttleMap = _mapManager.CreateMap(); | ||
var options = new MapLoadOptions | ||
{ | ||
LoadMap = true, | ||
}; | ||
if (!_map.TryLoad(shuttleMap, gridPath, out var gridUids, options)) | ||
return; | ||
component.GridUids.Add(gridUids[0]); | ||
if (component.GridUids.Last() is not { } gridUid) | ||
return; | ||
_shuttle.SetIFFColor(gridUid, new Color(rnd.Next(256), rnd.Next(256), rnd.Next(256), 0)); | ||
var offset = _random.NextVector2(1800f, 2000f); | ||
var mapId = GameTicker.DefaultMap; | ||
var coords = new MapCoordinates(offset, mapId); | ||
var location = Spawn(null, coords); | ||
if (TryComp<ShuttleComponent>(component.GridUids.Last(), out var shuttle)) | ||
{ | ||
_shuttle.FTLTravel(gridUid, shuttle, location, 5.5f, 55f); | ||
} | ||
} | ||
} | ||
|
||
protected override void Ended( | ||
EntityUid uid, | ||
RandomShipyardErrorRuleComponent component, | ||
GameRuleComponent gameRule, | ||
GameRuleEndedEvent args) | ||
{ | ||
base.Ended(uid, component, gameRule, args); | ||
foreach (var gridId in component.GridUids) | ||
{ | ||
|
||
|
||
if (!EntityManager.TryGetComponent<TransformComponent>(gridId, out var gridTransform)) | ||
{ | ||
Log.Error("bluespace error objective was missing transform component"); | ||
return; | ||
} | ||
|
||
if (gridTransform.GridUid is not EntityUid gridUid) | ||
{ | ||
Log.Error("bluespace error has no associated grid?"); | ||
return; | ||
} | ||
|
||
var gridValue = _pricing.AppraiseGrid(gridUid, null); | ||
|
||
var mobQuery = AllEntityQuery<HumanoidAppearanceComponent, MobStateComponent, TransformComponent>(); | ||
_playerMobs.Clear(); | ||
|
||
while (mobQuery.MoveNext(out var mobUid, out _, out _, out var xform)) | ||
{ | ||
if (xform.GridUid == null || xform.MapUid == null || xform.GridUid != gridUid) | ||
continue; | ||
|
||
// Can't parent directly to map as it runs grid traversal. | ||
_playerMobs.Add(((mobUid, xform), xform.MapUid.Value, _transform.GetWorldPosition(xform))); | ||
_transform.DetachParentToNull(mobUid, xform); | ||
} | ||
|
||
// Deletion has to happen before grid traversal re-parents players. | ||
Del(gridUid); | ||
|
||
foreach (var mob in _playerMobs) | ||
{ | ||
_transform.SetCoordinates(mob.Entity.Owner, new EntityCoordinates(mob.MapUid, mob.LocalPosition)); | ||
} | ||
|
||
|
||
var query = EntityQuery<StationBankAccountComponent>(); | ||
foreach (var account in query) | ||
{ | ||
_cargo.DeductFunds(account, (int) -gridValue); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters