Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.53 KB | None | 0 0
  1. using Ennui.Api;
  2. using Ennui.Api.Direct.Object;
  3. using Ennui.Api.Meta;
  4. using Ennui.Api.Method;
  5. using Ennui.Api.Script;
  6. using System;
  7. using System.Collections.Generic;
  8.  
  9. namespace Ennui.Script.Official
  10. {
  11. public class GatherState : StateScript
  12. {
  13. private Configuration config;
  14. private Context context;
  15.  
  16. private IHarvestableObject harvestableTarget;
  17. private IMobObject mobTarget;
  18. private int gatherAttempts = 0;
  19. private List<long> blacklist = new List<long>();
  20. private Random rand = new Random();
  21.  
  22. public GatherState(Configuration config, Context context)
  23. {
  24. this.config = config;
  25. this.context = context;
  26. }
  27.  
  28. private void Reset()
  29. {
  30. harvestableTarget = null;
  31. mobTarget = null;
  32. gatherAttempts = 0;
  33. }
  34.  
  35. private bool NeedsNew()
  36. {
  37. if (harvestableTarget == null && mobTarget == null)
  38. {
  39. return true;
  40. }
  41.  
  42. if (mobTarget != null && (!mobTarget.IsValid || mobTarget.CurrentHealth <= 0))
  43. {
  44. return true;
  45. }
  46.  
  47. if (harvestableTarget != null && (!harvestableTarget.IsValid || harvestableTarget.Depleted))
  48. {
  49. return true;
  50. }
  51.  
  52. if (gatherAttempts >= config.GatherAttemptsTimeout)
  53. {
  54. if (harvestableTarget != null)
  55. {
  56. blacklist.Add(harvestableTarget.Id);
  57. }
  58. if (mobTarget != null)
  59. {
  60. blacklist.Add(mobTarget.Id);
  61. }
  62. return true;
  63. }
  64.  
  65. return false;
  66. }
  67.  
  68. private IMobObject Closest(Vector3<float> center, List<IMobObject> mobs)
  69. {
  70. var dist = 0.0f;
  71. IMobObject closest = null;
  72. foreach (var m in mobs)
  73. {
  74. var cdist = m.ThreadSafeLocation.SimpleDistance(center);
  75. if (closest == null || cdist < dist)
  76. {
  77. dist = cdist;
  78. closest = m;
  79. }
  80. }
  81.  
  82. return closest;
  83. }
  84.  
  85. private bool FindResource(Vector3<float> center)
  86. {
  87. context.State = "Finding resource...";
  88.  
  89. Reset();
  90. var territoryAreas = new List<Area>();
  91. var graph = Graphs.LookupByDisplayName(Game.ClusterName);
  92. foreach (var t in graph.Territories)
  93. {
  94. var tCenter = t.Center;
  95. var tSize = t.Size;
  96. var tCenter3d = new Vector3f(tCenter.X, 0, tCenter.Y);
  97. var tBegin = tCenter3d.Translate(0 - (tSize.X / 2), -100, 0 - (tSize.Y / 2));
  98. var tEnd = tCenter3d.Translate(tSize.X / 2, 100, tSize.Y / 2);
  99. var tArea = new Area(tBegin, tEnd);
  100. territoryAreas.Add(tArea);
  101. }
  102.  
  103. if (config.AttackMobs)
  104. {
  105. var lpo = Players.LocalPlayer;
  106. if (lpo != null && config.IgnoreMobsOnLowHealth && lpo.HealthPercentage > config.IgnoreMobHealthPercent)
  107. {
  108. var resourceMobs = new List<IMobObject>();
  109. foreach (var ent in Entities.MobChain.ExcludeByArea(territoryAreas.ToArray()).ExcludeWithIds(blacklist.ToArray()).AsList)
  110. {
  111. var drops = ent.HarvestableDropChain.FilterByTypeSet(SafeTypeSet.BatchConvert(config.TypeSetsToUse)).AsList;
  112. Logging.Log(drops.ToString());
  113. if (drops.Count > 0)
  114. {
  115.  
  116. resourceMobs.Add(ent);
  117. }
  118. }
  119.  
  120. if (resourceMobs.Count > 0)
  121. {
  122. mobTarget = Closest(center, resourceMobs);
  123. }
  124. }
  125. }
  126.  
  127. harvestableTarget = Objects
  128. .HarvestableChain
  129. .FilterDepleted()
  130. .ExcludeWithIds(blacklist.ToArray())
  131. .ExcludeByArea(territoryAreas.ToArray())
  132. .FilterByTypeSet(SafeTypeSet.BatchConvert(config.TypeSetsToUse))
  133. .FilterWithSetupState(HarvestableSetupState.Invalid)
  134. .Closest(center);
  135.  
  136. if (mobTarget != null && harvestableTarget != null)
  137. {
  138. var mobDist = mobTarget.ThreadSafeLocation.SimpleDistance(center);
  139. var resDist = harvestableTarget.ThreadSafeLocation.SimpleDistance(center);
  140. if (mobDist < resDist)
  141. {
  142. harvestableTarget = null;
  143. }
  144. else
  145. {
  146. mobTarget = null;
  147. }
  148. }
  149.  
  150. return harvestableTarget != null || mobTarget != null;
  151. }
  152.  
  153. private Vector3<float> RandomRoamPoint()
  154. {
  155. if (config.RoamPoints.Count == 0)
  156. {
  157. return null;
  158. }
  159. if (config.RoamPoints.Count == 1)
  160. {
  161. return config.RoamPoints[0].RealVector3();
  162. }
  163. return config.RoamPoints[rand.Next(config.RoamPoints.Count)].RealVector3();
  164. }
  165.  
  166. private Boolean ShouldUseMount(float heldWeight, float dist)
  167. {
  168. var localPlayer = Players.LocalPlayer;
  169. var useMount = false;
  170.  
  171. if (dist >= 2)
  172. {
  173. useMount = true;
  174. }
  175.  
  176. else if (localPlayer.WeighedDownPercent > 98)
  177. {
  178. useMount = true;
  179. }
  180.  
  181. return useMount;
  182. }
  183.  
  184. public override int OnLoop(IScriptEngine se)
  185. {
  186. if (config.RepairDest != null && Api.HasBrokenItems())
  187. {
  188. parent.EnterState("repair");
  189. return 0;
  190. }
  191.  
  192. var localPlayer = Players.LocalPlayer;
  193. if (localPlayer == null)
  194. {
  195. context.State = "Failed to find local player!";
  196. Logging.Log("Failed to find local player!", LogLevel.Error);
  197. return 10_000;
  198. }
  199.  
  200. if (localPlayer.IsUnderAttack)
  201. {
  202. Logging.Log("Local player under attack, fight back!", LogLevel.Atom);
  203. parent.EnterState("combat");
  204. return 0;
  205. }
  206.  
  207. var localLocation = localPlayer.ThreadSafeLocation;
  208. if (!config.GatherArea.RealArea(Api).Contains(localLocation))
  209. {
  210. context.State = "Walking to gather area...";
  211.  
  212. Logging.Log("Local player not in gather area, walk there!", LogLevel.Atom);
  213.  
  214. var moveConfig = new PointPathFindConfig();
  215. moveConfig.UseWeb = false;
  216. moveConfig.ClusterName = config.ResourceClusterName;
  217. if (Movement.PathFindTo(moveConfig) != PathFindResult.Success)
  218. {
  219. Logging.Log("Local player failed to find path to resource area!", LogLevel.Error);
  220. return 10_000;
  221. }
  222.  
  223. return 0;
  224. }
  225.  
  226. var heldWeight = localPlayer.WeighedDownPercent;
  227. if (localPlayer.WeighedDownPercent > 98)
  228. {
  229. localPlayer.ToggleMount(true);
  230. Time.SleepUntil(() =>
  231. {
  232. return localPlayer.IsMounted;
  233. }, 3000);
  234. }
  235. if (localPlayer.WeighedDownPercent > 98 && localPlayer.IsMounted)
  236. {
  237. parent.EnterState("bank");
  238. return 0;
  239. }
  240.  
  241. if (NeedsNew())
  242. {
  243. if (!FindResource(localLocation))
  244. {
  245. var point = RandomRoamPoint();
  246. if (point == null)
  247. {
  248. context.State = "Failed to find roam point!";
  249. Logging.Log("Cannot roam as roam points were not added!");
  250. return 15000;
  251. }
  252.  
  253. context.State = "Roaming";
  254. var moveConfig = new PointPathFindConfig();
  255. moveConfig.UseWeb = false;
  256. moveConfig.ClusterName = config.ResourceClusterName;
  257. moveConfig.Point = point;
  258. moveConfig.UseMount = true;
  259. moveConfig.ExitHook = (() =>
  260. {
  261. var local = Players.LocalPlayer;
  262. if (local != null)
  263. {
  264. return FindResource(local.ThreadSafeLocation);
  265. }
  266. return false;
  267. });
  268.  
  269. if (Movement.PathFindTo(moveConfig) != PathFindResult.Success)
  270. {
  271. context.State = "Failed to find path to roaming point...";
  272. return 10_000;
  273. }
  274. return 5000;
  275. }
  276. }
  277.  
  278. if (harvestableTarget != null)
  279. {
  280. var area = harvestableTarget.InteractBounds;
  281. if (area.Contains(localLocation))
  282. {
  283.  
  284.  
  285. context.State = "Attempting to gather " + harvestableTarget.Id + " (" + gatherAttempts + ")";
  286. harvestableTarget.Click();
  287.  
  288. Time.SleepUntil(() =>
  289. {
  290. return localPlayer.IsHarvesting;
  291. }, 1000);
  292.  
  293. if (!localPlayer.IsHarvesting)
  294. {
  295. gatherAttempts = gatherAttempts + 1;
  296.  
  297. }
  298.  
  299. return 10;
  300. }
  301. else
  302. {
  303. context.State = "Walking to resource...";
  304.  
  305. var dist = localLocation.SimpleDistance(harvestableTarget.ThreadSafeLocation);
  306. var config = new ResourcePathFindConfig();
  307. config.ClusterName = this.config.ResourceClusterName;
  308. config.UseWeb = false;
  309. config.Target = harvestableTarget;
  310. config.UseMount = ShouldUseMount(heldWeight, dist);
  311.  
  312. var result = Movement.PathFindTo(config);
  313. if (result == PathFindResult.Failed)
  314. {
  315. context.State = "Failed to path find to resource!";
  316. blacklist.Add(harvestableTarget.Id);
  317. Reset();
  318. }
  319. return 0;
  320. }
  321. }
  322. else if (mobTarget != null)
  323. {
  324. var mobGatherArea = mobTarget.ThreadSafeLocation.Expand(3, 3, 3);
  325. if (mobGatherArea.Contains(localLocation))
  326. {
  327. context.State = "Attempting to kill mob";
  328.  
  329. if (localPlayer.IsMounted)
  330. {
  331. localPlayer.ToggleMount(false);
  332. }
  333.  
  334. localPlayer.SetSelectedObject(mobTarget);
  335. localPlayer.AttackSelectedObject();
  336.  
  337. Time.SleepUntil(() =>
  338. {
  339. return localPlayer.IsUnderAttack;
  340. }, 3000);
  341.  
  342. if (localPlayer.IsUnderAttack)
  343. {
  344. parent.EnterState("combat");
  345. }
  346. else
  347. {
  348. gatherAttempts = gatherAttempts + 1;
  349. }
  350.  
  351. return 100;
  352. }
  353. else
  354. {
  355. context.State = "Walking to mob...";
  356.  
  357. var dist = localLocation.SimpleDistance(mobTarget.ThreadSafeLocation);
  358. var config = new PointPathFindConfig();
  359. config.ClusterName = this.config.ResourceClusterName;
  360. config.UseWeb = false;
  361. config.Point = mobTarget.ThreadSafeLocation;
  362. config.UseMount = ShouldUseMount(heldWeight, dist);
  363.  
  364. if (Movement.PathFindTo(config) == PathFindResult.Failed)
  365. {
  366. context.State = "Failed to path find to mob!";
  367. blacklist.Add(mobTarget.Id);
  368. Reset();
  369. }
  370. return 0;
  371. }
  372. }
  373. return 100;
  374. }
  375. }
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement