Skip to content

Commit

Permalink
more work on structure layers
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyGalbreath committed Jun 1, 2024
1 parent b03aba0 commit 7c2dbf2
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 24 deletions.
3 changes: 3 additions & 0 deletions resources/assets/livemap/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"lang.pinned": "Pinned",
"lang.unpinned": "Unpinned",
"lang.players": "Players",
"lang.spawn": "Spawn",
"lang.traders": "Traders",
"lang.translocators": "Translocators",
"lang.renderers": "Map Types",
"lang.copy": "Copy",
"lang.copy-alt": "Copy this location to the clipboard",
Expand Down
13 changes: 12 additions & 1 deletion src/configuration/Traders.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using livemap.layer.marker.options;
using livemap.layer.marker.options.type;
using Point = livemap.data.Point;

Expand All @@ -16,6 +17,16 @@ public class Traders {
Title = "",
Alt = "",
IconUrl = "#svg-trader",
IconSize = new Point(16, 16)
IconSize = new Point(16, 16),
Pane = "traders"
};

public TooltipOptions? Tooltip { get; set; } = new() {
Direction = "top",
Content = "{0}<br>{1}"
};

public PopupOptions? Popup { get; set; }

public string? Css { get; set; } = ".leaflet-traders-pane .leaflet-marker-icon { color: #204EA2; filter: drop-shadow(1px 0 0 black) drop-shadow(-1px 0 0 black) drop-shadow(0 1px 0 black) drop-shadow(0 -1px 0 black)}";
}
6 changes: 6 additions & 0 deletions src/layer/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public abstract class Layer : Keyed {
[JsonProperty(Order = 999)]
public virtual List<Marker> Markers { get; } = new();

/// <summary>
/// Custom CSS for this layer's map pane
/// </summary>
[JsonProperty(Order = 1000)]
public virtual string? Css { get; set; }

[JsonIgnore]
public virtual string Filename => Path.Combine(Files.MarkerDir, $"{Id}.json");

Expand Down
67 changes: 67 additions & 0 deletions src/layer/builtin/TradersLayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using livemap.configuration;
using livemap.layer.marker;
using livemap.layer.marker.options;
using livemap.util;
using Vintagestory.API.MathTools;
using Vintagestory.API.Util;

namespace livemap.layer.builtin;

public class TradersLayer : Layer {
public override int? Interval => Config.UpdateInterval;

public override bool? Hidden => !Config.DefaultShowLayer;

public override List<Marker> Markers {
get {
List<Marker> list = new();
_knownTraders.Values.Foreach(traders => {
traders.Foreach(trader => {
TooltipOptions? tooltip = Config.Tooltip?.DeepCopy();
if (tooltip?.Content != null) {
tooltip.Content = string.Format(tooltip.Content, trader.Name, trader.Type);
}
PopupOptions? popup = Config.Popup?.DeepCopy();
if (popup?.Content != null) {
popup.Content = string.Format(popup.Content, trader.Name, trader.Type);
}
string id = $"trader:{trader.Pos.X},{trader.Pos.Y},{trader.Pos.Z}";
list.Add(new Icon(id, trader.Pos.ToPoint(), Config.IconOptions) {
Tooltip = tooltip,
Popup = popup
});
});
});
return list;
}
}

public override string? Css => Config.Css;

public override string Filename => Path.Combine(Files.MarkerDir, $"{Id}.json");

private static Traders Config => LiveMap.Api.Config.Layers.Traders;

private readonly ConcurrentDictionary<ulong, HashSet<Trader>> _knownTraders = new();

public TradersLayer() : base("traders", "lang.traders".ToLang()) { }

public void SetTraders(ulong chunkIndex, HashSet<Trader> traders) {
_knownTraders[chunkIndex] = traders;
}

public class Trader {
public readonly string Type;
public readonly string? Name;
public readonly BlockPos Pos;

public Trader(string type, string? name, BlockPos pos) {
Type = type;
Name = name;
Pos = pos;
}
}
}
23 changes: 23 additions & 0 deletions src/layer/builtin/TranslocatorsLayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.IO;
using livemap.configuration;
using livemap.layer.marker;
using livemap.util;

namespace livemap.layer.builtin;

public class TranslocatorsLayer : Layer {
public override int? Interval => Config.UpdateInterval;

public override bool? Hidden => !Config.DefaultShowLayer;

public override List<Marker> Markers => new() {
//new Icon("livemap:spawn", LiveMap.Api.Sapi.World.DefaultSpawnPosition.ToPoint(), Config.IconOptions)
};

public override string Filename => Path.Combine(Files.MarkerDir, $"{Id}.json");

private static Translocators Config => LiveMap.Api.Config.Layers.Translocators;

public TranslocatorsLayer() : base("translocators", "lang.translocators".ToLang()) { }
}
2 changes: 1 addition & 1 deletion src/layer/marker/Marker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace livemap.layer.marker;
/// Represents a marker on the map
/// </summary>
[PublicAPI]
public abstract class Marker {
public class Marker {
/// <summary>
/// Type identifier for the marker
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/layer/marker/options/DivOverlayOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace livemap.layer.marker.options;
/// Optional settings for <see cref="TooltipOptions"/> and <see cref="PopupOptions"/> overlays
/// </summary>
[PublicAPI]
public abstract class DivOverlayOptions : InteractiveLayerOptions {
public class DivOverlayOptions : InteractiveLayerOptions {
/// <summary>
/// The offset of this overlay position
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/layer/marker/options/InteractiveLayerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace livemap.layer.marker.options;
/// </remarks>
/// </summary>
[PublicAPI]
public abstract class InteractiveLayerOptions : LayerOptions {
public class InteractiveLayerOptions : LayerOptions {
/// <summary>
/// When <see langword="true"/>, a mouse event on this marker will trigger the same event on the map
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/layer/marker/options/LayerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace livemap.layer.marker.options;
/// </remarks>
/// </summary>
[PublicAPI]
public abstract class LayerOptions : BaseOptions {
public class LayerOptions : BaseOptions {
/// <summary>
/// Map pane where this marker will be added
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/layer/marker/options/PathOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace livemap.layer.marker.options;
/// Do not use it directly
/// </remarks>
[PublicAPI]
public abstract class PathOptions : InteractiveLayerOptions {
public class PathOptions : InteractiveLayerOptions {
/// <summary>
/// Whether to draw stroke along the path. Set it to <see langword="false"/> to disable borders on polygons or circles
/// </summary>
Expand Down
20 changes: 18 additions & 2 deletions src/registry/LayerRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@ namespace livemap.registry;

[PublicAPI]
public class LayerRegistry : Registry<Layer> {
public PlayersLayer? Players { get; private set; }
public SpawnLayer? Spawn { get; private set; }
public TradersLayer? Traders { get; private set; }
public TranslocatorsLayer? Translocators { get; private set; }

public LayerRegistry() : base("layers") { }

public void RegisterBuiltIns() {
Register(new SpawnLayer());
Register(new PlayersLayer());
Register(Players = new PlayersLayer());
Register(Spawn = new SpawnLayer());
Register(Traders = new TradersLayer());
Register(Translocators = new TranslocatorsLayer());
}

public override void Dispose() {
Players = null;
Spawn = null;
Traders = null;
Translocators = null;

base.Dispose();
}
}
12 changes: 10 additions & 2 deletions src/registry/RendererRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ namespace livemap.registry;

[PublicAPI]
public class RendererRegistry : Registry<Renderer> {
public static BasicRenderer? Basic { get; private set; }
public static SepiaRenderer? Sepia { get; private set; }

public RendererRegistry() : base("renderers") { }

public void RegisterBuiltIns() {
Register(new BasicRenderer());
Register(new SepiaRenderer());
Register(Basic = new BasicRenderer());
Register(Sepia = new SepiaRenderer());
}

public override void Dispose() {
foreach ((string _, Renderer renderer) in this) {
renderer.Dispose();
}

Basic = null;
Sepia = null;

base.Dispose();
}
}
39 changes: 25 additions & 14 deletions src/task/RenderTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using livemap.layer.builtin;
using livemap.render;
using livemap.util;
using Vintagestory.API.Common;
Expand Down Expand Up @@ -95,7 +96,7 @@ private void ScanChunkColumn(ServerMapRegion region, ChunkPos chunkPos, BlockDat
}

// process things from structures
ProcessStructures(chunkSlices);
ProcessStructures(chunkPos, chunkSlices);
}

private void FindChunksToLoad(ServerMapRegion region, ServerMapChunk? mapChunk, ChunkPos chunkPos, List<int> chunkIndexesToLoad) {
Expand Down Expand Up @@ -128,8 +129,13 @@ private void FindChunksToLoad(ServerMapRegion region, ServerMapChunk? mapChunk,
});
}

private void ProcessStructures(ServerChunk?[] chunkSlices) {
// todo - wipe things that are no longer there
private void ProcessStructures(ChunkPos chunkPos, ServerChunk?[] chunkSlices) {
ulong chunkIndex = chunkPos.ToChunkIndex();

TradersLayer? tradersLayer = _server.LayerRegistry.Traders;

HashSet<TradersLayer.Trader> traders = new();

chunkSlices.Foreach(chunk => {
if (_server.Config.Layers.Translocators.Enabled) {
chunk?.BlockEntities.Values.Foreach(be => {
Expand All @@ -144,21 +150,26 @@ private void ProcessStructures(ServerChunk?[] chunkSlices) {
Logger.Warn($"Translocator at {pos} points to {loc}");
});
}
if (_server.Config.Layers.Traders.Enabled) {
chunk?.Entities.Foreach(e => {
if (e is not EntityTrader trader) {
return;
}
string type = trader.GetName();
string? name = trader.WatchedAttributes.GetTreeAttribute("nametag")?.GetString("name");
BlockPos pos = trader.Pos.AsBlockPos;
if (tradersLayer != null) {
if (_server.Config.Layers.Traders.Enabled) {
chunk?.Entities.Foreach(e => {
if (e is not EntityTrader trader) {
return;
}
// save trader to file
Logger.Warn($"Trader at {pos} is named {name} (type: {type})");
});
string type = trader.GetName();
string? name = trader.WatchedAttributes.GetTreeAttribute("nametag")?.GetString("name");
BlockPos pos = trader.Pos.AsBlockPos;
traders.Add(new TradersLayer.Trader(type, name, pos));
Logger.Warn($"Trader at {pos} is named {name} (type: {type})");
});
}
}
});

tradersLayer?.SetTraders(chunkIndex, traders);
}

private BlockData.Data ScanBlockColumn(int x, int z, ServerMapChunk? mapChunk, ServerChunk?[] chunkSlices) {
Expand Down
6 changes: 6 additions & 0 deletions src/util/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Reflection;
using JetBrains.Annotations;
using livemap.data;
using livemap.layer.marker.options;
using Newtonsoft.Json;
using Vintagestory.API.Common;
using Vintagestory.API.Common.Entities;
using Vintagestory.API.Config;
Expand Down Expand Up @@ -104,6 +106,10 @@ public static void AutoSaveNow(this ICoreServerAPI api) {
}, 1);
}

public static T DeepCopy<T>(this T self) where T: BaseOptions {
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(self))!;
}

public static Dictionary<string, object> GetHealth(this IPlayer player) {
EntityBehaviorHealth health = player.Entity.GetBehavior<EntityBehaviorHealth>();
return new Dictionary<string, object> {
Expand Down
2 changes: 2 additions & 0 deletions web/src/layer/MarkersLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export class MarkersLayer extends L.LayerGroup {
protected initial(json: object): void {
const layerJson: LayerJson = json as LayerJson;

console.log(layerJson);

this._label = layerJson.label ?? ''; // set _something_ so we don't keep reloading json every tick
this._interval = layerJson.interval ?? 300;
this._defaults = layerJson.defaults;
Expand Down

0 comments on commit 7c2dbf2

Please sign in to comment.