Exemplo n.º 1
0
        public void CheckGrowing()
        {
            var entityGrowingManager = new EntityGrowingManager(null);

            var random = new Random(1);

            var entity = new PlantGrowingEntity();

            entity.GrowLevels.Add(new GrowLevel {
                GrowTime = UtopiaTimeSpan.FromHours(1)
            });
            entity.GrowLevels.Add(new GrowLevel {
                GrowTime = UtopiaTimeSpan.FromHours(1)
            });
            entity.GrowLevels.Add(new GrowLevel {
                GrowTime = UtopiaTimeSpan.FromHours(1)
            });
            entity.GrowLevels.Add(new GrowLevel {
                GrowTime = UtopiaTimeSpan.FromHours(1)
            });
            entity.LastGrowUpdate = new UtopiaTime() + UtopiaTimeSpan.FromMinutes(1);

            var now = new UtopiaTime() + UtopiaTimeSpan.FromHours(2.5);

            entityGrowingManager.EntityGrowCheck(now, entity, null, random);
            Assert.AreEqual(2, entity.CurrentGrowLevelIndex);

            entity = new PlantGrowingEntity();
            entity.GrowingSeasons.Add(UtopiaTime.TimeConfiguration.Seasons[0].Name);
            entity.GrowingSeasons.Add(UtopiaTime.TimeConfiguration.Seasons[2].Name);
            entity.GrowLevels.Add(new GrowLevel {
                GrowTime = UtopiaTimeSpan.FromDays(10)
            });
            entity.GrowLevels.Add(new GrowLevel {
                GrowTime = UtopiaTimeSpan.FromDays(10)
            });
            entity.GrowLevels.Add(new GrowLevel {
                GrowTime = UtopiaTimeSpan.FromDays(10)
            });
            entity.GrowLevels.Add(new GrowLevel {
                GrowTime = UtopiaTimeSpan.FromDays(10)
            });
            entity.LastGrowUpdate = new UtopiaTime() + UtopiaTimeSpan.FromMinutes(1);

            now = new UtopiaTime() + UtopiaTimeSpan.FromMinutes(50);
            entityGrowingManager.EntityGrowCheck(now, entity, null, random);
            entity.LastGrowUpdate = now;
            Assert.AreEqual(0, entity.CurrentGrowLevelIndex);

            now = new UtopiaTime() + UtopiaTimeSpan.FromYears(1);
            entityGrowingManager.EntityGrowCheck(now, entity, null, random);
            entity.LastGrowUpdate = now;
            Assert.AreEqual(1, entity.CurrentGrowLevelIndex);

            now = new UtopiaTime() + UtopiaTimeSpan.FromYears(1) + UtopiaTimeSpan.FromMinutes(1);
            entityGrowingManager.EntityGrowCheck(now, entity, null, random);
            entity.LastGrowUpdate = now;
            Assert.AreEqual(2, entity.CurrentGrowLevelIndex);
        }
Exemplo n.º 2
0
        private void SpawnBiomeEntities(UtopiaTime gametime, ServerChunk chunk)
        {
            var chunkBiome = _configuration.ProcessorParam.Biomes[chunk.BlockData.ChunkMetaData.ChunkMasterBiomeType];

            foreach (var spawnableEntity in chunkBiome.SpawnableEntities)
            {
                bool isStaticEntity = _configuration.BluePrints[spawnableEntity.BluePrintId] is IStaticEntity;

                //Remark : isChunkGenerationSpawning is set to true for static entities, and false for dynamic entities, maybe worth renaming the properties
                //The aim of it is to avoid dynamic entity creation at chunk generation time (Pure chunk).
                //Apply creation constaint :
                //1) if static entity with is isWildChunkNeeded and chunk is not wild => Do nothing
                if (spawnableEntity.IsWildChunkNeeded && chunk.BlockData.ChunkMetaData.IsWild == false)
                {
                    continue;
                }

                if (chunk.PureGenerated && isStaticEntity)
                {
                    continue;
                }

                var isDayTime = gametime.TimeOfDay > UtopiaTimeSpan.FromHours(8) &&
                                gametime.TimeOfDay < UtopiaTimeSpan.FromHours(20);
                // check daytime constraints
                if (!spawnableEntity.SpawningDayTime.HasFlag(ChunkSpawningDayTime.Day) &&
                    isDayTime)
                {
                    continue;
                }

                if (!spawnableEntity.SpawningDayTime.HasFlag(ChunkSpawningDayTime.Night) &&
                    !isDayTime)
                {
                    continue;
                }

                // check season constraint
                var weatherService = _server.Services.GetService <WeatherService>();

                if (weatherService != null && weatherService.CurrentSeason != null &&
                    spawnableEntity.SpawningSeasons.Count > 0 &&
                    !spawnableEntity.SpawningSeasons.Contains(weatherService.CurrentSeason.Name))
                {
                    continue;
                }

                ByteChunkCursor chunkCursor = new ByteChunkCursor(chunk.BlockData.GetBlocksBytes(), chunk.BlockData.ColumnsInfo);
                // ==> Maybe worth to automaticaly create this specialize cursor at server chunk creation ? Multithreading problem ?
                //It is only use for reading chunk block data in fast way
                Vector3D entityLocation;
                if (_entitySpawningControler.TryGetSpawnLocation(spawnableEntity, chunk, chunkCursor, _fastRandom,
                                                                 out entityLocation))
                {
                    var entity       = _server.EntityFactory.CreateFromBluePrint(spawnableEntity.BluePrintId);
                    var staticEntity = entity as IStaticEntity;

                    if (staticEntity != null)
                    {
                        //Check the maximum amount of static entities;
                        int maxEntityAmount;
                        chunk.BlockData.ChunkMetaData.InitialSpawnableEntitiesAmount.TryGetValue(spawnableEntity.BluePrintId,
                                                                                                 out maxEntityAmount);
                        if (maxEntityAmount == 0)
                        {
                            continue;
                        }
                        if (chunk.Entities.Where(e => e.BluePrintId == spawnableEntity.BluePrintId)
                            .CountAtLeast(maxEntityAmount))
                        {
                            continue;
                        }

                        staticEntity.Position = entityLocation;

                        var cursor = _server.LandscapeManager.GetCursor(entityLocation);
                        logger.Debug("Spawning new static entity : {0} at location {1}", staticEntity.Name, entityLocation);

                        var blockLinkedItem = staticEntity as IBlockLinkedEntity;
                        if (blockLinkedItem != null)
                        {
                            blockLinkedItem.LinkedCube = (Vector3I)entityLocation + Vector3I.Down;
                        }

                        cursor.AddEntity(staticEntity);
                    }

                    var charEntity = entity as CharacterEntity;
                    if (charEntity != null)
                    {
                        if (DisableNPCSpawn)
                        {
                            continue;
                        }

                        var radius = Math.Max(8, spawnableEntity.DynamicEntitySpawnRadius);
                        if (
                            _server.AreaManager.EnumerateAround(entityLocation, radius)
                            .CountAtLeast(spawnableEntity.MaxEntityAmount))
                        {
                            continue;
                        }

                        charEntity.Position = entityLocation;
                        logger.Debug("Spawning new dynamic entity : {0} at location {1}", charEntity.Name, entityLocation);
                        _server.EntityManager.AddNpc(charEntity);
                    }
                }
            }
        }
Exemplo n.º 3
0
        public void InitSinglePlayerServer(WorldParameters worldParam)
        {
            if (Server != null)
            {
                throw new InvalidOperationException("Already initialized");
            }

            _worldParam = worldParam;

            _serverFactory        = new EntityFactory();
            _serverFactory.Config = _worldParam.Configuration;
            var dbPath = Path.Combine(_vars.ApplicationDataPath, "Server", "Singleplayer", worldParam.WorldName, "ServerWorld.db");

            logger.Info("Local world db path is {0}", dbPath);

            _serverSqliteStorageSinglePlayer = new SqliteStorageManager(dbPath, _serverFactory, worldParam);
            _serverSqliteStorageSinglePlayer.Register("local", "qwe123".GetSHA1Hash(), UserRole.Administrator);

            var settings = new XmlSettingsManager <ServerSettings>(@"Server\localServer.config");

            settings.Load();
            settings.Save();

            //Utopia New Landscape Test

            IWorldProcessor          processor = null;
            IEntitySpawningControler entitySpawningControler = null;

            switch (worldParam.Configuration.WorldProcessor)
            {
            case WorldConfiguration.WorldProcessors.Flat:
                processor = new FlatWorldProcessor();
                break;

            case WorldConfiguration.WorldProcessors.Utopia:
                processor = new UtopiaProcessor(worldParam, _serverFactory, _landscapeEntityManager);
                entitySpawningControler = new UtopiaEntitySpawningControler((UtopiaWorldConfiguration)worldParam.Configuration);
                break;

            default:
                break;
            }

            var worldGenerator = new WorldGenerator(worldParam, processor);

            worldGenerator.EntitySpawningControler = entitySpawningControler;

            //Old s33m3 landscape
            //IWorldProcessor processor1 = new s33m3WorldProcessor(worldParam);
            //IWorldProcessor processor2 = new LandscapeLayersProcessor(worldParam, _serverFactory);
            //var worldGenerator = new WorldGenerator(worldParam, processor1, processor2);

            //Vlad Generator
            //var planProcessor = new PlanWorldProcessor(wp, _serverFactory);
            //var worldGenerator = new WorldGenerator(wp, planProcessor);
            settings.Settings.ChunksCountLimit = 1024 * 3; // better use viewRange * viewRange * 3

            var port = 4815;

            while (!TcpConnectionListener.IsPortFree(port))
            {
                port++;
            }
            settings.Settings.ServerPort = port;

            _server = new ServerCore(settings, worldGenerator, _serverSqliteStorageSinglePlayer, _serverSqliteStorageSinglePlayer, _serverSqliteStorageSinglePlayer, _serverSqliteStorageSinglePlayer, _serverFactory, worldParam);
            _serverFactory.LandscapeManager     = Server.LandscapeManager;
            _serverFactory.DynamicEntityManager = Server.AreaManager;
            _serverFactory.GlobalStateManager   = Server.GlobalStateManager;
            _serverFactory.ScheduleManager      = Server.Scheduler;
            _serverFactory.ServerSide           = true;

            _server.Initialize();

            Server.ConnectionManager.LocalMode = true;
            Server.ConnectionManager.Listen();
            Server.LoginManager.PlayerEntityNeeded  += LoginManagerPlayerEntityNeeded;
            Server.LoginManager.GenerationParameters = default(Utopia.Shared.World.PlanGenerator.GenerationParameters); // planProcessor.WorldPlan.Parameters;
            Server.Clock.SetCurrentTimeOfDay(UtopiaTimeSpan.FromHours(12));
        }