Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- +---------------------+------------+---------------------+
- -- | | | |
- -- | | Card API | |
- -- | | | |
- -- +---------------------+------------+---------------------+
- local version = "Version 0.3.0"
- -- A ComputerCraft API for card games, by Jeffrey Alexander (aka Bomb Bloke).
- -- http://www.computercraft.info/forums2/index.php?/topic/18480-bbcards
- ---------------------------------------------
- ------------Variable Declarations------------
- ---------------------------------------------
- local line = {{16,16,16,16,16},{16,16,17,16,16},{17,16,16,16,17},{17,16,17,16,17},{16,17,16,17,16}} -- Row templates for numbered cards.
- local numbered = {{2,1,1,1,2},{2,1,2,1,2},{3,1,1,1,3},{3,1,2,1,3},{3,1,3,1,3},{3,2,3,1,3}, -- Numbered cards, 2-10.
- {3,2,3,2,3},{4,1,4,1,4},{3,5,3,5,3}}
- local freeCell = {{12,7,12,7,12,7,12},{7,16,16,16,16,16,7},{12,16,16,16,16,16,12}, -- A free cell.
- {7,16,16,16,16,16,7},{12,16,16,16,16,16,12},{7,16,16,16,16,16,7},{12,7,12,7,12,7,12}}
- local ace, back, royal
- local value = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}
- local suit = {["colour"] = {colours.black,colours.red,colours.black,colours.red}}
- if _HOST then
- ace = {{{{32,159,131,144,32},{159,129,32,130,144},{129,32,32,32,130},{144,135,32,139,159},{32,136,143,132,32}},
- {"000f0","00f0f","0fff0","0ffff","0fff0"},{"0ff00","ffff0","fffff","f0f00","00000"}}, -- Spades.
- {{{159,139,32,135,144},{151,129,32,32,32},{146,32,32,32,159},{130,32,32,32,129},{32,130,143,129,32}},
- {"00006","60666","06666","66666","06660"},{"66060","06666","66660","06660","00000"}}, -- Hearts.
- {{{32,135,131,139,32},{159,139,32,135,144},{32,130,32,129,32},{139,129,32,130,135},{32,136,143,132,32}},
- {"00000","0ffff","f0f0f","fffff","0fff0"},{"0fff0","f0f00","fffff","00f00","00000"}}, -- Clubs.
- {{{32,159,131,144,32},{159,32,32,32,144},{32,32,32,32,32},{130,32,32,32,129},{32,130,143,129,32}},
- {"000e0","0eeee","eeeee","eeeee","0eee0"},{"0ee00","eeee0","eeeee","0eee0","00000"}}} -- Diamonds.
- for j = 1, #ace do
- local rows = ace[j][1]
- for i = 1, 5 do rows[i] = string.char(unpack(rows[i])) end
- end
- suit.symbol = {string.char(6),string.char(3),string.char(5),string.char(4)}
- -- Flipped cards (higher res):
- back = {{{{150,131,131,131,131,131,150},{149,129,135,131,139,130,149},{149,140,32,140,32,140,149,},{149,135,32,136,137,137,149},
- {149,159,129,32,32,32,149},{149,32,135,144,132,146,149},{154,143,143,143,143,143, 154}},
- {"5555554","5e111ee","50b0b0b","5b3fff3","5339999","599ccc4","4444445"},
- {"4411145","e144415","bbbbbb5","b333335","3999995","9944445","5555554"}},
- {{{150,131,131,131,131,131,150},{149,129,32,32,32,130,149},{149,129,131,131,131,130,149},{149,32,129,32,130,32,149},
- {149,32,144,32,159,32,149},{149,144,143,143,143,159,149},{154,143,143,143,143,143,154}},
- {"777777b","7300033","7011100","7414140","7414f40","7044440","b000007"},
- {"b33e337","3000007","0144417","04f4f47","04f4147","0411107","777777b"}},
- {{{150,131,131,131,131,131,150},{149,32,32,159,140,153,149},{149,32,143,143,143,32,149},{149,158,148,32,32,130,149},
- {149,138,129,32,32,32,149},{149,130,143,143,143,129,149},{154,143,143,143,143,143,154}},
- {"ffffff7","f777847","f77f777","ff8ff77","f80fff7","ffffff7","777777f"},
- {"777777f","777871f","77f7f7f","77fffff","7ffffff","777777f","ffffff7"}},
- {{{151,131,131,140,131,131,148},{149,129,32,32,32,130,149},{149,32,129,32,130,32,149},{149,32,136,32,132,32,149},
- {149,32,144,32,159,32,149},{149,144,32,32,32,159,149},{138,143,143,140,143,143,133}},
- {"888788e","8abbbae","8bb8bb7","8b88877","8bb87b7","8abbbbe","ee777ee"},
- {"ee787e8","ebbbbb8","7b787b8","7b78778","7b78bb8","ebbbba8","8888888"}},
- {{{150,131,131,131,131,131,150},{149,156,140,140,140,147,149},{149,149,153,32,32,149,149},{149,149,32,32,32,149,149},
- {149,138,143,143,143,133,149},{149,143,143,143,141,142,149},{154,143,143,143,143,143,154}},
- {"777777f","74444ff","740ffff","71fffff","7ffffff","744444f","ffffff7"},
- {"ffffff7","fffff47","fffff47","fffff17","f444447","ffffff7","777777f"}},
- {{{150,131,131,131,131,131,150},{149,129,130,143,129,130,149},{149,151,129,32,32,32,149},{149,130,32,32,32,32,149},
- {149,148,32,32,32,151,149},{149,32,139,32,135,32,149},{154,143,143,143,143,143,154}},
- {"2222226","2444446","2e0eeee","20eeeee","24eeee6","24eee46","6666662"},
- {"6666662","6eeeee2","e0eeee2","eeeeee2","6eeee42","644e442","2222226"}},
- {{{150,131,131,131,131,131,150},{149,158,32,140,32,146,149},{149,32,132,153,136,32,149},{149,145,153,140,153,157,149},
- {149,32,132,153,136,32,149},{149,146,32,140,32,158,149},{154,143,143,143,143,143,154}},
- {"777777b","79bbbbb","7bbbbb9","7b9bb3b","7bbbbb9","7bbbb9b","bbbbbb7"},
- {"bbbbbb7","bbb3b97","9b393b7","b3b39b7","9b393b7","b9b3bb7","777777b"}},
- {{{150,131,131,131,131,131,150},{149,159,131,131,131,144,149},{149,129,150,32,32,130,149},{149,139,143,131,143,135,149},
- {149,144,32,131,32,159,149},{149,130,143,143,143,129,149},{154,143,143,143,143,143,154}},
- {"ccccccb","cb9b9c9","c90ee9b","ceeeee9","c90700b","c800089","b9b9b9c"},
- {"b9b9b9c","9ceeebc","beeeeec","9ff7ffc","b00009c","9b9b9bc","ccccccb"}}}
- for j = 1, #back do
- local rows = back[j][1]
- for i = 1, 7 do rows[i] = string.char(unpack(rows[i])) end
- end
- royal = {{{{144,146,130,32,32},{159,129,32,135,32},{129,129,133,132,32},{132,132,131,159,144},{144,131,131,159,129}},
- {"0e000","0e440","04f60","0a60e","03344"},{"ece00","44400","46600","fff46","14100"}}, -- Jack.
- {{{144,139,32,32,32},{159,129,130,144,32},{159,151,133,132,32},{151,132,131,159,144},{144,131,131,159,129}},
- {"00000","000e0","ecf60","c8602","0aadd"},{"44000","eee00","c6600","fffd6","bdb00"}}, -- Queen.
- {{{146,143,158,32,32},{129,133,133,32,32},{144,129,133,132,32},{136,130,131,159,144},{144,131,131,159,129}},
- {"0e400","08800","07f60","9660a","01133"},{"44000","77700","76600","fff36","e3e00"}}} -- King.
- for j = 1, #royal do
- local rows = royal[j][1]
- for i = 1, 5 do rows[i] = string.char(unpack(rows[i])) end
- end
- else
- ace = {{{16,16,15,16,16},{16,15,15,15,16},{15,15,15,15,15},{15,16,15,16,15},{16,16,15,16,16}}, -- Spades.
- {{16,6,16,6,16},{6,6,6,6,6},{6,6,6,6,6},{16,6,6,6,16},{16,16,6,16,16}}, -- Hearts.
- {{16,7,7,7,16},{16,16,7,16,16},{7,16,7,16,7},{7,7,7,7,7},{16,16,7,16,16}}, -- Clubs.
- {{16,16,14,16,16},{16,14,14,14,16},{14,14,14,14,14},{16,14,14,14,16},{16,16,14,16,16}}} -- Diamonds.
- suit.symbol = {"^","v","+","<"}
- -- Flipped cards (lower res):
- back = {{"bbbbbbb","b3b3b3b","bb3b3bb","b3b3b3b","bb3b3bb","b3b3b3b","bbbbbbb"},
- {"4111114","e14441e","bbbbbbb","b333333","3399999","9944444","4444444"},
- {"ffeeeff","0000000","0444440","04f4f40","04f4f40","0444440","0000000"},
- {"8888888","8f0fff8","8ff0ff8","8f0fff8","8fffff8","8fffff8","8888888"},
- {"8d5d8d5","5ffdff8","dffdffc","5ddfd5d","d8fffdd","8dfdfd8","5cddd5d"},
- {"9977799","9bbbbb9","7b777b7","7b77777","7b777b7","9bbbbb9","9977799"},
- {"77778e7","7778777","77fff77","7fffff7","7fffff7","77fff77","7777777"},
- {"7ee7ee7","e0eeeee","eeeeeee","eeeeeee","7eeeee7","77eee77","777e777"}}
- for i = 1, #back do for y = 1, 7 do
- local temp = {}
- for x = 1, 7 do if back[i][y]:byte(x) < 58 then temp[x] = back[i][y]:byte(x) - 48 else temp[x] = back[i][y]:byte(x) - 87 end end
- back[i][y] = temp
- end end
- royal = {{{14,14,14,16,16},{16,4,4,16,16},{4,6,6,16,17},{15,15,15,16,14},{1,4,1,4,16}}, -- Jack.
- {{4,4,16,16,16},{16,14,14,16,16},{14,6,6,16,17},{15,15,15,16,4},{11,13,11,13,16}}, -- Queen.
- {{4,4,4,16,16},{7,7,7,16,16},{7,6,6,16,17},{15,15,15,16,10},{14,3,14,3,16}}} -- King.
- end
- local dealtCards, cell, cellWindow = 0, {}, {}
- local deck, cardBack, selected, resizeCell
- local outline ={{156,140,140,140,140,140,148},{149, 32, 32, 32, 32, 32,149},{149, 32, 32, 32, 32, 32,149},{149, 32, 32, 32, 32, 32,149},
- {149, 32, 32, 32, 32, 32,149},{149, 32, 32, 32, 32, 32,149},{141,140,140,140,140,140,133}}
- for y = 1, 7 do for x = 1, 7 do outline[y][x] = string.char(outline[y][x]) end end
- local BGColour, CardBGColour, selectionColour = colours.green, colours.white, colours.cyan
- ---------------------------------------------
- ------------ Exposed Variables ------------
- ---------------------------------------------
- spade, heart, club, diamond, spades, hearts, clubs, diamonds, straight, up, left, down, right = 1, 2, 3, 4, 1, 2, 3, 4, 0, 1, 2, 3, 4
- victoryPile, drawPile, discardPile, temporary = 1, 2, 3, 4
- ---------------------------------------------
- ------------Function Declarations------------
- ---------------------------------------------
- local function drawSelectionRectangle()
- local cellX1, cellY1 = cellWindow[deck[selected].cell].xPos, cellWindow[deck[selected].cell].yPos
- local cellX2, cellY2 = cellX1 + cellWindow[deck[selected].cell].width, cellY1 + cellWindow[deck[selected].cell].height
- paintutils.drawLine(cellX1+deck[selected].xPos-2, cellY1+deck[selected].yPos-2, cellX2, cellY1+deck[selected].yPos-2, selectionColour)
- paintutils.drawLine(cellX1+deck[selected].xPos-2, cellY1+deck[selected].yPos-1, cellX1+deck[selected].xPos-2, cellY2-1, selectionColour)
- paintutils.drawLine(cellX2, cellY1+deck[selected].yPos-1, cellX2, cellY2-1, selectionColour)
- paintutils.drawLine(cellX1+deck[selected].xPos-2, cellY2, cellX2, cellY2, selectionColour)
- end
- local function drawCell(targetCell, xMin, yMin, xMax, yMax)
- if not cellWindow[targetCell].visible then return end
- xMin = xMin or 1
- yMin = yMin or 1
- xMax = xMax or 7
- yMax = yMax or 7
- resizeCell(targetCell,7,7)
- if _HOST then term.setBackgroundColor(BGColour) end
- for y = yMin, yMax do
- term.setCursorPos(cellWindow[targetCell].xPos+xMin-1,cellWindow[targetCell].yPos+y-1)
- for x = xMin, xMax do
- if _HOST then
- term.setTextColor(freeCell[y][x] == 16 and BGColour or bit.blshift(1,freeCell[y][x]))
- else
- term.setBackgroundColor(freeCell[y][x] == 16 and BGColour or bit.blshift(1,freeCell[y][x]))
- end
- term.write(_HOST and outline[y][x] or " ")
- end
- end
- end
- function renderCard(cardValue, cardSuit, xPos, yPos, cardFaceDown)
- if cardFaceDown then
- local back = back[cardBack]
- for y = 1, 7 do
- term.setCursorPos(xPos,yPos+y-1)
- if _HOST then
- term.blit(back[1][y], back[2][y], back[3][y])
- else
- local row = back[y]
- for x = 1, 7 do
- term.setBackgroundColor(bit.blshift(1,row[x]))
- term.write(" ")
- end
- end
- end
- return
- end
- local side = suit.symbol[cardSuit]..value[cardValue]..string.rep(" ",6-#value[cardValue])
- term.setTextColor(suit.colour[cardSuit])
- term.setBackgroundColor(CardBGColour)
- for y = 1, 7 do
- term.setCursorPos(xPos,yPos+y-1)
- for x = 1, 7 do if y == 1 then
- term.write(side)
- break
- elseif y == 7 then
- term.write(side:reverse())
- break
- else if x == 1 then
- term.write(side:sub(y,y))
- elseif x == 7 then
- term.write(side:reverse():sub(y,y))
- else if cardValue == 1 then
- local ace = ace[cardSuit]
- if _HOST then
- term.blit(ace[1][y-1]:sub(x-1,x-1),ace[2][y-1]:sub(x-1,x-1),ace[3][y-1]:sub(x-1,x-1))
- else
- if ace[y-1][x-1] ~= 16 then
- term.setBackgroundColor(bit.blshift(1,ace[y-1][x-1]))
- term.write(" ")
- term.setBackgroundColor(CardBGColour)
- else term.write(" ") end
- end
- elseif cardValue > 10 then
- local royal = royal[cardValue-10]
- if _HOST then
- if x == 6 and y == 4 then
- term.write(suit.symbol[cardSuit])
- else term.blit(royal[1][y-1]:sub(x-1,x-1),royal[2][y-1]:sub(x-1,x-1),royal[3][y-1]:sub(x-1,x-1)) end
- else
- if royal[y-1][x-1] < 17 then
- if royal[y-1][x-1] < 16 then
- term.setBackgroundColor(bit.blshift(1,royal[y-1][x-1]))
- term.write(" ")
- term.setBackgroundColor(CardBGColour)
- else term.write(" ") end
- else term.write(suit.symbol[cardSuit]) end
- end
- else term.write(line[numbered[cardValue-1][y-1]][x-1] == 16 and " " or suit.symbol[cardSuit]) end end end end
- end
- end
- local function drawCard(card, xMin, yMin, xMax, yMax)
- if not cellWindow[deck[card].cell].visible then return end
- xMin = xMin or 1
- yMin = yMin or 1
- xMax = xMax or 7
- yMax = yMax or 7
- if deck[card].faceDown then
- local back = back[cardBack]
- for y = yMin, yMax do
- term.setCursorPos(cellWindow[deck[card].cell].xPos+deck[card].xPos+xMin-2,cellWindow[deck[card].cell].yPos+deck[card].yPos+y-2)
- if _HOST then
- term.blit(back[1][y]:sub(xMin, xMax), back[2][y]:sub(xMin, xMax), back[3][y]:sub(xMin, xMax))
- else
- local row = back[y]
- for x = xMin, xMax do
- term.setBackgroundColor(bit.blshift(1,row[x]))
- term.write(" ")
- end
- end
- end
- return
- end
- local side = suit.symbol[deck[card].suit]..value[deck[card].value]..string.rep(" ",6-#value[deck[card].value])
- term.setTextColor(suit.colour[deck[card].suit])
- term.setBackgroundColor(CardBGColour)
- for y = yMin, yMax do
- term.setCursorPos(cellWindow[deck[card].cell].xPos+deck[card].xPos+xMin-2,cellWindow[deck[card].cell].yPos+deck[card].yPos+y-2)
- for x = xMin, xMax do if y == 1 then
- term.write(side:sub(xMin,xMax))
- break
- elseif y == 7 then
- term.write(side:reverse():sub(xMin,xMax))
- break
- else if x == 1 then
- term.write(side:sub(y,y))
- elseif x == 7 then
- term.write(side:reverse():sub(y,y))
- else if deck[card].value == 1 then
- local ace = ace[deck[card].suit]
- if _HOST then
- term.blit(ace[1][y-1]:sub(x-1,x-1),ace[2][y-1]:sub(x-1,x-1),ace[3][y-1]:sub(x-1,x-1))
- else
- if ace[y-1][x-1] ~= 16 then
- term.setBackgroundColor(bit.blshift(1,ace[y-1][x-1]))
- term.write(" ")
- term.setBackgroundColor(CardBGColour)
- else term.write(" ") end
- end
- elseif deck[card].value > 10 then
- local royal = royal[deck[card].value-10]
- if _HOST then
- if x == 6 and y == 4 then
- term.write(suit.symbol[deck[card].suit])
- else term.blit(royal[1][y-1]:sub(x-1,x-1),royal[2][y-1]:sub(x-1,x-1),royal[3][y-1]:sub(x-1,x-1)) end
- else
- if royal[y-1][x-1] < 17 then
- if royal[y-1][x-1] < 16 then
- term.setBackgroundColor(bit.blshift(1,royal[y-1][x-1]))
- term.write(" ")
- term.setBackgroundColor(CardBGColour)
- else term.write(" ") end
- else term.write(suit.symbol[deck[card].suit]) end
- end
- else term.write(line[numbered[deck[card].value-1][y-1]][x-1] == 16 and " " or suit.symbol[deck[card].suit]) end end end end
- end
- end
- local function redrawCell(targetCell)
- if not cellWindow[targetCell].visible then return end
- if cell[targetCell].visibleCards > 0 then
- local temp = cell[targetCell].topCard
- for j = 1, cell[targetCell].visibleCards do
- drawCard(temp,1,1,cell[targetCell].stackDirection == left and 1 or 7, cell[targetCell].stackDirection == down and 1 or 7)
- temp = deck[temp].onCard
- end
- if cell[targetCell].stackDirection ~= straight then drawCard(cell[targetCell].topCard,cell[targetCell].stackDirection == left and 2 or 1, cell[targetCell].stackDirection == down and 2 or 1, 7, 7) end
- else drawCell(targetCell) end
- end
- function redraw(x1, y1, x2, y2, ignoreSelected)
- term.setBackgroundColor(BGColour)
- if not x1 then
- term.clear()
- for i = 1, #cell do redrawCell(i) end
- if selected and not ignoreSelected then drawSelectionRectangle() end
- return
- end
- for y = y1, y2 do
- term.setCursorPos(x1,y)
- term.write(string.rep(" ", x2 - x1 + 1))
- end
- for i = 1, #cell do if not (x1 > cellWindow[i].xPos + cellWindow[i].width - 1 or x2 < cellWindow[i].xPos or y1 > cellWindow[i].yPos + cellWindow[i].height - 1 or y2 < cellWindow[i].yPos) then
- redrawCell(i)
- if selected and deck[selected].cell == i and not ignoreSelected then drawSelectionRectangle() end
- end end
- end
- function resizeCell(targetCell, width, height) -- Actually local via forward declaration.
- local oldWidth, oldHeight = cellWindow[targetCell].width, cellWindow[targetCell].height
- cellWindow[targetCell].width, cellWindow[targetCell].height = width, height
- if width < oldWidth then redraw(cellWindow[targetCell].xPos+width, cellWindow[targetCell].yPos, cellWindow[targetCell].xPos+oldWidth-1, cellWindow[targetCell].yPos+height-1) end
- if height < oldHeight then redraw(cellWindow[targetCell].xPos, cellWindow[targetCell].yPos+height, cellWindow[targetCell].xPos+oldWidth-1, cellWindow[targetCell].yPos+oldHeight-1) end
- end
- function shuffle()
- local pile, temp = {}
- deck = {}
- cardBack = math.random(#back)
- dealtCards = 0
- for i = 1, 52 do pile[i] = i-1 end
- for i = 1, 52 do
- temp = math.random(#pile)
- deck[i] = {["value"] = pile[temp]%13+1,
- ["suit"] = math.floor(pile[temp]/13)+1,
- ["faceDown"] = false}
- table.remove(pile,temp)
- end
- term.setBackgroundColor(BGColour)
- term.clear()
- selected = nil
- for i = 1, #cell do
- cell[i].visibleCards = 0
- cell[i].topCard = nil
- drawCell(i)
- end
- end
- function reset()
- cell = {}
- shuffle()
- end
- function displayCell(targetCell, visible)
- cellWindow[targetCell].visible = visible
- if visible then redrawCell(targetCell) end
- end
- function dealCard(targetCell, faceDown)
- local thisCard = 52 - dealtCards
- deck[thisCard].cell = targetCell
- deck[thisCard].faceDown = faceDown
- if cell[targetCell].topCard then
- deck[thisCard].onCard = cell[targetCell].topCard
- deck[cell[targetCell].topCard].underCard = thisCard
- end
- cell[targetCell].topCard = thisCard
- cell[targetCell].visibleCards = cell[targetCell].stackDirection == straight and 1 or (cell[targetCell].visibleCards + 1)
- dealtCards = dealtCards + 1
- if deck[thisCard].onCard and cell[targetCell].stackDirection > 0 then
- if cell[targetCell].stackDirection == left then
- deck[thisCard].xPos = deck[deck[thisCard].onCard].xPos+1
- deck[thisCard].yPos = deck[deck[thisCard].onCard].yPos
- resizeCell(targetCell,cell[targetCell].visibleCards+6,7)
- else
- deck[thisCard].xPos = deck[deck[thisCard].onCard].xPos
- deck[thisCard].yPos = deck[deck[thisCard].onCard].yPos+1
- resizeCell(targetCell,7,cell[targetCell].visibleCards+6)
- end
- else deck[thisCard].xPos, deck[thisCard].yPos = 1, 1 end
- drawCard(thisCard)
- end
- function getRemainingDeckSize() return 52 - dealtCards end
- function save(gameType)
- local cellPositions = {}
- for i=1,#cellWindow do cellPositions[i] = {cellWindow[i].xPos, cellWindow[i].yPos} end
- local saveData = {gameType,deck,cell,cellPositions}
- local saveFile = fs.open("bbcards.sav","w")
- saveFile.writeLine(textutils.serialize(saveData))
- saveFile.close()
- end
- function load()
- local saveFile = fs.open("bbcards.sav","r")
- local saveData = textutils.unserialize(saveFile.readAll())
- saveFile.close()
- deck = saveData[2]
- cell = saveData[3]
- local cellPositions, temp = saveData[4]
- term.setBackgroundColor(BGColour)
- term.clear()
- selected = nil
- cellWindow = {}
- for i = 1, #cell do
- cellWindow[i] = {["xPos"] = cellPositions[i][1],
- ["yPos"] = cellPositions[i][2],
- ["width"] = cell[i].stackDirection == left and (6 + cell[i].visibleCards) or 7,
- ["height"] = cell[i].stackDirection == down and (6 + cell[i].visibleCards) or 7,
- ["visible"] = true}
- redrawCell(i)
- end
- return saveData[1]
- end
- function straightenCell(targetCell)
- if cell[targetCell].visibleCards > 1 then
- local curCard = cell[targetCell].topCard
- for i=1,cell[targetCell].visibleCards-1 do
- deck[curCard].xPos = 1
- deck[curCard].yPos = 1
- curCard = deck[curCard].onCard
- end
- drawCard(cell[targetCell].topCard)
- cell[targetCell].visibleCards = 1
- resizeCell(targetCell,7,7)
- end
- end
- function defineCell(xPos, yPos, stackDirection, cellType, param1, param2)
- local thisCell = #cell+1
- cell[thisCell] = {}
- cell[thisCell].visibleCards = 0
- cellWindow[thisCell] = {["xPos"] = xPos, ["yPos"] = yPos, ["width"]= 7, ["height"] = 7, ["visible"] = true}
- if cellType then
- cell[thisCell].special = cellType
- if cellType == drawPile then
- cell[thisCell].flipTo = param1
- cell[thisCell].flipAmount = param2
- elseif cellType == temporary then
- cell[thisCell].visible = false
- end
- end
- drawCell(thisCell)
- -- 0 = Straight, 2 = Left, 3 = Down. Up / Right aren't available.
- if stackDirection then
- if stackDirection ~= straight and stackDirection ~= left and stackDirection ~= down then
- error("Invalid cell direction: Use either card.straight (0), card.left (2) or card.down (3).",2)
- end
- cell[thisCell].stackDirection = stackDirection
- else cell[thisCell].stackDirection = straight end
- end
- local function shiftCards(thisCard, targetCell, faceDown)
- local fromCell, amountMoving = deck[thisCard].cell
- if cell[fromCell].visibleCards > 1 then
- if cell[fromCell].stackDirection == left then
- amountMoving = cell[fromCell].visibleCards - deck[thisCard].xPos + 1
- resizeCell(fromCell,cell[fromCell].visibleCards>amountMoving and (6+cell[fromCell].visibleCards-amountMoving) or 7,7)
- else
- amountMoving = cell[fromCell].visibleCards - deck[thisCard].yPos + 1
- resizeCell(fromCell,7,cell[fromCell].visibleCards>amountMoving and (6+cell[fromCell].visibleCards-amountMoving) or 7)
- end
- else amountMoving = 1 end
- cell[fromCell].visibleCards = cell[fromCell].visibleCards - amountMoving
- if deck[thisCard].onCard then
- cell[fromCell].topCard = deck[thisCard].onCard
- deck[cell[fromCell].topCard].underCard = nil
- if cell[fromCell].visibleCards == 0 then
- drawCard(cell[fromCell].topCard)
- cell[fromCell].visibleCards = 1
- else if cell[fromCell].stackDirection == left then drawCard(cell[fromCell].topCard,2) else drawCard(cell[fromCell].topCard,1,2) end end
- else
- cell[fromCell].topCard = nil
- drawCell(deck[thisCard].cell)
- end
- deck[thisCard].onCard = cell[targetCell].topCard
- if cell[targetCell].topCard then deck[cell[targetCell].topCard].underCard = thisCard end
- if cell[targetCell].stackDirection ~= straight then
- cell[targetCell].visibleCards = cell[targetCell].visibleCards + amountMoving
- if cell[targetCell].topCard or amountMoving > 1 then if cell[targetCell].stackDirection == left then resizeCell(targetCell,cell[targetCell].visibleCards+6,7) else resizeCell(targetCell,7,cell[targetCell].visibleCards+6) end end
- else cell[targetCell].visibleCards = 1 end
- for i = 1, amountMoving do
- deck[thisCard].cell = targetCell
- if type(faceDown) == "boolean" then deck[thisCard].faceDown = faceDown end
- if cell[targetCell].stackDirection == left then
- deck[thisCard].xPos = deck[thisCard].onCard and (deck[deck[thisCard].onCard].xPos+1) or 1
- deck[thisCard].yPos = deck[thisCard].onCard and (deck[deck[thisCard].onCard].yPos) or 1
- drawCard(thisCard,1,1,1)
- elseif cell[targetCell].stackDirection == down then
- deck[thisCard].xPos = deck[thisCard].onCard and (deck[deck[thisCard].onCard].xPos) or 1
- deck[thisCard].yPos = deck[thisCard].onCard and (deck[deck[thisCard].onCard].yPos+1) or 1
- drawCard(thisCard,1,1,7,1)
- else
- deck[thisCard].xPos = 1
- deck[thisCard].yPos = 1
- end
- if deck[thisCard].underCard then thisCard = deck[thisCard].underCard end
- end
- if cell[targetCell].stackDirection == left then drawCard(thisCard,2) elseif cell[targetCell].stackDirection == down then drawCard(thisCard,1,2) else drawCard(thisCard) end
- cell[targetCell].topCard = thisCard
- end
- function moveTo(thisCard, targetCell, faceDown, skipAnimation)
- if not skipAnimation then
- local myTimer, oldCell, myEvent = os.startTimer(0.05), deck[thisCard].cell
- local xStart, yStart, xDes, yDes = cellWindow[oldCell].xPos + deck[thisCard].xPos - 1, cellWindow[oldCell].yPos + deck[thisCard].yPos - 1
- if cell[targetCell].stackDirection ~= straight and cell[targetCell].topCard then
- if cell[targetCell].stackDirection == left then
- xDes, yDes = cellWindow[targetCell].xPos + deck[cell[targetCell].topCard].xPos - 1, cellWindow[targetCell].yPos
- else xDes, yDes = cellWindow[targetCell].xPos, cellWindow[targetCell].yPos + deck[cell[targetCell].topCard].yPos - 1 end
- else xDes, yDes = cellWindow[targetCell].xPos, cellWindow[targetCell].yPos end
- local xPos1 = math.floor(xStart + (xDes - xStart) / 3)
- local yPos1 = math.floor(yStart + (yDes - yStart) / 3)
- local xPos2 = math.floor(xStart + (xDes - xStart) * 2 / 3)
- local yPos2 = math.floor(yStart + (yDes - yStart) * 2 / 3)
- defineCell(xPos1, yPos1, cell[oldCell].stackDirection, temporary)
- shiftCards(thisCard, #cell, faceDown)
- local width = cell[#cell].visibleCards + 5
- displayCell(#cell, true)
- repeat myEvent = {os.pullEvent()} until myEvent[1] == "timer" and myEvent[2] == myTimer
- myTimer = os.startTimer(0.05)
- cellWindow[#cell].xPos = xPos2
- cellWindow[#cell].yPos = yPos2
- cellWindow[#cell].stackDirection = cell[targetCell].stackDirection
- if cell[targetCell].stackDirection == straight then cell[#cell].visibleCards = 1 end
- resizeCell(#cell, 1 + (cell[targetCell].stackDirection == left and width or 6), 1 + (cell[targetCell].stackDirection == down and width or 6))
- redrawCell(#cell)
- redraw(xPos1, yPos1, xPos1 + (cell[oldCell].stackDirection == left and width or 6), yPos1 + (cell[oldCell].stackDirection == down and width or 6))
- repeat myEvent = {os.pullEvent()} until myEvent[1] == "timer" and myEvent[2] == myTimer
- displayCell(#cell, false)
- shiftCards(thisCard, targetCell, faceDown)
- cell[#cell] = nil
- redraw(xPos2, yPos2, xPos2 + (cell[targetCell].stackDirection == left and width or 6), yPos2 + (cell[targetCell].stackDirection == down and width or 6))
- else shiftCards(thisCard, targetCell, faceDown) end
- end
- function select(targetCard, hidden)
- selected = targetCard
- if not hidden then drawSelectionRectangle() end
- end
- function getSelected() return selected end
- function deselect()
- if selected then
- local cellX, cellY = cellWindow[deck[selected].cell].xPos, cellWindow[deck[selected].cell].yPos
- redraw(cellX+deck[selected].xPos-2,cellY+deck[selected].yPos-2,cellX+cellWindow[deck[selected].cell].width,cellY+cellWindow[deck[selected].cell].height,true)
- selected = nil
- end
- end
- function getSpecial(targetCell) return cell[targetCell].special end
- function getTopCard(targetCell) return cell[targetCell].topCard end
- function getTarget(targetCell) return cell[targetCell].flipTo end
- function getFlipAmount(targetCell) return cell[targetCell].flipAmount end
- function getValue(targetCard) return deck[targetCard].value end
- function getSuit(targetCard) return deck[targetCard].suit end
- function isFaceDown(targetCard) return deck[targetCard].faceDown end
- function setFaceDown(targetCard, faceDown)
- deck[targetCard].faceDown = faceDown
- drawCard(targetCard)
- end
- function getHigherCard(targetCard) return deck[targetCard].underCard end
- function getLowerCard(targetCard) return deck[targetCard].onCard end
- function checkClick(xPos,yPos)
- local cellX1, cellY1, cellX2, cellY2
- for thisCell = 1, #cell do
- cellX1, cellY1 = cellWindow[thisCell].xPos, cellWindow[thisCell].yPos
- cellX2, cellY2 = cellX1 + cellWindow[thisCell].width - 1, cellY1 + cellWindow[thisCell].height - 1
- if xPos >= cellX1 and xPos <= cellX2 and yPos >= cellY1 and yPos <= cellY2 then
- local thisCard = cell[thisCell].topCard
- if cell[thisCell].visibleCards > 1 then
- if cell[thisCell].stackDirection == left and xPos < cellX2 - 6 then
- for i = 1, cellX2-xPos-6 do thisCard = deck[thisCard].onCard end
- elseif cell[thisCell].stackDirection == down and yPos < cellY2 - 6 then
- for i = 1, cellY2-yPos-6 do thisCard = deck[thisCard].onCard end
- end
- end
- return thisCard, thisCell
- end
- end
- end
- function victoryAnimation(monSide)
- local curCard, counter, xDir, yDir, myEvent = {}, 1, -(math.random(15) / 10 + 0.5), math.random(30) / 10
- local xSize, ySize = term.getSize()
- for i = 1, #cell do if cell[i].special == victoryPile then curCard[#curCard+1] = getTopCard(i) end end
- local xPos, yPos = cellWindow[deck[curCard[counter]].cell].xPos + deck[curCard[counter]].xPos - 1, cellWindow[deck[curCard[counter]].cell].yPos + deck[curCard[counter]].yPos - 1
- local myTimer = os.startTimer(0.05)
- while true do
- if xPos < -6 or xPos > xSize then
- curCard[counter] = card.getLowerCard(curCard[counter])
- counter = counter == #curCard and 1 or (counter + 1)
- if not curCard[counter] then return end
- xPos, yPos = cellWindow[deck[curCard[counter]].cell].xPos + deck[curCard[counter]].xPos - 1, cellWindow[deck[curCard[counter]].cell].yPos + deck[curCard[counter]].yPos - 1
- xDir, yDir = -(math.random(15) / 10 + 0.5), math.random(30) / 10
- if math.random(4) == 4 then xDir = -xDir end
- if math.random(4) == 4 then yDir = -yDir end
- end
- myEvent = {os.pullEvent()}
- if myEvent[1] == "timer" and myEvent[2] == myTimer then
- renderCard(getValue(curCard[counter]), getSuit(curCard[counter]), math.floor(xPos), yPos > ySize - 6 and ySize - 6 or math.floor(yPos))
- xPos, yPos = xPos + xDir, yPos + yDir
- if yPos > ySize - 6 then yDir = -yDir else if yDir > 0 then yDir = yDir + 0.5 else yDir = yDir + 0.7 end end
- myTimer = os.startTimer(0.05)
- elseif myEvent[1] == "key" or myEvent[1] == "mouse_click" or (monSide and myEvent[1] == "monitor_touch" and monSide == myEvent[2]) then
- return
- end
- end
- end
- ---------------------------------------------
- ------------ Init ------------
- ---------------------------------------------
- if fs.exists("\\cards") and fs.isDir("\\cards") then
- local fileList, myLine = fs.list("\\cards")
- for i = 1, #fileList do if not fs.isDir("\\cards\\"..fileList[i]) then
- local myFile = fs.open("\\cards\\"..fileList[i],"r")
- back[#back+1] = {}
- for y = 1, 7 do
- back[#back][y] = {}
- myLine = myFile.readLine()
- for x = 1, 7 do if myLine:byte(x) < 58 then back[#back][y][x] = myLine:byte(x) - 48 else back[#back][y][x] = myLine:byte(x) - 87 end end
- end
- myFile.close()
- end end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement