public function createProperties(TES4VariableDeclarationList $variableList, TES5GlobalScope $globalScope)
 {
     /**
      * @var TES4VariableDeclaration[] $alreadyDefinedVariables
      */
     $alreadyDefinedVariables = [];
     foreach ($variableList->getVariableList() as $variable) {
         $theVariableName = strtolower($variable->getVariableName());
         if (isset($alreadyDefinedVariables[$theVariableName])) {
             if ($variable->getVariableType() == $alreadyDefinedVariables[$theVariableName]->getVariableType()) {
                 continue;
                 //Same variable defined twice, smack the original script developer and fallthrough silently.
             }
             throw new ConversionException("Double definition of variable named " . $variable->getVariableName() . " with different types ( " . $alreadyDefinedVariables[$theVariableName]->getVariableType()->value() . " and " . $variable->getVariableType()->value() . " )");
         }
         switch ($variable->getVariableType()) {
             case TES4Type::T_FLOAT():
                 $property = new TES5Property($variable->getVariableName(), TES5BasicType::T_FLOAT(), null);
                 break;
             case TES4Type::T_INT():
             case TES4Type::T_SHORT():
             case TES4Type::T_LONG():
                 $property = new TES5Property($variable->getVariableName(), TES5BasicType::T_INT(), null);
                 break;
             case TES4Type::T_REF():
                 $property = $this->createPropertyFromReference($variable);
                 break;
             default:
                 throw new ConversionException("Unknown variable declaration type.");
         }
         $globalScope->add($property);
         $alreadyDefinedVariables[$theVariableName] = $variable;
     }
 }
 public function getType()
 {
     if ($this->leftValue->getType() == TES5BasicType::T_FLOAT() || $this->rightValue->getType() == TES5BasicType::T_FLOAT()) {
         return TES5BasicType::T_FLOAT();
     }
     return TES5BasicType::T_INT();
 }
 public function __construct(TES5ObjectPropertyFactory $objectPropertyFactory)
 {
     $this->objectPropertyFactory = $objectPropertyFactory;
     //Those are used to hook in the internal Skyblivion systems.
     $special_conversions = ['TES4AttrStrength' => TES5BasicType::T_GLOBALVARIABLE(), 'TES4AttrIntelligence' => TES5BasicType::T_GLOBALVARIABLE(), 'TES4AttrWillpower' => TES5BasicType::T_GLOBALVARIABLE(), 'TES4AttrAgility' => TES5BasicType::T_GLOBALVARIABLE(), 'TES4AttrSpeed' => TES5BasicType::T_GLOBALVARIABLE(), 'TES4AttrEndurance' => TES5BasicType::T_GLOBALVARIABLE(), 'TES4AttrPersonality' => TES5BasicType::T_GLOBALVARIABLE(), 'TES4AttrLuck' => TES5BasicType::T_GLOBALVARIABLE(), 'tContainer' => TES5TypeFactory::memberByValue("TES4Container", TES5BasicType::T_QUEST()), 'tTimer' => TES5TypeFactory::memberByValue("TES4TimerHelper", TES5BasicType::T_QUEST()), 'tGSPLocalTimer' => TES5BasicType::T_FLOAT(), 'TES4CyrodiilCrimeFaction' => TES5BasicType::T_FACTION(), self::MESSAGEBOX_VARIABLE_CONST => TES5BasicType::T_INT()];
     $this->special_conversions = $special_conversions;
 }
 private function inferenceTypeOfMethodArguments(TES5ObjectCall $objectCall, TES5MultipleScriptsScope $multipleScriptsScope)
 {
     /**
      * Inference the arguments
      */
     $arguments = $objectCall->getArguments();
     $argumentNumber = 0;
     $calledOnType = $objectCall->getAccessedObject()->getType()->getNativeType();
     foreach ($arguments->getArguments() as $argument) {
         /**
          * Get the argument type according to TES5Inheritance graph.
          */
         $argumentTargetType = TES5InheritanceGraphAnalyzer::findTypeByMethodParameter($calledOnType, $objectCall->getFunctionName(), $argumentNumber);
         if ($argument->getType() == $argumentTargetType) {
             ++$argumentNumber;
             continue;
             //Same type matched. We do not need to do anything :)
         }
         /**
          * todo - maybe we should move getReferencesTo() to TES5Value and make all of the rest TES5Values just have null references as they do not reference anything? :)
          */
         if (TES5InheritanceGraphAnalyzer::isExtending($argumentTargetType, $argument->getType()->getNativeType()) && $argument instanceof TES5Referencer) {
             //HACKY!
             $this->inferenceType($argument->getReferencesTo(), $argumentTargetType, $multipleScriptsScope);
         } else {
             //So there's one , one special case where we actually have to cast a var from one to another even though they are not ,,inheriting" from themselves, because they are primitives.
             //Scenario: there's an T_INT argument, and we feed it with a T_FLOAT variable reference. It won't work :(
             //We need to cast it on call level ( NOT inference it ) to make it work and not break other possible scenarios ( more specifically, when a float would be inferenced to int and there's a
             //float assigment somewhere in the code )
             if ($argumentTargetType == TES5BasicType::T_INT() && $argument->getType() == TES5BasicType::T_FLOAT()) {
                 if ($argument instanceof TES5Reference) {
                     //HACKY! When we'll clean up this interface, it will dissapear :)
                     $argument->setManualCastTo(TES5BasicType::T_INT());
                 }
             }
         }
         ++$argumentNumber;
     }
 }
 public function createCodeChunk(TES4VariableDeclarationList $chunk, TES5CodeScope $codeScope)
 {
     foreach ($chunk->getVariableList() as $variable) {
         switch ($variable->getVariableType()) {
             case TES4Type::T_FLOAT():
                 $property = new TES5LocalVariable($variable->getVariableName(), TES5BasicType::T_FLOAT());
                 break;
             case TES4Type::T_INT():
             case TES4Type::T_SHORT():
             case TES4Type::T_LONG():
                 $property = new TES5LocalVariable($variable->getVariableName(), TES5BasicType::T_INT());
                 break;
             case TES4Type::T_REF():
                 //most basic one, if something from inherited class is used, we will set to the inheriting class
                 $property = new TES5LocalVariable($variable->getVariableName(), TES5BasicType::T_FORM());
                 break;
             default:
                 throw new ConversionException("Unknown local variable declaration type.");
         }
         $codeScope->getLocalScope()->addVariable($property);
     }
     return null;
 }
 public function getType()
 {
     return TES5BasicType::T_INT();
 }
 private function convertArithmeticExpression(TES4Expression $expression, TES5CodeScope $codeScope, TES5GlobalScope $globalScope, TES5MultipleScriptsScope $multipleScriptsScope)
 {
     $sets = [[$expression->getLeftValue(), $expression->getRightValue()], [$expression->getRightValue(), $expression->getLeftValue()]];
     /**
      * Scenario 1 - Special functions converted on expression level
      * @var TES4Value[] $set
      */
     foreach ($sets as $set) {
         if (!$set[0] instanceof TES4Callable) {
             continue;
         }
         $function = $set[0]->getFunction();
         switch (strtolower($function->getFunctionCall()->getFunctionName())) {
             case "getweaponanimtype":
                 $calledOn = $this->createCalledOnReferenceOfCalledFunction($set[0], $codeScope, $globalScope, $multipleScriptsScope);
                 $leftValue = $this->createObjectCall($this->createObjectCall($calledOn, "GetEquippedWeapon", $multipleScriptsScope), "GetWeaponType", $multipleScriptsScope);
                 switch ((int) $set[1]->getData()) {
                     case 0:
                         $targetedWeaponTypes = [0];
                         break;
                     case 1:
                         $targetedWeaponTypes = [1, 2, 3, 4];
                         break;
                     case 2:
                         $targetedWeaponTypes = [5, 6, 8];
                         break;
                     case 3:
                         $targetedWeaponTypes = [7, 9];
                         break;
                     default:
                         throw new ConversionException("GetWeaponAnimType() - Unknown weapon type in expression");
                 }
                 $expressions = [];
                 foreach ($targetedWeaponTypes as $targetedWeaponType) {
                     $expressions[] = $this->expressionFactory->createArithmeticExpression($leftValue, TES5ArithmeticExpressionOperator::OPERATOR_EQUAL(), new TES5Integer($targetedWeaponType));
                 }
                 $resultExpression = $expressions[0];
                 unset($expressions[0]);
                 while (!empty($expressions)) {
                     $resultExpression = $this->expressionFactory->createLogicalExpression($resultExpression, TES5LogicalExpressionOperator::OPERATOR_OR(), array_pop($expressions));
                 }
                 return $resultExpression;
             case "getdetected":
                 $inversedArgument = new TES5ObjectCallArguments();
                 $inversedArgument->add($this->createCalledOnReferenceOfCalledFunction($set[0], $codeScope, $globalScope, $multipleScriptsScope));
                 $leftValue = $this->createObjectCall($this->createReadReference($function->getArguments()->getValue(0)->getData(), $globalScope, $multipleScriptsScope, $codeScope->getLocalScope()), "isDetectedBy", $multipleScriptsScope, $inversedArgument);
                 $rightValue = new TES5Integer((int) $set[1]->getData() == 0 ? 0 : 1);
                 $expression = $this->expressionFactory->createArithmeticExpression($leftValue, TES5ArithmeticExpressionOperator::OPERATOR_EQUAL(), $rightValue);
                 return $expression;
             case "getdetectionlevel":
                 if (!$set[1]->hasFixedValue()) {
                     throw new ConversionException("Cannot convert getDetectionLevel calls with dynamic comparision");
                 }
                 $boolean = new TES5Bool($set[1]->getData() == 3);
                 //true only if the compared value was 3
                 $inversedArgument = new TES5ObjectCallArguments();
                 $inversedArgument->add($this->createCalledOnReferenceOfCalledFunction($set[0], $codeScope, $globalScope, $multipleScriptsScope));
                 $expression = $this->expressionFactory->createArithmeticExpression($this->createObjectCall($this->createReadReference($function->getArguments()->getValue(0)->getData(), $globalScope, $multipleScriptsScope, $codeScope->getLocalScope()), "isDetectedBy", $multipleScriptsScope, $inversedArgument), TES5ArithmeticExpressionOperator::OPERATOR_EQUAL(), $boolean);
                 return $expression;
             case "getcurrentaiprocedure":
                 if (!$set[1]->hasFixedValue()) {
                     throw new ConversionException("Cannot convert getCurrentAIProcedure() calls with dynamic comparision");
                 }
                 switch ((int) $set[1]->getData()) {
                     case 4:
                         //ref.getSleepState() == 3
                         $expression = $this->expressionFactory->createArithmeticExpression($this->createObjectCall($this->createCalledOnReferenceOfCalledFunction($set[0], $codeScope, $globalScope, $multipleScriptsScope), "IsInDialogueWithPlayer", $multipleScriptsScope), TES5ArithmeticExpressionOperator::OPERATOR_EQUAL(), new TES5Bool((bool) $expression->getOperator() == TES4ArithmeticExpressionOperator::OPERATOR_EQUAL()));
                         return $expression;
                     case 8:
                         //ref.getSleepState() == 3
                         $expression = $this->expressionFactory->createArithmeticExpression($this->createObjectCall($this->createCalledOnReferenceOfCalledFunction($set[0], $codeScope, $globalScope, $multipleScriptsScope), "getSleepState", $multipleScriptsScope), TES5ArithmeticExpressionOperator::OPERATOR_EQUAL(), new TES5Integer(3));
                         return $expression;
                     case 13:
                         //ref.getSleepState() == 3
                         $expression = $this->expressionFactory->createArithmeticExpression($this->createObjectCall($this->createCalledOnReferenceOfCalledFunction($set[0], $codeScope, $globalScope, $multipleScriptsScope), "IsInCombat", $multipleScriptsScope), TES5ArithmeticExpressionOperator::OPERATOR_EQUAL(), new TES5Bool((bool) $expression->getOperator() == TES4ArithmeticExpressionOperator::OPERATOR_EQUAL()));
                         return $expression;
                     case 0:
                     case 7:
                     case 15:
                     case 17:
                         //@INCONSISTENCE Wander.. idk how to check it tbh. We return always true. Think about better representation
                         return new TES5Bool((bool) $expression->getOperator() == TES4ArithmeticExpressionOperator::OPERATOR_EQUAL());
                     default:
                         throw new ConversionException("Cannot convert GetCurrentAiProcedure - unknown TES4 procedure number arg " . (int) $set[1]->getData());
                 }
                 break;
             case "isidleplaying":
             case "getknockedstate":
             case "gettalkedtopc":
                 return new TES5Bool(true);
                 //This is so unimportant that i think it's not worth to find a good alternative and waste time.
             case "getsitting":
                 //WARNING: Needs to implement Horse sittings, too.
                 //SEE: http://www.gameskyrim.com/papyrus-isridinghorse-function-t255012.html
                 switch ((int) $set[1]->getData()) {
                     case 0:
                         $goTo = 0;
                         break;
                     case 1:
                     case 2:
                     case 11:
                     case 12:
                         $goTo = 2;
                         break;
                     case 3:
                     case 13:
                         $goTo = 3;
                         break;
                     case 4:
                     case 14:
                         $goTo = 4;
                         break;
                     default:
                         throw new ConversionException("GetSitting - unknown state found");
                 }
                 //ref.getSleepState() == 3
                 $expression = $this->expressionFactory->createArithmeticExpression($this->createObjectCall($this->createCalledOnReferenceOfCalledFunction($set[0], $codeScope, $globalScope, $multipleScriptsScope), "GetSitState", $multipleScriptsScope), TES5ArithmeticExpressionOperator::memberByValue($expression->getOperator()->value()), new TES5Integer($goTo));
                 return $expression;
         }
     }
     $leftValue = $this->createValue($expression->getLeftValue(), $codeScope, $globalScope, $multipleScriptsScope);
     $rightValue = $this->createValue($expression->getRightValue(), $codeScope, $globalScope, $multipleScriptsScope);
     $tes5sets = [[$leftValue, $rightValue], [$rightValue, $leftValue]];
     $objectReferenceType = TES5BasicType::T_FORM();
     //used just to make sure.
     $operator = TES5ArithmeticExpressionOperator::memberByValueWithDefault($expression->getOperator()->value(), null);
     /**
      * @var TES5Value[] $tes5set
      * Scenario 2: Comparision of ObjectReferences to integers ( quick formid check )
      */
     foreach ($tes5sets as $tes5set) {
         if ($tes5set[0]->getType() == $objectReferenceType || TES5InheritanceGraphAnalyzer::isExtending($tes5set[0]->getType(), $objectReferenceType)) {
             if ($tes5set[1]->getType() == TES5BasicType::T_INT()) {
                 $tes5set[0] = $this->createObjectCall($tes5set[0], "GetFormID", $multipleScriptsScope);
                 return $this->expressionFactory->createArithmeticExpression($tes5set[0], $operator, $tes5set[1]);
             }
         } else {
             if ($tes5set[0]->getType() == TES5TypeFactory::void()) {
                 if ($tes5set[1] instanceof TES5Integer || $tes5set[1] instanceof TES5Float) {
                     if ($tes5set[1]->getValue() == 0) {
                         return $this->expressionFactory->createArithmeticExpression($tes5set[0], $operator, new TES5None());
                     }
                 }
             }
         }
     }
     return $this->expressionFactory->createArithmeticExpression($leftValue, $operator, $rightValue);
 }
 public function createFromBlockType($blockType, TES5GlobalScope $globalScope)
 {
     $localScope = new TES5LocalScope();
     switch ($blockType) {
         case 'OnUpdate':
             break;
         case 'OnActivate':
             $localScope->addVariable(new TES5LocalVariable("akActivateRef", TES5BasicType::T_OBJECTREFERENCE(), [TES5LocalVariableParameterMeaning::ACTIVATOR()]));
             break;
         case 'OnInit':
             break;
         case 'OnSell':
             $localScope->addVariable(new TES5LocalVariable("akSeller", TES5BasicType::T_ACTOR(), [TES5LocalVariableParameterMeaning::ACTIVATOR()]));
             break;
         case 'OnDeath':
             $localScope->addVariable(new TES5LocalVariable("akKiller", TES5BasicType::T_ACTOR(), [TES5LocalVariableParameterMeaning::ACTIVATOR()]));
             break;
         case 'OnLoad':
             break;
         case 'OnObjectEquipped':
             $localScope->addVariable(new TES5LocalVariable("akBaseObject", TES5BasicType::T_FORM(), [TES5LocalVariableParameterMeaning::CONTAINER()]));
             $localScope->addVariable(new TES5LocalVariable("akReference", TES5BasicType::T_OBJECTREFERENCE()));
             break;
         case 'OnTriggerEnter':
             $localScope->addVariable(new TES5LocalVariable("akActivateRef", TES5BasicType::T_OBJECTREFERENCE(), [TES5LocalVariableParameterMeaning::ACTIVATOR(), TES5LocalVariableParameterMeaning::CONTAINER()]));
             break;
         case 'OnEquipped':
             $localScope->addVariable(new TES5LocalVariable("akActor", TES5BasicType::T_ACTOR(), [TES5LocalVariableParameterMeaning::ACTIVATOR(), TES5LocalVariableParameterMeaning::CONTAINER()]));
             break;
         case 'OnUnequipped':
             $localScope->addVariable(new TES5LocalVariable("akActor", TES5BasicType::T_ACTOR(), [TES5LocalVariableParameterMeaning::ACTIVATOR()]));
             break;
         case 'OnContainerChanged':
             $localScope->addVariable(new TES5LocalVariable("akNewContainer", TES5BasicType::T_OBJECTREFERENCE()));
             $localScope->addVariable(new TES5LocalVariable("akOldContainer", TES5BasicType::T_OBJECTREFERENCE()));
             break;
         case 'OnTrigger':
             $localScope->addVariable(new TES5LocalVariable("akActivateRef", TES5BasicType::T_OBJECTREFERENCE(), [TES5LocalVariableParameterMeaning::ACTIVATOR()]));
             break;
         case 'OnHit':
             $localScope->addVariable(new TES5LocalVariable("akAggressor", TES5BasicType::T_OBJECTREFERENCE(), [TES5LocalVariableParameterMeaning::ACTIVATOR()]));
             $localScope->addVariable(new TES5LocalVariable("akSource", TES5BasicType::T_FORM()));
             $localScope->addVariable(new TES5LocalVariable("akProjectile", TES5BasicType::T_PROJECTILE()));
             $localScope->addVariable(new TES5LocalVariable("abPowerAttack", TES5BasicType::T_BOOL()));
             $localScope->addVariable(new TES5LocalVariable("abSneakAttack", TES5BasicType::T_BOOL()));
             $localScope->addVariable(new TES5LocalVariable("abBashAttack", TES5BasicType::T_BOOL()));
             $localScope->addVariable(new TES5LocalVariable("abHitBlocked", TES5BasicType::T_BOOL()));
             break;
         case 'OnCombatStateChanged':
             $localScope->addVariable(new TES5LocalVariable("akTarget", TES5BasicType::T_ACTOR(), [TES5LocalVariableParameterMeaning::ACTIVATOR()]));
             $localScope->addVariable(new TES5LocalVariable("aeCombatState", TES5BasicType::T_INT()));
             break;
         case 'OnPackageStart':
             $localScope->addVariable(new TES5LocalVariable("akNewPackage", TES5BasicType::T_PACKAGE()));
             break;
         case 'OnPackageDone':
             $localScope->addVariable(new TES5LocalVariable("akDonePackage", TES5BasicType::T_PACKAGE()));
             break;
         case 'OnPackageEnd':
             $localScope->addVariable(new TES5LocalVariable("akOldPackage", TES5BasicType::T_PACKAGE()));
             break;
         case 'OnPackageChange':
             $localScope->addVariable(new TES5LocalVariable("akOldPackage", TES5BasicType::T_PACKAGE()));
             break;
         case 'OnMagicEffectApply':
             $localScope->addVariable(new TES5LocalVariable("akCaster", TES5BasicType::T_OBJECTREFERENCE(), [TES5LocalVariableParameterMeaning::ACTIVATOR()]));
             $localScope->addVariable(new TES5LocalVariable("akMagicEffect", TES5BasicType::T_MAGICEFFECT()));
             break;
         case 'OnReset':
             break;
         case 'OnEffectStart':
             $localScope->addVariable(new TES5LocalVariable("akTarget", TES5BasicType::T_ACTOR()));
             $localScope->addVariable(new TES5LocalVariable("akCaster", TES5BasicType::T_ACTOR(), [TES5LocalVariableParameterMeaning::ACTIVATOR()]));
             break;
         case 'OnEffectFinish':
             $localScope->addVariable(new TES5LocalVariable("akTarget", TES5BasicType::T_ACTOR()));
             $localScope->addVariable(new TES5LocalVariable("akCaster", TES5BasicType::T_ACTOR(), [TES5LocalVariableParameterMeaning::ACTIVATOR()]));
             break;
         default:
             throw new ConversionException("Cannot find new block type local scope variables out of " . $blockType);
     }
     return $localScope;
 }