From 2590330a541a41a77c5d97badafa067e6525128b Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Fri, 3 Aug 2018 10:55:04 +0200 Subject: Makes it a bit less ugly. --- src/map-editor/src/Comm/SetMap.elm | 3 + src/map-editor/src/Struct/Tile.elm | 49 +++++++--- src/map-editor/src/Struct/TilePattern.elm | 101 ++++++++++++++------- .../src/Update/PrettifySelectedTiles.elm | 50 ++++++---- src/map-editor/src/Update/SetToolboxTemplate.elm | 1 + 5 files changed, 145 insertions(+), 59 deletions(-) (limited to 'src') diff --git a/src/map-editor/src/Comm/SetMap.elm b/src/map-editor/src/Comm/SetMap.elm index e74471d..452ab92 100644 --- a/src/map-editor/src/Comm/SetMap.elm +++ b/src/map-editor/src/Comm/SetMap.elm @@ -34,6 +34,7 @@ deserialize_tile_instance map_width index t = type_id 0 Constants.Movement.cost_when_out_of_bounds + -1 ) [type_id, border_id, variant_ix] -> @@ -44,6 +45,7 @@ deserialize_tile_instance map_width index t = border_id variant_ix Constants.Movement.cost_when_out_of_bounds + -1 ) _ -> @@ -54,6 +56,7 @@ deserialize_tile_instance map_width index t = 0 0 Constants.Movement.cost_when_out_of_bounds + -1 ) internal_decoder : MapData -> Struct.ServerReply.Type diff --git a/src/map-editor/src/Struct/Tile.elm b/src/map-editor/src/Struct/Tile.elm index 16d42df..254dd78 100644 --- a/src/map-editor/src/Struct/Tile.elm +++ b/src/map-editor/src/Struct/Tile.elm @@ -14,6 +14,8 @@ module Struct.Tile exposing get_location, get_icon_id, get_type_id, + get_family, + get_instance_family, get_variant_ix, get_local_variant_ix, solve_tile_instance, @@ -41,20 +43,25 @@ type alias PartiallyDecoded = { id : Int, nam : String, - ct : Int + ct : Int, + fa : Int, + de : Int } type alias Type = { id : Int, name : String, - crossing_cost : Int + crossing_cost : Int, + family : Int, + depth : Int } type alias Instance = { location : Struct.Location.Type, crossing_cost : Int, + family : Int, type_id : Int, border_id : Int, variant_ix : Int @@ -86,31 +93,36 @@ finish_decoding add_tile = { id = add_tile.id, name = add_tile.nam, - crossing_cost = add_tile.ct + crossing_cost = add_tile.ct, + family = add_tile.fa, + depth = add_tile.de } -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- -------------------------------------------------------------------------------- -new : Int -> String -> Int -> Type -new id name crossing_cost = +new : Int -> String -> Int -> Int -> Int -> Type +new id name crossing_cost family depth = { id = id, name = name, - crossing_cost = crossing_cost + crossing_cost = crossing_cost, + family = family, + depth = depth } clone_instance : Struct.Location.Type -> Instance -> Instance clone_instance loc inst = {inst | location = loc} -new_instance : Int -> Int -> Int -> Int -> Int -> Int -> Instance -new_instance x y type_id border_id variant_ix crossing_cost = +new_instance : Int -> Int -> Int -> Int -> Int -> Int -> Int -> Instance +new_instance x y type_id border_id variant_ix crossing_cost family = { location = {x = x, y = y}, type_id = type_id, border_id = border_id, variant_ix = variant_ix, - crossing_cost = crossing_cost + crossing_cost = crossing_cost, + family = family } error_tile_instance : Int -> Int -> Instance @@ -120,6 +132,7 @@ error_tile_instance x y = type_id = 0, border_id = 0, variant_ix = 0, + family = 0, crossing_cost = Constants.Movement.cost_when_out_of_bounds } @@ -151,6 +164,12 @@ get_icon_id tile_inst = get_type_id : Instance -> Int get_type_id tile_inst = tile_inst.type_id +get_family : Type -> Int +get_family tile = tile.family + +get_instance_family : Instance -> Int +get_instance_family tile_inst = tile_inst.family + get_variant_ix : Instance -> Int get_variant_ix tile_inst = tile_inst.variant_ix @@ -169,10 +188,16 @@ solve_tile_instance : (Dict.Dict Int Type) -> Instance -> Instance solve_tile_instance tiles tile_instance = case (Dict.get tile_instance.type_id tiles) of (Just tile) -> - {tile_instance | crossing_cost = tile.crossing_cost} + {tile_instance | + crossing_cost = tile.crossing_cost, + family = tile.family + } Nothing -> - {tile_instance | crossing_cost = -1} + {tile_instance | + crossing_cost = -1, + family = -1 + } decoder : (Json.Decode.Decoder Type) decoder = @@ -183,5 +208,7 @@ decoder = |> (Json.Decode.Pipeline.required "id" Json.Decode.int) |> (Json.Decode.Pipeline.required "nam" Json.Decode.string) |> (Json.Decode.Pipeline.required "ct" Json.Decode.int) + |> (Json.Decode.Pipeline.required "fa" Json.Decode.int) + |> (Json.Decode.Pipeline.required "de" Json.Decode.int) ) ) diff --git a/src/map-editor/src/Struct/TilePattern.elm b/src/map-editor/src/Struct/TilePattern.elm index e86a80b..b54dbb1 100644 --- a/src/map-editor/src/Struct/TilePattern.elm +++ b/src/map-editor/src/Struct/TilePattern.elm @@ -18,6 +18,7 @@ import Constants.UI import Constants.Movement import Struct.Location +import Struct.Tile import Util.List @@ -40,20 +41,21 @@ type alias Type = -- LOCAL ----------------------------------------------------------------------- -------------------------------------------------------------------------------- matches_internals : ( - Int -> - (List Int) -> + Struct.Tile.Instance -> + (List Struct.Tile.Instance) -> (List PatternElement) -> (Maybe Int) -> + (Maybe Int) -> (Bool, Int) ) -matches_internals source neighbors pattern maybe_border = +matches_internals source neighbors pattern maybe_border_fa maybe_border_mc = case ((Util.List.pop neighbors), (Util.List.pop pattern)) of (Nothing, Nothing) -> ( True, ( - case maybe_border of - Nothing -> source + case maybe_border_mc of + Nothing -> (Struct.Tile.get_type_id source) (Just e) -> e ) ) @@ -61,22 +63,42 @@ matches_internals source neighbors pattern maybe_border = ((Just (n, r_n)), (Just (p, r_p))) -> if (matches_pattern source n p) then - if - ( - (maybe_border == (Just source)) - || (maybe_border == Nothing) - || (maybe_border == (Just n)) - || (maybe_border == (Just -1)) - ) - then (matches_internals source r_n r_p (Just n)) - else - if ((n == -1) || (n == source)) - then (matches_internals source r_n r_p maybe_border) - else (matches_internals source r_n r_p maybe_border) --(False, source) + let + source_mc = (Struct.Tile.get_type_id source) + source_fa = (Struct.Tile.get_instance_family source) + n_mc = (Struct.Tile.get_type_id n) + n_fa = (Struct.Tile.get_instance_family n) + in + if + ( + (maybe_border_fa == (Just source_fa)) + || (maybe_border_fa == Nothing) + || (maybe_border_fa == (Just n_fa)) + || (maybe_border_fa == (Just -1)) + ) + then + (matches_internals + source + r_n + r_p + (Just n_fa) + (Just n_mc) + ) + else + if ((n_fa == -1) || (n_fa == source_fa)) + then + (matches_internals + source + r_n + r_p + maybe_border_fa + maybe_border_mc + ) + else (False, source_mc) else - (False, source) + (False, (Struct.Tile.get_type_id source)) - (_, _) -> (False, source) + (_, _) -> (False, (Struct.Tile.get_type_id source)) finish_decoding_pattern : String -> PatternElement finish_decoding_pattern str = @@ -117,23 +139,40 @@ target_decoder = -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- -------------------------------------------------------------------------------- -matches_pattern : Int -> Int -> PatternElement -> Bool +matches_pattern : ( + Struct.Tile.Instance -> + Struct.Tile.Instance -> + PatternElement -> + Bool + ) matches_pattern source n p = + let + source_fa = (Struct.Tile.get_instance_family source) + n_fa = (Struct.Tile.get_instance_family n) + in case p of Any -> True - Major -> (source < n) - Minor -> ((source == n) || (n == -1)) - -matches : (List Int) -> Int -> Type -> (Bool, Int, Int, Int) + Major -> (source_fa < n_fa) + Minor -> ((source_fa == n_fa) || (n_fa == -1)) + +matches : ( + (List Struct.Tile.Instance) -> + Struct.Tile.Instance -> + Type -> + (Bool, Int, Int, Int) + ) matches neighbors source tile_pattern = - case (matches_internals source neighbors tile_pattern.p Nothing) of + case (matches_internals source neighbors tile_pattern.p Nothing Nothing) of (False, _) -> (False, 0, 0, 0) - (True, border) -> - case tile_pattern.t of - (Minor, Major) -> (True, source, border, tile_pattern.tv) - (Minor, Minor) -> (True, source, source, tile_pattern.tv) - (Major, Minor) -> (True, border, source, tile_pattern.tv) - (_, _) -> (True, border, border, tile_pattern.tv) + (True, border_mc) -> + let + source_mc = (Struct.Tile.get_type_id source) + in + case tile_pattern.t of + (Minor, Major) -> (True, source_mc, border_mc, tile_pattern.tv) + (Minor, Minor) -> (True, source_mc, source_mc, tile_pattern.tv) + (Major, Minor) -> (True, border_mc, source_mc, tile_pattern.tv) + (_, _) -> (True, border_mc, border_mc, tile_pattern.tv) decoder : (Json.Decode.Decoder Type) decoder = diff --git a/src/map-editor/src/Update/PrettifySelectedTiles.elm b/src/map-editor/src/Update/PrettifySelectedTiles.elm index 5e0e5d5..5f63e48 100644 --- a/src/map-editor/src/Update/PrettifySelectedTiles.elm +++ b/src/map-editor/src/Update/PrettifySelectedTiles.elm @@ -18,6 +18,7 @@ import Util.List -- LOCAL ----------------------------------------------------------------------- -------------------------------------------------------------------------------- set_tile_to : ( + (Dict.Dict Int Struct.Tile.Type) -> Struct.Location.Type -> Int -> Int -> @@ -25,7 +26,7 @@ set_tile_to : ( Struct.Map.Type -> Struct.Map.Type ) -set_tile_to loc main_class border_class variant_ix map = +set_tile_to tiles loc main_class border_class variant_ix map = let true_variant_ix = if (variant_ix >= 0) @@ -37,20 +38,35 @@ set_tile_to loc main_class border_class variant_ix map = in (Struct.Map.set_tile_to loc - (Struct.Tile.new_instance - loc.x - loc.y - main_class - border_class - true_variant_ix - -1 + (case (Dict.get main_class tiles) of + Nothing -> + (Struct.Tile.new_instance + loc.x + loc.y + main_class + border_class + true_variant_ix + -1 + -1 + ) + + (Just t) -> + (Struct.Tile.new_instance + loc.x + loc.y + main_class + border_class + true_variant_ix + (Struct.Tile.get_cost t) + (Struct.Tile.get_family t) + ) ) map ) find_matching_pattern : ( - Int -> - (List Int) -> + Struct.Tile.Instance -> + (List Struct.Tile.Instance) -> (List Struct.TilePattern.Type) -> (Maybe (Int, Int, Int)) ) @@ -75,26 +91,26 @@ apply_to_location model loc map = Nothing -> map (Just base) -> let - base_id = (Struct.Tile.get_type_id base) - full_neighborhood_class_ids = + oob_tile = (Struct.Tile.new_instance -1 -1 -1 -1 -1 -1 -1) + full_neighborhood = (List.map (\e -> case (Struct.Map.try_getting_tile_at e map) of - Nothing -> -1 - (Just t) -> (Struct.Tile.get_type_id t) + Nothing -> oob_tile + (Just t) -> t ) (Struct.Location.get_full_neighborhood loc) ) in case (find_matching_pattern - base_id - full_neighborhood_class_ids + base + full_neighborhood model.tile_patterns ) of (Just (main, border, variant)) -> - (set_tile_to loc main border variant map) + (set_tile_to model.tiles loc main border variant map) Nothing -> map diff --git a/src/map-editor/src/Update/SetToolboxTemplate.elm b/src/map-editor/src/Update/SetToolboxTemplate.elm index eefc622..e140e26 100644 --- a/src/map-editor/src/Update/SetToolboxTemplate.elm +++ b/src/map-editor/src/Update/SetToolboxTemplate.elm @@ -33,6 +33,7 @@ apply_to model main_class border_class variant_ix = border_class variant_ix -1 + -1 ) model.toolbox ) -- cgit v1.2.3-70-g09d2