Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate trades in potion_master_cleric.sc #362

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions programs/survival/potion_master_cleric.sc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ global_max_potion_trades = 3;
// this code does not handle cycling villager trades.
global_potion_master_level = 3;

// Nbt data for each avalible trade.
// Nbt data for each available trade.
global_potion_trades = [
// Dolphins Grace
'{
Expand Down Expand Up @@ -130,15 +130,60 @@ global_potion_trades = [

];

// potentially override chosen max trades to prevent duplicates
global_max_potion_trades = min(global_max_potion_trades,length(global_potion_trades) * length(global_potion_types));

_add_trades(cleric, p, nbt) -> (
trades = {};
effects = copy(global_potion_trades);
// add 0-max number of potion trades
loop(rand(1 + global_max_potion_trades),

i = _;
mapped_effects = map(effects, _ ~ '(?<=Potion of )\\w*');
Copy link
Collaborator Author

@rv3r rv3r Feb 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you take the time to map the effects instead of using them directly?

Because debugging with a list that has entries that are 12+ lines of text is painful.

trade = nbt(str(
// select a random trade from the list above
global_potion_trades:rand(length(global_potion_trades)),
// Randomize the potion type
global_potion_types:rand(length(global_potion_types)),
count = {...mapped_effects};
// count how many of each potion have been chosen
for(
filter(
pairs(trades),
mapped_effects ~ (_:1:'potion') != null
),
count:(_:1:'potion') += 1;
);
// delete options that will produce duplicates
for(
filter(
pairs(count),
_:1 >= length(global_potion_types)
),
if(mapped_effects ~ (_:0) != null,
delete(effects,mapped_effects ~ (_:0));
);
);
trade_choice = effects:rand(length(effects));
// note potion effect choice for later
trades:_ = {
['potion', trade_choice ~ '(?<=Potion of )\\w*'],
['type', null]
};
// here we actually give the potion effect to str()
trade_choice,
types = copy(global_potion_types);
// if same potion effect, delete potion type options to prevent duplicates
for(filter(pairs(trades),
_:0 < i &&
_:1:'potion' == trades:i:'potion' &&
types ~ (_:1:'type') != null
),
delete(types,types ~ (_:1:'type'));
);
// Randomize the potion type with the leftover options
type_choice = types:rand(length(types));
// note potion type choice for later
trades:_:'type' = type_choice;
// here we actually give the potion type to str()
type_choice,
// randomize the cost of each trade
rand((global_potion_emerald_cost:1) - global_potion_emerald_cost:0) + global_potion_emerald_cost:0
));
Expand All @@ -165,4 +210,3 @@ __on_player_interacts_with_entity(player, entity, hand) -> (
);
)
);