MeasureTimerFrameTime() public static method

public static MeasureTimerFrameTime ( System.Drawing.Point timerLocation ) : int
timerLocation System.Drawing.Point
return int
Beispiel #1
0
        public void ResetTimer(ObjectMeasureData measureData)
        {
            ActiveTimerMetadata activeTimer;

            if (this.WorldMetadata.ActiveTimers.TryGetValue(measureData.OriginTileLocation, out activeTimer))
            {
                activeTimer.FramesLeft = AdvancedCircuits.MeasureTimerFrameTime(measureData.OriginTileLocation);
            }
        }
Beispiel #2
0
        public void RegisterUnregisterTimer(TSPlayer triggeringPlayer, ObjectMeasureData measureData, bool register)
        {
            bool alreadyRegistered = this.WorldMetadata.ActiveTimers.ContainsKey(measureData.OriginTileLocation);

            if (register)
            {
                if (!alreadyRegistered)
                {
                    this.WorldMetadata.ActiveTimers.Add(
                        measureData.OriginTileLocation, new ActiveTimerMetadata(AdvancedCircuits.MeasureTimerFrameTime(measureData.OriginTileLocation), triggeringPlayer.Name)
                        );
                }
            }
            else if (alreadyRegistered)
            {
                this.WorldMetadata.ActiveTimers.Remove(measureData.OriginTileLocation);
            }
        }
Beispiel #3
0
        public void HandleGameUpdate()
        {
            this.frameCounter++;

            if (this.frameCounter % CircuitHandler.TimerUpdateFrameRate == 0)
            {
                List <DPoint> timersToDelete = null;
                List <KeyValuePair <DPoint, ActiveTimerMetadata> > timersToProcess = null;

                foreach (KeyValuePair <DPoint, ActiveTimerMetadata> activeTimer in this.WorldMetadata.ActiveTimers)
                {
                    DPoint timerLocation = activeTimer.Key;
                    ActiveTimerMetadata timerMetadata = activeTimer.Value;

                    ITile timerTile = TerrariaUtils.Tiles[timerLocation];
                    if (timerMetadata == null || !timerTile.active() || timerTile.type != TileID.Timers)
                    {
                        if (timersToDelete == null)
                        {
                            timersToDelete = new List <DPoint>();
                        }

                        timersToDelete.Add(timerLocation);
                        continue;
                    }

                    if (timerMetadata.FramesLeft <= 0)
                    {
                        if (timersToProcess == null)
                        {
                            timersToProcess = new List <KeyValuePair <DPoint, ActiveTimerMetadata> >();
                        }

                        timersToProcess.Add(activeTimer);
                        timerMetadata.FramesLeft = AdvancedCircuits.MeasureTimerFrameTime(timerLocation);

                        continue;
                    }

                    timerMetadata.FramesLeft -= CircuitHandler.TimerUpdateFrameRate;
                }

                if (timersToProcess != null)
                {
                    DateTime now = DateTime.UtcNow;

                    foreach (KeyValuePair <DPoint, ActiveTimerMetadata> activeTimer in timersToProcess)
                    {
                        SignalType signalType;
                        // Is Advanced Circuit?
                        if (!TerrariaUtils.Tiles[activeTimer.Key].HasWire())
                        {
                            signalType = SignalType.On;
                        }
                        else
                        {
                            signalType = SignalType.Swap;
                        }

                        try {
                            CircuitProcessingResult result = this.ProcessCircuit(null, activeTimer.Key, signalType, false);
                            // If the circuit had errors or if it reached its max activity time, deactivate the timer.
                            if (
                                result.CancellationReason != CircuitCancellationReason.None || (
                                    this.Config.MaxTimerActivityTime != TimeSpan.Zero &&
                                    (now - activeTimer.Value.TimeOfRegistration) >= this.Config.MaxTimerActivityTime
                                    )
                                )
                            {
                                ObjectMeasureData measureData = TerrariaUtils.Tiles.MeasureObject(activeTimer.Key);
                                this.RegisterUnregisterTimer(result.TriggeringPlayer, measureData, false);
                                TerrariaUtils.Tiles.SetObjectState(measureData, false);
                            }
                        } catch (Exception ex) {
                            this.PluginTrace.WriteLineError("Circuit processing for a Timer at {0} failed. See inner exception for details.\n{1}", activeTimer.Key, ex.ToString());
                        }
                    }
                }

                if (timersToDelete != null)
                {
                    foreach (DPoint timerLocation in timersToDelete)
                    {
                        this.WorldMetadata.ActiveTimers.Remove(timerLocation);
                    }
                }
            }

            if (this.frameCounter % CircuitHandler.ClockUpdateFrameRate == 0)
            {
                bool isDaylight      = (Main.dayTime && Main.time >= 7200 && Main.time <= 46800);
                bool dayTimeChanged  = (Main.dayTime != this.isDayTime);
                bool daylightChanged = (this.isDaylight != isDaylight);

                if (dayTimeChanged || daylightChanged)
                {
                    List <DPoint> clocksToRemove = null;

                    foreach (KeyValuePair <DPoint, GrandfatherClockMetadata> clock in this.WorldMetadata.Clocks)
                    {
                        DPoint clockLocation = clock.Key;
                        ITile  clockTile     = TerrariaUtils.Tiles[clockLocation];

                        if (!clockTile.active() || clockTile.type != TileID.GrandfatherClocks)
                        {
                            if (clocksToRemove == null)
                            {
                                clocksToRemove = new List <DPoint>();
                            }

                            clocksToRemove.Add(clock.Key);
                            continue;
                        }

                        bool signal;
                        switch ((PaintColor)TerrariaUtils.Tiles[clockLocation].color())
                        {
                        case AdvancedCircuits.Paint_Clock_ByDaylight:
                            if (!daylightChanged)
                            {
                                continue;
                            }

                            signal = !isDaylight;
                            break;

                        case AdvancedCircuits.Paint_Clock_ByNighttimeAndBloodmoon:
                            if (!dayTimeChanged)
                            {
                                continue;
                            }

                            signal = !Main.dayTime && Main.bloodMoon;
                            break;

                        case AdvancedCircuits.Paint_Clock_ByNighttimeAndFullmoon:
                            if (!dayTimeChanged)
                            {
                                continue;
                            }

                            signal = !Main.dayTime && Main.moonPhase == 0;
                            break;

                        default:
                            if (!dayTimeChanged)
                            {
                                continue;
                            }

                            signal = !Main.dayTime;
                            break;
                        }

                        try {
                            TSPlayer triggeringPlayer = null;
                            if (clock.Value.TriggeringPlayerName != null)
                            {
                                triggeringPlayer = TShockEx.GetPlayerByName(clock.Value.TriggeringPlayerName);
                            }
                            if (triggeringPlayer == null)
                            {
                                triggeringPlayer = TSPlayer.Server;
                            }

                            this.ProcessCircuit(triggeringPlayer, clockLocation, AdvancedCircuits.BoolToSignal(signal), false);
                        } catch (Exception ex) {
                            this.PluginTrace.WriteLineError(
                                "Circuit processing for a Grandfather Clock at {0} failed. See inner exception for details.\n{1}",
                                clockLocation, ex.ToString()
                                );
                        }
                    }

                    if (clocksToRemove != null)
                    {
                        foreach (DPoint clockLocation in clocksToRemove)
                        {
                            this.WorldMetadata.Clocks.Remove(clockLocation);
                        }
                    }

                    if (dayTimeChanged)
                    {
                        this.isDayTime = Main.dayTime;
                    }
                    if (daylightChanged)
                    {
                        this.isDaylight = isDaylight;
                    }
                }

                if (this.frameCounter >= 100000)
                {
                    this.frameCounter = 0;
                }
            }
        }