Skip to content

Battle Script Snippets

coyotte508 edited this page Jun 19, 2013 · 6 revisions

Scripting/Battle/Snippets

Pre-Battle Message

This example will teach you how to send a message before the battle starts, instead of only afterwards.

({
onTierNotification: function (tier) {
	battle.battleMessage(battle.id, "hello");
}
});

We can use the onTierNotification event since it's something that happens once at the beginning of every battle. You can replace the "hello" in battle.battleMessage(battle.id, "hello"); with whatever you'd like, as long as there are quotes surrounding it.

Say "Hi" to Spectators

This example will demonstrate a way to say hello to people watching your battle!

({
hicounter: 0,
maxhis: 3,

onSpectatorJoin: function (id, name) {
	if (this.hicounter < this.maxhis) {
		battle.battleMessage(battle.id, "hello " + name);
		this.hicounter++;
		if (this.hicounter == this.maxhis) {
			battle.battleMessage(battle.id, "ok i'm not saying hi anymore");
		}
	}
}
});

The onSpectatorJoin event will be called every time somebody starts watching a battle. It even gives you a name variable to use, which will makes this look pretty neat. What we're doing here is declaring two variables, which represent how many times we've said hi, and the maximum amount of times we want to say hi repectively. Each time someone spectates, we check to see if the amount of times we've already said "hi" isn't more than the preset limit. If it's not, we say "hello" and the name of the spectator, and the hicounter variable increases by one. If it's at the limit (if (this.hicounter == this.maxhis)), we inform people that we won't be saying hi anymore, as this can become obnoxious in tours and such. Since hicounter is now higher than maxhis, our greeting will stop.

Alternatively, if you would just like to say "hi", you can replace "hello " + name with just "hi".

Random bot

var useAI=true;

var channel = 0;

var nick = function(spot) { return battle.data.field.poke(spot).pokemon.nick; };
var verb = false;
var send = function(msg) {
   if (!verb) /*print (msg)*/;
   else client.network().sendChanMessage(channel, msg);
};

var poke = function(spot) { return battle.data.team(spot).poke(0);};
var fpoke = function(spot) { return battle.data.field.poke(spot);};
var tpoke = function(ind) { return battle.data.team(battle.me).poke(ind);};

({
onBeginTurn : function(turn) {
    send("Turn " + turn + " of the battle!");
},
onKo : function(spot) {
    send("Oh no! " + nick(spot) + " fainted!");
},
onDamageDone: function(spot, damage) {
    if (spot == battle.me) {
        send(":(( My " + nick(spot) + " lost " + damage + " HP!");
    } else {
        send(nick(spot) + " lost " + damage + "% ;D !");
    }
},
onChoiceSelection: function(player) {
    if (player!=battle.me || !useAI) {
        return;
    }
    var switches = [];
    for (var i = 1; i < 6; i++) {
        if (!tpoke(i).isKoed()) {
           switches.push(i);
        }
    }

   var r = sys.rand(0, 8);

    if (r == 0 || (fpoke(battle.me).onTheField && !poke(battle.me).isKoed() && (r != 1 || switches.length == 0))) {
        choice = {"slot": battle.me, "type":"attack", "attackSlot":sys.rand(0,4)};
    } else {
        var cswitch = switches[sys.rand(0,switches.length)];
   
        choice = {"slot": battle.me, "type":"switch", "pokeSlot": cswitch};
    }
    battle.battleCommand(battle.id, choice);
},
onChoiceCancellation: function(player) {
    this.onChoiceSelection(player);
},
onDrawRequest: function (player) {
    this.onChoiceCancelled(player);
},
onChoiceCancelled: function(player) {
//    print ("old useAI: " + useAI);
    useAI = !useAI;
    print ("new useAI: " + useAI);
}
,
onPlayerMessage: function(player, message) {
    if (player == battle.me) {
        if (message == "annoy") {
            verb = true;
        } else if (message == "debug") {
            verb = false;
        } else if (message.substr(0, 5) == "eval ") {
             sys.eval(message.substr(5));
        }
    }
}
})

This is a sample battling script.

If you say "annoy" it will start printing what happens in your battle in the selected channel (0 by default, which is the main channel). If you cancel a move or suggest a draw, it will switch to using an AI or not. The AI currently only selects moves at random.