Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --level designer
- --copyright 2014 Jackson McNeill
- --you are free to modify, distribute, and do not have to use the same license. No
- --credit must be given, however this only applies to this specific program, not the
- --actual game's executable. You must leave this license here and use the same license.
- --[[
- How this works:
- Using a paint-like interface, you will paint houses and nonsolids and backgrounds
- and all that stuff. This gets saved to a level file that is a serialized table that
- containts this stuff and is drawn by the game. From there things like position checks
- and all that neat stuff can happen. If you want to load a level, it will take that
- table and use it for the positions of stuff.
- also there will be an "event" system that can call certain functions when the character walks over them.
- ]]
- --theoriginalbit's save format thingy:
- local function log2( n )
- return math.floor(math.log(n) / math.log(2))
- end
- local function packBoolean( nibble, bool )
- return bit.bor(bit.blshift(nibble, 1), bool and 1 or 0)
- end
- local function unpackBoolean( byte, mask )
- return bit.band(byte, mask) == mask
- end
- local function packByte( color, solid, cliff, water, battle )
- return bit.bor(bit.blshift(packBoolean(packBoolean(packBoolean(packBoolean(0, solid), cliff), water), battle), 4), log2(color))
- end
- --# returns in the order color, solid, cliff, water, battle
- local function unpackByte( byte )
- return 2^bit.band(byte, 0xF), unpackBoolean(byte, 128), unpackBoolean(byte, 64), unpackBoolean(byte, 32), unpackBoolean(byte, 16)
- end
- --ill make my own meathod in the real game but for the level editor this is fine
- --maybe i wont, whatevs.
- --my own code:
- function packEntireMap()--well c&p some of the code used to draw
- condensedMap={}
- condensedStrings={}
- for y,x in pairs(currentMap) do--for how many Y coords there are
- if not condensedMap[y] then
- condensedMap[y]={}
- end
- if not condensedStrings[y] then
- condensedStrings[y]=""
- end
- --find the last x value
- for u,i in pairs(currentMap[y]) do
- lastX=u
- end
- for c,v in pairs(currentMap[y]) do
- if not condensedMap[y][c] then
- condensedMap[y][c]={}
- end
- --tempbugfix
- currentMap[y][c]["e"] = false
- currentMap[y][c]["w"] = false
- currentMap[y][c]["g"] = false
- --norm
- condensedMap[y][c]=packByte(currentMap[y][c]["c"],currentMap[y][c]["s"],currentMap[y][c]["e"],currentMap[y][c]["w"],currentMap[y][c]["g"])
- 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.
- condensedStrings[y]=condensedStrings[y].." "
- end
- condensedStrings[y]= condensedStrings[y]..condensedMap[y][c]
- end
- end
- end
- --do varios stuff. We'll pretend this is a function for no reason at all.
- --load apis:
- if not redirect then
- os.loadAPI("redirect")
- end
- --vars set:
- buttons={}
- if not currentMap then--incase it crashed they can resume progress
- currentMap={}
- end
- scrollx=0 scrolly=0
- --end
- function drawUI()
- --draw all paintstuffs
- term.clear()
- paintutils.drawPixel(51,1,colors.white)
- paintutils.drawPixel(51,2,colors.orange)
- paintutils.drawPixel(51,3,colors.magenta)
- paintutils.drawPixel(51,4,colors.lightBlue)
- paintutils.drawPixel(51,5,colors.yellow)
- paintutils.drawPixel(51,6,colors.lime)
- paintutils.drawPixel(51,7,colors.pink)
- paintutils.drawPixel(51,8,colors.gray)
- paintutils.drawPixel(51,9,colors.lightGray)
- paintutils.drawPixel(51,10,colors.cyan)
- paintutils.drawPixel(51,11,colors.purple)
- paintutils.drawPixel(51,12,colors.blue)
- paintutils.drawPixel(51,13,colors.brown)
- paintutils.drawPixel(51,14,colors.green)
- paintutils.drawPixel(51,15,colors.red)
- paintutils.drawPixel(51,16,colors.black)
- --draw the textboxfield:
- --we have 2 more spaces
- term.setCursorPos(51,17)
- term.setBackgroundColor(colors.white) term.setTextColor(colors.black)
- term.write"X"
- --draw the border line
- paintutils.drawLine(50,1,50,19,colors.lightGray)
- paintutils.drawLine(49,1,49,19,colors.lightGray)
- paintutils.drawLine(48,1,48,19,colors.lightGray)
- paintutils.drawLine(47,1,47,19,colors.lightGray)
- paintutils.drawLine(46,1,46,19,colors.lightGray)
- paintutils.drawLine(45,1,45,19,colors.yellow)
- term.setBackgroundColor(colors.orange)
- term.setTextColor(colors.blue)
- solid=sideWriteB("solid",46,1)
- nonsolid=sideWriteB("nonsolid",48,1)
- event=sideWriteB("event",50,1)
- noEvent=sideWriteB("no",50,7)
- loadB=sideWriteB("load",47,10)
- saveB=sideWriteB("save",49,10)
- paintutils.drawLine(46,18,51,18,colors.white)
- end
- function sideWriteB(text2,x3,stay)--this makes a button and side writes
- sideWrite(text2,x3,stay)
- return makeButton(x3,stay,x3,stay+#text2)
- end
- function sideWrite(text,x,starty)--we'll be doing a lot of this
- for i=1,#text do
- term.setCursorPos(x,starty+i-1)
- term.write(string.sub(text,i,i))
- end
- end
- function makeButton(startx2,starty2,endx,endy)--we'll be a doing a bit of this aswell
- buttons[#buttons+1] = {startx=startx2,starty=starty2,endx=endx,endy=endy}
- return #buttons
- end
- function getButtonPress(x2,y)
- for i=1,#buttons do
- if x2>=buttons[i].startx and y>=buttons[i].starty and y<=buttons[i].endy and x2<=buttons[i].endx then
- return i
- end
- end
- end
- function handleLevelEditor()
- local events={os.pullEvent()}
- if events[1] == "mouse_click" then
- buttonPressed=getButtonPress(events[3],events[4])
- if buttonPressed==solid then--set the stuffs to be solidz
- currentSolid=true
- paintutils.drawPixel(46,17,colors.green)
- elseif buttonPressed==nonsolid then--set it to be nonsolid
- currentSolid=nil
- paintutils.drawPixel(46,17,colors.red)
- elseif buttonPressed==event then--they want it to be an event
- currentlyEvent=true
- paintutils.drawPixel(47,17,colors.orange)
- elseif buttonPressed==noEvent then--they no want event
- currentlyEvent=nil
- paintutils.drawPixel(47,17,colors.black)
- elseif buttonPressed==saveB then
- term.setBackgroundColor(colors.black)
- save()
- elseif buttonPressed==loadB then
- load()
- end
- end
- if events[1] == "mouse_click" and events[3] ==51 then--they clicked a color
- if events[4] == 1 then
- currentColor=colors.white
- elseif events[4] == 2 then
- currentColor=colors.orange
- elseif events[4] == 3 then
- currentColor=colors.magenta
- elseif events[4]==4 then
- currentColor=colors.lightBlue
- elseif events[4]==5 then
- currentColor=colors.yellow
- elseif events[4] == 6 then
- currentColor=colors.lime
- elseif events[4]==7 then
- currentColor=colors.pink
- elseif events[4]==8 then
- currentColor=colors.gray
- elseif events[4] == 9 then
- currentColor=colors.lightGray
- elseif events[4] == 10 then
- currentColor=colors.cyan
- elseif events[4] ==11 then
- currentColor=colors.purple
- elseif events[4] ==12 then
- currentColor=colors.blue
- elseif events[4]==13 then
- currentColor=colors.brown
- elseif events[4]==14 then
- currentColor=colors.green
- elseif events[4] ==15 then
- currentColor=colors.red
- elseif events[4] ==16 then
- currentColor=colors.black
- elseif events[4] ==17 then--erasing
- currentColor=nil
- end
- if currentColor then
- paintutils.drawPixel(51,19,currentColor)
- else
- paintutils.drawPixel(51,19,colors.red)
- term.setCursorPos(51,19)
- write"E"
- end
- end
- if events[1]=="mouse_click" and events[3]<45 or events[1]=="mouse_drag" and events[3]<45 then
- doPaint(events)
- end
- if events[1]=="char" then
- if events[2]=="w" then
- scrolly=scrolly+1
- term.setCursorPos(46,15) term.setBackgroundColor(colors.gray) term.setTextColor(colors.orange)
- write" "
- term.setCursorPos(46,15)
- write(scrolly)
- elseif events[2]=="s" then
- scrolly=scrolly-1
- term.setCursorPos(46,15) term.setBackgroundColor(colors.gray) term.setTextColor(colors.orange)
- write" "
- term.setCursorPos(46,15)
- write(scrolly)
- elseif events[2]=="d" then
- scrollx=scrollx-1
- term.setCursorPos(46,16) term.setBackgroundColor(colors.gray) term.setTextColor(colors.orange)
- write" "
- term.setCursorPos(46,16)
- write(scrollx)
- elseif events[2]=="a" then
- scrollx=scrollx+1
- term.setCursorPos(46,16) term.setBackgroundColor(colors.gray) term.setTextColor(colors.orange)
- write" "
- term.setCursorPos(46,16)
- write(scrollx)
- end
- end
- end
- function doPaint(events)
- --we'll paint crap to the screen now
- --each x coord is a table containting its y coords. This seems
- --like the most sense making way and should give less worse
- --tearing effects if any.
- if currentColor then--it's a color, not erasing
- --make the table
- if not currentMap[events[4]-scrolly] then
- currentMap[events[4]-scrolly]={}
- end
- if not currentMap[events[4]-scrolly][events[3]-scrollx] then
- currentMap[events[4]-scrolly][events[3]-scrollx] = {}
- end
- currentMap[events[4]-scrolly][events[3]-scrollx]["c"]=currentColor--set the color
- if solid then--we want to be as space conserving as possible and write as little as possible.
- currentMap[events[4]-scrolly][events[3]-scrollx]["s"]=currentSolid--if it's a solid, true, else, false
- end
- --if there's an event we'll want to promp them. We'll use the ui textbox.
- if currentlyEvent then
- term.setCursorPos(46,18)
- term.setTextColor(colors.black)term.setBackgroundColor(colors.white)
- local input=io.read()
- currentMap[events[4]-scrolly][events[3]-scrollx]["event"]=input--write it
- --since we can't serialize type function, we leave it as a string.
- end
- elseif currentMap[events[4]-scrolly]--[[make sure it exists!]] then--we're erasing something
- currentMap[events[4]-scrolly][events[3]-scrollx]=nil --we'll erase the entire X table
- yammount=0
- for _,i in pairs(currentMap[events[4]-scrolly]) do--get #of stuff in table
- yammount=yammount+1
- end
- if yammount ==0 then--if this Y coord is empty
- currentMap[events[4]-scrolly]=nil--erase this Y coord
- end
- end
- end
- function oldsave()
- term.setCursorPos(46,18)--gui textbox location
- term.setBackgroundColor(colors.white) term.setTextColor(colors.black)
- _=fs.open(read(),"w")
- --we now remove whitespace
- theSave = textutils.serialize(currentMap)
- theSave=string.gsub(theSave,"\n","")
- theSave=string.gsub(theSave," ","")
- _.write(theSave)
- _.close()
- term.setCursorPos(46,18)
- print"suc "
- end
- function save()
- packEntireMap()
- theSave=fs.open(io.read(),"w")
- local writeThis=""
- for i,_ in pairs(condensedStrings) do
- writeThis=writeThis..condensedStrings[i]
- writeThis=writeThis.."\n"
- end
- theSave.write(writeThis)
- theSave.close()
- end
- function load()
- end
- function drawBoard()--draw what we've drawn
- for i=1,19 do
- paintutils.drawLine(1,i,44,i,colors.black)
- end
- for y,x in pairs(currentMap) do--for how many Y coords there are
- for c,v in pairs(currentMap[y]) do
- if c+scrollx < 45 then--so it doesn't do gui tearing
- paintutils.drawPixel(c+scrollx,y+scrolly,currentMap[y][c]["c"])
- end
- end
- end
- end
- drawUI()
- while true do
- handleLevelEditor()
- drawBoard()
- end
Advertisement
Add Comment
Please, Sign In to add comment