Date: 2014/03/18 18:36:54 UTC-07:00
Type: Denizen Script
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
################################################################################
# #
# 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. #
# #
################################################################################
'WeightedRandomChoice':
#
# Choose a random number from 1 to 100 and return which category the number is
# within.
#
type: task
script:
- define list <npc.flag[WeightedRandomChoice].as_list>
- define listSize <def[list].size>
- define number <util.random.int[1].to[100]>
- define counter 0
- foreach <def[list].as_list> {
- if <def[counter].is[OR_LESS].than[%listSize%]> {
- define counter <math:%counter%+1>
- define entry_<def[counter].as_int> <def[list].get[<def[counter].as_int>]>
- define percentChance <def[entry_<def[counter].as_int>].split[/].get[1]>
- define returnValue <def[entry_<def[counter].as_int>].split[/].get[2]>
- define oldValue '<def[oldValue] || 0>'
- define lowValue '<math:%oldValue%+1>'
- define highValue '<math:%oldValue%+%percentChance%>'
- define oldValue '<def[highValue]>'
- if <proc[isbetween].context[%number%|%lowValue%|%highValue%]> {
- ^flag npc WeightedRandomChoice:<def[returnValue].as_int>
}
}
}
- define result <npc.flag[WeightedRandomChoice]>
#'Example Weighted Random Choice Usage':
#
# Run a random thing based on weight assignment
# - flag npc WeightedRandomChoice:percentChance/returnValue|...
#
# Make sure the sum of all the options is equal to 100.
#
# type: task
# script:
# - flag npc WeightedRandomChoice:5/1|20/2|25/3|35/4|15/5
# - inject s@WeightedRandomChoice instantly
#
# - if <def[outcome].is[==].to[1]> {
# - chat "5% Chance"
# - run s@SomeRareTask
# }
# else if <def[outcome].is[==].to[2]> {
# - chat "20% Chance"
# - run s@SomeLessCommonTask
# }
# else if <def[outcome].is[==].to[3]> {
# - chat "25% Chance"
# - run s@SomeCommonTask
# }
# else if <def[outcome].is[==].to[4]> {
# - chat "35% Chance"
# - run s@SomeMoreCommonTask
# }
# else if <def[outcome].is[==].to[5]> {
# - chat "15% Chance"
# - run s@SomeKindaRareTask
# }
# else {
# - chat "something went horribly wrong"
# }
# - flag npc WeightedRandomChoice:!