Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editor: multiple fixes & improvements related to LODs #556

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions [editor]/edf/edf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,19 @@ end

-- basic element create functions table (cdata holds creation parameters)
local edfCreateBasic = {
object = function(cdata)
object = function(cdata, editorMode)
local object = createObject(cdata.model, cdata.position[1], cdata.position[2], cdata.position[3], cdata.rotation[1], cdata.rotation[2], cdata.rotation[3])
setObjectScale(object, cdata.scale)
-- Create object's LOD if editing a map in Map Editor & LODs are enabled in the map's settings
if editorMode and exports.editor_main:getMapSettingValue("useLODs") then
local lodModel = getObjectLowLODModel(cdata.model)
if lodModel then
local lodObject = createObject(lodModel, cdata.position[1], cdata.position[2], cdata.position[3], cdata.rotation[1], cdata.rotation[2], cdata.rotation[3], true)
setObjectScale(lodObject, cdata.scale)
setLowLODElement(object, lodObject)
setElementParent(lodObject, object)
end
end
return object
end,
vehicle = function(cdata)
Expand Down Expand Up @@ -498,7 +508,7 @@ function edfRepresentElement(theElement, resource, parentData, editorMode, restr
end

-- create our basic element
component = edfCreateBasic[definedChild.type](childData)
component = edfCreateBasic[definedChild.type](childData, editorMode)

componentHandle = component

Expand Down Expand Up @@ -596,7 +606,7 @@ function edfCreateElement(elementType, creatorClient, fromResource, parametersTa
childData[property] = parametersTable[property] or propertyData.default
end

theElement = edfCreateBasic[elementType](childData)
theElement = edfCreateBasic[elementType](childData, editorMode)

setElementInterior(theElement, parametersTable.interior)
setElementDimension(theElement, parametersTable.dimension)
Expand Down
1 change: 1 addition & 0 deletions [editor]/edf/meta.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<meta>
<info author="jbeta" description="Element Definition Format" edf:definition="edf.edf" version="1.0.0" />

<!-- TODO: bump min server version due to getObjectLowLODModel -->
<min_mta_version server="1.6.0-9.21765" />

<script src="sharedutil.lua" type="shared" />
Expand Down
11 changes: 0 additions & 11 deletions [editor]/editor_main/client/mapEditorScriptingExtension_c.lua

This file was deleted.

2 changes: 2 additions & 0 deletions [editor]/editor_main/meta.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<meta>
<info author="erorr404,jbeta,Talidan" description="Map editor's main component" edf:definition="editor_main.edf" version="1.0.0" />

<!-- TODO: bump min server version due to getObjectLowLODModel -->
<min_mta_version server="1.5.8-9.20957" />

<!-- Script to shutdown the running gamemode and maps, because otherwise you get trouble -->
Expand Down Expand Up @@ -50,6 +51,7 @@
<script src="server/synchronization.lua" type="server" />
<script src="server/locking.lua" type="server" />
<script src="server/mapsettingssync.lua" type="server" />
<export function="getMapSettingValue" type="server" />
<script src="server/definitionssync.lua" type="server" />

<!-- ID assignment handling code -->
Expand Down
97 changes: 78 additions & 19 deletions [editor]/editor_main/server/dumpxml.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local SCRIPTING_EXTENSION_CODE

local DESTROYED_ELEMENT_DIMENSION = getWorkingDimension() + 1
function toAttribute(value)
if type(value) == "table" then
Expand Down Expand Up @@ -193,37 +195,30 @@ function dumpMeta ( xml, extraNodes, resource, filename, test )
end
end

--Add the mapEditorScriptingExtension scripts to meta
local scriptName = "mapEditorScriptingExtension_s.lua"
--Add the map editor scripting extension scripts to meta
local scriptName = "newMapEditorScriptingExtension_s.lua"
local scriptContent = SCRIPTING_EXTENSION_CODE
local foundScriptInMeta = false
for i, child in ipairs(xmlNodeGetChildren(xml)) do
if (xmlNodeGetAttribute(child, "src") == scriptName) then
foundScriptInMeta = true
break
end
end
if (not foundScriptInMeta) then
local scriptNode = xmlCreateChild(xml, "script")
xmlNodeSetAttribute(scriptNode, "src", scriptName)
xmlNodeSetAttribute(scriptNode, "type", "server")
end
fileCopy("server/"..scriptName, ":"..getResourceName(resource).."/"..scriptName, true)

scriptName = "mapEditorScriptingExtension_c.lua"
foundScriptInMeta = false
for i, child in ipairs(xmlNodeGetChildren(xml)) do
if (xmlNodeGetAttribute(child, "src") == scriptName) then
foundScriptInMeta = true
break
-- If for some reason the file fails to create, we won't add it to meta.xml
local scriptExtFile = fileCreate(":"..getResourceName(resource).."/"..scriptName)
local scriptExtFileSuccess = false
if scriptExtFile then
if fileWrite(scriptExtFile, scriptContent) then
scriptExtFileSuccess = true
end
fileClose(scriptExtFile)
end
if (not foundScriptInMeta) then
if scriptExtFileSuccess and (not foundScriptInMeta) then
local scriptNode = xmlCreateChild(xml, "script")
xmlNodeSetAttribute(scriptNode, "src", scriptName)
xmlNodeSetAttribute(scriptNode, "type", "client")
xmlNodeSetAttribute(scriptNode, "validate", "false")
xmlNodeSetAttribute(scriptNode, "type", "server")
end
fileCopy("client/"..scriptName, ":"..getResourceName(resource).."/"..scriptName, true)

return xmlSaveFile(xml)
end
Expand All @@ -239,3 +234,67 @@ function getMapElementData ( element )
end
return elementData
end

SCRIPTING_EXTENSION_CODE = [[-- FILE: newMapEditorScriptingExtension_s.lua
-- PURPOSE: Prevent the map editor feature set being limited by what MTA can load from a map file by adding a script file to maps
-- VERSION: November 20, 2024
-- IMPORTANT: Check the resource 'editor_main' at https://github.com/mtasa-resources/ for updates

-- Makes removeWorldObject map entries and Object LOD work
local function onResourceStartOrStop(startedResource)
local startEvent = eventName == "onResourceStart"

-- On start: remove world objects
-- On stop: restore the removed world objects
local removeObjectsTable = getElementsByType("removeWorldObject", source)
for i = 1, #removeObjectsTable do
local objectElement = removeObjectsTable[i]
local objectModel = getElementData(objectElement, "model")
local objectLODModel = getElementData(objectElement, "lodModel")
local posX = getElementData(objectElement, "posX")
local posY = getElementData(objectElement, "posY")
local posZ = getElementData(objectElement, "posZ")
local objectInterior = getElementData(objectElement, "interior") or 0
local objectRadius = getElementData(objectElement, "radius")
if startEvent then
removeWorldModel(objectModel, objectRadius, posX, posY, posZ, objectInterior)
removeWorldModel(objectLODModel, objectRadius, posX, posY, posZ, objectInterior)
else
restoreWorldModel(objectModel, objectRadius, posX, posY, posZ, objectInterior)
restoreWorldModel(objectLODModel, objectRadius, posX, posY, posZ, objectInterior)
end
end

-- On start only: create Low Level of Detail objects if enabled
if not startEvent then
return
end
local useLODs = get(getResourceName(resource)..".useLODs")
if not useLODs then
return
end
local objectsTable = getElementsByType("object", source)
for i = 1, #objectsTable do
local objectElement = objectsTable[i]
local objectModel = getElementModel(objectElement)
local lodModel = getObjectLowLODModel(objectModel)
if lodModel then
local objectX, objectY, objectZ = getElementPosition(objectElement)
local objectRX, objectRY, objectRZ = getElementRotation(objectElement)
local objectScaleX, objectScaleY, objectScaleZ = getObjectScale(objectElement)
local objectInterior = getElementInterior(objectElement)
local objectDimension = getElementDimension(objectElement)
local lodObject = createObject(lodModel, objectX, objectY, objectZ, objectRX, objectRY, objectRZ, true)
if lodObject then
setObjectScale(lodObject, objectScaleX, objectScaleY, objectScaleZ)
setElementInterior(lodObject, objectInterior)
setElementDimension(lodObject, objectDimension)
setElementParent(lodObject, objectElement)
setLowLODElement(objectElement, lodObject)
end
end
end
end
addEventHandler("onResourceStart", resourceRoot, onResourceStartOrStop, false)
addEventHandler("onResourceStop", resourceRoot, onResourceStartOrStop, false)
]]
Loading