Jump to content

Saving Maps


shaio

Recommended Posts

Hello, I've downloaded offroader23's in-game map editor (offedit) from the community, and I've added one of my old save systems to it, and it doesn't wanna work. I have talked to offroader about this and he can't fix it, I'm just expanding for help.. Here is the full code. Note that it is the only load and save portion of the code that I edited. commands "loadmap" and "savemap" those are what you wanna look at.

 

local playerobj = {}
objectid = {}
maxobjects = 500

function playerLeave ()
	setElementData ( source, "tempbuilder", false )
	setElementData ( source, "permbuilder", false )
end

function playerJoin ()
	setElementData ( source, "tempbuilder", false )
	setElementData ( source, "permbuilder", false )
	local pname = getPlayerName(source)
	if (pname == "example")
	or (pname == "example2")
	then
		setElementData ( source, "permbuilder", true )
	end
end

function thisResourceStart ()
	local players = getElementsByType ( "player" )
	for k,v in ipairs(players) do
		setElementData ( v, "tempbuilder", false )
		setElementData ( v, "permbuilder", false )
		local pname = getPlayerName(v)
	if (pname == "example")
	or (pname == "example2")
		then
			setElementData ( v, "permbuilder", true )
		end
	end
end

addEventHandler ( "onResourceStart", getResourceRootElement(getThisResource()), thisResourceStart )
addEventHandler ( "onPlayerJoin", getRootElement(), playerJoin )
addEventHandler ( "onPlayerQuit", getRootElement(), playerLeave )

function guestBuildPlayer ( player, commandName, player2nick )
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) then
	local player2 = getPlayerFromName ( player2nick )
	local player2n = getPlayerName ( player2 )
	if (getElementData ( player2, "tempbuilder" ) == false) then
		outputChatBox ( "#FFFF00"..player2n.." recieved map editor pass.", getRootElement(), 255, 255, 255, true )
		setElementData ( player2, "tempbuilder" , true )
	else
		outputChatBox ( "#FFFF00"..player2n.." map editor pass removed.", getRootElement(), 255, 255, 255, true )
		setElementData ( player2, "tempbuilder" , false )
	end
else 
	outputChatBox ( "You do not have permission to do that.", player )
end
end
addCommandHandler("guestb", guestBuildPlayer )

function createCurrentObject (player,cmd,objid)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if playerobj[player] then
		outputChatBox ( "You are already editing an object.  Please save or delete to make another.", player )
	else
		local x, y, z = getElementPosition(player)
		destroyMObj (player)
		playerobj[player] = createObject(tonumber(objid), x + 5, y + 5, z - 1)
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "mcreate", createCurrentObject )

function cloneCurrentObject (player, cmd, addx, addy, addz, addrx, addry, addrz)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if addx then 
		addx = tonumber(addx)
	else
		addx = 0
	end
	if addy then 
		addy = tonumber(addy)
	else
		addy = 0
	end
	if addz then 
		addz = tonumber(addz)
	else
		addz = 0
	end
	if addrx then 
		addrx = tonumber(addrx)
	else
		addrx = 0
	end
	if addry then 
		addry = tonumber(addry)
	else
		addry = 0
	end
	if addrz then 
		addrz = tonumber(addrz)
	else
		addrz = 0
	end
	if playerobj[player] then
		local x, y, z = getElementPosition(playerobj[player])
		local rx, ry, rz = getObjectRotation(playerobj[player])
		local model = getElementModel ( playerobj[player] )
		saveObjID(player)
		playerobj[player] = createObject(model, x + addx, y + addy, z + addz, rx + addrx, ry + addry, rz + addrz)
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "mclone", cloneCurrentObject )

function stackCurrentObject (player, cmd, times, addx, addy, addz, addrx, addry, addrz)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if tonumber(times) < 50 and addx then
		times = tonumber(times)
		addx = tonumber(addx)
		if addy then 
			addy = tonumber(addy)
		else
			addy = 0
		end
		if addz then 
			addz = tonumber(addz)
		else
			addz = 0
		end
		if addrx then 
			addrx = tonumber(addrx)
		else	
			addrx = 0
		end
		if addry then 
			addry = tonumber(addry)
		else
			addry = 0
		end
		if addrz then 
			addrz = tonumber(addrz)
		else
			addrz = 0
		end
		if playerobj[player] then
			for i=0,times do
				local x, y, z = getElementPosition(playerobj[player])
				local rx, ry, rz = getObjectRotation(playerobj[player])
				local model = getElementModel ( playerobj[player] )
				saveObjID(player)
				playerobj[player] = createObject(model, x + addx, y + addy, z + addz, rx + addrx, ry + addry, rz + addrz)
			end
		end
	else
		outputChatBox ( "Failed to stack object.  You can only stack up to 50 items at once.  Also make sure you at least specified an X value.", player )
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "mstack", stackCurrentObject )

function loopCurrentObject (player, cmd, pieces, radi, offset, rotaxis, loops, rota)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	local angle
	local newi
	if loops then
		loops = tonumber(loops)
	else
		loops = 1
	end
	if rota then 
		rota = rota*(math.pi/180)
	else 
		rota = 0
	end
	if rotaxis == "x" or rotaxis == "-x" or rotaxis == "y" or rotaxis == "-y" then
	--do nothing
	else
		rotaxis = "x"
	end
	if radi and pieces and offset then
		radi = tonumber(radi)
		pieces = tonumber(pieces)
		offset = tonumber(offset)
		local spiralp = math.atan((offset/2)/(2*radi))/loops
		if playerobj[player] then
			local orx, ory, orz = getElementPosition(playerobj[player])
			newi = -1
			for i=0,pieces do
				local x, y, z = getElementPosition(playerobj[player])
				local rx, ry, rz = getObjectRotation(playerobj[player])
				local model = getElementModel ( playerobj[player] )
				saveObjID(player)
				local weight = 1-(1/(pieces/2))*math.abs((pieces/2)-i)
				local radians = (i/pieces)*(2*math.pi)*loops
				local newx = orx + radi*math.sin(radians)*math.cos(rota)+(offset/2)*math.cos(radians/(2*loops))*-math.sin(rota)
				local newy = ory +(offset/2)*math.cos(radians/(2*loops))*math.cos(rota)+radi*math.sin(radians)*math.sin(rota)
				local newz = orz + radi*-math.cos(radians)
				angle = (((360/pieces)* loops )* newi)
				if angle <= 359.9999999999999999999999999 then
					newi = newi + 1
				end
				angle = (((360/pieces)* loops )* newi)
				if angle >= 360 then
					newi = -1
					angle = (((360/pieces)* loops )* newi)
				end
				if rotaxis == "x" then
					newrotx = angle
					newroty = ry --+ ((radians)-math.cos(rota)*spiralp*weight)
				elseif rotaxis == "-x" then
					newrotx = -angle
					newroty = ry --+ ((radians)-math.cos(rota)*spiralp*weight)
				elseif rotaxis == "y" then
					newrotx = rx --+ ((radians)-math.cos(rota)*spiralp*weight)
					newroty = angle
				elseif rotaxis == "-y" then
					newrotx = rx --+ ((radians)-math.cos(rota)*spiralp*weight)
					newroty = -angle
				end
				playerobj[player] = createObject(model, newx, newy, newz, newrotx, newroty, rz)
			end
		end
	else
		outputChatBox ( "Failed to make loop.  Use /mloop <radius> <pieces> <offset>", player )
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "mloop", loopCurrentObject )

function moveObjZ (player,cmd,newz)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if playerobj[player] then
		local x, y, z = getElementPosition(playerobj[player])
		moveObject ( playerobj[player], 200, x, y, z + tonumber(newz) )
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "oz", moveObjZ )

function moveObjX (player,cmd,newx)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if playerobj[player] then
		local x, y, z = getElementPosition(playerobj[player])
		moveObject ( playerobj[player], 200, x + tonumber(newx), y, z )
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "ox", moveObjX )

function moveObjY (player,cmd,newy)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if playerobj[player] then
		local x, y, z = getElementPosition(playerobj[player])
		moveObject ( playerobj[player], 200, x, y + tonumber(newy), z )
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "oy", moveObjY )

function moveRotZ (player,cmd,newrz)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if playerobj[player] then
		local x, y, z = getElementPosition(playerobj[player])
		moveObject ( playerobj[player], 200, x, y, z, 0, 0, tonumber(newrz) )
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "rz", moveRotZ )

function moveRotX (player,cmd,newrx)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if playerobj[player] then
		local x, y, z = getElementPosition(playerobj[player])
		moveObject ( playerobj[player], 200, x, y, z, tonumber(newrx), 0, 0 )
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "rx", moveRotX )

function moveRotY (player,cmd,newry)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if playerobj[player] then
		local x, y, z = getElementPosition(playerobj[player])
		moveObject ( playerobj[player], 200, x, y, z, 0, tonumber(newry), 0 )
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "ry", moveRotY )

function destroyMObj (player,cmd)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if playerobj[player] then
		for i=0,maxobjects do
			if objectid[i] == playerobj[player] then 
				local numstr = tostring(i)
				outputChatBox ( "Object ID deleted: " ..numstr.. "", player )
				local succeed = destroyElement(playerobj[player])
				objectid[i] = nil
				playerobj[player] = nil
				break
			end
		end
	end
	if playerobj[player] then destroyElement(playerobj[player])	end
	playerobj[player] = nil
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "mdestroy", destroyMObj )

function openTheFile(objFile)
	local mapFile = fileOpen ( "objects.txt" )
	if not mapFile then
		outputChatBox ( "objects.txt not Found...")
		mapFile = fileCreate( "objects.txt" )
		fileWrite (mapFile, "--Objects Script Created\r\n\r\n")
		outputChatBox ( "File Created")
	end
	return mapFile
end

function commentFix ( comment )
	if not ( comment ) then comment = "na" end
return comment
end

function saveObjID ( player, cmd )
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if playerobj[player] then
		for i=0,maxobjects do
			if objectid[i] == playerobj[player] then 
				local numstr = tostring(i)
				outputChatBox ( "Object saved as ID: " ..numstr.. "", player )
				playerobj[player] = nil
				break
			elseif objectid[i] then 
			--do nothing
			else
				objectid[i] = playerobj[player]
				local numstr = tostring(i)
				outputChatBox ( "Object saved as ID: " ..numstr.. "", player )
				playerobj[player] = nil
				break
			end
		end
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "msave", saveObjID )

function selectObject ( player, cmd, selid )
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	num = tonumber(selid)
	if playerobj[player] then
		saveObjID(player)
		outputChatBox ( "You were editing an object.  It was saved as ID: " ..selid.. "", player )
	end
	if objectid[num] then 
		playerobj[player] = objectid[num] 
		local x, y, z = getElementPosition(playerobj[player])
		outputChatBox ( "Object ID selected: " ..selid.. "", player )
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "msel", selectObject )

function getEndOFile(mapFile)
  local temp, endPos = fileGetPos ( mapFile )
  while not fileIsEOF ( mapFile ) do    
    fileRead ( mapFile, 500 )                      
  end
  endPos = fileGetPos ( mapFile )
  fileSetPos ( mapFile, temp )
  return endPos
end

function getObjInfo ( player)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	if playerobj[player] then
		local model = getElementModel (playerobj[player])
		local x, y, z = getElementPosition(playerobj[player])
		local rx, ry, rz = getObjectRotation(playerobj[player])
		outputChatBox ( "Object Info - Object Model ID: " .. model .. "", player)
		outputChatBox ( "Pos X: " .. x .. " Pos Y: " .. y .. " Pos Z: " .. z .. "", player)
		outputChatBox ( "Rot X: " .. rx .. " Rot Y: " .. ry .. " Rot Z: " .. rz .. "", player)
	else
		outputChatBox ( "You currently have no object selected.", player)
	end
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "minfo", getObjInfo )

function saveObjPOS ( player, cmd, comment )
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true) or (getElementData ( player, "tempbuilder" ) == true) then 
	comment = commentFix ( comment )
	local model = getElementModel ( playerobj[player] )
	local x, y, z = getElementPosition(playerobj[player])
	local rx, ry, rz = getObjectRotation(playerobj[player])
	local pname = getPlayerName(player)
	local objFile = fileOpen ( "objects.txt" )
	if objFile then
		local endPos = getEndOFile (objFile)
		fileSetPos(objFile, endPos)
	else
		outputChatBox ( "objects.txt not Found...")
		objFile = fileCreate( "objects.txt" )
		fileSetPos(objFile, 0)
		fileWrite (objFile, "--Objects Script Created Using OffRoader23's In Game Editor\r\n\r\n")
		outputChatBox ( "File Created")
	end
	fileWrite ( objFile, "createObject(" .. model .. ", " .. x .. ", " .. y .. ", " .. z .. ", " .. rx .. ", " .. ry .. ", " .. rz .. ") --" .. comment .. " " .. pname .. "\r\n")
	outputChatBox ( "Object Saved as: " ..comment.. "", player )
	fileClose ( objFile )
	saveObjID (player)
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "saveobj", saveObjPOS )


function saveMap(player,cmd,mapName) 
    local account = getPlayerAccount(player) 
    if (isGuestAccount(account) == true) then 
        outputChatBox("You need to be logged in to use this!", player, 0, 255, 255) 
    return end 
    if mapName then 
        if not string.find(mapName, '%W') then 
            if (getElementData(player,"tempbuilder") == true) then 
                if playerobj then
                    if #playerobj >= 1 then 
                        local tempdata = {}  
                        tempdata['info'] = {} 
                        tempdata['info']['creator'] = getPlayerName(player) 
                        tempdata['info']['serial'] = getPlayerSerial(player) 
                        tempdata['info']['ip'] = getPlayerIP(player)
                        tempdata['info']['dimension'] = getElementDimension(playerobj[i]) 
                        for _,i in ipairs(playerobj[player]) do 
                            if playerobj[player][i] then 
                                tempdata[i] = {} 
                                tempdata[i]['model'] = getElementModel(playerobj[i]) 
                                tempdata[i]['col'] = getElementCollisionsEnabled(playerobj[i]) 
                                local x, y, z = getElementPosition(playerobj[i]) 
								local rx, ry, rz = getElementRotation(playerobj[i]) 
                                tempdata[i]['pos'] = {x, y, z, rx, ry, rz} 
                                local scalex, scaley, scalez = getObjectScale(playerobj[i]) 
                                tempdata[i]['scale'] = {scalex, scaley, scalez} 
                                tempdata[i]['type'] = 'object'
                            end 
                        end 
                        local file = fileCreate('maps/'..mapName..'.json') 
                        fileWrite(file, toJSON(tempdata)) 
                        fileClose(file)   
                        tempdata = {} 
                        outputChatBox('Saved map as '..mapName, player, 0, 255, 255) 
                    end 
                end 
            end 
        else 
            outputChatBox('Invalid map name.', player, 0, 255, 255) 
        end 
    else 
        outputChatBox('SYNTAX: /savemap name', player, 0, 255, 255) 
    end 
end
addCommandHandler("savemap",saveMap)

function loadMap(player,cmd,mapName) 
    local account = getPlayerAccount(player) 
    if (isGuestAccount(account) == true) then 
        outputChatBox("You need to be logged in to use this!", player, 0, 255, 255) 
    return end 
    if fileExists('maps/'..mapName..'.json') then
        if isElement(player) then
            outputChatBox('Map loaded: '..mapName, player, 0, 255, 255) 
            name = getPlayerName(player) 
        else 
            name = 'the server' 
        end 
        local file = fileOpen('maps/'..mapName..'.json') 
        local size = fileGetSize(file)
        local buffer = fileRead(file, size)
        local tempdata = fromJSON(buffer)
        fileClose(file)
        for k,v in pairs (tempdata) do 
            if k ~= 'info' then 
                local id = tonumber(k)
                local ids = tostring(k)
				local x, y, z, rx, ry, rz = unpack(tempdata[id]['pos'])
                playerobj[id] = createObject(tempdata[ids]['model'], x, y, z, rx, ry, rz) 
                scalex, scaley, scalez = unpack(tempdata[ids]['scale']) 
                setObjectScale(playerobj[id], scalex, scaley, scalez)
                setElementCollisionsEnabled(playerobj[id], tempdata[ids]['col']) 
            end 
        end
       else 
        outputChatBox("Map "..mapName.." failed to load.", player, 0, 255, 255) 
    end 
end
addCommandHandler("loadmap",loadMap)

function clearObjects (player)
if ( hasObjectPermissionTo ( player, "function.unbanSerial", true)) or (getElementData ( player, "permbuilder" ) == true)  then 
	for i=0,maxobjects do
		if objectid[i] then
			destroyElement(objectid[i])
		end
	end
	objectid = nil
	objectid = {}
	local players = getElementsByType ( "player" )
	for k,v in ipairs(players) do
		if playerobj[v] then destroyElement(playerobj[v]) end
		playerobj[v] = nil
	end
	if playerobj[player] then destroyElement(playerobj[player]) end
	playerobj[player] = nil
	outputChatBox ( "Map cleared.", player )
else 
	outputChatBox ( "You do not have permission to build.", player )
end
end
addCommandHandler( "mclear", clearObjects )

 

There are no errors in console or debugscript.

Edited by shaio
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...