Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Unit from Energy to Core #271

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 147 additions & 4 deletions src/main/java/nova/core/util/math/MathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,84 @@ public static int max(int... numbers) {
return max;
}

/**
* Returns the smaller number of a and b.
* @param a value.
* @param b value.
* @return min
*/
public static long min(long a, long b) {
return a < b ? a : b;
}

/**
* Returns the smaller number of a, b and c.
* @param a value.
* @param b value.
* @param c value.
* @return min
*/
public static long min(long a, long b, long c) {
return min(min(a, b), c);
}

/**
* Returns the smallest number contained in the provided array.
* @param numbers Array of numbers
* @return min
*/
public static long min(long... numbers) {
if (numbers.length < 1) {
throw new IllegalArgumentException();
}
long min = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
}
return min;
}

/**
* Returns the bigger number of a and b.
* @param a value.
* @param b value.
* @return max
*/
public static long max(long a, long b) {
return a > b ? a : b;
}

/**
* Returns the bigger number of a, b and c.
* @param a value.
* @param b value.
* @param c value.
* @return max
*/
public static long max(long a, long b, long c) {
return max(max(a, b), c);
}

/**
* Returns the biggest number contained in the provided array.
* @param numbers Array of numbers
* @return max
*/
public static long max(long... numbers) {
if (numbers.length < 1) {
throw new IllegalArgumentException();
}
long max = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
return max;
}

/**
* Returns the smaller number of a and b.
* @param a value.
Expand Down Expand Up @@ -263,7 +341,7 @@ public static float max(float... numbers) {
}
return max;
}

/**
* Clamps the given number so that {@code min <= a <= max}
* @param a value.
Expand All @@ -275,6 +353,17 @@ public static int clamp(int a, int min, int max) {
return min(max(a, min), max);
}

/**
* Clamps the given number so that {@code min <= a <= max}
* @param a value.
* @param min lower limit
* @param max upper limit
* @return {@code min <= a <= max}
*/
public static long clamp(long a, long min, long max) {
return min(max(a, min), max);
}

/**
* Clamps the given number so that {@code min <= a <= max}
* @param a value.
Expand All @@ -297,23 +386,57 @@ public static double lerp(double a, double b, double f) {
return a + f * (b - a);
}

/**
* Linear interpolates isBetween point a and point b
* @param a value.
* @param b value.
* @param f A percentage value isBetween 0 to 1
* @return The interpolated value
*/
public static float lerp(float a, float b, float f) {
return a + f * (b - a);
}

/**
* Linear interpolates isBetween point a and point b
* @param a value.
* @param b value.
* @param f A percentage value isBetween 0 to 1
* @return The interpolated value
*/
public static Vector3D lerp(Vector3D a, Vector3D b, float f) {
return a.add((b.subtract(a)).scalarMultiply(f));
}

/**
* Clamps a value isBetween -bounds to +bounds
* @return A value capped isBetween two bounds.
* Clamps a value between -bounds to +bounds
* @return A value capped between two bounds.
*/
public static int absClamp(int value, int bounds) {
return min(max(value, -bounds), bounds);
}

/**
* Clamps a value between -bounds to +bounds
* @return A value capped between two bounds.
*/
public static long absClamp(long value, long bounds) {
return min(max(value, -bounds), bounds);
}

/**
* Clamps a value between -bounds to +bounds
* @return A value capped between two bounds.
*/
public static double absClamp(double value, double bounds) {
return min(max(value, -bounds), bounds);
}

public static float absClamp(int value, int bounds) {
/**
* Clamps a value between -bounds to +bounds
* @return A value capped between two bounds.
*/
public static float absClamp(float value, float bounds) {
return min(max(value, -bounds), bounds);
}

Expand All @@ -325,6 +448,10 @@ public static int log(int x, int base) {
return (int) (Math.log(x) / Math.log(base));
}

public static long log(long x, long base) {
return (long) (Math.log(x) / Math.log(base));
}

public static double log(double x, double base) {
return Math.log(x) / Math.log(base);
}
Expand All @@ -339,4 +466,20 @@ public static boolean isBetween(double a, double x, double b) {
return a <= x && x <= b;
}

/**
* Rounds a number to a specific number place places
*
* @param d - the number
* @return The rounded number
*/
public static double roundDecimals(double d, int decimalPlaces) {
long i = Math.round(d * Math.pow(10, decimalPlaces));
return i / Math.pow(10, decimalPlaces);
}

public static String toString(double d, boolean removeTrailingZeroes) {
if (removeTrailingZeroes && d % 1 == 0)
return Long.toString((long) d);
return Double.toString(d);
}
}
140 changes: 140 additions & 0 deletions src/main/java/nova/core/util/unit/Unit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright (c) 2015 NOVA, All rights reserved.
* This library is free software, licensed under GNU Lesser General Public License version 3
*
* This file is part of NOVA.
*
* NOVA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NOVA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NOVA. If not, see <http://www.gnu.org/licenses/>.
*/

package nova.core.util.unit;

import nova.core.language.LanguageManager;
import nova.core.language.Translateable;
import nova.core.util.Identifiable;
import nova.core.util.registry.Registry;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author ExE Boss
*/
public final class Unit implements Identifiable, Translateable {
private static final Registry<Unit> REGISTRY = new Registry<>();

public static final Unit METRE = getOrCreateUnit("nova:metre", "m");
public static final Unit LITRE = getOrCreateUnit("nova:litre", "l");
public static final Unit GRAM = getOrCreateUnit("nova:gram", "g", UnitPrefix.KILO);
// TODO: Define More Units

public static final Unit CELSIUS = getOrCreateUnit("nova:celsius", "°C");
public static final Unit KELVIN = getOrCreateUnit("nova:kelvin", "K");
public static final Unit FAHRENHEIT = getOrCreateUnit("nova:fahrenheit", "°F");

public static final Unit AMPERE = getOrCreateUnit("nova:ampere", "I");
public static final Unit AMP_HOUR = getOrCreateUnit("nova:amp_hour", "Ah");
public static final Unit VOLTAGE = getOrCreateUnit("nova:voltage", "V");
public static final Unit WATT = getOrCreateUnit("nova:watt", "W");
public static final Unit WATT_HOUR = getOrCreateUnit("nova:watt_hour", "Wh");
public static final Unit RESISTIVITY = getOrCreateUnit("nova:resistivity", "Ω");
public static final Unit CONDUCTIVITY = getOrCreateUnit("nova:conductivity", "S");
public static final Unit JOULE = getOrCreateUnit("nova:joule", "J");
public static final Unit NEWTON_METRE = getOrCreateUnit("nova:newton_metre", "Nm");

private final String id;
private final String unlocalizedName;
private final String symbol;
private UnitPrefix basePrefix = null;

private Unit(String id, String symbol) {
this(id, id.replace(':', '.'), symbol);
}

private Unit(String id, String unlocalizedName, String symbol) {
this.id = id;
this.unlocalizedName = unlocalizedName;
this.symbol = symbol;
}

private Unit setBasePrefix(UnitPrefix basePrefix) {
if (this.basePrefix == null)
this.basePrefix = basePrefix;
return this;
}

@Override
public String getUnlocalizedName() {
return "unit." + this.unlocalizedName;
}

@Override
public String getLocalizedName() {
return LanguageManager.instance().translate(this.getUnlocalizedName() + ".name", this.getReplacements());
}

public String getPluralName() {
return LanguageManager.instance().translate(this.getUnlocalizedName() + ".plural", this.getReplacements());
}

public String getSymbol() {
return this.symbol;
}

public UnitPrefix getBasePrefix() {
return basePrefix == null ? UnitPrefix.BASE : basePrefix;
}

@Override
public String getID() {
return id;
}

// TODO: Move to Registry
public Set<Unit> getUnitsFromMod(String modId) {
return REGISTRY.stream().filter((e) -> {
String id = e.getID();
if (id.contains(":")) {
return id.substring(0, id.lastIndexOf(':')).equals(modId);
} else {
return modId == null || modId.isEmpty();
}
}).collect(Collectors.toSet());
}

public Optional<Unit> getUnit(String id) {
return REGISTRY.get(id);
}

public static Unit getOrCreateUnit(String id, String unit) {
if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
return REGISTRY.register(new Unit(id, unit));
}

public static Unit getOrCreateUnit(String id, String unlocalizedName, String unit) {
if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
return REGISTRY.register(new Unit(id, unlocalizedName, unit));
}

public static Unit getOrCreateUnit(String id, String unit, UnitPrefix basePrefix) {
if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
return REGISTRY.register(new Unit(id, unit)).setBasePrefix(basePrefix);
}

public static Unit getOrCreateUnit(String id, String unlocalizedName, String unit, UnitPrefix basePrefix) {
if (REGISTRY.contains(id)) return REGISTRY.get(id).get();
return REGISTRY.register(new Unit(id, unlocalizedName, unit)).setBasePrefix(basePrefix);
}
}
Loading