Code:
import java.util.LinkedList;
import java.util.List;
import java.util.Collections;
/**
* @author tj007razor
* Description: Handle the running of godwars games
**/
public class GodWar
{
// possible rewards from godwars
private static final int rewards[] = {995, 995, 995, 366, 366, 366, 995, 995, 995, 995, 366, 366, 366, 995, 1213, 1359, 1113, 1147, 1289, 1432, 1079, 1163, 1333, 1347, 1093, 1185, 1303, 1373, 1127, 1201, 1319, 3101, 4824, 1215, 6739, 3140, 1149, 1249, 1434, 4087, 7461, 4587, 3204, 4585, 1187, 1305, 1377, 4212, 4224, 7158, 4151, 4131, 6609, 1311, 1317, 1313, 2599, 3474, 1213, 1359, 1113, 1147, 1289, 1432, 1079, 1163, 1333, 1347, 1093, 1185, 1303, 1373, 1127, 1201, 1319, 3101, 4824, 1215, 6739, 3140, 1149, 1249, 1434, 4087, 7461, 4587, 3204, 4585, 1187, 1305, 1377, 4212, 4224, 7158, 4151, 4131 /* ADD GODSWORDS/OTHER GODWARS SPECIFIC REWARDS HERE */};
// the godwar instance
public static GodWar war = new GodWar();
private Timer gwTimer; // timer for time left in the war
private List<Player> inGame; // list of players currently in the godwar
private int kills; // current kills team has recieved
private boolean bossesKilled[]; // keeps track of whether the 2 bosses have been killed yet
// initialize the GodWar instance variables
public GodWar()
{
gwTimer = new Timer();
inGame = Collections.synchronizedList(new LinkedList<Player>());
kills = 0;
bossesKilled = new boolean[2];
}
// start game by making the timer active
public void startGame()
{
gwTimer.setTimeToRun(60);
gwTimer.setActive(true);
}
// return the amount of time left in the game
public int getTimeLeft()
{
return gwTimer.getTimeLeft();
}
// return if the game is active
public boolean isActive()
{
return gwTimer.isActive();
}
// add a player to the list if there's less than 10 people
public boolean addPlayer(Player p)
{
if (inGame.size() < 10)
inGame.add(p);
return (inGame.size() < 10);
}
// check if player is in the godwar
public boolean isInGw(Player p)
{
return inGame.contains(p);
}
// check if player is in the godwar area
public boolean isInGwArea(Player p)
{
if (p.absY >= 9340 && p.absY <= 9415 && p.absX >= 3195 && p.absX <= 3305)
return true;
return false;
}
// kill player in the godwar(remove from list)
public void killPlayer(Player p)
{
// "kill" player but they just teleport away without losing items
p.updateRequired = true;
p.appearanceUpdateRequired = true;
((client)p).resetBar();
((client)p).sendMessage("Oh dear, you died and lost the godwar! You can no longer get a reward.");
((client)p).teleportTo(Location.HOME);
((client)p).DeadStats();
((client)p).ResetAttack();
((client)p).ResetAttackNPC();
((client)p).actionTimer = 0;
((client)p).NewHP = ((client)p).getLevelForXP(((client)p).playerXP[3]);
((client)p).setSkillLevel(3, ((client)p).getLevelForXP(((client)p).playerXP[3]), ((client)p).playerXP[p.playerHitpoints]);
((client)p).playerLevel[3] = ((client)p).getLevelForXP(((client)p).playerXP[3]);
inGame.remove(p);
((client)p).sendFrame126("", 6569);
((client)p).currentHealth = ((client)p).getLevelForXP(((client)p).playerXP[3]);
((client)p).hitDiff = 0;
p.updateRequired = true;
p.appearanceUpdateRequired = true;
// tell players who died
synchronized(inGame)
{
for (int i = 0; i < inGame.size(); i++)
{
Player p1 = inGame.get(i);
if (p != null && p.isActive)
{
((client)p1).sendMessage("Player "+p.playerName+" has died! There are "+inGame.size()+" players left.");
}
}
}
}
// adds a kill to the godwar
public void addKill()
{
if (kills < 50) // 50 kill max to prevent people from staying in all day
{
kills++;
gwTimer.setTimeToRun(gwTimer.getTimeLeft() + 10);
}
}
// when the team kills a boss
public void bossKill(int index)
{
if (!bossesKilled[index])
{
if (kills < 50) // 50 kill max to prevent people from staying in all day
{
kills++;
gwTimer.setTimeToRun(gwTimer.getTimeLeft() + 30);
}
bossesKilled[index] = true;
}
if (bossesKilled[0] && bossesKilled[1]) // both bosses killed, players win
winGame();
}
// "process" of the godwar
public void process()
{
if (gwTimer.isActive())
{
if (gwTimer.stop()) // ran out of time
{
resetGW();
return;
}
printStatus();
}
}
// players win the game and reset
public void winGame()
{
synchronized(inGame)
{
// teleport each player in turn and give drop if deemed as needing one
for (int i = 0; i < inGame.size(); i++)
{
Player p = inGame.get(i);
if (p != null && p.isActive)
{
((client)p).teleportTo(Location.HOME);
((client)p).sendFrame126("", 6569);
((client)p).sendMessage("You have won the Godwar!");
((client)p).ancPts += 500;
if (kills >= 10)
{
double dropChance = 1500 * 1/((kills*kills) + 1) + 1;
if (server.random.nextInt((int)dropChance) == 0)
{
int index = server.random.nextInt(rewards.length);
if (index <= 13) // coins or bass
((client)p).addItem(rewards[index], server.random.nextInt(500));
else
((client)p).addItem(rewards[index], 1);
}
else
((client)p).sendMessage("...and got nothing!");
}
else
((client)p).sendMessage("...however your team failed to reach the 10 kill threshold and you got nothing.");
}
}
}
// reset gw vars
gwTimer.setActive(false);
inGame = Collections.synchronizedList(new LinkedList<Player>());
kills = 0;
bossesKilled = new boolean[2];
}
// reset godwars in preparation for a new game
private void resetGW()
{
synchronized(inGame)
{
// teleport each player in turn away from godwars
for (int i = 0; i < inGame.size(); i++)
{
Player p = inGame.get(i);
if (p != null && p.isActive)
{
((client)p).sendFrame126("", 6569);
((client)p).teleportTo(Location.HOME);
((client)p).sendMessage("You have lost the Godwar!");
}
}
}
// reset gw vars
gwTimer.setActive(false);
inGame = Collections.synchronizedList(new LinkedList<Player>());
kills = 0;
bossesKilled = new boolean[2];
}
// print the status of the current godwar on each players screen
private void printStatus()
{
synchronized(inGame)
{
for (int i = 0; i < inGame.size(); i++)
{
Player p = inGame.get(i);
if (p == null || !p.isActive || !isInGwArea(p))
inGame.remove(inGame.indexOf(p));
else
{
// print info on time left and player's drop chance to the screen
String line = "@[email protected]: ";
double dropChance = 1500 * 1/((kills*kills) + 1) + 1;
if (kills < 10) // below 10 kill threshold, no drop
line += kills+" Time left: "+gwTimer.getTimeLeft()+" seconds : Chance of drop: 0";
else if (kills == 50) // max kills, print drop chance
line += "@[email protected]@[email protected] Time left: "+gwTimer.getTimeLeft()+" seconds : Chance of drop: 1/"+((int)dropChance);
else // x kills, drop chance
line += "@[email protected]"+kills+"@[email protected] Time left: "+gwTimer.getTimeLeft()+" seconds : Chance of drop: 1/"+((int)dropChance);
((client)p).sendFrame126(line, 6569);
}
}
}
}
}
Next create a file named GodwarWaitingRoom.java and put the following in:
Code:
import java.util.LinkedList;
import java.util.List;
import java.util.Collections;
/**
* @author tj007razor
* Description: Handle the waiting room for godwars
**/
public class GodwarWaitingRoom
{
// the godwar instance
public static GodwarWaitingRoom waiting = new GodwarWaitingRoom();
// timer for game to start
private static Timer waitTimer;
// list of players currently in the waiting room
private List<Player> inWait;
// initialize the GodWar instance variables
public GodwarWaitingRoom()
{
waitTimer = new Timer();
inWait = Collections.synchronizedList(new LinkedList<Player>());
}
// add a player to the list if there's less than 10 people
public void addPlayer(Player p)
{
if (inWait.contains(p))
((client)p).sendMessage("You are already in the waiting room!");
else
{
Location newLoc = new Location(Location.GW_WAIT.getXCoord()+server.random.nextInt(5), Location.GW_WAIT.getYCoord()+server.random.nextInt(5), Location.GW_WAIT.getHeight());
((client)p).teleportTo(newLoc);
((client)p).sendMessage("Please wait for the next game to start!");
// show info on the game
String info[] =
{
"-5 players needed, up to 10 can go in",
"-Probability of drop is based on kills",
"---10 kills min for drop",
"---50 kills max",
"-Kill both bosses in center to win",
"-Kills gain you 10 seconds of time",
"-You do NOT lose items in the war."
};
((client)p).showMenu("@[email protected] info", info);
inWait.add(p);
}
}
// check if player is in the waiting room
public boolean isInWait(Player p)
{
return inWait.contains(p);
}
// check if player is in the godwar area
private boolean isInWaitArea(Player p)
{
if (p.absY >= 9360 && p.absY <= 9394 && p.absX >= 3306 && p.absX <= 3326)
return true;
return false;
}
//remove player if they leave the room
private void checkWaitArea(Player p)
{
if (!isInWaitArea(p))
{
((client)p).sendMessage("You have been removed from the waiting room.");
inWait.remove(inWait.indexOf(p));
((client)p).sendFrame126("", 6569);
}
}
// "process" of the waiting room
public void process()
{
printStatus();
if (!GodWar.war.isActive())
{
// with enough players and no game going on, activate 20 second buffer for next game
if (inWait.size() >= 5 && !waitTimer.isActive())
{
waitTimer.setTimeToRun(20);
waitTimer.setActive(true);
}
// 20 seconds is up, start the game
else if (waitTimer.isActive() && waitTimer.stop())
{
startGodwar();
waitTimer.setActive(false);
}
}
}
// add players and start godwar game
private void startGodwar()
{
GodWar.war.startGame();
synchronized(inWait)
{
for (int i = 0; i < inWait.size(); i++)
{
Player p = inWait.get(i);
if (!(p == null) && p.isActive)
{
if (!GodWar.war.addPlayer(p))
((client)p).sendMessage("Godwars is full. You will be added next game.");
else
{
((client)p).sendMessage("You have entered the Godwar!");
((client)p).teleportTo(Location.GODWAR);
inWait.set(inWait.indexOf(p), null);
}
}
}
}
}
// print the status of the current godwar on each players screen
private void printStatus()
{
synchronized(inWait)
{
for (int i = 0; i < inWait.size(); i++)
{
Player p = inWait.get(i);
if (p == null || !p.isActive)
inWait.remove(inWait.indexOf(p));
else
{
checkWaitArea(p);
if (waitTimer.isActive() && !waitTimer.stop())
{
((client)p).sendFrame126("@[email protected] game starts in: "+waitTimer.getTimeLeft()+" seconds. Players: " + inWait.size() + "/10", 6569);
}
else
{
if (inWait.size() < 5)
{
if (GodWar.war.isActive())
((client)p).sendFrame126("@[email protected] more players...(Have "+inWait.size()+", need 5) Game ends in: " + GodWar.war.getTimeLeft() + " seconds", 6569);
else
((client)p).sendFrame126("@[email protected] more players...(Have "+inWait.size()+", need 5)", 6569);
}
else
((client)p).sendFrame126("@[email protected] game ends in: " + GodWar.war.getTimeLeft() + " seconds", 6569);
}
}
}
}
}
}