heum well
This commit is contained in:
parent
253e49b1cc
commit
bc1f4f750b
@ -47,19 +47,19 @@ PlayerInfo* Connection::getInfo() const { return (PlayerInfo*)&m_playinfo; }
|
||||
|
||||
sockaddr_in* Connection::getAddr() const { return (sockaddr_in*)&m_addr; }
|
||||
|
||||
void Connection::getPacks() {
|
||||
void Connection::getPacks(SOCKET sock) {
|
||||
std::vector<char*> lsPck;
|
||||
Input in;
|
||||
while (true) {
|
||||
lsPck = recvPacksFrom(m_sock, m_buf, m_addr);
|
||||
lsPck = recvPacksFrom(sock, &m_buf, m_addr);
|
||||
|
||||
for (auto& pck : lsPck) {
|
||||
uint32_t bsize = m_buf->len - (pck - m_buf->ptr);
|
||||
uint32_t bsize = m_buf.len - (pck - m_buf.ptr);
|
||||
switch (netprot::getType(pck, 1)) {
|
||||
using enum netprot::PACKET_TYPE;
|
||||
case INPUT:
|
||||
if (Deserialize(&in, pck, &bsize))
|
||||
m_input_manifest.insert({ in.timestamp, in });
|
||||
m_input_manifest[in.timestamp] = in;
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
@ -68,7 +68,17 @@ void Connection::getPacks() {
|
||||
}
|
||||
}
|
||||
|
||||
std::thread Connection::Start(){ return std::thread(getPacks); }
|
||||
void Connection::sendPacks(SOCKET sock, std::unordered_map<uint64_t, Connection*> conns) {
|
||||
while (m_last_out < m_output_manifest.size()) {
|
||||
Output out = m_output_manifest.at(m_last_out++);
|
||||
|
||||
for (auto& [key, conn] : conns) {
|
||||
if (m_playinfo.id == conn->GetHash(true))
|
||||
continue;
|
||||
sendPackTo<Output>(sock, &out, &m_bufout, conn->getAddr());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::Run(World* world) {
|
||||
Input in, last;
|
||||
@ -78,17 +88,27 @@ void Connection::Run(World* world) {
|
||||
if (m_input_manifest.size() < 2)
|
||||
return;
|
||||
|
||||
in = m_input_manifest.at(m_input_manifest.size());
|
||||
last = m_input_manifest.at(m_input_manifest.size() - 1);
|
||||
while (m_last_in < m_input_manifest.size()) {
|
||||
in = m_input_manifest.at(m_last_in + 1);
|
||||
last = m_input_manifest.at(m_last_in);
|
||||
|
||||
el = (float)(in.timestamp - last.timestamp) / 1000.;
|
||||
el = (float)(in.timestamp - last.timestamp) / 1000.;
|
||||
player.get()->SetDirection(in.direction);
|
||||
player.get()->ApplyPhysics(player.get()->GetInput(in.keys.forward,
|
||||
in.keys.backward,
|
||||
in.keys.left,
|
||||
in.keys.right,
|
||||
in.keys.jump, false, el), world, el);
|
||||
|
||||
player.get()->SetDirection(in.direction);
|
||||
player.get()->ApplyPhysics(player.get()->GetInput(in.keys.forward, in.keys.backward, in.keys.left, in.keys.right, in.keys.jump, false, el), world, el);
|
||||
out.position = player.get()->GetPosition();
|
||||
out.direction = in.direction;
|
||||
out.timestamp = in.timestamp;
|
||||
out.id = m_playinfo.id;
|
||||
|
||||
out.position = player.get()->GetPosition();
|
||||
out.direction = in.direction;
|
||||
out.timestamp = in.timestamp;
|
||||
m_output_manifest[out.timestamp] = out;
|
||||
|
||||
++m_last_in;
|
||||
}
|
||||
}
|
||||
|
||||
void Connection::CleanInputManifest(Timestamp time) {
|
||||
|
@ -33,9 +33,8 @@ public:
|
||||
PlayerInfo* getInfo() const;
|
||||
sockaddr_in* getAddr() const;
|
||||
|
||||
void getPacks();
|
||||
|
||||
std::thread Start();
|
||||
void getPacks(SOCKET sock);
|
||||
void sendPacks(SOCKET sock, std::unordered_map<uint64_t, Connection*> conns);
|
||||
|
||||
void Run(World* world);
|
||||
|
||||
@ -50,6 +49,11 @@ private:
|
||||
LoginInfo m_loginfo;
|
||||
PlayerInfo m_playinfo;
|
||||
|
||||
Buffer* m_buf;
|
||||
Timestamp m_startsync;
|
||||
|
||||
uint64_t m_last_in = 0,
|
||||
m_last_out = 0;
|
||||
Buffer m_buf,
|
||||
m_bufout;
|
||||
};
|
||||
#endif
|
||||
|
@ -155,7 +155,7 @@ int Server::Ready() {
|
||||
play.tid = log->tid;
|
||||
|
||||
sendPack<GameInfo>(sock, &m_game, &m_buf);
|
||||
std::unique_ptr<Connection> conn = std::make_unique<Connection>(sock, sockad, *log, play);
|
||||
Connection* conn = new Connection(sock, sockad, *log, play);
|
||||
|
||||
for (auto& [key, player] : m_players) {
|
||||
sendPack<PlayerInfo>(player->getSock(), &play, &m_buf); // Envoyer les infos de joueur distant aux joueurs d<>j<EFBFBD> connect<63>s
|
||||
@ -183,16 +183,12 @@ void Server::Run() {
|
||||
Log("Debut de la partie...", false, false);
|
||||
|
||||
int players = m_players.size();
|
||||
std::thread* getPacksThreads = new std::thread[players];
|
||||
|
||||
m_world = std::make_unique<World>();
|
||||
m_world = new World();
|
||||
m_world->SetSeed(m_game.seed);
|
||||
m_world->GetChunks().Reset(nullptr);
|
||||
m_world->BuildWorld();
|
||||
|
||||
for (int x = 0; x < players; ++x)
|
||||
getPacksThreads[x] = m_players[x].get()->start();
|
||||
|
||||
|
||||
for (auto& [key, conn] : m_players) { // Creation des instances de joueurs et premier sync.
|
||||
conn->player = std::make_unique<Player>(Vector3f(8.5f, CHUNK_SIZE_Y + 1.8f, 8.5f));
|
||||
Sync sync;
|
||||
@ -207,7 +203,9 @@ void Server::Run() {
|
||||
|
||||
while (!endgame) {
|
||||
for (auto& [key, conn] : m_players) {
|
||||
conn->player->GetInput
|
||||
conn->getPacks(m_sock_udp);
|
||||
conn->Run(m_world);
|
||||
conn->sendPacks(m_sock_udp, m_players);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -36,12 +36,12 @@ private:
|
||||
|
||||
Buffer m_buf;
|
||||
|
||||
std::unordered_map<uint64_t, std::unique_ptr<Connection>> m_players;
|
||||
std::unordered_map<uint64_t, Connection*> m_players;
|
||||
std::unordered_map<Timestamp, Chat> m_chatlog;
|
||||
std::vector<uint64_t> m_ids;
|
||||
GameInfo m_game;
|
||||
|
||||
std::unique_ptr<World> m_world = nullptr;
|
||||
World* m_world = nullptr;
|
||||
const bool m_manual_setup = SRV_MANUAL_SETUP;
|
||||
|
||||
std::string LogTimestamp();
|
||||
|
@ -1111,6 +1111,7 @@ void Engine::Render(float elapsedTime) {
|
||||
using namespace std::chrono;
|
||||
using namespace netprot;
|
||||
Input input;
|
||||
static std::vector<char*> lsPck;
|
||||
|
||||
input.sid = m_conn.getId();
|
||||
input.direction = m_player.GetDirection();
|
||||
@ -1123,10 +1124,27 @@ void Engine::Render(float elapsedTime) {
|
||||
input.keys.block = m_mouseR;
|
||||
input.keys.shoot = m_mouseL;
|
||||
|
||||
sendPackTo<Input>(m_conn.m_sock_udp, &input, &m_buf, &m_conn.m_srvsockaddr);
|
||||
sendPackTo<Input>(m_conn.m_sock_udp, &input, &m_bufout, &m_conn.m_srvsockaddr);
|
||||
|
||||
// TODO: Faire la gestion de la réception de paquets.
|
||||
lsPck = recvPacks(m_conn.m_sock_udp, &m_buf);
|
||||
|
||||
for (auto& pck : lsPck) {
|
||||
uint32_t bsize = m_buf.len - (pck - m_buf.ptr);
|
||||
netprot::Sync sync;
|
||||
netprot::Output out;
|
||||
switch (netprot::getType(pck, 1)) {
|
||||
using enum netprot::PACKET_TYPE;
|
||||
case SYNC:
|
||||
if (!netprot::Deserialize(&sync, pck, &bsize)) {}
|
||||
break;
|
||||
case OUTPUT:
|
||||
if (!netprot::Deserialize(&out, pck, &bsize)) {}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
lsPck.clear();
|
||||
}
|
||||
}
|
||||
else if (m_gamestate == GameState::MAIN_MENU || m_gamestate == GameState::OPTIONS)
|
||||
|
@ -90,7 +90,7 @@ private:
|
||||
Bullet* m_bullets[MAX_BULLETS];
|
||||
|
||||
std::unordered_map<uint64_t, Player*> m_players;
|
||||
netprot::Buffer m_buf;
|
||||
netprot::Buffer m_buf, m_bufout;
|
||||
std::chrono::high_resolution_clock::time_point m_startTime;
|
||||
|
||||
//Menu
|
||||
|
Loading…
Reference in New Issue
Block a user