forked from JavascriptBattle/hero-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hero.js
234 lines (194 loc) · 7.73 KB
/
hero.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
The only function that is required in this file is the "move" function
You MUST export the move function, in order for your code to run
So, at the bottom of this code, keep the line that says:
module.exports = move;
The "move" function must return "North", "South", "East", "West", or "Stay"
(Anything else will be interpreted by the game as "Stay")
The "move" function should accept two arguments that the website will be passing in:
- a "gameData" object which holds all information about the current state
of the battle
- a "helpers" object, which contains useful helper functions
- check out the helpers.js file to see what is available to you
(the details of these objects can be found on javascriptbattle.com/#rules)
This file contains four example heroes that you can use as is, adapt, or
take ideas from and implement your own version. Simply uncomment your desired
hero and see what happens in tomorrow's battle!
Such is the power of Javascript!!!
*/
//TL;DR: If you are new, just uncomment the 'move' function that you think sounds like fun!
// (and comment out all the other move functions)
// // The "Northerner"
// // This hero will walk North. Always.
// var move = function(gameData, helpers) {
// var myHero = gameData.activeHero;
// return 'North';
// };
// // The "Blind Man"
// // This hero will walk in a random direction each turn.
// var move = function(gameData, helpers) {
// var myHero = gameData.activeHero;
// var choices = ['North', 'South', 'East', 'West'];
// return choices[Math.floor(Math.random()*4)];
// };
// // The "Priest"
// // This hero will heal nearby friendly champions.
// var move = function(gameData, helpers) {
// var myHero = gameData.activeHero;
// if (myHero.health < 60) {
// return helpers.findNearestHealthWell(gameData);
// } else {
// return helpers.findNearestTeamMember(gameData);
// }
// };
// // The "Unwise Assassin"
// // This hero will attempt to kill the closest enemy hero. No matter what.
// var move = function(gameData, helpers) {
// var myHero = gameData.activeHero;
// if (myHero.health < 30) {
// return helpers.findNearestHealthWell(gameData);
// } else {
// return helpers.findNearestEnemy(gameData);
// }
// };
// // The "Careful Assassin"
// // This hero will attempt to kill the closest weaker enemy hero.
// var move = function(gameData, helpers) {
// var myHero = gameData.activeHero;
// if (myHero.health < 50) {
// return helpers.findNearestHealthWell(gameData);
// } else {
// return helpers.findNearestWeakerEnemy(gameData);
// }
// };
var hero_helpers = {};
hero_helpers.findNearestTeamMemberHealInfo = function(gameData, helpers) {
var hero = gameData.activeHero;
var board = gameData.board;
//Get the path info object
var pathInfoObject = helpers.findNearestObjectDirectionAndDistance(board, hero, function(heroTile) {
return heroTile.type === 'Hero' && heroTile.team === hero.team && heroTile.health < 100;
});
//Return the direction that needs to be taken to achieve the goal
return pathInfoObject;
};
hero_helpers.findNearestWeakerEnemy = function(gameData, helpers) {
var hero = gameData.activeHero;
var board = gameData.board;
//Get the path info object
var pathInfoObject = helpers.findNearestObjectDirectionAndDistance(board, hero, function(enemyTile) {
return enemyTile.type === 'Hero' && enemyTile.team !== hero.team && enemyTile.health < hero.health;
});
//Return the direction that needs to be taken to achieve the goal
//If no weaker enemy exists, will simply return undefined, which will
//be interpreted as "Stay" by the game object
return pathInfoObject;
};
hero_helpers.findNearestNonTeamDiamondMineDirectionAndDistance = function(gameData, helpers) {
var hero = gameData.activeHero;
var board = gameData.board;
//Get the path info object
var pathInfoObject = helpers.findNearestObjectDirectionAndDistance(board, hero, function(mineTile) {
if (mineTile.type === 'DiamondMine') {
if (mineTile.owner) {
return mineTile.owner.team !== hero.team;
} else {
return true;
}
} else {
return false;
}
}, board);
//Return the direction that needs to be taken to achieve the goal
return pathInfoObject;
};
hero_helpers.findNearestGrave = function(gameData, helpers) {
return helpers.findNearestObjectDirectionAndDistance(gameData.board, gameData.activeHero, function(tile) {
if (tile.subType === 'Bones') {
return true;
} else {
return false;
}
}, gameData.board);
}
// The opportunist
var move = function(gameData, helpers) {
var myHero = gameData.activeHero;
//Get stats on the nearest health well
var healthWellStats = helpers.findNearestObjectDirectionAndDistance(gameData.board, myHero, function(boardTile) {
if (boardTile.type === 'HealthWell') {
return true;
}
});
var teamMemberInfo = hero_helpers.findNearestTeamMemberHealInfo(gameData, helpers);
var enemy = hero_helpers.findNearestWeakerEnemy(gameData, helpers);
var mine = hero_helpers.findNearestNonTeamDiamondMineDirectionAndDistance(gameData, helpers);
var grave = hero_helpers.findNearestGrave(gameData, helpers);
var anyEnemy = helpers.findNearestEnemy(gameData);
if (myHero.health < 65) {
//Heal no matter what if low health
return healthWellStats.direction;
} else if (enemy && enemy.health <= 30 && enemy.health >= 20 && enemy.distance === 1) {
return enemy.direction;
} else if (myHero.health < 100 && healthWellStats.distance === 1) {
//Heal if you aren't full health and are close to a health well already
return healthWellStats.direction;
} else if(mine && mine.distance === 1) {
return mine.direction;
} else if(teamMemberInfo && teamMemberInfo.distance === 1) {
return teamMemberInfo.direction;
} else if(enemy && enemy.distance === 1) {
return enemy.direction;
} else if(grave && grave.distance === 1) {
return grave.direction;
} else if(mine && mine.distance <= 3) {
return mine.direction;
} else if(enemy && enemy.distance <= 2) {
return enemy.direction;
} else if(teamMemberInfo && teamMemberInfo.distance <= 2) {
return teamMemberInfo.direction;
} else if(mine) {
return mine.direction;
} else if(myHero.health < 100) {
return healthWellStats.direction;
} else if(grave && grave.distance <= 3) {
return grave.direction;
} else if(enemy) {
return enemy.direction;
} else if(teamMemberInfo) {
return teamMemberInfo.direction;
} else if(anyEnemy) {
return anyEnemy;
}
return mine.direction;
};
// // The "Selfish Diamond Miner"
// // This hero will attempt to capture diamond mines (even those owned by teammates).
// var move = function(gameData, helpers) {
// var myHero = gameData.activeHero;
// //Get stats on the nearest health well
// var healthWellStats = helpers.findNearestObjectDirectionAndDistance(gameData.board, myHero, function(boardTile) {
// if (boardTile.type === 'HealthWell') {
// return true;
// }
// });
// var distanceToHealthWell = healthWellStats.distance;
// var directionToHealthWell = healthWellStats.direction;
// if (myHero.health < 40) {
// //Heal no matter what if low health
// return directionToHealthWell;
// } else if (myHero.health < 100 && distanceToHealthWell === 1) {
// //Heal if you aren't full health and are close to a health well already
// return directionToHealthWell;
// } else {
// //If healthy, go capture a diamond mine!
// return helpers.findNearestUnownedDiamondMine(gameData);
// }
// };
// // The "Coward"
// // This hero will try really hard not to die.
// var move = function(gameData, helpers) {
// return helpers.findNearestHealthWell(gameData);
// }
// Export the move function here
module.exports = move;