-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapComposer.cs
More file actions
53 lines (47 loc) · 2.46 KB
/
Copy pathMapComposer.cs
File metadata and controls
53 lines (47 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using RustPlusBot.Features.Connections.Listening;
using RustPlusBot.Features.Events.State;
using RustPlusBot.Features.Map.Rendering;
namespace RustPlusBot.Features.Map.Composing;
/// <summary>Gathers the cached base map + live markers and renders the map PNG.</summary>
/// <param name="cache">The base-map cache.</param>
/// <param name="events">Live marker state.</param>
/// <param name="query">Live query seam (supplies the static map dimensions).</param>
/// <param name="renderer">The image renderer.</param>
public sealed class MapComposer(BaseMapCache cache, IEventState events, IRustServerQuery query, MapRenderer renderer)
{
private static readonly MarkerKind[] DrawnKinds =
[
MarkerKind.CargoShip, MarkerKind.PatrolHelicopter, MarkerKind.Chinook,
];
/// <summary>Composes the map PNG for a server, or null when no base map is available yet.</summary>
/// <param name="guildId">The owning guild snowflake.</param>
/// <param name="serverId">The target server id.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>PNG bytes, or null.</returns>
public async Task<byte[]?> ComposeAsync(ulong guildId, Guid serverId, CancellationToken cancellationToken)
{
var baseImage = await cache.GetAsync(guildId, serverId, cancellationToken).ConfigureAwait(false);
if (baseImage is null)
{
return null;
}
// Dimensions come from the map itself (not from a marker), so the grid renders even when no
// markers are present — e.g. on a freshly-connected or low-activity server.
var dims = await query.GetMapDimensionsAsync(guildId, serverId, cancellationToken).ConfigureAwait(false);
if (dims is null)
{
// Dimensions unavailable: render the base tile only (grid/markers both need world→pixel).
return renderer.Render(baseImage, new MapDimensions(0, 0, 0), markers: [],
new MapLayerSet(Grid: false, Markers: false, Monuments: false, Vendor: false, Rigs: false));
}
var placements = DrawnKinds
.SelectMany(kind => events.GetActiveMarkers(guildId, serverId, kind))
.Select(m =>
{
var (px, py) = WorldToPixel.ToPixel(m.X, m.Y, dims, MapRenderer.OutputSize);
return new MarkerPlacement(m.Kind, px, py);
})
.ToList();
return renderer.Render(baseImage, dims, placements, MapLayerSet.Default2b);
}
}