/
GameClient.cs
2041 lines (1795 loc) · 68.5 KB
/
GameClient.cs
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
namespace PROProtocol
{
public class GameClient
{
public InventoryItem GroundMount;
public InventoryItem WaterMount;
public Random Rand { get; private set; }
public Language I18n { get; private set; }
public bool IsConnected { get; private set; }
public bool IsAuthenticated { get; private set; }
public string PlayerName { get; private set; }
public int PlayerX { get; private set; }
public int PlayerY { get; private set; }
public string MapName { get; private set; }
public Map Map { get; private set; }
public int PokedexOwned { get; private set; }
public int PokedexSeen { get; private set; }
public int PokedexEvolved { get; private set; }
public bool IsInBattle { get; private set; }
public bool IsSurfing { get; private set; }
public bool IsBiking { get; private set; }
public bool IsOnGround { get; private set; }
public bool IsPCOpen { get; private set; }
public bool CanUseCut { get; private set; }
public bool CanUseSmashRock { get; private set; }
public bool IsPrivateMessageOn { get; private set; }
public int Money { get; private set; }
public int Coins { get; private set; }
public bool IsMember { get; private set; }
public List<Pokemon> Team { get; private set; }
public List<Pokemon> CurrentPCBox { get; private set; }
public List<InventoryItem> Items { get; private set; }
public string PokemonTime { get; private set; }
public string Weather { get; private set; }
public int PCGreatestUid { get; private set; }
public bool IsScriptActive { get; private set; }
public string ScriptId { get; private set; }
public int ScriptStatus { get; private set; }
public string[] DialogContent { get; private set; }
public Battle ActiveBattle { get; private set; }
public Shop OpenedShop { get; private set; }
public MoveRelearner MoveRelearner { get; private set; }
public List<ChatChannel> Channels { get; }
public List<string> Conversations { get; }
public Dictionary<string, PlayerInfos> Players { get; }
private DateTime _updatePlayers;
private DateTime _refreshBoxTimeout;
public bool IsPCBoxRefreshing { get; private set; }
public int CurrentPCBoxId { get; private set; }
public bool IsCreatingNewCharacter { get; private set; }
public event Action ConnectionOpened;
public event Action<Exception> ConnectionFailed;
public event Action<Exception> ConnectionClosed;
public event Action LoggedIn;
public event Action<AuthenticationResult> AuthenticationFailed;
public event Action<int> QueueUpdated;
public event Action<string, int, int> PositionUpdated;
public event Action<string, int, int> TeleportationOccuring;
public event Action<string> MapLoaded;
public event Action<List<Npc>> NpcReceived;
public event Action PokemonsUpdated;
public event Action InventoryUpdated;
public event Action BattleStarted;
public event Action<string> BattleMessage;
public event Action BattleEnded;
public event Action BattleUpdated;
public event Action<string> DialogOpened;
public event Action<string, string, int> EmoteMessage;
public event Action<string, string, string> ChatMessage;
public event Action RefreshChannelList;
public event Action<string, string, string, string> ChannelMessage;
public event Action<string, string> ChannelSystemMessage;
public event Action<string, string, string, string> ChannelPrivateMessage;
public event Action<string> SystemMessage;
public event Action<string, string, string, string> PrivateMessage;
public event Action<string, string, string> LeavePrivateMessage;
public event Action<PlayerInfos> PlayerUpdated;
public event Action<PlayerInfos> PlayerAdded;
public event Action<PlayerInfos> PlayerRemoved;
public event Action<string, string> InvalidPacket;
public event Action<int, string, int> LearningMove;
public event Action<int, int> Evolving;
public event Action<string, string> PokeTimeUpdated;
public event Action<Shop> ShopOpened;
public event Action<MoveRelearner> MoveRelearnerOpened;
public event Action<List<Pokemon>> PCBoxUpdated;
public event Action<string> LogMessage;
public event Action ActivePokemonChanged;
public event Action OpponentChanged;
private const string Version = "Dolphin";
private GameConnection _connection;
private DateTime _lastMovement;
private List<Direction> _movements = new List<Direction>();
private Direction? _slidingDirection;
private bool _surfAfterMovement;
private Queue<object> _dialogResponses = new Queue<object>();
private Timeout _movementTimeout = new Timeout();
private Timeout _battleTimeout = new Timeout();
private Timeout _loadingTimeout = new Timeout();
private Timeout _mountingTimeout = new Timeout();
private Timeout _teleportationTimeout = new Timeout();
private Timeout _dialogTimeout = new Timeout();
private Timeout _swapTimeout = new Timeout();
private Timeout _itemUseTimeout = new Timeout();
private Timeout _fishingTimeout = new Timeout();
private Timeout _refreshingPCBox = new Timeout();
private Timeout _moveRelearnerTimeout = new Timeout();
private Timeout _npcBattleTimeout = new Timeout();
private Npc _npcBattler;
private MapClient _mapClient;
public void ClearPath()
{
_movements.Clear();
}
public bool IsInactive =>
_movements.Count == 0
&& !_movementTimeout.IsActive
&& !_battleTimeout.IsActive
&& !_loadingTimeout.IsActive
&& !_mountingTimeout.IsActive
&& !_teleportationTimeout.IsActive
&& !_dialogTimeout.IsActive
&& !_swapTimeout.IsActive
&& !_itemUseTimeout.IsActive
&& !_fishingTimeout.IsActive
&& !_refreshingPCBox.IsActive
&& !_npcBattleTimeout.IsActive
&& !_moveRelearnerTimeout.IsActive
&& !IsCreatingNewCharacter;
public bool IsTeleporting => _teleportationTimeout.IsActive;
public GameServer Server => _connection.Server;
public bool IsMapLoaded => Map != null;
public bool AreNpcReceived { get; private set; }
public GameClient(GameConnection connection, MapConnection mapConnection)
{
_mapClient = new MapClient(mapConnection);
_mapClient.ConnectionOpened += MapClient_ConnectionOpened;
_mapClient.ConnectionClosed += MapClient_ConnectionClosed;
_mapClient.ConnectionFailed += MapClient_ConnectionFailed;
_mapClient.MapLoaded += MapClient_MapLoaded;
_connection = connection;
_connection.PacketReceived += OnPacketReceived;
_connection.Connected += OnConnectionOpened;
_connection.Disconnected += OnConnectionClosed;
Rand = new Random();
I18n = new Language();
Team = new List<Pokemon>();
CurrentPCBox = new List<Pokemon>();
Items = new List<InventoryItem>();
Channels = new List<ChatChannel>();
Conversations = new List<string>();
Players = new Dictionary<string, PlayerInfos>();
PCGreatestUid = -1;
IsPrivateMessageOn = true;
}
public void Open()
{
_connection.Connect();
}
public void Close(Exception error = null)
{
_connection.Close(error);
}
public void Update()
{
_mapClient.Update();
_connection.Update();
if (!IsAuthenticated)
return;
_movementTimeout.Update();
_battleTimeout.Update();
_loadingTimeout.Update();
_mountingTimeout.Update();
_teleportationTimeout.Update();
_dialogTimeout.Update();
_swapTimeout.Update();
_itemUseTimeout.Update();
_fishingTimeout.Update();
_refreshingPCBox.Update();
_moveRelearnerTimeout.Update();
SendRegularPing();
UpdateMovement();
UpdateScript();
UpdatePlayers();
UpdatePCBox();
UpdateNpcBattle();
}
public void CloseChannel(string channelName)
{
if (Channels.Any(e => e.Name == channelName))
{
SendMessage("/cgleave " + channelName);
}
}
public void CloseConversation(string pmName)
{
if (Conversations.Contains(pmName))
{
SendMessage("/pm rem " + pmName + "-=-" + PlayerName + '|' + PlayerName);
Conversations.Remove(pmName);
}
}
private int _pingCurrentStep = 0;
private bool _isPingSwapped = false;
private void SendRegularPing()
{
if ((DateTime.UtcNow - _lastMovement).TotalSeconds >= 6)
{
_lastMovement = DateTime.UtcNow;
// DSSock.Update
int packetType;
if (_pingCurrentStep == 5)
{
packetType = _isPingSwapped ? 1 : 2;
_pingCurrentStep = 0;
}
else
{
packetType = Rand.Next(2) + 1;
}
if (packetType == 1)
{
_isPingSwapped = !_isPingSwapped;
}
_pingCurrentStep++;
SendPacket(packetType.ToString());
}
}
private void UpdateMovement()
{
if (!IsMapLoaded) return;
if (!_movementTimeout.IsActive && _movements.Count > 0)
{
if (GroundMount != null && !_itemUseTimeout.IsActive && !IsBiking && !IsSurfing && Map.IsOutside)
{
LogMessage?.Invoke($"Mounting [{GroundMount.Name}]");
UseItem(GroundMount.Id);
return;
}
Direction direction = _movements[0];
_movements.RemoveAt(0);
if (ApplyMovement(direction))
{
SendMovement(direction.AsChar());
_movementTimeout.Set(IsBiking ? 125 : 250);
if (Map.HasLink(PlayerX, PlayerY))
{
_teleportationTimeout.Set();
}
else
{
Npc battler = Map.Npcs.FirstOrDefault(npc => npc.CanBattle && npc.IsInLineOfSight(PlayerX, PlayerY));
if (battler != null)
{
battler.CanBattle = false;
LogMessage?.Invoke("The NPC " + (battler.Name ?? battler.Id.ToString()) + " saw us, interacting...");
_npcBattler = battler;
int distanceFromBattler = DistanceBetween(PlayerX, PlayerY, battler.PositionX, battler.PositionY);
_npcBattleTimeout.Set(Rand.Next(1000, 2000) + distanceFromBattler * 250);
ClearPath();
}
}
}
if (_movements.Count == 0 && _surfAfterMovement)
{
_movementTimeout.Set(Rand.Next(750, 2000));
}
}
if (!_movementTimeout.IsActive && _movements.Count == 0 && _surfAfterMovement)
{
_surfAfterMovement = false;
UseSurf();
}
}
private void UpdatePlayers()
{
if (_updatePlayers < DateTime.UtcNow)
{
foreach (string playerName in Players.Keys.ToArray())
{
if (Players[playerName].IsExpired())
{
PlayerRemoved?.Invoke(Players[playerName]);
Players.Remove(playerName);
}
}
_updatePlayers = DateTime.UtcNow.AddSeconds(5);
}
}
private void UpdatePCBox()
{
// if we did not receive an answer, then the box is empty
if (IsPCBoxRefreshing && _refreshBoxTimeout > DateTime.UtcNow)
{
IsPCBoxRefreshing = false;
if (Map.IsPC(PlayerX, PlayerY - 1))
{
IsPCOpen = true;
}
CurrentPCBox = new List<Pokemon>();
PCBoxUpdated?.Invoke(CurrentPCBox);
}
}
private void UpdateNpcBattle()
{
if (_npcBattler == null) return;
_npcBattleTimeout.Update();
if (_npcBattleTimeout.IsActive) return;
TalkToNpc(_npcBattler);
_npcBattler = null;
}
private bool ApplyMovement(Direction direction)
{
int destinationX = PlayerX;
int destinationY = PlayerY;
bool isOnGround = IsOnGround;
bool isSurfing = IsSurfing;
direction.ApplyToCoordinates(ref destinationX, ref destinationY);
Map.MoveResult result = Map.CanMove(direction, destinationX, destinationY, isOnGround, isSurfing, CanUseCut, CanUseSmashRock);
if (Map.ApplyMovement(direction, result, ref destinationX, ref destinationY, ref isOnGround, ref isSurfing))
{
PlayerX = destinationX;
PlayerY = destinationY;
IsOnGround = isOnGround;
IsSurfing = isSurfing;
PositionUpdated?.Invoke(MapName, PlayerX, PlayerY);
if (result == Map.MoveResult.Icing)
{
_movements.Insert(0, direction);
}
if (result == Map.MoveResult.Sliding)
{
int slider = Map.GetSlider(destinationX, destinationY);
if (slider != -1)
{
_slidingDirection = Map.SliderToDirection(slider);
}
}
if (_slidingDirection != null)
{
_movements.Insert(0, _slidingDirection.Value);
}
return true;
}
_slidingDirection = null;
return false;
}
private void UpdateScript()
{
if (IsScriptActive && !_dialogTimeout.IsActive)
{
if (ScriptStatus == 0)
{
_dialogResponses.Clear();
IsScriptActive = false;
}
else if (ScriptStatus == 1)
{
SendDialogResponse(0);
_dialogTimeout.Set();
}
else if (ScriptStatus == 3 || ScriptStatus == 4)
{
SendDialogResponse(GetNextDialogResponse());
_dialogTimeout.Set();
}
}
}
private int GetNextDialogResponse()
{
if (_dialogResponses.Count > 0)
{
object response = _dialogResponses.Dequeue();
if (response is int)
{
return (int)response;
}
else if (response is string)
{
string text = ((string)response).ToUpperInvariant();
for (int i = 1; i < DialogContent.Length; ++i)
{
if (DialogContent[i].ToUpperInvariant().Equals(text))
{
return i;
}
}
}
}
return 1;
}
public int DistanceTo(int cellX, int cellY)
{
return Math.Abs(PlayerX - cellX) + Math.Abs(PlayerY - cellY);
}
public static int DistanceBetween(int fromX, int fromY, int toX, int toY)
{
return Math.Abs(fromX - toX) + Math.Abs(fromY - toY);
}
public void SendPacket(string packet)
{
#if DEBUG
Console.WriteLine("[>] " + packet);
#endif
_connection.Send(packet);
}
public void SendMessage(string text)
{
// DSSock.sendMSG
SendPacket("{|.|" + text);
}
public void SendPrivateMessage(string nickname, string text)
{
// DSSock.sendMSG
string pmHeader = "/pm " + PlayerName + "-=-" + nickname;
SendPacket("{|.|" + pmHeader + '|' + text);
}
public void SendStartPrivateMessage(string nickname)
{
SendMessage("/pm " + PlayerName + "-=-" + nickname);
}
public void SendFriendToggle(string nickname)
{
SendMessage("/friend " + nickname);
}
public void SendIgnoreToggle(string nickname)
{
SendMessage("/ignore " + nickname);
}
public void CreateCharacter(int hair, int colour, int tone, int clothe, int eyes)
{
if (!IsCreatingNewCharacter) return;
IsCreatingNewCharacter = false;
SendCreateCharacter(hair, colour, tone, clothe, eyes);
_dialogTimeout.Set();
}
private void SendCreateCharacter(int hair, int colour, int tone, int clothe, int eyes)
{
SendMessage("/setchar " + hair + "," + colour + "," + tone + "," + clothe + "," + eyes);
}
public void SendAuthentication(string username, string password, Guid deviceId)
{
// DSSock.AttemptLogin
SendPacket("+|.|" + username + "|.|" + password + "|.|" + Version + "|.|" + deviceId + "|.|" + "Windows 10 (10.0.0) 64bit");
// TODO: Add an option to select the OS we want, it could be useful.
// I use Windows 10 here because the version is the same for everyone. This is not the case on Windows 7 or Mac.
}
public void SendUseItem(int id, int pokemon = 0)
{
string toSend = "*|.|" + id;
if (pokemon != 0)
{
toSend += "|.|" + pokemon;
}
SendPacket(toSend);
}
public void SendGiveItem(int pokemonUid, int itemId)
{
SendMessage("/giveitem " + pokemonUid + "," + itemId);
}
public void SendTakeItem(int pokemonUid)
{
SendMessage("/takeitem " + pokemonUid);
}
public void LearnMove(int pokemonUid, int moveToForgetUid)
{
_swapTimeout.Set();
SendLearnMove(pokemonUid, moveToForgetUid);
}
private void SendLearnMove(int pokemonUid, int moveToForgetUid)
{
SendPacket("^|.|" + pokemonUid + "|.|" + moveToForgetUid);
}
private void SendMovePokemonToPC(int pokemonUid)
{
SendPacket("?|.|" + pokemonUid + "|.|-1");
}
// if there is a pokemon in teamSlot, it will be swapped
private void SendMovePokemonFromPC(int pokemonUid, int teamSlot)
{
SendPacket("?|.|" + pokemonUid + "|.|" + teamSlot);
}
private void SendRefreshPCBox(int box, string search)
{
SendPacket("M|.|" + box + "|.|" + search);
}
private void SendReleasePokemon(int pokemonUid)
{
SendMessage("/release " + pokemonUid);
}
private void SendPrivateMessageOn()
{
SendMessage("/pmon");
}
private void SendPrivateMessageOff()
{
SendMessage("/pmoff");
}
private void SendPrivateMessageAway()
{
SendMessage("/pmaway");
}
public bool PrivateMessageOn()
{
IsPrivateMessageOn = true;
SendPrivateMessageOn();
return true;
}
public bool PrivateMessageOff()
{
IsPrivateMessageOn = false;
SendPrivateMessageOff();
return true;
}
// /pmaway does not seem to do anything
public bool PrivateMessageAway()
{
SendPrivateMessageAway();
return true;
}
public bool ReleasePokemonFromPC(int boxId, int boxPokemonId)
{
if (!IsPCOpen || IsPCBoxRefreshing || boxId < 1 || boxId > 67
|| boxPokemonId < 1 || boxPokemonId > 15 || boxPokemonId > CurrentPCBox.Count)
{
return false;
}
int pokemonUid = GetPokemonPCUid(boxId, boxPokemonId);
if (pokemonUid == -1 || pokemonUid != CurrentPCBox[boxPokemonId - 1].Uid)
{
return false;
}
_refreshingPCBox.Set(Rand.Next(1500, 2000));
SendReleasePokemon(pokemonUid);
return true;
}
public bool ReleasePokemonFromTeam(int pokemonUid)
{
if (!IsPCOpen || IsPCBoxRefreshing
|| pokemonUid < 1 || pokemonUid > 6 || pokemonUid > Team.Count)
{
return false;
}
_refreshingPCBox.Set(Rand.Next(1500, 2000));
SendReleasePokemon(pokemonUid);
return true;
}
public bool RefreshPCBox(int boxId)
{
if (!IsPCOpen || boxId < 1 || boxId > 67 || _refreshingPCBox.IsActive || IsPCBoxRefreshing)
{
return false;
}
_refreshingPCBox.Set(Rand.Next(1500, 2000)); // this is the amount of time we wait for an answer
CurrentPCBoxId = boxId;
IsPCBoxRefreshing = true;
CurrentPCBox = null;
_refreshBoxTimeout = DateTime.UtcNow.AddSeconds(5); // this is to avoid a flood of the function
SendRefreshPCBox(boxId - 1, "ID");
return true;
}
public bool RefreshCurrentPCBox()
{
return RefreshPCBox(CurrentPCBoxId);
}
private int GetPokemonPCUid(int box, int id)
{
if (box < 1 || box > 67 || id < 1 || id > 15)
{
return -1;
}
int result = (box - 1) * 15 + 6 + id;
// ensures we cannot access a pokemon we do not have or know
if (result > PCGreatestUid || CurrentPCBox == null || box != CurrentPCBoxId)
{
return -1;
}
return result;
}
public bool DepositPokemonToPC(int pokemonUid)
{
if (!IsPCOpen || pokemonUid < 1 || pokemonUid > 6 || Team.Count < pokemonUid)
{
return false;
}
SendMovePokemonToPC(pokemonUid);
return true;
}
public bool WithdrawPokemonFromPC(int boxId, int boxPokemonId)
{
int pcPokemonUid = GetPokemonPCUid(boxId, boxPokemonId);
if (pcPokemonUid == -1)
{
return false;
}
if (!IsPCOpen || pcPokemonUid < 7 || pcPokemonUid > PCGreatestUid || Team.Count >= 6)
{
return false;
}
SendMovePokemonFromPC(pcPokemonUid, Team.Count + 1);
return true;
}
public bool SwapPokemonFromPC(int boxId, int boxPokemonId, int teamPokemonUid)
{
int pcPokemonUid = GetPokemonPCUid(boxId, boxPokemonId);
if (pcPokemonUid == -1)
{
return false;
}
if (!IsPCOpen || pcPokemonUid < 7 || pcPokemonUid > PCGreatestUid
|| teamPokemonUid < 1 || teamPokemonUid > 6 || Team.Count < teamPokemonUid)
{
return false;
}
SendMovePokemonFromPC(pcPokemonUid, teamPokemonUid);
return true;
}
public bool SwapPokemon(int pokemon1, int pokemon2)
{
if (IsInBattle || pokemon1 < 1 || pokemon2 < 1 || Team.Count < pokemon1 || Team.Count < pokemon2 || pokemon1 == pokemon2)
{
return false;
}
if (!_swapTimeout.IsActive)
{
SendSwapPokemons(pokemon1, pokemon2);
_swapTimeout.Set();
return true;
}
return false;
}
public void Move(Direction direction)
{
_movements.Add(direction);
}
public void RequestResync()
{
SendMessage("/syn");
_teleportationTimeout.Set();
}
public void UseAttack(int number)
{
SendAttack(number.ToString());
_battleTimeout.Set();
}
public void UseItem(int id, int pokemonUid = 0)
{
if (!(pokemonUid >= 0 && pokemonUid <= 6) || !HasItemId(id))
{
return;
}
InventoryItem item = GetItemFromId(id);
if (item == null || item.Quantity == 0)
{
return;
}
if (pokemonUid == 0) // simple use
{
if (!_itemUseTimeout.IsActive && !IsInBattle && (item.Scope == 8 || item.Scope == 10 || item.Scope == 15))
{
SendUseItem(id);
_itemUseTimeout.Set();
}
else if (!_battleTimeout.IsActive && IsInBattle && item.Scope == 5)
{
SendAttack("item" + id);
_battleTimeout.Set();
}
}
else // use item on pokemon
{
if (!_itemUseTimeout.IsActive && !IsInBattle
&& (item.Scope == 2 || item.Scope == 3 || item.Scope == 9
|| item.Scope == 13 || item.Scope == 14))
{
SendUseItem(id, pokemonUid);
_itemUseTimeout.Set();
}
else if (!_battleTimeout.IsActive && IsInBattle && item.Scope == 2)
{
SendAttack("item" + id + ":" + pokemonUid);
_battleTimeout.Set();
}
}
}
public bool GiveItemToPokemon(int pokemonUid, int itemId)
{
if (!(pokemonUid >= 1 && pokemonUid <= Team.Count))
{
return false;
}
InventoryItem item = GetItemFromId(itemId);
if (item == null || item.Quantity == 0)
{
return false;
}
if (!_itemUseTimeout.IsActive && !IsInBattle
&& (item.Scope == 2 || item.Scope == 3 || item.Scope == 9 || item.Scope == 13
|| item.Scope == 14 || item.Scope == 5 || item.Scope == 12 || item.Scope == 6))
{
SendGiveItem(pokemonUid, itemId);
_itemUseTimeout.Set();
return true;
}
return false;
}
public bool TakeItemFromPokemon(int pokemonUid)
{
if (!(pokemonUid >= 1 && pokemonUid <= Team.Count))
{
return false;
}
if (!_itemUseTimeout.IsActive && Team[pokemonUid - 1].ItemHeld != "")
{
SendTakeItem(pokemonUid);
_itemUseTimeout.Set();
return true;
}
return false;
}
public bool HasSurfAbility()
{
return (HasMove("Surf") || WaterMount != null) &&
(Map.Region == "1" && HasItemName("Soul Badge") ||
Map.Region == "2" && HasItemName("Fog Badge") ||
Map.Region == "3" && HasItemName("Balance Badge") ||
Map.Region == "4" && HasItemName("Relic Badge"));
}
public bool HasCutAbility()
{
return (HasMove("Cut") || HasTreeaxe()) &&
(Map.Region == "1" && HasItemName("Cascade Badge") ||
Map.Region == "2" && HasItemName("Hive Badge") ||
Map.Region == "3" && HasItemName("Stone Badge") ||
Map.Region == "4" && HasItemName("Forest Badge"));
}
public bool HasRockSmashAbility()
{
return HasMove("Rock Smash") || HasPickaxe();
}
public bool HasTreeaxe()
{
return HasItemId(838) && HasItemId(317);
}
public bool HasPickaxe()
{
return HasItemId(839);
}
public bool PokemonUidHasMove(int pokemonUid, string moveName)
{
return Team.FirstOrDefault(p => p.Uid == pokemonUid)?.Moves.Any(m => m.Name?.Equals(moveName, StringComparison.InvariantCultureIgnoreCase) ?? false) ?? false;
}
public bool HasMove(string moveName)
{
return Team.Any(p => p.Moves.Any(m => m.Name?.Equals(moveName, StringComparison.InvariantCultureIgnoreCase) ?? false));
}
public int GetMovePosition(int pokemonUid, string moveName)
{
return Team[pokemonUid].Moves.FirstOrDefault(m => m.Name?.Equals(moveName, StringComparison.InvariantCultureIgnoreCase) ?? false)?.Position ?? -1;
}
public InventoryItem GetItemFromId(int id)
{
return Items.FirstOrDefault(i => i.Id == id && i.Quantity > 0);
}
public bool HasItemId(int id)
{
return GetItemFromId(id) != null;
}
public InventoryItem GetItemFromName(string itemName)
{
return Items.FirstOrDefault(i => i.Name.Equals(itemName, StringComparison.InvariantCultureIgnoreCase) && i.Quantity > 0);
}
public bool HasItemName(string itemName)
{
return GetItemFromName(itemName) != null;
}
public bool HasPokemonInTeam(string pokemonName)
{
return FindFirstPokemonInTeam(pokemonName) != null;
}
public Pokemon FindFirstPokemonInTeam(string pokemonName)
{
return Team.FirstOrDefault(p => p.Name.Equals(pokemonName, StringComparison.InvariantCultureIgnoreCase));
}
public void UseSurf()
{
if (WaterMount == null)
{
SendMessage("/surf");
}
else
{
LogMessage?.Invoke($"Mounting [{WaterMount.Name}]");
UseItem(WaterMount.Id);
}
_mountingTimeout.Set();
}
public void UseSurfAfterMovement()
{
_surfAfterMovement = true;
}
public void RunFromBattle()
{
UseAttack(5);
}
public void ChangePokemon(int number)
{
UseAttack(number + 5);
}
public void TalkToNpc(Npc npc)
{
npc.CanBattle = false;
SendTalkToNpc(npc.Id);
_dialogTimeout.Set();
}
public bool OpenPC()
{
if (!Map.IsPC(PlayerX, PlayerY - 1))
{
return false;
}
IsPCOpen = true;
return RefreshPCBox(1);
}
public void PushDialogAnswer(int index)
{
_dialogResponses.Enqueue(index);
}
public void PushDialogAnswer(string text)
{
_dialogResponses.Enqueue(text);
}
public bool BuyItem(int itemId, int quantity)
{
if (OpenedShop != null && OpenedShop.Items.Any(item => item.Id == itemId))
{
_itemUseTimeout.Set();
SendShopPokemart(OpenedShop.Id, itemId, quantity);
return true;
}
return false;
}
public bool PurchaseMove(string moveName)
{
if (MoveRelearner != null && MoveRelearner.Moves.Any(move => move.Name == moveName.ToLowerInvariant()))
{
_moveRelearnerTimeout.Set();
SendPurchaseMove(MoveRelearner.SelectedPokemonUid, moveName);
return true;
}
return false;
}
private void SendPurchaseMove(int pokemonUid, string moveName)
{
// DSSock.cs handles Move Relearn as below.
if (MoveRelearner != null)
{
if (!MoveRelearner.IsEgg)
{
SendPacket("z|.|" + pokemonUid + "|.|" + moveName);
}
else
{
int moveId = MovesManager.Instance.GetMoveId(moveName);
if (moveId != -1)
{
SendPacket("b|.|" + pokemonUid + "|.|" + moveId);
}
}
}
}
private void MapClient_ConnectionOpened()