/*
         * Extracts implicit reference from calls.
         * Returns a reference from calls like:
         * Enable
         * Disable
         * Activate
         * GetInFaction whatsoever
         */
        public ITES5Referencer ExtractImplicitReference(TES5GlobalScope globalScope, TES5MultipleScriptsScope multipleScriptsScope, TES5LocalScope localScope)
        {
            ITES5Type type = globalScope.ScriptHeader.BasicScriptType;

            if (type == TES5BasicType.T_OBJECTREFERENCE || type == TES5BasicType.T_ACTOR)//Change:  WTM:  Added Actor here.
            {
                return(CreateReferenceToSelf(globalScope));
            }
            else if (type == TES5BasicType.T_ACTIVEMAGICEFFECT)
            {
                TES5SelfReference self = CreateReferenceToSelf(globalScope);
                return(this.objectCallFactory.CreateObjectCall(self, "GetTargetActor", multipleScriptsScope));
            }
            else if (type == TES5BasicType.T_QUEST)
            {
                //todo - this should not be done like this
                //we should actually not try to extract the implicit reference on the non-reference oblivion functions like "stopQuest"
                //think of this line as a hacky way to just get code forward.
                return(CreateReferenceToSelf(globalScope));
            }
            else if (type == TES5BasicType.T_TOPICINFO)
            {
                /*
                 * TIF Fragments
                 */
                return(this.CreateReadReference("akSpeakerRef", globalScope, multipleScriptsScope, localScope));
            }

            throw new ConversionException("Cannot extract implicit reference - unknown basic script type.");
        }
        public List <ITES5CodeChunk> GenerateObjectiveHandling(ITES5CodeBlock codeBlock, TES5GlobalScope globalScope, List <int> stageMap)
        {
            List <ITES5CodeChunk> result = new List <ITES5CodeChunk>();

            //WTM:  Change:
            if (!stageMap.Any())
            {
                return(result);
            }
            TES5LocalVariable       castedToQuest        = new TES5LocalVariable("__temp", TES5BasicType.T_QUEST);//WTM:  Note:  Why is this variable even necessary?
            TES5Reference           referenceToTemp      = TES5ReferenceFactory.CreateReferenceToVariableOrProperty(castedToQuest);
            TES5SelfReference       questSelfReference   = TES5ReferenceFactory.CreateReferenceToSelf(globalScope);
            TES5VariableAssignation tempQuestAssignation = TES5VariableAssignationFactory.CreateAssignation(referenceToTemp, questSelfReference);

            result.Add(tempQuestAssignation);
            TES5LocalScope localScope = codeBlock.CodeScope.LocalScope;

            localScope.AddVariable(castedToQuest);
            int i = 0;

            foreach (var stageTargetState in stageMap)
            {
                TES5Integer             targetIndex        = new TES5Integer(i);
                TES5ObjectCallArguments displayedArguments = new TES5ObjectCallArguments()
                {
                    targetIndex
                };
                TES5ObjectCall           isObjectiveDisplayed         = objectCallFactory.CreateObjectCall(referenceToTemp, "IsObjectiveDisplayed", displayedArguments, inference: false);
                int                      isObjectiveDisplayedArgument = stageTargetState != 0 ? 0 : 1;
                TES5ComparisonExpression expression = TES5ExpressionFactory.CreateComparisonExpression(isObjectiveDisplayed, TES5ComparisonExpressionOperator.OPERATOR_EQUAL, new TES5Integer(isObjectiveDisplayedArgument));
                TES5ObjectCallArguments  arguments  = new TES5ObjectCallArguments()
                {
                    targetIndex, new TES5Integer(1)
                };
                string         setObjectiveFunction   = stageTargetState != 0 ? "SetObjectiveDisplayed" : "SetObjectiveCompleted";
                TES5ObjectCall setObjectiveObjectCall = objectCallFactory.CreateObjectCall(referenceToTemp, setObjectiveFunction, arguments, inference: false);
                TES5Branch     branch = TES5BranchFactory.CreateSimpleBranch(expression, localScope);
                branch.MainBranch.CodeScope.AddChunk(setObjectiveObjectCall);
                result.Add(branch);
                ++i;
            }

            return(result);
        }
        /*
         * Extracts implicit reference from calls.
         * Returns a reference from calls like:
         * Enable
         * Disable
         * Activate
         * GetInFaction whatsoever
         */
        public ITES5Referencer ExtractImplicitReference(TES5GlobalScope globalScope, TES5MultipleScriptsScope multipleScriptsScope, TES5LocalScope localScope)
        {
            ITES5Type type = globalScope.ScriptHeader.ScriptType.NativeType;

            if (type == TES5BasicType.T_ACTIVEMAGICEFFECT)
            {
                TES5SelfReference self = CreateReferenceToSelf(globalScope);
                return(this.objectCallFactory.CreateObjectCall(self, "GetTargetActor"));
            }
            if (type == TES5BasicType.T_QUEST)
            {
                //todo - this should not be done like this
                //we should actually not try to extract the implicit reference on the non-reference oblivion functions like "stopQuest"
                //think of this line as a hacky way to just get code forward.
                return(CreateReferenceToSelf(globalScope));
            }
            if (type == TES5BasicType.T_TOPICINFO)
            {
                //TIF Fragments
                return(this.CreateReadReference("akSpeakerRef", globalScope, multipleScriptsScope, localScope));
            }
            //WTM:  Change:  I made this the new default result instead of the previous algorithm of exhaustively listing types and throwing an exception if no type matches.
            return(CreateReferenceToSelf(globalScope));
        }