Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Roll-O-Matic by MrStump
- --CUSTOMIZATION VARIABLES BEGIN HERE
- --Distance the dice are placed from the tool
- radius = 8
- --How high (relative to the tool) the dice are placed
- height = 1
- --How many seconds after the dice come to rest before they are auto-cleaned
- --If you want cleanup with no delay after roll results, use 0
- --A value of -1 will cause them not to be cleaned up automatically
- cleanupAfterRoll = 0
- --If the results of the roll are printed, true or false
- printRollResults = true
- --If the roll results are put into order or left as they are
- --Only applies to the printing of roll results, the dice are not impacted
- organizeRollResults = true
- --END OF CUSTOMIZATION VARIABLES
- function onSave()
- local data_to_save = {da=diceActive, wd=watchingDice}
- saved_data = JSON.encode(data_to_save)
- return saved_data
- end
- function onLoad(saved_data)
- if saved_data ~= "" then
- --Set up information off of loaded_data
- local loaded_data = JSON.decode(saved_data)
- diceActive = loaded_data.da
- watchingDice = loaded_data.wd
- else
- diceActive = {}
- watchingDice = false
- end
- createButtons()
- numberButtonPressed(30, "White")
- rollResults = {0,0,0,0,0,0}
- end
- --Activated by button press, pulls dice and starts process
- function numberButtonPressed(num, color)
- if watchingDice == false then
- --Delete old dice if they exist
- if #diceActive > 0 then
- deleteCurrentDice()
- end
- watchingDice = true
- math.randomseed(os.time())
- local bagPos, bagRot = self.getPosition(), self.getRotation()
- for i=1, num do
- --Create Position
- local pos = getRadialPosition(i, num, bagPos, bagRot)
- --Create random rotation
- local rot = randomRotation()
- --Removes objects from bag
- applyTakeObject(pos, rot, color, num)
- end
- else
- --Error if you try to roll dice again before the first set finishes
- broadcastToColor("Dice are already being rolled.", color, {0.8,0.2,0.2})
- end
- end
- function applyTakeObject(pos, rot, color, num)
- self.takeObject({
- position=pos, rotation=rot,
- callback="afterSpawn", callback_owner=self, params={color=color, num=num}
- })
- end
- --Callback, runs after dice are spawned
- function afterSpawn(die, params)
- table.insert(diceActive, die.getGUID())
- if #diceActive == params.num then
- self.setVar("colorForCoroutine", params.color)
- startLuaCoroutine(self, "watchDice")
- end
- end
- --Coroutine, watching for all the dice to come to rest.
- function watchDice()
- local color = self.getVar("colorForCoroutine")
- local startTime = os.time()
- repeat
- local restingCount = 0
- for i, dieGUID in ipairs(diceActive) do
- local die = getObjectFromGUID(dieGUID)
- if not die or die.resting == true then
- restingCount = restingCount + 1
- end
- end
- coroutine.yield(0)
- until restingCount == #diceActive or os.time() > startTime + 3
- watchingDice = false
- --Prints the results if printRollResults is enabled
- if printRollResults == true then
- formatRollResults(color)
- end
- --Automatically deletes the dice if cleanupAfterRoll is 0 or higher
- if cleanupAfterRoll == 0 then
- deleteCurrentDice()
- elseif cleanupAfterRoll > 0 then
- Timer.destroy("cleanupAfterRollTimer")
- Timer.create({
- identifier="cleanupAfterRollTimer", function_name="deleteCurrentDice",
- function_owner=self, delay=cleanupAfterRoll
- })
- end
- return 1
- end
- --Used to delete dice from previous rolls
- function deleteCurrentDice()
- for _, dieGUID in ipairs(diceActive) do
- local die = getObjectFromGUID(dieGUID)
- if die then
- destroyObject(die)
- end
- end
- diceActive = {}
- watchingDice = false
- numberButtonPressed(30, "White")
- end
- --Obtains values for rolls and orders them
- function formatRollResults(color)
- for _, dieGUID in ipairs(diceActive) do
- local die = getObjectFromGUID(dieGUID)
- if die then
- local value = die.getValue()
- rollResults[value] = rollResults[value] + 1
- end
- end
- print("One: "..rollResults[1])
- print("Two: "..rollResults[2])
- print("Three: "..rollResults[3])
- print("Four: "..rollResults[4])
- print("Five: "..rollResults[5])
- print("Six: "..rollResults[6])
- print("-----------------")
- end
- --Get a radial position to place item
- function getRadialPosition(i, i_max, pos, rot)
- local spokes = 360/i_max
- local posX = pos.x + math.sin( math.rad((spokes*i)+rot.y) ) * radius
- local posY = pos.y + height
- local posZ = pos.z + math.cos( math.rad((spokes*i)+rot.y) ) * radius
- return {x=posX, y=posY, z=posZ}
- end
- --Gets a random rotation vector
- function randomRotation()
- --Credit for this function goes to Revinor (forums)
- --Get 3 random numbers
- local u1 = math.random();
- local u2 = math.random();
- local u3 = math.random();
- --Convert them into quats to avoid gimbal lock
- local u1sqrt = math.sqrt(u1);
- local u1m1sqrt = math.sqrt(1-u1);
- local qx = u1m1sqrt *math.sin(2*math.pi*u2);
- local qy = u1m1sqrt *math.cos(2*math.pi*u2);
- local qz = u1sqrt *math.sin(2*math.pi*u3);
- local qw = u1sqrt *math.cos(2*math.pi*u3);
- --Apply rotation
- local ysqr = qy * qy;
- local t0 = -2.0 * (ysqr + qz * qz) + 1.0;
- local t1 = 2.0 * (qx * qy - qw * qz);
- local t2 = -2.0 * (qx * qz + qw * qy);
- local t3 = 2.0 * (qy * qz - qw * qx);
- local t4 = -2.0 * (qx * qx + ysqr) + 1.0;
- --Correct
- if t2 > 1.0 then t2 = 1.0 end
- if t2 < -1.0 then ts = -1.0 end
- --Convert back to X/Y/Z
- local xr = math.asin(t2);
- local yr = math.atan2(t3, t4);
- local zr = math.atan2(t1, t0);
- --Return result
- return {math.deg(xr),math.deg(yr),math.deg(zr)}
- end
- --Button creation
- function createButtons()
- --Spawns number buttons and assigns a function trigger for each
- for i, pos in ipairs(buttonPositionList) do
- self.createButton({
- click_function="but"..i, function_owner=self,
- position=pos, height=150, width=150
- })
- local func = function(_,color) numberButtonPressed(i, color) end
- self.setVar("but"..i, func)
- end
- --Spawns deleteCurrentDice button
- self.createButton({
- click_function="deleteCurrentDice", function_owner=self,
- position={0,0,0.73}, height=150, width=150
- })
- end
- --Data table of positions for number buttons 1-9, in order
- buttonPositionList = {
- {-0.435,0,0.59}, {-0.693,0,0.22}, {-0.693,0,-0.22}, {-0.435,0,-0.59},
- {0,0,-0.73}, {0.435,0,-0.59}, {0.693,0,-0.22}, {0.693,0,0.22}, {0.435,0,0.59}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement