Esempio n. 1
0
        protected virtual IEnumerable <IStatementSyntax> GetItemTableDropItemsStatements(Map map, IEnumerable <RandomItemSet> itemSets, bool chooseItemClass)
        {
            if (map is null)
            {
                throw new ArgumentNullException(nameof(map));
            }

            if (itemSets is null)
            {
                throw new ArgumentNullException(nameof(itemSets));
            }

            var statements = new List <IStatementSyntax>();

            statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(SyntaxFactory.ParseTypeName(TypeName.Widget), VariableName.TrigWidget, JassNullLiteralExpressionSyntax.Value));
            statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(SyntaxFactory.ParseTypeName(TypeName.Unit), VariableName.TrigUnit, JassNullLiteralExpressionSyntax.Value));
            statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(JassTypeSyntax.Integer, VariableName.ItemId, SyntaxFactory.LiteralExpression(0)));
            statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(JassTypeSyntax.Boolean, VariableName.CanDrop, SyntaxFactory.LiteralExpression(true)));
            statements.Add(JassEmptyStatementSyntax.Value);

            statements.Add(SyntaxFactory.SetStatement(
                               VariableName.TrigWidget,
                               SyntaxFactory.VariableReferenceExpression(VariableName.BJLastDyingWidget)));

            statements.Add(SyntaxFactory.IfStatement(
                               new JassParenthesizedExpressionSyntax(SyntaxFactory.BinaryEqualsExpression(SyntaxFactory.VariableReferenceExpression(VariableName.TrigWidget), JassNullLiteralExpressionSyntax.Value)),
                               SyntaxFactory.SetStatement(VariableName.TrigUnit, SyntaxFactory.InvocationExpression(NativeName.GetTriggerUnit))));

            statements.Add(JassEmptyStatementSyntax.Value);

            var canDropConditionExpression = SyntaxFactory.UnaryNotExpression(SyntaxFactory.InvocationExpression(NativeName.IsUnitHidden, SyntaxFactory.VariableReferenceExpression(VariableName.TrigUnit)));

            var ifBody = new List <IStatementSyntax>()
            {
                SyntaxFactory.SetStatement(VariableName.CanDrop, canDropConditionExpression),
            };

            ifBody.Add(SyntaxFactory.IfStatement(
                           new JassParenthesizedExpressionSyntax(SyntaxFactory.BinaryAndExpression(
                                                                     SyntaxFactory.VariableReferenceExpression(VariableName.CanDrop),
                                                                     SyntaxFactory.BinaryNotEqualsExpression(SyntaxFactory.InvocationExpression(NativeName.GetChangingUnit), JassNullLiteralExpressionSyntax.Value))),
                           SyntaxFactory.SetStatement(
                               VariableName.CanDrop,
                               new JassParenthesizedExpressionSyntax(SyntaxFactory.BinaryEqualsExpression(
                                                                         SyntaxFactory.InvocationExpression(NativeName.GetChangingUnitPrevOwner),
                                                                         SyntaxFactory.InvocationExpression(NativeName.Player, SyntaxFactory.VariableReferenceExpression(GlobalVariableName.PlayerNeutralHostile)))))));

            statements.Add(SyntaxFactory.IfStatement(
                               new JassParenthesizedExpressionSyntax(SyntaxFactory.BinaryNotEqualsExpression(SyntaxFactory.VariableReferenceExpression(VariableName.TrigUnit), JassNullLiteralExpressionSyntax.Value)),
                               ifBody.ToArray()));
            statements.Add(JassEmptyStatementSyntax.Value);

            var i = 0;
            var randomDistStatements = new List <IStatementSyntax>();

            foreach (var itemSet in itemSets)
            {
                randomDistStatements.Add(new JassCommentStatementSyntax($" Item set {i}"));
                randomDistStatements.Add(SyntaxFactory.CallStatement(FunctionName.RandomDistReset));

                var summedChance = 0;
                foreach (var item in itemSet.Items)
                {
                    if (RandomItemProvider.IsRandomItem(item.ItemId, out var itemClass, out var level))
                    {
                        if (chooseItemClass)
                        {
                            randomDistStatements.Add(SyntaxFactory.CallStatement(
                                                         FunctionName.RandomDistAddItem,
                                                         SyntaxFactory.InvocationExpression(
                                                             NativeName.ChooseRandomItemEx,
                                                             SyntaxFactory.VariableReferenceExpression(itemClass.GetVariableName()),
                                                             SyntaxFactory.LiteralExpression(level)),
                                                         SyntaxFactory.LiteralExpression(item.Chance)));
                        }
                        else
                        {
                            randomDistStatements.Add(SyntaxFactory.CallStatement(
                                                         FunctionName.RandomDistAddItem,
                                                         SyntaxFactory.InvocationExpression(NativeName.ChooseRandomItem, SyntaxFactory.LiteralExpression(level)),
                                                         SyntaxFactory.LiteralExpression(item.Chance)));
                        }
                    }