Novaknight

dice mat

Sep 23rd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. --[[ Lua code. See documentation: http://berserk-games.com/knowledgebase/scripting/ --]]
  2. --[[ The OnLoad function. This is called after everything in the game save finishes loading.
  3. Most of your script code goes here. --]]
  4. function onload()
  5. self.createButton({
  6. label="---", click_function="none", function_owner=Global,
  7. position={-6.4,0.11,0.01}, rotation={0,0,0}, height=0, width=0, font_size=650,
  8. font_color={1, 1, 1},
  9. scale={0.25, 0.25, 0.25},
  10. alignment=2
  11. })
  12.  
  13. emptyText = ' '
  14. end
  15. --[[Function to reset description
  16. function setDefaultState()
  17. self.setDescription(JSON.encode({
  18. dice=6
  19. }))
  20. end
  21. ]]--
  22. --[[ The Update function. This is called once per frame. --]]
  23. function update ()
  24.  
  25. --[[ Reset description on null
  26. local data = JSON.decode(self.getDescription())
  27. if data==nil then
  28. setDefaultState()
  29. data = JSON.decode(self.getDescription())
  30. printToAll('Warning - invalid description. Restored defaut configuration.', {0.8,0.5,0})
  31. end
  32. -- Set text size based on description or reset if invalid.
  33. if data.dice==4 then
  34. self.editButton({ position={1.3,0.03,-1.99}, font_size=700})
  35. elseif data.dice==6 then
  36. self.editButton({ position={0.7,0.03,-1.99}, font_size=700})
  37. elseif data.dice==8 then
  38. self.editButton({ position={0.1,0.03,-1.99}, font_size=680})
  39. elseif data.dice==10 then
  40. self.editButton({ position={0.1,0.03,-1.99}, font_size=550})
  41. elseif data.dice==12 then
  42. self.editButton({ position={0.1,0.03,-2.1}, font_size=450})
  43. elseif data.dice==20 then
  44. self.editButton({ position={0.05,0.03,-2.5}, font_size=275})
  45. else
  46. setDefaultState()
  47. data = JSON.decode(self.getDescription())
  48. printToAll('Warning - invalid description. Restored defaut configuration.', {0.8,0.5,0})
  49. end
  50.  
  51. ]]--
  52. -- If the zone is moving, wait
  53. if not self.resting then
  54. return
  55. end
  56.  
  57. local ownPos = self.getPosition()
  58. local ownRotation = self.getRotation()['x']
  59. local ownScale = self.getScale()['x']
  60. local Rotation = self.getRotation()
  61. -- Compute the bounds of the box.
  62. -- Manipulate the offset to account for tray borders.
  63. -- y offset will affect how above and below it will see dice.
  64. -- I was too lazy to split the y offset to only extend above the tray.
  65. -- If you want to fix do so and message me. ~(^_^;~)
  66.  
  67. local xboundOffset = 6.9 --Horizontal Offset
  68. local yboundOffset = 2.5 --Up/Down Offset
  69. local zboundOffset = 1 --Vertical Offset
  70. if Rotation.y > 89 and Rotation.y < 91 then
  71. xboundOffset = 1
  72. zboundOffset = 6.9
  73. end
  74. if Rotation.y > 269 and Rotation.y < 270 then
  75. xboundOffset = 1
  76. zboundOffset = 6.9
  77. end
  78. local leftBound = ownPos['x'] - xboundOffset * ownScale
  79. local rightBound = ownPos['x'] + xboundOffset * ownScale
  80. local upperBound = ownPos['z'] + zboundOffset * ownScale
  81. local lowerBound = ownPos['z'] - zboundOffset * ownScale
  82. local yupperBound = ownPos['y'] + yboundOffset --* ownScale
  83. local ylowerBound = ownPos['y'] - yboundOffset --* ownScale
  84.  
  85. local valueToCounter = {}
  86. local valuesToSort = {}
  87.  
  88. -- Iterate all objects in the zone
  89. for _, obj in pairs(getAllObjects()) do
  90. -- Fetch resting dices
  91. if obj != nil and obj.tag == 'Dice' and obj.resting then
  92. -- Only use objects inside the zone
  93. local objPos = obj.getPosition()
  94. 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
  95. local value = obj.getValue()
  96. local counter = valueToCounter[value]
  97. -- First occurrence of this value
  98. if counter == nil then
  99. counter = 0
  100. valuesToSort[#valuesToSort + 1] = value
  101. end
  102. -- Increase the occurrence of this value
  103. counter = counter + 1
  104. valueToCounter[value] = counter
  105. end
  106. end
  107. end
  108.  
  109. -- Process the tracked values, sorted and build the lines to display
  110. local textLines = {}
  111. table.sort(valuesToSort)
  112. for index, value in ipairs(valuesToSort) do
  113. local counter = valueToCounter[value]
  114. local line = '#' .. value .. ': ' .. counter
  115. textLines[#textLines + 1] = line
  116. end
  117.  
  118. -- Display the text
  119. local text = table.concat(textLines, '\n')
  120. if text == '' then
  121. -- Suppress the default text 'Type Here'
  122. text = emptyText
  123. end
  124. self.editButton({index=0, label=text})
  125. end
Advertisement
Add Comment
Please, Sign In to add comment