Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---Checks if all prerequisite specializations are loaded
- -- @param table specializations specializations
- -- @return boolean hasPrerequisite true if all prerequisite specializations are loaded
- function PlaceableHusbandryAnimals.prerequisitesPresent(specializations)
- return SpecializationUtil.hasSpecialization(PlaceableHusbandry, specializations)
- end
- ---
- function PlaceableHusbandryAnimals.registerEvents(placeableType)
- SpecializationUtil.registerEvent(placeableType, "onHusbandryAnimalsCreated")
- SpecializationUtil.registerEvent(placeableType, "onHusbandryAnimalsUpdate")
- end
- ---
- function PlaceableHusbandryAnimals.registerFunctions(placeableType)
- SpecializationUtil.registerFunction(placeableType, "onExternalNavigationMeshLoaded", PlaceableHusbandryAnimals.onExternalNavigationMeshLoaded)
- SpecializationUtil.registerFunction(placeableType, "createNavigationMeshFromContour", PlaceableHusbandryAnimals.createNavigationMeshFromContour)
- SpecializationUtil.registerFunction(placeableType, "createNavigationMesh", PlaceableHusbandryAnimals.createNavigationMesh)
- SpecializationUtil.registerFunction(placeableType, "setMaxNumAnimals", PlaceableHusbandryAnimals.setMaxNumAnimals)
- SpecializationUtil.registerFunction(placeableType, "createHusbandry", PlaceableHusbandryAnimals.createHusbandry)
- SpecializationUtil.registerFunction(placeableType, "updateVisualAnimals", PlaceableHusbandryAnimals.updateVisualAnimals)
- SpecializationUtil.registerFunction(placeableType, "getNumOfFreeAnimalSlots", PlaceableHusbandryAnimals.getNumOfFreeAnimalSlots)
- SpecializationUtil.registerFunction(placeableType, "getNumOfAnimals", PlaceableHusbandryAnimals.getNumOfAnimals)
- SpecializationUtil.registerFunction(placeableType, "getMaxNumOfAnimals", PlaceableHusbandryAnimals.getMaxNumOfAnimals)
- SpecializationUtil.registerFunction(placeableType, "getNumOfClusters", PlaceableHusbandryAnimals.getNumOfClusters)
- SpecializationUtil.registerFunction(placeableType, "getSupportsAnimalSubType", PlaceableHusbandryAnimals.getSupportsAnimalSubType)
- SpecializationUtil.registerFunction(placeableType, "getClusters", PlaceableHusbandryAnimals.getClusters)
- SpecializationUtil.registerFunction(placeableType, "getCluster", PlaceableHusbandryAnimals.getCluster)
- SpecializationUtil.registerFunction(placeableType, "getClusterById", PlaceableHusbandryAnimals.getClusterById)
- SpecializationUtil.registerFunction(placeableType, "getClusterSystem", PlaceableHusbandryAnimals.getClusterSystem)
- SpecializationUtil.registerFunction(placeableType, "getAnimalTypeIndex", PlaceableHusbandryAnimals.getAnimalTypeIndex)
- SpecializationUtil.registerFunction(placeableType, "renameAnimal", PlaceableHusbandryAnimals.renameAnimal)
- SpecializationUtil.registerFunction(placeableType, "addCluster", PlaceableHusbandryAnimals.addCluster)
- SpecializationUtil.registerFunction(placeableType, "addAnimals", PlaceableHusbandryAnimals.addAnimals)
- SpecializationUtil.registerFunction(placeableType, "updatedClusters", PlaceableHusbandryAnimals.updatedClusters)
- SpecializationUtil.registerFunction(placeableType, "consoleCommandAddAnimals", PlaceableHusbandryAnimals.consoleCommandAddAnimals)
- SpecializationUtil.registerFunction(placeableType, "getAnimalSupportsRiding", PlaceableHusbandryAnimals.getAnimalSupportsRiding)
- SpecializationUtil.registerFunction(placeableType, "getAnimalCanBeRidden", PlaceableHusbandryAnimals.getAnimalCanBeRidden)
- SpecializationUtil.registerFunction(placeableType, "startRiding", PlaceableHusbandryAnimals.startRiding)
- SpecializationUtil.registerFunction(placeableType, "onLoadedRideable", PlaceableHusbandryAnimals.onLoadedRideable)
- SpecializationUtil.registerFunction(placeableType, "getIsInAnimalDeliveryArea", PlaceableHusbandryAnimals.getIsInAnimalDeliveryArea)
- SpecializationUtil.registerFunction(placeableType, "loadDeliveryArea", PlaceableHusbandryAnimals.loadDeliveryArea)
- SpecializationUtil.registerFunction(placeableType, "getOutdoorContourPolygon", PlaceableHusbandryAnimals.getOutdoorContourPolygon)
- SpecializationUtil.registerFunction(placeableType, "createNavigationMeshPlacementCollision", PlaceableHusbandryAnimals.createNavigationMeshPlacementCollision)
- SpecializationUtil.registerFunction(placeableType, "deleteNavigationMeshPlacementCollision", PlaceableHusbandryAnimals.deleteNavigationMeshPlacementCollision)
- end
- ---
- function PlaceableHusbandryAnimals.registerOverwrittenFunctions(placeableType)
- SpecializationUtil.registerOverwrittenFunction(placeableType, "getNeedDayChanged", PlaceableHusbandryAnimals.getNeedDayChanged)
- SpecializationUtil.registerOverwrittenFunction(placeableType, "updateInfo", PlaceableHusbandryAnimals.updateInfo)
- SpecializationUtil.registerOverwrittenFunction(placeableType, "updateOutput", PlaceableHusbandryAnimals.updateOutput)
- SpecializationUtil.registerOverwrittenFunction(placeableType, "canBeSold", PlaceableHusbandryAnimals.canBeSold)
- SpecializationUtil.registerOverwrittenFunction(placeableType, "getConditionInfos", PlaceableHusbandryAnimals.getConditionInfos)
- SpecializationUtil.registerOverwrittenFunction(placeableType, "getAnimalInfos", PlaceableHusbandryAnimals.getAnimalInfos)
- SpecializationUtil.registerOverwrittenFunction(placeableType, "getAnimalDescription", PlaceableHusbandryAnimals.getAnimalDescription)
- end
- ---
- function PlaceableHusbandryAnimals.registerEventListeners(placeableType)
- SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableHusbandryAnimals)
- SpecializationUtil.registerEventListener(placeableType, "onDelete", PlaceableHusbandryAnimals)
- SpecializationUtil.registerEventListener(placeableType, "onFinalizePlacement", PlaceableHusbandryAnimals)
- SpecializationUtil.registerEventListener(placeableType, "onReadStream", PlaceableHusbandryAnimals)
- SpecializationUtil.registerEventListener(placeableType, "onWriteStream", PlaceableHusbandryAnimals)
- SpecializationUtil.registerEventListener(placeableType, "onUpdate", PlaceableHusbandryAnimals)
- SpecializationUtil.registerEventListener(placeableType, "onPeriodChanged", PlaceableHusbandryAnimals)
- SpecializationUtil.registerEventListener(placeableType, "onDayChanged", PlaceableHusbandryAnimals)
- end
- ---
- function PlaceableHusbandryAnimals.registerXMLPaths(schema, basePath)
- schema:setXMLSpecializationType("Husbandry")
- basePath = basePath .. ".husbandry.animals"
- schema:register(XMLValueType.NODE_INDEX, basePath .. ".navigation#rootNode", "Navigation mesh rootnode")
- schema:register(XMLValueType.NODE_INDEX, basePath .. ".navigation#node", "Navigation mesh node")
- schema:register(XMLValueType.NODE_INDEX, basePath .. ".navigation#shape", "Shape to generate navigation mesh from")
- schema:register(XMLValueType.STRING, basePath .. ".navigation#filename", "Filename for an external navigation mesh")
- schema:register(XMLValueType.STRING, basePath .. ".navigation#nodePath", "Nodepath for an external navigation mesh")
- schema:register(XMLValueType.STRING, basePath .. "#type", "Animal type")
- schema:register(XMLValueType.STRING, basePath .. "#filename", "Animal configuration file")
- schema:register(XMLValueType.FLOAT, basePath .. "#placementRaycastDistance", "Placement raycast distance", 2)
- schema:register(XMLValueType.INT, basePath .. "#maxNumAnimals", "Max number of animals", 16)
- schema:register(XMLValueType.INT, basePath .. "#baseMaxNumAnimals", "Base max number of animals without outoor area", 16)
- schema:register(XMLValueType.INT, basePath .. "#sqmPerAnimal", "Square meter need per animal")
- schema:register(XMLValueType.INT, basePath .. "#maxNumVisualAnimals", "Max number of visual animals")
- schema:register(XMLValueType.NODE_INDEX, basePath .. ".loadingTrigger#node", "Animal loading trigger")
- schema:register(XMLValueType.NODE_INDEX, basePath .. ".deliveryAreas.deliveryArea(?)#startNode", "Animal delivery area start node")
- schema:register(XMLValueType.NODE_INDEX, basePath .. ".deliveryAreas.deliveryArea(?)#widthNode", "Animal delivery area width node")
- schema:register(XMLValueType.NODE_INDEX, basePath .. ".deliveryAreas.deliveryArea(?)#heightNode", "Animal delivery area height node")
- schema:setXMLSpecializationType()
- end
- ---
- function PlaceableHusbandryAnimals.registerSavegameXMLPaths(schema, basePath)
- schema:setXMLSpecializationType("Husbandry")
- AnimalClusterSystem.registerSavegameXMLPaths(schema, basePath .. ".clusters")
- schema:setXMLSpecializationType()
- end
- ---
- function PlaceableHusbandryAnimals.initSpecialization()
- g_storeManager:addSpecType("numberAnimals", "shopListAttributeIconCapacity", PlaceableHusbandryAnimals.loadSpecValueNumberAnimals, PlaceableHusbandryAnimals.getSpecValueNumberAnimals, StoreSpecies.PLACEABLE)
- if g_isDevelopmentVersion then
- addConsoleCommand("gsHusbandryAddAnimals", "Add or remove animals from husbandry where player is currently located", "consoleCommandAddAnimals", PlaceableHusbandryAnimals, "numAnimals; [subTypeIndex]")
- addConsoleCommand("gsHusbandryDebugToggle", "Toggle husbandry debug mode", "consoleCommandToggleDebug", PlaceableHusbandryAnimals)
- end
- end
- ---
- function PlaceableHusbandryAnimals:onLoad(savegame)
- local spec = self.spec_husbandryAnimals
- local xmlFile = self.xmlFile
- spec.infoHealth = {title=g_i18n:getText("ui_horseHealth"), text=""}
- spec.infoNumAnimals = {title=g_i18n:getText("ui_numAnimals"), text=""}
- spec.updateVisuals = false
- local animalTypeName = xmlFile:getValue("placeable.husbandry.animals#type")
- if animalTypeName == nil then
- Logging.xmlError(xmlFile, "Missing animal type!")
- self:setLoadingState(PlaceableLoadingState.ERROR)
- return
- end
- local mission = g_currentMission
- spec.animalType = mission.animalSystem:getTypeByName(animalTypeName)
- if spec.animalType == nil then
- Logging.xmlError(xmlFile, "Animal type '%s' not found!", animalTypeName)
- self:setLoadingState(PlaceableLoadingState.ERROR)
- return
- end
- spec.animalTypeIndex = spec.animalType.typeIndex
- spec.navigationMeshRootNode = xmlFile:getValue("placeable.husbandry.animals.navigation#rootNode", nil, self.components, self.i3dMappings)
- spec.navigationMesh = xmlFile:getValue("placeable.husbandry.animals.navigation#node", nil, self.components, self.i3dMappings)
- local navigationMeshShape = xmlFile:getValue("placeable.husbandry.animals.navigation#shape", nil, self.components, self.i3dMappings)
- if navigationMeshShape ~= nil then
- local navMeshShapeValid = true
- if not getHasClassId(navigationMeshShape, ClassIds.SHAPE) then
- Logging.xmlError(xmlFile, "Given navigation shape %q at %q is not of type 'SHAPE'", getName(navigationMeshShape), "placeable.husbandry.animals.navigation#shape")
- navMeshShapeValid = false
- end
- if getHasClassId(navigationMeshShape, ClassIds.NAVIGATION_MESH) then
- Logging.xmlError(xmlFile, "Given navigation shape %q at %q is a navigation mesh instead of a regular shape", getName(navigationMeshShape), "placeable.husbandry.animals.navigation#shape")
- navMeshShapeValid = false
- end
- if navMeshShapeValid and not getShapeIsCPUMesh(navigationMeshShape) then
- Logging.xmlError(xmlFile, "Given navigation shape %q at %q is missing the 'CPU Mesh' flag", getName(navigationMeshShape), "placeable.husbandry.animals.navigation#shape")
- navMeshShapeValid = false
- end
- if navMeshShapeValid then
- setIsNonRenderable(navigationMeshShape, true)
- spec.navigationMeshShape = navigationMeshShape
- end
- end
- local navigationMeshFilename = xmlFile:getValue("placeable.husbandry.animals.navigation#filename", nil)
- if navigationMeshFilename ~= nil then
- navigationMeshFilename = Utils.getFilename(navigationMeshFilename, self.baseDirectory)
- local loadingTask = self:createLoadingTask(spec)
- spec.navigationMeshNodePath = xmlFile:getValue("placeable.husbandry.animals.navigation#nodePath", "0")
- local arguments = {
- loadingTask = loadingTask
- }
- spec.sharedLoadRequestId = g_i3DManager:loadSharedI3DFileAsync(navigationMeshFilename, true, false, self.onExternalNavigationMeshLoaded, self, arguments)
- end
- -- polygon for the nav mesh area defined by the fence outline, excluding the area defined by the navigationMeshShape
- spec.outdoorContourPolygon = nil
- spec.placementRaycastDistance = xmlFile:getValue("placeable.husbandry.animals#placementRaycastDistance", 10.0)
- spec.sqmPerAnimal = self.xmlFile:getValue("placeable.husbandry.animals#sqmPerAnimal", 100)
- spec.configMaxNumAnimals = xmlFile:getValue("placeable.husbandry.animals#maxNumAnimals", 16)
- spec.baseMaxNumAnimals = self.xmlFile:getValue("placeable.husbandry.animals#baseMaxNumAnimals", spec.configMaxNumAnimals)
- spec.configMaxNumVisualAnimals = self.xmlFile:getValue("placeable.husbandry.animals#maxNumVisualAnimals")
- spec.clusterHusbandry = AnimalClusterHusbandry.new(self, animalTypeName, 0) --spec.maxNumVisualAnimals)
- spec.clusterSystem = AnimalClusterSystem.new(self.isServer, self)
- g_messageCenter:subscribe(AnimalClusterUpdateEvent, self.updatedClusters, self)
- local animalLoadingTriggerNode = xmlFile:getValue("placeable.husbandry.animals.loadingTrigger#node", nil, self.components, self.i3dMappings)
- if animalLoadingTriggerNode ~= nil then
- spec.animalLoadingTrigger = AnimalLoadingTrigger.new(self.isServer, self.isClient)
- if not spec.animalLoadingTrigger:load(animalLoadingTriggerNode, self) then
- spec.animalLoadingTrigger:delete()
- end
- end
- spec.deliveryAreas = {}
- self.xmlFile:iterate("placeable.husbandry.animals.deliveryAreas.deliveryArea", function(_, key)
- local area = {}
- if self:loadDeliveryArea(self.xmlFile, key, area) then
- table.insert(spec.deliveryAreas, area)
- end
- end)
- spec.info = {title=g_i18n:getText("statistic_productivity"), text=""}
- if not self.isServer or not g_isDevelopmentVersion then
- removeConsoleCommand("gsHusbandryAddAnimals")
- end
- end
- ---
- function PlaceableHusbandryAnimals:onDelete()
- local spec = self.spec_husbandryAnimals
- g_messageCenter:unsubscribe(AnimalClusterUpdateEvent, self)
- g_messageCenter:unsubscribe(MessageType.CURRENT_MISSION_START, self)
- self:deleteNavigationMeshPlacementCollision()
- if spec.clusterHusbandry ~= nil then
- local mission = g_currentMission
- mission.husbandrySystem:removeClusterHusbandry(spec.clusterHusbandry)
- spec.clusterHusbandry:delete()
- spec.clusterHusbandry = nil
- end
- if spec.animalLoadingTrigger ~= nil then
- spec.animalLoadingTrigger:delete()
- spec.animalLoadingTrigger = nil
- end
- if spec.sharedLoadRequestId ~= nil then
- g_i3DManager:releaseSharedI3DFile(spec.sharedLoadRequestId)
- end
- end
- ---
- function PlaceableHusbandryAnimals:onFinalizePlacement()
- self:createNavigationMesh()
- if self.isLoadedFromSavegame then
- g_messageCenter:subscribeOneshot(MessageType.CURRENT_MISSION_START, PlaceableHusbandryAnimals.onMissionStarted, self)
- end
- end
- ---
- function PlaceableHusbandryAnimals:onReadStream(streamId, connection)
- local spec = self.spec_husbandryAnimals
- spec.clusterSystem:readStream(streamId, connection)
- end
- ---
- function PlaceableHusbandryAnimals:onWriteStream(streamId, connection)
- local spec = self.spec_husbandryAnimals
- spec.clusterSystem:writeStream(streamId, connection)
- end
- ---
- function PlaceableHusbandryAnimals:saveToXMLFile(xmlFile, key, usedModNames)
- local spec = self.spec_husbandryAnimals
- spec.clusterSystem:saveToXMLFile(xmlFile, key .. ".clusters", usedModNames)
- end
- ---
- function PlaceableHusbandryAnimals:loadFromXMLFile(xmlFile, key)
- local spec = self.spec_husbandryAnimals
- spec.clusterSystem:loadFromXMLFile(xmlFile, key .. ".clusters")
- end
- ---
- function PlaceableHusbandryAnimals:onUpdate(dt)
- local spec = self.spec_husbandryAnimals
- if self.isServer then
- spec.clusterSystem:update(dt)
- end
- if spec.clusterHusbandry ~= nil then
- spec.clusterHusbandry:update(dt)
- end
- if spec.updateVisuals then
- self:updateVisualAnimals()
- spec.updateVisuals = false
- end
- if spec.clusterHusbandry:getNeedsUpdate() then
- self:raiseActive()
- end
- end
- ---
- function PlaceableHusbandryAnimals:onExternalNavigationMeshLoaded(node, failedReason, args)
- local spec = self.spec_husbandryAnimals
- local loadingTask = args.loadingTask
- if node == 0 or node == nil then
- self:finishLoadingTask(loadingTask)
- Logging.error("Missing navigation mesh in external navigation mesh file!")
- return
- end
- if spec.navigationMeshRootNode ~= nil then
- spec.navigationMesh = I3DUtil.indexToObject(node, spec.navigationMeshNodePath)
- link(spec.navigationMeshRootNode, spec.navigationMesh)
- end
- delete(node)
- self:finishLoadingTask(loadingTask)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement