summaryrefslogtreecommitdiff
path: root/src/type
diff options
context:
space:
mode:
Diffstat (limited to 'src/type')
-rw-r--r--src/type/battlemap.erl38
-rw-r--r--src/type/battlemap/cross.erl60
-rw-r--r--src/type/battlemap_instance.erl111
-rw-r--r--src/type/battlemap_instance_shim.erl40
-rw-r--r--src/type/battlemap_shim.erl56
-rw-r--r--src/type/character.erl34
-rw-r--r--src/type/character_instance.erl72
-rw-r--r--src/type/character_shim.erl47
8 files changed, 458 insertions, 0 deletions
diff --git a/src/type/battlemap.erl b/src/type/battlemap.erl
new file mode 100644
index 0000000..cce1a76
--- /dev/null
+++ b/src/type/battlemap.erl
@@ -0,0 +1,38 @@
+-module(battlemap).
+-record
+(
+ battlemap,
+ {
+ id,
+ width,
+ height,
+ content,
+ instances
+ }
+).
+-export
+(
+ [
+ get_id/1,
+ get_width/1,
+ get_height/1,
+ list_tiles/1,
+ get_instances/1
+ ]
+).
+-export
+(
+ [dist/2]
+).
+-export([cross/5]).
+
+get_id (Battlemap) -> Battlemap#battlemap.id.
+get_width (Battlemap) -> Battlemap#battlemap.width.
+get_height (Battlemap) -> Battlemap#battlemap.height.
+list_tiles (Battlemap) -> array:sparse_to_list(Battlemap#battlemap.content).
+get_instances (Battlemap) -> Battlemap#battlemap.instances.
+
+-include("battlemap/cross.erl").
+
+dist ({OX, OY}, {DX, DY}) ->
+ (abs(OX - DX) + abs(OY + DY)).
diff --git a/src/type/battlemap/cross.erl b/src/type/battlemap/cross.erl
new file mode 100644
index 0000000..aa1645e
--- /dev/null
+++ b/src/type/battlemap/cross.erl
@@ -0,0 +1,60 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% LOCAL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+next_loc (X, Y, <<"L">>) -> {(X - 1), Y};
+next_loc (X, Y, <<"R">>) -> {(X + 1), Y};
+next_loc (X, Y, <<"U">>) -> {X, (Y - 1)};
+next_loc (X, Y, <<"D">>) -> {X, (Y + 1)}.
+
+loc_to_index(X, Y, Map) ->
+ if
+ (X < 0) -> error;
+ (Y < 0) -> error;
+ (X >= Map#battlemap.width) -> error;
+ true -> ((Y * Map#battlemap.width) + X)
+ end.
+
+calc_new_loc (X, Y, [], Points, _Map, _CharInstsLocs) ->
+ io:format("~nPoints remaining: ~p ~n", [Points]),
+ true = (Points >= 0),
+ {X, Y};
+calc_new_loc (X, Y, [Step|Path], Points, Map, CharInstsLocs) ->
+ io:format("~nStep - Points remaining: ~p ~n", [Points]),
+ {NX, NY} = next_loc(X, Y, Step),
+ TileCost =
+ tile:get_cost
+ (
+ array:get
+ (
+ loc_to_index(NX, NY, Map),
+ Map#battlemap.content
+ )
+ ),
+ io:format("~nStep cost: ~p ~n", [TileCost]),
+ NPoints =
+ (
+ Points
+ -
+ TileCost
+ ),
+ false = lists:member({NX, NY}, CharInstsLocs),
+ calc_new_loc(NX, NY, Path, NPoints, Map, CharInstsLocs).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% EXPORTED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+cross (Battlemap, {X, Y}, Points, Path, CharInsts) ->
+ calc_new_loc
+ (
+ X,
+ Y,
+ Path,
+ Points,
+ Battlemap,
+ lists:map
+ (
+ fun character_instance:get_location/1,
+ CharInsts
+ )
+ ).
diff --git a/src/type/battlemap_instance.erl b/src/type/battlemap_instance.erl
new file mode 100644
index 0000000..26ca5b1
--- /dev/null
+++ b/src/type/battlemap_instance.erl
@@ -0,0 +1,111 @@
+-module(battlemap_instance).
+-record
+(
+ battlemap_instance,
+ {
+ id,
+ chars,
+ curr_player,
+ players,
+ rem_chars,
+ last_turn
+ }
+).
+-export
+(
+ [
+ list_characters/1
+ ]
+).
+-export
+(
+ [
+ get_char_instances/1,
+ get_char_instance/2,
+ set_char_instance/3,
+ can_play_char_instance/3,
+ post_play_char_instance/2
+ ]
+).
+
+list_characters (BattlemapInstance) ->
+ dict:to_list(BattlemapInstance#battlemap_instance.chars).
+
+get_char_instances (BattlemapInstance) ->
+ lists:map
+ (
+ fun ({_K, V}) -> V end,
+ dict:to_list(BattlemapInstance#battlemap_instance.chars)
+ ).
+
+can_play_char_instance
+(
+ BattlemapInstance,
+ PlayerID,
+ CharInstID
+) ->
+ (
+ (
+ array:get
+ (
+ BattlemapInstance#battlemap_instance.curr_player,
+ BattlemapInstance#battlemap_instance.players
+ )
+ =:=
+ PlayerID
+ )
+ and
+ lists:member(CharInstID, BattlemapInstance#battlemap_instance.rem_chars)
+ ).
+
+post_play_char_instance (BattlemapInstance, CharInstID) ->
+ case BattlemapInstance#battlemap_instance.rem_chars of
+ [CharInstID|[]] ->
+ NextPlayer =
+ (
+ (BattlemapInstance#battlemap_instance.curr_player + 1)
+ rem
+ array:size(BattlemapInstance#battlemap_instance.players)
+ ),
+ BattlemapInstance#battlemap_instance
+ {
+ curr_player = NextPlayer,
+ rem_chars =
+ lists:filtermap
+ (
+ fun ({K, V}) ->
+ case character_instance:get_owner(V) of
+ NextPlayer -> {true, K};
+ _ -> false
+ end
+ end,
+ dict:to_list(BattlemapInstance#battlemap_instance.chars)
+ )
+ };
+
+ _ ->
+ BattlemapInstance#battlemap_instance
+ {
+ rem_chars =
+ lists:delete
+ (
+ CharInstID,
+ BattlemapInstance#battlemap_instance.rem_chars
+ )
+ }
+ end.
+
+get_char_instance (BattlemapInstance, CharInstID) ->
+ dict:fetch(CharInstID, BattlemapInstance#battlemap_instance.chars).
+
+set_char_instance (BattlemapInstance, CharInstID, CharInst) ->
+ BattlemapInstance#battlemap_instance
+ {
+ chars =
+ dict:store
+ (
+ CharInstID,
+ CharInst,
+ BattlemapInstance#battlemap_instance.chars
+ )
+ }.
diff --git a/src/type/battlemap_instance_shim.erl b/src/type/battlemap_instance_shim.erl
new file mode 100644
index 0000000..80a1007
--- /dev/null
+++ b/src/type/battlemap_instance_shim.erl
@@ -0,0 +1,40 @@
+-module(battlemap_instance_shim).
+-record
+(
+ battlemap_instance,
+ {
+ id,
+ chars,
+ curr_player,
+ players,
+ rem_chars,
+ last_turn
+ }
+).
+-export
+(
+ [
+ generate_random/2
+ ]
+).
+
+generate_random (CharInsts, Players) ->
+ #battlemap_instance
+ {
+ id = <<"0">>,
+ chars = dict:from_list(CharInsts),
+ curr_player = 0,
+ players = array:from_list(Players),
+ rem_chars =
+ lists:filtermap
+ (
+ fun ({K, V}) ->
+ case character_instance:get_owner(V) of
+ 0 -> {true, K};
+ _ -> false
+ end
+ end,
+ CharInsts
+ ),
+ last_turn = []
+ }.
diff --git a/src/type/battlemap_shim.erl b/src/type/battlemap_shim.erl
new file mode 100644
index 0000000..1fb035e
--- /dev/null
+++ b/src/type/battlemap_shim.erl
@@ -0,0 +1,56 @@
+-module(battlemap_shim).
+-record
+(
+ battlemap,
+ {
+ id,
+ width,
+ height,
+ content,
+ instances
+ }
+).
+-export
+(
+ [
+ generate_random/0
+ ]
+).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% LOCAL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+generate(_Prev, Result, _X, 0, _BaseWidth) ->
+ Result;
+generate(Prev, Result, 0, Y, BaseWidth) ->
+ generate(Prev, Result, BaseWidth, (Y - 1), BaseWidth);
+generate(Prev, Result, X, Y, BaseWidth) ->
+ case rand:uniform(100) of
+ N when (N >= 10) ->
+ generate(Prev, [Prev|Result], (X - 1), Y, BaseWidth);
+
+ N ->
+ NewTileType = (N - 1),
+ generate
+ (
+ NewTileType,
+ [NewTileType|Result],
+ (X - 1),
+ Y,
+ BaseWidth
+ )
+ end.
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% EXPORTED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+generate_random () ->
+ Width = (rand:uniform(48) + 16),
+ Height = (rand:uniform(48) + 16),
+ #battlemap
+ {
+ id = <<"0">>,
+ width = Width,
+ height = Height,
+ content = array:from_list(generate(0, [], Width, Height, Width))
+ }.
diff --git a/src/type/character.erl b/src/type/character.erl
new file mode 100644
index 0000000..e5b6d9f
--- /dev/null
+++ b/src/type/character.erl
@@ -0,0 +1,34 @@
+-module(character).
+-record
+(
+ character,
+ {
+ id,
+ name,
+ icon,
+ portrait,
+ health,
+ mov_pts,
+ atk_rg
+ }
+).
+-export
+(
+ [
+ get_id/1,
+ get_name/1,
+ get_icon/1,
+ get_portrait/1,
+ get_max_health/1,
+ get_movement_points/1,
+ get_attack_range/1
+ ]
+).
+
+get_id (Char) -> Char#character.id.
+get_name (Char) -> Char#character.name.
+get_icon (Char) -> Char#character.icon.
+get_portrait (Char) -> Char#character.portrait.
+get_max_health (Char) -> Char#character.health.
+get_movement_points (Char) -> Char#character.mov_pts.
+get_attack_range (Char) -> Char#character.atk_rg.
diff --git a/src/type/character_instance.erl b/src/type/character_instance.erl
new file mode 100644
index 0000000..5432c99
--- /dev/null
+++ b/src/type/character_instance.erl
@@ -0,0 +1,72 @@
+-module(character_instance).
+-record
+(
+ character_instance,
+ {
+ x,
+ y,
+ health,
+ team
+ }
+).
+-export
+(
+ [
+ get_location/1,
+ get_current_health/1,
+ get_owner/1
+ ]
+).
+-export
+(
+ [
+ new_instance_of/3
+ ]
+).
+-export
+(
+ [
+ set_location/3,
+ mod_health/3,
+ is_dead/1 % is_alive is reserved.
+ ]
+).
+
+get_location (CharInst) ->
+ {CharInst#character_instance.x, CharInst#character_instance.y}.
+
+get_current_health (CharInst) -> CharInst#character_instance.health.
+
+get_owner (CharInst) -> CharInst#character_instance.team.
+
+new_instance_of (Char, Owner, {X, Y}) ->
+ #character_instance
+ {
+ x = X,
+ y = Y,
+ health = character:get_max_health(Char),
+ team = Owner
+ }.
+
+set_location (CharInst, X, Y) ->
+ CharInst#character_instance
+ {
+ x = X,
+ y = Y
+ }.
+
+mod_health (CharInst, MaxHealth, HealthMod) ->
+ NewHealth = (CharInst#character_instance.health + HealthMod),
+ if
+ (NewHealth < 0) ->
+ CharInst#character_instance{ health = 0 };
+
+ (NewHealth > MaxHealth) ->
+ CharInst#character_instance{ health = MaxHealth };
+
+ true ->
+ CharInst#character_instance{ health = NewHealth }
+ end.
+
+is_dead (CharInst) -> (CharInst#character_instance.health == 0).
+
diff --git a/src/type/character_shim.erl b/src/type/character_shim.erl
new file mode 100644
index 0000000..0ad357d
--- /dev/null
+++ b/src/type/character_shim.erl
@@ -0,0 +1,47 @@
+-module(character_shim).
+-record
+(
+ character,
+ {
+ id,
+ name,
+ icon,
+ portrait,
+ health,
+ mov_pts,
+ atk_rg
+ }
+).
+-export
+(
+ [
+ generate_random/1
+ ]
+).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% LOCAL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+generate_char (N) ->
+ IDAsString = list_to_binary(integer_to_list(N)),
+ #character
+ {
+ id = IDAsString, % ID
+ name = IDAsString, % Name
+ icon = IDAsString, % Icon
+ portrait = IDAsString, % Portrait
+ health = (rand:uniform(5) + 1),
+ mov_pts = (rand:uniform(10) + 10), % Movement Points
+ atk_rg = (rand:uniform(5) - 1) % Attack Range
+ }.
+
+generate (0, Result) ->
+ Result;
+generate (N, Prev) ->
+ generate((N - 1), [generate_char(N - 1)|Prev]).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% EXPORTED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+generate_random (N) ->
+ generate(N, []).