Advertisement
Hachem16

Pattern Based Generator V0.2

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