Skip to content

Commit

Permalink
Smooth a zone (#369)
Browse files Browse the repository at this point in the history
* smooth the whole zone

* parallelize smoother for whole zone

* oof
  • Loading branch information
MikeJeffers committed Aug 8, 2021
1 parent 9e84f1a commit 2447282
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/Perpetuum.Bootstrapper/PerpetuumBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2809,6 +2809,7 @@ private void RegisterZoneRequestHandlers()
RegisterZoneRequestHandler<PBSSetReinforceOffset>(Commands.PBSSetReinforceOffset);
RegisterZoneRequestHandler<PBSSetEffect>(Commands.PBSSetEffect);
RegisterZoneRequestHandler<ZoneDrawRamp>(Commands.ZoneDrawRamp);
RegisterZoneRequestHandler<ZoneSmooth>(Commands.ZoneSmooth);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@
<Compile Include="Zone\ZoneSetRuntimeZoneEntityName.cs" />
<Compile Include="Zone\ZoneSwitchDegrade.cs" />
<Compile Include="Zone\ZoneTerraformTest.cs" />
<Compile Include="Zone\ZoneSmooth.cs" />
<Compile Include="Zone\ZoneUploadScanResult.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
8 changes: 1 addition & 7 deletions src/Perpetuum.RequestHandlers/Zone/ZoneDrawRamp.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using Perpetuum.Accounting.Characters;
using Perpetuum.Host.Requests;
using Perpetuum.Host.Requests;
using Perpetuum.Players;
using Perpetuum.Zones;
using Perpetuum.Zones.Beams;
using Perpetuum.Zones.NpcSystem.Reinforcements;
using Perpetuum.Zones.Terrains;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Perpetuum.RequestHandlers.Zone
{
Expand Down
53 changes: 53 additions & 0 deletions src/Perpetuum.RequestHandlers/Zone/ZoneSmooth.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Perpetuum.Host.Requests;
using Perpetuum.Zones;
using Perpetuum.Zones.Terrains;
using System.Threading.Tasks;

namespace Perpetuum.RequestHandlers.Zone
{
public class ZoneSmooth : IRequestHandler<IZoneRequest>
{
private int CalculateBufferOffset(int x, int y, Area area)
{
return (x - area.X1) + (y - area.Y1) * area.Width;
}
public void HandleRequest(IZoneRequest request)
{
var zone = request.Zone;
zone.IsLayerEditLocked.ThrowIfTrue(ErrorCodes.TileTerraformProtected);
var area = Area.FromRectangle(0, 0, zone.Size.Width, zone.Size.Height);
var altBuffer = new ushort[area.Ground];
var workAreas = area.Slice(32);
Parallel.ForEach(workAreas, (workArea) =>
{
foreach (var p in workArea.GetPositions())
{
var sum = 0.0;
var count = 0;
foreach (var n in p.EightNeighbours)
{
if (zone.IsValidPosition(n.intX, n.intY))
{
sum += zone.Terrain.Altitude.GetAltitudeAsDouble(n.intX, n.intY);
count++;
}
}
if (count > 0)
{
var smoothed = sum / count;
var shortAlt = System.Convert.ToUInt16(smoothed * 32);
altBuffer[CalculateBufferOffset(p.intX, p.intY, area)] = shortAlt;
}
}
});
Parallel.ForEach(workAreas, (workArea) =>
{
foreach (var p in workArea.GetPositions())
{
zone.Terrain.Altitude.SetValue(p.intX, p.intY, altBuffer[CalculateBufferOffset(p.intX, p.intY, area)]);
}
zone.Terrain.Slope.UpdateSlopeByArea(workArea);
});
}
}
}
6 changes: 6 additions & 0 deletions src/Perpetuum/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public static Command GetCommandByText(string commandText)
}
};

public static readonly Command ZoneSmooth = new Command
{
Text = "zoneSmooth",
AccessLevel = AccessLevel.admin,
};

public static readonly Command ZoneDisplayMissionSpots = new Command
{
Text = "zoneDisplayMissionSpots",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,33 @@ public static void ZoneRandomFillGroundType(AdminCommandData data)
HandleLocalRequest(data, cmd);
SendMessageToAll(data, $"Command completed");
}
[ChatCommand("ZoneSmoothAll")]
public static void ZoneUpdateSlope(AdminCommandData data)
{
if (!IsDevModeEnabled(data))
return;

int zoneId = data.Sender.ZoneId ?? -1;

try
{
if(data.Command.Args.Length > 0)
zoneId = int.Parse(data.Command.Args[0]);
}
catch (Exception ex)
{
SendMessageToAll(data, "Bad args");
if (ex is ArgumentNullException)
throw PerpetuumException.Create(ErrorCodes.RequiredArgumentIsNotSpecified);
throw;
}

CheckZoneId(data, zoneId);
var cmd = string.Format("{0}:zone_{1}:null", Commands.ZoneSmooth.Text, zoneId);
SendMessageToAll(data, $"Sending: {cmd}");
HandleLocalRequest(data, cmd);
SendMessageToAll(data, $"Command completed");
}
[ChatCommand("ZoneLockLayers")]
public static void ZoneLockLayers(AdminCommandData data)
{
Expand Down

0 comments on commit 2447282

Please sign in to comment.