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