Code
versionString = "1.1.0"
-- Hello! Welcome to the recipe code of FreezeShakr 1.1.0! Feel free to read through the code. Please PM me if you have any questions or comments.
-- This recipe freezes the backbone of your protein, and makes two Bands to Space, whose strength is changed to literally "shake" the protein forward and backward to gain points.
-- This recipe is similar to Wiggle Sidechains, but you get more points than with Wiggle Sidechains.
-- I would suggest you use this recipe in "Auto" Wiggle Power, and with "1.00" Clashing Importance if possible.
-- But this time in version 1.1.0, I fixed the bug which happens on locked segments and made the protein go back to its original position. I also made the protein centered in puzzles with no locked segments.
--
-- Duly credit goes to Formula350, who inspired and helped me make this recipe, and Susume, who also helped me make this recipe. Of course the Foldit Wiki gets credit.
--
-- Thank you for reading through this message!
iterations = 0
dialogResult = 0
totalResidues = structure.GetCount()
areThereLockedSegments = false
function loadData()
print("Loading data...")
seg = 1
score = math.floor(current.GetEnergyScore()*100000)/100000
previousScore = score
gain = 0
totalGain = 0
dialogResult = 0
print("Data loaded!")
end
function addBands()
print("Adding bands...")
while(structure.IsLocked(seg)) do
seg = seg + 1
end
if seg == 1 then
print("There are no locked segments in this puzzle.")
seg = 2
else
print("There are locked segments in this puzzle.")
areThereLockedSegments = true
end
band.Add(seg, seg - 1, seg + 1, 20, 0, 0)
activeBand = band.Add(seg, seg + 1, seg - 1, 20, 0, 0)
if areThereLockedSegments then
startSegment = seg
while(not structure.IsLocked(seg)) do
if seg == totalResidues then
break
else
seg = seg + 1
end
end
seg = seg - 1
band.SetStrength(band.Add(seg, seg - 1, seg + 1, 0.01, 0, 0), 10)
band.SetGoalLength(band.GetCount(), 0.0)
band.SetStrength(band.Add(seg, seg + 1, seg - 1, 0.01, 0, 0), 10)
band.SetGoalLength(band.GetCount(), 0.0)
middleSegment = math.floor((seg+startSegment)/2)
seg = middleSegment
band.SetStrength(band.Add(seg, seg - 1, seg + 1, 0.01, 0, 0), 10)
band.SetGoalLength(band.GetCount(), 0.0)
band.SetStrength(band.Add(seg, seg + 1, seg - 1, 0.01, 0, 0), 10)
band.SetGoalLength(band.GetCount(), 0.0)
seg = math.floor((startSegment+middleSegment)/2)
band.SetStrength(band.Add(seg, seg - 1, seg + 1, 0.01, 0, 0), 10)
band.SetGoalLength(band.GetCount(), 0.0)
band.SetStrength(band.Add(seg, seg + 1, seg - 1, 0.01, 0, 0), 10)
band.SetGoalLength(band.GetCount(), 0.0)
end
end
function prepProtein()
print("Prepping up the protein...")
freeze.UnfreezeAll()
band.DisableAll()
print("Prepping completed!")
end
function freezeBackbone()
print("Freezing backbone...")
selection.SelectAll()
freeze.FreezeSelected(true, false)
end
function confirmRun()
confirmDialog = dialog.CreateDialog("Confirmation")
confirmDialog.Text = dialog.AddLabel("Are you sure you want to run FreezeShakr " .. versionString .. "?")
confirmDialog.Yes = dialog.AddButton("Yes", 1)
confirmDialog.No = dialog.AddButton("No", 0)
dialogResult = dialog.Show(confirmDialog)
if (dialogResult == 1) then
print("Ok, so you're sure you want to run this recipe after all!")
confirmed = true
end
end
function getSettings()
settingsDialog = dialog.CreateDialog("Settings")
settingsDialog.IterationsText = dialog.AddLabel("How many times shall the protein be shaken?")
settingsDialog.IterationsSlider = dialog.AddSlider("Times",100,1,100,0)
settingsDialog.IterationsSliderButton = dialog.AddButton("OK", 1)
settingsDialog.InfiniteIterationsButton = dialog.AddButton("Unlimited!", 2)
dialogResult = dialog.Show(settingsDialog)
statusGetSettings = xpcall(setSettings, handleError)
if confirmed then
print("I need to confirm if these settings are the right ones.")
statusConfirmSettings = xpcall (confirmSettings, handleError)
end
end
function setSettings ()
print("I am setting the settings...")
if(dialogResult == 1) then
iterations = settingsDialog.IterationsSlider.value
print("The number of times is " .. iterations)
else
if(dialogResult == 2) then
print("There are an unlimited number of times!")
AreIterationsUnlimited = true
else
print("The settings dialog has been closed!")
confirmed = false
end
end
print("The settings are set!")
end
function confirmSettings()
settingsConfirmationDialog = dialog.CreateDialog("Settings Confirmation")
settingsConfirmationDialog.Text = dialog.AddLabel("The settings are:")
if AreIterationsUnlimited then
settingsConfirmationDialog.Text = dialog.AddLabel("The protein will be shaken with unlimited times.")
else
if(iterations == 1) then
settingsConfirmationDialog.Text = dialog.AddLabel("The protein will be shaken once.")
else
settingsConfirmationDialog.Text = dialog.AddLabel("The protein will be shaken " .. iterations .. " times.")
end
end
settingsConfirmationDialog.Text2 = dialog.AddLabel("Are these settings the right ones?")
settingsConfirmationDialog.Yes = dialog.AddButton("Yes", 1)
settingsConfirmationDialog.No = dialog.AddButton("No", 2)
dialogResult = dialog.Show(settingsConfirmationDialog);
print("I am setting the settings...")
if(dialogResult == 1) then
print("The settings are confirmed!")
else
if(dialogResult == 2) then
print("The settings are not confirmed.")
getSettings()
else
print("The settings confirmation dialog has been closed!")
confirmed = false
end
end
print("The settings are set!")
end
function main()
statusConfirmRun = xpcall(confirmRun, handleError)
print("Hello " .. user.GetPlayerName() .. "! Welcome to FreezeShakr " .. versionString .. ", by donuts554!")
if confirmed then
statusGetSettings = xpcall(getSettings, handleError)
if confirmed then
print("The recipe has begun!")
statusPrepProtein = xpcall(prepProtein, handleError)
statusLoadData = xpcall(loadData, handleError)
print("Your score is " .. score)
statusFreezeBackbone = xpcall(freezeBackbone, handleError)
statusAddBands = xpcall(addBands, handleError)
print("And now for the freeze shaking!")
if AreIterationsUnlimited then
statusFreezeShake = xpcall(freezeShake, handleError)
while(current.GetEnergyScore()>score) do
previousScore = score
score = math.floor(current.GetEnergyScore()*100000)/100000
statusFreezeShake = xpcall(freezeShake, handleError)
end
else
if(iterations == 1) then
print("Your protein will be shaken once.")
else
print("Now, your protein will be shaken " .. iterations .. " times.")
end
counter = 0
while(counter<iterations) do
previousScore = score
score = math.floor(current.GetEnergyScore()*100000)/100000
statusFreezeShake = xpcall(freezeShake, handleError)
counter = counter + 1
end
end
statusConcludeRecipe = xpcall(concludeRecipe, handleError)
else
print("Ok, you don't want to use the current settings.")
end
else
print("Oh, it was a mistake. Sorry for the inconvenience.")
end
end
function freezeShake()
numberOfBands = band.GetCount()
if areThereLockedSegments then
band.Disable(numberOfBands)
band.Disable(numberOfBands-1)
band.Disable(numberOfBands-2)
band.Disable(numberOfBands-3)
band.Disable(numberOfBands-4)
band.Disable(numberOfBands-5)
end
band.SetStrength(activeBand,0)
structure.WiggleAll(10,true,true)
band.SetStrength(activeBand,10)
structure.WiggleAll(10,true,true)
if areThereLockedSegments then
freeze.FreezeAll()
band.Enable(numberOfBands)
band.Enable(numberOfBands-1)
band.Enable(numberOfBands-2)
band.Enable(numberOfBands-3)
band.Enable(numberOfBands-4)
band.Enable(numberOfBands-5)
band.SetStrength(activeBand,1)
structure.WiggleAll(25,true,false)
else
ui.CenterViewport()
end
freeze.UnfreezeAll()
selection.SelectAll()
freeze.FreezeSelected(true, false)
previousScore = score
score = math.floor(current.GetEnergyScore()*100000)/100000
gain = math.floor((score - previousScore)*100000)/100000
totalGain = totalGain + gain
print("Gained " .. gain .. " points, your score is " .. score .. " points.")
end
function handleError(err)
if string.find(err,"Cancelled")~=nil then
print("The recipe was cancelled.")
statusConcludeRecipe = xpcall(concludeRecipe, handleError)
else
print("Sadly, there is an error.")
print(err)
end
end
function concludeRecipe()
print("Restoring the original conditions...")
if areThereLockedSegments then
band.Delete(band.GetCount())
band.Delete(band.GetCount())
band.Delete(band.GetCount())
band.Delete(band.GetCount())
band.Delete(band.GetCount())
band.Delete(band.GetCount())
end
band.Delete(band.GetCount())
band.Delete(band.GetCount())
freeze.UnfreezeAll()
band.EnableAll()
print("The original conditions are restored.")
if totalGain<0 then
print("The recipe is done! In total, you lost " .. totalGain .. " points.")
else
print("The recipe is done! In total, you gained " .. totalGain .. " points!")
end
end
status = xpcall(main, handleError)