-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
492 lines (421 loc) · 17.5 KB
/
Form1.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace GPSTracker
{
public partial class Form1 : Form
{
public SortedList<int, SortedList<string, string>> z_layers_player = new SortedList<int, SortedList<string, string>>();
public SortedList<int, SortedList<string, string>> z_layers_gps = new SortedList<int, SortedList<string, string>>();
public SortedList<int, SortedList<string, string>> z_layers_map = new SortedList<int, SortedList<string, string>>();
public SortedList<int, GraphicsState> z_map = new SortedList<int, GraphicsState>();
Graphics g = null;
bool active = false;
public Form1()
{
InitializeComponent();
g = display.CreateGraphics();
UpdateMapValues();
GetCoordsInFile();
UpdateGPSValues();
AddLayer(3, z_layers_gps);
AddLayer(4, z_layers_gps);
AddLayer(5, z_layers_gps);
AddLayer(6, z_layers_gps);
z_layer_combo_box.SelectedItem = z_layer_combo_box.Items[0];
toolTip1.ShowAlways = true;
//Refresh();
}
private void GetCoordsInFile()
{
List<string> coordsList = new List<string>();
StreamReader reader = new StreamReader("coordinates.txt", System.Text.Encoding.GetEncoding(1251));
string line = "";
while ((line = reader.ReadLine()) != null)
{
textBox1.Text += line + Environment.NewLine;
}
reader.Dispose();
reader.Close();
}
private void AddLayer(int index, SortedList<int, SortedList<string, string>> z_layers)
{
if(!z_layers.ContainsKey(index))
z_layers.Add(index, new SortedList<string, string>());
if (!z_layer_combo_box.Items.Contains(index))
{
z_layer_combo_box.Items.Add(index);
z_layer_combo_box.SelectedItem = index;
}
}
private void UpdateValues(int x, int y, int z, string color, SortedList<int, SortedList<string, string>> z_layers)
{
if (!z_layers.ContainsKey(z))
AddLayer(z, z_layers);
string key = String.Format("{0}:{1}", x, y);
if (!z_layers[z].ContainsKey(key))
z_layers[z].Add(key, color);
z_layers[z][key] = color;
}
private void UpdateGPSValues()
{
List<int[]> coords_array_gps = GetGpsData();
z_layers_gps.Clear();
foreach (int[] coords_int in coords_array_gps)
{
UpdateValues(coords_int[0], coords_int[1], coords_int[2], "#FF0000", z_layers_gps);
}
}
private void UpdateMapValues()
{
List<string> mapsList = new List<string>();
StreamReader reader = new StreamReader("maps.txt", System.Text.Encoding.GetEncoding(1251));
string line = "";
while ((line = reader.ReadLine()) != null)
{
string map = line.Split('=')[0];
mapsList.Add(map);
}
reader.Dispose();
reader.Close();
foreach (string maps in mapsList)
{
List<KeyValuePair<int[], string>> coords_array_map = GetMapData(maps);
foreach (KeyValuePair<int[], string> coords_int in coords_array_map)
{
UpdateValues(coords_int.Key[0], coords_int.Key[1], coords_int.Key[2], coords_int.Value, z_layers_map);
}
}
}
private void display_Paint()
{
/*PaintPoints(fone, z_layers_map, new SolidBrush(Color.DarkKhaki), 1, false);
GridDraw(fone);*/
int z = Convert.ToInt32(z_layer_combo_box.SelectedItem.ToString());
if (!z_map.ContainsKey(z) || z_map[z] == null)
{
g.Clear(Color.Black);
PaintPoints(g, z_layers_map, 0.5, false);
GridDraw(g);
if (!z_map.ContainsKey(z))
z_map.Add(z, g.Save());
else
z_map[z] = g.Save();
}
else
g.Restore(z_map[z]);
PaintPoints(g, z_layers_player, 0.5);
PaintPoints(g, z_layers_gps, 0.5, false);
//Refresh();
}
public void PaintPoints(Graphics g, SortedList<int, SortedList<string, string>> z_layers, double coef_scale = 1, bool connect = true)
{
if (!Convert.ToBoolean(z_layer_combo_box.SelectedItem))
return;
int z = Convert.ToInt32(z_layer_combo_box.SelectedItem.ToString());
if (!z_layers.ContainsKey(z))
return;
int width = display.Width;
int height = display.Height;
int coef_X = width / 255;
int coef_y = height / 255;
int[] last = new int[2]
{
-1,
-1,
};
foreach (string coords in z_layers[z].Keys)
{
int[] coords_int = Array.ConvertAll(coords.Split(':'), int.Parse);
int[] point = new int[2]
{
(coords_int[0] * coef_X),
(height - (coords_int[1] * coef_y))
};
Color clr = ColorTranslator.FromHtml(z_layers[z][coords]);
Brush color = new SolidBrush(clr);
g.FillRectangle(color, point[0], point[1], float.Parse((coef_X * coef_scale).ToString()), float.Parse((coef_y * coef_scale).ToString()));
if (last[0] != -1 && last[1] != -1 && connect)
g.DrawLine(new Pen(color), last[0], last[1], point[0], point[1]);
last = point;
}
}
public void GridDraw(Graphics g)
{
float coef = display.Width / 255;
for (float i = display.Width; i >= 0; i -= coef * 15)
{
g.DrawLine(new Pen(new SolidBrush(Color.DarkGreen)), 0, i, display.Width, i);
g.DrawString((256 - (i / coef)).ToString(), new Font("Arial", 5), new SolidBrush(Color.White), 0, i);
g.DrawLine(new Pen(new SolidBrush(Color.DarkGreen)), i, 0, i, display.Width);
g.DrawString((i / coef).ToString(), new Font("Arial", 5), new SolidBrush(Color.White), i, 0);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
UpdateGPSValues();
if (!active)
return;
int[] coords = GetPlayerGPSData();
if (coords.Length < 3 || coords[0] == -1)
return;
UpdateValues(coords[0], coords[1], coords[2], "#ffffff", z_layers_player);
//z_layer_combo_box.SelectedItem = coords[2];
//z_map[Convert.ToInt32(z_layer_combo_box.SelectedItem)] = null;
display_Paint();
}
private List<int[]> GetGpsData()
{
List<string> coords_gps_list = ParseCoordsList(textBox1.Text);
string GPSData = File.ReadAllText("GPSData.txt");
PositionGps positionGps = JsonConvert.DeserializeObject<PositionGps>(GPSData);
foreach (string gpsOther in positionGps.OtherGps)
{
Regex regex = new Regex(@"\D+", RegexOptions.Compiled);
string result = regex.Replace(gpsOther, " ");
regex = new Regex(@"^\s", RegexOptions.Compiled);
result = regex.Replace(result, "");
regex = new Regex(@"\s$", RegexOptions.Compiled);
result = regex.Replace(result, "");
coords_gps_list.Add(result);
}
List<int[]> coords_array = new List<int[]>();
foreach (string coords_string in coords_gps_list)
{
string[] coords_array_string = coords_string.Split(' ');
if (coords_array_string.Length < 3)
continue;
if (coords_array_string[2] == "unknown")
continue;
int[] coords_array_item = new int[coords_array_string.Length];
for (int x = 0; x < coords_array_string.Length; x++)
{
coords_array_item[x] = Convert.ToInt32(coords_array_string[x]);
}
coords_array.Add(coords_array_item);
}
return coords_array;
}
private int[] GetPlayerGPSData()
{
string GPSData = File.ReadAllText("GPSData.txt");
PositionGps positionGps = JsonConvert.DeserializeObject<PositionGps>(GPSData);
string coords = positionGps.Position;
Regex regex = new Regex(@"\D+", RegexOptions.Compiled);
string result = regex.Replace(coords, " ");
regex = new Regex(@"^\s", RegexOptions.Compiled);
result = regex.Replace(result, "");
regex = new Regex(@"\s$", RegexOptions.Compiled);
result = regex.Replace(result, "");
string[] coords_array_string = result.Split(' ');
if (coords_array_string.Length < 3)
return new int[3]
{
-1,
-1,
-1,
};
int[] coords_array = new int[coords_array_string.Length];
for (int x = 0; x < coords_array_string.Length; x++)
{
coords_array[x] = Convert.ToInt32(coords_array_string[x]);
}
return coords_array;
}
private List<KeyValuePair<int[], string>> GetMapData(string map)
{
List<KeyValuePair<int[], string>> coords_map = new List<KeyValuePair<int[], string>>();
StreamReader reader_maps = new StreamReader("maps.txt");
string z_level = "";
do
{
if (z_level != "")
break;
string map_obj = reader_maps.ReadLine();
if(map_obj.Split('=')[0] == map)
z_level = map_obj.Split('=')[1];
} while (!reader_maps.EndOfStream);
reader_maps.Close();
if (z_level == "")
return coords_map;
StreamReader reader = new StreamReader(map);
List<KeyValuePair<string, string>> tags_vision = new List<KeyValuePair<string, string>>();
string line = "";
StreamReader reader_objects = new StreamReader("objects_vision.txt");
List<KeyValuePair<string, string>> vision_obj = new List<KeyValuePair<string, string>>();
while ((line = reader_objects.ReadLine()) != null)
{
string[] line_obj = line.Split('=');
if(line_obj.Length <= 1)
vision_obj.Add(new KeyValuePair<string, string>(line_obj[0], "#f6b26b"));
else
vision_obj.Add(new KeyValuePair<string, string>(line_obj[0], line_obj[1]));
}
reader_objects.Close();
line = "";
string find_tag = String.Format(@" \= \(");
Regex regex_tag = new Regex(find_tag, RegexOptions.Compiled);
while ((line = reader.ReadLine()) != null)
{
if (!regex_tag.IsMatch(line))
{
continue;
}
string tag = line.Split(' ')[0];
tag = tag.Trim(new char[] { '"' });
bool success = false;
do
{
if ((line = reader.ReadLine()) == null)
break;
if (success)
continue;
foreach (KeyValuePair<string, string> obj in vision_obj)
{
if (line.Contains(obj.Key))
{
tags_vision.Add(new KeyValuePair<string, string>(tag, obj.Value));
success = true;
break;
}
}
} while (!line.Contains(")"));
}
reader.Close();
reader = new StreamReader(map);
for (int i = 1; i <= 255; i++)
{
string find = String.Format(@"\({0}\,1\,1\)", i);
Regex regex = new Regex(find, RegexOptions.Compiled);
line = "";
if ((line = reader.ReadLine()) == null)
{
return coords_map;
}
while (!regex.IsMatch(line))
{
line = reader.ReadLine();
}
for (int o = 1; o <= 255; o++)
{
foreach(KeyValuePair<string, string> tag in tags_vision)
{
if (line == tag.Key)
{
coords_map.Add(new KeyValuePair<int[], string>(new int[3] { i, 257 - o, Convert.ToInt32(z_level) }, tag.Value));
}
}
line = reader.ReadLine();
}
}
return coords_map;
}
private List<string> ParseCoordsList(string line)
{
List<string> coords_list = new List<string>();
if (line.Length < 1)
return coords_list;
Regex regex = new Regex(@"[(]", RegexOptions.Compiled);
while (regex.IsMatch(line))
{
line = line.Substring(regex.Match(line).Index + 1);
Regex regex_val = new Regex(@"[)]", RegexOptions.Compiled);
string result = line.Substring(0, regex_val.Match(line).Index);
regex_val = new Regex(@"\D+", RegexOptions.Compiled);
result = regex_val.Replace(result, " ");
if (result.Split(' ').Length < 3 && result.Split(' ').Length > 1)
result += " unknown";
coords_list.Add(result);
}
return coords_list;
}
private string ParseCoords(string line)
{
Regex regex = new Regex(@"[(]", RegexOptions.Compiled);
Regex regex_val = new Regex(@"[)]", RegexOptions.Compiled);
if (!regex.IsMatch(line) && !regex_val.IsMatch(line))
return "";
regex = new Regex(@"\D+", RegexOptions.Compiled);
string result = regex.Replace(line, " ");
regex = new Regex(@"^\s", RegexOptions.Compiled);
result = regex.Replace(result, "");
regex = new Regex(@"\s$", RegexOptions.Compiled);
result = regex.Replace(result, "");
return result;
}
private void button1_Click(object sender, EventArgs e)
{
active = !active;
if (active)
label3.Text = "ON";
else
label3.Text = "OFF";
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
z_layers_gps.Clear();
z_layers_player.Clear();
z_map.Clear();
textBox1.Text = "";
GetCoordsInFile();
UpdateGPSValues();
display_Paint();
}
private void z_layer_combo_box_SelectedIndexChanged(object sender, EventArgs e)
{
z_map[Convert.ToInt32(z_layer_combo_box.SelectedItem)] = null;
display_Paint();
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length < 1)
GetCoordsInFile();
UpdateGPSValues();
z_layer_combo_box.SelectedItem = z_layer_combo_box.Items[0];
display_Paint();
}
private void label3_Click(object sender, EventArgs e)
{
}
private void display_MouseDown(object sender, MouseEventArgs e)
{
float coef = display.Width / 255;
int x = Convert.ToInt32((e.X - coef / 2) / coef);
int y = Convert.ToInt32(255 - (e.Y - coef / 2) / coef);
int z = Convert.ToInt32(z_layer_combo_box.SelectedItem.ToString());
Form3 form = new Form3();
form.form = this;
form.z_layers_gps = z_layers_gps;
form.z_layers_map = z_layers_map;
form.z_layers_player = z_layers_player;
form.initial_coords = new KeyValuePair<int, KeyValuePair<int, int>>(z, new KeyValuePair<int, int>(x, y));
form.Show();
}
private void display_MouseMove(object sender, MouseEventArgs e)
{
float coef = display.Width / 255;
int x = Convert.ToInt32((e.X - coef / 2) / coef);
int y = Convert.ToInt32(255 - (e.Y - coef / 2) / coef);
int z = Convert.ToInt32(z_layer_combo_box.SelectedItem.ToString());
string coords_text = String.Format("{0} {1} {2}", x, y, z);
toolTip1.SetToolTip(display, coords_text);
}
private void coords_LocationChanged(object sender, EventArgs e)
{
}
}
public class PositionGps
{
public string Position { get; set; }
public string SavedPosition { get; set; }
public List<string> OtherGps { get; set; }
}
}