XmdsInstanceUnitStateMachine.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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.checkAdjustLaunchSkillTime = new TimeInterval<int>(zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS);
  274. this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
  275. base.onStart();
  276. }
  277. protected override void onUpdateFollowed(IPositionObject target)
  278. {
  279. this.can_attack = zone.IsAttackable(unit, targetUnit, expectTarget, AttackReason.Tracing, this.ExpectSkill);
  280. if (can_attack)
  281. {
  282. unit.faceTo(target.X, target.Y);
  283. if (TryLaunchSkill() == null)
  284. {
  285. SkillTemplate expect_skill = ExpectSkill;
  286. if (expect_skill != null && expect_skill.AttackKeepRange > 0)
  287. {
  288. if (checkAdjustLaunchSkillTime.IntervalTimeMS == 0 || checkAdjustLaunchSkillTime.Update(zone.UpdateIntervalMS))
  289. {
  290. if (CUtils.RandomPercent(zone.RandomN, zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_ADJUST_ESCAPE_PCT))
  291. {
  292. unit.startAdjustLaunchSkill(expect_skill, targetUnit, this.OnEndAdjustKeepRange);
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. protected override void onStop()
  300. {
  301. base.onStop();
  302. }
  303. protected override void onUpdate()
  304. {
  305. if (IsActive)
  306. {
  307. resetMaxMinRange();
  308. }
  309. base.onUpdate();
  310. }
  311. private void resetMaxMinRange()
  312. {
  313. var expect_skill = ExpectSkillState;
  314. min_distance = unit.BodyBlockSize + targetUnit.BodyBlockSize;
  315. max_distance = Math.Max(min_distance, unit.BodyBlockSize + targetUnit.BodyHitSize);
  316. bool needCheck = true;
  317. if (expect_skill == null)
  318. {
  319. var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
  320. if (new_skill != null)
  321. {
  322. ExpectSkillState = new_skill;
  323. expect_skill = new_skill;
  324. needCheck = false;
  325. }
  326. }
  327. if (expect_skill != null)
  328. {
  329. if (can_random_skill)
  330. {
  331. if (needCheck == true && !expect_skill.checkTargetRange(targetUnit))
  332. {
  333. var new_skill = unit.getRandomLaunchableExpectSkill(targetUnit, expectTarget, AttackReason.Tracing, true);
  334. if (new_skill != null)
  335. {
  336. ExpectSkillState = new_skill;
  337. expect_skill = new_skill;
  338. }
  339. }
  340. }
  341. float skill_distance = unit.GetSkillAttackRange(expect_skill.Data);
  342. float half = (skill_distance - min_distance) / 2;
  343. if (expect_skill.Data.AttackKeepRange > 0 && expect_skill.Data.AttackRange > expect_skill.Data.AttackKeepRange)
  344. {
  345. min_distance = Math.Max(min_distance, unit.GetSkillAttackRange(expect_skill.Data.AttackKeepRange) + targetUnit.BodyBlockSize);
  346. max_distance = Math.Max(max_distance, min_distance);
  347. }
  348. else if (half > 0)
  349. {
  350. min_distance = min_distance + half;
  351. max_distance = Math.Max(max_distance, min_distance);
  352. }
  353. else
  354. {
  355. min_distance = Math.Max(min_distance, skill_distance + targetUnit.BodyBlockSize);
  356. max_distance = Math.Max(max_distance, min_distance);
  357. }
  358. max_distance = Math.Max(max_distance, skill_distance + targetUnit.BodyHitSize);
  359. }
  360. }
  361. protected override bool CheckTargetInMaxRange()
  362. {
  363. return CMath.includeRoundPoint(unit.X, unit.Y, max_distance, targetUnit.X, targetUnit.Y);
  364. }
  365. protected override bool CheckTargetInMinRange()
  366. {
  367. return CMath.includeRoundPoint(unit.X, unit.Y, min_distance, targetUnit.X, targetUnit.Y);
  368. }
  369. protected virtual bool OnEndAdjustKeepRange(InstanceUnit.StateMove m)
  370. {
  371. unit.faceTo(Target.X, Target.Y);
  372. unit.changeState(this);
  373. return true;
  374. }
  375. protected virtual SkillTemplate TryLaunchSkill()
  376. {
  377. if (launchSkill == null)
  378. {
  379. this.launchSkill = unit.getRandomLaunchableExpectSkill(expectTarget);
  380. }
  381. InstanceUnit.StateSkill ret = null;
  382. Vector2 targetPos = targetUnit == null ? null : new Vector2(targetUnit.X, targetUnit.Y);
  383. if (launchSkill != null)
  384. {
  385. ret = unit.launchSkill(launchSkill.ID, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target, false, 0, true, true));
  386. if (ret != null)
  387. {
  388. return ret.SkillData;
  389. }
  390. }
  391. if (can_random_skill)
  392. {
  393. ret = unit.launchRandomSkill(expectTarget, new InstanceUnit.LaunchSkillParam(targetUnit.ID, targetPos, can_auto_focus_near_target, false, 0, true, true));
  394. if (ret != null)
  395. {
  396. return ret.SkillData;
  397. }
  398. }
  399. return null;
  400. }
  401. public void ChangeAttackTarget(InstanceUnit.SkillState expectSkill, InstanceUnit target)
  402. {
  403. TargetUnit = target;
  404. ExpectSkillState = expectSkill;
  405. ChangeMoveTarget(target);
  406. }
  407. }
  408. /// <summary>
  409. /// 嘲讽状态
  410. /// </summary>
  411. public class XmdsInstanceUnitStateMock : XmdsStateFollow, IStateNoneControllable
  412. {
  413. private InstanceUnit targetUnit;
  414. // 是否能攻击
  415. private bool can_attack = true;
  416. //标记失效
  417. public bool mMarkRemove = false;
  418. private float max_distance;
  419. public override bool IsActive
  420. {
  421. get
  422. {
  423. return targetUnit.IsActive && !mMarkRemove;
  424. }
  425. }
  426. public XmdsInstanceUnitStateMock(InstanceUnit unit, InstanceUnit targetUnit)
  427. : base(unit, targetUnit, false)
  428. {
  429. this.targetUnit = targetUnit;
  430. this.StartMoveHoldTimeMS = zone.Templates.CFG.AI_FOLLOW_AND_ATTACK_HOLD_TIME_MS;
  431. this.max_distance = unit.DefaultSkill.AttackRange - targetUnit.BodyBlockSize;
  432. }
  433. public InstanceUnit GetTargetUnit() { return this.targetUnit; }
  434. protected override void onStart()
  435. {
  436. unit.SetActionStatus(UnitActionStatus.ServerSyncMove);
  437. //unit.SendForceSync();
  438. base.onStart();
  439. }
  440. protected override void onStop()
  441. {
  442. unit.doSomething();
  443. base.onStop();
  444. }
  445. public override bool onBlock(InstanceUnit.State new_state)
  446. {
  447. if (!IsActive || (unit.IsDead() && new_state is StateDead))
  448. {
  449. return true;
  450. }
  451. if (new_state is XmdsInstanceUnitStateMock)
  452. {
  453. return true;
  454. }
  455. else if (new_state is StateSkill)
  456. {
  457. StateSkill ss = new_state as StateSkill;
  458. // 反击或者状态解除//
  459. if (ss.Skill.GetSkillType() == XmdsSkillType.normalAtk)
  460. {
  461. return true;
  462. }
  463. }
  464. return false;
  465. }
  466. protected override void onUpdateFollowed(IPositionObject target)
  467. {
  468. this.can_attack = zone.IsAttackable(unit, targetUnit, SkillTemplate.CastTarget.Enemy, AttackReason.Tracing, this.unit.DefaultSkill);
  469. if (can_attack)
  470. {
  471. unit.faceTo(targetUnit.X, targetUnit.Y);
  472. unit.launchSkill(this.unit.DefaultSkillStatus(), new InstanceUnit.LaunchSkillParam(targetUnit.ID));
  473. }
  474. }
  475. protected override bool CheckTargetInMinRange()
  476. {
  477. return CMath.includeRoundPoint(unit.X, unit.Y, max_distance, targetUnit.X, targetUnit.Y);
  478. }
  479. protected override bool CheckTargetInMaxRange()
  480. {
  481. return CMath.includeRoundPoint(unit.X, unit.Y, max_distance, targetUnit.X, targetUnit.Y);
  482. }
  483. public State AsState()
  484. {
  485. return this;
  486. }
  487. }
  488. }
  489. }