-
Notifications
You must be signed in to change notification settings - Fork 19
/
mines.lua
367 lines (314 loc) · 9.47 KB
/
mines.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
local assdraw = require 'mp.assdraw'
local W = 0
local H = 0
-- 2D array of size W*H that's addressed via gField[x][y]. Each entry is a
-- table, with the following fields:
-- is_mine: true or false
-- area_mines: mine count in the 3x3 surrounding area
-- is_covered: true or false, for visibility
-- flag: flag put by user, one of FLAG_*
local gField = nil
local FLAG_NONE = "" -- not flagged
local FLAG_MINE = "⚑" -- "⚐" -- flagged as containing mine
local FLAG_MAYBE_MINE = "!" -- flagged as maybe containing mine
local FLAG_MAYBE_SAFE = "?" -- flagged as maybe empty
local STATUS_PLAYING = "playing"
local STATUS_WON = "won"
local STATUS_LOST = "lost"
local gStatus = nil
local gMines = 0
local gX = 0
local gY = 0
local gNeedRefresh = false
local gHidden = true
local gTransparent = false
local PRESETS = {
-- taken from kmines
{ name = "easy", w = 9, h = 9, mines = 10 },
{ name = "medium", w = 16, h = 16, mines = 40 },
{ name = "hard", w = 30, h = 16, mines = 99 },
}
local gCurrentPreset = 2
function init_field()
local preset = PRESETS[gCurrentPreset]
gStatus = STATUS_PLAYING
gField = {}
W = preset.w
H = preset.h
gMines = math.min(preset.mines, W * H - 1)
gX = 1
gY = 1
for x = 1, W do
gField[x] = {}
for y = 1, H do
gField[x][y] = {
is_mine = false,
area_mines = 0,
is_covered = true,
flag = FLAG_NONE,
}
end
end
-- place mines using the dumbfuck algorithm
local place_mines = gMines
while place_mines > 0 do
local x = math.random(1, W)
local y = math.random(1, H)
if not gField[x][y].is_mine then
gField[x][y].is_mine = true
place_mines = place_mines - 1
end
end
-- pick a random start position (also using dumbfuck algorithm)
for i = 1, 1000000 do
local x = math.random(1, W)
local y = math.random(1, H)
if not gField[x][y].is_mine then
gX = x
gY = y
break
end
end
-- compute proximities after mines have been placed
for y = 1, H do
for x = 1, W do
local tile = gField[x][y]
for a_x = -1, 1 do
for a_y = -1, 1 do
local t_x = x + a_x
local t_y = y + a_y
if t_x >= 1 and t_x <= W and t_y >= 1 and t_y <= H and
gField[t_x][t_y].is_mine
then
tile.area_mines = tile.area_mines + 1
end
end
end
end
end
uncover()
check_status()
gNeedRefresh = true
end
function uncover_at(x, y)
if x < 1 or x > W or y < 1 or y > H then
return
end
local tile = gField[x][y]
if not tile.is_covered then
return
end
tile.is_covered = false
if tile.is_mine then
return -- lost anyway
end
-- uncover mines as far as it goes
-- apparently, the standard thing to do is recursively uncovering all
-- tiles which have 0 neightbours - tiles with 1 or more neighbours are
-- uncovered, but not recursively
if tile.area_mines == 0 then
for a_x = -1, 1 do
for a_y = -1, 1 do
uncover_at(x + a_x, y + a_y)
end
end
end
gNeedRefresh = true
end
function check_status()
if gStatus ~= STATUS_PLAYING then
return
end
local won = true
local lost = false
for y = 1, H do
for x = 1, W do
local tile = gField[x][y]
won = won and (tile.is_mine == tile.is_covered)
lost = lost or (tile.is_mine and not tile.is_covered)
end
end
if lost then
gStatus = STATUS_LOST
gNeedRefresh = true
elseif won then
gStatus = STATUS_WON
gNeedRefresh = true
end
end
function uncover()
if gStatus ~= STATUS_PLAYING and not gField[gX][gY].is_covered then
init_field()
render()
return
end
uncover_at(gX, gY)
check_status()
render()
end
function flag()
local tile = gField[gX][gY]
local cycle = {FLAG_NONE, FLAG_MINE, FLAG_MAYBE_MINE, FLAG_MAYBE_SAFE}
for i = 1, #cycle do
if tile.flag == cycle[i] then
tile.flag = cycle[(i - 1 + 1) % #cycle + 1]
break
end
end
if not tile.is_covered then
tile.flag = FLAG_NONE
end
force_render()
end
function move(x, y)
gX = math.min(math.max(gX + x, 1), W)
gY = math.min(math.max(gY + y, 1), H)
force_render()
end
function force_render()
gNeedRefresh = true
render()
end
function render()
if not gNeedRefresh then
return
end
if gHidden then
mp.set_osd_ass(1280, 720, "")
return
end
local canvas_w = 1280
local canvas_h = 720
local dw, dh, da = mp.get_osd_size()
if dw ~= nil and dw > 0 and dh > 0 then
canvas_w = dw / dh * canvas_h
end
local tile_wh = 32
local o_x = canvas_w / 2 - tile_wh * W / 2
local o_y = canvas_h / 2 - tile_wh * (H + 2) / 2 + tile_wh
local ass = assdraw.ass_new()
local transp = nil
if gTransparent then
transp = "{\\1a&HA0&\\3a&HA0&}"
end
-- some shitty background
ass:new_event()
ass:append("{\\1c&Ha3a3a3&\\1a&H30&}")
if transp then
ass:append(transp)
end
ass:pos(o_x - tile_wh, o_y - tile_wh)
ass:draw_start()
ass:rect_cw(0, 0, (W + 2) * tile_wh, (H + 2) * tile_wh)
-- grid
local function grid_line(x0, y0, x1, y1)
ass:new_event()
ass:append("{\\bord0.5}")
if transp then
ass:append(transp)
end
ass:pos(x0, y0)
ass:draw_start()
ass:coord(0, 0)
ass:line_to(x1 - x0, y1 - y0)
end
for x = 0, W do
local p_x = x * tile_wh + o_x
grid_line(p_x, o_y, p_x, o_y + tile_wh * H)
end
for y = 0, H do
local p_y = y * tile_wh + o_y
grid_line(o_x, p_y, o_x + tile_wh * W, p_y)
end
local function draw_sym(x, y, sym, c)
ass:new_event()
ass:pos(x, y)
ass:append("{\\an5\\fs25\\bord0\\1c&H" .. c .. "&\\b1}" .. sym)
end
for x = 1, W do
for y = 1, H do
local tile = gField[x][y]
local p_x = (x - 1) * tile_wh + tile_wh / 2 + o_x
local p_y = (y - 1) * tile_wh + tile_wh / 2 + o_y
local wh = tile_wh - 4
local sym = nil
if tile.is_covered then
ass:new_event()
if transp then
ass:append(transp)
end
ass:pos(p_x, p_y)
ass:draw_start()
ass:round_rect_cw(-wh / 2, -wh / 2, wh / 2, wh / 2, 5)
ass:draw_stop()
elseif tile.is_mine then
draw_sym(p_x, p_y, "💣", "0000FF")
elseif tile.area_mines > 0 then
draw_sym(p_x, p_y, tile.area_mines, "000000")
end
if tile.flag ~= FLAG_NONE then
draw_sym(p_x, p_y, tile.flag, "FF0000")
end
if x == gX and y == gY then
local wh = tile_wh - 12
ass:new_event()
ass:append("{\\1a&HFF&}")
ass:pos(p_x, p_y)
ass:draw_start()
ass:rect_cw(-wh / 2, -wh / 2, wh / 2, wh / 2)
ass:draw_stop()
end
end
end
local banner = nil
if gStatus == STATUS_WON then
banner = "You may have won, but actually you just wasted time."
elseif gStatus == STATUS_LOST then
banner = "You lost (and wasted time)."
end
if banner then
ass:new_event()
ass:pos(o_x + tile_wh * W / 2, o_y - tile_wh - 10)
ass:append("{\\fs40\\b1\\an2}" .. banner)
end
mp.set_osd_ass(canvas_w, canvas_h, ass.text)
end
mp.observe_property("osd-width", "native", force_render)
mp.observe_property("osd-height", "native", force_render)
init_field()
force_render()
function toggle_transp()
gTransparent = not gTransparent
force_render()
end
function cycle_preset()
gCurrentPreset = (gCurrentPreset + 1 - 1) % #PRESETS + 1
init_field()
render()
end
function toggle_show()
if gHidden then
gHidden = false
local REP = {repeatable = true}
mp.add_forced_key_binding("left", "mines-left", function() move(-1, 0) end, REP)
mp.add_forced_key_binding("right", "mines-right", function() move(1, 0) end, REP)
mp.add_forced_key_binding("up", "mines-up", function() move(0, -1) end, REP)
mp.add_forced_key_binding("down", "mines-down", function() move(0, 1) end, REP)
mp.add_forced_key_binding("space", "mines-uncover", uncover)
mp.add_forced_key_binding("b", "mines-flag", flag)
mp.add_forced_key_binding("t", "mines-transp", toggle_transp)
mp.add_forced_key_binding("w", "mines-preset", cycle_preset)
else
gHidden = true
mp.remove_key_binding("mines-left")
mp.remove_key_binding("mines-right")
mp.remove_key_binding("mines-up")
mp.remove_key_binding("mines-down")
mp.remove_key_binding("mines-uncover")
mp.remove_key_binding("mines-flag")
mp.remove_key_binding("mines-transp")
mp.remove_key_binding("mines-preset")
end
force_render()
end
mp.add_forced_key_binding("ctrl+x", "mines-show", toggle_show)