Icon representing a recipe

Recipe: Repeat Rebuild Selected v1.1

created by StackOverflow

Profile


Name
Repeat Rebuild Selected v1.1
ID
48954
Shared with
Public
Parent
None
Children
None
Created on
May 01, 2014 at 08:51 AM UTC
Updated on
May 01, 2014 at 08:51 AM UTC
Description

Continuously rebuild a selected region

Best for


Code


--- Repeat Rebuild Selection v1.1 --- --- added scaling CI factor --- Repeat Rebuild Selection v1.0 --- --- by StackOverflow -- script_name = "Repeat Rebuild Selection" select_index = 0 select_length = 0 function GetSelection () for i = 1 , structure.GetCount () do if ( freeze.IsFrozen ( i ) ) then if ( select_index == 0 ) then select_index = i end else if ( select_index > 0 and select_length == 0 ) then select_length = i - select_index end end end if ( select_index == 0 ) then for i = 1 , structure.GetCount () do if ( selection.IsSelected ( i ) ) then if ( select_index == 0 ) then select_index = i end else if ( select_index > 0 and select_length == 0 ) then select_length = i - select_index end end end end if ( select_index > 0 and select_length == 0 ) then select_length = structure.GetCount () - select_index + 1 end freeze.UnfreezeAll () end function Error () d = dialog.CreateDialog ( script_name ) d.lbl1 = dialog.AddLabel ( "First, freeze the segment you want rebuilt" ) d.lbl2 = dialog.AddLabel ( "Then, run this recipe again" ) d.lbl3 = dialog.AddLabel ( "" ) d.lbl4 = dialog.AddLabel ( "Or alternatively, use the selection interface" ) d.btn = dialog.AddButton( "Oh, Ok!" , 1 ) dialog.Show ( d ) GetSelection () end function main () GetSelection () if ( select_index > 0 and select_length > 0 ) then can_mutate = check_can_mutate () print ( script_name .. " by StackOverflow" ) print ( "You have chosen segment " .. select_index .. "-" .. (select_index + select_length - 1) ) print ( "Starting Score: " .. flr ( savedScore , 2 ) ) running = true cut = false iterations = 1 ci = 0.75 while ( running ) do print ( "Rebuild " .. iterations .. " Iteration" .. iif( iterations == 1 , "" , "s") .. iif( cut , " + Cut" , "" ) .. " CI " .. ci ) freeze.UnfreezeAll () if ( cut ) then SliceNSelect ( select_index , select_length , false ) else Select ( select_index , select_length ) end structure.RebuildSelected ( iterations ) behavior.SetClashImportance ( ci ) --Wiggle () structure.WiggleAll ( 6 , true , true ) behavior.SetClashImportance ( 1 ) if ( cut ) then Wiggle () SliceNSelect ( select_index , select_length , true ) cut = false iterations = iterations + 1 if ( iterations == 20 ) then iterations = 1 end if ( ci <= 0.15 ) then ci = ci - 0.01 else ci = ci - 0.15 end if ( ci < 0.01 ) then ci = 0.35 end else cut = true end for rep = 1 , 2 do if ( can_mutate and current.GetScore () > 0 ) then structure.MutateSidechainsSelected ( 1 ) end structure.ShakeSidechainsSelected ( 1 ) Wiggle () end if ( current.GetScore () > savedScore - 20 ) then MaximumWiggle () -- we're close, see it through end RestoreBest () if ( flr ( savedScore ) > flr ( startScore ) ) then print ( "Best Score: " .. flr ( savedScore , 2 ) ) end end else print ( "You must choose a segment to rebuild" ) print ( "To do so, freeze that segment before running script" ) print ( "Or alternatively, use the selection interface" ) Error () end end function check_can_mutate () for i = select_index , select_index + select_length - 1 do if ( structure.CanMutate ( i , "a" ) ) then return true end end return false end function cleanup () running = false behavior.SetClashImportance ( clash ) RestoreBest () freeze.UnfreezeAll () print ( "Cancelled" ) if ( select_index > 0 ) then Report () end end function Report () d = dialog.CreateDialog ( script_name ) d.lbl1 = dialog.AddLabel ( script_name .. " deactivated" ) if ( savedScore > startScore ) then d.lbl2 = dialog.AddLabel ( "+" .. ( savedScore - startScore ) .. " points!" ) rank = scoreboard.GetRank () if ( rank < startRank ) then d.lbl3 = dialog.AddLabel ( "Gained " .. ( startRank - rank ) .. " Ranks!" ) end else d.lbl4 = dialog.AddLabel ( "No gains achieved, sorry :(" ) end d.btn = dialog.AddButton( "Thanks" , 1 ) dialog.Show ( d ) end function RestoreBest () SaveOrRestoreBest ( true ) end function SaveBest () SaveOrRestoreBest ( false ) end function SaveOrRestoreBest ( restore ) local cscore = current.GetScore () if ( cscore > savedScore ) then if ( cscore - savedScore > 0.0001 ) then print ( "+" .. round ( (cscore - savedScore) , 4 ) .. " points!" ) end savedScore = cscore save.Quicksave ( saveSlot ) else if ( restore == true ) then save.Quickload ( saveSlot ) end end end function SliceNSelect ( index , sz , undo ) if ( undo == true ) then if ( index > 1 ) then structure.DeleteCut ( index - 1 ) end if ( index + sz <= structure.GetCount () ) then structure.DeleteCut ( index + sz - 1) end else if ( index > 1 ) then structure.InsertCut ( index - 1 ) end if ( index + sz <= structure.GetCount () ) then structure.InsertCut ( index + sz - 1 ) end Select ( index , sz ) end end function Select ( index , sz ) selection.DeselectAll () selection.SelectRange ( index , index + sz - 1 ) end -- maximum wiggle, tracks score increase, wiggling until the score stops going up -- function MaximumWiggle () _maximumWiggle ( 2 , 4 ) end -- maximum wiggle, tracks score increase, wiggling until the score stops going up -- function Wiggle () _maximumWiggle ( 0 , 2 ) end -- maximum wiggle, tracks score increase, wiggling until the score stops going up -- function _maximumWiggle ( decimals , start ) local dec = decimals local cscr = current.GetScore () - 1 local nscr = round ( current.GetScore () , dec ) local iter = start while ( nscr > cscr ) do if ( current.GetScore () > savedScore - 1 ) then -- we're so close! push it out! if ( dec < 3 ) then dec = 3 end if ( iter < 5 ) then iter = iter + 2 end end structure.WiggleAll ( iter , true , true ) cscr = nscr nscr = round ( current.GetScore () , dec ) iter = iter + 1 end end function round(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / mult end function flr(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult) / mult end function iif ( condition , true_part , false_part) if ( condition ) then return true_part else return false_part end end saveSlot = 86 startScore = current.GetScore () savedScore = current.GetScore () startRank = scoreboard.GetRank () clash = behavior.GetClashImportance () save.Quicksave ( saveSlot ) xpcall ( main , cleanup ) --main ()

Comments


StackOverflow Lv 1

What it Does:
This recipe rebuilds the section you specify
It rebuilds, wiggles, shakes, fuzes. Keeps the highest scoring fold.
It also mutates if the puzzle swings that way

How to Use:
There are two ways to tell the recipe what segment you want to rebuild.
You can freeze a section, then run the recipe.
Or, you can select segments in the selection interface, then run the recipe.

More Info:
Recipe runs indefinitely, until you cancel it.
Every second round will use cuts to isolate the rebuild
The rebuild iteration power increases each round, up to a maximum of 19 (at which point it resets back to 1)

StackOverflow Lv 1

Update to v1.1

Now uses a scaling clash importance.
Starting from CI 0.75 and dropping by 0.15 each iteration
(original script always used .75)