25 | function AIJobVehicle.initSpecialization() |
26 | local schema = Vehicle.xmlSchema |
27 | schema:setXMLSpecializationType("AIJobVehicle") |
28 | schema:register(XMLValueType.NODE_INDEX, "vehicle.ai.steeringNode#node", "Steering node") |
29 | schema:register(XMLValueType.NODE_INDEX, "vehicle.ai.reverserNode#node", "Reverser node") |
30 | schema:register(XMLValueType.FLOAT, "vehicle.ai.steeringSpeed", "Speed of steering" , 1) |
31 | schema:register(XMLValueType.BOOL, "vehicle.ai#supportsAIJobs", "If true vehicle supports ai jobs", true) |
32 | schema:setXMLSpecializationType() |
33 | end |
102 | function AIJobVehicle:onLoad(savegame) |
103 | local spec = self.spec_aiJobVehicle |
104 | |
105 | spec.actionEvents = {} |
106 | spec.job = nil |
107 | spec.lastJob = nil |
108 | spec.startedFarmId = nil |
109 | |
110 | spec.aiSteeringSpeed = self.xmlFile:getValue("vehicle.ai.steeringSpeed", 1) * 0.001 |
111 | spec.steeringNode = self.xmlFile:getValue("vehicle.ai.steeringNode#node", nil, self.components, self.i3dMappings) |
112 | spec.reverserNode = self.xmlFile:getValue("vehicle.ai.reverserNode#node", nil, self.components, self.i3dMappings) |
113 | spec.supportsAIJobs = self.xmlFile:getValue("vehicle.ai#supportsAIJobs", true) |
114 | |
115 | spec.texts = {} |
116 | spec.texts.dismissEmployee = g_i18n:getText("action_dismissEmployee") |
117 | spec.texts.openHelperMenu = g_i18n:getText("action_openHelperMenu") |
118 | spec.texts.hireEmployee = g_i18n:getText("action_hireEmployee") |
119 | |
120 | if savegame ~= nil then |
121 | local aiJobTypeManager = g_currentMission.aiJobTypeManager |
122 | local savegameKey = savegame.key .. ".aiJobVehicle.lastJob" |
123 | local jobTypeName = savegame.xmlFile:getString(savegameKey .. "#type") |
124 | |
125 | local jobTypeIndex = aiJobTypeManager:getJobTypeIndexByName(jobTypeName) |
126 | if jobTypeIndex ~= nil then |
127 | local job = aiJobTypeManager:createJob(jobTypeIndex) |
128 | if job ~= nil and job.loadFromXMLFile ~= nil then |
129 | job:loadFromXMLFile(savegame.xmlFile, savegameKey) |
130 | spec.lastJob = job |
131 | end |
132 | end |
133 | end |
134 | end |
153 | function AIJobVehicle:onReadStream(streamId, connection) |
154 | |
155 | -- we only need to sync lastjob if no active job is running |
156 | |
157 | if streamReadBool(streamId) then |
158 | local jobId = streamReadInt32(streamId) |
159 | local startedFarmId = streamReadUIntN(streamId, FarmManager.FARM_ID_SEND_NUM_BITS) |
160 | local helperIndex = streamReadUInt8(streamId) |
161 | local job = g_currentMission.aiSystem:getJobById(jobId) |
162 | |
163 | self:aiJobStarted(job, helperIndex, startedFarmId) |
164 | elseif streamReadBool(streamId) then |
165 | local jobTypeIndex = streamReadInt32(streamId) |
166 | local spec = self.spec_aiJobVehicle |
167 | spec.lastJob = g_currentMission.aiJobTypeManager:createJob(jobTypeIndex) |
168 | spec.lastJob:readStream(streamId, connection) |
169 | end |
170 | end |
573 | function AIJobVehicle:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection) |
574 | if self.isClient then |
575 | local spec = self.spec_aiJobVehicle |
576 | self:clearActionEventsTable(spec.actionEvents) |
577 | |
578 | if spec.supportsAIJobs and self:getIsActiveForInput(true, true) then |
579 | local _, eventId = self:addActionEvent(spec.actionEvents, InputAction.TOGGLE_AI, self, AIJobVehicle.actionEventToggleAIState, false, true, false, true, nil) |
580 | g_inputBinding:setActionEventTextPriority(eventId, GS_PRIO_HIGH) |
581 | |
582 | AIJobVehicle.updateActionEvents(self) |
583 | end |
584 | end |
585 | end |
176 | function AIJobVehicle:onWriteStream(streamId, connection) |
177 | local spec = self.spec_aiJobVehicle |
178 | |
179 | -- we only need to sync lastjob if no active job is running |
180 | |
181 | if streamWriteBool(streamId, spec.job ~= nil) then |
182 | streamWriteInt32(streamId, spec.job.jobId) |
183 | streamWriteUIntN(streamId, spec.startedFarmId, FarmManager.FARM_ID_SEND_NUM_BITS) |
184 | streamWriteUInt8(streamId, spec.currentHelper.index) |
185 | elseif streamWriteBool(streamId, spec.lastJob ~= nil) then |
186 | local jobTypeIndex = g_currentMission.aiJobTypeManager:getJobTypeIndex(spec.lastJob) |
187 | streamWriteInt32(streamId, jobTypeIndex) |
188 | spec.lastJob:writeStream(streamId, connection) |
189 | end |
190 | end |
88 | function AIJobVehicle.registerEventListeners(vehicleType) |
89 | SpecializationUtil.registerEventListener(vehicleType, "onLoad", AIJobVehicle) |
90 | SpecializationUtil.registerEventListener(vehicleType, "onDelete", AIJobVehicle) |
91 | SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", AIJobVehicle) |
92 | SpecializationUtil.registerEventListener(vehicleType, "onUpdate", AIJobVehicle) |
93 | SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", AIJobVehicle) |
94 | SpecializationUtil.registerEventListener(vehicleType, "onSetBroken", AIJobVehicle) |
95 | SpecializationUtil.registerEventListener(vehicleType, "onReadStream", AIJobVehicle) |
96 | SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", AIJobVehicle) |
97 | end |
46 | function AIJobVehicle.registerFunctions(vehicleType) |
47 | SpecializationUtil.registerFunction(vehicleType, "getShowAIToggleActionEvent", AIJobVehicle.getShowAIToggleActionEvent) |
48 | SpecializationUtil.registerFunction(vehicleType, "stopCurrentAIJob", AIJobVehicle.stopCurrentAIJob) |
49 | SpecializationUtil.registerFunction(vehicleType, "skipCurrentTask", AIJobVehicle.skipCurrentTask) |
50 | SpecializationUtil.registerFunction(vehicleType, "aiJobStarted", AIJobVehicle.aiJobStarted) |
51 | SpecializationUtil.registerFunction(vehicleType, "aiJobFinished", AIJobVehicle.aiJobFinished) |
52 | SpecializationUtil.registerFunction(vehicleType, "toggleAIVehicle", AIJobVehicle.toggleAIVehicle) |
53 | SpecializationUtil.registerFunction(vehicleType, "getCanToggleAIVehicle", AIJobVehicle.getCanToggleAIVehicle) |
54 | SpecializationUtil.registerFunction(vehicleType, "getCanStartAIVehicle", AIJobVehicle.getCanStartAIVehicle) |
55 | SpecializationUtil.registerFunction(vehicleType, "setAIMapHotspotBlinking", AIJobVehicle.setAIMapHotspotBlinking) |
56 | SpecializationUtil.registerFunction(vehicleType, "getCurrentHelper", AIJobVehicle.getCurrentHelper) |
57 | SpecializationUtil.registerFunction(vehicleType, "aiBlock", AIJobVehicle.aiBlock) |
58 | SpecializationUtil.registerFunction(vehicleType, "aiContinue", AIJobVehicle.aiContinue) |
59 | SpecializationUtil.registerFunction(vehicleType, "getAIDirectionNode", AIJobVehicle.getAIDirectionNode) |
60 | SpecializationUtil.registerFunction(vehicleType, "getAISteeringNode", AIJobVehicle.getAISteeringNode) |
61 | SpecializationUtil.registerFunction(vehicleType, "getAIReverserNode", AIJobVehicle.getAIReverserNode) |
62 | SpecializationUtil.registerFunction(vehicleType, "getAISteeringSpeed", AIJobVehicle.getAISteeringSpeed) |
63 | SpecializationUtil.registerFunction(vehicleType, "getAIJobFarmId", AIJobVehicle.getAIJobFarmId) |
64 | SpecializationUtil.registerFunction(vehicleType, "getStartableAIJob", AIJobVehicle.getStartableAIJob) |
65 | SpecializationUtil.registerFunction(vehicleType, "getHasStartableAIJob", AIJobVehicle.getHasStartableAIJob) |
66 | SpecializationUtil.registerFunction(vehicleType, "getStartAIJobText", AIJobVehicle.getStartAIJobText) |
67 | SpecializationUtil.registerFunction(vehicleType, "getJob", AIJobVehicle.getJob) |
68 | SpecializationUtil.registerFunction(vehicleType, "getLastJob", AIJobVehicle.getLastJob) |
69 | end |
73 | function AIJobVehicle.registerOverwrittenFunctions(vehicleType) |
74 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsVehicleControlledByPlayer", AIJobVehicle.getIsVehicleControlledByPlayer) |
75 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsActive", AIJobVehicle.getIsActive) |
76 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsAIActive", AIJobVehicle.getIsAIActive) |
77 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getAllowTireTracks", AIJobVehicle.getAllowTireTracks) |
78 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getDeactivateOnLeave", AIJobVehicle.getDeactivateOnLeave) |
79 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getStopMotorOnLeave", AIJobVehicle.getStopMotorOnLeave) |
80 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getDisableVehicleCharacterOnLeave", AIJobVehicle.getDisableVehicleCharacterOnLeave) |
81 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getFullName", AIJobVehicle.getFullName) |
82 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getActiveFarm", AIJobVehicle.getActiveFarm) |
83 | SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsMapHotspotVisible", AIJobVehicle.getIsMapHotspotVisible) |
84 | end |