[MCBE] 紅包/抽獎系統(tǒng) (微信紅包算法)

///提醒: 此指令系統(tǒng)不是隨隨便便粘貼復制就能做成的,您需要擁有一定的指令與數(shù)學基礎。
///使用之前,先確保你有一個名為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余數(shù)
/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為次數(shù),random值為輸出值
///算法講解
--/如果random=0,times≥2,在2^31-1整數(shù)極限中取random值,計算(int/times)*2值作為operation范圍,random/operation取余數(shù),若余數(shù)大于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鏈: 啟動紅包分發(fā)
///該指令鏈設定int=1000 times=10??勺远x分發(fā)數(shù)量,次數(shù),如何分發(fā)。僅供參考。
///注: int值要大于(times+1)值。否則可能會random計算出一個超級大數(shù)(random/0取余,沒有數(shù)學意義,不能正常觸發(fā))
/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)--無條件的