Draw Lots  1.0.0
Lottery that can designate winning weights.
DrawLots.h
1 
9 #pragma once
10 #include <Kismet/BlueprintFunctionLibrary.h>
11 #include "DrawLots.generated.h"
12 
13 /*
14 Probability of Winning and Lottery Items
15 
16 当選確率と抽選アイテム
17 */
18 USTRUCT(BlueprintType)
19 struct DRAWLOTS_API FLotteryItem
20 {
21  GENERATED_BODY()
22 
23  /*
24  Lottery probability
25 
26  抽選確率
27  */
28  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "DrawLots", meta = (ClampMin = "1"))
29  uint8 Odds = 1;
30 
31  /*
32  Winning item
33 
34  当選アイテム
35  */
36  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "DrawLots")
37  TObjectPtr<UObject> Item;
38 
39  FLotteryItem()
40  : Odds(1)
41  , Item(nullptr)
42  {}
43 
44  FLotteryItem(const uint8 odds, UObject* item) noexcept
45  : Odds(odds)
46  , Item(item)
47  {}
48 };
49 
50 /*
51 Lottery data for which the probability of winning can be specified
52 Add the FLotteryItem you want to draw to LotteryItems and call the DrawLotsObject function to draw the lots.
53 
54 当選確率が指定可能な抽選データ
55 LotteryItemsに抽選したいFLotteryItemを追加してDrawLotsObject関数を呼ぶと抽選します
56 */
57 USTRUCT(BlueprintType)
58 struct DRAWLOTS_API FDrawLotsObject
59 {
60  GENERATED_BODY()
61 
62  /*
63  Probability of Winning and Lottery Items
64 
65  当選確率と抽選アイテム
66  */
67  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "DrawLots")
68  TArray<FLotteryItem> LotteryItems;
69 
70  /*
71  Drawing lots
72 
73  抽選します
74  */
75  UObject* Draw(const bool consecutive) const;
76 
77 private:
78  mutable int32 mLastLotteryNumber = -1;
79 };
80 
81 /*
82 Drawing data for which the probability of winning can be specified
83 Define probabilities in Odds and call DrawLotsIndex function to draw lots.
84 
85 当選確率が指定可能な抽選データ
86 Oddsに確率を定義してDrawLotsIndex関数を呼ぶと抽選します
87 */
88 USTRUCT(BlueprintType)
89 struct DRAWLOTS_API FDrawLotsIndex
90 {
91  GENERATED_BODY()
92 
93  /*
94  Probability (enter a value greater than or equal to 1)
95 
96  確率(1以上の値を入力してください)
97  */
98  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "DrawLots")
99  TArray<uint8> Odds;
100 
101  /*
102  Drawing lots
103 
104  抽選します
105  */
106  int32 Draw(const bool consecutive) const;
107 
108 private:
109  mutable int32 mLastLotteryNumber = -1;
110 };
111 
112 /*
113 Probability-specific draw functions
114 
115 確率指定可能な抽選関数
116 */
117 UCLASS(Blueprintable)
118 class DRAWLOTS_API UDrawLotsBlueprint : public UBlueprintFunctionLibrary
119 {
120  GENERATED_BODY()
121 
122 public:
123  /*
124  Drawing with specifiable probability of winning.
125  Returns the winning item.
126  Returns nullptr if the item cannot be drawn, e.g. if the probability is set to all 0's. If "consecutive" is disabled, the same item will not be drawn consecutively.
127  If you disable the "directive", the same item will not be drawn consecutively.
128 
129  当選確率が指定可能な抽選
130  当選したアイテムを返します。
131  確率をすべて0にした場合など抽選できない場合はnullptrを返します。
132  consecutiveを無効にすると同じアイテムが連続で当選しません
133  */
134  UFUNCTION(BlueprintCallable, Category = "DrawLots")
135  static UObject* DrawLotsObject(const FDrawLotsObject& drawLotsObject, const bool consecutive = true)
136  {
137  return drawLotsObject.Draw(consecutive);
138  }
139 
140  /*
141  Draws a lottery with specifiable probability of winning.
142  Returns the winning sequence number.
143  Returns -1 if the lottery cannot be performed, e.g. if all probabilities are set to 0.
144  If you disable the "sequence" option, the same sequence number will not be drawn consecutively.
145 
146  当選確率が指定可能な抽選をします
147  当選した配列番号を返します。
148  確率をすべて0にした場合など抽選できない場合は-1を返します。
149  consecutiveを無効にすると同じ番号が連続で当選しません
150  */
151  UFUNCTION(BlueprintCallable, Category = "DrawLots")
152  static int32 DrawLotsIndex(const FDrawLotsIndex& drawLotsIndex, const bool consecutive = true)
153  {
154  return drawLotsIndex.Draw(consecutive);
155  }
156 };
Definition: DrawLots.h:119
Definition: DrawLots.h:90
Definition: DrawLots.h:59
UObject * Draw(const bool consecutive) const
Definition: DrawLots.cpp:12
Definition: DrawLots.h:20