Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using RimWorld;
  7. using Verse;
  8.  
  9. namespace FerrexBusiness
  10. {
  11.     public class FRBShip : PassingShip, ITrader, IThingHolder
  12.     {
  13.         public TraderKindDef def;
  14.  
  15.         private ThingOwner things;
  16.  
  17.         private List<Pawn> soldPrisoners = new List<Pawn>();
  18.  
  19.         private int randomPriceFactorSeed = -1;
  20.  
  21.         private static List<string> tmpExtantNames = new List<string>();
  22.  
  23.         public override string FullTitle
  24.         {
  25.             get
  26.             {
  27.                 return this.name + " (" + this.def.label + ")";
  28.             }
  29.         }
  30.  
  31.         public int Silver
  32.         {
  33.             get
  34.             {
  35.                 return this.CountHeldOf(ThingDefOf.Silver, null);
  36.             }
  37.         }
  38.  
  39.         public IThingHolder ParentHolder
  40.         {
  41.             get
  42.             {
  43.                 return base.Map;
  44.             }
  45.         }
  46.  
  47.         public TraderKindDef TraderKind
  48.         {
  49.             get
  50.             {
  51.                 return this.def;
  52.             }
  53.         }
  54.  
  55.         public int RandomPriceFactorSeed
  56.         {
  57.             get
  58.             {
  59.                 return this.randomPriceFactorSeed;
  60.             }
  61.         }
  62.  
  63.         public string TraderName
  64.         {
  65.             get
  66.             {
  67.                 return this.name;
  68.             }
  69.         }
  70.  
  71.         public bool CanTradeNow
  72.         {
  73.             get
  74.             {
  75.                 return !base.Departed;
  76.             }
  77.         }
  78.  
  79.         public float TradePriceImprovementOffsetForPlayer
  80.         {
  81.             get
  82.             {
  83.                 return 0f;
  84.             }
  85.         }
  86.  
  87.         public Faction Faction
  88.         {
  89.             get
  90.             {
  91.                 return null;
  92.             }
  93.         }
  94.  
  95.         public IEnumerable<Thing> Goods
  96.         {
  97.             get
  98.             {
  99.                 for (int i = 0; i < this.things.Count; i++)
  100.                 {
  101.                     Pawn p = this.things[i] as Pawn;
  102.                     if (p == null || !this.soldPrisoners.Contains(p))
  103.                     {
  104.                         yield return this.things[i];
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.  
  110.         public FRBShip()
  111.         {}
  112.             public FRBShip(TraderKindDef def)
  113.             {
  114.                 this.def = def;
  115.                 this.things = new ThingOwner<Thing>(this);
  116.                 FRBShip.tmpExtantNames.Clear();
  117.                 List<Map> maps = Find.Maps;
  118.                 for (int i = 0; i < maps.Count; i++)
  119.                 {
  120.                     FRBShip.tmpExtantNames.AddRange(from x in maps[i].passingShipManager.passingShips
  121.                                                       select x.name);
  122.                 }
  123.                 this.name = NameGenerator.GenerateName(RulePackDefOf.NamerTraderGeneral, FRBShip.tmpExtantNames, false);
  124.                 this.randomPriceFactorSeed = Rand.RangeInclusive(1, 10000000);
  125.                 this.loadID = Find.World.uniqueIDsManager.GetNextPassingShipID();
  126.             }
  127.  
  128.         [DebuggerHidden]
  129.         public IEnumerable<Thing> ColonyThingsWillingToBuy(Pawn playerNegotiator)
  130.         {
  131.             foreach (Thing t in TradeUtility.AllLaunchableThings(base.Map))
  132.             {
  133.                 yield return t;
  134.             }
  135.             foreach (Pawn p in TradeUtility.AllSellableColonyPawns(base.Map))
  136.             {
  137.                 yield return p;
  138.             }
  139.         }
  140.  
  141.         public void GenerateThings()
  142.         {
  143.             ItemCollectionGeneratorParams parms = default(ItemCollectionGeneratorParams);
  144.             parms.traderDef = this.def;
  145.             parms.forTile = base.Map.Tile;
  146.             this.things.TryAddRange(ItemCollectionGeneratorDefOf.TraderStock.Worker.Generate(parms), true);
  147.         }
  148.  
  149.         public override void PassingShipTick()
  150.         {
  151.             base.PassingShipTick();
  152.             for (int i = this.things.Count - 1; i >= 0; i--)
  153.             {
  154.                 Pawn pawn = this.things[i] as Pawn;
  155.                 if (pawn != null)
  156.                 {
  157.                     pawn.Tick();
  158.                     if (pawn.Dead)
  159.                     {
  160.                         this.things.Remove(pawn);
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.  
  166.         public override void ExposeData()
  167.         {
  168.             base.ExposeData();
  169.             Scribe_Defs.Look<TraderKindDef>(ref this.def, "def");
  170.             Scribe_Deep.Look<ThingOwner>(ref this.things, "things", new object[]
  171.             {
  172.                 this
  173.             });
  174.             Scribe_Collections.Look<Pawn>(ref this.soldPrisoners, "soldPrisoners", LookMode.Reference, new object[0]);
  175.             Scribe_Values.Look<int>(ref this.randomPriceFactorSeed, "randomPriceFactorSeed", 0, false);
  176.             if (Scribe.mode == LoadSaveMode.PostLoadInit)
  177.             {
  178.                 this.soldPrisoners.RemoveAll((Pawn x) => x == null);
  179.             }
  180.         }
  181.  
  182.         public override void TryOpenComms(Pawn negotiator)
  183.         {
  184.             if (!this.CanTradeNow)
  185.             {
  186.                 return;
  187.             }
  188.             Find.WindowStack.Add(new Dialog_Trade(negotiator, this));
  189.             LessonAutoActivator.TeachOpportunity(ConceptDefOf.BuildOrbitalTradeBeacon, OpportunityType.Critical);
  190.             string empty = string.Empty;
  191.             string empty2 = string.Empty;
  192.             PawnRelationUtility.Notify_PawnsSeenByPlayer(this.Goods.OfType<Pawn>(), ref empty, ref empty2, "LetterRelatedPawnsTradeShip".Translate(), false);
  193.             if (!empty2.NullOrEmpty())
  194.             {
  195.                 Find.LetterStack.ReceiveLetter(empty, empty2, LetterDefOf.Good, null);
  196.             }
  197.             TutorUtility.DoModalDialogIfNotKnown(ConceptDefOf.TradeGoodsMustBeNearBeacon);
  198.         }
  199.  
  200.         public override void Depart()
  201.         {
  202.             base.Depart();
  203.             this.things.ClearAndDestroyContentsOrPassToWorld(DestroyMode.Vanish);
  204.             this.soldPrisoners.Clear();
  205.         }
  206.  
  207.         public override string GetCallLabel()
  208.         {
  209.             return this.name + " (" + this.def.label + ")";
  210.         }
  211.  
  212.         public int CountHeldOf(ThingDef thingDef, ThingDef stuffDef = null)
  213.         {
  214.             Thing thing = this.HeldThingMatching(thingDef, stuffDef);
  215.             if (thing != null)
  216.             {
  217.                 return thing.stackCount;
  218.             }
  219.             return 0;
  220.         }
  221.    
  222.         public void GiveSoldThingToTrader(Thing toGive, int countToGive, Pawn playerNegotiator)
  223.         {
  224.             Thing thing = toGive.SplitOff(countToGive);
  225.             thing.PreTraded(TradeAction.PlayerSells, playerNegotiator, this);
  226.             Thing thing2 = TradeUtility.ThingFromStockToMergeWith(this, thing);
  227.             if (thing2 != null)
  228.             {
  229.                 if (!thing2.TryAbsorbStack(thing, false))
  230.                 {
  231.                     thing.Destroy(DestroyMode.Vanish);
  232.                 }
  233.             }
  234.             else
  235.             {
  236.                 Pawn pawn = thing as Pawn;
  237.                 if (pawn != null && pawn.RaceProps.Humanlike)
  238.                 {
  239.                     this.soldPrisoners.Add(pawn);
  240.                 }
  241.                 this.things.TryAdd(thing, false);
  242.             }
  243.         }
  244.  
  245.         public void GiveSoldThingToPlayer(Thing toGive, int countToGive, Pawn playerNegotiator)
  246.         {
  247.             Thing thing = toGive.SplitOff(countToGive);
  248.             thing.PreTraded(TradeAction.PlayerBuys, playerNegotiator, this);
  249.             Pawn pawn = thing as Pawn;
  250.             if (pawn != null)
  251.             {
  252.                 this.soldPrisoners.Remove(pawn);
  253.             }
  254.             TradeUtility.SpawnDropPod(DropCellFinder.TradeDropSpot(base.Map), base.Map, thing);
  255.         }
  256.  
  257.         private Thing HeldThingMatching(ThingDef thingDef, ThingDef stuffDef)
  258.         {
  259.             for (int i = 0; i < this.things.Count; i++)
  260.             {
  261.                 if (this.things[i].def == thingDef && this.things[i].Stuff == stuffDef)
  262.                 {
  263.                     return this.things[i];
  264.                 }
  265.             }
  266.             return null;
  267.         }
  268.  
  269.         public void ChangeCountHeldOf(ThingDef thingDef, ThingDef stuffDef, int count)
  270.         {
  271.             Thing thing = this.HeldThingMatching(thingDef, stuffDef);
  272.             if (thing == null)
  273.             {
  274.                 Log.Error("Changing count of thing trader doesn't have: " + thingDef);
  275.             }
  276.             thing.stackCount += count;
  277.         }
  278.  
  279.         public override string ToString()
  280.         {
  281.             return this.FullTitle;
  282.         }
  283.  
  284.         public ThingOwner GetDirectlyHeldThings()
  285.         {
  286.             return this.things;
  287.         }
  288.  
  289.         public void GetChildHolders(List<IThingHolder> outChildren)
  290.         {
  291.             ThingOwnerUtility.AppendThingHoldersFromThings(outChildren, this.GetDirectlyHeldThings());
  292.         }
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement