int main(int argc, char **argv) { int ret; struct sigaction act; time(&start_time); opts.outfile = 0; opts.poll_sleep = millis_to_timespec(POLL_SLEEP_MILLIS); opts.new_data_thresh = NEW_DATA_THRESH; opts.ms_per_sample = MS_PER_SAMPLE; opts.cpu_freq = CPU_FREQ; argp_parse(&parser_def, argc, argv, 0, 0, &opts); fprintf(stderr, "ms_per_sample = %ld\n", opts.ms_per_sample); /* ensure that if we get a signal, we'll do cleanup, then exit */ act.sa_handler = close_handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGHUP, &act, NULL); sigaction(SIGTERM, &act, NULL); sigaction(SIGINT, &act, NULL); ret = monitor_tbufs(); dump_stats(); msync(new_qos, sizeof(_new_qos_data), MS_SYNC); disable_tracing(); return ret; }
/* command parser for GNU argp - see GNU docs for more info */ error_t cmd_parser(int key, char *arg, struct argp_state *state) { settings_t *setup = (settings_t *)state->input; switch ( key ) { case 't': /* set new records threshold for logging */ { char *inval; setup->new_data_thresh = strtol(arg, &inval, 0); if ( inval == arg ) argp_usage(state); } break; case 's': /* set sleep time (given in milliseconds) */ { char *inval; setup->poll_sleep = millis_to_timespec(strtol(arg, &inval, 0)); if ( inval == arg ) argp_usage(state); } break; case 'm': /* set ms_per_sample */ { char *inval; setup->ms_per_sample = strtol(arg, &inval, 0); if ( inval == arg ) argp_usage(state); } break; case ARGP_KEY_ARG: { if ( state->arg_num == 0 ) setup->outfile = arg; else argp_usage(state); } break; default: return ARGP_ERR_UNKNOWN; } return 0; }
/** * @brief Grorld entry point * * Grorld application entry point and main loop. It also contains * the logic to automatically grind CivWorld bonus resources! */ int main(const int argc, const char **argv) { std::cout << "Grorld, version 1" << std::endl; // Create a pseudorandom number generator instance // (http://en.wikipedia.org/wiki/C%2B%2B0x#Extensible_random_number_facility) std::mt19937 engine(time(NULL)); // Initialize mouse & screen, put the target window in front Mouse_Initialize(); XImage *grab = Screen_Initialize("CivWorld on Facebook"); assert(grab); // Create the macthing algoritm object Match m(grab); // Load images that we want to match/find on the screen const cv::Mat bonus = Match::loadTemplate("assets/bonus.png"); const cv::Mat city = Match::loadTemplate("assets/city.png"); // Main loop (http://en.wikipedia.org/wiki/Event_loop) while (true) { // Grab a new frame (much like doing a screenshot) Screen_Get(); // Prepare the matching algoritm with the new frame... m.prepare(); // Search (via a template matching algorithm) for a bonus bubbles std::tuple<cv::Point, double> mr = m.match(bonus); #ifndef TEST if (std::get<1>(mr) > MATCHING_THRESHOLD) { // We got a hit on the serach image, translate that point into a screen coordinate for the mouse to hover Screen_TranslateCoordinates(&std::get<0>(mr).x, &std::get<0>(mr).y); // Hover the mouse over it (use some randomness for the pointer placement...) Mouse_SetCoords(std::get<0>(mr).x + std::uniform_int_distribution<int>(0, bonus.size().width)(engine), std::get<0>(mr).y + std::uniform_int_distribution<int>(0, bonus.size().height)(engine)); std::cout << time(NULL) << "\tBONUS: at " << std::get<0>(mr).x << "x" << std::get<0>(mr).y << " (score: " << std::get<1>(mr) << ")" << std::endl; continue; } // Once in a while, check if we need to press the city button... else { std::tuple<cv::Point, double> mr = m.match(city); if (std::get<1>(mr) > MATCHING_THRESHOLD) { // We got a hit on the serach image, translate that point into a screen coordinate for the mouse to hover Screen_TranslateCoordinates(&std::get<0>(mr).x, &std::get<0>(mr).y); // Hover the mouse over it and click Mouse_SetCoords(std::get<0>(mr).x + (city.size().width / 2), std::get<0>(mr).y + (city.size().height / 2)); Mouse_Click(Button1); std::cout << time(NULL) << "\tCITY: at " << std::get<0>(mr).x << "x" << std::get<0>(mr).y << " (score: " << std::get<1>(mr) << ")" << std::endl; // Wait for the window to redraw (it's slow) before trying something clever struct timespec delay_click = millis_to_timespec(std::uniform_int_distribution<int>(2000, 4000)(engine)); clock_nanosleep(CLOCK_MONOTONIC, 0, &delay_click, NULL); continue; } } #endif // Give the computer some time to rest before processing the next frame struct timespec sleep = millis_to_timespec(std::uniform_int_distribution<int>(40, 60)(engine)); clock_nanosleep(CLOCK_MONOTONIC, 0, &sleep, NULL); } // Clean up and exit Screen_Deinitialize(); Mouse_Deinitialize(); return EXIT_SUCCESS; }