Advertisement
Guest User

InfiniteHatcher.c

a guest
Jun 7th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.82 KB | None | 0 0
  1. /*
  2. Pokemon Sword & Shield Fast Egg Hatching
  3. Logic by kirbunkle aka Adam Hanny
  4. Based on the LUFA library's Low-Level Joystick Demo
  5.     (C) Dean Camera
  6. Based on the HORI's Pokken Tournament Pro Pad design
  7.     (C) HORI
  8. This project implements a modified version of HORI's Pokken Tournament Pro Pad
  9. USB descriptors to allow for the creation of custom controllers for the
  10. Nintendo Switch. This also works to a limited degree on the PS3.
  11. Since System Update v3.0.0, the Nintendo Switch recognizes the Pokken
  12. Tournament Pro Pad as a Pro Controller. Physical design limitations prevent
  13. the Pokken Controller from functioning at the same level as the Pro
  14. Controller. However, by default most of the descriptors are there, with the
  15. exception of Home and Capture. Descriptor modification allows us to unlock
  16. these buttons for our use.
  17. */
  18.  
  19. #include "../Joystick.h"
  20. #include "Commands.h"
  21. #include "Config.h"
  22.  
  23. /*------------------------------------------*/
  24. // INSTRUCTIONS
  25. // -> Your player must be on their bicycle in Bridge Field, no menus or dialogues open
  26. // -> You must have ONLY 1 pokemon in your party
  27. // -> The pokemon in your party needs to have Flame Body, Magma Armor, or Steam Engine
  28. // -> You must be disconnected from the internet
  29. // -> Your cursor must be over "Town Map" in the main menu (game remembers its position)
  30. // -> Your main menu must be the default without menu items moved around!
  31. // -> If they are moved, "Town Map" needs to be 1 down and 1 left from "Pokemon" in the main menu
  32. // -> You must have a series of EMPTY boxes that you want to fill, and you must have selected the
  33. //    first empty box in the series (game remembers which box you had open last)
  34. // -> You have to start this program at the Change Grip/Order menu
  35.  
  36. /*------------------------------------------*/
  37. // NOTES
  38. // -> This program does not guarantee picking up the maximum number of eggs per cycle due to the
  39. //    RNG for collecting eggs. If you have the oval charm, and the pokemon are same species and different
  40. //    trainers, there is an 88% chance per pick-up that an egg will be available. This means even given
  41. //    the best setup, you will likely only hatch ~9 eggs per two cycles, and not 10. The bot cannot
  42. //    know if we pick up an egg or not, so we assume that we always get an egg. You will most definitely
  43. //    have some empty spaces in your boxes after running this for a while, but it can't be helped.
  44. // -> For reasons mentioned above, it is highly recommended that you have the oval charm.
  45. // -> Adjust the value for NUMBER_OF_BOXES_TO_FILL below to the number of sequential empty boxes you
  46. //    have available.
  47. // -> The table below shows the number of steps to hatch an egg, and then the recommended settings based on
  48. //    that information. If you find eggs in your boxes, or the bot gets desynced, you may want to increase
  49. //    the HATCHING_TIME_SEC value below.
  50.  
  51. /*------------------------------------------*/
  52. // RECOMMENDED SETTINGS
  53. // -> You will want to find the number of steps it takes for your pokemon to hatch, that information
  54. //    can be found here: https://bulbapedia.bulbagarden.net/wiki/List_of_Pok%C3%A9mon_by_base_Egg_cycles
  55. // -> 5120 steps - HATCHING_TIME_SEC = 145, MAX_NUM_OF_EGGS = 5 (time to fill a box ~29 mins)
  56.  
  57. /* #define HATCHING_TIME_SEC 145      // seconds for hatching */
  58. #define MAX_NUM_OF_EGGS 5          // number of eggs to hold before hatching
  59. #define HATCHING_TIME_SEC 49
  60.  
  61. // Main entry point.
  62. int main(void) {
  63.     // We'll start by performing hardware and peripheral setup.
  64.     SetupHardware();
  65.     // We'll then enable global interrupts for our use.
  66.     GlobalInterruptEnable();
  67.     // Once that's done, we'll enter an infinite loop.
  68.     for (;;)
  69.     {
  70.         // We need to run our task to process and deliver data for our IN and OUT endpoints.
  71.         HID_Task();
  72.         // We also need to run the main USB management task.
  73.         USB_USBTask();
  74.     }
  75. }
  76.  
  77. // Configures hardware and peripherals, such as the USB peripherals.
  78. void SetupHardware(void) {
  79.     // We need to disable watchdog if enabled by bootloader/fuses.
  80.     MCUSR &= ~(1 << WDRF);
  81.     wdt_disable();
  82.  
  83.     // We need to disable clock division before initializing the USB hardware.
  84.     //clock_prescale_set(clock_div_1);
  85.     // We can then initialize our hardware and peripherals, including the USB stack.
  86.  
  87.     #ifdef ALERT_WHEN_DONE
  88.     // Both PORTD and PORTB will be used for the optional LED flashing and buzzer.
  89.     #warning LED and Buzzer functionality enabled. All pins on both PORTB and \
  90. PORTD will toggle when printing is done.
  91.     DDRD  = 0xFF; //Teensy uses PORTD
  92.     PORTD =  0x0;
  93.                   //We'll just flash all pins on both ports since the UNO R3
  94.     DDRB  = 0xFF; //uses PORTB. Micro can use either or, but both give us 2 LEDs
  95.     PORTB =  0x0; //The ATmega328P on the UNO will be resetting, so unplug it?
  96.     #endif
  97.     // The USB stack should be initialized last.
  98.     USB_Init();
  99. }
  100.  
  101. // Fired to indicate that the device is enumerating.
  102. void EVENT_USB_Device_Connect(void) {
  103.     // We can indicate that we're enumerating here (via status LEDs, sound, etc.).
  104. }
  105.  
  106. // Fired to indicate that the device is no longer connected to a host.
  107. void EVENT_USB_Device_Disconnect(void) {
  108.     // We can indicate that our device is not ready (via status LEDs, sound, etc.).
  109. }
  110.  
  111. // Fired when the host set the current configuration of the USB device after enumeration.
  112. void EVENT_USB_Device_ConfigurationChanged(void) {
  113.     bool ConfigSuccess = true;
  114.  
  115.     // We setup the HID report endpoints.
  116.     ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_OUT_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
  117.     ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_IN_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
  118.  
  119.     // We can read ConfigSuccess to indicate a success or failure at this point.
  120. }
  121.  
  122. // Process control requests sent to the device from the USB host.
  123. void EVENT_USB_Device_ControlRequest(void) {
  124.     // We can handle two control requests: a GetReport and a SetReport.
  125.  
  126.     // Not used here, it looks like we don't receive control request from the Switch.
  127. }
  128.  
  129. // Process and deliver data from IN and OUT endpoints.
  130. void HID_Task(void) {
  131.     // If the device isn't connected and properly configured, we can't do anything here.
  132.     if (USB_DeviceState != DEVICE_STATE_Configured)
  133.         return;
  134.  
  135.     // We'll start with the OUT endpoint.
  136.     Endpoint_SelectEndpoint(JOYSTICK_OUT_EPADDR);
  137.     // We'll check to see if we received something on the OUT endpoint.
  138.     if (Endpoint_IsOUTReceived())
  139.     {
  140.         // If we did, and the packet has data, we'll react to it.
  141.         if (Endpoint_IsReadWriteAllowed())
  142.         {
  143.             // We'll create a place to store our data received from the host.
  144.             USB_JoystickReport_Output_t JoystickOutputData;
  145.             // We'll then take in that data, setting it up in our storage.
  146.             while(Endpoint_Read_Stream_LE(&JoystickOutputData, sizeof(JoystickOutputData), NULL) != ENDPOINT_RWSTREAM_NoError);
  147.             // At this point, we can react to this data.
  148.  
  149.             // However, since we're not doing anything with this data, we abandon it.
  150.         }
  151.         // Regardless of whether we reacted to the data, we acknowledge an OUT packet on this endpoint.
  152.         Endpoint_ClearOUT();
  153.     }
  154.  
  155.     // We'll then move on to the IN endpoint.
  156.     Endpoint_SelectEndpoint(JOYSTICK_IN_EPADDR);
  157.     // We first check to see if the host is ready to accept data.
  158.     if (Endpoint_IsINReady())
  159.     {
  160.         // We'll create an empty report.
  161.         USB_JoystickReport_Input_t JoystickInputData;
  162.         // We'll then populate this report with what we want to send to the host.
  163.         GetNextReport(&JoystickInputData);
  164.         // Once populated, we can output this data to the host. We do this by first writing the data to the control stream.
  165.         while(Endpoint_Write_Stream_LE(&JoystickInputData, sizeof(JoystickInputData), NULL) != ENDPOINT_RWSTREAM_NoError);
  166.         // We then send an IN packet on this endpoint.
  167.         Endpoint_ClearIN();
  168.     }
  169. }
  170.  
  171. typedef enum {
  172.     PROCESS,
  173.     DONE
  174. } State_t;
  175. State_t state = PROCESS;
  176.  
  177. #define ECHOES 2
  178. int echoes = 0;
  179. USB_JoystickReport_Input_t last_report;
  180.  
  181. Command tempCommand;
  182. int durationCount = 0;
  183.  
  184. const int8_t LAST_COMMAND = (sizeof(m_command) / sizeof(m_command[0])) - 1; // used for debugging
  185.  
  186. int8_t m_commandIndex = 0;    // current executing command
  187. int8_t m_endIndex = 26;       // last command to execute in sequence, then we check for new command
  188. int8_t m_eggCount = 0;        // how many eggs we are holding right now
  189. int16_t m_spinCount = 0;      // how many times we have spun this iteration
  190. int16_t m_spinMax = 0;        // how many times we need to spin to move on
  191. int8_t m_phase = 0;           // 0 = collecting, 1 = hatching
  192. int8_t m_columnPosition = 0;  // where the cursor is in the boxes menu
  193. int8_t m_nextColumn = 1;      // where we want the cursor to be to drop the pokemon
  194. int8_t m_boxesFilled = 0;     // number of boxes we filled up
  195.  
  196. // Prepare the next report for the host.
  197. void GetNextReport(USB_JoystickReport_Input_t* const ReportData) {
  198.  
  199.     // Prepare an empty report
  200.     memset(ReportData, 0, sizeof(USB_JoystickReport_Input_t));
  201.     ReportData->LX = STICK_CENTER;
  202.     ReportData->LY = STICK_CENTER;
  203.     ReportData->RX = STICK_CENTER;
  204.     ReportData->RY = STICK_CENTER;
  205.     ReportData->HAT = HAT_CENTER;
  206.  
  207.     // Repeat ECHOES times the last report
  208.     if (echoes > 0)
  209.     {
  210.         memcpy(ReportData, &last_report, sizeof(USB_JoystickReport_Input_t));
  211.         echoes--;
  212.         return;
  213.     }
  214.  
  215.     // States and moves management
  216.     switch (state)
  217.     {
  218.         case PROCESS:
  219.             // quit executing if we are at the last command
  220.             if ((m_commandIndex == LAST_COMMAND) && (m_command[m_commandIndex].button == NOTHING))
  221.             {
  222.                 return;
  223.             }
  224.  
  225.             // quit executing if we filled all the boxes
  226.             if (m_boxesFilled >= m_boxesToFill) return;
  227.  
  228.             // Get the next command sequence (new start and end)
  229.             if (m_commandIndex == -1)
  230.             {
  231.                 if (m_endIndex == 26) // we just picked up an egg (hopefully)
  232.                 {
  233.                     m_eggCount++;
  234.  
  235.                     if (m_eggCount < MAX_NUM_OF_EGGS)
  236.                     {
  237.                         m_commandIndex = 27; // spin
  238.                         m_endIndex = 28;
  239.                         m_spinCount = 0;
  240.                         m_spinMax = 14; // 2 "spins" per second (7 seconds should be enough for next egg)
  241.                     }
  242.                     else
  243.                     {
  244.                         m_phase = 1; // set to hatching phase
  245.                         m_commandIndex = 27; // spin
  246.                         m_endIndex = 28;
  247.                         m_spinCount = 0;
  248.                         m_spinMax = (HATCHING_TIME_SEC * m_eggStepGroup) * 2; // 2 "spins" per second
  249.                     }
  250.                 }
  251.                 else if (m_endIndex == 28) // We are spinning
  252.                 {
  253.                     m_spinCount++;
  254.                     if (m_spinCount < m_spinMax)
  255.                     {
  256.                         m_commandIndex = 27; // keep spinning
  257.                     }
  258.                     else
  259.                     {
  260.                         if (m_phase == 0) // we are still collecting
  261.                         {
  262.                             m_commandIndex = 3; // go back to get an egg
  263.                             m_endIndex = 26;
  264.                         }
  265.                         else
  266.                         {
  267.                             m_commandIndex = 29; // put mon in boxes
  268.                             m_endIndex = 63;
  269.                             m_phase = 0; // set to egg collecting phase
  270.                         }
  271.                     }
  272.                 }
  273.                 else if (m_endIndex == 63) // We opened the pokemon menu, selected the pokemon, and moved right
  274.                 {
  275.                     m_columnPosition++;
  276.                     if (m_columnPosition < m_nextColumn)
  277.                     {
  278.                         m_commandIndex = 62; // we need to keep moving right
  279.                     }
  280.                     else
  281.                     {
  282.                         m_commandIndex = 64; // we are at an open column, put them in
  283.                         m_endIndex = 67;
  284.                     }
  285.                 }
  286.                 else if (m_endIndex == 67) // We just put the pokemon in the box
  287.                 {
  288.                     if (m_nextColumn < 6)
  289.                     {
  290.                         m_commandIndex = 70; // just quit the menu
  291.                         m_endIndex = 77;
  292.                         m_nextColumn++;
  293.                     }
  294.                     else
  295.                     {
  296.                         m_commandIndex = 68; // advance to the next box, then quit the menu
  297.                         m_endIndex = 77;
  298.                         m_nextColumn = 1;
  299.                         m_boxesFilled++;
  300.                     }
  301.                 }
  302.                 else if (m_endIndex == 77) // We finished putting away the hatched mon and are in the menu
  303.                 {
  304.                     m_eggCount = 0;
  305.                     m_columnPosition = 0;
  306.                     m_commandIndex = 5; // start over!
  307.                     m_endIndex = 26;
  308.                 }
  309.             }
  310.        
  311.             memcpy_P(&tempCommand, &(m_command[m_commandIndex]), sizeof(Command));
  312.             switch (tempCommand.button)
  313.             {
  314.                 case UP:
  315.                     ReportData->LY = STICK_MIN;            
  316.                     break;
  317.  
  318.                 case LEFT:
  319.                     ReportData->LX = STICK_MIN;            
  320.                     break;
  321.  
  322.                 case DOWN:
  323.                     ReportData->LY = STICK_MAX;            
  324.                     break;
  325.  
  326.                 case RIGHT:
  327.                     ReportData->LX = STICK_MAX;            
  328.                     break;
  329.  
  330.                 case UP_RIGHT:
  331.                     ReportData->LY = STICK_MIN;
  332.                     ReportData->LX = STICK_MAX;
  333.                     break;
  334.  
  335.                 case UP_LEFT:
  336.                     ReportData->LY = STICK_MIN;
  337.                     ReportData->RX = STICK_MIN;
  338.                     break;
  339.  
  340.                 case X:
  341.                     ReportData->Button |= SWITCH_X;
  342.                     break;
  343.  
  344.                 case Y:
  345.                     ReportData->Button |= SWITCH_Y;
  346.                     break;
  347.  
  348.                 case A:
  349.                     ReportData->Button |= SWITCH_A;
  350.                     break;
  351.  
  352.                 case B:
  353.                     ReportData->Button |= SWITCH_B;
  354.                     break;
  355.  
  356.                 case L:
  357.                     ReportData->Button |= SWITCH_L;
  358.                     break;
  359.  
  360.                 case R:
  361.                     ReportData->Button |= SWITCH_R;
  362.                     break;
  363.  
  364.                 case ZL:
  365.                     ReportData->Button |= SWITCH_A;
  366.                     ReportData->LY = STICK_MIN;
  367.                     ReportData->RX = STICK_MIN;
  368.                     break;
  369.  
  370.                 case ZR:
  371.                     ReportData->Button |= SWITCH_ZR;
  372.                     break;
  373.  
  374.                 case MINUS:
  375.                     ReportData->Button |= SWITCH_MINUS;
  376.                     break;
  377.  
  378.                 case PLUS:
  379.                     ReportData->Button |= SWITCH_PLUS;
  380.                     break;
  381.  
  382.                 case LCLICK:
  383.                     ReportData->Button |= SWITCH_LCLICK;
  384.                     break;
  385.  
  386.                 case RCLICK:
  387.                     ReportData->Button |= SWITCH_RCLICK;
  388.                     break;
  389.  
  390.                 case TRIGGERS:
  391.                     ReportData->Button |= SWITCH_L | SWITCH_R;
  392.                     break;
  393.  
  394.                 case HOME:
  395.                     ReportData->Button |= SWITCH_HOME;
  396.                     break;
  397.  
  398.                 case CAPTURE:
  399.                     ReportData->Button |= SWITCH_CAPTURE;
  400.                     break;
  401.  
  402.                 default:
  403.                     // really nothing lol
  404.                     break;
  405.             }
  406.  
  407.             durationCount++;
  408.  
  409.             if (durationCount > tempCommand.duration)
  410.             {
  411.                 m_commandIndex++;
  412.                 durationCount = 0;     
  413.  
  414.                 // We reached the end of a command sequence
  415.                 if (m_commandIndex > m_endIndex)
  416.                 {
  417.                     m_commandIndex = -1;
  418.                 }      
  419.             }
  420.  
  421.             break;
  422.  
  423.         case DONE: return;
  424.     }
  425.  
  426.     // Prepare to echo this report
  427.     memcpy(&last_report, ReportData, sizeof(USB_JoystickReport_Input_t));
  428.     echoes = ECHOES;
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement