PDA

View Full Version : My custom multi-player Godwars (with waiting room)



Halfy
October 28th, 2010, 15:44
Godwars is a new group mini-game in my test server in which all players need to help out in order to win. The game can hold between 5 and 10 players in a game. This is a timed game, starting with 60 seconds, counting down. Each kill you get will add 10 seconds to your time unless it is a boss kill, which will add 30 seconds. Chance of any reward is 1 out of (1/killsē)*1500 + 1, with a minimum of 10 kills. The objective is to kill both bosses before running out of time.[/quote]

Purpose: To create a timed multi-player minigame, my own version of Godwars
Difficulty: 6, this may require heavy modification to work on your server, so be sure to back it up.
Assumed Knowledge: How to copy and paste and how to not flame uselessly
Server Base: Some old cheezescape
Classes Modified: GodWar, GodwarWaitingRoom, NPCHandler, client, Location, playerHandler
Required classes: Must have my Timer and Location classes:
Timer: Only the registered members can see the link.(System)-Timers
Location: Only the registered members can see the link.
Procedure

Step 1: We need to create the new classes
Create a new file called GodWar.java and put the following code in(This can be used/modified for other minigames):

import java.util.LinkedList;
import java.util.List;
import java.util.Collections;

/**
* @author Halfy
* 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 = "@red@Kills: ";
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 += "@gre@MAX@red@ Time left: "+gwTimer.getTimeLeft()+" seconds : Chance of drop: 1/"+((int)dropChance);
else // x kills, drop chance
line += "@yel@"+kills+"@red@ 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:

import java.util.LinkedList;
import java.util.List;
import java.util.Collections;

/**
* @author Halfy
* 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.rando m.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("@blu@Godwars 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("@gre@Next game starts in: "+waitTimer.getTimeLeft()+" seconds. Players: " + inWait.size() + "/10", 6569);
}
else
{
if (inWait.size() < 5)
{
if (GodWar.war.isActive())
((client)p).sendFrame126("@red@Need more players...(Have "+inWait.size()+", need 5) Game ends in: " + GodWar.war.getTimeLeft() + " seconds", 6569);
else
((client)p).sendFrame126("@red@Need more players...(Have "+inWait.size()+", need 5)", 6569);
}
else
((client)p).sendFrame126("@yel@Current game ends in: " + GodWar.war.getTimeLeft() + " seconds", 6569);
}
}
}
}
}
}

Step 2: Now we need to add the NPCs adding to kill-count, I used Fire Giants
In NPCHandler.java find the public void MonsterDropItem method and add:

if(npcs[NPCID].npcType == 1585) {
if (GodWar.war.isActive())
GodWar.war.addKill();
}
if(npcs[NPCID].npcType == Boss ID 1)
{
if (GodWar.war.isActive())
GodWar.war.bossKill(0);
}
if(npcs[NPCID].npcType == Boss ID 2)
{
if (GodWar.war.isActive())
GodWar.war.bossKill(1);
}

Step 3: Now we modify the client class
In client.java add this command:

if (command.equalsIgnoreCase("gw") || command.equalsIgnoreCase("godwars")) {
GodwarWaitingRoom.waiting.addPlayer(this);
}
and in your process method, add the following:

// kick players to home when in godwars but no the game running or add them to the game
if (GodWar.war.isInGwArea(this) && !GodWar.war.isInGw(this))
{
if (!GodWar.war.isActive())
{
teleportTo(Location.HOME);
sendMessage("You are not part of the Godwar");
}
else if (GodWar.war.isActive() && GodWar.war.addPlayer(this))
{
sendMessage("You joined the Godwar effort!");
}
else
{
teleportTo(Location.HOME);
sendMessage("You are not part of the Godwar");
}
}
Now find ApplyDead(); and change it to
if (GodWar.war.isInGw(this))
{
GodWar.war.killPlayer(this);
}
else
ApplyDead();
You will also need this method:

// Halfy: print an array of text lines onto a menu
public void showMenu(String title, String[] text) {
clearQuestInterface();
sendQuest(title, 8144);

for(int i = 8147; i < 8147+text.length; i++) {
sendQuest(text[i-8147], i);
}
sendQuestSomething(8143);
showInterface(8134);
flushOutStream();
}

Step 4: Now we add the spawns
In autospawn.cfg, add the following:

spawn = BOSS ID1 HERE 3249 9367 0 0 0 0 0 1 Godwars boss 1
spawn = 1585 3274 9357 0 0 0 0 0 1 God fire giant
spawn = 1585 3275 9379 0 0 0 0 0 1 God fire giant
spawn = 1585 3262 9399 0 0 0 0 0 1 God fire giant
spawn = 1585 3260 9376 0 0 0 0 0 1 God fire giant
spawn = 1585 3250 9348 0 0 0 0 0 1 God fire giant
spawn = 1585 3238 9366 0 0 0 0 0 1 God fire giant
spawn = 1585 3273 9389 0 0 0 0 0 1 God fire giant
spawn = 1585 3258 9388 0 0 0 0 0 1 God fire giant
spawn = 1585 3245 9349 0 0 0 0 0 1 God fire giant
spawn = 1585 3234 9363 0 0 0 0 0 1 God fire giant
spawn = 1585 3284 9356 0 0 0 0 0 1 God fire giant
spawn = 1585 3256 9392 0 0 0 0 0 1 God fire giant
spawn = 1585 3290 9365 0 0 0 0 0 1 God fire giant
spawn = 1585 3235 9359 0 0 0 0 0 1 God fire giant
spawn = 1585 3228 9355 0 0 0 0 0 1 God fire giant
spawn = 1585 3231 9368 0 0 0 0 0 1 God fire giant
spawn = 1585 3235 9371 0 0 0 0 0 1 God fire giant
spawn = 1585 3249 9400 0 0 0 0 0 1 God fire giant
spawn = 1585 3235 9401 0 0 0 0 0 1 God fire giant
spawn = 1585 3239 9384 0 0 0 0 0 1 God fire giant
spawn = 1585 3261 9364 0 0 0 0 0 1 God fire giant
spawn = 1585 3260 9366 0 0 0 0 0 1 God fire giant
spawn = 1585 3256 9351 0 0 0 0 0 1 God fire giant
spawn = 1585 3245 9360 0 0 0 0 0 1 God fire giant
spawn = 1585 3245 9368 0 0 0 0 0 1 God fire giant
spawn = 1585 3245 9365 0 0 0 0 0 1 God fire giant
spawn = BOSS ID2 HERE 3249 9359 0 0 0 0 0 1 Godwars boss 2

Step 5: Now we add the Locations
In Location.java, add these constants:

public static final Location GW_WAIT = new Location(3315, 9376, 0); // Godwars waiting room Location
public static final Location GODWAR = new Location(3302, 9373, 0); // Godwars Location

Step 6: Finally, we add the "processing"
In playerHandler, find public void process and add -

GodWar.war.process();
GodwarWaitingRoom.waiting.process();


Credits: Me, Halfy


Picture: Only the registered members can see the link.
(Thanks to a friend for taking the picture)


You need to add godswords and such to the rewards array and you need to put the ID for the godwars bosses in yourself.

Halfy
October 28th, 2010, 15:56
Completed, for now.

Supermannnnn
October 28th, 2010, 18:50
Damn, you should make ur own server again or become a coder for someone.

Halfy
October 28th, 2010, 18:56
Damn, you should make ur own server again or become a coder for someone.

I haven't really thought about helping another server, to be honest.

Mish
October 28th, 2010, 19:05
Good and clean.:)

Halfy
October 28th, 2010, 19:06
Good and clean.:)

Thanks. :)

Mish
October 28th, 2010, 19:08
Thanks. :)

Thats okay.:p i'm going to add soon.

dragonslyfe
October 28th, 2010, 21:54
will this work with project insanity?

vrmsasha
October 29th, 2010, 04:04
Nice i like it

_1Greg1_
October 29th, 2010, 08:27
Also, why does it say in the autospawn, BOSS ID 1 HERE?
What should I change it to?

Also, I get this error:


NPCHandler.java:1279: ')' expected
if(npcs[NPCID].npcType == Boss ID 1)
^
NPCHandler.java:1279: not a statement
if(npcs[NPCID].npcType == Boss ID 1)
^
NPCHandler.java:1279: ';' expected
if(npcs[NPCID].npcType == Boss ID 1)
^
NPCHandler.java:1284: ')' expected
if(npcs[NPCID].npcType == Boss ID 2)
^
NPCHandler.java:1284: not a statement
if(npcs[NPCID].npcType == Boss ID 2)
^
NPCHandler.java:1284: ';' expected
if(npcs[NPCID].npcType == Boss ID 2)
^
6 errors

Halfy
October 29th, 2010, 09:19
Remove this post.

Halfy
October 29th, 2010, 17:56
@gggccc

Change Boss ID 1 / 2 to the ID of your boss

example:

if(npcs[NPCID.npcType == 50)

the 50 is the npc ID for KBD...

Stacx
October 29th, 2010, 17:58
Nice, the actual code looks nice.

Though, the list doesn't really need to be synchornized :)

my pker pure
October 29th, 2010, 18:28
Also, why does it say in the autospawn, BOSS ID 1 HERE?
What should I change it to?

Also, I get this error:


NPCHandler.java:1279: ')' expected
if(npcs[NPCID].npcType == Boss ID 1)
^
NPCHandler.java:1279: not a statement
if(npcs[NPCID].npcType == Boss ID 1)
^
NPCHandler.java:1279: ';' expected
if(npcs[NPCID].npcType == Boss ID 1)
^
NPCHandler.java:1284: ')' expected
if(npcs[NPCID].npcType == Boss ID 2)
^
NPCHandler.java:1284: not a statement
if(npcs[NPCID].npcType == Boss ID 2)
^
NPCHandler.java:1284: ';' expected
if(npcs[NPCID].npcType == Boss ID 2)
^
6 errors



Change it to the ID of the boss obviously ...

Halfy
October 29th, 2010, 18:30
Nice, the actual code looks nice.

Though, the list doesn't really need to be synchornized :)

I suppose, and thanks.

Charlie`
October 29th, 2010, 18:35
Looks Decent Actually.

Halfy
October 29th, 2010, 18:37
Looks Decent Actually.

Thanks.