stom66

CheckCardsInAZoneForExplosiveCombinations

Dec 15th, 2020
969
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local explosiveCombinations = {
  2.     --[[
  3.         make a table of the cards that should explode with each other
  4.         combinations only need to appear once for this to work but you can
  5.         duplicate them if that makes it easier to manage.
  6.     --]]
  7.  
  8.     {"Fire", "Water"},
  9.     {"Fire", "Ice"},
  10.     {"Fire", "Another Card Name"},
  11. }
  12.  
  13. function isExplosive(name1, name2)
  14.     --[[
  15.         Simple function to loop through the table of explosiveCombinations and check for
  16.         entries that match the card names provided
  17.     --]]
  18.     for _,card in ipairs(explosiveCombinations) do
  19.         if (card[1]==name1 and card[2]==name2) or
  20.             (card[1]==name2 and card[2]==name1) then
  21.             return true
  22.         end
  23.     end
  24.  
  25.     return false
  26. end
  27.  
  28. function compareCardsInZone(zoneGUID)
  29.     --[[
  30.         Loops through all the objects in a zone, finds the cards, and reads their names to look for
  31.         any of the explosive compbinations declared above
  32.     --]]
  33.    
  34.     --get a reference to the zone
  35.     local zone = getObjectFromGUID(zoneGUID)
  36.  
  37.     --create a blank table to store found Card object references
  38.     local cards = {}
  39.  
  40.     --loop through all the Objects in the zone and add any Cards found to the blank cards table we just created
  41.     for _,obj in ipairs(zone.getObjects()) do
  42.         if obj.tag == "Card" then
  43.             table.insert(cards, obj)
  44.         end
  45.     end
  46.  
  47.     --abort if we don't find at least 2 Cards in the zone
  48.     if #cards < 2 then return false end
  49.  
  50.     --loop through all the found Cards, checking if they're explosive with any of the other found Cards
  51.     for i=1,#cards-1 do
  52.  
  53.         --pop the first Card from the table of found cards
  54.         local card1 = table.remove(cards, 1)
  55.         local card1Name = card1.getName()
  56.  
  57.         --loop through the other found Cards and check if they form an explosive combination
  58.         for _,card2 in ipairs(cards) do
  59.             local card2Name = card2.getName()
  60.  
  61.             if isExplosive(card1Name, card2Name) then
  62.                 print("The cards "..card1Name.." and "..card2Name.." are explosive!")
  63.             end
  64.         end
  65.     end
  66. end
RAW Paste Data