public static JassFunctionDeclarationSyntax CreateAllItems(MapUnits mapUnits) { const string LocalItemIdVariableName = "itemID"; var statements = new List <IStatementSyntax>(); statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(JassTypeSyntax.Integer, LocalItemIdVariableName)); foreach (var item in mapUnits.Units.Where(item => item.IsItem() && !item.IsPlayerStartLocation())) { if (item.IsRandomItem()) { var randomData = item.RandomData; switch (randomData) { case RandomUnitAny randomUnitAny: statements.Add(SyntaxFactory.SetStatement( LocalItemIdVariableName, SyntaxFactory.InvocationExpression( nameof(ChooseRandomItemEx), SyntaxFactory.InvocationExpression(nameof(ConvertItemType), SyntaxFactory.LiteralExpression((int)randomUnitAny.Class)), SyntaxFactory.LiteralExpression(randomUnitAny.Level)))); break; case RandomUnitGlobalTable randomUnitGlobalTable: break; case RandomUnitCustomTable randomUnitCustomTable: statements.Add(SyntaxFactory.CallStatement(nameof(War3Api.Blizzard.RandomDistReset))); var summedChance = 0; foreach (var randomItem in randomUnitCustomTable.RandomUnits) { IExpressionSyntax id = RandomItemProvider.IsRandomItem(randomItem.UnitId, out var itemClass, out var level) ? SyntaxFactory.InvocationExpression( nameof(ChooseRandomItemEx), SyntaxFactory.InvocationExpression(nameof(ConvertItemType), SyntaxFactory.LiteralExpression((int)itemClass)), SyntaxFactory.LiteralExpression(level)) : SyntaxFactory.FourCCLiteralExpression(randomItem.UnitId); statements.Add(SyntaxFactory.CallStatement( nameof(War3Api.Blizzard.RandomDistAddItem), id, SyntaxFactory.LiteralExpression(randomItem.Chance))); summedChance += randomItem.Chance; } if (summedChance < 100) { statements.Add(SyntaxFactory.CallStatement( nameof(War3Api.Blizzard.RandomDistAddItem), SyntaxFactory.LiteralExpression(-1), SyntaxFactory.LiteralExpression(100 - summedChance))); } statements.Add(SyntaxFactory.SetStatement( LocalItemIdVariableName, SyntaxFactory.InvocationExpression(nameof(War3Api.Blizzard.RandomDistChoose)))); break; default: break; } statements.Add(SyntaxFactory.IfStatement( SyntaxFactory.BinaryNotEqualsExpression(SyntaxFactory.VariableReferenceExpression(LocalItemIdVariableName), SyntaxFactory.LiteralExpression(-1)), SyntaxFactory.CallStatement( nameof(CreateItem), SyntaxFactory.VariableReferenceExpression(LocalItemIdVariableName), SyntaxFactory.LiteralExpression(item.Position.X), SyntaxFactory.LiteralExpression(item.Position.Y)))); } else { var args = new List <IExpressionSyntax>() { SyntaxFactory.FourCCLiteralExpression(item.TypeId), SyntaxFactory.LiteralExpression(item.Position.X), SyntaxFactory.LiteralExpression(item.Position.Y), }; var hasSkin = item.SkinId != 0 && item.SkinId != item.TypeId; if (hasSkin) { args.Add(SyntaxFactory.FourCCLiteralExpression(item.SkinId)); } statements.Add(SyntaxFactory.CallStatement(hasSkin ? nameof(BlzCreateItemWithSkin) : nameof(CreateItem), args.ToArray())); } } return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(CreateAllItems)), statements)); }
public static JassFunctionDeclarationSyntax InitRandomGroups(IEnumerable <RandomUnitTable> randomUnitTables) { const string LocalCurrentSetVariableName = "curset"; var statements = new List <IStatementSyntax>(); statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(JassTypeSyntax.Integer, LocalCurrentSetVariableName)); statements.Add(JassEmptyStatementSyntax.Value); foreach (var unitTable in randomUnitTables) { statements.Add(new JassCommentStatementSyntax($" Group {unitTable.Index} - {unitTable.Name}")); statements.Add(SyntaxFactory.CallStatement(nameof(War3Api.Blizzard.RandomDistReset))); for (var i = 0; i < unitTable.UnitSets.Count; i++) { statements.Add(SyntaxFactory.CallStatement( nameof(War3Api.Blizzard.RandomDistAddItem), SyntaxFactory.LiteralExpression(i), SyntaxFactory.LiteralExpression(unitTable.UnitSets[i].Chance))); } statements.Add(SyntaxFactory.SetStatement(LocalCurrentSetVariableName, SyntaxFactory.InvocationExpression(nameof(War3Api.Blizzard.RandomDistChoose)))); statements.Add(JassEmptyStatementSyntax.Value); var groupVarName = unitTable.GetVariableName(); var ifElseifBlocks = new List <(IExpressionSyntax Condition, IStatementSyntax[] Body)>(); for (var setIndex = 0; setIndex < unitTable.UnitSets.Count; setIndex++) { var set = unitTable.UnitSets[setIndex]; var condition = SyntaxFactory.BinaryEqualsExpression(SyntaxFactory.VariableReferenceExpression(LocalCurrentSetVariableName), SyntaxFactory.LiteralExpression(setIndex)); var bodyStatements = new List <IStatementSyntax>(); for (var position = 0; position < unitTable.Types.Count; position++) { var id = set?.UnitIds[position] ?? 0; var unitTypeExpression = RandomUnitProvider.IsRandomUnit(id, out var level) ? SyntaxFactory.InvocationExpression(nameof(ChooseRandomCreep), SyntaxFactory.LiteralExpression(level)) : id == 0 ? SyntaxFactory.LiteralExpression(-1) : SyntaxFactory.FourCCLiteralExpression(id); bodyStatements.Add(SyntaxFactory.SetStatement( groupVarName, SyntaxFactory.LiteralExpression(position), unitTypeExpression)); } ifElseifBlocks.Add((new JassParenthesizedExpressionSyntax(condition), bodyStatements.ToArray())); } var elseClauseStatements = new List <IStatementSyntax>(); for (var position = 0; position < unitTable.Types.Count; position++) { elseClauseStatements.Add(SyntaxFactory.SetStatement( groupVarName, SyntaxFactory.LiteralExpression(position), SyntaxFactory.LiteralExpression(-1))); } statements.Add(SyntaxFactory.IfStatement( ifElseifBlocks.First().Condition, SyntaxFactory.StatementList(ifElseifBlocks.First().Body), ifElseifBlocks.Skip(1).Select(elseIf => new JassElseIfClauseSyntax(elseIf.Condition, SyntaxFactory.StatementList(elseIf.Body))), new JassElseClauseSyntax(SyntaxFactory.StatementList(elseClauseStatements)))); statements.Add(JassEmptyStatementSyntax.Value); } return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(InitRandomGroups)), statements)); }
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))); } }
protected virtual JassFunctionDeclarationSyntax CreateAllItems(Map map) { if (map is null) { throw new ArgumentNullException(nameof(map)); } var mapUnits = map.Units; if (mapUnits is null) { throw new ArgumentException($"Function '{nameof(CreateAllItems)}' cannot be generated without {nameof(MapUnits)}.", nameof(map)); } var statements = new List <IStatementSyntax>(); statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(JassTypeSyntax.Integer, VariableName.ItemId)); foreach (var item in mapUnits.Units.Where(item => CreateAllItemsConditionSingleItem(map, item))) { if (item.IsRandomItem()) { var randomData = item.RandomData; switch (randomData) { case RandomUnitAny randomUnitAny: statements.Add(SyntaxFactory.SetStatement( VariableName.ItemId, SyntaxFactory.InvocationExpression( NativeName.ChooseRandomItemEx, SyntaxFactory.InvocationExpression(NativeName.ConvertItemType, SyntaxFactory.LiteralExpression((int)randomUnitAny.Class)), SyntaxFactory.LiteralExpression(randomUnitAny.Level)))); break; case RandomUnitGlobalTable randomUnitGlobalTable: break; case RandomUnitCustomTable randomUnitCustomTable: statements.Add(SyntaxFactory.CallStatement(FunctionName.RandomDistReset)); var summedChance = 0; foreach (var randomItem in randomUnitCustomTable.RandomUnits) { IExpressionSyntax id = RandomItemProvider.IsRandomItem(randomItem.UnitId, out var itemClass, out var level) ? SyntaxFactory.InvocationExpression( NativeName.ChooseRandomItemEx, SyntaxFactory.InvocationExpression(NativeName.ConvertItemType, SyntaxFactory.LiteralExpression((int)itemClass)), SyntaxFactory.LiteralExpression(level)) : SyntaxFactory.FourCCLiteralExpression(randomItem.UnitId); statements.Add(SyntaxFactory.CallStatement( FunctionName.RandomDistAddItem, id, SyntaxFactory.LiteralExpression(randomItem.Chance))); summedChance += randomItem.Chance; } if (summedChance < 100) { statements.Add(SyntaxFactory.CallStatement( FunctionName.RandomDistAddItem, SyntaxFactory.LiteralExpression(-1), SyntaxFactory.LiteralExpression(100 - summedChance))); } statements.Add(SyntaxFactory.SetStatement( VariableName.ItemId, SyntaxFactory.InvocationExpression(FunctionName.RandomDistChoose))); break; default: break; } statements.Add(SyntaxFactory.IfStatement( SyntaxFactory.BinaryNotEqualsExpression(SyntaxFactory.VariableReferenceExpression(VariableName.ItemId), SyntaxFactory.LiteralExpression(-1)), SyntaxFactory.CallStatement( NativeName.CreateItem, SyntaxFactory.VariableReferenceExpression(VariableName.ItemId), SyntaxFactory.LiteralExpression(item.Position.X), SyntaxFactory.LiteralExpression(item.Position.Y)))); } else { var args = new List <IExpressionSyntax>() { SyntaxFactory.FourCCLiteralExpression(item.TypeId), SyntaxFactory.LiteralExpression(item.Position.X), SyntaxFactory.LiteralExpression(item.Position.Y), }; var hasSkin = item.SkinId != 0 && item.SkinId != item.TypeId; if (hasSkin) { args.Add(SyntaxFactory.FourCCLiteralExpression(item.SkinId)); } statements.Add(SyntaxFactory.CallStatement(hasSkin ? NativeName.BlzCreateItemWithSkin : NativeName.CreateItem, args.ToArray())); } } return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(CreateAllItems)), statements)); }
protected virtual JassFunctionDeclarationSyntax InitRandomGroups(Map map) { if (map is null) { throw new ArgumentNullException(nameof(map)); } var randomUnitTables = map.Info.RandomUnitTables; if (randomUnitTables is null) { throw new ArgumentException($"Function '{nameof(InitRandomGroups)}' cannot be generated without {nameof(MapInfo.RandomUnitTables)}.", nameof(map)); } var statements = new List <IStatementSyntax>(); statements.Add(SyntaxFactory.LocalVariableDeclarationStatement(JassTypeSyntax.Integer, VariableName.CurrentSet)); statements.Add(JassEmptyStatementSyntax.Value); foreach (var unitTable in randomUnitTables) { statements.Add(new JassCommentStatementSyntax($" Group {unitTable.Index} - {unitTable.Name}")); statements.Add(SyntaxFactory.CallStatement(FunctionName.RandomDistReset)); for (var i = 0; i < unitTable.UnitSets.Count; i++) { statements.Add(SyntaxFactory.CallStatement( FunctionName.RandomDistAddItem, SyntaxFactory.LiteralExpression(i), SyntaxFactory.LiteralExpression(unitTable.UnitSets[i].Chance))); } statements.Add(SyntaxFactory.SetStatement(VariableName.CurrentSet, SyntaxFactory.InvocationExpression(FunctionName.RandomDistChoose))); statements.Add(JassEmptyStatementSyntax.Value); var groupVarName = unitTable.GetVariableName(); var ifElseifBlocks = new List <(IExpressionSyntax Condition, IStatementSyntax[] Body)>(); for (var setIndex = 0; setIndex < unitTable.UnitSets.Count; setIndex++) { var set = unitTable.UnitSets[setIndex]; var condition = SyntaxFactory.BinaryEqualsExpression(SyntaxFactory.VariableReferenceExpression(VariableName.CurrentSet), SyntaxFactory.LiteralExpression(setIndex)); var bodyStatements = new List <IStatementSyntax>(); for (var position = 0; position < unitTable.Types.Count; position++) { var id = set?.UnitIds[position] ?? 0; var unitTypeExpression = RandomUnitProvider.IsRandomUnit(id, out var level) ? SyntaxFactory.InvocationExpression(NativeName.ChooseRandomCreep, SyntaxFactory.LiteralExpression(level)) : id == 0 ? SyntaxFactory.LiteralExpression(-1) : SyntaxFactory.FourCCLiteralExpression(id); bodyStatements.Add(SyntaxFactory.SetStatement( groupVarName, SyntaxFactory.LiteralExpression(position), unitTypeExpression)); } ifElseifBlocks.Add((new JassParenthesizedExpressionSyntax(condition), bodyStatements.ToArray())); } var elseClauseStatements = new List <IStatementSyntax>(); for (var position = 0; position < unitTable.Types.Count; position++) { elseClauseStatements.Add(SyntaxFactory.SetStatement( groupVarName, SyntaxFactory.LiteralExpression(position), SyntaxFactory.LiteralExpression(-1))); } statements.Add(SyntaxFactory.IfStatement( ifElseifBlocks.First().Condition, SyntaxFactory.StatementList(ifElseifBlocks.First().Body), ifElseifBlocks.Skip(1).Select(elseIf => new JassElseIfClauseSyntax(elseIf.Condition, SyntaxFactory.StatementList(elseIf.Body))), new JassElseClauseSyntax(SyntaxFactory.StatementList(elseClauseStatements)))); statements.Add(JassEmptyStatementSyntax.Value); } return(SyntaxFactory.FunctionDeclaration(SyntaxFactory.FunctionDeclarator(nameof(InitRandomGroups)), statements)); }