Pull to refresh

HabraWars: Графический debug

Reading time2 min
Views601
С идеей HabraWars Вы уже наверняка знакомы, крайний из топиков анонсирует первый турнир. Который случиться сегодня. Регистрация новых участников будет после 20 ноября.

Правил и кода sample robot достаточно чтобы начать писать своего покорителя пьедестала. Но отладка при помощи только консоли FireBug может быть не достаточно наглядна. Гораздо удобнее получать информацию а мыслях робота прямо на арену.

А для этого достаточно всего лишь, что бы очистка поля происходила в начале тика и в функцию мыслительного процесса передавался Canvas:

...
this.culc_frame = function() {
// Mutex
if (mutex)
return;
else
mutex = true;

// clear Area is now separated from draw
var padding = 10;
this.canvas.fillStyle = 'black';
this.canvas.fillRect(0, 0, this.canvas_width - 1, this.canvas_height - 1);
this.canvas.strokeStyle = 'white';
this.canvas.lineWidth = 1;
this.canvas.strokeRect(padding, padding, this.arena_width, this.arena_height);

...
control = this.shells[i].action(robot_info[i], robot_info.filter(exclude_myself), shot_info, {arena_width: this.arena_width, arena_height: this.arena_height, robot_radius: this.robot_radius, shot_speed: 12},engine.canvas);
...
// Draws current frame on canvas from string
// Using string params useful for battle replays
this.draw = function(situation) {

// Clear Background
var padding = 10;
this.canvas.fillStyle = 'black';
this.canvas.fillRect(0, 0, this.canvas_width - 1, this.canvas_height - 1);
this.canvas.strokeStyle = 'white';
this.canvas.lineWidth = 1;
this.canvas.strokeRect(padding, padding, this.arena_width, this.arena_height);

// Draw robots
current_index = 0;
...


А в коде робота добавить параметр:
this.action = function(my_state, enemies, shots, params,this_canvas)
Теперь в теле мыслительной функции можно рисовать отладку:
// На пример места, куда попадут летящие сейчас снаряды
for (var i = 0; i < shots.length; i++)
{
var shot_dest = get_shot_destination(shots[i]);
this_canvas.globalAlpha = 0.3;
this_canvas.beginPath();
this_canvas.arc(10 + shot_dest.x, 10 + shot_dest.y, 20, 0, Math.PI * 2, true);
this_canvas.closePath();
this_canvas.fillStyle = 'red';
this_canvas.fill();
this_canvas.globalAlpha = 1;
}

И тогда прибудет удобство и наглядность:
image
Или ещё удобно показывать прогноз положения вражеского робота:
image

P.S.: К сожалению до тех пор пока эти изменение не появится в основном движке боёв, придётся разрабатывать своего робота в офф-лайне. И естественно перед переносом кода в онлайн необходимо убрать упоминания о this_canvas из кода робота.
Tags:
Hubs:
Total votes 13: ↑10 and ↓3+7
Comments3

Articles