private static InserterPosition GetPositions(PlayerAction_Build __instance, PlanetFactory ___factory, PlanetAuxData ___planetAux, NearColliderLogic ___nearcdLogic, BuildPreview buildPreview, CachedInserter cachedInserter) { Vector3 absoluteBuildingPos; Quaternion absoluteBuildingRot; // When using AdvancedBuildDestruct mod, all buildPreviews are positioned 'absolutely' on the planet surface. // In 'normal' mode the buildPreviews are relative to __instance.previewPose. // This means that in 'normal' mode the (only) buildPreview is always positioned at {0,0,0} if (buildPreview.lpos == Vector3.zero) { absoluteBuildingPos = __instance.previewPose.position; absoluteBuildingRot = __instance.previewPose.rotation; } else { absoluteBuildingPos = buildPreview.lpos; absoluteBuildingRot = buildPreview.lrot; } InserterPosition position = null; if (currentPositionCache.Count > 0) { position = currentPositionCache.Dequeue(); } bool isCacheValid = position != null && position.cachedInserter == cachedInserter && position.absoluteBuildingPos == absoluteBuildingPos && position.absoluteBuildingRot == absoluteBuildingRot; if (isCacheValid) { nextPositionCache.Enqueue(position); return(position); } var posDelta = cachedInserter.posDelta; var pos2Delta = cachedInserter.pos2Delta; Vector3 absoluteInserterPos = absoluteBuildingPos + absoluteBuildingRot * cachedInserter.posDelta; Vector3 absoluteInserterPos2 = absoluteBuildingPos + absoluteBuildingRot * cachedInserter.pos2Delta; Quaternion absoluteInserterRot = absoluteBuildingRot * cachedInserter.rot; Quaternion absoluteInserterRot2 = absoluteBuildingRot * cachedInserter.rot2; int startSlot = cachedInserter.startSlot; int endSlot = cachedInserter.endSlot; short pickOffset = cachedInserter.pickOffset; short insertOffset = cachedInserter.insertOffset; // Find the desired belt/building position // As delta doesn't work over distance, re-trace the Grid Snapped steps from the original // to find the target belt/building for this inserters other connection var testPos = absoluteBuildingPos; // Note: rotates each move relative to the rotation of the new building for (int u = 0; u < cachedInserter.snapCount; u++) { testPos = ___planetAux.Snap(testPos + absoluteBuildingRot * cachedInserter.snapMoves[u], true, false); } // Find the other entity at the target location int otherId = 0; // find building nearby int found = ___nearcdLogic.GetBuildingsInAreaNonAlloc(testPos, 0.2f, _nearObjectIds); // find nearest building float maxDistance = 0.2f; for (int x = 0; x < found; x++) { var id = _nearObjectIds[x]; float distance; ItemProto proto; if (id == 0 || id == buildPreview.objId) { continue; } else if (id > 0) { EntityData entityData = ___factory.entityPool[id]; proto = LDB.items.Select((int)entityData.protoId); distance = Vector3.Distance(entityData.pos, testPos); } else { PrebuildData prebuildData = ___factory.prebuildPool[-id]; proto = LDB.items.Select((int)prebuildData.protoId); if (proto.prefabDesc.isBelt) { // ignore unbuilt belts continue; } distance = Vector3.Distance(prebuildData.pos, testPos); } // ignore entitites that ore not (built) belts or don't have inserterPoses if ((proto.prefabDesc.isBelt == cachedInserter.otherIsBelt || proto.prefabDesc.insertPoses.Length > 0) && distance < maxDistance) { otherId = id; maxDistance = distance; } } if (otherId != 0) { var buildingId = buildPreview.objId; if (buildingId == 0) { // the original calculatePose code doesn't correctly handle calculation where one of the entity is a BuildPreview // as it has objId 0 and is not registered in either the entityPool or prebuildPool // To address that we override the 4 critical methods GetObjectPose/GetObjectProtoId/ObjectIsBelt/GetLocalInserts // only for this specific execution of CalculatePose, by returning the expected value for the current buildPreview overridePoseMethods = true; overriddenProto = buildPreview.item; overriddenPose = new Pose(absoluteBuildingPos, absoluteBuildingRot); } if (cachedInserter.incoming) { CalculatePose(__instance, otherId, buildingId); } else { CalculatePose(__instance, buildingId, otherId); } overridePoseMethods = false; bool hasNearbyPose = false; if (__instance.posePairs.Count > 0) { float minDistance = 1000f; PlayerAction_Build.PosePair bestFit = new PlayerAction_Build.PosePair(); for (int j = 0; j < __instance.posePairs.Count; ++j) { var posePair = __instance.posePairs[j]; if ( (cachedInserter.incoming && cachedInserter.endSlot != posePair.endSlot) || (!cachedInserter.incoming && cachedInserter.startSlot != posePair.startSlot) ) { continue; } float startDistance = Vector3.Distance(posePair.startPose.position, absoluteInserterPos); float endDistance = Vector3.Distance(posePair.endPose.position, absoluteInserterPos2); float poseDistance = startDistance + endDistance; if (poseDistance < minDistance) { minDistance = poseDistance; bestFit = posePair; hasNearbyPose = true; } } if (hasNearbyPose) { // if we were able to calculate a close enough sensible pose // use that instead of the (visually) imprecise default absoluteInserterPos = bestFit.startPose.position; absoluteInserterPos2 = bestFit.endPose.position; absoluteInserterRot = bestFit.startPose.rotation; absoluteInserterRot2 = bestFit.endPose.rotation * Quaternion.Euler(0.0f, 180f, 0.0f); pickOffset = (short)bestFit.startOffset; insertOffset = (short)bestFit.endOffset; startSlot = bestFit.startSlot; endSlot = bestFit.endSlot; posDelta = Quaternion.Inverse(absoluteBuildingRot) * (absoluteInserterPos - absoluteBuildingPos); pos2Delta = Quaternion.Inverse(absoluteBuildingRot) * (absoluteInserterPos2 - absoluteBuildingPos); } } } position = new InserterPosition() { cachedInserter = cachedInserter, absoluteBuildingPos = absoluteBuildingPos, absoluteBuildingRot = absoluteBuildingRot, otherId = otherId, posDelta = posDelta, pos2Delta = pos2Delta, absoluteInserterPos = absoluteInserterPos, absoluteInserterPos2 = absoluteInserterPos2, absoluteInserterRot = absoluteInserterRot, absoluteInserterRot2 = absoluteInserterRot2, pickOffset = pickOffset, insertOffset = insertOffset, startSlot = startSlot, endSlot = endSlot, }; nextPositionCache.Enqueue(position); return(position); }
public static InserterPosition GetPositions(PlayerAction_Build actionBuild, InserterCopy copiedInserter, float yawRad, int pasteIndex, bool connectToPasted) { var pastedEntities = BlueprintManager.pastedEntities; var player = actionBuild.player; var pastedReferenceEntityId = BlueprintManager_Paste.PASTE_INDEX_MULTIPLIER * pasteIndex + copiedInserter.referenceBuildingId; var pastedReferenceEntity = pastedEntities[pastedReferenceEntityId]; var pastedReferenceEntityBuildPreview = pastedReferenceEntity.buildPreview; Quaternion absoluteBuildingRot = pastedReferenceEntity.pose.rotation; Vector3 absoluteBuildingPos = pastedReferenceEntity.pose.position; Vector2 absoluteBuildingPosSpr = absoluteBuildingPos.ToSpherical(); var posDelta = copiedInserter.posDelta.Rotate(yawRad, copiedInserter.posDeltaCount); Vector3 absoluteInserterPos = absoluteBuildingPosSpr .ApplyDelta(posDelta, copiedInserter.posDeltaCount) .ToCartesian(GameMain.localPlanet.realRadius + 0.2f); var pos2Delta = copiedInserter.pos2Delta.Rotate(yawRad, copiedInserter.pos2DeltaCount); Vector3 absoluteInserterPos2 = absoluteBuildingPosSpr .ApplyDelta(pos2Delta, copiedInserter.pos2DeltaCount) .ToCartesian(GameMain.localPlanet.realRadius + 0.2f); if (pastedReferenceEntity.sourceBuilding == null) { absoluteBuildingRot = Maths.SphericalRotation(absoluteBuildingPos, yawRad * Mathf.Rad2Deg); } Quaternion absoluteInserterRot = absoluteBuildingRot * copiedInserter.rot; Quaternion absoluteInserterRot2 = absoluteBuildingRot * copiedInserter.rot2; int startSlot = copiedInserter.startSlot; int endSlot = copiedInserter.endSlot; short pickOffset = copiedInserter.pickOffset; short insertOffset = copiedInserter.insertOffset; var referenceObjId = pastedReferenceEntity.objId; var otherPastedId = 0; var otherEntityId = 0; var otherObjId = 0; var pastedPickTargetId = BlueprintManager_Paste.PASTE_INDEX_MULTIPLIER * pasteIndex + copiedInserter.pickTarget; var pastedInsertTargetId = BlueprintManager_Paste.PASTE_INDEX_MULTIPLIER * pasteIndex + copiedInserter.insertTarget; if (pastedEntities.ContainsKey(pastedPickTargetId) && pastedEntities.ContainsKey(pastedInsertTargetId)) { // cool we copied both source and target of the inserters otherPastedId = pastedPickTargetId == pastedReferenceEntityId ? pastedInsertTargetId : pastedPickTargetId; otherObjId = pastedEntities[otherPastedId].objId; } else { // Find the other entity at the target location var nearcdLogic = actionBuild.nearcdLogic; var factory = actionBuild.factory; // Find the desired belt/building position // As delta doesn't work over distance, re-trace the Grid Snapped steps from the original // to find the target belt/building for this inserters other connection var otherPosDelta = copiedInserter.otherPosDelta.Rotate(yawRad, copiedInserter.otherPosDeltaCount); Vector3 testPos = absoluteBuildingPosSpr .ApplyDelta(otherPosDelta, copiedInserter.otherPosDeltaCount) .SnapToGrid(); if (connectToPasted) { var compatibleType = copiedInserter.otherIsBelt ? EPastedType.BELT : EPastedType.BUILDING; foreach (var pastedEntity in BlueprintManager.pastedEntities.Values) { // find the first compatible entity that will not been removed, that has in the same/previous/next pasteIndex and that is near enough if (pastedEntity.type == compatibleType && pastedEntity.status != EPastedStatus.REMOVE && pastedEntity.pasteId != pastedReferenceEntityId && Math.Abs(pastedEntity.pasteIndex - pasteIndex) <= 1 && Vector3.Distance(pastedEntity.pose.position, testPos) < 0.2) { // found a pasted entity that we can connect to ! otherPastedId = pastedEntity.pasteId; otherObjId = pastedEntity.objId; break; } } } if (otherObjId == 0) { int[] _nearObjectIds = new int[256]; // find building nearby int found = nearcdLogic.GetBuildingsInAreaNonAlloc(testPos, 0.2f, _nearObjectIds, false); // find nearest building float maxDistance = 1f; for (int x = 0; x < found; x++) { var id = _nearObjectIds[x]; float distance; ItemProto proto; if (id == 0 || id == pastedReferenceEntityBuildPreview.objId) { continue; } else if (id > 0) { EntityData entityData = factory.entityPool[id]; proto = LDB.items.Select((int)entityData.protoId); // ignore buildings without inserter poses if (!proto.prefabDesc.isBelt && proto.prefabDesc.insertPoses.Length == 0) { continue; } distance = Vector3.Distance(entityData.pos, testPos); } else { PrebuildData prebuildData = factory.prebuildPool[-id]; proto = LDB.items.Select((int)prebuildData.protoId); // ignore unbuilt belts and buildings without inserter poses if (proto.prefabDesc.isBelt || proto.prefabDesc.insertPoses.Length == 0) { continue; } distance = Vector3.Distance(prebuildData.pos, testPos); } // ignore entitites that ore not (built) belts or don't have inserterPoses if ((proto.prefabDesc.isBelt == copiedInserter.otherIsBelt || proto.prefabDesc.insertPoses.Length > 0) && distance < maxDistance) { otherObjId = otherEntityId = id; maxDistance = distance; } } } } if (otherObjId != 0) { if (copiedInserter.incoming) { InserterPoses.CalculatePose(actionBuild, otherObjId, referenceObjId); } else { InserterPoses.CalculatePose(actionBuild, referenceObjId, otherObjId); } bool hasNearbyPose = false; if (actionBuild.posePairs.Count > 0) { float minDistance = 1000f; PlayerAction_Build.PosePair bestFit = new PlayerAction_Build.PosePair(); for (int j = 0; j < actionBuild.posePairs.Count; ++j) { var posePair = actionBuild.posePairs[j]; if ( (copiedInserter.incoming && copiedInserter.endSlot != posePair.endSlot && copiedInserter.endSlot != -1) || (!copiedInserter.incoming && copiedInserter.startSlot != posePair.startSlot && copiedInserter.startSlot != -1) ) { continue; } float startDistance = Vector3.Distance(posePair.startPose.position, absoluteInserterPos); float endDistance = Vector3.Distance(posePair.endPose.position, absoluteInserterPos2); float poseDistance = startDistance + endDistance; if (poseDistance < minDistance) { minDistance = poseDistance; bestFit = posePair; hasNearbyPose = true; } } if (hasNearbyPose) { // if we were able to calculate a close enough sensible pose // use that instead of the (visually) imprecise default absoluteInserterPos = bestFit.startPose.position; absoluteInserterPos2 = bestFit.endPose.position; absoluteInserterRot = bestFit.startPose.rotation; absoluteInserterRot2 = bestFit.endPose.rotation * Quaternion.Euler(0.0f, 180f, 0.0f); pickOffset = (short)bestFit.startOffset; insertOffset = (short)bestFit.endOffset; startSlot = bestFit.startSlot; endSlot = bestFit.endSlot; posDelta = bestFit.startPose.position.ToSpherical() - absoluteBuildingPosSpr; pos2Delta = bestFit.endPose.position.ToSpherical() - absoluteBuildingPosSpr; } } } InserterPosition position = new InserterPosition() { copiedInserter = copiedInserter, absoluteBuildingPos = absoluteBuildingPos, absoluteBuildingRot = absoluteBuildingRot, posDelta = posDelta, pos2Delta = pos2Delta, absoluteInserterPos = absoluteInserterPos, absoluteInserterPos2 = absoluteInserterPos2, absoluteInserterRot = absoluteInserterRot, absoluteInserterRot2 = absoluteInserterRot2, pickOffset = pickOffset, insertOffset = insertOffset, startSlot = startSlot, endSlot = endSlot, condition = EBuildCondition.Ok }; if (otherEntityId != 0) { Vector3 forward = absoluteInserterPos2 - absoluteInserterPos; Pose pose; pose.position = Vector3.Lerp(absoluteInserterPos, absoluteInserterPos2, 0.5f); pose.rotation = Quaternion.LookRotation(forward, absoluteInserterPos.normalized); var colliderData = copiedInserter.itemProto.prefabDesc.buildColliders[0]; colliderData.ext = new Vector3(colliderData.ext.x, colliderData.ext.y, Vector3.Distance(absoluteInserterPos2, absoluteInserterPos) * 0.5f + colliderData.ext.z - 0.5f); if (copiedInserter.otherIsBelt) { if (copiedInserter.incoming) { colliderData.pos.z -= 0.4f; colliderData.ext.z += 0.4f; } else { colliderData.pos.z += 0.4f; colliderData.ext.z += 0.4f; } } if (colliderData.ext.z < 0.1f) { colliderData.ext.z = 0.1f; } colliderData.pos = pose.position + pose.rotation * colliderData.pos; colliderData.q = pose.rotation * colliderData.q; int mask = 165888; Collider[] _tmp_cols = new Collider[16]; int collisionsFound = Physics.OverlapBoxNonAlloc(colliderData.pos, colliderData.ext, _tmp_cols, colliderData.q, mask, QueryTriggerInteraction.Collide); PlanetPhysics physics2 = player.planetData.physics; for (int j = 0; j < collisionsFound; j++) { physics2.GetColliderData(_tmp_cols[j], out ColliderData colliderData2); if (colliderData2.objId != 0 && colliderData2.objId != otherEntityId && colliderData2.usage == EColliderUsage.Build) { position.condition = EBuildCondition.Collide; otherPastedId = 0; otherEntityId = 0; break; } } } position.inputEntityId = copiedInserter.incoming ? otherEntityId : 0; position.inputPastedId = copiedInserter.incoming ? otherPastedId : pastedReferenceEntityId; position.outputEntityId = copiedInserter.incoming ? 0 : otherEntityId; position.outputPastedId = copiedInserter.incoming ? pastedReferenceEntityId : otherPastedId; return(position); }
public static void PlayerAction_BuildNotifyBuiltPrefix(PlayerAction_Build __instance, int postObjId, PlanetAuxData ___planetAux) { var entityBuilt = pc.player.factory.entityPool[postObjId]; ModelProto modelProto = LDB.models.Select(entityBuilt.modelIndex); var prefabDesc = modelProto.prefabDesc; if (!prefabDesc.isInserter) { // Check for pending inserter requests if (PatchCopyInserters.pendingInserters.Count > 0) { var factory = pc.player.factory; for (int i = pendingInserters.Count - 1; i >= 0; i--) // Reverse loop for removing found elements { var pi = pendingInserters[i]; // Is the NotifyBuilt assembler in the expected position for this pending inserter? var distance = Vector3.Distance(entityBuilt.pos, pi.AssemblerPos); if (distance < 0.2) { var assemblerId = entityBuilt.id; // Create inserter Prebuild data var pbdata = new PrebuildData(); pbdata.protoId = (short)pi.ci.protoId; pbdata.modelIndex = (short)LDB.items.Select(pi.ci.protoId).ModelIndex; pbdata.insertOffset = pi.ci.insertOffset; pbdata.pickOffset = pi.ci.pickOffset; pbdata.filterId = pi.ci.filterId; pbdata.refCount = pi.ci.refCount; // Calculate inserter start and end positions from stored deltas and the building's rotation pbdata.pos = ___planetAux.Snap(entityBuilt.pos + entityBuilt.rot * pi.ci.posDelta, true, false); pbdata.pos2 = ___planetAux.Snap(entityBuilt.pos + entityBuilt.rot * pi.ci.pos2Delta, true, false); // Get inserter rotation relative to the building's pbdata.rot = entityBuilt.rot * pi.ci.rot; pbdata.rot2 = entityBuilt.rot * pi.ci.rot2; if (!pi.ci.incoming) { CalculatePose(__instance, assemblerId, pi.otherId); } else { CalculatePose(__instance, pi.otherId, assemblerId); } if (__instance.posePairs.Count > 0) { float minDistance = 1000f; PlayerAction_Build.PosePair bestFit = new PlayerAction_Build.PosePair(); bool hasNearbyPose = false; for (int j = 0; j < __instance.posePairs.Count; ++j) { if (__instance.posePairs[j].startSlot != pi.ci.startSlot || __instance.posePairs[j].endSlot != pi.ci.endSlot) { continue; } float startDistance = Vector3.Distance(__instance.posePairs[j].startPose.position, pbdata.pos); float endDistance = Vector3.Distance(__instance.posePairs[j].endPose.position, pbdata.pos2); float poseDistance = startDistance + endDistance; if (poseDistance < minDistance) { minDistance = poseDistance; bestFit = __instance.posePairs[j]; hasNearbyPose = true; } } if (hasNearbyPose) { // if we were able to calculate a close enough sensible pose // use that instead of the (visually) ugly default pbdata.pos = bestFit.startPose.position; pbdata.pos2 = bestFit.endPose.position; pbdata.rot = bestFit.startPose.rotation; pbdata.rot2 = bestFit.endPose.rotation * Quaternion.Euler(0.0f, 180f, 0.0f); } } // Check the player has the item in inventory, no cheating here var itemcount = pc.player.package.GetItemCount(pi.ci.protoId); // If player has none; skip this request, as we dont create prebuild ghosts, must avoid confusion if (itemcount > 0) { var qty = 1; pc.player.package.TakeTailItems(ref pi.ci.protoId, ref qty); int pbCursor = factory.AddPrebuildData(pbdata); // Add the inserter request to Prebuild pool // Otherslot -1 will try to find one, otherwise could cache this from original assembler if it causes problems if (pi.ci.incoming) { factory.WriteObjectConn(-pbCursor, 0, true, assemblerId, -1); // assembler connection factory.WriteObjectConn(-pbCursor, 1, false, pi.otherId, -1); // other connection } else { factory.WriteObjectConn(-pbCursor, 0, false, assemblerId, -1); // assembler connection factory.WriteObjectConn(-pbCursor, 1, true, pi.otherId, -1); // other connection } } pendingInserters.RemoveAt(i); } } } } }
public static void PlayerAction_BuildSetCopyInfoPostfix(PlayerAction_Build __instance, ref PlanetFactory ___factory, PlanetAuxData ___planetAux, int objectId, int protoId) { cachedInserters.Clear(); // Remove previous copy info if (objectId < 0) // Copied item is a ghost, no inserters to cache { return; } var sourceEntityProto = LDB.items.Select(protoId); // Ignore building without inserter slots if (sourceEntityProto.prefabDesc.insertPoses.Length == 0) { return; } var sourceEntityId = objectId; var sourceEntity = ___factory.entityPool[sourceEntityId]; var sourcePos = sourceEntity.pos; var sourceRot = sourceEntity.rot; // Find connected inserters var inserterPool = ___factory.factorySystem.inserterPool; var entityPool = ___factory.entityPool; for (int i = 1; i < ___factory.factorySystem.inserterCursor; i++) { if (inserterPool[i].id == i) { var inserter = inserterPool[i]; var inserterEntity = entityPool[inserter.entityId]; var pickTarget = inserter.pickTarget; var insertTarget = inserter.insertTarget; if (pickTarget == sourceEntityId || insertTarget == sourceEntityId) { bool incoming = insertTarget == sourceEntityId; var otherId = incoming ? pickTarget : insertTarget; // The belt or other building this inserter is attached to var otherPos = entityPool[otherId].pos; // Store the Grid-Snapped moves from assembler to belt/other int path = 0; Vector3[] snaps = new Vector3[6]; var snappedPointCount = ___planetAux.SnapLineNonAlloc(sourcePos, otherPos, ref path, snaps); Vector3 lastSnap = sourcePos; Vector3[] snapMoves = new Vector3[snappedPointCount]; for (int s = 0; s < snappedPointCount; s++) { // note: reverse rotation of the delta so that rotation works Vector3 snapMove = Quaternion.Inverse(sourceRot) * (snaps[s] - lastSnap); snapMoves[s] = snapMove; lastSnap = snaps[s]; } // Cache info for this inserter var ci = new CachedInserter(); ci.incoming = incoming; ci.protoId = inserterEntity.protoId; // rotations + deltas relative to the source building's rotation ci.rot = Quaternion.Inverse(sourceRot) * inserterEntity.rot; ci.rot2 = Quaternion.Inverse(sourceRot) * inserter.rot2; ci.posDelta = Quaternion.Inverse(sourceRot) * (inserterEntity.pos - sourcePos); // Delta from copied building to inserter pos ci.pos2Delta = Quaternion.Inverse(sourceRot) * (inserter.pos2 - sourcePos); // Delta from copied building to inserter pos2 ItemProto itemProto = LDB.items.Select(ci.protoId); ci.refCount = Mathf.RoundToInt((float)(inserter.stt - 0.499f) / itemProto.prefabDesc.inserterSTT); // compute the start and end slot that the cached inserter uses if (!incoming) { CalculatePose(__instance, sourceEntityId, otherId); } else { CalculatePose(__instance, otherId, sourceEntityId); } if (__instance.posePairs.Count > 0) { float minDistance = 1000f; PlayerAction_Build.PosePair bestFit = new PlayerAction_Build.PosePair(); bool hasNearbyPose = false; for (int j = 0; j < __instance.posePairs.Count; ++j) { float startDistance = Vector3.Distance(__instance.posePairs[j].startPose.position, inserterEntity.pos); float endDistance = Vector3.Distance(__instance.posePairs[j].endPose.position, inserter.pos2); float poseDistance = startDistance + endDistance; if (poseDistance < minDistance) { minDistance = poseDistance; bestFit = __instance.posePairs[j]; hasNearbyPose = true; } } if (hasNearbyPose) { ci.startSlot = bestFit.startSlot; ci.endSlot = bestFit.endSlot; } } // not important? ci.pickOffset = inserter.pickOffset; ci.insertOffset = inserter.insertOffset; // needed for pose? ci.t1 = inserter.t1; ci.t2 = inserter.t2; ci.filterId = inserter.filter; ci.snapMoves = snapMoves; ci.snapCount = snappedPointCount; cachedInserters.Add(ci); } } } }
public static void PlayerAction_BuildSetCopyInfoPostfix(PlayerAction_Build __instance, ref PlanetFactory ___factory, int objectId, PlanetAuxData ___planetAux) { cachedInserters.Clear(); // Remove previous copy info if (objectId < 0) // Copied item is a ghost, no inserters to cache { return; } var sourceEntity = objectId; var sourcePos = ___factory.entityPool[objectId].pos; // Find connected inserters int matches = 0; var inserterPool = ___factory.factorySystem.inserterPool; var entityPool = ___factory.entityPool; for (int i = 1; i < ___factory.factorySystem.inserterCursor; i++) { if (inserterPool[i].id == i) { var inserter = inserterPool[i]; var pickTarget = inserter.pickTarget; var insertTarget = inserter.insertTarget; if (pickTarget == sourceEntity || insertTarget == sourceEntity) { matches++; var inserterType = ___factory.entityPool[inserter.entityId].protoId; bool incoming = insertTarget == sourceEntity; var otherId = incoming ? pickTarget : insertTarget; // The belt or other building this inserter is attached to var otherPos = ___factory.entityPool[otherId].pos; // Store the Grid-Snapped moves from assembler to belt/other Vector3 begin = sourcePos; Vector3 end = otherPos; int path = 0; Vector3[] snaps = new Vector3[6]; var snappedPointCount = ___planetAux.SnapLineNonAlloc(begin, end, ref path, snaps); Vector3 lastSnap = begin; Vector3[] snapMoves = new Vector3[snappedPointCount]; for (int s = 0; s < snappedPointCount; s++) { Vector3 snapMove = snaps[s] - lastSnap; snapMoves[s] = snapMove; lastSnap = snaps[s]; } // Cache info for this inserter var ci = new CachedInserter(); ci.otherDelta = (otherPos - sourcePos); // Delta from copied building to other belt/building ci.incoming = incoming; ci.protoId = inserterType; ci.rot = ___factory.entityPool[inserter.entityId].rot; ci.rot2 = inserter.rot2; ci.pos2delta = (inserter.pos2 - entityPool[inserter.entityId].pos); // Delta from inserter pos2 to copied building var posDelta = entityPool[inserter.entityId].pos - sourcePos; // Delta from copied building to inserter pos if (!incoming) { posDelta = sourcePos - entityPool[inserter.entityId].pos; // Reverse for outgoing inserters } ci.posDelta = posDelta; // compute the start and end slot that the cached inserter uses if (!incoming) { CalculatePose(__instance, sourceEntity, otherId); } else { CalculatePose(__instance, otherId, sourceEntity); } if (__instance.posePairs.Count > 0) { float minDistance = 1000f; PlayerAction_Build.PosePair bestFit = new PlayerAction_Build.PosePair(); bool hasNearbyPose = false; for (int j = 0; j < __instance.posePairs.Count; ++j) { float startDistance = Vector3.Distance(__instance.posePairs[j].startPose.position, entityPool[inserter.entityId].pos); float endDistance = Vector3.Distance(__instance.posePairs[j].endPose.position, inserter.pos2); float poseDistance = startDistance + endDistance; if (poseDistance < minDistance) { minDistance = poseDistance; bestFit = __instance.posePairs[j]; hasNearbyPose = true; } } if (hasNearbyPose) { ci.startSlot = bestFit.startSlot; ci.endSlot = bestFit.endSlot; } } // not important? ci.pickOffset = inserter.pickOffset; ci.insertOffset = inserter.insertOffset; // needed for pose? ci.t1 = inserter.t1; ci.t2 = inserter.t2; ci.filterId = inserter.filter; ci.snapMoves = snapMoves; ci.snapCount = snappedPointCount; cachedInserters.Add(ci); } } } }