14 Commits

Author SHA1 Message Date
0efbd480ac Add LICENSE.txt
All checks were successful
Linux arm64 / Build (push) Successful in 11m20s
2024-05-23 17:25:13 +02:00
7c4ed3910f action: xmake latest
All checks were successful
Linux arm64 / Build (push) Successful in 1m4s
2024-02-24 13:06:56 +01:00
90c834cc49 Squashed commit of the following:
All checks were successful
Linux arm64 / Build (push) Successful in 6m2s
commit a8cbebc8d11d3621924b189a58a4f150d30324e9
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Sat Feb 10 14:00:26 2024 +0100

    jsp

commit a00ae1f6d9db33efef5e276bfc7101a1d24cf7d8
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Sat Feb 10 13:17:03 2024 +0100

    lol

commit d3dcaf3659251bb8fb0d4984ea188601a058757b
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Sat Feb 10 13:02:12 2024 +0100

    zsdf

commit 8ce343ff361ba0618d09d488404ff15c7d19de66
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Sat Feb 10 12:55:39 2024 +0100

    caca

commit 9f2a818ee57c751cb1283f12cfcc2c3617a3bd63
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Sat Feb 10 12:38:20 2024 +0100

    remove run name

commit b618dc176980ef18ae9a04f43a8abef311ca4329
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Sat Feb 10 12:20:35 2024 +0100

    change action name

commit 3092a403fdb2ded81d6f7558f621d0f8ac5dc1f5
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Sat Feb 10 12:06:03 2024 +0100

    fix action typo

commit 0cd675ef6c42758bef182307cce55d74aa2959fe
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Sat Feb 10 11:56:16 2024 +0100

    add action cache name

    Signed-off-by: Simon Pribylski <sim16.prib@gmail.com>

commit 4a6e6754f0a82a35fa4d6793321c7d27cb3e9f37
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Sat Feb 10 11:28:48 2024 +0100

    remove ssl ignore

    Signed-off-by: Simon Pribylski <sim16.prib@gmail.com>

commit 1808ca96ec84390915ace0678bc824d92cc3d1b3
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:29:10 2024 +0100

    change action name

commit 5a966bbe77463a93ab210cd4b6a122ab1d440486
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:22:16 2024 +0100

    cpp standard global

commit 673d1457162dda9018712d0a19ac5b3954ad265f
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:21:36 2024 +0100

    add glew dep

commit b64eb6dd3ef535183dd0620beeeaa99deb323c97
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:16:23 2024 +0100

    confirm

commit 1bf8065179b74f3dd194e05739a064097bdc2ebd
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:15:37 2024 +0100

    remove sudo

commit 2dc4ee69ebba3821916963a96621d6f8b59d4808
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:09:55 2024 +0100

    install apt

commit 69574b9c5a5074c2fdf87dfc8c068d9c64f7704c
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 10:31:11 2024 +0100

    add xmake cache

commit 4d7d04c6722ca8e94d10569f25ae582265866948
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 01:21:45 2024 +0100

    aaaaaaaaaaaaaaaaaa

commit aa7dac26cd9900c676e4662fadb7832700a3363a
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 01:13:08 2024 +0100

    fiiiiiiiiiiiiiix xmake root

commit 3604b44d04f492254911a0508e8555aca61b4d26
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 01:05:43 2024 +0100

    fix xmake root

commit cfcb8d4df46c6febeca47dff2f0447048b8f68ad
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 01:03:56 2024 +0100

    run xmake as root

commit dbef2d27138ccce1124cc1979867baf15e897a7f
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 00:54:54 2024 +0100

    xmake action

commit 42196398ced1e7f38c0234d82dc7cd65ef7aeb6d
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 00:53:29 2024 +0100

    xmake

commit 51256d8c15066120c2df3a72063856a0a47f8b59
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 00:47:35 2024 +0100

    Fix Linux.yaml

    add cert

commit 9c024fa8eb92765aff3fb195ce7c8c10a1b49d0b
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Sun Dec 31 22:34:54 2023 +0100

    Add Linux actions
2024-02-10 16:02:51 +01:00
d23761dc10 Squashed commit of the following:
Some checks failed
Linux arm / Build (push) Has been cancelled
commit 1808ca96ec84390915ace0678bc824d92cc3d1b3
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:29:10 2024 +0100

    change action name

commit 5a966bbe77463a93ab210cd4b6a122ab1d440486
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:22:16 2024 +0100

    cpp standard global

commit 673d1457162dda9018712d0a19ac5b3954ad265f
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:21:36 2024 +0100

    add glew dep

commit b64eb6dd3ef535183dd0620beeeaa99deb323c97
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:16:23 2024 +0100

    confirm

commit 1bf8065179b74f3dd194e05739a064097bdc2ebd
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:15:37 2024 +0100

    remove sudo

commit 2dc4ee69ebba3821916963a96621d6f8b59d4808
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 13:09:55 2024 +0100

    install apt

commit 69574b9c5a5074c2fdf87dfc8c068d9c64f7704c
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 10:31:11 2024 +0100

    add xmake cache

commit 4d7d04c6722ca8e94d10569f25ae582265866948
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 01:21:45 2024 +0100

    aaaaaaaaaaaaaaaaaa

commit aa7dac26cd9900c676e4662fadb7832700a3363a
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 01:13:08 2024 +0100

    fiiiiiiiiiiiiiix xmake root

commit 3604b44d04f492254911a0508e8555aca61b4d26
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 01:05:43 2024 +0100

    fix xmake root

commit cfcb8d4df46c6febeca47dff2f0447048b8f68ad
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 01:03:56 2024 +0100

    run xmake as root

commit dbef2d27138ccce1124cc1979867baf15e897a7f
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 00:54:54 2024 +0100

    xmake action

commit 42196398ced1e7f38c0234d82dc7cd65ef7aeb6d
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 00:53:29 2024 +0100

    xmake

commit 51256d8c15066120c2df3a72063856a0a47f8b59
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Mon Jan 1 00:47:35 2024 +0100

    Fix Linux.yaml

    add cert

commit 9c024fa8eb92765aff3fb195ce7c8c10a1b49d0b
Author: Simon Pribylski <sim16.prib@gmail.com>
Date:   Sun Dec 31 22:34:54 2023 +0100

    Add Linux actions
2024-02-10 15:49:25 +01:00
3d8fd2e519 refactor xmake.lua 2023-09-03 11:02:22 +02:00
4bd32e8d0b chore: bump version to alpha-0.4.0 2023-09-02 20:29:51 +02:00
09bba12814 prevent player from joining mid game 2023-09-02 20:27:26 +02:00
bb76e9493f sync player when joining mid game 2023-08-26 11:49:52 +02:00
Simon Pribylski
808ef7b3f6 moved player leave packet 2023-08-26 11:27:05 +02:00
Simon Pribylski
23938a0cb5 fix mob id 2023-08-26 11:21:28 +02:00
Simon Pribylski
5631efcf9e remove mobs on player leave 2023-08-26 10:46:20 +02:00
Simon Pribylski
8e7b446003 add RemoveMobPacket 2023-08-26 10:35:26 +02:00
Simon Pribylski
51ec035490 remove towers on player leave 2023-08-26 10:25:52 +02:00
1f94ae2586 use of SAFE_CHECK 2023-08-15 19:33:09 +02:00
21 changed files with 260 additions and 71 deletions

View File

@@ -0,0 +1,32 @@
name: Linux arm64
run-name: Build And Test
on: [push]
jobs:
Build:
runs-on: ubuntu-latest
steps:
- name: Install deps
run : |
apt update
apt install -y libsdl2-dev libglew-dev
- name: Check out repository code
uses: actions/checkout@v3
- name: Prepare Xmake
uses: xmake-io/github-action-setup-xmake@v1
with:
xmake-version: latest
actions-cache-folder: '.xmake-cache'
actions-cache-key: 'ubuntu'
- name: XMake config
run: xmake f -p linux -y --root
- name: Build
run: xmake --root
- name: Test
run: xmake test --root

21
LICENSE.txt Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 Simon Pribylski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -20,6 +20,7 @@ public:
virtual void HandlePacket(const protocol::SpawnMobPacket* packet) override;
virtual void HandlePacket(const protocol::UpgradeTowerPacket* packet) override;
virtual void HandlePacket(const protocol::WorldAddTowerPacket* packet) override;
virtual void HandlePacket(const protocol::RemoveMobPacket* packet) override;
virtual void HandlePacket(const protocol::RemoveTowerPacket* packet) override;
virtual void HandlePacket(const protocol::UpdateMobStatesPacket* packet) override;
virtual void HandlePacket(const protocol::UpdateCastleLifePacket* packet) override;

View File

@@ -2,7 +2,7 @@
#include "td/misc/DataBuffer.h"
#define TD_VERSION "alpha-0.3.0"
#define TD_VERSION "alpha-0.4.0"
namespace td {
namespace utils {

View File

@@ -35,6 +35,7 @@ public:
virtual void OnGameClose() override;
virtual void OnPlayerJoin(game::PlayerID id) override;
virtual void OnPlayerLeave(game::PlayerID id) override;
private:
void BalanceTeams();
void InitPlayerStats();

View File

@@ -99,7 +99,7 @@ private:
utils::CooldownTimer m_AttackTimer;
public:
Mob(MobID id, MobLevel level, PlayerID sender) : m_Sender(sender), m_Level(level),
Mob(MobID id, MobLevel level, PlayerID sender) : m_ID(id), m_Sender(sender), m_Level(level),
m_HitCooldown(0), m_EffectFireTimer(1000), m_EffectPoisonTimer(1000),
m_EffectHealTimer(1000), m_CastleTarget(nullptr), m_AttackTimer(1000) {

View File

@@ -174,6 +174,7 @@ public:
void Reset(); // clear mobs and towers
void SpawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID sender, float x, float y, Direction dir);
MobPtr RemoveMob(MobID id);
TowerPtr PlaceTowerAt(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder);
TowerPtr RemoveTower(TowerID id);
@@ -219,7 +220,9 @@ public:
const TeamList& GetTeams() const;
TowerList& GetTowers() { return m_Towers; }
const TowerList& GetTowers() const { return m_Towers; }
TowerPtr GetTowerById(TowerID tower);
const Player* GetPlayerById(PlayerID id) const;

View File

@@ -27,6 +27,7 @@ public:
virtual void HandlePacket(const PlayerLeavePacket* packet) {}
virtual void HandlePacket(const PlayerListPacket* packet) {}
virtual void HandlePacket(const PlayerLoginPacket* packet) {}
virtual void HandlePacket(const RemoveMobPacket* packet) {}
virtual void HandlePacket(const RemoveTowerPacket* packet) {}
virtual void HandlePacket(const SelectTeamPacket* packet) {}
virtual void HandlePacket(const SendMobsPacket* packet) {}

View File

@@ -8,6 +8,7 @@
#include "packets/PlayerLeavePacket.h"
#include "packets/PlayerListPacket.h"
#include "packets/PlayerLoginPacket.h"
#include "packets/RemoveMobPacket.h"
#include "packets/RemoveTowerPacket.h"
#include "packets/SelectTeamPacket.h"
#include "packets/SendMobsPacket.h"

View File

@@ -29,6 +29,7 @@ class UpdateCastleLifePacket;
class UpdateMobStatesPacket;
class PlayerBuyItemPacket;
class PlayerBuyMobUpgradePacket;
class RemoveMobPacket;
} // namespace protocol
} // namespace td

View File

@@ -13,7 +13,6 @@ enum class PacketType : std::uint8_t {
// client --> server
PlayerLogin = 0,
SelectTeam,
SpawnMob,
SendMobs,
PlaceTower,
@@ -33,6 +32,8 @@ enum class PacketType : std::uint8_t {
WorldAddTower,
UpdateMobStates,
UpdateCastleLife,
SpawnMob,
RemoveMob,
// client <--> server
KeepAlive,

View File

@@ -0,0 +1,27 @@
#pragma once
#include "td/protocol/Protocol.h"
#include "td/game/BaseGame.h"
namespace td {
namespace protocol {
class RemoveMobPacket : public Packet {
private:
game::MobID m_MobID;
public:
RemoveMobPacket() {}
RemoveMobPacket(game::MobID id) : m_MobID(id) {}
virtual ~RemoveMobPacket() {}
virtual DataBuffer Serialize(bool packetID = true) const;
virtual void Deserialize(DataBuffer& data);
virtual void Dispatch(PacketHandler* handler) const;
game::MobID GetMobID() const { return m_MobID; }
virtual PacketType GetType() const { return PacketType::RemoveMob; }
};
} // namespace protocol
} // namespace td

View File

@@ -8,6 +8,7 @@
#include "td/protocol/packets/WorldDataPacket.h"
#include "td/protocol/packets/SpawnMobPacket.h"
#include "td/protocol/packets/UpgradeTowerPacket.h"
#include "td/protocol/packets/RemoveMobPacket.h"
#include "td/protocol/packets/RemoveTowerPacket.h"
#include "td/protocol/packets/UpdateCastleLifePacket.h"
#include "td/protocol/packets/UpdateMobStatesPacket.h"
@@ -21,6 +22,7 @@ WorldClient::WorldClient(ClientGame* game) : game::World(game), protocol::Packet
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldData, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::SpawnMob, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::UpgradeTower, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::RemoveMob, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::RemoveTower, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateCastleLife, this);
GetDispatcher()->RegisterHandler(protocol::PacketType::UpdateMobStates, this);
@@ -50,7 +52,9 @@ void WorldClient::HandlePacket(const protocol::SpawnMobPacket* packet) {
void WorldClient::HandlePacket(const protocol::UpgradeTowerPacket* packet) {
game::TowerPtr tower = GetTowerById(packet->GetTowerID());
if (tower == nullptr) return; // this should not happen but who knows ?
SAFE_CHECK(tower);
tower->Upgrade(packet->GetTowerLevel().GetLevel(), packet->GetTowerLevel().GetPath());
}
@@ -63,9 +67,17 @@ void WorldClient::HandlePacket(const protocol::WorldAddTowerPacket* packet) {
void WorldClient::HandlePacket(const protocol::RemoveTowerPacket* packet) {
game::TowerPtr tower = RemoveTower(packet->GetTowerID());
if (tower != nullptr) {
GetWorldNotifier().NotifyListeners(&WorldListener::OnTowerRemove, tower);
}
SAFE_CHECK(tower);
GetWorldNotifier().NotifyListeners(&WorldListener::OnTowerRemove, tower);
}
void WorldClient::HandlePacket(const protocol::RemoveMobPacket* packet) {
game::MobPtr mob = RemoveMob(packet->GetMobID());
SAFE_CHECK(mob);
//GetWorldNotifier().NotifyListeners(&MobListener::OnMobDie, mob.get());
}
void WorldClient::HandlePacket(const protocol::UpdateMobStatesPacket* packet) {

View File

@@ -163,10 +163,14 @@ void Server::BroadcastPacket(const protocol::Packet* packet) {
}
void Server::RemoveConnexion(std::uint8_t connexionID) {
std::string playerName = GetGame().GetPlayerById(connexionID)->GetName();
m_Connections.erase(connexionID);
td::game::Player* player = GetGame().GetPlayerById(connexionID);
SAFE_CHECK(player);
std::string playerName = player->GetName();
GetGame().RemovePlayer(connexionID);
m_Connections.erase(connexionID);
m_Lobby.OnPlayerLeave(connexionID);
utils::LOG(utils::format("\t[%s] left !", playerName.c_str()));
@@ -176,13 +180,10 @@ void Server::RemoveConnexion(std::uint8_t connexionID) {
void Server::OnPlayerJoin(std::uint8_t id) {
m_Lobby.OnPlayerJoin(id);
GetPlayers().insert({ id, game::Player{id} });
}
void Server::OnPlayerLeave(std::uint8_t id) {
protocol::PlayerLeavePacket packet(id);
BroadcastPacket(&packet);
GetGame().NotifyListeners(&game::GameListener::OnPlayerLeave, id);
if (GetPlayers().empty() && m_Game.GetGameState() != game::GameState::Lobby) {
utils::LOG("All players left. Go back to lobby ...");

View File

@@ -13,6 +13,7 @@
#include "td/protocol/packets/RemoveTowerPacket.h"
#include "td/protocol/packets/SelectTeamPacket.h"
#include "td/protocol/packets/SendMobsPacket.h"
#include "td/protocol/packets/SpawnMobPacket.h"
#include "td/protocol/packets/UpdatePlayerTeamPacket.h"
#include "td/protocol/packets/UpdateGameStatePacket.h"
#include "td/protocol/packets/UpgradeTowerPacket.h"
@@ -92,62 +93,94 @@ void ServerConnexion::SendKeepAlive() {
}
void ServerConnexion::HandlePacket(const protocol::PlayerLoginPacket* packet) {
if (m_Player->GetName().empty() && !packet->GetPlayerName().empty()) {
m_Player->SetName(packet->GetPlayerName());
utils::LOG(utils::format("\t[%s] joined !", m_Player->GetName().c_str()));
if (m_Server->GetGame().GetGameState() != game::GameState::Lobby) {
protocol::DisconnectPacket packet("Cannot join during game");
SendPacket(&packet);
protocol::PlayerJoinPacket joinPacket(m_ID, m_Player->GetName());
m_Server->BroadcastPacket(&joinPacket);
CloseConnection();
return;
}
std::map<std::uint8_t, protocol::PlayerInfo> playerNames;
for (const auto& pair : m_Server->GetPlayers()) {
const game::Player& player = pair.second;
if (!player.GetName().empty()) {
protocol::PlayerInfo playerInfo;
playerInfo.name = player.GetName();
playerInfo.team = player.GetTeamColor();
playerNames.insert({ player.GetID(), playerInfo });
}
SAFE_CHECK(!packet->GetPlayerName().empty());
if (!m_Player) { // player does not exist yet
auto playerPos = m_Server->GetPlayers().insert({ m_ID, game::Player{m_ID} });
m_Player = &playerPos.first->second;
}
SAFE_CHECK(m_Player->GetName().empty());
m_Player->SetName(packet->GetPlayerName());
utils::LOG(utils::format("\t[%s] joined !", m_Player->GetName().c_str()));
protocol::PlayerJoinPacket joinPacket(m_ID, m_Player->GetName());
m_Server->BroadcastPacket(&joinPacket);
std::map<std::uint8_t, protocol::PlayerInfo> playerNames;
for (const auto& pair : m_Server->GetPlayers()) {
const game::Player& player = pair.second;
if (!player.GetName().empty()) {
protocol::PlayerInfo playerInfo;
playerInfo.name = player.GetName();
playerInfo.team = player.GetTeamColor();
playerNames.insert({ player.GetID(), playerInfo });
}
}
protocol::PlayerListPacket listPacket(playerNames);
SendPacket(&listPacket);
protocol::PlayerListPacket listPacket(playerNames);
SendPacket(&listPacket);
protocol::UpdateGameStatePacket statePacket(m_Server->GetGame().GetGameState());
SendPacket(&statePacket);
protocol::UpdateGameStatePacket statePacket(m_Server->GetGame().GetGameState());
SendPacket(&statePacket);
m_Server->GetGame().NotifyListeners(&game::GameListener::OnPlayerJoin, m_ID);
m_Server->GetGame().NotifyListeners(&game::GameListener::OnPlayerJoin, m_ID);
protocol::WorldBeginDataPacket headerDataPacket(m_Server->GetGame().GetWorld());
protocol::WorldDataPacket dataPacket(m_Server->GetGame().GetWorld());
SendPacket(&headerDataPacket);
SendPacket(&dataPacket);
protocol::WorldBeginDataPacket headerDataPacket(m_Server->GetGame().GetWorld());
protocol::WorldDataPacket dataPacket(m_Server->GetGame().GetWorld());
SendPacket(&headerDataPacket);
SendPacket(&dataPacket);
// place towers
for (auto tower : m_Server->GetGame().GetWorld()->GetTowers()) {
protocol::WorldAddTowerPacket packet(tower->GetID(), static_cast<std::int32_t>(tower->GetCenterX() - 0.5f),
static_cast<std::int32_t>(tower->GetCenterY() - 0.5f), tower->GetType(), tower->GetBuilder());
SendPacket(&packet);
}
// spawn mobs
for (auto mob : m_Server->GetGame().GetWorld()->GetMobList()) {
protocol::SpawnMobPacket packet(mob->GetMobID(), mob->GetType(), mob->GetLevel(), mob->GetSender(),
mob->GetCenterX(), mob->GetCenterY(), mob->GetDirection());
SendPacket(&packet);
// TODO : update health
}
}
void ServerConnexion::HandlePacket(const protocol::SelectTeamPacket* packet) {
SAFE_CHECK(m_Server->GetGame().GetGameState() == game::GameState::Lobby);
SAFE_CHECK(static_cast<std::int8_t>(packet->GetSelectedTeam()) >= -1 ||
static_cast<std::int8_t>(packet->GetSelectedTeam()) <= 1);
if (static_cast<std::int8_t>(packet->GetSelectedTeam()) >= -1 || static_cast<std::int8_t>(packet->GetSelectedTeam()) <= 1) {
if (m_Player->GetTeamColor() == game::TeamColor::None) { // join a team
m_Server->GetGame().GetTeam(packet->GetSelectedTeam()).AddPlayer(m_Player);
} else if (packet->GetSelectedTeam() == game::TeamColor::None) { // leave a team
m_Server->GetGame().GetTeam(m_Player->GetTeamColor()).RemovePlayer(m_Player);
m_Player->SetTeamColor(game::TeamColor::None);
} else { // change team
m_Server->GetGame().GetTeam(m_Player->GetTeamColor()).RemovePlayer(m_Player);
m_Server->GetGame().GetTeam(packet->GetSelectedTeam()).AddPlayer(m_Player);
}
m_Player->SetTeamColor(packet->GetSelectedTeam());
protocol::UpdatePlayerTeamPacket updateTeamPacket(m_ID, packet->GetSelectedTeam());
m_Server->BroadcastPacket(&updateTeamPacket);
if (m_Player->GetTeamColor() == game::TeamColor::None) { // join a team
m_Server->GetGame().GetTeam(packet->GetSelectedTeam()).AddPlayer(m_Player);
} else if (packet->GetSelectedTeam() == game::TeamColor::None) { // leave a team
m_Server->GetGame().GetTeam(m_Player->GetTeamColor()).RemovePlayer(m_Player);
m_Player->SetTeamColor(game::TeamColor::None);
} else { // change team
m_Server->GetGame().GetTeam(m_Player->GetTeamColor()).RemovePlayer(m_Player);
m_Server->GetGame().GetTeam(packet->GetSelectedTeam()).AddPlayer(m_Player);
}
m_Player->SetTeamColor(packet->GetSelectedTeam());
protocol::UpdatePlayerTeamPacket updateTeamPacket(m_ID, packet->GetSelectedTeam());
m_Server->BroadcastPacket(&updateTeamPacket);
}
void ServerConnexion::HandlePacket(const protocol::KeepAlivePacket* packet) {
if (packet->GetAliveID() == m_KeepAlive.keepAliveID)
m_KeepAlive.recievedResponse = true;
SAFE_CHECK(packet->GetAliveID() == m_KeepAlive.keepAliveID);
m_KeepAlive.recievedResponse = true;
}
void ServerConnexion::HandlePacket(const protocol::DisconnectPacket* packet) {
@@ -156,7 +189,6 @@ void ServerConnexion::HandlePacket(const protocol::DisconnectPacket* packet) {
void ServerConnexion::SetServer(Server* server) {
m_Server = server;
m_Player = &m_Server->GetPlayers().at(m_ID);
InitConnection();
SendKeepAlive();
}
@@ -209,8 +241,9 @@ void ServerConnexion::HandlePacket(const protocol::RemoveTowerPacket* packet) {
}
ServerConnexion::~ServerConnexion() {
if (GetDispatcher() != nullptr)
GetDispatcher()->UnregisterHandler(this);
SAFE_CHECK(GetDispatcher() != nullptr);
GetDispatcher()->UnregisterHandler(this);
}
} // namespace server

View File

@@ -2,6 +2,9 @@
#include "server/Server.h"
#include "td/protocol/packets/DisconnectPacket.h"
#include "td/protocol/packets/PlayerLeavePacket.h"
#include "td/protocol/packets/RemoveMobPacket.h"
#include "td/protocol/packets/RemoveTowerPacket.h"
#include "td/protocol/packets/UpdatePlayerTeamPacket.h"
#include "td/protocol/packets/UpdateGameStatePacket.h"
#include "td/protocol/packets/UpdateMoneyPacket.h"
@@ -134,10 +137,41 @@ void ServerGame::OnGameClose() {
m_Server->Restart();
}
void ServerGame::OnPlayerJoin(game::PlayerID id){
void ServerGame::OnPlayerJoin(game::PlayerID id) {
if (m_GameState == game::GameState::Game)
BalanceTeams();
}
void ServerGame::OnPlayerLeave(game::PlayerID playerId) {
// temporary fix
auto& mobList = GetWorld()->GetMobList();
for(std::size_t i = 0; i < mobList.size(); i++) {
auto mob = mobList.at(i);
if(mob->GetSender() == playerId) {
protocol::RemoveMobPacket packet(mob->GetMobID());
m_Server->BroadcastPacket(&packet);
mobList.erase(mobList.begin() + i);
}
}
auto& towerList = GetWorld()->GetTowers();
for(std::size_t i = 0; i < towerList.size(); i++) {
auto tower = towerList.at(i);
if(tower->GetBuilder() == playerId) {
protocol::RemoveTowerPacket packet(tower->GetID());
m_Server->BroadcastPacket(&packet);
towerList.erase(towerList.begin() + i);
}
}
protocol::PlayerLeavePacket packet(playerId);
m_Server->BroadcastPacket(&packet);
}
} // namespace game
} // namespace td

View File

@@ -123,6 +123,17 @@ void World::SpawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID send
GetMobNotifier().NotifyListeners(&MobListener::OnMobSpawn, mob.get());
}
MobPtr World::RemoveMob(MobID mobId) {
auto it = std::find_if(m_Mobs.begin(), m_Mobs.end(), [mobId](MobPtr mob) { return mob->GetMobID() == mobId;});
if (it == m_Mobs.end()) return nullptr;
MobPtr mob = *it;
m_Mobs.erase(it);
return mob;
}
TowerPtr World::PlaceTowerAt(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder) {
TowerPtr tower = TowerFactory::CreateTower(type, id, x, y, builder);
m_Towers.push_back(tower);

View File

@@ -36,6 +36,7 @@ static std::map<PacketType, PacketCreator> packets = {
{PacketType::UpdateMobStates, []() -> PacketPtr {return std::make_unique<UpdateMobStatesPacket>(); } },
{PacketType::PlayerBuyItem, []() -> PacketPtr {return std::make_unique<PlayerBuyItemPacket>(); } },
{PacketType::PlayerBuyMobUpgrade, []() -> PacketPtr {return std::make_unique<PlayerBuyMobUpgradePacket>(); } },
{PacketType::RemoveMob, []() -> PacketPtr {return std::make_unique<RemoveMobPacket>(); } },
};
PacketPtr CreatePacket(PacketType type, DataBuffer& buffer) {

View File

@@ -39,6 +39,7 @@ REGISTER_DISPATCH_CLASS(UpdateCastleLifePacket)
REGISTER_DISPATCH_CLASS(UpdateMobStatesPacket)
REGISTER_DISPATCH_CLASS(PlayerBuyItemPacket)
REGISTER_DISPATCH_CLASS(PlayerBuyMobUpgradePacket)
REGISTER_DISPATCH_CLASS(RemoveMobPacket)
} // namespace protocol
} // namespace td

View File

@@ -0,0 +1,19 @@
#include "td/protocol/packets/RemoveMobPacket.h"
namespace td {
namespace protocol {
DataBuffer RemoveMobPacket::Serialize(bool packetID) const {
DataBuffer data;
WritePacketID(data, packetID);
data << m_MobID;
return data;
}
void RemoveMobPacket::Deserialize(DataBuffer& data) {
data >> m_MobID;
}
} // namespace protocol
} // namespace td

View File

@@ -3,6 +3,7 @@ add_rules("mode.debug", "mode.release")
add_defines("TD_IMPL_OPENGL_LOADER_GLEW")
add_requires("libsdl >= 2", "zlib", "glew")
set_languages("c++17")
target("TowerDefenseData")
set_kind("static")
@@ -10,8 +11,6 @@ target("TowerDefenseData")
add_includedirs("include")
add_files("src/td/**.cpp")
set_languages("c++17")
add_packages("zlib")
add_links("pthread")
@@ -21,18 +20,7 @@ target("TowerDefenseData")
add_links("ws2_32") -- link network stuff
end
if is_mode("release") then
-- mark symbols visibility as hidden
set_symbols("hidden")
-- strip all symbols
add_ldflags("-s")
set_warnings("all", "error")
set_optimize("fastest")
else -- debug stuff
if is_mode("debug") then
if is_os("linux") then
add_links("dw")
end