LUADOC - Farming Simulator 22

EnvironmentalScore

Functions

delete

Description
Definition
delete()
Code
104function EnvironmentalScore:delete()
105 self.ui.iconOverlay:delete()
106 self.ui.gradientOverlay:delete()
107 self.ui.gradientIndicatorOverlay:delete()
108 self.ui.smallBarOverlay:delete()
109
110 g_messageCenter:unsubscribe(MessageType.PERIOD_CHANGED, self)
111end

drawFarmlandScore

Description
Definition
drawFarmlandScore()
Code
482function EnvironmentalScore:drawFarmlandScore(x, y, dataKey, farmlandId, zoom)
483 local scale = zoom / 1.5
484
485 local alpha = math.min(math.max(scale - 0.55, 0) / 0.05, 1)
486 if alpha == 0 then
487 return
488 end
489
490 local farmlandData = self.ui.farmlandData[dataKey]
491 if farmlandData == nil then
492 farmlandData = {state = 0}
493 self.ui.farmlandData[dataKey] = farmlandData
494 end
495
496 local windowWidth, windowHeight = self.ui.fieldInfoWidth * scale, ((self.ui.fieldInfoHeight - self.ui.fieldInfoHeightSmall) * farmlandData.state + self.ui.fieldInfoHeightSmall) * scale
497 local windowX, windowY = x - windowWidth * 0.5, y - windowHeight - self.ui.fieldInfoHeightOffset * scale
498
499 local isHovering = false
500 if self.currentInputHelpMode == GS_INPUT_HELP_MODE_GAMEPAD then
501 if 0.5 > windowX and 0.5 < windowX + windowWidth then
502 if 0.5 > windowY and 0.5 < windowY + windowHeight then
503 isHovering = true
504 end
505 end
506 elseif g_inputBinding.mousePosXLast ~= nil then
507 if g_inputBinding.mousePosXLast > windowX and g_inputBinding.mousePosXLast < windowX + windowWidth then
508 if g_inputBinding.mousePosYLast > windowY and g_inputBinding.mousePosYLast < windowY + windowHeight then
509 isHovering = true
510 end
511 end
512 end
513
514 local target = (isHovering and 1) or 0
515 local direction = MathUtil.sign(target - farmlandData.state)
516 farmlandData.state = MathUtil.clamp(farmlandData.state + direction * g_currentDt / 150, 0, 1)
517
518 windowHeight = ((self.ui.fieldInfoHeight - self.ui.fieldInfoHeightSmall) * farmlandData.state + self.ui.fieldInfoHeightSmall) * scale
519 windowY = y - windowHeight - self.ui.fieldInfoHeightOffset * scale
520
521 drawFilledRect(windowX, windowY, windowWidth, windowHeight, self.ui.colorBackground[1], self.ui.colorBackground[2], self.ui.colorBackground[3], self.ui.colorBackground[4] * alpha)
522
523 local iconOffsetX = windowWidth * 0.3 - self.ui.iconWidth * scale * 0.5
524 local iconOffsetY = windowHeight - self.ui.topOffset * scale - self.ui.iconHeight * scale
525 self.ui.iconOverlay:setDimension(self.ui.iconWidth * scale, self.ui.iconHeight * scale)
526 self.ui.iconOverlay:setColor(self.ui.colorMainUI[1], self.ui.colorMainUI[2], self.ui.colorMainUI[3], self.ui.colorMainUI[4] * alpha)
527 self.ui.iconOverlay:setPosition(windowX + iconOffsetX, windowY + iconOffsetY)
528 self.ui.iconOverlay:render()
529
530 setTextColor(1, 1, 1, alpha)
531 setTextBold(true)
532 setTextAlignment(RenderText.ALIGN_CENTER)
533
534 local score = self:getFarmlandScore(farmlandId)
535
536 renderText(windowX + windowWidth - iconOffsetX - self.ui.iconOverlay.width * 0.5, windowY + iconOffsetY + self.ui.textOffsetHeader * scale, self.ui.textSizeHeader * scale, string.format("%d", score))
537
538 local gradientX, gradientY = windowX + windowWidth * 0.5 - self.ui.gradientOverlay.width * 0.5, windowY + windowHeight - self.ui.topOffset * scale - self.ui.iconOverlay.height - self.ui.spacingY * scale - self.ui.gradientOverlay.height
539 self.ui.gradientOverlay:setDimension(self.ui.scoreBarMainWidth * scale, self.ui.scoreBarMainHeight * scale)
540 self.ui.gradientOverlay:setColor(1, 1, 1, alpha)
541 self.ui.gradientOverlay:setPosition(gradientX, gradientY)
542 self.ui.gradientOverlay:render()
543
544 self.ui.gradientIndicatorOverlay:setDimension(self.ui.scoreBarMainIndicatorWidth * scale, self.ui.scoreBarMainIndicatorHeight * scale)
545 self.ui.gradientIndicatorOverlay:setColor(1, 1, 1, alpha)
546 self.ui.gradientIndicatorOverlay:setPosition(gradientX - self.ui.scoreBarMainIndicatorWidth * scale * 0.5 + (self.ui.gradientOverlay.width * (score/100)), gradientY - (self.ui.scoreBarMainIndicatorHeight-self.ui.scoreBarMainHeight) * 0.5 * scale)
547 self.ui.gradientIndicatorOverlay:render()
548
549 if farmlandData.state == 1 then
550 for i=1, #self.scoreValues do
551 local posX, posY = windowX + windowWidth * 0.5 - self.ui.scoreBarSmallWidth * scale * 0.5, windowY + self.ui.spacingY * scale + (self.ui.scoreBarSmallHeight * scale + self.ui.scoreBarOffset * scale) * (#self.scoreValues-i)
552 self.ui.smallBarOverlay:setPosition(posX, posY)
553 self.ui.smallBarOverlay:setDimension(self.ui.scoreBarSmallWidth * scale, self.ui.scoreBarSmallHeight * scale)
554 self.ui.smallBarOverlay:setColor(self.ui.colorBackground[1], self.ui.colorBackground[2], self.ui.colorBackground[3], self.ui.colorBackground[4] * alpha)
555 self.ui.smallBarOverlay:render()
556
557 self.ui.smallBarOverlay:setPosition(posX, posY)
558 self.ui.smallBarOverlay:setDimension(self.ui.scoreBarSmallWidth * self.scoreValues[i].object:getScore(farmlandId) * scale, self.ui.scoreBarSmallHeight * scale)
559 self.ui.smallBarOverlay:setColor(self.ui.colorMainUI[1], self.ui.colorMainUI[2], self.ui.colorMainUI[3], self.ui.colorMainUI[4] * alpha)
560 self.ui.smallBarOverlay:render()
561 end
562 end
563end

getFarmlandScore

Description
Definition
getFarmlandScore()
Code
327function EnvironmentalScore:getFarmlandScore(farmlandId)
328 local sum = 0
329 for i=1, #self.scoreValues do
330 sum = sum + self.scoreValues[i].object:getScore(farmlandId) * self.scoreValues[i].maxScore
331 end
332
333 return sum
334end

getTotalScore

Description
Definition
getTotalScore()
Code
338function EnvironmentalScore:getTotalScore(farmId)
339 local sumFarmlandSize = 0
340
341 if farmId ~= FarmManager.SPECTATOR_FARM_ID then
342 for farmlandId, _farmId in pairs(g_farmlandManager.farmlandMapping) do
343 if _farmId == farmId then
344 local farmland = g_farmlandManager:getFarmlandById(farmlandId)
345 if farmland ~= nil and farmland.totalFieldArea ~= nil and farmland.totalFieldArea > 0.01 then
346 sumFarmlandSize = sumFarmlandSize + farmland.totalFieldArea
347 end
348 end
349 end
350
351 if sumFarmlandSize > 0 then
352 local sum = 0
353 for farmlandId, _farmId in pairs(g_farmlandManager.farmlandMapping) do
354 if _farmId == farmId then
355 local farmland = g_farmlandManager:getFarmlandById(farmlandId)
356 if farmland ~= nil and farmland.totalFieldArea ~= nil and farmland.totalFieldArea > 0.01 then
357 for i=1, #self.scoreValues do
358 sum = sum + (self.scoreValues[i].object:getScore(farmlandId) * self.scoreValues[i].maxScore) * (farmland.totalFieldArea / sumFarmlandSize)
359 end
360 end
361 end
362 end
363
364 return sum
365 end
366 end
367
368 return 50
369end

loadFromItemsXML

Description
Definition
loadFromItemsXML()
Code
164function EnvironmentalScore:loadFromItemsXML(xmlFile, key)
165 key = key .. ".environmentalScore"
166
167 for i=1, #self.scoreValues do
168 local scoreValue = self.scoreValues[i]
169 if scoreValue.object ~= nil then
170 scoreValue.object:loadFromItemsXML(xmlFile, key)
171 end
172 end
173
174 xmlFile:iterate(key .. ".harvestedStates.harvestedState", function(index, stateKey)
175 local farmlandId = xmlFile:getInt(stateKey .. "#farmlandId")
176 local state = xmlFile:getInt(stateKey .. "#state")
177 if farmlandId ~= nil and state ~= nil then
178 self.harvestedStates[farmlandId] = state
179 end
180 end)
181end

loadFromXML

Description
Definition
loadFromXML()
Code
115function EnvironmentalScore:loadFromXML(xmlFile, key, baseDirectory, configFileName, mapFilename)
116 self.infoTextPos = g_i18n:getText("environmentalScore_rewardPos", EnvironmentalScore.MOD_NAME)
117 self.infoTextNeg = g_i18n:getText("environmentalScore_rewardNeg", EnvironmentalScore.MOD_NAME)
118 self.infoTextNone = g_i18n:getText("environmentalScore_rewardNone", EnvironmentalScore.MOD_NAME)
119
120 local i = 0
121 while true do
122 local baseKey = string.format("%s.scoreValues.scoreValue(%d)", key, i)
123 if not hasXMLProperty(xmlFile, baseKey) then
124 break
125 end
126
127 local scoreValue = {}
128 scoreValue.id = getXMLString(xmlFile, baseKey.."#id")
129 if scoreValue.id ~= nil then
130 scoreValue.id = scoreValue.id:upper()
131 scoreValue.name = g_i18n:convertText(getXMLString(xmlFile, baseKey.."#name"), EnvironmentalScore.MOD_NAME)
132
133 local className = getXMLString(xmlFile, baseKey.."#className")
134 if className ~= nil and self.scoreObjects[className] ~= nil then
135 scoreValue.object = self.scoreObjects[className]
136 scoreValue.object:loadFromXML(xmlFile, baseKey, baseDirectory, configFileName, mapFilename)
137 end
138
139 if scoreValue.name ~= nil then
140 if scoreValue.object ~= nil then
141 scoreValue.maxScore = getXMLInt(xmlFile, baseKey .. "#maxScore") or 10
142
143 scoreValue.curScore = scoreValue.maxScore * 0.5
144
145 table.insert(self.scoreValues, scoreValue)
146 else
147 Logging.warning("Missing score object className in '%s'", baseKey)
148 end
149 else
150 Logging.warning("Missing scoreValue name in '%s'", baseKey)
151 end
152 else
153 Logging.warning("Missing scoreValue id in '%s'", baseKey)
154 end
155
156 i = i + 1
157 end
158
159 return true
160end

new

Description
Definition
new()
Code
34function EnvironmentalScore.new(pfModule, customMt)
35 local self = setmetatable({}, customMt or EnvironmentalScore_mt)
36
37 self.pfModule = pfModule
38 self.mapFrame = nil
39
40 self.moneyChangeTypePos = MoneyType.register("other", "info_environmentalScoreReward", SoilMap.MOD_NAME)
41 self.moneyChangeTypeNeg = MoneyType.register("other", "info_environmentalScorePenalty", SoilMap.MOD_NAME)
42
43 self.scoreUpdateTimer = 0
44
45 self.harvestedStates = {}
46 self.farmRevenueIncrease = {}
47 self.farmRevenueIncreaseMessageDirty = false
48
49 self.overwrittenWindowState = false
50 self.currentInputHelpMode = g_inputBinding:getInputHelpMode()
51
52 self.scoreValues = {}
53
54 self.scoreObjects = {}
55 self.scoreObjects["EnvironmentalScoreHerbicide"] = EnvironmentalScoreHerbicide.new(pfModule)
56 self.scoreObjects["EnvironmentalScoreNitrogen"] = EnvironmentalScoreNitrogen.new(pfModule)
57 self.scoreObjects["EnvironmentalScorePH"] = EnvironmentalScorePH.new(pfModule)
58 self.scoreObjects["EnvironmentalScoreSoilSample"] = EnvironmentalScoreSoilSample.new(pfModule)
59 self.scoreObjects["EnvironmentalScoreTillage"] = EnvironmentalScoreTillage.new(pfModule)
60
61 local _
62 self.ui = {}
63 self.ui.fieldInfoWidth, self.ui.fieldInfoHeight = getNormalizedScreenValues(110, 110)
64 _, self.ui.fieldInfoHeightSmall = getNormalizedScreenValues(0, 65)
65 self.ui.scoreBarMainWidth, self.ui.scoreBarMainHeight = getNormalizedScreenValues(92, 11)
66 self.ui.scoreBarMainIndicatorWidth, self.ui.scoreBarMainIndicatorHeight = getNormalizedScreenValues(2, 15)
67 self.ui.scoreBarSmallWidth, self.ui.scoreBarSmallHeight = getNormalizedScreenValues(70, 5)
68 self.ui.iconWidth, self.ui.iconHeight = getNormalizedScreenValues(40, 40)
69 _, self.ui.scoreBarOffset = getNormalizedScreenValues(0, 4)
70 _, self.ui.topOffset = getNormalizedScreenValues(0, 2)
71 _, self.ui.spacingY = getNormalizedScreenValues(0, 5)
72 _, self.ui.fieldInfoHeightOffset = getNormalizedScreenValues(0, 12)
73 _, self.ui.textSizeHeader = getNormalizedScreenValues(0, 30)
74 _, self.ui.textOffsetHeader = getNormalizedScreenValues(0, 7)
75
76 self.ui.iconOverlay = Overlay.new(EnvironmentalScore.GUI_ELEMENTS, 0, 0, self.ui.iconWidth, self.ui.iconHeight)
77 self.ui.iconOverlay:setUVs(GuiUtils.getUVs({512, 64, 64, 64}))
78 self.ui.iconOverlay:setColor(1, 1, 1, 1)
79
80 self.ui.gradientOverlay = Overlay.new(EnvironmentalScore.GUI_ELEMENTS, 0, 0, self.ui.scoreBarMainWidth, self.ui.scoreBarMainHeight)
81 self.ui.gradientOverlay:setUVs(GuiUtils.getUVs({194, 27, 252, 12}))
82 self.ui.gradientOverlay:setColor(1, 1, 1, 1)
83
84 self.ui.gradientIndicatorOverlay = Overlay.new(g_baseHUDFilename, 0, 0, self.ui.scoreBarSmallWidth, self.ui.scoreBarSmallHeight)
85 self.ui.gradientIndicatorOverlay:setUVs(GuiUtils.getUVs(HUDElement.UV.FILL))
86 self.ui.gradientIndicatorOverlay:setColor(1, 1, 1, 1)
87
88 self.ui.smallBarOverlay = Overlay.new(g_baseHUDFilename, 0, 0, self.ui.scoreBarSmallWidth, self.ui.scoreBarSmallHeight)
89 self.ui.smallBarOverlay:setUVs(GuiUtils.getUVs(HUDElement.UV.FILL))
90 self.ui.smallBarOverlay:setColor(1, 1, 1, 1)
91
92 self.ui.colorBackground = {0.018, 0.016, 0.015, 0.6}
93 self.ui.colorMainUI = {0.0227, 0.5346, 0.8519, 1.0}
94
95 self.ui.farmlandData = {}
96
97 g_messageCenter:subscribe(MessageType.PERIOD_CHANGED, self.onPeriodChanged, self)
98
99 return self
100end

onDraw

Description
Definition
onDraw()
Code
432function EnvironmentalScore:onDraw(element, ingameMap)
433 if g_currentMission:getFarmId() ~= FarmManager.SPECTATOR_FARM_ID then
434 for _, hotspot in pairs(ingameMap.hotspots) do
435 if hotspot.getField ~= nil then
436 local worldX, worldZ = hotspot:getWorldPosition()
437
438 local objectX = (worldX + ingameMap.worldCenterOffsetX) / ingameMap.worldSizeX * 0.5 + 0.25
439 local objectZ = (worldZ + ingameMap.worldCenterOffsetZ) / ingameMap.worldSizeZ * 0.5 + 0.25
440
441 local x, y, _, visible = ingameMap.layout:getMapObjectPosition(objectX, objectZ, hotspot:getWidth(), hotspot:getHeight(), 0, hotspot:getIsPersistent())
442 if visible then
443 self:onDrawFieldNumber(element, ingameMap, x + hotspot:getWidth() * 0.5, y, hotspot:getField())
444 end
445 end
446 end
447
448 local farmId = g_currentMission:getFarmId()
449 for farmlandId, _farmId in pairs(g_farmlandManager.farmlandMapping) do
450 if _farmId == farmId then
451 if g_fieldManager.farmlandIdFieldMapping[farmlandId] == nil then
452 local farmland = g_farmlandManager:getFarmlandById(farmlandId)
453 if farmland.totalFieldArea ~= nil and farmland.totalFieldArea > 0.01 and (farmland.xWorldPos ~= 0 or farmland.zWorldPos ~= 0) then
454 local objectX = (farmland.xWorldPos + ingameMap.worldCenterOffsetX) / ingameMap.worldSizeX * 0.5 + 0.25
455 local objectZ = (farmland.zWorldPos + ingameMap.worldCenterOffsetZ) / ingameMap.worldSizeZ * 0.5 + 0.25
456
457 local x, y, _, _ = ingameMap.layout:getMapObjectPosition(objectX, objectZ, 0.0001, 0.0001, 0, true)
458
459 self:drawFarmlandScore(x, y, farmland, farmlandId, ingameMap.layout:getIconZoom())
460 end
461 end
462 end
463 end
464 end
465end

onDrawFieldNumber

Description
Definition
onDrawFieldNumber()
Code
469function EnvironmentalScore:onDrawFieldNumber(element, ingameMap, x, y, field)
470 if field.farmland ~= nil then
471 local farmlandId = field.farmland.id
472 if field.farmland.totalFieldArea ~= nil and field.farmland.totalFieldArea > 0.01 then
473 if g_farmlandManager.farmlandMapping[farmlandId] == g_currentMission:getFarmId() then
474 self:drawFarmlandScore(x, y, field, farmlandId, ingameMap.layout:getIconZoom())
475 end
476 end
477 end
478end

onHarvestScoreReset

Description
Definition
onHarvestScoreReset()
Code
316function EnvironmentalScore:onHarvestScoreReset(farmlandId)
317 for i=1, #self.scoreValues do
318 local scoreValue = self.scoreValues[i]
319 if scoreValue.object ~= nil and scoreValue.object.onHarvestScoreReset ~= nil then
320 scoreValue.object:onHarvestScoreReset(farmlandId)
321 end
322 end
323end

onMapFrameOpen

Description
Definition
onMapFrameOpen()
Code
303function EnvironmentalScore:onMapFrameOpen(mapFrame)
304 if g_server == nil and g_client ~= nil then
305 local farmId = g_currentMission:getFarmId()
306 if farmId ~= FarmManager.SPECTATOR_FARM_ID then
307 g_client:getServerConnection():sendEvent(RequestEnvironmentalScoreEvent.new(farmId))
308 end
309 end
310
311 mapFrame.envScoreWindow:setVisible(g_currentMission:getFarmId() ~= FarmManager.SPECTATOR_FARM_ID and not mapFrame.economicAnalysisWindow:getIsVisible())
312end

onPeriodChanged

Description
Definition
onPeriodChanged()
Code
567function EnvironmentalScore:onPeriodChanged(currentPeriod)
568 for farmlandId, state in pairs(self.harvestedStates) do
569 if state == 2 then
570 self.harvestedStates[farmlandId] = 0
571 self:onHarvestScoreReset(farmlandId)
572 end
573
574 if state == 1 then
575 self.harvestedStates[farmlandId] = 2
576 end
577 end
578end

overwriteGameFunctions

Description
Definition
overwriteGameFunctions()
Code
582function EnvironmentalScore:overwriteGameFunctions(pfModule)
583 for _, object in pairs(self.scoreObjects) do
584 object:overwriteGameFunctions(pfModule)
585 end
586
587 pfModule:overwriteGameFunction(CoverMap, "updateCoverArea", function(superFunc, coverMap, fruitTypes, startWorldX, startWorldZ, widthWorldX, widthWorldZ, heightWorldX, heightWorldZ, useMinForageState)
588 local phMapUpdated, nMapUpdated = superFunc(coverMap, fruitTypes, startWorldX, startWorldZ, widthWorldX, widthWorldZ, heightWorldX, heightWorldZ, useMinForageState)
589
590 local farmlandId = g_farmlandManager:getFarmlandIdAtWorldPosition(startWorldX, startWorldZ)
591 if farmlandId ~= nil then
592 self.harvestedStates[farmlandId] = 1
593 end
594
595 return phMapUpdated, nMapUpdated
596 end)
597
598 pfModule:overwriteGameFunction(SellingStation, "sellFillType", function(superFunc, sellingStation, farmId, fillDelta, fillTypeIndex, toolType, extraAttributes)
599 local price = superFunc(sellingStation, farmId, fillDelta, fillTypeIndex, toolType, extraAttributes)
600
601 local revenue = price * self:getSellPriceFactor(farmId)
602 if revenue ~= 0 then
603 if self.farmRevenueIncrease[farmId] == nil then
604 self.farmRevenueIncrease[farmId] = {revenue=0, lastSellTime=g_time}
605 end
606 self.farmRevenueIncrease[farmId].revenue = self.farmRevenueIncrease[farmId].revenue + revenue
607 self.farmRevenueIncrease[farmId].lastSellTime = g_time
608
609 g_currentMission:addMoney(revenue, farmId, revenue < 0 and self.moneyChangeTypeNeg or self.moneyChangeTypePos, true)
610
611 self.farmRevenueIncreaseMessageDirty = true
612 end
613
614 return price
615 end)
616end

readStream

Description
Read data on client side
Definition
readStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
207function EnvironmentalScore:readStream(streamId, connection, farmId)
208 for i=1, #self.scoreValues do
209 local scoreValue = self.scoreValues[i]
210 if scoreValue.object ~= nil then
211 scoreValue.object:readStream(streamId, connection, farmId)
212 end
213 end
214end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
185function EnvironmentalScore:saveToXMLFile(xmlFile, key, usedModNames)
186 key = key .. ".environmentalScore"
187
188 for i=1, #self.scoreValues do
189 local scoreValue = self.scoreValues[i]
190 if scoreValue.object ~= nil then
191 scoreValue.object:saveToXMLFile(xmlFile, key, usedModNames)
192 end
193 end
194
195 local index = 0
196 for farmlandId, state in pairs(self.harvestedStates) do
197 xmlFile:setInt(string.format("%s.harvestedStates.harvestedState(%d)#farmlandId", key, index), farmlandId)
198 xmlFile:setInt(string.format("%s.harvestedStates.harvestedState(%d)#state", key, index), state)
199 index = index + 1
200 end
201end

setInputHelpMode

Description
Definition
setInputHelpMode()
Code
289function EnvironmentalScore:setInputHelpMode(currentInputHelpMode)
290 self.currentInputHelpMode = currentInputHelpMode
291end

setMapFrame

Description
Definition
setMapFrame()
Code
295function EnvironmentalScore:setMapFrame(mapFrame)
296 self.mapFrame = mapFrame
297 self.envScoreWindowSize = self.mapFrame.envScoreWindow.size[2]
298 self:updateUI()
299end

toggleWindowSize

Description
Definition
toggleWindowSize()
Code
279function EnvironmentalScore:toggleWindowSize(state)
280 if state == nil then
281 state = not self.overwrittenWindowState
282 end
283
284 self.overwrittenWindowState = state
285end

update

Description
Definition
update()
Code
231function EnvironmentalScore:update(dt)
232 if self.mapFrame ~= nil then
233 local x, y = self.mapFrame.envScoreWindow.absPosition[1], self.mapFrame.envScoreWindow.absPosition[2]
234 local sizeX, sizeY = self.mapFrame.envScoreWindow.absSize[1], self.mapFrame.envScoreWindow.absSize[2]
235 local isHovering = self.overwrittenWindowState
236
237 if g_inputBinding.mousePosXLast ~= nil then
238 if g_inputBinding.mousePosXLast > x and g_inputBinding.mousePosXLast < x + sizeX then
239 if g_inputBinding.mousePosYLast > y and g_inputBinding.mousePosYLast < y + sizeY then
240 isHovering = true
241 end
242 end
243 end
244
245 local currentSizeY = self.mapFrame.envScoreWindow.size[2]
246 local target = (isHovering and self.envScoreWindowSize * 2.692) or self.envScoreWindowSize
247 local direction = MathUtil.sign(target - currentSizeY)
248 self.mapFrame.envScoreWindow:setSize(nil, MathUtil.clamp(currentSizeY + direction * dt / 1000, self.envScoreWindowSize, self.envScoreWindowSize * 2.692))
249
250 self.scoreUpdateTimer = self.scoreUpdateTimer + dt
251 if self.scoreUpdateTimer > EnvironmentalScore.SCORE_UPDATE_TIME then
252 self:updateUI()
253 self.scoreUpdateTimer = 0
254 end
255 end
256
257 if self.farmRevenueIncreaseMessageDirty then
258 local keepDirty = false
259 for farmId, data in pairs(self.farmRevenueIncrease) do
260 if data.revenue ~= 0 and g_time - data.lastSellTime > 1000 then
261 data.lastSellTime = 0
262
263 g_currentMission:showMoneyChange(data.revenue < 0 and self.moneyChangeTypeNeg or self.moneyChangeTypePos, nil, nil, farmId)
264
265 data.revenue = 0
266 elseif data.revenue ~= 0 then
267 keepDirty = true
268 end
269 end
270
271 if not keepDirty then
272 self.farmRevenueIncreaseMessageDirty = false
273 end
274 end
275end

updateUI

Description
Definition
updateUI()
Code
383function EnvironmentalScore:updateUI()
384 if self.mapFrame ~= nil then
385 local farmId = g_currentMission:getFarmId()
386 local totalScore = self:getTotalScore(farmId)
387 local percentage = self:getTotalScore(farmId) / 100
388 self.mapFrame.envScoreBarNumber:setText(string.format("%d", MathUtil.round(totalScore)))
389
390 self.mapFrame.envScoreBarDynamic:setSize(self.mapFrame.envScoreBarStatic.size[1] * percentage)
391
392 local uvs = GuiOverlay.getOverlayUVs(self.mapFrame.envScoreBarStatic.overlay, true)
393 self.mapFrame.envScoreBarDynamic:setImageUVs(true, uvs[1], uvs[2], uvs[3], uvs[4], (uvs[5] - uvs[1]) * percentage + uvs[1], uvs[6], (uvs[7] - uvs[3]) * percentage + uvs[3], uvs[8])
394
395 local indicatorX = self.mapFrame.envScoreBarStatic.position[1] + self.mapFrame.envScoreBarStatic.size[1] * percentage
396 self.mapFrame.envScoreBarIndicator:setPosition(indicatorX - self.mapFrame.envScoreBarIndicator.size[1] * 0.5)
397 self.mapFrame.envScoreBarNumber:setPosition(indicatorX - self.mapFrame.envScoreBarNumber.size[1] * 0.5)
398
399 for i=1, #self.scoreValues do
400 local scoreValue = self.scoreValues[i]
401 if self.mapFrame.envScoreDistributionText[i] ~= nil then
402 local score = 0
403 if scoreValue.object ~= nil then
404 local numFarmlands = 0
405 for farmlandId, _farmId in pairs(g_farmlandManager.farmlandMapping) do
406 if _farmId == farmId then
407 score = score + scoreValue.object:getScore(farmlandId)
408 numFarmlands = numFarmlands + 1
409 end
410 end
411
412 if numFarmlands > 0 then
413 score = score / numFarmlands
414 end
415 end
416
417 self.mapFrame.envScoreDistributionText[i]:setText(scoreValue.name)
418 self.mapFrame.envScoreDistributionValue[i]:setText(string.format("%.1f", MathUtil.round(score * scoreValue.maxScore, 1)))
419
420 self.mapFrame.envScoreDistributionBar[i]:setSize(self.mapFrame.envScoreDistributionBarBackground[i].size[1] * score)
421 end
422 end
423
424 local factor = MathUtil.round(self:getSellPriceFactor(farmId) * 100)
425 local text = factor >= 1 and self.infoTextPos or (factor <= -1 and self.infoTextNeg or self.infoTextNone)
426 self.mapFrame.envScoreInfoText:setText(string.format(text, math.abs(factor)))
427 end
428end

writeStream

Description
Write data to client
Definition
writeStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
220function EnvironmentalScore:writeStream(streamId, connection, farmId)
221 for i=1, #self.scoreValues do
222 local scoreValue = self.scoreValues[i]
223 if scoreValue.object ~= nil then
224 scoreValue.object:writeStream(streamId, connection, farmId)
225 end
226 end
227end