Skip to content

Commit

Permalink
start/stop maturation on status change (#1294)
Browse files Browse the repository at this point in the history
  • Loading branch information
cadon committed Dec 4, 2022
1 parent 216629a commit 9a4f2fd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 25 deletions.
16 changes: 8 additions & 8 deletions ARKBreedingStats/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1768,34 +1768,34 @@ private void buttonRecalculateTops_Click(object sender, EventArgs e)

private void aliveToolStripMenuItem_Click(object sender, EventArgs e)
{
SetStatusOfSelected(CreatureStatus.Available);
SetStatusOfSelectedCreatures(CreatureStatus.Available);
}

private void deadToolStripMenuItem_Click(object sender, EventArgs e)
{
SetStatusOfSelected(CreatureStatus.Dead);
SetStatusOfSelectedCreatures(CreatureStatus.Dead);
}

private void unavailableToolStripMenuItem_Click(object sender, EventArgs e)
{
SetStatusOfSelected(CreatureStatus.Unavailable);
SetStatusOfSelectedCreatures(CreatureStatus.Unavailable);
}

private void obeliskToolStripMenuItem1_Click(object sender, EventArgs e)
{
SetStatusOfSelected(CreatureStatus.Obelisk);
SetStatusOfSelectedCreatures(CreatureStatus.Obelisk);
}

private void SetStatusOfSelected(CreatureStatus s)
private void SetStatusOfSelectedCreatures(CreatureStatus s)
{
List<Creature> cs = new List<Creature>();
foreach (int i in listViewLibrary.SelectedIndices)
cs.Add(_creaturesDisplayed[i]);
if (cs.Any())
SetStatus(cs, s);
SetCreatureStatus(cs, s);
}

private void SetStatus(IEnumerable<Creature> cs, CreatureStatus s)
private void SetCreatureStatus(IEnumerable<Creature> cs, CreatureStatus s)
{
bool changed = false;
List<string> speciesBlueprints = new List<string>();
Expand Down Expand Up @@ -1854,7 +1854,7 @@ private void ShowBestBreedingPartner(Creature c)
"Selected Creature not Available",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
SetStatus(new List<Creature> { c }, CreatureStatus.Available);
SetCreatureStatus(new List<Creature> { c }, CreatureStatus.Available);
breedingPlan1.BreedingPlanNeedsUpdate = false;
}
else
Expand Down
11 changes: 5 additions & 6 deletions ARKBreedingStats/Form1.library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using System.Text.RegularExpressions;
using ARKBreedingStats.library;
using ARKBreedingStats.settings;
using System.Runtime.ConstrainedExecution;

namespace ARKBreedingStats
{
Expand Down Expand Up @@ -1712,27 +1711,27 @@ private void toolStripMenuItemRemove_Click(object sender, EventArgs e)

private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
SetStatusOfSelected(CreatureStatus.Available);
SetStatusOfSelectedCreatures(CreatureStatus.Available);
}

private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
SetStatusOfSelected(CreatureStatus.Unavailable);
SetStatusOfSelectedCreatures(CreatureStatus.Unavailable);
}

private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
SetStatusOfSelected(CreatureStatus.Dead);
SetStatusOfSelectedCreatures(CreatureStatus.Dead);
}

private void obeliskToolStripMenuItem_Click(object sender, EventArgs e)
{
SetStatusOfSelected(CreatureStatus.Obelisk);
SetStatusOfSelectedCreatures(CreatureStatus.Obelisk);
}

private void cryopodToolStripMenuItem_Click(object sender, EventArgs e)
{
SetStatusOfSelected(CreatureStatus.Cryopod);
SetStatusOfSelectedCreatures(CreatureStatus.Cryopod);
}

private void currentValuesToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down
41 changes: 30 additions & 11 deletions ARKBreedingStats/library/Creature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,24 @@ public CreatureStatus Status
get => _status;
set
{
if (_status == value) return;

if (Maturation < 1)
{
if (value == CreatureStatus.Dead)
PauseMaturationTimer();
else if ((_status == CreatureStatus.Cryopod || _status == CreatureStatus.Obelisk)
&& (value == CreatureStatus.Available || value == CreatureStatus.Unavailable))
StartMaturationTimer();
else if ((_status == CreatureStatus.Available || _status == CreatureStatus.Unavailable)
&& (value == CreatureStatus.Cryopod || value == CreatureStatus.Obelisk))
PauseMaturationTimer();
}

_status = value;
// remove other status while keeping the other flags
flags = (flags & CreatureFlags.StatusMask) | (CreatureFlags)(1 << (int)value);

}
}

Expand Down Expand Up @@ -420,25 +435,32 @@ public void RecalculateNewMutations()
/// <summary>
/// Starts the timer for maturation.
/// </summary>
private void StartTimer()
private void StartMaturationTimer()
{
if (growingPaused)
{
growingPaused = false;
growingUntil = DateTime.Now.Add(growingLeft);
if (growingLeft.Ticks <= 0)
growingUntil = null;
else
growingUntil = DateTime.Now.Add(growingLeft);
}
}

/// <summary>
/// Pauses the timer for maturation.
/// </summary>
private void PauseTimer()
private void PauseMaturationTimer()
{
if (!growingPaused)
{
growingPaused = true;
growingLeft = growingUntil?.Subtract(DateTime.Now) ?? new TimeSpan();
if (growingLeft.TotalHours < 0) growingLeft = new TimeSpan();
growingLeft = growingUntil?.Subtract(DateTime.Now) ?? TimeSpan.Zero;
if (growingLeft.Ticks <= 0)
{
growingLeft = TimeSpan.Zero;
growingUntil = null;
}
}
}

Expand All @@ -448,8 +470,8 @@ private void PauseTimer()
public void StartStopMatureTimer(bool start)
{
if (start)
StartTimer();
else PauseTimer();
StartMaturationTimer();
else PauseMaturationTimer();
}

/// <summary>
Expand All @@ -460,11 +482,8 @@ public void StartStopMatureTimer(bool start)
public string GrowingLeftString
{
get => System.Xml.XmlConvert.ToString(growingLeft);
set
{
growingLeft = string.IsNullOrEmpty(value) ?
set => growingLeft = string.IsNullOrEmpty(value) ?
TimeSpan.Zero : System.Xml.XmlConvert.ToTimeSpan(value);
}
}

/// <summary>
Expand Down

0 comments on commit 9a4f2fd

Please sign in to comment.