5bitesofcookies

Untitled

Jun 29th, 2014
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.44 KB | None | 0 0
  1. --level designer
  2. --copyright 2014 Jackson McNeill
  3. --you are free to modify, distribute, and do not have to use the same license. No
  4. --credit must be given, however this only applies to this specific program, not the
  5. --actual game's executable. You must leave this license here and use the same license.
  6.  
  7. --[[
  8. How this works:
  9. Using a paint-like interface, you will paint houses and nonsolids and backgrounds
  10. and all that stuff. This gets saved to a level file that is a serialized table that
  11. containts this stuff and is drawn by the game. From there things like position checks
  12. and all that neat stuff can happen. If you want to load a level, it will take that
  13. table and use it for the positions of stuff.
  14. also there will be an "event" system that can call certain functions when the character walks over them.
  15. ]]
  16. --theoriginalbit's save format thingy:
  17. local function log2( n )
  18.   return math.floor(math.log(n) / math.log(2))
  19. end
  20.  
  21. local function packBoolean( nibble, bool )
  22.   return bit.bor(bit.blshift(nibble, 1), bool and 1 or 0)
  23. end
  24.  
  25. local function unpackBoolean( byte, mask )
  26.   return bit.band(byte, mask) == mask
  27. end
  28.  
  29. local function packByte( color, solid, cliff, water, battle )
  30.   return bit.bor(bit.blshift(packBoolean(packBoolean(packBoolean(packBoolean(0, solid), cliff), water), battle), 4), log2(color))
  31. end
  32.  
  33. --# returns in the order color, solid, cliff, water, battle
  34. local function unpackByte( byte )
  35.   return 2^bit.band(byte, 0xF), unpackBoolean(byte, 128), unpackBoolean(byte, 64), unpackBoolean(byte, 32), unpackBoolean(byte, 16)
  36. end
  37. --ill make my own meathod in the real game but for the level editor this is fine
  38. --maybe i wont, whatevs.
  39. --my own code:
  40. function packEntireMap()--well c&p some of the code used to draw
  41.   condensedMap={}
  42.   condensedStrings={}
  43.   for y,x in pairs(currentMap) do--for how many Y coords there are
  44.     if not condensedMap[y] then
  45.       condensedMap[y]={}
  46.     end
  47.     if not condensedStrings[y] then
  48.       condensedStrings[y]=""
  49.     end
  50.     --find the last x value
  51.     for u,i in pairs(currentMap[y]) do
  52.       lastX=u
  53.     end
  54.     for c,v in pairs(currentMap[y]) do
  55.       if not condensedMap[y][c] then
  56.         condensedMap[y][c]={}
  57.       end
  58.  
  59.  
  60.       --tempbugfix
  61.       currentMap[y][c]["e"] = false
  62.       currentMap[y][c]["w"] = false
  63.       currentMap[y][c]["g"] = false
  64.  
  65.       --norm
  66.       condensedMap[y][c]=packByte(currentMap[y][c]["c"],currentMap[y][c]["s"],currentMap[y][c]["e"],currentMap[y][c]["w"],currentMap[y][c]["g"])
  67.       for i=2,c-#condensedStrings[y] do --will make the difference between the last x value and the current minus one then after this loop it will write the byte.
  68.         condensedStrings[y]=condensedStrings[y].." "
  69.       end
  70.       condensedStrings[y]= condensedStrings[y]..condensedMap[y][c]
  71.     end
  72.   end
  73. end
  74. --do varios stuff. We'll pretend this is a function for no reason at all.
  75.   --load apis:
  76.   if not redirect then
  77.     os.loadAPI("redirect")
  78.   end
  79.   --vars set:
  80.   buttons={}
  81.   if not currentMap then--incase it crashed they can resume progress
  82.     currentMap={}
  83.   end
  84.   scrollx=0 scrolly=0
  85.   --end
  86. function drawUI()
  87.   --draw all paintstuffs
  88.   term.clear()
  89.   paintutils.drawPixel(51,1,colors.white)
  90.   paintutils.drawPixel(51,2,colors.orange)
  91.   paintutils.drawPixel(51,3,colors.magenta)
  92.   paintutils.drawPixel(51,4,colors.lightBlue)
  93.   paintutils.drawPixel(51,5,colors.yellow)
  94.   paintutils.drawPixel(51,6,colors.lime)
  95.   paintutils.drawPixel(51,7,colors.pink)
  96.   paintutils.drawPixel(51,8,colors.gray)
  97.   paintutils.drawPixel(51,9,colors.lightGray)
  98.   paintutils.drawPixel(51,10,colors.cyan)
  99.   paintutils.drawPixel(51,11,colors.purple)
  100.   paintutils.drawPixel(51,12,colors.blue)
  101.   paintutils.drawPixel(51,13,colors.brown)
  102.   paintutils.drawPixel(51,14,colors.green)
  103.   paintutils.drawPixel(51,15,colors.red)
  104.   paintutils.drawPixel(51,16,colors.black)
  105.   --draw the textboxfield:
  106.   --we have 2 more spaces
  107.   term.setCursorPos(51,17)
  108.   term.setBackgroundColor(colors.white) term.setTextColor(colors.black)
  109.   term.write"X"
  110.   --draw the border line
  111.   paintutils.drawLine(50,1,50,19,colors.lightGray)
  112.   paintutils.drawLine(49,1,49,19,colors.lightGray)
  113.   paintutils.drawLine(48,1,48,19,colors.lightGray)
  114.   paintutils.drawLine(47,1,47,19,colors.lightGray)
  115.   paintutils.drawLine(46,1,46,19,colors.lightGray)
  116.   paintutils.drawLine(45,1,45,19,colors.yellow)
  117.   term.setBackgroundColor(colors.orange)
  118.   term.setTextColor(colors.blue)
  119.   solid=sideWriteB("solid",46,1)
  120.   nonsolid=sideWriteB("nonsolid",48,1)
  121.   event=sideWriteB("event",50,1)
  122.   noEvent=sideWriteB("no",50,7)
  123.   loadB=sideWriteB("load",47,10)
  124.   saveB=sideWriteB("save",49,10)
  125.     paintutils.drawLine(46,18,51,18,colors.white)
  126.  
  127. end
  128. function sideWriteB(text2,x3,stay)--this makes a button and side writes
  129.   sideWrite(text2,x3,stay)
  130.   return makeButton(x3,stay,x3,stay+#text2)
  131. end
  132. function sideWrite(text,x,starty)--we'll be doing a lot of this
  133.   for i=1,#text do
  134.     term.setCursorPos(x,starty+i-1)
  135.     term.write(string.sub(text,i,i))
  136.   end
  137. end
  138. function makeButton(startx2,starty2,endx,endy)--we'll be a doing a bit of this aswell
  139.   buttons[#buttons+1] = {startx=startx2,starty=starty2,endx=endx,endy=endy}
  140.   return #buttons
  141. end
  142. function getButtonPress(x2,y)
  143.   for i=1,#buttons do
  144.     if x2>=buttons[i].startx and y>=buttons[i].starty and y<=buttons[i].endy and x2<=buttons[i].endx then
  145.       return i
  146.     end
  147.   end
  148. end
  149. function handleLevelEditor()
  150.   local events={os.pullEvent()}
  151.   if events[1] == "mouse_click" then
  152.     buttonPressed=getButtonPress(events[3],events[4])
  153.     if buttonPressed==solid then--set the stuffs to be solidz
  154.       currentSolid=true
  155.       paintutils.drawPixel(46,17,colors.green)
  156.     elseif buttonPressed==nonsolid then--set it to be nonsolid
  157.       currentSolid=nil
  158.       paintutils.drawPixel(46,17,colors.red)
  159.     elseif buttonPressed==event then--they want it to be an event
  160.       currentlyEvent=true
  161.       paintutils.drawPixel(47,17,colors.orange)
  162.     elseif buttonPressed==noEvent then--they no want event
  163.       currentlyEvent=nil
  164.       paintutils.drawPixel(47,17,colors.black)
  165.     elseif buttonPressed==saveB then
  166.       term.setBackgroundColor(colors.black)
  167.       save()
  168.     elseif buttonPressed==loadB then
  169.       load()
  170.     end
  171.   end
  172.   if events[1] == "mouse_click" and events[3] ==51 then--they clicked a color
  173.     if events[4] == 1 then
  174.       currentColor=colors.white
  175.     elseif events[4] == 2 then
  176.       currentColor=colors.orange
  177.     elseif events[4] == 3 then
  178.       currentColor=colors.magenta
  179.     elseif events[4]==4 then
  180.       currentColor=colors.lightBlue
  181.     elseif events[4]==5 then
  182.       currentColor=colors.yellow
  183.     elseif events[4] == 6 then
  184.       currentColor=colors.lime
  185.     elseif events[4]==7 then
  186.       currentColor=colors.pink
  187.     elseif events[4]==8 then
  188.       currentColor=colors.gray
  189.     elseif events[4] == 9 then
  190.       currentColor=colors.lightGray
  191.     elseif events[4] == 10 then
  192.       currentColor=colors.cyan
  193.     elseif events[4] ==11 then
  194.       currentColor=colors.purple
  195.     elseif events[4] ==12 then
  196.       currentColor=colors.blue
  197.     elseif events[4]==13 then
  198.       currentColor=colors.brown
  199.     elseif events[4]==14 then
  200.       currentColor=colors.green
  201.     elseif events[4] ==15 then
  202.       currentColor=colors.red
  203.     elseif events[4] ==16 then
  204.       currentColor=colors.black
  205.     elseif events[4] ==17 then--erasing
  206.       currentColor=nil
  207.     end
  208.     if currentColor then
  209.       paintutils.drawPixel(51,19,currentColor)
  210.     else
  211.       paintutils.drawPixel(51,19,colors.red)
  212.       term.setCursorPos(51,19)
  213.       write"E"
  214.     end
  215.   end
  216.   if events[1]=="mouse_click" and events[3]<45 or events[1]=="mouse_drag" and events[3]<45 then
  217.     doPaint(events)
  218.   end
  219.   if events[1]=="char" then
  220.     if events[2]=="w" then
  221.       scrolly=scrolly+1
  222.       term.setCursorPos(46,15) term.setBackgroundColor(colors.gray) term.setTextColor(colors.orange)
  223.             write"     "
  224.       term.setCursorPos(46,15)
  225.       write(scrolly)
  226.     elseif events[2]=="s" then
  227.       scrolly=scrolly-1
  228.       term.setCursorPos(46,15) term.setBackgroundColor(colors.gray) term.setTextColor(colors.orange)
  229.             write"     "
  230.       term.setCursorPos(46,15)
  231.       write(scrolly)
  232.     elseif events[2]=="d" then
  233.       scrollx=scrollx-1
  234.       term.setCursorPos(46,16) term.setBackgroundColor(colors.gray) term.setTextColor(colors.orange)
  235.             write"     "
  236.       term.setCursorPos(46,16)
  237.       write(scrollx)
  238.     elseif events[2]=="a" then
  239.       scrollx=scrollx+1
  240.       term.setCursorPos(46,16) term.setBackgroundColor(colors.gray) term.setTextColor(colors.orange)
  241.       write"     "
  242.       term.setCursorPos(46,16)
  243.       write(scrollx)
  244.     end
  245.   end
  246. end
  247. function doPaint(events)
  248.   --we'll paint crap to the screen now
  249.   --each x coord is a table containting its y coords. This seems
  250.   --like the most sense making way and should give less worse
  251.   --tearing effects if any.
  252.   if currentColor then--it's a color, not erasing
  253.     --make the table
  254.     if not currentMap[events[4]-scrolly] then
  255.       currentMap[events[4]-scrolly]={}
  256.     end
  257.     if not currentMap[events[4]-scrolly][events[3]-scrollx] then
  258.       currentMap[events[4]-scrolly][events[3]-scrollx] = {}
  259.     end
  260.     currentMap[events[4]-scrolly][events[3]-scrollx]["c"]=currentColor--set the color
  261.     if solid then--we want to be as space conserving as possible and write as little as possible.
  262.       currentMap[events[4]-scrolly][events[3]-scrollx]["s"]=currentSolid--if it's a solid, true, else, false
  263.     end
  264.     --if there's an event we'll want to promp them. We'll use the ui textbox.
  265.     if currentlyEvent then
  266.       term.setCursorPos(46,18)
  267.       term.setTextColor(colors.black)term.setBackgroundColor(colors.white)
  268.       local input=io.read()
  269.       currentMap[events[4]-scrolly][events[3]-scrollx]["event"]=input--write it
  270.       --since we can't serialize type function, we leave it as a string.
  271.     end
  272.   elseif currentMap[events[4]-scrolly]--[[make sure it exists!]] then--we're erasing something
  273.     currentMap[events[4]-scrolly][events[3]-scrollx]=nil --we'll erase the entire X table
  274.     yammount=0
  275.     for _,i in pairs(currentMap[events[4]-scrolly]) do--get #of stuff in table
  276.       yammount=yammount+1
  277.     end
  278.     if yammount ==0 then--if this Y coord is empty
  279.       currentMap[events[4]-scrolly]=nil--erase this Y coord
  280.     end
  281.   end
  282. end
  283. function oldsave()
  284.   term.setCursorPos(46,18)--gui textbox location
  285.   term.setBackgroundColor(colors.white) term.setTextColor(colors.black)
  286.   _=fs.open(read(),"w")
  287.   --we now remove whitespace
  288.   theSave = textutils.serialize(currentMap)
  289.   theSave=string.gsub(theSave,"\n","")
  290.   theSave=string.gsub(theSave," ","")
  291.   _.write(theSave)
  292.   _.close()
  293.   term.setCursorPos(46,18)
  294.   print"suc  "
  295. end
  296. function save()
  297.   packEntireMap()
  298.   theSave=fs.open(io.read(),"w")
  299.   local writeThis=""
  300.   for i,_ in pairs(condensedStrings) do
  301.     writeThis=writeThis..condensedStrings[i]
  302.     writeThis=writeThis.."\n"
  303.   end
  304.   theSave.write(writeThis)
  305.   theSave.close()
  306. end
  307. function load()
  308.  
  309. end
  310.  
  311. function drawBoard()--draw what we've drawn
  312.   for i=1,19 do
  313.     paintutils.drawLine(1,i,44,i,colors.black)
  314.   end
  315.   for y,x in pairs(currentMap) do--for how many Y coords there are
  316.     for c,v in pairs(currentMap[y]) do
  317.       if c+scrollx < 45 then--so it doesn't do gui tearing
  318.         paintutils.drawPixel(c+scrollx,y+scrolly,currentMap[y][c]["c"])
  319.       end
  320.     end
  321.   end
  322. end
  323. drawUI()
  324. while true do
  325.   handleLevelEditor()
  326.   drawBoard()
  327. end
Advertisement
Add Comment
Please, Sign In to add comment