Rev 42 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | muzer | 1 | #include "bike.h" |
| 2 | |||
| 3 | Bike::Bike(QTcpSocket *sock, int i) |
||
| 4 | { |
||
| 9 | muzer | 5 | socket = sock; |
| 6 | id = i; |
||
| 1 | muzer | 7 | |
| 9 | muzer | 8 | connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); |
| 9 | connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); |
||
| 1 | muzer | 10 | |
| 9 | muzer | 11 | isDisconnected = false; |
| 1 | muzer | 12 | |
| 40 | muzer | 13 | x = rand() % 80000; |
| 14 | y = rand() % 60000; |
||
| 41 | muzer | 15 | //linePoints.append(QPoint(x, y)); |
| 1 | muzer | 16 | |
| 40 | muzer | 17 | velocity = 100; |
| 26 | muzer | 18 | angle = (rand() % 4) * 90; |
| 19 | muzer | 19 | abpool = 0; |
| 9 | muzer | 20 | name = ""; |
| 21 | show = false; |
||
| 22 | isReady = false; |
||
| 23 | hadGo = false; |
||
| 24 | dead = false; |
||
| 25 | collided = false; |
||
| 40 | muzer | 26 | speed = 500; |
| 1 | muzer | 27 | |
| 9 | muzer | 28 | colour.setBlue(0); |
| 29 | colour.setRed(0); |
||
| 30 | colour.setGreen(0); |
||
| 1 | muzer | 31 | |
| 40 | muzer | 32 | greet(); |
| 33 | |||
| 9 | muzer | 34 | if (!socket->waitForReadyRead(2000)) |
| 35 | { |
||
| 36 | socket->disconnectFromHost(); |
||
| 37 | show = false; |
||
| 38 | isReady = true; |
||
| 39 | hadGo = false; |
||
| 40 | } |
||
| 1 | muzer | 41 | } |
| 42 | |||
| 14 | muzer | 43 | void Bike::draw(QPainter *painter, QList<Bike *> bikes) |
| 1 | muzer | 44 | { |
| 9 | muzer | 45 | if (show) |
| 46 | { |
||
| 47 | painter->setPen(colour); |
||
| 48 | painter->setFont(QFont("sans", 12)); |
||
| 1 | muzer | 49 | |
| 9 | muzer | 50 | for (int i = 0; i < linePoints.count(); i++) |
| 51 | { |
||
| 52 | QPoint point1; |
||
| 53 | QPoint point2; |
||
| 54 | if (i == 0) |
||
| 55 | { |
||
| 56 | point1 = linePoints[0]; |
||
| 57 | } |
||
| 58 | else |
||
| 59 | { |
||
| 60 | point1 = linePoints[i - 1]; |
||
| 61 | } |
||
| 1 | muzer | 62 | |
| 9 | muzer | 63 | point2 = linePoints[i]; |
| 1 | muzer | 64 | |
| 40 | muzer | 65 | painter->drawLine(point1/100., point2/100.); |
| 9 | muzer | 66 | } |
| 1 | muzer | 67 | |
| 9 | muzer | 68 | if (!collided) |
| 69 | { |
||
| 14 | muzer | 70 | collided = hasCollided(bikes); |
| 9 | muzer | 71 | if (angle == 0) |
| 72 | { |
||
| 73 | y -= velocity; |
||
| 74 | } |
||
| 75 | else if (angle == 90) |
||
| 76 | { |
||
| 77 | x += velocity; |
||
| 78 | } |
||
| 79 | else if (angle == 180) |
||
| 80 | { |
||
| 81 | y += velocity; |
||
| 82 | } |
||
| 83 | else if (angle == 270) |
||
| 84 | { |
||
| 85 | x -= velocity; |
||
| 86 | } |
||
| 28 | muzer | 87 | if (angle == 0 || angle == 180) |
| 88 | { |
||
| 40 | muzer | 89 | painter->fillRect(x/100. - 5, y/100. - 15, 10, 30, colour); |
| 28 | muzer | 90 | } |
| 91 | else |
||
| 92 | { |
||
| 40 | muzer | 93 | painter->fillRect(x/100. - 15, y/100. - 5, 30, 10, colour); |
| 28 | muzer | 94 | } |
| 40 | muzer | 95 | painter->drawText(x/100., y/100. - 20, name); |
| 9 | muzer | 96 | } |
| 97 | else |
||
| 98 | { |
||
| 99 | linePoints.clear(); |
||
| 100 | } |
||
| 1 | muzer | 101 | |
| 102 | |||
| 28 | muzer | 103 | |
| 104 | |||
| 9 | muzer | 105 | } |
| 1 | muzer | 106 | } |
| 107 | |||
| 34 | tom | 108 | void Bike::run() |
| 1 | muzer | 109 | { |
| 36 | muzer | 110 | if(!haveSentAlready){ |
| 111 | for (int i = 0; i < bikes.count(); i++) |
||
| 9 | muzer | 112 | { |
| 36 | muzer | 113 | |
| 114 | |||
| 115 | Bike *bike = bikes[i]; |
||
| 116 | if (bike->isReady && bike->show) |
||
| 9 | muzer | 117 | { |
| 36 | muzer | 118 | if (bike->collided) |
| 119 | { |
||
| 120 | socket->write("DEAD "); |
||
| 121 | socket->write(bike->name.toAscii()); |
||
| 122 | socket->write("\n"); |
||
| 123 | } |
||
| 124 | else |
||
| 125 | { |
||
| 126 | socket->write("BIKE "); |
||
| 127 | socket->write(bike->name.toAscii()); |
||
| 128 | socket->write(" "); |
||
| 129 | socket->write(QString::number(bike->x).toAscii()); |
||
| 130 | socket->write(" "); |
||
| 131 | socket->write(QString::number(bike->y).toAscii()); |
||
| 132 | socket->write(" "); |
||
| 133 | socket->write(QString::number(bike->colour.red()).toAscii()); |
||
| 134 | socket->write(" "); |
||
| 135 | socket->write(QString::number(bike->colour.green()).toAscii()); |
||
| 136 | socket->write(" "); |
||
| 137 | socket->write(QString::number(bike->colour.blue()).toAscii()); |
||
| 138 | socket->write("\n"); |
||
| 139 | } |
||
| 9 | muzer | 140 | } |
| 141 | } |
||
| 36 | muzer | 142 | linePoints.append(QPoint(x, y)); |
| 9 | muzer | 143 | } |
| 1 | muzer | 144 | |
| 4 | tom | 145 | |
| 36 | muzer | 146 | |
| 9 | muzer | 147 | if (dead) |
| 148 | { |
||
| 149 | show = false; |
||
| 150 | isReady = true; |
||
| 151 | hadGo = false; |
||
| 152 | dead = false; |
||
| 153 | } |
||
| 1 | muzer | 154 | |
| 9 | muzer | 155 | if (!dead) |
| 156 | { |
||
| 36 | muzer | 157 | if(!haveSentAlready){ |
| 158 | socket->write("G\n"); |
||
| 159 | socket->flush(); |
||
| 160 | } |
||
| 1 | muzer | 161 | |
| 9 | muzer | 162 | hadGo = false; |
| 40 | muzer | 163 | couldReadLine = false; |
| 36 | muzer | 164 | if (!socket->waitForReadyRead(1)) |
| 9 | muzer | 165 | { |
| 36 | muzer | 166 | // socket->disconnectFromHost(); |
| 167 | // dead = true; |
||
| 168 | haveSentAlready = true; |
||
| 169 | return; |
||
| 9 | muzer | 170 | } |
| 40 | muzer | 171 | if (!hadGo && couldReadLine) |
| 9 | muzer | 172 | { |
| 40 | muzer | 173 | cout << "KILLING BECAUSE HE DIDN'T HAVE A GO\n"; |
| 9 | muzer | 174 | socket->disconnectFromHost(); |
| 175 | dead = true; |
||
| 176 | } |
||
| 177 | } |
||
| 42 | muzer | 178 | if(hadGo || dead) |
| 179 | hasHadGo = true; |
||
| 34 | tom | 180 | |
| 181 | time_t result = time(NULL); |
||
| 43 | muzer | 182 | if(hasHadGo) |
| 183 | cout << result << ": Recieved next move from " << name.toStdString().c_str() << endl; |
||
| 1 | muzer | 184 | } |
| 185 | |||
| 19 | muzer | 186 | int sign(int x){ |
| 187 | if(x > 0){ |
||
| 9 | muzer | 188 | return 1; |
| 189 | } |
||
| 19 | muzer | 190 | if(x < 0){ |
| 9 | muzer | 191 | return -1; |
| 192 | } |
||
| 193 | return 0; |
||
| 194 | } |
||
| 195 | |||
| 14 | muzer | 196 | bool Bike::hasCollided(QList<Bike *> bikes) |
| 8 | tom | 197 | { |
| 9 | muzer | 198 | // Do collision detection here |
| 199 | // use linePoints |
||
| 200 | int i = linePoints.count() - 1; |
||
| 41 | muzer | 201 | if(i <= 0) |
| 202 | return false; |
||
| 40 | muzer | 203 | if(linePoints[i-1].x() < 0 || linePoints[i-1].x() >= 80000 || linePoints[i-1].y() < 0 || linePoints[i-1].y() >= 60000) |
| 9 | muzer | 204 | return true; |
| 40 | muzer | 205 | if(linePoints[i].x() < 0 || linePoints[i].x() >= 80000 || linePoints[i].y() < 0 || linePoints[i].y() >= 60000) |
| 9 | muzer | 206 | return true; |
| 18 | muzer | 207 | int j, r; |
| 14 | muzer | 208 | Bike *bike; |
| 19 | muzer | 209 | for(r = 0; r < bikes.count(); r++) |
| 18 | muzer | 210 | { |
| 211 | bike = bikes[r]; |
||
| 21 | muzer | 212 | int forsubtract = 1; |
| 213 | if(bike->name == name){ |
||
| 214 | forsubtract = 3; |
||
| 215 | } |
||
| 216 | for(j = 0; j < bike->linePoints.count() - forsubtract; j++) |
||
| 18 | muzer | 217 | { |
| 20 | muzer | 218 | int jx = bike->linePoints[j].x(); |
| 219 | int j1x = bike->linePoints[j+1].x(); |
||
| 220 | int jy = bike->linePoints[j].y(); |
||
| 221 | int j1y = bike->linePoints[j+1].y(); |
||
| 222 | int ix = linePoints[i-1].x(); |
||
| 223 | int i1x = linePoints[i].x(); |
||
| 224 | int iy = linePoints[i-1].y(); |
||
| 225 | int i1y = linePoints[i].y(); |
||
| 21 | muzer | 226 | |
| 20 | muzer | 227 | if(angle == 0){ |
| 228 | iy += 1; |
||
| 229 | i1y -= 1; |
||
| 230 | } |
||
| 231 | if(angle == 90){ |
||
| 232 | ix -= 1; |
||
| 233 | i1x += 1; |
||
| 234 | } |
||
| 235 | if(angle == 180){ |
||
| 236 | iy -= 1; |
||
| 237 | i1y += 1; |
||
| 238 | } |
||
| 239 | if(angle == 270){ |
||
| 240 | ix += 1; |
||
| 241 | i1x -= 1; |
||
| 242 | } |
||
| 243 | if(!(jx == j1x && i1x == ix) && !(jy == j1y && i1y == iy)) |
||
| 18 | muzer | 244 | { |
| 16 | muzer | 245 | |
| 18 | muzer | 246 | // If not parallel |
| 20 | muzer | 247 | if(jx == j1x) |
| 18 | muzer | 248 | { |
| 249 | // x equal |
||
| 16 | muzer | 250 | |
| 40 | muzer | 251 | if((ix > jx && i1x < jx) || (ix < jx && i1x > jx)) |
| 18 | muzer | 252 | { |
| 20 | muzer | 253 | if((sign(iy - jy) != sign(iy - j1y))) |
| 18 | muzer | 254 | return true; |
| 255 | } |
||
| 256 | } |
||
| 20 | muzer | 257 | if(jy == j1y) |
| 18 | muzer | 258 | { |
| 16 | muzer | 259 | |
| 40 | muzer | 260 | if((iy > jy && i1y < jy) || (iy < jy && i1y > jy)) |
| 18 | muzer | 261 | { |
| 20 | muzer | 262 | if((sign(ix - jx) != sign(ix - j1x))) |
| 18 | muzer | 263 | return true; |
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 9 | muzer | 268 | } |
| 269 | return false; |
||
| 8 | tom | 270 | } |
| 271 | |||
| 30 | tom | 272 | void Bike::setText(QString text) |
| 273 | { |
||
| 36 | muzer | 274 | socket->write(text.toAscii().data()); |
| 275 | socket->flush(); |
||
| 30 | tom | 276 | } |
| 277 | |||
| 1 | muzer | 278 | void Bike::reset() |
| 279 | { |
||
| 36 | muzer | 280 | cout << "Reset was called\n"; |
| 40 | muzer | 281 | x = rand() % 80000; |
| 282 | y = rand() % 60000; |
||
| 41 | muzer | 283 | cout << "x: " << x << " y: " << y << endl; |
| 9 | muzer | 284 | linePoints.clear(); |
| 1 | muzer | 285 | |
| 41 | muzer | 286 | //linePoints.append(QPoint(x, y)); |
| 1 | muzer | 287 | |
| 40 | muzer | 288 | velocity = 100; |
| 9 | muzer | 289 | angle = 0; |
| 19 | muzer | 290 | abpool = 0; |
| 9 | muzer | 291 | show = true; |
| 292 | isReady = true; |
||
| 293 | hadGo = false; |
||
| 294 | dead = false; |
||
| 295 | collided = false; |
||
| 40 | muzer | 296 | speed = 500; |
| 1 | muzer | 297 | |
| 9 | muzer | 298 | socket->write("RESET\n"); |
| 1 | muzer | 299 | } |
| 300 | |||
| 40 | muzer | 301 | void Bike::greet() |
| 302 | { |
||
| 303 | cout << "Greeting\n"; |
||
| 304 | socket->write("ARENA 80000 60000\n"); |
||
| 305 | } |
||
| 306 | |||
| 1 | muzer | 307 | void Bike::readyRead() |
| 308 | { |
||
| 9 | muzer | 309 | while (socket->canReadLine()) |
| 310 | { |
||
| 311 | QByteArray data = socket->readLine(); |
||
| 312 | QString line = data.trimmed(); |
||
| 40 | muzer | 313 | couldReadLine = true; |
| 1 | muzer | 314 | |
| 9 | muzer | 315 | if (line == "L") |
| 316 | { |
||
| 317 | angle -= 90; |
||
| 1 | muzer | 318 | |
| 9 | muzer | 319 | if (angle >= 360) |
| 320 | { |
||
| 321 | angle -= 360; |
||
| 322 | } |
||
| 323 | if (angle < 0) |
||
| 324 | { |
||
| 325 | angle += 360; |
||
| 326 | } |
||
| 36 | muzer | 327 | if(velocity < speed) |
| 40 | muzer | 328 | velocity += 30; |
| 36 | muzer | 329 | else if(velocity > speed) |
| 40 | muzer | 330 | velocity -= 30; |
| 331 | if(abs(speed-velocity)<30) |
||
| 36 | muzer | 332 | velocity = speed; |
| 19 | muzer | 333 | if(abpool<10) |
| 334 | abpool += 0.2; |
||
| 9 | muzer | 335 | hadGo = true; |
| 336 | } |
||
| 337 | else if (line == "R") |
||
| 338 | { |
||
| 339 | angle += 90; |
||
| 1 | muzer | 340 | |
| 9 | muzer | 341 | if (angle >= 360) |
| 342 | { |
||
| 343 | angle -= 360; |
||
| 344 | } |
||
| 345 | if (angle < 0) |
||
| 346 | { |
||
| 347 | angle += 360; |
||
| 348 | } |
||
| 36 | muzer | 349 | if(velocity < speed) |
| 40 | muzer | 350 | velocity += 30; |
| 36 | muzer | 351 | else if(velocity > speed) |
| 40 | muzer | 352 | velocity -= 30; |
| 353 | if(abs(speed-velocity)<30) |
||
| 36 | muzer | 354 | velocity = speed; |
| 19 | muzer | 355 | if(abpool<10) |
| 356 | abpool += 0.2; |
||
| 9 | muzer | 357 | hadGo = true; |
| 358 | } |
||
| 359 | else if (line == "A") |
||
| 360 | { |
||
| 19 | muzer | 361 | if(abpool > 0){ |
| 40 | muzer | 362 | velocity += 20; |
| 36 | muzer | 363 | abpool -= 0.5; |
| 20 | muzer | 364 | } else { |
| 36 | muzer | 365 | if(velocity < speed) |
| 40 | muzer | 366 | velocity += 30; |
| 36 | muzer | 367 | else if(velocity > speed) |
| 40 | muzer | 368 | velocity -= 30; |
| 369 | if(abs(speed-velocity)<30) |
||
| 36 | muzer | 370 | velocity = speed; |
| 19 | muzer | 371 | } |
| 9 | muzer | 372 | hadGo = true; |
| 373 | } |
||
| 374 | else if (line == "D") |
||
| 375 | { |
||
| 19 | muzer | 376 | if(abpool > 0){ |
| 40 | muzer | 377 | velocity -= 20; |
| 19 | muzer | 378 | abpool -= 0.5; |
| 379 | } |
||
| 20 | muzer | 380 | else { |
| 36 | muzer | 381 | if(velocity < speed) |
| 40 | muzer | 382 | velocity += 30; |
| 36 | muzer | 383 | else if(velocity > speed) |
| 40 | muzer | 384 | velocity -= 30; |
| 385 | if(abs(speed-velocity)<30) |
||
| 36 | muzer | 386 | velocity = speed; |
| 20 | muzer | 387 | } |
| 9 | muzer | 388 | hadGo = true; |
| 389 | } |
||
| 390 | else if (line == "N") |
||
| 391 | { |
||
| 28 | muzer | 392 | |
| 36 | muzer | 393 | if(velocity < speed) |
| 40 | muzer | 394 | velocity += 30; |
| 36 | muzer | 395 | else if(velocity > speed) |
| 40 | muzer | 396 | velocity -= 30; |
| 397 | if(abs(speed-velocity)<30) |
||
| 36 | muzer | 398 | velocity = speed; |
| 19 | muzer | 399 | if(abpool<10) |
| 400 | abpool += 0.2; |
||
| 9 | muzer | 401 | hadGo = true; |
| 402 | } |
||
| 403 | else if (line.startsWith("NAME ")) |
||
| 404 | { |
||
| 405 | name = line.remove(0, 5); |
||
| 406 | isReady = true; |
||
| 407 | show = true; |
||
| 408 | } |
||
| 409 | else if (line.startsWith("COLOUR ")) |
||
| 410 | { |
||
| 411 | QStringList list = line.split(" "); |
||
| 412 | if (list.count() >= 2) |
||
| 413 | { |
||
| 414 | colour.setRed(list[1].toInt()); |
||
| 415 | } |
||
| 416 | if (list.count() >= 3) |
||
| 417 | { |
||
| 418 | colour.setGreen(list[2].toInt()); |
||
| 419 | } |
||
| 420 | if (list.count() >= 4) |
||
| 421 | { |
||
| 422 | colour.setBlue(list[3].toInt()); |
||
| 423 | } |
||
| 424 | } |
||
| 36 | muzer | 425 | else if (line.startsWith("CHAT ")) |
| 426 | { |
||
| 427 | QString message = line.remove(0, 5); |
||
| 31 | tom | 428 | |
| 36 | muzer | 429 | if (!message.isEmpty()) |
| 430 | { |
||
| 431 | emit chat(name, message); |
||
| 432 | } |
||
| 31 | tom | 433 | |
| 40 | muzer | 434 | if(velocity < speed) |
| 435 | velocity += 30; |
||
| 436 | else if(velocity > speed) |
||
| 437 | velocity -= 30; |
||
| 438 | if(abs(speed-velocity)<30) |
||
| 439 | velocity = speed; |
||
| 440 | if(abpool<10) |
||
| 441 | abpool += 0.2; |
||
| 442 | |||
| 36 | muzer | 443 | hadGo = true; |
| 444 | } |
||
| 9 | muzer | 445 | } |
| 1 | muzer | 446 | } |
| 447 | |||
| 448 | void Bike::disconnected() |
||
| 449 | { |
||
| 9 | muzer | 450 | dead = true; |
| 451 | isDisconnected = true; |
||
| 452 | cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl; |
||
| 1 | muzer | 453 | } |