Ronit.GAMES
Nigdy nie dokończony Kościany Poker :) - Printable Version

+- Ronit.GAMES (https://forum.ronit.games)
+-- Forum: RonEngine Support / Wsparcie dla RonEngine (https://forum.ronit.games/forumdisplay.php?fid=18)
+--- Forum: LUA/XML Scripting / Skryptowanie LUA/XML (https://forum.ronit.games/forumdisplay.php?fid=22)
+--- Thread: Nigdy nie dokończony Kościany Poker :) (/showthread.php?tid=234)



Nigdy nie dokończony Kościany Poker :) - Meritus - 01-12-2010

Ogólna koncepcja (Schemat Blokowy)

www.ronit.pl/images/ProjektKoscianyPoker.jpg

Algorytm Wyboru Zwycięzcy (Schemat Blokowy)

www.ronit.pl/images/ProjektKoscianyPokerSh.jpg

Kod przystosowany do starej wersji typu pisania NPC:

Code:
focusesCount = 0
focuses = {}
talkStages = {}
SV = 6071
KrokGry = 0
STAWKA = 0
-- Dzielnik Dzieli ilosc kasy w banku gracza przez daną liczbę. Req odpowiada rozegranym partiom w pokera
-- Tablica Stopnie musi być poszegregowana rosnąco względem parametru REQ
-- Domyślna konfiguracja:
-- 3 Stany: Nowicjusz(0-50 partii) - Bywalec(51-500 partii) - Hazardzista(Ponad 500 wygranych)
-- Nowicjusz stawka = ilośc w banku / 100. Bywalec = ilośc w banku / 10. Hazardzista = bez ograniczeń. ( Wszystko przez ilośc graczy )
-- PLAYERStorageValue(SV) 6071 przechowuje rozegraną liczbe partii.     

STOPNIE = {
    {stopien = "Nowicjusz", req = 50, dzielnik = 100},
    {stopien = "Bywalec", req = 100, dzielnik = 10},
    {stopien = "Hazardzista", req = 500, dzielnik = 1},

}

-- Funkcja LosujKostki Zawiera CID koniecznie aktualnego gracza
-- Wiadomosc do skonwertowania maksymalnie do 3 slow

-- Nowa funkcja losująca powienna przelosowywać TYLKO raz wszystkie kostki aktualnych graczy bez parametrowa

ILOSC_GRAJACYCH = 0
ILOSC_GRACZY = 2
PLAYERS ={}
ID_KOSTEK = { 7376, 7377, 7378, 7379, 7380, 7381 } -- Wymagana ( w Tibia.dat kostki muszą być ułożone rosnąco )
PUNKTY = {}

POZYCJE_KOSTEK = {
    {x=498,y=497,z=7}, -- Gracza 1 (G1)
    {x=499,y=497,z=7}, -- Gracza 1 (G1)
    {x=500,y=497,z=7}, -- Gracza 1 (G1)
    {x=501,y=497,z=7}, -- Gracza 1 (G1)
    {x=502,y=497,z=7}, -- Gracza 1 (G1)

    
    {x=498,y=500,z=7}, -- Gracza 2 (G2)
    {x=499,y=500,z=7}, -- Gracza 2 (G2)
    {x=500,y=500,z=7}, -- Gracza 2 (G2)
    {x=501,y=500,z=7}, -- Gracza 2 (G2)
    {x=502,y=500,z=7}, -- Gracza 2 (G2)

    -- Dowolna ilosc graczy.
}

local function LosujWszystkieKostki()
TAB_POS_TEMP = {}
nr_kostek = { 0, 0, 0, 0, 0, 0 }
    for nr_gracza,v in pairs(PLAYERS) do
--        log(v,"Numer Gracza - ".. nr_gracza .. ".")
        for j, pos in ipairs(POZYCJE_KOSTEK) do
            if (j >= 5*nr_gracza-4) and (j <= 5*nr_gracza) then    
                TAB_POS_TEMP = {x = pos.x,y = pos.y, z = pos.z}
                item = getItemAttributes(TAB_POS_TEMP)
--                log(v,"losuj WSZYASTKIE kostki - ".. pos.x .. "," .. pos.y .. "," .. pos.z .."," .. item.itemid ..".")
                doTransformItemEx(item.itemid,math.random(ID_KOSTEK[1],ID_KOSTEK[6]),TAB_POS_TEMP)
            end        
        end    
    end
end

local function isGamePlayer(cid)
    for z,v in pairs(PLAYERS) do
        if v == cid then
            return z
        end
    end
    return 0
end

local function EffectInLoop(cid,nr_kostek,ON_OFF)

    nr_gracza = isGamePlayer(cid)
    if nr_gracza == 0 then
        return false
    end

    for j, pos in ipairs(POZYCJE_KOSTEK) do
        if (j >= 5*nr_gracza-4) and (j <= 5*nr_gracza) then
            if ((nr_kostek[1] == 1 and j == (5*nr_gracza-4)) or (nr_kostek[2] == 1 and j == (5*nr_gracza-3)) or (nr_kostek[3] == 1 and j == (5*nr_gracza-2)) or (nr_kostek[4] == 1 and j == (5*nr_gracza-1)) or (nr_kostek[5] == 1 and j == (5*nr_gracza))) then
                TAB_POS_TEMP = {x = pos.x,y = pos.y, z = pos.z}
                if ON_OFF == 1 then
                    doAddMagicEffectToLoop(cid,nr_gracza+j,TAB_POS_TEMP,71,1500)
                elseif ON_OFF == 0 then
                    doRemoveMagicEffectFromLoop(cid,nr_gracza+j)
                else
                    return false
                end
                
            end
        end        
    end
    
    return true
    
end

local function LosujKostki(cid,msg)
TAB_POS_TEMP = {}
nr_kostek = { 0, 0, 0, 0, 0, 0 }
nr_gracza = isGamePlayer(cid)

    if string.len(msg) <= 3 then
        if string.find(msg,"1") then nr_kostek[1] = 1 end
        if string.find(msg,"2") then nr_kostek[2] = 1 end
        if string.find(msg,"3") then nr_kostek[3] = 1 end
        if string.find(msg,"4") then nr_kostek[4] = 1 end
        if string.find(msg,"5") then nr_kostek[5] = 1 end
        if string.find(msg,"6") then nr_kostek[6] = 1 end
    else
        return false
    end


    
    if nr_gracza == 0 then
        return false
    end
    
--    log(PLAYERS[1],"Numer Gracza - ".. nr_gracza .. ".")
    for j, pos in ipairs(POZYCJE_KOSTEK) do
        if (j >= 5*nr_gracza-4) and (j <= 5*nr_gracza) then
            if ((nr_kostek[1] == 1 and j == (5*nr_gracza-4)) or (nr_kostek[2] == 1 and j == (5*nr_gracza-3)) or (nr_kostek[3] == 1 and j == (5*nr_gracza-2)) or (nr_kostek[4] == 1 and j == (5*nr_gracza-1)) or (nr_kostek[5] == 1 and j == (5*nr_gracza))) then
                TAB_POS_TEMP = {x = pos.x,y = pos.y, z = pos.z}
                item = getItemAttributes(TAB_POS_TEMP)
--                log(PLAYERS[1],"LOSUJ KOSTKI - ".. pos.x .. "," .. pos.y .. "," .. pos.z ..",".. item.itemid ..".")
                doTransformItemEx(item.itemid,math.random(ID_KOSTEK[1],ID_KOSTEK[6]),TAB_POS_TEMP)
            end
        end        
    end    

    return true
end

local function SP(ilosc_oczek,OutData,wart_pocz)
    while wart_pocz <= 6 do
        if OutData[wart_pocz] == ilosc_oczek then
            return wart_pocz
        end
        wart_pocz = wart_pocz + 1
    end
    return 0
end

local function LiczPKT()
-- Pobieranie Tablicy Out_DATA
TAB_POS_TEMP = {}

for t,k in pairs(PLAYERS) do

    OutData = {0,0,0,0,0,0}

--    log(k, "JESTEM W FUNKCJI LICZ PKT U gracza nr: ".. t ..".")
    
        for j, pos in ipairs(POZYCJE_KOSTEK) do
            if (j >= 5*t-4) and (j <= 5*t) then
                TAB_POS_TEMP = {x = pos.x, y = pos.y, z = pos.z}
                item = getItemAttributes(TAB_POS_TEMP)
--                log(t,"Log z LiczPKT - ".. pos.x .. "," .. pos.y .. "," .. pos.z ..". ".. item.itemid ..".")
                
                if item.itemid == ID_KOSTEK[1] then
                    OutData[1]=OutData[1]+1
                elseif item.itemid == ID_KOSTEK[2] then
                    OutData[2]=OutData[2]+1
                elseif item.itemid == ID_KOSTEK[3] then
                    OutData[3]=OutData[3]+1
                elseif item.itemid == ID_KOSTEK[4] then
                    OutData[4]=OutData[4]+1
                elseif item.itemid == ID_KOSTEK[5] then
                    OutData[5]=OutData[5]+1
                elseif item.itemid == ID_KOSTEK[6] then
                    OutData[6]=OutData[6]+1
                else
                    selfSay("Blad.1.") -- Wyjatek
                end
            end            
        end
        
--        log(t,"OutData Dla gracza nr: ".. t ..".")
--        for hu, hi in pairs(OutData) do log(t,"[".. hu .."] = ".. hi ..".") end -- Wypisuje w logu tablice OutData

-- Koniec Pobierania tablicy Out_DATA
-- Wyliczenie wspolczynnikow

--        log(t,"Przed funkcjiami SP")
    
        out5 = SP(5,OutData,1)
        out4 = SP(4,OutData,1)
        out3 = SP(3,OutData,1)
        out2 = SP(2,OutData,1)
        out_2 = SP(2,OutData,out2+1)
        
--        log(t,"WARTOSCI OUT:")
--        log(t,"".. out5 .."")
--        log(t,"".. out4 .."")
--        log(t,"".. out3 .."")
--        log(t,"".. out2 .."")
--        log(t,"".. out_2 .."")
        
        
    -- Drzewo Algorytmu

        if out5 > 0 then
            PUNKTY[t] = 369 + out5
        elseif out4 > 0 then
            PUNKTY[t] = 262 + out4
        elseif out3 > 0 then
            if out2 > 0 then
                PUNKTY[t] = 60*out3 + out2
            else
                PUNKTY[t] = 22 + out3
            end
        elseif out2 > 0 then
            if out_2 > 0 then
                PUNKTY[t] = (2*out2) + (2*out_2)
            else
                PUNKTY[t] = out2
            end
        elseif OutData[2] == 1 and OutData[3] == 1 and OutData[4] == 1 and OutData[5] == 1 and OutData[6] == 1 then
            PUNKTY[t] = 40
        elseif OutData[1] == 1 and OutData[2] == 1 and OutData[3] == 1 and OutData[4] == 1 and OutData[5] == 1 then
            PUNKTY[t] = 30
        else
            PUNKTY[t] = 0
        end
-- Koniec Drzewa algorytmu
        log(t,"Punkty GRACZA NR ".. t .." = ".. PUNKTY[t] .."")
end
    i = 1
    nr_win = 1
    pkt_win = PUNKTY[1]

    while i < ILOSC_GRACZY do
        if PUNKTY[i+1] > pkt_win then
            pkt_win = PUNKTY[i+1]
            nr_win = i + 1
        end
        i = i + 1
    end
    
    
    for y,z in pairs(PUNKTY) do
        if z == pkt_win and y ~= nr_win then
            log(PLAYERS[1],"REMIS! :".. nr_win .." z liczba punktow = ".. pkt_win .." jest takie samo jak ilosc pkt = ".. z .." gracza o nr ".. y .."")
            return -1
        end
    end    
    
    log(PLAYERS[1],"WYGRYWA GRACZ NUMER:".. nr_win .." z liczba punktow = ".. pkt_win .."")
    return PLAYERS[nr_win]

end

local function KoniecGry()
    for i, v in pairs(PLAYERS) do
        log(PLAYERS[i]," Usuwam gracza id cid = ".. v .."")
        --removeTalkStage(v)    
        table.remove(PLAYERS, i)
    end
    for m, x in pairs(PUNKTY) do table.remove(PUNKTY, m) end

    STAWKA = 0
    ILOSC_GRAJACYCH = 0
    KrokGry = 0
end

local function PobierzStawke(msg)
    for i,j in pairs(PLAYERS) do
        withdrawFromBank(j, msg)
    end
    STAWKA = ILOSC_GRACZY * msg
end

local function SprawdzStanyKontGraczy(ilosc_gp)
    for j,i in pairs(PLAYERS) do
         if tonumber(ilosc_gp) > (getBalance(PLAYERS[j]) / ILOSC_GRACZY) then
            return false
         end
    end
    return true
end

local function SprawdzStawke()
    i = 1
    while (i < ILOSC_GRACZY) do
        if getPlayerStorageValue(PLAYERS[i],SV) < getPlayerStorageValue(PLAYERS[i+1],SV) then
            najgorszy_gracz_partie = getPlayerStorageValue(PLAYERS[i],SV)
            wBanku = getBalance(PLAYERS[i])
        else
            najgorszy_gracz_partie = getPlayerStorageValue(PLAYERS[i+1],SV)
            wBanku = getBalance(PLAYERS[i+1])
        end    
        i=i+1
    end
    
    for u, h in pairs(STOPNIE) do
        if najgorszy_gracz_partie < h.req then
            return (wBanku / h.dzielnik*ILOSC_GRACZY)
        end    
    end
    
    log(PLAYERS[1], "SprawdzStawke - Blad - 1")
end

local function SprawdzRange(cid)
    ilerang = 0
    for i, v in pairs(STOPNIE) do ilerang = i end
    while ilerang > 0 do
        if STOPNIE[ilerang][req] < getPlayerStorageValue(cid,SV) then return STOPNIE[i][1] end
    end
    log(PLAYERS[1], "SprawdzRange - Blad - 1")
end

local function DodajGracza(cid)
nr_kostek = { 1, 1, 1, 1, 1, 1}
    for i, v in pairs(PLAYERS) do
        ILOSC_GRAJACYCH = ILOSC_GRAJACYCH + 1
        if (v == cid) then
            log(PLAYERS[1], "DodajGracza - Blad - 1 Ilosc Grajacych = ".. ILOSC_GRAJACYCH ..", Iteral = ".. i ..".")
            return false
        end
    end
    if(ILOSC_GRAJACYCH < ILOSC_GRACZY) then
        table.insert(PLAYERS, cid)
        EffectInLoop(cid,nr_kostek,1)
        return true
    else
        return false
    end
end

local function isFullPlayerList()
    for t,y in pairs(PLAYERS) do
        if t == ILOSC_GRACZY then
            return true
        end
    end
    return false
end

local function isTalkStaged(cid)
    for i, v in pairs(talkStages) do
        if(v.cid == cid) then
            return true
        end
    end
    return false
end

local function addTalkStage(cid, stage)
    if(not isTalkStaged(cid)) then
        talkStage = { cid = cid, talkstage = stage }
        table.insert(talkStages, talkStage)
        return true
    end
    return false
end

local function setTalkStage(cid, stage)
    if(addTalkStage(cid, stage)) then
        return true
    else
        for i, v in pairs(talkStages) do
            if(v.cid == cid) then
                v.talkstage = stage
                return true
            end
        end
    end
    return false
end

local function removeTalkStage(cid)
    for i, v in pairs(talkStages) do
        if(v.cid == cid) then
            table.remove(talkStages, i)
            break
        end
    end
    return false
end

local function getTalkStage(cid)
    for i, v in pairs(talkStages) do
        if(v.cid == cid) then
            return v.talkstage
        end
    end
    return 0
end

local function isFocused(cid)
    for i, v in pairs(focuses) do
        if(v == cid) then
            return true
        end
    end
    return false
end

local function addFocus(cid)
    if(not isFocused(cid)) then
        table.insert(focuses, cid)
        focusesCount = focusesCount + 1
    end
end

local function removeFocus(cid)
    for i, v in pairs(focuses) do
        if(v == cid) then
            table.remove(focuses, i)
            focusesCount = focusesCount - 1
            break
        end
    end
end

function onCreatureDisappear(cid, pos)
end

function msgcontains(txt, str)
    return (string.find(txt, str) and not string.find(txt, '(%w+)' .. str) and not string.find(txt, str .. '(%w+)'))
end

function getDistanceTo(id)
    if id == 0 or id == nil then selfGotoIdle() end
    cx, cy, cz = creatureGetPosition(id)    
    if cx == nil then return nil end
    sx, sy, sz = selfGetPosition()
    return math.max(math.abs(sx-cx), math.abs(sy-cy))
end

local function CzyAllKostkiWymienione()
CheckSum = 0
    for u,z in pairs(PLAYERS) do
        CheckSum = CheckSum + getTalkStage(PLAYERS[u])
    end

    if (CheckSum/ILOSC_GRACZY) == 6 then
        return true
    else
        return false
    end

end

function onCreatureSay(cid, type, msg, language)
    stopMoving(cid)    
    if isGamePlayer(cid) == 0 then
        if (msg == "hi" or msg == "witaj" or msg == "witam") and getDistanceTo(cid) < 5 and not isFocused(cid) then
            selfSayDelayed(500,"Witaj ".. creatureGetName(cid) .. ". Jestem Krupierem? Gry w Koscianego Pokera. Dostepne operacje:",cid,false)
            selfSayDelayed(2000,"{1.} Zaloz Gre.",cid,false)
            selfSayDelayed(4000,"{2.} Dolacz do Gry.",cid,false)
            selfSayDelayed(6000,"{3.} Sprawdz swoja Range.",cid,false)
            selfSayDelayed(8000,"{4.} Zasady Gry w Koscianeg Pokera.",cid,false)
            addFocus(cid)
        elseif (msg == "1") and getDistanceTo(cid) < 5 and isFocused(cid) then
            setTalkStage(cid, 1)
        elseif (msg == "2") and getDistanceTo(cid) < 5 and isFocused(cid) then
            setTalkStage(cid, 2)
        elseif (msg == "3") and getDistanceTo(cid) < 5 and isFocused(cid) then
        -- Wypisuje range i pyta gracza czy cofnąć (5)
        elseif (msg == "4") and getDistanceTo(cid) < 5 and isFocused(cid) then
        -- Wypisuje cały regulamin i pyta czy cofnąć (5)
        elseif (msg == "5") and getDistanceTo(cid) < 5 and isFocused(cid) then
            selfSayDelayed(500,"Operacje:",cid,false)
            selfSayDelayed(2000,"{1.} Zaloz Gre.",cid,false)
            selfSayDelayed(4000,"{2.} Dolacz do Gry.",cid,false)
            selfSayDelayed(6000,"{3.} Sprawdz swoja Range.",cid,false)
            selfSayDelayed(8000,"{4.} Zasady Gry w Koscianeg Pokera.",cid,false)
        end
    end

    if KrokGry == 0 then
        if getTalkStage(cid) == 1 then    
            selfSayDelayed(500,"Gra zostala zalozona...")
            DodajGracza(cid)
            selfSayDelayed(2000,"".. isGamePlayer(cid) .." - to numer rzedu Twoich kostek.",cid,false)
            KrokGry = 1
            return
        elseif getTalkStage(cid) == 2 then
            selfSayDelayed(500,"Gra nie istnieje, aby zalozyc gre wpisz {1}.",cid,false)
            removeTalkStage(cid)
        end
    end
        
    if KrokGry == 1 and getTalkStage(cid) == 2 then
        selfSayDelayed(500,"Dolaczyles do gry...",cid,false)
        DodajGracza(cid)
        selfSayDelayed(2000,"".. isGamePlayer(cid) .." - to numer rzedu Twoich kostek.",cid,false)
        if isFullPlayerList() then
            KrokGry = 2 -- Startujemy gre ( wyslanie odpowiedniej parii wiadomosci do grajacych graczy )
            selfSayDelayed(500,"Zaczynamy Gre. Gospodarz gry wybiera podstawowa stawke... maksymalnie ".. SprawdzStawke() ..".") --Do wszystkich
            setTalkStage(PLAYERS[1], 3)
            return
        end            
    end
    
    if KrokGry == 2 and getTalkStage(cid) == 3 then
        if tonumber(msg) <= SprawdzStawke() then
            if SprawdzStanyKontGraczy(msg) then
                KrokGry = 3
                PobierzStawke(msg)
                LosujWszystkieKostki()
                selfSayDelayed(500,"Z Banku kazdego gracza zostalo pobrane ".. msg .."gps i dodane do puli. Kostiki zostaly wstepnie przelosowane.")
                for x,y in pairs(PLAYERS) do
                    setTalkStage(PLAYERS[x], 5)
                    selfSayDelayed(100,"" .. creatureGetName(PLAYERS[x]) .. ". Ktore kostki wymieniasz (max 3, podaj informacje w jednym ciagu znakow np. 123 - wymienisz pierwsza, druga, i trzecia kostke liczac od lewej).", PLAYERS[x], false)
                end        
                return
            else
                selfSayDelayed(500,"Przynajmniej jeden z graczy nie posiada wymaganej ilosci gps w banku.") --Do wszystkich
                selfSayDelayed(500,"Podaj podstawowa stawke... maksymalnie - ".. SprawdzStawke() .."",PLAYERS[1],false) --Do gospodarza        
            end
        else
            selfSayDelayed(500,"Podana stawka jest wieksza niz maksymalna. Gospodarz gry powinien podac wlasciwa stawke.") --Do wszystkich
            selfSayDelayed(2000,"Podaj podstawowa stawke... maksymalnie - ".. SprawdzStawke() .."",PLAYERS[1],false) --Do gospodarza        
        end
    end
    
    if KrokGry == 3 and getTalkStage(cid) == 5 and LosujKostki(cid,msg) then
        setTalkStage(cid, 6)    
    end
end

function onThink()
    if KrokGry >= 2 then
        for h,d in pairs(PLAYERS) do
            if isPlayer(d) == -1 then
                selfSayDelayed(500,"Brak wszystkich graczy...Ewentualne gps, wrocily na konta bankowe. Koncze gre...")
                depositToBank(d, STAWKA/ILOSC_GRACZY)
                KoniecGry()
            end
        end
    end
    
    if KrokGry == 3 and CzyAllKostkiWymienione() then
        CID_WYGRANEGO = LiczPKT()
        if CID_WYGRANEGO == -1 then
            for j,i in pairs(PLAYERS) do
                depositToBank(i, STAWKA/ILOSC_GRACZY)
            end
            STAWKA = 0
            KrokGry = 2
            setTalkStage(PLAYERS[1], 3)
            selfSayDelayed(500,"Remis... GP wrocilo do wlascicieli...")
            selfSayDelayed(2000,"W celu stoczenia kolejnej partii gospodarz gry wybiera stawke... maksymalnie ".. SprawdzStawke() ..".")
        elseif CID_WYGRANEGO > 0 then
            depositToBank(CID_WYGRANEGO, STAWKA)
            KrokGry = 2
            setTalkStage(PLAYERS[1], 3)
            selfSayDelayed(500,"".. creatureGetName(CID_WYGRANEGO) .." Wygrywa ".. STAWKA .."gps!")
            log(CID_WYGRANEGO,"".. creatureGetName(CID_WYGRANEGO) .." Wygrywa ".. STAWKA .."gps!") -- log        
            selfSayDelayed(2000,"W celu stoczenia kolejnej partii gospodarz gry wybiera stawke... maksymalnie ".. SprawdzStawke() ..".")
            STAWKA = 0    
        else
            log(PLAYERS[1],"Blad w onThink(), problem z LiczPKT()")
            KoniecGry()
        end
    end
end

function onThingMove(creature, thing, oldpos, oldstackpos)

end

function onCreatureAppear(creature)

end

Wersja kolorowa http://wklej.org/id/429443/ Wink