Subversion Repositories QTron

Compare Revisions

Ignore whitespace Rev 44 → Rev 39

/bike.h
25,13 → 25,12
bool hasCollided(QList<Bike *> bikes);
void setText(QString text);
void reset();
void greet();
bool isReady;
bool dead;
bool show;
bool hadGo;
int x;
int y;
float x;
float y;
QString name;
int angle;
QList<QPoint> linePoints;
46,7 → 45,6
float speed;
QTcpSocket *socket;
bool haveSentAlready;
bool couldReadLine;
 
private slots:
void disconnected();
/QTron.pro
6,10 → 6,8
TEMPLATE = app
SOURCES += main.cpp \
mainwindow.cpp \
bike.cpp \
speccy.cpp
bike.cpp
HEADERS += mainwindow.h \
bike.h \
speccy.h
bike.h
FORMS +=
OTHER_FILES += docs.txt
/mainwindow.h
2,7 → 2,6
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QApplication>
#include <QPaintEvent>
#include <QTcpServer>
#include <QList>
11,7 → 10,6
 
 
#include "bike.h"
#include "speccy.h"
 
class MainWindow : public QGLWidget
{
27,9 → 25,7
 
private:
QTcpServer *server;
QTcpServer *respec;
QList<Bike *> bikes;
QList<Speccy *> specs;
QTimer *timer;
int id;
QTimer *suddenDeathTimer;
38,7 → 34,6
 
private slots:
void newConnection();
void newSpec();
void checkClients();
void chat(QString name, QString message);
void activateSuddenDeath();
/bike.cpp
10,11 → 10,11
 
isDisconnected = false;
 
x = rand() % 80000;
y = rand() % 60000;
//linePoints.append(QPoint(x, y));
x = rand() % 800;
y = rand() % 600;
linePoints.append(QPoint(x, y));
 
velocity = 100;
velocity = 1;
angle = (rand() % 4) * 90;
abpool = 0;
name = "";
23,19 → 23,17
hadGo = false;
dead = false;
collided = false;
speed = 500;
speed = 5;
 
colour.setBlue(0);
colour.setRed(0);
colour.setGreen(0);
 
greet();
 
if (!socket->waitForReadyRead(2000))
{
socket->disconnectFromHost();
show = false;
//isReady = true;
isReady = true;
hadGo = false;
}
}
62,7 → 60,7
 
point2 = linePoints[i];
 
painter->drawLine(point1/100., point2/100.);
painter->drawLine(point1, point2);
}
 
if (!collided)
86,13 → 84,13
}
if (angle == 0 || angle == 180)
{
painter->fillRect(x/100. - 5, y/100. - 15, 10, 30, colour);
painter->fillRect(x - 5, y - 15, 10, 30, colour);
}
else
{
painter->fillRect(x/100. - 15, y/100. - 5, 30, 10, colour);
painter->fillRect(x - 15, y - 5, 30, 10, colour);
}
painter->drawText(x/100., y/100. - 20, name);
painter->drawText(x, y - 20, name);
}
else
{
160,7 → 158,6
}
 
hadGo = false;
couldReadLine = false;
if (!socket->waitForReadyRead(1))
{
// socket->disconnectFromHost();
168,19 → 165,16
haveSentAlready = true;
return;
}
if (!hadGo && couldReadLine)
if (!hadGo)
{
cout << "KILLING BECAUSE HE DIDN'T HAVE A GO\n";
socket->disconnectFromHost();
dead = true;
}
}
if(hadGo || dead)
hasHadGo = true;
hasHadGo = true;
 
time_t result = time(NULL);
if(hasHadGo)
cout << result << ": Recieved next move from " << name.toStdString().c_str() << endl;
cout << result << ": Recieved next move from " << name.toStdString().c_str() << endl;
}
 
int sign(int x){
198,11 → 192,9
// Do collision detection here
// use linePoints
int i = linePoints.count() - 1;
if(i <= 0)
return false;
if(linePoints[i-1].x() < 0 || linePoints[i-1].x() >= 80000 || linePoints[i-1].y() < 0 || linePoints[i-1].y() >= 60000)
if(linePoints[i-1].x() < 0 || linePoints[i-1].x() > 800 || linePoints[i-1].y() < 0 || linePoints[i-1].y() > 600)
return true;
if(linePoints[i].x() < 0 || linePoints[i].x() >= 80000 || linePoints[i].y() < 0 || linePoints[i].y() >= 60000)
if(linePoints[i].x() < 0 || linePoints[i].x() > 800 || linePoints[i].y() < 0 || linePoints[i].y() > 600)
return true;
int j, r;
Bike *bike;
248,7 → 240,7
{
// x equal
 
if((ix > jx && i1x < jx) || (ix < jx && i1x > jx))
if(ix > jx && i1x < jx || ix < jx && i1x > jx)
{
if((sign(iy - jy) != sign(iy - j1y)))
return true;
257,7 → 249,7
if(jy == j1y)
{
 
if((iy > jy && i1y < jy) || (iy < jy && i1y > jy))
if(iy > jy && i1y < jy || iy < jy && i1y > jy)
{
if((sign(ix - jx) != sign(ix - j1x)))
return true;
278,14 → 270,13
void Bike::reset()
{
cout << "Reset was called\n";
x = rand() % 80000;
y = rand() % 60000;
cout << "x: " << x << " y: " << y << endl;
x = rand() % 800;
y = rand() % 600;
linePoints.clear();
 
//linePoints.append(QPoint(x, y));
linePoints.append(QPoint(x, y));
 
velocity = 100;
velocity = 1;
angle = 0;
abpool = 0;
show = true;
293,17 → 284,11
hadGo = false;
dead = false;
collided = false;
speed = 500;
speed = 5;
 
socket->write("RESET\n");
}
 
void Bike::greet()
{
cout << "Greeting\n";
socket->write("ARENA 80000 60000\n");
}
 
void Bike::readyRead()
{
while (socket->canReadLine())
310,9 → 295,6
{
QByteArray data = socket->readLine();
QString line = data.trimmed();
std::cout << "Bike " << name.toStdString() << " " << line.toStdString()
<< std::endl;
couldReadLine = true;
 
if (line == "L")
{
327,10 → 309,10
angle += 360;
}
if(velocity < speed)
velocity += 30;
velocity += 0.3;
else if(velocity > speed)
velocity -= 30;
if(abs(speed-velocity)<30)
velocity -= 0.3;
if(abs(speed-velocity)<0.3)
velocity = speed;
if(abpool<10)
abpool += 0.2;
349,10 → 331,10
angle += 360;
}
if(velocity < speed)
velocity += 30;
velocity += 0.3;
else if(velocity > speed)
velocity -= 30;
if(abs(speed-velocity)<30)
velocity -= 0.3;
if(abs(speed-velocity)<0.3)
velocity = speed;
if(abpool<10)
abpool += 0.2;
361,14 → 343,14
else if (line == "A")
{
if(abpool > 0){
velocity += 20;
velocity += 0.1;
abpool -= 0.5;
} else {
if(velocity < speed)
velocity += 30;
velocity += 0.3;
else if(velocity > speed)
velocity -= 30;
if(abs(speed-velocity)<30)
velocity -= 0.3;
if(abs(speed-velocity)<0.3)
velocity = speed;
}
hadGo = true;
376,15 → 358,15
else if (line == "D")
{
if(abpool > 0){
velocity -= 20;
velocity -= 0.2;
abpool -= 0.5;
}
else {
if(velocity < speed)
velocity += 30;
velocity += 0.3;
else if(velocity > speed)
velocity -= 30;
if(abs(speed-velocity)<30)
velocity -= 0.3;
if(abs(speed-velocity)<0.3)
velocity = speed;
}
hadGo = true;
393,10 → 375,10
{
 
if(velocity < speed)
velocity += 30;
velocity += 0.3;
else if(velocity > speed)
velocity -= 30;
if(abs(speed-velocity)<30)
velocity -= 0.3;
if(abs(speed-velocity)<0.3)
velocity = speed;
if(abpool<10)
abpool += 0.2;
433,15 → 415,6
emit chat(name, message);
}
 
if(velocity < speed)
velocity += 30;
else if(velocity > speed)
velocity -= 30;
if(abs(speed-velocity)<30)
velocity = speed;
if(abpool<10)
abpool += 0.2;
 
hadGo = true;
}
}
/mainwindow.cpp
8,10 → 8,6
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
server->listen(QHostAddress::Any, 4567);
 
respec = new QTcpServer(this);
connect(respec, SIGNAL(newConnection()), this, SLOT(newSpec()));
respec->listen(QHostAddress::Any, 4568);
 
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(50);
61,11 → 57,6
{
bikes[i]->reset();
}
 
foreach (Speccy *spec, specs)
{
spec->reset();
}
}
 
void MainWindow::paintEvent(QPaintEvent *e)
98,12 → 89,7
{
// cout << ":: Sent move request..." << endl;
bool alldone = false;
time_t start = time(NULL);
foreach (Speccy *spec, specs)
{
spec->bikes = bikes;
spec->run();
}
timer2->start(10000);
while(!alldone){
for (int i = 0; i < bikes.count(); i++)
{
119,19 → 105,15
alldone = false;
}
}
if(time(NULL) - 10 > start)
clientTimeOut();
}
timer2->stop();
}
 
if(ready)
for (int i = 0; i < bikes.count(); i++)
{
for (int i = 0; i < bikes.count(); i++)
if (bikes[i]->show)
{
if (bikes[i]->show)
{
bikes[i]->draw(&painter,bikes);
}
bikes[i]->draw(&painter,bikes);
}
}
 
154,26 → 136,10
}
}
 
void MainWindow::newSpec()
{
while(respec->hasPendingConnections())
{
QTcpSocket *socket = respec->nextPendingConnection();
Speccy *speccy = new Speccy(socket);
specs.append(speccy);
cout << ":: RESPEC!!!!!!! " << socket->peerAddress().toString().toStdString() << endl;
}
}
 
void MainWindow::checkClients()
{
bool everyoneDied = true;
 
foreach (Speccy *spec, specs)
{
if(spec->isDisconnected)
specs.removeAll(spec);
}
for (int i = bikes.count() - 1; i >= 0; i--)
{
if (bikes[i]->isDisconnected)
180,8 → 146,7
{
for (int j = 0; j < bikes.count(); j++)
{
if(bikes[i]->isReady)
bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n"));
bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n"));
}
 
delete bikes[i];
216,11 → 181,6
packet.append(message);
packet.append("\n");
 
foreach (Speccy *spec, specs)
{
spec->setText(packet);
}
 
for (int i = 0; i < bikes.count(); i++)
{
bikes[i]->setText(packet);
232,7 → 192,6
if (!suddenDeath)
{
suddenDeath = true;
cout << "SUDDEN DEATH ACTIVATED!" << endl;
chat("Server", "SUDDEN DEATH ACTIVATED!");
}
}