Subversion Repositories QTron

Compare Revisions

Ignore whitespace Rev 39 → Rev 44

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