11-- @author : 4c65736975, All Rights Reserved
2- -- @version: 1.0.0.1, 11/02/ 2023
2+ -- @version: 1.0.0.2, 09|05| 2023
33-- @filename: ManualPipeDischargeable.lua
44
5- -- Changelog (1.0.0.1) :
6- --
5+ -- Changelog (1.0.0.1):
76-- cleaned code
87-- fixed AI bug where they couldn't discharge
98-- fixed forage harvesters discharging
109-- moved some functions from ManualDischargeUtil.lua
1110
11+ -- Changelog (1.0.0.2):
12+ -- cleaned and improved code
13+ -- added configuration
14+ -- the code responsible for the settings has been removed
15+
16+ source (g_currentModDirectory .. ' src/vehicles/specializations/events/SetManualPipeDischargeStateEvent.lua' )
17+
1218ManualPipeDischargeable = {
13- MANUAL_PIPE_DISCHARGEABLE_STATE_OFF = false ,
14- MANUAL_PIPE_DISCHARGEABLE_STATE_ON = true ,
15- SETTINGS = {
16- HARVESTERS = ' isHarvestersDischargeManually' ,
17- POTATOVEHICLES = ' isPotatoHarvestersDischargeManually' ,
18- BEETVEHICLES = ' isBeetHarvestersDischargeManually' ,
19- AUGERWAGONS = ' isAugerWagonsDischargeManually'
19+ PIPE_DISCHARGE_STATE = {
20+ OFF = false ,
21+ ON = true
2022 }
2123}
2224
2325function ManualPipeDischargeable .prerequisitesPresent (specializations )
2426 return SpecializationUtil .hasSpecialization (Pipe , specializations ) and SpecializationUtil .hasSpecialization (Dischargeable , specializations )
2527end
2628
29+ function ManualPipeDischargeable .initSpecialization ()
30+ g_configurationManager :addConfigurationType (' manualDischarge' , g_i18n :getText (' configuration_manualDischarge' ), nil , nil , nil , nil , ConfigurationUtil .SELECTOR_MULTIOPTION )
31+ end
32+
2733function ManualPipeDischargeable .registerFunctions (vehicleType )
28- SpecializationUtil .registerFunction (vehicleType , ' getIsDischargeableManually' , ManualPipeDischargeable .getIsDischargeableManually )
2934 SpecializationUtil .registerFunction (vehicleType , ' setManualPipeDischargeState' , ManualPipeDischargeable .setManualPipeDischargeState )
3035 SpecializationUtil .registerFunction (vehicleType , ' getManualPipeDischargeState' , ManualPipeDischargeable .getManualPipeDischargeState )
3136 SpecializationUtil .registerFunction (vehicleType , ' getIsManualPipeDischargeToggleable' , ManualPipeDischargeable .getIsManualPipeDischargeToggleable )
32- SpecializationUtil .registerFunction (vehicleType , ' getVehicleCategoryName' , ManualPipeDischargeable .getVehicleCategoryName )
33- SpecializationUtil .registerFunction (vehicleType , ' getAttachedImplementCategoryName' , ManualPipeDischargeable .getAttachedImplementCategoryName )
3437end
3538
3639function ManualPipeDischargeable .registerOverwrittenFunctions (vehicleType )
@@ -50,13 +53,24 @@ function ManualPipeDischargeable:onLoad(savegame)
5053 self .spec_manuallyPipeDischargeable = {}
5154 local spec = self .spec_manuallyPipeDischargeable
5255
53- if self .spec_pipe .automaticDischarge == false then
54- self .spec_pipe .automaticDischarge = true
55- end
56+ spec .isManual = self .configurations .manualDischarge and self .configurations .manualDischarge > 1 or false
57+
58+ if spec .isManual then
59+ if self .spec_pipe .automaticDischarge == false then
60+ self .spec_pipe .automaticDischarge = true
61+ end
5662
57- spec .actionEvents = {}
63+ spec .actionEvents = {}
64+ spec .currentManualPipeDischargeState = ManualPipeDischargeable .PIPE_DISCHARGE_STATE .OFF
65+ end
5866
59- spec .currentManualPipeDischargeState = ManualPipeDischargeable .MANUAL_PIPE_DISCHARGEABLE_STATE_OFF
67+ if not spec .isManual then
68+ SpecializationUtil .removeEventListener (self , ' onUpdate' , ManualPipeDischargeable )
69+ SpecializationUtil .removeEventListener (self , ' onUpdateTick' , ManualPipeDischargeable )
70+ SpecializationUtil .removeEventListener (self , ' onRegisterActionEvents' , ManualPipeDischargeable )
71+ SpecializationUtil .removeEventListener (self , ' onReadStream' , ManualPipeDischargeable )
72+ SpecializationUtil .removeEventListener (self , ' onWriteStream' , ManualPipeDischargeable )
73+ end
6074end
6175
6276function ManualPipeDischargeable :onReadStream (streamId , connection )
7892function ManualPipeDischargeable :onUpdate (dt )
7993 local spec = self .spec_manuallyPipeDischargeable
8094
81- if ( self :getIsAIActive () or not self : getIsDischargeableManually () or self .spec_pipe .autoAimingStates [self .spec_pipe .currentState ]) then
82- if spec .currentManualPipeDischargeState == ManualPipeDischargeable .MANUAL_PIPE_DISCHARGEABLE_STATE_OFF then
83- self :setManualPipeDischargeState (ManualPipeDischargeable .MANUAL_PIPE_DISCHARGEABLE_STATE_ON )
95+ if self :getIsAIActive () or self .spec_pipe .autoAimingStates [self .spec_pipe .currentState ] then
96+ if spec .currentManualPipeDischargeState == ManualPipeDischargeable .PIPE_DISCHARGE_STATE . OFF then
97+ self :setManualPipeDischargeState (ManualPipeDischargeable .PIPE_DISCHARGE_STATE . ON )
8498 end
85- else
86- if not self :getIsManualPipeDischargeToggleable () then
87- if spec .currentManualPipeDischargeState == ManualPipeDischargeable .MANUAL_PIPE_DISCHARGEABLE_STATE_ON then
88- self :setManualPipeDischargeState (ManualPipeDischargeable .MANUAL_PIPE_DISCHARGEABLE_STATE_OFF )
89- end
99+ elseif not self :getIsManualPipeDischargeToggleable () then
100+ if spec .currentManualPipeDischargeState == ManualPipeDischargeable .PIPE_DISCHARGE_STATE .ON then
101+ self :setManualPipeDischargeState (ManualPipeDischargeable .PIPE_DISCHARGE_STATE .OFF )
90102 end
91103 end
92104end
@@ -96,10 +108,8 @@ function ManualPipeDischargeable:onUpdateTick(dt)
96108 local dischargeNode = self .spec_dischargeable .currentDischargeNode
97109 local fillTypeIndex = self :getFillUnitFillType (dischargeNode .fillUnitIndex )
98110
99- if self :getIsDischargeableManually () then
100- if self :getIsActiveForInput () and self :getIsManualPipeDischargeToggleable () then
101- g_manualDischarge :showPipeDischargeContext (fillTypeIndex )
102- end
111+ if self :getIsActiveForInput () and self :getIsManualPipeDischargeToggleable () then
112+ g_currentMission :showPipeDischargeContext (fillTypeIndex )
103113 end
104114
105115 if self .isClient then
@@ -113,10 +123,10 @@ function ManualPipeDischargeable.updateActionEvents(self)
113123 local isActive = false
114124
115125 if actionEvent ~= nil then
116- if self :getIsManualPipeDischargeToggleable () and self : getIsDischargeableManually () then
126+ if self :getIsManualPipeDischargeToggleable () then
117127 isActive = true
118128
119- if spec .currentManualPipeDischargeState == ManualPipeDischargeable .MANUAL_PIPE_DISCHARGEABLE_STATE_OFF then
129+ if spec .currentManualPipeDischargeState == ManualPipeDischargeable .PIPE_DISCHARGE_STATE . OFF then
120130 g_inputBinding :setActionEventText (actionEvent .actionEventId , g_i18n :getText (' action_startOverloading' ))
121131 else
122132 g_inputBinding :setActionEventText (actionEvent .actionEventId , g_i18n :getText (' action_stopOverloading' ))
@@ -133,13 +143,11 @@ function ManualPipeDischargeable:onRegisterActionEvents(isActiveForInput, isActi
133143
134144 self :clearActionEventsTable (spec .actionEvents )
135145
136- if isActiveForInputIgnoreSelection then
146+ if isActiveForInput then
137147 if self :getPipeDischargeNodeIndex () ~= nil then
138148 local _ , actionEventId = self :addPoweredActionEvent (spec .actionEvents , InputAction .TOGGLE_MANUAL_DISCHARGE_PIPE , self , ManualPipeDischargeable .actionEventManualDischargePipe , false , true , false , true , nil )
139149
140150 g_inputBinding :setActionEventTextPriority (actionEventId , GS_PRIO_VERY_HIGH )
141-
142- ManualPipeDischargeable .updateActionEvents (self )
143151 end
144152 end
145153 end
@@ -164,34 +172,30 @@ end
164172function ManualPipeDischargeable :handleDischargeRaycast (superFunc , dischargeNode , hitObject , hitShape , hitDistance , hitFillUnitIndex , hitTerrain )
165173 local spec = self .spec_manuallyPipeDischargeable
166174
167- if self .spec_pipe .automaticDischarge then
175+ if spec . isManual and self .spec_pipe .automaticDischarge then
168176 local stopDischarge = false
169177
170178 if self :getIsPowered () and hitObject ~= nil then
171179 local fillType = self :getDischargeFillType (dischargeNode )
172180 local allowFillType = hitObject :getFillUnitAllowsFillType (hitFillUnitIndex , fillType )
173181
174182 if allowFillType and hitObject :getFillUnitFreeCapacity (hitFillUnitIndex , fillType , self :getOwnerFarmId ()) > 0 then
175- if spec .currentManualPipeDischargeState == ManualPipeDischargeable .MANUAL_PIPE_DISCHARGEABLE_STATE_ON then
183+ if spec .currentManualPipeDischargeState == ManualPipeDischargeable .PIPE_DISCHARGE_STATE . ON then
176184 self :setDischargeState (Dischargeable .DISCHARGE_STATE_OBJECT , true )
177185 else
178186 stopDischarge = true
179187 end
180188 else
181189 stopDischarge = true
182190
183- if spec .currentManualPipeDischargeState == ManualPipeDischargeable .MANUAL_PIPE_DISCHARGEABLE_STATE_ON then
184- self :setManualPipeDischargeState (ManualPipeDischargeable .MANUAL_PIPE_DISCHARGEABLE_STATE_OFF )
191+ if spec .currentManualPipeDischargeState == ManualPipeDischargeable .PIPE_DISCHARGE_STATE . ON then
192+ self :setManualPipeDischargeState (ManualPipeDischargeable .PIPE_DISCHARGE_STATE . OFF )
185193 end
186194 end
187195 elseif self :getIsPowered () and self .spec_pipe .toggleableDischargeToGround then
188196 self :setDischargeState (Dischargeable .DISCHARGE_STATE_GROUND , true )
189197 else
190198 stopDischarge = true
191-
192- if spec .currentManualPipeDischargeState == ManualPipeDischargeable .MANUAL_PIPE_DISCHARGEABLE_STATE_ON then
193- self :setManualPipeDischargeState (ManualPipeDischargeable .MANUAL_PIPE_DISCHARGEABLE_STATE_OFF )
194- end
195199 end
196200
197201 if stopDischarge and self :getDischargeState () == Dischargeable .DISCHARGE_STATE_OBJECT then
@@ -234,42 +238,4 @@ function ManualPipeDischargeable:getIsManualPipeDischargeToggleable()
234238 end
235239
236240 return true
237- end
238-
239- function ManualPipeDischargeable :getIsDischargeableManually ()
240- local spec = self .spec_manuallyPipeDischargeable
241- local vehicleCategoryName = self :getVehicleCategoryName (self .configFileName )
242- local implementCategoryName = self :getAttachedImplementCategoryName (self .configFileName )
243-
244- if vehicleCategoryName ~= nil or implementCategoryName ~= nil then
245- for categoryName , settingName in pairs (ManualPipeDischargeable .SETTINGS ) do
246- if categoryName == vehicleCategoryName or categoryName == implementCategoryName then
247- return g_manualDischarge :getManualDischargeableSettingState (settingName )
248- end
249- end
250- end
251-
252- return false
253- end
254-
255- function ManualPipeDischargeable :getVehicleCategoryName (configFileName )
256- return g_storeManager :getItemByXMLFilename (configFileName ).categoryName
257- end
258-
259- function ManualPipeDischargeable :getAttachedImplementCategoryName (configFileName )
260- for _ , vehicle in pairs (g_currentMission .vehicles ) do
261- if vehicle ~= nil then
262- if SpecializationUtil .hasSpecialization (AttacherJoints , vehicle .specializations ) then
263- local attachedImplements = vehicle :getAttachedImplements ()
264-
265- if attachedImplements ~= nil then
266- for _ , attachedImplement in pairs (attachedImplements ) do
267- if attachedImplement .object .configFileName == configFileName then
268- return g_storeManager :getItemByXMLFilename (attachedImplement .object .configFileName ).categoryName
269- end
270- end
271- end
272- end
273- end
274- end
275241end
0 commit comments