troptrop

Flaaffy Bot Code

Jul 6th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5.  
  6. public class GoldRattataTASState
  7. {
  8.  
  9. public string Log;
  10. public GscTile Tile;
  11. public int EdgeSet;
  12. public int WastedFrames;
  13. public Action BlockedActions;
  14. public byte HRandomAdd;
  15. public byte HRandomSub;
  16. public byte RDiv;
  17.  
  18. public override int GetHashCode()
  19. {
  20.  
  21. var hash = new HashCode();
  22. hash.Add(Tile.X);
  23. hash.Add(Tile.Y);
  24. hash.Add(EdgeSet);
  25. hash.Add(WastedFrames);
  26. hash.Add(BlockedActions);
  27. hash.Add(HRandomAdd);
  28. hash.Add(HRandomSub);
  29. hash.Add(RDiv);
  30. return hash.ToHashCode();
  31. }
  32. }
  33.  
  34. // Code heavily plagiarized from: https://github.com/entrpntr/gb-rta-bruteforce/blob/master/src/dabomstew/rta/entei/GSToto.java
  35. public static class GoldRattataTAS
  36. {
  37.  
  38. const int MaxCost = 300;
  39. static StreamWriter Writer;
  40. public static HashSet<int> seenStates = new HashSet<int>();
  41.  
  42. public static void OverworldSearch(Gsc gb, GoldRattataTASState state)
  43. {
  44. if (!seenStates.Add(state.GetHashCode()))
  45. {
  46. return;
  47. }
  48. byte[] oldState = gb.SaveState();
  49.  
  50. foreach (Edge<GscTile> edge in state.Tile.Edges[state.EdgeSet])
  51. {
  52. gb.LoadState(oldState);
  53.  
  54. if (edge.Cost + state.WastedFrames > MaxCost) continue;
  55.  
  56. if ((state.BlockedActions & edge.Action) > 0) continue;
  57.  
  58. int ret = gb.Execute(edge.Action);
  59.  
  60. if (ret == gb.SYM["DoPlayerMovement.BumpSound"])
  61. {
  62. continue;
  63. }
  64.  
  65.  
  66. if (ret == 681765) //wild encounter
  67. {
  68. // Console.WriteLine("encounter");
  69.  
  70. gb.Hold(Joypad.B, gb.SYM["CalcMonStats"]);
  71.  
  72. if(gb.CpuRead("wEnemyMonSpecies") == gb.Species["FLAAFFY"].Id && gb.CpuRead("wEnemyMonLevel") == 17){
  73.  
  74. Console.WriteLine("Flaaffy Encounter");
  75. // gb.Hold(Joypad.B, gb.SYM["GetOpponentItem"]);
  76.  
  77. int dvs = gb.CpuRead("wEnemyMonDVs") << 8 | gb.CpuRead(gb.SYM["wEnemyMonDVs"] + 1);
  78.  
  79. //int hp = (((dvs >> 9) & 8) | ((dvs >> 6) & 4) | ((dvs >> 3) & 2) | (dvs & 1)) & 0xf;
  80. int atk = (dvs >> 12) & 0xf;
  81. int def = (dvs >> 8) & 0xf;
  82. int spd = (dvs >> 4) & 0xf;
  83. int spc = dvs & 0xf;
  84.  
  85. var statcheck = "";
  86.  
  87.  
  88. int item = gb.CpuRead("wEnemyMonItem") << 8 | gb.CpuRead(gb.SYM["wEnemyMonItem"] + 1);
  89.  
  90. if((spc == 15) && (spd >= 10)){
  91.  
  92. var foundSeadra = $"[{state.WastedFrames} cost] {state.Log}{edge.Action.LogString()} - 0x{dvs:x4}";
  93.  
  94.  
  95. Writer.WriteLine(foundSeadra);
  96. Writer.Flush();
  97. Console.WriteLine(foundSeadra);
  98. continue;
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. }
  107. else{
  108. continue;
  109. }
  110.  
  111.  
  112. }
  113.  
  114.  
  115.  
  116. if(ret == 12770){
  117. continue;
  118. }
  119.  
  120.  
  121. Action blockedActions = state.BlockedActions;
  122.  
  123. if ((edge.Action & Action.A) > 0)
  124. blockedActions |= Action.A;
  125. else
  126. blockedActions &= ~(Action.A);
  127.  
  128. OverworldSearch(gb, new GoldRattataTASState
  129. {
  130. Log = state.Log + edge.Action.LogString() + " ",
  131. Tile = edge.NextTile,
  132. EdgeSet = edge.NextEdgeset,
  133. WastedFrames = state.WastedFrames + edge.Cost,
  134. BlockedActions = blockedActions,
  135. HRandomAdd = gb.CpuRead("hRandomAdd"),
  136. HRandomSub = gb.CpuRead("hRandomSub"),
  137. RDiv = gb.CpuRead(0xFF04)
  138. });
  139. gb.LoadState(oldState);
  140. }
  141. }
  142.  
  143. public static void StartSearch(int numThreads = 4)
  144. {
  145. Silver dummyGb = new Silver();
  146.  
  147. GscMap mtMortar = dummyGb.Maps["BurnedTowerB1F"];
  148. GscMap route17 = dummyGb.Maps["Route17"];
  149. GscMap route35Map = dummyGb.Maps["Route42"];
  150.  
  151. GscTile startTile = route35Map[46, 10];
  152.  
  153.  
  154.  
  155. for (int j = 0; j <= 7; j++)
  156. {
  157. for(int i = 0; i < 3; i++){
  158. route35Map[46 + j, 10 + i].AddEdge(0, new Edge<GscTile>() { Action = Action.Down, NextTile = route35Map[46 + j, 11 + i], NextEdgeset = 0, Cost = 8 });
  159. route35Map[46 + j, 10 + i].AddEdge(0, new Edge<GscTile>() { Action = Action.Down | Action.A, NextTile = route35Map[46 + j, 11 + i], NextEdgeset = 0, Cost = 8 });
  160. route35Map[46 + j, 11 + i].AddEdge(0, new Edge<GscTile>() { Action = Action.Up, NextTile = route35Map[46 + j, 10 + i], NextEdgeset = 0, Cost = 8 });
  161. route35Map[46 + j, 11 + i].AddEdge(0, new Edge<GscTile>() { Action = Action.Up | Action.A, NextTile = route35Map[46 + j, 10 + i], NextEdgeset = 0, Cost = 8 });
  162. }
  163. }
  164.  
  165. for (int j = 0; j < 7; j++)
  166. {
  167. for(int i = 0; i <= 3; i++){
  168. route35Map[46 + j, 10 + i].AddEdge(0, new Edge<GscTile>() { Action = Action.Right, NextTile = route35Map[47 + j, 10 + i], NextEdgeset = 0, Cost = 8 });
  169. route35Map[46 + j, 10 + i].AddEdge(0, new Edge<GscTile>() { Action = Action.Right | Action.A, NextTile = route35Map[47 + j, 10 + i], NextEdgeset = 0, Cost = 8 });
  170. route35Map[47 + j, 10 + i].AddEdge(0, new Edge<GscTile>() { Action = Action.Left, NextTile = route35Map[46 + j, 10 + i], NextEdgeset = 0, Cost = 8 });
  171. route35Map[47 + j, 10 + i].AddEdge(0, new Edge<GscTile>() { Action = Action.Left | Action.A, NextTile = route35Map[46 + j, 10 + i], NextEdgeset = 0, Cost = 8 });
  172. }
  173. }
  174.  
  175. // mtMortar.Sprites.Remove(7,3);
  176. // mtMortar.Sprites.Remove(10,4);
  177. // mtMortar.Sprites.Remove(12,3);
  178. Pathfinding.DebugDrawEdges(route35Map, 0);
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. dummyGb.Dispose();
  186. Writer = new StreamWriter("gold_rattata_tas" + DateTime.Now.Ticks + ".txt");
  187.  
  188. for (int threadIndex = 0; threadIndex < numThreads; threadIndex++)
  189. {
  190. new Thread(parameter => {
  191. int index = (int)parameter;
  192. Silver gb = new Silver();
  193. gb.SetRTCOffset(-69);
  194. Console.WriteLine("starting movie");
  195.  
  196. gb.LoadStateBiz("basesaves/Core.bin", 1);
  197. Console.WriteLine("finished movie");
  198. gb.Hold(Joypad.B, "OWPlayerInput");
  199. //gb.Show();
  200. /*for (int i = 0; i < index; i++)
  201. {
  202. gb.AdvanceFrame();
  203. gb.Hold(Joypad.B, "OWPlayerInput");
  204. }*/
  205. //gb.Record("test");
  206. /*var startrng = (gb.CpuRead("hRandomAdd") << 8) | gb.CpuRead("hRandomSub");
  207. var startrdiv = gb.CpuRead(0xFF04);
  208. var startrngframe = gb.CpuRead("hVBlankCounter");
  209. Console.WriteLine($"0x{startrng:x4}");
  210. Console.WriteLine($"0x{startrdiv:x2}");
  211. Console.WriteLine($"0x{startrngframe:x2}");*/
  212.  
  213.  
  214. OverworldSearch(gb, new GoldRattataTASState
  215. {
  216. Log = $"thread {index} ",
  217. Tile = startTile,
  218. WastedFrames = 0,
  219. EdgeSet = 0,
  220. BlockedActions = Action.A,
  221. HRandomAdd = gb.CpuRead("hRandomAdd"),
  222. HRandomSub = gb.CpuRead("hRandomSub"),
  223. RDiv = gb.CpuRead(0xFF04)
  224. });
  225. }).Start(threadIndex);
  226. }
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment