示例#1
0
        public static IntVec3 SpawnList <TE>(Map map, List <TE> pawns, bool attackCell
                                             , Func <TE, bool> getPirate
                                             , Action <Thing, TE> spawn      = null
                                             , Func <Thing, IntVec3> getCell = null)
            where TE : ThingEntry
        {
            if (MainHelper.DebugMode)
            {
                Loger.Log("SpawnList...");
            }

            //на основе UpdateWorldController.DropToWorldObjectDo
            var factionPirate = Find.FactionManager.AllFactions.FirstOrDefault(f => f.def.defName == "Pirate")
                                ?? Find.FactionManager.OfAncientsHostile; //SessionClientController.Data.FactionPirate;

            IntVec3 ret = new IntVec3();

            ModBaseData.RunMainThreadSync(() =>
            {
                Thing thinXZ;
                for (int i = 0; i < pawns.Count; i++)
                {
                    var thing = pawns[i];
                    //GenSpawn.Spawn(pawn, cell, map, Rot4.Random, WipeMode.Vanish, false);

                    if (MainHelper.DebugMode)
                    {
                        Loger.Log("Prepare...");
                    }
                    var thin = PrepareSpawnThingEntry(thing, factionPirate, getPirate(thing));

                    var cell = getCell != null ? getCell(thin) : thin.Position;
                    if (i == 0)
                    {
                        ret = cell;
                    }

                    //if (MainHelper.DebugMode)
                    try
                    {
                        Loger.Log("Spawn... " + thin.Label);
                    }
                    catch
                    {
                        Loger.Log("Spawn... ");
                    }
                    if (thin is Pawn)
                    {
                        if (MainHelper.DebugMode)
                        {
                            Loger.Log("Pawn... " + thin.Position.x + " " + thin.Position.y);
                        }
                        try
                        {
                            GenSpawn.Spawn((Pawn)thin, cell, map);
                        }
                        catch (Exception exp)
                        {
                            Loger.Log("SpawnList Exception " + thing.Name + ": " + exp.ToString());
                            Thread.Sleep(5);
                            GenSpawn.Spawn((Pawn)thin, cell, map);
                        }
                    }
                    else
                    {
                        GenDrop.TryDropSpawn(thin, cell, map, ThingPlaceMode.Near, out thinXZ, null);
                    }
                    if (spawn != null)
                    {
                        spawn(thin, thing);
                    }
                    if (MainHelper.DebugMode)
                    {
                        Loger.Log("Spawn...OK");
                    }
                }
            });
            return(ret);
        }
示例#2
0
 public ModBaseData()
     : base()
 {
     GlobalData = this;
 }
        /// <summary>
        /// Только для своих объетков
        /// </summary>
        public static WorldObjectEntry GetWorldObjectEntry(WorldObject worldObject
                                                           , PlayerGameProgress gameProgress
                                                           , Dictionary <Map, List <Pawn> > cacheColonists)
        {
            var worldObjectEntry = new WorldObjectEntry();

            worldObjectEntry.Type       = worldObject is Caravan ? WorldObjectEntryType.Caravan : WorldObjectEntryType.Base;
            worldObjectEntry.Tile       = worldObject.Tile;
            worldObjectEntry.Name       = worldObject.LabelCap;
            worldObjectEntry.LoginOwner = SessionClientController.My.Login;
            worldObjectEntry.FreeWeight = 999999;

            //определяем цену и вес
            var caravan = worldObject as Caravan;

            if (caravan != null)
            {
                //Loger.Log("Client TestBagSD 002");
                var transferables = CalculateTransferables(caravan);
                //Loger.Log("Client TestBagSD 003");

                List <ThingCount> stackParts = new List <ThingCount>();
                for (int i = 0; i < transferables.Count; i++)
                {
                    TransferableUtility.TransferNoSplit(transferables[i].things, transferables[i].MaxCount /*CountToTransfer*/, delegate(Thing originalThing, int toTake)
                    {
                        stackParts.Add(new ThingCount(originalThing, toTake));
                    }, false, false);
                }
                //Loger.Log("Client TestBagSD 004");
                worldObjectEntry.FreeWeight = CollectionsMassCalculator.Capacity(stackParts)
                                              - CollectionsMassCalculator.MassUsage(stackParts, IgnorePawnsInventoryMode.Ignore, false, false);
                //Loger.Log("Client TestBagSD 005");

                worldObjectEntry.MarketValue     = 0f;
                worldObjectEntry.MarketValuePawn = 0f;
                for (int i = 0; i < stackParts.Count; i++)
                {
                    int count = stackParts[i].Count;

                    if (count > 0)
                    {
                        Thing thing = stackParts[i].Thing;
                        if (thing is Pawn)
                        {
                            worldObjectEntry.MarketValuePawn += thing.MarketValue
                                                                + WealthWatcher.GetEquipmentApparelAndInventoryWealth(thing as Pawn);
                            GameProgressAdd(gameProgress, thing as Pawn);
                        }
                        else
                        {
                            worldObjectEntry.MarketValue += thing.MarketValue * (float)count;
                        }
                    }
                }
                //Loger.Log("Client TestBagSD 006");
            }
            else if (worldObject is Settlement)
            {
                //Loger.Log("Client TestBagSD 007");
                var map = (worldObject as Settlement).Map;
                if (map != null)
                {
                    //Loger.Log("Client TestBagSD 008");
                    try
                    {
                        DateTime lastForceRecount;
                        if (!LastForceRecount.TryGetValue(map.uniqueID, out lastForceRecount))
                        {
                            LastForceRecount.Add(map.uniqueID, DateTime.UtcNow.AddSeconds(new Random(map.uniqueID * 7).Next(0, 10)));
                        }
                        else if ((DateTime.UtcNow - lastForceRecount).TotalSeconds > 30)
                        {
                            LastForceRecount[map.uniqueID] = DateTime.UtcNow;
                            ModBaseData.RunMainThread(() =>
                            {
                                map.wealthWatcher.ForceRecount();
                            });
                        }
                        worldObjectEntry.MarketValue = map.wealthWatcher.WealthTotal;
                    }
                    catch
                    {
                        Thread.Sleep(100);
                        try
                        {
                            worldObjectEntry.MarketValue = map.wealthWatcher.WealthTotal;
                        }
                        catch
                        {
                        }
                    }

                    worldObjectEntry.MarketValuePawn = 0;

                    //Loger.Log("Client TestBagSD 015");
                    List <Pawn> ps;
                    if (!cacheColonists.TryGetValue(map, out ps))
                    {
                        var mapPawnsA = new Pawn[map.mapPawns.AllPawnsSpawned.Count];
                        map.mapPawns.AllPawnsSpawned.CopyTo(mapPawnsA);

                        ps = mapPawnsA.Where(p => p.Faction == Faction.OfPlayer && p.RaceProps.Humanlike).ToList();
                        cacheColonists[map] = ps;
                    }

                    //Loger.Log("Client TestBagSD 016");
                    foreach (Pawn current in ps)
                    {
                        worldObjectEntry.MarketValuePawn += current.MarketValue;

                        GameProgressAdd(gameProgress, current);
                    }
                    //Loger.Log("Client TestBagSD 017");
                    //Loger.Log("Map things "+ worldObjectEntry.MarketValue + " pawns " + worldObjectEntry.MarketValuePawn);
                }
            }
            //Loger.Log("Client TestBagSD 018");

            WorldObjectEntry storeWO;

            if (WorldObjectEntrys.TryGetValue(worldObject.ID, out storeWO))
            {
                //если серверу приходит объект без данного ServerId, значит это наш новый объект (кроме первого запроса, т.к. не было ещё загрузки)
                worldObjectEntry.ServerId = storeWO.ServerId;
            }
            //Loger.Log("Client TestBagSD 019");

            return(worldObjectEntry);
        }