Skip to content

Commit

Permalink
rewrite #11
Browse files Browse the repository at this point in the history
rewrote spawn condition handling
  • Loading branch information
Traben-0 committed Nov 24, 2023
1 parent 98561d5 commit 32f667f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import traben.entity_texture_features.config.screens.skin.ETFConfigScreenSkinTool;
import traben.entity_texture_features.features.player.ETFPlayerEntity;
import traben.entity_texture_features.features.player.ETFPlayerTexture;
import traben.entity_texture_features.features.property_reading.properties.RandomProperty;
import traben.entity_texture_features.features.texture_handlers.ETFDirectory;
import traben.entity_texture_features.features.texture_handlers.ETFTexture;
import traben.entity_texture_features.features.texture_handlers.ETFTextureVariator;
Expand Down Expand Up @@ -45,7 +44,7 @@ public class ETFManager {
public final Object2IntOpenHashMap<EntityType<?>> ENTITY_TYPE_VANILLA_BRIGHTNESS_OVERRIDE_VALUE = new Object2IntOpenHashMap<>();
public final ObjectOpenHashSet<EntityType<?>> ENTITY_TYPE_IGNORE_PARTICLES = new ObjectOpenHashSet<>();
public final Object2IntOpenHashMap<EntityType<?>> ENTITY_TYPE_RENDER_LAYER = new Object2IntOpenHashMap<>();
public final ETFLruCache<UUID, Object2BooleanOpenHashMap<RandomProperty>> ENTITY_SPAWN_CONDITIONS_CACHE = new ETFLruCache<>();
// public final ETFLruCache<UUID, Object2BooleanOpenHashMap<RandomProperty>> ENTITY_SPAWN_CONDITIONS_CACHE = new ETFLruCache<>();
//this is a cache of all known ETFTexture versions of any existing resource-pack texture, used to prevent remaking objects
public final Object2ReferenceOpenHashMap<@NotNull Identifier, @Nullable ETFTexture> ETF_TEXTURE_CACHE = new Object2ReferenceOpenHashMap<>();
// public final Object2BooleanOpenHashMap<UUID> ENTITY_IS_UPDATABLE = new Object2BooleanOpenHashMap<>();
Expand Down Expand Up @@ -178,7 +177,7 @@ public void grabSpecialProperties(Properties props, ETFEntity entity) {

public void removeThisEntityDataFromAllStorage(UUID uuid) {
//todo still needed? expand?
ENTITY_SPAWN_CONDITIONS_CACHE.removeEntryOnly(uuid);
// ENTITY_SPAWN_CONDITIONS_CACHE.removeEntryOnly(uuid);
// ENTITY_IS_UPDATABLE.removeBoolean(uuid);
ENTITY_DEBUG_QUEUE.remove(uuid);
ENTITY_BLINK_TIME.removeLong(uuid);
Expand Down Expand Up @@ -306,4 +305,5 @@ public int put(UUID uuid, int v) {
return this.putAndMoveToFirst(uuid, v);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package traben.entity_texture_features.features.property_reading;

import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import net.minecraft.client.MinecraftClient;
import net.minecraft.resource.Resource;
import net.minecraft.resource.ResourceManager;
Expand All @@ -15,6 +14,7 @@
import traben.entity_texture_features.features.texture_handlers.ETFDirectory;
import traben.entity_texture_features.utils.ETFEntity;
import traben.entity_texture_features.utils.ETFUtils2;
import traben.entity_texture_features.utils.EntityBooleanLRU;

import java.util.*;

Expand All @@ -23,7 +23,7 @@ public class PropertiesRandomProvider implements ETFApi.ETFVariantSuffixProvider

protected final List<RandomPropertyRule> propertyRules;

protected final Object2BooleanOpenHashMap<UUID> entityCanUpdate = new Object2BooleanOpenHashMap<>();
protected final EntityBooleanLRU entityCanUpdate = new EntityBooleanLRU(1000);

protected final String packname;

Expand Down Expand Up @@ -158,11 +158,30 @@ public int size() {
public int getSuffixForETFEntity(ETFEntity entityToBeTested) {
if (entityToBeTested == null) return 0;
UUID id = entityToBeTested.etf$getUuid();
boolean entityTestedBefore = entityCanUpdate.containsKey(id);
for (RandomPropertyRule testCase : propertyRules) {
if (testCase.doesEntityMeetConditionsOfThisCase(entityToBeTested, entityTestedBefore, entityCanUpdate)) {
return testCase.getVariantSuffixFromThisCase(id);
boolean entityHasBeenTestedBefore = entityCanUpdate.containsKey(id);
if(entityHasBeenTestedBefore){
//return andNothingElse
for (RandomPropertyRule testCase : propertyRules) {
if (testCase.doesEntityMeetConditionsOfThisCase(entityToBeTested, true, entityCanUpdate)) {
return testCase.getVariantSuffixFromThisCase(id);
}
}
} else {
//return but capture spawn conditions of first time entity
int foundSuffix = -1;
for (RandomPropertyRule rule : propertyRules) {
if (rule.doesEntityMeetConditionsOfThisCase(entityToBeTested, false, entityCanUpdate)) {
foundSuffix = rule.getVariantSuffixFromThisCase(id);
break;
}
}
if(entityCanUpdate.getBoolean(entityToBeTested.etf$getUuid())) {
for (RandomPropertyRule rule : propertyRules) {
//cache entity spawns
rule.cacheEntityInitialResultsOfNonUpdatingProperties(entityToBeTested);
}
}
return foundSuffix == -1 ? 0 : foundSuffix;
}
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package traben.entity_texture_features.features.property_reading;

import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import traben.entity_texture_features.features.ETFManager;
import traben.entity_texture_features.features.property_reading.properties.RandomProperty;
import traben.entity_texture_features.utils.ETFEntity;
import traben.entity_texture_features.utils.ETFUtils2;
import traben.entity_texture_features.utils.EntityBooleanLRU;

import java.util.*;

Expand Down Expand Up @@ -59,20 +59,12 @@ public Set<Integer> getSuffixSet() {
return new HashSet<>(List.of(SUFFIX_NUMBERS_WEIGHTED));
}

public boolean doesEntityMeetConditionsOfThisCase(ETFEntity etfEntity, boolean isUpdate, Object2BooleanOpenHashMap<UUID> UUID_CaseHasUpdateablesCustom) {
public boolean doesEntityMeetConditionsOfThisCase(ETFEntity etfEntity, boolean isUpdate, EntityBooleanLRU UUID_CaseHasUpdateablesCustom) {
if (RULE_ALWAYS_APPROVED) return true;
if (etfEntity == null) return false;

UUID id = etfEntity.etf$getUuid();

Object2BooleanOpenHashMap<RandomProperty> spawnConditions;
if (ETFManager.getInstance().ENTITY_SPAWN_CONDITIONS_CACHE.containsKey(id)) {
spawnConditions = (ETFManager.getInstance().ENTITY_SPAWN_CONDITIONS_CACHE.get(id));
} else {
spawnConditions = new Object2BooleanOpenHashMap<>();
ETFManager.getInstance().ENTITY_SPAWN_CONDITIONS_CACHE.put(id, spawnConditions);
}

boolean wasEntityTestedByAnUpdatableProperty = false;
boolean entityMetRequirements = true;
try {
Expand All @@ -81,7 +73,7 @@ public boolean doesEntityMeetConditionsOfThisCase(ETFEntity etfEntity, boolean i
if (!entityMetRequirements) break;
if (property.isPropertyUpdatable())
wasEntityTestedByAnUpdatableProperty = true;
entityMetRequirements = property.testEntity(etfEntity, isUpdate, spawnConditions);
entityMetRequirements = property.testEntity(etfEntity, isUpdate);
}
} catch (Exception e) {
ETFUtils2.logWarn("Random Property file [" +
Expand All @@ -105,4 +97,16 @@ public int getVariantSuffixFromThisCase(UUID uuid) {
return SUFFIX_NUMBERS_WEIGHTED[randomSeededByUUID % SUFFIX_NUMBERS_WEIGHTED.length];
}


public void cacheEntityInitialResultsOfNonUpdatingProperties(ETFEntity entity){
try {
for (RandomProperty property :
PROPERTIES_TO_TEST) {
if (!property.isPropertyUpdatable()) {
property.cacheEntityInitialResult(entity);
}
}
} catch (Exception ignored) {}
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package traben.entity_texture_features.features.property_reading.properties;

import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import org.jetbrains.annotations.NotNull;
import traben.entity_texture_features.utils.ETFEntity;
import traben.entity_texture_features.utils.EntityBooleanLRU;

import java.util.Properties;

Expand Down Expand Up @@ -44,28 +44,21 @@ public static String readPropertiesOrThrow(Properties properties, int propertyNu
*
* @param entity the ETFEntity being tested by this property
* @param isUpdate flags if this test is part of an update
* @param spawnConditions the original spawn conditions map of this entity, possibly holding a prior value for this test
* @return true if the entity meets the requirements of this property
*/
public boolean testEntity(ETFEntity entity, boolean isUpdate, Object2BooleanOpenHashMap<RandomProperty> spawnConditions) {
public boolean testEntity(ETFEntity entity, boolean isUpdate) {
if (isUpdate && !isPropertyUpdatable()) {
return entityCachedInitialResult.getBoolean(entity.etf$getUuid());//false default value
}
try {
if (isUpdate && !isPropertyUpdatable()) {
if (spawnConditions.containsKey(this)) {
return spawnConditions.getBoolean(this);
}
return false;
}
boolean result = testEntityInternal(entity);
if (!isPropertyUpdatable())
spawnConditions.put(this, result);
return result;
return testEntityInternal(entity);
} catch (Exception ignored) {
return false;
}
}

/**
* Internal abstract method functionality for {@link RandomProperty#testEntity(ETFEntity, boolean, Object2BooleanOpenHashMap)}
* Internal abstract method functionality for {@link RandomProperty#testEntity(ETFEntity, boolean)}
*/
protected abstract boolean testEntityInternal(ETFEntity entity);

Expand Down Expand Up @@ -132,4 +125,10 @@ public RandomPropertyException(String reason) {
super("[ETF] " + reason);
}
}

protected EntityBooleanLRU entityCachedInitialResult = new EntityBooleanLRU();

public void cacheEntityInitialResult(ETFEntity entity){
entityCachedInitialResult.put(entity.etf$getUuid(),testEntityInternal(entity));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package traben.entity_texture_features.utils;

import it.unimi.dsi.fastutil.objects.Object2BooleanLinkedOpenHashMap;

import java.util.UUID;

public class EntityBooleanLRU extends Object2BooleanLinkedOpenHashMap<UUID> {
{
defaultReturnValue(false);
}

final int capacity;

public EntityBooleanLRU(int capacity) {
this.capacity = capacity;
}

public EntityBooleanLRU() {
this.capacity = 2048;
}

@Override
public boolean put(UUID uuid, boolean v) {
if (size() >= capacity) {
UUID lastKey = lastKey();
if (!lastKey.equals(uuid)) {
removeBoolean(lastKey);
}
}
return this.putAndMoveToFirst(uuid, v);
}
}

0 comments on commit 32f667f

Please sign in to comment.