You are on page 1of 10

Note: the cannon will attack npcs that are not unattackable.

meaning it will at tack shop npcs and crap. Also, you will have to change the imports and package name. You can release this on any other site as long as I get full credits for this an d you get none unless you alter the code, which gives you a small percentage. Name this DwarfCannon.java [code=java] package server.entity.pking.content.cannon; import import import import import import import import import server.GameEngine; server.Server; server.connections.util.Misc; server.entity.player.main.Client; server.entity.world.npc.NPC; server.entity.world.npc.NPCHandler; server.map.objects.Objects; server.util.event.cycle.CycleEvent; server.util.event.cycle.CycleEventContainer;

/** * * * * @author Rodrigo Molina */ public class DwarfCannon { private Client player; private State s = State.SOUTH_EAST; public DwarfCannon(Client $) { player = $; } public void fillCannon() { if(player.cannon != null) { if(player.getItems().playerHasItem(2)) { if(player.getItems().getItemCount(2) > 30) { player.cBalls = 30; player.getItems().deleteItem(2, 30); } else { player.cBalls = player.getItems().getItemCount(2); player.getItems().deleteItem(2, player.getItems().getIte mCount(2)); } startCannon(); } else { player.sendMessage("You need ammo to shoot this cannon!"); } } else { player.sendMessage("This is not your cannon!"); } } int j = 0;

public void destruct() { j = 0; if(player.cannon != null) { if(!player.cannon.getOwnerName().equalsIgnoreCase(player.playerN ame)) { player.sendMessage("This isn't your cannon!"); return; } } if(player.cannon == null) { player.sendMessage("This isn't your cannon!"); return; } Objects replace = new Objects(-1, player.cannon.getObjectX(), player .cannon.getObjectY(), 0, 1, 10, 0); Server.objectHandler.addObject(replace); Server.objectHandler.placeObject(replace); CannonCoords.clear(); player.getItems().addItem(12, 1); player.getItems().addItem(10, 1); player.getItems().addItem(8, 1); player.getItems().addItem(6, 1); player.cannon = null; if(player.cBalls >= 1) { player.getItems().addItem(2, player.cBalls); } player.cBalls = 0; b = true; } boolean b; public void buildCannon() { if(player.isMakingCannon) { player.sendMessage("You can't make another cannon??"); } else { if( player.getItems().playerHasItem(6) & player.getItems().playerHasItem(10) & player.getItems().playerHasItem(12) ) { if(player.cannon != null) { if(!player.cannon.getOwnerName().equalsIgnoreCase(player .playerName)) { player.sendMessage("You already have a cannon se t!"); return; } } player.isMakingCannon = true; player.getPA().walkTo(-1, 0); player.turnPlayerTo(player.absX + 2, player.absY + 2); GameEngine.getEngine().submit(player, new CycleEvent() { @Override public void execute(CycleEventContainer container) { j++; if(j == 1) {

player.startAnimation(827); Objects base = new Objects(7, player.absX + 1, p layer.absY, 0, 1, 10, 0, player.playerName); Server.objectHandler.addObject(base); Server.objectHandler.placeObject(base); player.getItems().deleteItem2(6, 1); } else if (j == 2) { player.startAnimation(827); Objects base = new Objects(8, player.absX + 1, p layer.absY, 0, 1, 10, 0, player.playerName); Server.objectHandler.addObject(base); Server.objectHandler.placeObject(base); player.getItems().deleteItem2(8, 1); } else if (j == 3) { player.startAnimation(827); Objects base = new Objects(9, player.absX + 1, p layer.absY, 0, 1, 10, 0, player.playerName); Server.objectHandler.addObject(base); Server.objectHandler.placeObject(base); player.getItems().deleteItem2(10, 1); } else if( j > 3) { final int x = player.absX + 1; final int y = player.absY; player.startAnimation(827); player.getPA().object(6,x, y, 0, 1, 10); player.cannon = new Objects(6, x, y, 0, 1, 10, 0 , player.playerName); Server.objectHandler.addObject(player.cannon); Server.objectHandler.placeObject(player.cannon); player.getItems().deleteItem2(12, 1); j = 0; container.stop(); } } @Override public void stop() { player.isMakingCannon = false; } }, 5); } else { player.sendMessage("You don't have all the items neccessary to make the cannon!"); } } } private boolean shooting = false; public void startCannon() { GameEngine.getEngine().submit(player, new CycleEvent() { @Override public void execute(CycleEventContainer container) { if(player.cBalls > 0) { for(int j = 0; j < NPCHandler.npcs.length; j++) { if(NPCHandler.npcs[j] != null) { if(!NPCHandler.npcs[j].isDead

& player.goodDistance(NPCHan dler.npcs[j].absX, NPCHandler.npcs[j].absY, player.cannon.getObjectX(), player.c annon.getObjectY(), 7) ) { if(checkFire(player.cann on, NPCHandler.npcs[j])) { if(!player.inMulti() & !NPCHandler.npcs[j].inMulti()) { fire(player.cann on, NPCHandler.npcs[j]); Server.npcHandle r.attackPlayer(player, j); break; } if(player.underAttac kBy > 0) { fire(player.cann on, NPCHandler.npcs[j]); break; } fire(player.cann on, NPCHandler.npcs[j]); Server.npcHandle r.attackPlayer(player, j); } } } } rotate(player.cannon); } else if(!b) { player.sendMessage("Your cannon has ran out of b alls!"); container.stop(); } } @Override public void stop() { b = false; shooting = false; s = State.SOUTH_EAST; } }, shooting ? Misc.random(6) : 2); } public boolean checkFire(Objects cannon, NPC npc) { int obX = cannon.getObjectX(); int obY = cannon.getObjectY(); int npcX = npc.absX; int npcY = npc.absY; if (!npc.isDead & npc.heightLevel == player.heightLevel) { CannonCoords.fillUp(obX, obY); switch (s) { case WEST: case SOUTH:

case case case case case case s, npcX, npcY); } } return false; }

NORTH: EAST: NORTH_EAST: SOUTH_EAST: SOUTH_WEST: NORTH_WEST: return CannonCoords.checkCoords(

public void fire(Objects cannon, NPC npc) { shooting = true; int oX = cannon.objectX; int oY = cannon.objectY; int offX = ((++oX - npc.getX()) * -1); int offY = ((++oY - npc.getY()) * -1); player.getPA().createPlayersProjectile(oX, oY, offY, offX, 50, 6 0, 53, 20, 20, - player.oldNpcIndex + 1, 30); int damage = Misc.random(30); npc.handleHitMask(damage); npc.HP -= damage; npc.hitUpdateRequired = true; npc.killerId = player.playerId; npc.facePlayer(player.playerId); player.cBalls--; } public void rotate(Objects cannon) { if(cannon == null) return; int x = cannon.getObjectX(); int y = cannon.getObjectY(); switch(s) { case NORTH: player.getPA().objectAnim(x, s = State.NORTH_EAST; break; case NORTH_EAST: player.getPA().objectAnim(x, s = State.EAST; break; case EAST: player.getPA().objectAnim(x, s = State.SOUTH_EAST; break; case SOUTH_EAST: player.getPA().objectAnim(x, s = State.SOUTH; break; case SOUTH: player.getPA().objectAnim(x, s = State.SOUTH_WEST; break; case SOUTH_WEST: player.getPA().objectAnim(x, s = State.WEST; break; case WEST:

y, s.getId(), 10, -1);

y, s.getId(), 10, -1);

y, s.getId(), 10, -1);

y, s.getId(), 10, -1);

y, s.getId(), 10, -1);

y, s.getId(), 10, -1);

player.getPA().objectAnim(x, y, s.getId(), 10, -1); s = State.NORTH_WEST; break; case NORTH_WEST: player.getPA().objectAnim(x, y, s.getId(), 10, -1); s = State.NORTH; break; } } } [/code]

CannonCoords [code=java]package server.entity.pking.content.cannon; import import import import java.util.HashMap; java.util.LinkedList; java.util.List; java.util.Map;

/** * For this to work and to be able to find all the npcs around within a 12 tile distance, and * find the npcs when the cannon is facing their way, we are going to preload al l the coords * into this map to be grabbed later on to check if any npcs are around. * * * @author Rodrigo Molina */ public class CannonCoords { private static Map<State, List<Integer>> coords = new HashMap<State, Lis t<Integer>>(); private static final int MAX_DISTANCE = 12; static { coords.put(State.NORTH, new LinkedList<Integer>()); coords.put(State.NORTH_WEST, new LinkedList<Integer>()); coords.put(State.NORTH_EAST, new LinkedList<Integer>()); coords.put(State.SOUTH, new LinkedList<Integer>()); coords.put(State.SOUTH_EAST, new LinkedList<Integer>()); coords.put(State.SOUTH_WEST, new LinkedList<Integer>()); coords.put(State.EAST, new LinkedList<Integer>()); coords.put(State.WEST, new LinkedList<Integer>()); } /** * Checks if the coords inside the map equals to the npc x and y. * @param s * @param obX * @param obY * @return */ public static boolean checkCoords(State s, int npcX, int npcY) {

return coords.get(s).contains(npcX) | coords.get(s).contains(npc Y); } /** * Fills up the map with the specified coords around the cannon. * @param obX * @param obY */ public static void fillUp(int obX, int obY) { if(!coords.get(State.NORTH).isEmpty()) return; addCoords(obX, obY); } public static void addCoords(final int ob1, final int ob2) { int obX = ob1; int obY = ob2; for(int j = obX; j <= obX + MAX_DISTANCE; j--) { if(j < obX - MAX_DISTANCE) break; coords.get(State.WEST).add(j); coords.get(State.SOUTH_WEST).add(j); } for(int j = obY; j <= obY + MAX_DISTANCE; j--) { if(j < obY - MAX_DISTANCE) break; coords.get(State.SOUTH).add(j); coords.get(State.SOUTH_EAST).add(j); } for(int j = obX; j <= obX + MAX_DISTANCE; j++) { if(j < obX - MAX_DISTANCE) break; coords.get(State.EAST).add(j); coords.get(State.NORTH_EAST).add(j); } for(int j = obY; j <= obY + MAX_DISTANCE; j++) { if(j < obY - MAX_DISTANCE) break; coords.get(State.NORTH).add(j); coords.get(State.NORTH_WEST).add(j); } } public static void clear() { coords.clear(); } } [/code] State.java [code] package server.entity.pking.content.cannon; import java.util.HashMap; import java.util.Map; public enum State { NORTH(516),

NORTH_EAST(517), EAST(518), SOUTH_EAST(519), SOUTH(520), SOUTH_WEST(521), WEST(514), NORTH_WEST(515); int id = 0; State(int id) { this.id = id; } private static Map<Integer, State> states = new HashMap<Integer, State>(); static { for(State s : states.values()) State.states.put(s.getId(), s); } int getId() { return id; } } [/code] ClickObject.java orActionHandler.java FirstClick [code=java] case 6: client.getDwarf().fillCannon(); break; [/code] SecondClick [code=java] case 6: client.getDwarf().destruct(); break; [/code] Now in Client.java yo ucan add this to logout() method or destruct() method. [code=java] getDwarf().destruct(); [/code] Client.java [code=java] private final DwarfCannon dw = new DwarfCannon(this); public DwarfCannon getDwarf() { return dw; } public boolean isMakingCannon; public Objects cannon = null; public int cBalls; [/code]

Walking.java [code=java] if(c.isMakingCannon) { return; } [/code] Also replace your Objects.java with this: [code=java] package server.map.objects; public class Objects { public public public public public public public public int objectId; int objectX; int objectY; int objectHeight; int objectFace; int objectType; int objectTicks; String owner;

public Objects(final int id, final int x, final int y, final int height, final int face, final int type, final int ticks, String owner) { objectId = id; objectX = x; objectY = y; objectHeight = height; objectFace = face; objectType = type; objectTicks = ticks; this.owner = owner; } public Objects(final int id, final int x, final int y, final int height, final int face, final int type, final int ticks) { objectId = id; objectX = x; objectY = y; objectHeight = height; objectFace = face; objectType = type; objectTicks = ticks; } public String getOwnerName() { return owner; } public int getObjectId() { return objectId; } public int getObjectX() { return objectX; }

public int getObjectY() { return objectY; } public int getObjectHeight() { return objectHeight; } public int getObjectFace() { return objectFace; } public int getObjectType() { return objectType; } } [/code]