Esempio n. 1
0
            public void processInputs()
            {
                // Process all pending messages from clients.
                while (true)
                {
                    LagNetwork.IMessage message = this.network.receive();
                    if (message == null)
                    {
                        break;
                    }
                    Client.Input input = ((LagNetwork.ClientMessage)message).input;

                    // Update the state of the entity, based on its input.
                    // We just ignore inputs that don't look valid; this is what prevents clients from cheating.
                    Console.WriteLine(input.press_time.TotalMilliseconds);
                    if (true || validateInput(input))
                    {
                        int id = input.entity_id;
                        entities[id].applyInput(input);
                        last_processed_input[id] = input.input_sequence_number;
                    }
                }
            }
Esempio n. 2
0
            public void processServerMessages()
            {
                while (true)
                {
                    LagNetwork.IMessage message = network.receive();
                    if (message == null)
                    {
                        break;
                    }

                    Server.State[] states = ((LagNetwork.ServerMessage)message).states;

                    // World state is a list of entity states.
                    for (int i = 0; i < states.Length; i++)
                    {
                        Server.State state = states[i];

                        // If this is the first time we see this entity, create a local representation.
                        if (!entities.ContainsKey(state.entity_id))
                        {
                            Entity newEntity = new Entity();
                            newEntity.entity_id       = state.entity_id;
                            entities[state.entity_id] = newEntity;
                        }

                        Entity entity = entities[state.entity_id];

                        if (state.entity_id == entity_id)
                        {
                            // Received the authoritative position of this client's entity.
                            entity.x = state.position;

                            if (server_reconciliation)
                            {
                                // Server Reconciliation. Re-apply all the inputs not yet processed by
                                // the server.
                                var j = 0;
                                while (j < this.pending_inputs.Count)
                                {
                                    Input input = pending_inputs[j];
                                    if (input.input_sequence_number <= state.last_processed_input)
                                    {
                                        // Already processed. Its effect is already taken into account into the world update
                                        // we just got, so we can drop it.
                                        this.pending_inputs.RemoveAt(j);
                                    }
                                    else
                                    {
                                        // Not processed by the server yet. Re-apply it.
                                        entity.applyInput(input);
                                        j++;
                                    }
                                }
                            }
                            else
                            {
                                // Reconciliation is disabled, so drop all the saved inputs.
                                pending_inputs.Clear();
                            }
                        }
                        else
                        {
                            // Received the position of an entity other than this client's.

                            if (!entity_interpolation)
                            {
                                // Entity interpolation is disabled - just accept the server's position.
                                entity.x = state.position;
                            }
                            else
                            {
                                // Add it to the position buffer.
                                DateTime timestamp = DateTime.Now;
                                entity.position_buffer.Add(new Entity.Position(timestamp, state.position));
                            }
                        }
                    }
                }
            }