LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

VinePrepruner

Description
Specialization for vine prepruners
Functions

initSpecialization

Description
Definition
initSpecialization()
Code
18function VinePrepruner.initSpecialization()
19 local schema = Vehicle.xmlSchema
20 schema:setXMLSpecializationType("VinePrepruner")
21 schema:register(XMLValueType.STRING, "vehicle.vinePrepruner#fruitType", "Fruit type")
22
23 local nodeKey = VinePrepruner.PRUNER_NODE_XML_KEY
24 schema:register(XMLValueType.NODE_INDEX, nodeKey .. "#node", "Pruner node that adjusts translation depending on raycast distance")
25 schema:register(XMLValueType.FLOAT, nodeKey .. "#offset", "Offset from raycast node to center of pruning unit", 0.5)
26 schema:register(XMLValueType.INT, nodeKey .. "#axis", "Move axis", 1)
27 schema:register(XMLValueType.INT, nodeKey .. "#direction", "Translation direction", 1)
28 schema:register(XMLValueType.FLOAT, nodeKey .. "#transMin", "Min. translation", 0)
29 schema:register(XMLValueType.FLOAT, nodeKey .. "#transMax", "Max. translation", 1)
30 schema:register(XMLValueType.FLOAT, nodeKey .. "#transSpeed", "Translation speed (m/sec)", 0.5)
31 schema:register(XMLValueType.INT, nodeKey .. "#numBits", "Number of bits to sync state in multiplayer", 8)
32
33 schema:register(XMLValueType.STRING, "vehicle.vinePrepruner.poleAnimation#name", "Name of pole animation (will be triggered as soon as pole has been detected)")
34 schema:register(XMLValueType.FLOAT, "vehicle.vinePrepruner.poleAnimation#speedScale", "Animation speed scale", 1)
35 schema:register(XMLValueType.FLOAT, "vehicle.vinePrepruner.poleAnimation#poleThreshold", "Defines when the pole is detected as percentage of segment length", 0.1)
36
37 EffectManager.registerEffectXMLPaths(schema, "vehicle.vinePrepruner.effect")
38
39 schema:setXMLSpecializationType()
40end

onDelete

Description
Called on deleting
Definition
onDelete()
Code
116function VinePrepruner:onDelete()
117 local spec = self.spec_vinePrepruner
118 g_effectManager:deleteEffects(spec.effects)
119end

onLoad

Description
Definition
onLoad()
Code
80function VinePrepruner:onLoad(savegame)
81 local spec = self.spec_vinePrepruner
82
83 local fruitTypeName = self.xmlFile:getValue("vehicle.vinePrepruner#fruitType")
84 local fruitType = g_fruitTypeManager:getFruitTypeByName(fruitTypeName)
85 if fruitType ~= nil then
86 spec.fruitTypeIndex = fruitType.index
87 else
88 spec.fruitTypeIndex = FruitType.GRAPE
89 end
90
91 spec.prunerNodes = {}
92 self.xmlFile:iterate("vehicle.vinePrepruner.prunerNode", function(index, key)
93 local entry = {}
94 if self:loadPreprunerNodeFromXML(self.xmlFile, key, entry) then
95 table.insert(spec.prunerNodes, entry)
96 end
97 end)
98
99 spec.poleAnimation = {}
100 spec.poleAnimation.name = self.xmlFile:getValue("vehicle.vinePrepruner.poleAnimation#name")
101 spec.poleAnimation.speedScale = self.xmlFile:getValue("vehicle.vinePrepruner.poleAnimation#speedScale", 1)
102 spec.poleAnimation.poleThreshold = 1 - self.xmlFile:getValue("vehicle.vinePrepruner.poleAnimation#poleThreshold", 0.1)
103
104 spec.lastWorkTime = -10000
105 spec.effectState = false
106
107 if self.isClient then
108 spec.effects = g_effectManager:loadEffect(self.xmlFile, "vehicle.vinePrepruner.effect", self.components, self, self.i3dMappings)
109 end
110
111 spec.dirtyFlag = self:getNextDirtyFlag()
112end

onReadStream

Description
Definition
onReadStream()
Code
123function VinePrepruner:onReadStream(streamId, connection)
124 VinePrepruner.readPrePrunerFromStream(self, streamId, true)
125end

onReadUpdateStream

Description
Definition
onReadUpdateStream()
Code
135function VinePrepruner:onReadUpdateStream(streamId, timestamp, connection)
136 if connection:getIsServer() then
137 if streamReadBool(streamId) then
138 VinePrepruner.readPrePrunerFromStream(self, streamId)
139 end
140 end
141end

onUpdate

Description
Definition
onUpdate()
Code
200function VinePrepruner:onUpdate(dt, isActive, isActiveForInput, isSelected)
201 local spec = self.spec_vinePrepruner
202 for i=1, #spec.prunerNodes do
203 local prunerNode = spec.prunerNodes[i]
204 if self:getIsPreprunerNodeActive(prunerNode) then
205 local curTrans = prunerNode.curTrans[prunerNode.axis]
206 if prunerNode.transTarget ~= curTrans then
207 local moveDirection = MathUtil.sign(prunerNode.transTarget - curTrans)
208 local func = moveDirection >= 0 and math.min or math.max
209 prunerNode.curTrans[prunerNode.axis] = func(curTrans + prunerNode.transSpeed * dt * moveDirection, prunerNode.transTarget)
210 setTranslation(prunerNode.node, prunerNode.curTrans[1], prunerNode.curTrans[2], prunerNode.curTrans[3])
211 end
212 end
213 end
214
215 if self.isServer then
216 local effectState = spec.lastWorkTime + 1000 > g_time
217 if effectState ~= spec.effectState then
218 spec.effectState = effectState
219
220 if self.isClient then
221 if effectState then
222 g_effectManager:setFruitType(spec.effects, spec.fruitTypeIndex)
223 g_effectManager:startEffects(spec.effects)
224 else
225 g_effectManager:stopEffects(spec.effects)
226 end
227 end
228
229 self:raiseDirtyFlags(spec.dirtyFlag)
230 end
231 end
232end

onWriteStream

Description
Definition
onWriteStream()
Code
129function VinePrepruner:onWriteStream(streamId, connection)
130 VinePrepruner.writePrePrunerToStream(self, streamId)
131end

onWriteUpdateStream

Description
Definition
onWriteUpdateStream()
Code
145function VinePrepruner:onWriteUpdateStream(streamId, connection, dirtyMask)
146 if not connection:getIsServer() then
147 local spec = self.spec_vinePrepruner
148 if streamWriteBool(streamId, bitAND(dirtyMask, spec.dirtyFlag) ~= 0) then
149 VinePrepruner.writePrePrunerToStream(self, streamId)
150 end
151 end
152end

prerequisitesPresent

Description
Definition
prerequisitesPresent()
Code
44function VinePrepruner.prerequisitesPresent(specializations)
45 return SpecializationUtil.hasSpecialization(VineDetector, specializations)
46end

readPrePrunerFromStream

Description
Definition
readPrePrunerFromStream()
Code
156function VinePrepruner.readPrePrunerFromStream(self, streamId, forceState)
157 local spec = self.spec_vinePrepruner
158 for i=1, #spec.prunerNodes do
159 local prunerNode = spec.prunerNodes[i]
160
161 local maxValue = (2 ^ prunerNode.numBits) - 1
162 local rawValue = streamReadUIntN(streamId, prunerNode.numBits)
163 prunerNode.transTarget = rawValue / maxValue * (prunerNode.transMax - prunerNode.transMin) + prunerNode.transMin
164 if forceState then
165 prunerNode.curTrans[prunerNode.axis] = prunerNode.transTarget
166 setTranslation(prunerNode.node, prunerNode.curTrans[1], prunerNode.curTrans[2], prunerNode.curTrans[3])
167 end
168 end
169
170 local effectState = streamReadBool(streamId)
171 if effectState ~= spec.effectState then
172 spec.effectState = effectState
173
174 if effectState then
175 g_effectManager:setFruitType(spec.effects, spec.fruitTypeIndex)
176 g_effectManager:startEffects(spec.effects)
177 else
178 g_effectManager:stopEffects(spec.effects)
179 end
180 end
181end

registerEventListeners

Description
Definition
registerEventListeners()
Code
66function VinePrepruner.registerEventListeners(vehicleType)
67 SpecializationUtil.registerEventListener(vehicleType, "onLoad", VinePrepruner)
68 SpecializationUtil.registerEventListener(vehicleType, "onDelete", VinePrepruner)
69 SpecializationUtil.registerEventListener(vehicleType, "onReadStream", VinePrepruner)
70 SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", VinePrepruner)
71 SpecializationUtil.registerEventListener(vehicleType, "onReadUpdateStream", VinePrepruner)
72 SpecializationUtil.registerEventListener(vehicleType, "onWriteUpdateStream", VinePrepruner)
73 SpecializationUtil.registerEventListener(vehicleType, "onUpdate", VinePrepruner)
74 SpecializationUtil.registerEventListener(vehicleType, "onTurnedOff", VinePrepruner)
75 SpecializationUtil.registerEventListener(vehicleType, "onAnimationPartChanged", VinePrepruner)
76end

registerFunctions

Description
Definition
registerFunctions()
Code
50function VinePrepruner.registerFunctions(vehicleType)
51 SpecializationUtil.registerFunction(vehicleType, "loadPreprunerNodeFromXML", VinePrepruner.loadPreprunerNodeFromXML)
52 SpecializationUtil.registerFunction(vehicleType, "getIsPreprunerNodeActive", VinePrepruner.getIsPreprunerNodeActive)
53end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
57function VinePrepruner.registerOverwrittenFunctions(vehicleType)
58 SpecializationUtil.registerOverwrittenFunction(vehicleType, "doCheckSpeedLimit", VinePrepruner.doCheckSpeedLimit)
59 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanStartVineDetection", VinePrepruner.getCanStartVineDetection)
60 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsValidVinePlaceable", VinePrepruner.getIsValidVinePlaceable)
61 SpecializationUtil.registerOverwrittenFunction(vehicleType, "handleVinePlaceable", VinePrepruner.handleVinePlaceable)
62end

writePrePrunerToStream

Description
Definition
writePrePrunerToStream()
Code
185function VinePrepruner.writePrePrunerToStream(self, streamId)
186 local spec = self.spec_vinePrepruner
187 for i=1, #spec.prunerNodes do
188 local prunerNode = spec.prunerNodes[i]
189
190 local maxValue = (2 ^ prunerNode.numBits) - 1
191 local state = (prunerNode.transTarget - prunerNode.transMin) / (prunerNode.transMax - prunerNode.transMin)
192 streamWriteUIntN(streamId, state * maxValue, prunerNode.numBits)
193 end
194
195 streamWriteBool(streamId, spec.effectState)
196end