Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/net/tfminecraft/games/Games.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ private boolean loadConfigs() {
+ " wager.y-offset=" + Cache.wagerYOffset
+ " wager.random-yaw=" + Cache.wagerRandomYaw
+ " wager.merge-range=" + Cache.wagerMergeRange
+ " wager.card-gap=" + Cache.wagerCardGap
+ " wager.vote-seconds=" + Cache.wagerVoteSeconds
+ " wager.place-seconds=" + Cache.wagerPlaceSeconds
+ " wager.payout-ticks=" + Cache.wagerPayoutTicks
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/tfminecraft/games/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public final class Cache {
public static float wagerItemScale = 0.35f;
public static boolean wagerRandomYaw = false;
public static double wagerMergeRange = 0.1;
public static double wagerCardGap = 0.1;
public static double wagerYOffset = 0;
public static int wagerVoteSeconds = 30;
public static int wagerPlaceSeconds = 10;
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/net/tfminecraft/games/layout/FeltClearance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package net.tfminecraft.games.layout;

import java.util.List;

import org.joml.Vector3f;

import net.tfminecraft.games.display.DisplayPose;

/**
* Keeps chip piles off the cards lying on the felt. A chip model is taller than a flat card, so a
* pile drawn over one hides it; the pile is drawn at the nearest spot that leaves the card showing.
*/
public final class FeltClearance {

/** The face in a card texture: 10 of 16 pixels wide and 14 of 16 tall, centred. */
static final float FACE_HALF_WIDTH = 5f / 16f;
static final float FACE_HALF_HEIGHT = 7f / 16f;
/** How finely, and how far past the gap, a pile looks for room beside the cards. */
static final double STEP = 0.02;
static final double REACH = 1.0;
static final int DIRECTIONS = 32;

private FeltClearance() {}

/** Where a pile may be drawn, such as on the felt and out of the shoe. */
@FunctionalInterface
public interface SpotFilter {
boolean allows(double x, double z);
}

/** A card's face flattened onto the table: world X/Z of its four corners, in order. */
public record Footprint(double[] xs, double[] zs) {}

/** The face of a card display at world {@code x}/{@code z} (its translation included) drawn with {@code pose}. */
public static Footprint footprint(double x, double z, DisplayPose pose) {
float[][] corners = {
{-FACE_HALF_WIDTH, -FACE_HALF_HEIGHT},
{FACE_HALF_WIDTH, -FACE_HALF_HEIGHT},
{FACE_HALF_WIDTH, FACE_HALF_HEIGHT},
{-FACE_HALF_WIDTH, FACE_HALF_HEIGHT}
};
Vector3f scale = pose.scale();
double[] xs = new double[4];
double[] zs = new double[4];
for (int i = 0; i < 4; i++) {
Vector3f corner = new Vector3f(corners[i][0], corners[i][1], 0f);
pose.rightRotation().transform(corner);
corner.mul(scale);
pose.leftRotation().transform(corner);
xs[i] = x + corner.x;
zs[i] = z + corner.z;
}
return new Footprint(xs, zs);
}

/** How far {@code x}/{@code z} is from the card, or 0 on top of it. */
public static double distance(double x, double z, Footprint card) {
double[] xs = card.xs();
double[] zs = card.zs();
boolean anyLeft = false;
boolean anyRight = false;
double best = Double.MAX_VALUE;
for (int i = 0; i < 4; i++) {
int j = (i + 1) % 4;
double ex = xs[j] - xs[i];
double ez = zs[j] - zs[i];
double cross = ex * (z - zs[i]) - ez * (x - xs[i]);
if (cross > 0) {
anyLeft = true;
} else if (cross < 0) {
anyRight = true;
}
best = Math.min(best, segmentDistance(x, z, xs[i], zs[i], ex, ez));
}
// Inside is on one side of every edge. A card squashed to a point has no inside.
return anyLeft != anyRight ? 0 : best;
}

private static double segmentDistance(double x, double z, double ax, double az, double ex, double ez) {
double lengthSq = ex * ex + ez * ez;
double t = lengthSq > 0 ? ((x - ax) * ex + (z - az) * ez) / lengthSq : 0;
t = Math.max(0, Math.min(1, t));
return Math.hypot(x - (ax + ex * t), z - (az + ez * t));
}

/** True when a pile at {@code x}/{@code z} keeps at least {@code gap} from every card. */
public static boolean clear(double x, double z, List<Footprint> cards, double gap) {
for (Footprint card : cards) {
if (distance(x, z, card) < gap) {
return false;
}
}
return true;
}

/**
* The closest spot to {@code x}/{@code z} that is clear of the cards and that {@code filter}
* allows, searched out to {@link #REACH} past the gap. The spot itself when it is already
* clear, and also when nothing in reach is, so a pile never jumps somewhere arbitrary.
*/
public static double[] nearestClear(double x, double z, List<Footprint> cards, double gap, SpotFilter filter) {
if (cards.isEmpty() || gap <= 0 || clear(x, z, cards, gap)) {
return new double[] {x, z};
}
int rings = (int) Math.round((REACH + gap) / STEP);
for (int ring = 1; ring <= rings; ring++) {
double radius = ring * STEP;
for (int i = 0; i < DIRECTIONS; i++) {
double angle = Math.PI * 2 * i / DIRECTIONS;
double cx = x + Math.cos(angle) * radius;
double cz = z + Math.sin(angle) * radius;
if (clear(cx, cz, cards, gap) && filter.allows(cx, cz)) {
return new double[] {cx, cz};
}
}
}
return new double[] {x, z};
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
2 changes: 2 additions & 0 deletions src/main/java/net/tfminecraft/games/loader/ConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public boolean loadSafe(File configFile) {
Cache.wagerYOffset = config.getDouble("wager.y-offset", Cache.wagerYOffset);
double merge = config.getDouble("wager.merge-range", Cache.wagerMergeRange);
Cache.wagerMergeRange = merge > 0 ? merge : Cache.wagerMergeRange;
double cardGap = config.getDouble("wager.card-gap", Cache.wagerCardGap);
Cache.wagerCardGap = cardGap >= 0 ? cardGap : Cache.wagerCardGap;
int voteSeconds = config.getInt("wager.vote-seconds", Cache.wagerVoteSeconds);
Cache.wagerVoteSeconds = voteSeconds > 0 ? voteSeconds : Cache.wagerVoteSeconds;
int placeSeconds = config.getInt("wager.place-seconds", Cache.wagerPlaceSeconds);
Expand Down
Loading
Loading