-- author: DD ModPassion
-- date: 31.01.2019
-- copyright (C): DD ModPassion, All Rights Reserved
-- version: farming simulator 19
workshop = {} function workshop.prerequisitesPresent(specializations) return true end function workshop.registerEventListeners(vehicleType) SpecializationUtil.registerEventListener(vehicleType, "onLoad", workshop) SpecializationUtil.registerEventListener(vehicleType, "onDelete", workshop) SpecializationUtil.registerEventListener(vehicleType, "onDraw", workshop) end function workshop:onLoad(savegame) local triggers = {} triggers.playerTrigger = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.workshop#sellTrigger"), self.i3dMappings) triggers.sellTriggerNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.workshop#sellAreaTrigger"), self.i3dMappings) self.serviceActivatable = Workshop:new(self, triggers) self.nomeMod = g_i18n:getText("shop_noVehicles") self.currentVehicle = nil end function workshop:onDelete() self.serviceActivatable:delete() end function workshop:onDraw() if self.isClient and self.currentVehicle ~= nil then local storeItem = g_storeManager:getItemByXMLFilename(self.currentVehicle.configFileName:lower()) self.nomeMod = string.format("%s  %s", g_brandManager:getBrandByIndex(storeItem.brandIndex).name, storeItem.name) g_currentMission:addExtraPrintText(g_i18n:getText("modTrovata").." "..self.nomeMod) end end Workshop = {} local Workshop_mt = Class(Workshop) function Workshop:new(id, triggers) local self = {} self.vehicle = id setmetatable(self, Workshop_mt) self.playerTrigger = triggers.playerTrigger self.sellTriggerNode = triggers.sellTriggerNode addTrigger(self.playerTrigger, "triggerCallback", self) addTrigger(self.sellTriggerNode, "sellAreaTriggerCallback", self) self.vehicleInRange = {} self.currentVehicle = nil self.currentVehicleId = 0 self.activateText = g_i18n:getText("action_configSellSpecificVehicle") self.isEnabled = true self.objectActivated = false return self end function Workshop:drawActivate() end function Workshop:delete() g_messageCenter:unsubscribeAll(self) if self.playerTrigger ~= nil then removeTrigger(self.playerTrigger) self.playerTrigger = nil end if self.sellTriggerNode ~= nil then removeTrigger(self.sellTriggerNode) self.sellTriggerNode = nil end end function Workshop:getIsActivatable() return self.isEnabled and g_currentMission.controlPlayer and (self.vehicle:getOwnerFarmId() == 0 or g_currentMission:getFarmId() == self.vehicle:getOwnerFarmId()) end function Workshop:onActivateObject() self:determineCurrentVehicle() g_gui:showDirectSellDialog({vehicle = self.currentVehicle, owner = self, ownWorkshop = true}) end function Workshop:triggerCallback(triggerId, otherId, onEnter, onLeave, onStay) if self.isEnabled and (not g_isPresentationVersion or g_isPresentationVersionShopEnabled) and g_currentMission.missionInfo:isa(FSCareerMissionInfo) then if onEnter or onLeave then if g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then if onEnter then if not self.objectActivated then g_currentMission:addActivatableObject(self) self.objectActivated = true end else if self.objectActivated then g_currentMission:removeActivatableObject(self) self.objectActivated = false end end end end end end function Workshop:sellAreaTriggerCallback(triggerId, otherId, onEnter, onLeave, onStay, otherShapeId) if otherShapeId ~= nil and (onEnter or onLeave) then if onEnter then self.vehicleInRange[otherShapeId] = true elseif onLeave then self.vehicleInRange[otherShapeId] = nil end self:determineCurrentVehicle() end end function Workshop:determineCurrentVehicle() self.currentVehicle = nil for vehicleId, inRange in pairs(self.vehicleInRange) do if inRange ~= nil then self.currentVehicle = g_currentMission.nodeToObject[vehicleId] self.vehicle.currentVehicle = self.currentVehicle if self.currentVehicle ~= nil then if not SpecializationUtil.hasSpecialization(Rideable, self.currentVehicle.specializations) or self.currentVehicle:getOwnerFarmId() ~= self.parent:getOwnerFarmId() then break end end end self.vehicleInRange[vehicleId] = nil end end