示例#1
0
            public static void PlayerAction_BuildAfterPrebuildPostfix(PlayerAction_Build __instance, ref PlanetFactory ___factory, PlanetAuxData ___planetAux)
            {
                // Do we have cached inserters?
                var ci = PatchCopyInserters.cachedInserters;

                if (ci.Count > 0)
                {
                    foreach (var buildPreview in __instance.buildPreviews)
                    {
                        var targetPos  = __instance.previewPose.position + __instance.previewPose.rotation * buildPreview.lpos;
                        var entityPool = ___factory.entityPool;
                        foreach (var inserter in ci)
                        {
                            // 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 currentPos = targetPos;
                            for (int u = 0; u < inserter.snapCount; u++)
                            {
                                currentPos = ___planetAux.Snap(currentPos + inserter.snapMoves[u], true, false);
                            }

                            var testPos = currentPos;

                            // Find the other entity at the target location
                            int otherId = 0;
                            for (int x = 1; x < ___factory.entityCursor; x++)
                            {
                                if (entityPool[x].id == x)
                                {
                                    var distance = Vector3.Distance(entityPool[x].pos, testPos);
                                    if (distance < 0.2)
                                    {
                                        otherId = entityPool[x].id;
                                        break;
                                    }
                                }
                            }

                            if (otherId != 0)
                            {
                                // Order an inserter
                                var pi = new PatchCopyInserters.PendingInserter();
                                pi.otherId      = otherId;
                                pi.ci           = inserter;
                                pi.AssemblerPos = targetPos;

                                PatchCopyInserters.pendingInserters.Add(pi);
                            }
                        }
                    }
                }
            }
        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);
        }
示例#3
0
            public static void PlayerAction_BuildNotifyBuiltPrefix(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;
                                //Debug.Log($"!!! found assembler id={assemblerId} at Pos={entityBuilt.pos} expected {pi.AssemblerPos} distance={distance}");

                                // 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.modelId    = factory.entityPool[pi.otherId].modelId;

                                pbdata.insertOffset = pi.ci.insertOffset;
                                pbdata.pickOffset   = pi.ci.pickOffset;
                                pbdata.filterId     = pi.ci.filterId;

                                // YukkuriC: recover absolute transforms
                                Helper.AbsTransform(entityBuilt.pos, pi.ci.posDelta, pi.ci.rot, out pbdata.pos, out pbdata.rot);
                                Helper.AbsTransform(entityBuilt.pos, pi.ci.pos2delta, pi.ci.rot2, out pbdata.pos2, out pbdata.rot2);
                                pbdata.pos  = ___planetAux.Snap(pbdata.pos, true, false);
                                pbdata.pos2 = ___planetAux.Snap(pbdata.pos2, true, false);

                                // 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);
                            }
                        }
                    }
                }
            }
示例#4
0
        public static BPGratBox GetBoundingRange(PlanetData _planet, PlanetAuxData aux, int[] _objIds, int _objCount, List <ReformData> reforms, float _divideLongitude)
        {
            if (reforms.Count == 0 && _objCount == 0)
            {
                return(BPGratBox.zero);
            }

            float startlong = float.MaxValue;
            float startLat  = float.MaxValue;
            float endLong   = float.MinValue;
            float endLat    = float.MinValue;

            bool isPole = true;

            EntityData[]   entityPool   = _planet.factory.entityPool;
            PrebuildData[] prebuildPool = _planet.factory.prebuildPool;
            for (int i = 0; i < _objCount; i++)
            {
                int     id         = _objIds[i];
                Vector3 pos        = (id > 0) ? entityPool[id].pos.normalized : prebuildPool[-id].pos.normalized;
                Vector3 normalized = aux.Snap(pos, true).normalized;
                float   num6       = Mathf.Asin(normalized.y);
                float   num7       = Mathf.Atan2(normalized.x, -normalized.z);
                if (normalized.x * normalized.x + normalized.z * normalized.z >= 5E-07f)
                {
                    isPole = false;
                    float correctLong = num7 - _divideLongitude;
                    if (correctLong <= -1.8E-05f)
                    {
                        correctLong += 6.2831855f;
                    }
                    if (correctLong < 0f)
                    {
                        correctLong = 0f;
                    }
                    startlong = ((startlong < correctLong) ? startlong : correctLong);
                    endLong   = ((endLong > correctLong) ? endLong : correctLong);
                }
                startLat = ((startLat < num6) ? startLat : num6);
                endLat   = ((endLat > num6) ? endLat : num6);
            }

            if (BlueprintCopyExtension.isEnabled)
            {
                foreach (var data in reforms)
                {
                    if (data.longitude != 0)
                    {
                        isPole = false;
                        float correctLong = data.longitude - _divideLongitude;
                        if (correctLong <= -1.8E-05f)
                        {
                            correctLong += 6.2831855f;
                        }

                        if (correctLong < 0f)
                        {
                            correctLong = 0f;
                        }

                        startlong = startlong < correctLong ? startlong : correctLong;
                        endLong   = endLong > correctLong ? endLong : correctLong;
                    }

                    startLat = startLat < data.latitude ? startLat : data.latitude;
                    endLat   = endLat > data.latitude ? endLat : data.latitude;
                }
            }

            if (startlong < 0f)
            {
                startlong = 0f;
            }
            else if (startlong > 6.2831674f)
            {
                startlong = 6.2831674f;
            }

            if (endLong < 0f)
            {
                endLong = 0f;
            }
            else if (endLong > 6.2831674f)
            {
                endLong = 6.2831674f;
            }

            if (isPole)
            {
                startlong = 0f;
                endLong   = 6.2831674f;
            }

            startlong += _divideLongitude;
            endLong   += _divideLongitude;
            if (startlong < -3.1415927f)
            {
                startlong += 6.2831855f;
            }
            else if (startlong > 3.1415927f)
            {
                startlong -= 6.2831855f;
            }

            if (endLong < -3.1415927f)
            {
                endLong += 6.2831855f;
            }
            else if (endLong > 3.1415927f)
            {
                endLong -= 6.2831855f;
            }

            return(new BPGratBox(startlong, startLat, endLong, endLat));
        }
示例#5
0
            public static void PlayerAction_BuildAfterPrebuildPostfix(PlayerAction_Build __instance, ref PlanetFactory ___factory, PlanetAuxData ___planetAux)
            {
                // Do we have cached inserters?
                var ci = PatchCopyInserters.cachedInserters;

                if (CopyInserters.copyEnabled && ci.Count > 0)
                {
                    foreach (BuildPreview buildPreview in __instance.buildPreviews)
                    {
                        Vector3    targetPos;
                        Quaternion targetRot;
                        if (__instance.buildPreviews.Count > 1)
                        {
                            targetPos = buildPreview.lpos;
                            targetRot = buildPreview.lrot;
                        }
                        else
                        {
                            targetPos = __instance.previewPose.position + __instance.previewPose.rotation * buildPreview.lpos;
                            targetRot = __instance.previewPose.rotation;
                        }

                        // ignore buildings not being built at ground level
                        if (__instance.multiLevelCovering)
                        {
                            continue;
                        }

                        var entityPool = ___factory.entityPool;
                        foreach (var inserter in ci)
                        {
                            // 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 = targetPos;
                            // Note: rotates each move relative to the rotation of the new building
                            for (int u = 0; u < inserter.snapCount; u++)
                            {
                                testPos = ___planetAux.Snap(testPos + targetRot * inserter.snapMoves[u], true, false);
                            }

                            // Find the other entity at the target location
                            int otherId = 0;
                            for (int x = 1; x < ___factory.entityCursor; x++)
                            {
                                if (entityPool[x].id == x)
                                {
                                    var distance = Vector3.Distance(entityPool[x].pos, testPos);
                                    if (distance < 0.2)
                                    {
                                        otherId = entityPool[x].id;
                                        break;
                                    }
                                }
                            }

                            if (otherId != 0)
                            {
                                // Order an inserter
                                var pi = new PatchCopyInserters.PendingInserter();
                                pi.otherId      = otherId;
                                pi.ci           = inserter;
                                pi.AssemblerPos = targetPos;

                                PatchCopyInserters.pendingInserters.Add(pi);
                            }
                        }
                    }
                }
            }
示例#6
0
            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);
                            }
                        }
                    }
                }
            }