View difference between Paste ID: LpzF7mYn and akPY1CPd
SHOW: | | - or go back to the newest paste.
1
_ms_h  = 9  // board height
2
_ms_w  = 9  // board width
3
_ms_m  = 10 // board mines
4
_ms_th = 9  // temp H
5
_ms_tw = 9  // temp W
6
_ms_tm = 10 // temp M
7-
_ms_tu = 1  // temp U
7+
8
_ms_hc = 0  // hit mines counter
9
_ms_bt = 0  // board tiles (total)
10
_ms_t1 = 0  // timer start (ms)
11
_ms_t2 = 0  // timer end (ms)
12
_ms_ua = 1  // undo toggle
13
_ms_u  = 0  // undo counter
14
_ms_pc = 0  // completion percentage
15
_ms_gm = 0  // game mode: 0 waiting | 1 active | 2 failure | 3 victory
16
17
_ms_mf = "" // board containing mine placement
18
_ms_nf = "" // board containing tile numbers
19
_ms_cf = "" // board of completion (displayed)
20
_ms_mp = "" // supplementary list with mine positions
21
_ms_hp = "" // supplementary list with hit mine positions
22
_ms_fp = "" // supplementary list with flag positions
23
_ms_tr = "" // supplementary list with tile reveal positions
24
_ms_tq = "" // supplementary list with blank tile queue
25
26
27
// ----------------------------------------- //
28
// ---------------- Tile ID ---------------- //
29
// ----------------------------------------- //
30
// 0x0     = blank tile
31-
// 0   = blank tile
31+
// 0x1-0x8 = number tiles
32-
// 1-8 = number tiles
32+
// 0x9     = hit mine tile
33-
// 9   = mine tile (failure)
33+
// 0xA     = mine tile (win)
34-
// 10  = mine tile (victory)
34+
// 0xB     = mine tile (lose)
35-
// 11  = hit mine tile
35+
// 0xC     = flagged tile
36-
// 12  = flagged tile
36+
// 0xD     = flagged mine tile (win)
37-
// 13  = flagged mine tile (success)
37+
// 0xE     = flagged mine tile (lose)
38-
// 14  = flagged mine tile (failure)
38+
// 0xF     = hidden tile
39-
// 15  = hidden tile
39+
40
41
append = [ $arg1 = (concat (getalias $arg1) $arg2) ]
42
precf  = [ substr $arg1 0 (+ (strstr $arg1 .) (+ $arg2 1)) ]
43
ms     = [ toggleui minesweeper ]
44
45
_ms_clock = [
46
    local n ; n = (mod $arg1 1000)
47
    format "%1:%2%3" (div $arg1 1000) (if (< $n 100) [? (< $n 10) "00" "0"]) $n
48-
    n = (mod $arg1 1000)
48+
49
50
_ms_set_options = [
51
    local n
52
    n = (* $_ms_th $_ms_tw)
53
    // calculate board sizes/mines and apply limits
54
    if $arg1 [ _ms_th = (max (min $arg1 24) 9) ]
55
    if $arg2 [ _ms_tw = (max (min $arg2 30) 9) ]
56
    _ms_tm = (max (min (div $n 3) $_ms_tm) (div $n 10))
57
]
58
59
_ms_set_markers = [
60
    // in case of failure, update flag markers
61
    if (= $_ms_gm 2) [
62
        looplist i (listdel $_ms_fp $_ms_mp) [
63
            _ms_tile_open 0xE $i 0
64-
        looplist i $_ms_fp [
64+
65-
            if (= (at $_ms_nf $i) "9") [
65+
66-
                _ms_tile_open "13" $i 0
66+
    // update still hidden mine tiles
67-
            ] [ _ms_tile_open "14" $i 0 ]
67+
68
        if (= (at $_ms_cf $i) 0xF) [
69
            _ms_tile_open (case $_ms_gm 2 0xA 3 0xB) $i 0
70-
    // update mine markers, discounting flagged ones
70+
71
    ]
72-
        case $_ms_gm 2 [
72+
73-
            if (< (indexof "11 13 14" (at $_ms_cf $i)) 0) [
73+
74-
                _ms_tile_open "9" $i 0
74+
75
    // start the game if still in waiting phase
76-
        ] 3 [
76+
77-
            if (< (indexof "11 12" (at $_ms_cf $i)) 0) [
77+
78-
                _ms_tile_open "10" $i 0
78+
79
    ]
80
    // reveal # tile at given position and add to percentage counter if needed
81
    _ms_cf = (listsplice $_ms_cf (tohex $arg1) $arg2 1)
82
    if $arg3 [ _ms_pc = (+ $_ms_pc 1) ]
83
]
84
85
_ms_tile_find = [
86
    // set variables and calculate repeated math
87
    local n r  p c1 c2 c3 c4 c5 c6 c7 c8
88
    n = 0 ; r = (mod $arg9 $_ms_w) ; p = (- $_ms_w 1)
89
    // calculate positions of adjacent tiles
90-
    // reveal # tile at given position and add to completion counter if needed
90+
91-
    _ms_cf = (listsplice $_ms_cf $arg1 $arg2 1)
91+
92
    c3 = (- $arg9 1)        // LT
93
    c4 = (+ $arg9 1)        // RT
94
    c5 = (- $arg9 $_ms_w 1) // UP-LT
95
    c6 = (- $arg9 $p)       // UP-RT
96
    c7 = (+ $arg9 $p)       // DN-LT
97
    c8 = (+ $arg9 $_ms_w 1) // DN-RT
98
    // if adjacent tiles fit conditions, execute
99
    if (> $c1 -1)                     $arg1 // UP
100
    if (< $c2 $_ms_bt)                $arg2 // DN
101
    if (> $r 0)                       $arg3 // LT
102
    if (< $r $p)                      $arg4 // RT
103
    if (&& [> $c5 -1] [> $r 0])       $arg5 // UP-LT
104
    if (&& [> $c6 -1] [< $r $p])      $arg6 // UP-RT
105
    if (&& [< $c7 $_ms_bt] [> $r 0])  $arg7 // DN-LT
106
    if (&& [< $c8 $_ms_bt] [< $r $p]) $arg8 // DN-RT
107
    arg10
108
]
109
110
_ms_tile_expl = [
111
    // add tile to work queue
112
    append _ms_tq $arg1
113
    // execute content as long as list is populated
114
    while [listlen $_ms_tq] [
115
        do [ _ms_tile_find @(
116
            loopconcat+ i 1 8 [result [[
117
                n = $c@@i
118
                @@@arg2
119
            ]]]
120
        ) (at $_ms_tq 0) ]
121
        _ms_tq = (sublist $_ms_tq 1)
122
    ]
123
]
124
125
_ms_tile_view = [
126
    local n t
127
    n = (at $_ms_nf $arg1)
128
    case $n 0x9 [
129
        // mark the mine as hit
130
        _ms_tile_open 0x9 $arg1 0
131
        append _ms_hp $arg1
132
        _ms_hc = (+ $_ms_hc 1)
133
        // if lives are enabled, reduce by one, otherwise
134
        if (&& $_ms_ua $_ms_u) [ _ms_u = (- $_ms_u 1) ] [
135
            // game over, board is populated by hit and undiscovered mines and flag marks
136
            _ms_gm = 2
137
            _ms_t2 = (getmillis)
138-
    case $n "9" [
138+
139
        ]
140-
        _ms_tile_open "11" $arg1 0
140+
    ] 0x0 [
141
        _ms_tile_open 0x0 $arg1 1
142
        // explore all surrounding tiles until number border
143
        _ms_tile_expl $arg1 [
144
            // make sure the tile is not revealed, otherwise skip tile
145
            if (= (at $_ms_cf $n) 0xF) [
146
                // ensure tile is blank and not in work queue, then add it
147
                if (&& [= (at $_ms_nf $n) 0x0] [< (indexof $_ms_tq $n) 0]) [
148
                    append _ms_tq $n
149
                ]
150-
    ] "0" [
150+
151-
        _ms_tile_open "0" $arg1 1
151+
152
            ]
153
        ]
154
    ] () [ _ms_tile_open $n $arg1 1 ]
155-
            if (= (at $_ms_cf $n) "15") [
155+
156
    if (= $_ms_pc (- $_ms_bt $_ms_m)) [
157-
                if (&& [= (at $_ms_nf $n) "0"] [< (indexof $_ms_tq $n) 0]) [
157+
158
        _ms_set_markers
159
    ]
160
]
161
162
_ms_make_board = [
163
    local i n r
164
    i = 0
165
    // initialise board variables
166
    _ms_th = $arg1
167
    _ms_tw = $arg2
168
    _ms_m  = $arg3
169
    _ms_h  = $_ms_th
170
    _ms_w  = $_ms_tw
171
    _ms_tm = $_ms_m
172
    // initialise game variables
173
    if $_ms_ua [ _ms_u  = (+ (div $_ms_m 20) 1) ]
174
    _ms_bt = (* $_ms_h $_ms_w) // total tiles
175
    _ms_pc = 0 // percentage counter
176
    _ms_t1 = 0 // timer start
177
    _ms_t2 = 0 // timer stop
178
    _ms_gm = 0 // gamemode: 0/1/2/3 = waiting/active/won/lost
179
    _ms_hc = 0 // mine hit counter
180
    _ms_fc = 0 // flag counter
181
    // initialise boards
182
    _ms_nf = ""
183-
    _ms_ua = $_ms_tu
183+
184
    _ms_cf = (loopconcat i $_ms_bt [result 0xF])
185-
    _ms_bt = (* $_ms_h $_ms_w)
185+
186-
    _ms_pc = 0
186+
187-
    _ms_t1 = 0
187+
188-
    _ms_t2 = 0
188+
189-
    _ms_gm = 0
189+
190-
    _ms_hc = 0
190+
191-
    _ms_fc = 0
191+
192
    while [< $i $_ms_m] [
193
        r = (rnd $_ms_bt)
194
        // avoid writing to a previously picked spot
195-
    _ms_cf = (loopconcat i $_ms_bt [result "15"])
195+
196
            _ms_mf = (listsplice $_ms_mf "1" $r 1)
197
            append _ms_mp $r
198
            i = (+ $i 1)
199
        ]
200
    ]
201
    // generate the numbers board
202
    loop p $_ms_bt [
203
        if (at $_ms_mf $p) [ append _ms_nf 0x9 ] [
204
            do [ _ms_tile_find @(
205
                loopconcat+ i 1 8 [result [[
206
                    n = (+ (at $_ms_mf $c@@i) $n)
207
                ]]]
208
            ) $p [ append _ms_nf (tohex $n) ] ]
209
        ]
210
    ]
211
]
212
213
UImenu "minesweeper" [
214-
        if (at $_ms_mf $p) [ append _ms_nf "9" ] [
214+
    uieschide 0
215
    uihlist 0.01 [
216
        uivlist 0 [
217
            UIbox "box d d" 0 0.04 [
218
                uiclamp 1 1
219-
            ) $p [ append _ms_nf $n ] ]
219+
220
                    uialign -1
221
                    uihlist 0.01 [
222
                        local n ; n = (- $_ms_m $_ms_fc $_ms_hc)
223
                        uiimage "media/interface/minesweeper/flag.png" 0.032 0.032
224-
// styles for the difficulty buttons
224+
                        uitext (concatword (? (< $n 0) "^f3") $n) 0.65
225-
_style_button_ms0 = [
225+
226-
    uihover [
226+
227-
        uicolor 0xF0E8E0
227+
228-
        uihold [ uimodvgradient 0x4DFF20 0x279910 ] [
228+
229-
            uimodvgradient 0x279910 0x4DFF20
229+
230
                        uitext (_ms_clock (
231-
        uioutline $_c_line
231+
232
                        )) 0.65
233
                        uiimage "media/interface/minesweeper/clock.png" 0.032 0.032
234-
_style_button_ms1 = [
234+
235-
    uihover [
235+
236-
        uicolor 0xF0E8E0
236+
237-
        uihold [ uimodvgradient 0xFFE020 0x998010 ] [
237+
            uispace 0.01 0.001 [
238-
            uimodvgradient 0x998010 0xFFE020
238+
                uigrid $_ms_w 0 0 [
239
                    loop i $_ms_bt [
240-
        uioutline $_c_line
240+
                        local c ;  c = (at $_ms_cf $i)
241
                        uiimage (format "media/interface/minesweeper/tiles/%1.png" $c) 0.04 0.04 [
242
                            if (< $_ms_gm 2) [
243-
_style_button_ms2 = [
243+
                                if (uihover?) [
244-
    uihover [
244+
                                    p = $i // last 
245-
        uicolor 0xF0E8E0
245+
                                    cond [= $c 0xF] [ // HIDDEN TILE
246-
        uihold [ uimodvgradient 0xFF2020 0x991010 ] [
246+
                                        if (uihold?) [
247-
            uimodvgradient 0x991010 0xFF2020
247+
                                            uioutline 0xFF0000
248
                                        ] [ if (uieschold?) [
249-
        uioutline $_c_line
249+
                                            uioutline 0xFF0000
250
                                        ] [ uioutline 0 ] ]
251
                                        uirelease [ _ms_tile_view $i ]
252
                                        uiescrelease [
253-
ui_menu "minesweeper" [
253+
                                            _ms_tile_open 0xC $i 0
254-
    uieschide (> $_ms_gm 1)
254+
                                            append _ms_fp $i
255
                                            _ms_fc = (+ $_ms_fc 1)
256
                                        ]
257-
            ui_title [
257+
                                    ] [= $c 0xC] [ // FLAGGED TILE
258
                                        uioutline (? (uieschold?) 0xFF0000 0)
259
                                        uiescrelease [
260
                                            _ms_tile_open 0xF $i 0
261
                                            _ms_fp = (listdel $_ms_fp $i)
262
                                            _ms_fc = (- $_ms_fc 1)
263-
                        uiimage "media/texture/minesweeper/flag.png" 0.028 0.028
263+
264-
                        uitext (concatword (? (< $n 0) "^f3") $n) 0.7
264+
                                    ] [!= $c 0x9] [ // -NOT- HIT MINE TILE
265
                                        if (|| [uiescpress?] [uialtpress?]) [
266
                                            _ms_tf = 0 ; o = 0
267
                                            _ms_tile_expl $i [
268
                                                case (at $_ms_cf $n) 0xF [
269
                                                    append _ms_tr $n
270
                                                ] 0xC [
271
                                                    _ms_tf = (+ $_ms_tf 1)
272-
                        )) 0.7
272+
                                                ] 0x9 [
273-
                        uiimage "media/texture/minesweeper/clock.png" 0.028 0.028
273+
                                                    _ms_tf = (+ $_ms_tf 1)
274
                                                ]
275
                                            ]
276-
            ] 0 0.032
276+
277-
            uivlist 0 [ loop y $_ms_h [
277+
                                        if (|| [uieschold?] [uialthold?]) [ uioutline 0x00FFFF ]
278-
                uihlist 0 [ loop x $_ms_w [
278+
                                        if (|| [uipress?] [uialtpress?]) [ o = 1 ]
279-
                    n = (+ (* $y $_ms_w) $x)
279+
                                        if (|| [uirelease?] [uialtrelease?]) [
280-
                    uiimage (format "media/texture/minesweeper/tiles/%1.png" (at $_ms_cf $n)) 0.04 0.04 [
280+
                                            if (= $_ms_tf (at $_ms_nf $i)) [
281-
                        if (< $_ms_gm 2) [
281+
                                                looplist n $_ms_tr [
282-
                            if (uihover?) [
282+
                                                    if (= (at $_ms_cf $n) 0xF) [ _ms_tile_view $n ]
283-
                                p = $n
283+
                                                ]
284-
                                cond [= (at $_ms_cf $n) "15"] [
284+
285-
                                    if (uihold?) [
285+
                                            o = 0
286-
                                        uioutline 0xFF0000
286+
                                            uieschold [] [ _ms_tr = "" ]
287-
                                    ] [ if (uieschold?) [
287+
288-
                                        uioutline 0xFF0000
288+
                                        uiescrelease [ _ms_tr = "" ]
289-
                                    ] [ uioutline 0 ] ]
289+
290-
                                    uirelease [ _ms_tile_view $n ]
290+
                                ] [
291-
                                    uiescrelease [
291+
                                    if (= (getalias p) $i) [ _ms_tr = "" ] [
292-
                                        _ms_tile_open "12" $n 0
292+
                                        if (> (indexof $_ms_tr $i) -1) [ uioutline (? $o 0xFF0000 0) ]
293-
                                        append _ms_fp $n
293+
294-
                                        _ms_fc = (+ $_ms_fc 1)
294+
295
                                uiclamp* 1 1 1 1
296-
                                ] [= (at $_ms_cf $n) "12"] [
296+
297-
                                    uioutline (? (uieschold?) 0xFF0000 0)
297+
298-
                                    uiescrelease [
298+
299-
                                        _ms_tile_open "15" $n 0
299+
300-
                                        _ms_fp = (listdel $_ms_fp $n)
300+
301-
                                        _ms_fc = (- $_ms_fc 1)
301+
            UIbox "box d d" 0 0.04 [
302
                uiclamp 1 1
303-
                                ] [!= (at $_ms_cf $n) "11"] [
303+
304-
                                    if (|| [uiescpress?] [uialtpress?]) [
304+
305-
                                        _ms_tf = 0 ; o = 0
305+
306-
                                        _ms_tile_expl $n [
306+
                        uiimage "media/interface/minesweeper/life.png" 0.032 0.032
307-
                                            case (at $_ms_cf $n) "15" [
307+
                        uitext (? $_ms_ua (? $_ms_u $_ms_u (concatword "^f3" $_ms_u)) "-") 0.65
308-
                                                append _ms_tr $n
308+
309-
                                            ] "12" [
309+
310-
                                                _ms_tf = (+ $_ms_tf 1)
310+
311-
                                            ] "11" [
311+
312-
                                                _ms_tf = (+ $_ms_tf 1)
312+
313
                        uitext (format "^f%1%2%%" (case $_ms_gm 2 3 3 0 () 7) (
314
                            precf (*f (divf $_ms_pc (- $_ms_bt $_ms_m)) 100) 1
315
                        )) 0.65
316-
                                    if (|| [uieschold?] [uialthold?]) [ uioutline 0x00FFFF ]
316+
                        uiimage "media/interface/minesweeper/globe.png" 0.032 0.032
317-
                                    if (|| [uipress?] [uialtpress?]) [ o = 1 ]
317+
318-
                                    if (|| [uirelease?] [uialtrelease?]) [
318+
319-
                                        if (= $_ms_tf (at $_ms_nf $n)) [
319+
320-
                                            looplist i $_ms_tr [
320+
321-
                                                if (= (at $_ms_cf $i) "15") [ _ms_tile_view $i ]
321+
322
            uiclamp 0 0 1 1
323
            UIbox "box d n n" 0.14 0 [
324-
                                        o = 0
324+
                uivlist 0.02 [
325-
                                        uieschold [] [ _ms_tr = "" ]
325+
                    UIbuttonarea "" 0.14 0.032 [
326
                        uirelease [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
327-
                                    uiescrelease [ _ms_tr = "" ]
327+
                        uitext "New Game" 0.6
328
                    ]
329-
                            ] [
329+
                    uigrid 2 0.01 0.01 [
330-
                                if (= (getalias p) $n) [ _ms_tr = "" ] [
330+
                        uiimage "media/interface/minesweeper/height.png" 0.032 0.032
331-
                                    if (> (indexof $_ms_tr $n) -1) [ uioutline (? $o 0xFF0000 0) ]
331+
                        uifill 0 0.032 [ uifield _ms_th 3 [ _ms_set_options $_ms_th 0 ] 0.55 style_generic_focus ]
332
                        uiimage "media/interface/minesweeper/width.png" 0.032 0.032
333
                        uifill 0 0.032 [ uifield _ms_tw 3 [ _ms_set_options 0 $_ms_tw ] 0.55 style_generic_focus ]
334-
                            uiclamp* 1 1 1 1
334+
                        uiimage "media/interface/minesweeper/mine.png" 0.032 0.032
335
                        uifill 0 0.032 [ uifield _ms_tm 3 [ _ms_set_options 0 0 ] 0.55 style_generic_focus ]
336
                    ]
337-
                ] ]
337+
                    UIbuttonarea "" 0.14 0.032 [
338-
            ] ]
338+
                        uirelease [ _ms_ua = (! $_ms_ua) ]
339-
            ui_title [
339+
                        uihlist 0.01 [
340
                            UIcheckbox $_ms_ua 0.016 0 0
341
                            uitext "Use Lives" 0.55
342
                        ]
343
                    ]
344-
                        uiimage "media/texture/minesweeper/life.png" 0.028 0.028
344+
345-
                        uitext (? $_ms_ua (? $_ms_u $_ms_u (concatword "^f3" $_ms_u)) "-") 0.7
345+
346
            uivlist 0.01 [
347
                UIbox "box d n n" 0.14 0 [
348
                    uiclamp 1 1
349
                    uivlist 0 [
350
                        uiclamp 1 1
351
                        UIbuttonarea "hold2" 0.14 0.032 [
352
                            uirelease [ _ms_make_board 9 9 10 ]
353-
                        )) 0.7
353+
                            uitext (concatword (? $uihover? "^f0") "Easy") 0.55
354-
                        uiimage "media/texture/minesweeper/globe.png" 0.028 0.028
354+
355
                        UIbuttonarea "hold2" 0.14 0.032 [
356
                            uirelease [ _ms_make_board 16 16 40 ]
357-
            ] 0.032
357+
                            uitext (concatword (? $uihover? "^f2") "Normal") 0.55
358
                        ]
359-
        //ui_bar 0 1
359+
                        UIbuttonarea "hold2" 0.14 0.032 [
360
                            uirelease [ _ms_make_board 16 30 80 ]
361
                            uitext (concatword (? $uihover? "^f3") "Hard") 0.55
362-
            uioutline 0x484848 0 0 [
362+
363-
                uivlist 0 [
363+
                        uiclamp* 1 1
364-
                    ui_button "" [ uitext "New Game" 0.6 ] 0 0.032 [
364+
365-
                        _ms_make_board $_ms_th $_ms_tw $_ms_tm
365+
366-
                    ] 0 [ uiclamp 1 1 ]
366+
                uitext (concat "^f4FPS:" (tabify $getfps 1)) 0.45
367-
                    uispace 0 0.02 [
367+
                uialign- 1 1
368-
                        uivlist 0.01 [
368+
369-
                            uihlist 0.01 [
369+
370-
                                uiimage "media/texture/minesweeper/height.png" 0.028 0.028
370+
371-
                                uifield _ms_th 3 [ _ms_set_options $_ms_th 0 ] 0.6 [ _style_generic_focus ]
371+
372
    // if game is running, restore clock on show
373-
                            uihlist 0.01 [
373+
374-
                                uiimage "media/texture/minesweeper/width.png" 0.028 0.028
374+
375-
                                uifield _ms_tw 3 [ _ms_set_options 0 $_ms_tw ] 0.6 [ _style_generic_focus ]
375+
376
] [
377-
                            uihlist 0.01 [
377+
378-
                                uiimage "media/texture/minesweeper/mine.png" 0.028 0.028
378+
379-
                                uifield _ms_tm 3 [ _ms_set_options 0 0 ] 0.6 [ _style_generic_focus ]
379+
380
    ]
381
] [] [ uitext "Minesweeper C.S." 0.8 ]