LUADOC - Farming Simulator 19

ObjectChangeUtil

Functions

loadObjectChangeFromXML

Description
Load object change from xml
Definition
loadObjectChangeFromXML(integer xmlFile, string key, table objects, integer rootNode, table parent)
Arguments
integerxmlFilefile id of xml file
stringkeykey
tableobjectstable to insert loaded objects
integerrootNodeid of root node
tableparentparent
Code
12function ObjectChangeUtil.loadObjectChangeFromXML(xmlFile, key, objects, rootNode, parent)
13 local i = 0
14 while true do
15 local nodeKey = string.format(key .. ".objectChange(%d)", i)
16 if not hasXMLProperty(xmlFile, nodeKey) then
17 break
18 end
19 local node = I3DUtil.indexToObject(rootNode, getXMLString(xmlFile, nodeKey .. "#node"), parent.i3dMappings)
20 if node ~= nil then
21 local object = {}
22 object.node = node
23 ObjectChangeUtil.loadValuesFromXML(xmlFile, nodeKey, node, object, parent)
24 table.insert(objects, object)
25 end
26 i = i + 1
27 end
28end

loadValuesFromXML

Description
Load object values from xml
Definition
loadValuesFromXML(integer xmlFile, string key, integer node, table object, table parent)
Arguments
integerxmlFilefile id of xml file
stringkeykey
integernodenode id to load from
tableobjecttable to insert loaded data
tableparentparent
Code
37function ObjectChangeUtil.loadValuesFromXML(xmlFile, key, node, object, parent)
38 object.visibilityActive = getXMLBool(xmlFile, key.."#visibilityActive")
39 object.visibilityInactive = getXMLBool(xmlFile, key.."#visibilityInactive")
40 object.translationActive = StringUtil.getVectorNFromString(getXMLString(xmlFile, key.."#translationActive"), 3)
41 object.translationInactive = StringUtil.getVectorNFromString(getXMLString(xmlFile, key.."#translationInactive"), 3)
42 object.rotationActive = StringUtil.getRadiansFromString(getXMLString(xmlFile, key.."#rotationActive"), 3)
43 object.rotationInactive = StringUtil.getRadiansFromString(getXMLString(xmlFile, key.."#rotationInactive"), 3)
44 object.scaleActive = StringUtil.getVectorNFromString(getXMLString(xmlFile, key.."#scaleActive"), 3)
45 object.scaleInactive = StringUtil.getVectorNFromString(getXMLString(xmlFile, key.."#scaleInactive"), 3)
46
47 XMLUtil.checkDeprecatedXMLElements(xmlFile, "", key.."#collisionActive", key.."#compoundChildActive or #rigidBodyTypeActive") --FS17 to FS19
48 XMLUtil.checkDeprecatedXMLElements(xmlFile, "", key.."#collisionInactive", key.."#compoundChildInactive or #rigidBodyTypeInactive") --FS17 to FS19
49
50 object.massActive = nil
51 object.massInactive = nil
52 local massActive = getXMLFloat(xmlFile, key.."#massActive")
53 if massActive ~= nil then
54 object.massActive = massActive / 1000
55 end
56 local massInactive = getXMLFloat(xmlFile, key.."#massInactive")
57 if massInactive ~= nil then
58 object.massInactive = massInactive / 1000
59 end
60 object.centerOfMassActive = StringUtil.getVectorNFromString(getXMLString(xmlFile, key.."#centerOfMassActive"), 3)
61 object.centerOfMassInactive = StringUtil.getVectorNFromString(getXMLString(xmlFile, key.."#centerOfMassInactive"), 3)
62 object.compoundChildActive = getXMLBool(xmlFile, key.."#compoundChildActive")
63 object.compoundChildInactive = getXMLBool(xmlFile, key.."#compoundChildInactive")
64 object.rigidBodyTypeActive = getXMLString(xmlFile, key.."#rigidBodyTypeActive")
65 object.rigidBodyTypeInactive = getXMLString(xmlFile, key.."#rigidBodyTypeInactive")
66
67 if object.rigidBodyTypeActive ~= nil then
68 local t = object.rigidBodyTypeActive
69 if t ~= "Static" and t ~= "Dynamic" and t ~= "Kinematic" and t ~= "NoRigidBody" then
70 g_logManager:warning("Invalid rigidBodyTypeActive '%s' for object change node '%s'. Use 'Static', 'Dynamic', 'Kinematic' or 'NoRigidBody'!", t, key)
71 object.rigidBodyTypeActive = nil
72 end
73 end
74 if object.rigidBodyTypeInactive ~= nil then
75 local t = object.rigidBodyTypeInactive
76 if t ~= "Static" and t ~= "Dynamic" and t ~= "Kinematic" and t ~= "NoRigidBody" then
77 g_logManager:warning("Invalid rigidBodyTypeInactive '%s' for object change node '%s'. Use 'Static', 'Dynamic', 'Kinematic' or 'NoRigidBody'!", t, key)
78 object.rigidBodyTypeInactive = nil
79 end
80 end
81
82 if parent ~= nil and parent.loadObjectChangeValuesFromXML ~= nil then
83 parent:loadObjectChangeValuesFromXML(xmlFile, key, node, object)
84 end
85end

setObjectChange

Description
Set object change
Definition
setObjectChange(table object, boolean isActive, table target, function updateFunc)
Arguments
tableobjectobjects to change
booleanisActiveis active
tabletargettarget for updateFunc
functionupdateFuncfunction to update
Code
105function ObjectChangeUtil.setObjectChange(object, isActive, target, updateFunc)
106 if isActive then
107 if object.visibilityActive ~= nil then
108 setVisibility(object.node, object.visibilityActive)
109 end
110 if object.translationActive ~= nil then
111 setTranslation(object.node, object.translationActive[1], object.translationActive[2], object.translationActive[3])
112 end
113 if object.rotationActive ~= nil then
114 setRotation(object.node, object.rotationActive[1], object.rotationActive[2], object.rotationActive[3])
115 end
116 if object.scaleActive ~= nil then
117 setScale(object.node, object.scaleActive[1], object.scaleActive[2], object.scaleActive[3])
118 end
119 if object.massActive ~= nil then
120 setMass(object.node, object.massActive)
121
122 if target ~= nil and target.components ~= nil then
123 for _, component in ipairs(target.components) do
124 if component.node == object.node then
125 component.defaultMass = object.massActive
126 target:setMassDirty()
127 end
128 end
129 end
130 end
131 if object.centerOfMassActive ~= nil then
132 setCenterOfMass(object.node, unpack(object.centerOfMassActive))
133 end
134 if object.compoundChildActive ~= nil then
135 setIsCompoundChild(object.node, object.compoundChildActive)
136 end
137 if object.rigidBodyTypeActive ~= nil then
138 setRigidBodyType(object.node, object.rigidBodyTypeActive)
139 end
140 else
141 if object.visibilityInactive ~= nil then
142 setVisibility(object.node, object.visibilityInactive)
143 end
144 if object.translationInactive ~= nil then
145 setTranslation(object.node, object.translationInactive[1], object.translationInactive[2], object.translationInactive[3])
146 end
147 if object.rotationInactive ~= nil then
148 setRotation(object.node, object.rotationInactive[1], object.rotationInactive[2], object.rotationInactive[3])
149 end
150 if object.scaleInactive ~= nil then
151 setScale(object.node, object.scaleInactive[1], object.scaleInactive[2], object.scaleInactive[3])
152 end
153 if object.massInactive ~= nil then
154 setMass(object.node, object.massInactive)
155
156 if target ~= nil and target.components ~= nil then
157 for _, component in ipairs(target.components) do
158 if component.node == object.node then
159 component.defaultMass = object.massInactive
160 target:setMassDirty()
161 end
162 end
163 end
164 end
165 if object.centerOfMassInactive ~= nil then
166 setCenterOfMass(object.node, unpack(object.centerOfMassInactive))
167 end
168 if object.compoundChildInactive ~= nil then
169 setIsCompoundChild(object.node, object.compoundChildInactive)
170 end
171 if object.rigidBodyTypeInactive ~= nil then
172 setRigidBodyType(object.node, object.rigidBodyTypeInactive)
173 end
174 end
175 if target ~= nil then
176 if target.setObjectChangeValues ~= nil then
177 target:setObjectChangeValues(object, isActive)
178 end
179 if updateFunc ~= nil then
180 updateFunc(target, object.node)
181 end
182 end
183end

setObjectChanges

Description
Set object changes
Definition
setObjectChanges(table object, boolean isActive, table target, function updateFunc)
Arguments
tableobjectobjects to change
booleanisActiveis active
tabletargettarget for updateFunc
functionupdateFuncfunction to update
Code
93function ObjectChangeUtil.setObjectChanges(objects, isActive, target, updateFunc)
94 for _, object in pairs(objects) do
95 ObjectChangeUtil.setObjectChange(object, isActive, target, updateFunc)
96 end
97end

updateObjectChanges

Description
Update object changes
Definition
updateObjectChanges(integer xmlFile, string key, integer configKey, integer rootNode, table parent)
Arguments
integerxmlFilefile id of xml file
stringkeykey
integerconfigKeyid of used config
integerrootNodeid of root node
tableparentparent
Code
192function ObjectChangeUtil.updateObjectChanges(xmlFile, key, configKey, rootNode, parent)
193 local i = 0
194 local activeI = (configKey - 1)
195 while true do
196 local objectChangeKey = string.format(key.."(%d)", i)
197 if not hasXMLProperty(xmlFile, objectChangeKey) then
198 break
199 end
200 if i ~= activeI then
201 local objects = {}
202 ObjectChangeUtil.loadObjectChangeFromXML(xmlFile, objectChangeKey, objects, rootNode, parent)
203 ObjectChangeUtil.setObjectChanges(objects, false, parent)
204 end
205 i = i + 1
206 end
207
208 -- Set the active config last so that it can overwrite settings of inactive configurations
209 if i > activeI then
210 local objectChangeKey = string.format(key.."(%d)", activeI)
211 local objects = {}
212 ObjectChangeUtil.loadObjectChangeFromXML(xmlFile, objectChangeKey, objects, rootNode, parent)
213 ObjectChangeUtil.setObjectChanges(objects, true, parent)
214 end
215end