//
// read_node
//
void ClientSidePrediction::read_node(MemoryBuffer& message)
{
    auto network = GetSubsystem<Network>();
    auto scene = network->GetServerConnection()->GetScene();

    auto node_id = message.ReadUInt();
    auto node = scene->GetNode(node_id);
    bool new_node = false;

    // Create the node if it doesn't exist
    if (!node)
    {
        new_node = true;
        // Add initially to the root level. May be moved as we receive the parent attribute
        node = scene->CreateChild(node_id, LOCAL);
        // Create smoothed transform component
        node->CreateComponent<SmoothedTransform>(LOCAL);
    }
    else
    {
        // Remove the node from the unused nodes list
        unused_nodes.erase(node);
    }

    // Read attributes
    read_network_attributes(*node, message);
    // ApplyAttributes() is deliberately skipped, as Node has no attributes that require late applying.
    // Furthermore it would propagate to components and child nodes, which is not desired in this case

    if (new_node)
    {
        // Snap the motion smoothing immediately to the end
        auto transform = node->GetComponent<SmoothedTransform>();
        if (transform)
            transform->Update(1.0f, 0.0f);
    }

    // Read user variables
    unsigned num_vars = message.ReadVLE();
    for (; num_vars > 0; --num_vars)
    {
        auto key = message.ReadStringHash();
        node->SetVar(key, message.ReadVariant());
    }

    // Read components
    unsigned num_components = message.ReadVLE();
    for (; num_components > 0; --num_components)
        read_component(message, node);
}
//
// read_scene_state
//
void ClientSidePrediction::read_scene_state(MemoryBuffer& message)
{
    auto network = GetSubsystem<Network>();
    auto scene = network->GetServerConnection()->GetScene();

    // Read last input ID
    auto new_server_id = message.ReadUInt();

    // Make sure it's more recent than the previous last ID since we're sending unordered messages
    // Handle range looping correctly
    if (id > server_id)
    {
        if (new_server_id < server_id)
            return;
    }
    else
    {
        if (new_server_id > server_id)
            return;
    }

    server_id = new_server_id;

    // Reset the unused nodes set
    unused_nodes.clear();
    for (auto node : scene_nodes[scene])
        unused_nodes.insert(node);

    // Read number of nodes
    auto num_nodes = message.ReadVLE();

    // Read nodes
    for (; num_nodes > 0; --num_nodes)
        read_node(message);

    // Remove unsued nodes
    for (auto node : unused_nodes)
        node->Remove();

    // Perform client side prediction
    predict();
}