[MCBE] 红包/抽奖系统 (微信红包算法)
///提醒: 此指令系统不是随随便便粘贴复制就能做成的,您需要拥有一定的指令与数学基础。
///使用之前,先确保你有一个名为Redpacket的记分板或着其他名字的记分板(需要把指令中大写的"Redpacket"改成记分板名)。
1链: times≥2时random算法
/scoreboard players test times Redpacket 2 *
RU
/scoreboard players test random Redpacket 0 0
CC
/scoreboard players random random Redpacket 1 2147483647
CC
/scoreboard players operation operation Redpacket = int Redpacket
CC
/scoreboard players operation operation Redpacket /= times Redpacket
CC
/scoreboard players operation operation Redpacket *= 2x Redpacket
CC
/scoreboard players operation random Redpacket %= operation Redpacket
CC
/scoreboard players test random Redpacket 1 *
CC
/scoreboard players operation int Redpacket -= random Redpacket
CC
/scoreboard players remove times Redpacket 1
CC
2链: times=1取int余数
/scoreboard players test times Redpacket 1 1
RU
/scoreboard players test random Redpacket 0 0
CC
/scoreboard players operation random Redpacket >< int Redpacket
CC
/scoreboard players set times Redpacket 0
CC
///int为输入值,times为次数,random值为输出值
///算法讲解
--/如果random=0,times≥2,在2^31-1整数极限中取random值,计算(int/times)*2值作为operation范围,random/operation取余数,若余数大于0则输出。
--/如果random=0,times=1,将int与random对调并将times重置至0。
3链: 红包接受机制
///可自定义。仅作参考。
/scoreboard players add @a Redpacket 0
RU
/scoreboard players test times Redpacket 1 *
CU
/titleraw @a[scores={Redpacket=0}] actionbar {"rawtext":[{"text":"§c§l新的红包待领取!\n§e抬头以抢红包!"}]}
CC
/scoreboard players test times Redpacket 1 *
CU
/execute @a[scores={Redpacket=0},c=1,rx=-89] ~~~ tellraw @a {"rawtext":[{"text":"§l§e>>>§d "},{"selector":"@a[scores={Redpacket=0},c=1,rx=-89]"},{"text":" §e抽到了§6 "},{"score":{"name":"random","objective":"Redpacket"}},{"text":" §e个钻石!"}]}
CC
/scoreboard players set @a[scores={Redpacket=0},c=1,rx=-89] Redpacket 1000
CC
/scoreboard players operation @a[scores={Redpacket=1000},c=1,rx=-89] Redpacket += random Redpacket
CC
/scoreboard players set random Redpacket 0
CC
/execute @a[scores={Redpacket=1000}] ~~~ scoreboard players test times Redpacket * 0
CU
/scoreboard players reset @a Redpacket
CC
/give @a[scores={Redpacket=1001..}] diamond 1
CU
/scoreboard players remove @a[scores={Redpacket=1001..}] Redpacket 1
CC
4链: 启动红包分发
///该指令链设定int=1000 times=10。可自定义分发数量,次数,如何分发。仅供参考。
///注: int值要大于(times+1)值。否则可能会random计算出一个超级大数(random/0取余,没有数学意义,不能正常触发)
/scoreboard players set int Redpacket 1000
I
/scoreboard players set times Redpacket 10
CU
/scoreboard players set random Redpacket 0
CU
/scoreboard players set 2x Redpacket 2
CU
使用原版标准缩写:
第一位:
I(Impulse)--脉冲
C(Chain)--链
R(Repeat)--重复
第二位:
C(Conditional)--有条件的
U(Unconditional)--无条件的

