Skip to content

Commit

Permalink
remake map editor script extensions & fix LODs not creating in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando-A-Rocha committed Oct 11, 2024
1 parent 86b5ead commit b6673ae
Show file tree
Hide file tree
Showing 6 changed files with 596 additions and 526 deletions.
9 changes: 9 additions & 0 deletions [editor]/edf/edf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ local edfCreateBasic = {
object = function(cdata)
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)
if exports.editor_main:getMapSettingValue("useLODs") == true 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
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
135 changes: 128 additions & 7 deletions [editor]/editor_main/server/dumpxml.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local SCRIPTING_EXTENSIONS

local DESTROYED_ELEMENT_DIMENSION = getWorkingDimension() + 1
function toAttribute(value)
if type(value) == "table" then
Expand Down Expand Up @@ -193,37 +195,55 @@ 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_EXTENSIONS["server"]
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
-- 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 scriptExtFileSuccess and (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"
scriptName = "newMapEditorScriptingExtension_c.lua"
scriptContent = SCRIPTING_EXTENSIONS["client"]
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
-- If for some reason the file fails to create, we won't add it to meta.xml
scriptExtFile = fileCreate(":"..getResourceName(resource).."/"..scriptName)
scriptExtFileSuccess = false
if scriptExtFile then
if fileWrite(scriptExtFile, scriptContent) then
scriptExtFileSuccess = true
end
fileClose(scriptExtFile)
end
if scriptExtFileSuccess and (not foundScriptInMeta) then
local scriptNode = xmlCreateChild(xml, "script")
xmlNodeSetAttribute(scriptNode, "src", scriptName)
xmlNodeSetAttribute(scriptNode, "type", "client")
xmlNodeSetAttribute(scriptNode, "validate", "false")
end
fileCopy("client/"..scriptName, ":"..getResourceName(resource).."/"..scriptName, true)

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

SCRIPTING_EXTENSIONS = {
["client"] = [[-- FILE: newMapEditorScriptingExtension_c.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 updatedLodDistances = {}
local function setLODsClient(lodTbl)
for model in pairs(lodTbl) do
updatedLodDistances[model] = true
engineSetModelLODDistance(model, 300)
end
end
addEvent(resourceName..":setLODsClient", true)
addEventHandler(resourceName..":setLODsClient", resourceRoot, setLODsClient)
local function handleClientResourceStop()
for model in pairs(updatedLodDistances) do
engineResetModelLODDistance(model)
end
end
addEventHandler("onClientResourceStop", resourceRoot, handleClientResourceStop, false)]],

["server"] = [[-- 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 = {}
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)
local function onPlayerResourceStart(res)
if res ~= resource then return end
triggerClientEvent(source, resourceName..":setLODsClient", resourceRoot, usedLODModels)
end
addEventHandler("onPlayerResourceStart", root, onPlayerResourceStart)
-- MTA LOD Table [object] = [lodmodel]]
}

SCRIPTING_EXTENSIONS["server"] = SCRIPTING_EXTENSIONS["server"] .. "\n" .. getLodsLuaTableString()
Loading

0 comments on commit b6673ae

Please sign in to comment.