Rev 40 | 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 | } |
||
| 178 | hasHadGo = true; |
||
| 34 | tom | 179 | |
| 180 | time_t result = time(NULL); |
||
| 181 | cout << result << ": Recieved next move from " << name.toStdString().c_str() << endl; |
||
| 1 | muzer | 182 | } |
| 183 | |||
| 19 | muzer | 184 | int sign(int x){ |
| 185 | if(x > 0){ |
||
| 9 | muzer | 186 | return 1; |
| 187 | } |
||
| 19 | muzer | 188 | if(x < 0){ |
| 9 | muzer | 189 | return -1; |
| 190 | } |
||
| 191 | return 0; |
||
| 192 | } |
||
| 193 | |||
| 14 | muzer | 194 | bool Bike::hasCollided(QList<Bike *> bikes) |
| 8 | tom | 195 | { |
| 9 | muzer | 196 | // Do collision detection here |
| 197 | // use linePoints |
||
| 198 | int i = linePoints.count() - 1; |
||
| 41 | muzer | 199 | if(i <= 0) |
| 200 | return false; |
||
| 40 | muzer | 201 | if(linePoints[i-1].x() < 0 || linePoints[i-1].x() >= 80000 || linePoints[i-1].y() < 0 || linePoints[i-1].y() >= 60000) |
| 9 | muzer | 202 | return true; |
| 40 | muzer | 203 | if(linePoints[i].x() < 0 || linePoints[i].x() >= 80000 || linePoints[i].y() < 0 || linePoints[i].y() >= 60000) |
| 9 | muzer | 204 | return true; |
| 18 | muzer | 205 | int j, r; |
| 14 | muzer | 206 | Bike *bike; |
| 19 | muzer | 207 | for(r = 0; r < bikes.count(); r++) |
| 18 | muzer | 208 | { |
| 209 | bike = bikes[r]; |
||
| 21 | muzer | 210 | int forsubtract = 1; |
| 211 | if(bike->name == name){ |
||
| 212 | forsubtract = 3; |
||
| 213 | } |
||
| 214 | for(j = 0; j < bike->linePoints.count() - forsubtract; j++) |
||
| 18 | muzer | 215 | { |
| 20 | muzer | 216 | int jx = bike->linePoints[j].x(); |
| 217 | int j1x = bike->linePoints[j+1].x(); |
||
| 218 | int jy = bike->linePoints[j].y(); |
||
| 219 | int j1y = bike->linePoints[j+1].y(); |
||
| 220 | int ix = linePoints[i-1].x(); |
||
| 221 | int i1x = linePoints[i].x(); |
||
| 222 | int iy = linePoints[i-1].y(); |
||
| 223 | int i1y = linePoints[i].y(); |
||
| 21 | muzer | 224 | |
| 20 | muzer | 225 | if(angle == 0){ |
| 226 | iy += 1; |
||
| 227 | i1y -= 1; |
||
| 228 | } |
||
| 229 | if(angle == 90){ |
||
| 230 | ix -= 1; |
||
| 231 | i1x += 1; |
||
| 232 | } |
||
| 233 | if(angle == 180){ |
||
| 234 | iy -= 1; |
||
| 235 | i1y += 1; |
||
| 236 | } |
||
| 237 | if(angle == 270){ |
||
| 238 | ix += 1; |
||
| 239 | i1x -= 1; |
||
| 240 | } |
||
| 241 | if(!(jx == j1x && i1x == ix) && !(jy == j1y && i1y == iy)) |
||
| 18 | muzer | 242 | { |
| 16 | muzer | 243 | |
| 18 | muzer | 244 | // If not parallel |
| 20 | muzer | 245 | if(jx == j1x) |
| 18 | muzer | 246 | { |
| 247 | // x equal |
||
| 16 | muzer | 248 | |
| 40 | muzer | 249 | if((ix > jx && i1x < jx) || (ix < jx && i1x > jx)) |
| 18 | muzer | 250 | { |
| 20 | muzer | 251 | if((sign(iy - jy) != sign(iy - j1y))) |
| 18 | muzer | 252 | return true; |
| 253 | } |
||
| 254 | } |
||
| 20 | muzer | 255 | if(jy == j1y) |
| 18 | muzer | 256 | { |
| 16 | muzer | 257 | |
| 40 | muzer | 258 | if((iy > jy && i1y < jy) || (iy < jy && i1y > jy)) |
| 18 | muzer | 259 | { |
| 20 | muzer | 260 | if((sign(ix - jx) != sign(ix - j1x))) |
| 18 | muzer | 261 | return true; |
| 262 | } |
||
| 263 | } |
||
| 264 | } |
||
| 265 | } |
||
| 9 | muzer | 266 | } |
| 267 | return false; |
||
| 8 | tom | 268 | } |
| 269 | |||
| 30 | tom | 270 | void Bike::setText(QString text) |
| 271 | { |
||
| 36 | muzer | 272 | socket->write(text.toAscii().data()); |
| 273 | socket->flush(); |
||
| 30 | tom | 274 | } |
| 275 | |||
| 1 | muzer | 276 | void Bike::reset() |
| 277 | { |
||
| 36 | muzer | 278 | cout << "Reset was called\n"; |
| 40 | muzer | 279 | x = rand() % 80000; |
| 280 | y = rand() % 60000; |
||
| 41 | muzer | 281 | cout << "x: " << x << " y: " << y << endl; |
| 9 | muzer | 282 | linePoints.clear(); |
| 1 | muzer | 283 | |
| 41 | muzer | 284 | //linePoints.append(QPoint(x, y)); |
| 1 | muzer | 285 | |
| 40 | muzer | 286 | velocity = 100; |
| 9 | muzer | 287 | angle = 0; |
| 19 | muzer | 288 | abpool = 0; |
| 9 | muzer | 289 | show = true; |
| 290 | isReady = true; |
||
| 291 | hadGo = false; |
||
| 292 | dead = false; |
||
| 293 | collided = false; |
||
| 40 | muzer | 294 | speed = 500; |
| 1 | muzer | 295 | |
| 9 | muzer | 296 | socket->write("RESET\n"); |
| 1 | muzer | 297 | } |
| 298 | |||
| 40 | muzer | 299 | void Bike::greet() |
| 300 | { |
||
| 301 | cout << "Greeting\n"; |
||
| 302 | socket->write("ARENA 80000 60000\n"); |
||
| 303 | } |
||
| 304 | |||
| 1 | muzer | 305 | void Bike::readyRead() |
| 306 | { |
||
| 9 | muzer | 307 | while (socket->canReadLine()) |
| 308 | { |
||
| 309 | QByteArray data = socket->readLine(); |
||
| 310 | QString line = data.trimmed(); |
||
| 40 | muzer | 311 | couldReadLine = true; |
| 1 | muzer | 312 | |
| 9 | muzer | 313 | if (line == "L") |
| 314 | { |
||
| 315 | angle -= 90; |
||
| 1 | muzer | 316 | |
| 9 | muzer | 317 | if (angle >= 360) |
| 318 | { |
||
| 319 | angle -= 360; |
||
| 320 | } |
||
| 321 | if (angle < 0) |
||
| 322 | { |
||
| 323 | angle += 360; |
||
| 324 | } |
||
| 36 | muzer | 325 | if(velocity < speed) |
| 40 | muzer | 326 | velocity += 30; |
| 36 | muzer | 327 | else if(velocity > speed) |
| 40 | muzer | 328 | velocity -= 30; |
| 329 | if(abs(speed-velocity)<30) |
||
| 36 | muzer | 330 | velocity = speed; |
| 19 | muzer | 331 | if(abpool<10) |
| 332 | abpool += 0.2; |
||
| 9 | muzer | 333 | hadGo = true; |
| 334 | } |
||
| 335 | else if (line == "R") |
||
| 336 | { |
||
| 337 | angle += 90; |
||
| 1 | muzer | 338 | |
| 9 | muzer | 339 | if (angle >= 360) |
| 340 | { |
||
| 341 | angle -= 360; |
||
| 342 | } |
||
| 343 | if (angle < 0) |
||
| 344 | { |
||
| 345 | angle += 360; |
||
| 346 | } |
||
| 36 | muzer | 347 | if(velocity < speed) |
| 40 | muzer | 348 | velocity += 30; |
| 36 | muzer | 349 | else if(velocity > speed) |
| 40 | muzer | 350 | velocity -= 30; |
| 351 | if(abs(speed-velocity)<30) |
||
| 36 | muzer | 352 | velocity = speed; |
| 19 | muzer | 353 | if(abpool<10) |
| 354 | abpool += 0.2; |
||
| 9 | muzer | 355 | hadGo = true; |
| 356 | } |
||
| 357 | else if (line == "A") |
||
| 358 | { |
||
| 19 | muzer | 359 | if(abpool > 0){ |
| 40 | muzer | 360 | velocity += 20; |
| 36 | muzer | 361 | abpool -= 0.5; |
| 20 | muzer | 362 | } else { |
| 36 | muzer | 363 | if(velocity < speed) |
| 40 | muzer | 364 | velocity += 30; |
| 36 | muzer | 365 | else if(velocity > speed) |
| 40 | muzer | 366 | velocity -= 30; |
| 367 | if(abs(speed-velocity)<30) |
||
| 36 | muzer | 368 | velocity = speed; |
| 19 | muzer | 369 | } |
| 9 | muzer | 370 | hadGo = true; |
| 371 | } |
||
| 372 | else if (line == "D") |
||
| 373 | { |
||
| 19 | muzer | 374 | if(abpool > 0){ |
| 40 | muzer | 375 | velocity -= 20; |
| 19 | muzer | 376 | abpool -= 0.5; |
| 377 | } |
||
| 20 | muzer | 378 | else { |
| 36 | muzer | 379 | if(velocity < speed) |
| 40 | muzer | 380 | velocity += 30; |
| 36 | muzer | 381 | else if(velocity > speed) |
| 40 | muzer | 382 | velocity -= 30; |
| 383 | if(abs(speed-velocity)<30) |
||
| 36 | muzer | 384 | velocity = speed; |
| 20 | muzer | 385 | } |
| 9 | muzer | 386 | hadGo = true; |
| 387 | } |
||
| 388 | else if (line == "N") |
||
| 389 | { |
||
| 28 | muzer | 390 | |
| 36 | muzer | 391 | if(velocity < speed) |
| 40 | muzer | 392 | velocity += 30; |
| 36 | muzer | 393 | else if(velocity > speed) |
| 40 | muzer | 394 | velocity -= 30; |
| 395 | if(abs(speed-velocity)<30) |
||
| 36 | muzer | 396 | velocity = speed; |
| 19 | muzer | 397 | if(abpool<10) |
| 398 | abpool += 0.2; |
||
| 9 | muzer | 399 | hadGo = true; |
| 400 | } |
||
| 401 | else if (line.startsWith("NAME ")) |
||
| 402 | { |
||
| 403 | name = line.remove(0, 5); |
||
| 404 | isReady = true; |
||
| 405 | show = true; |
||
| 406 | } |
||
| 407 | else if (line.startsWith("COLOUR ")) |
||
| 408 | { |
||
| 409 | QStringList list = line.split(" "); |
||
| 410 | if (list.count() >= 2) |
||
| 411 | { |
||
| 412 | colour.setRed(list[1].toInt()); |
||
| 413 | } |
||
| 414 | if (list.count() >= 3) |
||
| 415 | { |
||
| 416 | colour.setGreen(list[2].toInt()); |
||
| 417 | } |
||
| 418 | if (list.count() >= 4) |
||
| 419 | { |
||
| 420 | colour.setBlue(list[3].toInt()); |
||
| 421 | } |
||
| 422 | } |
||
| 36 | muzer | 423 | else if (line.startsWith("CHAT ")) |
| 424 | { |
||
| 425 | QString message = line.remove(0, 5); |
||
| 31 | tom | 426 | |
| 36 | muzer | 427 | if (!message.isEmpty()) |
| 428 | { |
||
| 429 | emit chat(name, message); |
||
| 430 | } |
||
| 31 | tom | 431 | |
| 40 | muzer | 432 | if(velocity < speed) |
| 433 | velocity += 30; |
||
| 434 | else if(velocity > speed) |
||
| 435 | velocity -= 30; |
||
| 436 | if(abs(speed-velocity)<30) |
||
| 437 | velocity = speed; |
||
| 438 | if(abpool<10) |
||
| 439 | abpool += 0.2; |
||
| 440 | |||
| 36 | muzer | 441 | hadGo = true; |
| 442 | } |
||
| 9 | muzer | 443 | } |
| 1 | muzer | 444 | } |
| 445 | |||
| 446 | void Bike::disconnected() |
||
| 447 | { |
||
| 9 | muzer | 448 | dead = true; |
| 449 | isDisconnected = true; |
||
| 450 | cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl; |
||
| 1 | muzer | 451 | } |