summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'elm/battlemap/src/Battlemap')
-rw-r--r--elm/battlemap/src/Battlemap/Html.elm4
-rw-r--r--elm/battlemap/src/Battlemap/Marker.elm5
-rw-r--r--elm/battlemap/src/Battlemap/Navigator.elm105
-rw-r--r--elm/battlemap/src/Battlemap/Navigator/Path.elm170
-rw-r--r--elm/battlemap/src/Battlemap/Navigator/RangeIndicator.elm (renamed from elm/battlemap/src/Battlemap/RangeIndicator.elm)52
-rw-r--r--elm/battlemap/src/Battlemap/Tile.elm27
6 files changed, 300 insertions, 63 deletions
diff --git a/elm/battlemap/src/Battlemap/Html.elm b/elm/battlemap/src/Battlemap/Html.elm
index 6506c0f..d7cfc63 100644
--- a/elm/battlemap/src/Battlemap/Html.elm
+++ b/elm/battlemap/src/Battlemap/Html.elm
@@ -33,7 +33,7 @@ view_battlemap_cell t =
case t.char_level of
Nothing ->
(Html.td
- [ (Html.Events.onClick (Event.SelectTile t.location)) ]
+ [ (Html.Events.onClick (Event.TileSelected t.location)) ]
[
(Html.text
(case t.mod_level of
@@ -47,7 +47,7 @@ view_battlemap_cell t =
)
(Just char_id) ->
(Html.td
- [ (Html.Events.onClick (Event.SelectCharacter char_id)) ]
+ [ (Html.Events.onClick (Event.CharacterSelected char_id)) ]
[
(Html.text ("[" ++ char_id ++ "]")),
(Html.text (nav_level_to_text t))
diff --git a/elm/battlemap/src/Battlemap/Marker.elm b/elm/battlemap/src/Battlemap/Marker.elm
new file mode 100644
index 0000000..ebefce6
--- /dev/null
+++ b/elm/battlemap/src/Battlemap/Marker.elm
@@ -0,0 +1,5 @@
+module Battlemap.Marker exposing (Type(..))
+
+type Type =
+ CanAttack
+ | CanGoTo
diff --git a/elm/battlemap/src/Battlemap/Navigator.elm b/elm/battlemap/src/Battlemap/Navigator.elm
index b040013..9cdfc1f 100644
--- a/elm/battlemap/src/Battlemap/Navigator.elm
+++ b/elm/battlemap/src/Battlemap/Navigator.elm
@@ -2,43 +2,94 @@ module Battlemap.Navigator exposing
(
Type,
new,
- reset
+ get_current_location,
+ get_remaining_points,
+ get_range_markers,
+ add_step
)
-import Set
+import Dict
-import Battlemap
-import Battlemap.Direction
import Battlemap.Location
-import Battlemap.Tile
+import Battlemap.Navigator.Path
+import Battlemap.Navigator.RangeIndicator
+--------------------------------------------------------------------------------
+-- TYPES -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
type alias Type =
{
- current_location : Battlemap.Location.Type,
- visited_locations : (Set.Set Battlemap.Location.Ref),
- previous_directions : (List Battlemap.Direction.Type),
- remaining_points : Int,
- starting_location : Battlemap.Location.Type,
- starting_points : Int
+ starting_location: Battlemap.Location.Type,
+ movement_dist: Int,
+ attack_dist: Int,
+ path: Battlemap.Navigator.Path.Type,
+ range_indicators:
+ (Dict.Dict
+ Battlemap.Location.Ref
+ Battlemap.Navigator.RangeIndicator.Type
+ )
}
-new : Battlemap.Location.Type -> Int -> Type
-new start points =
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+new : (
+ Battlemap.Location.Type ->
+ Int ->
+ Int ->
+ (Battlemap.Location.Type -> Bool) -> Type
+ )
+new start_loc mov_dist atk_dist can_cross_fun =
{
- current_location = start,
- visited_locations = Set.empty,
- previous_directions = [],
- remaining_points = points,
- starting_location = start,
- starting_points = points
+ starting_location = start_loc,
+ movement_dist = mov_dist,
+ attack_dist = atk_dist,
+ path = (Battlemap.Navigator.Path.new start_loc mov_dist),
+ range_indicators =
+ (Battlemap.Navigator.RangeIndicator.generate
+ start_loc
+ mov_dist
+ atk_dist
+ (can_cross_fun)
+ )
}
-reset : Type -> Type
-reset nav =
- {nav |
- current_location = nav.starting_location,
- visited_locations = Set.empty,
- previous_directions = [],
- remaining_points = nav.starting_points
- }
+get_current_location : Type -> Battlemap.Location.Type
+get_current_location navigator =
+ (Battlemap.Navigator.Path.get_current_location navigator.path)
+
+get_remaining_points : Type -> Int
+get_remaining_points navigator =
+ (Battlemap.Navigator.Path.get_remaining_points navigator.path)
+
+get_range_markers : (
+ Type ->
+ (List
+ (Battlemap.Location.Ref, Battlemap.Navigator.RangeIndicator.Type)
+ )
+ )
+get_range_markers navigator = (Dict.toList navigator.range_indicators)
+
+add_step : (
+ Type ->
+ Battlemap.Direction.Type ->
+ (Battlemap.Location.Type -> Bool) ->
+ (Maybe Type)
+ )
+add_step navigator dir can_cross =
+ case
+ (Battlemap.Navigator.Path.follow_direction
+ can_cross
+ (Just navigator.path)
+ dir
+ )
+ of
+ (Just path) -> (Just {navigator | path = path}
+ Nothing -> Nothing
diff --git a/elm/battlemap/src/Battlemap/Navigator/Path.elm b/elm/battlemap/src/Battlemap/Navigator/Path.elm
new file mode 100644
index 0000000..5ce2d4c
--- /dev/null
+++ b/elm/battlemap/src/Battlemap/Navigator/Path.elm
@@ -0,0 +1,170 @@
+module Battlemap.Navigator.Path exposing
+ (
+ Type,
+ new,
+ get_current_location,
+ get_remaining_points,
+ follow_directions
+ )
+
+import Set
+
+import Battlemap.Direction
+import Battlemap.Location
+import Battlemap.Tile
+
+--------------------------------------------------------------------------------
+-- TYPES -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+type alias Type =
+ {
+ current_location : Battlemap.Location.Type,
+ visited_locations : (Set.Set Battlemap.Location.Ref),
+ previous_directions : (List Battlemap.Direction.Type),
+ remaining_points : Int
+ }
+
+--------------------------------------------------------------------------------
+-- LOCAL -----------------------------------------------------------------------
+--------------------------------------------------------------------------------
+has_not_been_to : (
+ Type ->
+ Battlemap.Location.Type ->
+ Bool
+ )
+has_not_been_to path location =
+ (
+ (path.current_location /= location)
+ &&
+ (not
+ (Set.member
+ (Battlemap.Location.get_ref location)
+ path.visited_locations
+ )
+ )
+ )
+
+move_to : (
+ Type ->
+ Battlemap.Direction.Type ->
+ Battlemap.Location.Type ->
+ Int ->
+ Type
+ )
+move_to path dir next_loc cost =
+ {path |
+ current_location = next_loc,
+ visited_locations =
+ (Set.insert
+ (Battlemap.Location.get_ref path.current_location)
+ path.visited_locations
+ ),
+ previous_directions = (dir :: path.previous_directions),
+ remaining_points = (path.remaining_points - cost)
+ }
+
+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
+ )
+try_backtracking_to path location dir =
+ case (Util.List.pop nav.previous_directions) of
+ (Just (head, tail)) ->
+ if (head == (Battlemap.Direction.opposite_of dir))
+ then
+ (backtrack_to
+ nav
+ next_location
+ tail
+ )
+ )
+ else
+ (battlemap, nav)
+ Nothing -> (battlemap, nav)
+ move_to path next_location
+ if (can_move_to_new_tile path next_location)
+ then
+ else
+ {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 : (
+ Type ->
+ Battlemap.Direction.Type ->
+ (Battlemap.Type, Battlemap.Navigator.Type)
+ )
+to battlemap nav dir char_list =
+
+--------------------------------------------------------------------------------
+-- EXPORTED --------------------------------------------------------------------
+--------------------------------------------------------------------------------
+
+new : Battlemap.Location.Type -> Int -> Type
+new start points =
+ {
+ current_location = start,
+ visited_locations = Set.empty,
+ previous_directions = [],
+ remaining_points = points
+ }
+
+get_current_location : Type -> Battlemap.Location.Type
+get_current_location path = path.current_location
+
+get_remaining_points : Type -> Int
+get_remaining_points path = path.remaining_points
+
+follow_direction : (
+ (Battlemap.Location.Type -> Bool) ->
+ (Maybe Type) ->
+ Battlemap.Direction.Type ->
+ (Maybe Type)
+ )
+follow_direction can_cross cost_fun maybe_path dir =
+ case maybe_path of
+ (Just path) ->
+ let
+ next_location =
+ (Battlemap.Location.neighbor
+ nav.current_location
+ dir
+ )
+ in
+ if (can_cross path next_location)
+ then
+ if (has_not_been_to path next_location)
+ then
+ (Just (move_to path next_location dir))
+ else
+ (try_backtracking_to path next_location dir)
+ else
+ Nothing
+ else
+ (battlemap, nav)
+
+ Nothing -> Nothing
diff --git a/elm/battlemap/src/Battlemap/RangeIndicator.elm b/elm/battlemap/src/Battlemap/Navigator/RangeIndicator.elm
index 9276e49..c370d03 100644
--- a/elm/battlemap/src/Battlemap/RangeIndicator.elm
+++ b/elm/battlemap/src/Battlemap/Navigator/RangeIndicator.elm
@@ -1,12 +1,17 @@
-module Battlemap.RangeIndicator exposing (Type, generate)
+module Battlemap.Navigator.RangeIndicator exposing
+ (
+ Type,
+ generate,
+ get_marker
+ )
import Dict
import List
import Debug
-import Battlemap
import Battlemap.Direction
import Battlemap.Location
+import Battlemap.Marker
import Util.List
@@ -14,7 +19,8 @@ type alias Type =
{
distance: Int,
path: (List Battlemap.Direction.Type),
- node_cost: Int
+ node_cost: Int,
+ marker: Battlemap.Marker.Type
}
generate_row : (
@@ -172,14 +178,28 @@ search result remaining dist atk_dist =
{
distance = (atk_dist + 1),
path = [],
- node_cost = 99
+ node_cost = 99,
+ marker = Battlemap.Marker.CanAttack
}
)
remaining
)
in
(search
- (Dict.insert min_loc_ref min result)
+ (Dict.insert
+ min_loc_ref
+ {min |
+ marker =
+ (
+ if (min.distance > dist)
+ then
+ Battlemap.Marker.CanAttack
+ else
+ Battlemap.Marker.CanGoTo
+ )
+ }
+ result
+ )
(handle_neighbors
(Battlemap.Location.from_ref min_loc_ref)
dist
@@ -198,23 +218,23 @@ search result remaining dist atk_dist =
)
grid_to_range_indicators : (
- Battlemap.Type ->
+ (Battlemap.Location.Type -> Bool) ->
Battlemap.Location.Type ->
Int ->
(List Battlemap.Location.Type) ->
(Dict.Dict Battlemap.Location.Ref Type) ->
(Dict.Dict Battlemap.Location.Ref Type)
)
-grid_to_range_indicators battlemap location dist grid result =
+grid_to_range_indicators can_cross_fun location dist grid result =
case (Util.List.pop grid) of
Nothing -> result
(Just (head, tail)) ->
- if (Battlemap.has_location battlemap head)
+ if (can_cross_fun head)
then
-- TODO: test if the current char can cross that tile.
-- TODO: get tile cost.
(grid_to_range_indicators
- battlemap
+ (can_cross_fun)
location
dist
tail
@@ -230,26 +250,27 @@ grid_to_range_indicators battlemap location dist grid result =
(dist + 1)
),
path = [],
- node_cost = 1
+ node_cost = 1,
+ marker = Battlemap.Marker.CanGoTo
}
result
)
)
else
- (grid_to_range_indicators battlemap location dist tail result)
+ (grid_to_range_indicators (can_cross_fun) location dist tail result)
generate : (
- Battlemap.Type ->
Battlemap.Location.Type ->
Int ->
Int ->
+ (Battlemap.Location.Type -> Bool) ->
(Dict.Dict Battlemap.Location.Ref Type)
)
-generate battlemap location dist atk_dist =
+generate location dist atk_dist can_cross_fun =
(search
Dict.empty
(grid_to_range_indicators
- battlemap
+ (can_cross_fun)
location
atk_dist
(generate_grid location atk_dist (-atk_dist) [])
@@ -258,3 +279,6 @@ generate battlemap location dist atk_dist =
dist
atk_dist
)
+
+get_marker : Type -> Battlemap.Marker.Type
+get_marker indicator = indicator.marker
diff --git a/elm/battlemap/src/Battlemap/Tile.elm b/elm/battlemap/src/Battlemap/Tile.elm
index 7e0ae68..d761225 100644
--- a/elm/battlemap/src/Battlemap/Tile.elm
+++ b/elm/battlemap/src/Battlemap/Tile.elm
@@ -1,38 +1,25 @@
module Battlemap.Tile exposing
(
Type,
- TileModifier(..),
- set_direction,
- reset
+ set_character,
+ get_character
)
import Battlemap.Direction
+import Battlemap.Marker
import Battlemap.Location
import Character
-type TileModifier =
- CanBeReached
- | CanBeAttacked
-
type alias Type =
{
location : Battlemap.Location.Ref,
floor_level : Int,
- nav_level : Battlemap.Direction.Type,
char_level : (Maybe Character.Ref),
- mod_level : (Maybe TileModifier)
}
-set_direction : Battlemap.Direction.Type -> Type -> Type
-set_direction d t =
- {t |
- nav_level = d
- }
+set_character : (Maybe Character.Ref) -> Type -> Type
+set_character char_ref tile = {tile | char_level = char_ref}
-reset: Type -> Type
-reset t =
- {t |
- nav_level = Battlemap.Direction.None,
- mod_level = Nothing
- }
+get_character : Type -> (Maybe Character.Ref)
+get_character tile = tile.char_level