Browse Source

Refactor to concurrent list.

bugfix-slot-update-2
MagicBot 9 months ago
parent
commit
6d8866f019
  1. 4
      src/engine/devcmd/cmds/InfoCmd.java
  2. 4
      src/engine/devcmd/cmds/PurgeObjectsCmd.java
  3. 2
      src/engine/devcmd/cmds/RemoveObjectCmd.java
  4. 4
      src/engine/devcmd/cmds/SlotTestCmd.java
  5. 20
      src/engine/gameManager/BuildingManager.java
  6. 2
      src/engine/gameManager/NPCManager.java
  7. 10
      src/engine/net/client/handlers/OrderNPCMsgHandler.java
  8. 13
      src/engine/net/client/msg/ManageCityAssetsMsg.java
  9. 6
      src/engine/objects/Building.java
  10. 2
      src/engine/objects/City.java
  11. 2
      src/engine/objects/Mine.java

4
src/engine/devcmd/cmds/InfoCmd.java

@ -225,11 +225,11 @@ public class InfoCmd extends AbstractDevCmd {
output += newline; output += newline;
output += "Hirelings List: name / slot / floor"; output += "Hirelings List: name / slot / floor";
for (AbstractCharacter npc : targetBuilding.hirelings.keySet()) { for (AbstractCharacter npc : targetBuilding.hirelings) {
if (npc.getObjectType() != GameObjectType.NPC) if (npc.getObjectType() != GameObjectType.NPC)
continue; continue;
output += newline + npc.getName() + " slot " + targetBuilding.hirelings.get(npc); output += newline + npc.getName() + " slot " + targetBuilding.hirelings.indexOf(npc) + 1;
output += newline + "location " + npc.getLoc(); output += newline + "location " + npc.getLoc();
} }
} }

4
src/engine/devcmd/cmds/PurgeObjectsCmd.java

@ -48,7 +48,7 @@ public class PurgeObjectsCmd extends AbstractDevCmd {
for (Building building : zone.zoneBuildingSet) { for (Building building : zone.zoneBuildingSet) {
if (!BuildingManager.IsWallPiece(building)) if (!BuildingManager.IsWallPiece(building))
continue; continue;
for (AbstractCharacter ac : building.hirelings.keySet()) { for (AbstractCharacter ac : building.hirelings) {
NPC npc = null; NPC npc = null;
Mob mobA = null; Mob mobA = null;
@ -143,7 +143,7 @@ public class PurgeObjectsCmd extends AbstractDevCmd {
if ((building.getBlueprint() != null) && (building.getBlueprint().getBuildingGroup() == BuildingGroup.SPIRE)) if ((building.getBlueprint() != null) && (building.getBlueprint().getBuildingGroup() == BuildingGroup.SPIRE))
building.disableSpire(false); building.disableSpire(false);
for (AbstractCharacter ac : building.hirelings.keySet()) { for (AbstractCharacter ac : building.hirelings) {
NPC npc = null; NPC npc = null;
Mob mobA = null; Mob mobA = null;

2
src/engine/devcmd/cmds/RemoveObjectCmd.java

@ -144,7 +144,7 @@ public class RemoveObjectCmd extends AbstractDevCmd {
// Remove hirelings for this building // Remove hirelings for this building
for (AbstractCharacter abstractCharacter : building.hirelings.keySet()) for (AbstractCharacter abstractCharacter : building.hirelings)
BuildingManager.removeHireling(building, abstractCharacter); BuildingManager.removeHireling(building, abstractCharacter);
Zone zone = building.getParentZone(); Zone zone = building.getParentZone();

4
src/engine/devcmd/cmds/SlotTestCmd.java

@ -70,8 +70,8 @@ public class SlotTestCmd extends AbstractDevCmd {
outString += "\r\n\r\n"; outString += "\r\n\r\n";
outString += "Hirelings List:"; outString += "Hirelings List:";
for (AbstractCharacter hireling : building.hirelings.keySet()) for (AbstractCharacter hireling : building.hirelings)
outString += "\r\n" + hireling.getName() + " slot : " + building.hirelings.get(hireling); outString += "\r\n" + hireling.getName() + " slot : " + building.hirelings.indexOf(hireling) + 1;
} }

20
src/engine/gameManager/BuildingManager.java

@ -53,9 +53,9 @@ public enum BuildingManager {
int numOfSlots = _slotLocations.get(building.meshUUID).size(); int numOfSlots = _slotLocations.get(building.meshUUID).size();
for (int i = 1; i <= numOfSlots; i++) { for (int i = 0; i < numOfSlots; i++) {
if (!building.hirelings.containsValue(i)) if (!building.hirelings.contains(i))
return i; return i;
} }
return -1; return -1;
@ -73,9 +73,9 @@ public enum BuildingManager {
int numOfSlots = _slotLocations.get(building.meshUUID).size(); int numOfSlots = _slotLocations.get(building.meshUUID).size();
for (int i = numOfSlots; i > 0; i--) { for (int i = numOfSlots - 1; i >= 0; i--) {
if (!building.hirelings.containsValue(i)) if (!building.hirelings.contains(i))
return i; return i;
} }
return -1; return -1;
@ -103,9 +103,9 @@ public enum BuildingManager {
// for the current mesh. The position and region can change // for the current mesh. The position and region can change
// such as when upgrading a TOL to R8. // such as when upgrading a TOL to R8.
for (AbstractCharacter hireling : building.hirelings.keySet()) { for (AbstractCharacter hireling : building.hirelings) {
hireling.setLoc(getSlotLocation(building, building.hirelings.get(hireling)).getLocation()); hireling.setLoc(getSlotLocation(building, building.hirelings.indexOf(hireling) + 1).getLocation());
InterestManager.setObjectDirty(hireling); InterestManager.setObjectDirty(hireling);
} }
@ -314,7 +314,7 @@ public enum BuildingManager {
else else
newGuild = building.getOwner().getGuild(); newGuild = building.getOwner().getGuild();
for (AbstractCharacter hireling : building.hirelings.keySet()) { for (AbstractCharacter hireling : building.hirelings) {
hireling.setGuild(newGuild); hireling.setGuild(newGuild);
WorldGrid.updateObject(hireling); WorldGrid.updateObject(hireling);
} }
@ -399,7 +399,7 @@ public enum BuildingManager {
if (building.getRank() < 1) { if (building.getRank() < 1) {
for (AbstractCharacter slottedNPC : building.hirelings.keySet()) for (AbstractCharacter slottedNPC : building.hirelings)
BuildingManager.removeHireling(building, slottedNPC); BuildingManager.removeHireling(building, slottedNPC);
return; return;
@ -407,9 +407,9 @@ public enum BuildingManager {
// Delete hireling if building has deranked. // Delete hireling if building has deranked.
for (AbstractCharacter hireling : building.hirelings.keySet()) { for (AbstractCharacter hireling : building.hirelings) {
if (building.hirelings.get(hireling) > building.getBlueprint().getSlotsForRank(building.getRank())) if (building.hirelings.indexOf(hireling) + 1 > building.getBlueprint().getSlotsForRank(building.getRank()))
BuildingManager.removeHireling(building, hireling); BuildingManager.removeHireling(building, hireling);
} }

2
src/engine/gameManager/NPCManager.java

@ -197,7 +197,7 @@ public enum NPCManager {
// Pets are regular mobiles not hirelings (Siege engines) // Pets are regular mobiles not hirelings (Siege engines)
if (abstractCharacter.contract != null) if (abstractCharacter.contract != null)
abstractCharacter.building.hirelings.put(abstractCharacter, buildingSlot); abstractCharacter.building.hirelings.addIfAbsent(abstractCharacter);
// Override bind and location for this npc derived // Override bind and location for this npc derived
// from BuildingManager slot location data. // from BuildingManager slot location data.

10
src/engine/net/client/handlers/OrderNPCMsgHandler.java

@ -54,7 +54,7 @@ public class OrderNPCMsgHandler extends AbstractClientMsgHandler {
return; return;
} }
if (!building.hirelings.containsKey(hireling)) if (!building.hirelings.contains(hireling))
return; return;
BuildingManager.removeHireling(building, hireling); BuildingManager.removeHireling(building, hireling);
@ -402,7 +402,7 @@ public class OrderNPCMsgHandler extends AbstractClientMsgHandler {
if (building == null) if (building == null)
return true; return true;
if (building.hirelings.containsKey(npc) == false) if (building.hirelings.contains(npc) == false)
return true; return true;
@ -421,7 +421,7 @@ public class OrderNPCMsgHandler extends AbstractClientMsgHandler {
if (ManageCityAssetMsgHandler.playerCanManageNotFriends(player, building) == false) if (ManageCityAssetMsgHandler.playerCanManageNotFriends(player, building) == false)
return true; return true;
if (building.hirelings.containsKey(npc) == false) if (building.hirelings.contains(npc) == false)
return true; return true;
if (npc.remove() == false) { if (npc.remove() == false) {
@ -515,7 +515,7 @@ public class OrderNPCMsgHandler extends AbstractClientMsgHandler {
if (building == null) if (building == null)
return true; return true;
if (!building.hirelings.containsKey(mob)) if (!building.hirelings.contains(mob))
return true; return true;
if (player.getCharItemManager().getTradingWith() != null) { if (player.getCharItemManager().getTradingWith() != null) {
@ -531,7 +531,7 @@ public class OrderNPCMsgHandler extends AbstractClientMsgHandler {
if (BuildingManager.playerCanManage(player, building) == false) if (BuildingManager.playerCanManage(player, building) == false)
return true; return true;
if (building.hirelings.containsKey(mob) == false) if (building.hirelings.contains(mob) == false)
return true; return true;
BuildingManager.removeHireling(building, mob); BuildingManager.removeHireling(building, mob);

13
src/engine/net/client/msg/ManageCityAssetsMsg.java

@ -24,7 +24,6 @@ import org.joda.time.Period;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* Open manage city asset window * Open manage city asset window
@ -230,7 +229,7 @@ public class ManageCityAssetsMsg extends ClientNetMsg {
writer.putInt(0); writer.putInt(0);
writer.putInt(0); writer.putInt(0);
writer.putInt(asset.hirelings.size()); writer.putInt(asset.hirelings.size());
for (AbstractCharacter hireling : asset.hirelings.keySet()) { for (AbstractCharacter hireling : asset.hirelings) {
if (!hireling.getObjectType().equals(GameObjectType.NPC)) if (!hireling.getObjectType().equals(GameObjectType.NPC))
writer.putString(hireling.getName()); writer.putString(hireling.getName());
else { else {
@ -264,7 +263,7 @@ public class ManageCityAssetsMsg extends ClientNetMsg {
writer.putInt(asset.hirelings.size()); writer.putInt(asset.hirelings.size());
for (AbstractCharacter npcHire : asset.hirelings.keySet()) { for (AbstractCharacter npcHire : asset.hirelings) {
writer.putInt(npcHire.getObjectType().ordinal()); writer.putInt(npcHire.getObjectType().ordinal());
writer.putInt(npcHire.getObjectUUID()); writer.putInt(npcHire.getObjectUUID());
if (npcHire.getObjectType() == GameObjectType.NPC) if (npcHire.getObjectType() == GameObjectType.NPC)
@ -668,11 +667,11 @@ public class ManageCityAssetsMsg extends ClientNetMsg {
writer.putInt(1); writer.putInt(1);
} }
writer.putInt(building.hirelings.size());
ConcurrentHashMap<AbstractCharacter, Integer> npcList = building.hirelings; if (building.hirelings.size() > 0) {
writer.putInt(npcList.size());
if (npcList.size() > 0) { for (AbstractCharacter npcHire : building.hirelings) {
for (AbstractCharacter npcHire : npcList.keySet()) {
writer.putInt(npcHire.getObjectType().ordinal()); writer.putInt(npcHire.getObjectType().ordinal());
if (npcHire.getObjectType() == GameObjectType.Mob) if (npcHire.getObjectType() == GameObjectType.Mob)
writer.putInt(((Mob) npcHire).getDBID()); writer.putInt(((Mob) npcHire).getDBID());

6
src/engine/objects/Building.java

@ -41,6 +41,7 @@ import java.time.ZoneId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
@ -51,7 +52,7 @@ public class Building extends AbstractWorldObject {
// Used for thread safety // Used for thread safety
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
public ConcurrentHashMap<AbstractCharacter, Integer> hirelings = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); public CopyOnWriteArrayList<AbstractCharacter> hirelings = new CopyOnWriteArrayList<>();
private final HashMap<Integer, DoorCloseJob> doorJobs = new HashMap<>(); private final HashMap<Integer, DoorCloseJob> doorJobs = new HashMap<>();
public int meshUUID; public int meshUUID;
public Zone parentZone; public Zone parentZone;
@ -647,12 +648,11 @@ public class Building extends AbstractWorldObject {
// Add costs associated with hirelings // Add costs associated with hirelings
for (AbstractCharacter npc : this.hirelings.keySet()) { for (AbstractCharacter npc : this.hirelings) {
if (npc.getObjectType() != GameObjectType.NPC) if (npc.getObjectType() != GameObjectType.NPC)
continue; continue;
maintCost += Blueprint.getNpcMaintCost(npc.getRank()); maintCost += Blueprint.getNpcMaintCost(npc.getRank());
} }

2
src/engine/objects/City.java

@ -656,7 +656,7 @@ public class City extends AbstractWorldObject {
if (this.getTOL() == null) if (this.getTOL() == null)
return outNPC; return outNPC;
for (AbstractCharacter npc : getTOL().hirelings.keySet()) { for (AbstractCharacter npc : getTOL().hirelings) {
if (npc.getObjectType() == GameObjectType.NPC) if (npc.getObjectType() == GameObjectType.NPC)
if (((NPC) npc).getContract().isRuneMaster() == true) if (((NPC) npc).getContract().isRuneMaster() == true)
outNPC = (NPC) npc; outNPC = (NPC) npc;

2
src/engine/objects/Mine.java

@ -550,7 +550,7 @@ public class Mine extends AbstractGameObject {
Building mineBuilding = BuildingManager.getBuilding(this.buildingID); Building mineBuilding = BuildingManager.getBuilding(this.buildingID);
if (mineBuilding == null) if (mineBuilding == null)
return this.production.baseProduction; return this.production.baseProduction;
for (AbstractCharacter harvester : mineBuilding.hirelings.keySet()) { for (AbstractCharacter harvester : mineBuilding.hirelings) {
totalModded += baseModValue; totalModded += baseModValue;
totalModded += rankModValue * harvester.getRank(); totalModded += rankModValue * harvester.getRank();
} }

Loading…
Cancel
Save