Skip to content

Commit

Permalink
Reduce light radius check for mob despawning (#600)
Browse files Browse the repository at this point in the history
2 Approval Bypassed:
Reason: Critical bug fix
  • Loading branch information
Litorom authored Jan 4, 2024
2 parents 373ab37 + 56582b9 commit 33e6252
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
20 changes: 15 additions & 5 deletions src/client/java/minicraft/entity/mob/MobAi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import minicraft.core.io.Sound;
import minicraft.entity.Direction;
import minicraft.entity.Entity;
import minicraft.entity.furniture.Lantern;
import minicraft.entity.particle.TextParticle;
import minicraft.gfx.Color;
import minicraft.gfx.Point;
import minicraft.gfx.Rectangle;
import minicraft.gfx.Screen;
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.item.Item;
import minicraft.item.PotionType;
import minicraft.level.Level;
import minicraft.level.tile.Tile;
import minicraft.level.tile.TorchTile;

import java.util.Arrays;

Expand Down Expand Up @@ -66,11 +70,17 @@ public void handleDespawn() {
* @return {@code true} if the mob is within any light.
*/
protected boolean isWithinLight() {
return Arrays.stream(level.getEntityArray()).anyMatch(e -> e instanceof Lantern && isWithin(e.getLightRadius(), e))
|| !level.getMatchingTiles((tile, x, y) -> {
int xx = Math.abs(this.x - x), yy = Math.abs(this.y - y), l = tile.getLightRadius(level, x, y);
return xx * xx + yy * yy <= l * l;
}).isEmpty();
for (Entity e : level.getEntitiesInRect(e -> e instanceof Lantern, new Rectangle(x, y, 8, 8, Rectangle.CENTER_DIMS)))
if (e instanceof Lantern && isWithin(e.getLightRadius(), e))
return true;
for (Point p : level.getAreaTilePositions(x, y, 5)) {
Tile t = level.getTile(p.x, p.y);
int xx = Math.abs(x - p.x), yy = Math.abs(y - p.y), l = t.getLightRadius(level, p.x, p.y);
if (xx * xx + yy * yy <= l * l)
return true;
}

return false;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/client/java/minicraft/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,8 @@ public boolean isLight(int x, int y) {
if (t instanceof TorchTile)
return true;
for (Entity e : getEntitiesInRect(e -> e instanceof Lantern, new Rectangle(x, y, 8, 8, Rectangle.CENTER_DIMS))) {
if (Math.hypot((e.x >> 4) - x, (e.y >> 4) - y) < e.getLightRadius() - 1)
int xx = (e.x >> 4) - x, yy = (e.y >> 4) - y, rr = e.getLightRadius() - 1;
if (xx * xx + yy * yy < rr * rr)
return true;
}

Expand Down

0 comments on commit 33e6252

Please sign in to comment.