asmrobot
2019-11-25 2aeab450471cb80b59002da7da80faf251a0c4f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
using RichCreator.Models;
using RichCreator.Utility;
using RichCreator.Utility.Dnf;
using RichCreator.Utility.InputControl;
using RichCreator.Utility.Maps;
using RichCreator.Utility.Skills;
using RichCreator.Utility.Structs;
using RichCreator.Utility.Utilitys;
using System;
using System.Collections.Generic;
using System.Threading;
 
 
namespace RichCreator.Dnf
{
    /// <summary>
    /// DNF角色
    /// </summary>
    public class DnfRole
    {
        private const Int32 PaddingLeft = 140;//左边框内边距
        private const Int32 PaddingRight = 140;//右边框内边距
 
        /// <summary>
        /// 角色脚部距色块中央的偏移
        /// </summary>
        public static readonly ZTPoint RoleFootOffset = new ZTPoint(0, 143);
 
        /// <summary>
        /// 角色身体中间距色块中央的偏移
        /// </summary>
        public static readonly ZTPoint RoleHalfOffset = new ZTPoint(0, 88);
        
        public DnfRole(ZTRectangle gameRect)
        {
            this.gameRect = gameRect;
            speed = SpeedProvider.Define;
            moveCancelToken = new CancellationTokenSource();
        }
 
        private SpeedProvider speed;//速度
        private HouseInfo house;//所在房间
        private ZTRectangle gameRect = ZTRectangle.Empty;
        public MoveIntent movingIntent = MoveIntent.AttackMove;//移动意图
        private CancellationTokenSource moveCancelToken;//移动取消令牌
 
        private Int32 findRoleDir = 0;//寻找角色行走的方向
        private DateTime findRoleMoveTimeout = DateTime.MinValue;//方向移动过期时间
        
 
        /// <summary>
        /// 角色色块位置
        /// </summary>
        public ZTPoint RoleCBPosition { get; private set; }
 
        /// <summary>
        /// 角色脚部位置
        /// </summary>
        public ZTPoint Position
        {
            get
            {
                return CBToFootPosition(RoleCBPosition);
            }
        }
 
        /// <summary>
        /// 身体中间位置
        /// </summary>
        public ZTPoint HalfPosition
        {
            get
            {
                return CBToHalfPosition(RoleCBPosition);
            }
        }
 
        /// <summary>
        /// 按下的键,0:横向,1:纵向
        /// </summary>
        private HIDCode[] pressKeys = new HIDCode[] { HIDCode.NoEvent, HIDCode.NoEvent };//当前按键
        private bool isRun = false;//是否奔跑
 
        /// <summary>
        /// 设置当前房间信息
        /// </summary>
        /// <param name="house"></param>
        public void SetHouse(HouseInfo house)
        {
            this.house = house;
            toNextGate = true;
        }
 
        /// <summary>
        /// 设置当前角色速度
        /// </summary>
        /// <param name="speed"></param>
        public void SetSpeed(Int32 speed)
        {
            this.speed = new SpeedProvider(speed);
        }
 
        /// <summary>
        /// 更新位置
        /// </summary>
        /// <param name="roleCBPosition"></param>
        public void UpdatePosition(ZTPoint roleCBPosition)
        {
            this.RoleCBPosition = roleCBPosition;
        }
 
        private bool isMoving = false;
        /// <summary>
        /// 是否移动中
        /// </summary>
        public bool IsMoving
        {
            get
            {
                return isMoving;
                //return pressKeys[0] != HIDCode.NoEvent || pressKeys[1] != HIDCode.NoEvent;
            }
        }
 
        /// <summary>
        /// 是否指定移动行为
        /// </summary>
        /// <param name="intent"></param>
        /// <returns></returns>
        public bool IsMoveIntent(MoveIntent intent)
        {
            if (IsMoving && movingIntent == intent)
            {
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 攻击移动至
        /// </summary>
        /// <param name="skillReleasePoint"></param>
        public void AttackMove(ZTPoint skillReleasePoint, ParametersPoint screenLocation)
        {
            //屏幕坐标转地图坐标
            ZTPoint start = this.house.ScreenToMapCoordinate(this.Position, screenLocation);
            ZTPoint end = this.house.ScreenToMapCoordinate(DnfRole.HalfToFootPosition(skillReleasePoint), screenLocation);
            List<ZTPoint> paths = this.house.FindPath(start, end);
            this.movingIntent = MoveIntent.AttackMove;
            isMoving = true;
            ThreadPool.QueueUserWorkItem((obj) =>
            {
                this.MovePaths(start, paths, this.moveCancelToken.Token,"attack move");
            }, null);
        }
 
        /// <summary>
        /// 查找主角移动
        /// </summary>
        /// <param name="moveDistance"></param>
        public void FindRoleMove()
        {
            if (DateTime.Now >= findRoleMoveTimeout)
            {
                //转换方向
                findRoleDir++;
                ZTSize distance = ZTSize.Empty;
                switch (findRoleDir % 4)
                {
                    case 0:
                        //右
                        distance = new ZTSize(SpeedProvider.RandomMoveDistance, 0);
                        break;
                    case 1:
                        //下
                        distance = new ZTSize(0,SpeedProvider.RandomMoveDistance);
                        break;
                    case 2:
                        //左
                        distance = new ZTSize(-SpeedProvider.RandomMoveDistance, 0);
                        break;
                    case 3:
                        //上
                        distance = new ZTSize(0,-SpeedProvider.RandomMoveDistance);
                        break;
                }
 
                movingIntent = MoveIntent.FindRoleMove;
                isMoving = true;
                ThreadPool.QueueUserWorkItem((state) => {
                    CancellationToken ctoken = (CancellationToken)state;
                    this.SyncMoveKeyPress(distance);
                    if (!ctoken.IsCancellationRequested)
                    {
                        //如果没有取消则停止
                        StopMove("find role mvoe");
                    }
                }, this.moveCancelToken.Token);
 
            }
 
            
        }
 
        /// <summary>
        /// 进门移动
        /// </summary>
        public void EntryDoorMove(ZTPoint doorScreenPosition, ParametersPoint screenLocation, Direction doorDirect)
        {
            //先到进门点
            ZTPoint roleMapPoint = this.house.ScreenToMapCoordinate(this.Position, screenLocation);
            ZTPoint nextGatePoint = this.house.GetNextGatePoint(doorDirect);
            List<ZTPoint> paths = this.house.FindPath(roleMapPoint, DnfRole.HalfToFootPosition(nextGatePoint));
 
            //再到门后50像素
            ZTPoint dp = this.house.ScreenToMapCoordinate(doorScreenPosition, screenLocation);
            ZTPoint doorPoint = DnfRole.HalfToFootPosition(dp);
            switch (doorDirect)
            {
                case Direction.Up:
                    doorPoint = doorPoint.Add(0, -50);
                    break;
                case Direction.Right:
                    doorPoint = doorPoint.Add(50, 0);
                    break;
                case Direction.Bottom:
                    doorPoint = doorPoint.Add(0, 50);
                    break;
                case Direction.Left:
                    doorPoint = doorPoint.Add(-50, 0);
                    break;
            }
            paths.Add(doorPoint);
            this.movingIntent = MoveIntent.EntryDoor;
            isMoving = true;
            this.MovePaths(roleMapPoint, paths, this.moveCancelToken.Token,"entry move");
        }
 
        private bool toNextGate = true;
        private Int32 nextGate = 0;
        /// <summary>
        /// 移至巡逻点
        /// </summary>
        public void LoopMove(ParametersPoint screenLocation)
        {
            ZTPoint start = ZTPoint.Empty, end = ZTPoint.Empty;
            ZTPoint halfMapPosition= this.house.ScreenToMapCoordinate(this.HalfPosition, screenLocation); 
            ZTPoint footMapPosition= this.house.ScreenToMapCoordinate(this.Position, screenLocation); 
 
            if (toNextGate)
            {
                //判断当前是否进门点
                ZTPoint nextGatePoint=this.house.HousePathInfo.NextGates[nextGate].Point;
                ZTRectangle nextGateRect = new ZTRectangle(nextGatePoint.X - 20, nextGatePoint.Y - 20, nextGatePoint.X + 20, nextGatePoint.Y + 20);
                if (GeoHelper.IsInRect(halfMapPosition, nextGateRect))
                {
                    nextGate++;
                    nextGate = nextGate % this.house.HousePathInfo.NextGates.Count;
                    toNextGate = false;
                }
 
            }
            else
            {
                //判断当前是否在巡逻点
                ZTPoint loopPoint = this.house.HousePathInfo.LoopPoint;
                ZTRectangle loopRect = new ZTRectangle(loopPoint.X - 20, loopPoint.Y - 20, loopPoint.X + 20, loopPoint.Y + 20);
                if (GeoHelper.IsInRect(footMapPosition, loopRect))
                {
                    toNextGate = true;
                }
            }
 
 
            if (toNextGate)
            {
                //向进门点移动
                start = halfMapPosition;
                end=this.house.HousePathInfo.NextGates[nextGate].Point;
                G.Instance.InfoWriter($"nextgate->screen location:{screenLocation},start:{start},end:{end}");
            }
            else
            {
                //向巡逻点移动
                start = footMapPosition;
                end= this.house.HousePathInfo.LoopPoint;
                G.Instance.InfoWriter($"loop:screen location:{screenLocation},start:{start},end:{end}");
            }
 
            List<ZTPoint> paths = this.house.FindPath(start,end);
            this.movingIntent = MoveIntent.ToLoop;
            isMoving = true;
            ThreadPool.QueueUserWorkItem((obj) =>
            {
                this.MovePaths(start, paths, this.moveCancelToken.Token, "loop move");
            }, null);
        }
 
        /// <summary>
        /// 拾起物品移动
        /// </summary>
        public void PickupMove(ZTPoint screenThingPosition,ParametersPoint screenLocation)
        {
            //屏幕坐标转地图坐标
            ZTPoint start = this.house.ScreenToMapCoordinate(this.Position, screenLocation);
            ZTPoint end = this.house.ScreenToMapCoordinate(screenThingPosition, screenLocation);
            List<ZTPoint> paths = this.house.FindPath(start, end);
            this.movingIntent = MoveIntent.PickupMove;
            isMoving = true;
            //未被打断移动至色块后,拾取
            if (this.MovePaths(start, paths, this.moveCancelToken.Token,"pickup"))
            {
                G.Instance.InputControl.PressKey(RandomUtils.G(400, 500), HIDCode.X);
            }
        }
 
        /// <summary>
        /// 同步移动指定距离
        /// </summary>
        /// <param name="distance"></param>
        public void SyncMove(ZTPoint size)
        {
            ZTSize distance = new ZTSize(size.X, size.Y);
            isMoving = true;
            SyncMoveKeyPress(distance);
            StopMove("sync move");
        }
 
        /// <summary>
        /// 移动指定路径
        /// </summary>
        /// <param name="start"></param>
        /// <param name="paths"></param>
        /// <returns>true:完成移动,false:中间被打断</returns>
        private bool MovePaths(ZTPoint start, List<ZTPoint> paths, CancellationToken cancelToken,string from)
        {
            ZTPoint lastPoint = start;
            if (paths != null && paths.Count > 0)
            {
                for (int i = 0; i < paths.Count; i++)
                {
                    if (cancelToken.IsCancellationRequested)
                    {
                        break;
                    }
                    ZTPoint distance = paths[i].Sub(lastPoint);
                    this.SyncMoveKeyPress(new ZTSize(distance.X, distance.Y));
                    lastPoint = paths[i];
                }
            }
 
            if (!cancelToken.IsCancellationRequested)
            {
                //停止
                
                StopMove("move path,"+from);
                return true;
            }
            return false;
        }
        
        /// <summary>
        /// 同步移动指定距离的按键
        /// </summary>
        /// <param name="distance"></param>
        private void SyncMoveKeyPress(ZTSize distance)
        {
            HIDCode horizontal = HIDCode.NoEvent;
            HIDCode vertical = HIDCode.NoEvent;
            bool isRun = false;
            int runms, stepms;
            Int32 xsig = Math.Sign(distance.Width);
            Int32 ysig = Math.Sign(distance.Height);
 
            if (Math.Abs(distance.Width) >= speed.RunThresold)
            {
                isRun = true;
            }
 
            if (distance.Height > 0)
            {
                vertical = HIDCode.DownArrow;
            }
 
            if (distance.Height < 0)
            {
                vertical = HIDCode.UpArrow;
            }
 
            if (distance.Width > 0)
            {
                horizontal = HIDCode.RightArrow;
            }
 
            if (distance.Width < 0)
            {
                horizontal = HIDCode.LeftArrow;
            }
 
            PutDirectionKey(out stepms, out runms, horizontal, vertical, isRun);
            if (stepms > 0)
            {
                Int32 mdistance = (Int32)(stepms * speed.StepX);
                if (runms > 0)
                {
                    mdistance += (Int32)(runms * speed.RunX);
                }
                distance = new ZTSize(distance.Width - xsig * mdistance, distance.Height);
            }
 
            int ytime = (Int32)(Math.Abs(distance.Height) / speed.StepY);
            Int32 xtime = (Int32)(Math.Abs(distance.Width) / speed.StepX);
            if (isRun)
            {
                xtime = (Int32)(Math.Abs(distance.Width) / speed.RunX);
            }
 
            Int32 mintime = Math.Min(xtime, ytime);
            if (xtime <= 0)
            {
                mintime = ytime;
            }
            if (ytime <= 0)
            {
                mintime = xtime;
            }
            Thread.Sleep(mintime);
 
            xtime -= mintime;
            ytime -= mintime;
            if (xtime > 0)
            {
                PutDirectionKey(out stepms, out runms, horizontal, HIDCode.NoEvent, isRun);
                Thread.Sleep(xtime);
            }
            if (ytime > 0)
            {
                PutDirectionKey(out stepms, out runms, HIDCode.NoEvent, vertical, false);
                Thread.Sleep(ytime);
            }
        }
 
 
        /// <summary>
        /// 停止移动
        /// </summary>
        public void StopMove(string from)
        {
            if (IsMoving)
            {
                G.Instance.InfoWriter("stop move from:"+from);
                this.moveCancelToken.Cancel();
                this.moveCancelToken = new CancellationTokenSource();
 
                StateProvider.Instance.DirectionKey = "无";
                G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false);
                pressKeys[0] = HIDCode.NoEvent;
                pressKeys[1] = HIDCode.NoEvent;
                isMoving = false;
                isRun = false;
            }
        }
 
        /// <summary>
        /// 休眠
        /// </summary>
        /// <param name="millSecond"></param>
        public void Sleep(Int32 millSecond)
        {
            Thread.Sleep(millSecond);
        }
 
        /// <summary>
        /// 释放技能
        /// </summary>
        /// <param name="key"></param>
        public void ReleaseSkill(HIDCode key)
        {
            Int32 dur = RandomUtils.KeyPressDuration;
            G.Instance.InputControl.PressKey(dur, key);
            SkillInfo skill = SkillInfo.GetSkillInfo(key);
            if (skill != null)
            {
                Int32 subTime = (Int32)(skill.ReleaseWaitTime - dur);
                if (subTime > 0)
                {
                    Sleep(subTime);
                }
            }
        }
 
        #region 八方向移动
        /// <summary>
        /// 按下按键
        /// </summary>
        /// <param name="distance"></param>
        private void PutDirectionKey(ZTSize distance)
        {
            HIDCode horizontal = HIDCode.NoEvent;
            HIDCode vertical = HIDCode.NoEvent;
            bool isRun = false;
            if (distance.Width < 0)
            {
                horizontal = HIDCode.LeftArrow;
            }
            if (distance.Width > 0)
            {
                horizontal = HIDCode.RightArrow;
            }
 
            if (Math.Abs(distance.Width) >= speed.RunThresold)
            {
                isRun = true;
            }
 
 
            if (distance.Height < 0)
            {
                vertical = HIDCode.UpArrow;
            }
            if (distance.Height > 0)
            {
                vertical = HIDCode.DownArrow;
            }
 
            Int32 stepms, runms;
            PutDirectionKey(out stepms, out runms, horizontal, vertical, isRun);
        }
 
        /// <summary>
        /// 行走按键
        /// </summary>
        /// <param name="stepMS">已经步行ms</param>
        /// <param name="runMS">已经奔跑ms</param>
        /// <param name="horizontal">横向按键</param>
        /// <param name="vertical">纵向按键</param>
        /// <param name="isRun">是否奔跑</param>
        private void PutDirectionKey(out Int32 stepMS, out Int32 runMS, HIDCode horizontal, HIDCode vertical, bool isRun)
        {
            stepMS = 0;
            runMS = 0;
            if (horizontal == vertical)
            {
                return;
            }
            if (horizontal == pressKeys[0] && vertical == pressKeys[1])
            {
                return;
            }
 
            int keyCount = horizontal != HIDCode.NoEvent && vertical != HIDCode.NoEvent ? 2 : 1;
            Int32 tempwait = 0;
            if (isRun)
            {
                //跑
                if (!this.isRun || horizontal != pressKeys[0])
                {
                    //如果之前没有跑,或跑的方向不一样,则开始跑
                    G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, horizontal);
                    tempwait = RandomUtils.KeyPressDuration;
                    stepMS += tempwait;
                    Thread.Sleep(tempwait);
 
                    G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false);
                    Thread.Sleep(RandomUtils.KeyPressDuration);
                    G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, horizontal);
                    this.isRun = true;
 
                    if (keyCount == 2)
                    {
                        tempwait = RandomUtils.KeyPressDuration;
                        runMS += tempwait;
                        Thread.Sleep(tempwait);
                        G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, horizontal, vertical);
                        //StateProvider.Instance.DirectionKey = "跑," + horizontal.ToString() + "," + vertical.ToString();
                    }
                    //else
                    //{
                    //    StateProvider.Instance.DirectionKey = "跑," + horizontal.ToString();
                    //}
 
                }
                else
                {
                    //奔跑方向不变
                    int afterKeyCount = pressKeys[0] != HIDCode.NoEvent && pressKeys[1] != HIDCode.NoEvent ? 2 : 1;
                    if (afterKeyCount != keyCount)
                    {
                        //如果跟原来按键数量不一样
                        if (keyCount == 1)
                        {
                            G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, horizontal);
                            //StateProvider.Instance.DirectionKey = "走," + horizontal.ToString();
                        }
                        else
                        {
                            G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, horizontal, vertical);
                            //StateProvider.Instance.DirectionKey = "走," + horizontal.ToString() + "," + vertical.ToString();
                        }
 
                    }
 
                }
            }
            else
            {
                //走
                if (keyCount == 1)
                {
                    G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, horizontal != HIDCode.NoEvent ? horizontal : vertical);
                }
                else
                {
                    G.Instance.InputControl.PutDown(false, false, false, false, false, false, false, false, horizontal, vertical);
                }
 
            }
 
            pressKeys[0] = horizontal;
            pressKeys[1] = vertical;
        }
 
        /// <summary>
        /// 按钮是否变化
        /// </summary>
        /// <param name="pressKey"></param>
        /// <returns></returns>
        private bool IsChangeOfKey(HIDCode[] pressKey)
        {
            if (pressKey == null || pressKey.Length != 2)
            {
                return true;
            }
 
            if (pressKeys[0] == pressKey[0] && pressKeys[1] == pressKey[1])
            {
                return false;
            }
            return true;
        }
        #endregion
 
 
 
        #region role position convert
        /// <summary>
        /// 色块中央位置转为脚部位置
        /// </summary>
        /// <param name="cbPosition"></param>
        /// <returns></returns>
        public static ZTPoint CBToFootPosition(ZTPoint cbPosition)
        {
            return cbPosition.Add(RoleFootOffset);
        }
 
        /// <summary>
        /// 色块中央位置转为人物中间位置
        /// </summary>
        /// <param name="cbPosition"></param>
        /// <returns></returns>
        public static ZTPoint CBToHalfPosition(ZTPoint cbPosition)
        {
            return cbPosition.Add(RoleHalfOffset);
        }
 
        /// <summary>
        /// 脚部坐标到身份中央坐标转换
        /// </summary>
        /// <param name="footPosition"></param>
        /// <returns></returns>
        public static ZTPoint FootToHalfPosition(ZTPoint footPosition)
        {
            return footPosition.Sub(RoleFootOffset).Add(RoleHalfOffset);
        }
 
        /// <summary>
        /// 身体中央坐标到脚部坐标转换
        /// </summary>
        /// <param name="halfPosition"></param>
        /// <returns></returns>
        public static ZTPoint HalfToFootPosition(ZTPoint halfPosition)
        {
            return halfPosition.Sub(RoleHalfOffset).Add(RoleFootOffset);
        }
        #endregion
 
 
    }
}