Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rb 152 upgrade bullet #137

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 95 additions & 9 deletions docs/network/rfc/RFC.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ Table of Contents
3.2.2. READY
3.2.3. POSITION_RELATIVE
3.2.4. POSITION_ABSOLUTE
3.2.5. NEW_BULLET
3.2.5. NEW_MISSILE
3.2.6. LIFE_UPDATE
3.2.7. ENEMY_DEATH
3.2.8. PLAYER_DEATH
3.2.9. MISSILE_DEATH
3.3. Main Server -> Client
3.3.1. CONNECT_MAIN_SERVER_RESP
3.3.2. LIST_LOBBY
Expand All @@ -50,10 +51,11 @@ Table of Contents
3.4.3. ENEMY_DEATH
3.4.4. NEW_ENEMY
3.4.5. NEW_PLAYER
3.4.6. NEW_BULLET
3.4.6. NEW_MISSILE
3.4.7. POSITION_ABSOLUTE_BROADCAST
3.4.8. POSITION_RELATIVE_BROADCAST
3.4.9. PLAYER_DEATH
3.4.10. MISSILE_DEATH
3.5. Lobby Server -> Main Server
3.5.1. INFO_LOBBY
4. References
Expand Down Expand Up @@ -190,7 +192,7 @@ Table of Contents

3.1.1. CONNECT_MAIN_SERVER

To understand this action, the action header `magick` must be equal to `18`
To understand this action, the action header `magick` must be equal to `19`

The action body is composed of the following fields:
- `magick`
Expand Down Expand Up @@ -220,7 +222,7 @@ Table of Contents

3.1.3. CREATE_LOBBY

To understand this action, the action header `magick` must be equal to `15`
To understand this action, the action header `magick` must be equal to `17`

The action body is composed of the following fields:
- `magick`
Expand Down Expand Up @@ -391,14 +393,16 @@ Table of Contents
This field must be of size 4 bytes.
This field is signed (so starting from -((2^32)/2) to +(((2^32)/2)-1))

3.2.5. NEW_BULLET
3.2.5. NEW_MISSILE

To understand this action, the action header `magick` must be equal to `9`

The action body is composed of the following fields:
- `magick`
- `x`
- `y`
- `missileId`
- `missileHealth`
- `missileType`

*** Magick
Expand All @@ -423,6 +427,24 @@ Table of Contents
This field must be of size 4 bytes.
This field is signed (so starting from -((2^32)/2) to +(((2^32)/2)-1))

*** Missile ID

This field correspond to the ID of the missile.
This is useless because this is the server that will set the ID of the
missile.

This field must be of size 4 byte.
romainpanno marked this conversation as resolved.
Show resolved Hide resolved
This field must be equal to `0`

*** Missile Health

This field correspond to the health of the missile.
This is useless because this is the server that will set the health of the
missile.

This field must be of size 4 byte.
romainpanno marked this conversation as resolved.
Show resolved Hide resolved
This field must be equal to `0`

*** Missile Type

This field correspond to the type of the missile.
Expand Down Expand Up @@ -515,11 +537,35 @@ Table of Contents
This field is unsigned (so starting from 0 to 2^32)
This field is unique for each player.

3.2.9. MISSILE_DEATH

To understant This action, the aciton header `magick` must be equal to `15`

The action body is composed of ther following fields:
- `magick`
- `missileId`

*** Magick

This field help to know the packet is realy missile death action

This field must be of size 1 byte.
This field is unsigned (so starting from 0 to 2^8)
This field must be equal to the ascii `\x16`

*** Missile ID

This field correspond to the ID of the missile that died
romainpanno marked this conversation as resolved.
Show resolved Hide resolved

This field must be of size 4 bytes.
This field is unsigned (so starting from 0 to 2^32)
This field is unique for each missile.

3.3. Main Server -> Client

3.3.1. CONNECT_MAIN_SERVER_RESP

To understand this action, the action header `magick` must be equal to `19`
To understand this action, the action header `magick` must be equal to `20`

The action body is composed of the following fields:
- `magick`
Expand Down Expand Up @@ -820,14 +866,16 @@ Table of Contents
This field is equal to `1` if it is you
This field is equal to `0` if it is not you

3.4.6. NEW_BULLET
3.4.6. NEW_MISSILE

To understand this action, the action header `magick` must be equal to `9`

The action body is composed of the following fields:
- `magick`
- `x`
- `y`
- `missileId`
- `missileHealth`
- `missileType`

*** Magick
Expand All @@ -852,6 +900,20 @@ Table of Contents
This field must be of size 4 bytes.
This field is signed (so starting from -((2^32)/2) to +(((2^32)/2)-1))

*** Missile ID

This field correspond to the ID of the missile.
romainpanno marked this conversation as resolved.
Show resolved Hide resolved

This field must be of size 4 bytes.
romainpanno marked this conversation as resolved.
Show resolved Hide resolved
This field is signed (so starting from -((2^32)/2) to +(((2^32)/2)-1))

*** Missile Health

This field correspond to the health of the missile.
romainpanno marked this conversation as resolved.
Show resolved Hide resolved

This field must be of size 4 bytes.
romainpanno marked this conversation as resolved.
Show resolved Hide resolved
This field is signed (so starting from -((2^32)/2) to +(((2^32)/2)-1))

*** Missile Type

This field correspond to the type of the bullet.
Expand Down Expand Up @@ -972,11 +1034,35 @@ Table of Contents
This field is unsigned (so starting from 0 to 2^32)
This field is unique for each player.

3.2.10. MISSILE_DEATH

To understant This action, the aciton header `magick` must be equal to `15`

The action body is composed of ther following fields:
- `magick`
- `missileId`

*** Magick

This field help to know the packet is realy missile death action

This field must be of size 1 byte.
This field is unsigned (so starting from 0 to 2^8)
This field must be equal to the ascii `\x16`

*** Missile ID

This field correspond to the ID of the missile that died
romainpanno marked this conversation as resolved.
Show resolved Hide resolved

This field must be of size 4 bytes.
This field is unsigned (so starting from 0 to 2^32)
This field is unique for each missile.

3.5. Lobby Server -> Main Server

3.5.1. INFO_LOBBY

To understand this action, the action header `magick` must be equal to `17`
To understand this action, the action header `magick` must be equal to `18`

This action is used to let the Main Server know the current Lobby Server
exists.
Expand Down Expand Up @@ -1079,4 +1165,4 @@ Table of Contents
Structures used by the R-Bus game inside the network
<https://github.com/X-R-G-B/R-Bus/blob/dev/src/Nitwork/Nitwork.h>

```
```
1 change: 0 additions & 1 deletion src/Client/Systems/Events/EventsSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ namespace Systems {
Nitwork::NitworkClient::getInstance().addNewBulletMsg(
{Maths::removeIntDecimals(pos.x), Maths::removeIntDecimals(pos.y)},
missile.type);
createMissile(pos, missile);
clock_.restart(getClockIdFromMissileType(missile.type));
}
}
Expand Down
28 changes: 27 additions & 1 deletion src/Client/Systems/Network/ClientNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ namespace Systems {
}
}

void receiveMissileDeath(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
const auto missileDeath = std::any_cast<struct msgMissileDeath_s>(any);
auto &missiles = Registry::getInstance().getComponents<Types::Missiles>();
auto &arrHealth = Registry::getInstance().getComponents<struct health_s>();
std::vector<std::size_t> ids = missiles.getExistingsId();

for (auto id : ids) {
if (missiles[id].constId == missileDeath.missileId) {
if (arrHealth.exist(id)) {
arrHealth[id].hp = 0;
} else {
Logger::fatal("\n\n\n!!!! Missile has no health component, but is alive !!!!\n\n\n");
// TODO : remove missile
romainpanno marked this conversation as resolved.
Show resolved Hide resolved
Registry::getInstance().removeEntity(id);
}
return;
}
}
}

void handleStartWave(std::any &any, boost::asio::ip::udp::endpoint & /* unused */)
{
auto &director = SystemManagersDirector::getInstance();
Expand Down Expand Up @@ -210,6 +232,8 @@ namespace Systems {
void receiveNewBullet(std::any &any, boost::asio::ip::udp::endpoint & /* unused*/)
{
std::lock_guard<std::mutex> lock(Registry::getInstance().mutex);
auto missiles = Registry::getInstance().getComponents<Types::Missiles>();
auto health = Registry::getInstance().getComponents<struct health_s>();

const struct msgNewBullet_s &msgNewBullet = std::any_cast<struct msgNewBullet_s>(any);

Expand All @@ -218,7 +242,9 @@ namespace Systems {
Maths::addIntDecimals(msgNewBullet.pos.y),
};
struct Types::Missiles missileType = {static_cast<missileTypes_e>(msgNewBullet.missileType)};
Systems::createMissile(position, missileType);
auto id = Systems::createMissile(position, missileType);
missiles[id].constId = msgNewBullet.id;
health[id].hp = msgNewBullet.life;
}

void receiveBroadcastAbsolutePosition(std::any &any, boost::asio::ip::udp::endpoint & /* unused*/)
Expand Down
1 change: 1 addition & 0 deletions src/Client/Systems/Network/ClientNetwork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Systems {
void receiveRelativePosition(std::any &any, boost::asio::ip::udp::endpoint &);
void receiveBroadcastAbsolutePosition(std::any &any, boost::asio::ip::udp::endpoint &endpoint);
void receivePlayerDeath(std::any &any, boost::asio::ip::udp::endpoint &endpoint);
void receiveMissileDeath(std::any &any, boost::asio::ip::udp::endpoint & /* unused */);
void handleNewLobbyMsg(std::any &data, boost::asio::ip::udp::endpoint &endpoint);
void receiveConnectMainServerResp(std::any &data, boost::asio::ip::udp::endpoint &endpoint);
std::vector<std::function<void(std::size_t, std::size_t)>> getNetworkSystems();
Expand Down
2 changes: 2 additions & 0 deletions src/ECS/ECSCustomTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

unsigned int Types::Enemy::_enemyNb = 0;
std::mutex Types::Enemy::_mutex;
unsigned int Types::Missiles::_missileNb = 0;
std::mutex Types::Missiles::_mutex;

namespace Types {

Expand Down
23 changes: 23 additions & 0 deletions src/ECS/ECSCustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,30 @@ namespace Types {
};

struct Missiles {
public:
Missiles(enum missileTypes_e _type = CLASSIC) : type(_type), constId(0)
{
}

static void setMissileNb(unsigned int nb)
{
std::lock_guard<std::mutex> lock(_mutex);

_missileNb = nb;
}

static unsigned int getMissileNb()
{
std::lock_guard<std::mutex> lock(_mutex);
return _missileNb;
}

missileTypes_e type;
unsigned int constId;

private:
static unsigned int _missileNb;
static std::mutex _mutex;
};
romainpanno marked this conversation as resolved.
Show resolved Hide resolved

class Physics {
Expand Down
7 changes: 4 additions & 3 deletions src/ECS/Systems/BulletsSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ namespace Systems {
}
}

void createMissile(Types::Position pos, Types::Missiles &typeOfMissile)
std::size_t createMissile(Types::Position pos, Types::Missiles &typeOfMissile)
{
Json &json = Json::getInstance();
Registry::getInstance().addEntity();
Json &json = Json::getInstance();
std::size_t id = Registry::getInstance().addEntity();
nlohmann::json bulletData =
json.getJsonObjectById(JsonType::BULLETS, getMissileId(typeOfMissile.type), "bullets");
Types::CollisionRect collisionRect =
Expand All @@ -132,6 +132,7 @@ namespace Systems {
Registry::getInstance().getComponents<struct health_s>().insertBack(healthComp);
Registry::getInstance().getComponents<Types::Damage>().insertBack(damageComp);
Registry::getInstance().getComponents<Types::Dead>().insertBack(deadComp);
return id;
}

static void updateBouncePhysics(std::vector<std::size_t> ids)
Expand Down
12 changes: 11 additions & 1 deletion src/ECS/Systems/Systems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,24 @@ namespace Systems {

static void sendDeathMsg(std::size_t arrId)
{
auto &arrEnemies = Registry::getInstance().getComponents<Types::Enemy>();
auto &arrEnemies = Registry::getInstance().getComponents<Types::Enemy>();
auto &arrMissiles = Registry::getInstance().getComponents<Types::Missiles>();

#ifndef CLIENT
auto &arrOtherPlayer = Registry::getInstance().getComponents<Types::OtherPlayer>();
if (arrOtherPlayer.exist(arrId)) {
Nitwork::NitworkServer::getInstance().addPlayerDeathMsg(arrOtherPlayer[arrId].constId);
}
#endif

if (arrMissiles.exist(arrId)) {
#ifdef CLIENT
Nitwork::NitworkClient::getInstance().addMissileDeathMsg(arrMissiles[arrId].constId);
#else
Nitwork::NitworkServer::getInstance().addMissileDeathMsg(arrMissiles[arrId].constId);
#endif
}

if (arrEnemies.exist(arrId)) {
#ifdef CLIENT
Nitwork::NitworkClient::getInstance().addEnemyDeathMsg(arrEnemies[arrId].getConstId().id);
Expand Down
2 changes: 1 addition & 1 deletion src/ECS/Systems/Systems.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Systems {
const struct position_absolute_s &pos,
const struct health_s &life,
bool otherPlayer = false);
void createMissile(Types::Position pos, Types::Missiles &typeOfMissile);
std::size_t createMissile(Types::Position pos, Types::Missiles &typeOfMissile);
std::vector<std::function<void(std::size_t, std::size_t)>> getECSSystems();
std::vector<std::function<void(std::size_t, std::size_t)>> getBulletSystems();
} // namespace Systems
Loading
Loading