Lesson 3 - Attack
Introduction
From here on out, we'll focus on Javascript. We'll be building up your Dragons' A.I. "brains". In this Solo Quest, you'll learn how to command your Dragon to attack.Remember, always put your Javascript code within the
<script></script> tags of your Dragon A.I. code.
Javascript run function
As we saw in the previous lesson, your run function is your main function that gets executed on every round. This where you'll fill in the "brains" of your Dragons and tell them what to do on each round.
You started with an empty
run function that looked like this:function run(state)
{
}
Now we'll start adding to it to command your Dragons do cool stuff. For example, attack an enemy Dragon.
Game.attack()
The way you make your dragons do stuff in the game is by calling Game functions. For example, Game.attack(). But there is a specific way you must call the Game.attack() function:
Game.attack(unitID, target)- This tells your unit (specified by
unitId) to attack atarget. targetcan be an enemy unit's ID, or a position in the game board. E.g., {x: 0, y: 0}.
unitId and target are called "arguments" for the attack function. You must provide both the unitId and target arguments each time you call the function. But they can be different each time. E.g. you can attack one target in one turn, and another target in another turn.
So now you know to call the
Game.attack() function to attack. Next, let's talk about how to fill the unitId and target arguments.
Helper.getMyUnits()
In addition to the Game functions (that let you perform actions in the game), you also get Helper functions. These functions are provided to help beginners get started. As you level up your coding skills, you will outgrow these Helper functions and find yourself replacing them with your own versions.
Helper.getMyUnits(state)- This function returns you an array of your Dragon units. You can think of an array as a list. Like a shopping list. A list has many items, called "elements" in programming.
- Arrays are 0-indexed. That is, you say
array[0]for the 1st element in the list,array[1]for the 2nd element in the list, and so on.
Let's start filling in your
run function:
function run(state)
{
// get array of all my units
var myUnits = Helper.getMyUnits(state);
// get the first dragon in the array
var dragon = myUnits[0];
// get first dragon's ID
var dragonId = dragon.id;
}
Great! Now we have a dragon's unit ID for the 1st argument of
Game.attack(unitId, target). Next we'll learn how to get an enemy unit for the 2nd argument target.
Helper.getClosestEnemy()
One strategy for choosing which enemy to attack is by picking the closest enemy to your Dragon.
Helper.getClosestEnemy(state, dragon)- Pass this function the
statefrom yourrunfunction and one of your Dragons. - It will return you that Dragon's closest enemy.
Now we have all the pieces to finish filling in your
run function so it looks like the following:
function run(state)
{
// get array of all my units
var myUnits = Helper.getMyUnits(state);
// get the first Dragon in the array
var dragon = myUnits[0];
// get first Dragon's ID
var dragonId = dragon.id;
// get this Dragon's closest enemy
var enemyFound = Helper.getClosestEnemy(state, dragon);
if (!enemyFound)
{
// yay! no more enemies -- nothing left to do.
// calling "return" stops this function from
// executing any other code below.
return;
}
// get enemy Dragon's ID
var enemyId = enemyFound.unit.id;
// attack!
Game.attack(dragonId, enemyId);
}
Attacks vs distance
One thing you'll notice in this Quest is that Dragon attacks do more damage to near targets and less damage to far targets. In later lessons, you will learn how to use this to your advantage.Cheatcode
Spoiler alert! Click to reveal solution.
<!DOCTYPE html>
<html>
<body>
<div id="display">hello</div>
<script>
function run(state)
{
// get array of all my units
var myUnits = Helper.getMyUnits(state);
// get the first Dragon in the array
var dragon = myUnits[0];
// get first Dragon's ID
var dragonId = dragon.id;
// get this Dragon's closest enemy
var enemyFound = Helper.getClosestEnemy(state, dragon);
if (!enemyFound)
{
// yay! no more enemies -- nothing left to do.
// calling "return" stops this function from
// executing any other code below.
return;
}
// get enemy Dragon's ID
var enemyId = enemyFound.unit.id;
// attack!
Game.attack(dragonId, enemyId);
}
Game.register(run);
</script>
</body>
</html>