Code:
package com.rs2.model.content.minigames.BountyHunter;
import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;
import com.rs2.model.Position;
import com.rs2.model.players.Player;
import com.rs2.model.players.item.ItemDefinition;
import com.rs2.model.players.item.ItemManager;
import com.rs2.model.tick.CycleEvent;
import com.rs2.model.tick.CycleEventContainer;
import com.rs2.model.tick.CycleEventHandler;
import com.rs2.util.Misc;
/**
* Bounty Hunter minigame.
* @author Brandyn Thomas
*/
public class BountyHunter {
/**
* Adds a player to the minigame.
* @param player - The specified player to add.
*/
public static boolean add(final Player player) {
double enterPenalty = (player.getEnterPenalty() * 0.6);
if ((int) enterPenalty > 0) {
player.getDialogue().sendStatement(
"You need to wait " + (int) a + " seconds before reentering!");
return false;
}
for (int item_index = 0; item_index < player.getInventory().getItemContainer().size(); item_index++) {
if(player.getInventory().getItemContainer().get(item_index) == null)
continue;
if (player.getInventory().getItemAmount(item_index) > 1000) {
player.getActionSender().sendMessage("You can't bring more than 1000 of " + ItemDefinition.forId(player.getInventory().getItemContainer().getSlotById(item_index)).getName().toLowerCase() + " to the crater!");
return false;
}
if (player.getInventory().getItemAmount(item_index) > 0) {
if (player.getInventory().getItemContainer().get(item_index).getDefinition().isNoted()) {
player.getActionSender().sendMessage("You can't bring noted items into the crater!");
return false;
}
}
for (int index = 0; index < UNALLOWED_ITEMS.length; index++) {
if (player.getInventory().playerHasItem(UNALLOWED_ITEMS[index], 1) || player.getEquipment().playerHasItem(UNALLOWED_ITEMS[index], 1)) {
player.getActionSender().sendMessage("You cannot bring " + ItemDefinition.forId(UNALLOWED_ITEMS[index]).getName().toLowerCase() + " into the crater!");
return false;
}
}
}
final Crater crater = Crater.getCrater(player);
for (int crater_index = 0; crater_index > Crater.values().length; crater_index++) {
Crater crater_search = Crater.values()[crater_index];
if (player.getPosition().withinDistance(new Position(CRATER_ENTRANCES[crater_search.ordinal()][0], CRATER_ENTRANCES[crater_search.ordinal()][1], 0/*crater_search.getHeightLevel()*/), 4)) {
if(!crater_search.withinLevelRange(player)) {
player.getActionSender().sendMessage("You cannot enter this crater!");
player.getActionSender().sendMessage("For this crater you must be between combat levels " + crater_search.getCraterMin() + "-" + crater_search.getCraterMax());
return false;
}
}
}
if (crater.getParticipants().size() >= 200) {
player.getActionSender().sendMessage("This crater is full! Please try again later.");
return false;
}
final int[] CRATER_CAVE = CRATER_CAVES[Misc.random(CRATER_CAVES.length - 1)];
final Position CRATER_CAVE_LOCATION = new Position(CRATER_CAVE[0], CRATER_CAVE[1], crater.getHeightLevel());
player.getUpdateFlags().sendAnimation(ENTER_ANIMATION);
CycleEventHandler.getInstance().addEvent(player, new CycleEvent() {
@Override
public void execute(CycleEventContainer container) {
player.teleport(CRATER_CAVE_LOCATION);
player.setSkullIcon(Skull.getSkull(player).getHeadIcon());
player.setAppearanceUpdateRequired(true);
player.getUpdateFlags().sendAnimation(7377);
crater.assignTarget(player);
container.stop();
}
public void stop() {
}
}, 1);
return crater.getParticipants().add(player);
}
/**
* Deletes a specified player.
* @param player - The specified player to remove.
*/
public static boolean remove(final Player player, final boolean died) {
if (player.getPenalty() > 0 && player.isPenalised() && !died) {
player.getActionSender().sendMessage("You are currently penalised and must wait " + player.getPenalty() + " seconds to exit!");
return false;
}
if(!died) {
player.getUpdateFlags().sendAnimation(7376);
reEnterTime(player, 50);
}
Crater crater = Crater.getCrater(player);
boolean remove = crater.getParticipants().remove(player);
if(remove) {
for (Player participant : crater.getParticipants()) {
if (participant.getTarget() == player) {
if(died) {
participant.getActionSender().sendMessage("Your target has died, you have been reassigned to a new target.");
} else {
participant.getActionSender().sendMessage("Your target has left the crater! You have been reassigned to a new target.");
}
crater.assignTarget(participant);
}
}
CycleEventHandler.getInstance().addEvent(player, new CycleEvent() {
@Override
public void execute(CycleEventContainer container) {
if(!died)
player.teleport(new Position(CRATER_ENTRANCES[0][0], CRATER_ENTRANCES[0][1], 0));
player.getActionSender().sendWalkableInterface(-1);
player.setSkullIcon(-1);
player.setTarget(null);
player.setAppearanceUpdateRequired(true);
container.stop();
}
@Override
public void stop() {
}
}, 1);
}
return remove;
}
/**
* The re-enter time
*
* @param c
* the player
* @param time
* the time
*/
public static void reEnterTime(final Player c, int time) {
c.setEnterPenalty(time);
CycleEventHandler.getInstance().addEvent(c, new CycleEvent() {
@Override
public void execute(CycleEventContainer container) {
c.setEnterPenalty(c.getEnterPenalty() - 1);
if (c.getEnterPenalty() <= 0) {
container.stop();
}
}
@Override
public void stop() {
}
}, 1);
}
/**
* Starts leave pentalty
*
* @param c
* the player
*/
public static void startPenalty(final Player c) {
c.setPenalty(PICKUP_PENALTY);
CycleEventHandler.getInstance().addEvent(c, new CycleEvent() {
@Override
public void execute(CycleEventContainer container) {
decreasePenalty(c);
if (c.getPenalty() <= 0) {
container.stop();
}
}
@Override
public void stop() {
}
}, 5);
}
/**
* Decreases leave penalty
*
* @param c
* the player
*/
private static void decreasePenalty(Player c) {
c.setPenalty(c.getPenalty() - 3);
if (c.getPenalty() == 0) {
c.setPenalised(false);
}
}
/**
* Handles any object click relating to bounty hunter.
* @param objectId the id of the clicked object.
* @param player the player that is clicking.
*/
public static void object_action(int objectId, Player player) {
System.out.println("called " + objectId);
if (objectId == BOUNTY_EXIT) {
boolean remove = remove(player, false);
player.getActionSender().sendMessage("" + remove);
} else if (objectId == BOUNTY_ENTRANCE_LOW || objectId == BOUNTY_ENTRANCE_MID || objectId == BOUNTY_ENTRANCE_HIGH) {
boolean add = add(player);
player.getActionSender().sendMessage("" + add);
}
}
/**
* Checks the death whenever someone is killed inside the crater.
* @param killer the killer of the victim.
* @param victim the victim who has died.
*/
public static boolean attack_action(Player attacker, Player victim) {
Crater crater = Crater.getCrater(attacker);
LinkedList<Player> list = new LinkedList<Player>();
for (Player participant : crater.getParticipants()) {
if(participant.getCombatingEntity() != null) {
if (participant.getCombatingEntity().getIndex() == victim.getIndex()) {
if (participant.getTarget().getIndex() != victim.getIndex() && participant != attacker) {
list.add(participant);
}
}
}
}
//System.out.println(list.size() + " - " + attacker.getUsername());
if(list.size() == 0)
return true;
if(list.size() >= 1)
if(attacker.getTarget() == victim)
return true;
return false;
}
/**
* Checks the death whenever someone is killed inside the crater.
* @param killer the killer of the victim.
* @param victim the victim who has died.
*/
public static void death_action(Player killer, Player victim) {
if (killer.getTarget() != victim) {
startPenalty(killer);
killer.getActionSender().sendMessage("You have killed " + victim.getUsername() + ". They were not your target, so your Rogue rating increases!");
killer.getActionSender().sendMessage("This means you recieve the pickup penalty. If you pick up anything, you will not be able to leave!");
killer.rogueKill++;
} else if (killer.getTarget() == victim) {
killer.getActionSender().sendMessage("You have killed " + victim.getUsername() + ". They were your target, so your Hunter rating increases!");
killer.targetKill++;
}
System.out.println(remove(victim, true));
}
/**
* Checks if the item will give a pickup penalty.
* @param player the specified player.
*/
public static void pickup_action(Player player) {
if (player.getPenalty() > 0) {
startPenalty(player);
player.setPenalised(true);
player.getActionSender().sendMessage("You have been penalised and cannot leave for " + PICKUP_PENALTY + " seconds!");
}
}
/**
* Checks if the player can logout.
* @param player the specified player.
*/
public static boolean logout_action(Player player) {
if (player.getPenalty() > 0 && player.isPenalised()) {
player.getActionSender().sendMessage("You have been penalised and cannot logout for " + PICKUP_PENALTY + " seconds!");
return false;
}
if(!remove(player, false))
return false;
return true;
}
/**
* The penalty for picking up an item.
*/
public static final int BOUNTY_EXIT = 28122;
/**
* The penalty for picking up an item.
*/
public static final int BOUNTY_ENTRANCE_LOW = 28119;
/**
* The penalty for picking up an item.
*/
public static final int BOUNTY_ENTRANCE_MID = 28120;
/**
* The penalty for picking up an item.
*/
public static final int BOUNTY_ENTRANCE_HIGH = 28121;
/**
* The walkable interface for bounty hunter.
*/
public static final int BOUNTY_HUNTER_INTERFACE = 25347;
/**
* The interface string for the target.
*/
public static final int TARGET_TEXT = 25350;
/**
* The interface string for the penalty.
*/
public static final int PENALTY_TEXT = 25352;
/**
* The penalty for picking up an item.
*/
public static final int PICKUP_PENALTY = 180;
/**
* The animation for entering the crater.
*/
public static final int ENTER_ANIMATION = 7376;
/**
* The list of items which are unallowed in bounty hunter.
*/
public static final int[] UNALLOWED_ITEMS = { 995, 996, 13734, 13736, 13738, 13740, 13742, 13744, 18335, 18349, 18351, 18353, 18355,
18357, 18359, 18361, 18363, 1037, 1038, 1039, 1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049,
1050, 1051, 1052, 1053, 1054, 1055, 1056, 1057, 1058, 20072, 19780, 20135, 20139, 20143, 20147, 20151, 20155,
20159, 20163, 20167 };
/**
* The array of the numerous crater caves.
*/
public static final int[][] CRATER_CAVES = {{ 3163, 3696 }, { 3171, 3701 },
{ 3180, 3708 }, { 3181, 3720 }, { 3171, 3737 }, { 3170, 3746 },
{ 3163, 3753 }, { 3147, 3758 }, { 3135, 3758 }, { 3121, 3754 },
{ 3110, 3747 }, { 3091, 3735 }, { 3086, 3717 }, { 3091, 3706 },
{ 3096, 3692 }, { 3101, 3682 }, { 3108, 3671 }, { 3124, 3665 },
{ 3138, 3669 }, { 3146, 3681 }};
/**
* The array of the three crater entrances.
*/
public static final int[][] CRATER_ENTRANCES = {{ 3152, 3672 }, { 3158, 3680 },
{ 3164, 3685 }};
/**
* Represents the different craters and their requirements.
* @author Brandyn Thomas
*/
public static enum Crater {
LOW(3, 55, 4),
MID(50, 100, 8),
HIGH(100, 126, 12);
private Crater(int craterMin, int craterMax, int heightLevel) {
this.craterMin = craterMin;
this.craterMax = craterMax;
this.heightLevel = heightLevel;
this.participants = new LinkedList<Player>();
}
/**
* Gets the minimum level boundary for the crater.
*/
private int craterMin;
/**
* Gets the minimum level boundary for the crater.
* @return the min level boundary.
*/
public int getCraterMin() {
return craterMin;
}
/**
* Gets the maximum level boundary for the crater.
*/
private int craterMax;
/**
* Gets the maximum level boundary for the crater.
* @return the max level boundary.
*/
public int getCraterMax() {
return craterMax;
}
/**
* Gets the height level for this crater.
*/
private int heightLevel;
/**
* Gets the height level for the crater.
* @return the height level.
*/
public int getHeightLevel() {
return heightLevel;
}
/**
* The list of players in the crater.
*/
private LinkedList<Player> participants;
/**
* Gets all the players in the crate.
* @return gets the participants in the crater.
*/
public LinkedList<Player> getParticipants() {
return participants;
}
/**
* Checks if the player is within the min and max.
* @param player - The player to level-check.
*/
public boolean withinLevelRange(Player player) {
return player.getSkill().getCombatLevel() >= craterMin && player.getSkill().getCombatLevel() <= craterMax;
}
/**
* Assigns the specified player a bounty.
* @param player - The player to assign a bounty to.
*/
public boolean assignTarget(Player player) {
@SuppressWarnings("unchecked")
List<Player> contestantsRemaining = (List<Player>) participants.clone();
HashMap<Player, List<Player>> targetCount = new HashMap<Player, List<Player>>();
for (Player participant : participants) {
if (participant.getTarget() != null) {
List<Player> list = targetCount.get(participant.getTarget());
if (list == null) {
targetCount.put(participant.getTarget(), new LinkedList<Player>());
targetCount.get(participant.getTarget()).add(participant);
continue;
}
list.add(participant);
if (list.size() >= 2)
contestantsRemaining.remove(participant.getTarget());
if (list.size() >= 3)
participant.setTarget(null);
}
}
Player newTarget = contestantsRemaining.get(Misc.random(contestantsRemaining.size() - 1));
while(newTarget == player && contestantsRemaining.size() > 1) {
newTarget = contestantsRemaining.get(Misc.random(contestantsRemaining.size() - 1));
}
if (newTarget != player) {
player.setTarget(newTarget);
return true;
}
player.setTarget(null);
return false;
}
/**
* The cycle for the crater.
*/
public void process() {
for (Player participant : participants) {
participant.getActionSender().sendWalkableInterface(BOUNTY_HUNTER_INTERFACE);
if(participant.getTarget() != null) {
participant.getActionSender().sendString(Misc.formatPlayerName(participant.getTarget().getUsername()), TARGET_TEXT);
} else {
if(assignTarget(participant)) {
participant.getActionSender().sendString(Misc.formatPlayerName(participant.getTarget().getUsername()), TARGET_TEXT);
} else {
participant.getActionSender().sendString("Nobody!", TARGET_TEXT);
}
}
if (participant.getPenalty() > 0) {
//participant.setPenalty(participant.getPenalty() - 1);
participant.getActionSender().sendString(participant.isPenalised() ? "Can't leave for: " : "Pickup Penalty: ", 25351);
participant.getActionSender().sendString(participant.getPenalty() + " Sec", PENALTY_TEXT);
//if (participant.getPenalty() == 0) {
// participant.setPenalised(false);
//}
} else {
participant.getActionSender().sendString("", 25351);
participant.getActionSender().sendString("", PENALTY_TEXT);
}
}
}
/**
* Gets the crater for the specified player.
* @param player - The specified player.
* @return the crater.
*/
public static Crater getCrater(Player player) {
for (Crater crater : Crater.values()) {
if (player.getSkill().getCombatLevel() >= crater.craterMin && player.getSkill().getCombatLevel() <= crater.craterMax)
return crater;
}
return HIGH;
}
}
/**
* Represents the different bounty hunter skulls.
* @author Brandyn Thomas
*/
public static enum Skull {
BROWN(2, 0, 100000),
SILVER(3, 100000, 500000),
GREEN(4, 500000, 1100000),
BLUE(5, 1100000, 2500000),
RED(6, 2500000, Integer.MAX_VALUE);
/**
* Creates the bounty hunter skull.
*/
private Skull(int headIcon, int minimumRisk, int maximumRisk) {
this.headIcon = headIcon;
this.minimumRisk = minimumRisk;
this.maximumRisk = maximumRisk;
}
/**
* The headicon of the bounty skull.
*/
private int headIcon;
/**
* The minimum risk for the specific skull.
*/
private int minimumRisk;
/**
* The maximum risk for the specific skull.
*/
private int maximumRisk;
/**
* Gets the headicon of the bounty skull.
* @return the headicon for player updating.
*/
public int getHeadIcon() {
return headIcon;
}
/**
* Gets the minimum risk required for the skull.
* @return the minimum equipment/inventory risk for the skull.
*/
public int getMinimumRisk() {
return minimumRisk;
}
/**
* Gets the maximum risk required for the skull.
* @return the maximum equipment/inventory risk for the skull.
*/
public int getMaximumRisk() {
return maximumRisk;
}
/**
* Gets the skull for the specified client/player.
* @param client the specified client/player.
* @return the player's bounty hunter skull.
*/
public static Skull getSkull(Player player) {
int risk = getTotalCarrying(player);
for (Skull skull : Skull.values()) {
if (skull.getMinimumRisk() <= risk && skull.getMaximumRisk() >= risk)
return skull;
}
return null;
}
/**
* Gets the total amount carrying
*
* @param c
* the player
* @return the total amount
*/
public static int getTotalCarrying(final Player c) {
return ItemManager.getInstance().getEquipmentNet(c)
+ ItemManager.getInstance().getInventoryNet(c);
}
}
}
How targeting works: