Ejemplo n.º 1
0
        public static NpcFarm Create(Vector3Int farmPosition, int size)
        {
            var result = new NpcFarm();

            result.Origin = farmPosition;
            result.Size   = size;
            return(result);
        }
Ejemplo n.º 2
0
 public static void AfterNetworkSetup()
 {
     new Thread(() => {
         Thread.CurrentThread.IsBackground = true;
         Log.Write("Started kingdom spawner thread");
         while (true)
         {
             Thread.Sleep(DelayBetweenPlacingAttempts);
             try {
                 if (KingdomsTracker.Count < MaxNumberOfKingdoms)
                 {
                     for (int c = 0; c < NumOfSpotsToCheckPerAttempt; c++)
                     {
                         var kingdomPosition = GetRandomSpot(MaxRangeFromSpawn);
                         var farmSize        = 1 + Pipliz.Random.Next(NpcFarmBuilder.MAX_SIZE);
                         var npcKingdom      = NpcFarm.Create(kingdomPosition, farmSize);
                         var closestBanner   = BannerTracker.GetClosest(kingdomPosition, MinDistanceToBanners);
                         if (closestBanner == null)
                         {
                             LoadChunksBlocking(npcKingdom.GetPrimaryChunkPositions());
                             if (npcKingdom.IsAreaClear())
                             {
                                 LoadChunksBlocking(npcKingdom.GetTotalChunkPositions());
                                 npcKingdom.InitNew();
                                 if (KingdomsTracker.Count >= MaxNumberOfKingdoms)
                                 {
                                     Log.Write($"Reached maximum number ({MaxNumberOfKingdoms}) of kingdoms");
                                 }
                                 break;
                             }
                             try {
                                 currentlyUsedChunksLock.EnterWriteLock();
                                 currentlyUsedChunks.Clear();
                             } finally {
                                 if (currentlyUsedChunksLock.IsWriteLockHeld)
                                 {
                                     currentlyUsedChunksLock.ExitWriteLock();
                                 }
                             }
                         }
                         Thread.Sleep(DelayBetweenSpotChecks);
                     }
                 }
             } catch (Exception exception) {
                 Log.WriteError($"Exception in kingdom update thread; {exception.Message}");
             }
         }
     }).Start();
 }
Ejemplo n.º 3
0
 public bool TryDoCommand(Players.Player causedBy, string chattext)
 {
     try {
         if (!Permissions.PermissionsManager.CheckAndWarnPermission(causedBy, KingdomsModEntries.MOD_PREFIX + "farm"))
         {
             return(true);
         }
         var m = Regex.Match(chattext, @"/farm( (?<size>\d+))?");
         if (!m.Success)
         {
             Chat.Send(causedBy, "Command didn't match, use /farm [size]");
             return(true);
         }
         string strSize = m.Groups ["size"].Value;
         int    size;
         if (strSize.Length > 0)
         {
             if (!int.TryParse(strSize, out size))
             {
                 Chat.Send(causedBy, "Could not parse size");
                 return(true);
             }
             if (size < 1 || size > NpcFarmBuilder.MAX_SIZE)
             {
                 Chat.Send(causedBy, $"Size is out of range; min: 1; max: {NpcFarmBuilder.MAX_SIZE}");
                 return(true);
             }
         }
         else
         {
             size = 1 + Pipliz.Random.Next(NpcFarmBuilder.MAX_SIZE);
         }
         var farmPosition = ToFarmPosition(causedBy.Position);
         NpcFarm.Create(farmPosition, size).InitNew();
         Chat.Send(causedBy, $"You placed a farm at {farmPosition} with size {size}");
     } catch (Exception exception) {
         Log.WriteError($"Exception while parsing command; {exception.Message} - {exception.StackTrace}");
     }
     return(true);
 }
Ejemplo n.º 4
0
 public static void Load()
 {
     try {
         JSONNode jsonFileNode;
         if (JSON.Deserialize(JsonFilePath, out jsonFileNode, false))
         {
             try {
                 KingdomsLock.EnterWriteLock();
                 Kingdoms.Clear();
             } finally {
                 if (KingdomsLock.IsWriteLockHeld)
                 {
                     KingdomsLock.ExitWriteLock();
                 }
             }
             jsonFileNode.TryGetAsOrDefault("NextNpcID", out NextNpcID, DefaultNextNpcID);
             jsonFileNode.TryGetAsOrDefault("NotifyKingdomPermission", out NotifyKingdomPermission, DefaultNotifyPermission);
             jsonFileNode.TryGetAsOrDefault("NotifyLootboxPermission", out NotifyLootboxPermission, DefaultNotifyPermission);
             JSONNode jsonSpawner;
             if (jsonFileNode.TryGetAs("spawner", out jsonSpawner) && jsonSpawner.NodeType == NodeType.Object)
             {
                 KingdomSpawner.SetFromJson(jsonSpawner);
             }
             else
             {
                 Log.Write($"kingdom spawner not configured in {JsonFilePath}, loading defaults");
             }
             JSONNode jsonLoot;
             if (jsonFileNode.TryGetAs("loot", out jsonLoot))
             {
                 Lootbox.SetFromJson(jsonLoot);
             }
             JSONNode jsonKingdoms;
             if (!jsonFileNode.TryGetAs("kingdoms", out jsonKingdoms) || jsonKingdoms.NodeType != NodeType.Array)
             {
                 Log.WriteError($"No 'kingdoms' array found in '{JsonFilePath}'");
                 return;
             }
             foreach (JSONNode jsonNode in jsonKingdoms.LoopArray())
             {
                 string type;
                 if (jsonNode.TryGetAs("KingdomType", out type))
                 {
                     NpcKingdom kingdom;
                     if ("farm".Equals(type))
                     {
                         kingdom = new NpcFarm();
                     }
                     else
                     {
                         Log.WriteError($"Unknown npc kingdom type {type}");
                         continue;
                     }
                     kingdom.InitFromJson(jsonNode);
                     NextNpcID = System.Math.Max(NextNpcID, kingdom.NpcID);
                 }
             }
             Log.Write($"Loaded {Count} kingdoms from json");
         }
     } catch (Exception exception) {
         Log.WriteError($"Exception while loading kingdoms; {exception.Message}");
     }
 }