Skip to content

Commit

Permalink
Event Dependency Feature and important Bug Fix
Browse files Browse the repository at this point in the history
Events can now have Dependencies.
Events only activate if their Dependencies are active.
Fixed a Bug that caused attempted Activations to never be reset, leading to Events never happening again.
  • Loading branch information
Atilist committed Jun 25, 2024
1 parent 9148337 commit 7933881
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class CelestialListener {

public static CelestialEvent flyingDimando;
public static CelestialEvent fallingDimando;
public static CelestialEvent crashingDimando;
public static CelestialEvent spinningDimando;
public static CelestialEvent burningDimando;
public static CelestialEvent longDimando;
Expand All @@ -20,13 +21,16 @@ public void registerCelestialEvents(CelestialRegisterEvent event) {
SLTest.LOGGER.info("Register celestial events for testing");
flyingDimando = new FlyingDimando(4, "flying_dimando", event.world);
fallingDimando = new DebugCelestialEvent(2, "falling_dimando", event.world);
crashingDimando = new DebugCelestialEvent(2, "crashing_dimando", event.world);
spinningDimando = new DebugCelestialEvent(4, "spinning_dimando", event.world).setDayOffset(1);
burningDimando = new DebugCelestialEvent(2, "burning_dimando", event.world).setDayOffset(1);
longDimando = new DebugCelestialEvent(12, "long_dimando", event.world).setExtraDays(4);
flyingDimando.addIncompatibleEvent(fallingDimando);
spinningDimando.addIncompatibleEvent(burningDimando);
crashingDimando.addDependency(longDimando);
CelestialTimeManager.addCelestialEvent(flyingDimando, DayQuarter.MORNING, DayQuarter.MORNING);
CelestialTimeManager.addCelestialEvent(fallingDimando, DayQuarter.NOON, DayQuarter.MIDNIGHT);
CelestialTimeManager.addCelestialEvent(crashingDimando, DayQuarter.NOON, DayQuarter.NOON);
CelestialTimeManager.addCelestialEvent(spinningDimando, DayQuarter.EVENING, DayQuarter.NOON);
CelestialTimeManager.addCelestialEvent(burningDimando, DayQuarter.MIDNIGHT, DayQuarter.EVENING);
CelestialTimeManager.addCelestialEvent(longDimando, DayQuarter.MIDNIGHT, DayQuarter.MIDNIGHT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class CelestialEvent {
private boolean active;
private boolean initializationNeeded = true;
private final List<CelestialEvent> incompatibleEvents = new LinkedList<>();
private final List<CelestialEvent> dependencies = new LinkedList<>();
public final World world;

/**
Expand Down Expand Up @@ -118,9 +119,22 @@ public boolean activateEvent(long worldTime, Random random) {
return false;
}
}
long days = worldTime / dayLength + dayOffset;
for (CelestialEvent otherEvent : dependencies) {
if (otherEvent == null) continue;
if (!otherEvent.isActive()) {
activityState.active = false;
activityState.attemptedActivation = true;
activityState.markDirty();
return false;
}
}
long days = (worldTime / dayLength) + dayOffset;
if (days % frequency > extraDays) {
activityState.attemptedActivation = false;
return false;
}
if (!activityState.attemptedActivation) {
active = days % frequency == 0 && random.nextFloat() <= chance;
active = days % frequency <= extraDays && random.nextFloat() <= chance;
}
if (active) {
activityState.active = true;
Expand Down Expand Up @@ -162,7 +176,7 @@ public void updateEvent(long worldTime) {
if (!active && !activityState.active) return;
worldTime -= startingDaytime;
worldTime += endingDaytime;
long days = worldTime / dayLength + dayOffset;
long days = (worldTime / dayLength) + dayOffset;
active = days % frequency <= extraDays;
activityState.active = active;
if (!active) {
Expand Down Expand Up @@ -234,6 +248,17 @@ public void addIncompatibleEvent(CelestialEvent otherEvent) {
otherEvent.addIncompatibleEvent(this);
}

/**
* Adds dependency to an event, meaning another event has to happen for this one to happen.
* Dependency only gets added into one direction.
*
* @param dependency Event to be added to the dependencies list.
*/
public void addDependency(CelestialEvent dependency) {
if (dependencies.contains(dependency)) return;
dependencies.add(dependency);
}

/**
* Used by CelestialTimeManager to handle an edge-case where events would not be loaded when reloading the same world.
*/
Expand Down

0 comments on commit 7933881

Please sign in to comment.