Saturday, September 13, 2008

Javascript 101.

Firstly, apologies for the disturbance I caused last night, I was going slightly off balance.

Okay, today, I'll talk about Javascript and Private servers - how Javascript can be utilized to create simple NPC's and events. Let's start with a simple one - Sera.

var wui = 0;

function start() {
cm.sendOk ("Welcome to the world of MapleStory. The purpose of this training camp is to help beginners. Would you like to enter this training camp? Some people start their journey without taking the training program. But I strongly recommend you take the training program first.");
}

function action(mode, type, selection) {
if (mode == 0 || wui == 1) {
cm.dispose();
cm.gainItem(1002562, 1);
cm.gainItem(01052081, 1);
cm.warp(1, 0);
} else {
wui = 1;
var statup = new java.util.ArrayList();
var p = cm.c.getPlayer();
var totAp = p.getRemainingAp() + p.getStr() + p.getDex() + p.getInt() + p.getLuk();
p.setStr(4);
p.setDex(4);
p.setInt(4);
p.setLuk(4);
p.setRemainingAp (totAp - 16);
statup.add (new net.sf.odinms.tools.Pair(net.sf.odinms.client.MapleStat.STR, java.lang.Integer.valueOf(4)));
statup.add (new net.sf.odinms.tools.Pair(net.sf.odinms.client.MapleStat.DEX, java.lang.Integer.valueOf(4)));
statup.add (new net.sf.odinms.tools.Pair(net.sf.odinms.client.MapleStat.LUK, java.lang.Integer.valueOf(4)));
statup.add (new net.sf.odinms.tools.Pair(net.sf.odinms.client.MapleStat.INT, java.lang.Integer.valueOf(4)));
statup.add (new net.sf.odinms.tools.Pair(net.sf.odinms.client.MapleStat.AVAILABLEAP, java.lang.Integer.valueOf(p.getRemainingAp())));

p.getClient().getSession().write (net.sf.odinms.tools.MaplePacketCreator.updatePlayerStats(statup));
cm.sendOk ("Ok, the training begin !");
}
}


How's that?

var wui = 0;

var stands for Variable, much like C++'s "int", except its more all-encompassing.

function start() {
cm.sendOk ("Welcome to the world of MapleStory. The purpose of this training camp is to help beginners. Would you like to enter this training camp? Some people start their journey without taking the training program. But I strongly recommend you take the training program first.");
}


Declares the function "start()", which can be called anytime later by just using start();. sendOk is a predefined function (for PS's) that makes a textbox with just the button "OK" and "End chat".

function action(mode, type, selection) {

Declares a new function.

if (mode == 0 || wui == 1) {

Looks pretty impossble, doesn't it? wui has been set to 0! So thus, the whole block below if...

{
cm.dispose();
cm.gainItem(1002562, 1);
cm.gainItem(01052081, 1);
cm.warp(1, 0);
}


is skipped for now. Let's take a look at it. cm.dispose(); is basically a function that cuts the chatbox - the chat is thus "disposed". Then you gain items of itemcode 1002562, as well as 1052081. You are then warped to map 1, portal 0, the default spawnpoint.

Let's look at the next part. With "if", of course, comes "else".
else {
wui = 1;

See? The previous block of code HAS its use! But the "else" takes precedence, all the way till the end of the curly bracket.

var statup = new java.util.ArrayList();

This line declares the variable "statup", along with a new array. For what?


var p = cm.c.getPlayer();
var totAp = p.getRemainingAp() + p.getStr() + p.getDex() + p.getInt() + p.getLuk();


Now we see. The script first gets the stats of the player clicking the NPC, and stores the values in totAP to the new array. Remaining AP + all the four stats are stored in order.

p.setStr(4);
p.setDex(4);
p.setInt(4);
p.setLuk(4);
p.setRemainingAp (totAp - 16);

This is pretty obvious. It resets all 4 main stats to 4, and sets the remaining AP to totAP - 16. Since totAP is remaining+str+dex+int+luk, if you set the 4 to "4", then naturally, what's remaining will be everything BUT for the 16 still currently in there.

statup.add (new net.sf.odinms.tools.Pair(net.sf.odinms.client.MapleStat.STR, java.lang.Integer.valueOf(4)));
statup.add (new net.sf.odinms.tools.Pair(net.sf.odinms.client.MapleStat.DEX, java.lang.Integer.valueOf(4)));
statup.add (new net.sf.odinms.tools.Pair(net.sf.odinms.client.MapleStat.LUK, java.lang.Integer.valueOf(4)));
statup.add (new net.sf.odinms.tools.Pair(net.sf.odinms.client.MapleStat.INT, java.lang.Integer.valueOf(4)));
statup.add (new net.sf.odinms.tools.Pair(net.sf.odinms.client.MapleStat.AVAILABLEAP, java.lang.Integer.valueOf(p.getRemainingAp())));


This function sets your stats cleanly and makes sure that your stats won't freeze up. Pretty crummy if you resetted it and can't pump it back, right?

p.getClient().getSession().write (net.sf.odinms.tools.MaplePacketCreator.updatePlayerStats(statup));
cm.sendOk ("Ok, the training begin !");
}
}


This line terminates the "else" block, and thus, because wui has been set to "1", the warping occurs.

Posted by Denunciator at 7:50 PM