Advertisement
Hachem16

Pattern Based Generator v0.21

Nov 1st, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.79 KB | None | 0 0
  1. namedd          = nil
  2.  
  3.  
  4. blockwidth      = 25
  5. blockheight     = 15
  6. map_width       = math.floor(1600/blockwidth)
  7. map_height      = math.floor(400/blockheight)
  8.  
  9. print(map_height)
  10. print(map_width)
  11.  
  12. pattrns = {
  13.    
  14. }
  15.  
  16. wood = {
  17.     type            = 12,
  18.     width           = 40,
  19.     height          = 40,
  20.     color           = 0x012346,
  21.     foreground      = false,
  22.     friction        = 0.3,
  23.     restitution     = 0.2,
  24.     angle           = 0,
  25.     miceCollision   = true,
  26.     groundCollision = true,
  27.     dynamic         = false,
  28.     fixedRotation   = false,
  29.     mass            = 0,
  30.     linearDamping   = 0,
  31.     angularDamping  = 0,
  32. }
  33.  
  34. xml             = [[<C><P L="1600" H="400"/><Z><S><S L="1700" o="324aa0" H="28" X="800" Y="413" T="12" P="0,0,0.3,0.2,0,0,0,0" /></S><D /><O /></Z></C>]]
  35. -- If You will edit the below, EDIT IT WITH CARE. ;)
  36.  
  37. to_load = {}
  38. input = {}
  39. upd = {}
  40.  
  41. inp_id_start = 0
  42. inp_id_end = 0
  43. id = 1
  44.  
  45. ids = 1
  46.  
  47. selection = false
  48. pvs = false
  49.  
  50. tfm.exec.disableAutoNewGame (true)
  51.  
  52. function new_map( height, width, patterns, seed)
  53.     local width     = width
  54.     local height    = height
  55.     local diverse   = diverse
  56.     local pnum      = #patterns
  57.  
  58.     local seed  = seed or tonumber(math.random(0,99999)..math.random(0,99999))
  59.     local tiles = {}
  60.     local used  = {}
  61.    
  62.     if #patterns==0 then
  63.         return {}
  64.     end
  65.  
  66.     math.randomseed(seed)
  67.  
  68.     local function pattern(pattern, x, y)
  69.         for Y,v1 in ipairs(pattern) do
  70.             for X,v in ipairs(v1) do
  71.                 if X+x-1>height then
  72.                     return false
  73.                 elseif Y+y-1>width then
  74.                     return false
  75.                 end
  76.             end
  77.         end
  78.  
  79.         for Y,v1 in ipairs(pattern) do
  80.             for X,v in ipairs(v1) do
  81.  
  82.  
  83.                 if type(tiles[X+x]) == "table" then
  84.                         tiles[X+x-1][Y+y-1] = v
  85.                         used[X+x-1][Y+y-1]  = true
  86.                 end
  87.  
  88.  
  89.             end
  90.         end
  91.     end
  92.  
  93.     for x = 0, height do
  94.         tiles[x] = {}
  95.         used[x] = {}
  96.         for y = 0, width do
  97.             used[x][y] = false
  98.         end
  99.     end
  100.  
  101.     for y = 0, width do
  102.         for x = 0, height do
  103.             if not used[x][y] then
  104.                 local pat = math.random(1,pnum)
  105.                 pattern(patterns[pat], x, y)
  106.             end
  107.         end
  108.     end
  109.  
  110.     math.randomseed(tonumber(string.reverse(seed)))
  111.     return tiles
  112. end
  113.  
  114. function load_map(tiles, x, y, w, h, prot)
  115.     local prot  = prot or wood
  116.     local x     = x or 0
  117.     local y     = y or 0
  118.     local tiles = type(tiles)=="table" and tiles or error("table expected at #1, got "..type(tiles))
  119.     local w     = w or blockwidth
  120.     local h     = h or blockheight
  121.  
  122.     if #tiles==0 then return end
  123.  
  124.     prot.width  = w
  125.     prot.height = h
  126.  
  127.  
  128.     for x,gab in pairs(tiles) do
  129.         for y,val in pairs(gab) do
  130.             if val ~= 0 then
  131.                 local w2 = w/2
  132.                 local h2 = h/2
  133.                 local cx = x*w + w2
  134.                 local cy = y*h + h2
  135.  
  136.                 if ids >=420 then
  137.                     break;
  138.                 end
  139.  
  140.                 tfm.exec.addPhysicObject(ids , cx, cy, prot)
  141.                 tfm.exec.displayParticle(3, cx, cy, math.random(-1.5,1.5)*math.random(), math.random(-1,1)*math.random(), 0, 0, nil)
  142.                 ids = ids + 1
  143.  
  144.             end
  145.         end
  146.  
  147.         if (x+1)%6 == 0 then
  148.             coroutine.yield(x, y)
  149.         end
  150.  
  151.     end
  152.     ids = 1
  153. end
  154.  
  155. function add_patterns(nh, nv, np)
  156.     selection = true
  157.  
  158.     local w = nh
  159.     local h = nv
  160.  
  161.     __ly = 1
  162.     local vac = "o"
  163.  
  164.     inp_id_start = id
  165.  
  166.  
  167.     for _x=1,w do
  168.         input[_x] = {}
  169.         upd[_x] = {}
  170.     end
  171.  
  172.     for _y=1,h do
  173.         for _x=1,w do
  174.             id = id + 1
  175.             input[_x][_y] = false
  176.             upd[_x][_y] = id
  177.             ui.addTextArea(id, "", nil, 50+_x*blockwidth, 50+_y*blockheight, blockwidth-7, blockheight-7, nil, nil, 0.3, false)
  178.         end
  179.         __ly = 50+(_y+1)*blockheight
  180.     end
  181.  
  182.  
  183.     id = id + 1
  184.     ui.addTextArea(id, "<p align='center'>DONE</p>", nil, 50+blockwidth, __ly, blockwidth*w-7, 40, nil, nil, 0.3, false)
  185.     id = id + 1
  186.     ui.addTextArea(id, "<p align='center'>RELOAD</p>", nil, 5, 50+blockheight, 60, blockheight+7, nil, nil, 0.3, false)
  187.     inp_id_end = id
  188.  
  189.  
  190. end
  191.  
  192. function inside(x,y,w,h,gx,gy)
  193.     if gx<=x+w and gx>=x and gy<=y+h and gy>=y then
  194.         return true
  195.     else
  196.         return false
  197.     end
  198. end
  199.  
  200. function load_up(__patts)
  201.  
  202.     tfm.exec.newGame(xml)
  203.  
  204.     local map = new_map(map_width,map_height,__patts)
  205.     to_load[1] = {co = coroutine.create(load_map), map = map}
  206.     coroutine.resume(to_load[1].co, to_load[1].map, 0, 0, blockwidth, blockheight,wood)
  207. end
  208.  
  209. function to_pattern(tbl)
  210.     local tbl = tbl
  211.     local pl = false
  212.     local new_pat = {}
  213.     for x, oly in pairs(tbl) do
  214.         for y in pairs(oly) do
  215.             new_pat[y] = {}
  216.         end
  217.     end
  218.     for x, oly in pairs(tbl) do
  219.         for y,l in pairs(oly) do
  220.             if l == true then
  221.                 new_pat[y][x] = 1
  222.                 pl = true
  223.             else
  224.                 new_pat[y][x] = 0
  225.             end
  226.         end
  227.     end
  228.     if not pl then return {} end
  229.     return new_pat
  230. end
  231.  
  232.  
  233. ----------------------------------------------------------------------------------------------------------------------------
  234. ----------------------------------------------------------------------------------------------------------------------------
  235. -----------------------------------------------------CALLS AND EVENTS-------------------------------------------------------
  236. ----------------------------------------------------------------------------------------------------------------------------
  237. ----------------------------------------------------------------------------------------------------------------------------
  238.  
  239.  
  240. add_patterns(7, 7)
  241.  
  242.  
  243. load_up(pattrns)
  244.  
  245.  
  246. function eventLoop(np, lp)
  247.     local to_remove = {}
  248.     if selection and not pvs then
  249.         pvs = true
  250.     elseif not selection and pvs then
  251.         for p=inp_id_start,inp_id_end do
  252.              ui.removeTextArea(p)
  253.         end
  254.         pattrns[#pattrns+1] = to_pattern(input)
  255.         pvs = false
  256.         load_up(pattrns)
  257.         add_patterns(7,7)
  258.     end
  259.     for k,v in pairs(to_load) do
  260.  
  261.         coroutine.resume(v.co, v.map, 0, 0, blockwidth, blockheight, wood)
  262.  
  263.         if coroutine.status(v.co) == "dead" then
  264.             to_remove[k] = true
  265.         end
  266.     end
  267.  
  268.     for k,v in pairs(to_remove) do
  269.         if v == true then
  270.             to_load[k] = nil
  271.             to_remove[k] = false
  272.         end
  273.     end
  274. end
  275.  
  276.  
  277. if namedd == nil then
  278.     for k,v in pairs(tfm.get.room.playerList) do
  279.         system.bindMouse(k, true)
  280.     end
  281. else
  282.     system.bindMouse(namedd)
  283. end
  284.  
  285. --function eventAreaCallback to be added
  286.  
  287.  
  288. function eventMouse(name, x, y)
  289.     local wid = 0
  290.     if selection then
  291.         for ix,vl in pairs(input) do
  292.             for iy,val in pairs(vl) do
  293.                 if inside(50+ix*blockwidth, 50+iy*blockheight, blockwidth-2, blockheight-2, x, y) then
  294.                     if not input[ix][iy] then
  295.                         ui.removeTextArea(upd[ix][iy])
  296.                         ui.addTextArea(upd[ix][iy], "", nil, 50+ix*blockwidth, 50+iy*blockheight, blockwidth-7, blockheight-7, 0xBA7BCD, 0xBA7BCD+300,0, false)
  297.                         input[ix][iy] = true
  298.                         return
  299.                     elseif input[ix][iy] then
  300.                         ui.removeTextArea(upd[ix][iy])
  301.                         ui.addTextArea(upd[ix][iy], "", nil, 50+ix*blockwidth, 50+iy*blockheight, blockwidth-7, blockheight-7, nil,nil, 0.3, false)
  302.                         input[ix][iy] = false
  303.                         return
  304.                     end
  305.                 end
  306.             end
  307.             wid = wid + 1
  308.         end
  309.         if inside(50+blockwidth , __ly , blockwidth*wid , 40 , x , y) then
  310.             selection = false
  311.             print("End of Selection")
  312.             return
  313.         elseif inside(5, 50+blockheight, 60, blockheight+7, x, y) then
  314.             load_up(pattrns)
  315.             return
  316.         end
  317.     end
  318. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement