This commit is contained in:
MarcEricMartel 2023-12-02 11:05:00 -05:00
parent 20eb410b08
commit 5a491c5446
10 changed files with 63 additions and 26 deletions

View File

@ -7,7 +7,7 @@ Bullet::Bullet(Vector3f pos, Vector3f dir, uint64_t tid): m_startpos(pos), m_cur
Bullet::~Bullet() {} Bullet::~Bullet() {}
bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordered_map<uint64_t, Player*> mapPlayer) { bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordered_map<uint64_t, Player*> mapPlayer, netprot::ChunkMod** chunkmod) {
int max = 100 / perframe; int max = 100 / perframe;
float damage = 0.057f; float damage = 0.057f;
for (int x = 0; x < max; ++x) { 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)) if (!world->ChunkAt(m_currentpos))
return true; return true;
else if (world->BlockAt(m_currentpos) != BTYPE_AIR) { 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); world->ChangeBlockAtPosition(BTYPE_AIR, m_currentpos);
return true; return true;
} }

View File

@ -5,7 +5,7 @@
#include "define.h" #include "define.h"
#include "vector3.h" #include "vector3.h"
#include "player.h" #include "player.h"
#include "netprotocol.h"
class World; class World;
class Player; class Player;
@ -16,7 +16,7 @@ public:
Bullet(Vector3f pos, Vector3f dir, uint64_t tid); Bullet(Vector3f pos, Vector3f dir, uint64_t tid);
~Bullet(); ~Bullet();
bool Update(World* world, float elapsedtime, int perframe, std::unordered_map<uint64_t, Player*> m_mapPlayer); bool Update(World* world, float elapsedtime, int perframe, std::unordered_map<uint64_t, Player*> m_mapPlayer, netprot::ChunkMod** chunkmod);
void Transpose(int& x, int& z); void Transpose(int& x, int& z);
Vector3f getPos() const; Vector3f getPos() const;
Vector3f getVel() const; Vector3f getVel() const;

View File

@ -425,8 +425,9 @@ void netprot::Serialize(ChunkMod* chmod, char* buf[], uint32_t* buflen) {
memcpy(*buf + 1, vec8, sizeof(uint32_t) * 3); 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 + 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) { 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->pos, vec, sizeof(uint32_t) * 3);
memcpy(&chmod->b_type, &buf[1 + sizeof(uint8_t) * 12], sizeof(BlockType)); 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; return true;
} }

View File

@ -132,7 +132,7 @@ namespace netprot {
struct ChunkMod { struct ChunkMod {
Vector3f pos; Vector3f pos;
BlockType b_type; BlockType b_type, old_b_type;
}; };
struct ErrorLog { // srv -> cli TCP event struct ErrorLog { // srv -> cli TCP event

View File

@ -169,31 +169,26 @@ void World::Update(Bullet* bullets[MAX_BULLETS], const Vector3f& player_pos, Blo
UpdateWorld(player_pos, blockinfo); UpdateWorld(player_pos, blockinfo);
//TransposeWorld(player_pos, bullets); //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 currentPos = player_pos;
Vector3f currentBlock = currentPos; Vector3f currentBlock = currentPos;
Vector3f ray = player_dir; Vector3f ray = player_dir;
BlockType oldbtype;
netprot::ChunkMod* cmod = nullptr;
bool found = false; bool found = false;
if (block) return; if (block) return cmod;
while ((currentPos - currentBlock).Length() <= MAX_SELECTION_DISTANCE && !found) { while ((currentPos - currentBlock).Length() <= MAX_SELECTION_DISTANCE && !found) {
currentBlock += ray / 10.f; currentBlock += ray / 10.f;
BlockType bt = BlockAt(currentBlock); BlockType bt = BlockAt(currentBlock);
if (bt != BTYPE_AIR) if (bt != BTYPE_AIR) {
found = true; found = true;
oldbtype = bt;
}
} }
if (found) if (found)
@ -219,21 +214,30 @@ void World::ChangeBlockAtCursor(BlockType blockType, const Vector3f& player_pos,
(By == PyA || (By == PyA ||
By == PyB || By == PyB ||
By == PyC) && By == PyC) &&
Bz == Pz)) Bz == Pz)) {
found = true; found = true;
oldbtype = bt;
}
} }
} }
} }
if (found && (int)currentBlock.y < CHUNK_SIZE_Y) { 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 bx = (int)currentBlock.x % CHUNK_SIZE_X;
int by = (int)currentBlock.y % CHUNK_SIZE_Y; int by = (int)currentBlock.y % CHUNK_SIZE_Y;
int bz = (int)currentBlock.z % CHUNK_SIZE_Z; int bz = (int)currentBlock.z % CHUNK_SIZE_Z;
ChunkAt(currentBlock)->SetBlock(bx, by, bz, blockType, this); ChunkAt(currentBlock)->SetBlock(bx, by, bz, blockType, this);
ChunkAt(currentBlock)->MakeModified(); ChunkAt(currentBlock)->MakeModified();
block = true; block = true;
} }
return cmod;
} }
void World::ChangeBlockAtPosition(BlockType blockType, Vector3f pos) { void World::ChangeBlockAtPosition(BlockType blockType, Vector3f pos) {

View File

@ -11,6 +11,7 @@
#include "array2d.h" #include "array2d.h"
#include "bullet.h" #include "bullet.h"
#include "chunk.h" #include "chunk.h"
#include "netprotocol.h"
class Chunk; class Chunk;
class Bullet; class Bullet;
@ -37,7 +38,7 @@ public:
void GetScope(unsigned int& x, unsigned int& y); 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 ChangeBlockAtPosition(BlockType blockType, Vector3f pos);
void CleanUpWorld(int& deleteframes, bool clear); void CleanUpWorld(int& deleteframes, bool clear);
int GettbDeleted() const; int GettbDeleted() const;

View File

@ -122,6 +122,13 @@ void Connection::Run(World* world) {
last = m_input_vector.at(m_last_in); last = m_input_vector.at(m_last_in);
el = (double)(in.timestamp - last.timestamp) / 1000.; 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()->SetDirection(in.direction);
player.get()->ApplyPhysics(player.get()->GetInput(in.keys.forward, player.get()->ApplyPhysics(player.get()->GetInput(in.keys.forward,
in.keys.backward, in.keys.backward,
@ -129,6 +136,13 @@ void Connection::Run(World* world) {
in.keys.right, in.keys.right,
in.keys.jump, false, el), world, el); 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.position = player.get()->GetPositionAbs();
out.direction = in.direction; out.direction = in.direction;
out.timestamp = in.timestamp; out.timestamp = in.timestamp;

View File

@ -42,8 +42,8 @@ public:
bool m_nsync = true; bool m_nsync = true;
std::vector<Bullet> m_bullet_vector; std::vector<Bullet> Bullets;
std::vector<ChunkMod> m_diff_vector; std::vector<ChunkMod> ChunkDiffs;
private: private:
std::unordered_map<Timestamp, Input> m_input_manifest; std::unordered_map<Timestamp, Input> m_input_manifest;
@ -52,6 +52,7 @@ private:
std::deque<Output> m_output_vector; std::deque<Output> m_output_vector;
std::unordered_map<Timestamp, Chat> m_chatlog; std::unordered_map<Timestamp, Chat> m_chatlog;
float m_shoot_acc = 0;
SOCKET m_sock; SOCKET m_sock;
sockaddr_in m_addr; sockaddr_in m_addr;

View File

@ -1193,10 +1193,11 @@ void Engine::Render(float elapsedTime) {
if (bloc == BTYPE_LAST + 1) bloc = BTYPE_AIR + 1; if (bloc == BTYPE_LAST + 1) bloc = BTYPE_AIR + 1;
else if (bloc == BTYPE_AIR) bloc = BTYPE_LAST; // La selection de BTYPE_LAST <20>quipe l'arme. else if (bloc == BTYPE_AIR) bloc = BTYPE_LAST; // La selection de BTYPE_LAST <20>quipe l'arme.
m_mouseWU = m_mouseWD = m_key1 = m_key2 = false; m_mouseWU = m_mouseWD = m_key1 = m_key2 = false;
netprot::ChunkMod* cmod = nullptr;
if (m_mouseL) { if (m_mouseL) {
if (bloc != BTYPE_LAST) 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) { 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"). 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]) { if (!m_bullets[x]) {
@ -1218,12 +1219,12 @@ void Engine::Render(float elapsedTime) {
} }
} }
else if (m_mouseR) 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. for (int x = 0; x < MAX_BULLETS; ++x) { // Array de bullets en jeu.
if (m_bullets[x]) { if (m_bullets[x]) {
for (int b = 0; b < BULLET_UPDATES_PER_FRAME; ++b) { 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(); m_bullets[x]->~Bullet();
if (m_whoosh[x]) if (m_whoosh[x])
m_whoosh[x]->drop(); m_whoosh[x]->drop();
@ -1282,6 +1283,9 @@ void Engine::Render(float elapsedTime) {
static std::vector<char*> lsPck; static std::vector<char*> lsPck;
static int sync_acc = 0; static int sync_acc = 0;
if (cmod)
m_chunkmod_manifest.emplace_back(cmod);
if (last == 0) if (last == 0)
last = tstamp; last = tstamp;

View File

@ -101,10 +101,12 @@ private:
Player m_player = Player(Vector3f(.5f, CHUNK_SIZE_Y + 1.8f, .5f)); Player m_player = Player(Vector3f(.5f, CHUNK_SIZE_Y + 1.8f, .5f));
Bullet* m_bullets[MAX_BULLETS]; Bullet* m_bullets[MAX_BULLETS];
std::vector<netprot::ChunkMod*> m_chunkmod_manifest;
std::unordered_map<uint64_t, Player*> m_players; std::unordered_map<uint64_t, Player*> m_players;
netprot::Buffer m_buf, m_bufout; netprot::Buffer m_buf, m_bufout;
std::chrono::high_resolution_clock::time_point m_startTime; std::chrono::high_resolution_clock::time_point m_startTime;
netprot::ChunkMod* m_chunkmod = nullptr;
//Menu //Menu
enum class GameState: uint8_t { MAIN_MENU, OPTIONS, QUIT, NEWG, PLAY, PAUSE }; enum class GameState: uint8_t { MAIN_MENU, OPTIONS, QUIT, NEWG, PLAY, PAUSE };