From 1afaa63838cac6fd184d694894be4f83065e6108 Mon Sep 17 00:00:00 2001 From: torchlight Date: Sat, 8 Jan 2022 08:58:57 +0800 Subject: [PATCH 1/4] Add an include for WinAPIConstants.au3. --- Tetris.au3 | 1 + 1 file changed, 1 insertion(+) diff --git a/Tetris.au3 b/Tetris.au3 index cc40137..d329a69 100644 --- a/Tetris.au3 +++ b/Tetris.au3 @@ -26,6 +26,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . #ce +#include #include #include #include From 9eb1f7acd664ba2446d67b052ca37b1095a72def Mon Sep 17 00:00:00 2001 From: torchlight Date: Sat, 8 Jan 2022 10:35:45 +0800 Subject: [PATCH 2/4] Improve RNG seeding. --- Tetris.au3 | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Tetris.au3 b/Tetris.au3 index d329a69..7111a43 100644 --- a/Tetris.au3 +++ b/Tetris.au3 @@ -104,7 +104,7 @@ Global $PieceH = -1 Global $Swapped = False Global $Bag -Global $BagSeed = Random(0, 65535, 1) +Global $BagSeed = Random(0, 0x7fffffff, 1) Global $BagType = Number(IniRead('settings.ini', 'SETTINGS', 'BAG_TYPE', 0)) Global $BagPieces[7] = [0,1,2,3,4,5,6] ;ensure standard bag-type @@ -2175,7 +2175,7 @@ EndFunc Func __QueueEncode() Local $S = '' - $S &= Hex($BagSeed, 4) + $S &= Hex($BagSeed, 8) $S &= Hex($PieceH, 1) For $i = 0 To UBound($Bag) - 1 $S &= Hex($Bag[$i], 1) @@ -2194,16 +2194,16 @@ Func __QueueDecode($QueueData) Local $Queue $S = B64_Decode($QueueData) - $S = StringMid($S&'', 3) + $S = StringMid($S&'', 3) ; strip the leading '0x' - $Seed = StringMid($S, 1, 4) - $Hold = StringMid($S, 5, 1) - $Queue = StringMid($S, 6) + $Seed = StringMid($S, 1, 8) + $Hold = StringMid($S, 9, 1) + $Queue = StringMid($S, 10) $Queue = StringRight($Queue, 1) = 'E' ? StringTrimRight($Queue, 1) : $Queue $Queue = StringSplit($Queue, '', 2) ;check data is correct lengths - If StringLen($Seed) <> 4 Then Return + If StringLen($Seed) <> 8 Then Return If StringLen($Hold) <> 1 Then Return ;conversts values to decimal @@ -2775,7 +2775,7 @@ Func BagSeed() SRandom($BagSeed) EndFunc Func BagReseed() - $BagSeed = Random(0, 65535, 1) + $BagSeed = BitAND($BagSeed*123456789 + 1, 0x7fffffff) EndFunc From 8d6d4ee6df9b711094e4ad80c1df06000769d68f Mon Sep 17 00:00:00 2001 From: torchlight Date: Thu, 13 Jan 2022 01:51:39 +0800 Subject: [PATCH 3/4] Fix some timer race conditions; fix soft drop when SDS and SDD are both 0. --- Tetris.au3 | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/Tetris.au3 b/Tetris.au3 index 7111a43..31c50da 100644 --- a/Tetris.au3 +++ b/Tetris.au3 @@ -171,6 +171,7 @@ Global $REDO_MAX = 0 ;timers Global $GTimer = TimerInit() +Global $GlobalTime = 0 Global $tInput = 0 Global $tGravity = 0 Global $tSticky = 0 @@ -758,15 +759,23 @@ While 1 Main() DrawGame($DRW) + $GlobalTime = TimerDiff($GTimer) + #cs While TimerDiff($GTimer) > $tInput GameInput() $tInput += 1000/60 WEnd - - While TimerDiff($GTimer) > $tGravity + ; Doesn't seem like there's a reason to run this at 60 Hz + ; since it already uses loops to apply DAS and soft drop + ; inside `GameInput`. + #ce + GameInput() + $tInput = $GlobalTime + + While $GlobalTime > $tGravity $tGravity += 1000 / $Gravity If Not Tick() Then - If $tGravity < TimerDiff($GTimer) Then $tGravity = TimerDiff($GTimer) + If $tGravity < $GlobalTime Then $tGravity = $GlobalTime ExitLoop EndIf WEnd @@ -2543,10 +2552,14 @@ EndFunc Func GameInput() Local Static $tSDS = 0 + Local $Time = _WinAPI_GetTickCount() + Local $Diff + If Not $KEYBINDS[0][$KEYSTATE] And Not $KEYBINDS[1][$KEYSTATE] Then $DAS_DIR = '' If $DAS_DIR = 'L' Then If $KEYBINDS[0][$KEYSTATE] Then - While $KEYBINDS[0][$KEYTIME] + $DAS + $tARR < _WinAPI_GetTickCount() + $Diff = $Time - $KEYBINDS[0][$KEYTIME] - $DAS + While $tARR <= $Diff If Not PieceMove(0,-1,0) Then ExitLoop $tARR += $ARR If $ARR > 15 Then Sound('move') @@ -2561,7 +2574,8 @@ Func GameInput() ElseIf $DAS_DIR = 'R' Then If $KEYBINDS[1][$KEYSTATE] Then - While $KEYBINDS[1][$KEYTIME] + $DAS + $tARR < _WinAPI_GetTickCount() + $Diff = $Time - $KEYBINDS[1][$KEYTIME] - $DAS + While $tARR <= $Diff If Not PieceMove(0,+1,0) Then ExitLoop $tARR += $ARR If $ARR > 15 Then Sound('move') @@ -2577,7 +2591,8 @@ Func GameInput() If Not $KEYBINDS[2][$KEYSTATE] Then $tSDS = 0 If $KEYBINDS[2][$KEYSTATE] Then - While $KEYBINDS[2][$KEYTIME] + $SDD + $tSDS < _WinAPI_GetTickCount() + $Diff = $Time - $KEYBINDS[2][$KEYTIME] - $SDD + While $tSDS <= $Diff If Not PieceMove(0,0,+1) Then ExitLoop $tSDS += $SDS WEnd @@ -2627,7 +2642,11 @@ Func MoveD() $tSDS = 0 - Return PieceMove(0, 0, +1) + PieceMove(0, 0, +1) + If $SDD = 0 And $SDS = 0 Then + Do + Until Not PieceMove(0, 0, +1) + EndIf EndFunc Func MoveU() If $Lost Then Return @@ -2889,9 +2908,10 @@ Func PieceReset() $tSpin = False ;resets all timers so that the piece will not teleport around - $KEYBINDS[0][$KEYTIME] = _WinAPI_GetTickCount() - $DAS - $KEYBINDS[1][$KEYTIME] = _WinAPI_GetTickCount() - $DAS - $KEYBINDS[2][$KEYTIME] = _WinAPI_GetTickCount() - $SDD + Local $Time = _WinAPI_GetTickCount() + $KEYBINDS[0][$KEYTIME] = $Time - $DAS + $KEYBINDS[1][$KEYTIME] = $Time - $DAS + $KEYBINDS[2][$KEYTIME] = $Time - $SDD $tGravity = TimerDiff($GTimer) + (1000 / $Gravity) $tARR = 0 $tSDS = 0 From eea720269a4715a0e1373be2df641fe718749603 Mon Sep 17 00:00:00 2001 From: torchlight Date: Thu, 13 Jan 2022 01:53:59 +0800 Subject: [PATCH 4/4] Update comment in `StateEncode` to reflect that the seed is now 32 bits. --- Tetris.au3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tetris.au3 b/Tetris.au3 index 31c50da..0be21ea 100644 --- a/Tetris.au3 +++ b/Tetris.au3 @@ -2132,7 +2132,7 @@ Func StateEncode() Local $QueueData = '' Local $BoardData = '' - $QueueData = '[' & __QueueEncode() ;4 bits per piece + 16 bits (bag seed) + $QueueData = '[' & __QueueEncode() ;4 bits per piece + 32 bits (bag seed) $BoardData = '[' & __BoardEncode() ;4 bits per block, compressed Return $QueueData&$BoardData