summaryrefslogtreecommitdiff |
diff options
author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-09-22 16:04:17 +0200 |
---|---|---|
committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2017-09-22 16:04:17 +0200 |
commit | 0dadf66e7fd4bddf8e3f7b139f94045bd0b1b462 (patch) | |
tree | f399909fa56c7faab10181b384ce210adc22116f | |
parent | babe47cb2541113e245f7db4b5e981f23f985fa3 (diff) |
Clears up Battlemap/Navigator.elm
-rw-r--r-- | client/elm/battlemap/src/Battlemap/Navigator.elm | 106 | ||||
-rw-r--r-- | client/elm/battlemap/src/Battlemap/Navigator/Move.elm | 153 | ||||
-rw-r--r-- | client/elm/battlemap/src/Update/DirectionRequest.elm | 4 | ||||
-rw-r--r-- | client/elm/battlemap/src/Util/List.elm | 12 |
4 files changed, 168 insertions, 107 deletions
diff --git a/client/elm/battlemap/src/Battlemap/Navigator.elm b/client/elm/battlemap/src/Battlemap/Navigator.elm index 41593c5..79f1f73 100644 --- a/client/elm/battlemap/src/Battlemap/Navigator.elm +++ b/client/elm/battlemap/src/Battlemap/Navigator.elm @@ -2,19 +2,16 @@ module Battlemap.Navigator exposing ( Type, new_navigator, - reset_navigation, - go + reset_navigation ) import Set -- exposing (Set, member, empty, insert, remove) -import List -- exposing (head, tail) import Battlemap import Battlemap.Direction import Battlemap.Location import Battlemap.Tile -import Character type alias Type = { @@ -39,104 +36,3 @@ reset_navigation t = {t | nav_level = Battlemap.Direction.None } - -go : ( - Battlemap.Type -> - Type -> - Battlemap.Direction.Type -> - (List Character.Type) -> - (Battlemap.Type, Type) - ) -go battlemap nav dir char_list = - let - next_location = (Battlemap.Location.neighbor nav.current_location dir) - is_occupied = (List.any (\c -> (c.location == next_location)) char_list) - in - if - ( - (not is_occupied) - && (nav.remaining_points > 0) - && (Battlemap.has_location battlemap next_location) - && (nav.current_location /= next_location) - && - (not - (Set.member - (Battlemap.Location.get_ref next_location) - nav.visited_locations - ) - ) - ) - then - ( - (case - (Battlemap.apply_to_tile - battlemap - nav.current_location - (Battlemap.Tile.set_direction dir) - ) - of - Nothing -> battlemap - (Just bmap0) -> - (case - (Battlemap.apply_to_tile - bmap0 - next_location - (Battlemap.Tile.set_direction dir) - ) - of - Nothing -> battlemap - (Just bmap1) -> bmap1 - ) - ), - {nav | - current_location = next_location, - visited_locations = - (Set.insert - (Battlemap.Location.get_ref nav.current_location) - nav.visited_locations - ), - previous_directions = (dir :: nav.previous_directions), - remaining_points = (nav.remaining_points - 1) - } - ) - else if (not is_occupied) - then - case - ( - (List.head nav.previous_directions), - (List.tail nav.previous_directions) - ) - of - (Nothing, _) -> (battlemap, nav) - (_ , Nothing) -> (battlemap, nav) - ((Just prev_dir), (Just prev_dir_list)) -> - if (dir == (Battlemap.Direction.opposite_of prev_dir)) - then - ( - (case - (Battlemap.apply_to_tile - battlemap - nav.current_location - (Battlemap.Tile.set_direction - Battlemap.Direction.None - ) - ) - of - Nothing -> battlemap - (Just bmap) -> bmap - ), - {nav | - current_location = next_location, - visited_locations = - (Set.remove - (Battlemap.Location.get_ref next_location) - nav.visited_locations - ), - previous_directions = prev_dir_list, - remaining_points = (nav.remaining_points + 1) - } - ) - else - (battlemap, nav) - else - (battlemap, nav) diff --git a/client/elm/battlemap/src/Battlemap/Navigator/Move.elm b/client/elm/battlemap/src/Battlemap/Navigator/Move.elm new file mode 100644 index 0000000..924f715 --- /dev/null +++ b/client/elm/battlemap/src/Battlemap/Navigator/Move.elm @@ -0,0 +1,153 @@ +module Battlemap.Navigator.Move exposing (to) + +import Set +import List + +import Battlemap +import Battlemap.Direction +import Battlemap.Location +import Battlemap.Tile +import Battlemap.Navigator + +import Character + +import Util.List + +can_move_to_new_tile : ( + Battlemap.Navigator.Type -> + Battlemap.Type -> + Battlemap.Location.Type -> + Bool + ) +can_move_to_new_tile nav battlemap next_location = + ( + (nav.remaining_points > 0) + && (Battlemap.has_location battlemap next_location) + && (nav.current_location /= next_location) + && + (not + (Set.member + (Battlemap.Location.get_ref next_location) + nav.visited_locations + ) + ) + ) + +battlemap_move_to : ( + Battlemap.Type -> + Battlemap.Location.Type -> + Battlemap.Direction.Type -> + Battlemap.Location.Type -> + Battlemap.Type + ) +battlemap_move_to battlemap current_loc dir next_loc = + (Battlemap.apply_to_tile_unsafe + (Battlemap.apply_to_tile_unsafe + battlemap + current_loc + (Battlemap.Tile.set_direction dir) + ) + next_loc + (Battlemap.Tile.set_direction dir) + ) + +navigator_move_to : ( + Battlemap.Navigator.Type -> + Battlemap.Direction.Type -> + Battlemap.Location.Type -> + Battlemap.Navigator.Type + ) +navigator_move_to nav dir next_loc = + {nav | + current_location = next_loc, + visited_locations = + (Set.insert + (Battlemap.Location.get_ref nav.current_location) + nav.visited_locations + ), + previous_directions = (dir :: nav.previous_directions), + remaining_points = (nav.remaining_points - 1) + } + +battlemap_backtrack : ( + Battlemap.Type -> + Battlemap.Location.Type -> + Battlemap.Type + ) +battlemap_backtrack battlemap current_loc = + (Battlemap.apply_to_tile_unsafe + battlemap + current_loc + (Battlemap.Tile.set_direction + Battlemap.Direction.None + ) + ) + +navigator_backtrack : ( + Battlemap.Navigator.Type -> + Battlemap.Location.Type -> + (List Battlemap.Direction.Type) -> + Battlemap.Navigator.Type + ) +navigator_backtrack nav next_loc prev_dir_tail = + {nav | + current_location = next_loc, + visited_locations = + (Set.remove + (Battlemap.Location.get_ref next_loc) + nav.visited_locations + ), + previous_directions = prev_dir_tail, + remaining_points = (nav.remaining_points + 1) + } + +to : ( + Battlemap.Type -> + Battlemap.Navigator.Type -> + Battlemap.Direction.Type -> + (List Character.Type) -> + (Battlemap.Type, Battlemap.Navigator.Type) + ) +to battlemap nav dir char_list = + let + next_location = (Battlemap.Location.neighbor nav.current_location dir) + is_occupied = (List.any (\c -> (c.location == next_location)) char_list) + in + if (not is_occupied) + then + if (can_move_to_new_tile nav battlemap next_location) + then + ( + (battlemap_move_to + battlemap + nav.current_location + dir + next_location + ), + (navigator_move_to + nav + dir + next_location + ) + ) + else + case (Util.List.pop nav.previous_directions) of + Nothing -> (battlemap, nav) + (Just (head, tail)) -> + if (head == (Battlemap.Direction.opposite_of dir)) + then + ( + (battlemap_backtrack + battlemap + nav.current_location + ), + (navigator_backtrack + nav + next_location + tail + ) + ) + else + (battlemap, nav) + else + (battlemap, nav) diff --git a/client/elm/battlemap/src/Update/DirectionRequest.elm b/client/elm/battlemap/src/Update/DirectionRequest.elm index 6f30866..2d8b42c 100644 --- a/client/elm/battlemap/src/Update/DirectionRequest.elm +++ b/client/elm/battlemap/src/Update/DirectionRequest.elm @@ -3,7 +3,7 @@ module Update.DirectionRequest exposing (apply_to) import Dict import Battlemap.Direction -import Battlemap.Navigator +import Battlemap.Navigator.Move import Model @@ -15,7 +15,7 @@ apply_to model dir = ((Just char_id), (Just nav)) -> let (new_bmap, new_nav) = - (Battlemap.Navigator.go + (Battlemap.Navigator.Move.to model.battlemap nav dir diff --git a/client/elm/battlemap/src/Util/List.elm b/client/elm/battlemap/src/Util/List.elm new file mode 100644 index 0000000..c4db397 --- /dev/null +++ b/client/elm/battlemap/src/Util/List.elm @@ -0,0 +1,12 @@ +module Util.List exposing (pop) + +import List + +pop : List a -> (Maybe (a, List a)) +pop l = + case + ((List.head l), (List.tail l)) + of + (Nothing, _) -> Nothing + (_ , Nothing) -> Nothing + ((Just head), (Just tail)) -> (Just (head, tail)) |