summaryrefslogtreecommitdiff
blob: c3eabb006d0cb9a3ef0ff3ef4df517d1aa8b9b0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
-module(bm_battlemap).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-type id() :: binary().

-record
(
   battlemap,
   {
      id :: id(),
      width :: integer(),
      height :: integer(),
      tile_ids :: array:array(bm_tile:id())
   }
).

-opaque type() :: #battlemap{}.

-export_type([type/0, id/0]).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Accessors
-export
(
   [
      get_id/1,
      get_width/1,
      get_height/1,
      get_tile_ids/1,
      get_tile_id/2
   ]
).

-export
(
   [
      random/3
   ]
).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-spec generate_random_tile_ids
   (
      bm_tile:id(),
      list(bm_tile:id()),
      non_neg_integer(),
      non_neg_integer(),
      non_neg_integer()
   )
   -> list(bm_tile:id()).
generate_random_tile_ids (_PreviousTileID, Result, _X, 0, _Width) ->
   Result;
generate_random_tile_ids (PreviousTileID, Result, 0, Y, Width) ->
   generate_random_tile_ids(PreviousTileID, Result, Width, (Y - 1), Width);
generate_random_tile_ids (PreviousTileID, Result, X, Y, Width) ->
   NewTile =
      case sh_roll:percentage() of
         N when (N >= 10) -> PreviousTileID;
         _ -> bm_tile:random_id()
      end,
   generate_random_tile_ids(NewTile, [NewTile|Result], (X - 1), Y, Width).

-spec location_to_array_index
   (
      non_neg_integer(),
      bm_location:type()
   )
   -> ('error' | non_neg_integer()).
location_to_array_index (ArrayWidth, {X, Y}) ->
   if
      (X < 0) -> error;
      (Y < 0) -> error;
      (X >= ArrayWidth) -> error;
      true -> ((Y * ArrayWidth) + X)
   end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Accessors
-spec get_id (type()) -> id().
get_id (Battlemap) -> Battlemap#battlemap.id.

-spec get_width (type()) -> integer().
get_width (Battlemap) -> Battlemap#battlemap.width.

-spec get_height (type()) -> integer().
get_height (Battlemap) -> Battlemap#battlemap.height.

-spec get_tile_ids (type()) -> array:array(bm_tile:id()).
get_tile_ids (Battlemap) -> Battlemap#battlemap.tile_ids.

-spec get_tile_id (bm_location:type(), type()) -> bm_tile:id().
get_tile_id (Location, Battlemap) ->
   TileIX = location_to_array_index(Battlemap#battlemap.width, Location),
   array:get(TileIX, Battlemap#battlemap.tile_ids).

-spec random
   (
      non_neg_integer(),
      non_neg_integer(),
      non_neg_integer()
   )
   -> type().
random (ID, Width, Height) ->
   InitialTile = bm_tile:random_id(),
   TileIDs = generate_random_tile_ids(InitialTile, [], Width, Height, Width),

   #battlemap
   {
      id = list_to_binary(integer_to_list(ID)),
      width = Width,
      height = Height,
      tile_ids = array:from_list(TileIDs)
   }.