Guest User

Untitled

a guest
Nov 17th, 2013
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Windows.Forms;
  6. using ff14bot;
  7. using ff14bot.Behavior;
  8. using ff14bot.Helpers;
  9. using ff14bot.Managers;
  10. using ff14bot.Objects;
  11. using TreeSharp;
  12. using ff14bot.AClasses;
  13.  
  14.  
  15. using Action = TreeSharp.Action;
  16.  
  17. namespace Kupo
  18. {
  19. public abstract class KupoRoutine : CombatRoutine
  20. {
  21. public override sealed string Name { get { return "Kupo [" + GetType().Name + "]"; } }
  22.  
  23. #region CombatRoutine implementation
  24.  
  25. private Composite _heal, _combat, _preCombatbuffs,_combatbuffs, _pull;
  26.  
  27. public override void Pulse()
  28. {
  29. Extensions.DoubleCastPreventionDict.RemoveAll(t => DateTime.UtcNow > t);
  30. }
  31.  
  32. public sealed override Composite HealBehavior
  33. {
  34. get { return _heal ?? (_heal = CreateHeal()); }
  35. }
  36. public sealed override Composite CombatBehavior
  37. {
  38. get
  39. {
  40. return _combat ?? (_combat = CreateCombat());
  41. }
  42. }
  43. public sealed override Composite PreCombatBuffBehavior
  44. {
  45. get { return _preCombatbuffs ?? (_preCombatbuffs = CreatePreCombatBuffs()); }
  46. }
  47.  
  48. public sealed override Composite CombatBuffBehavior
  49. {
  50. get
  51. {
  52. return _combatbuffs ?? (_combatbuffs = new PrioritySelector(new Action(ret => { CacheAuras(); return RunStatus.Failure; }), CreateCombatBuffs()));
  53. }
  54. }
  55.  
  56. public sealed override Composite PullBehavior
  57. {
  58. get { return _pull ?? (_pull = CreatePull()); }
  59. }
  60.  
  61. #region Aura Caching for High Performance
  62.  
  63. /* Note: This only caches auras on you, and your target. Nothing else! */
  64. public static IEnumerable<Aura> LocalPlayerAuras, TargetAuras;
  65.  
  66. void CacheAuras()
  67. {
  68. LocalPlayerAuras = Core.Player.CharacterAuras;
  69.  
  70. if (Core.Player.HasTarget)
  71. {
  72. TargetAuras = Core.Player.CurrentTargetCharacter.CharacterAuras;
  73. }
  74.  
  75. }
  76.  
  77. #endregion
  78.  
  79. #region Hidden Overrides
  80.  
  81. public sealed override void Combat(){base.Combat();}
  82. public sealed override void CombatBuff(){base.CombatBuff();}
  83. public sealed override void Death(){base.Death();}
  84. public sealed override void Heal(){base.Heal();}
  85. public sealed override Composite MoveToTargetBehavior{get{return base.MoveToTargetBehavior;}}
  86. public sealed override bool NeedCombatBuffs{get{return base.NeedCombatBuffs;}}
  87. public sealed override bool NeedDeath{get{return base.NeedDeath;}}
  88. public sealed override bool NeedHeal{get{return base.NeedHeal;}}
  89. public sealed override bool NeedPreCombatBuffs{get{return base.NeedPreCombatBuffs;}}
  90. public sealed override bool NeedPullBuffs{get{return base.NeedPullBuffs;}}
  91. public sealed override bool NeedRest{get{return base.NeedRest;}}
  92. public sealed override void PreCombatBuff(){base.PreCombatBuff();}
  93. public sealed override void Pull(){base.Pull();}
  94. public sealed override void Rest(){base.Rest();}
  95.  
  96. public override void Initialize()
  97. {
  98. RegisterHotkeys();
  99. //CombatLogHandler.Initialize();
  100. }
  101.  
  102. public override void ShutDown()
  103. {
  104. UnregisterHotkeys();
  105. //CombatLogHandler.Shutdown();
  106. }
  107.  
  108. #endregion
  109.  
  110. #endregion
  111.  
  112. #region Required Implementations
  113.  
  114. protected virtual Composite CreateCombat()
  115. {
  116. return new HookExecutor("Kupo_Combat_Root",
  117. "Root composite for Kupo combat. Rotations will be plugged into this hook.",
  118. new Action(r=> RunStatus.Failure));
  119. }
  120. protected virtual Composite CreatePreCombatBuffs()
  121. {
  122. return new HookExecutor("Kupo_PreCombatBuffs_Root",
  123. "Root composite for Kupo buffs. Rotations will be plugged into this hook.",
  124. new Action(r => RunStatus.Failure));
  125. }
  126.  
  127. protected virtual Composite CreateCombatBuffs()
  128. {
  129. return new HookExecutor("Kupo_CombatBuffs_Root",
  130. "Root composite for Kupo buffs. Rotations will be plugged into this hook.",
  131. new Action(r => RunStatus.Failure));
  132. }
  133.  
  134. protected virtual Composite CreateHeal()
  135. {
  136. return new HookExecutor("Kupo_Heals_Root",
  137. "Root composite for Kupo heals. Rotations will be plugged into this hook.",
  138. new Action(r => RunStatus.Failure));
  139. }
  140.  
  141. protected virtual Composite CreatePull()
  142. {
  143. return new HookExecutor("Kupo_Pull_Root",
  144. "Root composite for Kupo pull. Rotations will be plugged into this hook.",
  145. new Action(r => RunStatus.Failure));
  146. }
  147.  
  148. protected bool InterruptsEnabled { get; set; }
  149. protected virtual void UnregisterHotkeys()
  150. {
  151. //HotkeysManager.Unregister("Kupo Toggle Interrupt");
  152. }
  153. protected virtual void RegisterHotkeys()
  154. {
  155. /*HotkeysManager.Register("Kupo Toggle Interrupt",
  156. Keys.NumPad1,
  157. ModifierKeys.Alt,
  158. o =>
  159. {
  160. InterruptsEnabled = !InterruptsEnabled;
  161. Logging.Write("Interrupts enabled: " + InterruptsEnabled);
  162. });
  163.  
  164. // Default this to true please. Thanks!
  165. InterruptsEnabled = true;*/
  166. }
  167.  
  168. #endregion
  169.  
  170. #region Unit Wrappers
  171. protected GameObject GetTargetMissingDebuff(GameObject near, string debuff, float distance = -1f)
  172. {
  173. return UnfriendlyUnits.FirstOrDefault(u => u.Location.Distance3D(near.Location) <= distance && !u.HasAura(debuff, true));
  174. }
  175. protected int EnemiesNearTarget(float range)
  176. {
  177. var target = Core.Player.CurrentTarget;
  178. if (target == null)
  179. return 0;
  180. var tarLoc = target.Location;
  181. return UnfriendlyUnits.Count(u => u.ObjectId != target.ObjectId && u.Location.Distance3D(tarLoc) <= range);
  182. }
  183. //protected IEnumerable<GameObject> UnfriendlyMeleeUnits { get { return UnfriendlyUnits.Where(u => Actionmanager.InSpellInRangeLOS()); } }
  184.  
  185. protected IEnumerable<GameObject> UnfriendlyUnits
  186. {
  187. get
  188. {
  189. return
  190. GameObjectManager.Characters.Where(u => !u.IsDead && u.CanAttack);
  191. }
  192. }
  193.  
  194. #endregion
  195.  
  196.  
  197.  
  198. #region Spell Casting
  199.  
  200. protected Composite Cast(string spell, Selection<bool> reqs = null, Selection<GameObject> onTarget = null,
  201. bool ignoreCanCast = false)
  202. {
  203. return
  204. new Decorator(
  205. ret =>
  206. {
  207. // Check reqs if its there.
  208. if (reqs != null && !reqs(ret))
  209. return false;
  210.  
  211. // To ignore CanCast stuff (CanCast returns false while channeling, useful to be ignored for dots)
  212. if (!ignoreCanCast && !Actionmanager.CanCast(spell, onTarget != null ? onTarget(ret) : Core.Player.CurrentTarget))
  213. return false;
  214.  
  215. return true;
  216. },
  217. new Action(ret =>
  218. {
  219. var castingSpell = Core.Player.SpellCastInfo;
  220. // If we're casting something other than what we should be, stop casting it. (Channeling /stopcast stuff)
  221. if (castingSpell != null && castingSpell.SpellData.Name != spell)
  222. Actionmanager.StopCasting();
  223. Logging.Write("Casting " + spell);
  224. Actionmanager.DoAction(spell, (onTarget != null ? onTarget(ret) : Core.Player.CurrentTarget));
  225. }));
  226. }
  227.  
  228. protected Composite Apply(string spell, Selection<bool> reqs = null, Selection<GameObject> onTarget = null,
  229. int msLeft = 0, bool ignoreCanCast = false)
  230. {
  231. return new Decorator(ret =>
  232. {
  233. // Check reqs if its there.
  234. if (reqs != null && !reqs(ret))
  235. return false;
  236.  
  237. // Specific target, or just our general target.
  238. GameObject target = onTarget != null ? onTarget(ret) : Core.Player.CurrentTarget;
  239.  
  240. // To ignore CanCast stuff (CanCast returns false while channeling, useful to be ignored for dots)
  241. if (!ignoreCanCast && !Actionmanager.CanCast(spell, target))
  242. return false;
  243.  
  244. if (Extensions.DoubleCastPreventionDict.Contains(target, spell))
  245. return false;
  246.  
  247. // Check aura. Should be doing stackCount = 0, but 1 is more accurate.
  248. if (!target.HasAura(spell, true, msLeft))
  249. return true;
  250.  
  251. return false;
  252. },
  253. new Action(ret =>
  254. {
  255. var castingSpell = Core.Player.SpellCastInfo;
  256. // If we're casting something other than what we should be, stop casting it. (Channeling /stopcast stuff)
  257. if (castingSpell != null && castingSpell.SpellData.Name != spell)
  258. Actionmanager.StopCasting();
  259.  
  260. var unit = (onTarget != null ? onTarget(ret) : Core.Player.CurrentTarget);
  261. Logging.Write("Applying " + spell);
  262. Actionmanager.DoAction(spell, unit);
  263.  
  264. Extensions.UpdateDoubleCastDict(spell, unit);
  265. }));
  266. }
  267. #endregion
  268.  
  269. protected delegate T Selection<out T>(object context);
  270. }
  271.  
  272. public static class Extensions
  273. {
  274.  
  275. public static void RemoveAll<TKey, TValue>(this Dictionary<TKey, TValue> dic,
  276. Func<TValue, bool> predicate)
  277. {
  278. var keys = dic.Keys.Where(k => predicate(dic[k])).ToList();
  279. foreach (var key in keys)
  280. {
  281. dic.Remove(key);
  282. }
  283. }
  284.  
  285. public static void UpdateDoubleCastDict(string spellName, GameObject unit)
  286. {
  287. if (unit == null)
  288. return;
  289.  
  290. DateTime expir = DateTime.UtcNow + TimeSpan.FromSeconds(3);
  291. string key = DoubleCastKey(unit.ObjectId, spellName);
  292. if (DoubleCastPreventionDict.ContainsKey(key))
  293. DoubleCastPreventionDict[key] = expir;
  294.  
  295. DoubleCastPreventionDict.Add(key, expir);
  296. }
  297.  
  298. public static string DoubleCastKey(uint guid, string spellName)
  299. {
  300. return guid.ToString("X") + "-" + spellName;
  301. }
  302.  
  303. public static string DoubleCastKey(GameObject unit, string spell)
  304. {
  305. return DoubleCastKey(unit.ObjectId, spell);
  306. }
  307.  
  308. public static bool Contains(this Dictionary<string, DateTime> dict, GameObject unit, string spellName)
  309. {
  310. return dict.ContainsKey(DoubleCastKey(unit, spellName));
  311. }
  312.  
  313. public static bool ContainsAny(this Dictionary<string, DateTime> dict, GameObject unit, params string[] spellNames)
  314. {
  315. return spellNames.Any(s => dict.ContainsKey(DoubleCastKey(unit, s)));
  316. }
  317.  
  318. public static bool ContainsAll(this Dictionary<string, DateTime> dict, GameObject unit, params string[] spellNames)
  319. {
  320. return spellNames.All(s => dict.ContainsKey(DoubleCastKey(unit, s)));
  321. }
  322.  
  323. public static readonly Dictionary<string, DateTime> DoubleCastPreventionDict = new Dictionary<string, DateTime>();
  324.  
  325. public static Aura GetCachedAuraByName(this GameObject unit, string auraName, bool myAura = true)
  326. {
  327. Aura aura = null;
  328. ulong guid = myAura ? Core.Player.ObjectId : 0;
  329. if (unit.IsMe)
  330. {
  331. foreach (var a in KupoRoutine.LocalPlayerAuras)
  332. {
  333. if (a.Name == auraName)
  334. {
  335. if (myAura && a.CasterId != guid)
  336. continue;
  337.  
  338. aura = a;
  339. break;
  340. }
  341. }
  342. }
  343. else if (unit.ObjectId == Core.Player.CurrentTargetObjId)
  344. {
  345. foreach (var a in KupoRoutine.TargetAuras)
  346. {
  347. if (a.Name == auraName)
  348. {
  349. if (myAura && a.CasterId != guid)
  350. continue;
  351.  
  352. aura = a;
  353. break;
  354. }
  355. }
  356. }
  357. else
  358. {
  359. // If we only want *my* auras, then check for it.
  360. if (myAura)
  361. {
  362. foreach (var a in unit.ToCharacter.CharacterAuras)
  363. {
  364. if (a.Name == auraName && a.CasterId == guid)
  365. return a;
  366. }
  367. return null;
  368. }
  369.  
  370. // Otherwise, return the first we find.
  371. aura = unit.ToCharacter.GetAuraByName(auraName);
  372. }
  373.  
  374. return aura;
  375. }
  376.  
  377. public static bool HasAura(this GameObject unit, string aura, bool isMyAura = false, int msLeft = 0)
  378. {
  379. Aura result = unit.GetCachedAuraByName(aura);
  380. if (result == null)
  381. return false;
  382.  
  383. if (isMyAura && result.CasterId != Core.Player.ObjectId)
  384. return false;
  385.  
  386. if (result.TimespanLeft.TotalMilliseconds > msLeft)
  387. return true;
  388.  
  389. return false;
  390. }
  391.  
  392. public static bool HasCachedAura(this GameObject unit, string aura)
  393. {
  394. return unit.GetCachedAuraByName(aura) != null;
  395. }
  396.  
  397. public static TimeSpan AuraTimeLeft(this GameObject unit, string aura, bool myAura = true)
  398. {
  399. var a = unit.GetCachedAuraByName(aura);
  400.  
  401. if (a == null)
  402. return TimeSpan.Zero;
  403.  
  404. return a.TimespanLeft;
  405. }
  406. }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment