From 7e3e337de086548afc9c15647c905d7421a3b70a Mon Sep 17 00:00:00 2001 From: fatboy Date: Tue, 16 Apr 2024 19:52:41 -0500 Subject: [PATCH] Threads for Zerg Mechanics --- src/engine/gameManager/SimulationManager.java | 6 - src/engine/jobs/ActivateBaneJob.java | 7 + src/engine/objects/Mine.java | 133 ++-------------- .../workthreads/ZergMechanicThread.java | 145 ++++++++++++++++++ 4 files changed, 161 insertions(+), 130 deletions(-) create mode 100644 src/engine/workthreads/ZergMechanicThread.java diff --git a/src/engine/gameManager/SimulationManager.java b/src/engine/gameManager/SimulationManager.java index 8e1c5017..ff5fd022 100644 --- a/src/engine/gameManager/SimulationManager.java +++ b/src/engine/gameManager/SimulationManager.java @@ -236,12 +236,6 @@ public enum SimulationManager { return; } - for (AbstractGameObject mineObject : mineList) { - mine = (Mine) mineObject; - if(mine.isActive) - mine.onEnterZerg(); - } - _minePulseTime = System.currentTimeMillis() + CITY_PULSE; } } diff --git a/src/engine/jobs/ActivateBaneJob.java b/src/engine/jobs/ActivateBaneJob.java index 65d3cd94..99e5f91d 100644 --- a/src/engine/jobs/ActivateBaneJob.java +++ b/src/engine/jobs/ActivateBaneJob.java @@ -16,6 +16,7 @@ import engine.job.AbstractScheduleJob; import engine.net.DispatchMessage; import engine.net.client.msg.chat.ChatSystemMsg; import engine.objects.City; +import engine.workthreads.ZergMechanicThread; import org.pmw.tinylog.Logger; public class ActivateBaneJob extends AbstractScheduleJob { @@ -67,6 +68,12 @@ public class ActivateBaneJob extends AbstractScheduleJob { msg.setChannel(ChatChannelType.SYSTEM.getChannelID()); DispatchMessage.dispatchMsgToAll(msg); + + if(city.getBane() != null) { + Thread zergMechanicThread = new Thread(new ZergMechanicThread(city.getBane(), null)); + zergMechanicThread.setName(city.getName() + "Bane"); + zergMechanicThread.start(); + } } @Override diff --git a/src/engine/objects/Mine.java b/src/engine/objects/Mine.java index c751f6df..7f0f0e9f 100644 --- a/src/engine/objects/Mine.java +++ b/src/engine/objects/Mine.java @@ -15,6 +15,7 @@ import engine.gameManager.*; import engine.net.ByteBufferWriter; import engine.net.client.msg.ErrorPopupMsg; import engine.server.MBServerStatics; +import engine.workthreads.ZergMechanicThread; import org.joda.time.DateTime; import org.pmw.tinylog.Logger; @@ -55,7 +56,7 @@ public class Mine extends AbstractGameObject { public final HashSet _playerMemory = new HashSet<>(); public final HashMap _recentMemory = new HashMap<>(); - HashMap> dividedPlayers; + public HashMap> dividedPlayers; public boolean hasProduced = false; @@ -509,6 +510,10 @@ public class Mine extends AbstractGameObject { Building building = BuildingManager.getBuildingFromCache(this.buildingID); if (building != null && !this.isActive) building.isDeranking.compareAndSet(true, false); + + Thread zergMechanicThread = new Thread(new ZergMechanicThread(null, this)); + zergMechanicThread.setName(this.zoneName + "Mine"); + zergMechanicThread.start(); } public boolean validForMine(Resource r) { @@ -688,84 +693,11 @@ public class Mine extends AbstractGameObject { amount = amount / value; return (int) amount; } - public void onEnterZerg() { - - HashSet currentPlayers; - PlayerCharacter player; - - // Gather current list of players within the zone bounds - Building tower = BuildingManager.getBuildingFromCache(this.buildingID); - currentPlayers = WorldGrid.getObjectsInRangePartial(tower.loc, Enum.CityBoundsType.GRID.extents * 0.5f, MBServerStatics.MASK_PLAYER); - - for (AbstractWorldObject playerObject : currentPlayers) { - - if (playerObject == null) - continue; - - player = (PlayerCharacter) playerObject; - - // Player is already in our memory - if (this._recentMemory.containsKey(player.getObjectUUID())) - this._recentMemory.remove(player.getObjectUUID()); - - if (this._playerMemory.contains(player.getObjectUUID())) - continue; - - // Add player to our city's memory - - this._playerMemory.add(player.getObjectUUID()); - // ***For debugging - // Logger.info("PlayerMemory for ", this.getCityName() + ": " + _playerMemory.size()); - } - boolean updated = false; - try { - //updated = onExitZerg(currentPlayers); - updated = onExit(currentPlayers); - } catch (Exception e) { - Logger.error(e.getMessage()); - } - this.dividedPlayers = new HashMap<>(); - for(Integer playerID : this._playerMemory){ - player = PlayerCharacter.getFromCache(playerID); - Guild nation = player.getGuild().getNation(); Guild entry; - if(this.dividedPlayers.containsKey(nation)){ - this.dividedPlayers.get(nation).add(playerID); - }else{ - ArrayList newEntry = new ArrayList<>(); - newEntry.add(playerID); - this.dividedPlayers.put(nation,newEntry); - } - } - //if(updated == true && this.dividedPlayers.size() > 1){ - for(Integer playerID : this._playerMemory){ - player = PlayerCharacter.getFromCache(playerID); - if(this.dividedPlayers.containsKey(player.getGuild().getNation())){ - int count = this.dividedPlayers.get(player.getGuild().getNation()).size(); - switch(this.capSize){ - case 5: - player.ZergMultiplier = ZergManager.getMultiplier5Man(count,player); - break; - case 10: - player.ZergMultiplier = ZergManager.getMultiplier10Man(count,player); - break; - case 20: - player.ZergMultiplier = ZergManager.getMultiplier20Man(count,player); - break; - } - player.mineAppliedID = this.getObjectUUID(); - //ChatManager.chatSystemInfo(player,"ZergBuff: " + player.ZergMultiplier); - } else{ - player.ZergMultiplier = 1.0f; //something went wrong reset to default until next cycle - //ChatManager.chatSystemInfo(player,"There Was a Problem"); - } - } - //} - } - private Boolean onExit(HashSet currentPlayers){ + public Boolean onExit(HashSet currentPlayers){ ArrayList purge = new ArrayList<>(); for(int id : this._playerMemory){ PlayerCharacter player = PlayerCharacter.getPlayerCharacter(id); - if(currentPlayers.contains(player) == false){ + if(!currentPlayers.contains(player)){ purge.add(id); player.ZergMultiplier = 1.0f; //ChatManager.chatSystemInfo(player,"Left Mine, Multiplier: " + player.ZergMultiplier); @@ -775,7 +707,7 @@ public class Mine extends AbstractGameObject { if(purge.size() > 0) { //this._playerMemory.removeAll(purge); for(int id : purge){ - if(this._recentMemory.containsKey(id) == false) { + if(!this._recentMemory.containsKey(id)) { this._recentMemory.put(id, System.currentTimeMillis() + 60000); } else if(this._recentMemory.get(id) > System.currentTimeMillis()){ this._playerMemory.remove(id); @@ -785,51 +717,4 @@ public class Mine extends AbstractGameObject { } return true; } - private Boolean onExitZerg(HashSet currentPlayers) { - - PlayerCharacter player; - int playerUUID = 0; - HashSet toRemove = new HashSet<>(); - Iterator iter = _playerMemory.iterator(); - while (iter.hasNext()) { - - playerUUID = iter.next(); - - - player = PlayerCharacter.getFromCache(playerUUID); - - if (currentPlayers.contains(player)) - continue; - - toRemove.add(playerUUID); - if(this._recentMemory.containsKey(playerUUID) == false) { - this._recentMemory.put(playerUUID, System.currentTimeMillis()); - PlayerCharacter.getPlayerCharacter(playerUUID).mineAppliedID = 0; - PlayerCharacter.getPlayerCharacter(playerUUID).ZergMultiplier = 1.0f; - } - //player.ZergMultiplier = 1.0f; // reset damage modifier to 1.0 - } - - // Remove players from mine memory - - //_playerMemory.removeAll(toRemove); - HashSet purge = new HashSet<>(); - for(Integer id : _recentMemory.keySet()){ - if(System.currentTimeMillis() > _recentMemory.get(playerUUID) + 60000){ - purge.add(id); - } - } - for(Integer id : purge){ - this._recentMemory.remove(id); - this._playerMemory.remove(id); - PlayerCharacter.getPlayerCharacter(playerUUID).mineAppliedID = 0; - PlayerCharacter.getPlayerCharacter(playerUUID).ZergMultiplier = 1.0f; - } - if(toRemove.isEmpty()){ - return false; - }else{ - return true; - } - } - } diff --git a/src/engine/workthreads/ZergMechanicThread.java b/src/engine/workthreads/ZergMechanicThread.java new file mode 100644 index 00000000..07f99b89 --- /dev/null +++ b/src/engine/workthreads/ZergMechanicThread.java @@ -0,0 +1,145 @@ +// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . +// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· +// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ +// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ +// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ +// Magicbane Emulator Project © 2013 - 2022 +// www.magicbane.com + + +package engine.workthreads; + +import engine.Enum; +import engine.InterestManagement.WorldGrid; +import engine.db.archive.DataWarehouse; +import engine.db.archive.MineRecord; +import engine.gameManager.*; +import engine.net.DispatchMessage; +import engine.net.MessageDispatcher; +import engine.net.client.msg.chat.ChatSystemMsg; +import engine.objects.*; +import engine.server.MBServerStatics; +import engine.server.world.WorldServer; +import org.pmw.tinylog.Logger; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.concurrent.ConcurrentHashMap; + +import static engine.server.MBServerStatics.MINE_LATE_WINDOW; + +public class ZergMechanicThread implements Runnable { +public Bane bane = null; +public Mine mine = null; +public boolean isMine = false; + + public ZergMechanicThread(Bane b, Mine m) { + if(b != null){ + this.isMine = false; + } + if(m != null){ + this.isMine = true; + } + } + + @Override + public void run() { + + if(mine != null) + while(mine.isActive) + RunMineMechanic(); + + if(bane != null) + while(bane.getSiegePhase().equals(Enum.SiegePhase.WAR)) + RunBaneMechanic(); + } + + public void RunMineMechanic(){ + + HashSet currentPlayers; + PlayerCharacter player; + + // Gather current list of players within the zone bounds + Building tower = BuildingManager.getBuildingFromCache(mine.getBuildingID()); + currentPlayers = WorldGrid.getObjectsInRangePartial(tower.loc, Enum.CityBoundsType.GRID.extents * 0.5f, MBServerStatics.MASK_PLAYER); + + for (AbstractWorldObject playerObject : currentPlayers) { + + if (playerObject == null) + continue; + + player = (PlayerCharacter) playerObject; + + if (mine._recentMemory.containsKey(player.getObjectUUID())) + mine._recentMemory.remove(player.getObjectUUID()); + + if (mine._playerMemory.contains(player.getObjectUUID())) + continue; + + mine._playerMemory.add(player.getObjectUUID()); + } + boolean updated = false; + try { + mine.onExit(currentPlayers); + } catch (Exception e) { + Logger.error(e.getMessage()); + } + mine.dividedPlayers = new HashMap<>(); + for(Integer playerID : mine._playerMemory){ + player = PlayerCharacter.getFromCache(playerID); + Guild nation = player.getGuild().getNation(); Guild entry; + if(mine.dividedPlayers.containsKey(nation)){ + mine.dividedPlayers.get(nation).add(playerID); + }else{ + ArrayList newEntry = new ArrayList<>(); + newEntry.add(playerID); + mine.dividedPlayers.put(nation,newEntry); + } + } + for(Integer playerID : mine._playerMemory){ + player = PlayerCharacter.getFromCache(playerID); + if(mine.dividedPlayers.containsKey(player.getGuild().getNation())){ + int count = mine.dividedPlayers.get(player.getGuild().getNation()).size(); + switch(mine.capSize){ + case 5: + player.ZergMultiplier = ZergManager.getMultiplier5Man(count,player); + break; + case 10: + player.ZergMultiplier = ZergManager.getMultiplier10Man(count,player); + break; + case 20: + player.ZergMultiplier = ZergManager.getMultiplier20Man(count,player); + break; + } + player.mineAppliedID = mine.getObjectUUID(); + } else{ + player.ZergMultiplier = 1.0f; + } + } + } + + public void RunBaneMechanic(){ + HashSet currentPlayers; + PlayerCharacter player; + + currentPlayers = WorldGrid.getObjectsInRangePartial(bane.getCity().loc, Enum.CityBoundsType.GRID.extents * 2.0f, MBServerStatics.MASK_PLAYER); + Guild attacker = bane.getStone().getGuild().getNation(); + Guild defender = bane.getCity().getGuild().getNation(); + int attackers = 0; + int defenders = 0; + for(AbstractWorldObject awo : currentPlayers){ + PlayerCharacter pc = (PlayerCharacter) awo; + if(pc.getGuild().getNation().equals(attacker) == false && pc.getGuild().getNation().equals(defender) == false) + pc.teleport(pc.bindLoc); + + if(pc.getGuild().getNation().equals(attacker)) + attackers++; + + if(pc.getGuild().getNation().equals(defender)) + defenders++; + } + + } +}