示例#1
0
        public static InserterCopy CopyInserter(EntityData sourceEntity, EntityData referenceEntity)
        {
            PlanetFactory      factory     = GameMain.data.localPlanet.factory;
            PlayerAction_Build actionBuild = GameMain.data.mainPlayer.controller.actionBuild;

            if (sourceEntity.inserterId == 0)
            {
                return(null);
            }

            InserterComponent inserter = factory.factorySystem.inserterPool[sourceEntity.inserterId];

            if (data.copiedInserters.FindIndex(x => x.originalId == inserter.entityId) != -1)
            {
                return(null);
            }

            int pickTarget   = inserter.pickTarget;
            int insertTarget = inserter.insertTarget;

            ItemProto itemProto = LDB.items.Select(sourceEntity.protoId);

            bool incoming = insertTarget == referenceEntity.id;
            int  otherId  = incoming ? pickTarget : insertTarget;


            Vector2 referenceSprPos = referenceEntity.pos.ToSpherical();
            Vector2 sourceSprPos    = sourceEntity.pos.ToSpherical();
            Vector2 sourceSprPos2   = inserter.pos2.ToSpherical();

            // The belt or other building this inserter is attached to
            Vector2   otherSprPos;
            ItemProto otherProto;

            if (otherId > 0)
            {
                otherProto  = LDB.items.Select(factory.entityPool[otherId].protoId);
                otherSprPos = factory.entityPool[otherId].pos.ToSpherical();
            }
            else if (otherId < 0)
            {
                otherProto  = LDB.items.Select(factory.prebuildPool[-otherId].protoId);
                otherSprPos = factory.prebuildPool[-otherId].pos.ToSpherical();
            }
            else
            {
                otherSprPos = sourceSprPos2;
                otherProto  = null;
            }

            bool otherIsBelt = otherProto == null || otherProto.prefabDesc.isBelt;


            // Cache info for this inserter
            InserterCopy copiedInserter = new InserterCopy
            {
                itemProto  = itemProto,
                protoId    = itemProto.ID,
                originalId = inserter.entityId,

                pickTarget   = pickTarget,
                insertTarget = insertTarget,

                referenceBuildingId = referenceEntity.id,

                incoming = incoming,

                // rotations + deltas relative to the source building's rotation
                rot       = Quaternion.Inverse(referenceEntity.rot) * sourceEntity.rot,
                rot2      = Quaternion.Inverse(referenceEntity.rot) * inserter.rot2,
                posDelta  = sourceSprPos - referenceSprPos,  // Delta from copied building to inserter pos
                pos2Delta = sourceSprPos2 - referenceSprPos, // Delta from copied building to inserter pos2

                posDeltaCount  = sourceSprPos.GetSegmentsCount(),
                pos2DeltaCount = sourceSprPos2.GetSegmentsCount(),

                otherPosDelta      = otherSprPos - referenceSprPos,
                otherPosDeltaCount = otherSprPos.GetSegmentsCount(),

                // not important?
                pickOffset   = inserter.pickOffset,
                insertOffset = inserter.insertOffset,

                filterId = inserter.filter,


                startSlot = -1,
                endSlot   = -1,

                otherIsBelt = otherIsBelt
            };

            InserterPoses.CalculatePose(actionBuild, pickTarget, insertTarget);

            if (actionBuild.posePairs.Count > 0)
            {
                float minDistance = 1000f;
                for (int j = 0; j < actionBuild.posePairs.Count; ++j)
                {
                    var   posePair      = actionBuild.posePairs[j];
                    float startDistance = Vector3.Distance(posePair.startPose.position, sourceEntity.pos);
                    float endDistance   = Vector3.Distance(posePair.endPose.position, inserter.pos2);
                    float poseDistance  = startDistance + endDistance;

                    if (poseDistance < minDistance)
                    {
                        minDistance = poseDistance;
                        copiedInserter.startSlot = posePair.startSlot;
                        copiedInserter.endSlot   = posePair.endSlot;

                        copiedInserter.pickOffset   = (short)posePair.startOffset;
                        copiedInserter.insertOffset = (short)posePair.endOffset;
                    }
                }
            }


            /*        factory.ReadObjectConn(sourceEntity.id, 1, out bool isOutput, out int connectedId, out int connectedSlot);
             *
             *          if (connectedId != 0)
             *          {
             *              copiedInserter.startSlot = connectedSlot;
             *          }
             *
             *
             *          factory.ReadObjectConn(sourceEntity.id, 0, out _, out connectedId, out connectedSlot);
             *          if (connectedId != 0)
             *          {
             *              copiedInserter.endSlot = connectedSlot;
             *          }
             */

            data.copiedInserters.Add(copiedInserter);

            return(copiedInserter);
        }
示例#2
0
        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);
        }