Generally improve SkillSet by utilising lambdas etc.

This commit is contained in:
Major-
2014-09-12 14:23:19 +01:00
parent a7d668c3dc
commit 3ce2d2386a
+20 -35
View File
@@ -1,10 +1,13 @@
package org.apollo.game.model.entity; package org.apollo.game.model.entity;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.apollo.game.model.skill.SkillListener; import org.apollo.game.model.skill.SkillListener;
import com.google.common.base.Preconditions;
/** /**
* Represents the set of the player's skills. * Represents the set of the player's skills.
* *
@@ -29,9 +32,9 @@ public final class SkillSet {
static { static {
int points = 0, output = 0; int points = 0, output = 0;
for (int lvl = 1; lvl <= 99; lvl++) { for (int level = 1; level <= 99; level++) {
EXPERIENCE_FOR_LEVEL[lvl] = output; EXPERIENCE_FOR_LEVEL[level] = output;
points += Math.floor(lvl + 300.0 * Math.pow(2.0, lvl / 7.0)); points += Math.floor(level + 300 * Math.pow(2, level / 7.0));
output = (int) Math.floor(points / 4); output = (int) Math.floor(points / 4);
} }
} }
@@ -43,9 +46,7 @@ public final class SkillSet {
* @return The minimum experience. * @return The minimum experience.
*/ */
public static int getExperienceForLevel(int level) { public static int getExperienceForLevel(int level) {
if (level < 1 || level > 99) { Preconditions.checkArgument(level >= 1 && level <= 99, "Level must be between 1 and 99, inclusive.");
throw new IllegalArgumentException("Level must be between 1 and 99, inclusive.");
}
return EXPERIENCE_FOR_LEVEL[level]; return EXPERIENCE_FOR_LEVEL[level];
} }
@@ -56,12 +57,12 @@ public final class SkillSet {
* @return The minimum level. * @return The minimum level.
*/ */
public static int getLevelForExperience(double experience) { public static int getLevelForExperience(double experience) {
if (experience < 0 || experience > MAXIMUM_EXP) { Preconditions.checkArgument(experience >= 0 && experience <= MAXIMUM_EXP,
throw new IllegalArgumentException("Experience must be between 0 and 200,000,000, inclusive."); "Experience must be between 0 and 200,000,000, inclusive.");
}
for (int level = 1; level <= 98; level++) { for (int level = 1; level <= 98; level++) {
if (experience < EXPERIENCE_FOR_LEVEL[level + 1]) { if (experience < EXPERIENCE_FOR_LEVEL[level + 1]) {
return level; return level; // TODO binary search?
} }
} }
return 99; return 99;
@@ -143,10 +144,8 @@ public final class SkillSet {
double base = (defence + hitpoints + Math.floor(prayer / 2)) * 0.25; double base = (defence + hitpoints + Math.floor(prayer / 2)) * 0.25;
double melee = (attack + strength) * 0.325; double melee = (attack + strength) * 0.325;
double range = ranged * 0.4875;
double mage = magic * 0.4875;
this.combatLevel = (int) (base + Math.max(melee, Math.max(range, mage))); this.combatLevel = (int) (base + Math.max(melee, Math.max(ranged, magic) * 0.4875));
} }
/** /**
@@ -212,11 +211,7 @@ public final class SkillSet {
* @return The total level. * @return The total level.
*/ */
public int getTotalLevel() { public int getTotalLevel() {
int total = 0; return Arrays.stream(skills).map(skill -> skill.getMaximumLevel()).reduce(0, (total, level) -> total + level);
for (Skill skill : skills) {
total += skill.getMaximumLevel();
}
return total;
} }
/** /**
@@ -226,11 +221,11 @@ public final class SkillSet {
for (int id = 0; id < skills.length; id++) { for (int id = 0; id < skills.length; id++) {
int current = skills[id].getCurrentLevel(), max = skills[id].getMaximumLevel(); int current = skills[id].getCurrentLevel(), max = skills[id].getMaximumLevel();
if (current == max) { if (current == max || id == Skill.PRAYER) {
continue; continue;
} }
current += current < max ? 1 : -1; current += (current < max ? 1 : -1);
setSkill(id, new Skill(skills[id].getExperience(), current, max)); setSkill(id, new Skill(skills[id].getExperience(), current, max));
} }
} }
@@ -326,18 +321,14 @@ public final class SkillSet {
* @throws IndexOutOfBoundsException If the id is out of bounds. * @throws IndexOutOfBoundsException If the id is out of bounds.
*/ */
private void checkBounds(int id) { private void checkBounds(int id) {
if (id < 0 || id >= skills.length) { Preconditions.checkElementIndex(id, skills.length, "Skill id is out of bounds.");
throw new IndexOutOfBoundsException("Skill id is out of bounds.");
}
} }
/** /**
* Initialises the skill set. * Initialises the skill set.
*/ */
private void init() { private void init() {
for (int id = 0; id < skills.length; id++) { Arrays.setAll(skills, id -> id == Skill.HITPOINTS ? new Skill(1154, 10, 10) : new Skill(0, 1, 1));
skills[id] = (id == Skill.HITPOINTS ? new Skill(1154, 10, 10) : new Skill(0, 1, 1));
}
} }
/** /**
@@ -348,9 +339,7 @@ public final class SkillSet {
private void notifyLevelledUp(int id) { private void notifyLevelledUp(int id) {
checkBounds(id); checkBounds(id);
if (firingEvents) { if (firingEvents) {
for (SkillListener listener : listeners) { listeners.forEach(listener -> listener.levelledUp(this, id, skills[id]));
listener.levelledUp(this, id, skills[id]);
}
} }
} }
@@ -359,9 +348,7 @@ public final class SkillSet {
*/ */
private void notifySkillsUpdated() { private void notifySkillsUpdated() {
if (firingEvents) { if (firingEvents) {
for (SkillListener listener : listeners) { listeners.forEach(listener -> listener.skillsUpdated(this));
listener.skillsUpdated(this);
}
} }
} }
@@ -373,9 +360,7 @@ public final class SkillSet {
private void notifySkillUpdated(int id) { private void notifySkillUpdated(int id) {
checkBounds(id); checkBounds(id);
if (firingEvents) { if (firingEvents) {
for (SkillListener listener : listeners) { listeners.forEach(listener -> listener.skillUpdated(this, id, skills[id]));
listener.skillUpdated(this, id, skills[id]);
}
} }
} }