Godcraft

Untitled

Jun 27th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 34.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Segment
  5. {
  6.     public const float SAVE_DELAY = 30f;
  7.     public const int MAX_DROPPED_ITEMS_PER_SEGMENT = 500;
  8.     public const int MAX_MOBS_PER_SEGMENT = 100;
  9.     public SegmentObjectScript gameObject;
  10.     public List<SegmentObjectScript> childObjects;
  11.     public List<FrustrumBase> maFrustrums;
  12.     public FrustrumBase mMasterFrustrum;
  13.     public ZipArchive mArchive;
  14.     public long baseX;
  15.     public long baseY;
  16.     public long baseZ;
  17.     public ushort[] maCubes;
  18.     public CubeData[] maCubeData;
  19.     public List<SegmentEntity>[] mEntities;
  20.     public List<DroppedItemData> mDroppedItems;
  21.     public List<MobEntity> mMobs;
  22.     public Dictionary<int, List<DynamicCustomObject>> mDynamicCustoms;
  23.     public List<GameObjectWrapper> mGameObjects;
  24.     public object mQueueLock;
  25.     public int mnVersion;
  26.     public int mnSaves;
  27.     public float mrTimestampForLastChange;
  28.     public float mrSaveDelay;
  29.     public bool mbProcessing;
  30.     public bool mbDirty;
  31.     public bool mbDoPerlin;
  32.     public bool mbInitialGenerationComplete;
  33.     public bool mbIsEmpty;
  34.     public bool mbSunLit;
  35.     public bool mbHasFaces;
  36.     public bool mbSaveRequested;
  37.     public bool mbLoadRequested;
  38.     public bool mbWasLoaded;
  39.     public bool mbDestroyed;
  40.     public bool mbHasGameObject;
  41.     public bool mbHasCustoms;
  42.     public bool mbOrdersQueueing;
  43.     public bool mbNeedsItemUpdate;
  44.     public bool mbNeedsUnityUpdate;
  45.     public bool mbNeedsLowFrequencyUpdate;
  46.     public bool mbNeedsTimePassedUpdate;
  47.     public bool mbIsQueuedForUpdate;
  48.     public bool mbInLocalFrustrum;
  49.     public bool mbUnityUpdateSuspended;
  50.     public RingBuffer<BuildOrder> maBuildOrders;
  51.     public int mnOrderBlockID;
  52.     public int mnLightStatus;
  53.     public int mnLightEdgeStatus;
  54.     public bool mbRedoLight;
  55.     public bool mbRedoSunLight;
  56.     public bool mbEverHadLightingPass;
  57.     public bool mbLightingDirty;
  58.     public bool mbGraphicsDirty;
  59.     public int mnDebugSegmentID;
  60.     private static int debugSegmentCounter;
  61.     public SegmentFluid mFluid;
  62.     public bool mbHasFluid;
  63.     private bool lbFoundFluid;
  64.     private int TicksWithNoFluidMovement;
  65.     private int mnFluidMoved;
  66.     public int FluidSleepTicks;
  67.     public int mnMagmaCount;
  68.     public Segment(long x, long y, long z)
  69.     {
  70.         if (WorldScript.mbIsServer)
  71.         {
  72.             this.maFrustrums = new List<FrustrumBase>(64);
  73.         }
  74.         this.baseX = x;
  75.         this.baseY = y;
  76.         this.baseZ = z;
  77.         this.mnVersion = 0;
  78.         this.mnSaves = 0;
  79.         this.mbDirty = false;
  80.         this.mbDestroyed = false;
  81.         this.mbLightingDirty = false;
  82.         this.mbRedoLight = false;
  83.         this.mnLightStatus = 0;
  84.         this.mbSaveRequested = false;
  85.         this.mbHasFaces = false;
  86.         this.mbHasGameObject = false;
  87.         this.mbHasCustoms = false;
  88.         this.mbIsEmpty = false;
  89.         this.mbOrdersQueueing = false;
  90.         this.mbGraphicsDirty = false;
  91.         this.mEntities = null;
  92.         this.mDynamicCustoms = null;
  93.         this.mGameObjects = null;
  94.         this.mQueueLock = new object();
  95.         this.mnDebugSegmentID = Segment.debugSegmentCounter++;
  96.         this.mFluid = new SegmentFluid();
  97.         this.mFluid.mOwner = this;
  98.     }
  99.     public bool IsSegmentInAGoodState()
  100.     {
  101.         return this.mbInitialGenerationComplete && !this.mbDestroyed;
  102.     }
  103.     public void SetIntialGenerationComplete()
  104.     {
  105.         this.mbInitialGenerationComplete = true;
  106.         this.ScanForFluid();
  107.     }
  108.     public void AddedFluid()
  109.     {
  110.         if (WorldScript.mbIsServer)
  111.         {
  112.             this.FluidSleepTicks = 0;
  113.             this.mbHasFluid = true;
  114.             this.mbNeedsLowFrequencyUpdate = true;
  115.             if (!this.mbIsQueuedForUpdate)
  116.             {
  117.                 WorldScript.instance.mSegmentUpdater.AddSegment(this);
  118.                 this.mbIsQueuedForUpdate = true;
  119.             }
  120.         }
  121.     }
  122.     public void CleanUp()
  123.     {
  124.         this.mbDestroyed = true;
  125.         this.mbLoadRequested = false;
  126.         this.mbDirty = false;
  127.         this.mbGraphicsDirty = false;
  128.         if (this.mbIsQueuedForUpdate)
  129.         {
  130.             WorldScript.instance.mSegmentUpdater.RemoveSegment(this);
  131.         }
  132.         this.DropGameObjects();
  133.         if (this.maCubes != null)
  134.         {
  135.             this.maCubes = null;
  136.             this.maCubeData = null;
  137.         }
  138.         if (this.mEntities != null)
  139.         {
  140.             for (int i = 0; i < 62; i++)
  141.             {
  142.                 List<SegmentEntity> list = this.mEntities[i];
  143.                 if (list != null && list.Count > 0)
  144.                 {
  145.                     for (int j = 0; j < list.Count; j++)
  146.                     {
  147.                         SegmentEntity segmentEntity = list[j];
  148.                         if (segmentEntity != null)
  149.                         {
  150.                             EntityManager.instance.CleanUpEntity(segmentEntity);
  151.                         }
  152.                     }
  153.                     list.Clear();
  154.                 }
  155.             }
  156.             this.mEntities = null;
  157.         }
  158.         if (this.mDroppedItems != null)
  159.         {
  160.             if (!WorldScript.mbIsServer)
  161.             {
  162.                 ItemManager.instance.RemoveClientItems(this);
  163.             }
  164.             else
  165.             {
  166.                 if (NetworkManager.mbHostingServer)
  167.                 {
  168.                 }
  169.                 foreach (DroppedItemData current in this.mDroppedItems)
  170.                 {
  171.                     if (current != null)
  172.                     {
  173.                         current.DropGameObject();
  174.                     }
  175.                 }
  176.             }
  177.         }
  178.         if (this.mMobs != null)
  179.         {
  180.             for (int k = 0; k < this.mMobs.Count; k++)
  181.             {
  182.                 MobEntity mobEntity = this.mMobs[k];
  183.                 if (mobEntity != null)
  184.                 {
  185.                     MobManager.instance.RemoveMob(mobEntity);
  186.                 }
  187.             }
  188.             this.mMobs = null;
  189.         }
  190.         if (this.mArchive != null)
  191.         {
  192.             this.mArchive.DropSegment(this);
  193.             this.mArchive = null;
  194.         }
  195.         if (this.maFrustrums != null && this.maFrustrums.Count > 0)
  196.         {
  197.             throw new AssertException("Segment being cleaned up still has frustrums!");
  198.         }
  199.     }
  200.     public void DropGameObjects()
  201.     {
  202.         if (this.childObjects != null && this.childObjects.Count > 0)
  203.         {
  204.             for (int i = 0; i < this.childObjects.Count; i++)
  205.             {
  206.                 this.DestroyMeshAndGameObject(this.childObjects[i].gameObject);
  207.             }
  208.             this.childObjects.Clear();
  209.         }
  210.         if (this.gameObject != null)
  211.         {
  212.             this.DestroyMeshAndGameObject(this.gameObject.gameObject);
  213.             this.gameObject = null;
  214.             this.mbHasGameObject = false;
  215.         }
  216.         if (this.mEntities != null)
  217.         {
  218.             for (int j = 0; j < 62; j++)
  219.             {
  220.                 List<SegmentEntity> list = this.mEntities[j];
  221.                 if (list != null && list.Count > 0)
  222.                 {
  223.                     for (int k = 0; k < list.Count; k++)
  224.                     {
  225.                         SegmentEntity segmentEntity = list[k];
  226.                         if (segmentEntity != null)
  227.                         {
  228.                             segmentEntity.DropGameObject();
  229.                         }
  230.                     }
  231.                 }
  232.             }
  233.         }
  234.         if (this.mDynamicCustoms != null)
  235.         {
  236.             Dictionary<int, List<DynamicCustomObject>>.KeyCollection keys = this.mDynamicCustoms.Keys;
  237.             foreach (int current in keys)
  238.             {
  239.                 List<DynamicCustomObject> list2 = this.mDynamicCustoms[current];
  240.                 if (list2 != null)
  241.                 {
  242.                     for (int l = 0; l < list2.Count; l++)
  243.                     {
  244.                         DynamicCustomObject dynamicCustomObject = list2[l];
  245.                         if (dynamicCustomObject.mbHasGameObject)
  246.                         {
  247.                             UnityEngine.Object.Destroy(dynamicCustomObject.mGameObject);
  248.                             dynamicCustomObject.mbHasGameObject = false;
  249.                         }
  250.                         CustomObjectManager.instance.mDynamicManager.RemoveObject(dynamicCustomObject);
  251.                     }
  252.                 }
  253.             }
  254.             this.mDynamicCustoms = null;
  255.         }
  256.         if (this.mGameObjects != null)
  257.         {
  258.             for (int m = 0; m < this.mGameObjects.Count; m++)
  259.             {
  260.                 SpawnableObjectManagerScript.instance.ClearObject(this.mGameObjects[m]);
  261.             }
  262.             this.mGameObjects = null;
  263.         }
  264.         if (this.mDroppedItems != null)
  265.         {
  266.             for (int n = 0; n < this.mDroppedItems.Count; n++)
  267.             {
  268.                 DroppedItemData droppedItemData = this.mDroppedItems[n];
  269.                 if (droppedItemData != null)
  270.                 {
  271.                     droppedItemData.DropGameObject();
  272.                 }
  273.             }
  274.         }
  275.         if (this.mMobs != null)
  276.         {
  277.             for (int num = 0; num < this.mMobs.Count; num++)
  278.             {
  279.                 MobEntity mobEntity = this.mMobs[num];
  280.                 if (mobEntity != null)
  281.                 {
  282.                     mobEntity.DropGameObject();
  283.                 }
  284.             }
  285.         }
  286.     }
  287.     public void SetClientFrustrum(WorldFrustrum frustrum)
  288.     {
  289.         if (this.mbDestroyed)
  290.         {
  291.             return;
  292.         }
  293.         this.mMasterFrustrum = frustrum;
  294.         this.mbInLocalFrustrum = true;
  295.         this.mbUnityUpdateSuspended = false;
  296.         if (!frustrum.mbLocalPlayerFrustrum)
  297.         {
  298.             throw new AssertException("Can't add non player client frustrum!");
  299.         }
  300.     }
  301.     public void AddFrustrum(FrustrumBase frustrum)
  302.     {
  303.         if (this.mbDestroyed)
  304.         {
  305.             return;
  306.         }
  307.         if (this.maFrustrums.Count == 0)
  308.         {
  309.             this.mMasterFrustrum = frustrum;
  310.         }
  311.         this.maFrustrums.Add(frustrum);
  312.         if (frustrum.mbLocalPlayerFrustrum)
  313.         {
  314.             this.mbInLocalFrustrum = true;
  315.             this.mbUnityUpdateSuspended = false;
  316.         }
  317.     }
  318.     public void ClearClientFrustrum()
  319.     {
  320.         this.mMasterFrustrum = null;
  321.         this.mbGraphicsDirty = false;
  322.         NetworkManager.instance.mCleanUpQueue.Enqueue(this);
  323.     }
  324.     public void ClearFrustrum(FrustrumBase frustrum)
  325.     {
  326.         if (!this.maFrustrums.Remove(frustrum))
  327.         {
  328.             Debug.Log("Tried to clear frustrum that we're not in!");
  329.         }
  330.         if (this.maFrustrums.Count == 0)
  331.         {
  332.             this.mMasterFrustrum = null;
  333.             this.mbLoadRequested = false;
  334.             this.mbGraphicsDirty = false;
  335.             if (this.mbProcessing || this.mbDirty || this.mbSaveRequested || this.mrSaveDelay > 0f)
  336.             {
  337.                 WorldScript.instance.mDiskThread.mCleanUpQueue.Enqueue(this);
  338.             }
  339.             else
  340.             {
  341.                 SegmentManagerThread.instance.mCleanUpQueue.Enqueue(this);
  342.             }
  343.         }
  344.         else
  345.         {
  346.             if (frustrum == this.mMasterFrustrum)
  347.             {
  348.                 this.mMasterFrustrum = this.maFrustrums[0];
  349.             }
  350.             if (frustrum.mbLocalPlayerFrustrum)
  351.             {
  352.                 bool flag = false;
  353.                 for (int i = 0; i < this.maFrustrums.Count; i++)
  354.                 {
  355.                     FrustrumBase frustrumBase = this.maFrustrums[i];
  356.                     if (frustrumBase.mbLocalPlayerFrustrum)
  357.                     {
  358.                         flag = true;
  359.                         break;
  360.                     }
  361.                 }
  362.                 if (!flag && this.mbInLocalFrustrum)
  363.                 {
  364.                     this.mbInLocalFrustrum = false;
  365.                     this.mbGraphicsDirty = false;
  366.                     SegmentManagerThread.instance.mDropGameObjectsQueue.Enqueue(this);
  367.                 }
  368.             }
  369.         }
  370.     }
  371.     public bool ContainsFrustrum(FrustrumBase frustrum)
  372.     {
  373.         return this.maFrustrums.Contains(frustrum);
  374.     }
  375.     public void UnityUpdate()
  376.     {
  377.         int num = 62;
  378.         bool flag = false;
  379.         if (this.mEntities != null)
  380.         {
  381.             for (int i = 0; i < num; i++)
  382.             {
  383.                 List<SegmentEntity> list = this.mEntities[i];
  384.                 if (list != null && list.Count > 0)
  385.                 {
  386.                     int count = list.Count;
  387.                     for (int j = 0; j < count; j++)
  388.                     {
  389.                         SegmentEntity segmentEntity = list[j];
  390.                         if (segmentEntity != null && !segmentEntity.mbDelete)
  391.                         {
  392.                             if (segmentEntity.mbNeedsUnityUpdate)
  393.                             {
  394.                                 segmentEntity.UnityUpdate();
  395.                                 flag = true;
  396.                             }
  397.                         }
  398.                     }
  399.                 }
  400.             }
  401.         }
  402.         if (!flag)
  403.         {
  404.             this.mbNeedsUnityUpdate = false;
  405.         }
  406.     }
  407.     public void UnitySuspended()
  408.     {
  409.         if (this.mEntities != null)
  410.         {
  411.             for (int i = 0; i < 62; i++)
  412.             {
  413.                 List<SegmentEntity> list = this.mEntities[i];
  414.                 if (list != null && list.Count > 0)
  415.                 {
  416.                     int count = list.Count;
  417.                     for (int j = 0; j < count; j++)
  418.                     {
  419.                         SegmentEntity segmentEntity = list[j];
  420.                         if (segmentEntity != null && !segmentEntity.mbDelete)
  421.                         {
  422.                             if (segmentEntity.mbNeedsUnityUpdate)
  423.                             {
  424.                                 segmentEntity.UnitySuspended();
  425.                             }
  426.                         }
  427.                     }
  428.                 }
  429.             }
  430.         }
  431.         this.mbUnityUpdateSuspended = true;
  432.     }
  433.     public void ScanForFluid()
  434.     {
  435.         if (!this.mbInitialGenerationComplete || this.mbDestroyed || this.mbIsEmpty)
  436.         {
  437.             return;
  438.         }
  439.         if (WorldScript.mbIsServer)
  440.         {
  441.             if (this.maCubes.Length != 4096)
  442.             {
  443.                 Debug.LogError("maCubes are a stupid size");
  444.             }
  445.             int num = this.maCubes.Length;
  446.             for (int i = 0; i < 4096; i++)
  447.             {
  448.                 if (CubeHelper.IsTypeConsideredFluid((int)this.maCubes[i]))
  449.                 {
  450.                     this.AddedFluid();
  451.                     return;
  452.                 }
  453.             }
  454.         }
  455.     }
  456.     private bool AttemptToMoveFluid(int x, int y, int z, int lXDir, int lYDir, int lZDir)
  457.     {
  458.         if (this.mbDestroyed)
  459.         {
  460.             return false;
  461.         }
  462.         if (this.maCubes == null)
  463.         {
  464.             Debug.LogWarning(this.mbInitialGenerationComplete);
  465.             Debug.LogWarning(this.mbDestroyed);
  466.             Debug.LogWarning(this.mbIsEmpty);
  467.             Debug.LogError("Error, why is cubes null?");
  468.         }
  469.         ushort num = 1;
  470.         ushort num2 = this.maCubes[this.GetIndexLocal(x, y, z)];
  471.         if (CubeHelper.IsTypeConsideredFluid((int)num2))
  472.         {
  473.             ushort num3 = num2;
  474.             this.lbFoundFluid = true;
  475.             Segment segment = this;
  476.             long num4 = this.baseX + (long)x + (long)lXDir;
  477.             long num5 = this.baseY + (long)y + (long)lYDir;
  478.             long num6 = this.baseZ + (long)z + (long)lZDir;
  479.             if (num4 < this.baseX || num5 < this.baseY || num6 < this.baseZ || num4 >= this.baseX + 16L || num5 >= this.baseY + 16L || num6 >= this.baseZ + 16L)
  480.             {
  481.                 segment = WorldScript.instance.GetSegment(num4, num5, num6);
  482.                 if (segment == null)
  483.                 {
  484.                     return false;
  485.                 }
  486.                 if (!segment.IsSegmentInAGoodState())
  487.                 {
  488.                     return false;
  489.                 }
  490.             }
  491.             ushort cube = segment.GetCube(num4, num5, num6);
  492.             if (cube == num)
  493.             {
  494.                 WorldScript.instance.BuildFromEntity(this, (long)x, (long)y, (long)z, num, global::TerrainData.GetDefaultValue(num));
  495.                 WorldScript.instance.BuildFromEntity(segment, num4, num5, num6, num3, global::TerrainData.GetDefaultValue(num3));
  496.                 this.mnFluidMoved++;
  497.                 return true;
  498.             }
  499.         }
  500.         return false;
  501.     }
  502.     private void DoFluidTick()
  503.     {
  504.         if (this.FluidSleepTicks > 0)
  505.         {
  506.             this.FluidSleepTicks--;
  507.             return;
  508.         }
  509.         int num = 0;
  510.         for (int i = 0; i < 4096; i++)
  511.         {
  512.             if (this.maCubes[i] == 168)
  513.             {
  514.                 num++;
  515.             }
  516.         }
  517.         this.mnMagmaCount = num;
  518.         if (WorldScript.mbIsServer)
  519.         {
  520.             this.mnFluidMoved = 0;
  521.             this.lbFoundFluid = false;
  522.             for (int j = 0; j < 16; j++)
  523.             {
  524.                 for (int k = 0; k < 16; k++)
  525.                 {
  526.                     for (int l = 0; l < 16; l++)
  527.                     {
  528.                         if (!this.AttemptToMoveFluid(j, l, k, 0, -1, 0))
  529.                         {
  530.                             if (!this.AttemptToMoveFluid(j, l, k, -1, -1, 0))
  531.                             {
  532.                                 if (!this.AttemptToMoveFluid(j, l, k, 1, -1, 0))
  533.                                 {
  534.                                     if (!this.AttemptToMoveFluid(j, l, k, 0, -1, -1))
  535.                                     {
  536.                                         if (this.AttemptToMoveFluid(j, l, k, 0, -1, 1))
  537.                                         {
  538.                                         }
  539.                                     }
  540.                                 }
  541.                             }
  542.                         }
  543.                     }
  544.                 }
  545.             }
  546.         }
  547.         if (!this.lbFoundFluid)
  548.         {
  549.             this.mbHasFluid = false;
  550.             this.DecideIfSegmentNeedsLF();
  551.             this.TicksWithNoFluidMovement = 0;
  552.         }
  553.         else if (this.mnFluidMoved == 0)
  554.         {
  555.             this.TicksWithNoFluidMovement++;
  556.             this.FluidSleepTicks = this.TicksWithNoFluidMovement;
  557.         }
  558.         else
  559.         {
  560.             this.TicksWithNoFluidMovement = 0;
  561.         }
  562.     }
  563.     private void DecideIfSegmentNeedsLF()
  564.     {
  565.         this.mbNeedsLowFrequencyUpdate = false;
  566.         if (this.mbHasFluid)
  567.         {
  568.             this.mbNeedsLowFrequencyUpdate = true;
  569.         }
  570.         if (this.mEntities != null)
  571.         {
  572.             this.mbNeedsLowFrequencyUpdate = true;
  573.         }
  574.     }
  575.     public void LowFrequencyUpdate()
  576.     {
  577.         if (this.mbDestroyed)
  578.         {
  579.             return;
  580.         }
  581.         if (!this.mbInitialGenerationComplete)
  582.         {
  583.             return;
  584.         }
  585.         if (this.mrSaveDelay > 0f)
  586.         {
  587.             this.mrSaveDelay -= LowFrequencyThread.mrPreviousUpdateTimeStep;
  588.             if (this.mrSaveDelay <= 0f)
  589.             {
  590.                 this.RequestSave();
  591.             }
  592.         }
  593.         bool flag = false;
  594.         if (this.mEntities != null)
  595.         {
  596.             for (int i = 0; i < 62; i++)
  597.             {
  598.                 if (this.mbDestroyed)
  599.                 {
  600.                     break;
  601.                 }
  602.                 if (i >= this.mEntities.Length)
  603.                 {
  604.                     break;
  605.                 }
  606.                 List<SegmentEntity> list = this.mEntities[i];
  607.                 if (list != null && list.Count > 0)
  608.                 {
  609.                     int count = list.Count;
  610.                     for (int j = 0; j < count; j++)
  611.                     {
  612.                         if (j > list.Count)
  613.                         {
  614.                             break;
  615.                         }
  616.                         SegmentEntity segmentEntity = list[j];
  617.                         if (segmentEntity != null && !segmentEntity.mbDelete)
  618.                         {
  619.                             if (segmentEntity.mbNeedsLowFrequencyUpdate)
  620.                             {
  621.                                 segmentEntity.LowFrequencyUpdate();
  622.                                 flag = true;
  623.                             }
  624.                         }
  625.                     }
  626.                 }
  627.             }
  628.         }
  629.         if (!flag)
  630.         {
  631.             this.mbNeedsLowFrequencyUpdate = false;
  632.             if (this.mrSaveDelay > 0f)
  633.             {
  634.                 this.RequestSave();
  635.             }
  636.         }
  637.         if (this.mbHasFluid)
  638.         {
  639.             this.mbNeedsLowFrequencyUpdate = true;
  640.             if (this.maCubes != null)
  641.             {
  642.                 this.DoFluidTick();
  643.             }
  644.         }
  645.     }
  646.     public void TimePassedUpdate()
  647.     {
  648.     }
  649.     public bool CheckDiskPresence()
  650.     {
  651.         return this.mArchive.Exists(DiskArchiver.GetSegmentFileName(this));
  652.     }
  653.     public void AddDynamicCustomObject(DynamicCustomObject obj)
  654.     {
  655.         if (this.mDynamicCustoms == null)
  656.         {
  657.             this.mDynamicCustoms = new Dictionary<int, List<DynamicCustomObject>>();
  658.         }
  659.         List<DynamicCustomObject> list;
  660.         if (!this.mDynamicCustoms.TryGetValue(obj.mnDesignId, out list))
  661.         {
  662.             list = new List<DynamicCustomObject>();
  663.             this.mDynamicCustoms.Add(obj.mnDesignId, list);
  664.         }
  665.         list.Add(obj);
  666.     }
  667.     public void RemoveDynamicCustomObject(int x, int y, int z)
  668.     {
  669.     }
  670.     public void AddGameObjectWrapper(GameObjectWrapper wrapper)
  671.     {
  672.         if (this.mGameObjects == null)
  673.         {
  674.             this.mGameObjects = new List<GameObjectWrapper>();
  675.         }
  676.         this.mGameObjects.Add(wrapper);
  677.     }
  678.     public GameObjectWrapper GetGameObjectWrapper(long x, long y, long z)
  679.     {
  680.         if (this.mGameObjects != null)
  681.         {
  682.             for (int i = 0; i < this.mGameObjects.Count; i++)
  683.             {
  684.                 GameObjectWrapper gameObjectWrapper = this.mGameObjects[i];
  685.                 if (gameObjectWrapper.mnX == x && gameObjectWrapper.mnY == y && gameObjectWrapper.mnZ == z)
  686.                 {
  687.                     return gameObjectWrapper;
  688.                 }
  689.             }
  690.         }
  691.         return null;
  692.     }
  693.     public void RemoveObjectsAt(long x, long y, long z)
  694.     {
  695.         if (this.mEntities != null)
  696.         {
  697.             SegmentEntity segmentEntity = this.SearchEntity(x, y, z);
  698.             if (segmentEntity != null && EntityManager.IsEntitySlotBased(segmentEntity.mType))
  699.             {
  700.                 Debug.Log("Removing Entity Type " + segmentEntity.mType.ToString());
  701.                 EntityManager.instance.DeleteEntity(segmentEntity);
  702.                 return;
  703.             }
  704.         }
  705.         if (this.mGameObjects != null)
  706.         {
  707.             for (int i = 0; i < this.mGameObjects.Count; i++)
  708.             {
  709.                 GameObjectWrapper gameObjectWrapper = this.mGameObjects[i];
  710.                 if (gameObjectWrapper.mnX == x && gameObjectWrapper.mnY == y && gameObjectWrapper.mnZ == z)
  711.                 {
  712.                     Debug.Log("Removing game object " + gameObjectWrapper.mType.ToString());
  713.                     this.mGameObjects.RemoveAt(i);
  714.                     i--;
  715.                     SpawnableObjectManagerScript.instance.ClearObject(gameObjectWrapper);
  716.                 }
  717.             }
  718.         }
  719.     }
  720.     public GameObjectWrapper SearchObject(long x, long y, long z)
  721.     {
  722.         if (this.mGameObjects == null)
  723.         {
  724.             return null;
  725.         }
  726.         for (int i = 0; i < this.mGameObjects.Count; i++)
  727.         {
  728.             GameObjectWrapper gameObjectWrapper = this.mGameObjects[i];
  729.             if (gameObjectWrapper.mnX == x && gameObjectWrapper.mnY == y && gameObjectWrapper.mnZ == z)
  730.             {
  731.                 return gameObjectWrapper;
  732.             }
  733.         }
  734.         return null;
  735.     }
  736.     public void AddEntity(SegmentEntity entity, bool doSave)
  737.     {
  738.         if (this.mEntities == null)
  739.         {
  740.             this.mEntities = new List<SegmentEntity>[62];
  741.         }
  742.         if (this.mEntities[(int)entity.mType] == null)
  743.         {
  744.             this.mEntities[(int)entity.mType] = new List<SegmentEntity>();
  745.         }
  746.         List<SegmentEntity> list = this.mEntities[(int)entity.mType];
  747.         lock (list)
  748.         {
  749.             int count = list.Count;
  750.             int i;
  751.             for (i = 0; i < count; i++)
  752.             {
  753.                 if (list[i] == null)
  754.                 {
  755.                     list[i] = entity;
  756.                     break;
  757.                 }
  758.             }
  759.             if (i == count)
  760.             {
  761.                 list.Add(entity);
  762.             }
  763.         }
  764.         if (doSave && entity.ShouldSave() && WorldScript.mbIsServer)
  765.         {
  766.             this.RequestDelayedSave();
  767.         }
  768.         if (entity.mbNeedsUnityUpdate || entity.mbNeedsLowFrequencyUpdate || entity.mbNeedsTimePassedUpdate)
  769.         {
  770.             this.mbNeedsUnityUpdate = (this.mbNeedsUnityUpdate || entity.mbNeedsUnityUpdate);
  771.             this.mbNeedsLowFrequencyUpdate = (this.mbNeedsLowFrequencyUpdate || entity.mbNeedsLowFrequencyUpdate);
  772.             this.mbNeedsTimePassedUpdate = (this.mbNeedsTimePassedUpdate || entity.mbNeedsTimePassedUpdate);
  773.             if (!this.mbIsQueuedForUpdate)
  774.             {
  775.                 WorldScript.instance.mSegmentUpdater.AddSegment(this);
  776.             }
  777.         }
  778.     }
  779.     public void RemoveEntity(SegmentEntity entity)
  780.     {
  781.         if (this.mEntities == null)
  782.         {
  783.             return;
  784.         }
  785.         List<SegmentEntity> list = this.mEntities[(int)entity.mType];
  786.         if (list == null)
  787.         {
  788.             return;
  789.         }
  790.         int count = list.Count;
  791.         for (int i = 0; i < count; i++)
  792.         {
  793.             if (list[i] == entity)
  794.             {
  795.                 list[i] = null;
  796.                 break;
  797.             }
  798.         }
  799.     }
  800.     public SegmentEntity FetchEntity(eSegmentEntity type, long x, long y, long z)
  801.     {
  802.         if (this.mEntities == null)
  803.         {
  804.             return null;
  805.         }
  806.         if (type >= (eSegmentEntity)this.mEntities.Length || type < eSegmentEntity.InjectionEntity)
  807.         {
  808.             Debug.LogError("FetchEntity Error? : Type was " + type);
  809.             return null;
  810.         }
  811.         List<SegmentEntity> list = this.mEntities[(int)type];
  812.         if (list == null)
  813.         {
  814.             Debug.Log("No entities of type " + type + " found in segment - this shouldn't happen");
  815.             return null;
  816.         }
  817.         int count = list.Count;
  818.         for (int i = 0; i < count; i++)
  819.         {
  820.             SegmentEntity segmentEntity = list[i];
  821.             if (segmentEntity != null && !segmentEntity.mbDelete)
  822.             {
  823.                 if (segmentEntity.mnX == x && segmentEntity.mnY == y && segmentEntity.mnZ == z)
  824.                 {
  825.                     return segmentEntity;
  826.                 }
  827.             }
  828.         }
  829.         Debug.Log("Warning, FetchEntity failed to find entity of type " + type.ToString());
  830.         return null;
  831.     }
  832.     public SegmentEntity FetchEntity(eSegmentEntity type, int id)
  833.     {
  834.         if (this.mEntities == null)
  835.         {
  836.             return null;
  837.         }
  838.         if (type >= (eSegmentEntity)this.mEntities.Length)
  839.         {
  840.             throw new Exception("wait what?");
  841.         }
  842.         List<SegmentEntity> list = this.mEntities[(int)type];
  843.         if (list == null)
  844.         {
  845.             return null;
  846.         }
  847.         int count = list.Count;
  848.         for (int i = 0; i < count; i++)
  849.         {
  850.             SegmentEntity segmentEntity = list[i];
  851.             if (segmentEntity != null && !segmentEntity.mbDelete)
  852.             {
  853.                 if (segmentEntity.mnID == id)
  854.                 {
  855.                     return segmentEntity;
  856.                 }
  857.             }
  858.         }
  859.         Debug.Log("Warning, FetchEntity failed to find entity of type " + type.ToString());
  860.         return null;
  861.     }
  862.     public SegmentEntity SearchEntity(long x, long y, long z)
  863.     {
  864.         if (this.mEntities == null)
  865.         {
  866.             return null;
  867.         }
  868.         for (int i = 0; i < this.mEntities.Length; i++)
  869.         {
  870.             List<SegmentEntity> list = this.mEntities[i];
  871.             if (list != null && list.Count > 0)
  872.             {
  873.                 int count = list.Count;
  874.                 for (int j = 0; j < count; j++)
  875.                 {
  876.                     SegmentEntity segmentEntity = list[j];
  877.                     if (segmentEntity != null && !segmentEntity.mbDelete)
  878.                     {
  879.                         if (segmentEntity.mnX == x && segmentEntity.mnY == y && segmentEntity.mnZ == z)
  880.                         {
  881.                             return segmentEntity;
  882.                         }
  883.                     }
  884.                 }
  885.             }
  886.         }
  887.         return null;
  888.     }
  889.     public bool HasEntities()
  890.     {
  891.         if (this.mEntities == null)
  892.         {
  893.             return false;
  894.         }
  895.         for (int i = 0; i < this.mEntities.Length; i++)
  896.         {
  897.             List<SegmentEntity> list = this.mEntities[i];
  898.             if (list != null && list.Count > 0)
  899.             {
  900.                 int count = list.Count;
  901.                 for (int j = 0; j < count; j++)
  902.                 {
  903.                     SegmentEntity segmentEntity = list[j];
  904.                     if (segmentEntity != null && !segmentEntity.mbDelete)
  905.                     {
  906.                         return true;
  907.                     }
  908.                 }
  909.             }
  910.         }
  911.         return false;
  912.     }
  913.     public void AddDroppedItem(DroppedItemData droppedItem, bool doSave)
  914.     {
  915.         if (this.mDroppedItems == null)
  916.         {
  917.             this.mDroppedItems = new List<DroppedItemData>();
  918.         }
  919.         if (this.mDroppedItems.Count > 500)
  920.         {
  921.             return;
  922.         }
  923.         lock (this.mDroppedItems)
  924.         {
  925.             int count = this.mDroppedItems.Count;
  926.             int i;
  927.             for (i = 0; i < count; i++)
  928.             {
  929.                 if (this.mDroppedItems[i] == null)
  930.                 {
  931.                     this.mDroppedItems[i] = droppedItem;
  932.                     break;
  933.                 }
  934.             }
  935.             if (i == count)
  936.             {
  937.                 this.mDroppedItems.Add(droppedItem);
  938.             }
  939.         }
  940.         if (!this.mbNeedsItemUpdate)
  941.         {
  942.             WorldScript.instance.mSegmentUpdater.AddItemSegment(this);
  943.         }
  944.         if (doSave)
  945.         {
  946.             this.RequestDelayedSave();
  947.         }
  948.     }
  949.     public bool RemoveDroppedItem(DroppedItemData droppedItem)
  950.     {
  951.         if (this.mDroppedItems == null)
  952.         {
  953.             return false;
  954.         }
  955.         for (int i = 0; i < this.mDroppedItems.Count; i++)
  956.         {
  957.             if (this.mDroppedItems[i] == droppedItem)
  958.             {
  959.                 this.mDroppedItems[i] = null;
  960.                 this.RequestDelayedSave();
  961.                 return true;
  962.             }
  963.         }
  964.         return false;
  965.     }
  966.     public void AddMob(MobEntity mob)
  967.     {
  968.         if (this.mMobs == null)
  969.         {
  970.             this.mMobs = new List<MobEntity>();
  971.         }
  972.         if (this.mMobs.Count > 100)
  973.         {
  974.             return;
  975.         }
  976.         lock (this.mMobs)
  977.         {
  978.             int count = this.mMobs.Count;
  979.             int i;
  980.             for (i = 0; i < count; i++)
  981.             {
  982.                 if (this.mMobs[i] == null)
  983.                 {
  984.                     this.mMobs[i] = mob;
  985.                     break;
  986.                 }
  987.             }
  988.             if (i == count)
  989.             {
  990.                 this.mMobs.Add(mob);
  991.             }
  992.         }
  993.         if (mob.ShouldSave())
  994.         {
  995.             this.RequestDelayedSave();
  996.         }
  997.     }
  998.     public MobEntity GetMob(int id)
  999.     {
  1000.         if (this.mMobs == null)
  1001.         {
  1002.             return null;
  1003.         }
  1004.         int count = this.mMobs.Count;
  1005.         for (int i = 0; i < count; i++)
  1006.         {
  1007.             MobEntity mobEntity = this.mMobs[i];
  1008.             if (mobEntity != null)
  1009.             {
  1010.                 if (mobEntity.mnServerID == id)
  1011.                 {
  1012.                     return mobEntity;
  1013.                 }
  1014.             }
  1015.         }
  1016.         return null;
  1017.     }
  1018.     public bool RemoveMob(MobEntity mob)
  1019.     {
  1020.         if (this.mMobs == null)
  1021.         {
  1022.             return false;
  1023.         }
  1024.         for (int i = 0; i < this.mMobs.Count; i++)
  1025.         {
  1026.             if (this.mMobs[i] == mob)
  1027.             {
  1028.                 this.mMobs[i] = null;
  1029.                 if (mob.ShouldSave())
  1030.                 {
  1031.                     this.RequestDelayedSave();
  1032.                 }
  1033.                 return true;
  1034.             }
  1035.         }
  1036.         return false;
  1037.     }
  1038.     public bool HasDroppedItems()
  1039.     {
  1040.         return this.mDroppedItems != null && this.mDroppedItems.Count > 0;
  1041.     }
  1042.     public void PushEntityUpdates(List<SegmentEntity> entityUpdateList)
  1043.     {
  1044.         if (this.mEntities == null)
  1045.         {
  1046.             return;
  1047.         }
  1048.         for (int i = 0; i < this.mEntities.Length; i++)
  1049.         {
  1050.             List<SegmentEntity> list = this.mEntities[i];
  1051.             if (list != null && list.Count > 0)
  1052.             {
  1053.                 int count = list.Count;
  1054.                 for (int j = 0; j < count; j++)
  1055.                 {
  1056.                     SegmentEntity segmentEntity = list[j];
  1057.                     if (segmentEntity != null && !segmentEntity.mbDelete && segmentEntity.ShouldNetworkUpdate())
  1058.                     {
  1059.                         entityUpdateList.Add(segmentEntity);
  1060.                     }
  1061.                 }
  1062.             }
  1063.         }
  1064.     }
  1065.     public bool GetMemoryBlock()
  1066.     {
  1067.         if (this.maCubes == null)
  1068.         {
  1069.             if (this.maCubeData != null)
  1070.             {
  1071.                 throw new AssertException("maCubes and maCubeData out of sync");
  1072.             }
  1073.             this.maCubes = new ushort[4096];
  1074.             this.maCubeData = new CubeData[4096];
  1075.         }
  1076.         return true;
  1077.     }
  1078.     public void DropMemoryBlock()
  1079.     {
  1080.         if (this.maCubes != null)
  1081.         {
  1082.             if (this.maCubeData == null)
  1083.             {
  1084.                 throw new AssertException("maCubes and maCubeData out of sync");
  1085.             }
  1086.             this.maCubes = null;
  1087.             this.maCubeData = null;
  1088.         }
  1089.     }
  1090.     public bool QueueBuildOrder(ref BuildOrder order)
  1091.     {
  1092.         bool result;
  1093.         lock (this.mQueueLock)
  1094.         {
  1095.             try
  1096.             {
  1097.                 if (this.maBuildOrders == null)
  1098.                 {
  1099.                     this.GetOrderBlock();
  1100.                 }
  1101.                 result = this.maBuildOrders.EnqueueAndIgnoreOverFlow(order);
  1102.             }
  1103.             catch (NullReferenceException)
  1104.             {
  1105.                 Debug.LogWarning("Race condition, build order dropped");
  1106.                 result = false;
  1107.             }
  1108.         }
  1109.         return result;
  1110.     }
  1111.     public bool GetOrderBlock()
  1112.     {
  1113.         bool result;
  1114.         lock (this.mQueueLock)
  1115.         {
  1116.             if (this.maBuildOrders == null)
  1117.             {
  1118.                 WorldScript.instance.mOrderBlockManager.GetBlock(this, out this.maBuildOrders, out this.mnOrderBlockID);
  1119.             }
  1120.             result = (this.maBuildOrders != null);
  1121.         }
  1122.         return result;
  1123.     }
  1124.     public void DropOrderBlock()
  1125.     {
  1126.         lock (this.mQueueLock)
  1127.         {
  1128.             if (this.maBuildOrders != null)
  1129.             {
  1130.                 WorldScript.instance.mOrderBlockManager.ReleaseBlock(this, this.mnOrderBlockID);
  1131.                 this.maBuildOrders = null;
  1132.             }
  1133.         }
  1134.     }
  1135.     public void PopulateEmptySegment()
  1136.     {
  1137.         if (!this.mbIsEmpty)
  1138.         {
  1139.             throw new AssertException("Trying to populate empty segment that isn't? " + this.ToString());
  1140.         }
  1141.         if (!this.GetMemoryBlock())
  1142.         {
  1143.             throw new AssertException("Ran out of memory blocks!");
  1144.         }
  1145.         byte mSunLightLevel = (!this.mbSunLit) ? 0 : 15;
  1146.         for (int i = 0; i < this.maCubes.Length; i++)
  1147.         {
  1148.             this.maCubes[i] = 1;
  1149.             CubeData cubeData = default(CubeData);
  1150.             cubeData.meFlags = 0;
  1151.             cubeData.mSunLightLevel = mSunLightLevel;
  1152.             cubeData.mTorchLightLevel = 0;
  1153.             cubeData.mLavaLightLevel = 0;
  1154.             cubeData.mValue = 0;
  1155.             this.maCubeData[i] = cubeData;
  1156.         }
  1157.         this.mbIsEmpty = false;
  1158.     }
  1159.     public void RequestRegenLighting()
  1160.     {
  1161.         this.mnLightStatus = 0;
  1162.         this.mbRedoLight = true;
  1163.         this.mbLightingDirty = true;
  1164.     }
  1165.     public void RequestRegenLighting(int flag)
  1166.     {
  1167.         this.ClearLightStatus(flag);
  1168.         this.mbLightingDirty = true;
  1169.     }
  1170.     public void RequestRegenerateGraphics()
  1171.     {
  1172.         if (!this.mbInLocalFrustrum)
  1173.         {
  1174.             Debug.Log("Requesting graphics for segment which isn't in a player frustrum!");
  1175.             return;
  1176.         }
  1177.         this.mbGraphicsDirty = true;
  1178.         SegmentMeshCreator.instance.mMeshRenderer.KickRenderThread();
  1179.     }
  1180.     public void RequestSave()
  1181.     {
  1182.         if (!this.mbInitialGenerationComplete || this.mbDestroyed)
  1183.         {
  1184.             return;
  1185.         }
  1186.         if (!this.mbIsEmpty && this.maCubes == null)
  1187.         {
  1188.             Debug.LogError("This is the error we are seeing");
  1189.         }
  1190.         this.mrSaveDelay = 0f;
  1191.         this.mbSaveRequested = true;
  1192.         if (this.mMasterFrustrum != null)
  1193.         {
  1194.             this.mMasterFrustrum.mbRequiresSaving = true;
  1195.         }
  1196.     }
  1197.     public void RequestDelayedSave()
  1198.     {
  1199.         if (this.mbIsQueuedForUpdate && this.mbNeedsLowFrequencyUpdate)
  1200.         {
  1201.             if (this.mrSaveDelay <= 0f)
  1202.             {
  1203.                 this.mrSaveDelay = 30f;
  1204.             }
  1205.         }
  1206.         else
  1207.         {
  1208.             this.RequestSave();
  1209.         }
  1210.     }
  1211.     public bool IsLightComplete()
  1212.     {
  1213.         return this.mnLightStatus == 127;
  1214.     }
  1215.     public bool HasLightStatus(int flag)
  1216.     {
  1217.         return (this.mnLightStatus & flag) > 0;
  1218.     }
  1219.     public void SetLightStatus(int flag)
  1220.     {
  1221.         this.mnLightStatus |= flag;
  1222.     }
  1223.     public void ClearLightStatus(int flag)
  1224.     {
  1225.         this.mnLightStatus &= ~flag;
  1226.     }
  1227.     public void SetLightEdge(int flag)
  1228.     {
  1229.         this.mnLightEdgeStatus |= flag;
  1230.     }
  1231.     public int GetIndexLocal(int x, int y, int z)
  1232.     {
  1233.         return (y << 8) + (z << 4) + x;
  1234.     }
  1235.     public int GetIndex(long x, long y, long z)
  1236.     {
  1237.         int num = (int)(x - this.baseX);
  1238.         int num2 = (int)(y - this.baseY);
  1239.         int num3 = (int)(z - this.baseZ);
  1240.         return (num2 << 8) + (num3 << 4) + num;
  1241.     }
  1242.     public ushort GetCube(long x, long y, long z)
  1243.     {
  1244.         if (!this.mbInitialGenerationComplete || this.mbDestroyed)
  1245.         {
  1246.             return 0;
  1247.         }
  1248.         if (this.mbIsEmpty)
  1249.         {
  1250.             return 1;
  1251.         }
  1252.         return this.maCubes[this.GetIndex(x, y, z)];
  1253.     }
  1254.     public CubeData GetCubeData(long x, long y, long z)
  1255.     {
  1256.         if (!this.mbInitialGenerationComplete || this.mbDestroyed || this.mbIsEmpty)
  1257.         {
  1258.             return default(CubeData);
  1259.         }
  1260.         return this.maCubeData[this.GetIndex(x, y, z)];
  1261.     }
  1262.     public ushort GetCubeNoChecking(int x, int y, int z)
  1263.     {
  1264.         return this.maCubes[(y << 8) + (z << 4) + x];
  1265.     }
  1266.     public CubeData GetCubeDataNoChecking(int x, int y, int z)
  1267.     {
  1268.         return this.maCubeData[(y << 8) + (z << 4) + x];
  1269.     }
  1270.     public void GetCubeDataNoChecking(int x, int y, int z, out ushort cubeType, out CubeData cubeData)
  1271.     {
  1272.         int num = (y << 8) + (z << 4) + x;
  1273.         cubeType = this.maCubes[num];
  1274.         cubeData = this.maCubeData[num];
  1275.     }
  1276.     public void SetCubeValueNoChecking(int x, int y, int z, ushort lData)
  1277.     {
  1278.         this.maCubeData[(y << 8) + (z << 4) + x].mValue = lData;
  1279.     }
  1280.     public void SetCubeTypeNoChecking(int x, int y, int z, ushort lType)
  1281.     {
  1282.         this.maCubes[(y << 8) + (z << 4) + x] = lType;
  1283.     }
  1284.     public void SetCubeTypeNoChecking(int x, int y, int z, ushort lType, ushort val)
  1285.     {
  1286.         int num = (y << 8) + (z << 4) + x;
  1287.         this.maCubes[num] = lType;
  1288.         this.maCubeData[num].mValue = val;
  1289.     }
  1290.     public void SetFlagsNoChecking(int x, int y, int z, byte flags)
  1291.     {
  1292.         this.maCubeData[(y << 8) + (z << 4) + x].meFlags = flags;
  1293.     }
  1294.     public void SpawnMobGraphics()
  1295.     {
  1296.         if (this.mMobs != null)
  1297.         {
  1298.             for (int i = 0; i < this.mMobs.Count; i++)
  1299.             {
  1300.                 MobEntity mobEntity = this.mMobs[i];
  1301.                 if (mobEntity != null)
  1302.                 {
  1303.                     if (mobEntity.mWrapper == null)
  1304.                     {
  1305.                         mobEntity.SpawnGameObject();
  1306.                     }
  1307.                 }
  1308.             }
  1309.         }
  1310.     }
  1311.     public void CreateGameObject()
  1312.     {
  1313.         WorldFrustrum mPlayerFrustrum = WorldScript.instance.mPlayerFrustrum;
  1314.         if (mPlayerFrustrum == null)
  1315.         {
  1316.             throw new Exception("Tried creating game object on dedicated server!");
  1317.         }
  1318.         long num = this.baseX - mPlayerFrustrum.mOriginX;
  1319.         long num2 = this.baseY - mPlayerFrustrum.mOriginY;
  1320.         long num3 = this.baseZ - mPlayerFrustrum.mOriginZ;
  1321.         Vector3 position = new Vector3((float)num, (float)num2, (float)num3);
  1322.         GameObject gameObject = UnityEngine.Object.Instantiate(WorldScript.SegmentPrefab, position, Quaternion.identity) as GameObject;
  1323.         this.gameObject = gameObject.GetComponent<SegmentObjectScript>();
  1324.         this.gameObject.mSegment = this;
  1325.         HUDManager.mnNodesInstantiated++;
  1326.         this.mbHasGameObject = true;
  1327.     }
  1328.     private void DropGameObject()
  1329.     {
  1330.         if (this.gameObject != null)
  1331.         {
  1332.             this.DestroyMeshAndGameObject(this.gameObject.gameObject);
  1333.             this.gameObject = null;
  1334.             this.mbHasGameObject = false;
  1335.         }
  1336.     }
  1337.     public void ResetObjectUsage()
  1338.     {
  1339.         if (this.gameObject != null)
  1340.         {
  1341.             this.gameObject.inUse = false;
  1342.         }
  1343.         if (this.childObjects != null)
  1344.         {
  1345.             foreach (SegmentObjectScript current in this.childObjects)
  1346.             {
  1347.                 current.inUse = false;
  1348.             }
  1349.         }
  1350.     }
  1351.     public void DropUnusedObjects()
  1352.     {
  1353.         if (this.childObjects != null)
  1354.         {
  1355.             for (int i = this.childObjects.Count - 1; i >= 0; i--)
  1356.             {
  1357.                 SegmentObjectScript segmentObjectScript = this.childObjects[i];
  1358.                 if (!segmentObjectScript.inUse)
  1359.                 {
  1360.                     this.DestroyMeshAndGameObject(segmentObjectScript.gameObject);
  1361.                     this.childObjects.RemoveAt(i);
  1362.                 }
  1363.             }
  1364.         }
  1365.         if (this.gameObject != null && !this.gameObject.inUse)
  1366.         {
  1367.             this.DestroyMeshAndGameObject(this.gameObject.gameObject);
  1368.             this.gameObject = null;
  1369.             this.mbHasGameObject = false;
  1370.         }
  1371.     }
  1372.     private void DestroyMeshAndGameObject(GameObject obj)
  1373.     {
  1374.         MeshFilter component = obj.GetComponent<MeshFilter>();
  1375.         if (component != null)
  1376.         {
  1377.             Mesh mesh = component.mesh;
  1378.             if (mesh != null)
  1379.             {
  1380.                 UnityEngine.Object.Destroy(mesh);
  1381.             }
  1382.         }
  1383.         UnityEngine.Object.Destroy(obj);
  1384.     }
  1385.     public SegmentObjectScript GetChildObject(SegmentRendererLayerType layerType, int index)
  1386.     {
  1387.         int num = 0;
  1388.         if (index == 0 && (this.gameObject == null || !this.gameObject.inUse))
  1389.         {
  1390.             if (this.gameObject == null)
  1391.             {
  1392.                 this.CreateGameObject();
  1393.             }
  1394.             this.gameObject.layerType = layerType;
  1395.             return this.gameObject;
  1396.         }
  1397.         if (this.gameObject.layerType == layerType)
  1398.         {
  1399.             num++;
  1400.         }
  1401.         SegmentObjectScript segmentObjectScript = null;
  1402.         if (this.childObjects != null)
  1403.         {
  1404.             foreach (SegmentObjectScript current in this.childObjects)
  1405.             {
  1406.                 if (current.layerType == layerType)
  1407.                 {
  1408.                     if (num == index)
  1409.                     {
  1410.                         segmentObjectScript = current;
  1411.                         break;
  1412.                     }
  1413.                     num++;
  1414.                 }
  1415.             }
  1416.         }
  1417.         if (segmentObjectScript == null)
  1418.         {
  1419.             segmentObjectScript = this.CreateChildObject(layerType);
  1420.         }
  1421.         return segmentObjectScript;
  1422.     }
  1423.     private SegmentObjectScript CreateChildObject(SegmentRendererLayerType layerType)
  1424.     {
  1425.         if (this.childObjects == null)
  1426.         {
  1427.             this.childObjects = new List<SegmentObjectScript>();
  1428.         }
  1429.         GameObject gameObject = UnityEngine.Object.Instantiate(WorldScript.SegmentPrefab, this.gameObject.transform.position, Quaternion.identity) as GameObject;
  1430.         gameObject.transform.localPosition = Vector3.zero;
  1431.         SegmentObjectScript component = gameObject.GetComponent<SegmentObjectScript>();
  1432.         component.layerType = layerType;
  1433.         this.childObjects.Add(component);
  1434.         HUDManager.mnChildrenInstantiated++;
  1435.         return component;
  1436.     }
  1437.     public override string ToString()
  1438.     {
  1439.         return string.Concat(new object[]
  1440.         {
  1441.             "Segment [",
  1442.             this.baseX - 4611686017890516992L,
  1443.             "][",
  1444.             this.baseY - 4611686017890516992L,
  1445.             "][",
  1446.             this.baseZ - 4611686017890516992L,
  1447.             "]"
  1448.         });
  1449.     }
  1450.     public bool IsEligibleForSave()
  1451.     {
  1452.         return true;
  1453.     }
  1454. }
Advertisement
Add Comment
Please, Sign In to add comment