forked from AnthonyAMC/Old-Denizen-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeightedRandomChoice.yml
52 lines (50 loc) · 2.65 KB
/
WeightedRandomChoice.yml
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
################################################################################
# #
# W e i g h t e d R a n d o m C h o i c e #
# #
# #
# This enables you to have a percent chance that something will happen. Good #
# for things such as NPC interactions and/or for daily loot. Easily set some #
# things as rare and others as common. Check out the example usage for ideas. #
# #
# The beauty of this method is that you can use this on a list of any size, #
# for nearly any purpose. #
# #
# Supply a list of weight/return values and have one of the values returned #
# to you randomly based on weight. #
# #
# - Usage: <proc[WeightedChoice].context[weight/return|...]> #
# #
# Weight: A numeric weight value #
# Return: The value the procedure should return if this weight is chosen #
# #
# #
################################################################################
WeightedChoice:
type: procedure
debug: false
script:
- define list 'li@'
- foreach <queue.definitions.exclude[list]> {
- define list '<def[list].include[<def[%value%]>]>'
}
- define weight '0'
- foreach <def[list].parse[split[/].get[1]]> {
- define weight '<def[weight].add[%value%]>'
}
- define number '<util.random.int[1].to[%weight%]>'
- define highValue '0'
- foreach %list% {
- define oldValue '%highValue%'
- define lowValue '<def[oldValue].add[1]>'
- define highValue '<def[oldValue].add[<def[value].split[/].get[1]>]>'
- if %number% >= %lowValue% && %number% <= %highValue% {
- determine '<def[value].split[/].get[2]>'
}
}
#WeightedChoiceExample:
# type: task
# debug: false
# script:
# - define possibilities '10/apples|1/gold|20/stone'
# - narrate "I want <proc[WeightedChoice].context[%possibilities%]>"