Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion update 2 #31

Merged
merged 6 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions Content.Server/Stories/Conversion/ConversionSystem.API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ public HashSet<EntityUid> GetEntitiesConvertedBy(EntityUid? uid, ProtoId<Convers

return entities;
}
public bool TryGetConversion(EntityUid uid, [NotNullWhen(true)] out ConversionData? data, ProtoId<ConversionPrototype> prototype, ConversionableComponent? component = null)
public bool TryGetConversion(EntityUid uid, string id, [NotNullWhen(true)] out ConversionData? conversion, ConversionableComponent? component = null)
{
if (Resolve(uid, ref component) && component.ActiveConversions.TryGetValue(prototype.Id, out var conversion))
{
data = conversion;
return true;
}
data = null;
return false;
conversion = null;

if (!Resolve(uid, ref component))
return false;

return component.ActiveConversions.TryGetValue(id, out conversion);
}
public bool TryRevert(EntityUid target, ProtoId<ConversionPrototype> prototype, EntityUid? performer = null, ConversionableComponent? component = null)
{
Expand All @@ -47,10 +46,10 @@ public bool TryRevert(EntityUid target, ProtoId<ConversionPrototype> prototype,
if (!CanRevert(target, prototype, performer, component))
return false;

Revert(target, proto, performer, component);
DoRevert(target, proto, performer, component);
return true;
}
public void Revert(EntityUid target, ConversionPrototype proto, EntityUid? performer = null, ConversionableComponent? component = null)
private void DoRevert(EntityUid target, ConversionPrototype proto, EntityUid? performer = null, ConversionableComponent? component = null)
{
if (!Resolve(target, ref component))
return;
Expand All @@ -61,6 +60,10 @@ public void Revert(EntityUid target, ConversionPrototype proto, EntityUid? perfo
if (!_mind.TryGetMind(target, out var mindId, out var mind))
return;

// До удаления компонентов, чтобы эти компоненты могли его обработать.
var ev = new RevertedEvent(target, performer, data);
RaiseLocalEvent(target, (object) ev, true);

if (proto.EndBriefing != null)
_antag.SendBriefing(target, Loc.GetString(proto.EndBriefing.Value.Text ?? ""), proto.EndBriefing.Value.Color, proto.EndBriefing.Value.Sound);

Expand All @@ -76,31 +79,23 @@ public void Revert(EntityUid target, ConversionPrototype proto, EntityUid? perfo

component.ActiveConversions.Remove(proto.ID);

var ev = new RevertedEvent(target, performer, proto);
RaiseLocalEvent(target, (object)ev, true);
Dirty(target, component);
}
public bool TryConvert(EntityUid target, ProtoId<ConversionPrototype> prototype, EntityUid? performer = null, ConversionableComponent? component = null)
{
if (!Resolve(target, ref component, false))
return false;

Logger.Error("1");

if (!_prototype.TryIndex(prototype, out var proto))
return false;

Logger.Error("2");

if (!CanConvert(target, prototype, performer, component))
return false;

Logger.Error("3");

Convert(target, proto, performer, component);
DoConvert(target, proto, performer, component);
return true;
}
public void Convert(EntityUid target, ConversionPrototype proto, EntityUid? performer = null, ConversionableComponent? component = null)
private void DoConvert(EntityUid target, ConversionPrototype proto, EntityUid? performer = null, ConversionableComponent? component = null)
{
if (!Resolve(target, ref component))
return;
Expand Down Expand Up @@ -132,7 +127,7 @@ public void Convert(EntityUid target, ConversionPrototype proto, EntityUid? perf
component.ActiveConversions.Add(proto.ID, conversion);

var ev = new ConvertedEvent(target, performer, conversion);
RaiseLocalEvent(target, (object)ev, true);
RaiseLocalEvent(target, (object) ev, true);
Dirty(target, component);
}
public void MindRemoveRoles(EntityUid mindId, ComponentRegistry components, MindComponent? mind = null, bool silent = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private void OnImplanted(EntityUid uid, ConversionableComponent component, MindS
{
foreach (var (key, conversion) in component.ActiveConversions)
{
Revert(uid, _prototype.Index(conversion.Prototype));
DoRevert(uid, _prototype.Index(conversion.Prototype));
}
}
}
4 changes: 3 additions & 1 deletion Content.Server/Stories/Conversion/ConversionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public override void Update(float frameTime)
{
foreach (var (key, conversion) in comp.ActiveConversions)
{
if (conversion.EndTime == null)
continue;
if (conversion.EndTime > _timing.CurTime)
continue;
var proto = _prototype.Index(conversion.Prototype);
Revert(uid, proto);
DoRevert(uid, proto);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions Content.Shared/Stories/Conversion/ConversionPrototype.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Content.Shared.Mobs;
using Content.Shared.StatusIcon;
using Content.Shared.Whitelist;
using Robust.Shared.Audio;
Expand Down Expand Up @@ -32,6 +33,9 @@ public sealed partial class ConversionPrototype : IPrototype
#endregion

#region Whitelist
[DataField]
public HashSet<MobState>? AllowedMobStates = [MobState.Alive];

[DataField]
public EntityWhitelist? Whitelist;

Expand Down
6 changes: 6 additions & 0 deletions Content.Shared/Stories/Conversion/ConversionSystem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Content.Shared.Mobs.Components;
using Content.Shared.Whitelist;
using Robust.Shared.Prototypes;

Expand Down Expand Up @@ -26,6 +27,11 @@ public bool CanConvert(EntityUid target, ProtoId<ConversionPrototype> prototype,

var proto = _prototype.Index(prototype);

if (TryComp<MobStateComponent>(target, out var mobState) &&
proto.AllowedMobStates != null &&
!proto.AllowedMobStates.Contains(mobState.CurrentState))
return false;

if (_entityWhitelist.IsWhitelistFail(proto.Whitelist, target))
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public sealed class ConvertedEvent(EntityUid target, EntityUid? performer, Conve
public readonly EntityUid? Performer = performer;
public readonly ConversionData Data = data;
}
public sealed class RevertedEvent(EntityUid target, EntityUid? performer, ConversionPrototype prototype) : HandledEntityEventArgs
public sealed class RevertedEvent(EntityUid target, EntityUid? performer, ConversionData data) : HandledEntityEventArgs
{
public readonly EntityUid Target = target;
public readonly EntityUid? Performer = performer;
public readonly ConversionPrototype Prototype = prototype;
public readonly ConversionData Data = data;
}
Loading