This commit is contained in:
gnembon committed 2020-01-17 17:10:22 -04:00
1 parent c85718a2c2
commit dd31d43192
4 files changed
+122 -7

No files matched your search

@@ -0,0 +1,12 @@
package carpet.mixins;
import net.minecraft.village.PointOfInterest;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(PointOfInterest.class)
public interface PointOfInterest_scarpetMixin
{
@Accessor("freeTickets")
int getFreeTickets();
}
@@ -5,6 +5,7 @@ import carpet.fakes.MinecraftServerInterface;
import carpet.fakes.StatTypeInterface; import carpet.fakes.StatTypeInterface;
import carpet.helpers.FeatureGenerator; import carpet.helpers.FeatureGenerator;
import carpet.mixins.ChunkTicketManager_scarpetMixin; import carpet.mixins.ChunkTicketManager_scarpetMixin;
import carpet.mixins.PointOfInterest_scarpetMixin;
import carpet.mixins.ServerChunkManager_scarpetMixin; import carpet.mixins.ServerChunkManager_scarpetMixin;
import carpet.script.Fluff.TriFunction; import carpet.script.Fluff.TriFunction;
import carpet.script.bundled.Module; import carpet.script.bundled.Module;
@@ -90,6 +91,8 @@ import net.minecraft.util.math.EulerAngle;
import net.minecraft.util.math.MutableIntBoundingBox; import net.minecraft.util.math.MutableIntBoundingBox;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import net.minecraft.village.PointOfInterest;
import net.minecraft.village.PointOfInterestStorage;
import net.minecraft.village.PointOfInterestType; import net.minecraft.village.PointOfInterestType;
import net.minecraft.world.Heightmap; import net.minecraft.world.Heightmap;
import net.minecraft.world.LightType; import net.minecraft.world.LightType;
@@ -790,21 +793,113 @@ public class CarpetExpression
return (c_, t_) -> retval; return (c_, t_) -> retval;
}); });
this.expr.addLazyFunction("block_poi", -1, (c, t, lv) -> // poi_get(pos, radius?, type?, occupation?)
this.expr.addLazyFunction("poi_get", -1, (c, t, lv) ->
{ {
CarpetContext cc = (CarpetContext) c; CarpetContext cc = (CarpetContext) c;
if (lv.size() == 0) if (lv.size() == 0)
{ {
throw new InternalExpressionException("Block requires at least one parameter"); throw new InternalExpressionException("'poi_get' requires at least one parameter");
} }
BlockPos pos = BlockValue.fromParams(cc, lv, 0, true).block.getPos(); BlockValue.LocatorResult locator = BlockValue.fromParams(cc, lv, 0, true);
PointOfInterestType poi = cc.s.getWorld().getPointOfInterestStorage().getType(pos).orElse(null); BlockPos pos = locator.block.getPos();
if (poi == null) PointOfInterestStorage store = cc.s.getWorld().getPointOfInterestStorage();
return LazyValue.NULL; if (lv.size() == locator.offset)
Value ret = new StringValue(poi.toString()); {
PointOfInterestType poiType = store.getType(pos).orElse(null);
if (poiType == null)
{
CarpetSettings.LOG.error("no poi");
return LazyValue.NULL;
}
// this feels wrong, but I don't want to mix-in more than I really need to.
// also distance adds 0.5 to each point which screws up accurate distance calculations
// you shoudn't be using POI with that in mind anyways, so I am not worried about it.
PointOfInterest poi = store.get(
poiType.getCompletionCondition(),
pos,
1,
PointOfInterestStorage.OccupationStatus.ANY
).filter(p -> p.getPos().equals(pos)).findFirst().orElse(null);
if (poi == null)
return LazyValue.NULL;
Value ret = ListValue.of(
new StringValue(poi.getType().toString()),
new NumericValue(poiType.getTicketCount() - ((PointOfInterest_scarpetMixin)poi).getFreeTickets())
);
return (_c, _t) -> ret;
}
long radius = NumericValue.asNumber(lv.get(locator.offset+0).evalValue(c)).getLong();
if (radius < 0) return ListValue.lazyEmpty();
Predicate<PointOfInterestType> condition = PointOfInterestType.ALWAYS_TRUE;
if (locator.offset + 1 < lv.size())
{
String poiType = lv.get(locator.offset+1).evalValue(c).getString().toLowerCase(Locale.ROOT);
if (!"any".equals(poiType))
{
PointOfInterestType type = Registry.POINT_OF_INTEREST_TYPE.get(new Identifier(poiType));
condition = (tt) -> tt == type;
}
}
PointOfInterestStorage.OccupationStatus status = PointOfInterestStorage.OccupationStatus.ANY;
if (locator.offset + 2 < lv.size())
{
String statusString = lv.get(locator.offset+2).evalValue(c).getString().toLowerCase(Locale.ROOT);
if ("occupied".equals(statusString))
status = PointOfInterestStorage.OccupationStatus.IS_OCCUPIED;
else if ("available".equals(statusString))
status = PointOfInterestStorage.OccupationStatus.HAS_SPACE;
}
Value ret = ListValue.wrap(store.get(condition, pos, (int)radius, status).map( p ->
ListValue.of(
new StringValue(p.getType().toString()),
new NumericValue(p.getType().getTicketCount() - ((PointOfInterest_scarpetMixin)p).getFreeTickets())
)
).collect(Collectors.toList()));
return (c_, t_) -> ret; return (c_, t_) -> ret;
}); });
//poi_set(pos, null) poi_set(pos, type, occupied?,
this.expr.addLazyFunction("poi_set", -1, (c, t, lv) ->
{
CarpetContext cc = (CarpetContext) c;
if (lv.size() == 0)
{
throw new InternalExpressionException("'poi_get' requires at least one parameter");
}
BlockValue.LocatorResult locator = BlockValue.fromParams(cc, lv, 0, true);
BlockPos pos = locator.block.getPos();
if (lv.size() < locator.offset)
throw new InternalExpressionException("'poi_set' requires the new poi type or null, after position argument");
Value poi = lv.get(locator.offset+0).evalValue(c);
PointOfInterestStorage store = cc.s.getWorld().getPointOfInterestStorage();
if (poi == Value.NULL)
{ // clear poi information
if (store.getType(pos).isPresent()) store.remove(pos);
return LazyValue.NULL;
}
String poiTypeString = poi.getString().toLowerCase(Locale.ROOT);
PointOfInterestType type = Registry.POINT_OF_INTEREST_TYPE.get(new Identifier(poiTypeString));
// solving lack of null with defaulted registries
if (type == PointOfInterestType.UNEMPLOYED && !"unemployed".equals(poiTypeString))
throw new InternalExpressionException("unknown POI type: "+poiTypeString);
int occupancy = 0;
if (locator.offset + 1 < lv.size())
{
occupancy = (int)NumericValue.asNumber(lv.get(locator.offset + 1).evalValue(c)).getLong();
if (occupancy < 0)
throw new InternalExpressionException("Occupancy cannot be negative");
}
if (store.getType(pos).isPresent()) store.remove(pos);
store.add(pos, type);
// setting occupancy for a
for (int i = 0; i < occupancy; i++) store.getPosition((p) -> p == type, p -> true, pos, 0);
return LazyValue.NULL;
});
this.expr.addLazyFunction("pos", 1, (c, t, lv) -> this.expr.addLazyFunction("pos", 1, (c, t, lv) ->
{ {
Value arg = lv.get(0).evalValue(c); Value arg = lv.get(0).evalValue(c);
@@ -1,5 +1,6 @@
package carpet.script.value; package carpet.script.value;
import carpet.script.LazyValue;
import carpet.script.exception.InternalExpressionException; import carpet.script.exception.InternalExpressionException;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
@@ -86,6 +87,12 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
return ListValue.wrap(Arrays.asList(list)); return ListValue.wrap(Arrays.asList(list));
} }
public static LazyValue lazyEmpty()
{
Value ret = new ListValue();
return (c, t) -> ret;
}
private ListValue() private ListValue()
{ {
items = new ArrayList<>(); items = new ArrayList<>();
+1
View File
@@ -149,6 +149,7 @@
"ChunkTicketManager_scarpetMixin", "ChunkTicketManager_scarpetMixin",
"ServerChunkManager_scarpetMixin", "ServerChunkManager_scarpetMixin",
"StatType_scarpetMixin", "StatType_scarpetMixin",
"PointOfInterest_scarpetMixin",
"PistonBlock_movableTEMixin", "PistonBlock_movableTEMixin",