Files
Server/src/engine/net/client/handlers/MinionTrainingMsgHandler.java
T

332 lines
13 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
package engine.net.client.handlers;
import engine.Enum;
import engine.Enum.DispatchChannel;
import engine.InterestManagement.WorldGrid;
import engine.exception.MsgSendException;
import engine.gameManager.BuildingManager;
import engine.gameManager.DbManager;
import engine.gameManager.NPCManager;
import engine.gameManager.SessionManager;
2023-05-27 20:53:52 -05:00
import engine.math.Vector3fImmutable;
2022-04-30 09:41:17 -04:00
import engine.net.Dispatch;
import engine.net.DispatchMessage;
import engine.net.client.ClientConnection;
import engine.net.client.msg.*;
import engine.objects.*;
import org.pmw.tinylog.Logger;
import java.util.ArrayList;
import java.util.HashMap;
/*
* @Author:
* @Summary: Processes application protocol message which
* processes training of minions in guard barracks
*/
public class MinionTrainingMsgHandler extends AbstractClientMsgHandler {
2023-07-15 09:23:48 -04:00
public static HashMap<Integer, ArrayList<Integer>> _minionsByCaptain = null;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
public MinionTrainingMsgHandler() {
super(MinionTrainingMessage.class);
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
@Override
protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) throws MsgSendException {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
MinionTrainingMessage minionMsg = (MinionTrainingMessage) baseMsg;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
PlayerCharacter player = SessionManager.getPlayerCharacter(origin);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (player == null)
return true;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (minionMsg.getNpcType() == Enum.GameObjectType.NPC.ordinal()) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
NPC npc = NPC.getFromCache(minionMsg.getNpcID());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (npc == null)
return true;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
Building b = BuildingManager.getBuildingFromCache(minionMsg.getBuildingID());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (b == null)
return true;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//clear minion
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (npc.minionLock.writeLock().tryLock()) {
try {
if (minionMsg.getType() == 2) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
Mob toRemove = Mob.getFromCache(minionMsg.getUUID());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (!npc.getSiegeMinionMap().containsKey(toRemove))
return true;
2022-04-30 09:41:17 -04:00
npc.getSiegeMinionMap().remove(toRemove);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
WorldGrid.RemoveWorldObject(toRemove);
if (toRemove.getParentZone() != null)
toRemove.getParentZone().zoneMobSet.remove(toRemove);
DbManager.removeFromCache(toRemove);
PlayerCharacter petOwner = toRemove.getOwner();
if (petOwner != null) {
petOwner.setPet(null);
toRemove.setOwner(null);
PetMsg petMsg = new PetMsg(5, null);
Dispatch dispatch = Dispatch.borrow(petOwner, petMsg);
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
}
// we Found the move to remove, lets break the for loop so it doesnt look for more.
ManageCityAssetsMsg mca1 = new ManageCityAssetsMsg(player, b);
mca1.actionType = 3;
mca1.setTargetType(b.getObjectType().ordinal());
mca1.setTargetID(b.getObjectUUID());
mca1.setTargetType3(npc.getObjectType().ordinal());
mca1.setTargetID3(npc.getObjectUUID());
mca1.setAssetName1(b.getName());
mca1.setUnknown54(1);
Dispatch dispatch = Dispatch.borrow(player, mca1);
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
ManageNPCMsg mnm = new ManageNPCMsg(npc);
dispatch = Dispatch.borrow(player, mnm);
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
//Add Minion
} else {
Zone zone = npc.getParentZone();
if (zone == null)
return true;
int maxSlots = 3;
if (npc.getContractID() == 842)
maxSlots = 1;
if (npc.getSiegeMinionMap().size() == maxSlots)
return true;
int mobBase;
switch (minionMsg.getMinion()) {
case 9:
mobBase = 13171;
break;
case 2:
mobBase = 13758;
break;
case 3:
mobBase = 13757;
break;
case 4:
mobBase = 2111;
break;
case 5:
mobBase = 12402;
break;
case 6:
mobBase = 2113;
break;
default:
mobBase = minionMsg.getMinion();
}
if (mobBase == 0)
return true;
2023-08-18 10:43:59 -04:00
Mob siegeMob = Mob.createSiegeMob(npc, mobBase, npc.getGuild(), zone, b.getLoc(), (short) 1);
2023-07-15 09:23:48 -04:00
2023-08-18 10:43:59 -04:00
if (siegeMob == null)
2023-07-15 09:23:48 -04:00
return true;
2023-08-18 10:43:59 -04:00
if (siegeMob != null) {
siegeMob.setSpawnTime(60 * 15);
2023-07-15 09:23:48 -04:00
Building building = BuildingManager.getBuilding(((MinionTrainingMessage) baseMsg).getBuildingID());
2023-08-18 10:43:59 -04:00
siegeMob.building = building;
siegeMob.parentZone = zone;
2023-08-18 11:06:48 -04:00
// Slot siege minion
2023-08-18 12:32:31 -04:00
// Can be either corner tower or bulwark.
int slot;
2023-08-18 13:08:05 -04:00
if (building.getBlueprint().getBuildingGroup().equals(Enum.BuildingGroup.ARTYTOWER))
2023-08-18 13:01:58 -04:00
slot = 2;
2023-08-18 12:32:31 -04:00
else
2023-08-18 13:01:58 -04:00
slot = ((NPC) siegeMob.npcOwner).getSiegeMinionMap().get(siegeMob) + 1; // First slot is for the captain
2023-08-18 11:06:48 -04:00
2023-08-18 10:58:06 -04:00
BuildingLocation slotLocation = BuildingManager._slotLocations.get(building.meshUUID).get(slot);
siegeMob.bindLoc = building.getLoc().add(slotLocation.getLocation());
2023-08-18 10:43:59 -04:00
// Rotate slot position by the building rotation
2023-08-18 10:58:06 -04:00
siegeMob.bindLoc = Vector3fImmutable.rotateAroundPoint(building.getLoc(), siegeMob.bindLoc, building.getBounds().getQuaternion().angleY);
2023-08-18 10:43:59 -04:00
2023-08-18 10:58:06 -04:00
siegeMob.loc = new Vector3fImmutable(siegeMob.bindLoc);
siegeMob.endLoc = new Vector3fImmutable(siegeMob.bindLoc);
2023-08-18 10:43:59 -04:00
zone.zoneMobSet.add(siegeMob);
2023-08-18 13:01:58 -04:00
siegeMob.setLoc(siegeMob.bindLoc);
}
2023-07-15 09:23:48 -04:00
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
ManageNPCMsg mnm = new ManageNPCMsg(npc);
mnm.setMessageType(1);
Dispatch dispatch = Dispatch.borrow(player, mnm);
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
} finally {
npc.minionLock.writeLock().unlock();
}
}
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
} else if (minionMsg.getNpcType() == Enum.GameObjectType.Mob.ordinal()) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
Mob npc = Mob.getFromCache(minionMsg.getNpcID());
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (npc == null)
return true;
2022-04-30 09:41:17 -04:00
2023-08-18 11:33:27 -04:00
Building building = BuildingManager.getBuildingFromCache(minionMsg.getBuildingID());
2022-04-30 09:41:17 -04:00
2023-08-18 11:33:27 -04:00
if (building == null)
2023-07-15 09:23:48 -04:00
return true;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
//clear minion
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (npc.minionLock.writeLock().tryLock()) {
try {
if (minionMsg.getType() == 2) {
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
Mob toRemove = Mob.getFromCache(minionMsg.getUUID());
2023-08-18 11:33:27 -04:00
2023-07-15 09:23:48 -04:00
if (!npc.getSiegeMinionMap().containsKey(toRemove))
return true;
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
if (!DbManager.MobQueries.REMOVE_FROM_GUARDS(npc.getObjectUUID(), toRemove.getMobBaseID(), npc.getSiegeMinionMap().get(toRemove)))
return true;
2022-04-30 09:41:17 -04:00
npc.getSiegeMinionMap().remove(toRemove);
2022-04-30 09:41:17 -04:00
2023-07-15 09:23:48 -04:00
WorldGrid.RemoveWorldObject(toRemove);
if (toRemove.getParentZone() != null)
toRemove.getParentZone().zoneMobSet.remove(toRemove);
DbManager.removeFromCache(toRemove);
PlayerCharacter petOwner = toRemove.getOwner();
if (petOwner != null) {
petOwner.setPet(null);
toRemove.setOwner(null);
PetMsg petMsg = new PetMsg(5, null);
Dispatch dispatch = Dispatch.borrow(petOwner, petMsg);
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
}
2023-07-15 09:23:48 -04:00
2023-08-18 11:33:27 -04:00
ManageCityAssetsMsg mca1 = new ManageCityAssetsMsg(player, building);
2023-07-15 09:23:48 -04:00
mca1.actionType = 3;
2023-08-18 11:33:27 -04:00
mca1.setTargetType(building.getObjectType().ordinal());
mca1.setTargetID(building.getObjectUUID());
2023-07-15 09:23:48 -04:00
mca1.setTargetType3(npc.getObjectType().ordinal());
mca1.setTargetID3(npc.getObjectUUID());
2023-08-18 11:33:27 -04:00
mca1.setAssetName1(building.getName());
2023-07-15 09:23:48 -04:00
mca1.setUnknown54(1);
Dispatch dispatch = Dispatch.borrow(player, mca1);
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
ManageNPCMsg mnm = new ManageNPCMsg(npc);
dispatch = Dispatch.borrow(player, mnm);
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
//Add Minion
} else {
Zone zone = npc.getParentZone();
if (zone == null)
return true;
int maxSlots = 5;
if (npc.getContract().getContractID() == 842)//artillery captain
maxSlots = 1;
if (npc.getContract().getContractID() == 910)//guard dogs
maxSlots = 0;
switch (npc.getRank()) {
case 1:
case 2:
maxSlots = 1;
break;
case 3:
maxSlots = 2;
break;
case 4:
case 5:
maxSlots = 3;
break;
case 6:
maxSlots = 4;
break;
case 7:
maxSlots = 5;
break;
}
if (npc.getSiegeMinionMap().size() == maxSlots)
return true;
int mobBase = npc.getMobBaseID();
if (mobBase == 0)
return true;
String pirateName = NPCManager.getPirateName(mobBase);
if (!DbManager.MobQueries.ADD_TO_GUARDS(npc.getObjectUUID(), mobBase, pirateName, npc.getSiegeMinionMap().size() + 1))
return true;
2023-08-18 11:33:27 -04:00
Mob toCreate = Mob.createGuardMob(npc, npc.getGuild(), zone, building.getLoc(), npc.getLevel(), pirateName);
2023-07-15 09:23:48 -04:00
if (toCreate == null)
return true;
if (toCreate != null) {
toCreate.setDeathTime(System.currentTimeMillis());
toCreate.parentZone.zoneMobSet.add(toCreate);
}
}
ManageNPCMsg mnm = new ManageNPCMsg(npc);
mnm.setMessageType(1);
Dispatch dispatch = Dispatch.borrow(player, mnm);
DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
} catch (Exception e) {
Logger.error(e);
} finally {
npc.minionLock.writeLock().unlock();
}
}
}
return true;
}
2022-04-30 09:41:17 -04:00
}