Exemple #1
0
        public override bool Equals(object obj)
        {
            RailGuide rg = obj as RailGuide;

            return(rg != null && rg.cubeBlock == cubeBlock);
        }
Exemple #2
0
        public override void UpdateBeforeSimulation()
        {
            if (!block_initialized)
            {
                InitLate();
            }

            frame++;

            if (frame % 10 == 0)
            {
                (Entity as IMyTerminalBlock).RefreshCustomInfo();
            }

            if (!(bool)SettingsStore.Get(Entity, "power_on", true))
            {
                UpdatePowerUsage(0);
                UpdatePowerState(false);
                return;
            }

            // this will be one frame late ... but close enough??
            // power requested that can be satisfied by the network * power required that can be requested given our max
            float power_ratio = sinkComp.SuppliedRatioByType(MyResourceDistributorComponent.ElectricityId) * power_ratio_available;

            if (!sinkComp.IsPoweredByType(MyResourceDistributorComponent.ElectricityId))
            {
                power_ratio = 0;
            }
            // MyLog.Default.WriteLine(String.Format("power ratio is {0}", power_ratio));

            float height = (float)SettingsStore.Get(Entity, "height_offset", 1.25f);

            double forceLimit = (double)(float)SettingsStore.Get(Entity, "force_slider", 100000.0f);

            var hoverCenter  = Entity.WorldMatrix.Translation;
            var searchCenter = Entity.WorldMatrix.Translation + Entity.WorldMatrix.Down * 2.499;
            // DebugDraw.Sphere(searchCenter, 2.5f, Color.Green);

            var rail_pos   = new Vector3D(0, 0, 0);
            var weight_sum = 0.0f;
            HashSet <RailGuide> lostGuides   = new HashSet <RailGuide>();
            RailGuide           anyRailGuide = null;

            foreach (var guide in activeRailGuides)
            {
                if (!guide.getGuidance(hoverCenter, ref rail_pos, ref weight_sum, height))
                {
                    // lost rail lock
                    lostGuides.Add(guide);
                    continue;
                }
                anyRailGuide = guide;
            }

            foreach (var guide in lostGuides)
            {
                activeRailGuides.Remove(guide);
            }
            lostGuides.Clear();

            if (weight_sum < 0.9f)
            {
                // not confident in our rail lock, look for possible new rails
                var area  = new BoundingSphereD(searchCenter, 2.5);
                var items = MyAPIGateway.Entities.GetEntitiesInSphere(ref area);
                rail_pos   = Vector3D.Zero;
                weight_sum = 0.0f;
                foreach (var ent in items)
                {
                    var guide = RailGuide.fromEntity(ent);
                    if (guide != null)
                    {
                        var test = guide.getGuidance(hoverCenter, ref rail_pos, ref weight_sum, height);
                        if (test)
                        {
                            activeRailGuides.Add(guide);
                            anyRailGuide = guide;
                        }
                    }
                }
            }

            // MyLog.Default.WriteLine(String.Format("{0}:- hovering at {1}", Entity.EntityId, hoverCenter));
            if (activeRailGuides.Count == 0)
            {
                UpdatePowerUsage(0);
                UpdatePowerState(true);                 // powered but idle
                return;
            }

            // average by weight
            rail_pos /= weight_sum;

            var guidance = rail_pos - hoverCenter;

            // MyLog.Default.WriteLine(String.Format("{0}: rail pos is {1}, due to weight correction by {2}; guidance {3}", Entity.EntityId, rail_pos, weight_sum, guidance));
            DebugDraw.Sphere(rail_pos, 0.15f, Color.Blue);
            DebugDraw.Sphere(rail_pos * 0.5 + hoverCenter * 0.5, 0.1f, Color.Blue);
            DebugDraw.Sphere(hoverCenter, 0.1f, Color.Blue);

            // DebugDraw.Sphere(searchCenter, 0.1f, Color.Green);

            float force_magnitude = 0;
            // correction force, pushes engine towards rail guide
            {
                var len = guidance.Length() / 2.5;                 // 0 .. 1
                if (len > 0.001)
                {
                    var weight = len;
                    if (weight > 0.99)
                    {
                        weight = 0.99;                                    // always some force
                    }
                    const double splitPoint = 0.5;
                    if (weight > splitPoint)
                    {
                        weight = 1.0 - (weight - splitPoint) / (1.0 - splitPoint);
                    }
                    else
                    {
                        weight = weight / splitPoint;
                    }
                    var factor        = Math.Pow(weight, 2.0);              // spiken
                    var guidanceForce = forceLimit * Vector3D.Normalize(guidance) * factor;
                    this.avgCorrectF.update(guidanceForce);
                    DebugDraw.Sphere(searchCenter, 0.1f, Color.Yellow);
                    anyRailGuide.applyForces(Entity, this.avgCorrectF.value * power_ratio);
                    force_magnitude += (float)this.avgCorrectF.value.Length();
                }
            }
            // dampening force, reduces oscillation over time
            var dF = guidance - this.avgGuidance.value;

            {
                // var len = guidance.Length() / 2.5;
                // if (len > 0.99) len = 0.99;
                // var factor = Math.Pow(len, 0.3);
                var factor      = 1.0;
                var dampenForce = forceLimit * 0.5 * dF * factor;                 // separate slider?
                this.avgDampenF.update(dampenForce);
                DebugDraw.Sphere(searchCenter + this.avgDampenF.value * 0.000001f, 0.1f, Color.Red);
                anyRailGuide.applyForces(Entity, this.avgDampenF.value * power_ratio);
                force_magnitude += (float)this.avgDampenF.value.Length();
            }
            this.avgGuidance.update(guidance);
            UpdatePowerUsage(force_magnitude * FORCE_POWER_COST_MW_N);
            UpdatePowerState(true);
        }