Compare commits

...

11 Commits

Author SHA1 Message Date
MarcEricMartel
63d70be488 Make player spawn point random working? 2023-12-08 05:07:05 -05:00
MarcEricMartel
3da68297e4 Cleanup + client. 2023-12-08 04:58:27 -05:00
MarcEricMartel
34976ef7ec /* delete[] mess */ Well there's your mem leak. 2023-12-08 04:55:00 -05:00
MarcEricMartel
7d46536acc Can now clean bullets off the forest. 2023-12-08 04:31:10 -05:00
MarcEricMartel
7e98eeb852 whyyyyyyyyy 2023-12-07 15:47:31 -05:00
MarcEricMartel
db95c6ef10 Merge branch 'SQC-15_online' of https://github.com/CegepSTH/SQCSim2023 into SQC-15_online 2023-12-07 15:14:24 -05:00
MarcEricMartel
486c823da1 OH 2023-12-07 15:14:16 -05:00
Claudel-D-Roy
fcd1c869b9 Enlever les commentaires 2023-12-07 15:13:45 -05:00
MarcEricMartel
8bfee4a9ff :Q 2023-12-07 14:24:09 -05:00
Claudel-D-Roy
58c97587bf Merge branch 'SQC-15_online' of https://github.com/CegepSTH/SQCSim2023 into SQC-15_online 2023-12-07 14:04:38 -05:00
MarcEricMartel
ab87da7d88 Ehm. 2023-12-07 14:03:58 -05:00
8 changed files with 95 additions and 112 deletions

View File

@ -16,11 +16,9 @@ bool Bullet::Update(World* world, float elapsedtime, int perframe, std::unordere
for (auto& [key, player] : mapPlayer) {
bool hit = false;
if ((m_currentpos - player->GetPosition()).Length() < .6f) {
std::cout << "hit" << std::endl;
hit = true;
}
if ((m_currentpos - player->GetPOV()).Length() < .2f) {
std::cout << "headshot" << std::endl;
damage *= 2; // HEADSHOT!
hit = true;
}

View File

@ -29,13 +29,13 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
Keys keys = in->keys;
uint8_t keys8 = // Reste un bit.
(keys.forward? 0b10000000: 0) |
(keys.backward? 0b01000000: 0) |
(keys.left? 0b00100000: 0) |
(keys.right? 0b00010000: 0) |
(keys.jump? 0b00001000: 0) |
(keys.shoot? 0b00000100: 0) |
(keys.block? 0b00000010: 0);
(keys.forward ? 0b10000000 : 0) |
(keys.backward ? 0b01000000 : 0) |
(keys.left ? 0b00100000 : 0) |
(keys.right ? 0b00010000 : 0) |
(keys.jump ? 0b00001000 : 0) |
(keys.shoot ? 0b00000100 : 0) |
(keys.block ? 0b00000010 : 0);
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;
uint8_t states8 =
(states.jumping? 0b10000000: 0) |
(states.shooting? 0b01000000: 0) |
(states.hit? 0b00100000: 0) |
(states.powerup? 0b00010000: 0) |
(states.dead? 0b00001000: 0) |
(states.still? 0b00000100: 0) |
(states.jumpshot? 0b00000010: 0) |
(states.running? 0b00000001: 0);
(states.jumping ? 0b10000000 : 0) |
(states.shooting ? 0b01000000 : 0) |
(states.hit ? 0b00100000 : 0) |
(states.powerup ? 0b00010000 : 0) |
(states.dead ? 0b00001000 : 0) |
(states.still ? 0b00000100 : 0) |
(states.jumpshot ? 0b00000010 : 0) |
(states.running ? 0b00000001 : 0);
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));
uint8_t hp8[4] = {
uint8_t hp8[4] = {
(uint8_t)((hp >> 24) & 0xFF),
(uint8_t)((hp >> 16) & 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))
return false;
@ -538,7 +538,7 @@ bool netprot::Deserialize(Input* in, char* buf, uint32_t *buflen) {
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
memcpy(diff, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t));
in->sid =
(uint64_t)diff[0] << 56 |
@ -583,7 +583,7 @@ bool netprot::Deserialize(Input* in, char* buf, uint32_t *buflen) {
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))
return false;
@ -662,7 +662,7 @@ bool netprot::Deserialize(Output* out, char* buf, uint32_t *buflen) {
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))
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));
uint32_t hp = (uint32_t)hp8[0] << 24 |
(uint32_t)hp8[1] << 16 |
(uint32_t)hp8[2] << 8 |
(uint32_t)hp8[3];
(uint32_t)hp8[1] << 16 |
(uint32_t)hp8[2] << 8 |
(uint32_t)hp8[3];
memcpy(&sync->hp, &hp, sizeof(float));
@ -736,7 +736,7 @@ bool netprot::Deserialize(Sync* sync, char* buf, uint32_t *buflen) {
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))
return false;
@ -764,7 +764,7 @@ bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t *buflen) {
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))
return false;
@ -803,7 +803,7 @@ bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t *buflen) {
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))
return false;
@ -842,7 +842,7 @@ bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t *buflen) {
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))
return false;
@ -885,7 +885,7 @@ bool netprot::Deserialize(GameInfo* ginfo, char* buf, uint32_t *buflen) {
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))
return false;
@ -935,7 +935,7 @@ bool netprot::Deserialize(Chat* chat, char* buf, uint32_t *buflen) {
strcpy(chat->mess, 140, ciboire);
//*buflen = messsize + sizeof(uint64_t) * 3 + 1;
return true;
}
@ -1037,7 +1037,7 @@ bool netprot::Deserialize(BulletAdd* bull, char* buf, uint32_t* buflen) {
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))
return false;
@ -1064,7 +1064,7 @@ netprot::PACKET_TYPE netprot::getType(char* buf, const uint32_t buflen) {
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 };
Input* in = nullptr;
Output* out = nullptr;
@ -1167,13 +1167,12 @@ netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) {
return pck;
}
std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf) {
std::vector<char*> lsPck;
int len = buf->tmp? buf->tmp - buf->ptr: 0,
void netprot::recvPacks(SOCKET sock, Buffer* buf, std::vector<char*>* lsPck) {
int len = buf->tmp ? buf->tmp - buf->ptr : 0,
end = 0;
char * cursor = buf->tmp ? buf->tmp: nullptr ,
* next = buf->tmp ? buf->tmp + 1: buf->ptr,
* last = buf->tmp ? buf->tmp: buf->ptr;
char* cursor = buf->tmp ? buf->tmp : nullptr,
* next = buf->tmp ? buf->tmp + 1 : buf->ptr,
* last = buf->tmp ? buf->tmp : buf->ptr;
bool ended = true;
struct pollfd fds[1];
@ -1184,15 +1183,15 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf)
if (!poll(fds, 1, 0)) {
if (ended)
buf->tmp = nullptr;
return lsPck;
return;
}
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 (ended)
buf->tmp = nullptr;
return lsPck;
}
return;
}
len += bytes;
end = len;
@ -1215,24 +1214,14 @@ std::vector<char*> netprot::recvPacks(SOCKET sock, Buffer* buf, Buffer* outbuf)
cmp = memcmp(cursor, Footer, sizeof(uint32_t));
if (cmp == 0) {
if (!outbuf) {
lsPck.push_back(last);
cursor += sizeof(uint32_t);
last = cursor;
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;
}
lsPck->push_back(last);
cursor += sizeof(uint32_t);
last = cursor;
next = cursor + 1;
}
}
else {
if (!outbuf)
buf->tmp = last;
buf->tmp = last;
cursor = &buf->ptr[len];
next = cursor + 1;
break;
@ -1241,8 +1230,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*> lsPck;
void netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from, std::vector<char*>* lsPck) {
int len = buf->tmp ? buf->tmp - buf->ptr : 0,
end = 0;
char* cursor = buf->tmp ? buf->tmp : nullptr,
@ -1260,14 +1248,14 @@ std::vector<char*> netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in
if (!poll(fds, 1, 0)) {
if (ended)
buf->tmp = nullptr;
return lsPck;
return;
}
int bytes = recvfrom(sock, &buf->ptr[len], buf->len - len, 0, (sockaddr*)&sockad, &socklen);
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)
buf->tmp = nullptr;
return lsPck;
return;
}
len += bytes;
end = len;
@ -1291,24 +1279,14 @@ std::vector<char*> netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in
cmp = memcmp(cursor, Footer, sizeof(uint32_t));
if (cmp == 0) {
if (!outbuf) {
lsPck.push_back(last);
cursor += sizeof(uint32_t);
last = cursor;
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;
}
lsPck->push_back(last);
cursor += sizeof(uint32_t);
last = cursor;
next = cursor + 1;
}
}
else {
if (!outbuf)
buf->tmp = last;
buf->tmp = last;
cursor = &buf->ptr[len];
next = cursor + 1;
break;

View File

@ -129,7 +129,7 @@ namespace netprot {
Chat() {}
Chat(Chat* cha) : src_id(cha->src_id), dest_id(cha->dest_id), dest_team_id(cha->dest_team_id) {
strcpy(mess, 140, cha->mess); }
~Chat() {/* delete[] mess; */}
~Chat() { delete[] mess; }
};
struct ChunkMod {
@ -193,8 +193,8 @@ namespace netprot {
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);
std::vector<char*> recvPacks(SOCKET sock, Buffer* buf, Buffer* oufbuf = nullptr);
std::vector<char*> recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from, Buffer* oufbuf = nullptr);
void recvPacks(SOCKET sock, Buffer* buf, std::vector<char*>* lsPck);
void recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from, std::vector<char*>* lsPck);
/* Templates */

View File

@ -51,7 +51,7 @@ void Connection::getPacks(SOCKET sock) {
std::vector<char*> lsPck;
Input in;
Sync sync;
lsPck = recvPacksFrom(sock, &m_buf, m_addr);
recvPacksFrom(sock, &m_buf, m_addr, &lsPck);
for (auto& pck : lsPck) {
uint32_t bsize = m_buf.len - (pck - m_buf.ptr);
switch (netprot::getType(pck, 1)) {

View File

@ -163,7 +163,6 @@ int Server::Ready() {
play.tid = log->tid;
sendPackTo<GameInfo>(m_sock_udp, &m_game, &m_buf, &sockad);
std::cout << m_game.seed << std::endl;
Connection* conn = new Connection(sock, sockad, *log, play);
m_conns[log->sid] = conn;
@ -201,8 +200,12 @@ void Server::Run() {
m_world->BuildWorld();
for (auto& [key, conn] : m_conns) { // Creation des instances de joueurs et premier sync.
int x = 0,// (rand() % (CHUNK_SIZE_X * WORLD_SIZE_X - 1)),// -(CHUNK_SIZE_X * WORLD_SIZE_X / 2),
y = 0;// (rand() % (CHUNK_SIZE_Y * WORLD_SIZE_Y - 1));// -(CHUNK_SIZE_Y * WORLD_SIZE_Y / 2);
if (!conn) {
m_conns.erase(key);
continue;
}
int x = (rand() % (CHUNK_SIZE_X * WORLD_SIZE_X - 1) - (CHUNK_SIZE_X * WORLD_SIZE_X / 2)) / 4,
y = (rand() % (CHUNK_SIZE_Y * WORLD_SIZE_Y - 1) - (CHUNK_SIZE_Y * WORLD_SIZE_Y / 2)) / 4;
conn->player = new Player(Vector3f(x + .5f, CHUNK_SIZE_Y + 1.8f, y + .5f));
conn->player->m_username = conn->GetName();
m_players[key] = conn->player;
@ -221,12 +224,14 @@ void Server::Run() {
Timestamp last = 0;
std::vector<Chat*> chatlog;
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<char*> lsPck;
Chat* startchat = new Chat();
startchat->src_id = 0;
char startmess[] = "How would -YOU- like to die today, motherfuckers?";
char startmess[] = "How would -YOU- like to die today, motherf-words?";
strcpy(startchat->mess, 140, startmess);
@ -248,29 +253,27 @@ void Server::Run() {
}
last = tstamp;
for (auto& [key, conn] : m_conns) {
for (auto& [key, conn] : m_conns) {
/* In */
std::vector<char*> lsPck;
Input in; Chat chat; Sync sync;
Input in; Sync sync;
lsPck = recvPacks(m_sock_udp, &m_buf);
recvPacks(m_sock_udp, &m_buf, &lsPck);
for (auto& pck : lsPck) {
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_conns[in.sid]->AddInput(in);
if (Deserialize(&in, pck, &bsize)) {
if (m_conns.count(in.sid))
m_conns[in.sid]->AddInput(in);
}
break;
case SYNC:
if (Deserialize(&sync, pck, &bsize)) {}
break;
case CHAT:
//if (Deserialize(&chat, pck, &bsize))
//chatlog.push_back(chat);
break;
default: break;
}
}
@ -327,17 +330,21 @@ void Server::Run() {
sendPackTo<BulletAdd>(m_sock_udp, bull, &m_buf, conn->getAddr());
delete bull;
}
for (auto& bull : bullets) {
netbull.clear();
for (auto bull = bullets.begin(); bull != bullets.end(); ++bull) {
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)
chunkdiffs.emplace_back(std::move(cmod));
outbox_bullets.emplace_back(std::move(bull));
chunkdiffs.emplace_back(cmod);
bullit.push_back(bull);
delete bullet;
}
}
netbull.clear();
for (auto& bull: bullit)
bullets.erase(bull);
bullit.clear();
for (auto& chat : chatlog) {
Log(chat->mess, false, false);
@ -353,18 +360,19 @@ void Server::Run() {
delete chmo;
}
chunkdiffs.clear();
for (auto& bull : outbox_bullets)
delete bull;
outbox_bullets.clear();
}
Chat end;
end.src_id = 0;
char endmess[] = "Game over, man. Game over.";
strcpy(end.mess, 140, endmess);
for (auto& [key, conn] : m_conns) {
std::string str = conn->player->GetUsername();
Log(str.append(" ").append(std::to_string(conn->player->GetHP())), false, false);
}
for (auto& [key, conn] : m_conns)
sendPackTo<Chat>(m_sock_udp, &end, &m_buf, conn->getAddr());

View File

@ -73,7 +73,7 @@ int Connector::Connect(const char* srv_addr, std::string name) {
int errors = 0;
std::vector<char*> lsPck;
while (!ready) {
lsPck = netprot::recvPacks(m_sock_udp, &bf);
netprot::recvPacks(m_sock_udp, &bf, &lsPck);
for (auto& pck : lsPck) {
uint32_t bsize = bf.len - (pck - bf.ptr);
@ -94,6 +94,7 @@ int Connector::Connect(const char* srv_addr, std::string name) {
++errors;
else {
m_players[pl->id] = pl;
std::cout << "A challenger appears! " << pl->name << std::endl;
}
break;
case TEAMINF:

View File

@ -1387,7 +1387,7 @@ void Engine::Render(float elapsedTime) {
sendPackTo<Input>(m_conn.m_sock_udp, &input, &m_bufout, &m_conn.m_srvsockaddr);
lsPck = recvPacks(m_conn.m_sock_udp, &m_buf);
recvPacks(m_conn.m_sock_udp, &m_buf, &lsPck);
char* prevptr = nullptr;
for (auto& pck : lsPck) { // We could make a few threads out of this.
Sync sync; Output out; ChunkMod cmod; BulletAdd bull; Chat chat;

View File

@ -32,8 +32,6 @@ void RemotePlayer::Feed(const netprot::Output out) {
m_position = Vector3f(out.position);
m_direction = Vector3f(out.direction);
std::cout << "Dead: " << out.states.dead << " Hit:" << out.states.hit << " Jump:" << out.states.jumping << " JumpShot: " << out.states.jumpshot << " Running: " << out.states.running << " Shooting: " << out.states.shooting << " Still: " << out.states.still << std::endl;
//std::cout << "powerUp " << out.states.powerup << std::endl;
current.states = out.states;
//current.position = out.position;