Added fixed level for exp tables!!

This commit is contained in:
Jules 2023-04-11 00:49:18 +02:00
parent 8c0e571194
commit 634eda113b
2 changed files with 37 additions and 21 deletions

View File

@ -16,7 +16,7 @@ import java.util.Random;
public class ExperienceItem {
private final String id;
private final int period, firstTrigger, lastTrigger;
private final int period, firstTrigger, lastTrigger, fixedLevel;
private final double claimChance, failReduction;
private final List<Trigger> triggers;
@ -28,7 +28,7 @@ public class ExperienceItem {
}
/**
* One item for an experience table
* One item for an experience table.
*
* @param period The experience item is claimed every X level ups
* @param firstTrigger The experience item if claimed for the first time at X level ups.
@ -50,6 +50,25 @@ public class ExperienceItem {
this.triggers = triggers;
this.firstTrigger = firstTrigger;
this.lastTrigger = lastTrigger;
this.fixedLevel = 0;
}
/**
* One item for an experience table.
*
* @param fixedLevel Level at which your item should drop.
* @param claimChance Chance for that item to be claimed every X level ups
* @param triggers Actions cast when the exp item is claimed
*/
public ExperienceItem(String id, int fixedLevel, double claimChance, List<Trigger> triggers) {
this.id = id;
this.period = 0;
this.claimChance = claimChance;
this.failReduction = 0;
this.triggers = triggers;
this.firstTrigger = 0;
this.lastTrigger = 0;
this.fixedLevel = fixedLevel;
}
public ExperienceItem(ConfigurationSection config) {
@ -60,6 +79,7 @@ public class ExperienceItem {
period = config.getInt("period", 1);
firstTrigger = config.getInt("first-trigger", period);
lastTrigger = config.getInt("last-trigger", Integer.MAX_VALUE);
fixedLevel = config.getInt("level", -1);
claimChance = config.getDouble("chance", 100) / 100;
failReduction = config.getDouble("fail-reduction", 80) / 100;
triggers = new ArrayList<>();
@ -76,24 +96,24 @@ public class ExperienceItem {
* @param professionLevel The profession level the player just reached
* @param timesCollected Amount of times the exp item has already been claimed by the player
* @return If the item should be claimed right now taking into
* account the randomness factor from the 'chance' parameter
* account the randomness factor from the 'chance' parameter
*/
public boolean roll(int professionLevel, int timesCollected) {
// Exact level. Overrides everything
if (fixedLevel > -1) return fixedLevel == professionLevel;
// Check for the last triggering level
if (professionLevel > lastTrigger)
return false;
if (professionLevel > lastTrigger) return false;
// A period of 0 means the item only triggers once
if (period == 0 && timesCollected > 0)
return false;
if (period == 0 && timesCollected > 0) return false;
// Basic formula
final int claimsRequired = (professionLevel + 1 - (firstTrigger + timesCollected * period));
if (claimsRequired < 1)
return false;
if (claimsRequired < 1) return false;
double chance = 1 - (1 - claimChance) * Math.pow(failReduction, claimsRequired);
final double chance = 1 - (1 - claimChance) * Math.pow(failReduction, claimsRequired);
return random.nextDouble() < chance;
}
@ -103,14 +123,12 @@ public class ExperienceItem {
}
/**
* Used when the player level is reset to 0 ( reallocate point in skill tree for instance)
* Creates an opposite playerModifier to compensate all the effect that existed before.
* Used when the player level is reset to 0, when using
* skill tree reallocation points for instance.
*/
public void removeTriggers(PlayerData playerData) {
for (Trigger trigger : triggers) {
if (trigger instanceof Removable)
((Removable) trigger).remove(playerData);
}
for (Trigger trigger : triggers)
if (trigger instanceof Removable) ((Removable) trigger).remove(playerData);
}
/**
@ -119,10 +137,7 @@ public class ExperienceItem {
* @param playerData
*/
public void applyStatTriggers(PlayerData playerData) {
for (Trigger trigger : triggers) {
if (trigger instanceof StatTrigger statTrigger)
statTrigger.apply(playerData);
}
for (Trigger trigger : triggers)
if (trigger instanceof StatTrigger statTrigger) statTrigger.apply(playerData);
}
}

View File

@ -48,6 +48,7 @@ public class ExperienceTable {
int timesClaimed = levelingUp.getClaims(object, this, item);
if (!item.roll(professionLevel, timesClaimed))
continue;
levelingUp.setClaims(object, this, item, timesClaimed + 1);
item.applyTriggers(levelingUp);
}