2023-09-24 08:45:40 -04:00
|
|
|
#include "connection.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-12-06 12:17:47 -05:00
|
|
|
Connection::Connection(SOCKET sock,
|
|
|
|
sockaddr_in sockaddr,
|
2023-12-09 12:02:04 -05:00
|
|
|
LoginInfo *log,
|
|
|
|
PlayerInfo *play) :
|
2023-12-06 12:17:47 -05:00
|
|
|
m_sock(sock),
|
|
|
|
m_addr(sockaddr),
|
2023-12-09 12:02:04 -05:00
|
|
|
m_loginfo(*log),
|
|
|
|
m_playinfo(*play) {
|
2023-09-24 08:45:40 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-09 12:02:04 -05:00
|
|
|
Connection::~Connection() {
|
|
|
|
delete player;
|
|
|
|
closesocket(m_sock); }
|
2023-09-24 08:45:40 -04:00
|
|
|
|
2023-12-06 12:17:47 -05:00
|
|
|
uint64_t Connection::GetHash(bool self) const { return self ? m_loginfo.sid : m_playinfo.id; }
|
2023-09-24 08:45:40 -04:00
|
|
|
|
2023-09-27 17:34:25 -04:00
|
|
|
uint64_t Connection::GetTeamHash() const { return m_loginfo.tid; }
|
2023-09-24 08:45:40 -04:00
|
|
|
|
2023-09-27 17:34:25 -04:00
|
|
|
std::string Connection::GetName() const { return m_loginfo.name; }
|
2023-09-24 08:45:40 -04:00
|
|
|
|
2023-12-05 13:44:54 -05:00
|
|
|
void Connection::AddInput(Input in) { m_input_manifest.insert({ in.timestamp, in }); m_input_vector.push_back(in); }
|
2023-09-24 11:07:03 -04:00
|
|
|
|
2023-10-25 12:16:14 -04:00
|
|
|
Output* Connection::getOutput(Timestamp time) {
|
2023-09-24 11:07:03 -04:00
|
|
|
auto out = m_output_manifest.find(time);
|
|
|
|
if (out != m_output_manifest.end())
|
|
|
|
return &out->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-10-25 12:16:14 -04:00
|
|
|
Sync Connection::getSync(Timestamp time) {
|
|
|
|
Sync sync;
|
2023-09-24 11:07:03 -04:00
|
|
|
auto out = m_output_manifest.find(time);
|
|
|
|
if (out != m_output_manifest.end()) {
|
|
|
|
sync.timestamp = out->second.timestamp;
|
|
|
|
sync.position = out->second.position;
|
2023-09-27 17:34:25 -04:00
|
|
|
sync.sid = m_loginfo.sid;
|
2023-09-24 11:07:03 -04:00
|
|
|
}
|
|
|
|
return sync;
|
|
|
|
}
|
|
|
|
|
2023-10-25 12:16:14 -04:00
|
|
|
SOCKET Connection::getSock() const { return m_sock; }
|
|
|
|
|
|
|
|
PlayerInfo* Connection::getInfo() const { return (PlayerInfo*)&m_playinfo; }
|
|
|
|
|
2023-10-26 17:57:42 -04:00
|
|
|
sockaddr_in* Connection::getAddr() const { return (sockaddr_in*)&m_addr; }
|
|
|
|
|
2023-11-06 17:59:57 -05:00
|
|
|
void Connection::getPacks(SOCKET sock) {
|
2023-11-06 16:43:52 -05:00
|
|
|
std::vector<char*> lsPck;
|
|
|
|
Input in;
|
2023-12-05 13:44:54 -05:00
|
|
|
Sync sync;
|
2023-12-08 04:55:00 -05:00
|
|
|
recvPacksFrom(sock, &m_buf, m_addr, &lsPck);
|
2023-12-06 12:17:47 -05:00
|
|
|
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:
|
2023-12-13 16:30:20 -05:00
|
|
|
if (player->AmIDead())
|
|
|
|
break;
|
|
|
|
else if (Deserialize(&in, pck, &bsize)) {
|
2023-12-06 12:17:47 -05:00
|
|
|
m_input_manifest[in.timestamp] = in;
|
|
|
|
m_input_vector.push_back(in);
|
2023-11-06 16:43:52 -05:00
|
|
|
}
|
2023-12-06 12:17:47 -05:00
|
|
|
break;
|
|
|
|
case SYNC:
|
|
|
|
if (Deserialize(&sync, pck, &bsize))
|
|
|
|
m_nsync = true;
|
|
|
|
break;
|
|
|
|
default: break;
|
2023-11-06 16:43:52 -05:00
|
|
|
}
|
2023-12-06 12:17:47 -05:00
|
|
|
}
|
|
|
|
lsPck.clear();
|
2023-11-06 16:43:52 -05:00
|
|
|
}
|
|
|
|
|
2023-12-05 13:44:54 -05:00
|
|
|
void Connection::sendPacks(SOCKET sock, std::unordered_map<uint64_t, Connection*> conns, const uint32_t timer) {
|
|
|
|
static int outs = 0;
|
|
|
|
static Timestamp last = 0;
|
2023-12-13 16:29:13 -05:00
|
|
|
static uint32_t lasttimer = timer;
|
|
|
|
|
|
|
|
if (m_output_vector.empty() && player->AmIDead()) {
|
|
|
|
if (timer != lasttimer) {
|
|
|
|
lasttimer = timer;
|
|
|
|
Sync sync;
|
|
|
|
sync.timestamp = sync.sid = m_loginfo.sid;
|
|
|
|
sync.hp = 0;
|
|
|
|
sync.ammo = -1;
|
|
|
|
sync.timer = timer;
|
|
|
|
sendPackTo<Sync>(sock, &sync, &m_bufout, &m_addr);
|
|
|
|
}
|
|
|
|
}
|
2023-12-13 15:04:08 -05:00
|
|
|
|
2023-12-05 13:44:54 -05:00
|
|
|
while (!m_output_vector.empty()) {
|
|
|
|
Output out = m_output_vector.front();
|
2023-12-13 15:04:08 -05:00
|
|
|
|
2023-11-06 17:59:57 -05:00
|
|
|
for (auto& [key, conn] : conns) {
|
2023-12-05 13:44:54 -05:00
|
|
|
if (m_playinfo.id == conn->GetHash(false))
|
2023-11-06 17:59:57 -05:00
|
|
|
continue;
|
|
|
|
sendPackTo<Output>(sock, &out, &m_bufout, conn->getAddr());
|
|
|
|
}
|
2023-11-27 16:12:12 -05:00
|
|
|
++outs;
|
|
|
|
|
|
|
|
[[unlikely]] if (last == 0) // !
|
|
|
|
last = out.timestamp;
|
|
|
|
|
|
|
|
outs += out.timestamp + last;
|
|
|
|
|
2023-12-13 16:10:23 -05:00
|
|
|
static bool syncdead = false;
|
|
|
|
bool dead = player->AmIDead();
|
|
|
|
|
|
|
|
if (outs >= SYNC_ACC || (!syncdead && dead)) {
|
2023-11-27 16:12:12 -05:00
|
|
|
Sync sync;
|
2023-12-13 16:10:23 -05:00
|
|
|
outs -= SYNC_ACC;
|
2023-11-27 16:12:12 -05:00
|
|
|
sync.timestamp = out.timestamp;
|
|
|
|
sync.position = out.position;
|
|
|
|
sync.sid = m_loginfo.sid;
|
|
|
|
sync.timer = timer;
|
|
|
|
sync.timestamp = out.timestamp;
|
|
|
|
sync.ammo = -1;
|
2023-12-13 16:10:23 -05:00
|
|
|
sync.hp = player->GetHP();
|
|
|
|
if (dead) {
|
|
|
|
sync.timestamp = m_loginfo.sid;
|
|
|
|
syncdead = true;
|
|
|
|
}
|
2023-11-27 16:12:12 -05:00
|
|
|
sendPackTo<Sync>(sock, &sync, &m_bufout, &m_addr);
|
|
|
|
}
|
|
|
|
|
2023-11-24 15:56:58 -05:00
|
|
|
m_output_vector.pop_front();
|
2023-11-06 17:59:57 -05:00
|
|
|
}
|
|
|
|
}
|
2023-11-06 16:43:52 -05:00
|
|
|
|
2023-12-18 13:29:38 -05:00
|
|
|
Timestamp Connection::Run(World* world, std::unordered_map<uint64_t, Booster*> boosters) {
|
2023-11-06 16:43:52 -05:00
|
|
|
Input in, last;
|
|
|
|
Output out;
|
2023-12-05 06:25:48 -05:00
|
|
|
Timestamp tstamp = 0;
|
2023-11-06 16:43:52 -05:00
|
|
|
float el;
|
|
|
|
|
2023-12-13 15:04:08 -05:00
|
|
|
bool dead = player->AmIDead();
|
|
|
|
|
2023-12-13 16:10:23 -05:00
|
|
|
if (m_input_manifest.size() < 2 && !dead)
|
2023-12-05 06:25:48 -05:00
|
|
|
return tstamp;
|
2023-11-06 16:43:52 -05:00
|
|
|
|
2023-12-13 15:04:08 -05:00
|
|
|
while (m_last_in < m_input_vector.size() - 1 || dead) {
|
|
|
|
if (!dead) {
|
|
|
|
in = m_input_vector.at(m_last_in + 1);
|
|
|
|
last = m_input_vector.at(m_last_in);
|
|
|
|
if (in.timestamp <= m_tstamp) {
|
|
|
|
++m_last_in;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
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->SetDirection(in.direction);
|
2023-12-13 14:27:11 -05:00
|
|
|
}
|
2023-12-13 15:04:08 -05:00
|
|
|
else {
|
|
|
|
el = 1. / 60.;
|
|
|
|
in = Input();
|
|
|
|
|
2023-12-05 13:44:54 -05:00
|
|
|
}
|
2023-12-13 15:04:08 -05:00
|
|
|
|
2023-12-06 12:17:47 -05:00
|
|
|
player->ApplyPhysics(player->GetInput(in.keys.forward,
|
|
|
|
in.keys.backward,
|
|
|
|
in.keys.left,
|
|
|
|
in.keys.right,
|
2023-12-18 13:29:38 -05:00
|
|
|
in.keys.jump, false, el),
|
2023-12-18 14:54:14 -05:00
|
|
|
world, el);
|
|
|
|
|
|
|
|
player->TakeBooster(boosters, el);
|
2023-12-06 12:17:47 -05:00
|
|
|
|
2023-12-06 14:31:05 -05:00
|
|
|
if (player->GetPosition().y < -20.) {
|
|
|
|
player->InflictDamage(9000.);
|
2023-12-06 11:16:39 -05:00
|
|
|
player->Killer = GetHash(true);
|
|
|
|
}
|
|
|
|
|
2023-12-09 12:02:04 -05:00
|
|
|
out.states.jumping = player->GetIsAirborne();
|
2023-12-13 15:04:08 -05:00
|
|
|
|
|
|
|
Vector3f horSpeed = player->GetVelocity();
|
|
|
|
horSpeed.y = 0;
|
|
|
|
out.states.running = horSpeed.Length() > .2f;
|
2023-12-07 12:48:48 -05:00
|
|
|
out.states.still = !out.states.running && !out.states.jumping;
|
2023-12-06 15:47:14 -05:00
|
|
|
out.states.hit = player->m_hit;
|
|
|
|
player->m_hit = false;
|
2023-12-06 12:17:47 -05:00
|
|
|
|
2023-12-06 11:16:39 -05:00
|
|
|
if (player->AmIDead()) {
|
|
|
|
in.keys.shoot = false;
|
|
|
|
in.keys.block = false;
|
|
|
|
out.states.dead = true;
|
|
|
|
}
|
2023-12-05 06:25:48 -05:00
|
|
|
|
2023-12-06 12:17:47 -05:00
|
|
|
static bool toggle = false;
|
2023-12-05 06:25:48 -05:00
|
|
|
if (in.keys.block) {
|
2023-12-06 12:17:47 -05:00
|
|
|
if (!toggle) {
|
|
|
|
toggle = true;
|
|
|
|
bool block = false;
|
|
|
|
ChunkMod* cmod = world->ChangeBlockAtCursor(BLOCK_TYPE::BTYPE_METAL,
|
|
|
|
player->GetPosition(),
|
|
|
|
player->GetDirection(),
|
|
|
|
block, true);
|
|
|
|
if (cmod)
|
2023-12-09 12:02:04 -05:00
|
|
|
ChunkDiffs.push_back(std::move(cmod));
|
2023-12-06 12:17:47 -05:00
|
|
|
}
|
2023-12-05 06:25:48 -05:00
|
|
|
}
|
2023-12-06 12:17:47 -05:00
|
|
|
else toggle = false;
|
2023-12-02 11:05:00 -05:00
|
|
|
|
2023-12-07 12:43:40 -05:00
|
|
|
out.states.shooting = in.keys.shoot;
|
|
|
|
|
|
|
|
if (out.states.jumping && out.states.shooting)
|
|
|
|
out.states.jumpshot = true;
|
|
|
|
else out.states.jumpshot = false;
|
|
|
|
|
2023-12-05 14:25:21 -05:00
|
|
|
if (in.keys.shoot && m_shoot_acc <= 0.) {
|
2023-12-16 15:27:20 -05:00
|
|
|
Bullets.emplace_back(new Bullet(player->GetPOV() + player->GetDirection(), player->GetDirection(), GetHash(true)));
|
2023-12-06 12:17:47 -05:00
|
|
|
m_shoot_acc = BULLET_TIME;
|
2023-12-05 14:25:21 -05:00
|
|
|
}
|
2023-12-06 12:17:47 -05:00
|
|
|
|
2023-12-05 06:25:48 -05:00
|
|
|
out.position = player->GetPositionAbs();
|
2023-11-06 17:59:57 -05:00
|
|
|
out.direction = in.direction;
|
|
|
|
out.timestamp = in.timestamp;
|
|
|
|
out.id = m_playinfo.id;
|
|
|
|
m_output_manifest[out.timestamp] = out;
|
2023-11-24 14:15:40 -05:00
|
|
|
m_output_vector.push_back(out);
|
2023-12-13 14:27:11 -05:00
|
|
|
m_tstamp = tstamp = out.timestamp;
|
2023-12-06 12:17:47 -05:00
|
|
|
|
2023-12-13 15:04:08 -05:00
|
|
|
if (!dead)
|
|
|
|
++m_last_in;
|
2023-12-13 15:29:12 -05:00
|
|
|
|
|
|
|
dead = false;
|
2023-11-06 17:59:57 -05:00
|
|
|
}
|
2023-12-05 06:25:48 -05:00
|
|
|
|
|
|
|
return tstamp;
|
2023-11-06 16:43:52 -05:00
|
|
|
}
|
|
|
|
|
2023-09-24 11:07:03 -04:00
|
|
|
void Connection::CleanInputManifest(Timestamp time) {
|
2023-12-06 12:17:47 -05:00
|
|
|
// auto wat = m_input_manifest.find(time);
|
2023-09-24 11:07:03 -04:00
|
|
|
|
2023-12-06 12:17:47 -05:00
|
|
|
// while (wat != m_input_manifest.begin())
|
|
|
|
// m_input_manifest.erase(wat--);
|
2023-09-24 08:45:40 -04:00
|
|
|
}
|
2023-12-05 06:25:48 -05:00
|
|
|
|
|
|
|
Timestamp Connection::GetTStamp() const { return m_tstamp; }
|