LocationList.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SOTF_Struct.Generic;
  7. namespace SOTF_Struct {
  8. [Flags]
  9. public enum eLocationList : int {
  10. Actor = 1,
  11. Construction = 2,
  12. StructureInstance = 4
  13. }
  14. public static class LocationList {
  15. public static void ExportLocation(List<Actor> list) {
  16. if (!Directory.Exists("locations"))
  17. Directory.CreateDirectory("locations");
  18. string file = getLocationFile(eLocationList.Actor);
  19. List<Position> poses = new List<Position>();
  20. if (File.Exists(file)) {
  21. StreamReader reader = new StreamReader(file);
  22. BinaryReader binreader = new BinaryReader(reader.BaseStream);
  23. while (binreader.BaseStream.Position < binreader.BaseStream.Length) {
  24. int area = binreader.ReadInt32();
  25. double x = binreader.ReadDouble();
  26. double y = binreader.ReadDouble();
  27. double z = binreader.ReadDouble();
  28. poses.Add(new Position(Math.Round(x/2), Math.Round(y/2), Math.Round(z/2)));
  29. }
  30. reader.Close();
  31. reader.Dispose();
  32. reader = null;
  33. }
  34. StreamWriter writer = new StreamWriter(file, true);
  35. BinaryWriter bin = new BinaryWriter(writer.BaseStream);
  36. int count = 0;
  37. foreach (Actor i in list) {
  38. Position tmpPos = new Position(Math.Round(i.Position.x / 2), Math.Round(i.Position.y / 2), Math.Round(i.Position.z / 2));
  39. if (IsValidActor(i) && !poses.Contains(tmpPos)) {
  40. bin.Write(i.GraphMask);
  41. bin.Write(i.Position.x);
  42. bin.Write(i.Position.y);
  43. bin.Write(i.Position.z);
  44. count++;
  45. }
  46. }
  47. System.Diagnostics.Debug.WriteLine(count.ToString() + " Position added (Actor[])");
  48. writer.Close();
  49. writer.Dispose();
  50. writer = null;
  51. }
  52. public static void ExportLocation(Actor actor) {
  53. if (!Directory.Exists("locations"))
  54. Directory.CreateDirectory("locations");
  55. if (!IsValidActor(actor)) return;
  56. string file = getLocationFile(eLocationList.Actor);
  57. List<Position> poses = new List<Position>();
  58. if (File.Exists(file)) {
  59. StreamReader reader = new StreamReader(file);
  60. BinaryReader binreader = new BinaryReader(reader.BaseStream);
  61. while (binreader.BaseStream.Position < binreader.BaseStream.Length) {
  62. int area = binreader.ReadInt32();
  63. double x = binreader.ReadDouble();
  64. double y = binreader.ReadDouble();
  65. double z = binreader.ReadDouble();
  66. poses.Add(new Position(Math.Round(x / 2), Math.Round(y / 2), Math.Round(z / 2)));
  67. }
  68. reader.Close();
  69. reader.Dispose();
  70. reader = null;
  71. }
  72. Position tmpPos = new Position(Math.Round(actor.Position.x / 2), Math.Round(actor.Position.y / 2), Math.Round(actor.Position.z / 2));
  73. if (poses.Contains(tmpPos))
  74. return;
  75. StreamWriter writer = new StreamWriter(file, true);
  76. BinaryWriter bin = new BinaryWriter(writer.BaseStream);
  77. bin.Write(actor.GraphMask);
  78. bin.Write(actor.Position.x);
  79. bin.Write(actor.Position.y);
  80. bin.Write(actor.Position.z);
  81. System.Diagnostics.Debug.WriteLine("1 Position added (Actor[])");
  82. writer.Close();
  83. writer.Dispose();
  84. writer = null;
  85. }
  86. public static void ExportLocation(List<Structure> list) {
  87. if (!Directory.Exists("locations"))
  88. Directory.CreateDirectory("locations");
  89. string file = getLocationFile(eLocationList.StructureInstance);
  90. List<Position> poses = new List<Position>();
  91. if (File.Exists(file)) {
  92. StreamReader reader = new StreamReader(file);
  93. BinaryReader binreader = new BinaryReader(reader.BaseStream);
  94. while (binreader.BaseStream.Position < binreader.BaseStream.Length) {
  95. int area = binreader.ReadInt32();
  96. double x = binreader.ReadDouble();
  97. double y = binreader.ReadDouble();
  98. double z = binreader.ReadDouble();
  99. poses.Add(new Position(Math.Round(x / 2), Math.Round(y / 2), Math.Round(z / 2)));
  100. }
  101. reader.Close();
  102. }
  103. StreamWriter writer = new StreamWriter(file, true);
  104. BinaryWriter bin = new BinaryWriter(writer.BaseStream);
  105. int count = 0;
  106. foreach (Structure i in list) {
  107. Position tmpPos = new Position(Math.Round(i.Pos.x / 2), Math.Round(i.Pos.y / 2), Math.Round(i.Pos.z / 2));
  108. if (!poses.Contains(tmpPos)) {
  109. bin.Write((int)1);
  110. bin.Write(i.Pos.x);
  111. bin.Write(i.Pos.y);
  112. bin.Write(i.Pos.z);
  113. count++;
  114. }
  115. }
  116. System.Diagnostics.Debug.WriteLine(count.ToString() + " Position added (Structure[])");
  117. writer.Close();
  118. }
  119. public static void ExportLocation(Structure structure) {
  120. if (!Directory.Exists("locations"))
  121. Directory.CreateDirectory("locations");
  122. string file = getLocationFile(eLocationList.StructureInstance);
  123. List<Position> poses = new List<Position>();
  124. if (File.Exists(file)) {
  125. StreamReader reader = new StreamReader(file);
  126. BinaryReader binreader = new BinaryReader(reader.BaseStream);
  127. while (binreader.BaseStream.Position < binreader.BaseStream.Length) {
  128. int area = binreader.ReadInt32();
  129. double x = binreader.ReadDouble();
  130. double y = binreader.ReadDouble();
  131. double z = binreader.ReadDouble();
  132. poses.Add(new Position(Math.Round(x / 2), Math.Round(y / 2), Math.Round(z / 2)));
  133. }
  134. reader.Close();
  135. reader.Dispose();
  136. reader = null;
  137. }
  138. StreamWriter writer = new StreamWriter(file, true);
  139. BinaryWriter bin = new BinaryWriter(writer.BaseStream);
  140. Position tmpPos = new Position(Math.Round(structure.Pos.x / 2), Math.Round(structure.Pos.y / 2), Math.Round(structure.Pos.z / 2));
  141. if (!poses.Contains(tmpPos)) {
  142. bin.Write((int)1);
  143. bin.Write(structure.Pos.x);
  144. bin.Write(structure.Pos.y);
  145. bin.Write(structure.Pos.z);
  146. System.Diagnostics.Debug.WriteLine("1 Position added (Structure[])");
  147. }
  148. writer.Close();
  149. writer.Dispose();
  150. writer = null;
  151. }
  152. public static void ExportLocation(int area, Position Position, eLocationList location) {
  153. if (!Directory.Exists("locations"))
  154. Directory.CreateDirectory("locations");
  155. if (location.HasFlag(eLocationList.Actor))
  156. ExportLocationWrite(area, Position, eLocationList.Actor);
  157. if (location.HasFlag(eLocationList.Construction))
  158. ExportLocationWrite(area, Position, eLocationList.Construction);
  159. if (location.HasFlag(eLocationList.StructureInstance))
  160. ExportLocationWrite(area, Position, eLocationList.StructureInstance);
  161. }
  162. public static void ExportLocation(int area, double[] pos, eLocationList location) {
  163. ExportLocation(area, new Position(pos[0], pos[1], pos[2]), location);
  164. }
  165. public static void ExportLocation(int area, double x, double y, double z, eLocationList location) {
  166. ExportLocation(area, new Position(x, y, z), location);
  167. }
  168. private static void ExportLocationWrite(int area, Position Position, eLocationList location) {
  169. string file = getLocationFile(eLocationList.Actor);
  170. List<Position> poses = new List<Position>();
  171. if (File.Exists(file)) {
  172. StreamReader reader = new StreamReader(file);
  173. BinaryReader binreader = new BinaryReader(reader.BaseStream);
  174. while (binreader.BaseStream.Position < binreader.BaseStream.Length) {
  175. int area2 = binreader.ReadInt32();
  176. double x = binreader.ReadDouble();
  177. double y = binreader.ReadDouble();
  178. double z = binreader.ReadDouble();
  179. poses.Add(new Position(Math.Round(x / 2), Math.Round(y / 2), Math.Round(z / 2)));
  180. }
  181. reader.Close();
  182. reader.Dispose();
  183. reader = null;
  184. }
  185. Position tmpPos = new Position(Math.Round(Position.x / 2), Math.Round(Position.y / 2), Math.Round(Position.z / 2));
  186. if (!poses.Contains(tmpPos)) {
  187. StreamWriter writer = new StreamWriter(file, true);
  188. BinaryWriter bin = new BinaryWriter(writer.BaseStream);
  189. bin.Write(area);
  190. bin.Write(Position.x);
  191. bin.Write(Position.y);
  192. bin.Write(Position.z);
  193. writer.Close();
  194. writer.Dispose();
  195. writer = null;
  196. System.Diagnostics.Debug.WriteLine("1 Position added (Position)");
  197. }
  198. }
  199. #region GetLocationNear
  200. public static Position? GetLocationNear(Position pos, double MinDistance, double MaxDistance, eLocationList location) {
  201. System.Diagnostics.Debug.WriteLine("GetLocationNear(Position, location)");
  202. System.Diagnostics.Debug.WriteLine(location.ToString());
  203. if (!Directory.Exists("locations")) {
  204. System.Diagnostics.Debug.WriteLine("Directory missing");
  205. return null;
  206. }
  207. Position? closes = null;
  208. double distanceCloses = double.MaxValue;
  209. if (location.HasFlag(eLocationList.Actor)) {
  210. System.Diagnostics.Debug.WriteLine("Get by Actor");
  211. getLocationNearJob(pos, 0, MinDistance, MaxDistance, eLocationList.Actor, ref distanceCloses, ref closes);
  212. }
  213. if (location.HasFlag(eLocationList.StructureInstance)) {
  214. System.Diagnostics.Debug.WriteLine("Get by StructureInstance");
  215. getLocationNearJob(pos, 0, MinDistance, MaxDistance, eLocationList.StructureInstance, ref distanceCloses, ref closes);
  216. }
  217. if (location.HasFlag(eLocationList.Construction)) {
  218. System.Diagnostics.Debug.WriteLine("Get by Construction");
  219. getLocationNearJob(pos, 0, MinDistance, MaxDistance, eLocationList.Construction, ref distanceCloses, ref closes);
  220. }
  221. return closes;
  222. }
  223. public static Position? GetLocationNear(Position pos, double MinDistance, eLocationList location) {
  224. return GetLocationNear(pos, 0, MinDistance, double.MaxValue, location);
  225. }
  226. public static Position? GetLocationNear(Position pos, eLocationList location) {
  227. return GetLocationNear(pos, 0, 0.0f, double.MaxValue, location);
  228. }
  229. public static Position? GetLocationNear(Position pos) {
  230. return GetLocationNear(pos, 0, 0.0f, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  231. }
  232. public static Position? GetLocationNear(double[] pos, eLocationList location) {
  233. if (pos.Length < 3) {
  234. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  235. return null;
  236. }
  237. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), 0, 0.0f, double.MaxValue, location);
  238. }
  239. public static Position? GetLocationNear(double[] pos) {
  240. if (pos.Length < 3) {
  241. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  242. return null;
  243. }
  244. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), 0, 0.0f, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  245. }
  246. public static Position? GetLocationNear(Position pos, double MinOffset) {
  247. return GetLocationNear(pos, 0, MinOffset, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  248. }
  249. public static Position? GetLocationNear(double[] pos, double MinOffset, eLocationList location) {
  250. if (pos.Length < 3) {
  251. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  252. return null;
  253. }
  254. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), 0, MinOffset, double.MaxValue, location);
  255. }
  256. public static Position? GetLocationNear(double[] pos, double MinOffset) {
  257. if (pos.Length < 3) {
  258. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  259. return null;
  260. }
  261. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), 0, MinOffset, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  262. }
  263. public static Position? GetLocationNear(Position pos, double MinOffset, double MaxOffset) {
  264. return GetLocationNear(pos, 0, MinOffset, MaxOffset, eLocationList.Actor | eLocationList.StructureInstance);
  265. }
  266. public static Position? GetLocationNear(double[] pos, double MinOffset, double MaxOffset, eLocationList location) {
  267. if (pos.Length < 3) {
  268. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  269. return null;
  270. }
  271. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), 0, MinOffset, MaxOffset, location);
  272. }
  273. public static Position? GetLocationNear(double[] pos, double MinOffset, double MaxOffset) {
  274. if (pos.Length < 3) {
  275. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  276. return null;
  277. }
  278. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), 0, MinOffset, MaxOffset, eLocationList.Actor | eLocationList.StructureInstance);
  279. }
  280. /////////////////////////////////////////////////////////////////////////////
  281. public static Position? GetLocationNear(Position pos, int area, double MinDistance, double MaxDistance, eLocationList location) {
  282. System.Diagnostics.Debug.WriteLine("GetLocationNear(Position, location)");
  283. System.Diagnostics.Debug.WriteLine(location.ToString());
  284. if (!Directory.Exists("locations")) {
  285. System.Diagnostics.Debug.WriteLine("Directory missing");
  286. return null;
  287. }
  288. Position? closes = null;
  289. double distanceCloses = double.MaxValue;
  290. if (location.HasFlag(eLocationList.Actor)) {
  291. System.Diagnostics.Debug.WriteLine("Get by Actor");
  292. getLocationNearJob(pos, area, MinDistance, MaxDistance, eLocationList.Actor, ref distanceCloses, ref closes);
  293. }
  294. if (location.HasFlag(eLocationList.StructureInstance)) {
  295. System.Diagnostics.Debug.WriteLine("Get by StructureInstance");
  296. getLocationNearJob(pos, area, MinDistance, MaxDistance, eLocationList.StructureInstance, ref distanceCloses, ref closes);
  297. }
  298. if (location.HasFlag(eLocationList.Construction)) {
  299. System.Diagnostics.Debug.WriteLine("Get by Construction");
  300. getLocationNearJob(pos, area, MinDistance, MaxDistance, eLocationList.Construction, ref distanceCloses, ref closes);
  301. }
  302. return closes;
  303. }
  304. public static Position? GetLocationNear(Position pos, int area, double MinDistance, eLocationList location) {
  305. return GetLocationNear(pos, area, MinDistance, double.MaxValue, location);
  306. }
  307. public static Position? GetLocationNear(Position pos, int area, eLocationList location) {
  308. return GetLocationNear(pos, area, 0.0f, double.MaxValue, location);
  309. }
  310. public static Position? GetLocationNear(Position pos, int area) {
  311. return GetLocationNear(pos, area, 0.0f, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  312. }
  313. public static Position? GetLocationNear(double[] pos, int area, eLocationList location) {
  314. if (pos.Length < 3) {
  315. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  316. return null;
  317. }
  318. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), area, 0.0f, double.MaxValue, location);
  319. }
  320. public static Position? GetLocationNear(double[] pos, int area) {
  321. if (pos.Length < 3) {
  322. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  323. return null;
  324. }
  325. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), area, 0.0f, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  326. }
  327. public static Position? GetLocationNear(Position pos, int area, double MinOffset) {
  328. return GetLocationNear(pos, area, MinOffset, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  329. }
  330. public static Position? GetLocationNear(double[] pos, int area, double MinOffset, eLocationList location) {
  331. if (pos.Length < 3) {
  332. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  333. return null;
  334. }
  335. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), area, MinOffset, double.MaxValue, location);
  336. }
  337. public static Position? GetLocationNear(double[] pos, int area, double MinOffset) {
  338. if (pos.Length < 3) {
  339. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  340. return null;
  341. }
  342. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), area, MinOffset, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  343. }
  344. public static Position? GetLocationNear(Position pos, int area, double MinOffset, double MaxOffset) {
  345. return GetLocationNear(pos, area, MinOffset, MaxOffset, eLocationList.Actor | eLocationList.StructureInstance);
  346. }
  347. public static Position? GetLocationNear(double[] pos, int area, double MinOffset, double MaxOffset, eLocationList location) {
  348. if (pos.Length < 3) {
  349. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  350. return null;
  351. }
  352. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), area, MinOffset, MaxOffset, location);
  353. }
  354. public static Position? GetLocationNear(double[] pos, int area, double MinOffset, double MaxOffset) {
  355. if (pos.Length < 3) {
  356. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  357. return null;
  358. }
  359. return GetLocationNear(new Position(pos[0], pos[1], pos[2]), area, MinOffset, MaxOffset, eLocationList.Actor | eLocationList.StructureInstance);
  360. }
  361. #endregion
  362. #region GetLocationArrayNear
  363. public static Position[]? GetLocationArrayNear(int Count, Position pos, double MinDistance, double MaxDistance, eLocationList location) {
  364. System.Diagnostics.Debug.WriteLine("GetLocationNear(Position, location)");
  365. System.Diagnostics.Debug.WriteLine(location.ToString());
  366. if (!Directory.Exists("locations")) {
  367. System.Diagnostics.Debug.WriteLine("Directory missing");
  368. return null;
  369. }
  370. Position[]? closes = new Position[Count];
  371. double[] distanceCloses = new double[Count];
  372. for (int i = 0; i < Count; i++)
  373. distanceCloses[i] = double.MaxValue;
  374. if (location.HasFlag(eLocationList.Actor)) {
  375. System.Diagnostics.Debug.WriteLine("Get by Actor");
  376. getLocationNearJob(pos, 0, MinDistance, MaxDistance, eLocationList.Actor, ref distanceCloses, ref closes);
  377. }
  378. if (location.HasFlag(eLocationList.StructureInstance)) {
  379. System.Diagnostics.Debug.WriteLine("Get by StructureInstance");
  380. getLocationNearJob(pos, 0, MinDistance, MaxDistance, eLocationList.StructureInstance, ref distanceCloses, ref closes);
  381. }
  382. if (location.HasFlag(eLocationList.Construction)) {
  383. System.Diagnostics.Debug.WriteLine("Get by Construction");
  384. getLocationNearJob(pos, 0, MinDistance, MaxDistance, eLocationList.Construction, ref distanceCloses, ref closes);
  385. }
  386. int itmCount = 0;
  387. for(int i = 0; i<Count;i++) {
  388. if (closes[i] != null)
  389. itmCount++;
  390. else break;
  391. }
  392. if (itmCount == 0) return new Position[0];
  393. Position[] res = new Position[itmCount];
  394. for (int i = 0; i < itmCount; i++)
  395. res[i] = closes[i];
  396. return res;
  397. }
  398. public static Position[]? GetLocationArrayNear(int Count, Position pos, double MinDistance, eLocationList location) {
  399. return GetLocationArrayNear(Count, pos, MinDistance, double.MaxValue, location);
  400. }
  401. public static Position[]? GetLocationArrayNear(int Count, Position pos, eLocationList location) {
  402. return GetLocationArrayNear(Count, pos, 0.0f, double.MaxValue, location);
  403. }
  404. public static Position[]? GetLocationArrayNear(int Count, Position pos) {
  405. System.Diagnostics.Debug.WriteLine("GetLocationNear(Position)");
  406. return GetLocationArrayNear(Count, pos, 0.0f, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  407. }
  408. public static Position[]? GetLocationArrayNear(int Count, double[] pos, eLocationList location) {
  409. if (pos.Length < 3) {
  410. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  411. return null;
  412. }
  413. return GetLocationArrayNear(Count, new Position(pos[0], pos[1], pos[2]), 0.0f, double.MaxValue, location);
  414. }
  415. public static Position[]? GetLocationArrayNear(int Count, double[] pos) {
  416. if (pos.Length < 3) {
  417. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  418. return null;
  419. }
  420. return GetLocationArrayNear(Count, new Position(pos[0], pos[1], pos[2]), 0.0f, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  421. }
  422. public static Position[]? GetLocationArrayNear(int Count, Position pos, double MinOffset) {
  423. System.Diagnostics.Debug.WriteLine("GetLocationNear(Position)");
  424. return GetLocationArrayNear(Count, pos, MinOffset, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  425. }
  426. public static Position[]? GetLocationArrayNear(int Count, double[] pos, double MinOffset, eLocationList location) {
  427. if (pos.Length < 3) {
  428. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  429. return null;
  430. }
  431. return GetLocationArrayNear(Count, new Position(pos[0], pos[1], pos[2]), MinOffset, double.MaxValue, location);
  432. }
  433. public static Position[]? GetLocationArrayNear(int Count, double[] pos, double MinOffset) {
  434. if (pos.Length < 3) {
  435. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  436. return null;
  437. }
  438. return GetLocationArrayNear(Count, new Position(pos[0], pos[1], pos[2]), MinOffset, double.MaxValue, eLocationList.Actor | eLocationList.StructureInstance);
  439. }
  440. public static Position[]? GetLocationArrayNear(int Count, Position pos, double MinOffset, double MaxOffset) {
  441. System.Diagnostics.Debug.WriteLine("GetLocationNear(Position)");
  442. return GetLocationArrayNear(Count, pos, MinOffset, MaxOffset, eLocationList.Actor | eLocationList.StructureInstance);
  443. }
  444. public static Position[]? GetLocationArrayNear(int Count, double[] pos, double MinOffset, double MaxOffset, eLocationList location) {
  445. if (pos.Length < 3) {
  446. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  447. return null;
  448. }
  449. return GetLocationArrayNear(Count, new Position(pos[0], pos[1], pos[2]), MinOffset, MaxOffset, location);
  450. }
  451. public static Position[]? GetLocationArrayNear(int Count, double[] pos, double MinOffset, double MaxOffset) {
  452. if (pos.Length < 3) {
  453. System.Diagnostics.Debug.WriteLine(" Array to small: " + pos.Length);
  454. return null;
  455. }
  456. return GetLocationArrayNear(Count, new Position(pos[0], pos[1], pos[2]), MinOffset, MaxOffset, eLocationList.Actor | eLocationList.StructureInstance);
  457. }
  458. #endregion
  459. private static void getLocationNearJob(Position pos, int area, double MinOffset, double MaxOffset, eLocationList location, ref double distanceCloses, ref Position? closes) {
  460. string file = getLocationFile(location);
  461. if (File.Exists(file)) {
  462. System.Diagnostics.Debug.WriteLine("Check Near by");
  463. System.Diagnostics.Debug.WriteLine(" File: " + file);
  464. System.Diagnostics.Debug.WriteLine(" Area: " + area.ToString());
  465. System.Diagnostics.Debug.WriteLine(" Coords: [" + pos.x.ToString() + " x " + pos.y.ToString() + " x " + pos.z.ToString() + "]");
  466. StreamReader reader = new StreamReader(file);
  467. BinaryReader bin = new BinaryReader(reader.BaseStream);
  468. while (bin.BaseStream.Position < bin.BaseStream.Length) {
  469. int a = bin.ReadInt32();
  470. double x = bin.ReadDouble();
  471. double y = bin.ReadDouble();
  472. double z = bin.ReadDouble();
  473. double dist = pos.DistanceTo(x, y, z);
  474. if (dist > MinOffset && dist < MaxOffset && (a == 0 || (a | area)==area)) {
  475. //System.Diagnostics.Debug.WriteLine(" [" + x.ToString() + " x " + y.ToString() + " x " + z.ToString() + "] = " + dist.ToString());
  476. if (dist < distanceCloses) {
  477. System.Diagnostics.Debug.WriteLine(" New nearest found");
  478. closes = new Position() {
  479. x = x,
  480. y = y,
  481. z = z
  482. };
  483. distanceCloses = dist;
  484. }
  485. }
  486. }
  487. reader.Close();
  488. reader.Dispose();
  489. reader = null;
  490. }
  491. }
  492. private static void getLocationNearJob(Position pos, int area, double MinOffset, double MaxOffset, eLocationList location, ref double[] distanceCloses, ref Position[]? closes) {
  493. string file = getLocationFile(location);
  494. if (File.Exists(file)) {
  495. System.Diagnostics.Debug.WriteLine("Check Near by");
  496. System.Diagnostics.Debug.WriteLine(" File: " + file);
  497. System.Diagnostics.Debug.WriteLine(" Area: " + area.ToString());
  498. System.Diagnostics.Debug.WriteLine(" Coords: [" + pos.x.ToString() + " x " + pos.y.ToString() + " x " + pos.z.ToString() + "]");
  499. StreamReader reader = new StreamReader(file);
  500. BinaryReader bin = new BinaryReader(reader.BaseStream);
  501. while (bin.BaseStream.Position < bin.BaseStream.Length) {
  502. int a = bin.ReadInt32();
  503. double x = bin.ReadDouble();
  504. double y = bin.ReadDouble();
  505. double z = bin.ReadDouble();
  506. double dist = pos.DistanceTo(x, y, z);
  507. if (dist > MinOffset && dist < MaxOffset && (a==0 || (a |area) == area)) {
  508. for(int i=0,c=distanceCloses.Length;i<c;i++) {
  509. if (dist < distanceCloses[i]) {
  510. for(int j=c-1;j>i;j--) {
  511. closes[j]=closes[j-1];
  512. distanceCloses[j] = distanceCloses[j-1];
  513. }
  514. closes[i] = new Position() {
  515. x = x,
  516. y = y,
  517. z = z
  518. };
  519. distanceCloses[i] = dist;
  520. break;
  521. }
  522. }
  523. }
  524. }
  525. reader.Close();
  526. reader.Dispose();
  527. reader = null;
  528. }
  529. }
  530. public static int GetPositionCount(eLocationList location) {
  531. string file = getLocationFile(location);
  532. if (!File.Exists(file))
  533. return 0;
  534. long size = (new FileInfo(file)).Length / 28;
  535. return (int) size;
  536. }
  537. public static Position? GetRandomPosition(int area, eLocationList location) {
  538. string file = getLocationFile(location);
  539. if (!File.Exists(file))
  540. return null;
  541. int count = GetPositionCount(location);
  542. StreamReader reader = new StreamReader(file);
  543. BinaryReader bin = new BinaryReader(reader.BaseStream);
  544. int a;
  545. double x;
  546. double y;
  547. double z;
  548. int c = 5;
  549. bool bFound = false;
  550. if (area == 1) {
  551. do {
  552. int rnd = Random.Shared.Next(0, count - 1);
  553. bin.BaseStream.Seek(rnd * 28, SeekOrigin.Begin);
  554. a = bin.ReadInt32();
  555. x = bin.ReadDouble();
  556. y = bin.ReadDouble();
  557. z = bin.ReadDouble();
  558. if(a == area) {
  559. reader.Close();
  560. reader.Dispose();
  561. reader = null;
  562. return new Position(x, y, z);
  563. }
  564. c--;
  565. } while (c >= 0);
  566. }
  567. List<Position> pos = new List<Position>();
  568. bin.BaseStream.Seek(0, SeekOrigin.Begin);
  569. do {
  570. a = bin.ReadInt32();
  571. x = bin.ReadDouble();
  572. y = bin.ReadDouble();
  573. z = bin.ReadDouble();
  574. if (a == area)
  575. pos.Add(new Position(x, y, z));
  576. } while (bin.BaseStream.Position <= bin.BaseStream.Length - 28);
  577. if (pos.Count > 0) {
  578. int rnd = Random.Shared.Next(0, pos.Count - 1);
  579. reader.Close();
  580. reader.Dispose();
  581. reader = null;
  582. return pos[rnd];
  583. }
  584. reader.Close();
  585. reader.Dispose();
  586. reader = null;
  587. return null;
  588. }
  589. public static Position? GetPositionAt(int area, eLocationList location, int index) {
  590. string file = getLocationFile(location);
  591. if (!File.Exists(file))
  592. return null;
  593. int count = GetPositionCount(location);
  594. if (index >= count)
  595. return null;
  596. StreamReader reader = new StreamReader(file);
  597. BinaryReader bin = new BinaryReader(reader.BaseStream);
  598. bin.BaseStream.Seek(index * 28, SeekOrigin.Begin);
  599. int a = bin.ReadInt32();
  600. double x = bin.ReadDouble();
  601. double y = bin.ReadDouble();
  602. double z = bin.ReadDouble();
  603. reader.Close();
  604. reader.Dispose();
  605. reader = null;
  606. return new Position(x, y, z);
  607. }
  608. private static bool IsValidActor(Actor a) {
  609. if (a.SpawnerId == 0 && a.ActorSeed == 0) return false;
  610. if (a == null) return false;
  611. if (a.TypeId < 3) return true;
  612. if (a.TypeId < 7) return false;
  613. if (a.TypeId == 34) return false;
  614. if (a.TypeId == 36) return false;
  615. if (a.TypeId == 37) return false;
  616. return true;
  617. }
  618. public static string getLocationFile(eLocationList loc) {
  619. if (loc == eLocationList.Actor)
  620. return "locations\\actor.pos";
  621. else if (loc == eLocationList.Construction)
  622. return "locations\\construction.pos";
  623. else if (loc == eLocationList.StructureInstance)
  624. return "locations\\structure.pos";
  625. return "temp.pos";
  626. }
  627. }
  628. }