Skip to content
Merged
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
17 changes: 16 additions & 1 deletion src/main/java/net/tfminecraft/magic/blood/BloodListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,27 @@ public void onAnimalHit(EntityDamageByEntityEvent event) {
AnimalRiteService.onHit(event);
}

/** LOWEST: before Cooking's death handler deletes the animal's keepers. */
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onAnimalDying(EntityDeathEvent event) {
Player killer = event.getEntity().getKiller();
if (killer != null) {
WhisperService.noteKeepers(killer, event.getEntity());
}
}

/** MONITOR: the noted keepers are dropped however the death ended. */
@EventHandler(priority = EventPriority.MONITOR)
public void onAnimalDead(EntityDeathEvent event) {
WhisperService.forgetKeepers(event.getEntity());
}

@EventHandler(ignoreCancelled = true)
public void onAnimalDeath(EntityDeathEvent event) {
AnimalRiteService.onDeath(event);
Player killer = event.getEntity().getKiller();
if (killer != null) {
WhisperService.add(killer, WhisperService.animalKillWeight(killer, event.getEntity()));
WhisperService.animalKill(killer, event.getEntity());
}
}

Expand Down
32 changes: 31 additions & 1 deletion src/main/java/net/tfminecraft/magic/blood/BloodRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public final class BloodRecord {
private double resonance;
/** Blood in the altar's pool once the path is complete; null until then (a full pool). */
private Double pool;
/** The UTC day (days since the epoch) ownAnimalWhisper counts for. */
private long ownAnimalDay;
/** Whisper gained on ownAnimalDay from killing the character's own animals (the daily cap). */
private double ownAnimalWhisper;

/**
* One character sacrificed at the altar. rank: the victim's closeness rank when taken (1 = closest),
Expand Down Expand Up @@ -102,6 +106,9 @@ public void normalize() {
if (whisper < 0 || Double.isNaN(whisper)) {
whisper = 0;
}
if (ownAnimalWhisper < 0 || Double.isNaN(ownAnimalWhisper)) {
ownAnimalWhisper = 0;
}
whisperLine = Math.max(0, whisperLine);
huntPoints = Math.max(0, huntPoints);
finalsDone = Math.max(0, finalsDone);
Expand Down Expand Up @@ -146,6 +153,29 @@ public double getWhisper() {
return whisper;
}

/** Whisper own animals gave on this UTC day; 0 for any other day. */
public double getOwnAnimalWhisper(long day) {
return day == ownAnimalDay ? ownAnimalWhisper : 0;
}

/** Counts whisper from own animals toward this UTC day's cap, starting afresh on a new day. */
public void addOwnAnimalWhisper(long day, double amount) {
if (day != ownAnimalDay) {
ownAnimalDay = day;
ownAnimalWhisper = 0;
}
ownAnimalWhisper += amount;
}

/** Drops an earlier day's own-animal count, so the record can empty out; true when one was dropped. */
public boolean forgetOwnAnimalWhisperBefore(long day) {
if (ownAnimalWhisper <= 0 || ownAnimalDay >= day) {
return false;
}
ownAnimalWhisper = 0;
return true;
}

public void setWhisper(double whisper) {
this.whisper = Math.max(0, whisper);
}
Expand Down Expand Up @@ -298,6 +328,6 @@ public boolean isEmpty() {
&& species.isEmpty() && altarId == null && huntPoints == 0 && finalsDone == 0
&& sacrifices.isEmpty() && finalTargets.isEmpty() && !pendingBacklash && !pendingNaming
&& artefacts.isEmpty() && charges.isEmpty() && pending.isEmpty() && resonance <= 0
&& pool == null;
&& pool == null && ownAnimalWhisper <= 0;
}
}
37 changes: 27 additions & 10 deletions src/main/java/net/tfminecraft/magic/blood/WhisperConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ public boolean coloured() {
private static double decayPerHour = 1;
private static Set<String> fastTraits = Set.of();
private static double fastMultiplier = 3;
private static double animalKill = 2;
private static double aloneOrNightMultiplier = 2;
private static double animalOthers = 4;
private static double animalOwn = 1;
private static double animalOwnDailyCap = 2;
private static double characterKill = 15;
private static double robbery = 4;
private static double corruption = 1;
Expand All @@ -84,8 +85,9 @@ static void reset() {
decayPerHour = 1;
fastTraits = Set.of();
fastMultiplier = 3;
animalKill = 2;
aloneOrNightMultiplier = 2;
animalOthers = 4;
animalOwn = 1;
animalOwnDailyCap = 2;
characterKill = 15;
robbery = 4;
corruption = 1;
Expand All @@ -111,8 +113,16 @@ static void load(ConfigurationSection whispers, ConfigurationSection voices, Log
}
fastTraits = Set.copyOf(traits);
fastMultiplier = Math.max(1, whispers.getDouble("fast_multiplier", 3));
animalKill = Math.max(0, whispers.getDouble("sources.animal_kill", 2));
aloneOrNightMultiplier = Math.max(1, whispers.getDouble("sources.alone_or_night_multiplier", 2));
ConfigurationSection kills = whispers.getConfigurationSection("sources.animal_kills");
if (kills != null) {
animalOthers = Math.max(0, kills.getDouble("others", 4));
animalOwn = Math.max(0, kills.getDouble("own_fast_trait", 1));
animalOwnDailyCap = Math.max(0, kills.getDouble("own_daily_cap", 2));
} else if (whispers.isSet("sources.animal_kill")) {
// The older single value: it now prices other keepers' animals only, and own ones give nothing.
animalOthers = Math.max(0, whispers.getDouble("sources.animal_kill"));
animalOwn = 0;
}
characterKill = Math.max(0, whispers.getDouble("sources.character_kill", 15));
robbery = Math.max(0, whispers.getDouble("sources.robbery", 4));
corruption = Math.max(0, whispers.getDouble("sources.corruption", 1));
Expand Down Expand Up @@ -215,12 +225,19 @@ public static double fastMultiplier() {
return fastMultiplier;
}

public static double animalKill() {
return animalKill;
/** Killing an animal another player keeps (before the fast-trait multiplier). */
public static double animalOthers() {
return animalOthers;
}

/** Killing an animal you keep yourself; fast-trait characters only, never multiplied. */
public static double animalOwn() {
return animalOwn;
}

public static double aloneOrNightMultiplier() {
return aloneOrNightMultiplier;
/** Most whisper own animals give a character per UTC day; 0 = no cap. */
public static double animalOwnDailyCap() {
return animalOwnDailyCap;
}

public static double characterKill() {
Expand Down
115 changes: 106 additions & 9 deletions src/main/java/net/tfminecraft/magic/blood/WhisperService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import java.util.Random;
import java.util.UUID;
import java.util.function.LongSupplier;
import java.util.function.Predicate;
import java.util.function.ToDoubleFunction;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
import org.bukkit.inventory.ItemStack;

import net.tfminecraft.magic.Magic;
Expand All @@ -31,13 +33,28 @@
public final class WhisperService {

private static final long MINUTE = 60_000L;
private static final long DAY = 86_400_000L;
private static final long FIRST_VOICE_DELAY = MINUTE;
private static final Map<UUID, Long> NEXT_VOICE = new HashMap<>();
/** Keepers of animals being killed, read before Cooking's death handler deletes them (by animal). */
private static final Map<UUID, List<HusbandryOwners.Owner>> KEEPERS_AT_DEATH = new HashMap<>();

static LongSupplier clock = System::currentTimeMillis;
static Random random = new Random();
/** The player's equilibrium, NaN without a resonance session. */
static ToDoubleFunction<Player> equilibrium = WhisperService::sessionEquilibrium;
/** Whether a character has a fast trait (the evil fast route). */
static Predicate<String> fastTraitOf = WhisperService::fastTrait;

/** Who keeps a killed animal, seen from its killer. */
enum Keeper {
/** Nobody keeps it. */
WILD,
/** The killer's account is one of its keepers. */
OWN,
/** Other players keep it, the killer doesn't. */
OTHERS
}

private WhisperService() {}

Expand All @@ -54,14 +71,18 @@ public static void add(Player player, double amount) {
* before the voices start. Returns true when this woke them.
*/
public static boolean addTo(String character, String owner, double amount, Player online) {
return addTo(character, owner, amount, online, true);
}

private static boolean addTo(String character, String owner, double amount, Player online, boolean multiply) {
if (!BloodConfig.isEnabled() || amount <= 0) {
return false;
}
BloodRecord record = BloodRecords.getOrCreate(character, owner);
if (record.getStage() != BloodStage.NONE) {
return false;
}
double gain = amount * (fastTrait(character) ? WhisperConfig.fastMultiplier() : 1);
double gain = amount * (multiply && fastTraitOf.test(character) ? WhisperConfig.fastMultiplier() : 1);
record.setWhisper(record.getWhisper() + gain);
boolean woke = record.getWhisper() >= WhisperConfig.threshold();
if (woke) {
Expand Down Expand Up @@ -89,14 +110,81 @@ static boolean fastTrait(String character) {
}
}

/** Whisper weight for killing a listed animal with a blade, more when alone or at night. */
public static double animalKillWeight(Player killer, Entity animal) {
if (!WhisperConfig.isAnimal(animal.getType()) || !blade(killer.getInventory().getItemInMainHand())) {
return 0;
/**
* Killing a listed animal with a blade. Another player's animal feeds anyone's meter (fast traits
* multiply it as usual); the killer's own only feeds a fast-trait character's, unmultiplied and up
* to a daily cap; a wild one never does.
*/
public static void animalKill(Player killer, Entity animal) {
if (!BloodConfig.isEnabled() || !WhisperConfig.isAnimal(animal.getType())
|| !blade(killer.getInventory().getItemInMainHand())) {
return;
}
switch (keeper(killer, animal)) {
case OTHERS -> add(killer, WhisperConfig.animalOthers());
case OWN -> ownAnimal(killer);
default -> { }
}
long time = animal.getWorld().getTime();
boolean night = time >= 13_000 && time < 23_000;
return WhisperConfig.animalKill() * (night || alone(killer) ? WhisperConfig.aloneOrNightMultiplier() : 1);
}

/**
* Remembers the keepers of a listed animal killed with a blade, as it dies. Cooking deletes them in
* its own death handler, which runs before Magic's at the same priority, so this runs at LOWEST.
*/
public static void noteKeepers(Player killer, Entity animal) {
if (BloodConfig.isEnabled() && WhisperConfig.isAnimal(animal.getType())
&& blade(killer.getInventory().getItemInMainHand())) {
KEEPERS_AT_DEATH.put(animal.getUniqueId(), HusbandryOwners.of(animal.getUniqueId()));
}
}

/** Drops the keepers noted for a death once it is over. */
public static void forgetKeepers(Entity animal) {
KEEPERS_AT_DEATH.remove(animal.getUniqueId());
}

/**
* Cooking's husbandry keepers (as noted at the death, else read now), and a vanilla tamer,
* decide whose animal it was.
*/
static Keeper keeper(Player killer, Entity animal) {
boolean kept = false;
List<HusbandryOwners.Owner> owners = KEEPERS_AT_DEATH.get(animal.getUniqueId());
for (HusbandryOwners.Owner owner : owners != null ? owners : HusbandryOwners.of(animal.getUniqueId())) {
if (owner.account().equals(killer.getUniqueId())) {
return Keeper.OWN;
}
kept = true;
}
if (animal instanceof Tameable tameable && tameable.getOwnerUniqueId() != null) {
if (tameable.getOwnerUniqueId().equals(killer.getUniqueId())) {
return Keeper.OWN;
}
kept = true;
}
return kept ? Keeper.OTHERS : Keeper.WILD;
}

/** An own animal: fast-trait characters only, never multiplied, no more than the daily cap. */
static void ownAnimal(Player killer) {
String character = AltarService.currentCharacter(killer);
double amount = WhisperConfig.animalOwn();
if (character == null || amount <= 0 || !fastTraitOf.test(character)) {
return;
}
String owner = killer.getUniqueId().toString();
BloodRecord record = BloodRecords.getOrCreate(character, owner);
if (record.getStage() != BloodStage.NONE) {
return;
}
long day = Math.floorDiv(clock.getAsLong(), DAY);
double cap = WhisperConfig.animalOwnDailyCap();
double gain = cap > 0 ? Math.min(amount, cap - record.getOwnAnimalWhisper(day)) : amount;
if (gain <= 0) {
return;
}
record.addOwnAnimalWhisper(day, gain);
addTo(character, owner, gain, killer, false);
}

static boolean blade(ItemStack item) {
Expand Down Expand Up @@ -145,8 +233,16 @@ private static void tenMinutes(Player player, String character) {
addTo(character, player.getUniqueId().toString(), WhisperConfig.corruption(), player);
}
BloodRecord record = BloodRecords.get(character);
if (record != null && record.getStage() == BloodStage.NONE && record.getWhisper() > 0) {
if (record == null) {
return;
}
// The daily cap's count is kept until its day is over, even with the meter drained to 0.
boolean changed = record.forgetOwnAnimalWhisperBefore(Math.floorDiv(clock.getAsLong(), DAY));
if (record.getStage() == BloodStage.NONE && record.getWhisper() > 0) {
record.setWhisper(record.getWhisper() - WhisperConfig.decayPerHour() / 6.0);
changed = true;
}
if (changed) {
BloodRecords.save(record);
}
}
Expand Down Expand Up @@ -344,6 +440,7 @@ static Long nextVoice(UUID player) {

public static void clear() {
NEXT_VOICE.clear();
KEEPERS_AT_DEATH.clear();
}

private static double sessionEquilibrium(Player player) {
Expand Down
13 changes: 9 additions & 4 deletions src/main/resources/wildmagic/bloodmagic/voices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ whispers:
fast_traits: [occultist]
fast_multiplier: 3.0
sources:
# Killing a listed animal with a blade.
animal_kill: 2.0
# Animal kills count this many times more at night or with nobody near.
alone_or_night_multiplier: 2.0
# Killing a listed animal with a blade. Keepers come from Cooking's husbandry records and a vanilla
# tamer; a wild animal (no keeper) never counts.
animal_kills:
# An animal other players keep and the killer doesn't. Counts for anyone; fast traits multiply it.
others: 4.0
# An animal the killer's account keeps. Fast-trait characters only, never multiplied.
own_fast_trait: 1.0
# Most whisper own animals give a character per UTC day (0 = no cap).
own_daily_cap: 2.0
# A character you killed died for good (permanent kills only; knockouts never count).
character_kill: 15.0
# An evil RP session started by robbing, pickpocketing, lockpicking or looting a locked grave
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/net/tfminecraft/magic/blood/BloodDomainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,32 @@ void recordNormalizesBrokenFieldsAndKnowsWhenItIsEmpty() throws Exception {
var line = BloodRecord.class.getDeclaredField("whisperLine");
line.setAccessible(true);
line.set(record, -3);
var ownAnimal = BloodRecord.class.getDeclaredField("ownAnimalWhisper");
ownAnimal.setAccessible(true);
ownAnimal.set(record, Double.NaN);
record.setAltarId(" ");
record.normalize();
assertEquals(BloodStage.NONE, record.getStage());
assertEquals(BloodPath.NONE, record.getPath());
assertTrue(record.getSpecies().isEmpty());
assertEquals(0, record.getWhisper());
assertEquals(0, record.getWhisperLine());
assertEquals(0, record.getOwnAnimalWhisper(0));
ownAnimal.set(record, -1.0);
record.normalize();
assertEquals(0, record.getOwnAnimalWhisper(0));
record.addOwnAnimalWhisper(0, 1);
record.addOwnAnimalWhisper(0, 0.5);
assertEquals(1.5, record.getOwnAnimalWhisper(0));
assertEquals(0, record.getOwnAnimalWhisper(1));
record.addOwnAnimalWhisper(1, 1);
assertEquals(1, record.getOwnAnimalWhisper(1));
assertEquals(0, record.getOwnAnimalWhisper(0));
assertFalse(record.isEmpty(), "a used daily cap keeps the record");
assertFalse(record.forgetOwnAnimalWhisperBefore(1));
assertTrue(record.forgetOwnAnimalWhisperBefore(2));
assertFalse(record.forgetOwnAnimalWhisperBefore(3));
assertEquals(0, record.getOwnAnimalWhisper(1));
assertNull(record.getAltarId());
assertTrue(record.isEmpty());
whisper.set(record, -1.0);
Expand Down
Loading