Permalink
Browse files

Initial release

  • Loading branch information...
1 parent 2b24a22 commit a508a6cbd836bad553d8f89f0bb823ec56536e49 @Silv3rPRO committed Jun 23, 2016
Showing with 7,293 additions and 0 deletions.
  1. +208 −0 PWOBot/BattleAI.cs
  2. +162 −0 PWOBot/BotClient.cs
  3. BIN PWOBot/KeraLua.dll
  4. BIN PWOBot/NLua.dll
  5. +73 −0 PWOBot/PWOBot.csproj
  6. +246 −0 PWOBot/Pathfinding.cs
  7. +36 −0 PWOBot/Properties/AssemblyInfo.cs
  8. +304 −0 PWOBot/Script.cs
  9. BIN PWOBot/lua52.dll
  10. +12 −0 PWOProtocol/AuthenticationResult.cs
  11. +90 −0 PWOProtocol/Battle.cs
  12. +14 −0 PWOProtocol/ChatChannel.cs
  13. +60 −0 PWOProtocol/Direction.cs
  14. +737 −0 PWOProtocol/GameClient.cs
  15. +214 −0 PWOProtocol/GameConnection.cs
  16. +23 −0 PWOProtocol/HardwareHash.cs
  17. +21 −0 PWOProtocol/InventoryItem.cs
  18. +109 −0 PWOProtocol/Map.cs
  19. +161 −0 PWOProtocol/MapLoader.cs
  20. +26 −0 PWOProtocol/Npc.cs
  21. +76 −0 PWOProtocol/PWOProtocol.csproj
  22. +68 −0 PWOProtocol/Pokemon.cs
  23. +24 −0 PWOProtocol/PokemonMove.cs
  24. +798 −0 PWOProtocol/PokemonNamesManager.cs
  25. +75 −0 PWOProtocol/PokemonType.cs
  26. +36 −0 PWOProtocol/Properties/AssemblyInfo.cs
  27. +46 −0 PWOProtocol/Rc4Encryption.cs
  28. +141 −0 PWOProtocol/SocksConnection.cs
  29. +30 −0 PWOProtocol/Timeout.cs
  30. +1,741 −0 PWOProtocol/TypesManager.cs
  31. BIN PWOProtocol/unrar.dll
  32. +34 −0 PWOSparkle.sln
  33. +6 −0 PWOSparkle/App.config
  34. +9 −0 PWOSparkle/App.xaml
  35. +24 −0 PWOSparkle/App.xaml.cs
  36. BIN PWOSparkle/Assets/logo-png/128.png
  37. BIN PWOSparkle/Assets/logo-png/16.png
  38. BIN PWOSparkle/Assets/logo-png/32.png
  39. BIN PWOSparkle/Assets/logo-png/48.png
  40. BIN PWOSparkle/Assets/logo-png/64.png
  41. BIN PWOSparkle/Assets/logo.ico
  42. +17 −0 PWOSparkle/ChatPanel.xaml
  43. +12 −0 PWOSparkle/ChatPanel.xaml.cs
  44. +17 −0 PWOSparkle/ChatView.xaml
  45. +227 −0 PWOSparkle/ChatView.xaml.cs
  46. +39 −0 PWOSparkle/InventoryView.xaml
  47. +34 −0 PWOSparkle/InventoryView.xaml.cs
  48. +49 −0 PWOSparkle/LoginWindow.xaml
  49. +131 −0 PWOSparkle/LoginWindow.xaml.cs
  50. +73 −0 PWOSparkle/MainWindow.xaml
  51. +467 −0 PWOSparkle/MainWindow.xaml.cs
  52. +159 −0 PWOSparkle/PWOSparkle.csproj
  53. +57 −0 PWOSparkle/Properties/AssemblyInfo.cs
  54. +71 −0 PWOSparkle/Properties/Resources.Designer.cs
  55. +117 −0 PWOSparkle/Properties/Resources.resx
  56. +30 −0 PWOSparkle/Properties/Settings.Designer.cs
  57. +7 −0 PWOSparkle/Properties/Settings.settings
  58. +30 −0 PWOSparkle/TeamView.xaml
  59. +92 −0 PWOSparkle/TeamView.xaml.cs
  60. BIN PWOSparkle/logo.ico
  61. BIN PWOSparkle/unrar.dll
  62. +30 −0 Scripts/Johto/Leveling/Route 30.lua
  63. +30 −0 Scripts/Kanto/Leveling/Route 22.lua
View
@@ -0,0 +1,208 @@
+using PWOProtocol;
+
+namespace PWOBot
+{
+ public class BattleAI
+ {
+ private GameClient _client;
+
+ public BattleAI(GameClient client)
+ {
+ _client = client;
+ }
+
+ public int UsablePokemonsCount
+ {
+ get
+ {
+ int usablePokemons = 0;
+ foreach (Pokemon pokemon in _client.Team)
+ {
+ if (IsPokemonUsable(pokemon))
+ {
+ usablePokemons += 1;
+ }
+ }
+ return usablePokemons;
+ }
+ }
+
+ public Pokemon ActivePokemon
+ {
+ get
+ {
+ return _client.Team[_client.Battle.ActiveIndex];
+ }
+ }
+
+ public bool Attack(bool useBestAttack = true)
+ {
+ if (UsePreconditions())
+ {
+ return true;
+ }
+ if (IsPokemonUsable(ActivePokemon) && UseAttack(useBestAttack))
+ {
+ return true;
+ }
+ if (SendNextPokemon())
+ {
+ return true;
+ }
+ if (_client.Battle.IsWild && Run())
+ {
+ return true;
+ }
+ return false;
+ }
+
+ public bool UseMove(string moveName)
+ {
+ if (UsePreconditions())
+ {
+ return true;
+ }
+ for (int i = 0; i < ActivePokemon.Moves.Length; ++i)
+ {
+ PokemonMove move = ActivePokemon.Moves[i];
+ if (move.CurrentPoints > 0)
+ {
+ if (move.Name.ToUpperInvariant() == moveName)
+ {
+ _client.Attack(i + 1);
+ return true;
+ }
+ }
+ }
+ return Attack();
+ }
+
+ public bool Run()
+ {
+ if (UsePreconditions())
+ {
+ return true;
+ }
+ if (!_client.Battle.IsWild)
+ {
+ return Attack();
+ }
+ _client.RunFromBattle();
+ return true;
+ }
+
+ private bool UsePreconditions()
+ {
+ if (ActivePokemon.CurrentHealth == 0)
+ {
+ return SendNextPokemon();
+ }
+ return false;
+ }
+
+ private bool UseAttack(bool useBestAttack)
+ {
+ PokemonMove bestMove = null;
+ int bestIndex = 0;
+ double bestPower = 0;
+
+ PokemonMove worstMove = null;
+ int worstIndex = 0;
+ double worstPower = 0;
+
+ for (int i = 0; i < ActivePokemon.Moves.Length; ++i)
+ {
+ PokemonMove move = ActivePokemon.Moves[i];
+ if (move.CurrentPoints > 0)
+ {
+ if (IsMoveOffensive(move))
+ {
+ PokemonType attackType = move.Type;
+
+ PokemonType playerType1 = TypesManager.Instance.Type1[ActivePokemon.PokedexId];
+ PokemonType playerType2 = TypesManager.Instance.Type2[ActivePokemon.PokedexId];
+
+ PokemonType opponentType1 = TypesManager.Instance.Type1[_client.Battle.OpponentPokedexId];
+ PokemonType opponentType2 = TypesManager.Instance.Type2[_client.Battle.OpponentPokedexId];
+
+ double accuracy = (move.Accuracy < 0 ? 101.0 : move.Accuracy);
+
+ double power = move.Power * accuracy;
+
+ if (attackType == playerType1 || attackType == playerType2)
+ {
+ power *= 1.5;
+ }
+
+ power *= TypesManager.Instance.GetMultiplier(attackType, opponentType1);
+ power *= TypesManager.Instance.GetMultiplier(attackType, opponentType2);
+
+ if (power > 0)
+ {
+ if (bestMove == null || power > bestPower)
+ {
+ bestMove = move;
+ bestPower = power;
+ bestIndex = i;
+ }
+
+ if (worstMove == null || power < worstPower)
+ {
+ worstMove = move;
+ worstPower = power;
+ worstIndex = i;
+ }
+ }
+ }
+ }
+ }
+
+ if (useBestAttack && bestMove != null)
+ {
+ _client.Attack(bestIndex + 1);
+ return true;
+ }
+ if (!useBestAttack && worstMove != null)
+ {
+ _client.Attack(worstIndex + 1);
+ return true;
+ }
+ return false;
+ }
+
+ public bool SendNextPokemon()
+ {
+ int i = 0;
+ foreach (Pokemon pokemon in _client.Team)
+ {
+ if (IsPokemonUsable(pokemon) && pokemon != ActivePokemon)
+ {
+ _client.SwitchPokemon(i);
+ return true;
+ }
+ ++i;
+ }
+ return false;
+ }
+
+ public bool IsPokemonUsable(Pokemon pokemon)
+ {
+ if (pokemon.CurrentHealth > 0)
+ {
+ foreach (PokemonMove move in pokemon.Moves)
+ {
+ if (move.CurrentPoints > 0 && IsMoveOffensive(move))
+ {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ private bool IsMoveOffensive(PokemonMove move)
+ {
+ return move.Power > 0;
+ }
+ }
+}
View
@@ -0,0 +1,162 @@
+using PWOProtocol;
+using System;
+using System.IO;
+using System.Text;
+
+namespace PWOBot
+{
+ public class BotClient
+ {
+ public GameClient Game { get; private set; }
+ public BattleAI AI { get; private set; }
+ public Script Script { get; private set; }
+
+ public bool Running { get; private set; }
+
+ public event Action<bool> StateChanged;
+ public event Action<string> LogMessage;
+
+ private Timeout _botTimeout = new Timeout();
+ private int _lastMovementSourceX;
+ private int _lastMovementSourceY;
+ private int _lastMovementDestinationX;
+ private int _lastMovementDestinationY;
+ private bool _requestedResync;
+
+ public void Update()
+ {
+ if (!Running)
+ {
+ return;
+ }
+
+ if (Game.IsAuthenticated && Game.IsInitialized && Game.IsInactive)
+ {
+ ExecuteNextAction();
+ }
+ }
+
+ public void Start()
+ {
+ if (Game != null && !Running && Script != null)
+ {
+ Running = true;
+ StateChanged?.Invoke(Running);
+ }
+ }
+
+ public void Stop()
+ {
+ if (Running)
+ {
+ Running = false;
+ StateChanged?.Invoke(Running);
+ }
+ }
+
+ public void SetClient(GameClient client)
+ {
+ Game = client;
+ AI = null;
+ Stop();
+
+ if (client != null)
+ {
+ AI = new BattleAI(client);
+ client.BattleEnded += Client_BattleEnded;
+ }
+ }
+
+ public void LoadScript(string filename)
+ {
+ string oldDirectory = Environment.CurrentDirectory;
+ Environment.CurrentDirectory = new FileInfo(filename).Directory.FullName;
+ Script = new Script(this, File.ReadAllText(filename, Encoding.UTF8));
+ Script.ScriptMessage += Script_ScriptMessage;
+ Script.Initialize();
+ Environment.CurrentDirectory = oldDirectory;
+ }
+
+ public bool MoveToCell(int x, int y, int requiredDistance = 0)
+ {
+ if (_lastMovementSourceX == Game.PlayerX && _lastMovementSourceY == Game.PlayerY
+ && _lastMovementDestinationX == x && _lastMovementDestinationY == y)
+ {
+ if (_requestedResync)
+ {
+ LogMessage?.Invoke("Bot stuck: stopping the script");
+ Stop();
+ }
+ else
+ {
+ LogMessage?.Invoke("Bot stuck: requesting synchronization");
+ _requestedResync = true;
+ Game.SendResyncRequest();
+ }
+ return false;
+ }
+
+ Pathfinding path = new Pathfinding(Game);
+ bool result;
+
+ if (Game.PlayerX == x && Game.PlayerY == y)
+ {
+ result = path.MoveToSameCell();
+ }
+ else
+ {
+ result = path.MoveTo(x, y, requiredDistance);
+ }
+
+ if (result)
+ {
+ _lastMovementSourceX = Game.PlayerX;
+ _lastMovementSourceY = Game.PlayerY;
+ _lastMovementDestinationX = x;
+ _lastMovementDestinationY = y;
+ _requestedResync = false;
+ }
+
+ return result;
+ }
+
+ private void ExecuteNextAction()
+ {
+ bool executed = false;
+ if (Game.IsInBattle)
+ {
+ executed = Script.ExecuteBattleAction();
+ }
+ else
+ {
+ executed = Script.ExecutePathAction();
+ }
+ _botTimeout.Update();
+ if (!executed && !_botTimeout.IsActive)
+ {
+ LogMessage?.Invoke("No action executed: stopping the bot");
+ Stop();
+ }
+ else
+ {
+ _botTimeout.Set();
+ }
+ }
+
+ private void Script_ScriptMessage(string message)
+ {
+ LogMessage?.Invoke(message);
+ }
+
+ private void Client_BattleEnded()
+ {
+ ResetResync();
+ }
+
+ private void ResetResync()
+ {
+ _requestedResync = false;
+ _lastMovementSourceX = -1;
+ }
+ }
+}
View
Binary file not shown.
View
Binary file not shown.
Oops, something went wrong.

0 comments on commit a508a6c

Please sign in to comment.