Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[holiday special] monster carnival

Newbie Spellweaver
Joined
Sep 17, 2013
Messages
82
Reaction score
137
Tested on a v62 server. Packets should be the same pre-bb, not sure about post. However, it should have the same structure with minimal modifications.

All new files should go inside of a new package called server/partyquest/mcpq/.

scripts/npc/2042000.js
PHP:
/**  *  [USER=19862]id[/USER] 2042000
 *    [USER=806871]NPC[/USER] Spiegelmann
 *    [USER=836108]Function[/USER] Monster Carnival Lobby NPC
 * @author s4nta
 */

// Relevant Monster Carnival classes
var MonsterCarnival = net.sf.odinms.server.partyquest.mcpq.MonsterCarnival;
var MCTracker = net.sf.odinms.server.partyquest.mcpq.MCTracker;
var MCParty = net.sf.odinms.server.partyquest.mcpq.MCParty;
var MCField = net.sf.odinms.server.partyquest.mcpq.MCField;
var MCTeam = net.sf.odinms.server.partyquest.mcpq.MCField.MCTeam;

// NPC variables
var status = -1;
var carnival, field;
var room = -1;

function start() {
    if (cm.getMapId() != 980000000) {
        MCTracker.log("Spiegelmann called on invalid map " + cm.getMapId() + " by player " + cm.getName());
        cm.sendOk("You are not authorized to do this.");
        cm.dispose();
        return;
    }
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == -1) {
        cm.dispose();
        return;
    }
    if (mode == 1) status++;
    else status--;

    if (status == 0) {
        if (cm.getParty() == null) {
            cm.sendOk("You are not in a party.");
            cm.dispose();
            return;
        } else if (!cm.isLeader()) {
            cm.sendOk("If you want to try Carnival PQ, please tell the #bleader of your party#k to talk to me.");
            cm.dispose();
            return;
        }
        carnival = MonsterCarnival.getMonsterCarnival(cm.getChannel());
        cm.sendSimple(carnival.getNPCAvailableFields());
    } else if (status == 1) {
        room = selection;
        if (room < 1 || room > 6) {
            cm.sendOk("That is not a valid room.");
            cm.dispose();
            return;
        }
        var code = carnival.registerStatus(cm.getParty(), selection);
        if (code == MonsterCarnival.STATUS_FIELD_FULL) {
            cm.sendOk("This room is currently full.")
        } else if (code == MonsterCarnival.STATUS_PARTY_SIZE) {
            cm.sendOk("Your party is not the right size for this field.");
        } else if (code == MonsterCarnival.STATUS_PARTY_LEVEL) {
            cm.sendOk("Please check to see that the members in your party are between level 30 and 50.");
        } else if (code == MonsterCarnival.STATUS_PARTY_MISSING) {
            cm.sendOk("Please make sure everyone in your party is in this lobby.");
        } else if (code == MonsterCarnival.STATUS_FIELD_INVALID) {
            cm.sendOk("Unauthorized request.");
        }

        if (code == MonsterCarnival.STATUS_PROCEED) {
            field = carnival.getField(room);
            party = carnival.createParty(cm.getParty());
            field.register(party, MCTeam.RED);
            cm.sendOk("You will have 3 minutes to accept challenges from other parties.");
        } else if (code == MonsterCarnival.STATUS_REQUEST) {
            cm.sendOk("Sending request to room " + room + ". You will be automatically warped in if they accept your challenge.");
            field = carnival.getField(room);
            party = carnival.createParty(cm.getParty());
            field.request(party);
        }
        cm.dispose();
    }
}

scripts/npc/2042002.js
PHP:
/** * [USER=19862]id[/USER] 2042002
 *  [USER=806871]NPC[/USER] Spiegelmann
 *  [USER=836108]Function[/USER] Monster Carnival NPC
 * @author s4nta
 *  [USER=497496]cred[/USER]its xirengfx (for store code, CPQ description)
 */
var DISABLED = false;

var SavedLocationType = Packages.net.sf.odinms.server.maps.SavedLocationType;

// Relevant Monster Carnival classes
var MonsterCarnival = Packages.net.sf.odinms.server.partyquest.mcpq.MonsterCarnival;
var MCTracker = Packages.net.sf.odinms.server.partyquest.mcpq.MCTracker;
var MCParty = Packages.net.sf.odinms.server.partyquest.mcpq.MCParty;
var MCField = Packages.net.sf.odinms.server.partyquest.mcpq.MCField;

// NPC variables
var status = -1;
var store = false;
var ctx = -1; //context
var storeInfo;
var purchaseId;
var purchaseCost;

// Reference
var coinId = 4001129;
var coinIcon = "#i" + coinId + "#";
var infoMaps = [220000000, 200000000, 103000000, 540000000]; // ludi, orbis, kerning, singapore
var gradeS = 600
var gradeA = 500
var gradeB = 400
var gradeC = 300
var gradeD = 200
var gradeE = 100

var expRewards = [[150000, 100000], // S Winner/Loser
                  [100000, 70000], // A Winner/Loser
                  [75000, 43250], // B Winner/Loser
                  [50000, 25000], // C Winner/Loser
                  [25000, 12500], // D Winner/Loser
                  [12500, 6250],  // E Winner/Loser
                  [5000, 2500]    // F Winner/Loser
                  ];

// Exchange stores
var warrior = [[1302004, 7], [1402006, 7], [1302009, 10], [1402007, 10],
               [1302010, 20], [1402003, 20], [1312006, 7], [1412004, 7],
               [1312007, 10], [1412005, 10], [1312018, 20], [1412003, 20],
               [1322015, 7], [1422008, 7], [1322016, 10], [1422007, 10],
               [1322017, 20], [1422005, 20], [1432003, 7], [1442003, 7],
               [1432005, 10], [1442009, 10], [1442005, 20], [1432004, 20]];

var magician = [[1372001, 7], [1382018, 7], [1372012, 10], [1382019, 10],
                [1382001, 20], [1372007, 20]];

var archer = [[1452006, 7], [1452007, 10], [1452008, 20], [1462005, 7],
              [1462006, 10], [1462007, 20]];

var thief = [[1472013, 7], [1472017, 10], [1472021, 20], [1332014, 7],
             [1332011, 10], [1332031, 10], [1332016, 20], [1332034, 20]];

var pirate = [[1482005, 7], [1482006, 10], [1482007, 20], [1492005, 7],
              [1492006, 10], [1492007, 20]];

var necklace = [[1122007, 50], [2041211, 40]];

// Long Text Descriptions
var infoText = "You wish to know about the Monster Carnival? Very well. The Monster Carnival is a place of trilling battles and exciting competiton against people just as strong and motivated as yourself. You must summon monsters and defeat the monsters summoned by the opposing party. That's the essence of the Monster Carnival. Once you enter the Carnival Field, the task is to earn CP by hunter monsters from the opposing party and use those CP's to distract the opposing party from hunting monsters. There are three ways to distract the other party; Summon a Monster, Skill or Protector. Please remember this though, it's never a good idea to save up CP just for the sake of it. The CP's you've used will also help determine the winner and the loser of the carnival.";
var no = "You do not have enough Maple Coins for this item. Come back to me when you acquire more!";

function getGrade(cp) {
    // Returns index of corresponding expRewards pair.
    if (cp >= gradeS) {
        return 0;
    } else if (cp >= gradeA) {
        return 1;
    } else if (cp >= gradeB) {
        return 2;
    } else if (cp >= gradeC) {
        return 3;
    } else if (cp >= gradeD) {
        return 4;
    } else if (cp >= gradeE) {
        return 5;
    } else {
        return 6;
    }
}

function isTownMap(map) {
    for (var i = 0; i < infoMaps.length; i++) {
        if (infoMaps[i] == map) {
            return true;
        }
    }
    return false;
}

function isExitMap(map) {
    return map == 980000010;
}

function isWinnerMap(map) {
    return (map >= 980000000 && map <= 980000700 && map % 10 == 3);
}

function isLoserMap(map) {
    return (map >= 980000000 && map <= 980000700 && map % 10 == 4);
}

var CONTEXT_NONE = -1;
var CONTEXT_TOWN = 0;
var CONTEXT_EXIT = 1;
var CONTEXT_WIN  = 2;
var CONTEXT_LOSE = 3;

function start() {  
    if (DISABLED) {
        cm.sendOk("CPQ is temporarily unavailable.");
        cm.dispose();
        return;
    }
    m = cm.getMapId();
    if (isTownMap(m)) {
        ctx = CONTEXT_TOWN;
    } else if (isExitMap(m)) {
        ctx = CONTEXT_EXIT;
    } else if (isWinnerMap(m)) {
        ctx = CONTEXT_WIN;
    } else if (isLoserMap(m)) {
        ctx = CONTEXT_LOSE;
    } else {
        ctx = CONTEXT_NONE;
    }

    action(1, 0, 0);
}

function doLoserMap(mode, type, selection) {
    if (cm.getPlayer().getMCPQParty() == null) {
        cm.warp(MonsterCarnival.MAP_LOBBY);
        cm.dispose();
        return;
    }

    if (mode == -1) {
        cm.dispose();
    } else {
        if (mode == 1) status++;
        else status--;

        if (status == 0) {
            cm.sendNext("Unfortunately, you did not manage to win this round. Better luck next time!");
        } else if (status == 1) {
            var points = cm.getPlayer().getMCPQParty().getTotalCP();
            var grade = getGrade(points);
            var letterGrade = "ABCDF"[grade];
            var expReward = expRewards[grade][1];

            cm.sendNext("Your grade is: #b" + letterGrade + "\r\n\r\n#kEXP Reward: " + expReward);
            cm.gainExp(expReward);
        } else if (status == 2) {
            cm.warp(MonsterCarnival.MAP_LOBBY);
            cm.dispose();
        }
    }
}

function doWinnerMap(mode, type, selection) {
    if (cm.getPlayer().getMCPQParty() == null) {
        cm.warp(MonsterCarnival.MAP_LOBBY);
        cm.dispose();
        return;
    }

    if (mode == -1) {
        cm.dispose();
    } else {
        if (mode == 1) status++;
        else status--;

        if (status == 0) {
            cm.sendNext("Congratulations! You managed to defeat the enemy team!");
        } else if (status == 1) {
            var points = cm.getPlayer().getMCPQParty().getTotalCP();
            var grade = getGrade(points);
            var letterGrade = "ABCDF"[grade];
            var expReward = expRewards[grade][0];

            cm.sendNext("Your grade is: #b" + letterGrade + "\r\n\r\n#kEXP Reward: " + expReward);
            cm.gainExp(expReward);
        } else if (status == 2) {
            cm.warp(MonsterCarnival.MAP_LOBBY);
            cm.dispose();
        }
    }
}

function doTown(mode, type, selection) {
    if (mode == -1) {
        cm.sendOk("Be sure to vote for the server every 24 hours!");
        cm.dispose();
    } else {
        if (mode == 1) status++;
        else status--;

        if (status == 0) {
            cm.sendSimple("What would you like to do? If you have never participated in the Monster Carnival, you'll need to know a thing or two about it before joining.\r\n\r\n#b#L0#Go to the Monster Carnival Field#l\r\n#L1#Learn about the Monster Carnival#l\r\n#L2#Trade Maple Coin#l");
        } else if (status == 1) {
            if (selection == 0) {
                if (cm.getChar().getLevel() < MonsterCarnival.MIN_LEVEL || cm.getChar().getLevel() > MonsterCarnival.MAX_LEVEL) {
                    cm.sendOk("You must be between level " + MonsterCarnival.MIN_LEVEL + " and level " + MonsterCarnival.MAX_LEVEL + " to enter.");
                    cm.dispose();
                    return;
                }
                cm.getChar().saveLocation(SavedLocationType.MONSTER_CARNIVAL);
                cm.warp(MonsterCarnival.MAP_LOBBY, 4);
                cm.dispose();
                return;
            } else if (selection == 1) {
                cm.sendPrev(infoText);
                cm.dispose();
                return;
            } else if (selection == 2) {
                store = true;
                cm.sendSimple("Select a category:\r\n" +
                    "#L101##bTrade Maple Coins for Warrior Weapons\r\n" +
                    "#L102#Trade Maple Coins for Magician Weapons\r\n" +
                    "#L103#Trade Maple Coins for Bowman Weapons\r\n" +
                    "#L104#Trade Maple Coins for Thief Weapons\r\n" +
                    "#L105#Trade Maple Coins for Pirate Weapons\r\n" +
                    "#L106#Trade Maple Coins for a Necklace");
            }
        } else if (status == 2) {
            if (store) {
                switch (selection) {
                    case 101:
                        storeInfo = warrior;
                        break;
                    case 102:
                        storeInfo = magician;
                        break;
                    case 103:
                        storeInfo = archer;
                        break;
                    case 104:
                        storeInfo = thief;
                        break;
                    case 105:
                        storeInfo = pirate;
                        break;
                    case 106:
                        storeInfo = necklace;
                        break;
                    default:
                        storeInfo = [];
                }
                if (storeInfo.length == 0) {
                    cm.sendOk("That store doesn't exist.");
                    cm.dispose();
                    return;
                }
                var storeText = "";
                for (var i = 0; i < storeInfo.length; ++i) {
                    var wepId = storeInfo[i][0];
                    var cost = storeInfo[i][1];
                    storeText += "#L" + i + "##v" + wepId + "# - #z" + wepId + "# - " + cost + " " + coinIcon + "#l\r\n";
                }
                cm.sendSimple(storeText);
            } else {
                MCTracker.log("[MCPQ_Info] CONTEXT_TOWN: Invalid status 2");
            }
        } else if (status == 3) {
            if (store) {
                purchaseId = storeInfo[selection][0];
                purchaseCost = storeInfo[selection][1];

                if (cm.haveItem(coinId, purchaseCost)) {
                    cm.sendYesNo("Are you sure you want to purchase #i" + purchaseId + "#? You will have #r#e" + (cm.itemQuantity(coinId) - purchaseCost) + " " + coinIcon + "##k#n remaining.");
                } else {
                    cm.sendOk("You don't have enough " + coinIcon + ".");
                    cm.dispose();
                }
            } else {
                MCTracker.log("[MCPQ_Info] CONTEXT_TOWN: Invalid status 3");
            }
        } else if (status == 4) {
            if (store) {
                if (cm.haveItem(coinId, purchaseCost)) {
                    cm.gainItem(coinId, -purchaseCost);
                    cm.gainItem(purchaseId);
                    cm.sendOk("Congratulations! Enjoy your new item.");
                    cm.dispose();
                }
            } else {
                MCTracker.log("[MCPQ_Info] CONTEXT_TOWN: Invalid status 4");
            }
        }
    }
}

function doExit() {
    cm.warp(MonsterCarnival.MAP_LOBBY);
    cm.sendOk("Hope you had fun in the Carnival PQ!");
    cm.dispose();
}

function action(mode, type, selection) {
    switch (ctx) {
        case CONTEXT_TOWN:
            doTown(mode, type, selection);
            break;
        case CONTEXT_EXIT:
            doExit();
            break;
        case CONTEXT_LOSE:
            doLoserMap(mode, type, selection);
            break;
        case CONTEXT_WIN:
            doWinnerMap(mode, type, selection);
            break;
        default:
            MCTracker.log("[MCPQ_INFO] Invalid context (value: " + ctx + ")");
            cm.dispose();
    } }

scripts/npc/2042003.js
PHP:
/** *  [USER=19862]id[/USER] 2042003
 *    [USER=806871]NPC[/USER] Assistant Red
 *    [USER=836108]Function[/USER] Monster Carnival Waiting Room NPC
 * @author s4nta
 */

// Relevant Monster Carnival classes
var MonsterCarnival = net.sf.odinms.server.partyquest.mcpq.MonsterCarnival;
var MCTracker = net.sf.odinms.server.partyquest.mcpq.MCTracker;
var MCParty = net.sf.odinms.server.partyquest.mcpq.MCParty;
var MCField = net.sf.odinms.server.partyquest.mcpq.MCField;
var MCTeam = net.sf.odinms.server.partyquest.mcpq.MCField.MCTeam;

// NPC variables
var status = -1;
var carnival, field;
var room = -1;

function start() {
    if (!MonsterCarnival.isLobbyMap(cm.getMapId())) {
        MCTracker.log("Assistant called on invalid map " + cm.getMapId() + " by player " + cm.getName());
        cm.sendOk("You are not authorized to do this.");
        cm.dispose();
        return;
    }
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == -1) {
        cm.dispose();
        return;
    }
    if (mode == 1) status++;
    else status--;

    if (status == 0) {
        if (cm.getParty() == null) {
            cm.warp(MonsterCarnival.MAP_LOBBY);
            cm.dispose();
            return;
        }
        options = ["#L1#Leave the room #r#e(WARNING: Abusing this will block you from future Carnival PQs)#b#n.#l",
                   "#L2#Close NPC#l"];
        if (cm.isLeader()) {
            options.unshift("#L0#View pending challenges#l");
        }

        text = "Welcome to Carnival PQ. I am #rAssistant Red#k. What can I do for you?#b\r\n";
        for (var i = 0; i < options.length; i++) {
            text += options[i];
            text += "\r\n";
        }
        cm.sendSimple(text);
    } else if (status == 1) {
        field = cm.getChar().getMCPQField();
        if (selection == 0) {
            if (!cm.isLeader()) {
                cm.sendOk("You are not authorized to do this.");
                cm.dispose();
                return;
            }
            if (!field.hasPendingRequests()) {
                cm.sendOk("There are no pending requests at this time.");
                cm.dispose();
                return;
            }
            cm.sendSimple(field.getNPCRequestString());
        } else if (selection == 1) {
            if (field != null) {
                field.deregister(true);
            } else {
                cm.warp(MonsterCarnival.MAP_EXIT);
            }
            cm.dispose();
        } else {
            cm.dispose();
        }
    } else if (status == 2) {
        var code = field.acceptRequest(selection);
        if (code == 1) {
            cm.sendOk("The challenge was accepted.");
        } else {
            cm.sendOk("An unknown error occurred.");
        }
        cm.dispose();
    }
}

scripts/npc/2042004.js
PHP:
/** *  [USER=19862]id[/USER] 2042004
 *    [USER=806871]NPC[/USER] Assistant Blue
 *    [USER=836108]Function[/USER] Monster Carnival Waiting Room NPC
 * @author s4nta
 */

// Relevant Monster Carnival classes
var MonsterCarnival = net.sf.odinms.server.partyquest.mcpq.MonsterCarnival;
var MCTracker = net.sf.odinms.server.partyquest.mcpq.MCTracker;
var MCParty = net.sf.odinms.server.partyquest.mcpq.MCParty;
var MCField = net.sf.odinms.server.partyquest.mcpq.MCField;
var MCTeam = net.sf.odinms.server.partyquest.mcpq.MCField.MCTeam;

// NPC variables
var status = -1;
var carnival, field;
var room = -1;

function start() {
    if (!MonsterCarnival.isLobbyMap(cm.getMapId())) {
        MCTracker.log("Assistant called on invalid map " + cm.getMapId() + " by player " + cm.getName());
        cm.sendOk("You are not authorized to do this.");
        cm.dispose();
        return;
    }
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == -1) {
        cm.dispose();
        return;
    }
    if (mode == 1) status++;
    else status--;

    if (status == 0) {
        if (cm.getParty() == null) {
            cm.warp(MonsterCarnival.MAP_LOBBY);
            cm.dispose();
            return;
        }
        options = ["#L1#Leave the room #r#e(WARNING: Abusing this will block you from future Carnival PQs)#b#n.#l",
                   "#L2#Close NPC#l"];
        if (cm.isLeader()) {
            options.unshift("#L0#View pending challenges#l");
        }

        text = "Welcome to Carnival PQ. I am #bAssistant Blue#k. What can I do for you?#b\r\n";
        for (var i = 0; i < options.length; i++) {
            text += options[i];
            text += "\r\n";
        }
        cm.sendSimple(text);
    } else if (status == 1) {
        field = cm.getChar().getMCPQField();
        if (selection == 0) {
            if (!cm.isLeader()) {
                cm.sendOk("You are not authorized to do this.");
                cm.dispose();
                return;
            }
            if (!field.hasPendingRequests()) {
                cm.sendOk("There are no pending requests at this time.");
                cm.dispose();
                return;
            }
            cm.sendSimple(field.getNPCRequestString());
        } else if (selection == 1) {
            if (field != null) {
                field.deregister(true);
            } else {
                cm.warp(MonsterCarnival.MAP_EXIT);
            }
            cm.dispose();
        } else {
            cm.dispose();
        }
    } else if (status == 2) {
        var code = field.acceptRequest(selection);
        if (code == 1) {
            cm.sendOk("The challenge was accepted.");
        } else {
            cm.sendOk("An unknown error occurred.");
        }
        cm.dispose();
    }
}

scripts/portal/MCrevive(1,2,...6).js:
PHP:
function enter(pi) {   player = pi.getPlayer();
   if (player.getMCPQField() != null) {
       player.getMCPQField().onRevive(player);
   } else {
       pi.warp(980000000);
   }
   return true;
}

scripts/portal/mc_out.js
PHP:
/*
	This file is part of the OdinMS Maple Story Server
    Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
                       Matthias Butz <matze@odinms.de>
                       Jan Christian Meyer <vimes@odinms.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

importPackage(Packages.net.sf.odinms.server.maps);

/*
Return from MCPQ map.
*/

function enter(pi) {
	var returnMap = pi.getPlayer().getSavedLocation(SavedLocationType.MONSTER_CARNIVAL);
	if (returnMap < 0) {
		returnMap = 100000000; // to fix people who entered the fm trough an unconventional way
	}
	var target = pi.getPlayer().getClient().getChannelServer().getMapFactory().getMap(returnMap);
	var targetPortal;

	if (returnMap == 230000000) {
		targetPortal = target.getPortal("market01");
	} else {
		targetPortal = target.getPortal("market00");
	}

	if (targetPortal == null)
		targetPortal = target.getPortal(0);

	if (pi.getPlayer().getMapId() != target) {
		pi.getPlayer().clearSavedLocation(SavedLocationType.MONSTER_CARNIVAL);
		pi.getPlayer().changeMap(target, targetPortal);
		return true;
	}
	return false;
}
-----

client/MapleCharacter.java
Add fields:
PHP:
private MCField.MCTeam MCPQTeam;private MCParty MCPQParty;
private MCField MCPQField;
private int availableCP = 0;
private int totalCP = 0;

under playerDead() method:
PHP:
if (player.getMap().isTown()) {    XPdummy *= 0.01;
} else if (MonsterCarnival.isBattlefieldMap(player.getMapId())) {
    XPdummy = 0;
}

under the giveDebuff() method, add a field that force adds the disease if some variable cpq is set, regardless of buffs.
method signature:
PHP:
public void giveDebuff(MapleDisease disease, MobSkill skill, boolean cpq)

Add these methods:
PHP:
public int getTeam() {    if (this.MCPQTeam == null) {
        return -1;
    }
    return this.MCPQTeam.code;
}

public MCField.MCTeam getMCPQTeam() {
    return MCPQTeam;
}

public void setMCPQTeam(MCField.MCTeam MCPQTeam) {
    this.MCPQTeam = MCPQTeam;
}

public MCParty getMCPQParty() {
    return MCPQParty;
}

public void setMCPQParty(MCParty MCPQParty) {
    this.MCPQParty = MCPQParty;
}

public MCField getMCPQField() {
    return MCPQField;
}

public void setMCPQField(MCField MCPQField) {
    this.MCPQField = MCPQField;
}

public int getAvailableCP() {
    return availableCP;
}

public void setAvailableCP(int availableCP) {
    this.availableCP = availableCP;
}

public int getTotalCP() {
    return totalCP;
}

public void setTotalCP(int totalCP) {
    this.totalCP = totalCP;
}

public void gainCP(int cp) {
    this.availableCP += cp;
    this.totalCP += cp;
}

public void loseCP(int cp) {
    this.availableCP -= cp;
}

client/MapleClient.java
Add this under disconnect() (right after event instance calls onPlayerDisconnect, preferably):
PHP:
if (chr.getMCPQField() != null) {    chr.getMCPQField().onPlayerDisconnected(player);
}

net/channel/handlers/ChangeMapHandler.java
Under the respawn map change code (!c.getPlayer().isAlive()), add before executeStandardPath is done:
PHP:
if (player.getMCPQField() != null) {    player.getMCPQField().onPlayerRespawn(player);
    return;
}

net/channel/handlers/ItemPickupHandler.java
Add before items are added to inventory, or after mesos are handled:
PHP:
else if (c.getPlayer().getMCPQField() != null) {   // CPQ Handling    boolean consumed = c.getPlayer().getMCPQField().onItemPickup(c.getPlayer(), mapitem);
    if (consumed) {
        c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.removeItemFromMap(mapitem.getObjectId(), 2, c.getPlayer().getId()),
                mapitem.getPosition());
        c.getPlayer().getCheatTracker().pickupComplete();
        c.getPlayer().getMap().removeMapObject(ob);
    } else {
        if (MapleInventoryManipulator.addFromDrop(c, mapitem.getItem(), true)) {
            c.getPlayer().getMap().broadcastMessage(
                    MaplePacketCreator.removeItemFromMap(mapitem.getObjectId(), 2, c.getPlayer().getId()),
                    mapitem.getPosition());
            c.getPlayer().getCheatTracker().pickupComplete();
            c.getPlayer().getMap().removeMapObject(ob);
        } else {
            c.getPlayer().getCheatTracker().pickupComplete();
            return;
        }
    }
}

net/channel/handlers/PetLootHandler.java
PHP:
else if (c.getPlayer().getMCPQField() != null) {   // CPQ Handling
                    boolean consumed = c.getPlayer().getMCPQField().onItemPickup(c.getPlayer(), mapitem);
                    if (consumed) {
                        c.getPlayer().getMap().broadcastMessage(MaplePacketCreator.removeItemFromMap(mapitem.getObjectId(), 2, c.getPlayer().getId()),
                                mapitem.getPosition());
                        c.getPlayer().getCheatTracker().pickupComplete();
                        c.getPlayer().getMap().removeMapObject(ob);
                    } else {
                        if (MapleInventoryManipulator.addFromDrop(c, mapitem.getItem(), true)) {
                            c.getPlayer().getMap().broadcastMessage(
                                    MaplePacketCreator.removeItemFromMap(mapitem.getObjectId(), 2, c.getPlayer().getId()),
                                    mapitem.getPosition());
                            c.getPlayer().getCheatTracker().pickupComplete();
                            c.getPlayer().getMap().removeMapObject(ob);
                        } else {
                            c.getPlayer().getCheatTracker().pickupComplete();
                            return;
                        }
                    }
                }
net/channel/handlers/MonsterCarnivalHandler.java
PHP:
package net.sf.odinms.net.channel.handler;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.net.AbstractMaplePacketHandler;
import net.sf.odinms.server.partyquest.mcpq.MCField;
import net.sf.odinms.server.partyquest.mcpq.MCTracker;
import net.sf.odinms.server.partyquest.mcpq.MonsterCarnival;
import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;

/**
 * Packet handler for Monster Carnival.
 * @author s4nta
 */
public class MonsterCarnivalHandler extends AbstractMaplePacketHandler {

    @Override
    public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
        int tab = slea.readByte();
        int num = slea.readByte();
        MapleCharacter chr = c.getPlayer();

        if (MonsterCarnival.DEBUG) {
            MCTracker.log("[MCHandler] " + chr.getName() + " used tab "  + tab + " num " + num);
            System.out.println("[MCHandler] " + chr.getName() + " used tab "  + tab + " num " + num);
        }

        if (chr.getMCPQField() == null || chr.getMCPQParty() == null) {
            MCTracker.log("[MCHandler] " + chr.getName() + " attempting to use Monster Carnival handler without being in Monster Carnival");
            return;
        }

        MCField field = chr.getMCPQField();
        if (tab == 0) {
            field.onAddSpawn(c.getPlayer(), num);
        } else if (tab == 1) {
            field.onUseSkill(c.getPlayer(), num);
        } else if (tab == 2) { // status
            field.onGuardianSummon(c.getPlayer(), num);
        }
    }
}

server/MapleStatEffect.java
Add fields:
PHP:
private boolean consumeOnPickup, party;
private int cp, nuffSkill;

Add to stat parsing (loadFromData()):
PHP:
ret.cp = MapleDataTool.getInt("cp", source, 0);ret.party = MapleDataTool.getInt("party", source, 0) > 0;
ret.consumeOnPickup = MapleDataTool.getInt("consumeOnPickup", source, 0) > 0;
ret.nuffSkill = MapleDataTool.getInt("nuffSkill", source, -1);

Add methods:
PHP:
public int getCP() {    return cp;
}

public boolean isParty() {
    return party;
}

public boolean isConsumeOnPickup() {
    return consumeOnPickup;
}

public int getNuffSkill() {
    return nuffSkill;
}

server/life/MapleLifeFactory.java
Add to getMonster(int monsterId):
PHP:
stats.setCp(MapleDataTool.getIntConvert("getCP", monsterInfoData, 0));

server/life/MapleMonster.java
Add fields:
PHP:
private int team = -1;

Add methods:
PHP:
public int getCP() {    return stats.getCp();
}

public int getTeam() {
    return team;
}

public void setTeam(int team) {
    this.team = team;
}

public void dispel() {
    if (!isAlive()) return;

    for (MonsterStatus i : MonsterStatus.values()) {
        if (monsterBuffs.contains(i)) {
            removeMonsterBuff(i);
            MaplePacket packet = MaplePacketCreator.cancelMonsterStatus(getObjectId(), Collections.singletonMap(i, Integer.valueOf(1)));
            map.broadcastMessage(packet, getPosition());
            if (getController() != null && !getController().isMapObjectVisible(MapleMonster.this)) {
                getController().getClient().getSession().write(packet);
            }
        }
    }
}

server/life/MapleMonsterStats.java
Add field:
PHP:
private int cp;

Add methods:
PHP:
public int getCp() {    return cp;
}

public void setCp(int cp) {
    this.cp = cp;
}

server/life/MobSkill.java
Add to applyEffect():

PHP:
case 150:    monStat = MonsterStatus.WEAPON_ATTACK_UP;
    break;
case 151:
    monStat = MonsterStatus.WEAPON_DEFENSE_UP;
    break;
case 152:
    monStat = MonsterStatus.MAGIC_ATTACK_UP;
    break;
case 153:
    monStat = MonsterStatus.MAGIC_DEFENSE_UP;
    break;
case 154:
    monStat = MonsterStatus.ACC;
    break;
case 155:
    monStat = MonsterStatus.AVOID;
    break;
case 156:
    monStat = MonsterStatus.SPEED;
    break;

server/life/SpawnPoint.java
Add field:
PHP:
private int team = -1;

Add constructor:
PHP:
public SpawnPoint(MapleMonster monster, Point pos, int mobTime, int team) {    super();
    this.monster = monster;
    this.pos = new Point(pos);
    this.mobTime = mobTime;
    this.immobile = !monster.isMobile();
    this.nextPossibleSpawn = System.currentTimeMillis();
    this.team = team;
}

Add monster death listener under spawnMonster():
PHP:
if (team > -1) {    final int cp = mob.getCP();
    mob.addListener(new MonsterListener() {

        @Override
        public void monsterKilled(MapleMonster monster, MapleCharacter highestDamageChar) {
            if (highestDamageChar == null) {
                return;
            }

            if (highestDamageChar.getMCPQParty() == null) {
                MCTracker.log("Attempted to give CP to character without assigned MCPQ Party.");
                return;
            }

            highestDamageChar.getMCPQField().monsterKilled(highestDamageChar, cp);

        }
    });
    mob.setTeam(team);
}

server/maps/MapleMap.java
Add fields:
PHP:
private boolean respawning = true;
private MCWZData mcpqData;

Add methods:
PHP:
public final List<MapleMonster> getAllMonsters() {    return getAllMapObjects(MapleMapObjectType.MONSTER);
}

public void addMonsterSpawn(MapleMonster monster, int mobTime, int team) {
    Point newpos = calcPointBelow(monster.getPosition());
    newpos.y -= 1;
    SpawnPoint sp = new SpawnPoint(monster, newpos, mobTime, team);
    monsterSpawn.add(sp);
    if (!respawning) return;

    if (sp.shouldSpawn() || mobTime == -1) {
        sp.spawnMonster(this);
    }
}

public void setReactorState(MapleReactor reactor, byte state) {
    synchronized (this.mapobjects) {
        reactor.setState(state);
        broadcastMessage(MaplePacketCreator.triggerReactor(reactor, state));
    }
}

public <E extends MapleMapObject> List<E> getAllMapObjects(MapleMapObjectType type) {
    List<E> ret = new ArrayList<>();
    synchronized (mapobjects) {
        for (MapleMapObject l : mapobjects.values()) {
            if (l.getType() == type) {
                ret.add((E) l);
            }
        }
    }
    return ret;
}

public void clearDrops() {
    List<MapleMapItem> items = getAllMapObjects(MapleMapObjectType.ITEM);
    for (MapleMapItem itemmo : items) {
        removeMapObject(itemmo);
        broadcastMessage(MaplePacketCreator.removeItemFromMap(itemmo.getObjectId(), 0, 0));
    }
}

public Collection<SpawnPoint> getSpawnPoints() {
    return monsterSpawn;
}

public void respawn() {
    for (SpawnPoint sp : this.monsterSpawn) {
        if (sp.shouldSpawn()) {
            sp.spawnMonster(this);
        }
    }
}

public void beginSpawning() {
    this.respawning = true;
    this.respawn();
}

public boolean isRespawning() {
    return respawning;
}

public void setRespawning(boolean respawning) {
    this.respawning = respawning;
}

public MCWZData getMCPQData() {
    return this.mcpqData;
}

public void setMCPQData(MCWZData data) {
    this.mcpqData = data;
}

server/maps/MapleMapFactory.java
Add to getMap() method, under where it parses PQ areas:
PHP:
MapleData mcData = mapData.getChildByPath("monsterCarnival");if (mcData != null) {
    MCWZData mcpqInfo = new MCWZData(mcData);
    map.setMCPQData(mcpqInfo);
    map.setRespawning(false);
}

Add to getMap() method, under where it parses mobTime:
PHP:
int team = MapleDataTool.getInt("team", life, -1);

Change addMonsterSpawn() method call to the following, using the new SpawnPoint construction we defined in MapleMap:
PHP:
map.addMonsterSpawn(monster, mobTime, team);

Add the method:
PHP:
public MapleMap instanceMap(int mapid, boolean respawns, boolean npcs) {    return instanceMap(mapid, respawns, npcs, true);
}

and

PHP:
public MapleMap instanceMap(int mapid, boolean respawns, boolean npcs, boolean reactors)
, where the code is the same as getMap but does not try to load from cache and does not store into cache.

server/maps/MapleReactor.java
Under hitReactor(), add before standard handling:
PHP:
if (c.getPlayer().getMCPQField() != null) {    c.getPlayer().getMCPQField().onGuardianHit(c.getPlayer(), this);
    return;
}

That's it for modifications made to OdinMS. The next post will contain the actual MCPQ logic and game files, as well as discussion about bytes added to packets.
 
Last edited:
Newbie Spellweaver
Joined
Sep 17, 2013
Messages
82
Reaction score
137
Create a new package called server/partyquest/mcpq, and add all following files:

MCBattlefield.java
PHP:
package net.sf.odinms.server.partyquest.mcpq;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.server.life.*;
import net.sf.odinms.server.maps.*;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.MonsterCarnivalPacket;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Keeps track of guardians and spawns in MCPQ.
 *
 * @author s4nta
 */
public class MCBattlefield {

    private MapleMap map;
    private MCWZData wzData;
    private int numGuardiansSpawned = 0;
    private int numMonstersSpawned = 0;
    // These map Guardian IDs (aka codes for status) to the guardian position.
    private Map<Integer, MCWZData.MCGuardianGenPos> redGuardianIdToPos = new HashMap<>();
    private Map<Integer, MCWZData.MCGuardianGenPos> blueGuardianIdToPos = new HashMap<>();
    // These map Reactor Object IDs to guardian objects.
    private Map<Integer, MCGuardian> redReactors = new HashMap<>();
    private Map<Integer, MCGuardian> blueReactors = new HashMap<>();
    // used for divided maps
    // we use an arraylist here for easier random element lookup.
    private List<MCWZData.MCGuardianGenPos> originalRedGuardianSpawns = new ArrayList<>();
    private List<MCWZData.MCGuardianGenPos> originalBlueGuardianSpawns = new ArrayList<>();
    // used for undivided map
    private List<MCWZData.MCGuardianGenPos> originalGuardianSpawns = new ArrayList<>();

    // used for divided maps
    // we use an arraylist here for easier random element lookup.
    private List<MCWZData.MCMobGenPos> originalRedSpawns = new ArrayList<>();
    private List<MCWZData.MCMobGenPos> originalBlueSpawns = new ArrayList<>();
    // use for undivided map
    private List<MCWZData.MCMobGenPos> originalUnifiedSpawns = new ArrayList<>();

    private List<SpawnPoint> originalSpawns = new ArrayList<>();
    private List<SpawnPoint> addedSpawns = new ArrayList<>();

    public MCBattlefield(MapleMap battleInstance) {
        this.map = battleInstance;
        fetchCarnivalData();
        getOriginalSpawnPoints();
        populateGuardianSpawns();
        populateMobSpawns();
    }

    private void fetchCarnivalData() {
        wzData = this.map.getMCPQData();
        if (wzData == null) {
            MCTracker.log("[MCPQ] Fetching carnival failed for map " + map.getId());
        }
    }

    private void getOriginalSpawnPoints() {
        for (SpawnPoint sp : this.map.getSpawnPoints()) {
            originalSpawns.add(sp);
        }
    }

    private void populateGuardianSpawns() {
        for (MCWZData.MCGuardianGenPos gpos : wzData.guardianGenPosList) {
            switch (gpos.team) {
                case 0:
                    originalRedGuardianSpawns.add(gpos);
                    break;
                case 1:
                    originalBlueGuardianSpawns.add(gpos);
                    break;
                default:
                    originalGuardianSpawns.add(gpos);
            }
        }
    }

    private void populateMobSpawns() {
        for (MCWZData.MCMobGenPos mpos : wzData.mobGenPosList) {
            switch (mpos.team) {
                case 0:
                    originalRedSpawns.add(mpos);
                    break;
                case 1:
                    originalBlueSpawns.add(mpos);
                    break;
                default:
                    originalUnifiedSpawns.add(mpos);
            }
        }
    }

    public void addSpawn(MapleCharacter chr, int num) {
        if (numMonstersSpawned > wzData.mobGenMax) {
            chr.getClient().announce(MonsterCarnivalPacket.CPQMessage(3));
            return;
        }

        MCWZData.MCSummonMob mobToSummon = wzData.summons.get(num);
        MCWZData.MCMobGenPos spawnPos = getRandomSpawnPos(chr.getMCPQTeam());

        MCField.MCTeam team = chr.getMCPQTeam();
        if (spawnPos == null) { // all positions used
            chr.getClient().announce(MonsterCarnivalPacket.CPQMessage(2));
            return;
        }

        int spendCp = mobToSummon.spendCP;
        if (spendCp > chr.getAvailableCP()) {
            readdSpawn(spawnPos, team);
            chr.getClient().announce(MonsterCarnivalPacket.CPQMessage(1));
            return;
        }

        chr.getMCPQField().loseCP(chr, spendCp);
        this.map.broadcastMessage(
                MonsterCarnivalPacket.CPQSummon(MonsterCarnival.TAB_SPAWNS, num, chr.getName()));
        numMonstersSpawned++; // TODO: AtomicInteger this

        MapleMonster monster = MapleLifeFactory.getMonster(mobToSummon.id);
        Point pos = new Point(spawnPos.x, spawnPos.y);
        SpawnPoint sp = new SpawnPoint(monster, pos, mobToSummon.mobTime, chr.getTeam());

        addedSpawns.add(sp);
        updateMonsterBuffs();
    }

    public void useSkill(MapleCharacter chr, int num) {
        if (!wzData.skills.containsKey(num)) {
            MCTracker.log("Attempting to use a null skill.");
            return;
        }
        int realSkill = wzData.skills.get(num);
        MCSkill skill = MCSkillFactory.getMCSkill(realSkill);
        // TODO: add skill cooldowns

        int spendCp = skill.getSpendCP();
        if (spendCp > chr.getAvailableCP()) {
            chr.getClient().announce(MonsterCarnivalPacket.CPQMessage(1));
            return;
        }

        MCParty teamToApply = chr.getMCPQParty().getEnemy();
        boolean success = teamToApply.applyMCSkill(skill);

        if (success) {
            chr.getMCPQField().loseCP(chr, spendCp);
            map.broadcastMessage(
                    MonsterCarnivalPacket.CPQSummon(MonsterCarnival.TAB_DEBUFF, num, chr.getName()));
        } else {
            chr.getClient().getSession().write(MonsterCarnivalPacket.CPQMessage(5));
        }
    }
    
    public void readdSpawn(MCWZData.MCMobGenPos pos, MCField.MCTeam team) {
        List<MCWZData.MCMobGenPos> lst = null;
        if (this.wzData.mapDivided) {
            if (team == MCField.MCTeam.RED) {
                lst = originalRedSpawns;
            } else if (team == MCField.MCTeam.BLUE) {
                lst = originalBlueSpawns;
            } else {
                return;
            }
        } else {
            lst = originalUnifiedSpawns;
        }
        
        if (lst == null) {
            return;
        } 
        lst.add(pos);
    }

    /**
     * Spawns a guardian, buffing all monsters on the map.
     *   [USER=2000183830]para[/USER]m chr Character that summoned the guardian.
     *   [USER=2000183830]para[/USER]m num Which guardian was spawned (F1-F9).
     */
    public void spawnGuardian(MapleCharacter chr, int num) {
        if (numGuardiansSpawned > wzData.guardianGenMax) {
            chr.getClient().announce(MonsterCarnivalPacket.CPQMessage(3));
            return;
        }

        int guardianId = wzData.guardians.get(num);
        MCGuardian guardian = MCSkillFactory.getMCGuardian(guardianId);
        if (guardian == null) {
            MCTracker.log("Attempting to spawn invalid guardian.");
            return;
        }

        MCField.MCTeam team = chr.getMCPQTeam();
        if (team == MCField.MCTeam.RED) {
            if (redGuardianIdToPos.containsKey(guardianId)) {
                chr.getClient().announce(MonsterCarnivalPacket.CPQMessage(4));
                return;
            }
        } else if (team == MCField.MCTeam.BLUE) {
            if (blueGuardianIdToPos.containsKey(guardianId)) {
                chr.getClient().announce(MonsterCarnivalPacket.CPQMessage(4));
                return;
            }
        }
        int spendCp = guardian.getSpendCP();
        if (spendCp > chr.getAvailableCP()) {
            chr.getClient().announce(MonsterCarnivalPacket.CPQMessage(1));
            return;
        }

        chr.getMCPQField().loseCP(chr, spendCp);
        this.map.broadcastMessage(
                MonsterCarnivalPacket.CPQSummon(MonsterCarnival.TAB_GUARDIAN, num, chr.getName()));
        numGuardiansSpawned++; // TODO: AtomicInteger this
        MCWZData.MCGuardianGenPos genPos = getRandomGuardianPos(team);
        Point spawnPos = new Point(genPos.x, genPos.y);

        MapleReactor reactor;
        if (team == MCField.MCTeam.RED) {
            reactor = new MapleReactor(MapleReactorFactory.getReactor(MonsterCarnival.GUARDIAN_RED),
                    MonsterCarnival.GUARDIAN_RED);
            reactor.setPosition(spawnPos);

            redGuardianIdToPos.put(num, genPos);
        } else if (team == MCField.MCTeam.BLUE){
            reactor = new MapleReactor(MapleReactorFactory.getReactor(MonsterCarnival.GUARDIAN_BLUE),
                    MonsterCarnival.GUARDIAN_BLUE);
            reactor.setPosition(spawnPos);

            blueGuardianIdToPos.put(num, genPos);
        } else {
            return;
        }

        reactor.setDelay(-1); // do not respawn
        map.spawnReactor(reactor);

        // This must take place after spawning the reactor because it assigns
        // the reactor an object id.
        if (team == MCField.MCTeam.RED) {
            redReactors.put(reactor.getObjectId(), MCSkillFactory.getMCGuardian(num));
        } else {
            blueReactors.put(reactor.getObjectId(), MCSkillFactory.getMCGuardian(num));
        }

        map.setReactorState(reactor, (byte) 1); // trigger the reactor, make it visible -.-
        updateMonsterBuffs();
    }

    public void onGuardianHit(MapleCharacter chr, MapleReactor reactor) {
        if (MonsterCarnival.DEBUG) {
            System.out.println("STATE: " + reactor.getState());
        }
        MCField.MCTeam team = chr.getMCPQTeam();
        if (team == MCField.MCTeam.RED && reactor.getId() == MonsterCarnival.GUARDIAN_RED) {
            return;
        }
        if (team == MCField.MCTeam.BLUE && reactor.getId() == MonsterCarnival.GUARDIAN_BLUE) {
            return;
        }
        reactor.setState((byte) (reactor.getState() + 1));
        map.broadcastMessage(MaplePacketCreator.triggerReactor(reactor, reactor.getState()));

        if (reactor.getState() > 3) {
            int reactorObjId = reactor.getObjectId();
            map.destroyReactor(reactorObjId);

            MCGuardian guard;
            MCWZData.MCGuardianGenPos guardianGenPos;
            if (team == MCField.MCTeam.RED) {
                // if your team is red, you are attacking blue guardians
                guard = blueReactors.remove(reactorObjId);
                guardianGenPos = blueGuardianIdToPos.remove(guard.getType());
            } else {
                guard = redReactors.remove(reactorObjId);
                guardianGenPos = redGuardianIdToPos.remove(guard.getType());
            }
            numGuardiansSpawned--;
            
            if (MonsterCarnival.DEBUG) {
                System.out.println("Removing reactor with x = " + guardianGenPos.x);
            }
            if (wzData.mapDivided) {
                if (team == MCField.MCTeam.RED) {
                    // again, here, if you destroyed a blue guardian, then you are on the red team.
                    // it is important to note that team here refers to the character's team,
                    // not the reactor's.
                    originalBlueGuardianSpawns.add(guardianGenPos);
                } else {
                    originalRedGuardianSpawns.add(guardianGenPos);
                }
            } else {
                originalGuardianSpawns.add(guardianGenPos);
            }

            if (MonsterCarnival.DEBUG) {
                System.out.println("Attempting to remove buff " + guard.getName());
            }
            updateMonsterBuffs();
        }
    }

    /**
     * Gets a guardian position for a team. That is, if the player spawning this guardian is on the
     * red team, a position corresponding to somewhere on the blue team's side will be returned.
     * This clarification does not matter for undivided maps.
     *
     *   [USER=2000183830]para[/USER]m team Team that is summoning the guardian, and also the "team" value in the WZs.
     *   [USER=850422]return[/USER] A random generated position.
     */
    private MCWZData.MCGuardianGenPos getRandomGuardianPos(MCField.MCTeam team) {
        if (this.wzData.mapDivided) {
            if (team == MCField.MCTeam.RED) {
                int randIndex = (int) Math.floor(Math.random() * this.originalRedGuardianSpawns.size());
                return originalRedGuardianSpawns.remove(randIndex);
            } else if (team == MCField.MCTeam.BLUE) {
                int randIndex = (int) Math.floor(Math.random() * this.originalBlueGuardianSpawns.size());
                return originalBlueGuardianSpawns.remove(randIndex);
            } else {
                return null;
            }
        } else {
            int randIndex = (int) Math.floor(Math.random() * this.originalGuardianSpawns.size());
            return originalGuardianSpawns.remove(randIndex);
        }
    }

    /**
     * Gets a guardian position for a team. That is, if the player spawning this guardian is on the
     * red team, a position corresponding to somewhere on the blue team's side will be returned.
     * This clarification does not matter for undivided maps.
     *
     *   [USER=2000183830]para[/USER]m team Team that is summoning the guardian, and also the "team" value in the WZs.
     *   [USER=850422]return[/USER] A random generated position.
     */
    private MCWZData.MCMobGenPos getRandomSpawnPos(MCField.MCTeam team) {
        List<MCWZData.MCMobGenPos> lst = null;
        if (this.wzData.mapDivided) {
            if (team == MCField.MCTeam.RED) {
                lst = originalRedSpawns;
            } else if (team == MCField.MCTeam.BLUE) {
                lst = originalBlueSpawns;
            } else {
                return null;
            }
        } else {
            lst = originalUnifiedSpawns;
        }
        
        if (lst == null) {
            return null;
        } 
        if (lst.size() == 0) {
            return null;
        }
        int randIndex = (int) Math.floor(Math.random() * lst.size());
        return lst.remove(randIndex);
    }

    private void updateMonsterBuffs() {
        List<MCGuardian> redGuardians = new ArrayList<>();
        List<MCGuardian> blueGuardians = new ArrayList<>();

        for (MCGuardian g : this.redReactors.values()) {
            redGuardians.add(g);
            if (MonsterCarnival.DEBUG) {
                System.out.println("update buff red " + g.getMobSkillID());
            }
        }
        for (MCGuardian g : this.blueReactors.values()) {
            blueGuardians.add(g);
            if (MonsterCarnival.DEBUG) {
                System.out.println("update buff blue " + g.getMobSkillID());
            }
        }

        for (MapleMapObject mmo : map.getAllMonsters()) {
            if (mmo.getType() == MapleMapObjectType.MONSTER) {
                MapleMonster mob = ((MapleMonster) mmo);
                mob.dispel();

                if (mob.getTeam() == MCField.MCTeam.RED.code) {
                    applyGuardians(mob, redGuardians);
                } else if (mob.getTeam() == MCField.MCTeam.BLUE.code) {
                    applyGuardians(mob, blueGuardians);
                } else {
                    MCTracker.log("[MCPQ] Attempting to give guardians to mob without team.");
                }
            }
        }
    }

    private void giveMonsterBuffs(MapleMonster mob) {
        List<MCGuardian> redGuardians = new ArrayList<>();
        List<MCGuardian> blueGuardians = new ArrayList<>();

        for (MCGuardian g : this.redReactors.values()) {
            redGuardians.add(g);
            if (MonsterCarnival.DEBUG) {
                System.out.println("update buff red " + g.getMobSkillID());
            }
        }
        for (MCGuardian g : this.blueReactors.values()) {
            blueGuardians.add(g);
            if (MonsterCarnival.DEBUG) {
                System.out.println("update buff blue " + g.getMobSkillID());
            }
        }

        if (mob.getTeam() == MCField.MCTeam.RED.code) {
            applyGuardians(mob, redGuardians);
        } else if (mob.getTeam() == MCField.MCTeam.BLUE.code) {
            applyGuardians(mob, blueGuardians);
        } else {
            MCTracker.log("[MCPQ] Attempting to give guardians to mob without team.");
        }
    }

    private void applyGuardians(MapleMonster mob, List<MCGuardian> guardians) {
        for (MCGuardian g : guardians) {
            MobSkill sk = MobSkillFactory.getMobSkill(g.getMobSkillID(), g.getLevel());
            sk.applyEffect(null, mob, true);
        }
    }

    public void spawningTask() {
        for (SpawnPoint sp : originalSpawns) {
            if (sp.shouldSpawn()) {
                MapleMonster mob = sp.spawnMonster(this.map);
                giveMonsterBuffs(mob);
            }
        }
        for (SpawnPoint sp : addedSpawns) {
            if (sp.shouldSpawn()) {
                MapleMonster mob = sp.spawnMonster(this.map);
                giveMonsterBuffs(mob);
            }
        }
    } }

MCField.java
PHP:
package net.sf.odinms.server.partyquest.mcpq;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.net.MaplePacket;
import net.sf.odinms.net.channel.ChannelServer;
import net.sf.odinms.server.MapleItemInformationProvider;
import net.sf.odinms.server.MaplePortal;
import net.sf.odinms.server.MapleStatEffect;
import net.sf.odinms.server.TimerManager;
import net.sf.odinms.server.maps.MapleMap;
import net.sf.odinms.server.maps.MapleMapItem;
import net.sf.odinms.server.maps.MapleReactor;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.MonsterCarnivalPacket;

import java.util.*;
import java.util.concurrent.ScheduledFuture;

/**
 * Keeps track of a specific field (1-6). Handles all packet broadcasting, etc.
 * @author s4nta
 */
public class MCField {

    /**
     * Different teams for MCPQ.
     */
    public enum MCTeam {
        RED(0),
        BLUE(1),
        NONE(-1);
        public final int code;

        MCTeam(int code) {
            this.code = code;
        }

        public final int getEnemyTeamCode() {
            return Math.abs(this.code - 1);
        }
    }

    /**
     * Represents the current state of the field.
     */
    public enum MCState {
        LOBBY,
        BATTLE,
        END;
    }

    /**
     * Keys to access different map instances relating to this field.
     */
    public enum MCMaps {
        LOBBY(0),
        BATTLEFIELD(1),
        RESURRECT(2),
        VICTORY(3),
        DEFEAT(4),
        NONE(-1);
        private final int code;

        MCMaps(int code) {
            this.code = code;
        }

        public static MCMaps getByCode(int code) {
            for (MCMaps m : values()) {
                if (m.code == code) {
                    return m;
                }
            }
            return NONE;
        }
    }

    private int arena;
    private ChannelServer cserv;
    private MCParty red, blue;
    private List<MCParty> requests = new ArrayList<>();
    private MCState state;
    private Map<MCMaps, MapleMap> mapInstances = new HashMap<>();
    private long startTime;
    private MCBattlefield battlefield;

    // Timer Tasks
    private ScheduledFuture<?> acceptRequestsTask, validateRoomTask, startBattleTask,
            validateBattleTask, runBattleTask, endBattleTask,
            spawnMonstersTask;

    public MCField(int arena, ChannelServer cserv, MCParty red, MCParty blue) {
        this.arena = arena;
        this.cserv = cserv;
        this.red = red;
        this.blue = blue;
        this.state = MCState.LOBBY;
    }

    public boolean isFull() {
        if (MonsterCarnival.DEBUG) {
            return false;
        }
        return this.red != null && this.blue != null;
    }

    public boolean needsRequest() {
        return this.red != null && this.blue == null;
    }

    /**
     * Resets the state of the field, warping out players and resetting tasks.
     *   [USER=2000183830]para[/USER]m warpPlayers Warp out players or not
     */
    public void deregister(boolean warpPlayers) {
        if (warpPlayers) {
            if (this.red != null) {
                this.red.deregisterPlayers();
            }
            if (this.blue != null) {
                this.blue.deregisterPlayers();
            }
        }


        this.red = null;
        this.blue = null;
        this.requests.clear();
        this.state = MCState.LOBBY;

        if (this.acceptRequestsTask != null) {
            this.acceptRequestsTask.cancel(true);
            this.acceptRequestsTask = null;
        }

        if (this.validateRoomTask != null) {
            this.validateRoomTask.cancel(true);
            this.validateRoomTask = null;
        }

        if (this.startBattleTask != null) {
            this.startBattleTask.cancel(true);
            this.startBattleTask = null;
        }

        if (this.endBattleTask != null) {
            this.endBattleTask.cancel(true);
            this.endBattleTask = null;
        }

        if (this.runBattleTask != null) {
            this.runBattleTask.cancel(true);
            this.runBattleTask = null;
        }

        if (this.validateBattleTask != null) {
            this.validateBattleTask.cancel(true);
            this.validateBattleTask = null;
        }

        if (this.spawnMonstersTask != null) {
            this.spawnMonstersTask.cancel(true);
            this.spawnMonstersTask = null;
        }

        for (MCMaps mapType : MCMaps.values()) {
            this.mapInstances.remove(mapType);
        }
    }

    public MCParty getRed() {
        return red;
    }

    public MCParty getBlue() {
        return blue;
    }

    public void announce(MaplePacket pkt) {
        if (this.red != null) {
            red.broadcast(pkt);
        } else {
            MCTracker.log("[MCPQ] Trying to announce packet to red when it is null.");
        }

        if (this.blue != null) {
            blue.broadcast(pkt);
        } else {
            MCTracker.log("[MCPQ] Trying to announce packet to blue when it is null.");
        }
    }

    /**
     * Gets a string representing the status of this room for the Spiegelmann NPC.
     *   [USER=850422]return[/USER] String representing the room's status.
     */
    public String getStatus() {
        if (isFull()) {
            return "";
        }
        if (this.state != MCState.LOBBY) {
            return "";
        }
        String waitingParty = "";
        if (this.red != null) {
            String fmt = " <Party Size: %d, Average Level: %d>";
            waitingParty = String.format(fmt, this.red.getSize(), this.red.getAverageLevel());
        }
        String fmt = "#L%d#Carnival Field %d%s#l\r\n";
        return String.format(fmt, this.arena, this.arena, waitingParty);
    }

    /**
     * Attempts to register a party in this field. If success, then all players in the party
     * will be warped to the waiting lobby. All players in the party will also have relevant
     * CPQ information assigned to them.
     *
     *   [USER=2000183830]para[/USER]m party The party to register.
     */
    public void register(MCParty party, MCTeam team) {
        if (this.red == null && team == MCTeam.RED) {
            party.setTeam(team);
            this.red = party;
        } else if (this.blue == null && team == MCTeam.BLUE) {
            party.setTeam(team);
            this.blue = party;
        } else {
            MCTracker.log("Attempting to register party when team is already set.");
            return;
        }
        party.setField(this);
        party.updatePlayers();
        party.warp(MCMaps.LOBBY);
        onPartyRegistered(party, team);
    }

    /**
     * Sends a challenge to the party in this field.
     *
     *   [USER=2000183830]para[/USER]m party The party requesting a challenge.
     */
    public void request(MCParty party) {
        if (this.red == null) {
            MCTracker.log("Attempting to request when waiting team is null.");
            this.deregister(true);
            return;
        }
        requests.add(party);
        this.red.notice("[MCPQ] A challenge has arrived! Click on the Assistant in your room to view it.");
    }

    /**
     * Accepts a challenge from a team.
     *   [USER=2000183830]para[/USER]m index Index of team in requests.
     *   [USER=850422]return[/USER] 1 if the challenge was accepted successfully, 0 otherwise.
     */
    public int acceptRequest(int index) {
        MCParty toAccept = this.requests.get(index);
        register(toAccept, MCTeam.BLUE);
        return 1;
    }

    /**
     * Checks for pending requests.
     *
     *   [USER=850422]return[/USER] True if there are pending requests.
     */
    public boolean hasPendingRequests() {
        return requests.size() > 0;
    }

    /**
     * Gets a pending list of requests, formatted for use in a NPC. Also cleans up the requests,
     * getting rid of MCParties that no longer exist.
     *
     *   [USER=850422]return[/USER] Formatted list of requests for NPC.
     */
    public String getNPCRequestString() {
        StringBuilder sb = new StringBuilder("Here is the list of pending requests:\r\n\r\n#b");
        for (MCParty pty : requests) {
            if (!pty.exists()) {
                continue;
            }
            sb.append("#L").append(requests.indexOf(pty)).append("#");
            String fmt = "<Party Size: %d, Average Level: %d>";
            fmt = String.format(fmt, pty.getSize(), pty.getAverageLevel());
            sb.append(fmt);
            sb.append("#l\r\n");
        }
        return sb.toString();
    }

    /**
     * Starts a R3 minute waiting timer in the lobby for teams to accept requests.
     * If the timer completes the countdown, teams are deregistered and sent back to the lobby.
     */
    private void startLobbyTask(MCParty host) {
        host.clock(MonsterCarnival.TIME_LOBBYWAIT);

        this.acceptRequestsTask = TimerManager.getInstance().schedule(
                new AcceptingRequestsTask(this, host),
                1000 * MonsterCarnival.TIME_LOBBYWAIT); // 3 minutes
        this.validateRoomTask = TimerManager.getInstance().register(
                new ValidateLobbyTask(this),
                1000, 1000); // repeat every second
    }

    /**
     * Event handling for when a team registers.
     *
     *   [USER=2000183830]para[/USER]m party Party that registers.
     *   [USER=2000183830]para[/USER]m team Team of party.
     */
    private void onPartyRegistered(MCParty party, MCTeam team) {
        if (team == MCTeam.RED) {
            startLobbyTask(party);
        }
        if (team == MCTeam.BLUE) { // both teams are in
            this.validateRoomTask.cancel(true);
            this.acceptRequestsTask.cancel(true);
            blue.clock(10);
            red.clock(10);

            this.startBattleTask = TimerManager.getInstance().schedule(
                    new GoBattlefieldTask(this), 1000 * 10); // 10 seconds

            red.notice("[MCPQ] The Carnival PQ will start in 10 seconds!");
            blue.notice("[MCPQ] The Carnival PQ will start in 10 seconds!");
        }
    }

    /**
     * Warps both parties in the field to the battlefield map.
     */
    private void goBattle() {
        MapleMap map = getMap(MCMaps.BATTLEFIELD);
        if (MonsterCarnival.DEBUG)
            System.out.println("warping to battle " + map + " " + map.getId());
        if (red != null) {
            red.warp(map, "red00");
        } else {
            MCTracker.log("[MCPQ] Trying to warp red party when it is null.");
        }

        if (blue != null) {
            blue.warp(map, "blue00");
        } else {
            MCTracker.log("[MCPQ] Trying to warp blue party when it is null.");
        }

        red.clock(MonsterCarnival.TIME_PREBATTLE);
        blue.clock(MonsterCarnival.TIME_PREBATTLE);

        red.notice("[MCPQ] The battle will start in 10 seconds!");
        blue.notice("[MCPQ] The battle will start in 10 seconds!");

        startBattleTask = TimerManager.getInstance().schedule(new BeginCarnivalTask(this),
                1000 * MonsterCarnival.TIME_PREBATTLE); // 10 seconds

        validateBattleTask = TimerManager.getInstance().register(new ValidateBattlefieldTask(this),
                1000, 500); // check every second

        battlefield = new MCBattlefield(getMap(MCMaps.BATTLEFIELD));

        red.setEnemy(blue);
        blue.setEnemy(red);
    }

    private void beginCarnival() {
        red.clock(MonsterCarnival.TIME_BATTLE);
        blue.clock(MonsterCarnival.TIME_BATTLE);
        startTime = System.currentTimeMillis();

        getMap(MCMaps.BATTLEFIELD).broadcastMessage(
                MaplePacketCreator.serverNotice(6, "[MCPQ] You have 10 minutes to kill monsters!"));

        endBattleTask = TimerManager.getInstance().schedule(new EndBattleTask(this),
                1000 * MonsterCarnival.TIME_BATTLE);
        spawnMonstersTask = TimerManager.getInstance().register(new SpawnTask(this.battlefield),
                1000 * 5);

    }

    public void endBattle(MCParty winner, MCParty loser) {
        endBattle(winner, loser, false);
    }

    public void endBattle(MCParty winner, MCParty loser, boolean abnormal) {
        // TODO: Abnormal win codes to prevent exploits

        validateBattleTask.cancel(true);
        spawnMonstersTask.cancel(true);

        MCWZData cpqData = this.getMap(MCMaps.BATTLEFIELD).getMCPQData();
        String effectWin = cpqData.effectWin;
        String effectLose = cpqData.effectLose;
        String soundWin = cpqData.soundWin;
        String soundLose = cpqData.soundLose;

        winner.broadcast(MaplePacketCreator.showEffect(effectWin));
        winner.broadcast(MaplePacketCreator.playSound(soundWin));
        loser.broadcast(MaplePacketCreator.showEffect(effectLose));
        loser.broadcast(MaplePacketCreator.playSound(soundLose));

        this.getMap(MCMaps.BATTLEFIELD).killAllMonsters(false);
        this.getMap(MCMaps.BATTLEFIELD).clearDrops();
        this.deregister(false);

        TimerManager.getInstance().schedule(
                new WarpEndBattleTask(this, winner, loser),
                1000 * 3);
    }

    /**
     * Handles CP gain and packet updates when a monster is killed.
     *   [USER=2000183830]para[/USER]m chr Character that kills the monster.
     *   [USER=2000183830]para[/USER]m cp CP gained.
     */
    public void monsterKilled(MapleCharacter chr, int cp) {
        if (MonsterCarnival.DEBUG) {
            // System.out.println(chr.getName() + " killed for +" + cp + " CP");
        }
        // TODO: Personal stats for CP gain
        this.gainCP(chr, cp);
    }

    /**
     * Handles game logic and packet broadcasting for CP gain.
     * Broadcasts personal CP update to chr, and broadcasts party CP update
     * to the entire field.
     *
     *   [USER=2000183830]para[/USER]m chr Character that gains CP.
     *   [USER=2000183830]para[/USER]m cp CP gained.
     */
    public void gainCP(MapleCharacter chr, int cp) {
        if (cp < 0) {
            MCTracker.log("[MCPQ] Adding negative CP.");
            if (MonsterCarnival.DEBUG) {
                System.out.println("Adding negative CP: stacktrace");
                new Exception().printStackTrace();
            }
        }
        MCParty pty = chr.getMCPQParty();
        chr.gainCP(cp);
        pty.gainCP(cp);
        chr.getClient().announce(MonsterCarnivalPacket.updatePersonalCP(chr));
        this.announce(MonsterCarnivalPacket.updatePartyCP(pty));
    }

    /**
     * Subtracts from available CP while leaving total CP untouched.
     *
     *   [USER=2000183830]para[/USER]m chr Character that loses CP.
     *   [USER=2000183830]para[/USER]m cp CP lost (should be positive number).
     */
    public void loseCP(MapleCharacter chr, int cp) {
        if (cp < 0) {
            MCTracker.log("[MCPQ] Losing negative CP.");
            if (MonsterCarnival.DEBUG) {
                System.out.println("Adding negative CP: stacktrace");
                new Exception().printStackTrace();
            }
        }
        MCParty pty = chr.getMCPQParty();
        chr.loseCP(cp);
        pty.loseCP(cp);
        chr.getClient().announce(MonsterCarnivalPacket.updatePersonalCP(chr));
        this.announce(MonsterCarnivalPacket.updatePartyCP(pty));
    }

    /**
     * Handles a player looting an item.
     *   [USER=2000183830]para[/USER]m player Player that picked up the object.
     *   [USER=2000183830]para[/USER]m mapitem Object picked up.
     *
     *   [USER=850422]return[/USER] True if pickup was successful, false otherwise.
     */
    public boolean onItemPickup(MapleCharacter player, MapleMapItem mapitem) {
        if (mapitem == null) {
            MCTracker.log("[MCPQ] Attempting to loot null object.");
            return false;
        }
        int itemid = mapitem.getItem().getItemId();
        if (!MonsterCarnival.isCPQConsumeItem(itemid)) {
            return false;
        }
        MCParty pty = player.getMCPQParty();
        MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
        MapleStatEffect itemEffect = ii.getItemEffect(itemid);
        if (!itemEffect.isConsumeOnPickup()) {
            return false;
        }

        if (itemEffect.isParty()) {
            for (MapleCharacter chr : pty.getMembers()) {
                if (chr.getHp() > 0) {
                    itemEffect.applyTo(chr);
                }
            }
        } else { // Single Target Item
            itemEffect.applyTo(player);
        } 
        // Status items
        if (itemEffect.getNuffSkill() != -1) {
            MCSkill debuff = MCSkillFactory.getMCSkill(itemEffect.getNuffSkill());
            if (debuff == null) {
                MCTracker.log("[MCPQ] debuff skill is null " + itemEffect.getNuffSkill());
                return false;
            }

            pty.getEnemy().applyMCSkill(debuff);
        }

        if (itemEffect.getCP() > 0) {
            this.gainCP(player, itemEffect.getCP());
        }

        return true;
    }

    public void onPlayerRespawn(MapleCharacter player) {
        int cpLoss = Math.min(player.getAvailableCP(), MonsterCarnival.CP_LOSS_ON_DEATH);
        this.announce(MonsterCarnivalPacket.CPQDied(player, cpLoss));
        this.loseCP(player, cpLoss);
        player.addMPHP(30000, 30000);
        player.changeMap(this.getMap(MCMaps.RESURRECT), this.getMap(MCMaps.RESURRECT).getPortal(0));
        player.getClient().getSession().write(MaplePacketCreator.getClock(getTimeRemaining()));
        player.getClient().getSession().write(MonsterCarnivalPacket.startCPQ(player));
    }

    public void onPlayerDisconnected(MapleCharacter player) {
        MCParty pty = player.getMCPQParty();
        if (pty != null) {
            pty.removePlayer(player);
        } else {
            MCTracker.log("[MCPQ] Attempting to run player disconnect event when party is null for character " + player.getName());
        }
    }

    public void onAddSpawn(MapleCharacter chr, int num) {
        if (this.battlefield != null) {
            battlefield.addSpawn(chr, num);
        } else {
            MCTracker.log("[MCPQ] Summoning guardian with null battlefield.");
        }
    }

    public void onUseSkill(MapleCharacter chr, int num) {
        if (this.battlefield != null) {
            battlefield.useSkill(chr, num);
        } else {
            MCTracker.log("[MCPQ] Summoning guardian with null battlefield.");
        }
    }

    public void onGuardianSummon(MapleCharacter chr, int num) {
        if (this.battlefield != null) {
            battlefield.spawnGuardian(chr, num);
        } else {
            MCTracker.log("[MCPQ] Summoning guardian with null battlefield.");
        }
    }

    public void onGuardianHit(MapleCharacter chr, MapleReactor reactor) {
        if (this.battlefield != null) {
            battlefield.onGuardianHit(chr, reactor);
        } else {
            MCTracker.log("[MCPQ] Hitting reactor with null battlefield.");
        }
    }

    public void onRevive(MapleCharacter player) {
        MCTeam team = player.getMCPQTeam();
        MaplePortal portal;
        if (team == MCTeam.RED) {
            portal = getMap(MCMaps.BATTLEFIELD).getPortal("red_revive");
        } else {
            portal = getMap(MCMaps.BATTLEFIELD).getPortal("blue_revive");
        }

        player.changeMap(getMap(MCMaps.BATTLEFIELD), portal);
        player.getClient().getSession().write(MaplePacketCreator.getClock(getTimeRemaining()));
        player.getClient().getSession().write(MonsterCarnivalPacket.startCPQ(player));
    }

    public int getTimeRemaining() {
        // TODO: add support for setting an explicit endTime instead of using the hack with MonsterCarnival variables
        return (int) ((startTime + 1000 * MonsterCarnival.TIME_BATTLE) - System.currentTimeMillis()) / 1000;
    }


    // Map Instances

    /**
     * Returns the map instance for a requested map. Creates a new map instance if unavailable.
     *   [USER=2000183830]para[/USER]m type Map instance to return.
     *   [USER=850422]return[/USER] The instanced map.
     */
    public MapleMap getMap(MCMaps type) {
        if (this.mapInstances.containsKey(type)) {
            return this.mapInstances.get(type);
        }
        return createInstanceMap(type);
    }

    /**
     * Attempts to create an instanced map, based on the type passed in. Also creates a mapping in
     * this.mapInstances.
     *
     *   [USER=2000183830]para[/USER]m type Type of map to generate.
     *   [USER=850422]return[/USER] MapleMap for the instanced map if type is supported, otherwise null.
     */
    private MapleMap createInstanceMap(MCMaps type) {
        int mapid = -1;
        switch (type) {
            case LOBBY:
                mapid = MonsterCarnival.getLobbyMap(this.arena);
                break;
            case BATTLEFIELD:
                mapid = MonsterCarnival.getBattleFieldMap(this.arena);
                break;
            case RESURRECT:
                mapid = MonsterCarnival.getResurrectionMap(this.arena);
                break;
            case VICTORY:
                mapid = MonsterCarnival.getVictoriousMap(this.arena);
                break;
            case DEFEAT:
                mapid = MonsterCarnival.getDefeatedMap(this.arena);
                break;
        }
        if (mapid == -1) return null;
        MapleMap mapInstance = this.cserv.getMapFactory().instanceMap(mapid, true, true);
        this.mapInstances.put(type, mapInstance);
        return mapInstance;
    }

    // Timer Tasks

    public class ValidateLobbyTask implements Runnable {

        private final MCField field;

        /**
         * Timer task to ensure all players are on the right field.
         * If anything is wrong with the parties, the field is deregistered.
         *   [USER=2000183830]para[/USER]m field Field to run the validation task on.
         */
        public ValidateLobbyTask(MCField field) {
            this.field = field;
        }

        @Override
        public void run() {
            if (this.field.red == null) {
                this.field.deregister(true);
                return;
            }
            for (MapleCharacter c : field.red.getMembers()) {
                if (c.getMap() != field.getMap(MCMaps.LOBBY)) {
                    this.field.deregister(true);
                    return;
                }
            }
            if (this.field.blue != null) {
                for (MapleCharacter c : field.blue.getMembers()) {
                    if (c.getMap() != field.getMap(MCMaps.LOBBY)) {
                        this.field.deregister(true);
                        return;
                    }
                }
            }
        }
    }

    public class ValidateBattlefieldTask implements Runnable {

        private final MCField field;

        /**
         * Timer task to ensure all players are on the right field.
         * If anything is wrong with the parties, the field is deregistered.
         *   [USER=2000183830]para[/USER]m field Field to run the validation task on.
         */
        public ValidateBattlefieldTask(MCField field) {
            this.field = field;
        }

        @Override
        public void run() {
            if (this.field.red == null || field.red.getSize() == 0) {
                MCTracker.log("[MCPQ] Red team null when validating battlefield");
                field.endBattle(blue, red);
                return;
            }
            Collection<MapleCharacter> members = Collections.unmodifiableCollection(field.red.getMembers());
            for (MapleCharacter c : members) {
                if (c.getMap() != field.getMap(MCMaps.BATTLEFIELD) &&
                        c.getMap() != field.getMap(MCMaps.RESURRECT)) {
                    this.field.announce(MonsterCarnivalPacket.leaveCPQ(MCTeam.RED.code, c.getName()));
                    red.removePlayer(c); // TODO: fix concurrent modification
                }
                if (c.getMap() == field.getMap(MCMaps.BATTLEFIELD) && c.isDead()) {
                    this.field.onPlayerRespawn(c);
                }
            }
            if (this.field.blue == null || field.blue.getSize() == 0) {
                MCTracker.log("[MCPQ] Blue team null when validating battlefield");
                field.endBattle(red, blue);
                return;
            }
            members = Collections.unmodifiableCollection(field.blue.getMembers());
            for (MapleCharacter c : members) {
                if (c.getMap() != field.getMap(MCMaps.BATTLEFIELD) &&
                        c.getMap() != field.getMap(MCMaps.RESURRECT)) {
                    this.field.announce(MonsterCarnivalPacket.leaveCPQ(MCTeam.BLUE.code, c.getName()));
                    blue.removePlayer(c);
                }
                if (c.getMap() == field.getMap(MCMaps.BATTLEFIELD) && c.isDead()) {
                    this.field.onPlayerRespawn(c);
                }
            }
        }
    }

    public class AcceptingRequestsTask implements Runnable {

        private final MCField field;
        private final MCParty host;

        /**
         * Runs a task that counts down for 3 minutes, then warps the hosting party out.
         *
         *   [USER=2000183830]para[/USER]m field Field to accept requests on.
         *   [USER=2000183830]para[/USER]m host Hosting party that will be warped out if they do not accept a request
         *             within 3 minutes.
         */
        public AcceptingRequestsTask(MCField field, MCParty host) {
            this.field = field;
            this.host = host;
        }

        @Override
        public void run() {
            Collection<MapleCharacter> chrs = this.host.getMembers();
            for (MapleCharacter c : chrs) {
                c.changeMap(MonsterCarnival.MAP_LOBBY);
            }
            this.field.deregister(true);
        }
    }

    public class GoBattlefieldTask implements Runnable {

        private final MCField field;

        public GoBattlefieldTask(MCField field) {
            this.field = field;
        }

        @Override
        public void run() {
            field.goBattle();

            field.red.startBattle();
            field.blue.startBattle();

            field.state = MCState.BATTLE;
        }
    }

    public class BeginCarnivalTask implements Runnable {

        private final MCField field;

        public BeginCarnivalTask(MCField field) {
            this.field = field;
        }

        @Override
        public void run() {
            MapleMap map = field.getMap(MCMaps.BATTLEFIELD);
            map.beginSpawning();
            field.beginCarnival();
        }
    }

    /* I have no idea why this doesn't work normally :/ */
    public class SpawnTask implements Runnable {

        private final MCBattlefield battleMap;

        public SpawnTask(MCBattlefield field) {
            this.battleMap = field;
        }

        @Override
        public void run() {
            // TODO: adjust spawn rates based on cp
            battleMap.spawningTask();
        }
    }

    public class EndBattleTask implements Runnable {
        private final MCField field;

        public EndBattleTask(MCField field) {
            this.field = field;
        }

        @Override
        public void run() {
            MCParty winner, loser;
            if (field.red.getTotalCP() > field.blue.getTotalCP()) {
                winner = field.red;
                loser = field.blue;
            } else if (field.red.getTotalCP() < field.blue.getTotalCP()) {
                winner = field.blue;
                loser = field.red;
            } else {
                // if tied: random chance
                // TODO: proper extension of time
                if (Math.random() < .5) {
                    winner = field.red;
                    loser = field.blue;
                } else {
                    winner = field.blue;
                    loser = field.red;
                }
            }

            field.state = MCState.END;
            field.endBattle(winner, loser);
        }
    }

    public class WarpEndBattleTask implements Runnable {

        private final MCField field;
        private final MCParty winner, loser;

        public WarpEndBattleTask(MCField field, MCParty winner, MCParty loser) {
            this.field = field;
            this.winner = winner;
            this.loser = loser;
        }

        @Override
        public void run() {
            winner.warp(field.getMap(MCMaps.VICTORY));
            loser.warp(field.getMap(MCMaps.DEFEAT));
        }
    } }

MCGuardian.java
PHP:
package net.sf.odinms.server.partyquest.mcpq;
/**
 * Object representing MCGuardian in Skill.wz/MCGuardian.img.
 *
 * @author s4nta
 */
public class MCGuardian {
    private final int type, spendCP, mobSkillID, level;
    private String name, desc;

    public MCGuardian(int type, int spendCP, int mobSkillID, int level) {
        this.type = type;
        this.mobSkillID = mobSkillID;
        this.level = level;
        this.spendCP = spendCP;
    }

    public int getType() {
        return type;
    }

    public int getSpendCP() {
        return spendCP;
    }

    public int getMobSkillID() {
        return mobSkillID;
    }

    public int getLevel() {
        return level;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

MCParty.java
PHP:
package net.sf.odinms.server.partyquest.mcpq;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.client.MapleDisease;
import net.sf.odinms.net.MaplePacket;
import net.sf.odinms.net.channel.ChannelServer;
import net.sf.odinms.net.world.MapleParty;
import net.sf.odinms.net.world.MaplePartyCharacter;
import net.sf.odinms.server.life.MobSkill;
import net.sf.odinms.server.life.MobSkillFactory;
import net.sf.odinms.server.maps.MapleMap;
import net.sf.odinms.server.partyquest.mcpq.MCField.MCTeam;
import net.sf.odinms.tools.MaplePacketCreator;
import net.sf.odinms.tools.MonsterCarnivalPacket;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

/**
 * Provides an interface for Monster Carnival-specific party methods and variables.
 *
 * @author s4nta
 */
public class MCParty {

    private MapleParty party;
    private List<MapleCharacter> characters = new ArrayList<>();
    private int availCP = 0;
    private int totalCP = 0;
    private MCField.MCTeam team = MCField.MCTeam.NONE;
    private MCField field;
    private MCParty enemy;

    public MCParty(MapleParty party) {
        this.party = party;
        for (MaplePartyCharacter chr : party.getMembers()) {
            if (!chr.isOnline()) continue;
            MapleCharacter c = ChannelServer.getInstance(chr.getChannel()).getPlayerStorage().getCharacterById(chr.getId());

            characters.add(c);
        }
    }

    public int getSize() {
        return this.characters.size();
    }

    /**
     * Checks if the underlying MapleParty still exists in the same way it did when it was created.
     * That is, if there were no players who left the party.
     *
     * [USER=850422]return[/USER] True if the underlying MapleParty still exists in its original format.
     */
    public boolean exists() {
        Collection<MapleCharacter> members = getMembers();
        for (MapleCharacter chr : members) {
            if (chr.getParty() == null || chr.getParty() != this.party) {
                return false;
            }
        }
        return true;
    }

    public int getAverageLevel() {
        int sum = 0, num = 0;
        for (MapleCharacter chr : getMembers()) {
            sum += chr.getLevel();
            num += 1;
        }
        return sum / num;
    }

    public boolean checkLevels() {
        if (MonsterCarnival.DEBUG) {
            return true;
        }
        for (MapleCharacter chr : getMembers()) {
            int lv = chr.getLevel();
            if (lv < MonsterCarnival.MIN_LEVEL || lv > MonsterCarnival.MAX_LEVEL) {
                return false;
            }
        }
        return true;
    }

    public boolean checkChannels() {
        if (MonsterCarnival.DEBUG) {
            return true;
        }
        for (MapleCharacter chr : getMembers()) {
            if (chr.getClient().getChannel() != party.getLeader().getChannel()) return false;
        }
        return true;
    }

    public boolean checkMaps() {
        if (MonsterCarnival.DEBUG) {
            return true;
        }
        for (MapleCharacter chr : getMembers()) {
            if (chr.getMapId() != MonsterCarnival.MAP_LOBBY) return false;
        }
        return true;
    }

    public void warp(int map) {
        for (MapleCharacter chr : getMembers()) {
            chr.changeMap(map);
        }
    }

    public void warp(MapleMap map) {
        for (MapleCharacter chr : getMembers()) {
            chr.changeMap(map, map.getPortal(0));
        }
    }

    public void warp(MapleMap map, String portal) {
        for (MapleCharacter chr : getMembers()) {
            chr.changeMap(map, map.getPortal(portal));
        }
    }

    public void warp(MCField.MCMaps type) {
        MapleMap m = this.field.getMap(type);
        for (MapleCharacter chr : getMembers()) {
            chr.changeMap(m, m.getPortal(0));
        }
    }

    public void clock(int secs) {
        for (MapleCharacter chr : getMembers()) {
            chr.getClient().announce(MaplePacketCreator.getClock(secs));
        }
    }

    public void notice(String msg) {
        broadcast(MaplePacketCreator.serverNotice(6, msg));
    }

    public void broadcast(MaplePacket pkt) {
        for (MapleCharacter chr : getMembers()) {
            chr.getClient().announce(pkt);
        }
    }

    public void broadcast(byte[] pkt) {
        for (MapleCharacter chr : getMembers()) {
            chr.getClient().announce(pkt);
        }
    }

    /**
     * Sets MCPQTeam, MCPQParty, and MCPQField for a given character.
     * [USER=2000183830]para[/USER]m chr Character to update.
     */
    public void updatePlayer(MapleCharacter chr) {
        chr.setMCPQTeam(this.team);
        chr.setMCPQParty(this);
        chr.setMCPQField(this.field);
    }

    /**
     * Sets MCPQTeam, MCPQParty, and MCPQ field for all characters in the party.
     * Unlike deregisterPlayers, this method does NOT warp players to the lobby map.
     */
    public void updatePlayers() {
        for (MapleCharacter chr : getMembers()) {
            this.updatePlayer(chr);
        }
    }

    /**
     * Resets MCPQ variables for a given character.
     * [USER=2000183830]para[/USER]m chr Character to reset.
     */
    public static void deregisterPlayer(MapleCharacter chr) {
        chr.setMCPQTeam(MCTeam.NONE);
        chr.setMCPQParty(null);
        chr.setMCPQField(null);

        chr.setAvailableCP(0);
        chr.setTotalCP(0);
    }

    /**
     * Resets MCPQ variables for all characters in the party.
     * Unlike updatePlayers, this method DOES warp players to the lobby map.
     */
    public void deregisterPlayers() {
        for (MapleCharacter chr : getMembers()) {
            MCParty.deregisterPlayer(chr);
            chr.changeMap(MonsterCarnival.MAP_EXIT);
        }
    }

    public void removePlayer(MapleCharacter chr) {
        characters.remove(chr);
        deregisterPlayer(chr);
    }

    public void startBattle() {
        for (MapleCharacter chr : characters) {
            chr.getClient().getSession().write(MonsterCarnivalPacket.startCPQ(chr));
        }
    }

    /**
     * Uses some amount of available CP.
     * [USER=2000183830]para[/USER]m use A positive integer to be subtracted from available CP.
     */
    public void loseCP(int use) {
        // TODO: locks?
        if (use < 0) {
            System.err.println("Attempting to use negative CP.");
        }
        this.availCP -= use;
    }

    public void gainCP(int gain) {
        // TODO: locks?
        this.availCP += gain;
        this.totalCP += gain;
    }

    public MCParty getEnemy() {
        return enemy;
    }

    public void setEnemy(MCParty enemy) {
        this.enemy = enemy;
    }

    /**
     * Applies a MCSkill to the entire team. This is used on the team's own players
     * because it is called when the enemy team uses a debuff/cube of darkness.
     * [USER=2000183830]para[/USER]m skill Skill to apply.
     * [USER=850422]return[/USER] True if skill was applied, false otherwise.
     */
    public boolean applyMCSkill(MCSkill skill) {
        MobSkill s = MobSkillFactory.getMobSkill(skill.getMobSkillID(), skill.getLevel());
        MapleDisease disease = MapleDisease.getType(skill.getMobSkillID());
        if (disease == null) {
            disease = MapleDisease.DARKNESS;
            s = MobSkillFactory.getMobSkill(121, 6); // HACK: darkness
        } else if (disease == MapleDisease.POISON) {
            return false;
        }

        // We only target players on the battlefield map.
        if (skill.getTarget() == 2) {
            for (MapleCharacter chr : getMembers()) {
                if (MonsterCarnival.isBattlefieldMap(chr.getMapId())) {
                    chr.giveDebuff(disease, s, true);
                }
            }
            return true;
        } else {
            if (getRandomMember() != null) {
                getRandomMember().giveDebuff(disease, s, true);
                return true;
            } else {
                return false;
            }
        }
    }

    public void setField(MCField field) {
        this.field = field;
    }

    public void setTeam(MCTeam newTeam) {
        this.team = newTeam;
    }

    public MCTeam getTeam() {
        return team;
    }

    /**
     * Returns a collection of online members in the party.
     * [USER=850422]return[/USER] Online MCParty members.
     */
    public Collection<MapleCharacter> getMembers() {
        return this.characters;
    }

    public MapleCharacter getRandomMember() {
        List<MapleCharacter> chrsOnMap = new ArrayList<>();
        for (MapleCharacter chr : this.characters) {
            if (MonsterCarnival.isBattlefieldMap(chr.getMapId())) {
                chrsOnMap.add(chr);
            }
        }
        if (chrsOnMap.isEmpty()) {
            return null;
        }
        return chrsOnMap.get(new Random().nextInt(chrsOnMap.size()));
    }

    public int getAvailableCP() {
        return availCP;
    }

    public int getTotalCP() {
        return totalCP;
    }
}

MCSkill.java
PHP:
package net.sf.odinms.server.partyquest.mcpq;
/**
 * Object representing MCSkill in Skill.wz/MCSkill.img.
 *
 * @author s4nta
 */
public class MCSkill {
    private final int id, target, mobSkillID, level, spendCP;
    private String name, desc;

    public MCSkill(int id, int target, int mobSkillID, int level, int spendCP) {
        this.id = id;
        this.target = target;
        this.mobSkillID = mobSkillID;
        this.level = level;
        this.spendCP = spendCP;
    }

    public int getId() {
        return id;
    }

    public int getTarget() {
        return target;
    }

    public int getMobSkillID() {
        return mobSkillID;
    }

    public int getLevel() {
        return level;
    }

    public int getSpendCP() {
        return spendCP;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

MCSkillFactory.java
PHP:
package net.sf.odinms.server.partyquest.mcpq;
import net.sf.odinms.provider.MapleData;
import net.sf.odinms.provider.MapleDataProvider;
import net.sf.odinms.provider.MapleDataProviderFactory;
import net.sf.odinms.provider.MapleDataTool;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Handles parsing of MCGuardian and MCSkill inside of Skill.wz.
 * Based off of MobSkillFactory.
 * @author s4nta
 */
public class MCSkillFactory {

    private static Map<Integer, MCSkill> mcSkills = new HashMap<>();
    private static Map<Integer, MCGuardian> mcGuardians = new HashMap<>();
    private static MapleDataProvider dataSource = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("net.sf.odinms.wzpath") + "/Skill.wz"));
    private static MapleData mcSkillRoot = dataSource.getData("MCSkill.img");
    private static MapleData mcGuardianRoot = dataSource.getData("MCGuardian.img");
    private static ReentrantReadWriteLock skillLock = new ReentrantReadWriteLock();
    private static ReentrantReadWriteLock guardianLock = new ReentrantReadWriteLock();

    public static MCSkill getMCSkill(int skillId) {
        skillLock.readLock().lock();
        try {
            MCSkill ret = mcSkills.get(skillId);
            if (ret != null) {
                return ret;
            }
        } finally {
            skillLock.readLock().unlock();
        }
        skillLock.writeLock().lock();
        try {
            MCSkill ret;
            ret = mcSkills.get(skillId);
            if (ret == null) {
                MapleData skillData = mcSkillRoot.getChildByPath(String.valueOf(skillId));
                if (skillData != null) {
                    int target = MapleDataTool.getInt("target", skillData, 0);
                    int spendCP = MapleDataTool.getInt("spendCP", skillData, 0);
                    int mobSkillID = MapleDataTool.getInt("mobSkillID", skillData, 0);
                    int level = MapleDataTool.getInt("level", skillData, 0);
                    ret = new MCSkill(skillId, target, mobSkillID, level, spendCP);

                    if (MonsterCarnival.DEBUG) {
                        String name = MapleDataTool.getString("name", skillData, "");
                        String desc = MapleDataTool.getString("desc", skillData, "");
                        ret.setName(name);
                        ret.setDesc(desc);
                    }
                    mcSkills.put(skillId, ret);
                }
            }
            return ret;
        } finally {
            skillLock.writeLock().unlock();
        }
    }

    public static MCGuardian getMCGuardian(int id) {
        guardianLock.readLock().lock();
        try {
            MCGuardian ret = mcGuardians.get(id);
            if (ret != null) {
                return ret;
            }
        } finally {
            guardianLock.readLock().unlock();
        }
        guardianLock.writeLock().lock();
        try {
            MCGuardian ret;
            ret = mcGuardians.get(id);
            if (ret == null) {
                MapleData skillData = mcGuardianRoot.getChildByPath(String.valueOf(id));
                if (skillData != null) {
                    int type = MapleDataTool.getInt("type", skillData, 0);
                    int spendCP = MapleDataTool.getInt("spendCP", skillData, 0);
                    int mobSkillID = MapleDataTool.getInt("mobSkillID", skillData, 0);
                    int level = MapleDataTool.getInt("level", skillData, 0);
                    ret = new MCGuardian(type, spendCP, mobSkillID, level);

                    if (MonsterCarnival.DEBUG) {
                        String name = MapleDataTool.getString("name", skillData, "");
                        String desc = MapleDataTool.getString("desc", skillData, "");
                        ret.setName(name);
                        ret.setDesc(desc);
                    }
                    mcGuardians.put(type, ret);
                }
            }
            return ret;
        } finally {
            guardianLock.writeLock().unlock();
        }
    }
}

MCTracker.java
PHP:
package net.sf.odinms.server.partyquest.mcpq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Logs various errors and also keeps data on Carnival PQ runs.
 * @author s4nta
 */
public class MCTracker {

    static Logger log = LoggerFactory.getLogger(MCTracker.class);

    // TODO:
    // Add field-specific info
    // Add methods for calls from different files
    // Maybe write own version of FilePrinter?

    static final String PATH = "Reports/MCPQ.txt";

    public static void log(String msg) {
        System.out.println(msg);
        log.debug(msg);
    }
}

MCWZData.java
PHP:
package net.sf.odinms.server.partyquest.mcpq;
import net.sf.odinms.provider.MapleData;
import net.sf.odinms.provider.MapleDataTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Representation of the data tree inside of the Map.wz file.
 *
 * @author s4nta
 */
public class MCWZData {

    public List<MCMobGenPos> mobGenPosList = new ArrayList<>();
    public List<MCGuardianGenPos> guardianGenPosList = new ArrayList<>();
    public Map<Integer, MCSummonMob> summons = new HashMap<>();
    public Map<Integer, Integer> skills = new HashMap<>();
    public Map<Integer, Integer> guardians = new HashMap<>();

    public String effectWin, effectLose, soundWin, soundLose;
    public int rewardMapWin, rewardMapLose;

    public int mobGenMax, guardianGenMax;
    public boolean mapDivided;
    public int deathCP;
    public int reactorRed, reactorBlue;

    public MCWZData(MapleData src) {
        parse(src);
    }

    public void parse(MapleData src) {
        populateMobGenPos(src.getChildByPath("mobGenPos"));
        populateSummonMobs(src.getChildByPath("mob"));
        effectWin = MapleDataTool.getString("effectWin", src);
        effectLose = MapleDataTool.getString("effectLose", src);
        soundWin = MapleDataTool.getString("soundWin", src);
        soundLose = MapleDataTool.getString("soundLose", src);

        rewardMapWin = MapleDataTool.getInt("rewardMapWin", src);
        rewardMapLose = MapleDataTool.getInt("rewardMapLose", src);

        populateSkills(src.getChildByPath("skill"));
        populateGuardianGenPos(src.getChildByPath("guardianGenPos"));
        populateGuardians(src.getChildByPath("guardian"));

        mobGenMax = MapleDataTool.getInt("mobGenMax", src, 20); // HACK: 20 default
        guardianGenMax = MapleDataTool.getInt("guardianGenMax", src, 20); // HACK: 20 default

        mapDivided = MapleDataTool.getInt("mapDivided", src) > 0;

        deathCP = MapleDataTool.getInt("deathCP", src);
        reactorRed = MapleDataTool.getInt("reactorRed", src);
        reactorBlue = MapleDataTool.getInt("reactorBlue", src);
    }

    private void populateMobGenPos(MapleData src) {
        for (MapleData n : src) {
            MCMobGenPos nn = new MCMobGenPos(MapleDataTool.getInt("x", n, 0),
                    MapleDataTool.getInt("y", n, 0),
                    MapleDataTool.getInt("fh", n, 0),
                    MapleDataTool.getInt("cy", n, 0),
                    MapleDataTool.getInt("team", n, -1));
            mobGenPosList.add(nn);
        }
    }

    private void populateSummonMobs(MapleData src) {
        for (MapleData n : src) {
            int id = Integer.parseInt(n.getName());
            MCSummonMob mcs = new MCSummonMob(
                    MapleDataTool.getInt("id", n, 0),
                    MapleDataTool.getInt("spendCP", n, 0),
                    MapleDataTool.getInt("mobTime", n, 0)
            );

            this.summons.put(id, mcs);
        }
    }

    private void populateSkills(MapleData src) {
        for (MapleData n : src) {
            int key = Integer.parseInt(n.getName());
            int val = MapleDataTool.getInt(n);

            skills.put(key, val);
        }
    }

    private void populateGuardianGenPos(MapleData src) {
        for (MapleData n : src) {
            MCGuardianGenPos nn = new MCGuardianGenPos(MapleDataTool.getInt("x", n, 0),
                    MapleDataTool.getInt("y", n, 0),
                    MapleDataTool.getInt("f", n, 0),
                    MapleDataTool.getInt("team", n, -1));
            guardianGenPosList.add(nn);
        }
    }

    private void populateGuardians(MapleData src) {
        for (MapleData n : src) {
            int key = Integer.parseInt(n.getName());
            int val = MapleDataTool.getInt(n);

            guardians.put(key, val);
        }
    }

    public class MCMobGenPos {

        public final int x, y, fh, cy, team;

        private MCMobGenPos(int x, int y, int fh, int cy, int team) {
            this.x = x;
            this.y = y;
            this.fh = fh;
            this.cy = cy;
            this.team = team;
        }
    }

    public class MCGuardianGenPos {

        public final int x, y, f, team;

        private MCGuardianGenPos(int x, int y, int f, int team) {
            this.x = x;
            this.y = y;
            this.f = f;
            this.team = team;
        }
    }

    public class MCSummonMob {

        public final int id, spendCP, mobTime;

        private MCSummonMob(int id, int spendCP, int mobTime) {
            this.id = id;
            this.spendCP = spendCP;
            this.mobTime = mobTime;
        }
    }
}

MonsterCarnival.java
PHP:
package net.sf.odinms.server.partyquest.mcpq;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.client.MapleDisease;
import net.sf.odinms.net.channel.ChannelServer;
import net.sf.odinms.net.world.MapleParty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Processes game logic for Monster Carnival PQ.
 *
 * TODO: Display IGNs/Jobs/Level in Pending Requests
 * TODO: fix reactor handling and make it less hacky
 * TODO: fix cube of darkness and make it less hacky
 *
 * @author s4nta
 */
public class MonsterCarnival {

    // Logger
    static Logger log = LoggerFactory.getLogger(MonsterCarnival.class);

    // Map of channel to a MonsterCarnival instance.
    private static final HashMap<Integer, MonsterCarnival> instances = new HashMap<>();

    /**
     * Returns the MonsterCarnival instance for a channel. Creates a new one and maps it
     * if it does not exist.
     *  [USER=2000183830]para[/USER]m channel Channel to check for.
     *  [USER=850422]return[/USER] MonsterCarnival instance for a channel.
     */
    public static MonsterCarnival getMonsterCarnival(int channel) {
        // TODO: synchronization?
        if (channel < 1 || channel > 20) {
            log.warn("Attempting to get a Monster Carnival instance for invalid channel.");
            return null;
        }
        if (instances.containsKey(channel)) {
            return instances.get(channel);
        }
        ChannelServer cserv = ChannelServer.getInstance(channel);
        if (cserv == null) {
            log.error("ChannelServer instance for channel " + channel + " is null.");
            return null;
        }
        MonsterCarnival inst = new MonsterCarnival(cserv);
        instances.put(channel, inst);
        return inst;
    }

    // Instance variables
    private ChannelServer cserv;
    private Map<Integer, MCField> fields = new HashMap<>();

    /**
     * Constructor for a MonsterCarnival instance.
     *  [USER=2000183830]para[/USER]m cserv Channel server for this instance.
     */
    public MonsterCarnival(ChannelServer cserv) {
        this.cserv = cserv;
        this.initFields();
    }

    /**
     * Initializes empty fields for the instance.
     */
    private void initFields() {
        for (int i = 1; i <= NUM_FIELDS; i++) {
            fields.put(i, new MCField(i, this.cserv, null, null));
        }
    }

    /**
     * Gets the field with a specified ID.
     *  [USER=2000183830]para[/USER]m id ID of field to retrieve.
     *  [USER=850422]return[/USER]
     */
    public MCField getField(int id) {
        if (id >= 1 && id <= NUM_FIELDS) {
            return fields.get(id);
        }
        return null;
    }

    /**
     * Checks if a party can join a field or not.
     *
     *  [USER=2000183830]para[/USER]m pty Party to register.
     *  [USER=2000183830]para[/USER]m room Room to join.
     *  [USER=850422]return[/USER] Different code based on status. OK if successful.
     */
    public int registerStatus(MapleParty pty, int room) {
        if (!isValidField(room)) {
            return STATUS_FIELD_INVALID;
        }
        MCField field = this.getField(room);
        if (field.isFull()) {
            return STATUS_FIELD_FULL;
        }
        MCParty party = new MCParty(pty);
        if (!sizeCheck(party.getSize(), room)) {
            return STATUS_PARTY_SIZE;
        }
        boolean levelCheck = party.checkLevels();
        if (!levelCheck) {
            return STATUS_PARTY_LEVEL;
        }
        boolean chanCheck = party.checkChannels();
        if (!chanCheck) {
            return STATUS_PARTY_MISSING;
        }
        boolean mapCheck = party.checkMaps();
        if (!mapCheck) {
            return STATUS_PARTY_MISSING;
        }
        if (field.needsRequest()) {
            return STATUS_REQUEST;
        }
        return STATUS_PROCEED;
    }

    /**
     * Creates a new MCParty based on a regular MapleParty object.
     *  [USER=2000183830]para[/USER]m pty Party to base off of.
     *  [USER=850422]return[/USER] Newly created MCParty.
     */
    public MCParty createParty(MapleParty pty) {
        return new MCParty(pty);
    }

    public void resetPlayer(MapleCharacter chr) {
        MCParty.deregisterPlayer(chr);
        chr.changeMap(MAP_LOBBY);
    }

    /**
     * Returns a String containing information about lobby waiting rooms.
     *
     *  [USER=850422]return[/USER] String containing lobby information formatted for NPC.
     */
    public String getNPCAvailableFields() {
        StringBuilder sb = new StringBuilder();
        sb.append("Welcome to the #bCarnival PQ#k! Rooms 1-4 can hold 2-4 people, and rooms 5-6 can hold 3-6.\r\n#b");
        for (int i = 1; i <= NUM_FIELDS; i++) {
            MCField field = this.fields.get(i);
            sb.append(field.getStatus());
        }

        return sb.toString();
    }

    // Reference Information

    // Game Constants
    public static final int CP_LOSS_ON_DEATH = 10;
    public static final int TIME_PREBATTLE = 10;
    public static final int TIME_BATTLE = 600;
    public static final int TIME_LOBBYWAIT = 180;

    public static final int TAB_SPAWNS   = 0;
    public static final int TAB_DEBUFF   = 1;
    public static final int TAB_GUARDIAN = 2;

    /**
     * Gets a random debuff for (Mini) Cube of Darkness.
     *  [USER=850422]return[/USER] Random MapleDisease.
     */
    public static MapleDisease getRandomDebuff() {
        return DEBUFFS[new Random().nextInt(DEBUFFS.length)];
    }

    /**
     * Checks party size. Information from hidden-street MCPQ page.
     *  [USER=2000183830]para[/USER]m size Size of the party.
     *  [USER=2000183830]para[/USER]m field Field to check for.
     *  [USER=850422]return[/USER] True if party size is okay, False otherwise.
     */
    public static final boolean sizeCheck(int size, int field) {
        if (DEBUG) {
            return true;
        }
        switch (field) {
            case 1:
            case 2:
            case 3:
            case 4:
                return size >= 2 && size <= 4;
            case 5:
            case 6:
                return size >= 3 && size <= 6;
            default:
                return false;
        }
    }

    public static final boolean isValidField(int field) {
        return field >= 1 && field <= 6;
    }

    public static final int getLobbyMap(int field) {
        if (field < 1 || field > NUM_FIELDS) {
            log.warn("Attempting to get lobby map for invalid field.");
            return MAP_EXIT;
        }
        return 980000000 + field * 100;
    }

    public static final boolean isLobbyMap(int mapid) {
        switch (mapid) {
            case 980000100:
            case 980000200:
            case 980000300:
            case 980000400:
            case 980000500:
            case 980000600:
                return true;
            default:
                return false;
        }
    }

    public static final int getBattleFieldMap(int field) {
        if (field < 1 || field > NUM_FIELDS) {
            log.warn("Attempting to get battlefield map for invalid field.");
            return MAP_EXIT;
        }
        return 980000000 + field * 100 + 1;
    }

    public static final boolean isBattlefieldMap(int mapid) {
        switch (mapid) {
            case 980000101:
            case 980000201:
            case 980000301:
            case 980000401:
            case 980000501:
            case 980000601:
                return true;
            default:
                return false;
        }
    }

    public static final int getResurrectionMap(int field) {
        if (field < 1 || field > NUM_FIELDS) {
            log.warn("Attempting to get resurrection map for invalid field.");
            return MAP_EXIT;
        }
        return 980000000 + field * 100 + 2;
    }

    public static final int getVictoriousMap(int field) {
        if (field < 1 || field > NUM_FIELDS) {
            log.warn("Attempting to get victory map for invalid field.");
            return MAP_EXIT;
        }
        return 980000000 + field * 100 + 3;
    }

    public static final int getDefeatedMap(int field) {
        if (field < 1 || field > NUM_FIELDS) {
            log.warn("Attempting to get defeat map for invalid field.");
            return MAP_EXIT;
        }
        return 980000000 + field * 100 + 4;
    }

    public static final boolean isCPQConsumeItem(int itemid) {
        switch (itemid) {
            case ITEM_CP_1:
            case ITEM_CP_2:
            case ITEM_CP_3:
            case ITEM_PTY_ELIX:
            case ITEM_PTY_PELIX:
            case ITEM_PTY_ALLC:
            case ITEM_MINICUBE:
            case ITEM_DARKCUBE:
            case ITEM_STUNNER:
            case ITEM_IND_WHITE:
            case ITEM_IND_MANA:
            case ITEM_IND_ELIX:
            case ITEM_IND_PELIX:
            case ITEM_IND_ALLC:
            case ITEM_PTY_MANA:
                return true;
        }
        return false;
    }

    // Error Codes
    // Note: These would be in an enum, but since these will be used in a NPC, they are not.
    public static final int STATUS_FIELD_FULL    = 0;
    public static final int STATUS_PARTY_SIZE    = 1;
    public static final int STATUS_PARTY_LEVEL   = 2;
    public static final int STATUS_PARTY_MISSING = 3;
    public static final int STATUS_FIELD_INVALID = 4;
    public static final int STATUS_REQUEST       = 98;
    public static final int STATUS_PROCEED       = 99;

    // Maps
    public static final int MAP_LOBBY = 980000000;
    public static final int MAP_EXIT  = 980000010;

    // NPCs
    public static final int NPC_LOBBY     = 2042000;
    public static final int NPC_ENTER     = 2042001; // Warp in from outside
    public static final int NPC_INFO      = 2042002;
    public static final int NPC_ASST_RED  = 2042003;
    public static final int NPC_ASST_BLUE = 2042004;

    // Items
    public static final int ITEM_CP_1      = 2022157;
    public static final int ITEM_CP_2      = 2022158;
    public static final int ITEM_CP_3      = 2022159;
    public static final int ITEM_PTY_MANA  = 2022160;
    public static final int ITEM_PTY_ELIX  = 2022161;
    public static final int ITEM_PTY_PELIX = 2022162;
    public static final int ITEM_PTY_ALLC  = 2022163;
    public static final int ITEM_MINICUBE  = 2022164;
    public static final int ITEM_DARKCUBE  = 2022165;
    public static final int ITEM_STUNNER   = 2022166;
    public static final int ITEM_IND_WHITE = 2022174;
    public static final int ITEM_IND_ELIX  = 2022175;
    public static final int ITEM_IND_PELIX = 2022176;
    public static final int ITEM_IND_MANA  = 2022177;
    public static final int ITEM_IND_ALLC  = 2022178;

    // Guardians
    public static final int GUARDIAN_RED = 9980000;
    public static final int GUARDIAN_BLUE = 9980001;

    // Debuffs
    public static final MapleDisease[] DEBUFFS = {
            MapleDisease.STUN,
            MapleDisease.DARKNESS,
            MapleDisease.WEAKEN}; // intentionally leave out a few

    // Miscellaneous
    public static final int MIN_LEVEL  = 30;
    public static final int MAX_LEVEL  = 50;
    public static final int NUM_FIELDS = 6;

    // Debug
    public static final boolean DEBUG = false; }

----
Packets:
This will depend on the version of MapleStory you're running -- see the discussion below for more information.

tools/packet/MonsterCarnivalPacket.java
PHP:
package net.sf.odinms.tools;
import net.sf.odinms.client.MapleCharacter;
import net.sf.odinms.net.MaplePacket;
import net.sf.odinms.net.SendPacketOpcode;
import net.sf.odinms.server.partyquest.mcpq.MCParty;
import net.sf.odinms.tools.data.output.MaplePacketLittleEndianWriter;

public class MonsterCarnivalPacket {

    public static MaplePacket startCPQ(MapleCharacter chr) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(SendPacketOpcode.MONSTER_CARNIVAL_START.getValue());
        mplew.write(chr.getTeam()); //team
        mplew.writeShort(chr.getAvailableCP()); //Available CP
        mplew.writeShort(chr.getTotalCP()); //Total Obtained CP
        mplew.writeShort(chr.getMCPQField().getRed().getAvailableCP()); //Available CP of the team
        mplew.writeShort(chr.getMCPQField().getRed().getTotalCP()); //Total Obtained CP of the team
        mplew.writeShort(chr.getMCPQField().getBlue().getAvailableCP()); //Available CP of the team
        mplew.writeShort(chr.getMCPQField().getBlue().getAvailableCP()); //Total Obtained CP of the team
        mplew.writeShort(0); //Probably useless nexon poop
        mplew.writeLong(0); //Probably useless nexon poop
        return mplew.getPacket();
    }

    public static MaplePacket updatePersonalCP(MapleCharacter chr) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(SendPacketOpcode.MONSTER_CARNIVAL_OBTAINED_CP.getValue());
        mplew.writeShort(chr.getAvailableCP()); //Obtained CP - Used CP
        mplew.writeShort(chr.getTotalCP()); //Total Obtained CP
        return mplew.getPacket();
    }

    public static MaplePacket updatePartyCP(MCParty pty) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(SendPacketOpcode.MONSTER_CARNIVAL_PARTY_CP.getValue());
        mplew.write(pty.getTeam().code); //Team where the points are given to.
        mplew.writeShort(pty.getAvailableCP()); //Obtained CP - Used CP
        mplew.writeShort(pty.getTotalCP()); //Total Obtained CP
        return mplew.getPacket();
    }

    public static MaplePacket CPQSummon(int tab, int num, String name) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(SendPacketOpcode.MONSTER_CARNIVAL_SUMMON.getValue());
        mplew.write(tab); //Tab
        mplew.write(num); //Number of summon inside the tab
        mplew.writeMapleAsciiString(name); //Name of the player that summons
        return mplew.getPacket();
    }

    public static MaplePacket CPQDied(MapleCharacter player, int loss) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(SendPacketOpcode.MONSTER_CARNIVAL_DIED.getValue());
        mplew.write(player.getTeam()); //Team
        mplew.writeMapleAsciiString(player.getName()); //Name of the player that died
        mplew.write(loss); //Lost CP
        return mplew.getPacket();
    }

    /**
     * Sends a CPQ Message
     *
     * Possible values for <code>message</code>:<br>
     * 1: You don't have enough CP to continue.
     * 2: You can no longer summon the Monster.
     * 3: You can no longer summon the being.
     * 4: This being is already summoned.
     * 5:     This request has failed due to an unknown error.
     *
     *     [USER=2000183830]para[/USER]m message Displays a message inside Carnival PQ
     **/
    public static MaplePacket CPQMessage(int message) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(SendPacketOpcode.MONSTER_CARNIVAL_MESSAGE.getValue());
        mplew.write(message); //Message
        return mplew.getPacket();
    }

    public static MaplePacket leaveCPQ(int team, String name) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(SendPacketOpcode.MONSTER_CARNIVAL_LEAVE.getValue());
        mplew.write(0); //Something?
        mplew.write(team); //Team
        mplew.writeMapleAsciiString(name); //Player name
        return mplew.getPacket();
    }

}

tools/MaplePacketCreator.java:

spawnMonsterInternal:
Replace
PHP:
mplew.writeShort(newSpawn ? -2 : -1)
with:
PHP:
mplew.write(newSpawn? -2 : -1);
mplew.write(life.getTeam());

spawnPlayerMapObject:
Add
PHP:
mplew.write(chr.getTeam());
to the end of the packet, after ring information (in v62 -- not sure about higher versions).

-----
Screens:

mKMyrsw - [holiday special] monster carnival - RaGEZONE Forums


jp5Yrk7 - [holiday special] monster carnival - RaGEZONE Forums



SuQLAfH - [holiday special] monster carnival - RaGEZONE Forums


KM6iJJa - [holiday special] monster carnival - RaGEZONE Forums
 

Attachments

You must be registered for see attachments list
Last edited:
Custom Title Activated
Loyal Member
Joined
Jan 18, 2010
Messages
3,109
Reaction score
1,139
Amazing work. I've always wanted full CPQ!

Only thing I noticed nobody ever did that I saw in IDA doing the packet portion was your long and short in the startCPQ packet, it's supposively adding in item information (MCItemInfo?), mob summons, etc that I recall (it was called like SpelledData or something). Not sure how it looks client-sided when used or what difference it makes, though. Also, the packets are the same post-bb from v95~v142 that I've ever tested (handlers bytes are changed, however).
 
Custom Title Activated
Loyal Member
Joined
Jan 18, 2010
Messages
3,109
Reaction score
1,139
There are ten bytes at the end of the startCPQ packet, is that what you're referring to? (I got my packets from http://forum.ragezone.com/f427/cpq-packets-689834/)

I'm also curious as to what it could be; if you could modify summons/skills, that would be amazing.

Yeah. The long and short that are marked as useless bytes. It says it is a list of summoned mobs.. I decided to re-check it myself and it's a forloop decoding 1 byte which is referenced as count (size) for Spelled Data. I wonder if this changes maximum amounts of specific mobs or something, no clue..

Here's the code:
4Lwtjoq - [holiday special] monster carnival - RaGEZONE Forums

iaelxD7 - [holiday special] monster carnival - RaGEZONE Forums


Also, if it makes use to anyone (even you), there's some extra bytes in the packets like leaveCPQ has a byte for party leader leaving and other random things, so here's all of the packets and messages for CPQ:

PHP:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package server.field;

import java.util.ArrayList;
import java.util.List;
import tools.data.output.MaplePacketLittleEndianWriter;

/**
 * CField_MonsterCarnival::OnPacket
 * 
 * @author Eric
 */
public class MonsterCarnival {
    private static enum CField_MonsterCarnival {
        Enter(0x121), // start
        PersonalCP(0x121), // obtain cp
        TeamCP(0x123), // party cp
        ResultSuccess(0x124), // summon
        ResultFail(0x125), // summon o.o
        Death(0x126), // died
        MemberOut(0x127), // leave?
        GameResult(0x128); // results of the two teams? idk l0l
        private final int val;
        private CField_MonsterCarnival(int val) {
            this.val = val;
        }
        
        public int getVal() {
            return val;
        }
    }
    
    public enum MCarnivalResCode {
        // MCarnivalReq
        SummonMob(0x0),
        UseSkill(0x1),
        SummonGuardian(0x2),
        // MCarnivalRes
        Success(0x0),
        Fail_CPLaking(0x1),
        Fail_MobOverflow(0x2),
        Fail_GuardianOverflow(0x3),
        Fail_GuardianAlreadySummoned(0x4),
        Fail_Unknown(0x5),
        // MCarnivalOut
        PartyBoss(0x6),
        PartyMember(0x7),
        // MCarnivalGameRes
        Win(0x8),
        Lose(0x9),
        Draw(0xA),
        Cancel(0xB);
        private final int res;
        
        private MCarnivalResCode(int res) {
            this.res = res;
        }
        
        public int getCode() {
            return res;
        }
    }
    
    public enum MCarnivalTeam {
        TeamRed(0x0),
        TeamBlue(0x1),
        TeamNo(0x2),
        TeamNone(0xFF);
        private final int team;
        private MCarnivalTeam(int team) {
            this.team = team;
        }
        
        public int getTeam() {
            return team;
        }
    }
    
    public static class MCItemInfo {
        public int nID;
        public String sName;
        public int nSpendCP;
        public String sDesc;
    }
    
    public static byte[] OnEnter(int nTeam, int nRestMyCP, int nTotMyCP, int nRestMyTeamCP, int nTotMyTeamCP, int nRestEnemyCP, int nTotEnemyCP) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(CField_MonsterCarnival.Enter.getVal());
        mplew.write(nTeam);
        mplew.writeShort(nRestMyCP); // PersonalCP for nRest
        mplew.writeShort(nTotMyCP); // PersonalCP for nTotal
        mplew.writeShort(nRestMyTeamCP); // TeamCP for nRest
        mplew.writeShort(nTotMyTeamCP); // TeamCP for nTotal
        mplew.writeShort(nRestEnemyCP); // EnemyCP for nRest
        mplew.writeShort(nTotEnemyCP); // EnemyCP for nTotal
        List<MCItemInfo> aSummonedMob = new ArrayList<>(1);
        aSummonedMob.add(new MCItemInfo());
        for (MCItemInfo info : aSummonedMob) {
            mplew.write(0);//CUIMonsterCarnival::InsertSpelledData
        }
        return mplew.getPacket();
    }
    
    public static byte[] OnPersonalCP(int nRest, int nTotal) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(CField_MonsterCarnival.PersonalCP.getVal());
        mplew.writeShort(nRest);
        mplew.writeShort(nTotal);
        return mplew.getPacket();
    }
    
    public static byte[] OnTeamCP(int nTeam, int nRest, int nTotal) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(CField_MonsterCarnival.PersonalCP.getVal());
        mplew.write(nTeam);
        mplew.writeShort(nRest);
        mplew.writeShort(nTotal);
        return mplew.getPacket();
    }
    
    public static byte[] OnResultSuccess(int nCategory, int nIdx, String sCharacterName) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(CField_MonsterCarnival.ResultSuccess.getVal());
        mplew.write(nCategory);
        mplew.write(nIdx);
        mplew.writeMapleAsciiString(sCharacterName);
        return mplew.getPacket();
    }
    
    public static byte[] OnResultFail(int nType) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(CField_MonsterCarnival.ResultFail.getVal());
        mplew.write(nType);
        // 1: You don't have enough CP to continue.
        // 2: You can no longer summon the Monster.
        // 3: You can no longer summon the being.
        // 4: This being is already summoned.
        // 5: This request has failed due to an unknown error.
        return mplew.getPacket();
    }
    
    public static byte[] OnProcessForDeath(int nTeam, String sCharacterName, boolean bLose) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(CField_MonsterCarnival.Death.getVal());
        mplew.write(nTeam);
        mplew.writeMapleAsciiString(sCharacterName);
        mplew.write(bLose);
        // bLose -- True: [%s] has become unable to fight and [%s]team has lost %d CP.
        // bLose -- Fale: [%s] has become unable to fight but [%s] has no CP so [%s] team did not lose any CP
        return mplew.getPacket();
    }
    
    public static byte[] OnShowMemberOutMsg(boolean bPartyLeader, int nTeam, String sCharacterName) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(CField_MonsterCarnival.MemberOut.getVal());
        mplew.write(bPartyLeader);
        mplew.write(nTeam);
        mplew.writeMapleAsciiString(sCharacterName);
        // bPartyLeader -- True: Since the leader of the Team [%s] quit the Monster Carnival, [%s] has been appointed as the new leader of the team.
        // bPartyLeader -- False: [%s] of Team [%s] has quit the Monster Carnival.
        return mplew.getPacket();
    }
    
    public static byte[] OnShowGameResult(int nType) {
        MaplePacketLittleEndianWriter mplew = new MaplePacketLittleEndianWriter();
        mplew.writeShort(CField_MonsterCarnival.GameResult.getVal());
        mplew.write(nType);
        // 8: You have won the Monster Carnival. Please wait as you'll be transported out of here shortly.
        // 9: Unfortunately, you have lost the Monster Carnival. Please wait as you'll be transported out of here shortly.
        // 10: Despite the Overtime, the carnival ended in a draw. Please wait as you'll be transported out of here shortly.
        // 11: Monster Carnival has ended abruptly due to the opposing team leaving the game too early. Please wait as you'll be transported out of here shortly.
        return mplew.getPacket();
    }
}
 

Attachments

You must be registered for see attachments list
Experienced Elementalist
Joined
Mar 12, 2015
Messages
238
Reaction score
43
*Still sees jibberish when looking at ida code*

awesome release, best pq Nexon ever made imo
 

Rey

The Shrewd
Loyal Member
Joined
Oct 29, 2011
Messages
1,336
Reaction score
196
AMAZING RELEASE! Thank you mate, it helps alot.



Does it work for v83?
 
Newbie Spellweaver
Joined
Sep 17, 2013
Messages
82
Reaction score
137
AMAZING RELEASE! Thank you mate, it helps alot.



Does it work for v83?

Update the packet headers, and find where the events should be called -- other than that, I don't think the game logic has changed significantly since it was implemented. In theory, this should work even in v169/v170 (assuming Nexon hasn't changed anything?)
 
  • Like
Reactions: Rey
Newbie Spellweaver
Joined
Jan 23, 2011
Messages
36
Reaction score
0
Wow thanks so much for release this.
Btw, I got some error after added the whole release ( i'm using MapleSolaxia V.83)
MapleMonster.java
Code:
public void dispel() {
        if (!isAlive()) return;

        for (MonsterStatus i : MonsterStatus.values()) {
            if ([COLOR="#FF0000"]monsterBuffs[/COLOR].contains(i)) {
                [COLOR="#FF0000"]removeMonsterBuff[/COLOR](i);
                [COLOR="#FF0000"]MaplePacket [/COLOR]packet = MaplePacketCreator.cancelMonsterStatus(getObjectId(), Collections.singletonMap(i, Integer.valueOf(1)));
                map.broadcastMessage(packet, getPosition());
                if (getController() != null && !getController().isMapObjectVisible(MapleMonster.this)) {
                    getController().getClient().getSession().write(packet);
                }
            }
        }
    }
MCField.java
Code:
public void announce([COLOR="#FF0000"]MaplePacket [/COLOR]pkt) {
Code:
public void endBattle(MCParty winner, MCParty loser, boolean abnormal) {
        // TODO: Abnormal win codes to prevent exploits

        validateBattleTask.cancel(true);
        spawnMonstersTask.cancel(true);

        MCWZData cpqData = this.getMap(MCMaps.BATTLEFIELD).getMCPQData();
        String effectWin = cpqData.effectWin;
        String effectLose = cpqData.effectLose;
        String soundWin = cpqData.soundWin;
        String soundLose = cpqData.soundLose;
        [COLOR="#FF0000"]winner.broadcast(MaplePacketCreator.showEffect(effectWin));
        winner.broadcast(MaplePacketCreator.playSound(soundWin));
        loser.broadcast(MaplePacketCreator.showEffect(effectLose));
        loser.broadcast(MaplePacketCreator.playSound(soundLose));

        this.getMap(MCMaps.BATTLEFIELD).killAllMonsters(false);[/COLOR]
        this.getMap(MCMaps.BATTLEFIELD).clearDrops();
        this.deregister(false);

        TimerManager.getInstance().schedule(
                new WarpEndBattleTask(this, winner, loser),
                1000 * 3);
    }
MCParty.java
Code:
public void notice(String msg) {
       [COLOR="#FF0000"] broadcast(MaplePacketCreator.serverNotice(6, msg));[/COLOR]
    }

    public void broadcast([COLOR="#FF0000"]MaplePacket [/COLOR]pkt) {
        for (MapleCharacter chr : getMembers()) {
            chr.getClient().getSession().write(pkt);
        }
    }
 
Junior Spellweaver
Joined
Jan 2, 2014
Messages
150
Reaction score
50
Wow thanks so much for release this.
Btw, I got some error after added the whole release ( i'm using MapleSolaxia V.83)
MapleMonster.java
Code:
public void dispel() {
        if (!isAlive()) return;

        for (MonsterStatus i : MonsterStatus.values()) {
            if ([COLOR=#FF0000]monsterBuffs[/COLOR].contains(i)) {
                [COLOR=#FF0000]removeMonsterBuff[/COLOR](i);
                [COLOR=#FF0000]MaplePacket [/COLOR]packet = MaplePacketCreator.cancelMonsterStatus(getObjectId(), Collections.singletonMap(i, Integer.valueOf(1)));
                map.broadcastMessage(packet, getPosition());
                if (getController() != null && !getController().isMapObjectVisible(MapleMonster.this)) {
                    getController().getClient().getSession().write(packet);
                }
            }
        }
    }
MCField.java
Code:
public void announce([COLOR=#FF0000]MaplePacket [/COLOR]pkt) {
Code:
public void endBattle(MCParty winner, MCParty loser, boolean abnormal) {
        // TODO: Abnormal win codes to prevent exploits

        validateBattleTask.cancel(true);
        spawnMonstersTask.cancel(true);

        MCWZData cpqData = this.getMap(MCMaps.BATTLEFIELD).getMCPQData();
        String effectWin = cpqData.effectWin;
        String effectLose = cpqData.effectLose;
        String soundWin = cpqData.soundWin;
        String soundLose = cpqData.soundLose;
        [COLOR=#FF0000]winner.broadcast(MaplePacketCreator.showEffect(effectWin));
        winner.broadcast(MaplePacketCreator.playSound(soundWin));
        loser.broadcast(MaplePacketCreator.showEffect(effectLose));
        loser.broadcast(MaplePacketCreator.playSound(soundLose));

        this.getMap(MCMaps.BATTLEFIELD).killAllMonsters(false);[/COLOR]
        this.getMap(MCMaps.BATTLEFIELD).clearDrops();
        this.deregister(false);

        TimerManager.getInstance().schedule(
                new WarpEndBattleTask(this, winner, loser),
                1000 * 3);
    }
MCParty.java
Code:
public void notice(String msg) {
       [COLOR=#FF0000] broadcast(MaplePacketCreator.serverNotice(6, msg));[/COLOR]
    }

    public void broadcast([COLOR=#FF0000]MaplePacket [/COLOR]pkt) {
        for (MapleCharacter chr : getMembers()) {
            chr.getClient().getSession().write(pkt);
        }
    }
Solaxia uses byte[] instead of MaplePacket.
Too lazy to look into the monsterBuffs stuff..
No clue why you have errors on the killAllMonsters line.. remove the false?
 
not a programmer
Joined
Mar 30, 2015
Messages
532
Reaction score
62
So if i understand correctly this works for v142, but i have to update some things?

Please don't burn me i'm new
IIRC I think they removed it before v142? Otherwise yeah there was a revamp.
 
Custom Title Activated
Loyal Member
Joined
Jan 18, 2010
Messages
3,109
Reaction score
1,139
IIRC I think they removed it before v142? Otherwise yeah there was a revamp.

I had it in v117.2, and I think I also had it in v142, but i don't even have that source anymore so rip.. The packets iirc weren't changed, but the handler was. The interfaced switch from CP points and multiple tabs to just keys F1~F5 and would randomize the skills/spawns given to the enemy team.

They removed the best map tho qq.. i miss the purple map was my fav oneee
 
Initiate Mage
Joined
Jan 29, 2016
Messages
1
Reaction score
0
After Complite i get Eror 38 and DC ,

if my Char meet with other char i get DC eror 38 not just in Cpq.

how i fix it?
 
Last edited:
Back
Top