public void Add(T item) { if (Data == null || Length >= Data.Length) { MPLog.LogError("MPArray overflow : " + typeof(T)); } Data[Length] = item; ++Length; }
public static PillarData LoadData(string path, string dataName) { FileStream stream = File.Open(path, FileMode.Open); PillarData data = new PillarData(); data.DataName = dataName; int readOffset = 0; //read setting int settingSize = data.setting.byteSize(); if (stream.Length - readOffset >= settingSize) { byte[] buff = new byte[settingSize]; int len = stream.Read(buff, readOffset, settingSize); data.setting.Reset(buff); readOffset += len; } else { MPLog.LogError("load setting failed"); return(null); } //read header int headerSize = data.setting.maxX * data.setting.maxZ; if (stream.Length - readOffset < headerSize * sizeof(uint)) { MPLog.LogError("load header failed"); return(null); } data.tree = new QuadTreeBase[headerSize]; byte[] bBuff = new byte[1] { 0 }; byte bRootLeafBuff = 1 << 4; for (int i = 0; i < headerSize; ++i) { //root mask stream.Read(bBuff, 0, 1); // if ((bBuff[0] & bRootLeafBuff) > 0) { data.tree[i] = new QuadTreeLeafSerializable(stream); } else { data.tree[i] = new QuadTreeNodeSerializable(bBuff[0], stream); } } MPLog.Log("load successed !"); stream.Close(); return(data); }
public void Enqueue(T item) { if (mHead == null) { mHead = item; return; } else if (item.Priority <= mHead.Priority) { item.Next = mHead; mHead = item; return; } else if (mHead.Next == null) { mHead.Next = item; return; } T check = mHead; while (check.Next != null) { if (item.Priority <= check.Next.Priority) { item.Next = check.Next; check.Next = item; return; } else { check = check.Next; } } if (check.Next == null) { check.Next = item; } else { MPLog.LogError("item is not add into queue"); } }
//dynamically add pillars in //x ~ (0, setting.maxX * power(2, subdivision)), x ~ (0, setting.maxZ * power(2, subdivision)) public void AddPillar(int subdivision, int x, int z, OrderedSlices rawSlices) { //first grade int u = x >> subdivision; // x / power(2, subdivision); int v = z >> subdivision; int subx = x - u * (1 << subdivision); int subz = z - v * (1 << subdivision); --subdivision; int idx = (subx >> subdivision) * 2 + (subz >> subdivision); if (subdivision > 0) { if (Children[idx] is QuadTreeLeaf) { SubdividLeaf(idx); } QuadTreeNode node = (QuadTreeNode)Children[idx]; node.AddPillar(subdivision, subx, subz, rawSlices); } else { if (Children[idx] is QuadTreeNode) { MPLog.LogError("AddPillar leaf still a tree : " + subdivision); return; } QuadTreeLeaf leaf = (QuadTreeLeaf)Children[idx]; if (leaf.Slices != null) { HeightSlicePool.Push(leaf.Header, leaf.Slices); } leaf.Reset(rawSlices.Count, rawSlices.HashValue); for (int i = 0; i < rawSlices.Count; ++i) { leaf.Slices[i] = SliceAccessor.packVal(rawSlices[i].heightGrade, 0, 0, rawSlices[i].flag); } } }
public void Unify(float startHeight, float heightPerGrade) { SortSlices(); if (Count == 0) { MPLog.LogError("pillar is empty."); } //merge the slices, slices should be floor|ceiling|floor|ceiling....|floor bool bNeedMerge = true; while (bNeedMerge && Count > 0) { bNeedMerge = false; for (int i = 0; i < Count - 1; ++i) { if (this[i].flag == this[i + 1].flag) { if ((this[i].flag & SliceAccessor.SliceCeiling) > 0) {//ceiling use lower one RemoveAt(i + 1); } else {//floor use higher one RemoveAt(i); } bNeedMerge = true; break; } } } HashValue = 0; for (int i = 0; i < Count; ++i) { RawSlice slice = this[i]; slice.heightGrade = (ushort)Math.Ceiling((slice.height - startHeight) / heightPerGrade); HashValue += SliceAccessor.packVal(slice.heightGrade, 0, 0, slice.flag); } }