<commit_before>;; -*- coding: utf-8 -*-
;;
;;   Copyright 2010-2014 <NAME>
;;
;;   This file is part of pyherc.
;;
;;   pyherc is free software: you can redistribute it and/or modify
;;   it under the terms of the GNU General Public License as published by
;;   the Free Software Foundation, either version 3 of the License, or
;;   (at your option) any later version.
;;
;;   pyherc is distributed in the hope that it will be useful,
;;   but WITHOUT ANY WARRANTY; without even the implied warranty of
;;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;   GNU General Public License for more details.
;;
;;   You should have received a copy of the GNU General Public License
;;   along with pyherc.  If not, see <http://www.gnu.org/licenses/>.

(require hy.contrib.anaphoric)
(require pyherc.macros)

(import [pyherc.data [add-location-tag add-trap floor-tile wall-tile
                      ornamentation distance-between get-location-tags]])

(defn new-section [corner0 corner1 level random-generator]
  "create a new section"
  {:corners [corner0 corner1]
   :border nil
   :level level
   :connections []
   :room-connections []
   :neighbours []
   :data {}
   :random-generator random-generator})

(defn equal-sections? [section1 section2]
  "two sections are considered equal when they occupy the same area"
  (= (section-corners section1) (section-corners section2)))

(defn section-in? [section sections]
  "check if given section is in a list of sections"
  (any (ap-map (equal-sections? section it) sections)))

(defn section-data [section key &optional [value :no-data]]
  "get/set section custom data"
  (when (!= value :no-data)
    (assoc (:data section) key value))
  (get (:data section) key))

(defn section-corners [section &optional [corners :no-corners]]
  "get/set corners of this section"
  (when (!= corners :no-corners) (setv (:corners section) corners))
  (:corners section))

(defn section-level [section]
  "get level where section is placed"
  (:level section))

(defn section-height [section]
  "get height of a section"
  (abs (- (y-coordinate (first (:corners section)))
          (y-coordinate (second (:corners section))))))

(defn section-width [section]
  "get width of a section"
  (abs (- (x-coordinate (first (:corners section)))
          (x-coordinate (second (:corners section))))))

(defn left-edge [section]
  "get left edge of the section"
  (let [[point₀ (x-coordinate (first (:corners section)))]
        [point₁ (x-coordinate (second (:corners section)))]]
    (min point₀ point₁)))

(defn right-edge [section]
  "get right edge of the section"
  (let [[point₀ (x-coordinate (first (:corners section)))]
        [point₁ (x-coordinate (second (:corners section)))]]
    (max point₀ point₁)))

(defn top-edge [section]
  "get top edge of the section"
  (let [[point₀ (y-coordinate (first (:corners section)))]
        [point₁ (y-coordinate (second (:corners section)))]]
    (min point₀ point₁)))

(defn bottom-edge [section]
  "get bottom edge of the section"
  (let [[point₀ (y-coordinate (first (:corners section)))]
        [point₁ (y-coordinate (second (:corners section)))]]
    (max point₀ point₁)))

(defn section-to-map [section location]
  "map section coordinates to map coordinates"
  #t((+ (left-edge section) (x-coordinate location))
     (+ (top-edge section) (y-coordinate location))))

(defn section-floor [section location &optional [tile-id :no-tile] location-type]
  "get/set floor tile in section"
  (let [[loc (section-to-map section location)]
        [level (:level section)]]
    (when (!= tile-id :no-tile) (floor-tile level loc tile-id))
    (when location-type (add-location-tag level loc location-type))
    (floor-tile level loc tile-id)))

(defn section-wall [section location &optional [tile-id :no-tile] location-type]
  "get/set wall tile in section"
  (let [[loc (section-to-map section location)]
        [level (:level section)]]
    (when (!= tile-id :no-tile) (wall-tile level loc tile-id))
    (when location-type (add-location-tag level loc location-type))
    (wall-tile level loc tile-id)))

(defn section-ornamentation [section location &optional [tile-id :no-tile]]
  "get/set ornament in section"
  (let [[loc (section-to-map section location)]
        [level (:level section)]]
    (when (!= tile-id :no-tile) (ornamentation level loc tile-id))
    (ornamentation level loc tile-id)))

(defn section-trap [section location trap]
  "set trap in section"
  (add-trap (:level section) (section-to-map section location) trap))

(defn section-location-tag [section location &optional [location-tag :no-tag]]
  "get/set location type in section"
  (let [[loc (section-to-map section location)]
        [level (:level section)]]
    (when (!= location-tag :no-tag) (add-location-tag level loc location-tag))
    (get-location-tags level loc)))

(defn section-connections [section]
  "get connections of a section"
  (genexpr con [con (:connections section)]))

(defn add-section-connection [section connection]
  "add a new connection to a section"
  (.append (:connections section) connection))

(defn room-connections [section]
  "get room connections of a section"
  (genexpr con [con (:room-connections section)]))

(defn connected? [section]
  "is this section connected"
  (count (section-connections section)))

(defn add-room-connection [section location direction]
  "add a new room connection to a section"
  (.append (:room-connections section) (Connection nil
                                                   location 
                                                   direction 
                                                   section)))

(defn neighbour-sections [section]
  "get sections next to this one"
  (genexpr sec [sec (:neighbours section)]))

(defn mark-all-neighbours [sections]
  "process list of sections and mark all neighbours"
  (for [#t(id₀ section₀) (enumerate sections)]
    (for [#t(id₁ section₁) (enumerate sections)]
      (when (and (!= id₀ id₁)
                 (not (section-in? section₀ (neighbour-sections section₁)))
                 (>= (len (list (common-border section₀ section₁))) 1))
        (mark-neighbours section₀ section₁)))))

(defn mark-neighbours [section neighbour]
  "mark two sections as neighbours"
  (.append (:neighbours section) neighbour)
  (.append (:neighbours neighbour) section))

(defn unconnected-neighbours [section]
  "get unconnected neighbours of a section"
  (ap-filter (not (connected? it)) (neighbour-sections section)))

(defn unconnected-neighbours? [section]
  "check if this section has unconnected neighbours"
  (count (unconnected-neighbours section)))

(defn section-border [section]
  "get border locations of a section"
  (when (not (:border section))
    (let [[#t(corner₀ corner₁) (section-corners section)]
          [temp-border []]]
      (for [loc (range (+ (x-coordinate corner₀) 1) (x-coordinate corner₁))]<commit_msg>optimizing bsp<commit_after>;; -*- coding: utf-8 -*-
;;
;;   Copyright 2010-2014 <NAME>
;;
;;   This file is part of pyherc.
;;
;;   pyherc is free software: you can redistribute it and/or modify
;;   it under the terms of the GNU General Public License as published by
;;   the Free Software Foundation, either version 3 of the License, or
;;   (at your option) any later version.
;;
;;   pyherc is distributed in the hope that it will be useful,
;;   but WITHOUT ANY WARRANTY; without even the implied warranty of
;;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;   GNU General Public License for more details.
;;
;;   You should have received a copy of the GNU General Public License
;;   along with pyherc.  If not, see <http://www.gnu.org/licenses/>.

(require hy.contrib.anaphoric)
(require pyherc.macros)

(import [pyherc.data [add-location-tag add-trap floor-tile wall-tile
                      ornamentation distance-between get-location-tags]])

(defn new-section [corner0 corner1 level random-generator]
  "create a new section"
  {:corners [corner0 corner1]
   :border nil
   :level level
   :connections []
   :room-connections []
   :neighbours []
   :data {}
   :random-generator random-generator})

(defn equal-sections? [section1 section2]
  "two sections are considered equal when they occupy the same area"
  (= (section-corners section1) (section-corners section2)))

(defn section-in? [section sections]
  "check if given section is in a list of sections"
  (any (ap-map (equal-sections? section it) sections)))

(defn section-data [section key &optional [value :no-data]]
  "get/set section custom data"
  (when (!= value :no-data)
    (assoc (:data section) key value))
  (get (:data section) key))

(defn section-corners [section &optional [corners :no-corners]]
  "get/set corners of this section"
  (when (!= corners :no-corners) (setv (:corners section) corners))
  (:corners section))

(defn section-level [section]
  "get level where section is placed"
  (:level section))

(defn section-height [section]
  "get height of a section"
  (abs (- (y-coordinate (first (:corners section)))
          (y-coordinate (second (:corners section))))))

(defn section-width [section]
  "get width of a section"
  (abs (- (x-coordinate (first (:corners section)))
          (x-coordinate (second (:corners section))))))

(defn left-edge [section]
  "get left edge of the section"
  (let [[point₀ (x-coordinate (first (:corners section)))]
        [point₁ (x-coordinate (second (:corners section)))]]
    (min point₀ point₁)))

(defn right-edge [section]
  "get right edge of the section"
  (let [[point₀ (x-coordinate (first (:corners section)))]
        [point₁ (x-coordinate (second (:corners section)))]]
    (max point₀ point₁)))

(defn top-edge [section]
  "get top edge of the section"
  (let [[point₀ (y-coordinate (first (:corners section)))]
        [point₁ (y-coordinate (second (:corners section)))]]
    (min point₀ point₁)))

(defn bottom-edge [section]
  "get bottom edge of the section"
  (let [[point₀ (y-coordinate (first (:corners section)))]
        [point₁ (y-coordinate (second (:corners section)))]]
    (max point₀ point₁)))

(defn section-to-map [section location]
  "map section coordinates to map coordinates"
  #t((+ (left-edge section) (x-coordinate location))
     (+ (top-edge section) (y-coordinate location))))

(defn section-floor [section location &optional [tile-id :no-tile] location-type]
  "get/set floor tile in section"
  (let [[loc (section-to-map section location)]
        [level (:level section)]]
    (when (!= tile-id :no-tile) (floor-tile level loc tile-id))
    (when location-type (add-location-tag level loc location-type))
    (floor-tile level loc tile-id)))

(defn section-wall [section location &optional [tile-id :no-tile] location-type]
  "get/set wall tile in section"
  (let [[loc (section-to-map section location)]
        [level (:level section)]]
    (when (!= tile-id :no-tile) (wall-tile level loc tile-id))
    (when location-type (add-location-tag level loc location-type))
    (wall-tile level loc tile-id)))

(defn section-ornamentation [section location &optional [tile-id :no-tile]]
  "get/set ornament in section"
  (let [[loc (section-to-map section location)]
        [level (:level section)]]
    (when (!= tile-id :no-tile) (ornamentation level loc tile-id))
    (ornamentation level loc tile-id)))

(defn section-trap [section location trap]
  "set trap in section"
  (add-trap (:level section) (section-to-map section location) trap))

(defn section-location-tag [section location &optional [location-tag :no-tag]]
  "get/set location type in section"
  (let [[loc (section-to-map section location)]
        [level (:level section)]]
    (when (!= location-tag :no-tag) (add-location-tag level loc location-tag))
    (get-location-tags level loc)))

(defn section-connections [section]
  "get connections of a section"
  (genexpr con [con (:connections section)]))

(defn add-section-connection [section connection]
  "add a new connection to a section"
  (.append (:connections section) connection))

(defn room-connections [section]
  "get room connections of a section"
  (genexpr con [con (:room-connections section)]))

(defn connected? [section]
  "is this section connected"
  (count (section-connections section)))

(defn add-room-connection [section location direction]
  "add a new room connection to a section"
  (.append (:room-connections section) (Connection nil
                                                   location 
                                                   direction 
                                                   section)))

(defn neighbour-sections [section]
  "get sections next to this one"
  (genexpr sec [sec (:neighbours section)]))

(defn mark-all-neighbours [sections]
  "process list of sections and mark all neighbours"
  (for [#t(id₀ section₀) (enumerate sections)]
    (for [#t(id₁ section₁) (enumerate sections)]
      (when (and (> id₁ id₀)
                 (>= (len (list (common-border section₀ section₁))) 1))
        (mark-neighbours section₀ section₁)))))

(defn mark-neighbours [section neighbour]
  "mark two sections as neighbours"
  (.append (:neighbours section) neighbour)
  (.append (:neighbours neighbour) section))

(defn unconnected-neighbours [section]
  "get unconnected neighbours of a section"
  (ap-filter (not (connected? it)) (neighbour-sections section)))

(defn unconnected-neighbours? [section]
  "check if this section has unconnected neighbours"
  (count (unconnected-neighbours section)))

(defn section-border [section]
  "get border locations of a section"
  (when (not (:border section))
    (let [[#t(corner₀ corner₁) (section-corners section)]
          [temp-border []]]
      (for [loc (range (+ (x-coordinate corner₀) 1) (x-coordinate corner₁))]
{'fraction_non_alphanumeric': 0.13030214991284136, 'fraction_numerical': 0.002905287623474724, 'mean_word_length': 3.5144262295081967, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1557563', 'n_tokens_mistral': 4373, 'n_tokens_neox': 4149, 'n_words': 1459}