1
0

CreateEntityTileReaction.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Content.Shared.Chemistry.Reaction;
  2. using Content.Shared.Chemistry.Reagent;
  3. using Content.Shared.FixedPoint;
  4. using Content.Shared.Maps;
  5. using Content.Shared.Whitelist;
  6. using Robust.Shared.Map;
  7. using Robust.Shared.Prototypes;
  8. using Robust.Shared.Random;
  9. using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
  10. using System.Numerics;
  11. namespace Content.Server.Chemistry.TileReactions;
  12. [DataDefinition]
  13. public sealed partial class CreateEntityTileReaction : ITileReaction
  14. {
  15. [DataField(required: true, customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
  16. public string Entity = default!;
  17. [DataField]
  18. public FixedPoint2 Usage = FixedPoint2.New(1);
  19. /// <summary>
  20. /// How many of the whitelisted entity can fit on one tile?
  21. /// </summary>
  22. [DataField]
  23. public int MaxOnTile = 1;
  24. /// <summary>
  25. /// The whitelist to use when determining what counts as "max entities on a tile".0
  26. /// </summary>
  27. [DataField("maxOnTileWhitelist")]
  28. public EntityWhitelist? Whitelist;
  29. [DataField]
  30. public float RandomOffsetMax = 0.0f;
  31. public FixedPoint2 TileReact(TileRef tile,
  32. ReagentPrototype reagent,
  33. FixedPoint2 reactVolume,
  34. IEntityManager entityManager,
  35. List<ReagentData>? data)
  36. {
  37. if (reactVolume >= Usage)
  38. {
  39. if (Whitelist != null)
  40. {
  41. int acc = 0;
  42. foreach (var ent in tile.GetEntitiesInTile())
  43. {
  44. var whitelistSystem = entityManager.System<EntityWhitelistSystem>();
  45. if (whitelistSystem.IsWhitelistPass(Whitelist, ent))
  46. acc += 1;
  47. if (acc >= MaxOnTile)
  48. return FixedPoint2.Zero;
  49. }
  50. }
  51. var random = IoCManager.Resolve<IRobustRandom>();
  52. var xoffs = random.NextFloat(-RandomOffsetMax, RandomOffsetMax);
  53. var yoffs = random.NextFloat(-RandomOffsetMax, RandomOffsetMax);
  54. var center = entityManager.System<TurfSystem>().GetTileCenter(tile);
  55. var pos = center.Offset(new Vector2(xoffs, yoffs));
  56. entityManager.SpawnEntity(Entity, pos);
  57. return Usage;
  58. }
  59. return FixedPoint2.Zero;
  60. }
  61. }