-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutility.cpp
More file actions
228 lines (187 loc) · 4.54 KB
/
utility.cpp
File metadata and controls
228 lines (187 loc) · 4.54 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// author: Kovacs Marton
// email: tetra666@gmail.com
// license: whatever. note my name
//
// utility.cpp
//
// implementation of some operations on the common stuff
//
#include "drawinterface.hpp"
#include "flyerz.hpp"
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <getopt.h>
Coordinate operator-(const Coordinate& op)
{
return Coordinate(-op.x, -op.y);
}
Coordinate operator+(const Coordinate& lhs, const Coordinate& rhs)
{
return Coordinate(lhs.x + rhs.x, lhs.y + rhs.y);
}
Coordinate operator-(const Coordinate& lhs, const Coordinate& rhs)
{
return lhs + -rhs;
}
Coordinate operator*(const Coordinate& lhs, const Coordinate::CoordType& rhs)
{
return Coordinate(lhs.x * rhs, lhs.y * rhs);
}
Coordinate operator*(const Coordinate::CoordType& lhs, const Coordinate& rhs)
{
return rhs * lhs;
}
Coordinate operator/(const Coordinate& lhs, const Coordinate::CoordType& rhs)
{
return Coordinate(lhs.x / rhs, lhs.y / rhs);
}
Coordinate& Coordinate::operator+=(const Coordinate& rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
Coordinate& Coordinate::operator*=(const Coordinate::CoordType& rhs)
{
x *= rhs;
y *= rhs;
return *this;
}
Coordinate& Coordinate::operator/=(const Coordinate::CoordType& rhs)
{
x /= rhs;
y /= rhs;
return *this;
}
Coordinate::CoordType Length(const Coordinate& vektor)
{
return std::sqrt(LengthSqr(vektor));
}
Coordinate::CoordType LengthSqr(const Coordinate& vektor)
{
return Sqr(vektor.x) + Sqr(vektor.y);
}
Coordinate::CoordType Distance(const Coordinate& lhs, const Coordinate& rhs)
{
return Length(lhs - rhs);
}
Coordinate::CoordType DistanceSqr(const Coordinate& lhs, const Coordinate& rhs)
{
return LengthSqr(lhs - rhs);
}
Coordinate Normalize(const Coordinate& vektor, Coordinate::CoordType length)
{
return vektor * length / Length(vektor);
}
Coordinate Rotate90Cw(const Coordinate& vektor)
{
return Coordinate(vektor.y, -vektor.x);
}
Coordinate Rotate90Ccw(const Coordinate& vektor)
{
return Coordinate(-vektor.y, vektor.x);
}
Coordinate::CoordType Dot(const Coordinate& lhs, const Coordinate& rhs)
{
return lhs.x * rhs.x + lhs.y * rhs.y;
}
const std::string usage = " --teamnum/-n \"N n1 n2 n3 ... nK\" [--size/-s WIDTHxHEIGHT] [--log <file>] [--time <time limit in seconds>] [--score <score limit>] [--font <font name>] [remote ai names, - for builtin]*";
#define RETURN_WITH_USAGE std::cerr << "usage: " << argv[0] << usage << '\n'; return false;
bool ParseCommandline(int argc, char* argv[], Options& options)
{
opterr = 0;
int playerCount = 0;
bool teamflag = false;
option longOptions[] = {
{"size", required_argument, 0, 's'},
{"teamnum", required_argument, 0, 'n'},
{"log", required_argument, 0, 'l'},
{"time", required_argument, 0, 't'},
{"score", required_argument, 0, 'p'},
{"font", required_argument, 0, 'f'}
};
int optionIndex;
int c;
while (-1 != (c = getopt_long(argc, argv, "m:s:n:", longOptions, &optionIndex)))
{
switch (c)
{
case 's':
{
int width;
int height;
char c;
std::istringstream str(optarg);
str >> width >> c >> height;
if (str.fail())
{
std::cerr << "invalid argument for --size: " << optarg << '\n';
RETURN_WITH_USAGE;
}
options.size.x = width;
options.size.y = height;
break;
}
case 'n':
{
int teamnum;
std::istringstream str(optarg);
str >> teamnum;
options.teams.reserve(teamnum);
for (int i = 0; i < teamnum; ++i)
{
int num;
str >> num;
options.teams.push_back(num);
playerCount += num;
}
if (!str)
{
std::cerr << "invalid argument for --teamnum: " << optarg << '\n';
RETURN_WITH_USAGE;
}
teamflag = true;
break;
}
case 'l':
{
options.logFile = optarg;
break;
}
case 't':
{
options.time = std::atoi(optarg);
break;
}
case 'p':
{
options.score = std::atoi(optarg);
break;
}
case 'f':
{
options.fontName = optarg;
break;
}
case '?':
{
RETURN_WITH_USAGE;
}
}
}
options.names.insert(options.names.end(), &argv[optind], &argv[argc]);
if (!teamflag)
{
RETURN_WITH_USAGE;
}
for (int i = options.names.size(); i < playerCount; ++i)
{
options.names.push_back("-");
}
return true;
}