Can now clean bullets off the forest.

This commit is contained in:
MarcEricMartel 2023-12-08 04:31:10 -05:00
parent 7e98eeb852
commit 7d46536acc
3 changed files with 65 additions and 86 deletions

View File

@ -29,13 +29,13 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
Keys keys = in->keys; Keys keys = in->keys;
uint8_t keys8 = // Reste un bit. uint8_t keys8 = // Reste un bit.
(keys.forward? 0b10000000: 0) | (keys.forward ? 0b10000000 : 0) |
(keys.backward? 0b01000000: 0) | (keys.backward ? 0b01000000 : 0) |
(keys.left? 0b00100000: 0) | (keys.left ? 0b00100000 : 0) |
(keys.right? 0b00010000: 0) | (keys.right ? 0b00010000 : 0) |
(keys.jump? 0b00001000: 0) | (keys.jump ? 0b00001000 : 0) |
(keys.shoot? 0b00000100: 0) | (keys.shoot ? 0b00000100 : 0) |
(keys.block? 0b00000010: 0); (keys.block ? 0b00000010 : 0);
memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t)); memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t));
@ -90,14 +90,14 @@ void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) {
States states = out->states; States states = out->states;
uint8_t states8 = uint8_t states8 =
(states.jumping? 0b10000000: 0) | (states.jumping ? 0b10000000 : 0) |
(states.shooting? 0b01000000: 0) | (states.shooting ? 0b01000000 : 0) |
(states.hit? 0b00100000: 0) | (states.hit ? 0b00100000 : 0) |
(states.powerup? 0b00010000: 0) | (states.powerup ? 0b00010000 : 0) |
(states.dead? 0b00001000: 0) | (states.dead ? 0b00001000 : 0) |
(states.still? 0b00000100: 0) | (states.still ? 0b00000100 : 0) |
(states.jumpshot? 0b00000010: 0) | (states.jumpshot ? 0b00000010 : 0) |
(states.running? 0b00000001: 0); (states.running ? 0b00000001 : 0);
memcpy(*buf + sizeof(uint64_t) * 2 + 1, &states8, sizeof(uint8_t)); memcpy(*buf + sizeof(uint64_t) * 2 + 1, &states8, sizeof(uint8_t));
@ -204,7 +204,7 @@ void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) {
memcpy(&hp, &sync->hp, sizeof(float)); memcpy(&hp, &sync->hp, sizeof(float));
uint8_t hp8[4] = { uint8_t hp8[4] = {
(uint8_t)((hp >> 24) & 0xFF), (uint8_t)((hp >> 24) & 0xFF),
(uint8_t)((hp >> 16) & 0xFF), (uint8_t)((hp >> 16) & 0xFF),
(uint8_t)((hp >> 8) & 0xFF), (uint8_t)((hp >> 8) & 0xFF),
@ -523,7 +523,7 @@ void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) {
bool netprot::Deserialize(Input* in, char* buf, uint32_t *buflen) { bool netprot::Deserialize(Input* in, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(Input)) if (*buflen <= sizeof(Input))
return false; return false;
@ -538,7 +538,7 @@ bool netprot::Deserialize(Input* in, char* buf, uint32_t *buflen) {
(uint64_t)diff[5] << 16 | (uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 | (uint64_t)diff[6] << 8 |
(uint64_t)diff[7]; (uint64_t)diff[7];
memcpy(diff, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t)); memcpy(diff, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t));
in->sid = in->sid =
(uint64_t)diff[0] << 56 | (uint64_t)diff[0] << 56 |
@ -583,7 +583,7 @@ bool netprot::Deserialize(Input* in, char* buf, uint32_t *buflen) {
return true; return true;
} }
bool netprot::Deserialize(Output* out, char* buf, uint32_t *buflen) { bool netprot::Deserialize(Output* out, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(Output)) if (*buflen <= sizeof(Output))
return false; return false;
@ -662,7 +662,7 @@ bool netprot::Deserialize(Output* out, char* buf, uint32_t *buflen) {
return true; return true;
} }
bool netprot::Deserialize(Sync* sync, char* buf, uint32_t *buflen) { bool netprot::Deserialize(Sync* sync, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(Sync)) if (*buflen <= sizeof(Sync))
return false; return false;
@ -725,9 +725,9 @@ bool netprot::Deserialize(Sync* sync, char* buf, uint32_t *buflen) {
memcpy(&hp8, &buf[2 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t)], sizeof(uint32_t)); memcpy(&hp8, &buf[2 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t)], sizeof(uint32_t));
uint32_t hp = (uint32_t)hp8[0] << 24 | uint32_t hp = (uint32_t)hp8[0] << 24 |
(uint32_t)hp8[1] << 16 | (uint32_t)hp8[1] << 16 |
(uint32_t)hp8[2] << 8 | (uint32_t)hp8[2] << 8 |
(uint32_t)hp8[3]; (uint32_t)hp8[3];
memcpy(&sync->hp, &hp, sizeof(float)); memcpy(&sync->hp, &hp, sizeof(float));
@ -736,7 +736,7 @@ bool netprot::Deserialize(Sync* sync, char* buf, uint32_t *buflen) {
return true; return true;
} }
bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t *buflen) { bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(LoginInfo)) if (*buflen <= sizeof(LoginInfo))
return false; return false;
@ -764,7 +764,7 @@ bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t *buflen) {
return true; return true;
} }
bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t *buflen) { bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(LoginInfo)) if (*buflen <= sizeof(LoginInfo))
return false; return false;
@ -803,7 +803,7 @@ bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t *buflen) {
return true; return true;
} }
bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t *buflen) { bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(PlayerInfo)) if (*buflen <= sizeof(PlayerInfo))
return false; return false;
@ -842,7 +842,7 @@ bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t *buflen) {
return true; return true;
} }
bool netprot::Deserialize(GameInfo* ginfo, char* buf, uint32_t *buflen) { bool netprot::Deserialize(GameInfo* ginfo, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(GameInfo)) if (*buflen <= sizeof(GameInfo))
return false; return false;
@ -885,7 +885,7 @@ bool netprot::Deserialize(GameInfo* ginfo, char* buf, uint32_t *buflen) {
return true; return true;
} }
bool netprot::Deserialize(Chat* chat, char* buf, uint32_t *buflen) { bool netprot::Deserialize(Chat* chat, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(Chat)) if (*buflen <= sizeof(Chat))
return false; return false;
@ -935,7 +935,7 @@ bool netprot::Deserialize(Chat* chat, char* buf, uint32_t *buflen) {
strcpy(chat->mess, 140, ciboire); strcpy(chat->mess, 140, ciboire);
//*buflen = messsize + sizeof(uint64_t) * 3 + 1; //*buflen = messsize + sizeof(uint64_t) * 3 + 1;
return true; return true;
} }
@ -1037,7 +1037,7 @@ bool netprot::Deserialize(BulletAdd* bull, char* buf, uint32_t* buflen) {
return true; return true;
} }
bool netprot::Deserialize(ErrorLog* errlog, char* buf, uint32_t *buflen) { bool netprot::Deserialize(ErrorLog* errlog, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(ErrorLog)) if (*buflen <= sizeof(ErrorLog))
return false; return false;
@ -1064,7 +1064,7 @@ netprot::PACKET_TYPE netprot::getType(char* buf, const uint32_t buflen) {
return (netprot::PACKET_TYPE)buf[0]; return (netprot::PACKET_TYPE)buf[0];
} }
netprot::Packet netprot::getPack(char* buf, uint32_t *buflen) { netprot::Packet netprot::getPack(char* buf, uint32_t* buflen) {
Packet pck = { nullptr, PACKET_TYPE::ERR }; Packet pck = { nullptr, PACKET_TYPE::ERR };
Input* in = nullptr; Input* in = nullptr;
Output* out = nullptr; Output* out = nullptr;
@ -1167,13 +1167,13 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) {
return pck; return pck;
} }
std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf) { std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf) {
std::vector<char*> lsPck; std::vector<char*> lsPck;
int len = buf->tmp? buf->tmp - buf->ptr: 0, int len = buf->tmp ? buf->tmp - buf->ptr : 0,
end = 0; end = 0;
char * cursor = buf->tmp ? buf->tmp: nullptr , char* cursor = buf->tmp ? buf->tmp : nullptr,
* next = buf->tmp ? buf->tmp + 1: buf->ptr, * next = buf->tmp ? buf->tmp + 1 : buf->ptr,
* last = buf->tmp ? buf->tmp: buf->ptr; * last = buf->tmp ? buf->tmp : buf->ptr;
bool ended = true; bool ended = true;
struct pollfd fds[1]; struct pollfd fds[1];
@ -1186,13 +1186,13 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf)
buf->tmp = nullptr; buf->tmp = nullptr;
return lsPck; return lsPck;
} }
int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0); int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0);
if (bytes <= 0) { // si recv() retourne -1 ou 0; ça veut dire qu'il y a plus rien a lire qui n'a pas déjà été traité. if (bytes <= 0) { // si recv() retourne -1 ou 0; ça veut dire qu'il y a plus rien a lire qui n'a pas déjà été traité.
if (ended) if (ended)
buf->tmp = nullptr; buf->tmp = nullptr;
return lsPck; return lsPck;
} }
len += bytes; len += bytes;
end = len; end = len;
@ -1207,7 +1207,7 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf)
if (end < 0) if (end < 0)
break; break;
cursor = (char*)memchr(next, '\r', end); cursor = (char*)std::move(memchr(next, '\r', end));
if (cursor) { if (cursor) {
next = cursor; next = cursor;
@ -1215,25 +1215,15 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf)
cmp = memcmp(cursor, Footer, sizeof(uint32_t)); cmp = memcmp(cursor, Footer, sizeof(uint32_t));
if (cmp == 0) { if (cmp == 0) {
if (!outbuf) { lsPck.push_back(std::move(last));
lsPck.push_back(last); cursor += sizeof(uint32_t);
cursor += sizeof(uint32_t); last = cursor;
last = cursor; next = cursor + 1;
next = cursor + 1;
}
else {
memcpy(&outbuf->ptr[cursor - last], last, cursor - last);
lsPck.push_back(&outbuf->ptr[cursor - last]);
cursor += sizeof(uint32_t);
last = cursor;
next = cursor + 1;
}
} }
} }
else { else {
if (!outbuf) buf->tmp = std::move(last);
buf->tmp = last; cursor = std::move(&buf->ptr[len]);
cursor = &buf->ptr[len];
next = cursor + 1; next = cursor + 1;
break; break;
}; };
@ -1241,7 +1231,7 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf)
} }
} }
std::vector<char*> netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from, Buffer* outbuf) { std::vector<char*> netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from) {
std::vector<char*> lsPck; std::vector<char*> lsPck;
int len = buf->tmp ? buf->tmp - buf->ptr : 0, int len = buf->tmp ? buf->tmp - buf->ptr : 0,
end = 0; end = 0;
@ -1291,24 +1281,14 @@ std::vector<char*> netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in
cmp = memcmp(cursor, Footer, sizeof(uint32_t)); cmp = memcmp(cursor, Footer, sizeof(uint32_t));
if (cmp == 0) { if (cmp == 0) {
if (!outbuf) { lsPck.push_back(last);
lsPck.push_back(last); cursor += sizeof(uint32_t);
cursor += sizeof(uint32_t); last = cursor;
last = cursor; next = cursor + 1;
next = cursor + 1;
}
else {
memcpy(&outbuf->ptr[cursor - last], last, cursor - last);
lsPck.push_back(&outbuf->ptr[cursor - last]);
cursor += sizeof(uint32_t);
last = cursor;
next = cursor + 1;
}
} }
} }
else { else {
if (!outbuf) buf->tmp = last;
buf->tmp = last;
cursor = &buf->ptr[len]; cursor = &buf->ptr[len];
next = cursor + 1; next = cursor + 1;
break; break;

View File

@ -193,8 +193,8 @@ namespace netprot {
template <class T> void sendPack(SOCKET sock, T* pack, Buffer* buf); template <class T> void sendPack(SOCKET sock, T* pack, Buffer* buf);
template <class T> void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad); template <class T> void sendPackTo(SOCKET sock, T* pack, Buffer* buf, sockaddr_in* sockad);
std::vector<char*> recvPacks(SOCKET sock, Buffer* buf, Buffer* oufbuf = nullptr); std::vector<char*> recvPacks(SOCKET sock, Buffer* buf);
std::vector<char*> recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from, Buffer* oufbuf = nullptr); std::vector<char*> recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from);
/* Templates */ /* Templates */

View File

@ -224,7 +224,8 @@ void Server::Run() {
Timestamp last = 0; Timestamp last = 0;
std::vector<Chat*> chatlog; std::vector<Chat*> chatlog;
std::vector<ChunkMod*> chunkdiffs; std::vector<ChunkMod*> chunkdiffs;
std::vector<Bullet*> bullets, outbox_bullets; std::vector<Bullet*> bullets;
std::vector<std::vector<Bullet*>::iterator> bullit;
std::vector<BulletAdd*> netbull; std::vector<BulletAdd*> netbull;
Chat* startchat = new Chat(); Chat* startchat = new Chat();
@ -334,19 +335,21 @@ void Server::Run() {
delete bull; delete bull;
} }
netbull.clear(); netbull.clear();
for (auto& bull : bullets) { for (auto bull = bullets.begin(); bull != bullets.end(); ++bull) {
ChunkMod* cmod = nullptr; ChunkMod* cmod = nullptr;
if (bull->Update(m_world, (1. / 60.), 20, m_players, &cmod)) { Bullet* bullet = *bull;
if (bullet->Update(m_world, (1. / 60.), 20, m_players, &cmod)) {
if (cmod) if (cmod)
chunkdiffs.emplace_back(std::move(cmod)); chunkdiffs.emplace_back(cmod);
outbox_bullets.emplace_back(std::move(bull)); bullit.push_back(bull);
delete bullet;
} }
} }
for (auto gtfo : outbox_bullets) for (auto& bull: bullit)
delete gtfo; bullets.erase(bull);
outbox_bullets.clear(); bullit.clear();
for (auto& chat : chatlog) { for (auto& chat : chatlog) {
Log(chat->mess, false, false); Log(chat->mess, false, false);
@ -362,10 +365,6 @@ void Server::Run() {
delete chmo; delete chmo;
} }
chunkdiffs.clear(); chunkdiffs.clear();
//for (auto& bull : outbox_bullets)
// delete bull;
//outbox_bullets.clear();
} }
Chat end; Chat end;