Exemple #1
0
 protected bool IsEnoughPower()
 {
     if (this.ResourceSink != null)
     {
         return(ResourceSink.IsPowerAvailable(MyResourceDistributorComponent.ElectricityId,
                                              BlockDefinition.PowerConsumptionMoving));
     }
     return(false);
 }
        protected override void UpdateProduction(int timeDelta)
        {
            if (!Enabled)
            {
                CurrentState = StateEnum.Disabled;
                return;
            }

            if (!ResourceSink.IsPowered || ResourceSink.CurrentInput < GetOperationalPowerConsumption())
            {
                if (!ResourceSink.IsPowerAvailable(MyResourceDistributorComponent.ElectricityId, GetOperationalPowerConsumption()))
                {
                    CurrentState = StateEnum.NotEnoughPower;
                    return;
                }
            }

            if (!IsWorking)
            {
                CurrentState = StateEnum.NotWorking;
                return;
            }

            if (IsQueueEmpty)
            {
                return;
            }

            var firstQueueItem = TryGetFirstQueueItem();

            while (timeDelta > 0)
            {
                if (!firstQueueItem.HasValue)
                {
                    CurrentProgress = 0f;
                    if (IsQueueEmpty)
                    {
                        IsProducing = false;
                        return;
                    }

                    if (!Sync.IsServer)
                    {
                        break;
                    }

                    firstQueueItem = TryGetFirstQueueItem();
                }

                var currentBlueprint = firstQueueItem.Value.Blueprint;
                CurrentState = CheckInventory(currentBlueprint);
                if (CurrentState != StateEnum.Ok)
                {
                    IsProducing = false;
                    return;
                }
                var remainingTime = calculateBlueprintProductionTime(currentBlueprint) - CurrentProgress * calculateBlueprintProductionTime(currentBlueprint);

                if (timeDelta >= remainingTime)
                {
                    if (Sync.IsServer)
                    {
                        if (DisassembleEnabled)
                        {
                            FinishDisassembling(currentBlueprint);
                        }
                        else
                        {
                            if (RepeatEnabled)
                            {
                                InsertQueueItemRequest(-1, currentBlueprint);
                            }
                            FinishAssembling(currentBlueprint);
                        }

                        RemoveFirstQueueItemAnnounce(1);
                    }
                    timeDelta      -= (int)Math.Ceiling(remainingTime);
                    CurrentProgress = 0;
                    firstQueueItem  = null;
                }
                else
                {
                    CurrentProgress += timeDelta / calculateBlueprintProductionTime(currentBlueprint);
                    timeDelta        = 0;
                }
            }
            IsProducing = IsWorking && !IsQueueEmpty;
        }
        public override void UpdateAfterSimulation10()
        {
            base.UpdateAfterSimulation10();

            if (!Sync.IsServer || !IsWorking)
            {
                return;
            }

            if (!ResourceSink.IsPowered)
            {
                if (ResourceSink.IsPowerAvailable(MyResourceDistributorComponent.ElectricityId, BlockDefinition.RequiredPowerInput))
                {
                    float origInput = ResourceSink.RequiredInput;
                    ResourceSink.SetRequiredInputByType(MyResourceDistributorComponent.ElectricityId, 0);
                    ResourceSink.SetRequiredInputByType(MyResourceDistributorComponent.ElectricityId, origInput);
                }
                else
                {
                    return;
                }
            }

            var rotation1 = Quaternion.CreateFromForwardUp(WorldMatrix.Forward, WorldMatrix.Up);
            var position1 = PositionComp.GetPosition() + Vector3D.Transform(PositionComp.LocalVolume.Center + (m_fieldMax.Value + m_fieldMin.Value) * 0.5f, rotation1);

            VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Recreate Field");
            if (m_recreateField)
            {
                m_recreateField = false;
                m_fieldShape.RemoveReference();
                m_fieldShape = GetHkShape();
                ResourceSink.Update();
            }
            VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock();

            var boundingBox = new BoundingBoxD(m_fieldMin.Value, m_fieldMax.Value).Translate(PositionComp.LocalVolume.Center).TransformFast(WorldMatrix.GetOrientation()).Translate(PositionComp.GetPosition());

            m_potentialPenetrations.Clear();
            MyGamePruningStructure.GetTopMostEntitiesInBox(ref boundingBox, m_potentialPenetrations);

            m_potentialVoxelPenetrations.Clear();
            MyGamePruningStructure.GetAllVoxelMapsInBox(ref boundingBox, m_potentialVoxelPenetrations);//disabled until heightmap queries are finished

            VRageRender.MyRenderProxy.GetRenderProfiler().StartProfilingBlock("Sensor Physics");
            LastDetectedEntity = null;
            bool empty = true;

            foreach (var entity in m_potentialPenetrations)
            {
                if (entity is MyVoxelBase)
                {
                    //voxels are handled in different loop (becaose of planets)
                    continue;
                }
                if (ShouldDetect(entity))
                {
                    Quaternion rotation2;
                    Vector3    posDiff;
                    HkShape?   shape2;
                    if (GetPropertiesFromEntity(entity, ref position1, out rotation2, out posDiff, out shape2))
                    {
                        if (entity.GetPhysicsBody().HavokWorld.IsPenetratingShapeShape(m_fieldShape, ref Vector3.Zero, ref rotation1, shape2.Value, ref posDiff, ref rotation2))
                        {
                            LastDetectedEntity = entity;
                            empty = false;
                            break;
                        }
                    }
                }
            }

            if (DetectAsteroids)
            {
                foreach (var entity in m_potentialVoxelPenetrations)
                {
                    var voxel = entity as MyVoxelPhysics;
                    if (voxel != null)
                    {
                        Vector3D localPositionMin, localPositionMax;

                        MyVoxelCoordSystems.WorldPositionToLocalPosition(boundingBox.Min, voxel.PositionComp.WorldMatrix, voxel.PositionComp.WorldMatrixInvScaled, voxel.SizeInMetresHalf, out localPositionMin);
                        MyVoxelCoordSystems.WorldPositionToLocalPosition(boundingBox.Max, voxel.PositionComp.WorldMatrix, voxel.PositionComp.WorldMatrixInvScaled, voxel.SizeInMetresHalf, out localPositionMax);
                        var aabb = new BoundingBox(localPositionMin, localPositionMax);
                        aabb.Translate(voxel.StorageMin);
                        if (voxel.Storage.Intersect(ref aabb) != ContainmentType.Disjoint)
                        {
                            LastDetectedEntity = voxel;
                            empty = false;
                            break;
                        }
                    }
                    else
                    {
                        Quaternion rotation2;
                        Vector3    posDiff;
                        HkShape?   shape2;
                        if (GetPropertiesFromEntity(entity, ref position1, out rotation2, out posDiff, out shape2))
                        {
                            if (entity.GetPhysicsBody().HavokWorld.IsPenetratingShapeShape(m_fieldShape, ref Vector3.Zero, ref rotation1, shape2.Value, ref posDiff, ref rotation2))
                            {
                                LastDetectedEntity = entity;
                                empty = false;
                                break;
                            }
                        }
                    }
                }
            }
            IsActive = !empty;
            m_potentialPenetrations.Clear();
            m_potentialVoxelPenetrations.Clear();
            VRageRender.MyRenderProxy.GetRenderProfiler().EndProfilingBlock();
        }