-
Notifications
You must be signed in to change notification settings - Fork 0
/
Egcb_ConsoleUtilityFunctions.cs
213 lines (199 loc) · 8.78 KB
/
Egcb_ConsoleUtilityFunctions.cs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using XRL.Core;
using XRL.World;
using XRL.World.Parts;
using ConsoleLib.Console;
using GameObject = XRL.World.GameObject;
using Color = UnityEngine.Color;
namespace Egocarib.Console
{
public class Coords
{
private readonly int x;
public int X { get { return x; } }
private readonly int y;
public int Y { get { return y; } }
public Coords(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class TileMaker
{
public string Tile;
public string RenderString;
public string BackgroundString;
public char DetailColorChar;
public char ForegroundColorChar;
public char BackgroundColorChar;
public ushort Attributes;
public Color DetailColor;
public Color ForegroundColor;
public Color BackgroundColor;
public bool IsValid()
{
return (!string.IsNullOrEmpty(this.Tile) || !string.IsNullOrEmpty(this.RenderString));
}
public TileMaker(string blueprintString)
{
GameObject go = null;
if (GameObjectFactory.Factory.Blueprints.ContainsKey(blueprintString))
{
go = GameObjectFactory.Factory.CreateObject(blueprintString);
}
this.Initialize(go, false);
if (go != null)
{
go.Destroy();
}
}
public TileMaker(GameObject go)
{
this.Initialize(go);
}
//writes a tile to a ScreenBuffer. You'll still need to draw the buffer yourself
public bool WriteTileToBuffer(ScreenBuffer scrapBuffer, int x, int y)
{
if (scrapBuffer == null || x < 0 || x >= scrapBuffer.Width || y < 0 || y >= scrapBuffer.Height)
{
return false;
}
scrapBuffer[x, y].Attributes = this.Attributes;
if (!string.IsNullOrEmpty(this.Tile))
{
scrapBuffer[x, y].TileLayerBackground[0] = this.DetailColor;
scrapBuffer[x, y].TileLayerForeground[0] = this.ForegroundColor;
scrapBuffer[x, y].Tile = this.Tile;
}
else if (!string.IsNullOrEmpty(this.RenderString))
{
scrapBuffer[x, y].ClearTileLayers();
scrapBuffer[x, y].Char = this.RenderString[0];
}
else
{
return false;
}
return true;
}
//returns true if the tile is already applied at the specified screen coordinates
//(can be used for efficiency to avoid unnecessary work if you're drawing every frame)
public bool IsTileOnScreen(int x, int y)
{
if (x < 0 || x >= TextConsole.CurrentBuffer.Width || y < 0 || y >= TextConsole.CurrentBuffer.Height)
{
return false;
}
ConsoleChar screenChar = TextConsole.CurrentBuffer[x, y];
bool tileApplied = screenChar != null
&& screenChar.Attributes == this.Attributes
&& ((!string.IsNullOrEmpty(this.Tile)
&& screenChar.Tile == this.Tile
&& screenChar.TileLayerBackground[0] == this.DetailColor
&& screenChar.TileLayerForeground[0] == this.ForegroundColor)
|| (!string.IsNullOrEmpty(this.RenderString)
&& screenChar.Char == this.RenderString[0]));
return tileApplied;
}
public bool IsTileOnScreen(Coords coords)
{
return this.IsTileOnScreen(coords.X, coords.Y);
}
private void Initialize(GameObject go, bool renderOK = true)
{
this.Tile = string.Empty;
this.RenderString = string.Empty;
this.BackgroundString = string.Empty;
this.DetailColorChar = 'k';
this.ForegroundColorChar = 'y';
this.BackgroundColorChar = 'k';
this.DetailColor = ConsoleLib.Console.ColorUtility.ColorMap['k'];
this.ForegroundColor = ConsoleLib.Console.ColorUtility.ColorMap['y'];
this.BackgroundColor = ConsoleLib.Console.ColorUtility.ColorMap['k'];
//gather render data for GameObject similar to how the game does it in Cell.cs
Render pRender = go?.pRender;
if (pRender == null || !pRender.Visible || Globals.RenderMode != RenderModeType.Tiles)
{
return;
}
RenderEvent renderData = new RenderEvent();
Examiner examinerPart = go.GetPart<Examiner>();
if (examinerPart != null && !string.IsNullOrEmpty(examinerPart.UnknownTile) && !go.Understood())
{
renderData.Tile = examinerPart.UnknownTile;
}
else
{
renderData.Tile = go.pRender.Tile;
}
if (!string.IsNullOrEmpty(pRender.TileColor))
{
renderData.ColorString = pRender.TileColor;
}
else
{
renderData.ColorString = pRender.ColorString;
}
if (renderOK) //we can't render blueprint-created objects, because the game will throw errors trying to check their current cell
{
go.Render(renderData);
}
//renderData.Tile can be null if something has a temporary character replacement, like the up arrow from flying
this.Tile = !string.IsNullOrEmpty(renderData.Tile) ? renderData.Tile : pRender.Tile;
this.RenderString = !string.IsNullOrEmpty(renderData.RenderString) ? renderData.RenderString : pRender.RenderString;
this.BackgroundString = renderData.BackgroundString;
////DEBUG
//UnityEngine.Debug.Log("Render data from GameObject.Render() for " + go.DisplayName + ":\n Tile=" + renderData.Tile
// + "\n ColorString=" + renderData.ColorString
// + "\n DetailColor=" + renderData.DetailColor
// + "\n RenderString=" + renderData.RenderString
// + "\n BackgroundString=" + renderData.BackgroundString
// + "\nRender data from object itself:"
// + "\n Tile=" + pRender.Tile
// + "\n RenderString=" + pRender.RenderString
// + "\n TileColor=" + pRender.TileColor
// + "\n ColorString=" + pRender.ColorString
// + "\n DetailColor=" + pRender.DetailColor);
////DEBUG
//save render data in our custom TileColorData format, using logic similar to QudItemListElement.InitFrom()
if (!string.IsNullOrEmpty(pRender.DetailColor))
{
this.DetailColor = ConsoleLib.Console.ColorUtility.ColorMap[pRender.DetailColor[0]];
this.DetailColorChar = pRender.DetailColor[0];
}
//from what I've been able to determine, I believe that the BackgroundString only applies to non-tiles (RenderString) entities (such as gas clouds)
string colorString = renderData.ColorString + (string.IsNullOrEmpty(this.Tile) ? this.BackgroundString : string.Empty);
if (!string.IsNullOrEmpty(colorString))
{
for (int j = 0; j < colorString.Length; j++)
{
if (colorString[j] == '&' && j < colorString.Length - 1)
{
if (colorString[j + 1] == '&')
{
j++;
}
else
{
this.ForegroundColor = ConsoleLib.Console.ColorUtility.ColorMap[colorString[j + 1]];
this.ForegroundColorChar = colorString[j + 1];
}
}
if (colorString[j] == '^' && j < colorString.Length - 1)
{
if (colorString[j + 1] == '^')
{
j++;
}
else
{
this.BackgroundColor = ConsoleLib.Console.ColorUtility.ColorMap[colorString[j + 1]];
this.BackgroundColorChar = colorString[j + 1];
}
}
}
}
this.Attributes = ColorUtility.MakeColor(ColorUtility.CharToColorMap[this.ForegroundColorChar], ColorUtility.CharToColorMap[this.BackgroundColorChar]);
}
}
}