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

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
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 = exports.editor_main:getLODModelFromObjectModel(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
11 changes: 0 additions & 11 deletions [editor]/editor_main/client/mapEditorScriptingExtension_c.lua

This file was deleted.

3 changes: 3 additions & 0 deletions [editor]/editor_main/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<!-- Utils -->
<script src="client/controlinterface.lua" type="client" />
<script src="server/util.lua" type="server" />
<script src="server/lods.lua" type="server" />
<export function="getLODModelFromObjectModel" type="server" />
<script src="server/orderedPairs.lua" type="server" />
<script src="client/matrix.lua" type="client" />
<script src="client/complex.lua" type="client" />
Expand Down Expand Up @@ -50,6 +52,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
103 changes: 84 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,73 @@ 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: 11/October/2024
-- IMPORTANT: Check the resource 'editor_main' at https://github.com/mtasa-resources/ for updates

local resourceName = getResourceName(resource)
local usedLODModels = {}
local LOD_MAP_NEW = {}

-- Makes removeWorldObject map entries and LODs work
local function onResourceStartOrStop(startedResource)
local startEvent = eventName == "onResourceStart"
local removeObjects = getElementsByType("removeWorldObject", source)

for removeID = 1, #removeObjects do
local objectElement = removeObjects[removeID]
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

if startEvent then
local useLODs = get(resourceName..".useLODs")

if useLODs then
local objectsTable = getElementsByType("object", source)

for objectID = 1, #objectsTable do
local objectElement = objectsTable[objectID]
local objectModel = getElementModel(objectElement)
local lodModel = LOD_MAP_NEW[objectModel]

if lodModel then
local objectX, objectY, objectZ = getElementPosition(objectElement)
local objectRX, objectRY, objectRZ = getElementRotation(objectElement)
local objectInterior = getElementInterior(objectElement)
local objectDimension = getElementDimension(objectElement)
local lodObject = createObject(lodModel, objectX, objectY, objectZ, objectRX, objectRY, objectRZ, true)

setElementInterior(lodObject, objectInterior)
setElementDimension(lodObject, objectDimension)

setElementParent(lodObject, objectElement)
setLowLODElement(objectElement, lodObject)

usedLODModels[lodModel] = true
end
end
end
end
end
addEventHandler("onResourceStart", resourceRoot, onResourceStartOrStop, false)
addEventHandler("onResourceStop", resourceRoot, onResourceStartOrStop, false)

-- MTA LOD Table [object] = [lodmodel]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs one more closing ] at the end, two to close the multi-line string and one to encapsulate 'lodmodel' in the final file

-- MTA LOD Table [object] = [lodmodel]]]


SCRIPTING_EXTENSION_CODE = SCRIPTING_EXTENSION_CODE .. "\n" .. getLodsLuaTableString()
Fernando-A-Rocha marked this conversation as resolved.
Show resolved Hide resolved
Loading