Skip to content

Commit

Permalink
Should value fresher rations more
Browse files Browse the repository at this point in the history
Closed #381
  • Loading branch information
Bernardo Sulzbach committed Oct 30, 2017
1 parent 693e585 commit 466ec2b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,17 @@ private void printItems() {
Writer.getDefaultWriter().write(text);
}

private double adjustHealingForFreshness(Item item, double healing) {
if (!item.hasTag(Item.Tag.DECOMPOSES)) {
return healing;
}
long age = item.getAge();
long decompositionPeriod = item.getDecompositionPeriod();
double x = age / (double) decompositionPeriod;
double f = Math.min(1.0, 4 * Math.pow(1 - x, 2));
return f * healing;
}

/**
* Attempts to eat an item.
*/
Expand All @@ -449,20 +460,20 @@ public void eatItem(String[] arguments) {
if (getInventory().hasItem(selectedItem)) {
FoodComponent food = selectedItem.getFoodComponent();
double remainingBites = selectedItem.getIntegrity().getCurrent() / (double) food.getIntegrityDecrementOnEat();
int healthChange;
double healthChange;
if (remainingBites >= 1.0) {
healthChange = food.getNutrition();
} else {
// The absolute value of the healthChange will never be equal to nutrition, only smaller.
healthChange = (int) (food.getNutrition() * remainingBites);
healthChange = food.getNutrition() * remainingBites;
}
selectedItem.decrementIntegrityByEat();
if (selectedItem.isBroken() && !selectedItem.hasTag(Item.Tag.REPAIRABLE)) {
Writer.getDefaultWriter().write("You ate " + selectedItem.getName() + ".");
} else {
Writer.getDefaultWriter().write("You ate a bit of " + selectedItem.getName() + ".");
}
addHealth(healthChange);
int healingAmount = (int) adjustHealingForFreshness(selectedItem, healthChange);
addHealth(healingAmount);
if (healthChange > 0) {
WELL_FED_EFFECT.affect(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public Weight getWeight() {
*
* @return a long representing an amount of seconds
*/
long getAge() {
public long getAge() {
Duration existence = new Duration(dateOfCreation, Game.getGameState().getWorld().getWorldDate());
return existence.getSeconds();
}
Expand Down Expand Up @@ -180,7 +180,7 @@ private String getIntegrityString() {
return IntegrityState.getIntegrityState(getIntegrity().getCurrent(), getIntegrity().getMaximum()).toString();
}

long getDecompositionPeriod() {
public long getDecompositionPeriod() {
return decompositionPeriod;
}

Expand Down

0 comments on commit 466ec2b

Please sign in to comment.