View difference between Paste ID: 2zBNGK2V and AgWAT4zm
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
8
_ms_fc = 0  // placed flags counter
9
_ms_hc = 0  // hit mines counter
10
_ms_bt = 0  // board tiles (total)
11
_ms_t1 = 0  // timer start (ms)
12
_ms_t2 = 0  // timer end (ms)
13
_ms_ua = 1  // undo toggle
14
_ms_u  = 0  // undo counter
15
_ms_pc = 0  // completion percentage
16
_ms_gm = 0  // game mode: 0 waiting; 1 active; 2 failure ; 3 victory
17
18
_ms_mf = "" // board containing mine placement
19
_ms_nf = "" // board containing tile numbers
20
_ms_cf = "" // board of completion (displayed)
21
_ms_mp = "" // supplementary list with mine positions
22
_ms_hp = "" // supplementary list with hit mine positions
23
_ms_fp = "" // supplementary list with flag positions
24
_ms_tq = "" // supplementary list with tile reveal queue
25
26
27
// ----------------------------------------- //
28
// ---------------- Tile ID ---------------- //
29
// ----------------------------------------- //
30
// 0   = blank tile
31
// 1-8 = number tiles
32
// H   = hidden tile
33
// X   = mine tile (failure)
34
// HX  = mine tile (victory)
35
// R   = hit mine tile
36
// F   = flagged tile
37
// FX  = flagged mine tile (success)
38
// FF  = flagged mine tile (failure)
39
40
41
append = [ $arg1 = (concat (getalias $arg1) $arg2) ]
42
43
_ms_clock = [
44
	local n
45
	n = (mod $arg1 1000)
46
	format "%1:%2%3" (div $arg1 1000) (? (< $n 100) (? (< $n 10) "00" "0")) $n
47
]
48
49
_ms_set_options = [
50
	local n
51
	n = (* $_ms_th $_ms_tw)
52
	// calculate board sizes/mines and apply limits
53
	if $arg1 [ _ms_th = (max (min $arg1 24) 9) ]
54
	if $arg2 [ _ms_tw = (max (min $arg2 30) 9) ]
55
	_ms_tm = (max (min (div $n 3) $_ms_tm) (div $n 10))
56
]
57
58
_ms_set_markers = [
59
	// in case of failure, update flag markers
60
	if (= $_ms_gm 2) [
61
		looplist i $_ms_fp [
62
			if (=s (at $_ms_nf $i) "X") [
63
				_ms_tile_open "FX" $i 0
64
			] [ _ms_tile_open "FF" $i 0 ]
65
		]
66
	]
67
	// update mine markers, discounting flagged ones
68
	looplist i $_ms_mp [
69
		case $_ms_gm 2 [
70
			if (< (indexof "R FX FF" (at $_ms_cf $i)) 0) [
71
				_ms_tile_open "X" $i 0
72
			]
73
		] 3 [
74
			if (< (indexof "R F" (at $_ms_cf $i)) 0) [
75
				_ms_tile_open "HX" $i 0
76
			]
77
		]
78
	]
79
]
80
81
_ms_tile_open = [
82
	// start the game if still in waiting phase
83
	if (= $_ms_gm 0) [
84
		_ms_gm = 1
85
		_ms_t1 = (getmillis)
86
	]
87
	// reveal # tile at given position and add to completion counter if needed
88
	_ms_cf = (listsplice $_ms_cf $arg1 $arg2 1)
89
	if $arg3 [ _ms_pc = (+ $_ms_pc 1) ]
90
]
91
92
_ms_tile_find = [
93
	local n c1 c2 c3 c4 c5 c6 c7 c8
94
	n = 0
95
	c1 = (- $arg9 $_ms_w)       // UP
96
	c2 = (+ $arg9 $_ms_w)       // DN
97
	c3 = (- $arg9 1)            // LT
98
	c4 = (+ $arg9 1)            // RT
99
	c5 = (- $arg9 (+ $_ms_w 1)) // UP-LT
100
	c6 = (- $arg9 (- $_ms_w 1)) // UP-RT
101
	c7 = (+ $arg9 (- $_ms_w 1)) // DN-LT
102
	c8 = (+ $arg9 (+ $_ms_w 1)) // DN-RT
103
	if (> $c1 -1)                                               $arg1 // UP
104
	if (< $c2 $_ms_bt)                                          $arg2 // DN
105
	if (> (mod $arg9 $_ms_w) 0)                                 $arg3 // LT
106
	if (< (mod $arg9 $_ms_w) (- $_ms_w 1))                      $arg4 // RT
107
	if (&& [> $c5 -1] [> (mod $arg9 $_ms_w) 0])                 $arg5 // UP-LT
108
	if (&& [> $c6 -1] [< (mod $arg9 $_ms_w) (- $_ms_w 1)])      $arg6 // UP-RT
109
	if (&& [< $c7 $_ms_bt] [> (mod $arg9 $_ms_w) 0])            $arg7 // DN-LT
110
	if (&& [< $c8 $_ms_bt] [< (mod $arg9 $_ms_w) (- $_ms_w 1)]) $arg8 // DN-RT
111
	arg10
112
]
113
114
_ms_tile_view = [
115
	local n t
116
	n = (at $_ms_nf $arg1)
117
	cases $n "X" [
118
		// mark the mine as hit
119
		_ms_tile_open "R" $arg1 0
120
		append _ms_hp $arg1
121
		_ms_hc = (+ $_ms_hc 1)
122
		// if lives are enabled, reduce by one, otherwise
123
		if (&& $_ms_ua $_ms_u) [ _ms_u = (- $_ms_u 1) ] [
124
			// game over, board is populated by hit and undiscovered mines and flag marks
125
			_ms_gm = 2
126
			_ms_t2 = (getmillis)
127
			_ms_set_markers
128
		]
129
	] "0" [
130
		_ms_tile_open "0" $arg1 1
131
		append _ms_tq $arg1
132
		// explore all surrounding tiles until number border
133
		while [listlen $_ms_tq] [
134
			do [ _ms_tile_find @(
135
				loopconcat i 8 [result [[
136
					n = $c@@(+ $i 1)
137
					// make sure the tile is not revealed, otherwise skip tile
138
					if (=s (at $_ms_cf $n) "H") [
139
						// ensure tile is blank and not in work queue, then add it
140
						if (&& [=s (at $_ms_nf $n) "0"] [< (indexof $_ms_tq $n) 0]) [
141
							append _ms_tq $n
142
						]
143
						// reveal the tile around the blank tile and add to completion percentage
144
						_ms_tile_open (at $_ms_nf $n) $n 1
145
					]
146
				]]]
147
			) (at $_ms_tq 0) ]
148
			// remove used tile from work queue
149
			_ms_tq = (sublist $_ms_tq 1)
150
		]
151
	] () [ _ms_tile_open $n $arg1 1 ]
152
	// in case of board completion, victory
153
	if (= $_ms_pc (- $_ms_bt $_ms_m)) [
154
		_ms_gm = 3 ; _ms_t2 = (getmillis)
155
		_ms_set_markers
156
	]
157
]
158
159
_ms_make_board = [
160
	local i n r
161
	i = 0
162
	// initialise board variables
163
	_ms_th = $arg1
164
	_ms_tw = $arg2
165
	_ms_m  = $arg3
166
	_ms_h  = $_ms_th
167
	_ms_w  = $_ms_tw
168
	_ms_tm = $_ms_m
169
	// initialise game variables
170
	_ms_ua = $_ms_tu
171
	if $_ms_ua [ _ms_u  = (+ (div $_ms_m 20) 1) ]
172
	_ms_bt = (* $_ms_h $_ms_w)
173
	_ms_pc = 0
174
	_ms_t1 = 0
175
	_ms_t2 = 0
176
	_ms_gm = 0
177
	_ms_hc = 0
178
	_ms_fc = 0
179
	// initialise boards
180
	_ms_nf = ""
181
	_ms_mf = (loopconcat i $_ms_bt [result "0"])
182
	_ms_cf = (loopconcat i $_ms_bt [result "H"])
183
	// initialise supplementary lists
184
	_ms_tq = ""
185
	_ms_mp = ""
186
	_ms_hp = ""
187
	_ms_fp = ""
188
	// generate the mines board
189
	while [< $i $_ms_m] [
190
		r = (rnd $_ms_bt)
191
		// avoid writing to a previously picked spot
192
		if (! (at $_ms_mf $r)) [
193
			_ms_mf = (listsplice $_ms_mf "1" $r 1)
194
			append _ms_mp $r
195
			i = (+ $i 1)
196
		]
197
	]
198
	// generate the numbers board
199
	loop y $_ms_bt [
200
		if (at $_ms_mf $y) [ append _ms_nf "X" ] [
201
			do [ _ms_tile_find @(
202
				loopconcat i 8 [result [[
203
					n = (+ (at $_ms_mf $c@@(+ $i 1)) $n)
204
				]]]
205
			) $y [ append _ms_nf $n ] ]
206
		]
207-
	uicolor 0x44444444 0 0 [
207+
208
]
209
a = [exec scripts/minesweeper.cfg; showui minesweeper]
210
211
_ms_ui_diff = [
212
	uimodcolor 0xEEEEEE 0.22 0.03 [
213
		uialign 0 1
214
		uihold    [ uimodvgradient $arg3 $arg2 ] [
215
			uialthold [ uimodvgradient $arg3 $arg2 ] [
216
				uihover   [ uimodvgradient $arg2 $arg3 ]
217
			]
218
		]
219
		uirelease [ arg4 ]
220
		uioutline 0x181818
221
		uiclamp* 1 1 1 1
222
		uispace 0.01 0.002 [
223
			uitext $arg1 0.6
224
		]
225
	]
226
]
227
228
newui minesweeper [
229
	uicolor 0x60444444 0 0 [
230
		uioutline 0x181818 0 0 [ uiclamp 1 1 1 1 ]
231
		uispace 0.005 0.005 [
232
			uihlist 0.01 [
233
				uivlist 0 [
234
					uihlist 0 [
235
						uiclamp 1 1 0 0
236
						uispace 0.015 0.002 [
237-
										uihover   [ uimodcolor 0xFFBBBB ]
237+
238-
										uihold    [ uimodcolor 0xCCCCCC ]
238+
239-
										uialthold [ uimodcolor 0xCCCCCC ]
239+
240
								uitext (format "^f%1%2%%" (case $_ms_gm 2 3 3 0 () 7) (
241
									absf (precf (*f (divf $_ms_pc (- $_ms_bt $_ms_m)) 100) 1))
242
								) 0.8
243
							]
244
						]
245
						uispace 0.015 0.002 [
246
							uialign 1 0
247
							uihlist 0.01 [
248
								uitext (_ms_clock (case $_ms_gm 0 0 1 (- $getmillis $_ms_t1) () (- $_ms_t2 $_ms_t1))) 0.8
249
								uiimage "media/texture/minesweeper/clock.png" 0.04 0.04
250
							]
251
						]
252
					]
253
					loop y $_ms_h [
254
						uihlist 0 [
255
							loop x $_ms_w [
256
								n = (+ (* $y $_ms_w) $x)
257
								uiimage (format "media/texture/minesweeper/tiles/%1.png" (at $_ms_cf $n)) 0.045 0.045 [
258
									if (&& [< $_ms_gm 2] [|| [=s (at $_ms_cf $n) "H"] [=s (at $_ms_cf $n) "F"]]) [
259
										uihover   [ uimodvgradient 0xCCCCCC 0xBBBBBB ]
260-
				uivlist 0.03 [
260+
										uihold    [ uimodvgradient 0xCCCCCC 0xEEEEEE ] [
261-
					uialign 0 -1
261+
											uialthold [ uimodvgradient 0xEEEEEE 0xDDDDDD ]
262-
					uimodcolor 0xDDDDDD 0 0 [
262+
263-
						uihover   [ uimodcolor 0xCCCCCC ]
263+
264-
						uihold    [ uimodcolor 0xBBBBBB ]
264+
265-
						uialthold [ uimodcolor 0xBBBBBB ]
265+
266-
						uirelease [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
266+
267-
						uioutline 0x181818
267+
268-
						uiclamp* 1 1 1 1
268+
269-
						uispace 0.01 0.002 [
269+
270
												_ms_fc = (+ $_ms_fc 1)
271-
								uialign 0 0
271+
272-
								uiimage "media/texture/minesweeper/new.png" 0.04 0.04
272+
273-
								uitext "New Game" 0.8
273+
274
												_ms_fc = (- $_ms_fc 1)
275
											]
276
										]
277-
					//uispace 0 0.01
277+
278
									]
279-
						uispace 0.01 0.002 [
279+
280
							]
281-
								uialign 0 0
281+
282-
								uiimage "media/texture/minesweeper/life.png" 0.04 0.04
282+
283-
								uitext (concatword (? $_ms_u "^f6" "^f3") (? $_ms_ua $_ms_u 0)) 0.8
283+
284
				uioutline 0x181818 0 0 [ uiclamp 1 1 1 1 ]
285
				uivlist 0 [
286-
						uispace 0.01 0.002 [
286+
					uiclamp 0 0 1 1
287
					uivlist 0.03 [
288-
								uialign 0 0
288+
						uimodcolor (? (> $_ms_gm 1) 0xCCFFCC 0xEEEEEE) 0.22 0 [
289-
								uiimage "media/texture/minesweeper/flag.png" 0.04 0.04
289+
							uialign 0 -1
290-
								uitext (- $_ms_m (listlen $_ms_fp) (listlen $_ms_hp)) 0.8
290+
							uihold [ uimodvgradient 0xCCCCCC 0xFFFFFF ] [
291
								uialthold [ uimodvgradient 0xCCCCCC 0xFFFFFF ] [
292
									uihover [ uimodvgradient 0xFFFFFF 0xCCCCCC ]
293
								]
294-
					
294+
295
							uirelease [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
296-
						uialign -1 -1
296+
							uioutline 0x181818
297-
						uivlist 0 [
297+
							uiclamp* 1 1 1 1
298
							uispace 0.01 0.002 [
299
								uihlist 0.01 [
300
									uialign 0 0
301-
									uiimage "media/texture/minesweeper/height.png" 0.03 0.03
301+
									uiimage "media/texture/minesweeper/new.png" 0.04 0.04
302-
									uifill 0 0 [
302+
									uitext "New Game" 0.8
303-
										uihover  [ uimodcolor 0xCCCCCC ]
303+
304-
										uifocus+ [ uimodcolor 0xBBBBBB ]
304+
305
						]
306-
										uifield _ms_th 2 [ _ms_set_options $_ms_th 0 ] 0.6 [
306+
307
							uialign -1 -1
308
							uispace 0.01 0.002 [
309
								uihlist 0.01 [
310
									uialign 0 0
311
									uiimage "media/texture/minesweeper/life.png" 0.04 0.04
312
									uitext (concatword (? $_ms_u "^f7" "^f3") (? $_ms_ua $_ms_u 0)) 0.8
313
								]
314
							]
315
							uispace 0.01 0.002 [
316-
									uiimage "media/texture/minesweeper/width.png" 0.03 0.03
316+
317-
									uifield _ms_tw 2 [ _ms_set_options 0 $_ms_tw ] 0.6 [
317+
318-
										uioutline 0x181818
318+
									uiimage "media/texture/minesweeper/flag.png" 0.04 0.04
319
									uitext (- (- $_ms_m $_ms_fc) $_ms_hc) 0.8
320
								]
321
							]
322
						]
323
					]
324-
						uispace 0 0.002 [
324+
					uivlist 0.02 [
325-
							uivlist 0.004 [
325+
326-
								uialign 0 0
326+
							uialign -1 -1
327-
								uiimage "media/texture/minesweeper/mine.png" 0.03 0.03
327+
							uivlist 0 [
328-
								uifield _ms_tm 3 [ _ms_set_options 0 0 ] 0.6 [
328+
								uispace 0.01 0.002 [
329-
									uioutline 0x181818
329+
									uihlist 0.01 [
330-
									uiclamp- 1 1 1 1
330+
										uialign 0 0
331
										uiimage "media/texture/minesweeper/height.png" 0.03 0.03
332
										uimodcolor 0xEEEEEE 0 0 [
333
											uihover  [ uimodvgradient 0xFFFFFF 0xCCCCCC ]
334
											uiclamp- 1 1 1 1
335-
					
335+
											uifield _ms_th 2 [ _ms_set_options $_ms_th 0 ] 0.6 [
336-
					
336+
												uioutline 0x181818
337
												uiclamp- 1 1 1 1
338
											]
339
										]
340
									]
341
								]
342
								uispace 0.01 0.002 [
343
									uihlist 0.01 [
344
										uialign 0 0
345
										uiimage "media/texture/minesweeper/width.png" 0.03 0.03
346
										uimodcolor 0xEEEEEE 0 0 [
347
											uihover  [ uimodvgradient 0xFFFFFF 0xCCCCCC ]
348
											uiclamp- 1 1 1 1
349
											uifield _ms_tw 2 [ _ms_set_options 0 $_ms_tw ] 0.6 [
350
												uioutline 0x181818
351
												uiclamp- 1 1 1 1
352
											]
353
										]
354
									]
355
								]
356
							]
357
							uispace 0 0.002 [
358
								uivlist 0.004 [
359
									uialign 0 0
360
									uiimage "media/texture/minesweeper/mine.png" 0.03 0.03
361
									uimodcolor 0xEEEEEE 0 0 [
362
										uihover  [ uimodvgradient 0xFFFFFF 0xCCCCCC ]
363
										uiclamp- 1 1 1 1
364
										uifield _ms_tm 3 [ _ms_set_options 0 0 ] 0.6 [
365
											uioutline 0x181818
366
											uiclamp- 1 1 1 1
367
										]
368
									]
369
								]
370
							]
371
						]
372
						uitext "Difficulty" 0.8
373
						uivlist 0.003 [
374
							uialign 0 1
375
							_ms_ui_diff "Easy"   0x4DFF00 0x279900 [ _ms_make_board  9  9 10 ]
376
							_ms_ui_diff "Normal" 0xFFD800 0x997400 [ _ms_make_board 16 16 40 ]
377
							_ms_ui_diff "Hard"   0xFF2010 0x991008 [ _ms_make_board 16 30 80 ]
378
							uispace 0 0.005
379
							uimodcolor 0xEEEEEE 0.22 0.03 [
380
								uialign 0 -1
381
								uihold [ uimodvgradient 0xCCCCCC 0xFFFFFF ] [
382
									uialthold [ uimodvgradient 0xCCCCCC 0xFFFFFF ] [
383
										uihover [ uimodvgradient 0xFFFFFF 0xCCCCCC ]
384
									]
385
								]
386
								uirelease [
387
									_ms_tu = (! $_ms_tu)
388
									if (! $_ms_gm) [ _ms_ua = $_ms_tu ]
389
								]
390
								uioutline 0x181818
391
								uiclamp* 1 1 1 1
392
								uispace 0.01 0.002 [
393
									uihlist 0.01 [
394
										uialign 0 0
395
										uiimage (concatword "media/texture/minesweeper/check_" (
396
											case (+ $_ms_tu $_ms_ua) 0 [ result "off" ] 1 [ result "sel" ] 2 [ result "on" ]
397
										) ".png") 0.025 0.025
398
										uitext "Use Lives" 0.6
399
									]
400
								]
401
							]
402
						]
403
					]
404
				]
405
			]
406
		]
407
	]
408
] [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]