Advertisement
Math_TM

Bataille navale

Oct 19th, 2016
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.81 KB | None | 0 0
  1. --[       Bataille Navale       ]--
  2. --[ Module crée par Athesdrake  ]--
  3. --[ lien:  https://goo.gl/opffTE]--
  4.  
  5. function main()
  6. --ids
  7.     idUi = 1
  8.     idJoueurs = 2
  9.     idMsg = 3
  10.     idBato = 4
  11.     idLegende = 5
  12.     idOPl = 6
  13. --tables
  14.     jeux = {}
  15.     task = {}
  16.     taskChange = {}
  17.     players = {}
  18.     directions = {"haut", "right", "bas", "left"}
  19.     bato = {}
  20.     bato[2] = {
  21.             haut={"■", "<font color='#00FF00'>■</font>"};
  22.             right={"<font color='#00FF00'>■</font>", "■"};
  23.             bas={"<font color='#00FF00'>■</font>", "■"};
  24.             left={"■", "<font color='#00FF00'>■</font>"}
  25.         }
  26.     bato[3] = {
  27.             haut={"■", "■", "<font color='#00FF00'>■</font>"};
  28.             right={"<font color='#00FF00'>■</font>", "■", "■"};
  29.             bas={"<font color='#00FF00'>■</font>", "■", "■"};
  30.             left={"■", "■", "<font color='#00FF00'>■</font>"}
  31.         }
  32.     bato[4] = {
  33.             haut={"■", "■", "■", "<font color='#00FF00'>■</font>"};
  34.             right={"<font color='#00FF00'>■</font>", "■", "■", "■"};
  35.             bas={"<font color='#00FF00'>■</font>", "■", "■", "■"};
  36.             left={"■", "■", "■", "<font color='#00FF00'>■</font>"}
  37.         }
  38.     bato[5] = {
  39.             haut={"■", "■", "■", "■", "<font color='#00FF00'>■</font>"};
  40.             right={"<font color='#00FF00'>■</font>", "■", "■", "■", "■"};
  41.             bas={"<font color='#00FF00'>■</font>", "■", "■", "■", "■"};
  42.             left={"■", "■", "■", "■", "<font color='#00FF00'>■</font>"}
  43.         }
  44.     playerList = tfm.get.room.playerList
  45.     for pl in pairs(playerList) do
  46.         eventNewPlayer(pl)
  47.     end
  48. end
  49.  
  50. function eventNewPlayer(name)
  51.     players[name] = {isPlaying = false}
  52.     showUi(name, 1)
  53. end
  54.  
  55. function showUi(name, nbr)
  56.     if nbr==1 then
  57.         ui.addTextArea(idUi, "<a href='event:playPlayer'>Jouer contre un joueur</a>", name, 0, 30, nil, nil, nil, nil, 1, true)
  58.     end
  59.     if nbr==2 then
  60.         ui.addTextArea(idUi, "<VP>Jouer contre un joueur</VP>", name, 0, 30, nil, nil, nil, nil, 1, true)
  61.     end
  62.     if nbr==3 then
  63.         ui.addTextArea(idUi*100+2, "", name, 193, 231, 60, 0, 0xA4A7AC, 0xA4A7AC, 1, true)
  64.         ui.addTextArea(idUi*100+3, "", name, 191, 214, 14, 60, 0xA4A7AC, 0xA4A7AC, 1, true)
  65.         ui.addTextArea(idUi*100+4, "", name, 2, 174, 100, 100, 0xA4A7AC, 0xA4A7AC, 1, true)
  66.         ui.addTextArea(idUi*100+5, "", name, 2, 28, 100, 100, 0xA4A7AC, 0xA4A7AC, 1, true)
  67.         ui.addTextArea(idUi*100+6, "", name, 277, 28, 100, 100, 0xA4A7AC, 0xA4A7AC, 1, true)
  68.         ui.addTextArea(idUi, "", name, 6, 229, 195, 41, 0x808080, 0x808080, 1, true)
  69.         ui.addTextArea(idUi*100, "", name, 6, 32, 367, 195, 0x808080, 0x808080, 1, true)
  70.         ui.addTextArea(idUi*100+1, "", name, 8, 220, 191, 14, 0x808080, 0x808080, 1, true)
  71.     end
  72. end
  73.  
  74. function eventTextAreaCallback(id, name, call)
  75.     if call:sub(0,4)=="play" then
  76.         if call:sub(5)=="Player" then
  77.             local txt = ""
  78.             for pl in pairs(playerList) do
  79.                 if pl==name then
  80.                 else
  81.                     txt = txt.."<a href='event:playWith"..pl.."'>"..pl.."</a>\n"
  82.                 end
  83.             end
  84.             txt = txt.."\n<a href='event:annuler'>annuler</a>"
  85.             ui.addTextArea(idJoueurs, txt, name,  10, 75, nil, nil, nil, nil, 1, true)
  86.         end
  87.         if call:sub(5,8)=="With" and players[call:sub(9)].isPlaying==false then
  88.             players[name].isPlaying = true
  89.             players[call:sub(9)].isPlaying = true
  90.             task[#task+1] = {name, call:sub(9)}
  91.             ui.addPopup(idJoueurs, 1, "<p align='center'>"..name.." veut faire un touché-coulé avec toi !\nAccepter ?", call:sub(9), 250, 200, 300, true)
  92.             ui.removeTextArea(idJoueurs, name)
  93.             showUi(name, 2)
  94.         end
  95.     end
  96.     if call:sub(0,4)=="boat" then
  97.         local dir = ""
  98.         for k,v in pairs(directions) do
  99.             if v==players[name].dir then
  100.                 if call:sub(5,8)=="Left" then
  101.                     dir = k-1
  102.                     if dir==0 then
  103.                         dir = 4
  104.                     end
  105.                 else
  106.                     dir = k+1
  107.                     if dir==5 then
  108.                         dir = 1
  109.                     end
  110.                 end
  111.             end
  112.         end
  113.         afficheBato(name, players[name].numBato, directions[dir])
  114.     end
  115.     if call:sub(0,5)=="place" then
  116.         local place = {}
  117.         for k, v in string.gmatch(call, "%S+") do
  118.             table.insert(place, k)
  119.         end
  120.         local id = tonumber(place[5])
  121.         local x = tonumber(place[3])
  122.         local y = tonumber(place[4])
  123.         local nbr = players[name].numBato
  124.         if checkPlace(name, x, y, id, nbr, players[name].dir) then
  125.             if nbr~=1 then
  126.                 for i=0, players[name].numBato-1 do
  127.                     if players[name].dir=="left" then
  128.                         jeux[id]["carte"..name][x-i][y] = 1
  129.                     elseif players[name].dir=="bas" then
  130.                         jeux[id]["carte"..name][x][y+i] = 1
  131.                     elseif players[name].dir=="right" then
  132.                         jeux[id]["carte"..name][x+i][y] = 1
  133.                     elseif players[name].dir=="haut" then
  134.                         jeux[id]["carte"..name][x][y-i] = 1
  135.                     end
  136.                 end
  137.                 nbr = nbr-1
  138.                 if players[name].bato3 and nbr==2 then
  139.                     nbr = 3
  140.                     players[name].bato3 = false
  141.                 end
  142.                 battle(id, false, false, "place", nbr, "haut", name)
  143.             end
  144.             if nbr==1 then
  145.                 players[name].finishPlace = true
  146.                 for _,pl in pairs(jeux[id].players) do
  147.                     if pl==name then
  148.                     else
  149.                         if players[pl].finishPlace then
  150.                             battle(id, jeux[id].players[1], jeux[id].players[2], "fire", 1)
  151.                         end
  152.                         ui.removeTextArea(idBato, name)
  153.                         ui.addTextArea(idLegende, "<p align='center'><font size='16'><b><u>Legende</u></b></p><p align='left'><font size='16'>\n■</font>\tPas découvert<font size='16' color='#00FF00'>\n■</font>\tBateau<font size='16' color='#007FFF'>\n■</font>\tEau<font size='16' color='#FF0000'>\n■</font>\tBateau Touché", name, 207, 35, 163, nil, nil, nil, 0, true)
  154.                         ui.addTextArea(idMsg, "<font color='#FF0000'><b>"..name.." est prêts pour le combat !</b></font>", pl, 9, 227, 189, 40, nil, nil, 0, true)
  155.                     end
  156.                 end
  157.             end
  158.         else
  159.             ui.addTextArea(idMsg, "<b>Tu ne peux pas mettre le bateau la !</b>", name, 9, 227, 189, 40, nil, nil, 0, true)
  160.         end
  161.     end
  162.     if call:sub(0,4)=="fire" and players[name].click then
  163.         local fire = {}
  164.         for k, v in string.gmatch(call, "%S+") do
  165.             table.insert(fire, k)
  166.         end
  167.         local uid = tonumber(fire[2])
  168.         local x = tonumber(fire[3])
  169.         local y = tonumber(fire[4])
  170.         local idP = tonumber(fire[5])
  171.         local num = tonumber(fire[6])
  172.         local name2 = jeux[idP].players[num]
  173.         local toucher = false
  174.         local txt = "<font size='16' color='#"
  175.         if jeux[idP]["carte"..name2][x][y]==1 then
  176.             txt = txt.."FF0000"
  177.             jeux[idP]["carte"..name2][x][y] = 3
  178.             toucher = true
  179.         elseif jeux[idP]["carte"..name2][x][y]==0 then
  180.             txt = txt.."007FFF"
  181.             jeux[idP]["carte"..name2][x][y] = 2
  182.         end
  183.         txt = txt.."'>■</font>"
  184.         for _,pl in pairs(jeux[idP].players) do
  185.             ui.addTextArea(uid, txt, pl, 10+x*18, 35+y*18, nil, nil, nil, nil, 0, true)
  186.         end
  187.         if toucher then
  188.             local boom = 0
  189.             players[name].boom = players[name].boom + 1
  190.             boom = players[name].boom
  191.             ui.addTextArea(idMsg, "<p align='center'><font size='14' color='#FF0000'><b>Touché!", name, 9, 227, 189, 40, nil, nil, 0, true)
  192.             ui.addTextArea(idMsg, "<p align='center'><font size='14' color='#FF0000'><b>Touché!", name2, 9, 227, 189, 40, nil, nil, 0, true)
  193.             if boom==17 then
  194.                 players[name].click = false
  195.                 ui.addTextArea(idMsg, name.." a gagné !", name2, 9, 227, 189, 40, nil, nil, 0, true)
  196.                 ui.addTextArea(idMsg, "Tu as gagné !", name, 9, 227, 189, 40, nil, nil, 0, true)
  197.                 ui.addTextArea(idOPl, "<a href='event:"..idP.."'>Cliques ici pour voir\nle jeu de "..name2.."</a>", name, 210, 200, nil, nil, nil, nil, 0, true)
  198.                 ui.addTextArea(idOPl, "<a href='event:"..idP.."'>Cliques ici pour voir\nle jeu de "..name.."</a>", name2, 210, 190, nil, nil, nil, nil, 0, true)
  199.             else
  200.                 battle(idP, jeux[idP].players[1], jeux[idP].players[2], "fire", num)
  201.             end
  202.         else
  203.             ui.addTextArea(idMsg, "<p align='center'><font size='14' color='#007FFF'><b>Plouf !", name, 9, 227, 189, 40, nil, nil, 0, true)
  204.             ui.addTextArea(idMsg, "<p align='center'><font size='14' color='#007FFF'><b>Plouf !", name2, 9, 227, 189, 40, nil, nil, 0, true)
  205.             players[name].click = false
  206.             table.insert(taskChange, {id = idP, p1 = jeux[idP].players[1], p2 = jeux[idP].players[2], stade = "fire", num = (num==1 and 2 or 1), time = os.time()+2000, name = name})
  207.         end
  208.     end
  209.     if id==idOPl then
  210.         local name2 = ""
  211.         for _,pl in pairs(jeux[tonumber(call)].players) do
  212.             if pl==name then
  213.             else
  214.                 name2 = pl
  215.             end
  216.         end
  217.         local txt = ""
  218.         for x=0, 9 do
  219.             for y=0, 9 do
  220.                 local uid = "666"..tostring(x).."0"..tostring(y)
  221.                 local carre = jeux[tonumber(call)]["carte"..name2][x][y]
  222.                 if carre==0 then
  223.                     txt = "<font size='16' color='#007FFF'>■</font>"
  224.                 elseif carre==1 or carre==3 then
  225.                     txt = "<font size='16' color='#00FF00'>■</font>"
  226.                 end
  227.                 ui.addTextArea(tonumber(uid), txt, name, 10+x*18, 35+y*18, nil, nil, nil, nil, 0, true)
  228.             end
  229.         end
  230.     end
  231.     if call=="annuler" then
  232.         ui.removeTextArea(idJoueurs, name)
  233.     end
  234. end
  235.  
  236. function eventPopupAnswer(id, name, ans)
  237.     if id==idJoueurs then
  238.         for k, v in pairs(task) do
  239.             if task[k][2]==name then
  240.                 local name2 = task[k][1]
  241.                 if ans=="yes" then
  242.                     local nbr = #jeux
  243.                     jeux[nbr] = {players = {name2, name}}
  244.                     jeux[nbr]["carte"..name2] = {}
  245.                     jeux[nbr]["carte"..name] = {}
  246.                     for i=0,9 do
  247.                         jeux[nbr]["carte"..name2][i] = {}
  248.                         jeux[nbr]["carte"..name][i] = {}
  249.                     end
  250.                     for i=0, 9 do
  251.                         for j=0, 9 do
  252.                             jeux[nbr]["carte"..name2][i][j] = 0
  253.                             jeux[nbr]["carte"..name][i][j] = 0
  254.                         end
  255.                     end
  256.                     for _,pl in pairs(jeux[nbr].players) do
  257.                         players[pl].click = true
  258.                         players[pl].bato3 = true
  259.                         players[pl].finishPlace = false
  260.                         players[pl].boom = 0
  261.                         battle(nbr, false, false, "place", 5, "haut", pl)
  262.                     end
  263.                 else
  264.                     players[name].isPlaying = false
  265.                     players[name2].isPlaying = false
  266.                     ui.addPopup(idJoueurs, 0, "<p align='center'>"..name.." a refusé :c</p>", name2, 300, 167.5, nil, true)
  267.                     showUi(name2, 1)
  268.                 end
  269.                 table.remove(task, k)
  270.                 break;
  271.             end
  272.         end
  273.     end
  274. end
  275.  
  276. function battle(id, p1, p2, stade, num, dir, pl)
  277.     if stade=="place" then
  278.         players[pl].dir = dir
  279.         players[pl].numBato = num
  280.         showUi(pl, 3)
  281.         local txt = ""
  282.         for x=0, 9 do
  283.             for y=0, 9 do
  284.                 local uid = "666"..tostring(x).."0"..tostring(y)
  285.                 if jeux[id]["carte"..pl][x][y]==0 then
  286.                     txt = "<font size='16' color='#007FFF'><a href='event:place "..uid.." "..x.." "..y.." "..id.."'>■</a></font>"
  287.                 elseif jeux[id]["carte"..pl][x][y]==1 then
  288.                     txt = "<font size='16' color='#00FF00'>■</font>"
  289.                 end
  290.                 ui.addTextArea(tonumber(uid), txt, pl, 10+x*18, 35+y*18, nil, nil, nil, nil, 0, true)
  291.             end
  292.         end
  293.         afficheBato(pl, num, dir)
  294.     end
  295.     if stade=="fire" then
  296.         local twoPl = {}
  297.         local name = ""
  298.         local name2 = ""
  299.         table.insert(twoPl, p1)
  300.         table.insert(twoPl, p2)
  301.         for k, v in pairs(twoPl) do
  302.             name = (num==1 and p1 or p2)
  303.             name2 = (num==2 and p1 or p2)
  304.             for x=0, 9 do
  305.                 for y=0, 9 do
  306.                     local uid = "666"..tostring(x).."0"..tostring(y)
  307.                     local case = jeux[id]["carte"..name][x][y]
  308.                     local txt = ""
  309.                     if case==0 or case==1 then -- 0 = rien // 1 = bato // 2 = eau // 3 = bato touché
  310.                         txt = k==num and (case==1 and "<font size='16' color='#00FF00'>■" or "<font size='16' color='#FFFFFF'>■") or "<font size='16' color='#FFFFFF'><a href='event:fire "..uid.." "..x.." "..y.." "..id.." "..num.."'>■</a>"
  311.                     elseif case==2 then
  312.                         txt = "<font size='16' color='#007FFF'>■"
  313.                     elseif case==3 then
  314.                         txt = "<font size='16' color='#FF0000'>■"
  315.                     end
  316.                     txt = txt.."</font>"
  317.                     ui.addTextArea(tonumber(uid), txt, v, 10+x*18, 35+y*18, nil, nil, nil, nil, 0, true)
  318.                 end
  319.             end
  320.         end
  321.     end
  322. end
  323.  
  324. function afficheBato(name, numBato, dir)
  325.     players[name].numBato = numBato
  326.     players[name].dir = dir
  327.     local txt = "<font size='16'>"
  328.     for k, v in pairs(bato) do
  329.         if k==numBato then
  330.             if dir=="bas" or dir=="haut" then
  331.                 for i, j in pairs(bato[k][dir]) do
  332.                     txt = txt..j.."\n"
  333.                 end
  334.             else
  335.                 txt = "<font size='22'>\n"
  336.                 for i, j in pairs(bato[k][dir]) do
  337.                     txt = txt..j
  338.                 end
  339.                 txt = txt.."\n\n"
  340.             end
  341.         end
  342.     end
  343.     txt = txt.."</font>"
  344.     ui.addTextArea(idBato, "<p align='center'><font size='16'>Place le bateau</font>\n\n"..txt.."\n\n<a href='event:boatLeft'>←</a>\t\t\t\t<a href='event:boatRight'>→</a></p>", name, 207, 35, 163, nil, nil, nil, 0, true)
  345. end
  346.  
  347. function checkPlace(name, x, y, id, numBato, dir)
  348.     if (x-(numBato-1)<0 and dir=="left") or (x+(numBato-1)>9 and dir=="right") or (y-(numBato-1)<0 and dir=="haut") or (y+(numBato-1)>9 and dir=="bas") then
  349.         return false;
  350.     else
  351.         local map = jeux[id]["carte"..name]
  352.         for i=0,numBato-1 do
  353.             if (dir=="left" and map[x-i][y]==0) or (dir=="right" and map[x+i][y]==0) or (dir=="haut" and map[x][y-i]==0) or (dir=="bas" and map[x][y+i]==0) then
  354.             else
  355.                 return false;
  356.             end
  357.         end
  358.         return true;
  359.     end
  360. end
  361.  
  362. function eventLoop(t1, t2)
  363.     for k, v in ipairs(taskChange) do
  364.         local tbl = taskChange[k]
  365.         if tbl.time<=os.time() then
  366.             battle(tbl.id, tbl.p1, tbl.p2, tbl.stade, tbl.num)
  367.             players[tbl.name].click = true
  368.             table.remove(taskChange, k)
  369.         end
  370.    end
  371. end
  372.  
  373. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement