Subversion Repositories QTron

Rev

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