Skip to content

Commit

Permalink
add example clients
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldomanek authored and michaeldomanek committed Apr 8, 2021
1 parent 7d34e76 commit 6fbb24a
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*** Added
- Added all robot functions to grpc server / client
- Added grpcServer/Client cpp file
- Add 4 example clients
*** Changed
- Changed directory structure
*** Removed
Expand Down
2 changes: 0 additions & 2 deletions include/robotClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ namespace Game {
RobotConfigurationMessage rcmsg;
rcmsg.ParseFromString(Base64::from_base64(data));

fmt::print("speed. {}\n", rcmsg.speed());

config = {rcmsg.speed(), rcmsg.health(), rcmsg.robotrotation(), rcmsg.turretrotation(),
rcmsg.minfirecountdown(), rcmsg.canshootandmove()};

Expand Down
8 changes: 4 additions & 4 deletions include/robotStartConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class RobotStartConfiguration {

static std::vector<RobotStartConfiguration> getStartConfigs(unsigned int width) {
std::vector<RobotStartConfiguration> startConfigs{
{{40 , 40 }, 135},
{{40 , width - 40.0f}, 45 },
{{width - 40.0f, 40 }, 225},
{{width - 40.0f, width - 40.0f}, 315}
{{40 , 40 }, 125},
{{40 , width - 40.0f}, 35},
{{width - 40.0f, 40 }, 215},
{{width - 40.0f, width - 40.0f}, 305}
};

auto seed = std::chrono::system_clock::now().time_since_epoch().count();
Expand Down
32 changes: 30 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ src_server = ['src/server_src/main.cpp', 'src/server_src/bullet.cpp',
'src/server_src/robot.cpp', 'src/server_src/window.cpp',
'src/server_src/grpcServer.cpp', generated, grpc_generated]

src_client = ['src/client_src/grpc-test.cpp', 'src/client_src/grpcClient.cpp', generated, grpc_generated]
src_domanek = ['src/client_src/domanek.cpp' , 'src/client_src/grpcClient.cpp', generated, grpc_generated]
src_domanekV2 = ['src/client_src/domanekV2.cpp' , 'src/client_src/grpcClient.cpp', generated, grpc_generated]
src_client = ['src/client_src/grpc-test.cpp' , 'src/client_src/grpcClient.cpp', generated, grpc_generated]
src_rect = ['src/client_src/rect-client.cpp' , 'src/client_src/grpcClient.cpp', generated, grpc_generated]
src_random = ['src/client_src/random-client.cpp', 'src/client_src/grpcClient.cpp', generated, grpc_generated]

graphics = dependency('sfml-graphics')
window = dependency('sfml-window')
Expand All @@ -59,8 +63,32 @@ executable('robo-server',
dependencies : [thread_dep, graphics, window, protobuf_dep, grpc_cpp_dep]
)

executable('robo-test-client',
executable('domanek',
sources : src_domanek,
include_directories : inc_dir,
dependencies : [thread_dep, graphics, window, protobuf_dep, grpc_cpp_dep]
)

executable('domanekV2',
sources : src_domanekV2,
include_directories : inc_dir,
dependencies : [thread_dep, graphics, window, protobuf_dep, grpc_cpp_dep]
)

executable('test-client',
sources : src_client,
include_directories : inc_dir,
dependencies : [thread_dep, graphics, window, protobuf_dep, grpc_cpp_dep]
)

executable('rect-client',
sources : src_rect,
include_directories : inc_dir,
dependencies : [thread_dep, graphics, window, protobuf_dep, grpc_cpp_dep]
)

executable('random-client',
sources : src_random,
include_directories : inc_dir,
dependencies : [thread_dep, graphics, window, protobuf_dep, grpc_cpp_dep]
)
82 changes: 82 additions & 0 deletions src/client_src/domanek.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "robotClient.h"
#include <math.h>

using namespace std;

int main() {
string port{"1113"};
string name{"Michael"};
string grpcPort{"50051"};

RobotProperties properties{name, sf::Color::Blue};

RobotConfiguration config;

int id = Game::connectToGame(port, properties, config);

fmt::print("speed: {}, health: {}, rotation: {}, turret: {}, fire: {}, shoot-move: {}\n",
config.getSpeed(), config.getHealth(), config.getRobotRotation(),
config.getTurretRotation(), config.getMinFireCountdown(), config.canShootAndMove()
);

if(id >= 0) {
try {
fmt::print("Game stated! Robot id: {}\n", id);

Robot_RPC_Client robotClient = Game::getRobot_RPC_Client(grpcPort, id);

robotClient.startShooting();
robotClient.moveForward();
sleep(4);
robotClient.stopMove();

while(true) {
sf::Vector2f enemypos{robotClient.getEnemiesRobotInformations()[0].getPosition()};
sf::Vector2f pos{robotClient.getPosition()};
float rot{robotClient.getTurretRotation()};

float slope{(enemypos.y - pos.y) / (enemypos.x - pos.x)};
// fmt::print("rot: {}\n", rot);
// fmt::print("x: {} | y: {}\n", pos.x, pos.y);
// fmt::print("ex: {} | ey: {}\n", enemypos.x, enemypos.y);

bool xGreater{pos.x > enemypos.x};
bool yGreater{pos.y > enemypos.y};
float degrees{abs(atan(slope) * (180.0f / (float)M_PI))};

float delta{1};
float desiredRot;
bool rotateLeft;

if(xGreater and yGreater) { //left top
desiredRot = 270 + degrees;
rotateLeft = rot < 90 + degrees or rot > desiredRot + delta;
} else if(!xGreater and yGreater) { //right top
desiredRot = 90 - degrees;
rotateLeft = rot < 180 + degrees and rot > desiredRot + delta;
} else if(!xGreater and !yGreater) { //right bottom
desiredRot = degrees;
rotateLeft = rot < 270 + degrees and rot > 90 + desiredRot + delta;
} else { //left bottom
desiredRot = 180 + (90 - degrees);
rotateLeft = rot < degrees or rot > desiredRot + delta;
}

// fmt::print("desired rot {}\n", desiredRot);
// fmt::print("rotateLeft: {}\n", rotateLeft);

if(rot > desiredRot - delta and rot < desiredRot + delta) {
robotClient.stopRotateWeapon();
} else if(rotateLeft) {
robotClient.rotateWeaponLeft();
} else {
robotClient.rotateWeaponRight();
}

usleep(10000);
}
} catch (exception& e) {
fmt::print("Game ended\n");
}
}
}
101 changes: 101 additions & 0 deletions src/client_src/domanekV2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include "robotClient.h"
#include <math.h>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
string port{"1113"};
string name{"MichaelV2"};
string grpcPort{"50051"};

RobotProperties properties{name, sf::Color::Blue};

RobotConfiguration config;

int id = Game::connectToGame(port, properties, config);

fmt::print("speed: {}, health: {}, rotation: {}, turret: {}, fire: {}, shoot-move: {}\n",
config.getSpeed(), config.getHealth(), config.getRobotRotation(),
config.getTurretRotation(), config.getMinFireCountdown(), config.canShootAndMove()
);

if(id >= 0) {
try {
fmt::print("Game stated! Robot id: {}\n", id);

Robot_RPC_Client robotClient = Game::getRobot_RPC_Client(grpcPort, id);

robotClient.startShooting();
robotClient.moveForward();
sleep(3);
robotClient.stopMove();
sf::Clock clock;

while(true) {
sf::Vector2f pos{robotClient.getPosition()};

vector<RobotInformation> enemies{robotClient.getEnemiesRobotInformations()};
auto nearestEnemy = min_element(enemies.begin(), enemies.end(), [&pos](auto a, auto b){
sf::Vector2f aEnemyPos{a.getPosition()};
sf::Vector2f bEnemyPos{b.getPosition()};
return sqrt(pow(aEnemyPos.x - pos.x, 2) + pow(aEnemyPos.y - pos.y, 2))
< sqrt(pow(bEnemyPos.x - pos.x, 2) + pow(bEnemyPos.y - pos.y, 2));
});

sf::Vector2f enemypos{nearestEnemy->getPosition()};
float rot{robotClient.getTurretRotation()};

float slope{(enemypos.y - pos.y) / (enemypos.x - pos.x)};

bool xGreater{pos.x > enemypos.x};
bool yGreater{pos.y > enemypos.y};
float degrees{abs(atan(slope) * (180.0f / (float)M_PI))};

float delta{1};
float desiredRot;
bool rotateLeft;
bool moveForward{true};

fmt::print("sec: {}\n", clock.getElapsedTime().asMilliseconds());
if(clock.getElapsedTime().asMilliseconds() > 2000) {
if (moveForward) {
moveForward = false;
robotClient.moveForward();
} else {
moveForward = true;
robotClient.moveBackward();
}
clock.restart();
}

if(xGreater and yGreater) { //left top
desiredRot = 270 + degrees;
rotateLeft = rot < 90 + degrees or rot > desiredRot + delta;
} else if(!xGreater and yGreater) { //right top
desiredRot = 90 - degrees;
rotateLeft = rot < 180 + degrees and rot > desiredRot + delta;
} else if(!xGreater and !yGreater) { //right bottom
desiredRot = degrees;
rotateLeft = rot < 270 + degrees and rot > 90 + desiredRot + delta;
} else { //left bottom
desiredRot = 180 + (90 - degrees);
rotateLeft = rot < degrees or rot > desiredRot + delta;
}

if(rot > desiredRot - delta and rot < desiredRot + delta) {
robotClient.stopRotateWeapon();
} else if(rotateLeft) {
robotClient.rotateWeaponLeft();
} else {
robotClient.rotateWeaponRight();
}

usleep(10000);
}
} catch (exception& e) {
fmt::print("Game ended\n");
}
}
}
91 changes: 47 additions & 44 deletions src/client_src/grpc-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ using namespace std;

int main() {
string port{"1113"};
string name{"testname"};
string name{"Grpc test client"};
string grpcPort{"50051"};

RobotProperties properties{name, sf::Color::Blue};
RobotProperties properties{name, sf::Color::Magenta};

RobotConfiguration config;

Expand All @@ -19,56 +19,59 @@ int main() {
);

if(id >= 0) {
fmt::print("Game stated! Robot id: {}\n", id);
try {
fmt::print("Game stated! Robot id: {}\n", id);

Robot_RPC_Client robotClient = Game::getRobot_RPC_Client(grpcPort, id);
Robot_RPC_Client robotClient = Game::getRobot_RPC_Client(grpcPort, id);

robotClient.moveForward();
sleep(2);

while(true) {
robotClient.moveForward();
sleep(1);
robotClient.moveBackward();
sleep(1);
robotClient.stopMove();
sleep(1);
robotClient.rotateLeft();
sleep(1);
robotClient.rotateRight();
sleep(1);
robotClient.stopRotate();
sleep(1);
robotClient.rotateWeaponLeft();
sleep(1);
robotClient.rotateWeaponRight();
sleep(1);
robotClient.stopRotateWeapon();
sleep(1);
robotClient.startShooting();
sleep(1);
robotClient.stopShooting();
sleep(1);
sleep(2);

sf::Vector2f pos{robotClient.getPosition()};
fmt::print("x: {} | y: {}\n", pos.x, pos.y);
while(true) {
robotClient.moveForward();
sleep(1);
robotClient.moveBackward();
sleep(1);
robotClient.stopMove();
sleep(1);
robotClient.rotateLeft();
sleep(1);
robotClient.rotateRight();
sleep(1);
robotClient.stopRotate();
sleep(1);
robotClient.rotateWeaponLeft();
sleep(1);
robotClient.rotateWeaponRight();
sleep(1);
robotClient.stopRotateWeapon();
sleep(1);
robotClient.startShooting();
sleep(1);
robotClient.stopShooting();
sleep(1);

float rotation{robotClient.getRotation()};
fmt::print("rotation: {}\n", rotation);
sf::Vector2f pos{robotClient.getPosition()};
fmt::print("x: {} | y: {}\n", pos.x, pos.y);

float weaponRotation{robotClient.getTurretRotation()};
fmt::print("weapon rotation: {}\n", weaponRotation);
float rotation{robotClient.getRotation()};
fmt::print("rotation: {}\n", rotation);

for (auto orientation : robotClient.getEnemiesRobotInformations()) {
fmt::print("id: {} | name: {} | x: {} | y: {} | rotation: {} | weapon rotation: {}\n",
orientation.getID(), orientation.getName(),
orientation.getPosition().x, orientation.getPosition().y,
orientation.getRotation(), orientation.getTurretRotation()
);
}
float weaponRotation{robotClient.getTurretRotation()};
fmt::print("weapon rotation: {}\n", weaponRotation);

sleep(1);
for (auto info : robotClient.getEnemiesRobotInformations()) {
fmt::print("id: {} | name: {} | x: {} | y: {} | rotation: {} | weapon rotation: {}\n",
info.getID(), info.getName(),
info.getPosition().x, info.getPosition().y,
info.getRotation(), info.getTurretRotation()
);
}

sleep(1);
}
} catch (exception& e) {
fmt::print("Game ended\n");
}
}
google::protobuf::ShutdownProtobufLibrary();
}
Loading

0 comments on commit 6fbb24a

Please sign in to comment.