<commit_before>/* 
 * File:   ServerGameSpeedestWin.cpp
 * Author: maxence
 * 
 * Created on 13 décembre 2014, 11:19
 */
#include <iostream>

#include "shared/game/GameType.h"
#include "shared/Utils.h"
#include "shared/network/MsgTurnHelm.h"
#include "shared/network/MsgTurnSail.h"
#include "shared/network/MsgDisconnect.h"
#include "shared/network/MsgAction.h"
#include "shared/network/MsgData.h"
#include "shared/ship/Ship.h"

#include "server/ServerPlayer.h"
#include "server/ServerPlayers.h"
#include "server/Server.h"
#include "server/game/ServerGameSpeedestWin.h"
#include "server/network/ServerNetwork.h"

sf::Vector2f operator*(const sf::Vector2f &v1, const sf::Vector2f &v2) {
    return sf::Vector2f(v1.x * v2.x, v1.y * v2.y);
}

sf::Vector2f operator*(const sf::Vector2f &v1, float coef) {
    return sf::Vector2f(v1.x * coef, v1.y * coef);
}

sf::Vector2f operator/(const sf::Vector2f &v1, const sf::Vector2f &v2) {
    return sf::Vector2f(v1.x / v2.x, v1.y / v2.y);
}

sf::Packet& operator<<(sf::Packet& packet, const ServerGameSpeedestWin& game) {
    return packet << game.getMap();
}

ServerGameSpeedestWin::ServerGameSpeedestWin(Server &server, ServerPlayers& players, const MapHeader &header)
: ServerGame(server, players)
, m_map(header) {
    m_checkPointManager.initialise(m_map.getHeightMap());
    sf::Vector2f initPosition = static_cast<sf::Vector2f>(m_checkPointManager.getCheckPoint(0).getPosition());
    sf::Vector2f position(initPosition);
    for (auto* player : m_players.inGame()) {
        Ship ship;
        ship.kinematics().position() = position;
        ship.setAngle(90.0f);
        ship.getSail().setAngle(m_map.getWind(sf::Vector2i(position.x / TILE_SIZE, position.y / TILE_SIZE)).direction());
        m_ships[player] = ship;
        position.x += TILE_SIZE/4;
        if(position.x >= initPosition.x + TILE_SIZE) {
            position.x = initPosition.x;
            position.y += TILE_SIZE/4;
        }
    }
}

ServerGameSpeedestWin::ServerGameSpeedestWin(const ServerGameSpeedestWin& other) :
ServerGameSpeedestWin(other.m_server, m_players, m_map.getHeader()) {
}

ServerGameSpeedestWin::~ServerGameSpeedestWin() {
}

const Map& ServerGameSpeedestWin::getMap() const {
    return m_map;
}

void ServerGameSpeedestWin::init() {
    sendInfo();
}

void ServerGameSpeedestWin::update(float dt) {
    for (auto& ship : m_ships) {
        updateShipState(ship.second, dt);
        updateSail(ship.second);
        sf::Vector2f shipVelocity = calculShipVelocity(ship.second);
        
        if(!collideWithMap(ship.second, shipVelocity)) {
            ship.second.kinematics().speed() = shipVelocity;
        }
        else {
            ship.second.kinematics().speed() = {0.0f,0.0f};
        }
        
        int checkPointCount = m_checkPointManager.getCheckPointCount();
        
        for (int i = 0; i < checkPointCount; i++) {
            ServerCheckpoint c = m_checkPointManager.getCheckPoint(i);
            if(!m_checkPointManager.isCompletedCheckpoint(ship.first, i)) {
                if(collideWithCheckPoint(ship.second, c)) {
                    m_checkPointManager.addCompletedCheckpoint(ship.first, i);
                    MsgData msgCheckpoint;
                    msgCheckpoint << MsgType::Checkpoint << static_cast<sf::Uint8>(i);
                    m_server.getNetwork()->getTCPManager().send(msgCheckpoint, ship.first->getTCPSocket());
                }   
            }
        }
        
        ship.second.update(dt);
    }
}

void ServerGameSpeedestWin::updateShipState(Ship& ship, float dt) {
    if (ship.isTurningNegative()) {
        ship.turnNegative(45.0f * dt);
        ship.getSail().turnNegative(45.0f * dt);
    }
    if (ship.isTurningPositive()) {
        ship.turnPositive(45.0f * dt);
        ship.getSail().turnPositive(45.0f * dt);
    }
    if (ship.getSail().isTurningNegative()) {
        ship.getSail().turnNegative(45.0f * dt);
    }
    if (ship.getSail().isTurningPositive()) {
        ship.getSail().turnPositive(45.0f * dt);
    }
}

void ServerGameSpeedestWin::updateSail(Ship& ship) {
    Wind wind = m_map.getWind(static_cast<sf::Vector2i> (ship.kinematics().position() / sf::Vector2f(TILE_SIZE, TILE_SIZE)));
    float angleShip = ship.getAngle();
    float diff = 360.0f - angleShip;
    float windDir = wind.getDirection() + diff;
    if (windDir >= 360.0f)
        windDir -= 360.0f;
    float sailDir = ship.getSail().getAngle() + diff;
    if (sailDir >= 360.0f)
        sailDir -= 360.0f;

    float diffSailWind = sailDir - windDir;
    if (windComeFromTribord(ship, wind)) {
        if (sailDir < 180.0f)
            ship.getSail().setAngle(ship.getSail().getAngle() + (180.0f - sailDir)*2.0f);
        if (diffSailWind > 0.0f)
            ship.getSail().setAngle(ship.getSail().getAngle() - diffSailWind);

    } else {
        if (sailDir > 180.0f)
            ship.getSail().setAngle(ship.getSail().getAngle() - (sailDir - 180.0f)*2.0f);
        if (diffSailWind < 0.0f)
            ship.getSail().setAngle(ship.getSail().getAngle() - diffSailWind);
    }
}

sf::Vector2f ServerGameSpeedestWin::calculShipVelocity(Ship& ship) {
    Wind wind = m_map.getWind(static_cast<sf::Vector2i> (ship.kinematics().position() / sf::Vector2f(TILE_SIZE, TILE_SIZE)));
    float shipDirRad = Kinematics::degToRad(ship.getAngle());
    sf::Vector2f outP(std::cos(shipDirRad), std::sin(shipDirRad));

    // Temporary equation 
    sf::Vector2f windForce = wind.getVector() * std::sin(Kinematics::degToRad(std::abs(ship.getSail().getAngle() - wind.getDirection())));
    sf::Vector2f velocity = std::sqrt(windForce.x * windForce.x + windForce.y * windForce.y) * outP;
    //ship.kinematics().speed() = velocity;
    return velocity;
}

bool ServerGameSpeedestWin::collideWithMap(const Ship & ship, const sf::Vector2f & velocity) {
    int x = (ship.kinematics().position().x + velocity.x) / TILE_SIZE;
    int y = (ship.kinematics().position().y + velocity.y) / TILE_SIZE;
    return !WATER(m_map.getHeightMap().getValue(x,y));
}

bool ServerGameSpeedestWin::collideWithCheckPoint(const Ship & ship, const ServerCheckpoint & checkPoint) {

    sf::Vector2f posShip = ship.kinematics().position();
    sf::Vector2i posCheckPoint = checkPoint.getPosition();
    
    std::cout << static_cast<int>(posShip.x / TILE_SIZE) << " " << posCheckPoint.x / TILE_SIZE << "  " << static_cast<int>(posShip.y / TILE_SIZE) << " "  << posCheckPoint.y / TILE_SIZE << std::endl;
    if (static_cast<int>(posShip.x / TILE_SIZE) == posCheckPoint.x / TILE_SIZE
     && static_cast<int>(posShip.y / TILE_SIZE) == posCheckPoint.y / TILE_SIZE) {
        //std::cout << "collision" << std::endl;
        return true;
    }
    
    return false;
}

bool ServerGameSpeedestWin::windComeFromFront(const Ship& ship, const Wind& wind) const {
    return (static_cast<int> (450 - ship.getAngle() + wind.getDirection()) % 360 > 180);
}

bool ServerGameSpeedestWin::windComeFromTribord(const Ship& ship, const Wind& wind) const {
    return (static_cast<int> (360 - ship.getAngle() + wind.getDirection()) % 360 > 180);
}

bool ServerGameSpeedestWin::gameIsEnded() {
    std::cout << "Nb players in game(" << m_players.inGame().size() << ")" << std::endl;
    return m_players.inGame().size() == 0;<commit_msg>Removed cout<commit_after>/* 
 * File:   ServerGameSpeedestWin.cpp
 * Author: maxence
 * 
 * Created on 13 décembre 2014, 11:19
 */
#include <iostream>

#include "shared/game/GameType.h"
#include "shared/Utils.h"
#include "shared/network/MsgTurnHelm.h"
#include "shared/network/MsgTurnSail.h"
#include "shared/network/MsgDisconnect.h"
#include "shared/network/MsgAction.h"
#include "shared/network/MsgData.h"
#include "shared/ship/Ship.h"

#include "server/ServerPlayer.h"
#include "server/ServerPlayers.h"
#include "server/Server.h"
#include "server/game/ServerGameSpeedestWin.h"
#include "server/network/ServerNetwork.h"

sf::Vector2f operator*(const sf::Vector2f &v1, const sf::Vector2f &v2) {
    return sf::Vector2f(v1.x * v2.x, v1.y * v2.y);
}

sf::Vector2f operator*(const sf::Vector2f &v1, float coef) {
    return sf::Vector2f(v1.x * coef, v1.y * coef);
}

sf::Vector2f operator/(const sf::Vector2f &v1, const sf::Vector2f &v2) {
    return sf::Vector2f(v1.x / v2.x, v1.y / v2.y);
}

sf::Packet& operator<<(sf::Packet& packet, const ServerGameSpeedestWin& game) {
    return packet << game.getMap();
}

ServerGameSpeedestWin::ServerGameSpeedestWin(Server &server, ServerPlayers& players, const MapHeader &header)
: ServerGame(server, players)
, m_map(header) {
    m_checkPointManager.initialise(m_map.getHeightMap());
    sf::Vector2f initPosition = static_cast<sf::Vector2f>(m_checkPointManager.getCheckPoint(0).getPosition());
    sf::Vector2f position(initPosition);
    for (auto* player : m_players.inGame()) {
        Ship ship;
        ship.kinematics().position() = position;
        ship.setAngle(90.0f);
        ship.getSail().setAngle(m_map.getWind(sf::Vector2i(position.x / TILE_SIZE, position.y / TILE_SIZE)).direction());
        m_ships[player] = ship;
        position.x += TILE_SIZE/4;
        if(position.x >= initPosition.x + TILE_SIZE) {
            position.x = initPosition.x;
            position.y += TILE_SIZE/4;
        }
    }
}

ServerGameSpeedestWin::ServerGameSpeedestWin(const ServerGameSpeedestWin& other) :
ServerGameSpeedestWin(other.m_server, m_players, m_map.getHeader()) {
}

ServerGameSpeedestWin::~ServerGameSpeedestWin() {
}

const Map& ServerGameSpeedestWin::getMap() const {
    return m_map;
}

void ServerGameSpeedestWin::init() {
    sendInfo();
}

void ServerGameSpeedestWin::update(float dt) {
    for (auto& ship : m_ships) {
        updateShipState(ship.second, dt);
        updateSail(ship.second);
        sf::Vector2f shipVelocity = calculShipVelocity(ship.second);
        
        if(!collideWithMap(ship.second, shipVelocity)) {
            ship.second.kinematics().speed() = shipVelocity;
        }
        else {
            ship.second.kinematics().speed() = {0.0f,0.0f};
        }
        
        int checkPointCount = m_checkPointManager.getCheckPointCount();
        
        for (int i = 0; i < checkPointCount; i++) {
            ServerCheckpoint c = m_checkPointManager.getCheckPoint(i);
            if(!m_checkPointManager.isCompletedCheckpoint(ship.first, i)) {
                if(collideWithCheckPoint(ship.second, c)) {
                    m_checkPointManager.addCompletedCheckpoint(ship.first, i);
                    MsgData msgCheckpoint;
                    msgCheckpoint << MsgType::Checkpoint << static_cast<sf::Uint8>(i);
                    m_server.getNetwork()->getTCPManager().send(msgCheckpoint, ship.first->getTCPSocket());
                }   
            }
        }
        
        ship.second.update(dt);
    }
}

void ServerGameSpeedestWin::updateShipState(Ship& ship, float dt) {
    if (ship.isTurningNegative()) {
        ship.turnNegative(45.0f * dt);
        ship.getSail().turnNegative(45.0f * dt);
    }
    if (ship.isTurningPositive()) {
        ship.turnPositive(45.0f * dt);
        ship.getSail().turnPositive(45.0f * dt);
    }
    if (ship.getSail().isTurningNegative()) {
        ship.getSail().turnNegative(45.0f * dt);
    }
    if (ship.getSail().isTurningPositive()) {
        ship.getSail().turnPositive(45.0f * dt);
    }
}

void ServerGameSpeedestWin::updateSail(Ship& ship) {
    Wind wind = m_map.getWind(static_cast<sf::Vector2i> (ship.kinematics().position() / sf::Vector2f(TILE_SIZE, TILE_SIZE)));
    float angleShip = ship.getAngle();
    float diff = 360.0f - angleShip;
    float windDir = wind.getDirection() + diff;
    if (windDir >= 360.0f)
        windDir -= 360.0f;
    float sailDir = ship.getSail().getAngle() + diff;
    if (sailDir >= 360.0f)
        sailDir -= 360.0f;

    float diffSailWind = sailDir - windDir;
    if (windComeFromTribord(ship, wind)) {
        if (sailDir < 180.0f)
            ship.getSail().setAngle(ship.getSail().getAngle() + (180.0f - sailDir)*2.0f);
        if (diffSailWind > 0.0f)
            ship.getSail().setAngle(ship.getSail().getAngle() - diffSailWind);

    } else {
        if (sailDir > 180.0f)
            ship.getSail().setAngle(ship.getSail().getAngle() - (sailDir - 180.0f)*2.0f);
        if (diffSailWind < 0.0f)
            ship.getSail().setAngle(ship.getSail().getAngle() - diffSailWind);
    }
}

sf::Vector2f ServerGameSpeedestWin::calculShipVelocity(Ship& ship) {
    Wind wind = m_map.getWind(static_cast<sf::Vector2i> (ship.kinematics().position() / sf::Vector2f(TILE_SIZE, TILE_SIZE)));
    float shipDirRad = Kinematics::degToRad(ship.getAngle());
    sf::Vector2f outP(std::cos(shipDirRad), std::sin(shipDirRad));

    // Temporary equation 
    sf::Vector2f windForce = wind.getVector() * std::sin(Kinematics::degToRad(std::abs(ship.getSail().getAngle() - wind.getDirection())));
    sf::Vector2f velocity = std::sqrt(windForce.x * windForce.x + windForce.y * windForce.y) * outP;
    //ship.kinematics().speed() = velocity;
    return velocity;
}

bool ServerGameSpeedestWin::collideWithMap(const Ship & ship, const sf::Vector2f & velocity) {
    int x = (ship.kinematics().position().x + velocity.x) / TILE_SIZE;
    int y = (ship.kinematics().position().y + velocity.y) / TILE_SIZE;
    return !WATER(m_map.getHeightMap().getValue(x,y));
}

bool ServerGameSpeedestWin::collideWithCheckPoint(const Ship & ship, const ServerCheckpoint & checkPoint) {

    sf::Vector2f posShip = ship.kinematics().position();
    sf::Vector2i posCheckPoint = checkPoint.getPosition();
    
    if (static_cast<int>(posShip.x / TILE_SIZE) == posCheckPoint.x / TILE_SIZE
     && static_cast<int>(posShip.y / TILE_SIZE) == posCheckPoint.y / TILE_SIZE) {
        return true;
    }
    
    return false;
}

bool ServerGameSpeedestWin::windComeFromFront(const Ship& ship, const Wind& wind) const {
    return (static_cast<int> (450 - ship.getAngle() + wind.getDirection()) % 360 > 180);
}

bool ServerGameSpeedestWin::windComeFromTribord(const Ship& ship, const Wind& wind) const {
    return (static_cast<int> (360 - ship.getAngle() + wind.getDirection()) % 360 > 180);
}

bool ServerGameSpeedestWin::gameIsEnded() {
    std::cout << "Nb players in game(" << m_players.inGame().size() << ")" << std::endl;
    return m_players.inGame().size() == 0;
{'fraction_non_alphanumeric': 0.1512205357767364, 'fraction_numerical': 0.02028397565922921, 'mean_word_length': 3.4034493378503234, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4257590', 'n_tokens_mistral': 5086, 'n_tokens_neox': 4930, 'n_words': 803}