Skip to content

Commit

Permalink
Add dynamic map scale in follow mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
LiZhenhuan1019 committed Aug 28, 2022
1 parent 7f21201 commit 65fc88a
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
### Changed
- Change "Show Map When Commanding" option to "Toggle Map When Commanding" option.

### Added
- Add dynamic map scale in follow mode. The map scale will change with character speed or camera height.

## [v1.1.0] - 2022-08-23
### Fixed
- Keep compatible with Bannerlord e1.8.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

<string id="BattleMiniMap_map_resolution" text="地图分辨率" />
<string id="BattleMiniMap_map_scale" text="地图比例" />
<string id="BattleMiniMap_dynamic_map_scale" text="动态地图比例" />
<string id="BattleMiniMap_dynamic_map_scale_hint" text="地图比例会随角色速度或相机高度而改变." />
<string id="BattleMiniMap_agent_marker_scale" text="士兵标记缩放倍率" />
<string id="BattleMiniMap_edge_opacity_factor" text="边缘不透明度" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<string id="BattleMiniMap_map_options" text="Map options" />
<string id="BattleMiniMap_show_map" text="Show Map" />
<string id="BattleMiniMap_enable_toggle_map_long_press_key" text="Enable the Toggle Map (Long Press) key" />
<string id="BattleMiniMap_show_map_when_commanding" text="Show Map When Commanding" />
<string id="BattleMiniMap_toggle_map_when_commanding" text="Toggle Map When Commanding" />
<string id="BattleMiniMap_map_width" text="Map Width" />

<string id="BattleMiniMap_horizontal_alignment" text="Horizontal Alignment" />
Expand All @@ -21,6 +21,8 @@

<string id="BattleMiniMap_map_resolution" text="Map Resolution" />
<string id="BattleMiniMap_map_scale" text="Map Scale" />
<string id="BattleMiniMap_dynamic_map_scale" text="Dynamic Map Scale" />
<string id="BattleMiniMap_dynamic_map_scale_hint" text="The map scale will change with character speed or camera height." />
<string id="BattleMiniMap_agent_marker_scale" text="Agent Marker Scale" />
<string id="BattleMiniMap_edge_opacity_factor" text="Edge Opacity Factor" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

<string id="str_battle_mini_map_map_resolution" text="{=BattleMiniMap_map_resolution}Map Resolution" />
<string id="str_battle_mini_map_map_scale" text="{=BattleMiniMap_map_scale}Map Scale" />
<string id="str_battle_mini_map_dynamic_map_scale" text="{=BattleMiniMap_dynamic_map_scale}Dynamic Map Scale" />
<string id="str_battle_mini_map_dynamic_map_scale_hint" text="{=BattleMiniMap_dynamic_map_scale_hint}The map scale will change with character speed or camera height." />
<string id="str_battle_mini_map_agent_marker_scale" text="{=BattleMiniMap_agent_marker_scale}Agent Marker Scale" />
<string id="str_battle_mini_map_edge_opacity_factor" text="{=BattleMiniMap_edge_opacity_factor}Edge Opacity Factor" />

Expand Down
29 changes: 27 additions & 2 deletions source/BattleMiniMap/src/Config/BattleMiniMapConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.IO;
using TaleWorlds.GauntletUI;
using TaleWorlds.Library;
using TaleWorlds.MountAndBlade;

namespace BattleMiniMap.Config
Expand All @@ -14,7 +15,9 @@ public enum KeyMode

public class BattleMiniMapConfig : MissionConfigBase<BattleMiniMapConfig>
{
protected static Version BinaryVersion => new Version(1, 0);
public static float DynamicScale = 1;

protected static Version BinaryVersion => new Version(1, 1);
public string ConfigVersion { get; set; } = BinaryVersion.ToString();

public bool ShowMap { get; set; } = true;
Expand All @@ -24,10 +27,12 @@ public class BattleMiniMapConfig : MissionConfigBase<BattleMiniMapConfig>
public bool ToggleMapWhenCommanding { get; set; } = true;
public int WidgetWidth { get; set; } = 400;

public bool FollowMode { get; set; } = false;
public bool FollowMode { get; set; } = true;

public float FollowModeScale { get; set; } = 0.5f;

public bool EnableDynamicScale { get; set; } = true;

public HorizontalAlignment HorizontalAlignment { get; set; } = HorizontalAlignment.Left;

public VerticalAlignment VerticalAlignment { get; set; } = VerticalAlignment.Top;
Expand All @@ -48,6 +53,11 @@ public class BattleMiniMapConfig : MissionConfigBase<BattleMiniMapConfig>

public bool ExcludeUnwalkableTerrain { get; set; } = false;

public float GetFollowModeScale()
{
return MathF.Clamp(FollowModeScale * DynamicScale, 0.1f, 3f);
}

protected override string SaveName { get; } = Path.Combine(ConfigPath.ConfigDir,
BattleMiniMapSubModule.ModuleId, nameof(BattleMiniMapConfig) + ".xml");

Expand All @@ -74,6 +84,21 @@ protected override void CopyFrom(BattleMiniMapConfig other)

protected override void UpgradeToCurrentVersion()
{
switch (ConfigVersion)
{
default:
ResetToDefault();
Serialize();
goto case "1.0";
case "1.0":
if (ShowMap)
FollowMode = true;
break;
case "1.1":
break;
}

ConfigVersion = BinaryVersion.ToString(2);
}

public static void OnMenuClosed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using MissionSharedLibrary.View.ViewModelCollection.Options.Selection;
using TaleWorlds.Core;
using TaleWorlds.GauntletUI;
using TaleWorlds.Localization;

namespace BattleMiniMap.Config
{
Expand Down Expand Up @@ -68,6 +67,11 @@ public static IIdProvider<AOptionClass> CreateOptionClassProvider(IMenuClassColl
GameTexts.FindText("str_battle_mini_map_map_scale"), null,
() => BattleMiniMapConfig.Get().FollowModeScale,
f => BattleMiniMapConfig.Get().FollowModeScale = f, 0.1f, 3, false, true));
optionCategory.AddOption(new BoolOptionViewModel(
GameTexts.FindText("str_battle_mini_map_dynamic_map_scale"),
GameTexts.FindText("str_battle_mini_map_dynamic_map_scale_hint"),
() => BattleMiniMapConfig.Get().EnableDynamicScale,
b => BattleMiniMapConfig.Get().EnableDynamicScale = b));
optionCategory.AddOption(new NumericOptionViewModel(
GameTexts.FindText("str_battle_mini_map_agent_marker_scale"), null,
() => BattleMiniMapConfig.Get().AgentMarkerScale,
Expand Down
4 changes: 2 additions & 2 deletions source/BattleMiniMap/src/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static Vec2 WorldToWidget(this IMiniMap miniMap, Vec2 p)
var position = camera.Position.AsVec2;
var direction = camera.Direction.AsVec2.Normalized().LeftVec();
var relativePosition = (p - position).TransformToLocalUnitF(direction);
return new Vec2(-relativePosition.y, relativePosition.x) * config.FollowModeScale / 100f * config.WidgetWidth + new Vec2(config.WidgetWidth / 2f, config.WidgetWidth / 2f);
return new Vec2(-relativePosition.y, relativePosition.x) * config.GetFollowModeScale() / 100f * config.WidgetWidth + new Vec2(config.WidgetWidth / 2f, config.WidgetWidth / 2f);
}
else
{
Expand All @@ -96,7 +96,7 @@ public static Vec2 WidgetToWorld(this IMiniMap miniMap, Vec2 p)
if (config.FollowMode)
{
p -= new Vec2(config.WidgetWidth / 2f, config.WidgetWidth / 2f);
p = p * 100f / config.FollowModeScale / config.WidgetWidth;
p = p * 100f / config.GetFollowModeScale() / config.WidgetWidth;
var relativePosition = new Vec2(-p.y, -p.x);

var camera = (MissionState.Current.Listener as MissionScreen).CombatCamera;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected override void OnUpdate(float dt)
//var size = Utility.WorldToMapF(10, MiniMap.Instance.Resolution, 0);
//_agentMarkerSize = Math.Max(MiniMap.Instance.MapFToWidget(size) * config.AgentMarkerScale, 1);
var size = Widgets.Utility.GetSize(this);
_agentMarkerSize = Math.Max(size.x * (config.FollowMode ? config.FollowModeScale * 0.025f : 0.01f) * config.AgentMarkerScale, 3);
_agentMarkerSize = Math.Max(size.x * (config.FollowMode ? config.GetFollowModeScale() * 0.025f : 0.01f) * config.AgentMarkerScale, 3);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected override void OnRender(TwoDimensionContext twoDimensionContext, TwoDim
var position = camera.Position.AsVec2;
var direction = camera.Direction.AsVec2.Normalized().LeftVec();
var size = Widgets.Utility.GetSize(this);
var scale = config.FollowModeScale / 100f * size.x;
var scale = config.GetFollowModeScale() / 100f * size.x;
var midPoint = new Vec2(size.X / 2, size.Y / 2);
var offset = (midPoint - MiniMap.Instance.MapToWidget(MiniMap.Instance.WorldToMapF(position)) * (scale * (MiniMap.Instance.MapBoundMax.y - MiniMap.Instance.MapBoundMin.y) / size.x) * ScaledSuggestedWidth / Math.Max(SuggestedWidth, 1));
var mesh = Widgets.Utility.CreateDrawObject2D(scale * (MiniMap.Instance.MapBoundMax.y - MiniMap.Instance.MapBoundMin.y), scale * (MiniMap.Instance.MapBoundMax.x - MiniMap.Instance.MapBoundMin.x), offset, midPoint,
Expand Down
27 changes: 25 additions & 2 deletions source/BattleMiniMap/src/View/Map/BattleMiniMapView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class BattleMiniMapView : MissionView
private MissionTimer _timer;
private bool _boundaryChanged;
private bool _isOrderViewEnabled;
private float _targetDynamicScale = 1;

public override void OnBehaviorInitialize()
{
Expand All @@ -33,7 +34,7 @@ public override void OnMissionScreenInitialize()
{
base.OnMissionScreenInitialize();

_layer = new GauntletLayer(12);
_layer = new GauntletLayer(12);
_layer.LoadMovie(nameof(BattleMiniMapView), _dataSource);
_layer.InputRestrictions.SetInputRestrictions(false, InputUsageMask.Mouse);
MissionScreen.AddLayer(_layer);
Expand All @@ -60,7 +61,7 @@ public override void OnMissionScreenTick(float dt)
{
toggleMapKeyDown = true;
}
else if(toggleMapKey.IsKeyPressed(Input))
else if (toggleMapKey.IsKeyPressed(Input))
{
BattleMiniMapConfig.Get().ShowMap = !BattleMiniMapConfig.Get().ShowMap;
if (BattleMiniMapConfig.Get().ShowMap ^ (_isOrderViewEnabled && BattleMiniMapConfig.Get().ToggleMapWhenCommanding))
Expand All @@ -74,13 +75,35 @@ public override void OnMissionScreenTick(float dt)
(_isOrderViewEnabled && BattleMiniMapConfig.Get().ToggleMapWhenCommanding)));

_dataSource.UpdateCamera();
UpdateDynamicScale(dt);

if (_timer.Check(true))
_dataSource.UpdateData();
else if (BattleMiniMapConfig.Get().FollowMode)
_dataSource.UpdateRenderData();
}

private void UpdateDynamicScale(float dt)
{
if (!_dataSource.IsEnabled || !BattleMiniMapConfig.Get().FollowMode || !BattleMiniMapConfig.Get().EnableDynamicScale)
return;

if (!MissionSharedLibrary.Utilities.Utility.IsAgentDead(MissionScreen.LastFollowedAgent))
{
var speed = MissionScreen.LastFollowedAgent.Velocity.Length;
_targetDynamicScale = 1 / MathF.Lerp(1f, 3f, speed / 50);
}
else
{
_targetDynamicScale = 1;
_targetDynamicScale *= 1 / MathF.Lerp(1f, 2f,
(MissionScreen.CombatCamera.Position.z - MiniMap.Instance.WaterLevel) / 120);
}

BattleMiniMapConfig.DynamicScale = MathF.Lerp(_targetDynamicScale, BattleMiniMapConfig.DynamicScale,
MathF.Pow(0.5f, dt));
}

public override void OnAgentBuild(Agent agent, Banner banner)
{
base.OnAgentBuild(agent, banner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected override void OnRender(TwoDimensionContext twoDimensionContext, TwoDim
var position = camera.Position.AsVec2;
var direction = camera.Direction.AsVec2.Normalized().LeftVec();
var size = Widgets.Utility.GetSize(this);
var scale = config.FollowModeScale / 100f * size.x;
var scale = config.GetFollowModeScale() / 100f * size.x;
var midPoint = new Vec2(size.X / 2, size.Y / 2);
var offset = (midPoint - MiniMap.Instance.MapToWidget(MiniMap.Instance.WorldToMapF(position)) * (scale * (MiniMap.Instance.MapBoundMax.y - MiniMap.Instance.MapBoundMin.y) / size.x) * ScaledSuggestedWidth / Math.Max(SuggestedWidth, 1));
var mesh = Widgets.Utility.CreateDrawObject2D(scale * (MiniMap.Instance.MapBoundMax.y - MiniMap.Instance.MapBoundMin.y), scale * (MiniMap.Instance.MapBoundMax.x - MiniMap.Instance.MapBoundMin.x), offset, midPoint,
Expand Down
27 changes: 14 additions & 13 deletions source/BattleMiniMap/src/View/MapTerrain/GdiMiniMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ private set

public Vec2 MapBoundMin { get; private set; }
public Vec2 MapBoundMax { get; private set; }
public float WaterLevel { get; private set; }
public float Resolution { get; private set; }
public float EdgeOpacityFactor { get; private set; }

Expand Down Expand Up @@ -144,7 +145,7 @@ private void SampleTerrainHeight(Scene scene, Bitmap image, float mapWidth, floa
// g = 255 * sin(height)
// b = 255 * (1 - cos(height))
//scene.GetTerrainMinMaxHeight(out var minHeight, out var maxHeight);
var waterLevel = scene.GetWaterLevel();
WaterLevel = scene.GetWaterLevel();
var minHeight = float.MaxValue;
var maxHeight = float.MinValue;
var minGroundHeight = float.MaxValue;
Expand All @@ -161,15 +162,15 @@ private void SampleTerrainHeight(Scene scene, Bitmap image, float mapWidth, floa
}


if (waterLevel == 0 && minGroundHeight == 2)
if (WaterLevel == 0 && minGroundHeight == 2)
{
waterLevel = minGroundHeight;
WaterLevel = minGroundHeight;
}

if (waterLevel < minHeight)
waterLevel = minHeight;
if (WaterLevel < minHeight)
WaterLevel = minHeight;

maxHeight = Math.Min(maxHeight - waterLevel, 120);
maxHeight = Math.Min(maxHeight - WaterLevel, 120);

for (var w = 0; w < mapWidth; w++)
for (var h = 0; h < mapHeight; h++)
Expand All @@ -181,11 +182,11 @@ private void SampleTerrainHeight(Scene scene, Bitmap image, float mapWidth, floa
var groundHeight = terrainHeight;
if (faceRecord.IsValid() || !scene.GetHeightAtPoint(pos, BodyFlags.CommonCollisionExcludeFlags, ref groundHeight))
{
SetPixel(image, BitmapWidth, BitmapHeight, w, h, terrainHeight, waterLevel, maxHeight, EdgeOpacityFactor);
SetPixel(image, BitmapWidth, BitmapHeight, w, h, terrainHeight, WaterLevel, maxHeight, EdgeOpacityFactor);

continue;
}
if (ExcludeUnwalkableTerrain && groundHeight >= waterLevel &&
if (ExcludeUnwalkableTerrain && groundHeight >= WaterLevel &&
Math.Abs(groundHeight - terrainHeight) < 0.5f)
{
scene.GetNavMeshFaceIndex(ref faceRecord, pos.ToVec3(terrainHeight), true);
Expand All @@ -197,15 +198,15 @@ private void SampleTerrainHeight(Scene scene, Bitmap image, float mapWidth, floa
}
}

SetPixel(image, BitmapWidth, BitmapHeight, w, h, groundHeight, waterLevel, maxHeight, EdgeOpacityFactor);
SetPixel(image, BitmapWidth, BitmapHeight, w, h, groundHeight, WaterLevel, maxHeight, EdgeOpacityFactor);
}
}

private void SetPixel(Bitmap image, int mapWidth, int mapHeight, int w, int h, float groundHeight, float waterLevel, float maxHeight, float edgeOpacityFactor)
private void SetPixel(Bitmap image, int mapWidth, int mapHeight, int w, int h, float groundHeight, float WaterLevel, float maxHeight, float edgeOpacityFactor)
{
var color = groundHeight > waterLevel
? AboveWater.GetColor((groundHeight - waterLevel) / maxHeight)
: BelowWater.GetColor((waterLevel - groundHeight) / 8);
var color = groundHeight > WaterLevel
? AboveWater.GetColor((groundHeight - WaterLevel) / maxHeight)
: BelowWater.GetColor((WaterLevel - groundHeight) / 8);
image.SetPixel(w, h,
Color.FromArgb(Math.Min(GetEdgeAlpha(w, h, edgeOpacityFactor), color.A), color.R, color.G, color.B));
}
Expand Down
1 change: 1 addition & 0 deletions source/BattleMiniMap/src/View/MapTerrain/IMiniMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IMiniMap

public Vec2 MapBoundMin { get; }
public Vec2 MapBoundMax { get; }
public float WaterLevel { get; }
public float Resolution { get; }
public float EdgeOpacityFactor { get; }
bool IsValid { get; }
Expand Down
2 changes: 2 additions & 0 deletions source/BattleMiniMap/src/View/MapTerrain/SimpleMiniMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class SimpleMiniMap : IMiniMap

public Vec2 MapBoundMin { get; private set; }
public Vec2 MapBoundMax { get; private set; }
public float WaterLevel { get; private set; }
public float Resolution { get; private set; }
public float EdgeOpacityFactor { get; set; }

Expand Down Expand Up @@ -72,6 +73,7 @@ public void UpdateMapSize(Mission mission, bool updateMap = false)
public void UpdateMapImage(Mission mission)
{
var scene = mission.Scene;
WaterLevel = scene.GetWaterLevel();
var newImage = new ImageRGBA(BitmapWidth, BitmapHeight);
SampleTerrainHeight(scene, newImage, BitmapWidth, BitmapHeight, MapBoundMin.x, MapBoundMin.y, Resolution);
SampleBoundaries(mission, newImage, BitmapWidth, BitmapHeight, MapBoundMin.x, MapBoundMin.y, Resolution);
Expand Down

0 comments on commit 65fc88a

Please sign in to comment.