912 | function MissionManager:getRandomVehicleGroup(missionType, fieldSize, variant) |
913 | local groups = self.missionVehicles[missionType] |
914 | if groups == nil then |
915 | return nil, 1.0 |
916 | end |
917 | |
918 | local sized = groups[fieldSize] |
919 | if sized == nil then |
920 | return nil, 1.0 |
921 | end |
922 | |
923 | local variantGroups = ListUtil.ifilter(sized, function (group) |
924 | return variant == nil or group.variant == variant |
925 | end) |
926 | |
927 | local group = ListUtil.getRandomElement(variantGroups) |
928 | if group == nil then |
929 | return nil, 1.0 |
930 | end |
931 | |
932 | return group.vehicles, group.identifier |
933 | end |
406 | function MissionManager:loadFromXMLFile(xmlFilename) |
407 | if xmlFilename == nil then |
408 | return false |
409 | end |
410 | |
411 | local xmlFile = loadXMLFile("missionsXML", xmlFilename) |
412 | if not xmlFile then |
413 | return false |
414 | end |
415 | |
416 | -- Active missions |
417 | local i = 0 |
418 | while true do |
419 | local key = string.format("missions.mission(%d)", i) |
420 | if not hasXMLProperty(xmlFile, key) then |
421 | break |
422 | end |
423 | |
424 | local missionTypeName = getXMLString(xmlFile, key.."#type") |
425 | local missionType = self:getMissionType(missionTypeName) |
426 | |
427 | if missionType ~= nil then |
428 | local mission = missionType.class:new(true, g_client ~= nil) |
429 | mission.type = missionType |
430 | mission.activeMissionId = getXMLInt(xmlFile, key .. "#activeId") -- can be nil |
431 | self:assignGenerationTime(mission) |
432 | |
433 | if not mission:loadFromXMLFile(xmlFile, key) then |
434 | mission:delete() |
435 | else |
436 | if mission.field ~= nil then |
437 | self.fieldToMission[mission.field.fieldId] = mission |
438 | end |
439 | |
440 | if mission.type.category == MissionManager.CATEGORY_TRANSPORT then |
441 | self.numTransportMissions = self.numTransportMissions + 1 |
442 | end |
443 | |
444 | mission:register() |
445 | table.insert(self.missions, mission) |
446 | end |
447 | else |
448 | print("Warning: Mission type '" .. tostring(missionType) .. "' not found!") |
449 | end |
450 | |
451 | i = i + 1 |
452 | end |
453 | |
454 | -- If there are any active missions, find any associated vehicles |
455 | if table.getn(self.missions) > 0 then |
456 | for _, vehicle in pairs(g_currentMission.vehicles) do |
457 | if vehicle.activeMissionId ~= nil then |
458 | local mission = self:getMissionForActiveMissionId(vehicle.activeMissionId) |
459 | if mission ~= nil and mission.vehicles ~= nil then |
460 | table.insert(mission.vehicles, vehicle) |
461 | end |
462 | end |
463 | end |
464 | end |
465 | |
466 | delete(xmlFile) |
467 | |
468 | return true |
469 | end |
76 | function MissionManager:loadMapData(xmlFile) |
77 | MissionManager:superClass().loadMapData(self) |
78 | |
79 | self:createMissionMap() |
80 | |
81 | if g_currentMission:getIsServer() then |
82 | g_currentMission:addUpdateable(self) |
83 | |
84 | self.missionNextGenerationTime = g_currentMission.time |
85 | |
86 | self.fieldDataDmod = DensityMapModifier:new(g_currentMission.terrainDetailId, g_currentMission.sprayFirstChannel, g_currentMission.sprayNumChannels) |
87 | self.fieldDataFilter = DensityMapFilter:new(g_currentMission.terrainDetailId, g_currentMission.terrainDetailTypeFirstChannel, g_currentMission.terrainDetailTypeNumChannels) |
88 | self.fieldDataFilter:setValueCompareParams("greater", 0) |
89 | end |
90 | |
91 | local p = getXMLString(xmlFile, "map.transportMissions#filename") |
92 | if p ~= nil then |
93 | local path = Utils.getFilename(p, g_currentMission.baseDirectory) |
94 | if path ~= nil and path ~= "" then |
95 | self:loadTransportMissions(path) |
96 | end |
97 | end |
98 | |
99 | local p = getXMLString(xmlFile, "map.missionVehicles#filename") |
100 | if p ~= nil then |
101 | local path = Utils.getFilename(p, g_currentMission.baseDirectory) |
102 | if path ~= nil then |
103 | self:loadMissionVehicles(path) |
104 | end |
105 | end |
106 | |
107 | if g_currentMission:getIsServer() then |
108 | -- Generate a weighted list of transport missions |
109 | for _, missionType in ipairs(self.missionTypes) do |
110 | if missionType.category == MissionManager.CATEGORY_TRANSPORT then |
111 | for i = 1, missionType.priority do |
112 | table.insert(self.possibleTransportMissionsWeighted, missionType) |
113 | end |
114 | end |
115 | end |
116 | end |
117 | |
118 | g_messageCenter:subscribe(MessageType.MISSION_DELETED, self.onMissionDeleted, self) |
119 | |
120 | if g_addTestCommands then |
121 | addConsoleCommand("gsGenerateFieldMission", "Force generating a new mission for given field", "consoleGenerateFieldMission", self) |
122 | addConsoleCommand("gsMissionLoadAllVehicles", "Loading and unloading all field mission vehicles", "consoleLoadAllFieldMissionVehicles", self) |
123 | |
124 | addConsoleCommand("gsMissionHarvestField", "Harvest a field and print the liters", "consoleHarvestField", self) |
125 | addConsoleCommand("gsMissionTestHarvests", "Run an expansive tests for harvest missions", "consoleHarvestTests", self) |
126 | end |
127 | end |
33 | function MissionManager:new(customMt) |
34 | local self = AbstractManager:new(customMt or MissionManager_mt) |
35 | |
36 | self.missionTypes = {} |
37 | self.missionTypeIdToType = {} |
38 | |
39 | self.defaultMissionMapWidth = 512 |
40 | self.defaultMissionMapHeight = 512 |
41 | self.missionMapNumChannels = 4 |
42 | |
43 | -- Only on new game start. Reset is done when quitting a game. |
44 | -- This value is changed very early in the loading process |
45 | self.numTransportTriggers = 0 |
46 | self.transportTriggers = {} |
47 | |
48 | return self |
49 | end |
381 | function MissionManager:saveToXMLFile(xmlFilename) |
382 | local xmlFile = createXMLFile("missionXML", xmlFilename, "missions") |
383 | |
384 | if xmlFile ~= nil then |
385 | for k, mission in ipairs(self.missions) do |
386 | local missionKey = string.format("missions.mission(%d)", k - 1) |
387 | |
388 | setXMLString(xmlFile, missionKey.."#type", mission.type.name) |
389 | if mission.activeMissionId ~= nil then |
390 | setXMLInt(xmlFile, missionKey.."#activeId", mission.activeMissionId) |
391 | end |
392 | |
393 | mission:saveToXMLFile(xmlFile, missionKey) |
394 | end |
395 | |
396 | saveXMLFile(xmlFile) |
397 | delete(xmlFile) |
398 | end |
399 | |
400 | return false |
401 | end |
352 | function MissionManager:unloadMapData() |
353 | g_messageCenter:unsubscribeAll() |
354 | g_currentMission:removeUpdateable(self) |
355 | |
356 | self.numTransportTriggers = 0 |
357 | self.transportTriggers = {} |
358 | |
359 | self.fieldDataDmod = nil |
360 | self.fieldDataFilter = nil |
361 | |
362 | self.fieldToMission = {} |
363 | self.possibleTransportMissionsWeighted = {} |
364 | |
365 | self:destroyMissionMap() |
366 | |
367 | if g_addTestCommands then |
368 | removeConsoleCommand("gsGenerateFieldMission") |
369 | removeConsoleCommand("gsMissionLoadAllVehicles") |
370 | removeConsoleCommand("gsMissionHarvestField") |
371 | removeConsoleCommand("gsMissionTestHarvests") |
372 | end |
373 | |
374 | MissionManager:superClass().unloadMapData(self) |
375 | end |