public static AbstractActor SpawnAmbushVehicle(Team team, Lance ambushLance, Vector3 ambushOrigin, Vector3 spawnPos, Quaternion spawnRot) { // Randomly determine one of the spawnpairs from the current ambushdef List <VehicleAndPilotDef> shuffledSpawns = new List <VehicleAndPilotDef>(); shuffledSpawns.AddRange(ModState.VehicleAmbushDefForContract.SpawnPool); shuffledSpawns.Shuffle(); VehicleAndPilotDef ambushDef = shuffledSpawns[0]; PilotDef pilotDef = ModState.Combat.DataManager.PilotDefs.Get(ambushDef.PilotDefId); VehicleDef vehicleDef = ModState.Combat.DataManager.VehicleDefs.Get(ambushDef.VehicleDefId); vehicleDef.Refresh(); // Adjust position so we don't spawn in the ground. spawnPos.y = ModState.Combat.MapMetaData.GetLerpedHeightAt(spawnPos, true); // Rotate to face the ambush origin Vector3 spawnDirection = Vector3.RotateTowards(spawnRot.eulerAngles, ambushOrigin, 1f, 0f); Quaternion spawnRotation = Quaternion.LookRotation(spawnDirection); Vehicle vehicle = ActorFactory.CreateVehicle(vehicleDef, pilotDef, team.EncounterTags, ModState.Combat, team.GetNextSupportUnitGuid(), "", null); vehicle.Init(spawnPos, spawnRotation.eulerAngles.y, true); vehicle.InitGameRep(null); Mod.Log.Debug?.Write($"Spawned vehicle {CombatantUtils.Label(vehicle)} at position: {spawnPos}"); if (vehicle == null) { Mod.Log.Error?.Write($"Failed to spawn vehicleDefId: {ambushDef.VehicleDefId} / pilotDefId: {ambushDef.PilotDefId} !"); } Mod.Log.Debug?.Write($" Spawned ambush vehicle, adding to team: {team} and lance: {ambushLance}"); team.AddUnit(vehicle); vehicle.AddToTeam(team); vehicle.AddToLance(ambushLance); vehicle.BehaviorTree = BehaviorTreeFactory.MakeBehaviorTree(ModState.Combat.BattleTechGame, vehicle, BehaviorTreeIDEnum.CoreAITree); Mod.Log.Debug?.Write("Enabled vehicle behavior tree"); UnitSpawnedMessage message = new UnitSpawnedMessage("CJ_VEHICLE", vehicle.GUID); ModState.Combat.MessageCenter.PublishMessage(message); return(vehicle); }