Sharing my "Form Shield" and "Form Ring" commands :D

Here are 2 commands I’ve scribbled up I figured could be very usefull to other people. :slight_smile:

this.formRing = function(units, place, size) {
	for(var u=0; u < units.length; u++) {
		var angle = Math.PI * 2 * (u / units.length);
		var defendPos = {x:place.x, y:place.y};
		defendPos.x += size * Math.cos(angle);
		defendPos.y += size * Math.sin(angle);
		this.command(units[u], "defend", defendPos);
	}
};


this.formShield = function(units, place, size, target){
	var tangR = 0;
	if(target){
		tangR = Math.atan2(target.pos.y-place.y, target.pos.x-place.x);
	}
	for(var u=0; u < units.length; u++) {
		var angle = Math.PI * 2 * (u / units.length);
		var realU = u;
		if(tangR){
			angle = tangR+(Math.PI * (Math.cos(angle/2)-1));
			realU = Math.floor(((tangR)/(Math.PI*2))*units.length)-u;
			while(realU >= units.length){
				realU -= units.length;
			}
			while(realU < 0){
				realU += units.length;
			}
		}
		var defendPos = {x:place.x, y:place.y};
		defendPos.x += size * Math.cos(angle);
		defendPos.y += size * Math.sin(angle);
		this.command(units[realU], "defend", defendPos);
	}
};

Example use:

this.formRing(this.findByType("archer"), {x:77,y:61}, 6);
this.formShield(this.findByType("soldier"), this.pos, 12, enemy);

Where the shield function will automatically do this when there is a target:

4 Likes

Very nice. If you would allow me, I would like to put them in my Brawl levels.

Here are the Python versions, a bit simplified, using vectors:

def formRing(units, center, size):
    offset = Vector(size, 0)
    angle = Math.PI * 2 / len(units)
    for friend in units:
        defendPos = Vector.add(center, offset)
        self.command(friend, "defend", defendPos)
        offset = Vector.rotate(offset, angle)
def formShield(units, center, size, target):
    #
    # coming soon!
    #

By the way, did you know that if you command your allies to defend you (= self.command(friend, "defend", self) ) they will automatically form a ring around you?

[put screenshot here]

They only form a ring if there are enemies. Otherwise, they crowd you and push you around.