-
Notifications
You must be signed in to change notification settings - Fork 2
/
FreightCartMob.cs
1950 lines (1859 loc) · 93.4 KB
/
FreightCartMob.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System;
using UnityEngine;
using FortressCraft.Community.Utilities;
public class FreightCartMob : MobEntity
{
public int mnMaxStorage = 25;
private float mrSpeedScalar = 1f;
public float mrSpeed = 1f;
//public List<CubeCoord> mVisitedLocations = new List<CubeCoord>();
private float mrTargetSpeed;
public int mnUsedStorage;
//private ItemBase[] maStoredItems;
public FreightCartMob.eMinecartType meType;
private float mrLoadTimer;
//private MinecartStation mStation;
private FreightCartStation FCStation;
private string NetworkID;
public FreightCartMob.eLoadState meLoadState;
private System.Random rand;
private static int MineCartID;
public float mrVisualLoadTimer;
public ConcurrentQueue<Vector8> maCartPositions;
private int SlopeCatch = -1; //Adding this to try to catch odd derailing carts
private float lrTimer;
private int mnUpdatesPerTick;
private float mrSpeedPerTick;
private int mnUpdates;
private int mnCurrentUpdateTick;
private float mrSpeedBoost;
private Segment mUnderSegment;
private Segment mPrevGetSeg;
private bool SlowTrack;
public static ushort FreightStationType = ModManager.mModMappings.CubesByKey["steveman0.FreightCartStation"].CubeType;
public static ushort TourStationType = ModManager.mModMappings.CubesByKey["steveman0.TourCartStation"].CubeType;
public static ushort JunctionType = ModManager.mModMappings.CubesByKey["steveman0.TrackJunction"].CubeType;
public static ushort JunctionVal = ModManager.mModMappings.CubesByKey["steveman0.TrackJunction"].ValuesByKey["steveman0.TrackJunctionStandard"].Value;
public static ushort ScrapJunctionVal = ModManager.mModMappings.CubesByKey["steveman0.TrackJunction"].ValuesByKey["steveman0.ScrapTrackJunction"].Value;
public static ushort ScrapTrackType = ModManager.mModMappings.CubesByKey["steveman0.ScrapTrack"].CubeType;
public static ushort ScrapStraightVal = ModManager.mModMappings.CubesByKey["steveman0.ScrapTrack"].ValuesByKey["steveman0.ScrapTrackStraight"].Value;
public static ushort ScrapCornerVal = ModManager.mModMappings.CubesByKey["steveman0.ScrapTrack"].ValuesByKey["steveman0.ScrapTrackCorner"].Value;
public static ushort ScrapSlopeVal = ModManager.mModMappings.CubesByKey["steveman0.ScrapTrack"].ValuesByKey["steveman0.ScrapTrackSlope"].Value;
public static ushort FreightStationValue = 0;
public Dictionary<string, MachineInventory> LocalInventory = new Dictionary<string, MachineInventory>();
public MachineInventory TransferInventory;
public MassInventory CurrentNetworkStock;
List<KeyValuePair<ItemBase, int>> StationOfferings;
public bool LoadCheckIn = false;
public bool LoadCheckOut = false;
public bool FullEscape = false;
public bool EmptyEscape = false;
public bool CartCheckin = false;
private bool CartLoadIn = false;
public bool CartQueuelock = false;
private bool TransitCheckIn = true;
public bool OffloadingExcess = true;
private List<ItemBase> CheckInList = new List<ItemBase>();
public ItemBase OreFreighterItem;
public FreightTrackJunction LastJunction;
public FreightTrackJunction NextJunction;
public FreightTrackJunction DestinationJunction;
public Stack<FreightTrackJunction> JunctionRoute = new Stack<FreightTrackJunction>();
public int DestinationDirection = -1;
public FreightCartStation AssignedStation;
public static bool ReadNullDebug = false;
public FreightCartMob(FreightCartMob.eMinecartType leType, ModCreateMobParameters modmobtype)
: base(modmobtype, SpawnableObjectEnum.Minecart_T3)
{
++FreightCartMob.MineCartID;
this.rand = new System.Random();
this.mrSpeed = 0.17345f;
this.mrSpeedScalar = 1f;
this.meType = leType;
this.mType = MobType.Mod;
this.SetStatsFromType();
this.mrTargetSpeed = (float)this.rand.Next(95, 105) / 100f * this.mrSpeedScalar;
if (WorldScript.meGameMode == eGameMode.eCreative)
this.mrTargetSpeed = 5f;
this.ConfigSpeedTicks();
this.mnHealth = 1;
//this.maStoredItems = new ItemBase[100];
this.meLoadState = FreightCartMob.eLoadState.eTravelling;
this.mbHostile = false;
this.maCartPositions = new ConcurrentQueue<Vector8>();
this.DoThreadedRaycastVis = true;
this.MaxRayCastVis = 64f;
}
public override void SpawnGameObject()
{
if (this.meType == FreightCartMob.eMinecartType.FreightCart_T1 || this.meType == eMinecartType.OreFreighter_T1)
this.mObjectType = SpawnableObjectEnum.Minecart_T1;
if (this.meType == FreightCartMob.eMinecartType.FreightCart_T2 || this.meType == eMinecartType.OreFreighter_T2)
this.mObjectType = SpawnableObjectEnum.Minecart_T2;
if (this.meType == FreightCartMob.eMinecartType.FreightCart_T3 || this.meType == eMinecartType.OreFreighter_T3)
this.mObjectType = SpawnableObjectEnum.Minecart_T3;
if (this.meType == FreightCartMob.eMinecartType.FreightCart_T4 || this.meType == eMinecartType.OreFreighter_T4)
this.mObjectType = SpawnableObjectEnum.Minecart_T4;
if (this.meType == FreightCartMob.eMinecartType.FreightCartTour || this.meType == eMinecartType.FreightCartTourBasic)
this.mObjectType = SpawnableObjectEnum.Minecart_T10;
if (this.meType == FreightCartMob.eMinecartType.FreightCartMK1)
this.mObjectType = SpawnableObjectEnum.Minecart_T4;
base.SpawnGameObject();
if (this.mWrapper.mGameObjectList == null && !CartQueuelock)
{
//this.mWrapper.mGameObjectList = new List<GameObject>();
ManagerSync.CartLoader.Enqueue(this);
this.CartQueuelock = true; // Prevent requeueing for another script if the manager gets bogged down by many cart requests
if (!this.CartLoadIn)
this.CartLoadIn = true;
else
ManagerSync.instance.CartCounter--;
}
}
private void SetStatsFromType()
{
if (this.meType == FreightCartMob.eMinecartType.ScrapCartMK1)
{
this.mrSpeedScalar = 0.5f;
this.mnMaxStorage = 5;
this.IsScrapCart = true;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == eMinecartType.ScrapOreFreighterMK1)
{
this.mrSpeedScalar = 0.5f;
this.mnMaxStorage = 50;
this.IsScrapCart = true;
this.IsOreFreighter = true;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == FreightCartMob.eMinecartType.FreightCart_T1)
{
this.mrSpeedScalar = 1f;
this.mnMaxStorage = 25;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == FreightCartMob.eMinecartType.FreightCart_T2)
{
this.mrSpeedScalar = 2f;
this.mnMaxStorage = 25;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == FreightCartMob.eMinecartType.FreightCart_T3)
{
this.mrSpeedScalar = 1f;
this.mnMaxStorage = 50;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == FreightCartMob.eMinecartType.FreightCart_T4)
{
this.mrSpeedScalar = 2f;
this.mnMaxStorage = 50;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == FreightCartMob.eMinecartType.FreightCartMK1)
{
this.mrSpeedScalar = 2f;
this.mnMaxStorage = 50;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == FreightCartMob.eMinecartType.FreightCartTour)
{
this.mrSpeedScalar = 4f;
this.mnMaxStorage = 0;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == FreightCartMob.eMinecartType.FreightCartTourBasic)
{
this.mrSpeedScalar = 2f;
this.mnMaxStorage = 0;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == FreightCartMob.eMinecartType.OreFreighter_T1)
{
this.mrSpeedScalar = 1f;
this.mnMaxStorage = 250;
this.IsOreFreighter = true;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == FreightCartMob.eMinecartType.OreFreighter_T2)
{
this.mrSpeedScalar = 2f;
this.mnMaxStorage = 250;
this.IsOreFreighter = true;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == FreightCartMob.eMinecartType.OreFreighter_T3)
{
this.mrSpeedScalar = 1f;
this.mnMaxStorage = 500;
this.IsOreFreighter = true;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
if (this.meType == FreightCartMob.eMinecartType.OreFreighter_T4)
{
this.mrSpeedScalar = 2f;
this.mnMaxStorage = 500;
this.IsOreFreighter = true;
this.TransferInventory = new MachineInventory(this, this.mnMaxStorage);
}
}
public bool IsOreFreighter
{
get; private set;
}
public bool IsScrapCart
{
get; private set;
}
private void ConfigSpeedTicks()
{
this.mnUpdatesPerTick = (int)(this.mrSpeed * 0.333000004291534);
++this.mnUpdatesPerTick;
if (this.mnUpdatesPerTick < 1)
this.mnUpdatesPerTick = 1;
this.mrSpeedPerTick = this.mrSpeed / (float)this.mnUpdatesPerTick;
}
public override int TakeDamage(int lnDamage)
{
return -1;
}
public override void MobUpdate()
{
if (FreightCartMod.CartCheckin && !this.CartCheckin)
{
FreightCartMod.LiveCarts++;
this.CartCheckin = true;
}
else if (!FreightCartMod.CartCheckin && this.CartCheckin)
this.CartCheckin = false;
this.TransitCheck();
//if (this.AssignedStation == null)
//{
//}
if (this.mnUsedStorage < 0)
this.RecountInventory();
++this.mnUpdates;
if (this.SlopeCatch - this.mnUpdates > 5)
this.SlopeCatch = -1;
this.UpdatePlayerDistanceInfo();
this.lrTimer -= MobUpdateThread.mrPreviousUpdateTimeStep;
if (this.mLook.y > 0.0)
{
float num = 0.5f * this.mrSpeedScalar;
if (this.mrSpeed > (double)num)
{
this.mrSpeed *= 0.5f;
if (this.mrSpeed < (double)num)
this.mrSpeed = num;
this.ConfigSpeedTicks();
}
}
if (this.mLook.y < 0.0 && this.mrSpeed < 2.0 * this.mrSpeedScalar)
{
this.mrSpeed += MobUpdateThread.mrPreviousUpdateTimeStep * (1.5f * this.mrSpeedScalar);
this.ConfigSpeedTicks();
}
if (this.SlowTrack && !this.IsScrapCart) // Slow down regular carts on scrap track
{
float speedfactor = 0.4f * this.mrSpeedScalar;
if (this.mrSpeed > (double)speedfactor)
{
this.mrSpeed *= 0.4f;
if (this.mrSpeed < (double)speedfactor)
this.mrSpeed = speedfactor;
this.ConfigSpeedTicks();
}
}
this.UpdateLoadStatus();
float num1 = this.mrTargetSpeed * (1f + this.mrSpeedBoost);
float num2 = MobUpdateThread.mrPreviousUpdateTimeStep / 5f;
if (this.mrSpeedBoost > 0.5)
num2 = MobUpdateThread.mrPreviousUpdateTimeStep;
this.mrSpeed += (num1 - this.mrSpeed) * num2;
this.mrSpeedBoost *= 0.99f;
if (this.mrTargetSpeed == 0.0)
this.mrSpeed *= 0.0f;
this.ConfigSpeedTicks();
for (this.mnCurrentUpdateTick = 0; this.mnCurrentUpdateTick < this.mnUpdatesPerTick; ++this.mnCurrentUpdateTick)
this.UpdateMove(this.mrSpeedPerTick);
}
/// <summary>
/// Registers the current inventory as 'in transit' on the network if the network registry wasn't loaded in the Master when the cart was loaded.
/// Not currently ideal as if the registry is missing for one item it will be skipped and we don't return as the previous ones have already been handled
/// would need to revert previous changes, check that all have valid registries first, or remember which ones still need to be registered as in transit...
/// </summary>
private void TransitCheck()
{
if (!this.TransitCheckIn)
{
bool checkreg = true;
if (this.LocalInventory == null)
{
Debug.LogWarning("FreightCartMob is trying to do transit check prior to having an inventory!");
return;
}
List<string> keys = this.LocalInventory.Keys.ToList();
int count = keys.Count;
for (int m = 0; m < count; m++)
{
string networkid = keys[m];
if (string.IsNullOrEmpty(networkid) || this.LocalInventory[networkid] == null || this.LocalInventory[networkid].Inventory == null)
{
Debug.LogWarning("FreightCartMob TransitCheck found null network or inventory! Networking failure?");
return;
}
MachineInventory checkinv;
if (this.LocalInventory.TryGetValue(networkid, out checkinv))
{
int count2 = checkinv.Inventory.Count;
for (int n = 0; n < count2; n++)
{
ItemBase item = checkinv.Inventory[n];
if (item == null)
{
Debug.LogWarning("FreightCartMob Transit Check got a null item. Suspect data corruption!");
continue;
}
if (FreightCartManager.instance == null)
return;
checkreg = FreightCartManager.instance.NetworkAdd(networkid, item, item.GetAmount());
if (!checkreg)
{
if (n == 0)
return;
continue;
}
}
}
}
this.TransitCheckIn = true;
}
}
private void UpdateLoadStatus()
{
this.mrLoadTimer += MobUpdateThread.mrPreviousUpdateTimeStep;
if (this.FCStation == null && (this.meLoadState == eLoadState.eLoading || this.meLoadState == eLoadState.eUnloading) || (this.FCStation != null && this.FCStation.mbDelete))
{
Debug.LogError((object)"Error, our station went null or player destroyed it while it had a cart!");
this.LeaveStation();
}
// Scrap carts only check load/unload once a second-ish effectively slowing their throughput at a station to 60 items/min.
if (this.IsScrapCart && this.mnUpdates % 5 > 0)
return;
if (this.meLoadState == FreightCartMob.eLoadState.eUnloading)
this.UpdateCartUnload();
if (this.meLoadState == FreightCartMob.eLoadState.eLoading)
this.UpdateCartLoad();
}
private bool HasAttachedConsumer()
{
return (this.FCStation.ConnectedInventory != null || this.FCStation.AttachedInterface != null || this.FCStation.HopperInterface != null);
}
private void UpdateCartUnload()
{
if (string.IsNullOrEmpty(this.NetworkID))
{
this.LeaveStation();
}
else
{
this.mBlockOffset = Vector3.zero;
if (this.FCStation.mValue != FreightStationValue)
Debug.LogError(("Attempted to unload into the wrong type of station! (Expected Freight station val, got " + this.FCStation.mValue));
//if (!this.FCStation.mbWaitForFullLoad && this.mrLoadTimer > 300.0)
// this.LeaveStation();
if (this.TransferInventory.IsEmpty() && this.LoadCheckIn)
{
this.meLoadState = eLoadState.eLoading;
this.LoadCheckIn = false;
this.LoadCheckOut = false;
this.EmptyEscape = true;
}
else
{
//Get the network stock to compare and adjust accordingly to unloading etc.
//if (this.CurrentNetworkStock == null)
// this.CurrentNetworkStock = FreightCartManager.instance.GetNetworkStock(this.FCStation);
if (HasAttachedConsumer() && this.TransferInventory.ItemCount() == 0 && this.LocalInventory.ContainsKey(this.NetworkID) && this.LocalInventory[this.NetworkID].ItemCount() != 0 && !string.IsNullOrEmpty(this.FCStation.NetworkID))
{
List<KeyValuePair<ItemBase, int>> needs = FreightCartManager.instance.GetStationNeeds(this.FCStation);
for (int index = 0; index < needs.Count; index++)
{
this.RetrieveItem(needs[index]);
this.mrVisualLoadTimer = 1f;
}
//Check for all items that aren't needed by the network and transfer them if applicable
if (this.OffloadingExcess && this.FCStation.massStorageCrate != null)
this.RetrieveExcess();
}
if (this.TransferInventory.ItemCount() > 0 && HasAttachedConsumer() && !(this.FCStation.HopperInterface != null && this.FCStation.HopperInterface.Machine.IsFull()))
{
ItemBase deposited = this.TransferInventory.RemoveAnySingle(1);
int remainder = this.FCStation.DepositItem(deposited);
if (remainder == 0)
{
FreightCartManager.instance.NetworkRemove(this.NetworkID, deposited, 1);
this.mnUsedStorage--;
this.mrVisualLoadTimer = 1f;
if (this.mnUsedStorage == 0 && this.OreFreighterItem != null)
this.OreFreighterItem = null;
}
else
{
if (deposited != null && this.FCStation.ConnectedInventory != null)
{
Debug.LogWarning("Station named " + (!String.IsNullOrEmpty(this.FCStation.StationName) ? this.FCStation.StationName : "UNNAMED") + " failed to deposit item " + deposited.ToString() + " into inventory " + (!String.IsNullOrEmpty(this.FCStation.ConnectedInventory.Name) ? this.FCStation.ConnectedInventory.Name : "UNNAMED") + " at FreightCart unload step. Returning to local inventory.");
}
else if (this.FCStation.HopperInterface != null)
Debug.Log("FreightCart attempted to deposit item at FreightInterface but interface was full? We already check for this!");
else
Debug.LogWarning("FreightCartMob failed to deposit item to freight interface most likely!");
if (!string.IsNullOrEmpty(this.NetworkID) && this.LocalInventory.ContainsKey(this.NetworkID))
this.LocalInventory[this.NetworkID].AddItem(deposited.SetAmount(remainder));
else
{
if (!string.IsNullOrEmpty(this.NetworkID))
{
this.LocalInventory.Add(this.NetworkID, new MachineInventory(this, this.mnMaxStorage));
this.LocalInventory[this.NetworkID].AddItem(deposited.SetAmount(remainder));
}
else
Debug.LogWarning("NetworkID is null so we can't restore the item! Initialization error?");
}
}
}
else if (this.TransferInventory.ItemCount() > 0 && !(this.FCStation.HopperInterface != null && this.FCStation.HopperInterface.Machine.IsFull()))
{
Debug.LogWarning("Freight cart still had items to transfer but lost mass storage! Remaining items: " + this.TransferInventory.ItemCount());
}
this.FCStation.mrCartOnUs = 1f;
//this.FCStation.mrLastCartLeavTimer = this.mrLoadTimer;
this.LoadCheckIn = true;
}
}
}
private void UpdateCartLoad()
{
if (string.IsNullOrEmpty(this.FCStation.NetworkID))
{
this.FullEscape = false;
this.LeaveStation();
}
else
{
this.mBlockOffset = Vector3.zero;
if ((int)this.FCStation.mValue != FreightStationValue)
Debug.LogError(("Attempted to unload into the wrong type of station! (Expected FreightStationValue, got " + this.FCStation.mValue));
if ((this.mnUsedStorage == this.mnMaxStorage || this.LoadCheckOut || this.mrLoadTimer > 30.0f) && !(FCStation.mbWaitForFullLoad && this.mnUsedStorage < this.mnMaxStorage))
{
//if (this.LoadCheckOut)
// FloatingCombatTextManager.instance.QueueText(this.mnX, this.mnY + 2L, this.mnZ, 1f, "LoadCheckOut", Color.red, 1f, 64f);
//if (this.mrLoadTimer > 30.0)
// FloatingCombatTextManager.instance.QueueText(this.mnX, this.mnY + 2L, this.mnZ, 1f, "LoadTimer", Color.red, 1f, 64f);
//if (this.mnUsedStorage == this.mnMaxStorage)
// FloatingCombatTextManager.instance.QueueText(this.mnX, this.mnY + 2L, this.mnZ, 1f, "MaxStorage", Color.red, 1f, 64f);
this.LeaveStation();
}
else
{
//if (this.CurrentNetworkStock == null)
// this.CurrentNetworkStock = FreightCartManager.instance.GetNetworkStock(this.FCStation);
this.FCStation.mrCartOnUs = 1f;
//this.FCStation.mrLastCartLeavTimer = this.mrLoadTimer;
// Refresh the offerings for the wait for full case in case items change while we're waiting
if (HasAttachedConsumer() && this.StationOfferings == null || this.mrLoadTimer > 35)
{
this.StationOfferings = FreightCartManager.instance.GetStationOfferings(this.FCStation);
this.mrLoadTimer = 0;
}
if (this.StationOfferings != null && this.StationOfferings.Count > 0)
{
ItemBase item = this.GetNextOffer();
ItemBase itemout = null;
if (item != null)
{
this.FCStation.WithdrawItem(item, out itemout);
if (itemout != null)
{
if (!this.LocalInventory.ContainsKey(this.NetworkID))
this.LocalInventory.Add(this.NetworkID, new MachineInventory(this, this.mnMaxStorage));
ItemBase remainder = this.LocalInventory[FCStation.NetworkID].AddItem(itemout);
if (remainder == null)
{
//this.CurrentNetworkStock.Inventory[item]++;
FreightCartManager.instance.NetworkAdd(this.NetworkID, item, 1);
this.mnUsedStorage++;
if (this.IsOreFreighter)
this.OreFreighterItem = item;
this.mrVisualLoadTimer = 1f;
this.MarkDirtyDelayed();
}
else
{
Debug.LogWarning("Freight cart withdrawal failed? Trying to collect item: " + remainder.ToString() + " with cart inventory of: " + this.mnUsedStorage.ToString() + " and inventory count: " + this.LocalInventory[FCStation.NetworkID].ItemCount());
if (this.mnUsedStorage < this.LocalInventory[FCStation.NetworkID].ItemCount())
this.RecountInventory();
this.FCStation.DepositItem(remainder);
}
}
else
{
this.LoadCheckOut = true;
//FloatingCombatTextManager.instance.QueueText(this.mnX, this.mnY + 3L, this.mnZ, 1f, "null itemout", Color.red, 1f, 64f);
}
}
else
{
this.LoadCheckOut = true;
//FloatingCombatTextManager.instance.QueueText(this.mnX, this.mnY + 3L, this.mnZ, 1f, "null next offer", Color.red, 1f, 64f);
}
}
else
{
this.LoadCheckOut = true;
//FloatingCombatTextManager.instance.QueueText(this.mnX, this.mnY + 3L, this.mnZ, 1f, "station has no offers list", Color.red, 1f, 64f);
}
//if (this.StoreItem(this.FCStation.mProferredItem))
//{
// this.mrVisualLoadTimer = 1f;
// this.FCStation.mProferredItem = (ItemBase)null;
// if (this.mnUsedStorage == this.mnMaxStorage)
// this.LeaveStation();
// this.mrLoadTimer = 0.0f;
//}
//else
// this.LeaveStation();
}
}
}
private void RecountInventory()
{
if (this.LocalInventory == null || this.LocalInventory.Count == 0)
return;
var keys = this.LocalInventory.Keys.ToList();
int itemcount = 0;
for (int n = 0; n < keys.Count; n++)
itemcount += this.LocalInventory[keys[n]].ItemCount();
this.mnUsedStorage = itemcount;
Debug.LogWarning("FreightCartMob Used Storage had to be updated for missing item count!");
}
private ItemBase GetNextOffer()
{
if (!WorldScript.mbIsServer)
return null;
//Start on a random item to help balance input
int getitem = this.rand.Next(0, this.StationOfferings.Count);
ItemBase item = this.StationOfferings[getitem].Key;
if (FreightCartManager.instance.NetworkNeeds(this.NetworkID, item) > 0 && this.FreighterAccepted(item))
{
return item.NewInstance().SetAmount(1);
}
else
{
int check;
for (int index = getitem + 1; index < this.StationOfferings.Count + getitem; ++index)
{
check = index;
if (index >= this.StationOfferings.Count)
check -= this.StationOfferings.Count;
item = this.StationOfferings[check].Key;
if (!this.FreighterAccepted(item))
continue;
if (FreightCartManager.instance.NetworkNeeds(this.NetworkID, item) > 0)
return item.NewInstance().SetAmount(1);
}
}
this.FullEscape = true;
return null;
}
private bool FreighterAccepted(ItemBase item)
{
if (!this.IsOreFreighter)
return true;
else if (this.OreFreighterItem != null && item.Compare(this.OreFreighterItem))
return true;
else if (this.OreFreighterItem == null && item.mType == ItemType.ItemCubeStack && CubeHelper.IsOre((item as ItemCubeStack).mCubeType))
return true;
return false;
}
private void RetrieveItem(KeyValuePair<ItemBase, int> keypair)
{
ItemBase item = keypair.Key;
int amount = keypair.Value;
this.MarkDirtyDelayed();
if (this.LocalInventory.ContainsKey(this.NetworkID))
{
if (this.LocalInventory[this.NetworkID].IsEmpty())
{
Debug.LogError("Error, can't retrieve item from Minecart as it's empty.");
this.LocalInventory.Remove(this.NetworkID);
}
this.LocalInventory[this.NetworkID].RemoveWhiteList(ref this.TransferInventory.Inventory, item, this.TransferInventory.StorageCapacity, amount);
if (this.LocalInventory[this.NetworkID].IsEmpty())
{
this.LocalInventory.Remove(this.NetworkID);
if (this.IsOreFreighter && this.mnUsedStorage <= 0)
this.OreFreighterItem = null;
}
}
}
private void RetrieveExcess()
{
int count = 0;
ItemBase item = null;
if (this.LocalInventory.ContainsKey(this.NetworkID))
{
count = this.LocalInventory[this.NetworkID].ItemCount();
for (int n = 0; n < this.LocalInventory[this.NetworkID].Inventory.Count; n++)
{
ItemBase item2 = this.LocalInventory[this.NetworkID].Inventory[n];
if (FreightCartManager.instance.RegistryDeficit(this.NetworkID, item) == 0)
{
List<FreightRegistry> reg = FreightCartManager.instance.GetFreightEntries(this.NetworkID, this.FCStation.massStorageCrate);
if (reg.Exists(x => x.FreightItem.Compare(item)))
{
item = item2;
this.LocalInventory[this.NetworkID].RemoveWhiteList(ref this.TransferInventory.Inventory, item, this.TransferInventory.StorageCapacity, item.GetAmount());
if (this.LocalInventory[this.NetworkID].IsEmpty())
{
this.LocalInventory.Remove(this.NetworkID);
break;
}
}
}
}
//if (this.LocalInventory.ContainsKey(this.NetworkID))
// Debug.LogWarning("FreightCartMob offloaded " + (count - this.LocalInventory[this.NetworkID].ItemCount()).ToString() + " items as excess to " + (string.IsNullOrEmpty(this.FCStation.StationName) ? "UNAMED" : this.FCStation.StationName) + " freight cart station from network: " + this.NetworkID + ". Last item was: " + (item != null ? (item.ToString() + " and deficit: " + FreightCartManager.instance.NetworkNeeds(this.NetworkID, item)) : ""));
//else
// Debug.LogWarning("FreightCartMob offloaded " + count + " items as excess to " + (string.IsNullOrEmpty(this.FCStation.StationName) ? "UNAMED" : this.FCStation.StationName) + " freight cart station" + " freight cart station from network: " + this.NetworkID + ". Last item was: " + (item != null ? (item.ToString() + " and deficit: " + FreightCartManager.instance.NetworkNeeds(this.NetworkID, item)) : ""));
}
this.OffloadingExcess = false;
}
private string GetStorageString()
{
int num1 = 0;
string str = string.Empty;
int num2 = -1;
if (string.IsNullOrEmpty(this.NetworkID) || !this.LocalInventory.ContainsKey(this.NetworkID))
return "";
for (int index = 0; index < this.LocalInventory[this.NetworkID].Inventory.Count; ++index)
{
if (this.LocalInventory[this.NetworkID].Inventory[index] != null)
{
if (str == string.Empty)
{
str = ItemManager.GetItemName(this.LocalInventory[this.NetworkID].Inventory[index]);
num2 = this.LocalInventory[this.NetworkID].Inventory[index].mType != ItemType.ItemCubeStack ? this.LocalInventory[this.NetworkID].Inventory[index].mnItemID : (this.LocalInventory[this.NetworkID].Inventory[index] as ItemCubeStack).mCubeType;
num1 = this.LocalInventory[this.NetworkID].Inventory[index].GetAmount();
}
}
}
//for (int index = 0; index < this.mnMaxStorage; ++index)
//{
// if (this.maStoredItems[index] != null)
// {
// if (str == string.Empty)
// {
// str = ItemManager.GetItemName(this.maStoredItems[index]);
// num2 = this.maStoredItems[index].mType != ItemType.ItemCubeStack ? this.maStoredItems[index].mnItemID : (int)(this.maStoredItems[index] as ItemCubeStack).mCubeType;
// }
// if (this.maStoredItems[index].mType == ItemType.ItemCubeStack)
// {
// if ((int)(this.maStoredItems[index] as ItemCubeStack).mCubeType == num2)
// ++num1;
// }
// else if (this.maStoredItems[index].mnItemID == num2)
// ++num1;
// }
//}
if (num2 == -1)
return "Nothing";
return num1 + "x " + str;
}
//private bool StoreItem(ItemBase lItem)
//{
// this.MarkDirtyDelayed();
// this.mnUsedStorage = this.LocalInventory.ItemCount();
// ItemBase remainder = this.LocalInventory.AddItem(lItem);
// if (remainder == null)
// return true;
// //for (int index = 0; index < this.mnMaxStorage; ++index)
// //{
// // if (this.maStoredItems[index] == null)
// // {
// // this.maStoredItems[index] = lItem;
// // return true;
// // }
// //}
// return false;
//}
private void LeaveStation()
{
if (this.FCStation != null)
{
this.FCStation.mrCartOnUs = -1f;
this.FCStation = (FreightCartStation)null;
}
if (this.mDotWithPlayerForwards > 0.5 && this.mDistanceToPlayer < 5.0 && (this.meLoadState == FreightCartMob.eLoadState.eLoading && this.mnUsedStorage > 0) && this.mrSpeed < 0.100000001490116)
FloatingCombatTextManager.instance.QueueText(this.mnX, this.mnY + 1L, this.mnZ, 1f, "Loaded " + this.mnUsedStorage.ToString() + "x Freight", Color.cyan, 1f, 64f);
this.meLoadState = FreightCartMob.eLoadState.eTravelling;
this.mrTargetSpeed = (float)this.rand.Next(95, 105) / 100f * this.mrSpeedScalar;
this.mbNetworkUpdateRequested = true;
this.LoadCheckIn = false;
this.LoadCheckOut = false;
this.StationOfferings = null;
this.CurrentNetworkStock = null;
this.NetworkID = null;
}
private void StoreNewTrackPiece()
{
}
private void MoveToNewVox(int lnXMove, int lnYMove, int lnZMove)
{
this.mrLoadTimer = 0.0f;
this.mBlockOffset.x -= lnXMove;
this.mBlockOffset.y -= lnYMove;
this.mBlockOffset.z -= lnZMove;
ushort lValue1 = 0;
byte lFlags1 = 0;
ushort type = this.GetCube(this.mnX + lnXMove, this.mnY + lnYMove - 1L, this.mnZ + lnZMove, out lValue1, out lFlags1);
this.mUnderSegment = this.mPrevGetSeg;
ushort lValue2 = 0;
byte lFlags2 = 0;
ushort cube = this.GetCube(this.mnX + (long)lnXMove, this.mnY + lnYMove - 2L, this.mnZ + lnZMove, out lValue2, out lFlags2);
if (type == 0 || cube == 0)
{
this.mBlockOffset.x += lnXMove;
this.mBlockOffset.y += lnYMove;
this.mBlockOffset.z += lnZMove;
FreightCartMob minecartEntity = this;
Vector3 vector3 = minecartEntity.mBlockOffset - this.mLook * 0.333f * this.mrSpeedPerTick;
minecartEntity.mBlockOffset = vector3;
this.mnUpdatesPerTick = 0;
if (type == 0)
CentralPowerHub.mnMinecartY = this.mnY + lnYMove - 1L;
if (cube == 0)
CentralPowerHub.mnMinecartY = this.mnY + lnYMove - 2L;
CentralPowerHub.mnMinecartX = this.mnX + lnXMove;
CentralPowerHub.mnMinecartZ = this.mnZ + lnZMove;
}
else if (!MobManager.instance.MoveMob(this, this.mnX + lnXMove, this.mnY + lnYMove, this.mnZ + lnZMove, this.mBlockOffset))
{
this.mBlockOffset.x += lnXMove;
this.mBlockOffset.y += lnYMove;
this.mBlockOffset.z += lnZMove;
FreightCartMob minecartEntity = this;
Vector3 vector3 = minecartEntity.mBlockOffset - this.mLook * 0.2f * this.mrSpeedPerTick;
minecartEntity.mBlockOffset = vector3;
CentralPowerHub.mnMinecartX = this.mnX + lnXMove;
CentralPowerHub.mnMinecartY = this.mnY + lnYMove;
CentralPowerHub.mnMinecartZ = this.mnZ + lnZMove;
Debug.LogWarning("Minecart failed to move - requesting segment page in!");
this.mnUpdatesPerTick = 0;
}
else
{
if (this.mbMovedToNewSegment)
{
this.mbMovedToNewSegment = false;
CentralPowerHub.mnMinecartX = this.mnX;
CentralPowerHub.mnMinecartY = this.mnY;
CentralPowerHub.mnMinecartZ = this.mnZ;
}
this.mbNetworkUpdateRequested = true;
this.StoreNewTrackPiece();
if (type == 0)
Debug.LogError("Error, minecart attempted to continue moving into null segment");
if (this.mPrevGetSeg == null)
Debug.LogError(("Error, GetCube returned " + TerrainData.GetNameForValue(type, lValue1) + " but the segment was null!"));
bool flag = false;
if (type == 1)
{
Segment segment = this.mPrevGetSeg;
type = cube;
lFlags1 = lFlags2;
lValue1 = lValue2;
if ((type == 538 && lValue1 == 2) || (type == ScrapTrackType && lValue1 == ScrapSlopeVal))
{
flag = true;
}
else
{
if (type == 0)
Debug.LogError("Error, freightCart has null under segment!");
if (this.mPrevGetSeg == null)
Debug.LogError("Error, prevseg was null!");
if (segment == null)
Debug.LogError("Error, old was null!");
if (this.mPrevGetSeg != segment)
Debug.LogWarning(("FreightCart is looking for a slope, and has had to check across segment boundaries for this![Old/New" + segment.GetName() + " -> " + this.mPrevGetSeg.GetName()));
Debug.LogWarning(("FreightCart hit air but located no underslope!" + TerrainData.GetNameForValue(type, lValue1)));
}
}
if (type == 538 || type == ScrapTrackType)
{
this.SlowTrack = type == 538 ? false : true;
if (this.meLoadState == FreightCartMob.eLoadState.eLoading || this.meLoadState == FreightCartMob.eLoadState.eUnloading || this.meLoadState == FreightCartMob.eLoadState.eCharging)
this.LeaveStation();
this.meLoadState = FreightCartMob.eLoadState.eTravelling;
Vector3 vector3_1 = SegmentCustomRenderer.GetRotationQuaternion(lFlags1) * Vector3.forward;
vector3_1.Normalize();
vector3_1.x = vector3_1.x >= -0.5 ? (vector3_1.x <= 0.5 ? 0.0f : 1f) : -1f;
vector3_1.y = vector3_1.y >= -0.5 ? (vector3_1.y <= 0.5 ? 0.0f : 1f) : -1f;
vector3_1.z = vector3_1.z >= -0.5 ? (vector3_1.z <= 0.5 ? 0.0f : 1f) : -1f;
if (type == 538 && lValue1 == 3)
{
this.mLook = -this.mLook;
}
else
{
if (type == 538 && lValue1 == 4)
{
this.mLook.y = 0.0f;
this.mLook.Normalize();
if (this.mLook == vector3_1)
lValue1 = 0;
else if (this.mnUsedStorage == this.mnMaxStorage || this.FullEscape)
{
lValue1 = 0;
this.FullEscape = false;
}
else
{
this.mLook = -this.mLook;
return;
}
}
if (type == 538 && lValue1 == 5)
{
this.mLook.y = 0.0f;
this.mLook.Normalize();
if (this.mLook == vector3_1)
lValue1 = 0;
else if (this.mnUsedStorage == 0 || this.EmptyEscape)
{
lValue1 = 0;
this.EmptyEscape = false;
}
else
{
this.mLook = -this.mLook;
return;
}
}
if ((type == 538 && lValue1 == 0) || (type == ScrapTrackType && lValue1 == ScrapStraightVal))
{
this.mLook.y = 0.0f;
this.mLook.Normalize();
if (vector3_1.y > 0.5 || vector3_1.y < -0.5)
this.RemoveCart("Track Straight hitting non-straight piece!");
else if (!(this.mLook == vector3_1) && !(this.mLook == -vector3_1))
this.mLook = vector3_1;
}
if ((type == 538 && lValue1 == 1) || (type == ScrapTrackType && lValue1 == ScrapCornerVal))
{
this.mLook.y = 0.0f;
this.mLook.Normalize();
if (this.mLook == new Vector3(-vector3_1.z, 0.0f, vector3_1.x))
this.mLook = new Vector3(this.mLook.z, 0.0f, -this.mLook.x);
else if (vector3_1 == -this.mLook)
this.mLook = new Vector3(-this.mLook.z, 0.0f, this.mLook.x);
else
this.RemoveCart("Minecart headed into invalid corner");
}
if ((type == 538 && lValue1 != 2) || (type == ScrapTrackType && lValue1 != ScrapSlopeVal))
return;
Vector3 vector3_2 = vector3_1;
this.mLook.y = 0.0f;
this.mLook.Normalize();
if (this.mLook == vector3_1)
{
if (flag)
{
this.RemoveCart("Minecart needed a downward slope, but found an upwards slope instead!");
}
else
{
vector3_2.y = 1f;
vector3_2.Normalize();
this.mLook = vector3_2;
this.mBlockOffset.y = this.mBlockOffset.x + this.mBlockOffset.z;
}
}
else if (this.mLook == -vector3_1)
{
this.mLook.y = -1f;
this.mLook.Normalize();
}
else
Debug.LogWarning(string.Concat(new object[4]
{
"Freightcart hit the side of a slope (Look was",
this.mLook,
" but track was ",
vector3_1
}));
}
}
else if (type == 539)
{
this.SlowTrack = false;
Vector3 vector3 = SegmentCustomRenderer.GetRotationQuaternion(lFlags1) * Vector3.forward;
if (this.mLook == vector3 || this.mLook == -vector3)
{
if (lValue1 == 2)
this.mrSpeedBoost = 2f;
if (lValue1 == 3)
{
//MinecartStation minecartStation = this.mUnderSegment.FetchEntity(eSegmentEntity.MinecartControl, this.mnX, this.mnY - 1L, this.mnZ) as MinecartStation;
//if (minecartStation == null)
this.LeaveStation();
//else if ((double)minecartStation.mrCartOnUs <= 0.0)
//{
// this.mStation = minecartStation;
// this.mrTargetSpeed = 0.0f;
// this.meLoadState = FreightCartMob.eLoadState.eUnloading;
// this.mrLoadTimer = 0.0f;
// ++this.mStation.mnCartsSeen;
// if (this.mnUsedStorage > 0 && FloatingCombatTextManager.Initialised)
// FloatingCombatTextManager.instance.QueueText(this.mnX, this.mnY + 1L, this.mnZ, 1f, "Unloading " + this.GetStorageString(), Color.green, 1f, 64f);
//}
}
if (lValue1 != 4 || this.mnUsedStorage == this.mnMaxStorage)
return;
//MinecartStation minecartStation1 = this.mUnderSegment.FetchEntity(eSegmentEntity.MinecartControl, this.mnX, this.mnY - 1L, this.mnZ) as MinecartStation;
//if (minecartStation1 == null)
//{
this.LeaveStation();
//}
//else
//{
// if ((double)minecartStation1.mrCartOnUs > 0.0)
// return;
// this.mStation = minecartStation1;
// this.mrTargetSpeed = 0.0f;
// this.meLoadState = FreightCartMob.eLoadState.eLoading;
// this.mrLoadTimer = 0.0f;
// ++this.mStation.mnCartsSeen;
//}
}
else
this.RemoveCart("MineCart hit sideways control piece! THIS IS UNFORGIVABLE!");
}//Inserted my Freight Cart Station type!
else if (type == FreightStationType)
{
this.SlowTrack = false;
this.FullEscape = false;
Vector3 vector3 = SegmentCustomRenderer.GetRotationQuaternion(lFlags1) * Vector3.forward;
if (this.mLook == vector3 || this.mLook == -vector3)
{
if (lValue1 == FreightStationValue)
{
this.EmptyEscape = false;
FreightCartStation Station = this.mUnderSegment.FetchEntity(eSegmentEntity.Mod, this.mnX, this.mnY - 1L, this.mnZ) as FreightCartStation;
if (Station == null || string.IsNullOrEmpty(Station.NetworkID))