Advertisement
Guest User

Untitled

a guest
Aug 31st, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. tfm.exec.disableAutoNewGame(true)
  2. tfm.exec.disableAfkDeath(true)
  3. tfm.exec.disableAutoShaman(true)
  4.  
  5. mine = {
  6. --[[ default configuration ]]--
  7.     width = 17;
  8.     height = 17;
  9.     bombs = 50;
  10.     block = 20;
  11.     area = {};
  12.     minechar = '☀';
  13.     flagchar = '◄';
  14.     flagbchar = '|';
  15.     startx = 300;
  16.     starty = 50;
  17.     positions = {}; -- [[ for bomb's random choice ]] --
  18.     player = 'Eshkation';
  19.     flagmode = false;
  20.     gamend = true;
  21.     clicks = 0;
  22.     flagsleft  = 'notset';
  23.     cellsleft = 0;
  24.     flagcp = 0
  25. }
  26.  
  27. function mine:init()
  28.     tfm.exec.newGame('@6221354')
  29.     system.bindMouse(mine.player, true)
  30.     tfm.exec.bindKeyboard(mine.player, 32, true, true)
  31.     id = 0
  32.     mine.positions = {}
  33.     mine.flagcp = 0
  34.     mine.flagsleft = mine.bombs+1
  35.     mine.flagmode = false
  36.     mine.clicks = 0
  37.     mine.cellsleft = mine.width*mine.height;
  38.     math.randomseed(os.time())
  39.     tfm.exec.addPhysicObject(-1, mine.startx+(mine.block*mine.width)/2-mine.block/2, mine.starty+(mine.block*mine.height)/2-mine.block/2, {type = 12, color = 0xA2A2A2, width = mine.width*mine.block+2, height = mine.height*mine.block+2, miceCollision = false})
  40.     for lin = mine.width, 1, -1 do
  41.         mine.area[lin] = {}
  42.         for col = mine.height, 1, -1 do
  43.             id = id+1
  44.             mine.area[lin][col] = {ismine = false, isflagged = false, isexposed = false, objectid = id}
  45.             table.insert(mine.positions, {lin, col})
  46.             tfm.exec.addPhysicObject(id, ((lin-1)*mine.block)+mine.startx, ((col-1)*mine.block)+mine.starty, {type = 12, color = 0xBFBFBF, width = mine.block-2, height = mine.block-2, miceCollision = false})
  47.         end
  48.     end
  49.     mine:setbombs()
  50.     for lin = mine.width, 1, -1 do
  51.         for col = mine.height, 1, -1 do
  52.             mine.area[lin][col].neighmines = mine:getneighbors(lin-1, col-1, lin+1, col+1, function(x, y) if mine.area[x][y].ismine then count = count+1 end end)
  53.         end
  54.     end
  55.     mine.gamend = false;
  56. end
  57.  
  58. function mine:getcell(posx, posy)
  59.     data = {false, false}
  60.     for lin = mine.width, 1, -1 do
  61.         for col = mine.height, 1, -1 do
  62.             if mine:checkinrange(lin*mine.block, col*mine.block, posx-mine.startx+mine.block, posy-mine.starty+mine.block, mine.block/2) then
  63.                 data =  {lin, col}
  64.             end
  65.         end
  66.     end
  67.     return data
  68. end
  69.  
  70. function mine:checkinrange(posx, posy, posx2, posy2, range)
  71.     return ((posx - posx2)^2 + (posy - posy2)^2 < range^2)
  72. end
  73.  
  74. function mine:getneighbors(posx, posy, posx2, posy2, functionto, discover)
  75.     count = 0
  76.     for lin = math.max(posx, 1), math.min(posx2, mine.width) do
  77.         for col = math.max(posy, 1), math.min(posy2, mine.height) do
  78.             if mine.area[lin] ~= nil and mine.area[lin][col] ~= nil then
  79.                 if not mine.area[lin][col].isexposed then
  80.                     functionto(lin, col)
  81.                     if discover then
  82.                         if mine.area[lin][col].neighmines == 0 then
  83.                             mine:getneighbors(lin-1, col-1, lin+1, col+1, function(x, y) if not mine.area[x][y].ismine then mine:removecell(x, y) end end, true)
  84.                         end
  85.                     end
  86.                 end
  87.             end
  88.         end
  89.     end
  90.     return count
  91. end
  92.  
  93. function mine:removecell(posx, posy)
  94.     if not mine.area[posx][posy].ismine and not mine.area[posx][posy].isflagged then
  95.         mine.cellsleft = mine.cellsleft-1
  96.         count = mine.area[posx][posy].neighmines   
  97.         mine.area[posx][posy].isexposed  = true
  98.         cellscolors = {[0] = '#A2A2A2', [1] = '#0000FE', [2] = '#007F00', [3] = '#FE0000', [4] = '#00007F', [5] = '#7F0100', [6] = '#007F80', [7] = '#000000', [8] = '#BEBEBE'}
  99.         tfm.exec.removePhysicObject(mine.area[posx][posy].objectid)
  100.         text = string.format('<b><font color="%s" size="15" face="Lucida Console">%s', cellscolors[count], count)
  101.         ui.addTextArea(mine.area[posx][posy].objectid, text, nil, (((posx-1)*mine.block)+mine.startx)-6, (((posy-1)*mine.block)+mine.starty)-6, nil, nil, 0, 0, 1, true)
  102.     end
  103. end
  104.  
  105. function eventKeyboard(player, keyid, down, x, y)
  106.     if (keyid == 32) then
  107.         if mine.flagmode then
  108.             mine.flagmode = false
  109.         else
  110.             mine.flagmode = true
  111.         end
  112.     end
  113. end
  114.  
  115. function eventTextAreaCallback(id, player, callback)
  116.     if callback == 'newgame' then
  117.         ui.removeTextArea(-3)
  118.         ui.removeTextArea(-4)
  119.         for lin = mine.width, 1, -1 do
  120.             for col = mine.height, 1, -1 do
  121.                 ui.removeTextArea(mine.area[lin][col].objectid)
  122.                 ui.removeTextArea(-mine.area[lin][col].objectid)
  123.                 ui.removeTextArea('-1'..mine.area[lin][col].objectid)
  124.             end
  125.         end
  126.         mine:init()
  127.     end
  128. end
  129.  
  130. function eventMouse(player, posx, posy)
  131.     if not mine.gamend then
  132.         pos = mine:getcell(posx, posy)
  133.         lin = pos[1]
  134.         col = pos[2]
  135.         if lin then
  136.             mine.clicks = mine.clicks+1
  137.             if not mine.flagmode then
  138.                 if not mine.area[lin][col].ismine then
  139.                     if not mine.area[lin][col].isflagged then
  140.                         d = mine.area[lin][col].neighmines
  141.                         mine:getneighbors(lin-1, col-1, lin+1, col+1, function(x, y) if not mine.area[x][y].ismine then mine:removecell(x, y) end end, true)
  142.                     end
  143.                 end
  144.                 if mine.area[lin][col].ismine then
  145.                     for lin = mine.width, 1, -1 do
  146.                         for col = mine.height, 1, -1 do
  147.                             if mine.area[lin][col].ismine then
  148.                                 mine.gamend = true
  149.                                 tfm.exec.addPhysicObject(mine.area[lin][col].objectid, ((lin-1)*mine.block)+mine.startx, ((col-1)*mine.block)+mine.starty, {type = 12, color = 0xBC0000, width = mine.block-2, height = mine.block-2, miceCollision = false})
  150.                                 text = '<b><font color="#000000" size="15" face="Lucida Console">'..mine.minechar
  151.                                 ui.addTextArea(mine.area[lin][col].objectid, text, nil, (((lin-1)*mine.block)+mine.startx)-8, (((col-1)*mine.block)+mine.starty)-7, nil, nil, 0, 0, 1, true)
  152.                             end
  153.                         end
  154.                     end
  155.                     ui.addTextArea(-4, '', nil, -500, -500, 9000, 9000, 0x000001, 0x000001, 0.6, true)
  156.                     ui.addTextArea(-3, '<R><font size="40" face="Lucida Console"><p align="center">Booooooom!</font>\n<font size="15"><N><a href="event:newgame">new game', nil, 270, 200, nil, nil, 0x0, 0x0, 0, true)                                
  157.                 end
  158.             else
  159.                 if not mine.area[lin][col].isflagged then
  160.                     if mine.flagsleft > 0 then
  161.                         mine.flagsleft = mine.flagsleft-1
  162.                         mine.area[lin][col].isflagged = true
  163.                         text = '<b><font color="#FF0000" size="15" face="Lucida Console">'..mine.flagchar
  164.                         text2 = '<b><font color="#000000" size="15" face="Lucida Console">'..mine.flagbchar
  165.                         ui.addTextArea(mine.area[lin][col].objectid, text, nil, (((lin-1)*mine.block)+mine.startx)-7, (((col-1)*mine.block)+mine.starty)-9, nil, nil, 0, 0, 1, false)
  166.                         ui.addTextArea('-1'..mine.area[lin][col].objectid, text2, nil, (((lin-1)*mine.block)+mine.startx)-3, (((col-1)*mine.block)+mine.starty)-8, nil, nil, 0, 0, 1, false)
  167.                         tfm.exec.addPhysicObject(mine.area[lin][col].objectid, ((lin-1)*mine.block)+mine.startx, ((col-1)*mine.block)+mine.starty, {type = 12, color = 0xCDC327, width = mine.block-2, height = mine.block-2, miceCollision = false})
  168.                         if mine.area[lin][col].ismine then
  169.                             mine.flagcp = mine.flagcp+1
  170.                             if mine.flagcp == mine.bombs+1 then
  171.                                 mine.gamend = true
  172.                                 for x = 120, 0, -1 do
  173.                                     tfm.exec.displayParticle(29, 400, 200, math.random(-2, 2), math.random(-2, 2), math.random(-2, 2), math.random(-2, 2))
  174.                                     math.randomseed(x*x)
  175.                                 end
  176.                                 ui.addTextArea(-4, '', nil, -500, -500, 9000, 9000, 0x000001, 0x000001, 0.6, true)
  177.                                 ui.addTextArea(-3, '<BV><font size="40" face="Lucida Console"><p align="center">You found all mines!</font>\n<font size="15"><N><a href="event:newgame">new game', nil, 230, 200, nil, nil, 0x0, 0x0, 0, true)
  178.                             end
  179.                         end
  180.                     end
  181.                 else
  182.                     if mine.area[lin][col].isflagged then
  183.                         mine.area[lin][col].isflagged = false
  184.                         tfm.exec.addPhysicObject(mine.area[lin][col].objectid, ((lin-1)*mine.block)+mine.startx, ((col-1)*mine.block)+mine.starty, {type = 12, color = 0xBFBFBF, width = mine.block-2, height = mine.block-2, miceCollision = false})                  
  185.                         ui.removeTextArea(mine.area[lin][col].objectid)
  186.                         ui.removeTextArea('-1'..mine.area[lin][col].objectid)
  187.                         mine.flagsleft = mine.flagsleft+1
  188.                         if mine.area[lin][col].ismine then
  189.                             mine.flagcp = mine.flagcp-1
  190.                         end
  191.                     end
  192.                 end
  193.             end
  194.         end
  195.     end
  196. end
  197.  
  198. function mine:setbombs()
  199.     for id = mine.bombs, 0, -1 do
  200.         math.randomseed(os.time()*id)
  201.         posid = math.random(#mine.positions)
  202.         lin = mine.positions[posid][1]
  203.         col = mine.positions[posid][2]
  204.         mine.area[lin][col].ismine = true
  205.         table.remove(mine.positions, posid)
  206.     end
  207. end
  208.  
  209. mine:init()
  210.  
  211. function eventLoop()
  212.     if not gamend then
  213.         text = '<font face="Lucida Console"><font size="20"><VP>MINESWEEPER<font size="12">\n\n<VP>Player: <N>%s\n<VP>Cells left: <N>%s\n<VP>Mines: <N>%s\n<VP>Flags left: <N>%s\n<VP>Clicks: <N>%s\n<J>Flag mode: <N>%s\n</font>'
  214.         ui.addTextArea(-2, string.format(text, mine.player, mine.cellsleft, mine.bombs, mine.flagsleft, mine.clicks, tostring(mine.flagmode)), nil, 20, 100, nil, nil, 0x7F7F7F, 0x7F7F7F, 1, true)
  215.     end
  216. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement