Binary Deployment-Help (Java Script)


#1

I am playing binary deployment, but I am having a few problems. I made a function that deploys the soldiers/archers in the correct order, but they don’t do it fast enough. I run out of time. if I run it too fast, they don’t receive my commands, but if I run it too slow, they don’t get done in time. My other problem is that I can’t find the paladin’s deployment attribute for the number.
When I hit submit, the paladin’s number changes, and I have to change the number that is plugged into my function. How do I save the number stated as a variable that can be used for my function? Here’s my code:

this.deployLine = function(number,paladinX,paladinY){
var binary = [“soldier”,“archer”];
if (number>=128){
this.summon(binary[1]);
var friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+5,y:paladinY});
number = number-128;
}else{
this.summon(binary[0]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+5,y:paladinY});
}
if (number>=64){
this.summon(binary[1]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+10,y:paladinY});
number = number-64;
}else{
this.summon(binary[0]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+10,y:paladinY});
}
if (number>=32){
this.summon(binary[1]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+15,y:paladinY});
number = number-32;
}else{
this.summon(binary[0]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+15,y:paladinY});
}
if (number>=16){
this.summon(binary[1]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+20,y:paladinY});
number=number-16;
}else{
this.summon(binary[0]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+20,y:paladinY});
}
if (number>=8){
this.summon(binary[1]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+25,y:paladinY});
}else{
this.summon(binary[0]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+25,y:paladinY});
}
if (number>=4){
this.summon(binary[1]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+30,y:paladinY});
number=number-4;
}else{
this.summon(binary[0]);
friend = this.findNearest(this.findFriends());
}
if (number>=2){
this.summon(binary[1]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+35,y:paladinY});
number=number-2;
}else{
this.summon(binary[0]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+35,y:paladinY});
}
if (number>=1){
this.summon(binary[1]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+40,y:paladinY});
number=number-1;
}else{
this.summon(binary[0]);
friend = this.findNearest(this.findFriends());
this.command(friend, “move”, {x:paladinX+40,y:paladinY});
}
};
this.deployLine(???);


#2

Paladins have a deployment property defined on them to determine the units needed. So something like:

this.deployLine(paladin.deployment, paladin.pos.x, paladin.pos.y)

Instead of using this.findNearest(this.findFriends()), you can get the last summoned unit with this.built[this.built.length - 1]. You might be trying to command the wrong minion otherwise…


#3

Thank you. That worked perfectly. Now all I need to do is figure out how to do it for the trinary one! :stuck_out_tongue:


#4

Hey guys, didn’t want to create a new topic, I also have problem with this level, I sort of found a way to summon the right troops, but now I just can’t figure out how to arrange them in neat columns, any suggestions? How could i target each troop, and make him move to the position of the last commanded troop?

this is my code so far

var toBinary = function(decNum){
return parseInt(decNum,10).toString(2);
};

loop{
var paladins = this.findByType("paladin");
for (i=0; i < paladins.length; i++){
    paladin = paladins[i];
    //all binaries looped through
    var binary = toBinary(paladin.deployment);
    
    var soldiersInLine = binary.length;
    var startY = paladin.pos.y;
    var columnY = startY;
    
    
    this.say(binary.length);
    for (j=0; j < binary.length; j++){
        singleBinary = binary[j]; //each binary number by number
       
        if (singleBinary === "0"){
            this.summon("soldier");    
        } else {
            this.summon("archer");    
        }
        friend = this.built[this.built.length-1];
        lastFriend = this.built[0];
  
        // this.say(singleBinary);
    }   
        
}

}


#5

Tell the last thing you built to move the paladin’s y position, then make the x coordinate a number added to another number multiple by j. It might look something like this:

// Assuming my paladin stands at (10,10)
friend = this.built(this.built.length-1);
this.command(friend,"move",{"x":x+(x*i),"y":paladin.pos.y})

where x is a number.


#6

For some reason, I think it’s not showing the first number of the binary number: why? How can I fix it?
Here’s my code:

var Binary = function(Num){
    return parseInt(Num, 10).toString(2);
};
var Trinary = function(Num){
    return parseInt(Num, 10).toString(3);
};
var paladins = hero.findByType("paladin");
var warlocks = hero.findByType("warlock");
for (var i in paladins) {
    var paladin = paladins[i];
    var binary = Binary(paladin.deployment);
    
    for (var j = 0; j < binary.length; j++){
        var abinary = binary[j];
        if (abinary === "0"){
            hero.summon("soldier");
        }
        else if (abinary === "1"){
            hero.summon("archer");
        }
        var friend = hero.built[hero.built.length-1];
        hero.command(friend,"move",{"x":12+(4*j),"y":paladin.pos.y});
    }
}
for (var a in warlocks) {
    var warlock = warlocks[a];
    var trinary = Trinary(warlock.deployment);
    for (var b = 0; b < trinary.length; b++) {
        var atrinary = trinary[b];
        if (atrinary === "0") {
            hero.summon("soldier");
        }
        else if (atrinary === "1") {
            hero.summon("archer");
        }
        else if (atrinary === "2") {
            hero.summon("griffin-rider");
        }
        var friend2 = hero.built[hero.built.length-1];
        hero.command(friend2,"move",{"x":12+(4*b),"y":warlock.pos.y});
    }
}

(I’ll remove the code after this is resolved, if requested)


#7

Hi.
I fixed it this way.

function DecToAnyBase(Number, Base, Length) {
    var baseLine = parseInt(Number, 10).toString(Base).split("");
    if (baseLine.length < Length) {
        baseLine.reverse();
        while(baseLine.length < Length) {
            baseLine.push("0");
        }
        baseLine.reverse();
    }
    return baseLine;
}

function TacticalFormationOfTroops (commanders) {
    var formationLine = [];
    for (var i in commanders) {
        var commander = commanders[i];
        if (commander.type == "paladin") {
            formationLine = DecToAnyBase(commander.deployment, 2, 8);
        } else if (commander.type == "warlock") {
            formationLine = DecToAnyBase(commander.deployment, 3, 8);
        }
        for (var j = 0; j <= formationLine.length - 1; j++){
            if (formationLine[j] === "0"){
                hero.summon("soldier");
            } else if (formationLine[j] === "1"){
                hero.summon("archer");
            } else if (formationLine[j] === "2") {
                hero.summon("griffin-rider");
            }
            var troop = hero.built[hero.built.length-1];
            hero.command(troop,"move",{x:20+(4*j),y:commander.pos.y});
        }
    }
}

TacticalFormationOfTroops(hero.findFriends());