diff options
-rw-r--r-- | main.cpp | 84 |
1 files changed, 65 insertions, 19 deletions
@@ -226,6 +226,26 @@ static void signalHandler(int sig){ #endif +struct RobotSpec{ + string fname; + bool random; + int x,y,heading; +}; + +int parseInt(const char *str){ + char *endp; + int v=strtol(str,&endp,10); + if(str[0]=='\0'||*endp!='\0'){ + cerr<<"Invalid number '"<<str<<"'"<<endl; + exit(1); + } + return v; +} + +void usage(char **argv){ + cerr<<"Usage: "<<argv[0]<<" [-r <fname>]* [-p <fname> <x> <y> <heading>]*"<<endl; +} + int main(int argc,char **argv){ struct timeval tv; gettimeofday(&tv,nullptr); @@ -234,31 +254,57 @@ int main(int argc,char **argv){ World world; vector<Team> teams; - bool opt_pos = false; - vector<tuple<int,int,int>> positions; - - int k = 1; // Increase past options; + vector<RobotSpec> robotSpecs; - if(argc>k && string(argv[k]) == "-p") { - opt_pos = true; - k++; + if(argc<=1){ + usage(argv); + return 1; } - for(int i=k;i<argc;i++){ // Start after options - ifstream f(argv[i]); - assert(f); - teams.push_back(assemble(preprocess(f))); - if(opt_pos) { - positions.emplace_back((int)strtol(argv[i+1],NULL,10), (int)strtol(argv[i+2],NULL,10), (int)strtol(argv[i+3],NULL,10)); // TODO: check if i>argc - i += 3; + for(int i=1;i<argc;i++){ + if(argv[i][0]!='-'||strlen(argv[i])!=2){ + cerr<<"Unexpected argument '"<<argv[i]<<"'"<<endl; + return 1; } - } + switch(argv[i][1]){ + case 'h': + usage(argv); + return 0; + + case 'r': + if(i>=argc-1){ + cerr<<"Expected file name after '-r'"<<endl; + return 1; + } + robotSpecs.push_back({argv[i+1],true,0,0,0}); + i+=1; + break; + + case 'p': + if(i>=argc-4){ + cerr<<"Expected file name, x, y and heading after '-p'"<<endl; + return 1; + } + robotSpecs.push_back({argv[i+1],false,parseInt(argv[i+2]),parseInt(argv[i+3]),parseInt(argv[i+4])}); + i+=4; + break; - for(int i=0; i<(int)teams.size(); i++) { - const Team &t = teams[i]; - Robot &r = opt_pos ? world.create(&t,2,t.banks.size(),false, get<0>(positions[i]), get<1>(positions[i]), get<2>(positions[i])) - : world.create(&t,2,t.banks.size(),false); + default: + cerr<<"Unknown switch in '"<<argv[i]<<"'"<<endl; + return 1; + } + } + for(const RobotSpec &rs : robotSpecs){ + ifstream f(rs.fname); + if(!f){ + cerr<<"Cannot open file '"<<rs.fname<<"'"<<endl; + return 1; + } + teams.push_back(assemble(preprocess(f))); + Team &t=teams.back(); + Robot &r=rs.random ? world.create(&t,2,t.banks.size(),false) + : world.create(&t,2,t.banks.size(),false,rs.x,rs.y,rs.heading); for(int i=0;i<(int)t.banks.size();i++){ r.load(i,t.banks[i]); r.active=1; |