Files
prestonbane/src/engine/gameManager/ArenaManager.java
T

114 lines
3.8 KiB
Java
Raw Normal View History

2025-01-01 17:44:34 -06:00
package engine.gameManager;
2025-01-01 19:18:31 -06:00
import engine.InterestManagement.WorldGrid;
2025-01-01 17:44:34 -06:00
import engine.math.Vector3fImmutable;
2025-01-01 19:18:31 -06:00
import engine.objects.*;
import engine.server.MBServerStatics;
2025-01-01 17:44:34 -06:00
import org.pmw.tinylog.Logger;
2025-01-01 19:18:31 -06:00
import java.util.*;
2025-01-01 17:44:34 -06:00
import java.util.concurrent.ThreadLocalRandom;
public class ArenaManager {
private static final List<Arena> activeArenas = new ArrayList<>();
2025-01-01 18:43:32 -06:00
public static final List<PlayerCharacter> playerQueue = new ArrayList<>();
2025-01-01 19:18:31 -06:00
public static Long pulseDelay = 180000L;
public static Long lastExecution = 0L;
2025-01-01 17:44:34 -06:00
public static void pulseArenas() {
2025-01-01 19:18:31 -06:00
if(lastExecution == 0L){
lastExecution = System.currentTimeMillis();
}
2025-01-01 19:26:02 -06:00
if(activeArenas.isEmpty() && playerQueue.isEmpty())
return;
2025-01-01 17:44:34 -06:00
Iterator<Arena> iterator = activeArenas.iterator();
while (iterator.hasNext()) {
Arena arena = iterator.next();
if (arena.checkToComplete()) {
iterator.remove();
}
}
2025-01-01 19:18:31 -06:00
if(lastExecution + pulseDelay < System.currentTimeMillis())
return;
lastExecution = System.currentTimeMillis();
2025-01-01 17:44:34 -06:00
while (playerQueue.size() > 1) {
createArena();
}
}
public static void joinQueue(PlayerCharacter player) {
if (!playerQueue.contains(player)) {
playerQueue.add(player);
}
}
public static void leaveQueue(PlayerCharacter player) {
playerQueue.remove(player);
}
private static void createArena() {
if (playerQueue.size() > 1) {
2025-01-01 19:18:31 -06:00
Collections.shuffle(playerQueue);
2025-01-01 17:44:34 -06:00
Arena newArena = new Arena();
//decide an arena location
newArena.loc = selectRandomArenaLocation();
// Assign players to the arena
newArena.player1 = playerQueue.remove(0);
newArena.player2 = playerQueue.remove(0);
// Teleport players to the arena location
MovementManager.translocate(newArena.player1, newArena.loc, Regions.GetRegionForTeleport(newArena.loc));
MovementManager.translocate(newArena.player2, newArena.loc, Regions.GetRegionForTeleport(newArena.loc));
// Add the new arena to the active arenas list
activeArenas.add(newArena);
}
}
public static void endArena(Arena arena, PlayerCharacter winner, PlayerCharacter loser, String condition) {
if (winner != null && loser != null) {
Logger.info("[ARENA] The fight between {} and {} is concluded. Victor: {}",
arena.player1.getName(), arena.player2.getName(), winner.getName());
} else {
Logger.info("[ARENA] The fight between {} and {} is concluded. No Winner Declared.",
arena.player1.getName(), arena.player2.getName());
}
activeArenas.remove(arena);
}
public static Vector3fImmutable selectRandomArenaLocation() {
boolean locSet = false;
Vector3fImmutable loc = Vector3fImmutable.ZERO;
while (!locSet) {
try {
// Generate random X and Z coordinates within the range [10,000, 90,000]
float x = ThreadLocalRandom.current().nextInt(10000, 90000);
float z = ThreadLocalRandom.current().nextInt(-10000, -90000);
float y = 0; // Y coordinate is always 0
loc = new Vector3fImmutable(x, y, z);
Zone zone = ZoneManager.findSmallestZone(loc);
if (zone.isContinent() && !ZoneManager.getSeaFloor().equals(zone)) {
2025-01-01 19:18:31 -06:00
HashSet<AbstractWorldObject> inRange = WorldGrid.getObjectsInRangePartial(loc,250f, MBServerStatics.MASK_PLAYER);
if(inRange.isEmpty())
locSet = true;
2025-01-01 17:44:34 -06:00
}
}catch(Exception e){
}
}
return loc;
}
}