 
     if @map.out_of_bound(destroy_area)
       @bom_on_destroy = true
-      @model.destroy()
+      @model.energy -= @max_depend_point
     else
       hit_map_units = @map.units_at(destroy_area)
       _.each(hit_map_units, (unit) =>
-        @model.energy -= unit.defend(@model, destroy_area)
+        defend_point = unit.defend(@model, destroy_area)
+        @bom_on_destroy = (defend_point == @max_depend_point)
+        @model.energy -= defend_point
       )
-      @model.destroy() if @model.energy <= 0
+    @model.destroy() if @model.energy <= 0
   destroy_area: ->
     switch @direction
       when 0

      when 270
        new MapArea2D(@x1 - factor * @width(), @y1, @x2, @y2)

class Map2D
  max_x: 520
  max_y: 520
  default_width: 40
  default_height: 40
  map_units: [] # has_many map_units
  constructor: (@battle_field, @canvas, @scene) ->
    @image = document.getElementById('resources')

        @inputs[key].push("keydown")

class EnemyAICommander extends Commander
  constructor: (@battle_field_object) ->
    super(@battle_field_object)
    @map = @battle_field_object.battle_field.map
    @target = null
  next: ->
    # move towards home
    # TODO attack user tank or home
    return @calculate_shortest_path() if @target is null
    if @target.equals(@battle_field_object.view.area)
      @target = null
    else
      # turn
      # move
  calculate_shortest_path: () ->
    @start_vertex = @map.home_vertex
    @end_vertex = _.first(@map.vertexes_at(@battle_field_object.view.area))
    @intialize_single_source()
    # dijkstra shortest path
    searched_vertexes = []
    remain_vertexes = @all_v()
    while _.size(remain_vertexes) > 0
      u = @extract_min(remain_vertexes)
      remain_vertexes = _.without(remain_vertexes, u)
      searched_vertexes.push u
      _.each(u.siblings, (v) => @relax(u, v, @map.weight(@battle_field_object, u, v)))
      break if u is @end_vertex
    @target = @pi[@end_vertex.vx][@end_vertex.vy]

  intialize_single_source: () ->
    @d = []
    @pi = []
    for x in _.range(0, @map.vertexes_columns)
      column_ds = []
      column_pi = []
      for y in _.range(0, @map.vertexes_rows)
        column_ds.push(@map.infinity)
        column_pi.push(null)
      @d.push(column_ds)
      @pi.push(column_pi)
    @d[@start_vertex.vx][@start_vertex.vy] = 0

  all_v: () ->
    _.flatten(@map.vertexes)

  relax: (u, v, w) ->
    if @d[v.vx][v.vy] > @d[u.vx][u.vy] + w
      @d[v.vx][v.vy] = @d[u.vx][u.vy] + w
      @pi[v.vx][v.vy] = u

  extract_min: (vertexes) ->
    _.min(vertexes, (vertex) =>
      @d[vertex.vx][vertex.vy]
    )

class MissileCommander extends Commander
  next: -> @move("start")
