<commit_before>﻿namespace Scenery
open System.Collections.Generic
open System.Numerics
open Prime
open TiledSharp
open Nu
open Nu.Declarative

[<RequireQualifiedAccess>]
module Simulants =

    // here we create an entity reference for SkyBox. This is useful for simulants that you want
    // to refer to from multiple places
    let Light = Simulants.Default.Group / "Light"

// this is a custom entity for performance testing
type CustomModelDispatcher () =
    inherit StaticModelDispatcher ()

    override this.Update (entity, world) =
        entity.SetRotation (entity.GetRotation world * Quaternion.CreateFromAxisAngle (v3Up, 0.01f)) world

module Field =

    let FieldGeometries = dictPlus HashIdentity.Structural []

    type Field =
        private
            { FieldTileMap : TileMap AssetTag }

    let getFieldGeometry (field : Field) world =
        match FieldGeometries.TryGetValue field.FieldTileMap with
        | (true, geometry) -> geometry
        | (false, _) ->
            let (_, tileSets, tileMap) = World.getTileMapMetadata field.FieldTileMap world
            let untraversableLayer = tileMap.Layers.["Untraversable"] :?> TmxLayer
            let untraversableHeightLayer = tileMap.Layers.["UntraversableHeight"] :?> TmxLayer
            let traversableLayer = tileMap.Layers.["Traversable"] :?> TmxLayer
            let traversableHeightLayer = tileMap.Layers.["TraversableHeight"] :?> TmxLayer

            let texCoords = Array.zeroCreate<single> (tileMap.Width * tileMap.Height * 2 * 6)

            for i in 0 .. dec tileMap.Width do
                for j in 0 .. dec tileMap.Height do
                    let t = i * tileMap.Width + j
                    let tile = untraversableLayer.Tiles.[t]
                    let mutable tileSetOpt = None
                    for (tileSet, _) in tileSets do
                        match tileSetOpt with
                        | None ->
                            if tile.Gid = 0 then
                                tileSetOpt <- Some tileSet // just use the first tile set for the empty tile
                            elif tile.Gid >= tileSet.FirstGid && tile.Gid < tileSet.FirstGid + tileSet.TileCount.GetValueOrDefault 0 then
                                tileSetOpt <- Some tileSet
                        | Some _ -> ()
                    match tileSetOpt with
                    | Some tileSet ->
                        let tileId = tile.Gid - tileSet.FirstGid
                        let texCoordX = single (tileId / tileSet.Columns.Value) / single tileSet.Image.Width.Value
                        let texCoordY = single (tileId % tileSet.Columns.Value) / single tileSet.Image.Height.Value
                        let texCoordX2 = texCoordX + 1.0f / single tileSet.Image.Width.Value
                        let texCoordY2 = texCoordY + 1.0f / single tileSet.Image.Height.Value
                        let u = t * 6
                        texCoords.[u] <- texCoordX
                        texCoords.[u+1] <- texCoordX2
                        texCoords.[u+2] <- texCoordY
                        texCoords.[u+3] <- texCoordX
                        texCoords.[u+4] <- texCoordY2
                        texCoords.[u+5] <- texCoordX2
                    | None -> ()

            let positions = Array.zeroCreate<single> (tileMap.Width * tileMap.Height * 3 * 6)

            for i in 0 .. dec tileMap.Width do
                for j in 0 .. dec tileMap.Height do
                    let t = i * tileMap.Width + j
                    let tile = untraversableHeightLayer.Tiles.[t]
                    let mutable tileSetOpt = None
                    for (tileSet, _) in tileSets do
                        match tileSetOpt with
                        | None ->
                            if tile.Gid = 0 then
                                tileSetOpt <- Some tileSet // just use the first tile set for the empty tile
                            elif tile.Gid >= tileSet.FirstGid && tile.Gid < tileSet.FirstGid + tileSet.TileCount.GetValueOrDefault 0 then
                                tileSetOpt <- Some tileSet
                        | Some _ -> ()
                    match tileSetOpt with
                    | Some tileSet ->
                        let height = tile.Gid - tileSet.FirstGid

// this is our Elm-style command type
type Command =
    | Update

// this is our Elm-style game dispatcher
type SceneryDispatcher () =
    inherit GameDispatcher<unit, unit, Command> (())

    // here we channel from events to signals
    override this.Channel (_, game) =
        [game.UpdateEvent => cmd Update]
<commit_msg>Intermediate commit.<commit_after>﻿namespace Scenery
open System.Collections.Generic
open System.Numerics
open Prime
open TiledSharp
open Nu
open Nu.Declarative

[<RequireQualifiedAccess>]
module Simulants =

    // here we create an entity reference for SkyBox. This is useful for simulants that you want
    // to refer to from multiple places
    let Light = Simulants.Default.Group / "Light"

// this is a custom entity for performance testing
type CustomModelDispatcher () =
    inherit StaticModelDispatcher ()

    override this.Update (entity, world) =
        entity.SetRotation (entity.GetRotation world * Quaternion.CreateFromAxisAngle (v3Up, 0.01f)) world

module Field =

    let FieldGeometries = dictPlus HashIdentity.Structural []

    type Field =
        private
            { FieldTileMap : TileMap AssetTag }

    let getFieldGeometry (field : Field) world =
        match FieldGeometries.TryGetValue field.FieldTileMap with
        | (true, geometry) -> geometry
        | (false, _) ->

            let (_, tileSets, tileMap) = World.getTileMapMetadata field.FieldTileMap world
            let untraversableLayer = tileMap.Layers.["Untraversable"] :?> TmxLayer
            let untraversableHeightLayer = tileMap.Layers.["UntraversableHeight"] :?> TmxLayer
            let traversableLayer = tileMap.Layers.["Traversable"] :?> TmxLayer
            let traversableHeightLayer = tileMap.Layers.["TraversableHeight"] :?> TmxLayer

            let texCoords = Array.zeroCreate<Vector2> (tileMap.Width * tileMap.Height * 6)

            for i in 0 .. dec tileMap.Width do
                for j in 0 .. dec tileMap.Height do
                    let t = i * tileMap.Width + j
                    let tile = untraversableLayer.Tiles.[t]
                    let mutable tileSetOpt = None
                    for (tileSet, _) in tileSets do
                        match tileSetOpt with
                        | None ->
                            if tile.Gid = 0 then
                                tileSetOpt <- Some tileSet // just use the first tile set for the empty tile
                            elif tile.Gid >= tileSet.FirstGid && tile.Gid < tileSet.FirstGid + tileSet.TileCount.GetValueOrDefault 0 then
                                tileSetOpt <- Some tileSet
                        | Some _ -> ()
                    match tileSetOpt with
                    | Some tileSet ->
                        let tileId = tile.Gid - tileSet.FirstGid
                        let texCoordX = single (tileId / tileSet.Columns.Value) / single tileSet.Image.Width.Value
                        let texCoordY = single (tileId % tileSet.Columns.Value) / single tileSet.Image.Height.Value
                        let texCoordX2 = texCoordX + 1.0f / single tileSet.Image.Width.Value
                        let texCoordY2 = texCoordY + 1.0f / single tileSet.Image.Height.Value
                        let u = t * 6
                        texCoords.[u] <- v2 texCoordX texCoordY
                        texCoords.[u+1] <- v2 texCoordX2 texCoordY
                        texCoords.[u+2] <- v2 texCoordX2 texCoordY2
                        texCoords.[u+3] <- v2 texCoordX texCoordY
                        texCoords.[u+4] <- v2 texCoordX2 texCoordY2
                        texCoords.[u+5] <- v2 texCoordX texCoordY
                    | None -> ()

            let positions = Array.zeroCreate<Vector3> (tileMap.Width * tileMap.Height * 6)

            for i in 0 .. dec tileMap.Width do
                for j in 0 .. dec tileMap.Height do
                    let t = i * tileMap.Width + j
                    let tile = untraversableHeightLayer.Tiles.[t]
                    let mutable tileSetOpt = None
                    for (tileSet, _) in tileSets do
                        match tileSetOpt with
                        | None ->
                            if tile.Gid = 0 then
                                tileSetOpt <- Some tileSet // just use the first tile set for the empty tile
                            elif tile.Gid >= tileSet.FirstGid && tile.Gid < tileSet.FirstGid + tileSet.TileCount.GetValueOrDefault 0 then
                                tileSetOpt <- Some tileSet
                        | Some _ -> ()
                    match tileSetOpt with
                    | Some tileSet ->
                        let height = tile.Gid - tileSet.FirstGid
                        let u = t * 3 * 6
                        let position = v3 (single i) (single j) (single height)
                        positions.[u] <- position
                        positions.[u+1] <- position
                        positions.[u+2] <- position
                        positions.[u+3] <- position
                        positions.[u+4] <- position
                        positions.[u+5] <- position
                    | None -> ()

            for i in 1 .. dec tileMap.Width do
                for j in 1 .. dec tileMap.Height do
                    let u = i * tileMap.Width + j
                    let positionM1 = &positions.[dec u]
                    let position = &positions.[u]
                    position.X <- (positionM1.X + position.X) / 2.0f
                    let positionM1 = &positions.[dec u+3]
                    let position = &positions.[u+3]
                    position.X <- (positionM1.X + position.X) / 2.0f
                    let positionM1 = &positions.[dec u+5]
                    let position = &positions.[u+5]
                    position.X <- (positionM1.X + position.X) / 2.0f

            let normals = Array.zeroCreate<Vector3> (tileMap.Width * tileMap.Height * 6)

            for i in 1 .. dec tileMap.Width do
                for j in 1 .. dec tileMap.Height do
                    let u = i * tileMap.Width + j
                    let normal = Vector3.Normalize (Vector3.Cross (positions.[u+1] - positions.[u], positions.[u+5] - positions.[u]))
                    normals.[u] <- normal
                    normals.[u+1] <- normal
                    normals.[u+2] <- normal
                    normals.[u+3] <- normal
                    normals.[u+4] <- normal
                    normals.[u+5] <- normal

// this is our Elm-style command type
type Command =
    | Update

// this is our Elm-style game dispatcher
type SceneryDispatcher () =
    inherit GameDispatcher<unit, unit, Command> (())

    // here we channel from events to signals
    override this.Channel (_, game) =
        [game.UpdateEvent => cmd Update]

{'fraction_non_alphanumeric': 0.08244824482448244, 'fraction_numerical': 0.009810981098109812, 'mean_word_length': 1.4286338797814209, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3682332', 'n_tokens_mistral': 3045, 'n_tokens_neox': 2807, 'n_words': 835}