From 0edf9610dff7d9847a29acfb6778d8e216f2aacb Mon Sep 17 00:00:00 2001 From: Treeku Date: Tue, 3 Dec 2013 16:43:01 +0000 Subject: [PATCH] Start of FactionStanding, Custom Object Scripts --- src/main/NGECore.java | 6 +- src/resources/common/Opcodes.java | 1 + .../objects/player/PlayerObject.java | 28 ++- src/services/gcw/FactionService.java | 171 ++++++++++++++++++ src/services/object/ObjectService.java | 57 +++++- 5 files changed, 258 insertions(+), 5 deletions(-) create mode 100644 src/services/gcw/FactionService.java diff --git a/src/main/NGECore.java b/src/main/NGECore.java index 3563a6ec..2c3606ae 100644 --- a/src/main/NGECore.java +++ b/src/main/NGECore.java @@ -51,6 +51,7 @@ import services.collections.CollectionService; import services.combat.CombatService; import services.command.CombatCommand; import services.command.CommandService; +import services.gcw.FactionService; import services.gcw.GCWService; import services.guild.GuildService; import services.map.MapService; @@ -115,6 +116,7 @@ public class NGECore { public AttributeService attributeService; public SUIService suiService; public GuildService guildService; + public FactionService factionService; public GCWService gcwService; public TradeService tradeService; public CombatService combatService; @@ -258,6 +260,9 @@ public class NGECore { guildService = new GuildService(this); zoneDispatch.addService(guildService); + factionService = new FactionService(this); + zoneDispatch.addService(factionService); + gcwService = new GCWService(this); zoneDispatch.addService(gcwService); @@ -274,7 +279,6 @@ public class NGECore { didServerCrash = false; System.out.println("Started Server."); setGalaxyStatus(2); - } diff --git a/src/resources/common/Opcodes.java b/src/resources/common/Opcodes.java index 670abfde..7b9b948f 100644 --- a/src/resources/common/Opcodes.java +++ b/src/resources/common/Opcodes.java @@ -55,5 +55,6 @@ public class Opcodes { public static int ClientMfdStatusUpdateMessage = CRC.StringtoCRC("ClientMdfStatusUpdateMessage"); public static int PlayMusicMessage = CRC.StringtoCRC("PlayMusicMessage"); public static int PlanetTravelPointListRequest = 0x96405D4D; + public static int FactionRequestMessage = CRC.StringtoCRC("FactionRequestMessage"); } diff --git a/src/resources/objects/player/PlayerObject.java b/src/resources/objects/player/PlayerObject.java index 25dd4e64..a5f10e4f 100644 --- a/src/resources/objects/player/PlayerObject.java +++ b/src/resources/objects/player/PlayerObject.java @@ -26,6 +26,7 @@ import java.util.BitSet; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.TreeMap; import resources.objects.waypoint.WaypointObject; @@ -119,6 +120,8 @@ public class PlayerObject extends SWGObject { @NotPersistent private long lastPlayTimeUpdate = System.currentTimeMillis(); + private Map factionStandingMap = new TreeMap(); + public PlayerObject() { super(); messageBuilder = new PlayerMessageBuilder(this); @@ -537,7 +540,30 @@ public class PlayerObject extends SWGObject { this.jediState = jediState; } } - + + public Map getFactionStandingMap() { + return factionStandingMap; + } + + public void setFactionStanding(String faction, int factionStanding) { + synchronized(objectMutex) { + factionStandingMap.put(faction, ((factionStanding < -5000) ? -5000 : ((factionStanding > 5000) ? 5000 : factionStanding))); + } + } + + public void modifyFactionStanding(String faction, int modifier) { + synchronized(objectMutex) { + int factionStanding = (((factionStandingMap.containsKey(faction)) ? factionStandingMap.get(faction) : 0) + modifier); + factionStandingMap.put(faction, ((factionStanding < -5000) ? -5000 : ((factionStanding > 5000) ? 5000 : factionStanding))); + } + } + + public int getFactionStanding(String faction) { + synchronized(objectMutex) { + return ((factionStandingMap.containsKey(faction)) ? factionStandingMap.get(faction) : 0); + } + } + @Override public void sendBaselines(Client destination) { diff --git a/src/services/gcw/FactionService.java b/src/services/gcw/FactionService.java new file mode 100644 index 00000000..5792f2cd --- /dev/null +++ b/src/services/gcw/FactionService.java @@ -0,0 +1,171 @@ +/******************************************************************************* + * Copyright (c) 2013 + * + * This File is part of NGECore2. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * Using NGEngine to work with NGECore2 is making a combined work based on NGEngine. + * Therefore all terms and conditions of the GNU Lesser General Public License cover the combination. + ******************************************************************************/ +package services.gcw; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +import main.NGECore; + +import org.apache.mina.core.buffer.IoBuffer; +import org.apache.mina.core.session.IoSession; +import org.python.core.Py; +import org.python.core.PyObject; + +import resources.common.FileUtilities; +import resources.common.Opcodes; +import resources.objects.creature.CreatureObject; +import resources.objects.player.PlayerObject; +import resources.objects.tangible.TangibleObject; + +import engine.clientdata.ClientFileManager; +import engine.clientdata.visitors.DatatableVisitor; +import engine.resources.common.CRC; +import engine.resources.objects.SWGObject; +import engine.resources.service.INetworkDispatch; +import engine.resources.service.INetworkRemoteEvent; + +public class FactionService implements INetworkDispatch { + + private NGECore core; + + private Map factionMap = new TreeMap(); + + public FactionService(NGECore core) { + DatatableVisitor factionTable; + + this.core = core; + + try { + factionTable = ClientFileManager.loadFile("strings/en/faction/faction_names.stf", DatatableVisitor.class); + + for (int s = 0; s < factionTable.getRowCount(); s++) { + if (factionTable.getObject(s, 0) != null) { + String faction = ((String) factionTable.getObject(s, 1)); + factionMap.put(faction, CRC.StringtoCRC(faction)); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public int getCRC(String faction) { + return CRC.StringtoCRC(faction); + } + + public String getName(int factionCrc) { + for (Entry entry : factionMap.entrySet()) { + if (factionCrc == entry.getValue()) { + return entry.getKey(); + } + } + + return ""; + } + + public void addFactionStanding(CreatureObject creature, String faction, int factionStanding) { + if (creature == null) { + return; + } + + PlayerObject player = (PlayerObject) creature.getSlottedObject("ghost"); + + if (player == null) { + return; + } + + player.modifyFactionStanding(faction, factionStanding); + } + + public void removeFactionStanding(CreatureObject creature, String faction, int factionStanding) { + if (creature == null) { + return; + } + + PlayerObject player = (PlayerObject) creature.getSlottedObject("ghost"); + + if (player == null) { + return; + } + + player.modifyFactionStanding(faction, factionStanding); + } + + public void updateFactionStanding(SWGObject killedObject) { + if (!(killedObject instanceof TangibleObject) || killedObject == null) { + return; + } + + TangibleObject target = (TangibleObject) killedObject; + + if (!(((TangibleObject) killedObject).getKiller() instanceof TangibleObject) || ((TangibleObject) killedObject).getKiller() == null) { + return; + } + + TangibleObject killer = ((TangibleObject) killedObject).getKiller(); + + if (killer != null && target != null && isFaction(target.getFaction())) { + if (FileUtilities.doesFileExist("scripts/faction/standing/" + target.getFaction() + ".py")) { + + PyObject method = core.scriptService.getMethod("scripts/faction/standing/", target.getFaction(), "update"); + + if (method != null && method.isCallable()) { + method.__call__(Py.java2py(core), Py.java2py(killer), Py.java2py(target)); + } + } + } + } + + public boolean isFaction(String faction) { + if (factionMap.containsKey(faction)) { + return true; + } + + return false; + } + + public Map getFactionMap() { + return factionMap; + } + + @Override + public void insertOpcodes(Map swgOpcodes, Map objControllerOpcodes) { + + swgOpcodes.put(Opcodes.FactionRequestMessage, new INetworkRemoteEvent() { + + @Override + public void handlePacket(IoSession session, IoBuffer buffer) throws Exception { + + } + + }); + } + + @Override + public void shutdown() { + // TODO Auto-generated method stub + + } + +} diff --git a/src/services/object/ObjectService.java b/src/services/object/ObjectService.java index 75ff8708..9c6d0e4f 100644 --- a/src/services/object/ObjectService.java +++ b/src/services/object/ObjectService.java @@ -132,7 +132,7 @@ public class ObjectService implements INetworkDispatch { } } - public SWGObject createObject(String Template, long objectID, Planet planet, Point3D position, Quaternion orientation) { + public SWGObject createObject(String Template, long objectID, Planet planet, Point3D position, Quaternion orientation, String customServerTemplate) { SWGObject object = null; CrcStringTableVisitor crcTable; try { @@ -199,16 +199,49 @@ public class ObjectService implements INetworkDispatch { object.setPlanetId(planet.getID()); + object.setAttachment("serverTemplate", ((customServerTemplate != null) ? customServerTemplate : object.getTemplate())); + loadServerTemplate(object); objectList.put(objectID, object); - + + // Set Default Tangible Options + /* + if (Template.startsWith("object/creature/") || Template.startsWith("object/mobile/")) { + ((CreatureObject) object).setOptionsBitmask(Options.MOBILE); + + if (Template.startsWith("object/mobile/beast_master/")) { + ((CreatureObject) object).setOptionsBitmask(Options.NONE); + } else if (Template.startsWith("object/mobile/vendor/")) { + ((CreatureObject) object).setOptionsBitmask(Options.INVULNERABLE | Options.USABLE); + } else if (Template.startsWith("object/mobile/vehicle/")) { + ((CreatureObject) object).addOption(Options.INVULNERABLE | Options.MOUNT); + } else if (Template.startsWith("object/mobile/hologram/")) { + ((CreatureObject) object).addOption(Options.INVULNERABLE); + } else if (Template.startsWith("object/creature/npc/theme_park/")) { + ((CreatureObject) object).addOption(Options.INVULNERABLE); + } else if (Template.startsWith("object/creature/general/")) { + ((CreatureObject) object).setOptionsBitmask(Options.INVULNERABLE | Options.CONVERSABLE); + } else if (Template.startsWith("object/creature/droid/")) { + ((CreatureObject) object).addOption(Options.INVULNERABLE); + } + } else if (object instanceof TangibleObject) { + ((TangibleObject) object).setOptionsBitmask(Options.INVULNERABLE); + + if (Template.startsWith("object/tangible/vendor/")) { + ((CreatureObject) object).addOption(Options.USABLE); + } else if (Template.startsWith("object/creature/droid/")) { + ((CreatureObject) object).addOption(Options.INVULNERABLE); + } + } + */ + return object; } public void loadServerTemplate(SWGObject object) { - String template = object.getTemplate(); + String template = ((object.getAttachment("serverTemplate") == null) ? object.getTemplate() : ((String) object.getAttachment("serverTemplate"))); String serverTemplate = template.replace(".iff", ""); // check if template is empty(4 default lines) to reduce RAM usage(saves about 500 MB of RAM) try { @@ -239,6 +272,10 @@ public class ObjectService implements INetworkDispatch { return createObject(Template, 0, planet, new Point3D(x, y, z), new Quaternion(1, 0, 0, 0)); } + public SWGObject createObject(String Template, long objectID, Planet planet, Point3D position, Quaternion orientation) { + return createObject(Template, objectID, planet, position, orientation, null); + } + public void addObjectToScene(SWGObject object) { core.simulationService.add(object, object.getPosition().x, object.getPosition().z); @@ -296,6 +333,18 @@ public class ObjectService implements INetworkDispatch { } } + if (object instanceof CreatureObject) { + if (core.factionService.getFactionMap().containsKey(((CreatureObject) object).getFaction())) { + if (FileUtilities.doesFileExist(filePath)) { + PyObject method = core.scriptService.getMethod("scripts/" + object.getTemplate().split("shared_" , 2)[0].replace("shared_", ""), object.getTemplate().split("shared_" , 2)[1].replace(".iff", ""), "destroy"); + + if (method != null && method.isCallable()) { + method.__call__(Py.java2py(core), Py.java2py(((TangibleObject) object).getKiller()), Py.java2py(object)); + } + } + } + } + if (object == null) { return; } @@ -505,6 +554,7 @@ public class ObjectService implements INetworkDispatch { }); + /* objControllerOpcodes.put(ObjControllerOpcodes.USE_OBJECT, new INetworkRemoteEvent() { @Override @@ -529,6 +579,7 @@ public class ObjectService implements INetworkDispatch { } }); + */ }