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