summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/battle/struct/btl_condition.erl')
-rw-r--r--src/battle/struct/btl_condition.erl188
1 files changed, 84 insertions, 104 deletions
diff --git a/src/battle/struct/btl_condition.erl b/src/battle/struct/btl_condition.erl
index fee7279..13d6387 100644
--- a/src/battle/struct/btl_condition.erl
+++ b/src/battle/struct/btl_condition.erl
@@ -9,7 +9,7 @@
(
none
| remove
- | {update, type(), ataxic:basic()}
+ | {update, ataxic:basic()}
).
-record
@@ -25,8 +25,9 @@
).
-opaque type() :: #btl_cond{}.
+-opaque collection() :: orddict:orddict(non_neg_integer(), type()).
--export_type([type/0, update_action/0]).
+-export_type([type/0, collection/0, update_action/0]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -64,11 +65,7 @@
-export
(
[
- triggers_on/2,
- apply/3,
- recursive_apply/3,
- apply_updates/2,
- ataxia_apply_updates/2
+ ataxia_apply_trigger/3
]
).
@@ -89,6 +86,42 @@
-> {list({binary(), any()})}.
encode_parameters (_Category, _Parameters) -> {[]}. % TODO.
+-spec update_actions_to_ataxic_update
+ (
+ list({non_neg_integer(), update_action()})
+ )
+ -> ataxic:basic().
+update_actions_to_ataxic_update (Updates) ->
+ AtaxicSequence =
+ lists:foldl
+ (
+ fun ({IX, Update}, AtaxicUpdates) ->
+ case Update of
+ none -> AtaxicUpdates;
+ remove ->
+ [
+ ataxic:apply_function
+ (
+ orddict,
+ erase,
+ [ataxic:constant(IX), ataxic:current_value()]
+ )
+ |AtaxicUpdates
+ ];
+
+ {update, Ataxic} ->
+ [
+ ataxic_sugar:update_orddict_element(IX, Ataxic)
+ |AtaxicUpdates
+ ]
+ end
+ end,
+ [],
+ Updates
+ ),
+
+ ataxic:sequence(AtaxicSequence).
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% EXPORTED FUNCTIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -235,123 +268,70 @@ get_duration_field () -> #btl_cond.duration.
-spec get_parameters_field () -> non_neg_integer().
get_parameters_field () -> #btl_cond.parameters.
--spec apply
+-spec ataxia_apply_trigger
(
- type(),
- shr_condition:context(),
- btl_character_turn_update:type()
+ shr_condition:context(_ReadOnlyDataType, VolatileDataType),
+ btl_character_turn_update:type(),
+ collection()
)
->
{
- shr_condition:context(),
- btl_character_turn_update:type(),
- update_action()
+ VolatileDataType,
+ ataxic:basic(),
+ btl_character_turn_update:type()
}.
-apply (Condition, S0Context, S0Update) ->
- Module = shr_condition_selector:get_module(get_category(Condition)),
-
- {S1Context, S1Update, UpdateAction} =
- erlang:apply(Module, apply, [S0Context, Condition, S0Update]),
-
- {S1Context, S1Update, UpdateAction}.
+ataxia_apply_trigger (Context, S0Update, Conditions) ->
+ {Trigger, ReadOnlyData, S0VolatileData} = Context,
+ RelevantConditions =
+ orddict:filter
+ (
+ fun (_IX, Condition) -> triggers_on(Trigger, Condition) end,
+ Conditions
+ ),
--spec recursive_apply
- (
- orddict:orddict(IndexType, type()),
- shr_condition:context(),
- btl_character_turn_update:type()
- )
- ->
- {
- shr_condition:context(),
- btl_character_turn_update:type(),
- list({IndexType, update_action()})
- }.
-recursive_apply (Conditions, S0Context, S0Update) ->
- {[LastContext, LastUpdate], AllUpdateActions} =
+ {LastVolatileData, LastUpdate, AllUpdateActions} =
orddict:fold
(
- fun (IX, Condition, {Parameters, UpdateActions}) ->
- {NextContext, NextUpdate, UpdateAction} =
- erlang:apply(btl_condition, apply, [Condition|Parameters]),
+ fun
+ (
+ IX,
+ Condition,
+ {
+ CurrentVolatileData,
+ CurrentUpdate,
+ UpdateActions
+ }
+ ) ->
+ Module = shr_condition_selector:get_module(get_category(Condition)),
+ {NextVolatileData, NextUpdate, UpdateAction} =
+ erlang:apply
+ (
+ Module,
+ apply,
+ [
+ Condition,
+ CurrentUpdate,
+ {Trigger, ReadOnlyData, CurrentVolatileData}
+ ]
+ ),
{
- [NextContext, NextUpdate],
+ NextVolatileData,
+ NextUpdate,
case UpdateAction of
none -> UpdateActions;
_ -> [{IX, UpdateAction}|UpdateActions]
end
}
end,
- [S0Context, S0Update, []],
- Conditions
+ {S0VolatileData, S0Update, []},
+ RelevantConditions
),
- {LastContext, LastUpdate, AllUpdateActions}.
-
--spec apply_updates
- (
- list({IndexType, update_action()}),
- orddict:orddict(IndexType, type())
- )
- -> orddict:orddict(IndexType, type()).
-apply_updates (Updates, Conditions) ->
- lists:foldl
- (
- fun ({IX, Update}, CurrentConditions) ->
- case Update of
- none -> CurrentConditions;
- remove -> orddict:erase(IX, CurrentConditions);
- {update, Val, _Ataxic} -> orddict:store(IX, Val, CurrentConditions)
- end
- end,
- Conditions,
- Updates
- ).
-
--spec ataxia_apply_updates
- (
- list({IndexType, update_action()}),
- orddict:orddict(IndexType, type())
- )
- -> {orddict:orddict(IndexType, type()), ataxic:basic()}.
-ataxia_apply_updates (Updates, Conditions) ->
- {FinalConditions, AtaxicSequence} =
- lists:foldl
- (
- fun ({IX, Update}, {CurrentConditions, AtaxicUpdates}) ->
- case Update of
- none -> {CurrentConditions, AtaxicUpdates};
- remove ->
- {
- orddict:erase(IX, CurrentConditions),
- [
- ataxic:apply_function
- (
- orddict,
- erase,
- [ataxic:constant(IX), ataxic:current_value()]
- )
- |AtaxicUpdates
- ]
- };
-
- {update, Val, Ataxic} ->
- {
- orddict:store(IX, Val, CurrentConditions),
- [
- ataxic_sugar:update_orddict_element(IX, Ataxic)
- |AtaxicUpdates
- ]
- }
- end
- end,
- Conditions,
- Updates
- ),
+ ConditionsAtaxiaUpdate = update_actions_to_ataxic_update(AllUpdateActions),
- {FinalConditions, ataxic:sequence(AtaxicSequence)}.
+ {LastVolatileData, ConditionsAtaxiaUpdate, LastUpdate}.
-spec encode (type()) -> {list({binary(), any()})}.
encode (Condition) -> {[]}. % TODO