Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Lua code. See documentation: http://berserk-games.com/knowledgebase/scripting/ --]]
- --[[ The OnLoad function. This is called after everything in the game save finishes loading.
- Most of your script code goes here. --]]
- function onload()
- self.createButton({
- label="---", click_function="none", function_owner=Global,
- position={-6.4,0.11,0.01}, rotation={0,0,0}, height=0, width=0, font_size=650,
- font_color={1, 1, 1},
- scale={0.25, 0.25, 0.25},
- alignment=2
- })
- emptyText = ' '
- end
- --[[Function to reset description
- function setDefaultState()
- self.setDescription(JSON.encode({
- dice=6
- }))
- end
- ]]--
- --[[ The Update function. This is called once per frame. --]]
- function update ()
- --[[ Reset description on null
- local data = JSON.decode(self.getDescription())
- if data==nil then
- setDefaultState()
- data = JSON.decode(self.getDescription())
- printToAll('Warning - invalid description. Restored defaut configuration.', {0.8,0.5,0})
- end
- -- Set text size based on description or reset if invalid.
- if data.dice==4 then
- self.editButton({ position={1.3,0.03,-1.99}, font_size=700})
- elseif data.dice==6 then
- self.editButton({ position={0.7,0.03,-1.99}, font_size=700})
- elseif data.dice==8 then
- self.editButton({ position={0.1,0.03,-1.99}, font_size=680})
- elseif data.dice==10 then
- self.editButton({ position={0.1,0.03,-1.99}, font_size=550})
- elseif data.dice==12 then
- self.editButton({ position={0.1,0.03,-2.1}, font_size=450})
- elseif data.dice==20 then
- self.editButton({ position={0.05,0.03,-2.5}, font_size=275})
- else
- setDefaultState()
- data = JSON.decode(self.getDescription())
- printToAll('Warning - invalid description. Restored defaut configuration.', {0.8,0.5,0})
- end
- ]]--
- -- If the zone is moving, wait
- if not self.resting then
- return
- end
- local ownPos = self.getPosition()
- local ownRotation = self.getRotation()['x']
- local ownScale = self.getScale()['x']
- local Rotation = self.getRotation()
- -- Compute the bounds of the box.
- -- Manipulate the offset to account for tray borders.
- -- y offset will affect how above and below it will see dice.
- -- I was too lazy to split the y offset to only extend above the tray.
- -- If you want to fix do so and message me. ~(^_^;~)
- local xboundOffset = 6.9 --Horizontal Offset
- local yboundOffset = 2.5 --Up/Down Offset
- local zboundOffset = 1 --Vertical Offset
- if Rotation.y > 89 and Rotation.y < 91 then
- xboundOffset = 1
- zboundOffset = 6.9
- end
- if Rotation.y > 269 and Rotation.y < 270 then
- xboundOffset = 1
- zboundOffset = 6.9
- end
- local leftBound = ownPos['x'] - xboundOffset * ownScale
- local rightBound = ownPos['x'] + xboundOffset * ownScale
- local upperBound = ownPos['z'] + zboundOffset * ownScale
- local lowerBound = ownPos['z'] - zboundOffset * ownScale
- local yupperBound = ownPos['y'] + yboundOffset --* ownScale
- local ylowerBound = ownPos['y'] - yboundOffset --* ownScale
- local valueToCounter = {}
- local valuesToSort = {}
- -- Iterate all objects in the zone
- for _, obj in pairs(getAllObjects()) do
- -- Fetch resting dices
- if obj != nil and obj.tag == 'Dice' and obj.resting then
- -- Only use objects inside the zone
- local objPos = obj.getPosition()
- if objPos['x'] > leftBound and objPos['x'] < rightBound and objPos['z'] > lowerBound and objPos['z'] < upperBound and objPos['y'] < yupperBound and objPos['y'] > ylowerBound then
- local value = obj.getValue()
- local counter = valueToCounter[value]
- -- First occurrence of this value
- if counter == nil then
- counter = 0
- valuesToSort[#valuesToSort + 1] = value
- end
- -- Increase the occurrence of this value
- counter = counter + 1
- valueToCounter[value] = counter
- end
- end
- end
- -- Process the tracked values, sorted and build the lines to display
- local textLines = {}
- table.sort(valuesToSort)
- for index, value in ipairs(valuesToSort) do
- local counter = valueToCounter[value]
- local line = '#' .. value .. ': ' .. counter
- textLines[#textLines + 1] = line
- end
- -- Display the text
- local text = table.concat(textLines, '\n')
- if text == '' then
- -- Suppress the default text 'Type Here'
- text = emptyText
- end
- self.editButton({index=0, label=text})
- end
Advertisement
Add Comment
Please, Sign In to add comment