summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornsensfel <SpamShield0@noot-noot.org>2018-10-01 17:28:10 +0200
committernsensfel <SpamShield0@noot-noot.org>2018-10-01 17:28:10 +0200
commitd4414ea403c77aae2433ea97cb18d4cf69d3b845 (patch)
tree1bf1509538970c110dc7931044ee13a81d27b879
parentad9535ddc9cc733548d7c1701add913ef9a7fc79 (diff)
Working on battle generation.
-rw-r--r--src/battle/btl_shim.erl2
-rw-r--r--src/battle/struct/btl_map.erl31
-rw-r--r--src/special/spe_battle.erl120
3 files changed, 140 insertions, 13 deletions
diff --git a/src/battle/btl_shim.erl b/src/battle/btl_shim.erl
index 915b75e..aeb4b10 100644
--- a/src/battle/btl_shim.erl
+++ b/src/battle/btl_shim.erl
@@ -231,7 +231,7 @@ generate_random_battle () ->
%MapWidth = 32, % shr_roll:between(16, 32),
%MapHeight = 32, %shr_roll:between(16, 32),
%Map = btl_map:random(0, MapWidth, MapHeight),
- Map = btl_map:from_list(0, 32, 32, new_demo_map()),
+ Map = btl_map:from_list(32, 32, new_demo_map()),
Characters = generate_random_characters(1, 8, 8, 0, Map, [], []),
PlayersAsList = [btl_player:new(0, 8, <<"0">>), btl_player:new(1, 0, <<"1">>)],
diff --git a/src/battle/struct/btl_map.erl b/src/battle/struct/btl_map.erl
index c784bca..157382d 100644
--- a/src/battle/struct/btl_map.erl
+++ b/src/battle/struct/btl_map.erl
@@ -3,13 +3,10 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--type id() :: binary().
-
-record
(
map,
{
- id :: id(),
width :: integer(),
height :: integer(),
tile_ids :: array:array(shr_tile:instance())
@@ -18,7 +15,7 @@
-opaque type() :: #map{}.
--export_type([type/0, id/0]).
+-export_type([type/0]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -27,7 +24,6 @@
-export
(
[
- get_id/1,
get_width/1,
get_height/1,
get_tile_instances/1,
@@ -38,7 +34,8 @@
-export
(
[
- from_list/4
+ from_list/3,
+ from_array/3
]
).
@@ -63,9 +60,6 @@ location_to_array_index (ArrayWidth, {X, Y}) ->
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Accessors
--spec get_id (type()) -> id().
-get_id (Map) -> Map#map.id.
-
-spec get_width (type()) -> integer().
get_width (Map) -> Map#map.width.
@@ -84,17 +78,30 @@ get_tile_instance (Location, Map) ->
(
non_neg_integer(),
non_neg_integer(),
- non_neg_integer(),
list(list(non_neg_integer()))
)
-> type().
-from_list (ID, Width, Height, List) ->
+from_list (Width, Height, List) ->
TileInstances = lists:map(fun shr_tile:instance_from_ints/1, List),
#map
{
- id = list_to_binary(integer_to_list(ID)),
width = Width,
height = Height,
tile_ids = array:from_list(TileInstances)
}.
+
+-spec from_array
+ (
+ non_neg_integer(),
+ non_neg_integer(),
+ array:array(shr_tile:instance())
+ )
+ -> type().
+from_array (Width, Height, TileInstances) ->
+ #map
+ {
+ width = Width,
+ height = Height,
+ tile_ids = TileInstances
+ }.
diff --git a/src/special/spe_battle.erl b/src/special/spe_battle.erl
new file mode 100644
index 0000000..1b3453c
--- /dev/null
+++ b/src/special/spe_battle.erl
@@ -0,0 +1,120 @@
+-module(spe_battle).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+-export([generate/2]).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+-spec reserve_id () -> binary().
+reserve_id () -> <<"0">>.
+
+-spec get_equipment_ids
+ (
+ list(btl_character:type())
+ )
+ -> {sets:set(binary()), sets:set(binary())}.
+get_equipment_ids (Characters) ->
+ {UsedWeaponIDs, UsedArmorIDs} =
+ lists:foldl
+ (
+ fun (Character, {UWIDs, UAIDs}) ->
+ {MWpID, SWpID} = btl_character:get_weapon_ids(Character),
+ AID = btl_character:get_armor_id(Character),
+ {
+ sets:add_element(MWpID, sets:add_element(SWpID, UWIDs)),
+ sets:add_element(AID, UAIDs)
+ }
+ end,
+ {sets:new(), sets:new()},
+ Characters
+ ),
+
+ {UsedWeaponIDs, UsedArmorIDs}.
+
+-spec get_tile_ids (array:array(shr_tile:type())) -> sets:set(binary()).
+get_tile_ids (TileInstances) ->
+ UsedTileIDs =
+ array:sparse_foldl
+ (
+ fun (_IX, TileInstance, CurrentTileIDs) ->
+ sets:add_element
+ (
+ shr_tile:extract_main_class_id(TileInstance),
+ CurrentTileIDs
+ )
+ end,
+ sets:new(),
+ TileInstances
+ ),
+
+ UsedTileIDs.
+
+-spec handle_rosters
+ (
+ list(rst_roster:type())
+ )
+ -> {list(btl_character:type()), list(btl_player:type())}.
+handle_rosters (_Rosters) ->
+ %% TODO Unimplemented.
+ {[], []}.
+
+-spec generate_battle
+ (
+ binary(),
+ map_map:type(),
+ list(rst_roster:type())
+ )
+ -> btl_battle:type().
+generate_battle (ID, Map, Rosters) ->
+ TileInstances = map_map:get_tile_instances(Map),
+ BattleMap =
+ btl_map:from_array
+ (
+ map_map:get_width(Map),
+ map_map:get_height(Map),
+ TileInstances
+ ),
+ {Characters, PlayersAsList} = handle_rosters(Rosters),
+ {UsedWeaponIDs, UsedArmorIDs} = get_equipment_ids(Characters),
+ UsedTileIDs = get_tile_ids(TileInstances),
+
+ Battle =
+ btl_battle:new
+ (
+ ID,
+ PlayersAsList,
+ BattleMap,
+ Characters,
+ sets:to_list(UsedWeaponIDs),
+ sets:to_list(UsedArmorIDs),
+ sets:to_list(UsedTileIDs)
+ ),
+
+ Battle.
+
+-spec commit (btl_battle:type()) -> ok.
+commit (_Battle) ->
+ %% TODO Unimplemented.
+ ok.
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+-spec generate
+ (
+ map_map:type(),
+ list(rst_roster:type())
+ )
+ -> btl_battle:type().
+generate (Map, Rosters) ->
+ ID = reserve_id(),
+ Battle = generate_battle(ID, Map, Rosters),
+ ok = commit(Battle),
+ Battle.