-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmwc_sample.lua
215 lines (168 loc) · 5.14 KB
/
mwc_sample.lua
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
--- Sample code for MWC plugin.
--
-- Permission is hereby granted, free of charge, to any person obtaining
-- a copy of this software and associated documentation files (the
-- "Software"), to deal in the Software without restriction, including
-- without limitation the rights to use, copy, modify, merge, publish,
-- distribute, sublicense, and/or sell copies of the Software, and to
-- permit persons to whom the Software is furnished to do so, subject to
-- the following conditions:
--
-- The above copyright notice and this permission notice shall be
-- included in all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--
-- [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
--
-- Optionally, enable this (and include it in the build settings)
-- require("plugin.bit")
local mwc = require("plugin.mwc")
do
local rng1 = mwc.MakeGenerator()
local rng2 = mwc.MakeGenerator()
for i = 1, 30 do
if i == 1 then
print("Random integers")
elseif i == 11 then
print("")
print("Random integers, 0-49")
elseif i == 21 then
print("")
print("Random floats")
end
if i <= 10 then
print(rng1(), rng2())
elseif i <= 20 then
print(rng1() % 50, rng2() % 50)
else
print(rng1("float"), rng2("float"))
end
end
print("")
print("Do two sequences match? (integer, float)")
for i = 1, 10 do
print(rng1() == rng2(), rng1("float") == rng2("float"))
end
end
do
print("")
print("Random integers, new sequence")
local rng = mwc.MakeGenerator()
for i = 1, 10 do
print(rng())
end
end
do
print("")
print("New integer sequences, custom w seed")
local rng1 = mwc.MakeGenerator{ w = 50 }
local rng2 = mwc.MakeGenerator{ w = 50 }
for i = 1, 10 do
print(rng1(), rng2())
end
end
do
print("")
print("New integer sequences, custom z seed")
local rng1 = mwc.MakeGenerator{ z = 1250 }
local rng2 = mwc.MakeGenerator{ z = 1250 }
for i = 1, 10 do
print(rng1(), rng2())
end
end
do
print("")
print("New integer sequences, custom w and z seeds")
local rng1 = mwc.MakeGenerator{ w = 150, z = 3000 }
local rng2 = mwc.MakeGenerator{ w = 150, z = 3000 }
for i = 1, 10 do
print(rng1(), rng2())
end
end
do
print("")
print("Some sequence of 10 floats")
local rng1 = mwc.MakeGenerator{ w = 1507, z = 3932 }
local z, w
for i = 1, 10 do
if i == 6 then
print("Making a note of our position")
z, w = rng1("get_zw")
end
print(rng1("float"))
end
print("")
print("Instantiating a new generator, resuming sequence from remembered position")
local rng2 = mwc.MakeGenerator{ w = w, z = z }
for i = 6, 10 do
print(rng2("float"))
end
end
do
local rng = mwc.MakeGenerator_Lib{ w = 23931 }
print("")
print("Lua-style generator, no arguments")
for i = 1, 10 do
print(rng())
end
print("")
print("Lua-style generator, single argument (25)")
for i = 1, 10 do
print(rng(25))
end
print("")
print("Lua-style generator, two arguments (15, 65)")
for i = 1, 10 do
print(rng(15, 65))
end
end
-- Pure examples (won't be run) --
-- #1
if false then
-- For each object, in a game for instance, we can assign a generator. To avoid all
-- objects coming up with the same sequence, each can take a unique seed. This could
-- itself be random, say by passing the current time, or some fixed value in order to
-- get a reproducible sequence.
object.m_generator = mwc.MakeGenerator{ w = math.floor(object.starting_position.x / 32) } -- tile column as seed
end
-- #2
if false then
-- We might want multiple generators, e.g. per property.
local col = math.floor(object.starting_position.x / 32)
object.m_pos_generator = mwc.MakeGenerator{ w = col, z = 32174 } -- some distinct integer
object.m_angle_generator = mwc.MakeGenerator{ w = col, z = 3232 } -- another
end
-- #3
if false then
-- Furthermore, we can save these seeds and use these to reseed our sequence, say when we
-- quit and later resume the game. In the one-generator case, for instance:
local w, z = GetSeedsFromSaveFile()
if w == nil then
w = math.floor(object.starting_position.x / 32)
end
object.m_generator = mwc.MakeGenerator{ w = w, z = z }
end
-- #4
if false then
-- Elsewhere in the file, when saving, we would have something along the lines of:
local w, z = object.m_generator("get_zw")
AddSeedsToSaveFile(w, z)
end
-- #5
if false then
-- Last but not least, we can make a Lua-style generator and use it like math.random().
-- For instance, to make 20 circles with random positions, radii, and colors:
local my_random = mwc.MakeGenerator_Lib{ w = 42 }
for i = 1, 20 do
local x, y = my_random(display.contentWidth), my_random(display.contentHeight)
local circle = display.newCircle(x, y, my_random(15, 35))
circle:setFillColor(my_random(), my_random(), my_random())
end
end