LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

IngameMap

Description
In-game map display element. This class is used to display the game map both in the HUD as well as in the in-game menu.
Parent
HUDElement
Functions

addMapHotspot

Description
Definition
addMapHotspot()
Code
325function IngameMap:addMapHotspot(mapHotspot)
326 table.insert(self.hotspots, mapHotspot)
327
328 -- On mobile we sort spatially
329 if GS_IS_MOBILE_VERSION then
330 local mapSize = 1024
331 table.sort(self.hotspots, function(v1, v2)
332 -- Split into 6 horizontal bands (must be an even number because map changes position based on the 0.5 split)
333 -- Sort horizontall within bands and vertically per band
334
335 local band1 = math.ceil((v1.worldZ + mapSize * 0.5) / (mapSize * 0.16666))
336 local band2 = math.ceil((v2.worldZ + mapSize * 0.5) / (mapSize * 0.16666))
337
338 if band1 == band2 then
339 return v1.worldX < v2.worldX or (v1.worldX == v2.worldX and v1.worldZ < v2.worldZ)
340 else
341 return (band1 - band2) < 0
342 end
343 end)
344 else
345 table.sort(self.hotspots, function(v1, v2) return v1:getCategory() > v2:getCategory() end)
346 end
347 self.hotspotsSorted = nil
348
349 return mapHotspot
350end

createBackground

Description
Create the empty background overlay.
Definition
createBackground()
Code
750function IngameMap:createBackground(hudAtlasPath)
751 local width, height = getNormalizedScreenValues(unpack(IngameMap.SIZE.SELF))
752 local posX, posY = self:getBackgroundPosition()
753
754 local overlay = Overlay.new(hudAtlasPath, posX, posY, width, height)
755 overlay:setUVs(GuiUtils.getUVs(IngameMap.UV.BACKGROUND_ROUND))
756 overlay:setColor(0,0,0,0.75)
757
758 return overlay
759end

createComponents

Description
Create required display components.
Definition
createComponents(string hudAtlasPath)
Arguments
stringhudAtlasPathPath to the HUD texture atlas
Code
764function IngameMap:createComponents(hudAtlasPath)
765 local baseX, baseY = self:getPosition()
766 local width, height = self:getWidth(), self:getHeight()
767
768 self:createToggleMapSizeGlyph(hudAtlasPath, baseX, baseY, width, height)
769end

createToggleMapSizeGlyph

Description
Create the input glyph for map size toggling.
Definition
createToggleMapSizeGlyph()
Code
773function IngameMap:createToggleMapSizeGlyph(hudAtlasPath, baseX, baseY, baseWidth, baseHeight)
774 local width, height = getNormalizedScreenValues(unpack(IngameMap.SIZE.INPUT_ICON))
775 local offX, offY = getNormalizedScreenValues(unpack(IngameMap.POSITION.INPUT_ICON))
776
777 local element = InputGlyphElement.new(self.inputDisplayManager, width, height)
778 local posX, posY = baseX + offX, baseY + offY
779
780 element:setPosition(posX, posY)
781 element:setKeyboardGlyphColor(IngameMap.COLOR.INPUT_ICON)
782 element:setAction(InputAction.TOGGLE_MAP_SIZE)
783
784 self.toggleMapSizeGlyph = element
785 self:addChild(element)
786end

delete

Description
Delete this element and all of its components.
Definition
delete()
Code
106function IngameMap:delete()
107 IngameMap:superClass().delete(self)
108
109 g_inputBinding:removeActionEventsByTarget(self)
110
111 self.mapElement:delete()
112 self:setSelectedHotspot(nil)
113
114 for _, layout in ipairs(self.layouts) do
115 layout:delete()
116 end
117
118 if self.mapOverlayGenerator ~= nil then
119 self.mapOverlayGenerator:delete()
120 end
121end

determinePlayerPosition

Description
Definition
determinePlayerPosition()
Code
296function IngameMap:determinePlayerPosition(player)
297 return player:getPositionData()
298end

determineVehiclePosition

Description
Definition
determineVehiclePosition()
Code
302function IngameMap:determineVehiclePosition(enterable)
303 local posX, posY, posZ = getTranslation(enterable.rootNode)
304
305 -- set arrow rotation
306 local dx, _, dz = localDirectionToWorld(enterable.rootNode, 0, 0, 1)
307 local yRot
308 if enterable.spec_drivable ~= nil and enterable.spec_drivable.reverserDirection == -1 then
309 yRot = MathUtil.getYRotationFromDirection(dx, dz)
310 else
311 yRot = MathUtil.getYRotationFromDirection(dx, dz) + math.pi
312 end
313
314 local vel = enterable:getLastSpeed()
315
316 return posX, posY, posZ, yRot, vel
317end

drawHotspot

Description
Draw a single hotspot on the map.
Definition
drawHotspot()
Code
694function IngameMap:drawHotspot(hotspot, smallVersion)
695 if hotspot == nil then
696 return
697 end
698
699 local worldX, worldZ = hotspot:getWorldPosition()
700 local rotation = hotspot:getWorldRotation()
701
702 local objectX = (worldX + self.worldCenterOffsetX) / self.worldSizeX * self.mapExtensionScaleFactor + self.mapExtensionOffsetX
703 local objectZ = (worldZ + self.worldCenterOffsetZ) / self.worldSizeZ * self.mapExtensionScaleFactor + self.mapExtensionOffsetZ
704
705 local zoom = self.layout:getIconZoom()
706 hotspot:setScale(self.uiScale * zoom)
707
708 local x, y, yRot, visible = self.layout:getMapObjectPosition(objectX, objectZ, hotspot:getWidth(), hotspot:getHeight(), rotation, hotspot:getIsPersistent())
709 if visible then
710 hotspot:setLastRenderInfo(x, y, yRot, self.layout)
711-- drawFilledRect(x, y, hotspot:getWidth(), hotspot:getHeight(), 1, 0, 0, 0.2)
712 hotspot:render(x, y, yRot, smallVersion)
713 end
714end

drawLatencyToServer

Description
Draw current latency to server as text.
Definition
drawLatencyToServer()
Code
639function IngameMap:drawLatencyToServer()
640 if g_client ~= nil and g_client.currentLatency ~= nil and g_currentMission.missionDynamicInfo.isMultiplayer and g_currentMission.missionDynamicInfo.isClient then
641 local color
642 if g_client.currentLatency <= 50 then
643 color = IngameMap.COLOR.LATENCY_GOOD
644 elseif g_client.currentLatency < 100 then
645 color = IngameMap.COLOR.LATENCY_MEDIUM
646 else
647 color = IngameMap.COLOR.LATENCY_BAD
648 end
649
650 self.layout:drawLatency(string.format("%dms", math.max(g_client.currentLatency, 10)), color)
651 end
652end

drawPlayersCoordinates

Description
Draw the player's current coordinates as text.
Definition
drawPlayersCoordinates()
Code
631function IngameMap:drawPlayersCoordinates()
632 local renderString = string.format("%.1f°, %d, %d", math.deg(-self.playerRotation % (2*math.pi)), self.normalizedPlayerPosX * self.worldSizeX, self.normalizedPlayerPosZ * self.worldSizeZ)
633
634 self.layout:drawCoordinates(renderString)
635end

getBackgroundPosition

Description
Get the base position of the entire element.
Definition
getBackgroundPosition()
Code
744function IngameMap:getBackgroundPosition()
745 return g_safeFrameOffsetX, g_safeFrameOffsetY
746end

loadMap

Description
Definition
loadMap()
Code
246function IngameMap:loadMap(filename, worldSizeX, worldSizeZ, fieldColor, grassFieldColor)
247 self.mapElement:delete() -- will also delete the wrapped Overlay
248
249 self:setWorldSize(worldSizeX, worldSizeZ)
250
251 self.mapOverlay = Overlay.new(filename, 0, 0, 1, 1)
252
253 self.mapElement = HUDElement.new(self.mapOverlay)
254 self:addChild(self.mapElement)
255
256 self:setScale(self.uiScale)
257
258 self.mapOverlayGenerator = MapOverlayGenerator.new(g_i18n, g_fruitTypeManager, g_fillTypeManager, g_farmlandManager, g_farmManager, g_currentMission.weedSystem)
259 self.mapOverlayGenerator:setColorBlindMode(false)
260 self.mapOverlayGenerator:setFieldColor(fieldColor, grassFieldColor)
261 self.fieldRefreshTimer = IngameMap.FIELD_REFRESH_INTERVAL
262end

new

Description
Create a new instance of IngameMap.
Definition
new(string hudAtlasPath, table inputDisplayManager)
Arguments
stringhudAtlasPathPath to the HUD atlas texture
tableinputDisplayManagerInputDisplayManager reference
Code
40function IngameMap.new(hud, hudAtlasPath, inputDisplayManager, customMt)
41 local self = IngameMap:superClass().new(nil, nil, customMt or IngameMap_mt)
42 self.overlay = self:createBackground(hudAtlasPath)
43
44 self.hud = hud
45 self.hudAtlasPath = hudAtlasPath
46 self.inputDisplayManager = inputDisplayManager
47
48 self.uiScale = 1.0
49
50 self.isVisible = true
51
52 self.layouts = {
53 IngameMapLayoutNone.new(),
54 IngameMapLayoutCircle.new(),
55 IngameMapLayoutSquare.new(),
56 IngameMapLayoutSquareLarge.new(),
57 IngameMapLayoutFullscreen.new(),
58 }
59 self.fullScreenLayout = self.layouts[#self.layouts]
60 self.state = 1
61 self.layout = self.layouts[self.state]
62
63 self.mapOverlay = Overlay.new(nil, 0, 0, 1, 1) -- null-object, obsoletes defensive checks
64 self.mapElement = HUDElement.new(self.mapOverlay) -- null-object
65
66 self:createComponents(hudAtlasPath)
67 for _, layout in ipairs(self.layouts) do
68 layout:createComponents(self, hudAtlasPath)
69 end
70
71 self.filter = {}
72 self.filter[MapHotspot.CATEGORY_FIELD] = true
73 self.filter[MapHotspot.CATEGORY_ANIMAL] = true
74 self.filter[MapHotspot.CATEGORY_MISSION] = true
75 self.filter[MapHotspot.CATEGORY_TOUR] = true
76 self.filter[MapHotspot.CATEGORY_STEERABLE] = true
77 self.filter[MapHotspot.CATEGORY_COMBINE] = true
78 self.filter[MapHotspot.CATEGORY_TRAILER] = true
79 self.filter[MapHotspot.CATEGORY_TOOL] = true
80 self.filter[MapHotspot.CATEGORY_UNLOADING] = true
81 self.filter[MapHotspot.CATEGORY_LOADING] = true
82 self.filter[MapHotspot.CATEGORY_PRODUCTION] = true
83 self.filter[MapHotspot.CATEGORY_SHOP] = true
84 self.filter[MapHotspot.CATEGORY_OTHER] = true
85 self.filter[MapHotspot.CATEGORY_AI] = true
86 self.filter[MapHotspot.CATEGORY_PLAYER] = true
87
88 self:setWorldSize(2048, 2048)
89
90 self.hotspots = {}
91 self.selectedHotspot = nil
92
93 self.mapExtensionOffsetX = 0.25
94 self.mapExtensionOffsetZ = 0.25
95 self.mapExtensionScaleFactor = 0.5
96
97 self.allowToggle = true
98
99 self.topDownCamera = nil -- set by screen views which use a top down view, used for map position update
100
101 return self
102end

removeMapHotspot

Description
Definition
removeMapHotspot()
Code
354function IngameMap:removeMapHotspot(mapHotspot)
355 if mapHotspot ~= nil then
356 for i=1, #self.hotspots do
357 if self.hotspots[i] == mapHotspot then
358 table.remove(self.hotspots, i)
359 break
360 end
361 end
362
363 if self.selectedHotspot == mapHotspot then
364 self:setSelectedHotspot(nil)
365 end
366
367 if g_currentMission ~= nil then
368 if g_currentMission.currentMapTargetHotspot == mapHotspot then
369 g_currentMission:setMapTargetHotspot(nil)
370 end
371 end
372
373 self.hotspotsSorted = nil
374 end
375end

resetSettings

Description
Definition
resetSettings()
Code
193function IngameMap:resetSettings()
194 if self.overlay == nil then
195 return -- instance has been deleted, ignore reset
196 end
197
198 -- self:setScale(self.uiScale) -- resets scaled values
199
200 -- local baseX, baseY = self:getBackgroundPosition()
201 -- self:setPosition(baseX + self.mapOffsetX, baseY + self.mapOffsetY)
202 -- self:setSize(self.mapWidth, self.mapHeight)
203
204 self:setSelectedHotspot(nil)
205end

setAllowToggle

Description
Definition
setAllowToggle()
Code
225function IngameMap:setAllowToggle(isAllowed)
226 self.allowToggle = isAllowed
227end

setFullscreen

Description
Set full-screen mode (for map overview) without affecting the mini-map state.
Definition
setFullscreen()
Code
129function IngameMap:setFullscreen(isFullscreen)
130 if self.isFullscreen == isFullscreen then
131 return
132 end
133
134 self.layout:deactivate()
135
136 self.isFullscreen = isFullscreen
137 if isFullscreen then
138 self.layout = self.fullScreenLayout
139 else
140 self.layout = self.layouts[self.state]
141 end
142
143 self.layout:activate()
144
145 g_inputBinding:setActionEventTextVisibility(self.toggleMapSizeEventId, self.layout:getShowsToggleActionText())
146end

setHotspotFilter

Description
Definition
setHotspotFilter()
Code
470function IngameMap:setHotspotFilter(category, isActive)
471 if category ~= nil then
472 if isActive then
473 g_gameSettings:setValue("ingameMapFilter", Utils.clearBit(g_gameSettings:getValue("ingameMapFilter"), category))
474 else
475 g_gameSettings:setValue("ingameMapFilter", Utils.setBit(g_gameSettings:getValue("ingameMapFilter"), category))
476 end
477 self.filter[category] = isActive
478 self.hotspotsSorted = nil
479 end
480end

setScale

Description
Set this element's scale.
Definition
setScale(float uiScale)
Arguments
floatuiScaleCurrent UI scale applied to both width and height of elements
Code
723function IngameMap:setScale(uiScale)
724 IngameMap:superClass().setScale(self, uiScale, uiScale)
725 self.uiScale = uiScale
726
727 self:storeScaledValues(uiScale)
728end

setSelectedHotspot

Description
Definition
setSelectedHotspot()
Code
379function IngameMap:setSelectedHotspot(hotspot)
380 if self.selectedHotspot ~= nil then
381 self.selectedHotspot:setSelected(false)
382 end
383 self.selectedHotspot = hotspot
384 if self.selectedHotspot ~= nil then
385 self.selectedHotspot:setSelected(true)
386 end
387end

setWorldSize

Description
Definition
setWorldSize()
Code
275function IngameMap:setWorldSize(worldSizeX, worldSizeZ)
276 self.worldSizeX = worldSizeX
277 self.worldSizeZ = worldSizeZ
278 self.worldCenterOffsetX = self.worldSizeX * 0.5
279 self.worldCenterOffsetZ = self.worldSizeZ * 0.5
280
281 for _, layout in ipairs(self.layouts) do
282 layout:setWorldSize(worldSizeX, worldSizeZ)
283 end
284end

storeScaledValues

Description
Store scaled positioning, size and offset values.
Definition
storeScaledValues()
Code
732function IngameMap:storeScaledValues(uiScale)
733 for _, layout in ipairs(self.layouts) do
734 layout:storeScaledValues(self, uiScale)
735 end
736end

toggleSize

Description
Definition
toggleSize()
Code
150function IngameMap:toggleSize(state, force)
151--#profile g_remoteProfiler.ZoneBeginN("IngameMap_toggleSize")
152 self.layout:deactivate()
153
154 if state ~= nil then
155 self.state = math.max(math.min(state, #self.layouts - 1), 1)
156 else
157 self.state = (self.state % (#self.layouts - 1)) + 1
158 end
159
160 self.layout = self.layouts[self.state]
161 self.layout:activate()
162
163 g_inputBinding:setActionEventTextVisibility(self.toggleMapSizeEventId, self.layout:getShowsToggleActionText())
164 g_gameSettings:setValue("ingameMapState", self.state)
165--#profile g_remoteProfiler.ZoneEnd()
166end

updateHotspotFilters

Description
Definition
updateHotspotFilters()
Code
457function IngameMap:updateHotspotFilters()
458 for category, _ in pairs(self.filter) do
459 if category == MapHotspot.CATEGORY_SHOP then
460 -- Make shop and 'other' equal to each other
461 self:setHotspotFilter(category, not Utils.isBitSet(g_gameSettings:getValue("ingameMapFilter"), MapHotspot.CATEGORY_OTHER))
462 else
463 self:setHotspotFilter(category, not Utils.isBitSet(g_gameSettings:getValue("ingameMapFilter"), category))
464 end
465 end
466end