-
Notifications
You must be signed in to change notification settings - Fork 11
/
GameUtils.cs
665 lines (590 loc) · 26.9 KB
/
GameUtils.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using HarmonyLib;
using UnityEngine;
using Object = UnityEngine.Object;
namespace BetterContinents
{
public static class GameUtils
{
private static void RegenerateHeightmaps()
{
var sw = new Stopwatch();
sw.Start();
// Regenerate all heightmaps
foreach (var heightmap in Resources.FindObjectsOfTypeAll<Heightmap>())
{
heightmap.CancelInvoke();
heightmap.Clear();
heightmap.Regenerate();
}
BetterContinents.Log($"Regenerating heightmaps took {sw.ElapsedMilliseconds} ms");
}
private static void RegenerateDistantLod()
{
foreach (var lod in Object.FindObjectsOfType<TerrainLod>())
{
lod.m_needRebuild = true;
lod.m_hmap.m_buildData = null;
}
}
private static Dictionary<ZDOID, ZDO> GetObjectsByID() => ZDOMan.instance.m_objectsByID;
public static void BeginTerrainChanges()
{
// Stop and reset the heightmap generator first
HeightmapBuilder.instance.Dispose();
var _ = new HeightmapBuilder();
}
public static void EndTerrainChanges()
{
Refresh();
}
public static void Refresh()
{
DespawnAll();
ClutterSystem.instance.ClearAll();
DeleteAllTaggedZDOs();
ResetLocationInstances();
RegenerateDistantLod();
FastMinimapRegen();
}
public static void RegenerateLocations()
{
DespawnAll();
ClutterSystem.instance.ClearAll();
DeleteLocationZDOs();
ResetLocationInstances();
ZoneSystem.instance.GenerateLocations();
ResetLocPins();
}
private static int MinimapOrigTextureSize = 0;
private static float MinimapOrigPixelSize = 0;
public static int MinimapDownscalingPower = 2;
public static void FastMinimapRegen()
{
int MinimapDownscaling = (int) Mathf.Pow(2, Mathf.Clamp(MinimapDownscalingPower, 0, 3));
if (MinimapOrigTextureSize == 0
|| Minimap.instance.m_textureSize != MinimapOrigTextureSize / MinimapDownscaling)
{
if(MinimapOrigTextureSize == 0)
{
MinimapOrigTextureSize = Minimap.instance.m_textureSize;
MinimapOrigPixelSize = Minimap.instance.m_pixelSize;
}
Minimap.instance.m_textureSize = MinimapOrigTextureSize / MinimapDownscaling;
Minimap.instance.m_pixelSize = MinimapOrigPixelSize * MinimapDownscaling;
Minimap.instance.m_mapTexture = new Texture2D(Minimap.instance.m_textureSize, Minimap.instance.m_textureSize, TextureFormat.RGBA32, false);
Minimap.instance.m_mapTexture.wrapMode = TextureWrapMode.Clamp;
Minimap.instance.m_forestMaskTexture = new Texture2D(Minimap.instance.m_textureSize, Minimap.instance.m_textureSize, TextureFormat.RGBA32, false);
Minimap.instance.m_forestMaskTexture.wrapMode = TextureWrapMode.Clamp;
Minimap.instance.m_heightTexture = new Texture2D(Minimap.instance.m_textureSize, Minimap.instance.m_textureSize, TextureFormat.RFloat, false);
Minimap.instance.m_heightTexture.wrapMode = TextureWrapMode.Clamp;
Minimap.instance.m_fogTexture = new Texture2D(Minimap.instance.m_textureSize, Minimap.instance.m_textureSize, TextureFormat.RGBA32, false);
Minimap.instance.m_fogTexture.wrapMode = TextureWrapMode.Clamp;
Minimap.instance.m_explored = new bool[Minimap.instance.m_textureSize * Minimap.instance.m_textureSize];
Minimap.instance.m_mapImageLarge.material = Object.Instantiate<Material>(Minimap.instance.m_mapImageLarge.material);
Minimap.instance.m_mapImageSmall.material = Object.Instantiate<Material>(Minimap.instance.m_mapImageSmall.material);
Minimap.instance.m_mapImageLarge.material.SetTexture("_MainTex", Minimap.instance.m_mapTexture);
Minimap.instance.m_mapImageLarge.material.SetTexture("_MaskTex", Minimap.instance.m_forestMaskTexture);
Minimap.instance.m_mapImageLarge.material.SetTexture("_HeightTex", Minimap.instance.m_heightTexture);
Minimap.instance.m_mapImageLarge.material.SetTexture("_FogTex", Minimap.instance.m_fogTexture);
Minimap.instance.m_mapImageSmall.material.SetTexture("_MainTex", Minimap.instance.m_mapTexture);
Minimap.instance.m_mapImageSmall.material.SetTexture("_MaskTex", Minimap.instance.m_forestMaskTexture);
Minimap.instance.m_mapImageSmall.material.SetTexture("_HeightTex", Minimap.instance.m_heightTexture);
Minimap.instance.m_mapImageSmall.material.SetTexture("_FogTex", Minimap.instance.m_fogTexture);
}
Minimap.instance.ForceRegen();
Minimap.instance.ExploreAll();
}
public static void SaveMinimap(string path, int size)
{
BetterContinents.instance.StartCoroutine(SaveMinimapImpl(path, size));
}
private static GameObject CreateQuad(float width, float height, float z, Material material)
{
var gameObject = new GameObject();
MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshRenderer.sharedMaterial = material;//new Material(Shader.Find("Standard"));
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3[4]
{
new Vector3(-width / 2, -height / 2, z),
new Vector3(width / 2, -height / 2, z),
new Vector3(-width / 2, height / 2, z),
new Vector3(width / 2, height / 2, z)
};
mesh.vertices = vertices;
int[] tris = new int[6]
{
// lower left triangle
0, 2, 1,
// upper right triangle
2, 3, 1
};
mesh.triangles = tris;
Vector3[] normals = new Vector3[4]
{
-Vector3.forward,
-Vector3.forward,
-Vector3.forward,
-Vector3.forward
};
mesh.normals = normals;
Vector2[] uv = new Vector2[4]
{
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(1, 1)
};
mesh.uv = uv;
meshFilter.mesh = mesh;
return gameObject;
}
private static Texture CloudTexture;
private static Texture TransparentTexture;
private static bool MinimapCloudsEnabled =>
Minimap.instance.m_mapImageLarge.material.GetTexture("_CloudTex") != TransparentTexture;
private static void EnableMinimapClouds()
{
if (!MinimapCloudsEnabled)
{
Minimap.instance.m_mapImageLarge.material.SetTexture("_CloudTex", CloudTexture);
}
}
private static void DisableMinimapClouds()
{
if(MinimapCloudsEnabled)
{
var mat = Minimap.instance.m_mapImageLarge.material;
CloudTexture = mat.GetTexture("_CloudTex");
if (TransparentTexture == null)
{
TransparentTexture = UI.CreateFillTexture(new Color32(0, 0, 0, 0));
}
mat.SetTexture("_CloudTex", TransparentTexture);
}
}
private static IEnumerator SaveMinimapImpl(string path, int size)
{
bool wasLarge = Minimap.instance.m_largeRoot.activeSelf;
if (!wasLarge)
{
Minimap.instance.SetMapMode(Minimap.MapMode.Large);
Minimap.instance.CenterMap(Vector3.zero);
}
bool wasClouds = MinimapCloudsEnabled;
DisableMinimapClouds();
var mapPanelObject = CreateQuad(100, 100, 10, Minimap.instance.m_mapImageLarge.material);
mapPanelObject.layer = 19;
var renderTexture = new RenderTexture(size, size, 24);
var cameraObject = new GameObject();
cameraObject.layer = 19;
var camera = cameraObject.AddComponent<Camera>();
camera.targetTexture = renderTexture;
camera.orthographic = true;
camera.rect = new Rect(0, 0, renderTexture.width, renderTexture.height);
camera.nearClipPlane = 0;
camera.farClipPlane = 100;
camera.orthographicSize = 50;
camera.cullingMask = 1 << 19;
camera.Render();
yield return new WaitForEndOfFrame();
RenderTexture.active = renderTexture;
var tex = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
tex.Apply();
RenderTexture.active = null;
Directory.CreateDirectory(Path.GetDirectoryName(path));
Console.instance.Print($"Screenshot of minimap saved to {path}");
File.WriteAllBytes(path, ImageConversion.EncodeToPNG(tex));
Object.Destroy(mapPanelObject);
Object.Destroy(cameraObject);
Object.Destroy(renderTexture);
Object.Destroy(tex);
if (!wasLarge)
{
Minimap.instance.SetMapMode(Minimap.MapMode.Small);
}
if (wasClouds)
{
EnableMinimapClouds();
}
}
private static void ResetLocPins()
{
Minimap.instance.UpdateLocationPins(1000);
}
private static void DeleteTaggedZDOs(params string[] tags)
{
foreach (var zdo in GetObjectsByID().Values
.Where(z => tags.Any(t => z.GetInt(t) == 1))
.ToList()
)
{
ZDOMan.instance.HandleDestroyedZDO(zdo.m_uid);
}
}
private static void DeleteAllTaggedZDOs() => DeleteTaggedZDOs("bc_loc", "bc_veg", "bc_spawn");
private static void DeleteLocationZDOs() => DeleteTaggedZDOs("bc_loc", "bc_spawn");
private static void ResetLocationInstances()
{
// For each location recreate its instance with the correct height, marking it as unplaced (as we deleted it above we hope!)
ZoneSystem.instance.m_locationInstances =
ZoneSystem.instance.m_locationInstances.ToDictionary(kv => kv.Key,
kv => new ZoneSystem.LocationInstance
{
m_location = kv.Value.m_location,
m_placed = false,
m_position = new Vector3(
kv.Value.m_position.x,
WorldGenerator.instance.GetHeight(kv.Value.m_position.x, kv.Value.m_position.z),
kv.Value.m_position.z
)
});
ResetLocPins();
}
// Does NOT support sub directories in the resources...
public static void UnpackDirectoryFromResources(string resourceDirectory, string targetDirectory)
{
var execAssembly = Assembly.GetExecutingAssembly();
try
{
Directory.CreateDirectory(targetDirectory);
BetterContinents.Log($"Extracting all files from {resourceDirectory} to {targetDirectory}");
if (!resourceDirectory.EndsWith("."))
resourceDirectory += ".";
foreach (string fullResourceName in execAssembly.GetManifestResourceNames()
.Where(str => str.StartsWith(resourceDirectory)))
{
string targetFileName =
Path.Combine(targetDirectory, fullResourceName.Replace(resourceDirectory, ""));
if (!File.Exists(targetFileName))
{
BetterContinents.Log($"Extracting {fullResourceName} to {targetFileName} ...");
using var stream = execAssembly.GetManifestResourceStream(fullResourceName);
using var targetStream = File.OpenWrite(targetFileName);
stream?.CopyTo(targetStream);
}
else
{
BetterContinents.Log($"{targetFileName} already exists, skipping extraction");
}
}
}
catch (Exception ex)
{
BetterContinents.LogError($"Failed to unpack resource directory {resourceDirectory} to {targetDirectory}: {ex.Message}");
}
}
// public static void UnpackFromResources(string partialResourceName, string targetFileName)
// {
// var execAssembly = Assembly.GetExecutingAssembly();
//
// try
// {
// string fullResourceName = execAssembly.GetManifestResourceNames()
// .Single(str => str.EndsWith(partialResourceName));
//
// using var stream = execAssembly.GetManifestResourceStream(fullResourceName);
// Directory.CreateDirectory(Path.GetDirectoryName(targetFileName));
// stream?.CopyTo(File.OpenWrite(targetFileName));
// }
// catch (Exception ex)
// {
// BetterContinents.LogError($"Failed to unpack resource {partialResourceName} to {targetFileName}: {ex.Message}");
// }
// }
public static AssetBundle GetAssetBundleFromResources(string partialResourceName)
{
var execAssembly = Assembly.GetExecutingAssembly();
try
{
string fullResourceName = execAssembly.GetManifestResourceNames()
.Single(str => str.EndsWith(partialResourceName));
BetterContinents.Log($"Loading asset bundle {fullResourceName}");
using var stream = execAssembly.GetManifestResourceStream(fullResourceName);
return AssetBundle.LoadFromStream(stream);
}
catch(Exception ex)
{
BetterContinents.LogError($"Failed to get asset bundle {partialResourceName}: {ex.Message}");
return null;
}
}
// [HarmonyPatch(typeof(Player))]
// private class PlayerPatch
// {
// [HarmonyPrefix, HarmonyPatch(nameof(Player.OnDestroy))]
// private static void PlayerOnDestroyPrefix(Player __instance)
// {
// BetterContinents.Log($"Butwhhhy?");
// }
// }
// [HarmonyPatch(typeof(Object))]
// private class ObjectPatch
// {
// private static IEnumerable<GameObject> All(GameObject obj)
// {
// yield return obj;
// var parent = obj.transform.parent;
// while (parent != null)
// {
// yield return parent.gameObject;
// parent = parent.parent;
// }
// }
//
// [HarmonyPrefix, HarmonyPatch(nameof(Object.Destroy), typeof(Object))]
// private static void DestroyPrefix(Object obj)
// {
// if (obj != null)
// {
// BetterContinents.Log($"Destroying {obj.name}");
// if(Player.m_localPlayer != null && All(Player.m_localPlayer.gameObject).Contains(obj))
// {
// BetterContinents.Log($"Butwhhhy?");
// }
// }
// }
//
// [HarmonyPrefix, HarmonyPatch(nameof(Object.Destroy), typeof(Object), typeof(float))]
// private static void Destroy2Prefix(Object obj)
// {
// if (obj != null)
// {
// BetterContinents.Log($"Destroying {obj.name}");
// if (Player.m_localPlayer != null && All(Player.m_localPlayer.gameObject).Contains(obj))
// {
// BetterContinents.Log($"Butwhhhy?");
// }
// }
// }
// }
public static void DespawnAll()
{
ZNetScene.instance.RemoveObjects(new List<ZDO>
{
Player.m_localPlayer.m_nview.m_zdo
}, new List<ZDO>());
foreach (var kv in ZoneSystem.instance.m_zones)
{
Object.Destroy(kv.Value.m_root);
}
ZoneSystem.instance.m_zones.Clear();
ZoneSystem.instance.m_generatedZones.Clear();
}
public static void ResetAll()
{
DespawnAll();
var playerZDO = Player.m_localPlayer.m_nview.m_zdo;
// Clear all the ZDOs except the player
var zdoToDestroy = GetObjectsByID().Values
.Where(z => z != playerZDO)
.ToList();
foreach (var zdo in zdoToDestroy)
{
ZDOMan.instance.HandleDestroyedZDO(zdo.m_uid);
}
ZDOMan.instance.ResetSectorArray();
ZDOMan.instance.AddToSector(playerZDO, playerZDO.m_sector);
ResetLocationInstances();
}
/*
* How to update locations:
* Prefix and postfix SpawnLocation, capture all created ZDOs in-between, add location tag to them all (ZDO.Set("location_tag", 0)).
* To properly fix-up all locations:
* 1. de-spawn all active zones
* 2. delete all ZDOs with location tags (can delete all creatures as well)
* 3. fixup heights of all LocationInstances
* 4. allow spawning again
*/
// This isn't correct yet, we don't want to fall ALL ZNetViews, only very specific ones (check above)
// private static void UpdateZNetViewHeights()
// {
// var GetFallHeight = AccessTools.Method(typeof(StaticPhysics), "GetFallHeight");
// var PushUp = AccessTools.Method(typeof(StaticPhysics), "CheckFall");
// var m_nview_FI = AccessTools.Field(typeof(StaticPhysics), "m_nview");
// foreach (var obj in Resources.FindObjectsOfTypeAll<ZNetView>()
// .Where(z => z.transform != null))
// {
// // Prefer to use static physics
// var staticPhysics = obj.GetComponent<StaticPhysics>();
// if (staticPhysics != null && (staticPhysics.m_fall || staticPhysics.m_pushUp))
// {
// if (staticPhysics.m_fall)
// {
// // Instant fall
// //CheckFall.Invoke(obj, new object[]{});
// float fallHeight = (float) GetFallHeight.Invoke(staticPhysics, new object[] { });
// Vector3 position = staticPhysics.transform.position;
// position.y = fallHeight;
// staticPhysics.transform.position = position;
// var m_nview = (ZNetView) m_nview_FI.GetValue(staticPhysics);
// if (m_nview && m_nview.IsValid() && m_nview.IsOwner())
// {
// m_nview.GetZDO().SetPosition(staticPhysics.transform.position);
// }
// }
//
// if (staticPhysics.m_pushUp)
// {
// PushUp.Invoke(staticPhysics, new object[] { });
// }
//
// // var pos = obj.transform.position;
// // pos.y = WorldGenerator.instance.GetHeight(pos.x, pos.z);
// // obj.transform.position = pos;
// }
// else if (staticPhysics == null && obj.GetZDO() != null)
// {
// obj.transform.position = obj.GetZDO().GetPosition();
// }
// }
// }
// public static void FallAllObjects()
// {
// foreach (var zdo in GetObjectsByID().Values)
// {
// var pos = zdo.GetPosition();
// pos.y = WorldGenerator.instance.GetHeight(pos.x, pos.z);
//
// zdo.SetPosition(pos);
// }
//
// UpdateZNetViewHeights();
//
// // //var m_nview_FI = AccessTools.Field(typeof(ZNetView), "m_nview");
// // foreach (var obj in Resources.FindObjectsOfTypeAll<ZNetView>())
// // {
// // //var m_nview = (ZNetView)m_nview_FI.GetValue(obj);
// // //if (m_nview && m_nview.IsValid() && m_nview.IsOwner())
// // //{
// // if (obj.GetZDO() != null)
// // {
// // obj.transform.position = obj.GetZDO().GetPosition();
// // }
// // else
// // {
// // var staticPhysics = obj.GetComponent<StaticPhysics>();
// // if (staticPhysics != null && (staticPhysics.m_fall || staticPhysics.m_pushUp))
// // {
// // var pos = obj.transform.position;
// // pos.y = WorldGenerator.instance.GetHeight(pos.x, pos.z);
// // obj.transform.position = pos;
// // }
// // }
// // //}
// // }
// }
// var CheckFall = AccessTools.Method(typeof(StaticPhysics), "CheckFall");
// var GetFallHeight = AccessTools.Method(typeof(StaticPhysics), "GetFallHeight");
// var PushUp = AccessTools.Method(typeof(StaticPhysics), "CheckFall");
// var m_nview_FI = AccessTools.Field(typeof(StaticPhysics), "m_nview");
// foreach (var obj in Resources.FindObjectsOfTypeAll<StaticPhysics>())
// {
// if (obj.m_fall)
// {
// // Instant fall
// //CheckFall.Invoke(obj, new object[]{});
// float fallHeight = (float)GetFallHeight.Invoke(obj, new object[]{});
// Vector3 position = obj.transform.position;
// position.y = fallHeight;
// obj.transform.position = position;
// var m_nview = (ZNetView)m_nview_FI.GetValue(obj);
// if (m_nview && m_nview.IsValid() && m_nview.IsOwner())
// {
// m_nview.GetZDO().SetPosition(obj.transform.position);
// }
// }
// if (obj.m_pushUp) PushUp.Invoke(obj, new object[]{});
// }
//
// ZDOMan.instance.
// }
public static Dictionary<Vector2i, ZoneSystem.LocationInstance> GetLocationInstances() =>
(Dictionary<Vector2i, ZoneSystem.LocationInstance>) AccessTools.Field(typeof(ZoneSystem), "m_locationInstances").GetValue(ZoneSystem.instance);
public static void ShowOnMap(params string[] list)
{
var locationInstances = GetLocationInstances();
foreach (var lg in locationInstances.Values.GroupBy(l => l.m_location.m_prefabName))
{
if (list == null || list.Length == 0 || list.Any(f => lg.Key.ToLower().StartsWith(f.ToLower())))
{
BetterContinents.Log($"Marking {lg.Count()} {lg.Key} locations on map");
int idx = 0;
foreach (var li in lg)
{
Minimap.instance.AddPin(li.m_position, Minimap.PinType.Icon3,
$"{li.m_location.m_prefabName} {idx++}", false, false);
}
}
}
}
public static void HideOnMap(params string[] list)
{
var pins = Minimap.instance.m_pins;
if (list == null || list.Length == 0)
{
foreach (var pin in pins.ToList())
{
Minimap.instance.RemovePin(pin);
}
}
else
{
var locationInstances = GetLocationInstances();
foreach (var lg in locationInstances.Values.GroupBy(l => l.m_location.m_prefabName))
{
if (list.Any(f => lg.Key.ToLower().StartsWith(f.ToLower())))
{
BetterContinents.Log($"Hiding {lg.Count()} {lg.Key} locations from the map");
int idx = 0;
foreach (var li in lg)
{
var name = $"{li.m_location.m_prefabName} {idx++}";
var pin = pins.FirstOrDefault(p => p.m_name == name && p.m_pos == li.m_position);
if (pin != null)
{
Minimap.instance.RemovePin(pins.FirstOrDefault());
}
}
}
}
}
}
public static void SimpleParallelFor(int taskCount, int from, int to, Action<int> action)
{
var tasks = new Task[taskCount];
int perTaskCount = (to - from) / taskCount;
for (int i = 0, f = from; i < taskCount - 1; i++, f += perTaskCount)
{
int taskFrom = f;
int taskTo = f + perTaskCount;
tasks[i] = Task.Run(() =>
{
for (int j = taskFrom; j < taskTo; j++)
{
action(j);
}
});
}
// Make sure last task definitely captures all the values
tasks[taskCount - 1] = Task.Run(() =>
{
for (int j = from + (taskCount - 1) * perTaskCount; j < to; j++)
{
action(j);
}
});
Task.WaitAll(tasks);
}
}
}