public Belt(PrebuildData prebuild, BeltComponent belt, int out1, int in1, int in2, int in3) { pd = prebuild; Init(); this.belt = belt; type = EDataType.Belt; beltOut = out1; beltIn1 = in1; beltIn2 = in2; beltIn3 = in3; isBelt = true; }
static PrebuildData GetPreDate(BeltComponent bc, EntityData ed) { PrebuildData prebuild = default(PrebuildData); prebuild.protoId = (short)ed.protoId; prebuild.modelIndex = (short)ed.modelIndex; prebuild.pos = ed.pos; prebuild.pos2 = VectorLF3.zero; prebuild.rot = ed.rot; prebuild.rot2 = Quaternion.identity; return(prebuild); }
public override void ProcessPacket(BeltUpdatePickupItemsPacket packet, NebulaConnection conn) { CargoTraffic traffic = GameMain.galaxy.PlanetById(packet.PlanetId)?.factory?.cargoTraffic; if (traffic != null) { //Iterate though belt updates and remove target items for (int i = 0; i < packet.BeltUpdates.Length; i++) { if (packet.BeltUpdates[i].BeltId >= traffic.beltPool.Length) { return; } BeltComponent beltComponent = traffic.beltPool[packet.BeltUpdates[i].BeltId]; CargoPath cargoPath = traffic.GetCargoPath(beltComponent.segPathId); int ItemId = packet.BeltUpdates[i].ItemId; //Check if belt exists if (cargoPath != null) { // Search downstream for target item for (int k = beltComponent.segIndex + beltComponent.segPivotOffset; k <= (beltComponent.segIndex + beltComponent.segLength - 1); k++) { if (cargoPath.TryPickItem(k - 4 - 1, 12, ItemId, out _, out _) != 0) { return; } } // Search upstream for target item for (int k = beltComponent.segIndex + beltComponent.segPivotOffset - 1; k >= beltComponent.segIndex; k--) { if (cargoPath.TryPickItem(k - 4 - 1, 12, ItemId, out _, out _) != 0) { return; } } Log.Warn($"BeltUpdatePickupItem: Cannot pick item{ItemId} on belt{packet.BeltUpdates[i].BeltId}, planet{packet.PlanetId}"); } } } }
public static List <BuildPreview> Paste(Vector3 targetPos, float yaw, bool pasteInserters = true) { PlayerAction_Build actionBuild = GameMain.data.mainPlayer.controller.actionBuild; pastedEntities.Clear(); parallelPastedEntities.Clear(); previews.Clear(); InserterPoses.ResetOverrides(); Vector2 targetSpr = targetPos.ToSpherical(); float yawRad = yaw * Mathf.Deg2Rad; for (int i = 0; i < data.copiedBuildings.Count; i++) { PasteBuilding(i, data.copiedBuildings[i], targetSpr, yaw); } for (int i = 0; i < data.copiedBelts.Count; i++) { PasteBelt(i, data.copiedBelts[i], targetSpr, yaw); } if (pasteInserters && data.copiedInserters.Count > 16) { var maxThreads = Environment.ProcessorCount - 1; var runningThreads = 0; var next = -1; ManualResetEvent done = new ManualResetEvent(false); for (int i = 0; i < maxThreads; i++) { ThreadPool.QueueUserWorkItem(_ => { int item; Interlocked.Increment(ref runningThreads); try { PlayerAction_Build ab = BuildLogic.ClonePlayerAction_Build(actionBuild); while ((item = Interlocked.Increment(ref next)) < data.copiedInserters.Count) { PasteInserter(ab, item, data.copiedInserters[item], yaw); } } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { if (Interlocked.Decrement(ref runningThreads) == 0) { done.Set(); } } }); } done.WaitOne(); } else if (pasteInserters) { for (var i = 0; i < data.copiedInserters.Count; i++) { PasteInserter(actionBuild, i, data.copiedInserters[i], yaw); } } // merge the inserter pastedEntities in the main dictionaries foreach (var item in parallelPastedEntities) { previews.Add(item.Value.buildPreview); pastedEntities.Add(item.Key, item.Value); } // after creating the belt previews this restore the correct connection to other belts and buildings foreach (BeltCopy belt in data.copiedBelts) { BuildPreview preview = pastedEntities[belt.originalId].buildPreview; if (belt.outputId != 0 && pastedEntities.TryGetValue(belt.outputId, out PastedEntity otherEntity) && Vector3.Distance(preview.lpos, otherEntity.buildPreview.lpos) < 10) // if the belts are too far apart ignore connection { preview.output = otherEntity.buildPreview; var otherBelt = data.copiedBelts[otherEntity.index]; if (otherBelt.backInputId == belt.originalId) { preview.outputToSlot = 1; } if (otherBelt.leftInputId == belt.originalId) { preview.outputToSlot = 2; } if (otherBelt.rightInputId == belt.originalId) { preview.outputToSlot = 3; } } if (belt.connectedBuildingId != 0 && pastedEntities.TryGetValue(belt.connectedBuildingId, out PastedEntity otherBuilding)) { if (belt.connectedBuildingIsOutput) { preview.output = otherBuilding.buildPreview; preview.outputToSlot = belt.connectedBuildingSlot; } else { preview.input = otherBuilding.buildPreview; preview.inputFromSlot = belt.connectedBuildingSlot; } } bool beltHasInput = pastedEntities.ContainsKey(belt.backInputId) || pastedEntities.ContainsKey(belt.leftInputId) || pastedEntities.ContainsKey(belt.rightInputId); bool beltHasOutput = pastedEntities.ContainsKey(belt.outputId); if (!beltHasInput || !beltHasOutput) { int found = actionBuild.nearcdLogic.GetBuildingsInAreaNonAlloc(preview.lpos, 0.34f, _nearObjectIds, false); for (int x = 0; x < found; x++) { int overlappingEntityId = _nearObjectIds[x]; if (overlappingEntityId <= 0) { continue; } EntityData overlappingEntityData = actionBuild.factory.entityPool[overlappingEntityId]; if (overlappingEntityData.beltId <= 0) { continue; } BeltComponent overlappingBelt = actionBuild.factory.cargoTraffic.beltPool[overlappingEntityData.beltId]; bool overlappingBeltHasInput = (overlappingBelt.backInputId + overlappingBelt.leftInputId + overlappingBelt.rightInputId) != 0; bool overlappingBeltHasOutput = overlappingBelt.outputId != 0; if ((beltHasOutput && !overlappingBeltHasOutput) || (beltHasInput && !overlappingBeltHasInput)) { // found overlapping belt that can be 'replaced' to connect to existing belts preview.coverObjId = overlappingEntityId; preview.ignoreCollider = true; preview.willCover = true; } } } } return(previews); }
public static BeltCopy CopyBelt(EntityData sourceEntity, EntityData referenceEntity) { PlanetFactory factory = GameMain.data.localPlanet.factory; ItemProto sourceEntityProto = LDB.items.Select(sourceEntity.protoId); if (!sourceEntityProto.prefabDesc.isBelt) { return(null); } BeltComponent belt = factory.cargoTraffic.beltPool[sourceEntity.beltId]; Vector2 sourceSprPos = sourceEntity.pos.ToSpherical(); BeltCopy copiedBelt = new BeltCopy() { originalId = sourceEntity.id, protoId = sourceEntityProto.ID, itemProto = sourceEntityProto, altitude = Mathf.RoundToInt(2 * (sourceEntity.pos.magnitude - GameMain.localPlanet.realRadius - 0.2f) / 1.3333333f), backInputId = factory.cargoTraffic.beltPool[belt.backInputId].entityId, leftInputId = factory.cargoTraffic.beltPool[belt.leftInputId].entityId, rightInputId = factory.cargoTraffic.beltPool[belt.rightInputId].entityId, outputId = factory.cargoTraffic.beltPool[belt.outputId].entityId, }; factory.ReadObjectConn(sourceEntity.id, 0, out bool isOutput, out int otherId, out int otherSlot); if (otherId > 0 && factory.entityPool[otherId].beltId == 0) { copiedBelt.connectedBuildingId = otherId; copiedBelt.connectedBuildingIsOutput = isOutput; copiedBelt.connectedBuildingSlot = otherSlot; } factory.ReadObjectConn(sourceEntity.id, 1, out isOutput, out otherId, out otherSlot); if (otherId > 0 && factory.entityPool[otherId].beltId == 0) { copiedBelt.connectedBuildingId = otherId; copiedBelt.connectedBuildingIsOutput = isOutput; copiedBelt.connectedBuildingSlot = otherSlot; } if (sourceEntity.id == referenceEntity.id) { data.referencePos = sourceSprPos; } else { copiedBelt.originalSegmentCount = sourceSprPos.GetSegmentsCount(); copiedBelt.cursorRelativePos = (sourceSprPos - data.referencePos).Clamp(); } data.copiedBelts.Add(copiedBelt); factory.ReadObjectConn(sourceEntity.id, 4, out _, out otherId, out _); if (otherId != 0) { // only copy belt to belt inserter if both belts are part fo the blueprint factory.ReadObjectConn(otherId, 0, out _, out int endId, out _); factory.ReadObjectConn(otherId, 1, out _, out int startId, out _); int idToFind = sourceEntity.id == endId ? startId : endId; if (data.copiedBelts.FindIndex(x => x.originalId == idToFind) != -1) { EntityData inserterEntity = factory.entityPool[otherId]; CopyInserter(inserterEntity, sourceEntity); } } hasData = true; return(copiedBelt); }
private void ChangedComponentType() { IMachComponent component = null; switch (ComponentType) { case DeviceComponentType.Bearing: { component = new BearingComponent() { Component = new BearingClass() { Guid = Guid.NewGuid() }, Guid = Guid.NewGuid(), Name = "新建轴承" }; break; } case DeviceComponentType.Belt: { component = new BeltComponent() { Component = new BeltClass(), Guid = Guid.NewGuid(), Name = "新建皮带" }; break; } case DeviceComponentType.Gear: { component = new GearComponent() { Component = new GearClass(), Guid = Guid.NewGuid(), Name = "新建齿轮" }; break; } case DeviceComponentType.Impeller: { component = new ImpellerComponent() { Component = new ImpellerClass(), Guid = Guid.NewGuid(), Name = "新建叶轮" }; break; } case DeviceComponentType.Motor: { component = new MotorComponent() { Component = new MotorClass(), Guid = Guid.NewGuid(), Name = "新建电机" }; break; } } if (component != null) { //ComponentType = component.ComponentType; var oldcomponent = selectedshaft.Component.SelectedComponent; int index = selectedshaft.Component.MachComponents.IndexOf(oldcomponent); selectedshaft.Component.MachComponents.Insert(index, component); selectedshaft.Component.MachComponents.Remove(oldcomponent); selectedshaft.Component.SelectedComponent = component; } }
private void AddComponent() { IMachComponent component = null; switch (ComponentType) { case DeviceComponentType.Bearing: { component = new BearingComponent() { Component = new BearingClass() { Guid = Guid.NewGuid() }, Guid = Guid.NewGuid(), Name = "新建轴承" }; BearingComponent = component as BearingComponent; break; } case DeviceComponentType.Belt: { component = new BeltComponent() { Component = new BeltClass(), Guid = Guid.NewGuid(), Name = "新建皮带" }; BeltComponent = component as BeltComponent; break; } case DeviceComponentType.Gear: { component = new GearComponent() { Component = new GearClass(), Guid = Guid.NewGuid(), Name = "新建齿轮" }; GearComponent = component as GearComponent; break; } case DeviceComponentType.Impeller: { component = new ImpellerComponent() { Component = new ImpellerClass(), Guid = Guid.NewGuid(), Name = "新建叶轮" }; ImpellerComponent = component as ImpellerComponent; break; } case DeviceComponentType.Motor: { component = new MotorComponent() { Component = new MotorClass(), Guid = Guid.NewGuid(), Name = "新建电机" }; MotorComponent = component as MotorComponent; break; } } if (component != null) { selectedshaft.Component.MachComponents.Add(component); selectedshaft.Component.SelectedComponent = component; } }
public static bool ColorPath1(CargoTraffic __instance, int beltId) { var _this = __instance; if (beltId == 0) { return(false); } _this.RemoveBeltRenderer(beltId); CargoPath cargoPath = _this.GetCargoPath(_this.beltPool[beltId].segPathId); if (cargoPath == null) { return(false); } BeltComponent beltComponent = _this.beltPool[beltId]; double num = (double)((float)beltComponent.segIndex - 0.5f); double num2 = (double)((float)(beltComponent.segIndex + beltComponent.segLength) - 0.5f); if (cargoPath.closed) { if (num < 4.0) { num = 4.0; } if (num2 + 9.0 + 1.0 >= (double)cargoPath.pathLength) { num2 = (double)(cargoPath.pathLength - 5); } } else { if (num < 4.0) { num = 4.0; } if (num2 + 5.0 >= (double)cargoPath.pathLength) { num2 = (double)(cargoPath.pathLength - 5 - 1); } } int num3 = 1; int num4 = 0; if (beltComponent.mainInputId > 0 && beltComponent.outputId > 0) { Vector3 pos = _this.factory.entityPool[_this.beltPool[beltComponent.mainInputId].entityId].pos; Vector3 pos2 = _this.factory.entityPool[beltComponent.entityId].pos; Vector3 pos3 = _this.factory.entityPool[_this.beltPool[beltComponent.outputId].entityId].pos; float num5 = Vector3.Angle(pos - pos2, pos3 - pos2); if (num5 > 165f) { num3 = 1; num4 = 0; } else if (num5 > 135f) { num3 = 2; num4 = 1; } else if (num5 > 100f) { num3 = 4; num4 = 2; } else { num3 = 8; num4 = 3; } if (beltComponent.segIndex + beltComponent.segLength == cargoPath.pathLength && cargoPath.outputPath != null) { num3 = 8; num4 = 3; } } if (beltComponent.speed <= 1) { } else if (beltComponent.speed <= 2) { num4 += 4; } else if (beltComponent.speed <= 5) { num4 += 8; } else if (beltComponent.speed <= 10) { num4 += 12; } else { num4 += 16; } var tmpBeltAnchors = new BeltAnchor[9]; for (int i = 0; i <= num3; i++) { double num6 = num + (num2 - num) / (double)num3 * (double)i; int num7 = (int)(Math.Floor(num6) + 1E-06); double num8 = num6 - (double)num7; int num9 = (num8 >= 1E-05) ? (num7 + 1) : num7; tmpBeltAnchors[i].t = (float)num6; if (num9 == num7) { tmpBeltAnchors[i].pos = cargoPath.pointPos[num7]; tmpBeltAnchors[i].rot = cargoPath.pointRot[num7]; } else { tmpBeltAnchors[i].pos = Vector3.Lerp(cargoPath.pointPos[num7], cargoPath.pointPos[num9], (float)num8); tmpBeltAnchors[i].rot = Quaternion.Slerp(cargoPath.pointRot[num7], cargoPath.pointRot[num9], (float)num8); } } _this.beltPool[beltId].modelBatchIndex = num4 + 1; _this.beltPool[beltId].modelIndex = _this.beltRenderingBatch[num4].AddNode(tmpBeltAnchors); return(false); }
public static void ConcurrentConnectBelt(int threadIndex, PastedEntity pastedEntity) { var actionBuild = _abs[threadIndex]; var pasteIndex = pastedEntity.pasteIndex; var belt = pastedEntity.sourceBelt; BuildPreview buildPreview = pastedEntity.buildPreview; buildPreview.output = null; buildPreview.outputToSlot = -1; buildPreview.outputFromSlot = 0; buildPreview.outputOffset = 0; buildPreview.input = null; buildPreview.inputFromSlot = -1; buildPreview.inputToSlot = 1; buildPreview.inputOffset = 0; buildPreview.coverObjId = 0; buildPreview.ignoreCollider = false; buildPreview.willCover = false; var pastedBackInputId = PASTE_INDEX_MULTIPLIER * pasteIndex + belt.backInputId; var pastedLeftInputId = PASTE_INDEX_MULTIPLIER * pasteIndex + belt.leftInputId; var pastedRightInputId = PASTE_INDEX_MULTIPLIER * pasteIndex + belt.rightInputId; var pastedOutputId = PASTE_INDEX_MULTIPLIER * pasteIndex + belt.outputId; var pastedConnectedBuildingId = PASTE_INDEX_MULTIPLIER * pasteIndex + belt.connectedBuildingId; PastedEntity otherPastedEntity; if (pastedOutputId != 0 && BlueprintManager.pastedEntities.TryGetValue(pastedOutputId, out otherPastedEntity) && otherPastedEntity.type == EPastedType.BELT && otherPastedEntity.status != EPastedStatus.REMOVE && Vector3.Distance(buildPreview.lpos, otherPastedEntity.buildPreview.lpos) < 10) // if the belts are too far apart ignore connection { buildPreview.output = otherPastedEntity.buildPreview; var otherBelt = otherPastedEntity.sourceBelt; if (otherBelt.backInputId == belt.originalId) { buildPreview.outputToSlot = 1; } if (otherBelt.leftInputId == belt.originalId) { buildPreview.outputToSlot = 2; } if (otherBelt.rightInputId == belt.originalId) { buildPreview.outputToSlot = 3; } } if (pastedConnectedBuildingId != 0 && BlueprintManager.pastedEntities.TryGetValue(pastedConnectedBuildingId, out otherPastedEntity) && otherPastedEntity.type == EPastedType.BUILDING && otherPastedEntity.status != EPastedStatus.REMOVE) { if (belt.connectedBuildingIsOutput) { buildPreview.output = otherPastedEntity.buildPreview; buildPreview.outputToSlot = belt.connectedBuildingSlot; } else { buildPreview.input = otherPastedEntity.buildPreview; buildPreview.inputFromSlot = belt.connectedBuildingSlot; } otherPastedEntity.connectedEntities.TryAdd(pastedEntity.pasteId, pastedEntity); //pastedEntity.connectedEntities.Add(otherPastedEntity); } bool beltHasInput = BlueprintManager.pastedEntities.ContainsKey(pastedBackInputId) || BlueprintManager.pastedEntities.ContainsKey(pastedLeftInputId) || BlueprintManager.pastedEntities.ContainsKey(pastedRightInputId); bool beltHasOutput = BlueprintManager.pastedEntities.ContainsKey(pastedOutputId); if (!beltHasInput || !beltHasOutput) { var nearObjId = _nearObjectIds[threadIndex]; int found = actionBuild.nearcdLogic.GetBuildingsInAreaNonAlloc(buildPreview.lpos, 0.34f, nearObjId, false); for (int x = 0; x < found; x++) { int overlappingEntityId = nearObjId[x]; if (overlappingEntityId <= 0) { continue; } EntityData overlappingEntityData = actionBuild.factory.entityPool[overlappingEntityId]; if (overlappingEntityData.beltId <= 0) { continue; } BeltComponent overlappingBelt = actionBuild.factory.cargoTraffic.beltPool[overlappingEntityData.beltId]; bool overlappingBeltHasInput = (overlappingBelt.backInputId + overlappingBelt.leftInputId + overlappingBelt.rightInputId) != 0; bool overlappingBeltHasOutput = overlappingBelt.outputId != 0; if ((beltHasOutput && !overlappingBeltHasOutput) || (beltHasInput && !overlappingBeltHasInput)) { // found overlapping belt that can be 'replaced' to connect to existing belts buildPreview.coverObjId = overlappingEntityId; buildPreview.ignoreCollider = true; buildPreview.willCover = true; } } } }