Noobyhead99

itsAlive

Mar 20th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.94 KB | None | 0 0
  1. -- Initial variable and function declarations
  2. local tArgs = {...}
  3. local cardsMain = {}
  4. local cardsMainKeys = {}
  5. local currentCardData = {}
  6. local eventParameters = {}
  7. local currentCardIndex = 1
  8. local wrappedLoreCount = 0
  9.  
  10. -- Used for loading a card file, localCardsMain is the table of all the currently loaded cards and path is the path of the card file to load. Returns localCardsMain with the cards from the card file added.
  11. local function registerCardFile(localCardsMain,path)
  12.   local handle = io.open(path,"r")
  13.   for line in handle:lines() do
  14.     local cardData = textutils.unserialize(line)
  15.     localCardsMain[cardData["id"]] = cardData
  16.   end
  17.   handle:close()
  18.   return localCardsMain
  19. end
  20.  
  21. -- Saves localCardsMain as a card file to path
  22. local function saveCardFile(localCardsMain,path)
  23.   local handle = io.open(path,"w")
  24.   for k,v in pairs(localCardsMain) do
  25.     handle:write(textutils.serialize(v).."\n")
  26.   end
  27.   handle:close()
  28. end
  29.  
  30. -- Returns a table containing a list of keys from localCardsMain
  31. local function generateKeyList(localTable)
  32.   local keys = {}
  33.   for k,v in pairs(localTable) do
  34.     table.insert(keys,k)
  35.   end
  36.   table.sort(keys)
  37.   return keys
  38. end
  39.  
  40. -- Returns a 6-character string representing the level of a monster from the integer level
  41. local function getLevelString(level)
  42.   local levelString = string.rep(":",math.max(0,level-6)) .. string.rep(".",math.min(level,12-level))
  43.   return string.rep(" ",math.ceil((6-string.len(levelString))/2)) .. levelString .. string.rep(" ",math.floor((6-string.len(levelString))/2))
  44. end
  45.  
  46. -- Returns a table containing a wrapped version of the card lore
  47. local function wrapLore(lore)
  48.   currentCardData["wrappedLore"] = {}
  49.   for i = 1, #lore, 31 do
  50.     table.insert(currentCardData["wrappedLore"],string.sub(lore,i,i+30))
  51.   end
  52.   return currentCardData, #currentCardData["wrappedLore"]
  53. end
  54.  
  55. -- Call when the card index changes
  56. local function updateIndex()
  57.   -- Localise the current card's data
  58.   currentCardData = cardsMain[cardsMainKeys[currentCardIndex]]
  59.   -- Add a temporary field in currentCardData with a wrapped version of the lore text
  60.   currentCardData, wrappedLoreCount = wrapLore(currentCardData["lore"])
  61. end
  62.  
  63. -- Create a new card entry
  64. local function createNew()
  65.   cardsMain[1] = {
  66.     ["cardType"]="Monster",
  67.     ["name"]="New Card",
  68.     ["image"]={[1]={[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,},[2]={[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,},[3]={[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,},[4]={[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,},[5]={[1]=1,[2]=1,[3]=1,[4]=1,[5]=1,[6]=1,},},
  69.     ["lore"]="This is a new card.",
  70.     ["id"]=1,
  71.     ["level"]=1,
  72.     ["attribute"]="DARK",
  73.     ["type"]="Aqua",
  74.     ["atk"]=0,
  75.     ["def"]=0
  76.   }
  77.   cardsMainKeys = generateKeyList(cardsMain)
  78.   currentCardIndex = 1
  79.   updateIndex()
  80. end
  81.  
  82. -- Splits the card type into its components, i.e. "Monster/Synchro/Effect" to {"Monster","Synchro","Effect"}
  83. local function parseCardType(cardType)
  84.   local parsedType = {}
  85.   for word in string.gmatch(cardType,"[^/]+") do
  86.     table.insert(parsedType,word)
  87.   end
  88.   return parsedType
  89. end
  90.  
  91. -- Draws the screen
  92. local function draw(cardsMain,currentCardIndex)
  93.   term.clear() -- Clear screen, declare supporting tables and functions
  94.   local parsedType = parseCardType(currentCardData["cardType"])
  95.  
  96.   -- Set the text and background colour
  97.   term.setTextColor(colors.white)
  98.   term.setBackgroundColor(colors.black)
  99.  
  100.   -- Colour choosing based on the cardType, WIP
  101.   -- Use a metatable for normal monsters, i.e. nothing written next to the type
  102.   local metatable = {
  103.     __index = function(t,k)
  104.       return {colors.yellow, colors.yellow}
  105.     end
  106.   }
  107.   local monsterCardColours = {
  108.     ["Effect"] = {colors.orange, colors.yellow},
  109.     ["Ritual"] = {colors.lightBlue, colors.lightBlue},
  110.     ["Fusion"] = {colors.purple, colors.magenta},
  111.     ["Xyz"] = {colors.gray, colors.lightGray},
  112.     ["Synchro"] = {colors.white, colors.white}
  113.   }
  114.   local metaMonsterCardColours = setmetatable(monsterCardColours,metatable)
  115.  
  116.   local cardColours = {
  117.     ["Monster"] = metaMonsterCardColours[parsedType[2]],
  118.     ["Spell"] = {colors.cyan,colors.lightBlue},
  119.     ["Trap"] = {colors.magenta,colors.pink}
  120.   }
  121.  
  122.   -- Draw the backing of the card
  123.   for i = 2, 11 do
  124.     paintutils.drawLine(2,i,11,i,cardColours[parsedType[1]][1])
  125.   end
  126.  
  127.   -- Draw the backing of the image
  128.   for i = 3, 9 do
  129.     paintutils.drawLine(3,i,10,i,colors.gray)
  130.   end
  131.  
  132.   -- Draw the image itself
  133.   paintutils.drawImage(currentCardData["image"],4,4)
  134.  
  135.   -- Draw the level stars / spell/trap type
  136.   term.setBackgroundColor(cardColours[parsedType[1]][2])
  137.   term.setCursorPos(4,10)
  138.   if parsedType[1] == "Monster" then
  139.     term.setTextColor(colors.red)
  140.     write(getLevelString(currentCardData["level"]))
  141.     term.setTextColor(colors.white)
  142.   else
  143.     local substring = string.sub(parsedType[2] or "",1,6)
  144.     write(substring..string.rep(" ",6-#substring))
  145.   end
  146.   term.setBackgroundColor(colors.black)
  147.  
  148.   -- Draw the navigation arrows
  149.   term.setBackgroundColor(colors.lightGray)
  150.   term.setCursorPos(3,13)
  151.   write("<")
  152.   term.setCursorPos(5,13)
  153.   write(">")
  154.  
  155.   -- Draw the 'New' button
  156.   term.setCursorPos(8,13)
  157.   write("New")
  158.  
  159.   -- Draw the 'Import' button
  160.   term.setCursorPos(4,15)
  161.   write("Import")
  162.  
  163.   -- Draw the 'Save' button
  164.   term.setCursorPos(5,16)
  165.   write("Save")
  166.  
  167.   term.setBackgroundColor(colors.black)
  168.  
  169.   -- Draw card values
  170.   term.setCursorPos(14,2)
  171.   write("@Card Type: " .. currentCardData["cardType"])
  172.   term.setCursorPos(14,3)
  173.   write("@Name: " .. currentCardData["name"])
  174.   term.setCursorPos(14,4)
  175.   write("@Lore: ")
  176.   for k,v in pairs(currentCardData["wrappedLore"]) do
  177.     write(v)
  178.     term.setCursorPos(21,4+k)
  179.   end
  180.   term.setCursorPos(14,4+wrappedLoreCount)
  181.   write("@ID: " .. currentCardData["id"])
  182.   if parsedType[1] == "Monster" then
  183.     term.setCursorPos(14,5+wrappedLoreCount)
  184.     write("@Level: " .. currentCardData["level"])
  185.     term.setCursorPos(14,6+wrappedLoreCount)
  186.     write("@Attribute: " .. currentCardData["attribute"])
  187.     term.setCursorPos(14,7+wrappedLoreCount)
  188.     write("@Type: " .. currentCardData["type"])
  189.     term.setCursorPos(14,8+wrappedLoreCount)
  190.     write("@ATK: " .. currentCardData["atk"])
  191.     term.setCursorPos(14,9+wrappedLoreCount)
  192.     write("@DEF: " .. currentCardData["def"])
  193.   end
  194. end
  195.  
  196. -- Functions to be called when certain events occur
  197. local eventResponseTable = {
  198.   ["mouse_click"]=function(args)
  199.     local coordinateEvents = {
  200.       [{3,3,13,13}]=function() if currentCardIndex > 1 then currentCardIndex = currentCardIndex - 1 updateIndex() end end,
  201.       [{5,5,13,13}]=function() if currentCardIndex < #cardsMainKeys then currentCardIndex = currentCardIndex + 1 updateIndex() end end,
  202.       [{8,10,13,13}]=function() term.setCursorPos(2,18) write("Create a new entry? (Y/[N]): ") if string.upper(read()) == "Y" then createNew() end end,
  203.       [{4,11,15,15}]=function() term.setCursorPos(2,18) write("Image Path (Absolute): ") currentCardData["image"] = paintutils.loadImage(read()) end,
  204.       [{5,8,16,16}]=function() term.setCursorPos(2,18) write("Are you sure you want to save? (Y/[N]): ") if string.upper(read()) == "Y" then saveCardFile(cardsMain,shell.resolve(tArgs[1])) end end,
  205.       [{14,14,2,2}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["cardType"] = read() end,
  206.       [{14,14,3,3}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["name"] = read() end,
  207.       [{14,14,4,4}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["lore"] = read() currentCardData, wrappedLoreCount = wrapLore(currentCardData["lore"]) end,
  208.       [{14,14,4+wrappedLoreCount,4+wrappedLoreCount}]=function()
  209.         term.setCursorPos(2,18)
  210.         write("New Value: ")
  211.         local oldID = currentCardData["id"]
  212.         currentCardData["id"] = tonumber(read())
  213.         cardsMain[currentCardData["id"]] = currentCardData
  214.         cardsMain[oldID] = nil
  215.         cardsMainKeys = generateKeyList(cardsMain)
  216.       end,
  217.       [{14,14,5+wrappedLoreCount,5+wrappedLoreCount}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["level"] = tonumber(read()) end,
  218.       [{14,14,6+wrappedLoreCount,6+wrappedLoreCount}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["attribute"] = read() end,
  219.       [{14,14,7+wrappedLoreCount,7+wrappedLoreCount}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["type"] = read() end,
  220.       [{14,14,8+wrappedLoreCount,8+wrappedLoreCount}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["atk"] = tonumber(read()) end,
  221.       [{14,14,9+wrappedLoreCount,9+wrappedLoreCount}]=function() term.setCursorPos(2,18) write("New Value: ") currentCardData["def"] = tonumber(read()) end
  222.     }
  223.     if args[2] == 1 then
  224.       for k,v in pairs(coordinateEvents) do
  225.         if args[3] >= k[1] and args[3] <= k[2] and args[4] >= k[3] and args[4] <= k[4] then
  226.           v()
  227.           break
  228.         end
  229.       end
  230.     end
  231.   end
  232. }
  233.  
  234. -- Argument checking
  235. if #tArgs == 0 or fs.isDir(shell.resolve(tArgs[1])) then
  236.   print("Usage: " .. fs.getName(shell.getRunningProgram()) .. " <path>")
  237.   return
  238. end
  239.  
  240. -- Startup
  241. print("Checking card file...")
  242. if not fs.exists(shell.resolve(tArgs[1])) then
  243.   print("File does not exist! Creating...")
  244.   io.open(shell.resolve(tArgs[1]),"w"):close()
  245. end
  246. local handle = io.open(shell.resolve(tArgs[1]),"r")
  247. local totalContents = handle:read("*a")
  248. handle:close()
  249. if totalContents == "" then
  250.   print("File empty! A blank entry will be created.")
  251.   createNew()
  252. else
  253.   print("Card file good!")
  254.   print("Registering card file...")
  255.   cardsMain = registerCardFile(cardsMain,shell.resolve(tArgs[1]))
  256. end
  257. print("Generating key table...")
  258. cardsMainKeys = generateKeyList(cardsMain)
  259. print("Updating card index...")
  260. updateIndex()
  261.  
  262. -- Event capturing
  263. while true do
  264.   draw(cardsMain,currentCardIndex)
  265.   eventParameters = {os.pullEvent()}
  266.   -- Local variables are much faster than global variables and tables
  267.   -- Replace eventResponseTable[eventParameters[1]] with a local variable during this section
  268.   if eventResponseTable[eventParameters[1]] then
  269.     eventResponseTable[eventParameters[1]](eventParameters)
  270.   end
  271. end
  272.  
  273. -- Type: Monster = 1 Spell = 2 Trap = 3
  274.  
  275. -- Name:
  276.  
  277. -- Attribute: Dark = 1 Divine = 2 Earth = 3 Fire = 4 Light = 5 Water = 6 Wind = 7
  278.  
  279. -- If the card is a Monster card: Level
  280. -- If the card is a Trap or Spell card: Normal = 1 Continuous = 2 (Trap:Counter)(Spell:Equip) = 3 (Spell:Quick-Play) = 4 (Spell:Field) = 5 (Spell:Ritual) = 6
  281.  
  282. -- Image path
  283. -- Old card format:
  284. -- 1111111111
  285. -- 1777777771
  286. -- 17      71
  287. -- 17      71
  288. -- 17      71
  289. -- 17      71
  290. -- 17      71
  291. -- 1777777771
  292. -- 1  4444  1
  293. -- 1111111111
  294.  
  295. -- If the card is a Monster card: Aqua 1 Beast 2 Beast-Warrior 3 Creator God 4 Dinosaur 5 Divine-Beast 6 Dragon 7 Fairy 8 Fiend 9 Fish 10 Insect 11 Machine 12 Plant 13 Psychic 14 Pyro 15 Reptile 16 Rock 17 Sea Serpent 18 Spellcaster 19 Thunder 20 Warrior 21 Winged Beast 22 Zombie 23
  296.  
  297. -- Description
  298.  
  299. -- ATK and DEF
  300.  
  301. -- Number on bottom left is the card's ID (or limitation text)
Add Comment
Please, Sign In to add comment