Subversion Repositories QTron

Rev

Rev 43 | 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;
44 muzer 38
        //isReady = true;
9 muzer 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();
44 muzer 313
        std::cout << "Bike " << name.toStdString() << " " << line.toStdString()
314
          << std::endl;
40 muzer 315
        couldReadLine = true;
1 muzer 316
 
9 muzer 317
        if (line == "L")
318
        {
319
            angle -= 90;
1 muzer 320
 
9 muzer 321
            if (angle >= 360)
322
            {
323
                angle -= 360;
324
            }
325
            if (angle < 0)
326
            {
327
                angle += 360;
328
            }
36 muzer 329
            if(velocity < speed)
40 muzer 330
                velocity += 30;
36 muzer 331
            else if(velocity > speed)
40 muzer 332
                velocity -= 30;
333
            if(abs(speed-velocity)<30)
36 muzer 334
                velocity = speed;
19 muzer 335
            if(abpool<10)
336
                abpool += 0.2;
9 muzer 337
            hadGo = true;
338
        }
339
        else if (line == "R")
340
        {
341
            angle += 90;
1 muzer 342
 
9 muzer 343
            if (angle >= 360)
344
            {
345
                angle -= 360;
346
            }
347
            if (angle < 0)
348
            {
349
                angle += 360;
350
            }
36 muzer 351
            if(velocity < speed)
40 muzer 352
                velocity += 30;
36 muzer 353
            else if(velocity > speed)
40 muzer 354
                velocity -= 30;
355
            if(abs(speed-velocity)<30)
36 muzer 356
                velocity = speed;
19 muzer 357
            if(abpool<10)
358
                abpool += 0.2;
9 muzer 359
            hadGo = true;
360
        }
361
        else if (line == "A")
362
        {
19 muzer 363
            if(abpool > 0){
40 muzer 364
                velocity += 20;
36 muzer 365
                abpool -= 0.5;
20 muzer 366
            } else {
36 muzer 367
                if(velocity < speed)
40 muzer 368
                    velocity += 30;
36 muzer 369
                else if(velocity > speed)
40 muzer 370
                    velocity -= 30;
371
                if(abs(speed-velocity)<30)
36 muzer 372
                    velocity = speed;
19 muzer 373
            }
9 muzer 374
            hadGo = true;
375
        }
376
        else if (line == "D")
377
        {
19 muzer 378
            if(abpool > 0){
40 muzer 379
                velocity -= 20;
19 muzer 380
                abpool -= 0.5;
381
            }
20 muzer 382
            else {
36 muzer 383
                if(velocity < speed)
40 muzer 384
                    velocity += 30;
36 muzer 385
                else if(velocity > speed)
40 muzer 386
                    velocity -= 30;
387
                if(abs(speed-velocity)<30)
36 muzer 388
                    velocity = speed;
20 muzer 389
            }
9 muzer 390
            hadGo = true;
391
        }
392
        else if (line == "N")
393
        {
28 muzer 394
 
36 muzer 395
            if(velocity < speed)
40 muzer 396
                velocity += 30;
36 muzer 397
            else if(velocity > speed)
40 muzer 398
                velocity -= 30;
399
            if(abs(speed-velocity)<30)
36 muzer 400
                velocity = speed;
19 muzer 401
            if(abpool<10)
402
                abpool += 0.2;
9 muzer 403
            hadGo = true;
404
        }
405
        else if (line.startsWith("NAME "))
406
        {
407
            name = line.remove(0, 5);
408
            isReady = true;
409
            show = true;
410
        }
411
        else if (line.startsWith("COLOUR "))
412
        {
413
            QStringList list = line.split(" ");
414
            if (list.count() >= 2)
415
            {
416
                colour.setRed(list[1].toInt());
417
            }
418
            if (list.count() >= 3)
419
            {
420
                colour.setGreen(list[2].toInt());
421
            }
422
            if (list.count() >= 4)
423
            {
424
                colour.setBlue(list[3].toInt());
425
            }
426
        }
36 muzer 427
        else if (line.startsWith("CHAT "))
428
        {
429
            QString message = line.remove(0, 5);
31 tom 430
 
36 muzer 431
            if (!message.isEmpty())
432
            {
433
                emit chat(name, message);
434
            }
31 tom 435
 
40 muzer 436
            if(velocity < speed)
437
                velocity += 30;
438
            else if(velocity > speed)
439
                velocity -= 30;
440
            if(abs(speed-velocity)<30)
441
                velocity = speed;
442
            if(abpool<10)
443
                abpool += 0.2;
444
 
36 muzer 445
            hadGo = true;
446
        }
9 muzer 447
    }
1 muzer 448
}
449
 
450
void Bike::disconnected()
451
{
9 muzer 452
    dead = true;
453
    isDisconnected = true;
454
    cout << ":: Disconnected: " << socket->peerAddress().toString().toStdString() << endl;
1 muzer 455
}