public async Task ConnectUniverseAsync(Universe universe)
        {
            Disconnect();
            IsConnecting = true;
            try
            {
                var server = await Task.Run(() => new TcpServer(universe, true));
                _server = server;

                var channel = await ConnectTcpAsync("127.0.0.1", server.Port);
                await ConnectChannelAsync(channel);
            }
            catch
            {
            }
            finally
            {
                IsConnecting = false;
            }
        }
Beispiel #2
0
 public World(Universe universe)
     : this(universe, null)
 {
 }
Beispiel #3
0
 public TcpServer(Universe universe, bool runPublic)
 {
     _universe = universe;
     RunHost(runPublic);
 }
Beispiel #4
0
 public World(Universe universe, ModData modData)
 {
     Universe = universe;
     ModData = modData;
 }
Beispiel #5
0
 public Channel(Universe universe, Entity player)
 {
     _universe = universe;
     _player = player;
 }
Beispiel #6
0
        private void RunUniverse()
        {
            try
            {
                var frameTime = GameTime.Now();

                while (!_isDisposed)
                {
                    var startTime = DateTime.Now;

                    lock (_mutex)
                    {
                        foreach (var channel in _channels)
                        {
                            while (true)
                            {
                                var playerEvent = channel.TakePlayerEvent();
                                if (playerEvent == null)
                                {
                                    break;
                                }
                                HandlePlayerEvent(channel, playerEvent);
                            }
                        }

                        var elapsedDuration = GameTime.Update(ref frameTime);
                        foreach (var entity in Universe.GetEntitiesWithComponent <PositionComponent>())
                        {
                            if (entity.Has <AiComponent>())
                            {
                                Ai.Control(Mod.PhysicsValues, elapsedDuration, entity);
                            }

                            var position = entity.Get <PositionComponent>();

                            Movement.MoveEntity(
                                Mod.PhysicsValues,
                                Universe.StartWorld,
                                position,
                                elapsedDuration,
                                position.Velocity);

                            entity.Set(position);
                        }

                        foreach (var channel in _channels)
                        {
                            if (channel.NeedsGameEvent())
                            {
                                var gameEvent = GetCurrentGameEvent(channel);
                                if (!channel.HasSentInitialValues)
                                {
                                    channel.HasSentInitialValues = true;
                                    gameEvent.PhysicsValues      = Mod.PhysicsValues;
                                }

                                channel.QueueGameEvent(gameEvent);
                            }
                        }
                    }

                    var endTime = DateTime.Now;

                    double sleepMillis = 10 - (endTime - startTime).TotalMilliseconds;
                    if (sleepMillis > 0)
                    {
                        Thread.Sleep((int)Math.Ceiling(sleepMillis));
                    }
                }
            }
            catch
            {
            }
        }
Beispiel #7
0
        private GameEvent GetCurrentGameEvent(Channel channel)
        {
            var player = channel.Player;

            var gameEvent = new GameEvent()
            {
                EntityInfos = new List <EntityData>()
            };

            foreach (var entity in Universe.GetEntitiesWithComponent <PositionComponent>())
            {
                if (entity != player)
                {
                    var position = entity.Get <PositionComponent>();
                    gameEvent.EntityInfos.Add(
                        new EntityData
                    {
                        PositionData = position,
                        ModelIndex   = Mod.MobTypes.GetOrDefault(entity.Get <MobTypeComponent>()?.MobType)?.Model?.Index,
                    });
                }
            }

            PositionComponent playerPosition;

            if (player.TryGet(out playerPosition))
            {
                var playerChunkPos = (ChunkPos)playerPosition.Placement.Pos;

                int chunksInPacket = 0;

                ChunkPos.IterateOutwards(
                    playerChunkPos,
                    ChunkViewRadiusXZ,
                    ChunkViewRadiusY,
                    chunkPos =>
                {
                    if (chunksInPacket < 5 && !channel.SentChunks.ContainsKey(chunkPos))
                    {
                        var chunk = Universe.StartWorld.GetChunk(chunkPos);

                        if (!chunk.IsCreated)
                        {
                            _worldGenerator.OnChunkRequested(chunk);
                        }
                        else
                        {
                            channel.SentChunks[chunkPos] = true;

                            if (chunk != null)
                            {
                                if (gameEvent.ChunkDataList == null)
                                {
                                    gameEvent.ChunkDataList = new List <ChunkData>();
                                }

                                gameEvent.ChunkDataList.Add(chunk.GetChunkData());
                                ++chunksInPacket;
                            }
                        }
                    }
                });
            }

            if (channel.SentBlockUpdates != _blockUpdates.Count)
            {
                gameEvent.BlockUpdates   = _blockUpdates.GetRange(channel.SentBlockUpdates, _blockUpdates.Count - channel.SentBlockUpdates);
                channel.SentBlockUpdates = _blockUpdates.Count;
            }

            return(gameEvent);
        }
Beispiel #8
0
        private async void Start()
        {
            using (Lock())
            {
                Save();

                await Task.Run(
                    () =>
                    {
                        try
                        {
                            using (var universe = new Universe(NullSaveFile.Instance, DataLoader.LoadMod(_modName)))
                            {
                                using (var container = new DependencyInjectionContainer())
                                {
                                    var gameLoop = container.Resolve<GameLoop>();
                                    var connectionManager = container.Resolve<GameConnectionManager>();
                                    gameLoop.Post(() => { var notAwaited = connectionManager.ConnectUniverseAsync(universe); });

                                    var gameApp = container.Resolve<GameApp>();
                                    gameApp.Run();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            if (Debugger.IsAttached)
                            {
                                Debugger.Break();
                            }

                            MessageBox.Show(ex.ToString(), "CubeHack", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    });
            }
        }