Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- #connect4 - lua script by Zigwin
- -- ! magic do not touch !
- -- ! магия . не трогать !
- do local _,name = pcall(nil) admin = string.match(name, "(.-)%.") end
- --[[ Settings / Настройки --]]
- -- "ru" - Русский
- -- "en" - English
- local language = tfm.get.room.playerList[admin].community
- -- If you sure you can change these
- -- Если уверены можете изменить опции ниже
- -- Debug mode
- -- Режим отладки
- local debug = false
- -- Amount of people that can play at once
- -- Should NOT be greather than 8. Recommended to play with 2 players
- -- Количество людей которые могут играть одновременно
- -- НЕ должно превышать 8. Рекомендуется играть вдвоем
- local maximumPlayers = 2
- -- 1 or 2
- -- 1 или 2
- local turnsPerPlayer = 1
- -- ! magic do not touch anything below !
- -- Init Values
- -- Vars
- local grid = {}
- local playerData = {}
- local queue = {}
- local players = {} -- Players that in game
- local isGameOn = false
- local turn = 1
- -- Const
- local colors = {
- [1] = 0xb13e53, -- Red
- [2] = 0x3b5dc9, -- Blue
- [3] = 0x5d275d, -- Magenta
- [4] = 0xef7d57, -- Orange
- [5] = 0x41a6f6, -- Cyan
- [6] = 0xa7f070, -- Green
- [7] = 0xf4f4f4, -- White
- [8] = 0x1a1c2c, -- Black
- }
- local colors2 = {
- [1] = "#b13e53", -- Red
- [2] = "#3b5dc9", -- Blue
- [3] = "#5d275d", -- Magenta
- [4] = "#ef7d57", -- Orange
- [5] = "#41a6f6", -- Cyan
- [6] = "#a7f070", -- Green
- [7] = "#f4f4f4", -- White
- [8] = "#1a1c2c", -- Black
- }
- text = {
- ru = {
- join = "Присоединиться",
- cancel = "Отмена",
- queue = "Очередь",
- },
- en = {
- join = "Join",
- cancel = "Cancel",
- queue = "Queue",
- }
- }
- if not text[language] then
- language = "en"
- end
- --[[ Main --]]
- -- Init player
- function eventNewPlayer(playerName, _)
- playerData[playerName] = {
- team = 0,
- isPlaying = false,
- inQueue = false
- }
- if not _ then
- -- Draw things if new player comes to room
- drawQueue()
- drawTurns()
- drawGrid()
- -- Map name
- ui.setMapName("<j>#connect4</j> <bl>- Zigwin</bl>")
- -- Respawn
- tfm.exec.respawnPlayer(playerName)
- end
- end
- for playerName in next, tfm.get.room.playerList do
- eventNewPlayer(playerName, true)
- end
- function initGame()
- -- Set game on and init settings
- isGameOn = true
- for i = 1, maximumPlayers do
- players[i] = queue[i]
- playerData[players[i]].team = i
- playerData[players[i]].isPlaying = true
- playerData[players[i]].inQueue = false
- end
- for i = 1, maximumPlayers do
- table.remove(queue, 1)
- end
- -- Remove winner text
- ui.removeTextArea(100)
- ui.removeTextArea(101)
- -- Clear grid
- for x = 1, 7 do
- grid[x] = {}
- for y = 1, 6 do
- grid[x][y] = 0
- -- Remove circles
- tfm.exec.removePhysicObject(100+x*10+y)
- end
- end
- -- Draw for new players
- for x = 1, 7 do
- for y = 1, 6 do
- tfm.exec.addPhysicObject(x..y, 200 + x * 50, y * 50 + 70, {
- type = 13,
- width = 20,
- color = 0x6A7495,
- miceCollision = false,
- groundCollision = false
- })
- end
- end
- -- First turn to random player
- turn = math.random(1, #players)
- -- Draw UI
- drawQueue()
- drawTurns()
- drawGrid()
- end
- function eventChatCommand(playerName, command)
- if not playerData[playerName].isPlaying then
- -- Queue System
- if command == "join" then
- -- Add
- if not playerData[playerName].inQueue then
- playerData[playerName].inQueue = true
- queue[#queue + 1] = playerName
- print("\n"..playerName.." join queue :"..table.concat(queue, "\n"))
- drawQueue()
- end
- elseif command == "cancel" then
- -- Remove
- if playerData[playerName].inQueue then
- playerData[playerName].inQueue = false
- for index, name in next, queue do
- if name == playerName then
- table.remove(queue, index)
- print("\n"..playerName.." left queue :"..table.concat(queue, "\n"))
- drawQueue()
- break
- end
- end
- end
- end
- end
- end
- --[[
- 0 - Empty
- 1 - 1 Team
- 2 - 2 Team
- --]]
- function drawGrid()
- if debug then
- for x = 1, 7 do
- for y = 1, 6 do
- local color = colors[grid[x][y]]
- ui.addTextArea(x..y, "", nil, 180 + x * 50, y * 50 + 50, 40, 40, nil, color, 0.5)
- end
- end
- end
- for x = 1, 7 do
- ui.addTextArea(x, "<a href='event:grid'>\t\t\t\n\t\t\t\n\t\t\t\n", nil, 180 + x * 50, 50, 40, 40, 0x00ff00, 0x00ff00, 0.25)
- end
- end
- function drawQueue()
- --"<b><v><p align='center'>Queue</p></v></b>\n<j>1. <b>Tigrounette#0001</b></j>\n<u><j>2. <b>Hilnova#1234</b></j></u>\n3. Bolodefchoco#5678\n4. Lobezito#9012\n5. Ubitfm#3456\n6. Akuch#7890\n7. Istopetri#1234"
- local out, c = {"<b><v><p align='center'>"..text[language].queue.."</p></v></b>\n"}, 2
- for index, playerName in next, queue do
- if c > 3 then
- out[c] = (c-1)..". "..playerName.."\n"
- elseif c == 2 then
- out[c] = "<j>"..(c-1)..". <b>"..playerName.."</b></j>\n"
- elseif c == 3 then
- out[c] = "<u><j>"..(c-1)..". <b>"..playerName.."</b></j></u>\n"
- end
- c = c + 1
- end
- for playerName, Data in next, playerData do
- print(playerName)
- print("que: "..tostring(Data.inQueue))
- print("pla: "..tostring(Data.isPlaying))
- if Data.inQueue then
- out[c] = "\n<a href='event:quecancel'><p align='center'><r>["..text[language].cancel.."]</r></p></a>"
- ui.addTextArea(102, table.concat(out), playerName, 15, 40, 160, 180, 0x000000, 0x333333, 0.5, true)
- elseif not Data.isPlaying then
- out[c] = "\n<a href='event:quejoin'><p align='center'><vp>["..text[language].join.."]</vp></p></a>"
- ui.addTextArea(102, table.concat(out), playerName, 15, 40, 160, 180, 0x000000, 0x333333, 0.5, true)
- else
- ui.addTextArea(102, table.concat(out), playerName, 15, 40, 160, 180, 0x000000, 0x333333, 0.5, true)
- end
- end
- end
- function drawTurns()
- --"<font color='#ff0000'><b><u>●</u></b></font> <vp><b>Zigwin#0000</b></vp>\n<font color='#0000ff'><b><u>●</u></b></font> Mapchest#0000"
- local out, c = {}, 1
- for index, playerName in next, players do
- local team = playerData[playerName].team
- local color = colors2[team]
- if math.floor(turn) == team then
- out[c] = "<font color='"..color.."' size='8'>●</font> <vp><b>"..playerName.."</b></vp>\n"
- else
- out[c] = "<font color='"..color.."' size='8'>●</font> "..playerName.."\n"
- end
- c = c + 1
- end
- ui.addTextArea(103, table.concat(out), nil, 15, 240, 160, 0, 0x000000, 0x333333, 0.5, true)
- end
- function checkGrid()
- -- Check Y (Vertical)
- --[0][_][0][_][_][_][_]
- --[0][_][2][_][_][_][_]
- --[1][_][2][_][_][_][_]
- --[1][_][1][_][_][_][_]
- --[1][_][1][_][_][_][_]
- --[1][_][1][_][_][_][_]
- local previousValue = 0
- local amount = 0
- for x = 1, 7 do
- for y = 1, 6 do
- local value = grid[x][y]
- if value ~= 0 and value == previousValue then
- amount = amount + 1
- if amount == 3 then
- print("Vertical Y")
- return players[value]
- end
- else
- amount = 0
- end
- previousValue = value
- end
- amount = 0
- previousValue = 0
- end
- -- Check X (Horisontal)
- amount = 0
- previousValue = 0
- for y = 1, 6 do
- for x = 1, 7 do
- local value = grid[x][y]
- if value ~= 0 and value == previousValue then
- amount = amount + 1
- if amount == 3 then
- print("Horisontal X")
- return players[value]
- end
- else
- amount = 0
- end
- previousValue = value
- end
- amount = 0
- previousValue = 0
- end
- -- Check (Positive X) Diagonal
- amount = 0
- previousValue = 0
- --[0][0][0][x][>][>][x]
- --[0][0][x][v][>][>][x]
- --[0][x][#][>][>][>][x]
- --[x][#][x][#][#][x][0]
- --[#][x][#][#][x][0][0]
- --[x][#][#][x][0][0][0]
- for x = 4, 7 do
- for y = 1, 3 do
- for i = 0, 3 do
- local value = grid[x - i][y + i]
- if value ~= 0 and value == previousValue then
- amount = amount + 1
- if amount == 3 then
- print("Diagonal X")
- return players[value]
- end
- else
- amount = 0
- end
- previousValue = value
- end
- amount = 0
- previousValue = 0
- end
- end
- -- Check (Negative X) Diagonal
- --[1][2][3][4][_][_][_]
- --[2][1][2][3][4][_][_]
- --[3][2][1][2][3][4][_]
- --[_][3][2][1][2][3][4]
- --[_][_][3][2][_][_][_]
- --[_][_][_][3][_][_][_]
- amount = 0
- previousValue = 0
- for x = 1, 4 do
- for y = 1, 3 do
- for i = 0, 3 do
- local value = grid[x + i][y + i]
- if value ~= 0 and value == previousValue then
- amount = amount + 1
- if amount == 3 then
- print("Diagonal -X")
- return players[value]
- end
- else
- amount = 0
- end
- previousValue = value
- end
- amount = 0
- previousValue = 0
- end
- end
- return false
- end
- function eventTextAreaCallback(textAreaId, playerName, eventName)
- if eventName:sub(1,3) == 'que' then
- eventChatCommand(playerName, eventName:sub(4))
- end
- if isGameOn then
- local Data = playerData[playerName]
- if debug then
- turn = Data.team
- end
- if eventName == "grid" and Data.isPlaying and Data.team == math.floor(turn) then
- local x = textAreaId
- -- Check that cell empty
- if grid[x][1] ~= 0 then
- return
- end
- -- Move Circle Down
- local i = 1
- repeat
- grid[x][i-1] = 0
- grid[x][i] = Data.team
- i = i + 1
- until not grid[x][i] or grid[x][i] ~= 0
- -- Draw Circle
- local y = i - 1
- tfm.exec.addPhysicObject(100 + (x * 10) + y, 200 + x * 50, y * 50 + 70, {
- type = 13,
- width = 20,
- color = colors[Data.team],
- miceCollision = true,
- groundCollision = false
- })
- -- Draw particles and arrow
- tfm.exec.addShamanObject(0, 200 + x * 50, 50)
- for i = 1, 5 do
- tfm.exec.displayParticle(3, 200 + x * 50, y * 50 + 70, math.random(-10, 10)/10, math.random(-10, 10)/10)
- end
- -- Next Turn
- if not debug then
- turn = turn + (1 / turnsPerPlayer)
- if math.floor(turn) > #players then
- turn = 1
- end
- drawTurns()
- end
- -- Check for winner
- local winner = checkGrid()
- if checkGrid() then
- print(winner.." won!")
- -- Give some score to winner
- tfm.exec.setPlayerScore(winner, 10, true)
- -- Set some values to default
- isGameOn = false
- for index, playerName in next, players do
- playerData[playerName].isPlaying = false
- playerData[playerName].team = 0
- end
- players = {}
- -- Draw queue to players that played
- drawQueue()
- -- Draw particles and some text for winner
- local x, y = tfm.get.room.playerList[winner].x, tfm.get.room.playerList[winner].y
- for i = 1, 20 do
- --[[redConfetti : 21
- greenConfetti : 22
- blueConfetti : 23
- yellowConfetti : 24--]]
- tfm.exec.playEmote(winner, 9)
- tfm.exec.displayParticle(math.random(21,24), x, y, math.random(-10, 10)/10, math.random(-10, 10)/10, math.random(-10, 10)/100, math.random(-10, 10)/100)
- ui.addTextArea(101, "<font face='Courier New' size='24' color='#000000'><p align='center'><b>"..winner.." won!", nil, 2, 102, 800, 0, nil, nil, 0)
- ui.addTextArea(100, "<font face='Courier New' size='24'><p align='center'><j><b>"..winner.." won!", nil, 0, 100, 800, 0, nil, nil, 0)
- end
- end
- -- Update Grid
- drawGrid()
- end
- end
- end
- function eventLoop(elapsedTime, remainingTime)
- if not isGameOn then
- if #queue >= maximumPlayers then
- initGame()
- else
- print("Not enough players in queue")
- end
- end
- end
- function eventPlayerDied(playerName)
- tfm.exec.respawnPlayer(playerName)
- end
- -- Init map & game settings
- tfm.exec.disableAutoNewGame(true)
- tfm.exec.disableAutoScore(true)
- tfm.exec.disableAutoShaman(true)
- tfm.exec.disableAutoTimeLeft(true)
- tfm.exec.disableAfkDeath(true)
- for playerName in next, tfm.get.room.playerList do
- tfm.exec.setPlayerScore(playerName, 0)
- end
- --
- local xml = [[<C><P /><Z><S><S P="0,0,0.3,0.2,0,0,0,0" H="320" L="380" o="202020" X="400" c="4" Y="240" T="12" /><S P="0,0,0.3,0.2,0,0,0,0" H="10" L="20" o="6a7495" X="250" c="4" Y="70" T="13" /><S P="0,0,0.3,0.2,0,0,0,0" H="10" L="20" o="6a7495" X="300" c="4" Y="70" T="13" /><S P="0,0,0.3,0.2,0,0,0,0" H="10" L="20" o="6a7495" X="350" c="4" Y="70" T="13" /><S P="0,0,0.3,0.2,0,0,0,0" H="10" L="20" o="6a7495" X="400" c="4" Y="70" T="13" /><S P="0,0,0.3,0.2,0,0,0,0" H="10" L="20" o="6a7495" X="450" c="4" Y="70" T="13" /><S P="0,0,0.3,0.2,0,0,0,0" H="10" L="20" o="6a7495" X="500" c="4" Y="70" T="13" /><S P="0,0,0.3,0.2,0,0,0,0" H="10" L="20" o="6a7495" X="550" c="4" Y="70" T="13" /><S P="0,0,0.3,0.2,0,0,0,0" L="800" o="324650" H="80" X="400" Y="440" T="12" /><S P="0,0,0.3,0.2,0,0,0,0" X="220" L="20" H="320" c="1" Y="240" T="14" /><S P="0,0,0.3,0.2,0,0,0,0" H="320" L="10" X="275" c="1" Y="240" T="14" /><S P="0,0,0.3,0.2,0,0,0,0" X="325" L="10" H="320" c="1" Y="240" T="14" /><S P="0,0,0.3,0.2,0,0,0,0" H="320" L="10" X="375" c="1" Y="240" T="14" /><S P="0,0,0.3,0.2,0,0,0,0" X="425" L="10" H="320" c="1" Y="240" T="14" /><S P="0,0,0.3,0.2,0,0,0,0" H="320" L="10" X="475" c="1" Y="240" T="14" /><S P="0,0,0.3,0.2,0,0,0,0" X="525" L="10" H="320" c="1" Y="240" T="14" /><S P="0,0,0.3,0.2,0,0,0,0" H="320" L="20" X="580" c="1" Y="240" T="14" /></S><D><DS Y="385" X="100" /></D><O /></Z></C>]]
- tfm.exec.newGame(xml)
- ui.setMapName("<j>#connect4</j> <bl>- Zigwin</bl>")
- -- Draw map grounds
- for x = 1, 7 do
- for y = 1, 6 do
- tfm.exec.addPhysicObject(x..y, 200 + x * 50, y * 50 + 70, {
- type = 13,
- width = 20,
- color = 0x6A7495,
- miceCollision = false,
- groundCollision = false
- })
- end
- end
- -- Draw Interface
- drawQueue()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement