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

@ -1167,8 +1167,7 @@ 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;
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,
@ -1184,14 +1183,14 @@ 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,23 +1214,13 @@ 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]);
lsPck->push_back(last);
cursor += sizeof(uint32_t);
last = cursor;
next = cursor + 1;
}
}
}
else {
if (!outbuf)
buf->tmp = last;
cursor = &buf->ptr[len];
next = cursor + 1;
@ -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,23 +1279,13 @@ 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]);
lsPck->push_back(last);
cursor += sizeof(uint32_t);
last = cursor;
next = cursor + 1;
}
}
}
else {
if (!outbuf)
buf->tmp = last;
cursor = &buf->ptr[len];
next = cursor + 1;

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) {
/* 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))
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,18 +330,22 @@ void Server::Run() {
sendPackTo<BulletAdd>(m_sock_udp, bull, &m_buf, conn->getAddr());
delete bull;
}
for (auto& bull : bullets) {
ChunkMod* cmod = nullptr;
if (bull->Update(m_world, (1. / 60.), 20, m_players, &cmod)) {
if (cmod)
chunkdiffs.emplace_back(std::move(cmod));
outbox_bullets.emplace_back(std::move(bull));
}
}
netbull.clear();
for (auto bull = bullets.begin(); bull != bullets.end(); ++bull) {
ChunkMod* cmod = nullptr;
Bullet* bullet = *bull;
if (bullet->Update(m_world, (1. / 60.), 20, m_players, &cmod)) {
if (cmod)
chunkdiffs.emplace_back(cmod);
bullit.push_back(bull);
delete bullet;
}
}
for (auto& bull: bullit)
bullets.erase(bull);
bullit.clear();
for (auto& chat : chatlog) {
Log(chat->mess, false, false);
for (auto& [key, conn] : m_conns)
@ -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;