-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseMission.lua
More file actions
1959 lines (1509 loc) · 52 KB
/
BaseMission.lua
File metadata and controls
1959 lines (1509 loc) · 52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
source("dataS/scripts/events/VehicleRemoveEvent.lua")
source("dataS/scripts/events/OnCreateLoadedObjectEvent.lua")
BaseMission = {}
local BaseMission_mt = Class(BaseMission)
BaseMission.STATE_INTRO = 0
BaseMission.STATE_READY = 1
BaseMission.STATE_RUNNING = 2
BaseMission.STATE_FINISHED = 3
BaseMission.STATE_FAILED = 5
BaseMission.STATE_CONTINUED = 6
BaseMission.INPUT_CONTEXT_VEHICLE = "VEHICLE"
BaseMission.INPUT_CONTEXT_PAUSE = "PAUSE"
BaseMission.INPUT_CONTEXT_SYNCHRONIZING = "MP_SYNC"
function BaseMission.new(baseDirectory, customMt, missionCollaborators)
local self = setmetatable({}, customMt or BaseMission_mt)
self.baseDirectory = baseDirectory
self.server = g_server
self.client = g_client
self.messageCenter = missionCollaborators.messageCenter
self.savegameController = missionCollaborators.savegameController
self.inputManager = missionCollaborators.inputManager
self.inputDisplayManager = missionCollaborators.inputDisplayManager
self.achievementManager = missionCollaborators.achievementManager
self.modManager = missionCollaborators.modManager
self.fillTypeManager = missionCollaborators.fillTypeManager
self.fruitTypeManager = missionCollaborators.fruitTypeManager
self.guiSoundPlayer = missionCollaborators.guiSoundPlayer
self.hud = nil
self.placeableSystem = PlaceableSystem.new(self)
self.itemSystem = ItemSystem.new(self)
self.onCreateObjectSystem = OnCreateObjectSystem.new(self)
self.beehiveSystem = BeehiveSystem.new(self)
self.cancelLoading = false
self.vertexBufferMemoryUsage = 0
self.indexBufferMemoryUsage = 0
self.textureMemoryUsage = 0
self.waitForDLCVerification = false
self.waitForCorruptDlcs = false
self.finishedFirstUpdate = false
self.waterY = -200
self.isInsideBuilding = false
self.players = {}
self.connectionsToPlayer = {}
self.updateables = {}
self.nonUpdateables = {}
self.drawables = {}
self.triggerMarkers = {}
self.triggerMarkersAreVisible = true
self.helpTriggers = {}
self.helpTriggersAreVisible = true
self.dynamicallyLoadedObjects = {}
self.isPlayerFrozen = false
self.environment = nil
self.state = BaseMission.STATE_INTRO
self.isRunning = false
self.isLoaded = false
self.numLoadingTasks = 0
self.isMissionStarted = false
self.controlledVehicle = nil
self.controlledVehicles = {}
self.controlPlayer = true
self.isToggleVehicleAllowed = true
self.vehicles = {}
self.enterables = {}
self.interactiveVehicles = {}
self.attachables = {}
self.inputAttacherJoints = {}
self.ownedItems = {}
self.leasedVehicles = {}
self.vehiclesToDelete = {}
self.loadSpawnPlaces = {}
self.storeSpawnPlaces = {}
self.restrictedZones = {}
self.usedLoadPlaces = {}
self.usedStorePlaces = {}
self.vehiclesToSpawn = {}
self.vehiclesToSpawnDirty = false
self.vehiclesToSpawnLoading = false
self.nodeToObject = {}
self.maps = {}
self.surfaceSounds = {}
self.cuttingSounds = {}
self.preSimulateTime = 4000
self.snapAIDirection = true
if GS_IS_CONSOLE_VERSION then
self.maxNumHirables = 6
elseif GS_IS_MOBILE_VERSION then
self.maxNumHirables = 4
else
self.maxNumHirables = 10
end
self.time = 0
self.activatableObjectsSystem = ActivatableObjectsSystem.new(self)
self.pauseListeners = {}
self.paused = false
self.pressStartPaused = false
self.manualPaused = false
self.suspendPaused = false
self.lastNonPauseGameState = GameState.PLAY
self.isLoadingMap = false
self.numLoadingMaps = 0
self.loadingMapBaseDirectory = ""
self.objectsToClassName = {}
self.vehiclesToAttach = {}
self.lastInteractionTime = -1
self.isExitingGame = false
return self
end
function BaseMission:initialize()
self:subscribeSettingsChangeMessages()
self:subscribeGuiOpenCloseMessages()
self.messageCenter:subscribe(MessageType.GAME_STATE_CHANGED, self.onGameStateChange, self)
self.hud = self:createHUD()
self.placementManager = PlacementManager.new()
self.benchmark = Benchmark.new()
end
function BaseMission:createHUD()
local class = Platform.isMobile and MobileHUD or HUD
local hud = class.new(g_server ~= nil, g_client ~= nil, GS_IS_CONSOLE_VERSION, self.messageCenter, g_i18n, self.inputManager, self.inputDisplayManager, self.modManager, self.fillTypeManager, self.fruitTypeManager, self.guiSoundPlayer, self, g_farmManager, g_farmlandManager)
return hud
end
function BaseMission:delete()
self.messageCenter:unsubscribeAll(self)
self.isExitingGame = true
self.isRunning = false
self:setMapTargetHotspot(nil)
if BaseMission.MAP_TARGET_MARKER ~= nil then
delete(BaseMission.MAP_TARGET_MARKER)
BaseMission.MAP_TARGET_MARKER = nil
end
if self:getIsClient() and not self.controlPlayer and self.controlledVehicle ~= nil then
self:onLeaveVehicle()
end
for k, v in pairs(self.nonUpdateables) do
v:delete()
self.nonUpdateables[k] = nil
end
if g_server ~= nil then
g_server:delete()
g_server = nil
end
if g_client ~= nil then
g_client:delete()
g_client = nil
end
setCamera(g_defaultCamera)
if self.hud ~= nil then
self.messageCenter:unsubscribeAll(self.hud)
self.hud:setEnvironment(nil)
self.hud:delete()
self.hud = nil
end
if self.placementManager ~= nil then
self.placementManager:delete()
end
if self.benchmark ~= nil then
self.benchmark:delete()
end
if self.player ~= nil then
self.player:delete()
end
if self.trafficSystem ~= nil then
self.trafficSystem:setEnabled(false)
self.trafficSystem:reset()
end
if self.pedestrianSystem ~= nil then
self.pedestrianSystem:delete()
self.pedestrianSystem = nil
end
g_terrainDeformationQueue:cancelAllJobs()
for _, v in pairs(self.vehicles) do
v:delete()
end
self.vehicles = {}
self.leasedVehicles = {}
self.ownedItems = {}
for _, vehicle in ipairs(self.vehiclesToDelete) do
if not vehicle.isDeleted then
vehicle:delete()
end
end
self.placeableSystem:delete()
self.itemSystem:delete()
self.onCreateObjectSystem:delete()
self.beehiveSystem:delete()
for _, object in pairs(self.dynamicallyLoadedObjects) do
delete(object)
end
if self.environment ~= nil then
self.inGameMenu:setEnvironment(nil)
self.environment:delete()
self.environment = nil
end
for k, updateable in pairs(self.updateables) do
if updateable.delete ~= nil then
updateable:delete()
end
self.updateables[k] = nil
end
for i = #g_modEventListeners, 1, -1 do
if g_modEventListeners[i].deleteMap ~= nil then
g_modEventListeners[i]:deleteMap()
end
end
for _, v in pairs(self.maps) do
delete(v)
end
for _, surfaceSound in pairs(self.surfaceSounds) do
g_soundManager:deleteSample(surfaceSound.sample)
end
self.surfaceSounds = {}
for _, cuttingSound in pairs(self.cuttingSounds) do
g_soundManager:deleteSample(cuttingSound)
end
self.cuttingSounds = {}
self:unregisterActionEvents()
removeConsoleCommand("gsCameraFovSet")
removeConsoleCommand("gsRender360Screenshot")
removeConsoleCommand("gsVehicleRemoveAll")
removeConsoleCommand("gsItemRemoveAll")
removeConsoleCommand("gsShaderParamsSet")
self.inputManager:clearAllContexts()
g_gui:setCurrentMission(nil)
g_gui:setClient(nil)
end
function BaseMission:load()
self:startLoadingTask()
self.controlPlayer = true
self.controlledVehicle = nil
addConsoleCommand("gsCameraFovSet", "Sets camera field of view angle", "consoleCommandSetFOV", self)
if self:getIsServer() and g_addTestCommands then
addConsoleCommand("gsRender360Screenshot", "Renders 360 screenshots from current camera position", "consoleCommandRender360Screenshot", self)
addConsoleCommand("gsVehicleRemoveAll", "Removes all vehicles from current mission", "consoleCommandVehicleRemoveAll", self)
addConsoleCommand("gsItemRemoveAll", "Removes all items from current mission", "consoleCommandItemRemoveAll", self)
addConsoleCommand("gsShaderParamsSet", "Sets shader parameters for given nodeName and shader parameter name", "consoleCommandSetShaderParameter", self)
end
self:finishLoadingTask()
end
function BaseMission:startLoadingTask()
self.numLoadingTasks = self.numLoadingTasks + 1
if self.numLoadingTasks == 1 then
setStreamLowPriorityI3DFiles(false)
if self.missionDynamicInfo.isMultiplayer then
netSetIsEventProcessingEnabled(false)
end
end
end
function BaseMission:finishLoadingTask()
self.numLoadingTasks = self.numLoadingTasks - 1
if self.numLoadingTasks <= 0 then
if not self.isLoaded then
self:onFinishedLoading()
end
setStreamLowPriorityI3DFiles(true)
if self.missionDynamicInfo.isMultiplayer then
netSetIsEventProcessingEnabled(true)
end
end
end
function BaseMission:onFinishedLoading()
self.isLoaded = true
g_gui:setCurrentMission(self)
g_gui:setClient(g_client)
end
function BaseMission:canStartMission()
if self:getIsServer() then
return true
end
for i = 1, #self.vehicles do
local vehicle = self.vehicles[i]
if not vehicle:getIsSynchronized() then
return false
end
end
return self.player ~= nil
end
function BaseMission:onStartMission()
self:fadeScreen(-1, 1500, nil)
self.isMissionStarted = true
self:setShowTriggerMarker(g_gameSettings:getValue("showTriggerMarker"))
self:setShowHelpTrigger(g_gameSettings:getValue("showHelpTrigger"))
if self:getIsClient() then
local context = Player.INPUT_CONTEXT_NAME
self.inputManager:setContext(context, true, true)
self:registerActionEvents()
self:registerPauseActionEvents()
end
end
function BaseMission:onObjectCreated(object)
if object:isa(Player) then
self.players[object.rootNode] = object
if object.isOwner then
self.player = object
self.inGameMenu:setPlayer(object)
self.hud:setPlayer(object)
end
if self:getIsServer() then
self.connectionsToPlayer[object.networkInformation.creatorConnection] = object
end
g_messageCenter:publish(MessageType.PLAYER_CREATED, object)
elseif object:isa(Vehicle) or object:isa(RailroadVehicle) then
self:addVehicle(object)
elseif object:isa(Farm) then
g_farmManager:onFarmObjectCreated(object)
end
end
function BaseMission:onObjectDeleted(object)
if object:isa(Player) then
if self.player == object then
self.player = nil
end
self.players[object.rootNode] = nil
if self:getIsServer() then
self.connectionsToPlayer[object.networkInformation.creatorConnection] = nil
end
elseif object:isa(Vehicle) or object:isa(RailroadVehicle) then
if object.isAddedToMission then
self:removeVehicle(object, false)
end
elseif object:isa(Farm) then
g_farmManager:onFarmObjectDeleted(object)
end
end
function BaseMission:loadMap(filename, addPhysics, asyncCallbackFunction, asyncCallbackObject, asyncCallbackArguments)
if addPhysics == nil then
addPhysics = true
end
local modMapName, baseDirectory = Utils.getModNameAndBaseDirectory(filename)
if self.numLoadingMaps == 0 then
self.loadingMapModName = modMapName
self.loadingMapBaseDirectory = baseDirectory
resetModOnCreateFunctions()
for modName, loaded in pairs(g_modIsLoaded) do
if loaded and not g_modManager:isModMap(modName) then
_G[modName].g_onCreateUtil.activateOnCreateFunctions()
end
end
if modMapName ~= nil then
_G[modMapName].g_onCreateUtil.activateOnCreateFunctions()
end
self.isLoadingMap = true
elseif self.loadingMapBaseDirectory ~= baseDirectory then
print("Warning: Asynchronous map loading from different mods. onCreate functions will not work correctly")
end
self.numLoadingMaps = self.numLoadingMaps + 1
if asyncCallbackFunction ~= nil then
local args = {
filename = filename,
asyncCallbackFunction = asyncCallbackFunction,
asyncCallbackObject = asyncCallbackObject,
asyncCallbackArguments = asyncCallbackArguments
}
g_i3DManager:loadI3DFileAsync(filename, true, addPhysics, self.loadMapFinished, self, args)
else
Logging.error("Loading the map in sync is not allowed anymore! Please call loadMap with a async callback.")
printCallstack()
end
end
function BaseMission:loadMapFinished(node, failedReason, arguments, callAsyncCallback)
g_mpLoadingScreen:hitLoadingTarget(MPLoadingScreen.LOAD_TARGETS.MAP)
local filename = arguments.filename
local asyncCallbackFunction = arguments.asyncCallbackFunction
local asyncCallbackObject = arguments.asyncCallbackObject
local asyncCallbackArguments = arguments.asyncCallbackArguments
if node ~= 0 then
self:findDynamicObjects(node)
end
self.numLoadingMaps = self.numLoadingMaps - 1
if self.numLoadingMaps == 0 then
self.isLoadingMap = false
resetModOnCreateFunctions()
self.loadingMapModName = nil
self.loadingMapBaseDirectory = ""
end
if node ~= 0 and not g_currentMission.cancelLoading then
table.insert(self.maps, node)
link(getRootNode(), node)
end
for _, v in pairs(g_modEventListeners) do
if v.loadMap ~= nil then
v:loadMap(filename)
end
end
if not self.cancelLoading then
self:setShowFieldInfo(g_gameSettings:getValue("showFieldInfo"))
end
if (callAsyncCallback == nil or callAsyncCallback) and asyncCallbackFunction ~= nil then
asyncCallbackFunction(asyncCallbackObject, node, asyncCallbackArguments)
end
end
function BaseMission:findDynamicObjects(node)
for i = 1, getNumOfChildren(node) do
local c = getChildAt(node, i - 1)
if RigidBodyType.DYNAMIC == getRigidBodyType(c) then
if (not getHasClassId(c, ClassIds.SHAPE) or getSplitType(c) == 0) and self.missionDynamicInfo.isMultiplayer then
local mpCreatePhysicsObject = Utils.getNoNil(getUserAttribute(c, "mpCreatePhysicsObject"), false)
local mpRemoveRigidBody = Utils.getNoNil(getUserAttribute(c, "mpRemoveRigidBody"), true)
if mpCreatePhysicsObject then
local object = PhysicsObject.new(self:getIsServer(), self:getIsClient())
g_currentMission.onCreateObjectSystem:add(object)
object:loadOnCreate(c)
object:register(true)
elseif mpRemoveRigidBody then
setRigidBodyType(c, RigidBodyType.NONE)
end
end
else
self:findDynamicObjects(c)
end
end
end
function BaseMission:loadMapSounds(xmlFilename, baseDirectory)
if not self:getIsClient() then
return
end
local xmlFile = loadXMLFile("mapSoundXML", xmlFilename)
if xmlFile == 0 then
return
end
self.surfaceSounds = {}
local i = 0
while true do
local key = string.format("sound.surface.material(%d)", i)
if not hasXMLProperty(xmlFile, key) then
break
end
local entry = {}
local audioGroup = AudioGroup.ENVIRONMENT
entry.type = Utils.getNoNil(getXMLString(xmlFile, key .. "#type"), "wheel")
if entry.type == "wheel" then
audioGroup = AudioGroup.VEHICLE
end
entry.materialId = getXMLInt(xmlFile, key .. "#materialId")
entry.name = getXMLString(xmlFile, key .. "#name")
local loopCount = getXMLInt(xmlFile, key .. "#loopCount") or 0
entry.sample = g_soundManager:loadSampleFromXML(xmlFile, "sound.surface", string.format("material(%d)", i), baseDirectory, getRootNode(), loopCount, audioGroup, nil, nil)
if entry.sample ~= nil then
table.insert(self.surfaceSounds, entry)
end
i = i + 1
end
self.cuttingSounds = {}
local j = 0
while true do
local key = string.format("sound.cutting.sample(%d)", j)
if not hasXMLProperty(xmlFile, key) then
break
end
local name = getXMLString(xmlFile, key .. "#name")
local sample = g_soundManager:loadSampleFromXML(xmlFile, "sound.cutting", string.format("sample(%d)", j), baseDirectory, getRootNode(), 1, AudioGroup.ENVIRONMENT, nil, nil)
if name ~= nil then
self.cuttingSounds[name] = sample
else
print("Warning: a cutting sound does not have a name")
end
j = j + 1
end
delete(xmlFile)
end
function BaseMission:loadObjectAtPlace(xmlFilename, places, usedPlaces, rotationOffset, ownerFarmId)
local size = StoreItemUtil.getSizeValues(xmlFilename, "object", rotationOffset)
local isLimitReached = false
local x, y, z, place, width, _ = PlacementUtil.getPlace(places, size, usedPlaces, true, false, true)
if x == nil then
return nil, true, isLimitReached
end
local object = nil
local yRot = MathUtil.getYRotationFromDirection(place.dirPerpX, place.dirPerpZ)
yRot = yRot + rotationOffset
local xmlFile = loadXMLFile("tempObjectXML", xmlFilename)
local className = Utils.getNoNil(getXMLString(xmlFile, "object.className"), "")
local filename = getXMLString(xmlFile, "object.filename")
local class = ClassUtil.getClassObject(className)
if class ~= nil then
if filename ~= nil then
object = class.new(self:getIsServer(), self:getIsClient())
object:setOwnerFarmId(ownerFarmId, true)
filename = Utils.getFilename(filename, self.baseDirectory)
if object:load(filename, x, y, z, 0, yRot, 0, xmlFilename) then
object:register()
object:setFillLevel(object.capacity, false)
else
object:delete()
object = nil
end
else
print("Warning: File '" .. tostring(filename) .. "' not found!")
end
else
print("Warning: Class '" .. tostring(className) .. "' not found!")
end
delete(xmlFile)
if object ~= nil then
PlacementUtil.markPlaceUsed(usedPlaces, place, width)
return object, false, isLimitReached
end
return nil, false, isLimitReached
end
function BaseMission:addOwnedItem(item)
BaseMission.addItemToList(self.ownedItems, item)
end
function BaseMission:removeOwnedItem(item)
BaseMission.removeItemFromList(self.ownedItems, item)
end
function BaseMission:getNumOwnedItems(storeItem, farmId)
return BaseMission.getNumListItems(self.ownedItems, storeItem, farmId)
end
function BaseMission:addLeasedItem(item)
BaseMission.addItemToList(self.leasedVehicles, item)
end
function BaseMission:removeLeasedItem(item)
BaseMission.removeItemFromList(self.leasedVehicles, item)
end
function BaseMission:getNumLeasedItems(storeItem, farmId)
return BaseMission.getNumListItems(self.leasedVehicles, storeItem, farmId)
end
function BaseMission.getNumListItems(list, storeItem, farmId)
local numItems = 0
if storeItem.bundleInfo == nil then
if list[storeItem] ~= nil then
if farmId == nil then
numItems = list[storeItem].numItems
else
numItems = 0
for _, item in pairs(list[storeItem].items) do
if item:getOwnerFarmId() == farmId then
numItems = numItems + 1
end
end
end
end
else
local maxNumOfItems = math.huge
for _, bundleItem in pairs(storeItem.bundleInfo.bundleItems) do
maxNumOfItems = math.min(maxNumOfItems, BaseMission.getNumListItems(list, bundleItem.item, farmId))
end
numItems = maxNumOfItems
end
return numItems
end
function BaseMission.addItemToList(list, item)
if list == nil or item == nil then
return
end
local storeItem = g_storeManager:getItemByXMLFilename(item.configFileName)
if storeItem ~= nil then
if list[storeItem] == nil then
list[storeItem] = {
numItems = 0,
storeItem = storeItem,
items = {}
}
end
if list[storeItem].items[item] == nil then
list[storeItem].numItems = list[storeItem].numItems + 1
list[storeItem].items[item] = item
end
end
end
function BaseMission.removeItemFromList(list, item)
if list == nil or item == nil then
return
end
local storeItem = g_storeManager:getItemByXMLFilename(item.configFileName)
if storeItem ~= nil and list[storeItem] ~= nil and list[storeItem].items[item] ~= nil then
list[storeItem].numItems = list[storeItem].numItems - 1
list[storeItem].items[item] = nil
if list[storeItem].numItems == 0 then
list[storeItem] = nil
end
end
end
function BaseMission:addVehicle(vehicle)
table.addElement(self.vehicles, vehicle)
vehicle.isAddedToMission = true
if vehicle.propertyState == Vehicle.PROPERTY_STATE_OWNED then
self:addOwnedItem(vehicle)
elseif vehicle.propertyState == Vehicle.PROPERTY_STATE_LEASED then
self:addLeasedItem(vehicle)
end
end
function BaseMission:removeVehicle(vehicle, callDelete)
if self:getIsClient() and vehicle == self.controlledVehicle then
self:onLeaveVehicle()
end
if vehicle.propertyState == Vehicle.PROPERTY_STATE_OWNED then
self:removeOwnedItem(vehicle)
elseif vehicle.propertyState == Vehicle.PROPERTY_STATE_LEASED then
self:removeLeasedItem(vehicle)
end
table.removeElement(self.vehicles, vehicle)
vehicle:removeNodeObjectMapping(self.nodeToObject)
table.removeElement(self.vehiclesToDelete, vehicle)
vehicle.isAddedToMission = false
if callDelete == nil or callDelete == true then
if self:getIsServer() then
table.addElement(self.vehiclesToDelete, vehicle)
else
g_client:getServerConnection():sendEvent(VehicleRemoveEvent.new(vehicle))
end
end
end
function BaseMission:addVehicleToDelete(vehicle)
table.addElement(self.vehiclesToDelete, vehicle)
end
function BaseMission:addVehicleToSpawn(xmlFilename, xmlKey)
table.insert(self.vehiclesToSpawn, {
xmlFilename = xmlFilename,
xmlKey = xmlKey
})
self.vehiclesToSpawnDirty = true
end
function BaseMission:addUpdateable(updateable, key)
assert(updateable.isa == nil or not updateable:isa(Object), "No network objects allowed in addUpdateable")
if updateable.update == nil then
Logging.error("Given updateable has no update function")
printCallstack()
return
end
self.updateables[key or updateable] = updateable
end
function BaseMission:removeUpdateable(updateable)
self.updateables[updateable] = nil
end
function BaseMission:getHasUpdateable(updateable)
return self.updateables[updateable] ~= nil
end
function BaseMission:getHasDrawable(drawable)
return self.drawables[drawable] ~= nil
end
function BaseMission:addDrawable(drawable, key)
self.drawables[key or drawable] = drawable
end
function BaseMission:removeDrawable(drawable)
self.drawables[drawable] = nil
end
function BaseMission:addNonUpdateable(nonUpdateable)
assert(nonUpdateable.isa == nil or not nonUpdateable:isa(Object), "No network objects allowed in addNonUpdateable")
self.nonUpdateables[nonUpdateable] = nonUpdateable
end
function BaseMission:removeNonUpdateable(nonUpdateable)
self.nonUpdateables[nonUpdateable] = nil
end
function BaseMission:addNodeObject(node, object)
if self.nodeToObject[node] ~= nil then
Logging.error("Node '%s' already has a node-object mapping '%s'", getName(node), tostring(object))
printCallstack()
return
end
self.nodeToObject[node] = object
end
function BaseMission:removeNodeObject(node)
self.nodeToObject[node] = nil
end
function BaseMission:getNodeObject(node)
return self.nodeToObject[node]
end
function BaseMission:pauseGame()
if not self.paused then
self:doPauseGame()
if self:getIsServer() then
GamePauseEvent.sendEvent()
end
end
end
function BaseMission:tryUnpauseGame()
if self:canUnpauseGame() then
self:doUnpauseGame()
if self:getIsServer() then
GamePauseEvent.sendEvent()
end
return true
end
return false
end
function BaseMission:canUnpauseGame()
return self.paused and not self.manualPaused and not self.suspendPaused and not self.pressStartPaused
end
function BaseMission:setManualPause(doPause)
if (self:getIsServer() or self.isMasterUser) and doPause ~= self.manualPaused then
self.manualPaused = doPause
if self:getIsServer() then
if doPause then
self:pauseGame()
else
self:tryUnpauseGame()
end
else
g_client:getServerConnection():sendEvent(GamePauseRequestEvent.new(doPause))
end
end
end
function BaseMission:doPauseGame()
self.paused = true
self.isRunning = false
simulatePhysics(false)
simulateParticleSystems(false)
self:resetGameState()
if self.hud ~= nil and not g_gameSettings:getValue(GameSettings.SETTING.SHOW_HELP_MENU) then
self.hud:setInputHelpVisible(true)
end
for target, callbackFunc in pairs(self.pauseListeners) do
callbackFunc(target, self.paused)
end
g_messageCenter:publish(MessageType.PAUSE, true)
if self.trafficSystem ~= nil then
self.trafficSystem:setEnabled(false)
end
if self.pedestrianSystem ~= nil then
self.pedestrianSystem:setEnabled(false)
end
end
function BaseMission:doUnpauseGame()
self.paused = false
self.isRunning = true
simulatePhysics(true)
simulateParticleSystems(true)
if self.hud ~= nil and not g_gameSettings:getValue(GameSettings.SETTING.SHOW_HELP_MENU) then
self.hud:setInputHelpVisible(g_gameSettings:getValue(GameSettings.SETTING.SHOW_HELP_MENU))
end
local lastNonPauseGameState = self.lastNonPauseGameState
if lastNonPauseGameState == GameState.MENU_INGAME and g_gui.currentGuiName ~= "InGameMenu" then
lastNonPauseGameState = GameState.PLAY
end
g_gameStateManager:setGameState(lastNonPauseGameState)
for target, callbackFunc in pairs(self.pauseListeners) do
callbackFunc(target, self.paused)
end
g_messageCenter:publish(MessageType.PAUSE, false)
if self.trafficSystem ~= nil then
self.trafficSystem:setEnabled(g_currentMission.missionInfo.trafficEnabled)
end
if self.pedestrianSystem ~= nil then
self.pedestrianSystem:setEnabled(true)
end
end
function BaseMission:addPauseListeners(target, callbackFunc)
self.pauseListeners[target] = callbackFunc
end
function BaseMission:removePauseListeners(target)
self.pauseListeners[target] = nil
end
function BaseMission:resetGameState()
if self.pressStartPaused then
g_gameStateManager:setGameState(GameState.LOADING)
elseif self.paused then
g_gameStateManager:setGameState(GameState.PAUSED)
else
g_gameStateManager:setGameState(GameState.PLAY)
end
end
function BaseMission:toggleVehicle(delta)
if not self.isToggleVehicleAllowed then
return
end
local numVehicles = #self.enterables
if numVehicles > 0 then
local index = 1
local oldIndex = 1
if not self.controlPlayer and self.controlledVehicle ~= nil then
for i = 1, numVehicles do
if self.controlledVehicle == self.enterables[i] then
oldIndex = i
index = i + delta
if numVehicles < index then
index = 1
end
if index < 1 then
index = numVehicles
end
break
end
end
elseif delta < 0 then
index = numVehicles
end
local found = false
repeat
local enterable = self.enterables[index]
if enterable:getIsTabbable() and enterable:getIsEnterable() then