Subversion Repositories QTron

Rev

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