Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle less than 1 regen and removing Regen logic #10

Merged
merged 5 commits into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/main/java/org/terasology/logic/health/RegenAuthoritySystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.terasology.logic.health.event.OnFullyHealedEvent;
import org.terasology.protobuf.EntityData;
import org.terasology.registry.In;
import org.terasology.world.generation.BaseFacetedWorldGenerator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accidental import?


import java.util.Iterator;
import java.util.LinkedList;
Expand All @@ -43,6 +44,9 @@
*/
@RegisterSystem(RegisterMode.AUTHORITY)
public class RegenAuthoritySystem extends BaseComponentSystem implements UpdateSubscriberSystem {
public static final String ALL_REGEN = "all";
public static final String BASE_REGEN = "baseRegen";
public static final String WAIT = "wait";

private static final Logger logger = LoggerFactory.getLogger(RegenComponent.class);
/**
Expand All @@ -51,9 +55,9 @@ public class RegenAuthoritySystem extends BaseComponentSystem implements UpdateS
private static final int CHECK_INTERVAL = 200;

/**
* Integer storing when last time entities were regenerated.
* Long storing when entities are to be regenerated again.
*/
private static long lastTick = 0;
private static long nextTick;

// Stores when next to check for new value of regen, contains only entities which are being regenerated
private SortedSetMultimap<Long, EntityRef> regenSortedByTime = TreeMultimap.create(Ordering.natural(),
Expand All @@ -74,9 +78,9 @@ public class RegenAuthoritySystem extends BaseComponentSystem implements UpdateS
public void update(float delta) {
final long currentTime = time.getGameTimeInMs();
// Execute regen schedule
if (currentTime > lastTick + CHECK_INTERVAL) {
if (currentTime > nextTick) {
invokeRegenOperations(currentTime);
lastTick = currentTime;
nextTick = currentTime + CHECK_INTERVAL;
}
}

Expand Down Expand Up @@ -116,7 +120,7 @@ private void regenerate(long currentTime) {
for (EntityRef entity : regenSortedByTime.values()) {
RegenComponent regen = entity.getComponent(RegenComponent.class);
HealthComponent health = entity.getComponent(HealthComponent.class);
if (health.nextRegenTick < currentTime) {
if (health != null && health.nextRegenTick < currentTime) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added null check because NPE is thrown when blocks are destroyed.

logger.warn("regenerating " + regen.getRegenValue());
health.currentHealth += regen.getRegenValue();
health.nextRegenTick = currentTime + 1000;
Expand Down Expand Up @@ -154,10 +158,10 @@ public void onRegenAddedWithoutComponent(ActivateRegenEvent event, EntityRef ent
private void addRegenToScheduler(ActivateRegenEvent event, EntityRef entity, RegenComponent regen,
HealthComponent health) {
logger.warn("Regen added " + event.id);
if (event.id.equals("baseRegen")) {
if (event.id.equals(BASE_REGEN)) {
// setting endTime to MAX_VALUE because natural regen happens till entity fully regenerates
regen.addRegen(event.id, health.regenRate, Long.MAX_VALUE);
regen.addRegen("wait", -health.regenRate,
regen.addRegen(BASE_REGEN, health.regenRate, Long.MAX_VALUE);
regen.addRegen(WAIT, -health.regenRate,
time.getGameTimeInMs() + (long) (health.waitBeforeRegen * 1000));
} else {
regen.addRegen(event.id, event.value, time.getGameTimeInMs() + (long) (event.endTime * 1000));
Expand All @@ -169,12 +173,12 @@ private void addRegenToScheduler(ActivateRegenEvent event, EntityRef entity, Reg
public void onRegenRemoved(DeactivateRegenEvent event, EntityRef entity, HealthComponent health,
RegenComponent regen) {
regenSortedByTime.remove(regen.getLowestEndTime(), entity);
if (event.id.equals("all")) {
if (event.id.equals(ALL_REGEN)) {
entity.removeComponent(RegenComponent.class);
} else {
regen.removeRegen(event.id);
if (event.id.equals("baseRegen")) {
regen.removeRegen("wait");
if (event.id.equals(BASE_REGEN)) {
regen.removeRegen(WAIT);
}
if (!regen.isEmpty()) {
regenSortedByTime.put(regen.getLowestEndTime(), entity);
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/terasology/logic/health/RegenComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,9 @@ public int getRegenValue() {
for (float value: regenValue.values()) {
totalValue += value;
}
remainder = Math.min(1, remainder);
totalValue = Math.max(0, totalValue);
int returnValue = TeraMath.floorToInt(totalValue);
if (returnValue != totalValue) {
remainder += totalValue - returnValue;
}
return returnValue;
remainder = totalValue % 1;
return TeraMath.floorToInt(totalValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

import org.terasology.entitySystem.event.Event;

import static org.terasology.logic.health.RegenAuthoritySystem.BASE_REGEN;

public class ActivateRegenEvent implements Event {
public String id;
public int value;
public float endTime;

public ActivateRegenEvent() {
id = "baseRegen";
id = BASE_REGEN;
endTime = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@

import org.terasology.entitySystem.event.Event;

import static org.terasology.logic.health.RegenAuthoritySystem.BASE_REGEN;

public class DeactivateRegenEvent implements Event {
public String id;

public DeactivateRegenEvent() {
id = "baseRegen";
id = BASE_REGEN;
}
syntaxi marked this conversation as resolved.
Show resolved Hide resolved

public DeactivateRegenEvent(String id) {
Expand Down