public static PrefabDesc GetPrefabDesc(BuildingCopy copiedBuilding) { ModelProto modelProto = LDB.models.Select(copiedBuilding.modelIndex); if (modelProto != null) { return(modelProto.prefabDesc); } else { return(copiedBuilding.itemProto.prefabDesc); } }
public static void PasteBuilding(int index, BuildingCopy building, Vector2 targetSpr, float yaw) { Vector2 newRelative = building.cursorRelativePos.Rotate(yaw * Mathf.Deg2Rad, building.originalSegmentCount); Vector2 sprPos = newRelative + targetSpr; float rawLatitudeIndex = (sprPos.x - Mathf.PI / 2) / 6.2831855f * 200; int latitudeIndex = Mathf.FloorToInt(Mathf.Max(0f, Mathf.Abs(rawLatitudeIndex) - 0.1f)); int newSegmentCount = PlanetGrid.DetermineLongitudeSegmentCount(latitudeIndex, 200); float sizeDeviation = building.originalSegmentCount / (float)newSegmentCount; sprPos = new Vector2(newRelative.x, newRelative.y * sizeDeviation) + targetSpr; Vector3 absoluteBuildingPos = sprPos.SnapToGrid(); Quaternion absoluteBuildingRot = Maths.SphericalRotation(absoluteBuildingPos, yaw + building.cursorRelativeYaw); PrefabDesc desc = GetPrefabDesc(building); BuildPreview bp = BuildPreview.CreateSingle(building.itemProto, desc, true); bp.ResetInfos(); bp.desc = desc; bp.item = building.itemProto; bp.recipeId = building.recipeId; bp.lpos = absoluteBuildingPos; bp.lrot = absoluteBuildingRot; Pose pose = new Pose(absoluteBuildingPos, absoluteBuildingRot); int objId = InserterPoses.AddOverride(pose, building.itemProto); pastedEntities.Add(building.originalId, new PastedEntity() { type = EPastedType.BUILDING, index = index, sourceBuilding = building, pose = pose, objId = objId, buildPreview = bp }); ActivateColliders(absoluteBuildingPos); previews.Add(bp); }
public static BuildingCopy CopyBuilding(EntityData sourceEntity, EntityData referenceEntity) { PlanetFactory factory = GameMain.data.localPlanet.factory; ItemProto sourceEntityProto = LDB.items.Select(sourceEntity.protoId); Vector3 sourcePos = sourceEntity.pos; Quaternion sourceRot = sourceEntity.rot; Quaternion zeroRot = Maths.SphericalRotation(sourcePos, 0f); float yaw = Vector3.SignedAngle(zeroRot.Forward(), sourceRot.Forward(), zeroRot.Up()); BuildingCopy copiedBuilding = new BuildingCopy() { originalId = sourceEntity.id, protoId = sourceEntityProto.ID, itemProto = sourceEntityProto, modelIndex = sourceEntity.modelIndex }; if (sourceEntity.assemblerId > 0) { copiedBuilding.recipeId = factory.factorySystem.assemblerPool[sourceEntity.assemblerId].recipeId; } else if (sourceEntity.labId > 0) { LabComponent labComponent = factory.factorySystem.labPool[sourceEntity.labId]; copiedBuilding.recipeId = ((!labComponent.researchMode) ? labComponent.recipeId : -1); } else if (sourceEntity.powerGenId > 0) { PowerGeneratorComponent powerGeneratorComponent = factory.powerSystem.genPool[sourceEntity.powerGenId]; if (powerGeneratorComponent.gamma) { copiedBuilding.recipeId = ((powerGeneratorComponent.productId <= 0) ? 0 : 1); } } else if (sourceEntity.powerExcId > 0) { copiedBuilding.recipeId = Mathf.RoundToInt(factory.powerSystem.excPool[sourceEntity.powerExcId].targetState); } else if (sourceEntity.ejectorId > 0) { copiedBuilding.recipeId = factory.factorySystem.ejectorPool[sourceEntity.ejectorId].orbitId; } else if (sourceEntity.stationId > 0) { StationComponent stationComponent = factory.transport.stationPool[sourceEntity.stationId]; for (int i = 0; i < stationComponent.slots.Length; i++) { if (stationComponent.slots[i].storageIdx != 0) { copiedBuilding.slotFilters.Add(new SlotFilter() { slotIndex = i, storageIdx = stationComponent.slots[i].storageIdx }); } } for (int i = 0; i < stationComponent.storage.Length; i++) { if (stationComponent.storage[i].itemId != 0) { copiedBuilding.stationSettings.Add(new StationSetting() { index = i, itemId = stationComponent.storage[i].itemId, max = stationComponent.storage[i].max, localLogic = stationComponent.storage[i].localLogic, remoteLogic = stationComponent.storage[i].remoteLogic }); } } } else if (sourceEntity.splitterId > 0) { // TODO: find a way to restore splitter settings // SplitterComponent splitterComponent = factory.cargoTraffic.splitterPool[sourceEntity.splitterId]; } Vector2 sourceSprPos = sourcePos.ToSpherical(); if (sourceEntity.id == referenceEntity.id) { data.referencePos = sourceSprPos; copiedBuilding.cursorRelativeYaw = yaw; } else { copiedBuilding.originalSegmentCount = sourceSprPos.GetSegmentsCount(); copiedBuilding.cursorRelativePos = (sourceSprPos - data.referencePos).Clamp(); copiedBuilding.cursorRelativeYaw = yaw; } data.copiedBuildings.Add(copiedBuilding); for (int i = 0; i < sourceEntityProto.prefabDesc.insertPoses.Length; i++) { factory.ReadObjectConn(sourceEntity.id, i, out bool _, out int otherObjId, out int _); if (otherObjId > 0) { EntityData inserterEntity = factory.entityPool[otherObjId]; CopyInserter(inserterEntity, sourceEntity); } } hasData = true; return(copiedBuilding); }
public static BuildingCopy CopyBuilding(BlueprintData data, EntityData sourceEntity, EntityData referenceEntity) { PlanetFactory factory = GameMain.data.localPlanet.factory; ItemProto sourceEntityProto = LDB.items.Select(sourceEntity.protoId); Vector3 sourcePos = sourceEntity.pos; Quaternion sourceRot = sourceEntity.rot; Quaternion zeroRot = Maths.SphericalRotation(sourcePos, 0f); float yaw = Vector3.SignedAngle(zeroRot.Forward(), sourceRot.Forward(), zeroRot.Up()); BuildingCopy copiedBuilding = new BuildingCopy() { originalId = sourceEntity.id, protoId = sourceEntityProto.ID, itemProto = sourceEntityProto, modelIndex = sourceEntity.modelIndex, }; var prefabDesc = BlueprintManager.GetPrefabDesc(copiedBuilding); if (sourceEntity.assemblerId > 0) { copiedBuilding.recipeId = factory.factorySystem.assemblerPool[sourceEntity.assemblerId].recipeId; } else if (sourceEntity.labId > 0) { LabComponent labComponent = factory.factorySystem.labPool[sourceEntity.labId]; copiedBuilding.recipeId = ((!labComponent.researchMode) ? labComponent.recipeId : -1); } else if (sourceEntity.powerGenId > 0) { PowerGeneratorComponent powerGeneratorComponent = factory.powerSystem.genPool[sourceEntity.powerGenId]; if (powerGeneratorComponent.gamma) { copiedBuilding.recipeId = ((powerGeneratorComponent.productId <= 0) ? 0 : 1); } } else if (sourceEntity.powerExcId > 0) { copiedBuilding.recipeId = Mathf.RoundToInt(factory.powerSystem.excPool[sourceEntity.powerExcId].targetState); } else if (sourceEntity.ejectorId > 0) { copiedBuilding.recipeId = factory.factorySystem.ejectorPool[sourceEntity.ejectorId].orbitId; } else if (sourceEntity.stationId > 0) { StationComponent stationComponent = factory.transport.stationPool[sourceEntity.stationId]; copiedBuilding.stationConfig = new StationConfig() { workEnergyPerTick = factory.powerSystem.consumerPool[stationComponent.pcId].workEnergyPerTick, tripRangeDrones = stationComponent.tripRangeDrones, tripRangeShips = stationComponent.tripRangeShips, warpEnableDist = stationComponent.warpEnableDist, warperNecessary = stationComponent.warperNecessary, includeOrbitCollector = stationComponent.includeOrbitCollector, deliveryDrones = stationComponent.deliveryDrones, deliveryShips = stationComponent.deliveryShips }; for (int i = 0; i < stationComponent.slots.Length; i++) { if (stationComponent.slots[i].storageIdx != 0) { copiedBuilding.slotFilters.Add(new SlotFilter() { slotIndex = i, storageIdx = stationComponent.slots[i].storageIdx }); } } for (int i = 0; i < stationComponent.storage.Length; i++) { if (stationComponent.storage[i].itemId != 0) { copiedBuilding.stationSettings.Add(new StationSetting() { index = i, itemId = stationComponent.storage[i].itemId, max = stationComponent.storage[i].max, localLogic = stationComponent.storage[i].localLogic, remoteLogic = stationComponent.storage[i].remoteLogic }); } } } else if (sourceEntity.splitterId > 0) { var splitterComponent = factory.cargoTraffic.splitterPool[sourceEntity.splitterId]; copiedBuilding.splitterSettings = new SplitterSettings() { inPriority = splitterComponent.inPriority, outPriority = splitterComponent.outPriority, outFilter = splitterComponent.outFilter }; var slots = new List <int>(4) { splitterComponent.beltA, splitterComponent.beltB, splitterComponent.beltC, splitterComponent.beltD }; if (copiedBuilding.splitterSettings.inPriority) { copiedBuilding.splitterSettings.inPrioritySlot = slots.IndexOf(splitterComponent.input0); } if (copiedBuilding.splitterSettings.outPriority) { copiedBuilding.splitterSettings.outPrioritySlot = slots.IndexOf(splitterComponent.output0); } // TODO: find a way to restore splitter settings // SplitterComponent splitterComponent = factory.cargoTraffic.splitterPool[sourceEntity.splitterId]; } else if (sourceEntity.storageId > 0) { copiedBuilding.recipeId = factory.factoryStorage.storagePool[sourceEntity.storageId].bans; } Vector2 sourceSprPos = sourcePos.ToSpherical(); if (sourceEntity.id == referenceEntity.id) { data.referencePos = sourceSprPos; copiedBuilding.cursorRelativeYaw = yaw; } else { copiedBuilding.originalSegmentCount = sourceSprPos.GetSegmentsCount(); copiedBuilding.cursorRelativePos = (sourceSprPos - data.referencePos).Clamp(); copiedBuilding.cursorRelativeYaw = yaw; } data.copiedBuildings.Add(copiedBuilding); for (int i = 0; i < sourceEntityProto.prefabDesc.insertPoses.Length; i++) { factory.ReadObjectConn(sourceEntity.id, i, out bool _, out int otherObjId, out int _); if (otherObjId > 0) { EntityData inserterEntity = factory.entityPool[otherObjId]; CopyInserter(data, inserterEntity, sourceEntity); } } if (sourceEntityProto.prefabDesc.multiLevel) { copiedBuilding.altitude = Mathf.RoundToInt((sourceEntity.pos.magnitude - GameMain.localPlanet.realRadius - 0.2f) / prefabDesc.lapJoint.magnitude); if (copiedBuilding.altitude > 0) { copiedBuilding.connectedBuildingId = referenceEntity.id; } factory.ReadObjectConn(sourceEntity.id, 15, out bool _, out int otherObjId, out int _); if (otherObjId > 0) { EntityData stackedEntity = factory.entityPool[otherObjId]; CopyBuilding(data, stackedEntity, sourceEntity); } } return(copiedBuilding); }
public static PastedEntity ConcurrentPasteBuilding(int threadIndex, BuildingCopy building, Vector2 targetSpr, float yaw, int pasteIndex) { var actionBuild = _abs[threadIndex]; int pasteId = PASTE_INDEX_MULTIPLIER * pasteIndex + building.originalId; if (!BlueprintManager.pastedEntities.TryGetValue(pasteId, out PastedEntity pastedEntity)) { PrefabDesc desc = BlueprintManager.GetPrefabDesc(building); BuildPreview bp = BuildPreview.CreateSingle(building.itemProto, desc, true); bp.ResetInfos(); bp.desc = desc; bp.item = building.itemProto; bp.recipeId = building.recipeId; pastedEntity = new PastedEntity() { pasteIndex = pasteIndex, pasteId = pasteId, status = EPastedStatus.NEW, type = EPastedType.BUILDING, sourceBuilding = building, buildPreview = bp, }; BlueprintManager.pastedEntities.TryAdd(pasteId, pastedEntity); lock (actionBuild.buildPreviews) { actionBuild.buildPreviews.Add(bp); } } else { pastedEntity.status = EPastedStatus.UPDATE; } Vector2 newRelative = building.cursorRelativePos.Rotate(yaw * Mathf.Deg2Rad, building.originalSegmentCount); Vector2 sprPos = newRelative + targetSpr; int newSegmentCount = Util.GetSegmentsCount(sprPos); float sizeDeviation = building.originalSegmentCount / (float)newSegmentCount; sprPos = new Vector2(newRelative.x, newRelative.y * sizeDeviation) + targetSpr; Vector3 absoluteBuildingPos = sprPos.SnapToGrid(); Quaternion absoluteBuildingRot = Maths.SphericalRotation(absoluteBuildingPos, yaw + building.cursorRelativeYaw); absoluteBuildingPos += absoluteBuildingRot * (building.altitude * pastedEntity.buildPreview.desc.lapJoint); Pose pose = new Pose(absoluteBuildingPos, absoluteBuildingRot); pastedEntity.objId = InserterPoses.AddOverride(pose, building.itemProto); pastedEntity.pose = pose; pastedEntity.buildPreview.lpos = absoluteBuildingPos; pastedEntity.buildPreview.lrot = absoluteBuildingRot; pastedEntity.buildPreview.condition = EBuildCondition.Ok; pastedEntity.connectedEntities.Clear(); return(pastedEntity); }