Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tfm.exec.disableAutoNewGame(true)
- tfm.exec.disableAfkDeath(true)
- tfm.exec.disableAutoShaman(true)
- mine = {
- --[[ default configuration ]]--
- width = 17;
- height = 17;
- bombs = 50;
- block = 20;
- area = {};
- minechar = '☀';
- flagchar = '◄';
- flagbchar = '|';
- startx = 300;
- starty = 50;
- positions = {}; -- [[ for bomb's random choice ]] --
- player = 'Eshkation';
- flagmode = false;
- gamend = true;
- clicks = 0;
- flagsleft = 'notset';
- cellsleft = 0;
- flagcp = 0
- }
- function mine:init()
- tfm.exec.newGame('@6221354')
- system.bindMouse(mine.player, true)
- tfm.exec.bindKeyboard(mine.player, 32, true, true)
- id = 0
- mine.positions = {}
- mine.flagcp = 0
- mine.flagsleft = mine.bombs+1
- mine.flagmode = false
- mine.clicks = 0
- mine.cellsleft = mine.width*mine.height;
- math.randomseed(os.time())
- 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})
- for lin = mine.width, 1, -1 do
- mine.area[lin] = {}
- for col = mine.height, 1, -1 do
- id = id+1
- mine.area[lin][col] = {ismine = false, isflagged = false, isexposed = false, objectid = id}
- table.insert(mine.positions, {lin, col})
- 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})
- end
- end
- mine:setbombs()
- for lin = mine.width, 1, -1 do
- for col = mine.height, 1, -1 do
- 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)
- end
- end
- mine.gamend = false;
- end
- function mine:getcell(posx, posy)
- data = {false, false}
- for lin = mine.width, 1, -1 do
- for col = mine.height, 1, -1 do
- if mine:checkinrange(lin*mine.block, col*mine.block, posx-mine.startx+mine.block, posy-mine.starty+mine.block, mine.block/2) then
- data = {lin, col}
- end
- end
- end
- return data
- end
- function mine:checkinrange(posx, posy, posx2, posy2, range)
- return ((posx - posx2)^2 + (posy - posy2)^2 < range^2)
- end
- function mine:getneighbors(posx, posy, posx2, posy2, functionto, discover)
- count = 0
- for lin = math.max(posx, 1), math.min(posx2, mine.width) do
- for col = math.max(posy, 1), math.min(posy2, mine.height) do
- if mine.area[lin] ~= nil and mine.area[lin][col] ~= nil then
- if not mine.area[lin][col].isexposed then
- functionto(lin, col)
- if discover then
- if mine.area[lin][col].neighmines == 0 then
- 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)
- end
- end
- end
- end
- end
- end
- return count
- end
- function mine:removecell(posx, posy)
- if not mine.area[posx][posy].ismine and not mine.area[posx][posy].isflagged then
- mine.cellsleft = mine.cellsleft-1
- count = mine.area[posx][posy].neighmines
- mine.area[posx][posy].isexposed = true
- cellscolors = {[0] = '#A2A2A2', [1] = '#0000FE', [2] = '#007F00', [3] = '#FE0000', [4] = '#00007F', [5] = '#7F0100', [6] = '#007F80', [7] = '#000000', [8] = '#BEBEBE'}
- tfm.exec.removePhysicObject(mine.area[posx][posy].objectid)
- text = string.format('<b><font color="%s" size="15" face="Lucida Console">%s', cellscolors[count], count)
- 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)
- end
- end
- function eventKeyboard(player, keyid, down, x, y)
- if (keyid == 32) then
- if mine.flagmode then
- mine.flagmode = false
- else
- mine.flagmode = true
- end
- end
- end
- function eventTextAreaCallback(id, player, callback)
- if callback == 'newgame' then
- ui.removeTextArea(-3)
- ui.removeTextArea(-4)
- for lin = mine.width, 1, -1 do
- for col = mine.height, 1, -1 do
- ui.removeTextArea(mine.area[lin][col].objectid)
- ui.removeTextArea(-mine.area[lin][col].objectid)
- ui.removeTextArea('-1'..mine.area[lin][col].objectid)
- end
- end
- mine:init()
- end
- end
- function eventMouse(player, posx, posy)
- if not mine.gamend then
- pos = mine:getcell(posx, posy)
- lin = pos[1]
- col = pos[2]
- if lin then
- mine.clicks = mine.clicks+1
- if not mine.flagmode then
- if not mine.area[lin][col].ismine then
- if not mine.area[lin][col].isflagged then
- d = mine.area[lin][col].neighmines
- 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)
- end
- end
- if mine.area[lin][col].ismine then
- for lin = mine.width, 1, -1 do
- for col = mine.height, 1, -1 do
- if mine.area[lin][col].ismine then
- mine.gamend = true
- 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})
- text = '<b><font color="#000000" size="15" face="Lucida Console">'..mine.minechar
- 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)
- end
- end
- end
- ui.addTextArea(-4, '', nil, -500, -500, 9000, 9000, 0x000001, 0x000001, 0.6, true)
- 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)
- end
- else
- if not mine.area[lin][col].isflagged then
- if mine.flagsleft > 0 then
- mine.flagsleft = mine.flagsleft-1
- mine.area[lin][col].isflagged = true
- text = '<b><font color="#FF0000" size="15" face="Lucida Console">'..mine.flagchar
- text2 = '<b><font color="#000000" size="15" face="Lucida Console">'..mine.flagbchar
- 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)
- 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)
- 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})
- if mine.area[lin][col].ismine then
- mine.flagcp = mine.flagcp+1
- if mine.flagcp == mine.bombs+1 then
- mine.gamend = true
- for x = 120, 0, -1 do
- tfm.exec.displayParticle(29, 400, 200, math.random(-2, 2), math.random(-2, 2), math.random(-2, 2), math.random(-2, 2))
- math.randomseed(x*x)
- end
- ui.addTextArea(-4, '', nil, -500, -500, 9000, 9000, 0x000001, 0x000001, 0.6, true)
- 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)
- end
- end
- end
- else
- if mine.area[lin][col].isflagged then
- mine.area[lin][col].isflagged = false
- 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})
- ui.removeTextArea(mine.area[lin][col].objectid)
- ui.removeTextArea('-1'..mine.area[lin][col].objectid)
- mine.flagsleft = mine.flagsleft+1
- if mine.area[lin][col].ismine then
- mine.flagcp = mine.flagcp-1
- end
- end
- end
- end
- end
- end
- end
- function mine:setbombs()
- for id = mine.bombs, 0, -1 do
- math.randomseed(os.time()*id)
- posid = math.random(#mine.positions)
- lin = mine.positions[posid][1]
- col = mine.positions[posid][2]
- mine.area[lin][col].ismine = true
- table.remove(mine.positions, posid)
- end
- end
- mine:init()
- function eventLoop()
- if not gamend then
- 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>'
- 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)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement