This repository has been archived on 2024-10-30. You can view files and clone it, but cannot push or open issues or pull requests.
SDi-STOMP/app.cpp

150 lines
4.0 KiB
C++
Raw Permalink Normal View History

2023-11-29 10:53:20 +00:00
#include "app.h"
2023-12-28 13:57:13 +00:00
2023-12-26 17:10:52 +00:00
App::App(Stomp* st) {
st_ = st;
st_->connectRequest("sdi.hevs.ch", 61614, "/", "sdi10", "809c02f36becb0868da98761fe3209f6");
st_->subscribeRequest("/topic/sdi10.gem.field", 1);
2023-11-29 10:53:20 +00:00
}
2023-11-30 08:53:53 +00:00
2023-12-26 17:10:52 +00:00
void App::connectConfirmation(bool success, QString version) {
2023-11-30 08:53:53 +00:00
}
void App::sendConfirmation(bool success) {
}
void App::subscribeConfirmation(bool success) {
}
void App::receiveIndication(int id, QString destination, QString body) {
2023-12-26 17:10:52 +00:00
//qDebug() << "Indication " << id << " : " << destination << Qt::endl << body << Qt::endl;
if(destination.contains("field")){
fillField(body);
}
2023-12-26 21:09:42 +00:00
computeMove();
2023-11-30 08:53:53 +00:00
}
void App::disconnectConfirmation() {
}
void App::disconnectIndication() {
}
2023-12-26 17:10:52 +00:00
void App::addGem(int x, int y, int pts) {
Gem g;
2023-12-28 13:57:13 +00:00
g.coordinate = Vector2D(Vector2D::CreatePoint(x, GRID_SIZE-y));
2023-12-26 17:10:52 +00:00
g.pts = pts;
gems_.append(g);
}
void App::fillField(QString body) {
2023-12-26 21:09:42 +00:00
int x = 0;
int y = 0;
gems_.clear();
2023-12-26 17:10:52 +00:00
for(int i = 0; i<body.length(); i++) {
const QChar c = body.at(i);
if(c == 'Y') {
2023-12-28 13:57:13 +00:00
myVehicle_ = Vector2D(Vector2D::CreatePoint(x, GRID_SIZE-y));
2023-12-26 17:10:52 +00:00
}
2023-12-28 17:15:53 +00:00
if(c == 'h') {
otherVehicles_.append(Vector2D(Vector2D::CreatePoint(x, GRID_SIZE-y)));
}
2023-12-26 21:09:42 +00:00
if(c == 'g') addGem(x, y, 100);
if(c == 'G') addGem(x, y, 250);
if(c == 'D') addGem(x, y, 500);
2023-12-26 17:10:52 +00:00
if(c == '\n') {
y++;
x = 0;
2023-12-26 21:09:42 +00:00
} else {
x++;
2023-12-26 17:10:52 +00:00
}
}
}
2023-12-26 21:09:42 +00:00
2023-12-28 13:57:13 +00:00
Vector2D App::computeRelativePts(Gem g) {
Vector2D v = g.coordinate-myVehicle_;
v.reverse();
v.normalize(MAX_LENGHT);
2023-12-28 17:15:53 +00:00
return Vector2D(v.lenght()*g.pts, v.angle());
}
void App::computeRelativeDistance(Gem* g) {
int deltaX = abs(g->coordinate.x() - myVehicle_.x());
int deltaY = abs(g->coordinate.y() - myVehicle_.y());
g->relativeDistanceToMe = deltaX+deltaY;
g->relativeDistanceToOtherPlayer = MAX_LENGHT*2;
for(Vector2D v : otherVehicles_) {
deltaX = abs(g->coordinate.x() - v.x());
deltaY = abs(g->coordinate.y() - v.y());
if(deltaX+deltaY < g->relativeDistanceToOtherPlayer) {
g->relativeDistanceToOtherPlayer = deltaX+deltaY;
}
}
2023-12-26 21:09:42 +00:00
}
void App::computeMove() {
2023-12-28 13:57:13 +00:00
static const double PI = 3.14159265358979323846;
Vector2D myRelativePts;
2023-12-26 21:09:42 +00:00
for(Gem g : gems_) {
2023-12-28 17:15:53 +00:00
computeRelativeDistance(&g);
Vector2D v = computeRelativePts(g);
myRelativePts = myRelativePts+v;
if(g.relativeDistanceToMe <= g.relativeDistanceToOtherPlayer) {
myRelativePts = myRelativePts+v;
}
2023-12-28 13:57:13 +00:00
}
double angle = myRelativePts.angle();
if(angle <0) angle = angle+2*PI;
angle *= 180/PI;
qDebug() << "Angle: " << angle << "°";
qDebug() << "Length: " << myRelativePts.lenght() << Qt::endl;
if(angle > 360) {
qDebug() << "Error angle: " << angle << Qt::endl;
}
else if(angle <= -360) {
qDebug() << "Error angle: " << angle << Qt::endl;
}
else if(angle > 315) { // angle > 315°
st_->sendRequest("/topic/sdi10.gem.command", "right");
}
else if(angle <= -315) { // angle <= -315° || angle <= 45°
st_->sendRequest("/topic/sdi10.gem.command", "right");
}
else if(angle > 225) { // angle > 225°
st_->sendRequest("/topic/sdi10.gem.command", "down");
}
else if(angle <= -225) { // angle <= -225° || angle <= 135°
st_->sendRequest("/topic/sdi10.gem.command", "up");
}
else if(angle > 135) { // angle > 135°
st_->sendRequest("/topic/sdi10.gem.command", "left");
}
else if(angle <= -135) { // angle <= -135° || angle <= 225°
st_->sendRequest("/topic/sdi10.gem.command", "left");
}
else if(angle > 45) { // angle > 45°
st_->sendRequest("/topic/sdi10.gem.command", "up");
}
else if(angle <= -45) { // angle <= -45° || angle <= 315°
st_->sendRequest("/topic/sdi10.gem.command", "down");
}
else {
st_->sendRequest("/topic/sdi10.gem.command", "right");
2023-12-26 21:09:42 +00:00
}
}