Skip to content

Commit

Permalink
Merge pull request #7 from StollD/Development
Browse files Browse the repository at this point in the history
v1.3.0-2
  • Loading branch information
Sigma88 authored May 30, 2017
2 parents 6f47d2b + 8e59765 commit 2260b91
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bld/
[Oo]bj/
[Ll]og/
*.dll
!Distribution/Release/GameData/Kronometer/Plugins/Kronometer.dll

# Visual Studio 2015 cache/options directory
.vs/
Expand Down
6 changes: 6 additions & 0 deletions Distribution/Release/Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v1.3.0-2

- Fixed an issue in date calculation for non leap dates
- Added a bool to force values of time units to be integers


v1.3.0-1

- First Release
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"NAME": "Kronometer",
"NAME": "<b><color=#FF80FF>Kronometer</color></b>",
"URL": "https://raw.githubusercontent.com/StollD/Kronometer/master/Distribution/Release/GameData/Kronometer/Plugins/Kronometer.version",
"DOWNLOAD": "https://github.com/StollD/Kronometer/releases/latest",
"CHANGE_LOG_URL": "https://raw.githubusercontent.com/StollD/Kronometer/master/Distribution/Release/Changelog.txt",
Expand All @@ -14,7 +14,7 @@
"MAJOR": 1,
"MINOR": 3,
"PATCH": 0,
"BUILD": 1
"BUILD": 2
},
"KSP_VERSION":
{
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ The Kronometer root node contains five general settings and three nodes for more
- **plural**, *\<string\>*, which is the plural name of the time unit (eg. *'Years'*)
- **symbol**, *\<string\>*, which is the symbol used for the time unit (eg. *'y'*)
- **value**, *\<double\>*, which is the duration in 'real life seconds' of the time unit (eg. *'9201600'*)
- **roundToNearestInt**, *\<bool\>*, which (if true) makes sure the 'value' is an integer number

---

Expand Down
54 changes: 44 additions & 10 deletions src/Kronometer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,29 @@ void Start()
loader.Clock.year.value = Math.Abs(loader.Clock.year.value);
loader.Clock.day.value = Math.Abs(loader.Clock.day.value);

// If weird numbers, abort
if (double.IsInfinity(loader.Clock.day.value) || double.IsNaN(loader.Clock.day.value) || double.IsInfinity(loader.Clock.year.value) || double.IsNaN(loader.Clock.year.value))
// Round values where it is required
if (loader.Clock.year.round)
loader.Clock.year.value = Math.Round(loader.Clock.year.value, 0);
if (loader.Clock.day.round)
loader.Clock.day.value = Math.Round(loader.Clock.day.value, 0);
if (loader.Clock.hour.round)
loader.Clock.hour.value = Math.Round(loader.Clock.hour.value, 0);
if (loader.Clock.minute.round)
loader.Clock.minute.value = Math.Round(loader.Clock.minute.value, 0);
if (loader.Clock.second.round)
loader.Clock.second.value = Math.Round(loader.Clock.second.value, 0);

if // Make sure we still need the clock and all values are still defined properly
(
double.PositiveInfinity > loader.Clock.hour.value &&
loader.Clock.hour.value > loader.Clock.minute.value &&
loader.Clock.minute.value > loader.Clock.second.value &&
loader.Clock.second.value > 0
)
{
return;
// Replace the stock Formatter
KSPUtil.dateTimeFormatter = new ClockFormatter(loader);
}

// Replace the stock Formatter
KSPUtil.dateTimeFormatter = new ClockFormatter(loader);
}
}
}
Expand Down Expand Up @@ -389,14 +404,20 @@ public virtual Date GetDate(double time)
// Time passed this year
double timeThisYear = time - loader.Clock.year.value * year;

// Time carried over each year
double AnnualCarryOver = loader.Clock.year.value % loader.Clock.day.value;

// Time carried over to this day
double TotalCarryOver = AnnualCarryOver * year;

// Time carried over from last year
double CarryOver = CarryOver = loader.Clock.day.value - Math.Abs((loader.Clock.year.value % loader.Clock.day.value) * year);
double CarryOver = MOD(TotalCarryOver, loader.Clock.day.value);

// Current Day of the year
int day = (int)Math.Floor((timeThisYear - CarryOver) / loader.Clock.day.value.value);

// Time left to count
double left = (time % loader.Clock.day.value + loader.Clock.day.value) % loader.Clock.day.value;
double left = MOD(time, loader.Clock.day.value);

// Number of hours in this day
int hours = (int)(left / loader.Clock.hour.value);
Expand Down Expand Up @@ -473,7 +494,7 @@ public virtual Date GetLeapDate(double time)
// Now 'day' and 'year' correctly account for leap years

// Time left to count
double left = (time % loader.Clock.day.value + loader.Clock.day.value) % loader.Clock.day.value;
double left = MOD(time, loader.Clock.day.value);

// Number of hours in this day
int hours = (int)(left / loader.Clock.hour.value);
Expand Down Expand Up @@ -508,7 +529,7 @@ public virtual Date GetLeapDate(double time)
int daysBetweenResets = (daysInOneShortYear * loader.resetMonths) + (int)(chanceOfLeapDay * loader.resetMonths);

// Days passed since last month reset
int daysFromReset = (daysPassedTOT % daysBetweenResets + daysBetweenResets) % daysBetweenResets;
int daysFromReset = (int)MOD(daysPassedTOT, daysBetweenResets);



Expand Down Expand Up @@ -811,6 +832,19 @@ private static string CheckNum(double time)
return null;
}

/// <summary>
/// Returns the remainder after number is divided by divisor. The result has the same sign as divisor.
/// </summary>
/// <param name="number">The number for which you want to find the remainder.</param>
/// <param name="divisor">The number by which you want to divide number.</param>
/// <returns></returns>
public double MOD(double number, double divisor)
{
return (number % divisor + divisor) % divisor;
}

/// In these Properties is stored the length of each time unit in game seconds
/// These can be found in stock as well, and should be used by other mods that deal with time
public virtual int Second
{
get { return (int)loader.Clock.second.value; }
Expand Down
8 changes: 6 additions & 2 deletions src/KronometerLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,16 @@ public class TimeUnits
[ParserTarget("value")]
public NumericParser<double> value;

public TimeUnits(string singular, string plural, string symbol, double value)
[ParserTarget("roundToNearestInt")]
public NumericParser<bool> round;

public TimeUnits(string singular, string plural, string symbol, double value, bool round = false)
{
this.singular = singular;
this.plural = plural;
this.symbol = symbol;
this.value = value;
this.value = round ? Math.Round(value, 0) : value;
this.round = round;
}
}

Expand Down

0 comments on commit 2260b91

Please sign in to comment.