diff --git a/SQCSim-common/bullet.cpp b/SQCSim-common/bullet.cpp index 9b26861..b5a38e4 100644 --- a/SQCSim-common/bullet.cpp +++ b/SQCSim-common/bullet.cpp @@ -7,7 +7,7 @@ Bullet::Bullet(Vector3f pos, Vector3f dir, uint64_t tid): m_startpos(pos), m_cur Bullet::~Bullet() {} -bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordered_map mapPlayer) { +bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordered_map mapPlayer, netprot::ChunkMod** chunkmod) { int max = 100 / perframe; float damage = 0.057f; for (int x = 0; x < max; ++x) { @@ -23,6 +23,15 @@ bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordere if (!world->ChunkAt(m_currentpos)) return true; else if (world->BlockAt(m_currentpos) != BTYPE_AIR) { + if (chunkmod) { + using namespace netprot; + ChunkMod* cmod = *chunkmod; + cmod = new ChunkMod(); + cmod->old_b_type = world->BlockAt(m_currentpos); + cmod->b_type = BTYPE_AIR; + cmod->pos = m_currentpos; + } + world->ChangeBlockAtPosition(BTYPE_AIR, m_currentpos); return true; } diff --git a/SQCSim-common/bullet.h b/SQCSim-common/bullet.h index 04bbfad..34b21bf 100644 --- a/SQCSim-common/bullet.h +++ b/SQCSim-common/bullet.h @@ -5,7 +5,7 @@ #include "define.h" #include "vector3.h" #include "player.h" - +#include "netprotocol.h" class World; class Player; @@ -16,7 +16,7 @@ public: Bullet(Vector3f pos, Vector3f dir, uint64_t tid); ~Bullet(); - bool Update(World* world, float elapsedtime, int perframe, std::unordered_map m_mapPlayer); + bool Update(World* world, float elapsedtime, int perframe, std::unordered_map m_mapPlayer, netprot::ChunkMod** chunkmod); void Transpose(int& x, int& z); Vector3f getPos() const; Vector3f getVel() const; diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp index 5441a5b..2ba7889 100644 --- a/SQCSim-common/netprotocol.cpp +++ b/SQCSim-common/netprotocol.cpp @@ -425,8 +425,9 @@ void netprot::Serialize(ChunkMod* chmod, char* buf[], uint32_t* buflen) { memcpy(*buf + 1, vec8, sizeof(uint32_t) * 3); memcpy(*buf + sizeof(uint32_t) * 3 + 1, &chmod->b_type, sizeof(BlockType)); + memcpy(*buf + sizeof(uint32_t) * 3 + 2, &chmod->old_b_type, sizeof(BlockType)); - *buflen = sizeof(uint32_t) * 3 + 2; + *buflen = sizeof(uint32_t) * 3 + 3; } void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) { @@ -868,8 +869,9 @@ bool netprot::Deserialize(ChunkMod* chmod, char* buf, uint32_t* buflen) { memcpy(&chmod->pos, vec, sizeof(uint32_t) * 3); memcpy(&chmod->b_type, &buf[1 + sizeof(uint8_t) * 12], sizeof(BlockType)); + memcpy(&chmod->old_b_type, &buf[2 + sizeof(uint8_t) * 12], sizeof(BlockType)); - *buflen = sizeof(uint32_t) * 3 + 2; + *buflen = sizeof(uint32_t) * 3 + 3; return true; } diff --git a/SQCSim-common/netprotocol.h b/SQCSim-common/netprotocol.h index c7223dd..27e7f56 100644 --- a/SQCSim-common/netprotocol.h +++ b/SQCSim-common/netprotocol.h @@ -132,7 +132,7 @@ namespace netprot { struct ChunkMod { Vector3f pos; - BlockType b_type; + BlockType b_type, old_b_type; }; struct ErrorLog { // srv -> cli TCP event diff --git a/SQCSim-common/world.cpp b/SQCSim-common/world.cpp index 70d6eb6..2791ca1 100644 --- a/SQCSim-common/world.cpp +++ b/SQCSim-common/world.cpp @@ -169,31 +169,26 @@ void World::Update(Bullet* bullets[MAX_BULLETS], const Vector3f& player_pos, Blo UpdateWorld(player_pos, blockinfo); //TransposeWorld(player_pos, bullets); } -// -//void World::UpdateChunk(int& updates, unsigned int chx, unsigned int chy, BlockInfo* blockinfo[BTYPE_LAST]) { -// if (updates == 0 && ChunkAt(chx, 1, chy) && -// ChunkAt(chx, 1, chy)->IsDirty()) { -// ChunkAt(chx, 1, chy)->Update(blockinfo, this); -// updates = FRAMES_UPDATE_CHUNKS; -// } -// -//} -void World::ChangeBlockAtCursor(BlockType blockType, const Vector3f& player_pos, const Vector3f& player_dir, bool& block) { +netprot::ChunkMod* World::ChangeBlockAtCursor(BlockType blockType, const Vector3f& player_pos, const Vector3f& player_dir, bool& block, bool net) { Vector3f currentPos = player_pos; Vector3f currentBlock = currentPos; Vector3f ray = player_dir; + BlockType oldbtype; + netprot::ChunkMod* cmod = nullptr; bool found = false; - if (block) return; + if (block) return cmod; while ((currentPos - currentBlock).Length() <= MAX_SELECTION_DISTANCE && !found) { currentBlock += ray / 10.f; BlockType bt = BlockAt(currentBlock); - if (bt != BTYPE_AIR) + if (bt != BTYPE_AIR) { found = true; + oldbtype = bt; + } } if (found) @@ -219,21 +214,30 @@ void World::ChangeBlockAtCursor(BlockType blockType, const Vector3f& player_pos, (By == PyA || By == PyB || By == PyC) && - Bz == Pz)) + Bz == Pz)) { found = true; + oldbtype = bt; + } } } } if (found && (int)currentBlock.y < CHUNK_SIZE_Y) { + if (net) { + cmod = new netprot::ChunkMod(); + cmod->old_b_type = oldbtype; + cmod->b_type = blockType; + cmod->pos = currentBlock; + } int bx = (int)currentBlock.x % CHUNK_SIZE_X; int by = (int)currentBlock.y % CHUNK_SIZE_Y; int bz = (int)currentBlock.z % CHUNK_SIZE_Z; - ChunkAt(currentBlock)->SetBlock(bx, by, bz, blockType, this); ChunkAt(currentBlock)->MakeModified(); block = true; } + + return cmod; } void World::ChangeBlockAtPosition(BlockType blockType, Vector3f pos) { diff --git a/SQCSim-common/world.h b/SQCSim-common/world.h index e9b4840..06c513b 100644 --- a/SQCSim-common/world.h +++ b/SQCSim-common/world.h @@ -11,6 +11,7 @@ #include "array2d.h" #include "bullet.h" #include "chunk.h" +#include "netprotocol.h" class Chunk; class Bullet; @@ -37,7 +38,7 @@ public: void GetScope(unsigned int& x, unsigned int& y); - void ChangeBlockAtCursor(BlockType blockType, const Vector3f& player_pos, const Vector3f& player_dir, bool& block); + netprot::ChunkMod* ChangeBlockAtCursor(BlockType blockType, const Vector3f& player_pos, const Vector3f& player_dir, bool& block, bool net); void ChangeBlockAtPosition(BlockType blockType, Vector3f pos); void CleanUpWorld(int& deleteframes, bool clear); int GettbDeleted() const; diff --git a/SQCSim-srv/connection.cpp b/SQCSim-srv/connection.cpp index 74e0506..39fed03 100644 --- a/SQCSim-srv/connection.cpp +++ b/SQCSim-srv/connection.cpp @@ -122,6 +122,13 @@ void Connection::Run(World* world) { last = m_input_vector.at(m_last_in); el = (double)(in.timestamp - last.timestamp) / 1000.; + + if (m_shoot_acc > 0.) { + m_shoot_acc -= el; + if (m_shoot_acc < 0.) + m_shoot_acc = 0; + } + player.get()->SetDirection(in.direction); player.get()->ApplyPhysics(player.get()->GetInput(in.keys.forward, in.keys.backward, @@ -129,6 +136,13 @@ void Connection::Run(World* world) { in.keys.right, in.keys.jump, false, el), world, el); + //if (in.keys.block) + // ChunkDiffs.push_back() + + if (in.keys.shoot && m_shoot_acc <= 0.) + Bullets.push_back(Bullet(player.get()->GetPOV() + player.get()->GetDirection(), player.get()->GetDirection())); + + out.position = player.get()->GetPositionAbs(); out.direction = in.direction; out.timestamp = in.timestamp; diff --git a/SQCSim-srv/connection.h b/SQCSim-srv/connection.h index 4af3596..7554f5a 100644 --- a/SQCSim-srv/connection.h +++ b/SQCSim-srv/connection.h @@ -42,8 +42,8 @@ public: bool m_nsync = true; - std::vector m_bullet_vector; - std::vector m_diff_vector; + std::vector Bullets; + std::vector ChunkDiffs; private: std::unordered_map m_input_manifest; @@ -52,6 +52,7 @@ private: std::deque m_output_vector; std::unordered_map m_chatlog; + float m_shoot_acc = 0; SOCKET m_sock; sockaddr_in m_addr; diff --git a/SQCSim2021/engine.cpp b/SQCSim2021/engine.cpp index d8945db..f30b4bc 100644 --- a/SQCSim2021/engine.cpp +++ b/SQCSim2021/engine.cpp @@ -1193,10 +1193,11 @@ void Engine::Render(float elapsedTime) { if (bloc == BTYPE_LAST + 1) bloc = BTYPE_AIR + 1; else if (bloc == BTYPE_AIR) bloc = BTYPE_LAST; // La selection de BTYPE_LAST �quipe l'arme. m_mouseWU = m_mouseWD = m_key1 = m_key2 = false; + netprot::ChunkMod* cmod = nullptr; if (m_mouseL) { if (bloc != BTYPE_LAST) - m_world.ChangeBlockAtCursor(bloc, m_player.GetPosition(), m_player.GetDirection(), m_block); + cmod = m_world.ChangeBlockAtCursor(bloc, m_player.GetPosition(), m_player.GetDirection(), m_block, m_networkgame); else if (bulletTime <= 0.f) { for (int x = 0; x < MAX_BULLETS; ++x) // Ajouter une balle dans l'array (aussi connu sous le nom de "faire pow pow"). if (!m_bullets[x]) { @@ -1218,12 +1219,12 @@ void Engine::Render(float elapsedTime) { } } else if (m_mouseR) - m_world.ChangeBlockAtCursor(BTYPE_AIR, m_player.GetPosition(), m_player.GetDirection(), m_block); + cmod = m_world.ChangeBlockAtCursor(BTYPE_AIR, m_player.GetPosition(), m_player.GetDirection(), m_block, m_networkgame); for (int x = 0; x < MAX_BULLETS; ++x) { // Array de bullets en jeu. if (m_bullets[x]) { for (int b = 0; b < BULLET_UPDATES_PER_FRAME; ++b) { - if (m_bullets[x]->Update(&m_world, elapsedTime, BULLET_UPDATES_PER_FRAME, m_players)) { + if (m_bullets[x]->Update(&m_world, elapsedTime, BULLET_UPDATES_PER_FRAME, m_players, m_networkgame ? &m_chunkmod : nullptr)) { m_bullets[x]->~Bullet(); if (m_whoosh[x]) m_whoosh[x]->drop(); @@ -1282,6 +1283,9 @@ void Engine::Render(float elapsedTime) { static std::vector lsPck; static int sync_acc = 0; + if (cmod) + m_chunkmod_manifest.emplace_back(cmod); + if (last == 0) last = tstamp; diff --git a/SQCSim2021/engine.h b/SQCSim2021/engine.h index d7860ed..26e14dc 100644 --- a/SQCSim2021/engine.h +++ b/SQCSim2021/engine.h @@ -101,10 +101,12 @@ private: Player m_player = Player(Vector3f(.5f, CHUNK_SIZE_Y + 1.8f, .5f)); Bullet* m_bullets[MAX_BULLETS]; + std::vector m_chunkmod_manifest; std::unordered_map m_players; netprot::Buffer m_buf, m_bufout; std::chrono::high_resolution_clock::time_point m_startTime; + netprot::ChunkMod* m_chunkmod = nullptr; //Menu enum class GameState: uint8_t { MAIN_MENU, OPTIONS, QUIT, NEWG, PLAY, PAUSE };