XmdsInstanceUnitStateMachine.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. using CommonAI.Zone;
  2. using CommonAI.Zone.Helper;
  3. using CommonAI.Zone.Instance;
  4. using CommonLang;
  5. using CommonLang.Vector;
  6. using System;
  7. using static CommonAI.Zone.Instance.InstanceUnit;
  8. namespace XmdsCommonServer.Plugin.Units
  9. {
  10. public class XmdsInstanceUnitStateMachine
  11. {
  12. abstract public class XmdsStateFollow : InstanceUnit.State
  13. {
  14. public enum MoveState
  15. {
  16. Hold,
  17. Move,
  18. }
  19. // 被追目标
  20. private IPositionObject target;
  21. // 如果被单位碰撞,则向左或右挪动//
  22. private MoveAI moveAI;
  23. // 检测切换走停的间隔时间 //
  24. readonly private State<MoveState> state = new State<MoveState>(MoveState.Move);
  25. readonly private bool begin_in_min_range;
  26. // 检测切换走停的间隔时间 //
  27. private TimeInterval<int> start_move_hold_time;
  28. public abstract bool IsActive { get; }
  29. public bool IsNoWay { get { if (moveAI != null) { return moveAI.IsNoWay; } return false; } }
  30. public IPositionObject Target { get { return target; } }
  31. public MoveState FollowState { get { return state.Value; } }
  32. /// <summary>
  33. /// Hold到Move之间的检测间隔
  34. /// </summary>
  35. public int StartMoveHoldTimeMS
  36. {
  37. get { return (start_move_hold_time != null) ? start_move_hold_time.IntervalTimeMS : 0; }
  38. set
  39. {
  40. if (value > 0 && value != StartMoveHoldTimeMS)
  41. {
  42. start_move_hold_time = new TimeInterval<int>(value);
  43. }
  44. }
  45. }
  46. public virtual void MarkCannotArriveToAttack() { }
  47. public XmdsStateFollow(InstanceUnit unit, InstanceZoneObject target, bool beginInMinRange = true) : base(unit)
  48. {
  49. this.begin_in_min_range = beginInMinRange;
  50. this.target = target;
  51. this.moveAI = new MoveAI(unit);
  52. }
  53. public void ChangeMoveTarget(IPositionObject target)
  54. {
  55. if (moveAI.Target != target)
  56. {
  57. this.target = target;
  58. this.moveAI = new MoveAI(unit);
  59. }
  60. }
  61. public override bool onBlock(InstanceUnit.State new_state)
  62. {
  63. return true;
  64. }
  65. protected override void onStart()
  66. {
  67. if (cheekBeginInRange())
  68. {
  69. state.ChangeState(MoveState.Hold);
  70. unit.SetActionStatus(UnitActionStatus.Idle);
  71. }
  72. else
  73. {
  74. state.ChangeState(MoveState.Move);
  75. unit.SetActionStatus(UnitActionStatus.Move);
  76. moveAI.FindPath(target);
  77. }
  78. }
  79. protected override void onUpdate()
  80. {
  81. if (!IsActive)
  82. {
  83. unit.doSomething();
  84. }
  85. else
  86. {
  87. switch (FollowState)
  88. {
  89. case MoveState.Move:
  90. // 进入最小追踪距离 //
  91. if (CheckTargetInMinRange() == true)
  92. {
  93. changeToHold();
  94. }
  95. else
  96. {
  97. if (!this.unit.IsCannotMove)
  98. {
  99. if (moveAI.Target == null)
  100. {
  101. moveAI.FindPath(target);
  102. }
  103. moveAI.Update();
  104. }
  105. if (moveAI.IsNoWay)
  106. {
  107. if (CheckTargetInMaxRange() == true)
  108. {
  109. changeToHold();
  110. }
  111. else
  112. {
  113. //System.Console.WriteLine("标记不可攻击:" + this.unit.Name);
  114. this.MarkCannotArriveToAttack();
  115. unit.doSomething();
  116. }
  117. }
  118. }
  119. break;
  120. case MoveState.Hold:
  121. // 超过最大追踪范围 //
  122. if ((start_move_hold_time == null || start_move_hold_time.Update(zone.UpdateIntervalMS)) && (CheckTargetInMaxRange() == false))
  123. {
  124. changeToMove();
  125. }
  126. else
  127. {
  128. onUpdateFollowed(target);
  129. }
  130. break;
  131. }
  132. }
  133. }
  134. protected override void onStop() { }
  135. private bool cheekBeginInRange()
  136. {
  137. if (begin_in_min_range)
  138. {
  139. return CheckTargetInMinRange();
  140. }
  141. else
  142. {
  143. return CheckTargetInMaxRange();
  144. }
  145. }
  146. private void changeToMove()
  147. {
  148. state.ChangeState(MoveState.Move);
  149. unit.SetActionStatus(UnitActionStatus.Move);
  150. moveAI.FindPath(target);
  151. onInRangeChanged();
  152. onChangedToMove(target);
  153. }
  154. private void changeToHold()
  155. {
  156. state.ChangeState(MoveState.Hold);
  157. unit.SetActionStatus(UnitActionStatus.Idle);
  158. moveAI.Pause();
  159. onInRangeChanged();
  160. onChangedToHold(target);
  161. }
  162. /// <summary>
  163. /// 检查目标是否在跟随范围内。
  164. /// 检测为True,停止移动。
  165. /// </summary>
  166. protected abstract bool CheckTargetInMinRange();
  167. /// <summary>
  168. /// 检查目标是否在跟随范围内。
  169. /// 检测为False,开始移动。
  170. /// </summary>
  171. protected abstract bool CheckTargetInMaxRange();
  172. /// <summary>
  173. /// 行为改变
  174. /// </summary>
  175. protected virtual void onInRangeChanged()
  176. {
  177. }
  178. /// <summary>
  179. /// 目标已经被追踪到
  180. /// </summary>
  181. /// <param name="target"></param>
  182. protected virtual void onUpdateFollowed(IPositionObject target)
  183. {
  184. }
  185. /// <summary>
  186. /// 开始移动
  187. /// </summary>
  188. /// <param name="target"></param>
  189. protected virtual void onChangedToMove(IPositionObject target)
  190. {
  191. }
  192. /// <summary>
  193. /// changeToHold
  194. /// </summary>
  195. /// <param name="target"></param>
  196. protected virtual void onChangedToHold(IPositionObject target)
  197. {
  198. }
  199. }
  200. public class XmdsStateFollowAndAttack : XmdsStateFollow
  201. {
  202. /*readonly private*/
  203. InstanceUnit targetUnit;
  204. private SkillTemplate.CastTarget expectTarget;
  205. private TimeInterval<int> checkAdjustLaunchSkillTime;
  206. private InstanceUnit.SkillState launchSkill;
  207. private bool can_auto_focus_near_target;
  208. private bool can_random_skill = true;
  209. private bool can_attack = true;
  210. private float min_distance;
  211. private float max_distance;
  212. /// <summary>
  213. /// 是否追踪
  214. /// </summary>
  215. /// <returns></returns>
  216. override public bool IsActive
  217. {
  218. get { return can_attack && targetUnit.IsActive; }
  219. }
  220. public InstanceUnit.SkillState ExpectSkillState
  221. {
  222. get { return launchSkill; }
  223. set
  224. {
  225. launchSkill = value;
  226. expectTarget = value.Data.ExpectTarget;
  227. }
  228. }
  229. /** 标记不能达到目的,并攻击目标 */
  230. public override void MarkCannotArriveToAttack()
  231. {
  232. this.can_attack = false;
  233. }
  234. public SkillTemplate ExpectSkill
  235. {
  236. get { return (launchSkill != null) ? launchSkill.Data : unit.DefaultSkill; }
  237. }
  238. public InstanceUnit TargetUnit
  239. {
  240. get { return targetUnit; }
  241. set { targetUnit = value; }
  242. }
  243. public SkillTemplate.CastTarget ExpectTarget
  244. {
  245. get { return expectTarget; }
  246. }
  247. public bool IsLaunchRandomSkill
  248. {
  249. get { return can_random_skill; }
  250. set { can_random_skill = value; }
  251. }
  252. public bool IsAutoFocusNearTarget
  253. {
  254. get { return can_auto_focus_near_target; }
  255. set { can_auto_focus_near_target = value; }
  256. }
  257. public XmdsStateFollowAndAttack(
  258. InstanceUnit unit,
  259. InstanceUnit target,
  260. SkillTemplate.CastTarget expectTarget = SkillTemplate.CastTarget.Enemy,
  261. bool autoFocusNearTarget = false,
  262. InstanceUnit.SkillState lanuchSkill = null)
  263. : base(unit, target, false)
  264. {
  265. this.targetUnit = target;
  266. this.expectTarget = expectTarget;
  267. this.can_auto_focus_near_target = autoFocusNearTarget;
  268. this.StartMoveHoldTimeMS = zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS;
  269. this.launchSkill = lanuchSkill;
  270. }
  271. protected override void onStart()
  272. {
  273. this.resetMaxMinRange();
  274. this.checkAdjustLaunchSkillTime = new TimeInterval<int>(zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS);
  275. this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
  276. base.onStart();
  277. }
  278. protected override void onUpdateFollowed(IPositionObject target)
  279. {
  280. this.can_attack = zone.IsAttackable(unit, targetUnit, expectTarget, AttackReason.Tracing, this.ExpectSkill);
  281. if (can_attack)
  282. {
  283. unit.faceTo(target.X, target.Y);
  284. if (TryLaunchSkill() == null)
  285. {
  286. SkillTemplate expect_skill = ExpectSkill;
  287. if (expect_skill != null && expect_skill.AttackKeepRange > 0)
  288. {
  289. if (checkAdjustLaunchSkillTime.IntervalTimeMS == 0 || checkAdjustLaunchSkillTime.Update(zone.UpdateIntervalMS))
  290. {
  291. if (CUtils.RandomPercent(zone.RandomN, zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_ADJUST_ESCAPE_PCT))
  292. {
  293. unit.startAdjustLaunchSkill(expect_skill, targetUnit, this.OnEndAdjustKeepRange);
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. protected override void onStop()
  301. {
  302. base.onStop();
  303. }
  304. protected override void onUpdate()
  305. {
  306. if (IsActive)
  307. {
  308. resetMaxMinRange();
  309. }
  310. base.onUpdate();
  311. }
  312. private void resetMaxMinRange()
  313. {
  314. var expect_skill = ExpectSkillState;
  315. min_distance = unit.BodyBlockSize + targetUnit.BodyBlockSize;
  316. max_distance = min_distance;//Math.Max(min_distance, unit.BodyBlockSize + targetUnit.BodyHitSize);
  317. bool needCheck = true;
  318. if (expect_skill == null)
  319. {
  320. var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
  321. if (new_skill != null)
  322. {
  323. ExpectSkillState = new_skill;
  324. expect_skill = new_skill;
  325. needCheck = false;
  326. }
  327. }
  328. if (expect_skill != null)
  329. {
  330. if (can_random_skill)
  331. {
  332. if (needCheck == true && !expect_skill.checkTargetRange(targetUnit))
  333. {
  334. var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
  335. if (new_skill != null)
  336. {
  337. ExpectSkillState = new_skill;
  338. expect_skill = new_skill;
  339. }
  340. }
  341. }
  342. float skill_distance = unit.GetSkillAttackRange(expect_skill.Data);
  343. float half = (skill_distance - min_distance) / 2;
  344. if (expect_skill.Data.AttackKeepRange > 0 && expect_skill.Data.AttackRange > expect_skill.Data.AttackKeepRange)
  345. {
  346. min_distance = Math.Max(min_distance, unit.GetSkillAttackRange(expect_skill.Data.AttackKeepRange) + targetUnit.BodyBlockSize);
  347. max_distance = Math.Max(max_distance, min_distance);
  348. }
  349. else if (half > 0)
  350. {
  351. min_distance = min_distance + half;
  352. max_distance = Math.Max(max_distance, min_distance);
  353. }
  354. else
  355. {
  356. min_distance = Math.Max(min_distance, skill_distance + targetUnit.BodyBlockSize);
  357. max_distance = Math.Max(max_distance, min_distance);
  358. }
  359. max_distance = Math.Max(max_distance, skill_distance + targetUnit.BodyHitSize);
  360. }
  361. }
  362. protected override bool CheckTargetInMaxRange()
  363. {
  364. return CMath.includeRoundPoint(unit.X, unit.Y, max_distance, targetUnit.X, targetUnit.Y);
  365. }
  366. protected override bool CheckTargetInMinRange()
  367. {
  368. return CMath.includeRoundPoint(unit.X, unit.Y, min_distance, targetUnit.X, targetUnit.Y);
  369. }
  370. protected virtual bool OnEndAdjustKeepRange(InstanceUnit.StateMove m)
  371. {
  372. unit.faceTo(Target.X, Target.Y);
  373. unit.changeState(this);
  374. return true;
  375. }
  376. protected virtual SkillTemplate TryLaunchSkill()
  377. {
  378. if (launchSkill == null)
  379. {
  380. this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
  381. }
  382. InstanceUnit.StateSkill ret = null;
  383. Vector2 targetPos = targetUnit == null ? null : new Vector2(targetUnit.X, targetUnit.Y);
  384. if (launchSkill != null)
  385. {
  386. ret = unit.launchSkill(launchSkill.ID, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target, false, 0, true, true));
  387. if (ret != null)
  388. {
  389. return ret.SkillData;
  390. }
  391. }
  392. if (can_random_skill)
  393. {
  394. ret = unit.launchRandomSkill(expectTarget, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target, false, 0, true, true));
  395. if (ret != null)
  396. {
  397. return ret.SkillData;
  398. }
  399. }
  400. return null;
  401. }
  402. public void ChangeAttackTarget(InstanceUnit.SkillState expectSkill, InstanceUnit target)
  403. {
  404. TargetUnit = target;
  405. ExpectSkillState = expectSkill;
  406. ChangeMoveTarget(target);
  407. }
  408. }
  409. /// <summary>
  410. /// 嘲讽状态
  411. /// </summary>
  412. public class XmdsInstanceUnitStateMock : XmdsStateFollow, IStateNoneControllable
  413. {
  414. private InstanceUnit targetUnit;
  415. // 是否能攻击
  416. private bool can_attack = true;
  417. //标记失效
  418. public bool mMarkRemove = false;
  419. private float max_distance;
  420. public override bool IsActive
  421. {
  422. get
  423. {
  424. return targetUnit.IsActive && !mMarkRemove;
  425. }
  426. }
  427. public XmdsInstanceUnitStateMock(InstanceUnit unit, InstanceUnit targetUnit)
  428. : base(unit, targetUnit, false)
  429. {
  430. this.targetUnit = targetUnit;
  431. this.StartMoveHoldTimeMS = zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS;
  432. this.max_distance = unit.DefaultSkill.AttackRange - targetUnit.BodyBlockSize;
  433. }
  434. public InstanceUnit GetTargetUnit() { return this.targetUnit; }
  435. protected override void onStart()
  436. {
  437. unit.SetActionStatus(UnitActionStatus.ServerSyncMove);
  438. //unit.SendForceSync();
  439. base.onStart();
  440. }
  441. protected override void onStop()
  442. {
  443. unit.doSomething();
  444. base.onStop();
  445. }
  446. public override bool onBlock(InstanceUnit.State new_state)
  447. {
  448. if (!IsActive || (unit.IsDead() && new_state is StateDead))
  449. {
  450. return true;
  451. }
  452. if (new_state is XmdsInstanceUnitStateMock)
  453. {
  454. return true;
  455. }
  456. else if (new_state is StateSkill)
  457. {
  458. StateSkill ss = new_state as StateSkill;
  459. // 反击或者状态解除//
  460. if (ss.Skill.GetSkillType() == XmdsSkillType.normalAtk)
  461. {
  462. return true;
  463. }
  464. }
  465. return false;
  466. }
  467. protected override void onUpdateFollowed(IPositionObject target)
  468. {
  469. this.can_attack = zone.IsAttackable(unit, targetUnit, SkillTemplate.CastTarget.Enemy, AttackReason.Tracing, this.unit.DefaultSkill);
  470. if (can_attack)
  471. {
  472. unit.faceTo(targetUnit.X, targetUnit.Y);
  473. unit.launchSkill(this.unit.DefaultSkillStatus(), new InstanceUnit.LaunchSkillParam(targetUnit.ID));
  474. }
  475. }
  476. protected override bool CheckTargetInMinRange()
  477. {
  478. return CMath.includeRoundPoint(unit.X, unit.Y, max_distance, targetUnit.X, targetUnit.Y);
  479. }
  480. protected override bool CheckTargetInMaxRange()
  481. {
  482. return CMath.includeRoundPoint(unit.X, unit.Y, max_distance, targetUnit.X, targetUnit.Y);
  483. }
  484. public State AsState()
  485. {
  486. return this;
  487. }
  488. }
  489. }
  490. }