-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathInstanceDataDumper.cs
More file actions
142 lines (127 loc) · 4.52 KB
/
InstanceDataDumper.cs
File metadata and controls
142 lines (127 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.IO.Compression;
using ExileCore2;
using ExileCore2.PoEMemory.MemoryObjects;
namespace Radar;
public class Room
{
public string Name { get; set; }
public int MinX { get; set; }
public int MaxX { get; set; }
public int MinY { get; set; }
public int MaxY { get; set; }
}
public class TilePosition
{
public int X { get; set; }
public int Y { get; set; }
public int W { get; set; }
public int H { get; set; }
public List<string> Tiles { get; set; }
}
public class OptimizedInstanceData
{
public string Name { get; set; }
public int W { get; set; }
public int H { get; set; }
public float[] Heights { get; set; }
public int[] Walk { get; set; }
public int[] Target { get; set; }
public List<TilePosition> Tiles { get; set; }
public List<Room> Rooms { get; set; }
}
public partial class Radar
{
public void DumpInstanceData(string outputPath)
{
if (_heightData == null || _processedTerrainData == null || _processedTerrainTargetingData == null || _areaDimensions == null)
{
return;
}
try
{
var dimensions = _areaDimensions.Value;
// Create flattened arrays
var heights = new float[dimensions.X * dimensions.Y];
var walk = new int[dimensions.X * dimensions.Y];
var target = new int[dimensions.X * dimensions.Y];
// Fill the arrays
for (var y = 0; y < dimensions.Y && y < _heightData.Length; y++)
{
for (var x = 0; x < dimensions.X && x < _heightData[y].Length; x++)
{
var index = y * dimensions.X + x;
heights[index] = _heightData[y][x];
walk[index] = _processedTerrainData[y][x];
target[index] = _processedTerrainTargetingData[y][x];
}
}
// Convert to list of TilePositions
var tilePositions = _locationsByPosition.Select(kvp => new TilePosition
{
X = kvp.Key.X,
Y = kvp.Key.Y,
W = TileToGridConversion,
H = TileToGridConversion,
Tiles = kvp.Value
}).ToList();
var instanceData = new OptimizedInstanceData
{
Name = GameController.Area.CurrentArea.Area.RawName,
W = dimensions.X,
H = dimensions.Y,
Tiles = tilePositions,
Rooms = GameController.IngameState.Data.AreaGraphs.SelectMany(g => g.Rooms).Select(ToRoom).ToList(),
};
if (Settings.InstanceDumpSettings.IncludeGrids)
{
instanceData.Target = target;
instanceData.Walk = walk;
instanceData.Heights = heights;
}
// Create directory if it doesn't exist
var extension = Settings.InstanceDumpSettings.CompressDumps ? ".json.gz" : ".json";
var fullPath = Path.GetFullPath(outputPath + extension);
var directory = Path.GetDirectoryName(fullPath);
if (!string.IsNullOrEmpty(directory))
Directory.CreateDirectory(directory);
// Serialize
var json = JsonConvert.SerializeObject(
instanceData, new JsonSerializerSettings
{
Formatting = Settings.InstanceDumpSettings.CompressDumps ? Formatting.None : Formatting.Indented,
});
using var fileStream = File.Create(fullPath);
if (Settings.InstanceDumpSettings.CompressDumps)
{
using var gzipStream = new GZipStream(fileStream, CompressionLevel.Optimal);
using var writer = new StreamWriter(gzipStream);
writer.Write(json);
}
else
{
using var writer = new StreamWriter(fileStream);
writer.Write(json);
}
}
catch (Exception ex)
{
DebugWindow.LogError(ex.ToString());
}
}
private static Room ToRoom(AreaGraphRoomInstance x)
{
return new Room
{
Name = x.Name,
MinX = x.MinCoord.X * TileToGridConversion,
MinY = x.MinCoord.Y * TileToGridConversion,
MaxX = x.MaxCoord.X * TileToGridConversion,
MaxY = x.MaxCoord.Y * TileToGridConversion,
};
}
}