public void Run(ZACommons commons) { var timers = ZACommons.SearchBlocksOfName(commons.AllBlocks, STANDARD_LOOP_TIMER_BLOCK_NAME, block => block is IMyTimerBlock && block.IsFunctional && block.IsWorking && ((IMyTimerBlock)block).Enabled && !((IMyTimerBlock)block).IsCountingDown); timers.ForEach(timer => timer.ApplyAction("Start")); }
private void KickTimer(ZACommons commons) { IMyTimerBlock timer = null; // Name takes priority over group if (TimerName != null) { var blocks = ZACommons.SearchBlocksOfName(commons.Blocks, TimerName, block => block is IMyTimerBlock && ((IMyTimerBlock)block).Enabled); if (blocks.Count > 0) { timer = blocks[0] as IMyTimerBlock; } } if (timer == null && TimerGroup != null) { var group = commons.GetBlockGroupWithName(TimerGroup); if (group != null) { var blocks = ZACommons.GetBlocksOfType <IMyTimerBlock>(group.Blocks, block => block.CubeGrid == commons.Me.CubeGrid && ((IMyTimerBlock)block).Enabled); timer = blocks.Count > 0 ? (IMyTimerBlock)blocks[0] : null; } } if (timer != null) { // Rules are simple. If we have something in the tick queue, trigger now. // Otherwise, set timer delay appropriately (minimum 1 second) and kick. // If you want sub-second accuracy, always use ticks. if (TickQueue.First != null) { timer.ApplyAction("TriggerNow"); } else if (TimeQueue.First != null) { var next = (float)(TimeQueue.First.Value.When.TotalSeconds - TimeSinceStart.TotalSeconds); // Constrain appropriately (not sure if this will be done for us or if it // will just throw). Just do it to be safe. next = Math.Max(next, timer.GetMinimum <float>("TriggerDelay")); next = Math.Min(next, timer.GetMaximum <float>("TriggerDelay")); timer.SetValue <float>("TriggerDelay", next); timer.ApplyAction("Start"); } // NB If both queues are empty, we stop running } }
private static void StartTimerBlock(ZACommons commons, string name) { if (name.Length == 0) { return; } var timers = ZACommons.SearchBlocksOfName(commons.Blocks, name); // Find the first timer block that's enabled foreach (var block in timers) { var timer = block as IMyTimerBlock; if (timer != null && timer.Enabled) { // And start it if it isn't already counting down if (!timer.IsCountingDown) { timer.ApplyAction("Start"); } return; } } }