サンプル:たいむぼむ

--[[
TIMEBOMB SCRIPT BY MARKIMUS
http://steamcommunity.com/sharedfiles/filedetails/?id=824318483
Last Edited: 10/01/2016
--]]

playerCardInfo = {}
playerCardInfo[4] = {roles={swat=3, terrorist=2}, wires={boom=1, success=4, safe=15}}
playerCardInfo[5] = {roles={swat=3, terrorist=2}, wires={boom=1, success=5, safe=19}}
playerCardInfo[6] = {roles={swat=4, terrorist=2}, wires={boom=1, success=6, safe=23}}
playerCardInfo[7] = {roles={swat=5, terrorist=3}, wires={boom=1, success=7, safe=27}}
playerCardInfo[8] = {roles={swat=5, terrorist=3}, wires={boom=1, success=8, safe=31}}

counters = {}
bags = {}

configureButton = nil
dealCardsButton = nil
collectCardsButton = nil
roleCardsLocator = nil
wireCardsLocator = nil
neutralCardsLocator = nil
successCardsLocator = nil

function onLoad()
    bags["BOOM!!"] = getObjectFromGUID("91342a")
    bags["SUCCESS"] = getObjectFromGUID("61acf7")
    bags["SAFE"] = getObjectFromGUID("d0fe4c")
    
    bags["SWAT"] = getObjectFromGUID("df53c6")
    bags["terrorist"] = getObjectFromGUID("4741d3")
    
    configureButton = getObjectFromGUID("35002b")
    dealCardsButton = getObjectFromGUID("8237e1")
    collectCardsButton = getObjectFromGUID("a40b5a")
    
    roleCardsLocator = getObjectFromGUID("91d52f")
    wireCardsLocator = getObjectFromGUID("a14623")
    neutralCardsLocator = getObjectFromGUID("95ae76")
    successCardsLocator = getObjectFromGUID("1dd905")
    
    counters["White"] = getObjectFromGUID("0ba4f9")
    counters["Pink"] = getObjectFromGUID("eb325e")
    counters["Purple"] = getObjectFromGUID("92720e")
    counters["Blue"] = getObjectFromGUID("d790cc")
    counters["Green"] = getObjectFromGUID("16d986")
    counters["Yellow"] = getObjectFromGUID("039902")
    counters["Orange"] = getObjectFromGUID("6cdb9c")
    counters["Red"] = getObjectFromGUID("88adcd")
    
    
    local butParams = {}
    butParams.width = 500
    butParams.height = 500
    butParams.font_size = 100
    butParams.position = {0, 0.22, 0}
    butParams.function_owner = nil
    
    butParams.label = "Replenish\nDecks"
    butParams.click_function = "replenishDecks"
    
    configureButton:createButton(butParams)
    
    butParams.font_size = 130
    butParams.label = "Deal\nCards"
    butParams.click_function = "dealCards"
    
    dealCardsButton:createButton(butParams)
    
    butParams.label = "Collect\nCards"
    butParams.click_function = "collectCards"
    
    collectCardsButton:createButton(butParams)
    
end

function findDeckOrCard(deckPos)
    local radius = 0.5
    local a = getAllObjects()
    for _, v in pairs(a) do
        if v.tag == "Deck" or v.tag == "Card" then
            local pos = v:getPosition()
            if math.abs(pos.x - deckPos.x) < radius and math.abs(pos.z - deckPos.z) < radius then
                return v
            end
        end
    end
end

function addCards(deckPos, bag, amount, rot)
    
    local pos = {x=deckPos.x, y=deckPos.y, z=deckPos.z}
    
    local takeParams = {}
    local function cb() end
    takeParams.callback = "cb"
    if rot ~= nil then
        takeParams.rotation = rot
    end
    
    for i = 1, amount do
        takeParams.position = pos
        bag:takeObject(takeParams)
        pos.y = pos.y + 0.1
    end
    
    return pos
end

 

function removeCards(deck, cardName, amount)
    local takeParams = {}
    local function cb() end
    takeParams.callback = "cb"
    takeParams.position = deck:getPosition()
    
    --takeParams.position.y = takeParams.position.y + 10
    
    local amDeleted = 0
    local a = deck:getObjects()
    for _, v in pairs(a) do
        if v.nickname == cardName then
            takeParams.guid = v.guid
            local c = deck:takeObject(takeParams)
            c:destruct()
            amDeleted = amDeleted + 1
            if amDeleted == amount then break end
        end
    end
    
end

function checkForCards(deck, cardNames)
    
    local cardNamesAmounts = {}
    for _, cn in pairs(cardNames) do
        cardNamesAmounts[cn] = 0
    end
    
    if deck.tag == "Deck" then
        local a = deck:getObjects()
        for _, v in pairs(a) do
            for _, cn in pairs(cardNames) do
                if v.nickname == cn then
                    cardNamesAmounts[cn] = cardNamesAmounts[cn] + 1
                    break
                end
            end
        end
    elseif deck.tag == "Card" then
        local n = deck:getName()
        if cardNamesAmounts[n] ~= nil then
            cardNamesAmounts[n] = 1
        end
    end
    
    return cardNamesAmounts
end

function replenishDecks(obj, col)
    if Player[col].admin == false then return end
    
    local amPlayers = #getSeatedPlayers()
    if playerCardInfo[amPlayers] == nil then return end
    
    local amounts = {}
    amounts["SWAT"] = playerCardInfo[amPlayers]["roles"]["swat"]
    amounts["terrorist"] = playerCardInfo[amPlayers]["roles"]["terrorist"]
    amounts["BOOM!!"] = playerCardInfo[amPlayers]["wires"]["boom"]
    amounts["SUCCESS"] = playerCardInfo[amPlayers]["wires"]["success"]
    amounts["SAFE"] = playerCardInfo[amPlayers]["wires"]["safe"]
    
    local roleCardsPosition = roleCardsLocator:getPosition()
    local wireCardsPosition = wireCardsLocator:getPosition()
    roleCardsPosition.y = roleCardsPosition.y + 0.5
    wireCardsPosition.y = wireCardsPosition.y + 0.5
    
    local roleCardsRotation = roleCardsLocator:getRotation()
    local wireCardsRotation = wireCardsLocator:getRotation()
    
    local roleCards = findDeckOrCard(roleCardsPosition)
    local wireCards = findDeckOrCard(wireCardsPosition)
    
    
    if roleCards ~= nil then
        local roleAmounts = checkForCards(roleCards, {"SWAT", "terrorist"})
        
        local currentPos = roleCardsPosition
        for index, am in pairs(roleAmounts) do
            if am > amounts[index] then --IF COUNT IS HIGHER THAN NORMAL
                removeCards(roleCards, index, (am - amounts[index]) )
            elseif am < amounts[index] then --IF COUNT IS LOWER THAN NORMAL
                currentPos = addCards(currentPos, bags[index], (amounts[index] - am), roleCardsRotation)
            end
        end
        
    else
        
        local a = addCards(roleCardsPosition, bags["SWAT"], amounts["SWAT"], roleCardsRotation)
        addCards(a, bags["terrorist"], amounts["terrorist"], roleCardsRotation)
        
    end
    
    if wireCards ~= nil then
    
        local wireAmounts = checkForCards(wireCards, {"BOOM!!", "SUCCESS", "SAFE"})
        
        local currentPos = wireCards:getPosition()
        for index, am in pairs(wireAmounts) do
            if am > amounts[index] then --IF COUNT IS HIGHER THAN NORMAL
                removeCards(wireCards, index, (am - amounts[index]) )
            elseif am < amounts[index] then --IF COUNT IS LOWER THAN NORMAL
                currentPos = addCards(currentPos, bags[index], (amounts[index] - am), wireCardsRotation)
            end
        end
        
    else
        
        local a = addCards(wireCardsPosition, bags["BOOM!!"], amounts["BOOM!!"], wireCardsRotation)
        local b = addCards(a, bags["SAFE"], amounts["SAFE"], wireCardsRotation)
        addCards(b, bags["SUCCESS"], amounts["SUCCESS"], wireCardsRotation)
        
    end
    
end

function getSpreadPositions(playerColour, amCards)
    local cardSpacing = 5
    local leftPoint = 0 - ((amCards-1) * (cardSpacing/2))
    
    local hand = Player[playerColour]:getPlayerHand()
    local posX = hand["pos_x"] + ( hand["trigger_forward_x"] * 10.5 )
    local posY = hand["pos_y"] - 2
    local posZ = hand["pos_z"] + ( hand["trigger_forward_z"] * 10.5 )
    
    local positions = {}
    for i = 1, amCards do
        local addedBit = leftPoint + ((i-1) * cardSpacing)
        local newPosX = (posX + hand["trigger_right_x"] * addedBit)
        local newPosZ = (posZ + hand["trigger_right_z"] * addedBit)
        
        --RETURN RELATIVE POSITIONS.
        positions[i] = {newPosX, posY, newPosZ}
    end
    
    return positions, hand["rot_y"]
end

function getSpreadOffsets(amCards, cardSpacing, amForward)
    if cardSpacing == nil then cardSpacing = 5 end
    if amForward == nil then amForward = 10.5 end
    
    local leftPoint = 0 - ((amCards-1) * (cardSpacing/2))
    
    local positions = {}
    for i = 1, amCards do
        positions[i] = {}
        positions[i][1] = leftPoint + (cardSpacing*(i-1))
        positions[i][2] = 0
        positions[i][3] = amForward
    end

    return positions
end

function getSeatedPlayersExceptBlack()
    local a = getSeatedPlayers()
    for i, v in pairs(a) do
        if v == "Black" then
            a[i] = nil
            return a
        end
    end
    return a
end

function getPlayerAmounts(deckAmount)
    
    local playerAmounts = {}
    local a = getSeatedPlayersExceptBlack()
    
    for _, col in pairs(a) do
        playerAmounts[col] = 0
    end
    
    while (deckAmount > 0) do
        for _, col in pairs(a) do
            if deckAmount > 0 then
                playerAmounts[col] = playerAmounts[col] + 1
                deckAmount = deckAmount - 1
            else
                break
            end
        end
    end
    
    return playerAmounts
end

function dealCards(obj, col)
    if Player[col].admin == false then return end
    local seatedPlayers = getSeatedPlayersExceptBlack()
    
    local wiresDeck = findDeckOrCard(wireCardsLocator:getPosition())
    local roleDeck = findDeckOrCard(roleCardsLocator:getPosition())
    
    for _, v in pairs(counters) do
        v:setValue(0)
    end
    
    if roleDeck ~= nil and roleDeck.tag == "Deck" then
        roleDeck:shuffle()
        for _, v in pairs(seatedPlayers) do
            roleDeck:dealToColor(1, v)
        end
    end
    
    if wiresDeck ~= nil then
        wiresDeck:shuffle()
        
        if #seatedPlayers == 0 then return end
        
        local deckObjects = wiresDeck:getObjects()
        local playerAmounts = getPlayerAmounts(#deckObjects)
        
        local allSpreadPositions = {}
        for _, v in pairs(seatedPlayers) do
            allSpreadPositions[v] = getSpreadOffsets(playerAmounts[col])
        end
        
        local cardInfo = {}
        for _, v in pairs(seatedPlayers) do
            cardInfo[v] = {}
        end
        
        function dealOutCards()
            seatedPlayerIndex = 0
            offsetIndex = 1
            for _, v in pairs(deckObjects) do
                seatedPlayerIndex = seatedPlayerIndex + 1
                if seatedPlayerIndex > #seatedPlayers then
                    seatedPlayerIndex = 1
                    offsetIndex = offsetIndex + 1
                end
                local myColour = seatedPlayers[seatedPlayerIndex]
                local myPosition = allSpreadPositions[myColour][offsetIndex]
                if myPosition ~= nil then
                    local card = nil
                    if wiresDeck == nil then
                        while card == nil do
                            card = findDeckOrCard(wireCardsLocator:getPosition())
                            if card == nil then
                                coroutine.yield()
                            end
                        end
                        local pos, rot = getSpreadPositions(myColour, offsetIndex)
                        local cardRot = card:getRotation()
                        card:setPositionSmooth(pos[offsetIndex])
                        card:setRotationSmooth({cardRot.x, rot, cardRot.y})
                    else
                        card = wiresDeck:dealToColorWithOffset(myPosition, false, myColour)
                    end
                    function rotateCard()
                        local rotY = Player[myColour]:getPlayerHand().rot_y + 90
                        for i = 1, 40 do
                            coroutine.yield()
                        end
                        local cardRot = card:getRotation()
                        card:setRotationSmooth({cardRot.x, rotY, cardRot.z})
                        return 1
                    end
                    startLuaCoroutine(nil, "rotateCard")
                    
                    local cardName = card:getName()
                    
                    local check = false
                    for _, x in pairs(cardInfo[myColour]) do
                        if x[1] == cardName then
                            x[2] = x[2] + 1
                            check = true
                            break
                        end
                    end
                    
                    if check == false then
                        table.insert(cardInfo[myColour], {cardName, 1})
                    end
                    
                    for i = 1, 5 do
                        coroutine.yield()
                    end
                end
            end
        
            for i, v in pairs(cardInfo) do
                printOutCardInfo(i, v)
            end
            
            return 1
        end
        
        startLuaCoroutine(nil, "dealOutCards")
    end--]]
end

function isCardFaceDown(card)
    local rot = card:getRotation().z
    if rot > 90 and rot < 270 then
        return true
    end
    return false
end

function printOutCardInfo(col, amounts)
    local function sort_func(a, b)
        return a[2] > b[2]
    end
    amounts = table.sort(amounts, sort_func)
    
    local textColour = stringColorToRGB(col)
    printToColor("", col, textColour)
    for _, v in pairs(amounts) do
        broadcastToColor(v[1] .." -> [FFFFFF]".. v[2] .. "[-]", col, textColour)
    end
    printToColor("", col, textColour)
end

function removeHandCards(tab)
    for _, col in pairs(getSeatedPlayers()) do
        for _, obj in pairs(Player[col]:getHandObjects()) do
            for i, v in pairs(tab) do
                if v == obj then
                    tab[i] = nil
                end
            end
        end
    end
end

function getRelativePosition(pos, rot, posDif)
    
    --CALCULATE X POSITION.
    local returnTable = {}
    if posDif.x ~= nil and posDif.x ~= 0 then
        
        returnTable.x = pos.x + math.sin( (90+rot)*0.0174532 ) * posDif.x
        returnTable.y = pos.y
        returnTable.z = pos.z + math.cos( (90+rot)*0.0174532 ) * posDif.x
    else
        returnTable = {x=pos.x, y=pos.y, z=pos.z}
    end
    
    --CALCULATE Z POSITION.
    local returnTable2 = {x=returnTable.x, y=returnTable.y, z=returnTable.z}
    if posDif.z ~= nil and posDif.z ~= 0 then
        
        returnTable2.x = returnTable.x + math.sin( rot*0.0174532 ) * posDif.z
        returnTable2.y = returnTable.y
        returnTable2.z = returnTable.z + math.cos( rot*0.0174532 ) * posDif.z
    end
    
    return returnTable2
end

function collectCards(obj, col)
    if Player[col].admin == false then return end
    
    local a = getAllObjects()
    removeHandCards(a)
    
    local successCards = {}
    for _, v in pairs(a) do
        if v.tag == "Card" and v:getName() == "SUCCESS" and isCardFaceDown(v) == false then
            table.insert(successCards, v)
        end
    end
    
    local spreadPositions = getSpreadOffsets(#successCards, 3.75, 3.75)
    local successCardsPosition = successCardsLocator:getPosition()
    local successCardsRotation = successCardsLocator:getRotation()
    
    for i, pos in ipairs(spreadPositions) do
        local tab = {x=pos[1], y=pos[2], z=pos[3]}
        spreadPositions[i] = getRelativePosition(successCardsPosition, successCardsRotation.y, tab)
    end
    
    successCardsRotation.y = successCardsRotation.y + 180
    for i, v in ipairs(successCards) do
        v:setPositionSmooth(spreadPositions[i])
        v:setRotationSmooth(successCardsRotation)
    end
    
    local wireCardsPosition = wireCardsLocator:getPosition()
    local wireCardsRotation = wireCardsLocator:getRotation()
    wireCardsPosition.y = wireCardsPosition.y + 0.5
    
    local neutralCardsPosition = neutralCardsLocator:getPosition()
    local neutralCardsRotation = neutralCardsLocator:getRotation()
    neutralCardsPosition.y = neutralCardsPosition.y + 0.5
    
    local roleCardsPosition = roleCardsLocator:getPosition()
    local roleCardsRotation = roleCardsLocator:getRotation()
    roleCardsPosition.y = roleCardsPosition.y + 0.5
    
    for _, v in pairs(a) do
        if v.tag == "Card" then
            local name = v:getName()
            if (name == "SAFE" or name == "BOOM!!" or name == "SUCCESS") then
                local checkFaceDown = isCardFaceDown(v)
                if checkFaceDown == true then
                    v:setPositionSmooth(wireCardsPosition)
                    v:setRotationSmooth(wireCardsRotation)
                    wireCardsPosition.y = wireCardsPosition.y + 0.1
                else
                    if name == "SAFE" then
                        v:setPositionSmooth(neutralCardsPosition)
                        v:setRotationSmooth(neutralCardsRotation)
                    elseif name == "BOOM!!" then
                        v:setPositionSmooth(wireCardsPosition)
                        v:setRotationSmooth(wireCardsRotation)
                        wireCardsPosition.y = wireCardsPosition.y + 0.1
                    end
                end
            elseif (name == "terrorist" or name == "SWAT") then
                v:setPositionSmooth(roleCardsPosition)
                v:setRotationSmooth(roleCardsRotation)
                roleCardsPosition.y = roleCardsPosition.y + 0.1
            end
        end
    end
end

タグ:

+ タグ編集
  • タグ:
最終更新:2019年10月07日 22:39