Advertisement
Guest User

TTS Chip Converter

a guest
Jul 1st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. --[[Chip Converter]]--
  2. g_chip_lua = "function update()\nlocal ChipDes = self.getDescription()\nlocal Count = self.getQuantity()\nlocal ChipValue = Global.call('format_number', {tonumber(ChipDes)})\nif (Count > 0) then\nlocal Total = Global.call('format_number', {tonumber(Count * ChipDes)})\nself.setName('* '..ChipValue..' = $'..Total)\nelse\nself.setName('$'..ChipValue)\nend\nend"
  3. chip_table = {}
  4. chip_values = {}
  5. firstTime = 0
  6.  
  7. function sortTable(a, b)
  8. return a > b
  9. end
  10.  
  11. function initializeChipTable()
  12. local scripted_zone = getObjectFromGUID('##CHIP ZONE##') -- Where the chips are read from, if you have a stack at the back of your hidden zone
  13. local chips = scripted_zone.getObjects()
  14. for k, v in ipairs(chips) do
  15. local val = tonumber(v.getDescription()) or 0
  16. if val > 0 then
  17. chip_table[val] = v.getGUID()
  18. table.insert(chip_values, val)
  19. end
  20. end
  21. table.sort(chip_values, sortTable)
  22. end
  23.  
  24. function spawnChip(value)
  25. local params = {}
  26. params.position = {20, 15, -8.5}
  27. local original_chip = getObjectFromGUID(chip_table[value])
  28.  
  29. chip = original_chip.clone(params)
  30. chip.setLuaScript(g_chip_lua)
  31. chip.unlock()
  32. return chip
  33. end
  34.  
  35. function spawnChips(amount)
  36. local chip = nil
  37. local num = 0
  38.  
  39. for i, v in ipairs(chip_values) do
  40. --local num = math.floor(amount / v)
  41. local num = amount / v
  42. if num >= 0.99999 and num <= 1.00001 then
  43. num = 1
  44. end
  45. if num % 1 ~= 0 then
  46. num = math.floor(num)
  47. end
  48. if (num >= 1) then
  49. amount = amount - num * v
  50. for i = 1, num, 1 do
  51. chip = spawnChip(v)
  52. end
  53. end
  54. end
  55. end
  56.  
  57. function convertChipsDown(amount)
  58. if amount == 5 then
  59. spawnChip(amount)
  60. else
  61. for i, v in ipairs(chip_values) do
  62. if amount ~= v then
  63. local num = math.floor(amount / v)
  64. amount = amount - num * v
  65. for i = 1, num, 1 do
  66. chip = spawnChip(v)
  67. end
  68. end
  69. end
  70. end
  71. end
  72.  
  73. timeConvert = os.time()
  74.  
  75. function convertChips()
  76. if firstTime == 0 then
  77. firstTime = 1
  78. initializeChipTable()
  79. else
  80. if os.time() >= timeConvert then
  81. local scripted_zone = getObjectFromGUID('##CHIP CONVERT ZONE##')
  82. local chip_table = scripted_zone.getObjects()
  83. local amount = 0
  84. local amount_chips = 0
  85. local obj = {}
  86. local istack = 0
  87. for i, v in ipairs(chip_table) do
  88. local mult = 0
  89. local val = 0
  90.  
  91. mult = v.getQuantity()
  92. val = tonumber(v.getDescription()) or 0
  93. if mult == -1 then
  94. mult = 1
  95. end
  96. if val > 0 then
  97. amount_chips = amount_chips + 1
  98. istack = mult
  99. amount = amount + (val * mult)
  100. v.destruct()
  101. end
  102. end
  103. if amount_chips == 1 and istack == 1 then
  104. convertChipsDown(amount)
  105. else
  106. spawnChips(amount)
  107. end
  108. timeConvert = os.time() + 5
  109. end
  110. end
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement