|
|
|
|
@@ -36,40 +36,12 @@ const Model* Engine::GetStaticModel(std::string name) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Model* Engine::SpawnModel(std::string asset_name, std::string name) {
|
|
|
|
|
if (asset_name == "") {
|
|
|
|
|
Model model_to_spawn(nullptr);
|
|
|
|
|
model_to_spawn.visible = false;
|
|
|
|
|
models_[name] = std::move(model_to_spawn);
|
|
|
|
|
return &models_[name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (models_.find(name) == models_.end()) {
|
|
|
|
|
Model model_to_spawn(*GetStaticModel(asset_name));
|
|
|
|
|
models_[name] = std::move(model_to_spawn);
|
|
|
|
|
return &models_[name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::uint32_t i = 0;
|
|
|
|
|
for (i = 0; i < std::numeric_limits<std::uint32_t>::max();) {
|
|
|
|
|
if (models_.find(name + std::to_string(i)) == models_.end()) {
|
|
|
|
|
Model model_to_spawn(*GetStaticModel(asset_name));
|
|
|
|
|
models_[name + std::to_string(i)] = std::move(model_to_spawn);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i == std::numeric_limits<std::uint32_t>::max() - 1)
|
|
|
|
|
return nullptr;
|
|
|
|
|
else
|
|
|
|
|
return &models_[name + std::to_string(i)];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Model* Engine::SpawnLifedModel(std::string asset_name, std::string name,
|
|
|
|
|
std::float_t lifespan) {
|
|
|
|
|
if (asset_name == "") {
|
|
|
|
|
Model model_to_spawn(nullptr);
|
|
|
|
|
model_to_spawn.asset_name = "";
|
|
|
|
|
model_to_spawn.name = name;
|
|
|
|
|
model_to_spawn.visible = false;
|
|
|
|
|
model_to_spawn.lifespan = lifespan;
|
|
|
|
|
models_[name] = std::move(model_to_spawn);
|
|
|
|
|
@@ -78,6 +50,8 @@ Model* Engine::SpawnLifedModel(std::string asset_name, std::string name,
|
|
|
|
|
|
|
|
|
|
if (models_.find(name) == models_.end()) {
|
|
|
|
|
Model model_to_spawn(*GetStaticModel(asset_name));
|
|
|
|
|
model_to_spawn.asset_name = asset_name;
|
|
|
|
|
model_to_spawn.name = name;
|
|
|
|
|
model_to_spawn.lifespan = lifespan;
|
|
|
|
|
models_[name] = std::move(model_to_spawn);
|
|
|
|
|
return &models_[name];
|
|
|
|
|
@@ -87,6 +61,8 @@ Model* Engine::SpawnLifedModel(std::string asset_name, std::string name,
|
|
|
|
|
for (i = 0; i < std::numeric_limits<std::uint32_t>::max();) {
|
|
|
|
|
if (models_.find(name + std::to_string(i)) == models_.end()) {
|
|
|
|
|
Model model_to_spawn(*GetStaticModel(asset_name));
|
|
|
|
|
model_to_spawn.asset_name = asset_name;
|
|
|
|
|
model_to_spawn.name = name + std::to_string(i);
|
|
|
|
|
model_to_spawn.lifespan = lifespan;
|
|
|
|
|
models_[name + std::to_string(i)] = std::move(model_to_spawn);
|
|
|
|
|
break;
|
|
|
|
|
@@ -152,6 +128,11 @@ void Engine::Update() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (model.shouldBeDestroyed) {
|
|
|
|
|
it = models_.erase(it);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
model.lifespan -= delta_time;
|
|
|
|
|
|
|
|
|
|
models.push_back(&model);
|
|
|
|
|
@@ -178,6 +159,7 @@ void Engine::NetworkUpload(std::shared_ptr<Network::Socket> sock) {
|
|
|
|
|
std::vector<char> data;
|
|
|
|
|
for (auto& it : models_) {
|
|
|
|
|
if (!it.second.networkReplicated) continue;
|
|
|
|
|
if (!it.second.needsUpdate) continue;
|
|
|
|
|
|
|
|
|
|
Packet::Header header;
|
|
|
|
|
header.opcode = Packet::Opcode::UPDATEMODEL;
|
|
|
|
|
@@ -187,19 +169,123 @@ void Engine::NetworkUpload(std::shared_ptr<Network::Socket> sock) {
|
|
|
|
|
|
|
|
|
|
data.insert(data.end(), header_serialized.begin(), header_serialized.end());
|
|
|
|
|
data.insert(data.end(), model.begin(), model.end());
|
|
|
|
|
it.second.needsUpdate = false;
|
|
|
|
|
}
|
|
|
|
|
iocp_->send(*sock, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int recv_partial(Network::IOCP* iocp, Network::Socket& sock,
|
|
|
|
|
std::vector<char>& buffer) {
|
|
|
|
|
size_t total_received = 0;
|
|
|
|
|
size_t expected = buffer.size();
|
|
|
|
|
|
|
|
|
|
while (total_received < expected) {
|
|
|
|
|
if (total_received != 0)
|
|
|
|
|
Sleep(100); // 너무 빨리 재시도해서 큐가 채워질 틈이 없는 듯?
|
|
|
|
|
if (sock.sock == 0) return 0;
|
|
|
|
|
std::vector<char> temp(expected - total_received);
|
|
|
|
|
|
|
|
|
|
size_t received = iocp->recv(sock, temp);
|
|
|
|
|
if (received == 0) return 0;
|
|
|
|
|
|
|
|
|
|
std::copy(temp.begin(), temp.begin() + received,
|
|
|
|
|
buffer.begin() + total_received);
|
|
|
|
|
total_received += received;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return total_received;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void recv_fully(Network::IOCP* iocp, Network::Socket& sock,
|
|
|
|
|
std::vector<char>& buffer) {
|
|
|
|
|
size_t total_received = 0;
|
|
|
|
|
size_t expected = buffer.size();
|
|
|
|
|
|
|
|
|
|
while (total_received < expected) {
|
|
|
|
|
if (total_received != 0)
|
|
|
|
|
Sleep(100); // 너무 빨리 재시도해서 큐가 채워질 틈이 없는 듯?
|
|
|
|
|
if (sock.sock == 0) return;
|
|
|
|
|
std::vector<char> temp(expected - total_received);
|
|
|
|
|
|
|
|
|
|
size_t received = iocp->recv(sock, temp);
|
|
|
|
|
|
|
|
|
|
std::copy(temp.begin(), temp.begin() + received,
|
|
|
|
|
buffer.begin() + total_received);
|
|
|
|
|
total_received += received;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::ResponseToServerAndRefresh(std::shared_ptr<Network::Socket> sock) {
|
|
|
|
|
while (iocp_->GetRecvedBytes(sock->sock) < 6);
|
|
|
|
|
if (sock->sock == 0) return;
|
|
|
|
|
std::vector<char> recv_data(6);
|
|
|
|
|
iocp_->recv(*sock, recv_data);
|
|
|
|
|
if (recv_partial(iocp_, *sock, recv_data) == 0) return;
|
|
|
|
|
Packet::Header header;
|
|
|
|
|
header.Deserialize(recv_data);
|
|
|
|
|
recv_data.resize(header.body_length);
|
|
|
|
|
while (iocp_->GetRecvedBytes(sock->sock) < header.body_length);
|
|
|
|
|
iocp_->recv(*sock, recv_data);
|
|
|
|
|
recv_fully(iocp_, *sock, recv_data);
|
|
|
|
|
|
|
|
|
|
switch (header.opcode) {
|
|
|
|
|
case Packet::Opcode::UPDATEMODEL: {
|
|
|
|
|
veng::Model model;
|
|
|
|
|
model.Deserialize(recv_data);
|
|
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
|
for (auto& it : models_) {
|
|
|
|
|
if (it.second.ID == model.ID) {
|
|
|
|
|
it.second.position = model.position;
|
|
|
|
|
it.second.linear_velocity = model.linear_velocity;
|
|
|
|
|
it.second.linear_acceleration = model.linear_acceleration;
|
|
|
|
|
|
|
|
|
|
it.second.rotation = model.rotation;
|
|
|
|
|
it.second.angular_velocity = model.angular_velocity;
|
|
|
|
|
it.second.angular_acceleration = model.angular_acceleration;
|
|
|
|
|
|
|
|
|
|
it.second.scale = model.scale;
|
|
|
|
|
it.second.transform = model.transform;
|
|
|
|
|
|
|
|
|
|
it.second.original_offset = model.original_offset;
|
|
|
|
|
it.second.radius = model.radius;
|
|
|
|
|
it.second.lifespan = model.lifespan;
|
|
|
|
|
|
|
|
|
|
it.second.visible = model.visible;
|
|
|
|
|
it.second.colision = model.colision;
|
|
|
|
|
|
|
|
|
|
spdlog::info("model updated: {}", model.name);
|
|
|
|
|
spdlog::info("model pos: ({},{},{})", model.position.x,
|
|
|
|
|
model.position.y, model.position.z);
|
|
|
|
|
found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
|
auto spawnedModel =
|
|
|
|
|
SpawnLifedModel(model.asset_name, model.name, model.lifespan);
|
|
|
|
|
spawnedModel->ID = model.ID;
|
|
|
|
|
spawnedModel->position = model.position;
|
|
|
|
|
spawnedModel->linear_velocity = model.linear_velocity;
|
|
|
|
|
spawnedModel->linear_acceleration = model.linear_acceleration;
|
|
|
|
|
|
|
|
|
|
spawnedModel->rotation = model.rotation;
|
|
|
|
|
spawnedModel->angular_velocity = model.angular_velocity;
|
|
|
|
|
spawnedModel->angular_acceleration = model.angular_acceleration;
|
|
|
|
|
|
|
|
|
|
spawnedModel->scale = model.scale;
|
|
|
|
|
spawnedModel->transform = model.transform;
|
|
|
|
|
|
|
|
|
|
spawnedModel->original_offset = model.original_offset;
|
|
|
|
|
spawnedModel->radius = model.radius;
|
|
|
|
|
spawnedModel->lifespan = model.lifespan;
|
|
|
|
|
|
|
|
|
|
spawnedModel->visible = model.visible;
|
|
|
|
|
spawnedModel->colision = model.colision;
|
|
|
|
|
spawnedModel->networkReplicated = false;
|
|
|
|
|
spdlog::info("model spawned: {}", model.ID.snowflake);
|
|
|
|
|
}
|
|
|
|
|
} break;
|
|
|
|
|
default:
|
|
|
|
|
spdlog::error("unknown data type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace veng
|
|
|
|
|
|