Skip to content

Commit

Permalink
Merge pull request #4940 from kwvanderlinde/feature/4936-render-light…
Browse files Browse the repository at this point in the history
…s-per-lumens-level

Restrict each drawable light to its lumens level
  • Loading branch information
cwisniew authored Sep 26, 2024
2 parents f12401c + 740aec3 commit 8b200f3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/main/java/net/rptools/maptool/client/ui/zone/Illumination.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ public Optional<LumensLevel> getObscuredLumensLevel(int lumensStrength) {
return Collections.unmodifiableList(this.obscuredLumensLevels);
}

/**
* Look up a disjoint obscured lumens level based on the lumens strength.
*
* <p>This is useful for rendering individual lights, so that the light can be constrained to the
* area that is actually illuminated by the light and not by any stronger light.
*
* <p>See {@link #getDisjointObscuredLumensLevels()} for more information.
*
* @param lumensStrength The strength of lumens to find.
* @return The {#link LumensLevel} of strength {@code lumensStrength}. If no level exists for it,
* an empty optional is returned.
*/
public Optional<LumensLevel> getDisjointObscuredLumensLevel(int lumensStrength) {
return getDisjointObscuredLumensLevels().stream()
.filter(level -> level.lumensStrength() == lumensStrength)
.findFirst();
}

/**
* Get the disjoint obscured lumens levels.
*
Expand Down
37 changes: 29 additions & 8 deletions src/main/java/net/rptools/maptool/client/ui/zone/ZoneView.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import net.rptools.maptool.model.zones.TokensChanged;
import net.rptools.maptool.model.zones.TokensRemoved;
import net.rptools.maptool.model.zones.TopologyChanged;
import net.rptools.maptool.model.zones.ZoneLightingChanged;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -724,6 +725,8 @@ public Collection<DrawableLight> getDrawableLights(PlayerView view) {
drawableLights.computeIfAbsent(
view,
view2 -> {
final var lightingStyle = zone.getLightingStyle();

final var illuminationKey = illuminationKeyFromView(view2);
final var illuminationModel = getIlluminationModel(illuminationKey);

Expand Down Expand Up @@ -755,9 +758,16 @@ public Collection<DrawableLight> getDrawableLights(PlayerView view) {
// Make sure each drawable light is restricted to the area it covers,
// accounting for darkness effects.
final var obscuredArea = new Area(laud.litArea().area());

final var lumensStrength = Math.abs(laud.litArea().lumens());
final var lumensLevel =
illumination.getObscuredLumensLevel(Math.abs(laud.litArea().lumens()));
// Should always be present based on construction, but just in case.
switch (lightingStyle) {
case ENVIRONMENTAL -> illumination.getObscuredLumensLevel(
lumensStrength);
case OVERTOP -> illumination.getDisjointObscuredLumensLevel(
lumensStrength);
};
// Should always be present based on construction, but just in case...
if (lumensLevel.isEmpty()) {
return null;
}
Expand Down Expand Up @@ -794,14 +804,18 @@ public void flush() {
exposedAreaMap.clear();
visibleAreaMap.clear();

drawableLights.clear();
drawableAuras.clear();
flushLights();
}

public void flushFog() {
exposedAreaMap.clear();
}

private void flushLights() {
drawableLights.clear();
drawableAuras.clear();
}

/**
* Flush the ZoneView cache of the token. Remove token from {@link #tokenVisionCachePerView}, and
* {@link #illuminationModels}. Can clear {@link #tokenVisionCachePerView}, {@link
Expand Down Expand Up @@ -860,6 +874,15 @@ private void onTopologyChanged(TopologyChanged event) {
topologyTrees.clear();
}

@Subscribe
private void onZoneLightingChanged(ZoneLightingChanged event) {
if (event.zone() != this.zone) {
return;
}

flushLights();
}

private boolean flushExistingTokens(List<Token> tokens) {
boolean tokenChangedTopology = false;
for (Token token : tokens) {
Expand Down Expand Up @@ -896,8 +919,7 @@ private void onTokensRemoved(TokensRemoved event) {
}

if (anyLightingChanges) {
drawableLights.clear();
drawableAuras.clear();
flushLights();
}

if (event.tokens().stream().anyMatch(Token::hasAnyTopology)) {
Expand Down Expand Up @@ -963,8 +985,7 @@ private void updateLightSourcesFromTokens(Iterable<Token> tokens) {
}

if (anyLightingChanges) {
drawableLights.clear();
drawableAuras.clear();
flushLights();
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/net/rptools/maptool/model/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import net.rptools.maptool.model.zones.TokensChanged;
import net.rptools.maptool.model.zones.TokensRemoved;
import net.rptools.maptool.model.zones.TopologyChanged;
import net.rptools.maptool.model.zones.ZoneLightingChanged;
import net.rptools.maptool.server.Mapper;
import net.rptools.maptool.server.proto.DrawnElementListDto;
import net.rptools.maptool.server.proto.TopologyTypeDto;
Expand Down Expand Up @@ -515,6 +516,7 @@ public LightingStyle getLightingStyle() {

public void setLightingStyle(LightingStyle lightingStyle) {
this.lightingStyle = lightingStyle;
new MapToolEventBus().getMainEventBus().post(new ZoneLightingChanged(this));
}

public TokenSelection getTokenSelection() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.model.zones;

import net.rptools.maptool.model.Zone;

public record ZoneLightingChanged(Zone zone) {}

0 comments on commit 8b200f3

Please sign in to comment.