namespace XFEEDToolBox.Plugin; internal static unsafe partial class Navigation { internal sealed record GalaxyPowerTerritoryFact(int rawState,int stateIndex,int ownerInternalPowerId,float radius,float[] rawRgb,float[] linearRgb,float opacity); internal sealed class GalaxyPowerTerritoryContext { internal GalaxyPowerActivityContext activity=new(); internal nuint metadata; internal GalaxyPowerOption[] options=[]; } // Original resource86ec03... config+1C/11C/120/12C/130. All floats, // the .99 threshold cap, and both ED5A debug-flag RIP bindings are proven // in power-outer-sources/territory-constants.json. This is the raw mode1 // geometry style, before the caller's owner/state/UI visibility masks. static bool GalaxyPowerTerritoryRadius(float counter,bool activityEnabled,bool minimumEnabled,float minimum,out float radius) { radius=0; if(!float.IsFinite(minimum)||minimum is <0 or >1||minimumEnabled&&!activityEnabled)return false; var value=0f; if(activityEnabled) { if(!float.IsFinite(counter)||counter<0)return false; value=counter; if(minimumEnabled) { var threshold=Math.Min(.99f,minimum); value=Math.Clamp((value-threshold)/(1-threshold),0,1); } } // Original3B28170 mode1: lerp(config11C,120,c)*object64. // Original3B27690: multiply by config12C. No camera-distance term. var size=(.75f+(2f-.75f)*value)*.7f; radius=11f*size;return float.IsFinite(radius)&&radius>0; } static bool GalaxyPowerTerritoryOverridesClear(nuint expectedImage) =>Read(expectedImage+0x5FDB275)==0&&Read(expectedImage+0x5FDB277)==0; static float GalaxyPowerGeometryLinearColour(float value) { // Exact continuous transfer6CC950, not the rounded IEC cutoff. const float gamma=2.4f,offset=.055f; var denominator=gamma-1;var raised=MathF.Pow(offset*gamma/(denominator*(1+offset)),gamma); return value(descriptor+0x1D0); if(optional>1)return false; var state=optional==0?0:Read(descriptor+0x160); if(state<0||state>65535)return false; var value=data!=0&&(optional==0||state is <33 or >38 or 33 or 37); if(Read(node+8)!=address||Pointer(descriptor+0x30)!=data||Read(descriptor+0x1D0)!=optional|| optional!=0&&Read(descriptor+0x160)!=state||Pointer(metadata)!=expectedImage+0x551A1B0)return false; allowed=value;return true; } catch{return false;} } static bool CopyGalaxyPowerTerritoryStyle(GalaxyPowerTerritoryContext context,ulong address, bool activityEnabled,bool allPowers,int actualInternalId,int actionId,bool minimumEnabled,float minimum,out GalaxyPowerTerritoryFact? result) { result=null;var source=context.activity; if(address==0||address>=0x80000000000000UL||context.options.Length is <1 or >128|| context.options.Length!=source.optionCount||!float.IsFinite(minimum)||minimum is <0 or >1||minimumEnabled&&!activityEnabled)return false; try { if(!GalaxyPowerActivitySource(source.service,source.expectedImage,source.owner)||!GalaxyPowerTerritoryOverridesClear(source.expectedImage)|| !GalaxyPowerActivityFind(source.service+0x3C0,address,-1,0x180,16,out var node))return false; if(node==0)return true; var metadata=node+0x10;var ownerIndex=Read(metadata);var state=Read(metadata+0xC); if(state==0)return true; // Original geometry emits no unoccupied bubble. if(state<0||state>65535||ownerIndex>=(ulong)context.options.Length)return false; var special=state is 3 or 4?Read(metadata+0x11):(byte)0;if(special>1)return false; var stateIndex=state switch{1=>4,2=>5,3 or 4=>special!=0?7:6,_=>0}; if(!GalaxyPowerTerritoryAdmissible(context.metadata,source.expectedImage,address,out var allowed))return false; if(allowed!=true)return true; var counter=0f; if(activityEnabled) { var selectedIndex=-1; if(allPowers?actualInternalId!=-1:!source.indices.TryGetValue(actualInternalId,out selectedIndex))return false; if(!CopyGalaxyPowerActivityCounter(source.service,source.expectedImage,source.owner,address,allPowers,selectedIndex, source.optionCount,actionId,out var observed)||observed is null)return true; counter=observed.Value; if(minimumEnabled&&counter(node)!=address||Read(metadata)!=ownerIndex||Read(metadata+0xC)!=state|| state is 3 or 4&&Read(metadata+0x11)!=special)return false; // Ordinary getColour3B265D0 uses raw RGB (dual+0C). Geometry // producer3B27690 uses the FIRST/linear triplet from3B27100. // Preserve both channels and HDR floats rather than conflating // display RGB with original material inputs. Debug alpha is off. var raw=new[]{option.rawRed,option.rawGreen,option.rawBlue};var linear=raw.Select(GalaxyPowerGeometryLinearColour).ToArray(); if(linear.Any(value=>!float.IsFinite(value)))return false; result=new(state,stateIndex,option.internalPowerId,radius,raw,linear,.15f);return true; } catch{return false;} } static bool TryCreateGalaxyPowerTerritoryContext(Snapshot frame,GalaxyServices services,out GalaxyPowerTerritoryContext result) { result=new(); if(!TryCreateGalaxyPowerActivityContext(frame,services,out var source))return false; try { if(!GalaxyPowerTerritoryOverridesClear(source.expectedImage)|| !CopyGalaxyPowerOptions(source.service-0x58,source.owner,source.expectedImage,out var options)||options.Length!=source.optionCount)return false; var metadata=RegisteredComponent(services.registry,Read(image+0x5F05C24)); if(metadata<0x10000||Pointer(metadata)!=image+0x551A1B0)return false; result=new(){activity=source,metadata=metadata,options=options};return true; } catch{return false;} } static bool TryReadGalaxyPowerTerritoryStyle(GalaxyPowerTerritoryContext context,ulong address, bool activityEnabled,bool allPowers,int actualInternalId,int actionId,bool minimumEnabled,float minimum,out GalaxyPowerTerritoryFact? result) { result=null;var source=context.activity; if(!Enabled()||!inGame||source.thread==0||source.thread!=owningThread||GetCurrentThreadId()!=source.thread|| GetTickCount64()-source.tick>500||source.expectedImage!=image)return false; var frame=Current(); if(!frame.valid||frame.entity!=source.entity||(frame.referenceWorld&0x007FFFFFFFFFFFFFUL)!=source.currentSystem)return false; return CopyGalaxyPowerTerritoryStyle(context,address,activityEnabled,allPowers,actualInternalId,actionId,minimumEnabled,minimum,out result); } }