summaryrefslogtreecommitdiff |
diff options
author | nsensfel <SpamShield0@noot-noot.org> | 2018-02-26 13:52:29 +0100 |
---|---|---|
committer | nsensfel <SpamShield0@noot-noot.org> | 2018-02-26 13:52:29 +0100 |
commit | fd4d031ab5b31763d376c663cc049f62ee389243 (patch) | |
tree | cd09a29bb888acf772fa1aee59fe95e8bd75fe4e | |
parent | 66ec11ce5d2e227846d6e6b2899cda851a70fc04 (diff) |
Got it to run, at last.
-rw-r--r-- | src/battlemap/movement.erl | 68 | ||||
-rw-r--r-- | src/battlemap/roll.erl (renamed from src/shim/weapon_shim.erl) | 12 | ||||
-rw-r--r-- | src/io/timed_cache.erl | 4 | ||||
-rw-r--r-- | src/query/character_turn.erl | 53 | ||||
-rw-r--r-- | src/shim/attributes_shim.erl | 47 | ||||
-rw-r--r-- | src/shim/battlemap_instance_shim.erl | 55 | ||||
-rw-r--r-- | src/shim/battlemap_shim.erl | 64 | ||||
-rw-r--r-- | src/shim/character_shim.erl | 57 | ||||
-rw-r--r-- | src/shim/database_shim.erl | 104 | ||||
-rw-r--r-- | src/struct/attributes.erl | 19 | ||||
-rw-r--r-- | src/struct/battlemap.erl | 31 | ||||
-rw-r--r-- | src/struct/battlemap_instance.erl | 42 | ||||
-rw-r--r-- | src/struct/character.erl | 27 | ||||
-rw-r--r-- | src/struct/character_instance.erl | 35 | ||||
-rw-r--r-- | src/struct/statistics.erl | 2 | ||||
-rw-r--r-- | src/struct/tile.erl (renamed from src/data/tile.erl) | 10 | ||||
-rw-r--r-- | src/struct/weapon.erl | 27 |
17 files changed, 357 insertions, 300 deletions
diff --git a/src/battlemap/movement.erl b/src/battlemap/movement.erl new file mode 100644 index 0000000..7fc5db1 --- /dev/null +++ b/src/battlemap/movement.erl @@ -0,0 +1,68 @@ +-module(movement). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +-export +( + [ + cross/4, + steps_between/2 + ] +). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +location_after_step (Step, X, Y) -> + case Step of + <<"L">> -> {(X - 1), Y}; + <<"R">> -> {(X + 1), Y}; + <<"U">> -> {X, (Y - 1)}; + <<"D">> -> {X, (Y + 1)} + end. + +location_to_array_index (ArrayWidth, X, Y) -> + if + (X < 0) -> -1; + (Y < 0) -> -1; + (X >= ArrayWidth) -> error; + true -> ((Y * ArrayWidth) + X) + end. + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +cross (_Battlemap, _ForbiddenLocations, [], Cost, X, Y) -> + {{X, Y}, Cost}; +cross (Battlemap, ForbiddenLocations, [Step|NextSteps], Cost, X, Y) -> + BattlemapTiles = battlemap:get_tiles(Battlemap), + + {NextX, NextY} = location_after_step(Step, X, Y), + NextTileIX = + location_to_array_index(array:size(BattlemapTiles), NextX, NextY), + NextTile = array:get(array:get(NextTileIX, BattlemapTiles)), + NextCost = (Cost + tile:get_cost(NextTile)), + IsForbidden = + array:foldl + ( + fun (_IX, Location, Prev) -> + (Prev or ({NextX, NextY} == Location)) + end, + ForbiddenLocations + ), + + IsForbidden = false, + + cross(Battlemap, ForbiddenLocations, NextSteps, NextCost, NextX, NextY). + +cross (Battlemap, ForbiddenLocations, Path, {X, Y}) -> + cross(Battlemap, ForbiddenLocations, Path, 0, X, Y). + +steps_between ({OX, OY}, {DX, DY}) -> + (abs(DY - OY) + abs(DX - OX)). diff --git a/src/shim/weapon_shim.erl b/src/battlemap/roll.erl index e0364f0..803a6de 100644 --- a/src/shim/weapon_shim.erl +++ b/src/battlemap/roll.erl @@ -1,4 +1,4 @@ --module(weapon_shim). +-module(roll). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -10,7 +10,8 @@ -export ( [ - rand/0 + percentage/0, + between/2 ] ). @@ -21,4 +22,9 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -rand() -> (rand:uniform(25) - 1). +between (Min, Max) -> + Diff = (Max - Min), + (Min + (rand:uniform(Diff + 1) - 1)). + +percentage () -> + between(0, 100). diff --git a/src/io/timed_cache.erl b/src/io/timed_cache.erl index 132b70f..d76a4f0 100644 --- a/src/io/timed_cache.erl +++ b/src/io/timed_cache.erl @@ -98,9 +98,9 @@ update (DB, Owner, ObjectID, Data) -> [] -> ok; [{_OwnerID, TimerPID, _Data}] -> - gen_server:stop(TimerPID), + gen_server:stop(TimerPID) end, - add_update_to_cache(DB, Owner, ObjectID); + add_update_to_cache(DB, Owner, ObjectID, Data). invalidate (DB, Owner, ObjectID) -> diff --git a/src/query/character_turn.erl b/src/query/character_turn.erl index c71791c..0519a80 100644 --- a/src/query/character_turn.erl +++ b/src/query/character_turn.erl @@ -113,26 +113,28 @@ handle_character_instance_moving (QueryState, Input) -> character_instance:get_character(ControlledCharacterInstance), Path = Input#input.path, Battlemap = battlemap_instance:get_battlemap(BattlemapInstance), + ControlledCharacterStatistics = + character:get_statistics(ControlledCharacter), + ControlledCharacterMovementPoints = + statistics:get_movement_points(ControlledCharacterStatistics), + ForbiddenLocations = array:map ( - fun (CharacterInstance) -> - character_instance:get_location(CharacterInstance) - end, + fun character_instance:get_location/1, battlemap_instance:get_character_instances(BattlemapInstance) ), - {ok, NewLocation, _} = + {NewLocation, Cost} = movement:cross ( Battlemap, - character_instance:get_location(ControlledCharacterInstance), - statistics:get_movement_points - ( - character:get_statistics(ControlledCharacter) - ), + ForbiddenLocations, Path, - ForbiddenLocations + character_instance:get_location(ControlledCharacterInstance) ), + + true = (Cost =< ControlledCharacterMovementPoints), + QueryState#query_state { character_instance = @@ -346,11 +348,32 @@ play (QueryState, [attack|Next], Input) -> Input ). -send_to_database (_QueryResult, _TurnType, _Input) -> - unimplemented. +send_to_database (QueryResult, _TurnType, Input) -> + PlayerID = Input#input.player_id, + BattlemapInstanceID = Input#input.battlemap_instance_id, + BattlemapInstance = QueryResult#query_result.updated_battlemap_instance, -update_cache (_QueryResult, _TurnType, _Input) -> - unimplemented. + %% TODO: differential commit + database_shim:commit + ( + battlemap_instance_db, + PlayerID, + BattlemapInstanceID, + BattlemapInstance + ). + +update_cache (QueryResult, Input) -> + PlayerID = Input#input.player_id, + BattlemapInstanceID = Input#input.battlemap_instance_id, + BattlemapInstance = QueryResult#query_result.updated_battlemap_instance, + + timed_cache:update + ( + battlemap_instance_db, + PlayerID, + BattlemapInstanceID, + BattlemapInstance + ). generate_reply (_QueryResult, _TurnType, _Input) -> unimplemented. @@ -372,7 +395,7 @@ handle (Req) -> ) ), send_to_database(QueryResult, TurnType, Input), - update_cache(QueryResult, TurnType, Input), + update_cache(QueryResult, Input), security:unlock_queries(Input#input.player_id), generate_reply(QueryResult, TurnType, Input). diff --git a/src/shim/attributes_shim.erl b/src/shim/attributes_shim.erl deleted file mode 100644 index b80ee6d..0000000 --- a/src/shim/attributes_shim.erl +++ /dev/null @@ -1,47 +0,0 @@ --module(attributes_shim). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --record -( - attributes, - { - constitution, - dexterity, - intelligence, - mind, - speed, - strength - } -). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%% Accessors --export -( - [ - rand/0 - ] -). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -random_attribute (Min, Max) -> (rand:uniform(Max - Min) - 1) + Min. - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -rand () -> - #attributes - { - constitution = random_attribute(10, 100), - dexterity = random_attribute(10, 100), - intelligence = random_attribute(10, 100), - mind = random_attribute(10, 100), - speed = random_attribute(10, 100), - strength = random_attribute(10, 100) - }. diff --git a/src/shim/battlemap_instance_shim.erl b/src/shim/battlemap_instance_shim.erl deleted file mode 100644 index 94ae7ce..0000000 --- a/src/shim/battlemap_instance_shim.erl +++ /dev/null @@ -1,55 +0,0 @@ --module(battlemap_instance_shim). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --record -( - battlemap_instance, - { - id, - chars, - curr_player, - players, - rem_chars, - last_turn - } -). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --export -( - [ - generate_random/2 - ] -). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -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/shim/battlemap_shim.erl b/src/shim/battlemap_shim.erl deleted file mode 100644 index b19a653..0000000 --- a/src/shim/battlemap_shim.erl +++ /dev/null @@ -1,64 +0,0 @@ --module(battlemap_shim). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --record -( - battlemap, - { - id, - width, - height, - content, - instances - } -). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --export -( - [ - generate_random/0 - ] -). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -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 FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -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/shim/character_shim.erl b/src/shim/character_shim.erl deleted file mode 100644 index 35a0753..0000000 --- a/src/shim/character_shim.erl +++ /dev/null @@ -1,57 +0,0 @@ --module(character_shim). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% TYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --record -( - character, - { - id, - name, - icon, - portrait, - attributes, - weapons, - glyphs, - armors - } -). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% --export -( - [ - generate_random/1 - ] -). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -generate_char (N) -> - IDAsString = list_to_binary(integer_to_list(N)), - #character - { - id = IDAsString, % ID - name = IDAsString, % Name - icon = IDAsString, % Icon - portrait = IDAsString, % Portrait - attributes = attributes_shim:rand(), - weapons = {weapon_shim:rand(), weapon_shim:rand()}, - glyphs = [], - armors = [] - }. - -generate (0, Result) -> - Result; -generate (N, Prev) -> - generate((N - 1), [generate_char(N - 1)|Prev]). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -generate_random (N) -> - generate(N, []). diff --git a/src/shim/database_shim.erl b/src/shim/database_shim.erl index d55d848..1589d14 100644 --- a/src/shim/database_shim.erl +++ b/src/shim/database_shim.erl @@ -12,8 +12,7 @@ [ generate_db/1, fetch/2, - commit/4, - assert_session_is_valid/2 + commit/4 ] ). @@ -38,24 +37,47 @@ add_to_db (ID, Val) -> io:format("~nadd to db_shim: ~p.~n", [{ID, Val}]), ets:insert(db_shim, {ID, Val}). -generate_char_instances (Battlemap, Characters) -> - lists:map +generate_random_characters +( + 0, + 0, + _CharactersPerPlayer, + _TotalCharacterCount, + Result +) -> + Result; +generate_random_characters +( + MaxPlayerID, + 0, + CharactersPerPlayer, + TotalCharacterCount, + Result +) -> + generate_random_characters ( - fun (Char) -> - { - character:get_id(Char), - character_instance:new_instance_of - ( - Char, - (rand:uniform(2) - 1), % team, - { - rand:uniform(battlemap:get_width(Battlemap) - 1), % X - rand:uniform(battlemap:get_height(Battlemap) - 1) % Y - } - ) - } - end, - Characters + (MaxPlayerID - 1), + CharactersPerPlayer, + CharactersPerPlayer, + TotalCharacterCount, + Result + ); +generate_random_characters +( + MaxPlayerID, + PlayerCharacterCount, + CharactersPerPlayer, + TotalCharacterCount, + Result +) -> + NewCharacter = character:random(TotalCharacterCount, MaxPlayerID), + generate_random_characters + ( + MaxPlayerID, + (PlayerCharacterCount - 1), + CharactersPerPlayer, + (TotalCharacterCount + 1), + [NewCharacter|Result] ). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -67,29 +89,21 @@ generate_db (Heir) -> receive ok -> ok end, - Players = [<<"0">>, <<"1">>], - Battlemap = battlemap_shim:generate_random(), - Characters = character_shim:generate_random(rand:uniform(12) + 4), - CharacterInsts = generate_char_instances(Battlemap, Characters), + BattlemapWidth = roll:between(16, 64), + BattlemapHeight = roll:between(16, 64), + Battlemap = battlemap:random(0, BattlemapWidth, BattlemapHeight), + Characters = generate_random_characters(1, 7, 8, 0, []), + PlayersAsList = [<<"0">>, <<"1">>], BattlemapInstance = - battlemap_instance_shim:generate_random + battlemap_instance_shim:random ( - CharacterInsts, - Players + <<"0">>, + PlayersAsList, + Battlemap, + Characters ), - add_to_db({battlemap_db, battlemap:get_id(Battlemap)}, Battlemap), - lists:map - ( - fun (Char) -> - add_to_db({character_db, character:get_id(Char)}, Char) - end, - Characters - ), - add_to_db - ( - {battlemap_instance_db, battlemap_instance:get_id(BattlemapInstance)}, - BattlemapInstance - ). + + add_to_db({battlemap_instance_db, <<"0">>}, BattlemapInstance). fetch (DB, ObjectID) -> io:format("~ndb_shim lookup: ~p.~n", [{DB, ObjectID}]), @@ -98,13 +112,5 @@ fetch (DB, ObjectID) -> [] -> nothing end. -commit (DB, Owner, ObjectID, Value) -> - add_to_db({DB, ObjectID}, Value), - timed_cache:invalidate(DB, Owner, ObjectID). - -assert_session_is_valid (_PlayerID, _SessionToken) -> - % Ask PlayerID's login server if SessionToken is correct. - % If so, update last login time to prevent relogin within - % (database_timeout * 2). - % If not, crash. - ok. +commit (DB, _Owner, ObjectID, Value) -> + add_to_db({DB, ObjectID}, Value). diff --git a/src/struct/attributes.erl b/src/struct/attributes.erl index 022cad2..bb68032 100644 --- a/src/struct/attributes.erl +++ b/src/struct/attributes.erl @@ -39,6 +39,14 @@ ] ). +%%%% Accessors +-export +( + [ + random/0 + ] +). + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -66,3 +74,14 @@ set_speed (Val, Att) -> Att#attributes{ speed = Val }. set_strength (Val, Att) -> Att#attributes{ strength = Val }. + +random () -> + #attributes + { + constitution = roll:percentage(), + dexterity = roll:percentage(), + intelligence = roll:percentage(), + mind = roll:percentage(), + speed = roll:percentage(), + strength = roll:percentage() + }. diff --git a/src/struct/battlemap.erl b/src/struct/battlemap.erl index b0eb5fb..b999436 100644 --- a/src/struct/battlemap.erl +++ b/src/struct/battlemap.erl @@ -27,9 +27,28 @@ get_tile_ids/1 ] ). + +-export +( + [ + random/3 + ] +). + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +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 roll:percentage() of + N when (N >= 10) -> PreviousTileID; + _ -> tile:random_id() + end, + generate_random_tile_ids(NewTile, [NewTile|Result], (X - 1), Y, Width). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -39,3 +58,15 @@ get_id (Battlemap) -> Battlemap#battlemap.id. get_width (Battlemap) -> Battlemap#battlemap.width. get_height (Battlemap) -> Battlemap#battlemap.height. get_tile_ids (Battlemap) -> Battlemap#battlemap.tile_ids. + +random (ID, Width, Height) -> + InitialTile = tile:random_id(), + TileIDs = generate_random_tile_ids(InitialTile, [], Width, Height, Width), + + #battlemap + { + id = ID, + width = Width, + height = Height, + tile_ids = TileIDs + }. diff --git a/src/struct/battlemap_instance.erl b/src/struct/battlemap_instance.erl index 9a57d09..d031ccd 100644 --- a/src/struct/battlemap_instance.erl +++ b/src/struct/battlemap_instance.erl @@ -38,6 +38,13 @@ ] ). +-export +( + [ + random/4 + ] +). + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -92,3 +99,38 @@ set_last_turns_effects (Effects, BattlemapInstance) -> { last_turns_effects = Effects }. + +random (ID, PlayersAsList, Battlemap, Characters) -> + BattlemapWidth = battlemap:get_width(Battlemap), + BattlemapHeight = battlemap:get_height(Battlemap), + CharacterInstancesAsList = + lists:mapfoldl + ( + fun (Character, ForbiddenLocations) -> + NewCharacterInstance = + character_instance:random + ( + Character, + BattlemapWidth, + BattlemapHeight, + ForbiddenLocations + ), + NewCharacterInstanceLocation = + character_instance:get_location(NewCharacterInstance), + { + NewCharacterInstance, + [NewCharacterInstanceLocation|ForbiddenLocations] + } + end, + Characters + ), + + #battlemap_instance + { + id = ID, + battlemap = Battlemap, + character_instances = array:from_list(CharacterInstancesAsList), + players = array:from_list(PlayersAsList), + current_player_turn = player_turn:new(0, 0), + last_turns_effects = [] + }. diff --git a/src/struct/character.erl b/src/struct/character.erl index 0459214..90e449c 100644 --- a/src/struct/character.erl +++ b/src/struct/character.erl @@ -41,6 +41,13 @@ ] ). +-export +( + [ + random/2 + ] +). + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -71,3 +78,23 @@ set_statistics (Stats, Char) -> { statistics = Stats }. + +random (ID, OwnerID) -> + WeaponIDs = {weapon:random_id(), weapon:random_id()}, + Attributes = attributes:random(), + Statistics = statistics:new(Attributes, WeaponIDs), + IDAsListString = integer_to_list(ID), + IDAsBinaryString = list_to_binary(IDAsListString), + + #character + { + id = ID, + owner_id = OwnerID, + name = list_to_binary("Char" ++ IDAsListString), + icon = IDAsBinaryString, + portrait = IDAsBinaryString, + attributes = Attributes, + weapon_ids = WeaponIDs, + glyphs = [], + statistics = Statistics + }. diff --git a/src/struct/character_instance.erl b/src/struct/character_instance.erl index c530424..e736a4c 100644 --- a/src/struct/character_instance.erl +++ b/src/struct/character_instance.erl @@ -20,7 +20,8 @@ -export ( [ - new/2 + new/2, + random/4 ] ). @@ -43,6 +44,23 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +find_random_location (BattlemapWidth, BattlemapHeight, ForbiddenLocations) -> + X = roll:between(0, (BattlemapWidth - 1)), + Y = roll:between(0, (BattlemapHeight - 1)), + + IsForbidden = lists:member({X, Y}, ForbiddenLocations), + + case IsForbidden of + true -> + find_random_location + ( + BattlemapWidth, + BattlemapHeight, + ForbiddenLocations + ); + + _ -> {X, Y} + end. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -83,12 +101,19 @@ set_is_active (Active, CharInst) -> }. %%%% Utils -new (Char, Location) -> - Stats = character:get_statistics(Char), +new (Character, Location) -> + CharacterStatistics = character:get_statistics(Character), #character_instance { - character = Char, + character = Character, location = Location, - current_health = statistics:get_health(Stats), + current_health = statistics:get_health(CharacterStatistics), active = false }. + +random (Character, BattlemapWidth, BattlemapHeight, ForbiddenLocations) -> + new + ( + Character, + find_random_location(BattlemapWidth, BattlemapHeight, ForbiddenLocations) + ). diff --git a/src/struct/statistics.erl b/src/struct/statistics.erl index f5a4650..f8797bb 100644 --- a/src/struct/statistics.erl +++ b/src/struct/statistics.erl @@ -110,7 +110,7 @@ new (BaseAttributes, WeaponIDs) -> {ActiveWeaponID, _} = WeaponIDs, ActiveWeapon = weapon:from_id(ActiveWeaponID), {MinDamage, MaxDamage} = weapon:get_damages(ActiveWeapon), - Attributes = weapon:apply_to_attributes(ActiveWeapon, BaseAttributes), + Attributes = weapon:apply_to_attributes(BaseAttributes, ActiveWeapon), Constitution = attributes:get_constitution(Attributes), Dexterity = attributes:get_dexterity(Attributes), Intelligence = attributes:get_intelligence(Attributes), diff --git a/src/data/tile.erl b/src/struct/tile.erl index de5e6cc..05da3ec 100644 --- a/src/data/tile.erl +++ b/src/struct/tile.erl @@ -15,6 +15,13 @@ ] ). +-export +( + [ + random_id/0 + ] +). + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% LOCAL FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -29,3 +36,6 @@ get_cost (N) -> (N =< 200) -> (N + 8); true -> cost_when_oob() end. + +random_id () -> + roll:between(0, 15). diff --git a/src/struct/weapon.erl b/src/struct/weapon.erl index add445a..ca74ada 100644 --- a/src/struct/weapon.erl +++ b/src/struct/weapon.erl @@ -23,7 +23,8 @@ -export ( [ - get_id/1 + get_id/1, + random_id/0 ] ). @@ -32,7 +33,8 @@ [ from_id/1, get_ranges/1, - get_damages/1 + get_damages/1, + apply_to_attributes/2 ] ). @@ -285,3 +287,24 @@ from_id (24) -> damage_type = pierce, damage_mod = heavy }. + +random_id () -> + roll:between(0, 24). + +apply_to_attributes (Attributes, Weapon) -> + Dexterity = attributes:get_dexterity(Attributes), + Speed = attributes:get_dexterity(Attributes), + RangeModifier = Weapon#weapon.range_mod, + DamageModifier = Weapon#weapon.damage_mod, + WithRangeModifier = + case RangeModifier of + long -> + attributes:set_dexterity(max(0, (Dexterity - 20)), Attributes); + _ -> Attributes + end, + case DamageModifier of + heavy -> + attributes:set_speed(max(0, (Speed - 20)), WithRangeModifier); + _ -> WithRangeModifier + end. + |