Script v1.7.1.0
- AI
- Animals
- Contracts
- Debug
- Economy
- Effects
- Events
- Farms
- GUI
- Handtools
- I3d
- Materials
- Misc
- Objects
- Placeables
- Player
- Shop
- Sounds
- Specializations
- AIConveyorBelt
- AIImplement
- AIVehicle
- AnimatedVehicle
- ArticulatedAxis
- Attachable
- AttacherJointControl
- AttacherJoints
- BaleGrab
- BaleLoader
- Baler
- BaleWrapper
- BaseMaterial
- BunkerSiloCompacter
- BunkerSiloInteractor
- BuyableBale
- CCTDrivable
- Combine
- ConnectionHoses
- ConveyorBelt
- Cover
- CrabSteering
- Crawlers
- Cultivator
- Cutter
- Cylindered
- CylinderedFoldable
- Dashboard
- Dischargeable
- Drivable
- DynamicallyLoadedParts
- DynamicMountAttacher
- Enterable
- FertilizingCultivator
- FertilizingSowingMachine
- FillTriggerVehicle
- FillUnit
- FillVolume
- Foldable
- FoliageBending
- ForageWagon
- FrontloaderAttacher
- FruitPreparer
- GroundAdjustedNodes
- GroundReference
- Honk
- HookLiftContainer
- HookLiftTrailer
- IKChains
- JigglingParts
- Leveler
- Lights
- LivestockTrailer
- Locomotive
- LogGrab
- ManureBarrel
- MixerWagon
- Motorized
- Mountable
- Mower
- Pickup
- Pipe
- Plow
- PowerConsumer
- PowerTakeOffs
- RandomlyMovingParts
- ReceivingHopper
- ReverseDriving
- Rideable
- RidgeMarker
- Roller
- Ropes
- SemiTrailerFront
- Shovel
- SlopeCompensation
- SmartAttach
- SowingMachine
- SpeedRotatingParts
- SplineVehicle
- Sprayer
- StrawBlower
- StumpCutter
- Suspensions
- Tedder
- TensionBeltObject
- TensionBelts
- TipOccluder
- Trailer
- TreePlanter
- TreeSaw
- TurnOnVehicle
- Washable
- WaterTrailer
- Wearable
- Weeder
- Wheels
- Windrower
- Wipers
- WoodCrusher
- WoodHarvester
- WorkArea
- WorkMode
- WorkParticles
- Triggers
- Utils
- Vehicles
- Weather
Engine v1.7.1.0
- AI
- Animation
- Camera
- Entity
- Fillplanes
- General
- I3D
- Input
- Lighting
- Math
- Network
- Node
- Overlays
- Particle System
- Physics
- Rendering
- Scenegraph
- Shape
- Sound
- Spline
- String
- Terrain Detail
- Text Rendering
- Tire Track
- XML
- general
Foundation Reference
AIConveyorBelt
DescriptionSpecialization for extending conveyor belts with AI functionalityFunctions
- actionEventChangeAngle
- getAINeedsTrafficCollisionBox
- getCanBeSelected
- getCanStartAIVehicle
- getDirectionAndSpeedToTargetAngle
- onAIStart
- onLoad
- onPostLoad
- onReadStream
- onRegisterActionEvents
- onUpdate
- onUpdateTick
- onWriteStream
- prerequisitesPresent
- registerEventListeners
- registerFunctions
- registerOverwrittenFunctions
- saveToXMLFile
- setAIConveyorBeltAngle
actionEventChangeAngle
DescriptionDefinitionactionEventChangeAngle()Code
235 | function AIConveyorBelt.actionEventChangeAngle(self, actionName, inputValue, callbackState, isAnalog) |
236 | local spec = self.spec_aiConveyorBelt |
237 | local newAngle = spec.currentAngle + spec.stepSize |
238 | if newAngle > spec.maxAngle then |
239 | newAngle = spec.minAngle |
240 | end |
241 | |
242 | self:setAIConveyorBeltAngle(newAngle) |
243 | end |
getAINeedsTrafficCollisionBox
DescriptionDefinitiongetAINeedsTrafficCollisionBox()Code
202 | function AIConveyorBelt:getAINeedsTrafficCollisionBox(superFunc) |
203 | return false |
204 | end |
getCanBeSelected
DescriptionDefinitiongetCanBeSelected()Code
196 | function AIConveyorBelt:getCanBeSelected(superFunc) |
197 | return true |
198 | end |
getCanStartAIVehicle
DescriptionDefinitiongetCanStartAIVehicle()Code
182 | function AIConveyorBelt:getCanStartAIVehicle(superFunc) |
183 | if g_currentMission.disableAIVehicle then |
184 | return false |
185 | end |
186 | |
187 | if AIVehicle.numHirablesHired >= g_currentMission.maxNumHirables then |
188 | return false |
189 | end |
190 | |
191 | return self.spec_aiConveyorBelt.isAllowed |
192 | end |
getDirectionAndSpeedToTargetAngle
DescriptionDefinitiongetDirectionAndSpeedToTargetAngle()Code
156 | function AIConveyorBelt:getDirectionAndSpeedToTargetAngle(direction, minAngle, maxAngle) |
157 | local dx, _, dz = localDirectionToWorld(self.components[1].node, 0, 0, 1) |
158 | local yRot = MathUtil.getYRotationFromDirection(dx, dz) |
159 | |
160 | local angleDifference |
161 | if direction > 0 then |
162 | if yRot > maxAngle then |
163 | return -1, 0 |
164 | end |
165 | angleDifference = maxAngle - yRot |
166 | elseif direction < 0 then |
167 | if yRot < minAngle then |
168 | return 1, 0 |
169 | end |
170 | angleDifference = yRot - minAngle |
171 | else |
172 | angleDifference = 0 |
173 | end |
174 | |
175 | local speed = MathUtil.clamp(math.deg(angleDifference) / 2.5, 0.1, 1) * direction |
176 | |
177 | return direction, speed |
178 | end |
onAIStart
DescriptionDefinitiononAIStart()Code
208 | function AIConveyorBelt:onAIStart() |
209 | local spec = self.spec_aiConveyorBelt |
210 | |
211 | local dx, _, dz = localDirectionToWorld(self.components[1].node, 0, 0, 1) |
212 | local yRot = MathUtil.getYRotationFromDirection(dx, dz) |
213 | |
214 | spec.minTargetWorldYRot = yRot - math.rad(spec.currentAngle) / 2 |
215 | spec.maxTargetWorldYRot = yRot + math.rad(spec.currentAngle) / 2 |
216 | spec.currentDirection = 1 |
217 | end |
onLoad
DescriptionCalled on loadingDefinition
onLoad(table savegame)Arguments
table | savegame | savegame |
54 | function AIConveyorBelt:onLoad(savegame) |
55 | local spec = self.spec_aiConveyorBelt |
56 | |
57 | spec.isAllowed = hasXMLProperty(self.xmlFile, "vehicle.ai.conveyorBelt") |
58 | |
59 | spec.minAngle = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.ai.conveyorBelt#minAngle"), 5) |
60 | spec.maxAngle = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.ai.conveyorBelt#maxAngle"), 45) |
61 | spec.stepSize = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.ai.conveyorBelt#stepSize"), 5) |
62 | spec.currentAngle = spec.maxAngle |
63 | |
64 | spec.minTargetWorldYRot = 0 |
65 | spec.maxTargetWorldYRot = 0 |
66 | spec.currentDirection = 0 |
67 | spec.currentSpeed = 0 |
68 | |
69 | spec.speed = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.ai.conveyorBelt#speed"), 1) |
70 | spec.direction = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.ai.conveyorBelt#direction"), -1) |
71 | end |
onPostLoad
DescriptionDefinitiononPostLoad()Code
75 | function AIConveyorBelt:onPostLoad(savegame) |
76 | local spec = self.spec_aiConveyorBelt |
77 | if savegame ~= nil and not savegame.resetVehicles then |
78 | spec.currentAngle = Utils.getNoNil(getXMLFloat(savegame.xmlFile, savegame.key..".aiConveyorBelt#currentAngle"), spec.currentAngle) |
79 | end |
80 | end |
onReadStream
DescriptionCalled on client side on joinDefinition
onReadStream(integer streamId, integer connection)Arguments
integer | streamId | streamId |
integer | connection | connection |
93 | function AIConveyorBelt:onReadStream(streamId, connection) |
94 | self:setAIConveyorBeltAngle(streamReadInt8(streamId), true) |
95 | end |
onRegisterActionEvents
DescriptionDefinitiononRegisterActionEvents()Code
221 | function AIConveyorBelt:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection) |
222 | if self.isClient then |
223 | local spec = self.spec_aiConveyorBelt |
224 | self:clearActionEventsTable(spec.actionEvents) |
225 | |
226 | if isActiveForInputIgnoreSelection and spec.isAllowed then |
227 | local _, actionEventId = self:addActionEvent(spec.actionEvents, InputAction.IMPLEMENT_EXTRA3, self, AIConveyorBelt.actionEventChangeAngle, false, true, false, true, nil) |
228 | g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_NORMAL) |
229 | end |
230 | end |
231 | end |
onUpdate
DescriptionCalled on updateDefinition
onUpdate(float dt, boolean isActiveForInput, boolean isSelected)Arguments
float | dt | time since last call in ms |
boolean | isActiveForInput | true if vehicle is active for input |
boolean | isSelected | true if vehicle is selected |
110 | function AIConveyorBelt:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected) |
111 | local spec = self.spec_aiConveyorBelt |
112 | if self.isServer then |
113 | if self:getIsAIActive() then |
114 | spec.currentDirection, spec.currentSpeed = self:getDirectionAndSpeedToTargetAngle(spec.currentDirection, spec.minTargetWorldYRot, spec.maxTargetWorldYRot) |
115 | |
116 | self:getMotor():setSpeedLimit(math.abs(spec.currentSpeed * spec.speed)) |
117 | WheelsUtil.updateWheelsPhysics(self, dt, spec.currentSpeed * spec.speed * spec.direction, spec.currentDirection * spec.direction, false, true) |
118 | end |
119 | end |
120 | end |
onUpdateTick
DescriptionCalled on updateTickDefinition
onUpdateTick(float dt, boolean isActiveForInput, boolean isSelected)Arguments
float | dt | time since last call in ms |
boolean | isActiveForInput | true if vehicle is active for input |
boolean | isSelected | true if vehicle is selected |
127 | function AIConveyorBelt:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected) |
128 | local spec = self.spec_aiConveyorBelt |
129 | if self.isClient then |
130 | local actionEvent = spec.actionEvents[InputAction.IMPLEMENT_EXTRA3] |
131 | if actionEvent ~= nil then |
132 | g_inputBinding:setActionEventActive(actionEvent.actionEventId, isActiveForInputIgnoreSelection) |
133 | if isActiveForInputIgnoreSelection then |
134 | g_inputBinding:setActionEventText(actionEvent.actionEventId, string.format(g_i18n:getText("action_conveyorBeltChangeAngle"), string.format("%.0f", spec.currentAngle))) |
135 | end |
136 | end |
137 | end |
138 | end |
onWriteStream
DescriptionCalled on server side on joinDefinition
onWriteStream(integer streamId, integer connection)Arguments
integer | streamId | streamId |
integer | connection | connection |
101 | function AIConveyorBelt:onWriteStream(streamId, connection) |
102 | streamWriteInt8(streamId, self.spec_aiConveyorBelt.currentAngle) |
103 | end |
prerequisitesPresent
DescriptionChecks if all prerequisite specializations are loadedDefinition
prerequisitesPresent(table specializations)Arguments
table | specializations | specializations |
boolean | hasPrerequisite | true if all prerequisite specializations are loaded |
19 | function AIConveyorBelt.prerequisitesPresent(specializations) |
20 | return SpecializationUtil.hasSpecialization(AIVehicle, specializations) and SpecializationUtil.hasSpecialization(Motorized, specializations) |
21 | end |
registerEventListeners
DescriptionDefinitionregisterEventListeners()Code
40 | function AIConveyorBelt.registerEventListeners(vehicleType) |
41 | SpecializationUtil.registerEventListener(vehicleType, "onLoad", AIConveyorBelt) |
42 | SpecializationUtil.registerEventListener(vehicleType, "onPostLoad", AIConveyorBelt) |
43 | SpecializationUtil.registerEventListener(vehicleType, "onReadStream", AIConveyorBelt) |
44 | SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", AIConveyorBelt) |
45 | SpecializationUtil.registerEventListener(vehicleType, "onUpdate", AIConveyorBelt) |
46 | SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", AIConveyorBelt) |
47 | SpecializationUtil.registerEventListener(vehicleType, "onAIStart", AIConveyorBelt) |
48 | SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", AIConveyorBelt) |
49 | end |
registerFunctions
DescriptionDefinitionregisterFunctions()Code
25 | function AIConveyorBelt.registerFunctions(vehicleType) |
26 | SpecializationUtil.registerFunction(vehicleType, "setAIConveyorBeltAngle", AIConveyorBelt.setAIConveyorBeltAngle) |
27 | SpecializationUtil.registerFunction(vehicleType, "getDirectionAndSpeedToTargetAngle", AIConveyorBelt.getDirectionAndSpeedToTargetAngle) |
28 | end |
registerOverwrittenFunctions
DescriptionDefinitionregisterOverwrittenFunctions()Code
32 | function AIConveyorBelt.registerOverwrittenFunctions(vehicleType) |
33 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanStartAIVehicle", AIConveyorBelt.getCanStartAIVehicle) |
34 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanBeSelected", AIConveyorBelt.getCanBeSelected) |
35 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getAINeedsTrafficCollisionBox", AIConveyorBelt.getAINeedsTrafficCollisionBox) |
36 | end |
saveToXMLFile
DescriptionDefinitionsaveToXMLFile()Code
84 | function AIConveyorBelt:saveToXMLFile(xmlFile, key, usedModNames) |
85 | local spec = self.spec_aiConveyorBelt |
86 | setXMLFloat(xmlFile, key.."#currentAngle", spec.currentAngle) |
87 | end |
setAIConveyorBeltAngle
DescriptionDefinitionsetAIConveyorBeltAngle()Code
142 | function AIConveyorBelt:setAIConveyorBeltAngle(angle, noEventSend) |
143 | if noEventSend == nil or noEventSend == false then |
144 | if g_server ~= nil then |
145 | g_server:broadcastEvent(AIConveyorBeltSetAngleEvent:new(self, angle), nil, nil, self) |
146 | else |
147 | g_client:getServerConnection():sendEvent(AIConveyorBeltSetAngleEvent:new(self, angle)) |
148 | end |
149 | end |
150 | |
151 | self.spec_aiConveyorBelt.currentAngle = angle |
152 | end |