1 #include "core/backtrace.h"
2 #include "core/time.h"
3 #include "game/file.h"
4 #include "game/game.h"
5 #include "game/settings.h"
6 
7 #ifdef _MSC_VER
8 #include <direct.h>
9 #define chdir _chdir
10 #define getcwd _getcwd
11 #elif !defined(__vita__)
12 #include <unistd.h>
13 #endif
14 
15 #include <signal.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include "sav_compare.h"
19 
handler(int sig)20 static void handler(int sig)
21 {
22     fprintf(stderr, "Oops, crashed with signal %d :(", sig);
23     backtrace_print();
24     exit(1);
25 }
26 
run_ticks(int ticks)27 static void run_ticks(int ticks)
28 {
29     setting_reset_speeds(500, setting_scroll_speed());
30     time_set_millis(0);
31     for (int i = 1; i <= ticks; i++) {
32         time_set_millis(2 * i);
33         game_run();
34     }
35 }
36 
run_autopilot(const char * input_saved_game,const char * output_saved_game,int ticks_to_run)37 static int run_autopilot(const char *input_saved_game, const char *output_saved_game, int ticks_to_run)
38 {
39     printf("Running autopilot: %s --> %s in %d ticks\n", input_saved_game, output_saved_game, ticks_to_run);
40     signal(SIGSEGV, handler);
41 
42     if (!game_pre_init()) {
43         printf("Unable to run Game_preInit\n");
44         return 1;
45     }
46 
47     if (!game_init()) {
48         printf("Unable to run Game_init\n");
49         return 2;
50     }
51 
52     if (!game_file_load_saved_game(input_saved_game)) {
53         char wd[500];
54         if (getcwd(wd, 500)) {
55             printf("Unable to load saved game from %s\n", wd);
56         } else {
57             printf("Unable to load saved game\n");
58         }
59         return 3;
60     }
61     run_ticks(ticks_to_run);
62     printf("Saving game to %s\n", output_saved_game);
63     game_file_write_saved_game(output_saved_game);
64     printf("Done\n");
65 
66     game_exit();
67 
68     return 0;
69 }
70 
main(int argc,char ** argv)71 int main(int argc, char **argv)
72 {
73     if (argc != 5) {
74         printf("Incorrect number of arguments (%d)\n", argc);
75         return -1;
76     }
77     const char *input = argv[1];
78     const char *output = argv[2];
79     const char *expected = argv[3];
80     int ticks = atoi(argv[4]);
81     if (run_autopilot(input, output, ticks) == 0) {
82         return compare_files(expected, output);
83     } else {
84         return 1;
85     }
86 }
87