Skip to content

Commit

Permalink
Extract tile section to its own function. Bias selection for current …
Browse files Browse the repository at this point in the history
…grid. Use proper coords for box.
  • Loading branch information
nikthechampiongr committed Mar 26, 2024
1 parent 7afce78 commit ec42aea
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions Content.Server/Implants/SubdermalImplantSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,45 @@ private void OnScramImplant(EntityUid uid, SubdermalImplantComponent component,
_pullingSystem.TryStopPull(ent, pull);

var xform = Transform(ent);
var entityCoords = xform.Coordinates.ToMap(EntityManager, _xform);
var targetCoords = SelectRandomTileInRange(xform, implant.TeleportRadius);

var grids = _lookupSystem.GetEntitiesInRange<MapGridComponent>(entityCoords, implant.TeleportRadius).ToList();
if (targetCoords != null)
{
_xform.SetWorldPosition(ent, targetCoords.Value.Position);
_xform.AttachToGridOrMap(ent, xform);
_audio.PlayPvs(implant.TeleportSound, ent);
args.Handled = true;
}
}

private MapCoordinates? SelectRandomTileInRange(TransformComponent userXform, float radius)
{
var userCoords = userXform.Coordinates.ToMap(EntityManager, _xform);
var grids = _lookupSystem.GetEntitiesInRange<MapGridComponent>(userCoords, radius).ToList();
_random.Shuffle(grids);

// Give preference to the grid the entity is currently on.
var idx = grids.FindIndex(grid => grid.Owner == userXform.GridUid);
if (idx != -1)
{
if (_random.Prob(0.66f))
{
(grids[0], grids[idx]) = (grids[idx], grids[0]);
}
else
{
(grids[^1], grids[idx]) = (grids[idx], grids[^1]);
}
}

MapCoordinates? targetCoords = null;

foreach (var grid in grids)
{
var valid = false;

var range = (float) Math.Sqrt(implant.TeleportRadius);
var box = Box2.CenteredAround(entityCoords.Position, new Vector2(range, range));
var range = (float) Math.Sqrt(radius);
var box = Box2.CenteredAround(userCoords.Position, new Vector2(range, range));
var tilesInRange = _mapSystem.GetTilesEnumerator(grid.Owner, grid.Comp, box, false);
var tileList = new List<TileRef>();

Expand Down Expand Up @@ -161,13 +188,7 @@ private void OnScramImplant(EntityUid uid, SubdermalImplantComponent component,
break;
}

if (targetCoords != null)
{
_xform.SetWorldPosition(ent, targetCoords.Value.Position);
_xform.AttachToGridOrMap(ent, xform);
_audio.PlayPvs(implant.TeleportSound, ent);
args.Handled = true;
}
return targetCoords;
}

private void OnDnaScramblerImplant(EntityUid uid, SubdermalImplantComponent component, UseDnaScramblerImplantEvent args)
Expand Down

0 comments on commit ec42aea

Please sign in to comment.