Skip to content

Commit

Permalink
Debug UI, height update
Browse files Browse the repository at this point in the history
  • Loading branch information
billw2012 committed Apr 1, 2021
1 parent f8be275 commit deee3b4
Show file tree
Hide file tree
Showing 13 changed files with 816 additions and 231 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-chrome",
"request": "launch",
"name": "Launch Chrome against docs",
"url": "http://localhost:8080",
"webRoot": "${workspaceFolder}/docs/webpage"
}
]
}
18 changes: 18 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Launch file",
"command": "explorer",
"windows": {
"command": "explorer.exe"
},
"args": [
"${file}"
],
"problemMatcher": []
}
]
}
57 changes: 29 additions & 28 deletions BetterContinents.BetterContinentsSettings.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using HarmonyLib;
using UnityEngine;

namespace BetterContinents
{
public partial class BetterContinents
{
// These are what are baked into the world when it is created
private struct BetterContinentsSettings
public struct BetterContinentsSettings
{
// Add new properties at the end, and comment where new versions start
public const int LatestVersion = 5;
Expand Down Expand Up @@ -72,6 +73,13 @@ private struct BetterContinentsSettings
private ImageMapFloat Flatmap;
private ImageMapFloat Forestmap;

public bool HasHeightmap => this.Heightmap != null;
public bool HasBiomemap => this.Biomemap != null;
public bool HasSpawnmap => this.Spawnmap != null;
public bool HasRoughmap => this.Roughmap != null;
public bool HasFlatmap => this.Flatmap != null;
public bool HasForestmap => this.Forestmap != null;

public bool OverrideBiomes => this.Biomemap != null;
public bool UseSpawnmap => this.Spawnmap != null;
public bool UseRoughmap => this.Roughmap != null && this.RoughmapBlend > 0;
Expand Down Expand Up @@ -211,7 +219,7 @@ private void InitSettings(long worldUId, bool enabled)

var flatmapPath = FlatmapPath();
if (ConfigUseRoughInvertedForFlat.Value && Roughmap != null ||
!ConfigUseRoughInvertedForFlat.Value && string.IsNullOrEmpty(flatmapPath))
!ConfigUseRoughInvertedForFlat.Value && !string.IsNullOrEmpty(flatmapPath))
{
UseRoughInvertedAsFlat = ConfigUseRoughInvertedForFlat.Value;
FlatmapBlend = ConfigFlatmapBlend.Value;
Expand Down Expand Up @@ -659,48 +667,36 @@ public float ApplyForest(float x, float y, float forest)

public Vector2? FindSpawn(string spawn) => this.Spawnmap.FindSpawn(spawn);
public IEnumerable<Vector2> GetAllSpawns(string spawn) => this.Spawnmap.GetAllSpawns(spawn);

public void ReloadHeightmap()
{
if (Heightmap != null)
if (Heightmap != null && Heightmap.LoadSourceImage())
{
if (Heightmap.LoadSourceImage())
{
Heightmap.CreateMap();
}
Heightmap.CreateMap();
}
}

public void ReloadBiomemap()
{
if (Biomemap != null)
if (Biomemap != null && Biomemap.LoadSourceImage())
{
if (Biomemap.LoadSourceImage())
{
Biomemap.CreateMap();
}
Biomemap.CreateMap();
}
}

public void ReloadSpawnmap()
{
if (Spawnmap != null)
if (Spawnmap != null && Spawnmap.LoadSourceImage())
{
if (Spawnmap.LoadSourceImage())
{
Spawnmap.CreateMap();
}
Spawnmap.CreateMap();
}
}

public void ReloadRoughmap()
{
if (Roughmap != null)
if (Roughmap != null && Roughmap.LoadSourceImage())
{
if (Roughmap.LoadSourceImage())
{
Roughmap.CreateMap();
}
Roughmap.CreateMap();
}
}

Expand All @@ -710,12 +706,17 @@ public void ReloadFlatmap()
{
ReloadRoughmap();
}
else if (Flatmap != null)
else if (Flatmap != null && Flatmap.LoadSourceImage())
{
if (Flatmap.LoadSourceImage())
{
Flatmap.CreateMap();
}
Flatmap.CreateMap();
}
}

public void ReloadForestmap()
{
if (Forestmap != null && Forestmap.LoadSourceImage())
{
Forestmap.CreateMap();
}
}
}
Expand Down
109 changes: 5 additions & 104 deletions BetterContinents.ConsolePatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;

namespace BetterContinents
{
Expand All @@ -12,119 +13,19 @@ public partial class BetterContinents
[HarmonyPatch(typeof(Console))]
private class ConsolePatch
{
private static Texture CloudTexture;
private static Texture TransparentTexture;

[HarmonyPrefix, HarmonyPatch("InputText")]
private static void InputTextPrefix(Console __instance)
{
if (AllowDebugActions)
{
string text = __instance.m_input.text.Trim();
if (text.Trim() == "bc" || text.Trim() == "bc help")
{
// __instance.Print("Better Continents: bc reload (hm/bm/sm) - reload a specific image from the source file, or all of them");
__instance.Print("Better Continents: bc dump - dump all location instance counts to the log/console");
__instance.Print("Better Continents: bc show (filter) - pin locations matching optional filter oo the map");
__instance.Print("Better Continents: bc hide (filter) - odd locations matching optional filter oo the map");
__instance.Print("Better Continents: bc bosses - toggle pins for bosses");
__instance.Print("Better Continents: bc clouds - toggle the map clouds");
}
if (text == "bc reload hm" || text == "bc reload all")
{
Settings.ReloadHeightmap();
}

Dictionary<Vector2i, ZoneSystem.LocationInstance> GetLocationInstances() =>
(Dictionary<Vector2i, ZoneSystem.LocationInstance>)
AccessTools.Field(typeof(ZoneSystem), "m_locationInstances").GetValue(ZoneSystem.instance);

if (text.StartsWith("bc dump"))
if (text == "bc" || text == "bc help")
{
var locationInstances = GetLocationInstances();

foreach (var lg in locationInstances.Values.GroupBy(l => l.m_location.m_prefabName))
{
Log($"Placed {lg.Count()} {lg.Key} locations");
}
DebugUtils.ShowHelp();
}

if (text == "bc show" || text.StartsWith("bc show ") || text == "bc bosses")
{
var typeFilters = text == "bc show"
? null
: text == "bc bosses"
? new List<string>{"StartTemple", "Eikthymir", "GDKing", "GoblinKing", "Bonemass", "Dragonqueen", "Vendor"}
: text
.Replace("bc show ", "")
.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries)
.Select(f => f.Trim())
.ToList();

var locationInstances = GetLocationInstances();
foreach (var lg in locationInstances.Values.GroupBy(l => l.m_location.m_prefabName))
{
if(typeFilters == null || typeFilters.Any(f => lg.Key.ToLower().StartsWith(f)))
{
Log($"Marking {lg.Count()} {lg.Key} locations on map");
int idx = 0;
foreach (var li in lg)
{
Minimap.instance.AddPin(li.m_position, Minimap.PinType.Icon3,
$"{li.m_location.m_prefabName} {idx++}", false, false);
}
}
}
}

if (text == "bc hide" || text.StartsWith("bc hide "))
{
var typeFilters = text == "bc hide"
? null
: text
.Replace("bc hide ", "")
.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries)
.Select(f => f.Trim())
.ToList();

var locationInstances = GetLocationInstances();

var pins = (List<Minimap.PinData>)AccessTools.Field(typeof(Minimap), "m_pins").GetValue(Minimap.instance);
foreach (var lg in locationInstances.Values.GroupBy(l => l.m_location.m_prefabName))
{
if(typeFilters == null || typeFilters.Any(f => lg.Key.ToLower().StartsWith(f)))
{
Log($"Hiding {lg.Count()} {lg.Key} locations from the map");
int idx = 0;
foreach (var li in lg)
{
var name = $"{li.m_location.m_prefabName} {idx++}";
var pin = pins.FirstOrDefault(p => p.m_name == name && p.m_pos == li.m_position);
if (pin != null)
{
Minimap.instance.RemovePin(pins.FirstOrDefault());
}
}
}
}
}

if (text == "bc clouds")
else if (DebugUtils.Subcommand(ref text, "bc"))
{
var mat = Minimap.instance.m_mapImageLarge.material;
if (mat.GetTexture("_CloudTex") == TransparentTexture)
{
mat.SetTexture("_CloudTex", CloudTexture);
}
else
{
CloudTexture = mat.GetTexture("_CloudTex");
if (TransparentTexture == null)
{
TransparentTexture = CreateFillTexture(new Color32(0, 0, 0, 0));
}
mat.SetTexture("_CloudTex", TransparentTexture);
}
DebugUtils.RunConsoleCommand(text);
}
}
}
Expand Down
35 changes: 24 additions & 11 deletions BetterContinents.WorldGeneratorPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ public partial class BetterContinents
{
// Changes to height, biome, forests, rivers etc. (this is the functional part of the mod)
[HarmonyPatch(typeof(WorldGenerator))]
private class WorldGeneratorPatch
public class WorldGeneratorPatch
{
// The base map x, y coordinates in 0..1 range
private static Dictionary<Vector2, float> cachedHeights;
private static Dictionary<Vector2, float> cachedHeights;
private static bool cacheEnabled = true;

private static readonly string[] TreePrefixes =
{
Expand Down Expand Up @@ -46,11 +47,19 @@ private static void InitializePrefix(World world)
v.m_forestTresholdMin = 0f;
v.m_forestTresholdMax = 1.15f;
}

// Log($"{v.m_name}, {v.m_prefab}, {v.m_biomeArea}, {v.m_biome}, {v.m_inForest}, {v.m_forestTresholdMin}, {v.m_forestTresholdMax}, {v.m_forcePlacement}");
}
}
}


public static void DisableCache() => cacheEnabled = false;
public static void EnableCache()
{
cacheEnabled = true;
cachedHeights.Clear();
}

// wx, wy are [-10500, 10500]
// __result should be [0, 1]
[HarmonyPrefix, HarmonyPatch("GetBaseHeight")]
Expand All @@ -61,7 +70,7 @@ private static bool GetBaseHeightPrefix(ref float wx, ref float wy, bool menuTer
return true;
}

if (cachedHeights.TryGetValue(new Vector2(wx, wy), out __result))
if (cacheEnabled && cachedHeights.TryGetValue(new Vector2(wx, wy), out __result))
{
return false;
}
Expand Down Expand Up @@ -89,20 +98,24 @@ private static void GetBaseHeightPostfix(float wx, float wy, float ___m_offset0,
{
return;
}
if (cachedHeights.Count >= 100000)

if (cacheEnabled)
{
// Can't easily do LRU, so we will just clear it entirely
cachedHeights.Clear();
if (cachedHeights.Count >= 100000)
{
// Can't easily do LRU, so we will just clear it entirely
cachedHeights.Clear();
}

// Do this AFTER clearing so we have it available below
cachedHeights[new Vector2(wx, wy)] = __result;
}
// Do this AFTER clearing so we have it available below
cachedHeights[new Vector2(wx, wy)] = __result;
}

private delegate float GetBaseHeightDelegate(WorldGenerator instance, float wx, float wy, bool menuTerrain);
private static readonly GetBaseHeightDelegate GetBaseHeightMethod
= GetDelegate<GetBaseHeightDelegate>(typeof(WorldGenerator), "GetBaseHeight");

[HarmonyPostfix, HarmonyPatch("GetBiomeHeight")]
private static void GetBiomeHeightPostfix(WorldGenerator __instance, float wx, float wy, ref float __result, World ___m_world)
{
Expand Down
Loading

0 comments on commit deee3b4

Please sign in to comment.