Advertisement
Blockchain

Untitled

Jul 24th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1.  
  2. function powerupUsed( d ) -- data keys: setTarget zone, powerup object, setUser zone
  3. if Global.getVar("roundStateID")~=2 and Global.getVar("roundStateID")~=3 then return end
  4.  
  5. local tableZ1 = Global.call( "forwardFunction", {function_name="findCardsInZone", data={d.setTarget.zone}} )
  6. local decksOne = Global.call( "forwardFunction", {function_name="findDecksInZone", data={d.setTarget.zone}} )
  7. if d.setTarget.value > 21 then
  8. broadcastToColor("You are bust.", d.setUser.color, {1,0.5,0.5})
  9. return false
  10. end
  11. if (#tableZ1~=0 or #decksOne~=0) and (d.setUser.value<=21 or (d.setUser.value==68) or (d.setUser.value==69 and d.setUser.count==2) or (d.setUser.value==71 and d.setUser.count==2) or (d.setUser.value==70 and d.setUser.count==3)) then
  12. local allCards = {}
  13.  
  14. local handsWithCards = {}
  15. local objectSets = Global.getTable("objectSets")
  16. for i=2,#objectSets do
  17. if objectSets[i].count>0 and objectSets[i].value>0 then
  18. local find = Global.call( "forwardFunction", {function_name="findCardsInZone", data={objectSets[i].zone}} ) or {}
  19. local decks = Global.call( "forwardFunction", {function_name="findDecksInZone", data={objectSets[i].zone}} ) or {}
  20. if #find>0 or #decks>0 then
  21. table.insert( handsWithCards, objectSets[i] )
  22.  
  23. for n=1,#find do table.insert(allCards, find[n]) end
  24. for n=1,#decks do table.insert(allCards, decks[n]) end
  25. end
  26. end
  27. end
  28. if #handsWithCards<=1 then
  29. broadcastToColor("There must be at least two hands with cards to use this powerup.", d.setUser.color, {1,0.5,0.5})
  30. return false
  31. end
  32.  
  33. local cardCount = {} -- for i=1,#handsWithCards do cardCount[i] = 0 end
  34. local loggedCards = {}
  35. while #allCards>0 do
  36. local obj = allCards[#allCards]
  37. allCards[#allCards] = nil
  38.  
  39. if obj.tag=="Deck" then
  40. local chosenHand = math.random(1,#handsWithCards)
  41. cardCount[chosenHand] = (cardCount[chosenHand] or 0) + 1
  42. loggedCards[chosenHand] = (loggedCards[chosenHand] or {})
  43.  
  44. local pos = Global.call( "forwardFunction", {function_name="findCardPlacement", data={handsWithCards[chosenHand].zone, cardCount[chosenHand]}} )
  45. obj.setPosition(pos)
  46. obj.shuffle()
  47.  
  48. -- Can't log deck properly, it's destroyed when the second last card is removed. Thanks TTS devs.
  49.  
  50. for i=2,obj.getQuantity() do
  51. local chosenHand = math.random(1,#handsWithCards)
  52. cardCount[chosenHand] = (cardCount[chosenHand] or 0) + 1
  53. loggedCards[chosenHand] = (loggedCards[chosenHand] or {})
  54.  
  55. local pos = Global.call( "forwardFunction", {function_name="findCardPlacement", data={handsWithCards[chosenHand].zone, cardCount[chosenHand]}} )
  56. local taken = obj.takeObject({position=pos})
  57.  
  58. Global.call( "forwardFunction", {function_name="cardPlacedCallback", data={taken, {targetPos=pos, set=handsWithCards[chosenHand], isStarter=cardCount[chosenHand]<=2, flip=true}}} )
  59.  
  60. table.insert(loggedCards[chosenHand], taken)
  61. end
  62. elseif obj.tag=="Card" then
  63. local chosenHand = math.random(1,#handsWithCards)
  64. cardCount[chosenHand] = (cardCount[chosenHand] or 0) + 1
  65. loggedCards[chosenHand] = (loggedCards[chosenHand] or {})
  66.  
  67. local pos = Global.call( "forwardFunction", {function_name="findCardPlacement", data={handsWithCards[chosenHand].zone, cardCount[chosenHand]}} )
  68. obj.setPosition(pos)
  69. Global.call( "forwardFunction", {function_name="cardPlacedCallback", data={obj, {targetPos=pos, set=handsWithCards[chosenHand], isStarter=cardCount[chosenHand]<=2, flip=true}}} )
  70.  
  71. table.insert(loggedCards[chosenHand], obj)
  72. end
  73. end
  74.  
  75. local sortedLoggedCards = {}
  76. for k,v in pairs(loggedCards) do
  77. if cardCount[k]>1 and #v>0 then
  78. table.insert(sortedLoggedCards, {count=cardCount[k], id=k, cards=v})
  79. end
  80. end
  81. local emptyHands = {}
  82. for i=1,#handsWithCards do
  83. if not cardCount[i] then
  84. table.insert(emptyHands, i)
  85. end
  86. end
  87.  
  88. while #sortedLoggedCards>0 and #emptyHands>0 do -- Have moveable cards, not yet given every hand a card
  89. local fillHand = emptyHands[#emptyHands]
  90. local chosenStealKey = math.random(1,#sortedLoggedCards)
  91. local chosenSteal = sortedLoggedCards[chosenStealKey]
  92.  
  93. local pos = Global.call( "forwardFunction", {function_name="findCardPlacement", data={handsWithCards[fillHand].zone, 1}} )
  94. chosenSteal.cards[#chosenSteal.cards].setPosition(pos)
  95. Global.call( "forwardFunction", {function_name="cardPlacedCallback", data={obj, {targetPos=pos, set=handsWithCards[fillHand], isStarter=true, flip=true}}} )
  96.  
  97. table.remove(chosenSteal.cards)
  98.  
  99. cardCount[fillHand] = (cardCount[fillHand] or 0)+1
  100. cardCount[chosenSteal.id] = cardCount[chosenSteal.id] - 1
  101. if cardCount[chosenSteal.id]<=1 or #chosenSteal.cards==0 then
  102. table.remove(sortedLoggedCards, chosenStealKey)
  103. end
  104.  
  105. emptyHands[#emptyHands] = nil
  106. end
  107.  
  108. destroyObject(d.powerup)
  109.  
  110. return true
  111. else
  112. broadcastToColor("Must use powerup on your own hand while you have cards, cannot be played while busted.", d.setUser.color, {1,0.5,0.5})
  113. end
  114. end
  115. function onLoad()
  116. Global.call("AddPowerup", {obj=self, who="Self Only", effectName="Shuffle All"} )
  117. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement