# Ogres are trying to kill your reindeer!
# Keep your archers back while summoning soldiers to attack.
def pickUpCoin():
coin = self.findNearest(self.findItems())
if coin:
self.move(coin.pos)
pass
def summonTroops():
if self.gold > self.costOf("soldier"):
self.summon("soldier")
# Summon soldiers if you have the gold.
pass
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
enemy = soldier.findNearestEnemy
if enemy:
self.command(soldier, "attack", enemy)
# Soldiers should attack enemies.
pass
# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandarcher(archer):
enemy = archer.findNearest(archer.findenemies)
if enemy and archer.distanceto(enemy) < 25:
self.command(archer, "attack", enemy)
else:
self.command(archer, "move", archer.pos)
loop:
pickUpCoin()
summonTroops()
friends = self.findFriends()
for friend in friends:
if friend.type == "soldier":
# This friend will be assigned to the variable soldier in commandSoldier
commandSoldier(friend)
elif friend.type == "archer":
commandarcher(friend)
apparently i need to find the distance to a target unit :
def commandarcher(archer):
enemy = archer.findNearest(archer.findenemies)
if enemy and archer.distanceto(enemy) < 25:
self.command(archer, "attack", enemy)
else:
self.command(archer, "move", archer.pos)
This should be archer.findNearest(self.findEnemies()) #Although I am not very sure.
This is because our hero (self) can findEnemies with goggles/matrix lens. Archer cannot. archer.findNearest is correct though. This is because this is again not the archerās ability, it is our heroās ability thanks to glasses/matrix lens to find the closest enemy to the archer. Hero sees it, not the archer.
In this code, after the archer has attacked someone, its position may change (may go further from reindeer). Then it will not return to original position. So, I employ a little trick. I change the function for looping around friends to loop around self.findByType(āarcherā) with a for loop with index. It look like:
archers = self.findByType(āarcherā)
for index, archer in enumerate(archers):
same code as your except of for nearby enemy case, I ask them to defend fixed positions:
also; index, archer, archers are sent as arguments to the def commandarcher() ā¦ i.e. commandarcher(index, archer, archers)
@ZadonX - I suppose in this code combat discourse, thanking for small but productive helps are done by clicking on the ālikeā button, heart shaped and not by replies. Thereby minimizing text and allowing others to quickly glance over to the actual solution. Also, keeping a track of helpful users in the forum. Happy to help anyway
loop:
pickUpCoin()
summonTroops()
friends = self.findFriends()
for friend in friends:
if friend.type == āsoldierā:
# This friend will be assigned to the variable soldier in commandSoldier
commandSoldier(friend)
elif friend.type == āarcherā:
commandArcher(friend)
pass
Format your code by putting 3 backquotes (left-top button on the keyboard) around your code
Inside the backquotes you can indent your code using spaces.
Backquotes must be on a line by themselves without spaces or tabs
Here is my code.It says commandArcher isnāt identified.
Ogres are trying to kill your reindeer!
Keep your archers back while summoning soldiers to attack.
def pickUpCoin():
# Collect coins.
items = self.findItems()
nearestCoin = self.findNearest(items)
if nearestCoin:
self.move(nearestCoin.pos)
pass
def summonTroops():
# Summon soldiers if you have the gold.
if self.costOf(āsoldierā)<self.gold:
self.summon(āsoldierā)
pass
This function has an argument named soldier.
Arguments are like variables.
The value of an argument is determined when the function is called.
def commandSoldier(soldier):
# Soldiers should attack enemies.
for soldier in self.findFriends():
enemy = soldier.findNearestEnemy()
if enemy:
self.command(soldier, āattackā, enemy)
pass
Write a commandArcher function to tell your archers what to do!
It should take one argument that will represent the archer passed to the function when itās called.
Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandarcher(archer):
enemy = archer.findNearest(archer.findenemies)
if enemy and archer.distanceto(enemy) < 25:
self.command(archer, āattackā, enemy)
else:
self.command(archer, āmoveā, archer.pos)
loop:
pickUpCoin()
summonTroops()
friends = self.findFriends()
for friend in friends:
if friend.type == āsoldierā:
# This friend will be assigned to the variable soldier in commandSoldier
commandSoldier(friend)
elif friend.type == āarcherā: #commandArcher(friend)
commandArcher(friend)
pass
Thank you SO much. My archers kept moving, but now they donāt, and I achieved the bonus objective in addition to the main objective. That is a great code!
May I have some help please? The archers that are there always move about despite the code I have written. Here is my code:
// Ogres are trying to kill your reindeer!
// Keep your archers back while summoning soldiers to attack.
hero.pickUpCoin = function() {
// Collect coins.
var coin = hero.findNearest(hero.findItems());
if (coin) {
hero.move(coin.pos);
}
};
hero.summonTroops = function() {
// Summon soldiers if you have the gold.
if (hero.gold >= hero.costOf("soldier")) {
hero.summon("soldier");
}
};
// This function has an argument named soldier.
// Arguments are like variables.
// The value of an argument is determined when the function is called.
hero.commandSoldier = function(soldier) {
// Soldiers should attack enemies.
for(var j = 0; j < friends.length; j++) {
var friend = friends[i];
var enemy = friend.findNearestEnemy();
if (enemy) {
hero.command(friend, "attack", enemy);
}
}
};
// Write a commandArcher function to tell your archers what to do!
// It should take one argument that will represent the archer passed to the function when it's called.
// Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
hero.commandArcher = function(friend) {
var archerEnemy = friend.findNearestEnemy();
if (archerEnemy && friend.distanceTo(archerEnemy) < 10) {
hero.command(friend, "attack", enemy);
} else {
}
};
while(true) {
hero.pickUpCoin();
hero.summonTroops();
var friends = hero.findFriends();
for(var i = 0; i < friends.length; i++) {
var friend = friends[i];
if(friend.type == "soldier") {
// This friend will be assigned to the variable soldier in commandSoldier
hero.commandSoldier(friend);
} else if(friend.type == "archer") {
// Be sure to command your archers.
hero.commandArcher(friend);
}
}
}
I kinda feel like itās a bug but it probably isnāt.
The problem is, before you issue the first command to an unit, it will act on its own using a basic AI which usually just walks towards to and attacks the nearest enemy.
You are only commanding the archers when they are 10m away from an enemy, so while that doesnāt happen you donāt send any commands to them, which results in the archers doing whatever they want until you start commanding them.
To fix this, you can add a command in the else block inside the commandArcher function telling them to retreat or hold their positions (command them to "move" to a safe place, or to where they are already standing).