-
Notifications
You must be signed in to change notification settings - Fork 85
/
ExtCitizenInstanceManager.cs
1582 lines (1377 loc) · 74.1 KB
/
ExtCitizenInstanceManager.cs
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
namespace TrafficManager.Manager.Impl {
using ColossalFramework.Globalization;
using ColossalFramework;
using CSUtil.Commons;
using System.Collections.Generic;
using System;
using ColossalFramework.Math;
using TrafficManager.API.Manager;
using TrafficManager.API.Traffic.Data;
using TrafficManager.API.Traffic.Enums;
using TrafficManager.Custom.PathFinding;
using TrafficManager.State.ConfigData;
using TrafficManager.State;
using UnityEngine;
using TrafficManager.Util;
using TrafficManager.Util.Extensions;
public class ExtCitizenInstanceManager
: AbstractCustomManager,
ICustomDataManager<List<Configuration.ExtCitizenInstanceData>>,
IExtCitizenInstanceManager
{
private ExtCitizenInstanceManager() {
uint maxInstanceCount = CitizenManager.instance.m_instances.m_size;
ExtInstances = new ExtCitizenInstance[maxInstanceCount];
for (uint i = 0; i < maxInstanceCount; ++i) {
ExtInstances[i] = new ExtCitizenInstance((ushort)i);
}
}
public static readonly ExtCitizenInstanceManager Instance = new ExtCitizenInstanceManager();
/// <summary>
/// Gets all additional data for citizen instance. Index: citizen instance id.
/// </summary>
public ExtCitizenInstance[] ExtInstances { get; }
protected override void InternalPrintDebugInfo() {
base.InternalPrintDebugInfo();
Log._Debug("Extended citizen instance data:");
CitizenInstance[] instanceBuffer = CitizenManager.instance.m_instances.m_buffer;
for (uint citizenInstanceId = 0; citizenInstanceId < ExtInstances.Length; ++citizenInstanceId) {
ref CitizenInstance citizenInstance = ref instanceBuffer[citizenInstanceId];
if (!citizenInstance.IsValid()) {
continue;
}
Log._Debug($"Citizen instance {citizenInstanceId}: {ExtInstances[citizenInstanceId]}");
}
}
public void OnReleaseInstance(ushort instanceId) {
Reset(ref ExtInstances[instanceId]);
}
public void ResetInstance(ushort instanceId) {
Reset(ref ExtInstances[instanceId]);
}
public override void OnLevelUnloading() {
base.OnLevelUnloading();
Reset();
}
/// <summary>
/// Generates the localized status for a tourist, which is displayed on <c>TouristWorldInfoPanel</c>.
/// </summary>
///
/// <param name="instanceID">Citizen instance id.</param>
/// <param name="data">Citizen instance data.</param>
/// <param name="mayAddCustomStatus">Will be <c>true</c> if the status can be customised by callee.</param>
/// <param name="target">The instance of target building or node.</param>
///
/// <returns>Returns the localised tourist status.</returns>
public string GetTouristLocalizedStatus(ushort instanceID,
ref CitizenInstance data,
out bool mayAddCustomStatus,
out InstanceID target) {
mayAddCustomStatus = true;
target = InstanceID.Empty;
ushort targetBuildingId = data.m_targetBuilding;
if (IsSweaptAway(ref data) || targetBuildingId == 0) {
mayAddCustomStatus = false;
return Locale.Get("CITIZEN_STATUS_CONFUSED");
}
uint citizenId = data.m_citizen;
ushort vehicleId = 0;
// if true, it means the targetBuildingId is actually a node, not a building
bool targetIsNode = (data.m_flags & CitizenInstance.Flags.TargetIsNode) != 0;
if (citizenId != 0u) {
ref Citizen citizen = ref CitizenManager.instance.m_citizens.m_buffer[citizenId];
vehicleId = citizen.m_vehicle;
}
if (vehicleId != 0) {
ref Vehicle vehicle = ref vehicleId.ToVehicle();
VehicleInfo info = vehicle.Info;
switch (info.m_class.m_service) {
case ItemClass.Service.Residential
when info.m_vehicleType != VehicleInfo.VehicleType.Bicycle
&& IsVehicleOwnedByCitizen(ref vehicle, citizenId):
if (targetIsNode) {
target.NetNode = targetBuildingId;
return Locale.Get("CITIZEN_STATUS_DRIVINGTO");
}
if (IsOutsideConnection(targetBuildingId)) {
return Locale.Get("CITIZEN_STATUS_DRIVINGTO_OUTSIDE");
}
target.Building = targetBuildingId;
return Locale.Get("CITIZEN_STATUS_DRIVINGTO");
case ItemClass.Service.PublicTransport:
case ItemClass.Service.Disaster:
if (targetIsNode) {
if ((data.m_flags & CitizenInstance.Flags.WaitingTaxi) != 0) {
return Locale.Get("CITIZEN_STATUS_WAITING_TAXI");
}
ushort transportLine = targetBuildingId.ToNode().m_transportLine;
if (vehicle.m_transportLine != transportLine) {
target.NetNode = targetBuildingId;
return Locale.Get("CITIZEN_STATUS_TRAVELLINGTO");
}
break;
}
if (IsOutsideConnection(targetBuildingId)) {
return Locale.Get("CITIZEN_STATUS_TRAVELLINGTO_OUTSIDE");
}
target.Building = targetBuildingId;
return Locale.Get("CITIZEN_STATUS_TRAVELLINGTO");
}
}
if (targetIsNode) {
target.NetNode = targetBuildingId;
if ((data.m_flags & CitizenInstance.Flags.OnTour) != 0) {
return Locale.Get("CITIZEN_STATUS_VISITING");
}
return Locale.Get("CITIZEN_STATUS_GOINGTO");
}
if (IsOutsideConnection(targetBuildingId)) {
return Locale.Get("CITIZEN_STATUS_GOINGTO_OUTSIDE");
}
target.Building = targetBuildingId;
if (IsHangingAround(ref data)) {
mayAddCustomStatus = false;
return Locale.Get("CITIZEN_STATUS_VISITING");
}
return Locale.Get("CITIZEN_STATUS_GOINGTO");
}
/// <summary>
/// Generates the localized status for a resident, which is displayed on <c>CitizenWorldInfoPanel</c>.
/// </summary>
///
/// <param name="instanceID">Citizen instance id.</param>
/// <param name="data">Citizen instance data.</param>
/// <param name="mayAddCustomStatus">Will be <c>true</c> if the status can be customised by callee.</param>
/// <param name="target">The instance of target building or node.</param>
///
/// <returns>Returns the localised resident status.</returns>
public string GetResidentLocalizedStatus(ushort instanceID,
ref CitizenInstance data,
out bool mayAddCustomStatus,
out InstanceID target) {
mayAddCustomStatus = true;
target = InstanceID.Empty;
ushort targetBuildingId = data.m_targetBuilding;
if (IsSweaptAway(ref data) || targetBuildingId == 0) {
mayAddCustomStatus = false;
return Locale.Get("CITIZEN_STATUS_CONFUSED");
}
uint citizenId = data.m_citizen;
var isStudent = false;
ushort homeId = 0;
ushort workId = 0;
ushort vehicleId = 0;
// if true, it means the targetBuildingId is actually a node, not a building
bool targetIsNode = (data.m_flags & CitizenInstance.Flags.TargetIsNode) != 0;
if (citizenId != 0u) {
ref Citizen citizen = ref CitizenManager.instance.m_citizens.m_buffer[citizenId];
homeId = citizen.m_homeBuilding;
workId = citizen.m_workBuilding;
vehicleId = citizen.m_vehicle;
isStudent = (citizen.m_flags & Citizen.Flags.Student) != 0;
}
if (vehicleId != 0) {
ref Vehicle vehicle = ref vehicleId.ToVehicle();
VehicleInfo info = vehicle.Info;
switch (info.m_class.m_service) {
case ItemClass.Service.Residential
when info.m_vehicleType != VehicleInfo.VehicleType.Bicycle &&
IsVehicleOwnedByCitizen(ref vehicle, citizenId):
if (targetIsNode) {
target.NetNode = targetBuildingId;
return Locale.Get("CITIZEN_STATUS_DRIVINGTO");
}
if (IsOutsideConnection(targetBuildingId)) {
return Locale.Get("CITIZEN_STATUS_DRIVINGTO_OUTSIDE");
}
if (targetBuildingId == homeId) {
return Locale.Get("CITIZEN_STATUS_DRIVINGTO_HOME");
}
if (targetBuildingId == workId) {
return isStudent
? Locale.Get("CITIZEN_STATUS_DRIVINGTO_SCHOOL")
: Locale.Get("CITIZEN_STATUS_DRIVINGTO_WORK");
}
target.Building = targetBuildingId;
return Locale.Get("CITIZEN_STATUS_DRIVINGTO");
case ItemClass.Service.PublicTransport:
case ItemClass.Service.Disaster:
if (targetIsNode) {
if ((data.m_flags & CitizenInstance.Flags.WaitingTaxi) != 0) {
return Locale.Get("CITIZEN_STATUS_WAITING_TAXI");
}
ushort transportLine = targetBuildingId.ToNode().m_transportLine;
if (vehicle.m_transportLine != transportLine) {
target.NetNode = targetBuildingId;
return Locale.Get("CITIZEN_STATUS_TRAVELLINGTO");
}
break;
}
if ((data.m_flags & CitizenInstance.Flags.WaitingTaxi) != CitizenInstance.Flags.None) {
return Locale.Get("CITIZEN_STATUS_WAITING_TAXI");
}
if (IsOutsideConnection(targetBuildingId)) {
return Locale.Get("CITIZEN_STATUS_TRAVELLINGTO_OUTSIDE");
}
if (targetBuildingId == homeId) {
return Locale.Get("CITIZEN_STATUS_TRAVELLINGTO_HOME");
}
if (targetBuildingId == workId) {
return isStudent
? Locale.Get("CITIZEN_STATUS_TRAVELLINGTO_SCHOOL")
: Locale.Get("CITIZEN_STATUS_TRAVELLINGTO_WORK");
}
target.Building = targetBuildingId;
return Locale.Get("CITIZEN_STATUS_TRAVELLINGTO");
}
}
if (targetIsNode) {
target.NetNode = targetBuildingId;
if ((data.m_flags & CitizenInstance.Flags.OnTour) != 0) {
return Locale.Get("CITIZEN_STATUS_VISITING");
}
return Locale.Get("CITIZEN_STATUS_GOINGTO");
}
if (IsOutsideConnection(targetBuildingId)) {
return Locale.Get("CITIZEN_STATUS_GOINGTO_OUTSIDE");
}
if (targetBuildingId == homeId) {
if (IsHangingAround(ref data)) {
mayAddCustomStatus = false;
return Locale.Get("CITIZEN_STATUS_AT_HOME");
}
return Locale.Get("CITIZEN_STATUS_GOINGTO_HOME");
}
if (targetBuildingId == workId) {
if (IsHangingAround(ref data)) {
mayAddCustomStatus = false;
return isStudent
? Locale.Get("CITIZEN_STATUS_AT_SCHOOL")
: Locale.Get("CITIZEN_STATUS_AT_WORK");
}
return isStudent
? Locale.Get("CITIZEN_STATUS_GOINGTO_SCHOOL")
: Locale.Get("CITIZEN_STATUS_GOINGTO_WORK");
}
target.Building = targetBuildingId;
if (IsHangingAround(ref data)) {
mayAddCustomStatus = false;
return Locale.Get("CITIZEN_STATUS_VISITING");
}
return Locale.Get("CITIZEN_STATUS_GOINGTO");
}
public bool StartPathFind(
ushort instanceID,
ref CitizenInstance instanceData,
ref ExtCitizenInstance extInstance,
ref ExtCitizen extCitizen,
Vector3 startPos,
Vector3 endPos,
VehicleInfo vehicleInfo,
bool enableTransport,
bool ignoreCost) {
try {
return InternalStartPathFind(
instanceID,
ref instanceData,
ref extInstance,
ref extCitizen,
startPos,
endPos,
vehicleInfo,
enableTransport,
ignoreCost);
}
catch (Exception ex) {
// make sure we have copy of exception in TMPE.log
Log.Info(ex.ToString());
// run the code again, this time with full debug logging
try {
InternalStartPathFind(
instanceID,
ref instanceData,
ref extInstance,
ref extCitizen,
startPos,
endPos,
vehicleInfo,
enableTransport,
ignoreCost,
true);
}
catch {
// ignore
}
throw;
}
}
internal bool InternalStartPathFind(
ushort instanceID,
ref CitizenInstance instanceData,
ref ExtCitizenInstance extInstance,
ref ExtCitizen extCitizen,
Vector3 startPos,
Vector3 endPos,
VehicleInfo vehicleInfo,
bool enableTransport,
bool ignoreCost,
bool adhocDebugLog = false) {
#if DEBUG
bool citizenDebug
= (DebugSettings.CitizenInstanceId == 0
|| DebugSettings.CitizenInstanceId == instanceID)
&& (DebugSettings.CitizenId == 0
|| DebugSettings.CitizenId == instanceData.m_citizen)
&& (DebugSettings.SourceBuildingId == 0
|| DebugSettings.SourceBuildingId == instanceData.m_sourceBuilding)
&& (DebugSettings.TargetBuildingId == 0
|| DebugSettings.TargetBuildingId == instanceData.m_targetBuilding);
bool logParkingAi = DebugSwitch.BasicParkingAILog.Get() && citizenDebug;
bool extendedLogParkingAi = DebugSwitch.ExtendedParkingAILog.Get() && citizenDebug;
#else
bool logParkingAi = adhocDebugLog;
bool extendedLogParkingAi = adhocDebugLog;
#endif
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): called for citizen " +
$"{instanceData.m_citizen}, startPos={startPos}, endPos={endPos}, " +
$"sourceBuilding={instanceData.m_sourceBuilding}, " +
$"targetBuilding={instanceData.m_targetBuilding}, " +
$"pathMode={extInstance.pathMode}, enableTransport={enableTransport}, " +
$"ignoreCost={ignoreCost}");
}
// NON-STOCK CODE START
CitizenManager citizenManager = CitizenManager.instance;
ref Citizen citizen = ref citizenManager.m_citizens.m_buffer[instanceData.m_citizen];
ushort parkedVehicleId = citizen.m_parkedVehicle;
ushort homeId = citizen.m_homeBuilding;
CarUsagePolicy carUsageMode = CarUsagePolicy.Allowed;
var startsAtOutsideConnection = false;
ParkingAI parkingAiConf = GlobalConfig.Instance.ParkingAI;
if (SavedGameOptions.Instance.parkingAI) {
switch (extInstance.pathMode) {
case ExtPathMode.RequiresWalkingPathToParkedCar:
case ExtPathMode.CalculatingWalkingPathToParkedCar:
case ExtPathMode.WalkingToParkedCar:
case ExtPathMode.ApproachingParkedCar: {
if (parkedVehicleId == 0) {
// Parked vehicle not present but citizen wants to reach it
// -> Reset path mode
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen " +
$"has CurrentPathMode={extInstance.pathMode} but no parked " +
"vehicle present. Change to 'None'.");
}
Reset(ref extInstance);
} else {
// Parked vehicle is present and citizen wants to reach it
// -> Prohibit car usage
if (extendedLogParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen " +
$"has CurrentPathMode={extInstance.pathMode}. Change to " +
"'CalculatingWalkingPathToParkedCar'.");
}
extInstance.pathMode =
ExtPathMode.CalculatingWalkingPathToParkedCar;
carUsageMode = CarUsagePolicy.Forbidden;
}
break;
}
case ExtPathMode.RequiresWalkingPathToTarget:
case ExtPathMode.CalculatingWalkingPathToTarget:
case ExtPathMode.WalkingToTarget: {
// Citizen walks to target
// -> Reset path mode
if (extendedLogParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen " +
$"has CurrentPathMode={extInstance.pathMode}. Change to " +
"'CalculatingWalkingPathToTarget'.");
}
extInstance.pathMode = ExtPathMode.CalculatingWalkingPathToTarget;
carUsageMode = CarUsagePolicy.Forbidden;
break;
}
case ExtPathMode.RequiresCarPath:
case ExtPathMode.RequiresMixedCarPathToTarget:
case ExtPathMode.DrivingToTarget:
case ExtPathMode.DrivingToKnownParkPos:
case ExtPathMode.DrivingToAltParkPos:
case ExtPathMode.CalculatingCarPathToAltParkPos:
case ExtPathMode.CalculatingCarPathToKnownParkPos:
case ExtPathMode.CalculatingCarPathToTarget: {
if (parkedVehicleId == 0) {
// Citizen wants to drive to target but parked vehicle is not present
// -> Reset path mode
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): " +
$"Citizen has CurrentPathMode={extInstance.pathMode} but " +
"no parked vehicle present. Change to 'None'.");
}
Reset(ref extInstance);
} else {
// Citizen wants to drive to target and parked vehicle is present
// -> Force parked car usage
if (extendedLogParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): " +
$"Citizen has CurrentPathMode={extInstance.pathMode}. " +
"Change to 'RequiresCarPath'.");
}
extInstance.pathMode = ExtPathMode.RequiresCarPath;
carUsageMode = CarUsagePolicy.ForcedParked;
startPos = parkedVehicleId.ToParkedVehicle().m_position; // force to start from the parked car
}
break;
}
default: {
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen has " +
$"CurrentPathMode={extInstance.pathMode}. Change to 'None'.");
}
Reset(ref extInstance);
break;
}
}
startsAtOutsideConnection =
Constants.ManagerFactory.ExtCitizenInstanceManager.IsAtOutsideConnection(
instanceID,
ref instanceData,
ref extInstance,
startPos);
if (extInstance.pathMode == ExtPathMode.None) {
bool isOnTour = (instanceData.m_flags & CitizenInstance.Flags.OnTour) !=
CitizenInstance.Flags.None;
if (isOnTour || ignoreCost) {
// Citizen is on a walking tour or is a mascot
// -> Prohibit car usage
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen ignores " +
$"cost ({ignoreCost}) or is on a walking tour ({isOnTour}): Setting " +
"path mode to 'CalculatingWalkingPathToTarget'");
}
carUsageMode = CarUsagePolicy.Forbidden;
extInstance.pathMode = ExtPathMode.CalculatingWalkingPathToTarget;
} else {
// Citizen is not on a walking tour and is not a mascot
// -> Check if citizen is located at an outside connection and make them
// obey Parking AI restrictions
if (instanceData.m_sourceBuilding != 0) {
ItemClass.Service sourceBuildingService = instanceData
.m_sourceBuilding
.ToBuilding()
.Info
.m_class
.m_service;
if (startsAtOutsideConnection) {
if (sourceBuildingService == ItemClass.Service.Road) {
if (vehicleInfo != null) {
// Citizen is located at a road outside connection and can spawn a car
// -> Force car usage
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): " +
"Citizen is located at a road outside connection: " +
"Setting path mode to 'RequiresCarPath' and carUsageMode " +
$"to 'ForcedPocket', pos: {instanceData.GetLastFramePosition()}");
}
extInstance.pathMode = ExtPathMode.RequiresCarPath;
carUsageMode = CarUsagePolicy.ForcedPocket;
} else {
// Citizen is located at a non-road outside connection and
// cannot spawn a car
// -> Path-finding fails
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): " +
"Citizen is located at a road outside connection but " +
"does not have a car template: ABORTING PATH-FINDING");
}
Reset(ref extInstance);
return false;
}
} else {
// Citizen is located at a non-road outside connection
// -> Prohibit car usage
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): " +
"Citizen is located at a non-road outside connection: " +
$"Setting path mode to 'CalculatingWalkingPathToTarget', pos: {instanceData.GetLastFramePosition()}");
}
extInstance.pathMode =
ExtPathMode.CalculatingWalkingPathToTarget;
carUsageMode = CarUsagePolicy.Forbidden;
}
} // starts at outside connection
} // sourceBuilding != 0
} // not isOnTour && not ignoreCost
} // if (extInstance.pathMode == ExtPathMode.None)
if ((carUsageMode == CarUsagePolicy.Allowed ||
carUsageMode == CarUsagePolicy.ForcedParked) && parkedVehicleId != 0) {
// Reuse parked vehicle info
ref VehicleParked parkedVehicle = ref parkedVehicleId.ToParkedVehicle();
vehicleInfo = parkedVehicle.Info;
if (homeId != 0 && vehicleInfo &&
!citizen.m_flags.IsFlagSet(Citizen.Flags.Tourist) &&
ExtVehicleManager.MustSwapParkedCarWithElectric(vehicleInfo, homeId)) {
if (logParkingAi) {
Log.Info($"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen {instanceData.m_citizen}. Swapping currently parked vehicle ({parkedVehicleId}) with electric");
}
if (AdvancedParkingManager.SwapParkedVehicleWithElectric(
logParkingAi: logParkingAi,
citizenId: instanceData.m_citizen,
citizen: ref citizen,
position: parkedVehicle.m_position,
rotation: parkedVehicle.m_rotation,
electricVehicleInfo: out VehicleInfo electricVehicleInfo)) {
vehicleInfo = electricVehicleInfo;
parkedVehicleId = citizen.m_parkedVehicle;
parkedVehicle = ref parkedVehicleId.ToParkedVehicle();
}
}
// Check if the citizen should return their car back home
if (extInstance.pathMode == ExtPathMode.None && // initiating a new path
homeId != 0 && // home building present
instanceData.m_targetBuilding == homeId // current target is home
) {
// citizen travels back home
// -> check if their car should be returned
if ((extCitizen.lastTransportMode & ExtTransportMode.Car) !=
ExtTransportMode.None) {
// citizen travelled by car
// -> return car back home
extInstance.pathMode =
ExtPathMode.CalculatingWalkingPathToParkedCar;
carUsageMode = CarUsagePolicy.Forbidden;
if (extendedLogParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen used " +
"their car before and is not at home. Forcing to walk to parked car.");
}
} else {
// citizen traveled by other means of transport
// -> check distance between home and parked car. if too far away:
// force to take the car back home
float distHomeToParked = (parkedVehicle.m_position - homeId.ToBuilding().m_position).magnitude;
if (distHomeToParked > parkingAiConf.MaxParkedCarDistanceToHome) {
// force to take car back home
extInstance.pathMode =
ExtPathMode.CalculatingWalkingPathToParkedCar;
carUsageMode = CarUsagePolicy.Forbidden;
if (extendedLogParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen " +
"wants to go home and parked car is too far away " +
$"({distHomeToParked}). Forcing walking to parked car.");
}
}
}
}
}
//--------------------------------------------------------------
// The following holds:
// - pathMode is now either CalculatingWalkingPathToParkedCar,
// CalculatingWalkingPathToTarget, RequiresCarPath or None.
// - if pathMode is CalculatingWalkingPathToParkedCar or RequiresCarPath: parked
// car is present and citizen is not on a walking tour
// - carUsageMode is valid
// - if pathMode is RequiresCarPath: carUsageMode is either ForcedParked or
// ForcedPocket
//--------------------------------------------------------------
// modify path-finding constraints (vehicleInfo, endPos) if citizen is forced to walk
if (extInstance.pathMode == ExtPathMode.CalculatingWalkingPathToParkedCar ||
extInstance.pathMode == ExtPathMode.CalculatingWalkingPathToTarget) {
// vehicle must not be used since we need a walking path to either
// 1. a parked car or
// 2. the target building
if (extInstance.pathMode == ExtPathMode.CalculatingWalkingPathToParkedCar) {
// walk to parked car
// -> end position is parked car
endPos = parkedVehicleId.ToParkedVehicle().m_position;
if (extendedLogParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen shall " +
$"go to parked vehicle @ {endPos}");
}
}
}
} // if SavedGameOptions.Instance.ParkingAi
if (extendedLogParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen is allowed to " +
$"drive their car? {carUsageMode}");
}
// NON-STOCK CODE END
//------------------------------------------------------------------
// semi-stock code: determine path-finding parameters (laneTypes, vehicleTypes,
// extVehicleType, etc.)
//------------------------------------------------------------------
NetInfo.LaneType laneTypes = NetInfo.LaneType.Pedestrian;
VehicleInfo.VehicleType vehicleTypes = VehicleInfo.VehicleType.None;
VehicleInfo.VehicleCategory vehicleCategory = VehicleInfo.VehicleCategory.None;
bool randomParking = false;
bool combustionEngine = false;
ExtVehicleType extVehicleType = ExtVehicleType.None;
if (vehicleInfo != null)
{
if (vehicleInfo.m_class.m_subService == ItemClass.SubService.PublicTransportTaxi)
{
if ((instanceData.m_flags & CitizenInstance.Flags.CannotUseTaxi) ==
CitizenInstance.Flags.None
&& Singleton<DistrictManager>
.instance.m_districts.m_buffer[0].m_productionData.m_finalTaxiCapacity != 0u)
{
SimulationManager instance = Singleton<SimulationManager>.instance;
if (instance.m_isNightTime || instance.m_randomizer.Int32(2u) == 0) {
laneTypes |= NetInfo.LaneType.Vehicle |
NetInfo.LaneType.TransportVehicle;
vehicleTypes |= vehicleInfo.m_vehicleType;
vehicleCategory |= vehicleInfo.vehicleCategory;
extVehicleType = ExtVehicleType.Taxi; // NON-STOCK CODE
// NON-STOCK CODE START
if (SavedGameOptions.Instance.parkingAI) {
extInstance.pathMode = ExtPathMode.TaxiToTarget;
}
// NON-STOCK CODE END
}
}
} else {
switch (vehicleInfo.m_vehicleType) {
// NON-STOCK CODE START
case VehicleInfo.VehicleType.Car: {
if (carUsageMode != CarUsagePolicy.Forbidden) {
extVehicleType = ExtVehicleType.PassengerCar;
laneTypes |= NetInfo.LaneType.Vehicle;
vehicleTypes |= vehicleInfo.m_vehicleType;
vehicleCategory |= vehicleInfo.vehicleCategory;
combustionEngine =
vehicleInfo.m_class.m_subService ==
ItemClass.SubService.ResidentialLow;
}
break;
}
case VehicleInfo.VehicleType.Bicycle:
extVehicleType = ExtVehicleType.Bicycle;
laneTypes |= NetInfo.LaneType.Vehicle;
vehicleTypes |= vehicleInfo.m_vehicleType;
vehicleCategory |= vehicleInfo.vehicleCategory;
break;
}
}
// NON-STOCK CODE END
}
// NON-STOCK CODE START
ExtPathType extPathType = ExtPathType.None;
PathUnit.Position endPosA = default;
bool calculateEndPos = true;
bool allowRandomParking = true;
ref Building targetBuilding = ref instanceData.m_targetBuilding.ToBuilding();
if (SavedGameOptions.Instance.parkingAI) {
// Parking AI
if (extInstance.pathMode == ExtPathMode.RequiresCarPath) {
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Setting " +
$"startPos={startPos} for citizen instance {instanceID}. " +
$"CurrentDepartureMode={extInstance.pathMode}");
}
if (instanceData.m_targetBuilding == 0 ||
(targetBuilding.m_flags & Building.Flags.IncomingOutgoing) == Building.Flags.None) {
// the citizen is starting their journey and the target is not an outside
// connection
// -> find a suitable parking space near the target
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Finding parking " +
$"space at target for citizen instance {instanceID}. " +
$"CurrentDepartureMode={extInstance.pathMode} parkedVehicleId={parkedVehicleId}");
}
// find a parking space in the vicinity of the target
if (AdvancedParkingManager.Instance.FindParkingSpaceForCitizen(
endPos,
vehicleInfo,
ref instanceData,
ref extInstance,
homeId,
instanceData.m_targetBuilding == homeId,
0,
false,
out Vector3 parkPos,
ref endPosA,
out bool calcEndPos)
&& CalculateReturnPath(
ref extInstance,
parkPos,
endPos)) {
// success
extInstance.pathMode = ExtPathMode.CalculatingCarPathToKnownParkPos;
// if true, the end path position still needs to be calculated
calculateEndPos = calcEndPos;
// find a direct path to the calculated parking position
allowRandomParking = false;
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Finding " +
$"known parking space for citizen instance {instanceID}, parked " +
$"vehicle {parkedVehicleId} succeeded and return path " +
$"{extInstance.returnPathId} ({extInstance.returnPathState}) " +
$"is calculating. PathMode={extInstance.pathMode}");
}
// if (!extInstance.CalculateReturnPath(parkPos, endPos)) {
// // TODO retry?
// if (debug)
// Log._Debug(
// $"CustomCitizenAI.CustomStartPathFind: [PFFAIL] Could not
// calculate return path for citizen instance {instanceID}, parked vehicle
// {parkedVehicleId}. Calling OnPathFindFailed.");
// CustomHumanAI.OnPathFindFailure(extInstance);
// return false;
// }
}
}
if (extInstance.pathMode == ExtPathMode.RequiresCarPath) {
// no known parking space found (pathMode has not been updated in the block above)
// -> calculate direct path to target
if (logParkingAi) {
Log.Info(
$"CustomCitizenAI.ExtStartPathFind({instanceID}): Citizen instance " +
$"{instanceID} is still at CurrentPathMode={extInstance.pathMode} " +
"(no parking space found?). Setting it to CalculatingCarPath. " +
$"parkedVehicleId={parkedVehicleId}");
}
extInstance.pathMode = ExtPathMode.CalculatingCarPathToTarget;
}
}
// determine path type from path mode
extPathType = extInstance.GetPathType();
extInstance.atOutsideConnection = startsAtOutsideConnection;
//------------------------------------------
// the following holds:
// - pathMode is now either CalculatingWalkingPathToParkedCar,
// CalculatingWalkingPathToTarget, CalculatingCarPathToTarget,
// CalculatingCarPathToKnownParkPos or None.
//------------------------------------------
} // end if SavedGameOptions.Instance.ParkingAi
/*
* enable random parking if exact parking space was not calculated yet
*/
if (extVehicleType == ExtVehicleType.PassengerCar ||
extVehicleType == ExtVehicleType.Bicycle) {
if (allowRandomParking
&& instanceData.m_targetBuilding != 0
&& targetBuilding.Info
&& (
targetBuilding.Info.m_class.m_service > ItemClass.Service.Office
|| (instanceData.m_flags & CitizenInstance.Flags.TargetIsNode) != 0
)
)
{
randomParking = true;
}
}
// NON-STOCK CODE END
//---------------------------------------------------
// determine the path position of the parked vehicle
//---------------------------------------------------
PathUnit.Position parkedVehiclePathPos = default;
if (parkedVehicleId != 0 && extVehicleType == ExtVehicleType.PassengerCar) {
Vector3 position = parkedVehicleId.ToParkedVehicle().m_position;
Constants.ManagerFactory.ExtPathManager.FindPathPositionWithSpiralLoop(
position,
ItemClass.Service.Road,
NetInfo.LaneType.Vehicle | NetInfo.LaneType.TransportVehicle,
VehicleInfo.VehicleType.Car,
VehicleInfo.VehicleCategory.PassengerCar,
NetInfo.LaneType.Pedestrian,
VehicleInfo.VehicleType.None,
false,
false,
parkingAiConf.MaxBuildingToPedestrianLaneDistance,
false,
false,
out parkedVehiclePathPos);
}
bool allowUnderground =
(instanceData.m_flags &
(CitizenInstance.Flags.Underground | CitizenInstance.Flags.Transition)) !=
CitizenInstance.Flags.None;
if (logParkingAi) {
Log.Info($"CustomCitizenAI.ExtStartPathFind({instanceID}): Requesting path-finding " +
$"for citizen instance {instanceID}, citizen {instanceData.m_citizen}, " +
$"extVehicleType={extVehicleType}, extPathType={extPathType}, startPos={startPos}, " +
$"endPos={endPos}, sourceBuilding={instanceData.m_sourceBuilding}, " +
$"targetBuilding={instanceData.m_targetBuilding} pathMode={extInstance.pathMode}");
}
//-------------------------------------
// determine start & end path positions
//
// NON-STOCK CODE: with Parking AI enabled, the end position must be a pedestrian position
bool foundEndPos = !calculateEndPos || FindPathPosition(
instanceID,
ref instanceData,
endPos,
SavedGameOptions.Instance.parkingAI &&
(instanceData.m_targetBuilding == 0 ||
(targetBuilding.m_flags &
Building.Flags.IncomingOutgoing) == Building.Flags.None)
? NetInfo.LaneType.Pedestrian
: laneTypes,
vehicleTypes,
false,
out endPosA);
bool foundStartPos = false;
PathUnit.Position startPosA;
if (SavedGameOptions.Instance.parkingAI &&
(extInstance.pathMode == ExtPathMode.CalculatingCarPathToTarget ||
extInstance.pathMode == ExtPathMode.CalculatingCarPathToKnownParkPos)) {
// citizen will enter their car now
// -> find a road start position
foundStartPos = PathManager.FindPathPosition(
startPos,
ItemClass.Service.Road,
laneTypes & ~NetInfo.LaneType.Pedestrian,
vehicleTypes,
VehicleInfo.VehicleCategory.All,
allowUnderground,
false,
parkingAiConf.MaxBuildingToPedestrianLaneDistance,
false,
false,
out startPosA);
} else {
foundStartPos = FindPathPosition(
instanceID,
ref instanceData,
startPos,
laneTypes,
vehicleTypes,
allowUnderground,
out startPosA);
}
//---------------------
// start path-finding
//---------------------