160 | function PlaceableTriggerMarkers:onDelete() |
161 | local spec = self.spec_triggerMarkers |
162 | if spec.sharedLoadRequestIds ~= nil then |
163 | for _, sharedLoadRequestId in ipairs(spec.sharedLoadRequestIds) do |
164 | g_i3DManager:releaseSharedI3DFile(sharedLoadRequestId) |
165 | end |
166 | spec.sharedLoadRequestIds = nil |
167 | end |
168 | |
169 | if spec.triggerMarkers ~= nil then |
170 | for _, marker in ipairs(spec.triggerMarkers) do |
171 | g_currentMission:removeTriggerMarker(marker.node) |
172 | end |
173 | end |
174 | |
175 | g_messageCenter:unsubscribe(MessageType.PLAYER_FARM_CHANGED, self) |
176 | g_messageCenter:unsubscribe(MessageType.PLAYER_CREATED, self) |
177 | end |
181 | function PlaceableTriggerMarkers:onFinalizePlacement() |
182 | local spec = self.spec_triggerMarkers |
183 | for _, marker in ipairs(spec.triggerMarkers) do |
184 | if marker.adjustToGround and g_currentMission.terrainRootNode ~= nil then |
185 | local x, _, z = getWorldTranslation(marker.node) |
186 | local y = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x, 0, z) + marker.groundOffset |
187 | setWorldTranslation(marker.node, x, y, z) |
188 | end |
189 | end |
190 | |
191 | self:setShowMarkers(g_currentMission.accessHandler:canPlayerAccess(self)) |
192 | |
193 | -- Get notified when the player visibility state changes |
194 | g_messageCenter:subscribe(MessageType.PLAYER_FARM_CHANGED, self.onPlayerFarmChanged, self) |
195 | g_messageCenter:subscribe(MessageType.PLAYER_CREATED, self.onPlayerFarmChanged, self) |
196 | end |
55 | function PlaceableTriggerMarkers:onLoad(savegame) |
56 | local spec = self.spec_triggerMarkers |
57 | local xmlFile = self.xmlFile |
58 | |
59 | spec.sharedLoadRequestIds = {} |
60 | |
61 | spec.triggerMarkers = {} |
62 | xmlFile:iterate("placeable.triggerMarkers.triggerMarker", function(_, key) |
63 | local node = xmlFile:getValue(key .. "#node", nil, self.components, self.i3dMappings) |
64 | if node ~= nil then |
65 | local adjustToGround = xmlFile:getValue(key .. "#adjustToGround", false) |
66 | local groundOffset = xmlFile:getValue(key .. "#groundOffset") |
67 | |
68 | if groundOffset ~= nil and not adjustToGround then |
69 | Logging.xmlWarning(xmlFile, "'groundOffset=%.2f' given but 'adjustToGround' is false for '%s'", groundOffset, key) |
70 | end |
71 | groundOffset = groundOffset or 0.03 -- 3cm as default to prevent clipping of flat markers with terrain |
72 | |
73 | --#debug if not getEffectiveVisibility(getParent(node)) then |
74 | --#debug Logging.xmlWarning(xmlFile, "Parent of triggerMarker link node '%s' (%s) is not visible", getName(node), key .. "#node") |
75 | --#debug end |
76 | |
77 | local xmlFilename = self.xmlFile:getValue(key .. "#filename") |
78 | local i3dFilename |
79 | if xmlFilename ~= nil then |
80 | local id = self.xmlFile:getValue(key .. "#id") |
81 | if id == nil then |
82 | Logging.xmlWarning(xmlFile, "Missing marker id for '%s'", key) |
83 | return |
84 | end |
85 | |
86 | id = string.upper(id) |
87 | |
88 | xmlFilename = Utils.getFilename(xmlFilename, self.baseDirectory) |
89 | local markerXMLFile = XMLFile.load("triggerNode", xmlFilename, nil) |
90 | if markerXMLFile == nil then |
91 | Logging.xmlWarning(xmlFile, "Could not load marker config file '%s'", xmlFilename) |
92 | return |
93 | end |
94 | |
95 | i3dFilename = markerXMLFile:getString("markerIcons.filename") |
96 | if i3dFilename == nil then |
97 | Logging.xmlWarning(xmlFile, "Missing marker i3d file in '%s'", markerXMLFile) |
98 | return |
99 | end |
100 | |
101 | local loadingTask = self:createLoadingTask() |
102 | i3dFilename = Utils.getFilename(i3dFilename, self.baseDirectory) |
103 | local args = {} |
104 | args.markerXMLFile = markerXMLFile |
105 | args.node = node |
106 | args.markerId = id |
107 | args.loadingTask = loadingTask |
108 | |
109 | local sharedLoadRequestId = g_i3DManager:loadSharedI3DFileAsync(i3dFilename, false, false, self.onMarkerFileLoaded, self, args) |
110 | table.insert(spec.sharedLoadRequestIds, sharedLoadRequestId) |
111 | end |
112 | |
113 | table.insert(spec.triggerMarkers, {node=node, i3dFilename=i3dFilename, adjustToGround=adjustToGround, groundOffset=groundOffset}) |
114 | else |
115 | Logging.xmlWarning(xmlFile, "Missing trigger marker node for '%s'", key) |
116 | end |
117 | end) |
118 | end |
122 | function PlaceableTriggerMarkers:onMarkerFileLoaded(i3dNode, failedReason, args) |
123 | local markerXMLFile = args.markerXMLFile |
124 | local node = args.node |
125 | local markerId = args.markerId |
126 | local loadingTask = args.loadingTask |
127 | |
128 | if i3dNode ~= 0 then |
129 | local found = false |
130 | markerXMLFile:iterate("markerIcons.variations.variation", function(_, key) |
131 | local variationId = markerXMLFile:getString(key .. "#id") |
132 | if variationId ~= nil then |
133 | variationId = string.upper(variationId) |
134 | if variationId == markerId then |
135 | local markerNode = I3DUtil.indexToObject(i3dNode, markerXMLFile:getString(key .. "#node")) |
136 | if markerNode ~= nil then |
137 | link(node, markerNode) |
138 | found = true |
139 | return false |
140 | else |
141 | Logging.xmlWarning(markerXMLFile, "Could not load marker node for marker id '%s'", markerId) |
142 | end |
143 | end |
144 | end |
145 | end) |
146 | if not found then |
147 | Logging.xmlWarning(markerXMLFile, "Could not find marker id '%s'", markerId) |
148 | end |
149 | |
150 | delete(i3dNode) |
151 | end |
152 | |
153 | markerXMLFile:delete() |
154 | |
155 | self:finishLoadingTask(loadingTask) |
156 | end |
42 | function PlaceableTriggerMarkers.registerXMLPaths(schema, basePath) |
43 | schema:setXMLSpecializationType("TriggerMarkers") |
44 | schema:register(XMLValueType.NODE_INDEX, basePath .. ".triggerMarkers.triggerMarker(?)#node", "Trigger marker node") |
45 | schema:register(XMLValueType.STRING, basePath .. ".triggerMarkers.triggerMarker(?)#filename", "Trigger marker config file") |
46 | schema:register(XMLValueType.STRING, basePath .. ".triggerMarkers.triggerMarker(?)#id", "Trigger marker config file identifier") |
47 | schema:register(XMLValueType.BOOL, basePath .. ".triggerMarkers.triggerMarker(?)#adjustToGround", "Trigger marker adjusted to ground") |
48 | schema:register(XMLValueType.FLOAT, basePath .. ".triggerMarkers.triggerMarker(?)#groundOffset", "Hight of the trigger marker above the ground if adjustToGround is enabled", 0.03) |
49 | schema:setXMLSpecializationType() |
50 | end |