1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <unistd.h>
#include <cstdlib>
#include <cassert>
#include <sys/time.h>
#include <cstring>
#include "params.h"
#include "world.h"
#include "parse.h"
#ifndef _WIN32
# include <signal.h>
#endif
using namespace std;
static ScreenBuffer *sb;
#ifndef _WIN32
static void signalHandler(int sig){
if(sig==SIGINT){
sb->emergencyDeinit();
_exit(130);
}
}
#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>]* "
<<"[-s <sleeptime>] [-t] [-B]"<<endl
<<" -r Place robot at random position"<<endl
<<" -p Place robot at specified position"<<endl
<<" -s Modify usecs to sleep between frames"<<endl
<<" -t Don't sleep between frames but wait until keypress"<<endl
<<" -B Batch mode: simulate until no more active robots, then robot locations;"<<endl
<<" columns: team x y heading"<<endl;
}
int main(int argc,char **argv){
struct timeval tv;
gettimeofday(&tv,nullptr);
srand(tv.tv_sec*1000000+tv.tv_usec);
World world;
vector<Team> teams;
vector<RobotSpec> robotSpecs;
int sleeptime=3000;
bool step = false;
bool batch = false;
if(argc<=1){
usage(argv);
return 1;
}
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;
case 's':
if(i>=argc-1){
cerr<<"Expected sleep time after '-s'"<<endl;
return 1;
}
sleeptime=parseInt(argv[i+1]);
i+=1;
break;
case 't':
step = true;
break;
case 'B':
batch = true;
break;
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;
}
}
if(batch){
for(int i=0;i<C::autoTimeout;i++){
world.tick();
bool haveActive=false;
for(int j=0;j<SIZE*SIZE;j++){
const int x=j%SIZE,y=j/SIZE;
if(world.board[y][x]&&world.board[y][x]->active){
haveActive=true;
break;
}
}
if(!haveActive)break;
}
for(int y=0;y<SIZE;y++){
for(int x=0;x<SIZE;x++){
if(world.board[y][x]){
cout<<world.board[y][x]->team<<' '
<<x<<' '<<y<<' '
<<world.board[y][x]->heading<<endl;
}
}
}
} else {
sb=new ScreenBuffer(SIZE*3, SIZE+1);
#ifndef _WIN32
signal(SIGINT,signalHandler);
#endif
world.print(*sb);
sb->draw();
for(int i=0;i<C::autoTimeout;i++){
if(step)cin.get();
else usleep(sleeptime);
world.tick();
world.print(*sb);
sb->mvprintf(0, SIZE, "step: %5i", i+2);
sb->draw();
}
}
delete sb;
}
|