JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
draw connections between selected tiles
[hexbog.git] / main.coffee
index 02b1bdb..147b76a 100644 (file)
@@ -225,7 +225,7 @@ init_board_layout = () ->
                                unless is_top_tile and fw_other is -1
                                        push i + columns[col_num].height + fw_other
                        # will be dereferenced later
-                       space.neighbors = neighbors.clone() # FIXME ?remove ``.clone()``
+                       space.neighbors = neighbors
                col_offset += column.height
        # convert all space.neighbors arrays from containing space ids to referencing the space
        for s in spaces
@@ -332,9 +332,13 @@ unselect_tile = ->
        update_selection_display()
 
 _unselect_tile = ->
-       html_tile = selected.pop().dom
-       html_tile.removeClass 'selected_word'
-       html_tile.removeClass 'selected'
+       tile = selected.pop()
+       dom = tile.dom
+       if tile.connector?
+               tile.connector.remove()
+               delete tile.connector
+       dom.removeClass 'selected_word'
+       dom.removeClass 'selected'
 
 unselect_all = ->
        while selected.length
@@ -370,8 +374,106 @@ unsink = (tile) ->
        tile.text = new_letter().letter
        tile.dom.html tile.text
 
+
+# top-level key is word length
+# arrays are [difficulty] level, easiest to hardest
+booms = {
+       3: {
+               neighbors: {
+                       flips: [1,1,1,1,0]
+                       force: [2,2,1,1,1,1,0]
+               }
+               neighbor_neighbors: {
+                       flips: [0]
+                       force: [0]
+               }
+               board: {
+                       flips: [0]
+                       force: [0]
+               }
+       }
+       4: {
+               neighbors: {
+                       flips: ['all', 4,4,4,4,4,3,3,2,2,1]
+                       force: [4,3,3,3,3,3,2]
+               }
+               neighbor_neighbors: {
+                       flips: [0]
+                       force: [2,2,2,1,1,1,0]
+               }
+               board: {
+                       flips: [0]
+                       force: [0]
+               }
+       }
+       5: {
+               neighbors: {
+                       flips: ['all','all','all','all',5,5,5,4,4,4,3,3,3,2]
+                       force: [6,6,6,6,5,5,5,4,4,4,3]
+               }
+               neighbor_neighbors: {
+                       flips: [2,2,2,2,2,2,1,1,1,1,0]
+                       force: [4,3,3,3,3,2,2,2,1]
+               }
+               board: {
+                       flips: [0]
+                       force: [0]
+               }
+       }
+       6: {
+               neighbors: {
+                       flips: ['all','all','all','all','all',9,9,9,9,8,8,8,7,7,7,6,6,6,5,5,5,4]
+                       force: [9,9,9,9,9,8,8,8,7]
+               }
+               neighbor_neighbors: {
+                       flips: [5,5,5,5,5,5,5,4,4,4,3,3,3,2,2,2,1]
+                       force: [6,6,5,5,4,4,3,3,2]
+               }
+               board: {
+                       flips: [0]
+                       force: [0]
+               }
+       }
+       7: {
+               neighbors: {
+                       flips: ['all']
+                       force: [10]
+               }
+               neighbor_neighbors: {
+                       flips: ['all','all','all','all',9,8,7,6,5,4,3,2]
+                       force: [10]
+               }
+               board: {
+                       flips: [0]
+                       force: [5,4,3,2,1]
+               }
+       }
+       lots: {
+               neighbors: {
+                       flips: [0]
+                       force: [0]
+               }
+               neighbor_neighbors: {
+                       flips: [0]
+                       force: [0]
+               }
+               board: {
+                       flips: ['all']
+                       force: [10]
+               }
+       }
+}
+difficulty_level = 0
+next_level_at = 200
+adjust_difficulty_level = ->
+       while score > next_level_at
+               difficulty_level += 1
+               next_level_at *= 1.4
+
+
 # remove the selected tiles from the board, create new tiles, and slide everything into place
 blip_selection = ->
+       adjust_difficulty_level()
        word_length = selected_word().length
        faders = selected
        selected = []
@@ -394,77 +496,59 @@ blip_selection = ->
        # convert to arrays so we can sort, etc
        nneighbors = (v for k, v of nneighbors)
        neighbors = (v for k, v of neighbors)
-       boom = [
-               {
-                       tiles: neighbors,
-                       up: [],
+       areas = {
+               neighbors: {
+                       tiles: neighbors
+                       up: []
                        down: []
-               },
-               {
-                       tiles: nneighbors,
-                       up: [],
+               }
+               neighbor_neighbors: {
+                       tiles: nneighbors
+                       up: []
                        down: []
                }
-       ]
-       for n in boom
-               for t in n.tiles
+               board: {
+                       tiles: (space.tile for space in spaces)
+                       up: []
+                       down: []
+               }
+       }
+       for k, v of areas
+               for t in v.tiles
                        if t.hp is 0
-                               n.down.push t
+                               v.down.push t
                        else
-                               n.up.push t
-       switch word_length
-               when 3
-                       boom[0].flips = 1
-                       boom[0].force = 2
-                       boom[1].flips = 0
-                       boom[1].force = 0
-               when 4
-                       boom[0].flips = 'all'
-                       boom[0].force = 4
-                       boom[1].flips = 0
-                       boom[1].force = 2
-               when 5
-                       boom[0].flips = 'all'
-                       boom[0].force = 6
-                       boom[1].flips = 2
-                       boom[1].force = 4
-               when 6
-                       boom[0].flips = 'all'
-                       boom[0].force = 10
-                       boom[1].flips = 5
-                       boom[1].force = 6
-               when 7
-                       boom[0].flips = 'all'
-                       boom[0].force = 10
-                       boom[1].flips = 'all'
-                       boom[1].force = 10
+                               v.up.push t
+       if word_length < 8
+               boom = booms[word_length]
+       else
+               boom = booms.lots
+       for area_name, effects of boom
+               area = areas[area_name]
+               if difficulty_level < effects.flips.length
+                       flips = effects.flips[difficulty_level]
                else
-                       boom[0].flips = 0
-                       boom[0].force = 0
-                       boom[1].flips = 0
-                       boom[1].force = 0
-                       # unsink/heal the whole board
-                       for s in spaces
-                               if s.tile.hp is 0
-                                       unsink s.tile
-                               else
-                                       s.tile.new_hp = 10
-       for b in boom
-               if b.flips is 'all' or b.flips >= b.down.length
-                       for t in b.down
+                       flips = effects.flips.last()
+               if flips is 'all' or flips >= area.down.length
+                       for t in area.down
                                unsink t
                else
-                       down_count = b.down.length
-                       while b.flips > 0 and down_count
-                               b.flips -= 1
+                       down_count = area.down.length
+                       flips_left = flips
+                       while flips_left > 0 and down_count > 0
+                               flips_left -= 1
                                flipper = Math.floor(Math.random() * down_count)
-                               unsink b.down[flipper]
+                               unsink area.down[flipper]
                                down_count -= 1
                                # move the last tile back into range
-                               b.down[flipper] = b.down[down_count]
-               if b.force > 0
-                       for t in b.up
-                               t.new_hp = t.hp + b.force
+                               area.down[flipper] = area.down[down_count]
+               if difficulty_level < effects.force.length
+                       force = effects.force[difficulty_level]
+               else
+                       force = effects.force.last()
+               if force > 0
+                       for tile in area.up
+                               tile.new_hp = tile.hp + force
        for s in spaces
                s.tile.new_hp ?= s.tile.hp - 1
                if s.tile.new_hp < 0
@@ -519,10 +603,12 @@ score_for = (word) -> Math.round(Math.pow(1.7, word.length))
 activate_selection = ->
        word = selected_word()
        if word.length < 3
+               # should only happen when trying to blip a word with the keyboard
                # FIXME make this a hint
                log "Too short: \"#{word}\""
                return
        unless is_word word
+               # should only happen when trying to blip a word with the keyboard
                # FIXME make this automatically part of the selection display
                log "Not on word list: \"#{word}\""
                return
@@ -545,9 +631,48 @@ show_definition = (word, type, definition, language) ->
        html += '<div id="definition_credit">Definition &copy;<a href="http://en.wiktionary.org/" target="_blank">wiktionary.org</a> CC-BY-SA</div>'
        $definition_body.html html
 
+connector_width = 11
+connector_radius = 5
+connector_slant = 12
+add_connector = (tile, horiz, vert) ->
+       style = {}
+       switch horiz
+               when 'left'
+                       style.right = '100%'
+               when 'mid'
+                       style.left = 21 - connector_radius
+               when 'right'
+                       style.left = '100%'
+       switch vert
+               when 'top'
+                       style.bottom = '100%'
+               when 'up'
+                       style.top = 21 - connector_radius - connector_slant
+               when 'down'
+                       style.top = 21 - connector_radius + connector_slant
+               when 'bot'
+                       style.top = '100%'
+       tile.connector = $("<div class=\"connector\"></div>").css style
+       tile.dom.append tile.connector
 
 select_tile = (tile) ->
        selected.push tile
+       if selected.length > 1
+               prev = selected[selected.length - 2]
+               if prev.space.top_px < tile.space.top_px
+                       if prev.space.left_px < tile.space.left_px
+                               add_connector tile, 'left', 'up'
+                       else if prev.space.left_px is tile.space.left_px
+                               add_connector tile, 'mid', 'top'
+                       else
+                               add_connector tile, 'right', 'up'
+               else
+                       if prev.space.left_px < tile.space.left_px
+                               add_connector tile, 'left', 'down'
+                       else if prev.space.left_px is tile.space.left_px
+                               add_connector tile, 'mid', 'bot'
+                       else
+                               add_connector tile, 'right', 'down'
        update_selection_display()
 
 new_tile = (space, y) ->
@@ -569,7 +694,7 @@ new_tile = (space, y) ->
        space.tile = tile
 
        html_tile.click ->
-               return if tile.hp < 1
+               return unselect_all() if tile.hp < 1
                word = selected_word()
                if tile in selected
                        if selected_word().length > 2 and is_word(word) and tile is selected.last()
@@ -647,14 +772,14 @@ extract_wiktionary_definiton = (html) ->
        #   archive,codebase,data,usemap: <object>
        #                           href: <link>
        #                 id,class,style: background: url(foo.png), etc
-       html = html.replace /(src|onload|archive|codebase|data|usemap|href|style|id|class)=['"][^"']*['"]/ig, '', html
+       html = html.replace /[ ]?[a-z]+=['"][^"']*['"]/ig, '', html
+       html = html.replace /<\/?(audio|source|a|span|table|tr|td|table)>/ig, '', html
+       html = html.replace /\[edit\]/ig, '', html
 
        elements = $(html)
 
        valid_parts = ["Abbreviation", "Adjective", "Adverb", "Article", "Cardinal number", "Conjunction", "Determiner", "Interjection", "Noun", "Numeral", "Particle", "Preposition", "Pronoun", "Verb"]
 
-       edit_link_regex = new RegExp(' ?\\[edit\\] ?')
-
        elements.each (i, el) ->
                #which tag: el.tagName
                if el.tagName is 'H2'
@@ -663,10 +788,10 @@ extract_wiktionary_definiton = (html) ->
                        if found
                                return false # break
                        part = false # mark us not being in a definition section unless the next section finds a part of speach header
-                       language = $(el).text().replace(edit_link_regex, '')
+                       language = $(el).text()
                if language and el.tagName is 'H3' or el.tagName is 'H4' # eg yak def uses one for english and one for dutch
                        part = false
-                       text = $(el).text().replace(edit_link_regex, '')
+                       text = $(el).text()
                        for p in valid_parts
                                if text is "#{p}"
                                        part = p.toLowerCase()
@@ -717,6 +842,8 @@ look_up_definition = (word) ->
 start_over = ->
        selected = []
        score = 0
+       difficulty_level = 0
+       next_level_at = 200
        $score_display.html score
        for s in spaces
                selected.push s.tile