Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public class Segment
- {
- public const float SAVE_DELAY = 30f;
- public const int MAX_DROPPED_ITEMS_PER_SEGMENT = 500;
- public const int MAX_MOBS_PER_SEGMENT = 100;
- public SegmentObjectScript gameObject;
- public List<SegmentObjectScript> childObjects;
- public List<FrustrumBase> maFrustrums;
- public FrustrumBase mMasterFrustrum;
- public ZipArchive mArchive;
- public long baseX;
- public long baseY;
- public long baseZ;
- public ushort[] maCubes;
- public CubeData[] maCubeData;
- public List<SegmentEntity>[] mEntities;
- public List<DroppedItemData> mDroppedItems;
- public List<MobEntity> mMobs;
- public Dictionary<int, List<DynamicCustomObject>> mDynamicCustoms;
- public List<GameObjectWrapper> mGameObjects;
- public object mQueueLock;
- public int mnVersion;
- public int mnSaves;
- public float mrTimestampForLastChange;
- public float mrSaveDelay;
- public bool mbProcessing;
- public bool mbDirty;
- public bool mbDoPerlin;
- public bool mbInitialGenerationComplete;
- public bool mbIsEmpty;
- public bool mbSunLit;
- public bool mbHasFaces;
- public bool mbSaveRequested;
- public bool mbLoadRequested;
- public bool mbWasLoaded;
- public bool mbDestroyed;
- public bool mbHasGameObject;
- public bool mbHasCustoms;
- public bool mbOrdersQueueing;
- public bool mbNeedsItemUpdate;
- public bool mbNeedsUnityUpdate;
- public bool mbNeedsLowFrequencyUpdate;
- public bool mbNeedsTimePassedUpdate;
- public bool mbIsQueuedForUpdate;
- public bool mbInLocalFrustrum;
- public bool mbUnityUpdateSuspended;
- public RingBuffer<BuildOrder> maBuildOrders;
- public int mnOrderBlockID;
- public int mnLightStatus;
- public int mnLightEdgeStatus;
- public bool mbRedoLight;
- public bool mbRedoSunLight;
- public bool mbEverHadLightingPass;
- public bool mbLightingDirty;
- public bool mbGraphicsDirty;
- public int mnDebugSegmentID;
- private static int debugSegmentCounter;
- public SegmentFluid mFluid;
- public bool mbHasFluid;
- private bool lbFoundFluid;
- private int TicksWithNoFluidMovement;
- private int mnFluidMoved;
- public int FluidSleepTicks;
- public int mnMagmaCount;
- public Segment(long x, long y, long z)
- {
- if (WorldScript.mbIsServer)
- {
- this.maFrustrums = new List<FrustrumBase>(64);
- }
- this.baseX = x;
- this.baseY = y;
- this.baseZ = z;
- this.mnVersion = 0;
- this.mnSaves = 0;
- this.mbDirty = false;
- this.mbDestroyed = false;
- this.mbLightingDirty = false;
- this.mbRedoLight = false;
- this.mnLightStatus = 0;
- this.mbSaveRequested = false;
- this.mbHasFaces = false;
- this.mbHasGameObject = false;
- this.mbHasCustoms = false;
- this.mbIsEmpty = false;
- this.mbOrdersQueueing = false;
- this.mbGraphicsDirty = false;
- this.mEntities = null;
- this.mDynamicCustoms = null;
- this.mGameObjects = null;
- this.mQueueLock = new object();
- this.mnDebugSegmentID = Segment.debugSegmentCounter++;
- this.mFluid = new SegmentFluid();
- this.mFluid.mOwner = this;
- }
- public bool IsSegmentInAGoodState()
- {
- return this.mbInitialGenerationComplete && !this.mbDestroyed;
- }
- public void SetIntialGenerationComplete()
- {
- this.mbInitialGenerationComplete = true;
- this.ScanForFluid();
- }
- public void AddedFluid()
- {
- if (WorldScript.mbIsServer)
- {
- this.FluidSleepTicks = 0;
- this.mbHasFluid = true;
- this.mbNeedsLowFrequencyUpdate = true;
- if (!this.mbIsQueuedForUpdate)
- {
- WorldScript.instance.mSegmentUpdater.AddSegment(this);
- this.mbIsQueuedForUpdate = true;
- }
- }
- }
- public void CleanUp()
- {
- this.mbDestroyed = true;
- this.mbLoadRequested = false;
- this.mbDirty = false;
- this.mbGraphicsDirty = false;
- if (this.mbIsQueuedForUpdate)
- {
- WorldScript.instance.mSegmentUpdater.RemoveSegment(this);
- }
- this.DropGameObjects();
- if (this.maCubes != null)
- {
- this.maCubes = null;
- this.maCubeData = null;
- }
- if (this.mEntities != null)
- {
- for (int i = 0; i < 62; i++)
- {
- List<SegmentEntity> list = this.mEntities[i];
- if (list != null && list.Count > 0)
- {
- for (int j = 0; j < list.Count; j++)
- {
- SegmentEntity segmentEntity = list[j];
- if (segmentEntity != null)
- {
- EntityManager.instance.CleanUpEntity(segmentEntity);
- }
- }
- list.Clear();
- }
- }
- this.mEntities = null;
- }
- if (this.mDroppedItems != null)
- {
- if (!WorldScript.mbIsServer)
- {
- ItemManager.instance.RemoveClientItems(this);
- }
- else
- {
- if (NetworkManager.mbHostingServer)
- {
- }
- foreach (DroppedItemData current in this.mDroppedItems)
- {
- if (current != null)
- {
- current.DropGameObject();
- }
- }
- }
- }
- if (this.mMobs != null)
- {
- for (int k = 0; k < this.mMobs.Count; k++)
- {
- MobEntity mobEntity = this.mMobs[k];
- if (mobEntity != null)
- {
- MobManager.instance.RemoveMob(mobEntity);
- }
- }
- this.mMobs = null;
- }
- if (this.mArchive != null)
- {
- this.mArchive.DropSegment(this);
- this.mArchive = null;
- }
- if (this.maFrustrums != null && this.maFrustrums.Count > 0)
- {
- throw new AssertException("Segment being cleaned up still has frustrums!");
- }
- }
- public void DropGameObjects()
- {
- if (this.childObjects != null && this.childObjects.Count > 0)
- {
- for (int i = 0; i < this.childObjects.Count; i++)
- {
- this.DestroyMeshAndGameObject(this.childObjects[i].gameObject);
- }
- this.childObjects.Clear();
- }
- if (this.gameObject != null)
- {
- this.DestroyMeshAndGameObject(this.gameObject.gameObject);
- this.gameObject = null;
- this.mbHasGameObject = false;
- }
- if (this.mEntities != null)
- {
- for (int j = 0; j < 62; j++)
- {
- List<SegmentEntity> list = this.mEntities[j];
- if (list != null && list.Count > 0)
- {
- for (int k = 0; k < list.Count; k++)
- {
- SegmentEntity segmentEntity = list[k];
- if (segmentEntity != null)
- {
- segmentEntity.DropGameObject();
- }
- }
- }
- }
- }
- if (this.mDynamicCustoms != null)
- {
- Dictionary<int, List<DynamicCustomObject>>.KeyCollection keys = this.mDynamicCustoms.Keys;
- foreach (int current in keys)
- {
- List<DynamicCustomObject> list2 = this.mDynamicCustoms[current];
- if (list2 != null)
- {
- for (int l = 0; l < list2.Count; l++)
- {
- DynamicCustomObject dynamicCustomObject = list2[l];
- if (dynamicCustomObject.mbHasGameObject)
- {
- UnityEngine.Object.Destroy(dynamicCustomObject.mGameObject);
- dynamicCustomObject.mbHasGameObject = false;
- }
- CustomObjectManager.instance.mDynamicManager.RemoveObject(dynamicCustomObject);
- }
- }
- }
- this.mDynamicCustoms = null;
- }
- if (this.mGameObjects != null)
- {
- for (int m = 0; m < this.mGameObjects.Count; m++)
- {
- SpawnableObjectManagerScript.instance.ClearObject(this.mGameObjects[m]);
- }
- this.mGameObjects = null;
- }
- if (this.mDroppedItems != null)
- {
- for (int n = 0; n < this.mDroppedItems.Count; n++)
- {
- DroppedItemData droppedItemData = this.mDroppedItems[n];
- if (droppedItemData != null)
- {
- droppedItemData.DropGameObject();
- }
- }
- }
- if (this.mMobs != null)
- {
- for (int num = 0; num < this.mMobs.Count; num++)
- {
- MobEntity mobEntity = this.mMobs[num];
- if (mobEntity != null)
- {
- mobEntity.DropGameObject();
- }
- }
- }
- }
- public void SetClientFrustrum(WorldFrustrum frustrum)
- {
- if (this.mbDestroyed)
- {
- return;
- }
- this.mMasterFrustrum = frustrum;
- this.mbInLocalFrustrum = true;
- this.mbUnityUpdateSuspended = false;
- if (!frustrum.mbLocalPlayerFrustrum)
- {
- throw new AssertException("Can't add non player client frustrum!");
- }
- }
- public void AddFrustrum(FrustrumBase frustrum)
- {
- if (this.mbDestroyed)
- {
- return;
- }
- if (this.maFrustrums.Count == 0)
- {
- this.mMasterFrustrum = frustrum;
- }
- this.maFrustrums.Add(frustrum);
- if (frustrum.mbLocalPlayerFrustrum)
- {
- this.mbInLocalFrustrum = true;
- this.mbUnityUpdateSuspended = false;
- }
- }
- public void ClearClientFrustrum()
- {
- this.mMasterFrustrum = null;
- this.mbGraphicsDirty = false;
- NetworkManager.instance.mCleanUpQueue.Enqueue(this);
- }
- public void ClearFrustrum(FrustrumBase frustrum)
- {
- if (!this.maFrustrums.Remove(frustrum))
- {
- Debug.Log("Tried to clear frustrum that we're not in!");
- }
- if (this.maFrustrums.Count == 0)
- {
- this.mMasterFrustrum = null;
- this.mbLoadRequested = false;
- this.mbGraphicsDirty = false;
- if (this.mbProcessing || this.mbDirty || this.mbSaveRequested || this.mrSaveDelay > 0f)
- {
- WorldScript.instance.mDiskThread.mCleanUpQueue.Enqueue(this);
- }
- else
- {
- SegmentManagerThread.instance.mCleanUpQueue.Enqueue(this);
- }
- }
- else
- {
- if (frustrum == this.mMasterFrustrum)
- {
- this.mMasterFrustrum = this.maFrustrums[0];
- }
- if (frustrum.mbLocalPlayerFrustrum)
- {
- bool flag = false;
- for (int i = 0; i < this.maFrustrums.Count; i++)
- {
- FrustrumBase frustrumBase = this.maFrustrums[i];
- if (frustrumBase.mbLocalPlayerFrustrum)
- {
- flag = true;
- break;
- }
- }
- if (!flag && this.mbInLocalFrustrum)
- {
- this.mbInLocalFrustrum = false;
- this.mbGraphicsDirty = false;
- SegmentManagerThread.instance.mDropGameObjectsQueue.Enqueue(this);
- }
- }
- }
- }
- public bool ContainsFrustrum(FrustrumBase frustrum)
- {
- return this.maFrustrums.Contains(frustrum);
- }
- public void UnityUpdate()
- {
- int num = 62;
- bool flag = false;
- if (this.mEntities != null)
- {
- for (int i = 0; i < num; i++)
- {
- List<SegmentEntity> list = this.mEntities[i];
- if (list != null && list.Count > 0)
- {
- int count = list.Count;
- for (int j = 0; j < count; j++)
- {
- SegmentEntity segmentEntity = list[j];
- if (segmentEntity != null && !segmentEntity.mbDelete)
- {
- if (segmentEntity.mbNeedsUnityUpdate)
- {
- segmentEntity.UnityUpdate();
- flag = true;
- }
- }
- }
- }
- }
- }
- if (!flag)
- {
- this.mbNeedsUnityUpdate = false;
- }
- }
- public void UnitySuspended()
- {
- if (this.mEntities != null)
- {
- for (int i = 0; i < 62; i++)
- {
- List<SegmentEntity> list = this.mEntities[i];
- if (list != null && list.Count > 0)
- {
- int count = list.Count;
- for (int j = 0; j < count; j++)
- {
- SegmentEntity segmentEntity = list[j];
- if (segmentEntity != null && !segmentEntity.mbDelete)
- {
- if (segmentEntity.mbNeedsUnityUpdate)
- {
- segmentEntity.UnitySuspended();
- }
- }
- }
- }
- }
- }
- this.mbUnityUpdateSuspended = true;
- }
- public void ScanForFluid()
- {
- if (!this.mbInitialGenerationComplete || this.mbDestroyed || this.mbIsEmpty)
- {
- return;
- }
- if (WorldScript.mbIsServer)
- {
- if (this.maCubes.Length != 4096)
- {
- Debug.LogError("maCubes are a stupid size");
- }
- int num = this.maCubes.Length;
- for (int i = 0; i < 4096; i++)
- {
- if (CubeHelper.IsTypeConsideredFluid((int)this.maCubes[i]))
- {
- this.AddedFluid();
- return;
- }
- }
- }
- }
- private bool AttemptToMoveFluid(int x, int y, int z, int lXDir, int lYDir, int lZDir)
- {
- if (this.mbDestroyed)
- {
- return false;
- }
- if (this.maCubes == null)
- {
- Debug.LogWarning(this.mbInitialGenerationComplete);
- Debug.LogWarning(this.mbDestroyed);
- Debug.LogWarning(this.mbIsEmpty);
- Debug.LogError("Error, why is cubes null?");
- }
- ushort num = 1;
- ushort num2 = this.maCubes[this.GetIndexLocal(x, y, z)];
- if (CubeHelper.IsTypeConsideredFluid((int)num2))
- {
- ushort num3 = num2;
- this.lbFoundFluid = true;
- Segment segment = this;
- long num4 = this.baseX + (long)x + (long)lXDir;
- long num5 = this.baseY + (long)y + (long)lYDir;
- long num6 = this.baseZ + (long)z + (long)lZDir;
- if (num4 < this.baseX || num5 < this.baseY || num6 < this.baseZ || num4 >= this.baseX + 16L || num5 >= this.baseY + 16L || num6 >= this.baseZ + 16L)
- {
- segment = WorldScript.instance.GetSegment(num4, num5, num6);
- if (segment == null)
- {
- return false;
- }
- if (!segment.IsSegmentInAGoodState())
- {
- return false;
- }
- }
- ushort cube = segment.GetCube(num4, num5, num6);
- if (cube == num)
- {
- WorldScript.instance.BuildFromEntity(this, (long)x, (long)y, (long)z, num, global::TerrainData.GetDefaultValue(num));
- WorldScript.instance.BuildFromEntity(segment, num4, num5, num6, num3, global::TerrainData.GetDefaultValue(num3));
- this.mnFluidMoved++;
- return true;
- }
- }
- return false;
- }
- private void DoFluidTick()
- {
- if (this.FluidSleepTicks > 0)
- {
- this.FluidSleepTicks--;
- return;
- }
- int num = 0;
- for (int i = 0; i < 4096; i++)
- {
- if (this.maCubes[i] == 168)
- {
- num++;
- }
- }
- this.mnMagmaCount = num;
- if (WorldScript.mbIsServer)
- {
- this.mnFluidMoved = 0;
- this.lbFoundFluid = false;
- for (int j = 0; j < 16; j++)
- {
- for (int k = 0; k < 16; k++)
- {
- for (int l = 0; l < 16; l++)
- {
- if (!this.AttemptToMoveFluid(j, l, k, 0, -1, 0))
- {
- if (!this.AttemptToMoveFluid(j, l, k, -1, -1, 0))
- {
- if (!this.AttemptToMoveFluid(j, l, k, 1, -1, 0))
- {
- if (!this.AttemptToMoveFluid(j, l, k, 0, -1, -1))
- {
- if (this.AttemptToMoveFluid(j, l, k, 0, -1, 1))
- {
- }
- }
- }
- }
- }
- }
- }
- }
- }
- if (!this.lbFoundFluid)
- {
- this.mbHasFluid = false;
- this.DecideIfSegmentNeedsLF();
- this.TicksWithNoFluidMovement = 0;
- }
- else if (this.mnFluidMoved == 0)
- {
- this.TicksWithNoFluidMovement++;
- this.FluidSleepTicks = this.TicksWithNoFluidMovement;
- }
- else
- {
- this.TicksWithNoFluidMovement = 0;
- }
- }
- private void DecideIfSegmentNeedsLF()
- {
- this.mbNeedsLowFrequencyUpdate = false;
- if (this.mbHasFluid)
- {
- this.mbNeedsLowFrequencyUpdate = true;
- }
- if (this.mEntities != null)
- {
- this.mbNeedsLowFrequencyUpdate = true;
- }
- }
- public void LowFrequencyUpdate()
- {
- if (this.mbDestroyed)
- {
- return;
- }
- if (!this.mbInitialGenerationComplete)
- {
- return;
- }
- if (this.mrSaveDelay > 0f)
- {
- this.mrSaveDelay -= LowFrequencyThread.mrPreviousUpdateTimeStep;
- if (this.mrSaveDelay <= 0f)
- {
- this.RequestSave();
- }
- }
- bool flag = false;
- if (this.mEntities != null)
- {
- for (int i = 0; i < 62; i++)
- {
- if (this.mbDestroyed)
- {
- break;
- }
- if (i >= this.mEntities.Length)
- {
- break;
- }
- List<SegmentEntity> list = this.mEntities[i];
- if (list != null && list.Count > 0)
- {
- int count = list.Count;
- for (int j = 0; j < count; j++)
- {
- if (j > list.Count)
- {
- break;
- }
- SegmentEntity segmentEntity = list[j];
- if (segmentEntity != null && !segmentEntity.mbDelete)
- {
- if (segmentEntity.mbNeedsLowFrequencyUpdate)
- {
- segmentEntity.LowFrequencyUpdate();
- flag = true;
- }
- }
- }
- }
- }
- }
- if (!flag)
- {
- this.mbNeedsLowFrequencyUpdate = false;
- if (this.mrSaveDelay > 0f)
- {
- this.RequestSave();
- }
- }
- if (this.mbHasFluid)
- {
- this.mbNeedsLowFrequencyUpdate = true;
- if (this.maCubes != null)
- {
- this.DoFluidTick();
- }
- }
- }
- public void TimePassedUpdate()
- {
- }
- public bool CheckDiskPresence()
- {
- return this.mArchive.Exists(DiskArchiver.GetSegmentFileName(this));
- }
- public void AddDynamicCustomObject(DynamicCustomObject obj)
- {
- if (this.mDynamicCustoms == null)
- {
- this.mDynamicCustoms = new Dictionary<int, List<DynamicCustomObject>>();
- }
- List<DynamicCustomObject> list;
- if (!this.mDynamicCustoms.TryGetValue(obj.mnDesignId, out list))
- {
- list = new List<DynamicCustomObject>();
- this.mDynamicCustoms.Add(obj.mnDesignId, list);
- }
- list.Add(obj);
- }
- public void RemoveDynamicCustomObject(int x, int y, int z)
- {
- }
- public void AddGameObjectWrapper(GameObjectWrapper wrapper)
- {
- if (this.mGameObjects == null)
- {
- this.mGameObjects = new List<GameObjectWrapper>();
- }
- this.mGameObjects.Add(wrapper);
- }
- public GameObjectWrapper GetGameObjectWrapper(long x, long y, long z)
- {
- if (this.mGameObjects != null)
- {
- for (int i = 0; i < this.mGameObjects.Count; i++)
- {
- GameObjectWrapper gameObjectWrapper = this.mGameObjects[i];
- if (gameObjectWrapper.mnX == x && gameObjectWrapper.mnY == y && gameObjectWrapper.mnZ == z)
- {
- return gameObjectWrapper;
- }
- }
- }
- return null;
- }
- public void RemoveObjectsAt(long x, long y, long z)
- {
- if (this.mEntities != null)
- {
- SegmentEntity segmentEntity = this.SearchEntity(x, y, z);
- if (segmentEntity != null && EntityManager.IsEntitySlotBased(segmentEntity.mType))
- {
- Debug.Log("Removing Entity Type " + segmentEntity.mType.ToString());
- EntityManager.instance.DeleteEntity(segmentEntity);
- return;
- }
- }
- if (this.mGameObjects != null)
- {
- for (int i = 0; i < this.mGameObjects.Count; i++)
- {
- GameObjectWrapper gameObjectWrapper = this.mGameObjects[i];
- if (gameObjectWrapper.mnX == x && gameObjectWrapper.mnY == y && gameObjectWrapper.mnZ == z)
- {
- Debug.Log("Removing game object " + gameObjectWrapper.mType.ToString());
- this.mGameObjects.RemoveAt(i);
- i--;
- SpawnableObjectManagerScript.instance.ClearObject(gameObjectWrapper);
- }
- }
- }
- }
- public GameObjectWrapper SearchObject(long x, long y, long z)
- {
- if (this.mGameObjects == null)
- {
- return null;
- }
- for (int i = 0; i < this.mGameObjects.Count; i++)
- {
- GameObjectWrapper gameObjectWrapper = this.mGameObjects[i];
- if (gameObjectWrapper.mnX == x && gameObjectWrapper.mnY == y && gameObjectWrapper.mnZ == z)
- {
- return gameObjectWrapper;
- }
- }
- return null;
- }
- public void AddEntity(SegmentEntity entity, bool doSave)
- {
- if (this.mEntities == null)
- {
- this.mEntities = new List<SegmentEntity>[62];
- }
- if (this.mEntities[(int)entity.mType] == null)
- {
- this.mEntities[(int)entity.mType] = new List<SegmentEntity>();
- }
- List<SegmentEntity> list = this.mEntities[(int)entity.mType];
- lock (list)
- {
- int count = list.Count;
- int i;
- for (i = 0; i < count; i++)
- {
- if (list[i] == null)
- {
- list[i] = entity;
- break;
- }
- }
- if (i == count)
- {
- list.Add(entity);
- }
- }
- if (doSave && entity.ShouldSave() && WorldScript.mbIsServer)
- {
- this.RequestDelayedSave();
- }
- if (entity.mbNeedsUnityUpdate || entity.mbNeedsLowFrequencyUpdate || entity.mbNeedsTimePassedUpdate)
- {
- this.mbNeedsUnityUpdate = (this.mbNeedsUnityUpdate || entity.mbNeedsUnityUpdate);
- this.mbNeedsLowFrequencyUpdate = (this.mbNeedsLowFrequencyUpdate || entity.mbNeedsLowFrequencyUpdate);
- this.mbNeedsTimePassedUpdate = (this.mbNeedsTimePassedUpdate || entity.mbNeedsTimePassedUpdate);
- if (!this.mbIsQueuedForUpdate)
- {
- WorldScript.instance.mSegmentUpdater.AddSegment(this);
- }
- }
- }
- public void RemoveEntity(SegmentEntity entity)
- {
- if (this.mEntities == null)
- {
- return;
- }
- List<SegmentEntity> list = this.mEntities[(int)entity.mType];
- if (list == null)
- {
- return;
- }
- int count = list.Count;
- for (int i = 0; i < count; i++)
- {
- if (list[i] == entity)
- {
- list[i] = null;
- break;
- }
- }
- }
- public SegmentEntity FetchEntity(eSegmentEntity type, long x, long y, long z)
- {
- if (this.mEntities == null)
- {
- return null;
- }
- if (type >= (eSegmentEntity)this.mEntities.Length || type < eSegmentEntity.InjectionEntity)
- {
- Debug.LogError("FetchEntity Error? : Type was " + type);
- return null;
- }
- List<SegmentEntity> list = this.mEntities[(int)type];
- if (list == null)
- {
- Debug.Log("No entities of type " + type + " found in segment - this shouldn't happen");
- return null;
- }
- int count = list.Count;
- for (int i = 0; i < count; i++)
- {
- SegmentEntity segmentEntity = list[i];
- if (segmentEntity != null && !segmentEntity.mbDelete)
- {
- if (segmentEntity.mnX == x && segmentEntity.mnY == y && segmentEntity.mnZ == z)
- {
- return segmentEntity;
- }
- }
- }
- Debug.Log("Warning, FetchEntity failed to find entity of type " + type.ToString());
- return null;
- }
- public SegmentEntity FetchEntity(eSegmentEntity type, int id)
- {
- if (this.mEntities == null)
- {
- return null;
- }
- if (type >= (eSegmentEntity)this.mEntities.Length)
- {
- throw new Exception("wait what?");
- }
- List<SegmentEntity> list = this.mEntities[(int)type];
- if (list == null)
- {
- return null;
- }
- int count = list.Count;
- for (int i = 0; i < count; i++)
- {
- SegmentEntity segmentEntity = list[i];
- if (segmentEntity != null && !segmentEntity.mbDelete)
- {
- if (segmentEntity.mnID == id)
- {
- return segmentEntity;
- }
- }
- }
- Debug.Log("Warning, FetchEntity failed to find entity of type " + type.ToString());
- return null;
- }
- public SegmentEntity SearchEntity(long x, long y, long z)
- {
- if (this.mEntities == null)
- {
- return null;
- }
- for (int i = 0; i < this.mEntities.Length; i++)
- {
- List<SegmentEntity> list = this.mEntities[i];
- if (list != null && list.Count > 0)
- {
- int count = list.Count;
- for (int j = 0; j < count; j++)
- {
- SegmentEntity segmentEntity = list[j];
- if (segmentEntity != null && !segmentEntity.mbDelete)
- {
- if (segmentEntity.mnX == x && segmentEntity.mnY == y && segmentEntity.mnZ == z)
- {
- return segmentEntity;
- }
- }
- }
- }
- }
- return null;
- }
- public bool HasEntities()
- {
- if (this.mEntities == null)
- {
- return false;
- }
- for (int i = 0; i < this.mEntities.Length; i++)
- {
- List<SegmentEntity> list = this.mEntities[i];
- if (list != null && list.Count > 0)
- {
- int count = list.Count;
- for (int j = 0; j < count; j++)
- {
- SegmentEntity segmentEntity = list[j];
- if (segmentEntity != null && !segmentEntity.mbDelete)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- public void AddDroppedItem(DroppedItemData droppedItem, bool doSave)
- {
- if (this.mDroppedItems == null)
- {
- this.mDroppedItems = new List<DroppedItemData>();
- }
- if (this.mDroppedItems.Count > 500)
- {
- return;
- }
- lock (this.mDroppedItems)
- {
- int count = this.mDroppedItems.Count;
- int i;
- for (i = 0; i < count; i++)
- {
- if (this.mDroppedItems[i] == null)
- {
- this.mDroppedItems[i] = droppedItem;
- break;
- }
- }
- if (i == count)
- {
- this.mDroppedItems.Add(droppedItem);
- }
- }
- if (!this.mbNeedsItemUpdate)
- {
- WorldScript.instance.mSegmentUpdater.AddItemSegment(this);
- }
- if (doSave)
- {
- this.RequestDelayedSave();
- }
- }
- public bool RemoveDroppedItem(DroppedItemData droppedItem)
- {
- if (this.mDroppedItems == null)
- {
- return false;
- }
- for (int i = 0; i < this.mDroppedItems.Count; i++)
- {
- if (this.mDroppedItems[i] == droppedItem)
- {
- this.mDroppedItems[i] = null;
- this.RequestDelayedSave();
- return true;
- }
- }
- return false;
- }
- public void AddMob(MobEntity mob)
- {
- if (this.mMobs == null)
- {
- this.mMobs = new List<MobEntity>();
- }
- if (this.mMobs.Count > 100)
- {
- return;
- }
- lock (this.mMobs)
- {
- int count = this.mMobs.Count;
- int i;
- for (i = 0; i < count; i++)
- {
- if (this.mMobs[i] == null)
- {
- this.mMobs[i] = mob;
- break;
- }
- }
- if (i == count)
- {
- this.mMobs.Add(mob);
- }
- }
- if (mob.ShouldSave())
- {
- this.RequestDelayedSave();
- }
- }
- public MobEntity GetMob(int id)
- {
- if (this.mMobs == null)
- {
- return null;
- }
- int count = this.mMobs.Count;
- for (int i = 0; i < count; i++)
- {
- MobEntity mobEntity = this.mMobs[i];
- if (mobEntity != null)
- {
- if (mobEntity.mnServerID == id)
- {
- return mobEntity;
- }
- }
- }
- return null;
- }
- public bool RemoveMob(MobEntity mob)
- {
- if (this.mMobs == null)
- {
- return false;
- }
- for (int i = 0; i < this.mMobs.Count; i++)
- {
- if (this.mMobs[i] == mob)
- {
- this.mMobs[i] = null;
- if (mob.ShouldSave())
- {
- this.RequestDelayedSave();
- }
- return true;
- }
- }
- return false;
- }
- public bool HasDroppedItems()
- {
- return this.mDroppedItems != null && this.mDroppedItems.Count > 0;
- }
- public void PushEntityUpdates(List<SegmentEntity> entityUpdateList)
- {
- if (this.mEntities == null)
- {
- return;
- }
- for (int i = 0; i < this.mEntities.Length; i++)
- {
- List<SegmentEntity> list = this.mEntities[i];
- if (list != null && list.Count > 0)
- {
- int count = list.Count;
- for (int j = 0; j < count; j++)
- {
- SegmentEntity segmentEntity = list[j];
- if (segmentEntity != null && !segmentEntity.mbDelete && segmentEntity.ShouldNetworkUpdate())
- {
- entityUpdateList.Add(segmentEntity);
- }
- }
- }
- }
- }
- public bool GetMemoryBlock()
- {
- if (this.maCubes == null)
- {
- if (this.maCubeData != null)
- {
- throw new AssertException("maCubes and maCubeData out of sync");
- }
- this.maCubes = new ushort[4096];
- this.maCubeData = new CubeData[4096];
- }
- return true;
- }
- public void DropMemoryBlock()
- {
- if (this.maCubes != null)
- {
- if (this.maCubeData == null)
- {
- throw new AssertException("maCubes and maCubeData out of sync");
- }
- this.maCubes = null;
- this.maCubeData = null;
- }
- }
- public bool QueueBuildOrder(ref BuildOrder order)
- {
- bool result;
- lock (this.mQueueLock)
- {
- try
- {
- if (this.maBuildOrders == null)
- {
- this.GetOrderBlock();
- }
- result = this.maBuildOrders.EnqueueAndIgnoreOverFlow(order);
- }
- catch (NullReferenceException)
- {
- Debug.LogWarning("Race condition, build order dropped");
- result = false;
- }
- }
- return result;
- }
- public bool GetOrderBlock()
- {
- bool result;
- lock (this.mQueueLock)
- {
- if (this.maBuildOrders == null)
- {
- WorldScript.instance.mOrderBlockManager.GetBlock(this, out this.maBuildOrders, out this.mnOrderBlockID);
- }
- result = (this.maBuildOrders != null);
- }
- return result;
- }
- public void DropOrderBlock()
- {
- lock (this.mQueueLock)
- {
- if (this.maBuildOrders != null)
- {
- WorldScript.instance.mOrderBlockManager.ReleaseBlock(this, this.mnOrderBlockID);
- this.maBuildOrders = null;
- }
- }
- }
- public void PopulateEmptySegment()
- {
- if (!this.mbIsEmpty)
- {
- throw new AssertException("Trying to populate empty segment that isn't? " + this.ToString());
- }
- if (!this.GetMemoryBlock())
- {
- throw new AssertException("Ran out of memory blocks!");
- }
- byte mSunLightLevel = (!this.mbSunLit) ? 0 : 15;
- for (int i = 0; i < this.maCubes.Length; i++)
- {
- this.maCubes[i] = 1;
- CubeData cubeData = default(CubeData);
- cubeData.meFlags = 0;
- cubeData.mSunLightLevel = mSunLightLevel;
- cubeData.mTorchLightLevel = 0;
- cubeData.mLavaLightLevel = 0;
- cubeData.mValue = 0;
- this.maCubeData[i] = cubeData;
- }
- this.mbIsEmpty = false;
- }
- public void RequestRegenLighting()
- {
- this.mnLightStatus = 0;
- this.mbRedoLight = true;
- this.mbLightingDirty = true;
- }
- public void RequestRegenLighting(int flag)
- {
- this.ClearLightStatus(flag);
- this.mbLightingDirty = true;
- }
- public void RequestRegenerateGraphics()
- {
- if (!this.mbInLocalFrustrum)
- {
- Debug.Log("Requesting graphics for segment which isn't in a player frustrum!");
- return;
- }
- this.mbGraphicsDirty = true;
- SegmentMeshCreator.instance.mMeshRenderer.KickRenderThread();
- }
- public void RequestSave()
- {
- if (!this.mbInitialGenerationComplete || this.mbDestroyed)
- {
- return;
- }
- if (!this.mbIsEmpty && this.maCubes == null)
- {
- Debug.LogError("This is the error we are seeing");
- }
- this.mrSaveDelay = 0f;
- this.mbSaveRequested = true;
- if (this.mMasterFrustrum != null)
- {
- this.mMasterFrustrum.mbRequiresSaving = true;
- }
- }
- public void RequestDelayedSave()
- {
- if (this.mbIsQueuedForUpdate && this.mbNeedsLowFrequencyUpdate)
- {
- if (this.mrSaveDelay <= 0f)
- {
- this.mrSaveDelay = 30f;
- }
- }
- else
- {
- this.RequestSave();
- }
- }
- public bool IsLightComplete()
- {
- return this.mnLightStatus == 127;
- }
- public bool HasLightStatus(int flag)
- {
- return (this.mnLightStatus & flag) > 0;
- }
- public void SetLightStatus(int flag)
- {
- this.mnLightStatus |= flag;
- }
- public void ClearLightStatus(int flag)
- {
- this.mnLightStatus &= ~flag;
- }
- public void SetLightEdge(int flag)
- {
- this.mnLightEdgeStatus |= flag;
- }
- public int GetIndexLocal(int x, int y, int z)
- {
- return (y << 8) + (z << 4) + x;
- }
- public int GetIndex(long x, long y, long z)
- {
- int num = (int)(x - this.baseX);
- int num2 = (int)(y - this.baseY);
- int num3 = (int)(z - this.baseZ);
- return (num2 << 8) + (num3 << 4) + num;
- }
- public ushort GetCube(long x, long y, long z)
- {
- if (!this.mbInitialGenerationComplete || this.mbDestroyed)
- {
- return 0;
- }
- if (this.mbIsEmpty)
- {
- return 1;
- }
- return this.maCubes[this.GetIndex(x, y, z)];
- }
- public CubeData GetCubeData(long x, long y, long z)
- {
- if (!this.mbInitialGenerationComplete || this.mbDestroyed || this.mbIsEmpty)
- {
- return default(CubeData);
- }
- return this.maCubeData[this.GetIndex(x, y, z)];
- }
- public ushort GetCubeNoChecking(int x, int y, int z)
- {
- return this.maCubes[(y << 8) + (z << 4) + x];
- }
- public CubeData GetCubeDataNoChecking(int x, int y, int z)
- {
- return this.maCubeData[(y << 8) + (z << 4) + x];
- }
- public void GetCubeDataNoChecking(int x, int y, int z, out ushort cubeType, out CubeData cubeData)
- {
- int num = (y << 8) + (z << 4) + x;
- cubeType = this.maCubes[num];
- cubeData = this.maCubeData[num];
- }
- public void SetCubeValueNoChecking(int x, int y, int z, ushort lData)
- {
- this.maCubeData[(y << 8) + (z << 4) + x].mValue = lData;
- }
- public void SetCubeTypeNoChecking(int x, int y, int z, ushort lType)
- {
- this.maCubes[(y << 8) + (z << 4) + x] = lType;
- }
- public void SetCubeTypeNoChecking(int x, int y, int z, ushort lType, ushort val)
- {
- int num = (y << 8) + (z << 4) + x;
- this.maCubes[num] = lType;
- this.maCubeData[num].mValue = val;
- }
- public void SetFlagsNoChecking(int x, int y, int z, byte flags)
- {
- this.maCubeData[(y << 8) + (z << 4) + x].meFlags = flags;
- }
- public void SpawnMobGraphics()
- {
- if (this.mMobs != null)
- {
- for (int i = 0; i < this.mMobs.Count; i++)
- {
- MobEntity mobEntity = this.mMobs[i];
- if (mobEntity != null)
- {
- if (mobEntity.mWrapper == null)
- {
- mobEntity.SpawnGameObject();
- }
- }
- }
- }
- }
- public void CreateGameObject()
- {
- WorldFrustrum mPlayerFrustrum = WorldScript.instance.mPlayerFrustrum;
- if (mPlayerFrustrum == null)
- {
- throw new Exception("Tried creating game object on dedicated server!");
- }
- long num = this.baseX - mPlayerFrustrum.mOriginX;
- long num2 = this.baseY - mPlayerFrustrum.mOriginY;
- long num3 = this.baseZ - mPlayerFrustrum.mOriginZ;
- Vector3 position = new Vector3((float)num, (float)num2, (float)num3);
- GameObject gameObject = UnityEngine.Object.Instantiate(WorldScript.SegmentPrefab, position, Quaternion.identity) as GameObject;
- this.gameObject = gameObject.GetComponent<SegmentObjectScript>();
- this.gameObject.mSegment = this;
- HUDManager.mnNodesInstantiated++;
- this.mbHasGameObject = true;
- }
- private void DropGameObject()
- {
- if (this.gameObject != null)
- {
- this.DestroyMeshAndGameObject(this.gameObject.gameObject);
- this.gameObject = null;
- this.mbHasGameObject = false;
- }
- }
- public void ResetObjectUsage()
- {
- if (this.gameObject != null)
- {
- this.gameObject.inUse = false;
- }
- if (this.childObjects != null)
- {
- foreach (SegmentObjectScript current in this.childObjects)
- {
- current.inUse = false;
- }
- }
- }
- public void DropUnusedObjects()
- {
- if (this.childObjects != null)
- {
- for (int i = this.childObjects.Count - 1; i >= 0; i--)
- {
- SegmentObjectScript segmentObjectScript = this.childObjects[i];
- if (!segmentObjectScript.inUse)
- {
- this.DestroyMeshAndGameObject(segmentObjectScript.gameObject);
- this.childObjects.RemoveAt(i);
- }
- }
- }
- if (this.gameObject != null && !this.gameObject.inUse)
- {
- this.DestroyMeshAndGameObject(this.gameObject.gameObject);
- this.gameObject = null;
- this.mbHasGameObject = false;
- }
- }
- private void DestroyMeshAndGameObject(GameObject obj)
- {
- MeshFilter component = obj.GetComponent<MeshFilter>();
- if (component != null)
- {
- Mesh mesh = component.mesh;
- if (mesh != null)
- {
- UnityEngine.Object.Destroy(mesh);
- }
- }
- UnityEngine.Object.Destroy(obj);
- }
- public SegmentObjectScript GetChildObject(SegmentRendererLayerType layerType, int index)
- {
- int num = 0;
- if (index == 0 && (this.gameObject == null || !this.gameObject.inUse))
- {
- if (this.gameObject == null)
- {
- this.CreateGameObject();
- }
- this.gameObject.layerType = layerType;
- return this.gameObject;
- }
- if (this.gameObject.layerType == layerType)
- {
- num++;
- }
- SegmentObjectScript segmentObjectScript = null;
- if (this.childObjects != null)
- {
- foreach (SegmentObjectScript current in this.childObjects)
- {
- if (current.layerType == layerType)
- {
- if (num == index)
- {
- segmentObjectScript = current;
- break;
- }
- num++;
- }
- }
- }
- if (segmentObjectScript == null)
- {
- segmentObjectScript = this.CreateChildObject(layerType);
- }
- return segmentObjectScript;
- }
- private SegmentObjectScript CreateChildObject(SegmentRendererLayerType layerType)
- {
- if (this.childObjects == null)
- {
- this.childObjects = new List<SegmentObjectScript>();
- }
- GameObject gameObject = UnityEngine.Object.Instantiate(WorldScript.SegmentPrefab, this.gameObject.transform.position, Quaternion.identity) as GameObject;
- gameObject.transform.localPosition = Vector3.zero;
- SegmentObjectScript component = gameObject.GetComponent<SegmentObjectScript>();
- component.layerType = layerType;
- this.childObjects.Add(component);
- HUDManager.mnChildrenInstantiated++;
- return component;
- }
- public override string ToString()
- {
- return string.Concat(new object[]
- {
- "Segment [",
- this.baseX - 4611686017890516992L,
- "][",
- this.baseY - 4611686017890516992L,
- "][",
- this.baseZ - 4611686017890516992L,
- "]"
- });
- }
- public bool IsEligibleForSave()
- {
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment