Untitled

 avatar
unknown
plain_text
25 days ago
2.6 kB
4
Indexable
void Player::sendVariant(int delay = 0, int netID = -1, std::initializer_list<std::string> strings = {}, std::initializer_list<int> integers = {}, std::initializer_list<float> floats = {}, std::initializer_list<std::pair<float, float>> float_pairs = {}, std::initializer_list<std::tuple<float, float, float>> float_triplets = {})
{
    std::vector<uint8_t> packet_data(61, 0);
    int index = 0;

    int messageType = 0x4, packetType = 0x1, charState = 0x8;

    std::memcpy(packet_data.data(), &messageType, 4);
    std::memcpy(packet_data.data() + 4, &packetType, 4);
    std::memcpy(packet_data.data() + 8, &netID, 4);
    std::memcpy(packet_data.data() + 16, &charState, 4);
    std::memcpy(packet_data.data() + 24, &delay, 4);

    for (const auto& str : strings) {
        packet_data.push_back(index++);
        packet_data.push_back(0x2);
        uint16_t str_length = static_cast<uint16_t>(str.size());
        const uint8_t* len_ptr = reinterpret_cast<const uint8_t*>(&str_length);
        packet_data.insert(packet_data.end(), len_ptr, len_ptr + 2);
        packet_data.insert(packet_data.end(), str.begin(), str.end());
    }

    for (const auto& integer : integers) {
        packet_data.push_back(index++);
        packet_data.push_back(0x9);
        const uint8_t* int_ptr = reinterpret_cast<const uint8_t*>(&integer);
        packet_data.insert(packet_data.end(), int_ptr, int_ptr + sizeof(int));
    }

    for (const auto& value : floats) {
        packet_data.push_back(index++);
        packet_data.push_back(0x1);
        const uint8_t* float_ptr = reinterpret_cast<const uint8_t*>(&value);
        packet_data.insert(packet_data.end(), float_ptr, float_ptr + sizeof(float));
    }

    for (const auto& pair : float_pairs) {
        packet_data.push_back(index++);
        packet_data.push_back(0x3);
        const float* pair_values = &pair.first;
        const uint8_t* pair_ptr = reinterpret_cast<const uint8_t*>(pair_values);
        packet_data.insert(packet_data.end(), pair_ptr, pair_ptr + 2 * sizeof(float));
    }

    for (const auto& triplet : float_triplets) {
        packet_data.push_back(index++);
        packet_data.push_back(0x4);
        const float* triplet_values = &std::get<0>(triplet);
        const uint8_t* triplet_ptr = reinterpret_cast<const uint8_t*>(triplet_values);
        packet_data.insert(packet_data.end(), triplet_ptr, triplet_ptr + 3 * sizeof(float));
    }

    ENetPacket* packet = enet_packet_create(packet_data.data(), packet_data.size(), ENET_PACKET_FLAG_RELIABLE);
    enet_peer_send(this->m_peer, 0, packet);
}
Leave a Comment