Volver al índice
src/main/java/ar/com/companeros/runtime/ContactInterpretation.java
package ar.com.companeros.runtime;
import java.util.LinkedHashMap;
import java.util.UUID;
/** Distingue un toque humano aislado de un ataque; no vuelve invulnerable al bot. */
public final class ContactInterpretation {
private ContactInterpretation() { }
public static boolean hostile(boolean human, boolean armedOrProjectile, float damage, float health,
long ticksSincePreviousHit) {
return !human || armedOrProjectile || damage >= 3 || health - damage <= 6 || ticksSincePreviousHit <= 80;
}
/** Contexto efímero por servidor. Sólo observa gestos reales que el bot pudo ver. */
public static final class PeaceSignals {
public static final int MAX_HUMANS = 64, MAX_BOTS_PER_HUMAN = 16;
private static final long RESPONSE_TICKS = 60, CROUCH_TICKS = 30, SIGNAL_TICKS = 100, HOSTILE_TICKS = 100;
private final LinkedHashMap<UUID, LinkedHashMap<UUID, Contact>> humans = new LinkedHashMap<>(16, .75F, true);
private static final class Contact {
long lastHit = -1, pendingUntil = -1, crouchBegan = -1, peaceUntil = -1, blockedUntil = -1;
boolean crouching;
}
// Registrar daño no lo cancela. Un golpe nuevo siempre invalida cualquier gesto anterior.
public boolean recordHit(UUID bot, UUID human, boolean armed, float damage, float health,
long ticksSincePreviousHit, long now, boolean crouching) {
Contact contact = contact(bot, human, true);
long previous = contact.lastHit < 0 ? ticksSincePreviousHit
: Math.min(ticksSincePreviousHit, Math.max(0, now - contact.lastHit));
boolean serious = !Float.isFinite(damage) || !Float.isFinite(health) || damage <= 0
|| hostile(true, armed, damage, health, previous) || now < contact.blockedUntil;
contact.lastHit = now; contact.crouching = crouching;
contact.crouchBegan = -1; contact.peaceUntil = -1; contact.pendingUntil = -1;
if (serious) contact.blockedUntil = now + HOSTILE_TICKS;
else contact.pendingUntil = now + RESPONSE_TICKS;
return serious;
}
// El llamador comprueba cercanía y línea de visión antes de transmitir una postura.
public boolean needsPostureObservation(UUID bot, UUID human, long now) {
Contact contact = contact(bot, human, false);
return contact != null && now >= contact.lastHit && now < contact.pendingUntil
&& now >= contact.blockedUntil;
}
public void observePosture(UUID bot, UUID human, boolean crouching, long now) {
if (!needsPostureObservation(bot, human, now)) return;
Contact contact = contact(bot, human, false);
if (crouching == contact.crouching) return;
contact.crouching = crouching;
if (crouching) contact.crouchBegan = now;
else if (contact.crouchBegan >= contact.lastHit && now > contact.crouchBegan
&& now - contact.crouchBegan <= CROUCH_TICKS) {
contact.peaceUntil = now + SIGNAL_TICKS;
contact.pendingUntil = -1; contact.crouchBegan = -1;
} else contact.crouchBegan = -1;
}
// Es evidencia de intención posible, no absolución ni orden de abandonar un combate.
public boolean hasPeaceSignal(UUID bot, UUID human, long now) {
Contact contact = contact(bot, human, false);
return contact != null && now >= contact.lastHit && now >= contact.blockedUntil
&& now < contact.peaceUntil;
}
public boolean consumePeaceSignal(UUID bot, UUID human, long now) {
if (!hasPeaceSignal(bot, human, now)) return false;
contact(bot, human, false).peaceUntil = -1; return true;
}
// Desconexión y retiro eliminan contexto transitorio; la memoria duradera vive fuera de aquí.
public void forgetHuman(UUID human) { humans.remove(human); }
public void forgetBot(UUID bot) {
humans.values().forEach(contacts -> contacts.remove(bot));
humans.values().removeIf(contacts -> contacts.isEmpty());
}
private Contact contact(UUID bot, UUID human, boolean create) {
var contacts = humans.get(human);
if (contacts == null) {
if (!create) return null;
contacts = new LinkedHashMap<>(16, .75F, true); humans.put(human, contacts);
while (humans.size() > MAX_HUMANS) humans.remove(humans.keySet().iterator().next());
}
Contact contact = contacts.get(bot);
if (contact == null && create) {
contact = new Contact(); contacts.put(bot, contact);
while (contacts.size() > MAX_BOTS_PER_HUMAN) contacts.remove(contacts.keySet().iterator().next());
}
return contact;
}
}
}