Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | None | 0 0
  1. --[[ Lua code. See documentation: http://berserk-games.com/knowledgebase/scripting/ --]]
  2.  
  3. --[[ The OnLoad function. This is called after everything in the game save finishes loading.
  4. Most of your script code goes here. --]]
  5. function onload()
  6.    
  7.     -- Get the text tool
  8.     textObject = getObjectFromGUID('006bdc')
  9.     if textObject == nil then
  10.         -- The GUID is invalid
  11.         errorGuidInvalid()
  12.         textTool = nil
  13.     else
  14.         textTool = textObject.TextTool
  15.     end
  16.    
  17.     emptyText = ' '
  18.    
  19.     if textTool == nil then
  20.         -- The GUID is invalid
  21.         errorGuidInvalid()
  22.     else
  23.         -- Initialize the text options
  24.         local textColor = {}
  25.         textColor['r'] = 200
  26.         textColor['g'] = 200
  27.         textColor['b'] = 200
  28.         textTool.setFontColor(textColor)
  29.         textTool.setFontSize(40)
  30.         textTool.setValue(emptyText)
  31.     end
  32.    
  33. end
  34.  
  35. --[[ The Update function. This is called once per frame. --]]
  36. function update ()
  37.    
  38.     -- Validate text object
  39.     if textTool == nil then
  40.         -- The GUID is invalid
  41.         errorGuidInvalid()
  42.         return
  43.     end
  44.     --[[
  45.     -- Get and validate cardboard object
  46.     if cardboard == nil then
  47.         cardboard = getObjectFromGUID('11df74')
  48.         if cardboard == nil then
  49.             -- The GUID is invalid
  50.             errorGuidInvalid()
  51.             return
  52.         end
  53.     end
  54. --]]
  55.     -- If the zone is moving, wait
  56.     if not self.resting then
  57.         --textTool.setValue(emptyText)
  58.         --cardboard.setDescription(emptyText)
  59.         return
  60.     end
  61.  
  62.     local ownPos = self.getPosition()
  63.     local ownRotation = self.getRotation()['x']
  64.     local ownScale = self.getScale()['x']
  65.  
  66.     -- Move the display with the box
  67.    
  68.     local displayOffsetX = -0.96 * ownScale
  69.     local displayOffsetY = 0 * ownScale
  70.     local displayOffsetZ = 0.18 * ownScale
  71.    
  72.     --local displayOffsetX = -1 * ownScale
  73.     --local displayOffsetY = 0.05 * ownScale
  74.     --local displayOffsetZ = 1.8 * ownScale
  75.     --local displayOffsetX = 0
  76.     --local displayOffsetY = 0
  77.     --local displayOffsetZ = 0
  78.     textObject.setPosition({ownPos['x'] + displayOffsetX, ownPos['y'] + displayOffsetY, ownPos['z'] + displayOffsetZ})
  79.     --cardboard.setPosition({ownPos['x'] + displayOffsetX, ownPos['y'] + displayOffsetY, ownPos['z'] + displayOffsetZ})
  80.  
  81.     -- Compute the bounds of the box
  82.     local xboundOffset = 1.0
  83.     local zboundOffset = 0.2
  84.     local leftBound = ownPos['x'] - xboundOffset * ownScale
  85.     local rightBound = ownPos['x'] + xboundOffset * ownScale
  86.     local upperBound = ownPos['z'] + zboundOffset * ownScale
  87.     local lowerBound = ownPos['z'] - zboundOffset * ownScale
  88.  
  89.     local valueToCounter = {}
  90.     local valuesToSort = {}
  91.  
  92.     -- Iterate all objects in the zone
  93.     for _, obj in pairs(getAllObjects()) do
  94.         -- Fetch resting dices
  95.         if obj != nil and obj.tag == 'Dice' and obj.resting then
  96.             -- Only use objects inside the zone
  97.             local objPos = obj.getPosition()
  98.             if objPos['x'] > leftBound and objPos['x'] < rightBound and objPos['z'] > lowerBound and objPos['z'] < upperBound then
  99.                 local value = obj.getValue()
  100.                 local counter = valueToCounter[value]
  101.                 -- First occurrence of this value
  102.                 if counter == nil then
  103.                     counter = 0
  104.                     valuesToSort[#valuesToSort + 1] = value
  105.                 end
  106.                 -- Increase the occurrence of this value
  107.                 counter = counter + 1
  108.                 valueToCounter[value] = counter
  109.             end
  110.         end
  111.     end
  112.  
  113.     -- Process the tracked values, sorted and build the lines to display
  114.     local textLines = {}
  115.     table.sort(valuesToSort)
  116.     for index, value in ipairs(valuesToSort) do
  117.         local counter = valueToCounter[value]
  118.         local line = '#' .. value .. ': ' .. counter
  119.         textLines[#textLines + 1] = line
  120.     end
  121.  
  122.     -- Display the text
  123.     local text = table.concat(textLines, '\n')
  124.     if text == '' then
  125.         -- Suppress the default text 'Type Here'
  126.         text = emptyText
  127.     end
  128.     textTool.setValue(text)
  129.     --cardboard.setDescription(text)
  130. end
  131.  
  132. function errorGuidInvalid()
  133.     print('Error: GUID invalid')
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement