Skip to content

Commit

Permalink
wip patch for map boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
IceRaptor committed Jan 20, 2021
1 parent 95aa6dd commit 8221d97
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions IRTweaks/IRTweaks/IRTweaks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<Compile Include="Modules\UI\BulkScrapping.cs" />
<Compile Include="Modules\UI\CombatLog.cs" />
<Compile Include="Modules\UI\CombatSaves.cs" />
<Compile Include="Modules\UI\MapBoundary.cs" />
<Compile Include="Modules\UI\MechBayChanges.cs" />
<Compile Include="Modules\UI\SkirmishDifficultyPanelRelabel.cs" />
<Compile Include="Modules\UI\SkirmishFilter.cs" />
Expand Down
84 changes: 84 additions & 0 deletions IRTweaks/IRTweaks/Modules/UI/MapBoundary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using BattleTech;
using BattleTech.Designed;
using Harmony;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace IRTweaks.Modules.UI
{
[HarmonyPatch(typeof(EncounterLayerData), "GetEncounterBoundaryTexture")]
static class MapBoundary
{
// TODO: Add real conditional gate here.
static bool Prepare() => true;

static void Prefix(EncounterLayerData __instance,
ref Texture2D ___encounterBoundaryTexture, EncounterBoundaryChunkGameLogic ___encounterBoundaryChunk)
{
// Override the default texture composition if it's not already been created. The vanilla method will then return it.
if (___encounterBoundaryTexture == null)
{
int mapBoundaryWidth = SplatMapInfo.mapBoundaryWidth;
//int num = 2048 - SplatMapInfo.mapBoundaryWidth;
int num = 2048 - (SplatMapInfo.mapBoundaryWidth * 2);
Mod.Log.Info?.Write($"Generating boundary texture with mapBoundaryWidth:{mapBoundaryWidth} num: {num}");

// Recalculate the rectHolders for the encounter boundaries
__instance.CalculateEncounterBoundary();

if (___encounterBoundaryChunk != null && ___encounterBoundaryChunk.encounterBoundaryRectList.Count > 0)
{
___encounterBoundaryTexture = new Texture2D(512, 512, TextureFormat.ARGB32, mipChain: false);

// Initialize the map to a flat black
Color[] pixels = ___encounterBoundaryTexture.GetPixels();
for (int i = 0; i < pixels.Length; i++)
{
pixels[i] = Color.black;
}
___encounterBoundaryTexture.SetPixels(pixels);

// Iterate all the encounter boundaries
for (int rectIdx = 0; rectIdx < ___encounterBoundaryChunk.encounterBoundaryRectList.Count; rectIdx++)
{
Rect rect = ___encounterBoundaryChunk.encounterBoundaryRectList[rectIdx].rect;
int rectXOrigin = (int)rect.x;
int rectYOrigin = (int)rect.y;
Mod.Log.Info?.Write($"Texturing encounterBoundaryRect at idx: {rectIdx} with coordinates: {rectXOrigin}, {rectYOrigin} ");

for (int heightIdx = 0; (float)heightIdx < rect.height; heightIdx++)
{
for (int widthIdx = 0; (float)widthIdx < rect.width; widthIdx++)
{
int posX = widthIdx + 1024 + rectXOrigin;
int posY = heightIdx + 1024 + rectYOrigin;
if (mapBoundaryWidth < posX && posX < num && mapBoundaryWidth < posY && posY < num)
{
Mod.Log.Info?.Write($"Setting pixel at: {posX / 4}, {posY / 4} to white.");
___encounterBoundaryTexture.SetPixel(posX / 4, posY / 4, Color.white);
}
}
}
}
___encounterBoundaryTexture.Apply();

// Apply a shader (presumably that illuminates as you get closer?)
Material mat = new Material(Shader.Find("Hidden/BT-ConvertToSDF"));

// Blit the shader onto map and update mipmaps
RenderTexture temporary = RenderTexture.GetTemporary(___encounterBoundaryTexture.width, ___encounterBoundaryTexture.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
Graphics.Blit(___encounterBoundaryTexture, temporary, mat, 0);
RenderTexture.active = temporary;
//___encounterBoundaryTexture.ReadPixels(new Rect(0f, 0f, ___encounterBoundaryTexture.width, ___encounterBoundaryTexture.height), 0, 0);
___encounterBoundaryTexture.Apply(updateMipmaps: true, makeNoLongerReadable: false);

RenderTexture.active = null;
}
}
}
}
}

0 comments on commit 8221d97

Please sign in to comment.