<commit_before>(ns zetawar.views
  (:require
   [cljsjs.clipboard]
   [cljsjs.react-bootstrap]
   [clojure.string :as string]
   [datascript.core :as d]
   [posh.reagent :as posh]
   [reagent.core :as r :refer [with-let]]
   [taoensso.timbre :as log]
   [zetawar.data :as data]
   [zetawar.db :refer [e qe]]
   [zetawar.events.ui :as events.ui]
   [zetawar.game :as game]
   [zetawar.players :as players]
   [zetawar.site :as site]
   [zetawar.subs :as subs]
   [zetawar.tiles :as tiles]
   [zetawar.util :refer [breakpoint inspect only oonly]]
   [zetawar.views.common :refer [footer navbar]]))

(defn tile-border [{:as view-ctx :keys [conn]} q r]
  (let [[x y] (tiles/offset->pixel q r)]
    [:g {:id (str "border-" q "," r)}
     (cond
       ;; Selected
       @(subs/selected? conn q r)
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/borders/selected.png")}]

       ;; Enemy unit targeted
       (and @(subs/targeted? conn q r)
            @(subs/enemy-at? conn q r))
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/borders/targeted-enemy.png")}]

       ;; Friend unit targeted (for repair)
       (and @(subs/targeted? conn q r)
            @(subs/friend-at? conn q r))
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/borders/targeted-friend.png")}]

       ;; Terrain targeted
       @(subs/targeted? conn q r)
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/borders/selected.png")}])]))

(defn unit-image [unit]
  (let [color-name (-> unit game/unit-color name)]
    ;; TODO: return placeholder if terrain image is not found
    (some-> unit
            (get-in [:unit/type :unit-type/image])
            (string/replace "COLOR" color-name))))

(defn board-unit [{:as view-ctx :keys [conn dispatch]} q r]
  (when-let [unit @(subs/unit-at conn q r)]
    (let [[x y] (tiles/offset->pixel q r)
          image (unit-image unit)]
      [:g {:id (str "unit-" (e unit))}
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/" image)
                :on-click #(dispatch [::events.ui/select-hex q r])}]
       (when (:unit/capturing unit)
         [:image {:x x :y y
                  :width tiles/width :height tiles/height
                  :xlink-href (site/prefix "/images/game/capturing.gif")}])
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/health/" (:unit/count unit) ".png")}]])))

(defn tile-mask [{:as view-ctx :keys [conn]} q r]
  (let [[x y] (tiles/offset->pixel q r)
        show (or
              ;; No unit selected and tile contains current unit with no actions
              (and (not @(subs/unit-selected? conn))
                   @(subs/current-unit-at? conn q r)
                   (not @(subs/unit-can-act? conn q r)))

              ;; Unit selected and tile is a valid attack or move target
              ;; However, selected cannot repair others
              (and @(subs/unit-selected? conn)
                   (not @(subs/selected? conn q r))
                   (not @(subs/selected-can-repair-other? conn))
                   (not @(subs/enemy-in-range-of-selected? conn q r))
                   (not @(subs/valid-destination-for-selected? conn q r)))

              ;; Same as previous code block, but unit CAN repair others
              (and @(subs/unit-selected? conn)
                   @(subs/selected-can-repair-other? conn)
                   (not @(subs/selected? conn q r))
                   (not @(subs/enemy-in-range-of-selected? conn q r))
                   (not (and @(subs/friend-in-range-of-selected? conn q r)
                             @(subs/can-be-repaired? conn q r)))<commit_msg>Simplified tiling when unit is selected<commit_after>(ns zetawar.views
  (:require
   [cljsjs.clipboard]
   [cljsjs.react-bootstrap]
   [clojure.string :as string]
   [datascript.core :as d]
   [posh.reagent :as posh]
   [reagent.core :as r :refer [with-let]]
   [taoensso.timbre :as log]
   [zetawar.data :as data]
   [zetawar.db :refer [e qe]]
   [zetawar.events.ui :as events.ui]
   [zetawar.game :as game]
   [zetawar.players :as players]
   [zetawar.site :as site]
   [zetawar.subs :as subs]
   [zetawar.tiles :as tiles]
   [zetawar.util :refer [breakpoint inspect only oonly]]
   [zetawar.views.common :refer [footer navbar]]))

(defn tile-border [{:as view-ctx :keys [conn]} q r]
  (let [[x y] (tiles/offset->pixel q r)]
    [:g {:id (str "border-" q "," r)}
     (cond
       ;; Selected
       @(subs/selected? conn q r)
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/borders/selected.png")}]

       ;; Enemy unit targeted
       (and @(subs/targeted? conn q r)
            @(subs/enemy-at? conn q r))
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/borders/targeted-enemy.png")}]

       ;; Friend unit targeted (for repair)
       (and @(subs/targeted? conn q r)
            @(subs/friend-at? conn q r))
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/borders/targeted-friend.png")}]

       ;; Terrain targeted
       @(subs/targeted? conn q r)
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/borders/selected.png")}])]))

(defn unit-image [unit]
  (let [color-name (-> unit game/unit-color name)]
    ;; TODO: return placeholder if terrain image is not found
    (some-> unit
            (get-in [:unit/type :unit-type/image])
            (string/replace "COLOR" color-name))))

(defn board-unit [{:as view-ctx :keys [conn dispatch]} q r]
  (when-let [unit @(subs/unit-at conn q r)]
    (let [[x y] (tiles/offset->pixel q r)
          image (unit-image unit)]
      [:g {:id (str "unit-" (e unit))}
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/" image)
                :on-click #(dispatch [::events.ui/select-hex q r])}]
       (when (:unit/capturing unit)
         [:image {:x x :y y
                  :width tiles/width :height tiles/height
                  :xlink-href (site/prefix "/images/game/capturing.gif")}])
       [:image {:x x :y y
                :width tiles/width :height tiles/height
                :xlink-href (site/prefix "/images/game/health/" (:unit/count unit) ".png")}]])))

(defn tile-mask [{:as view-ctx :keys [conn]} q r]
  (let [[x y] (tiles/offset->pixel q r)
        show (or
              ;; No unit selected and tile contains current unit with no actions
              (and (not @(subs/unit-selected? conn))
                   @(subs/current-unit-at? conn q r)
                   (not @(subs/unit-can-act? conn q r)))

              ;; Unit selected and tile is a valid attack, repair, or move target
              (and @(subs/unit-selected? conn)
                   (not @(subs/selected? conn q r))
                   (not @(subs/enemy-in-range-of-selected? conn q r))
                   (not (and @(subs/friend-in-range-of-selected? conn q r)
                             @(subs/can-be-repaired? conn q r)
                             @(subs/selected-can-repair-other? conn)))
{'fraction_non_alphanumeric': 0.14905709119090674, 'fraction_numerical': 0.0, 'mean_word_length': 1.9307342922028765, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6315821', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2520, 'n_words': 726}