public void Setup(ActionSet actionSet) { if (IsSetup) { return; } IsSetup = true; SkipCount = Rule.DeltinScript.VarCollection.Assign("continueSkip", Rule.IsGlobal, true); TempHolder = Rule.DeltinScript.VarCollection.Assign("continueSkipTemp", Rule.IsGlobal, true); A_SkipIf skipAction = Element.Part <A_SkipIf> ( // Condition Element.Part <V_Compare>(SkipCount.GetVariable(), EnumData.GetEnumValue(Operators.Equal), new V_Number(0)), // Number of actions new V_Number(3) ); Skipper = new SkipStartMarker(actionSet); Skipper.SkipCount = TempHolder.GetVariable(); IActionList[] actions = ArrayBuilder <IActionList> .Build( new ALAction(A_Wait.MinimumWait), new ALAction(skipAction), new ALAction(TempHolder.SetVariable((Element)SkipCount.GetVariable())[0]), new ALAction(SkipCount.SetVariable(0)[0]), Skipper ); Rule.Actions.InsertRange(0, actions); }
protected override MethodResult Get() { IndexedVar player = IndexedVar.AssignInternalVar(TranslateContext.VarCollection, Scope, "OptimisedSphereHitbox: player", TranslateContext.IsGlobal); IndexedVar position = IndexedVar.AssignInternalVar(TranslateContext.VarCollection, Scope, "OptimisedSphereHitbox: position", TranslateContext.IsGlobal); Element[] actions = ArrayBuilder <Element> .Build ( player.SetVariable((Element)Parameters[0]), position.SetVariable((Element)Parameters[1]) ); Element radius = (Element)Parameters[2]; Element eyePos = Element.Part <V_EyePosition>(player.GetVariable()); Element range = Element.Part <V_DistanceBetween>(eyePos, position.GetVariable()); Element direction = Element.Part <V_FacingDirectionOf>(player.GetVariable()); Element raycast = Element.Part <V_RayCastHitPosition>(eyePos, ( eyePos + direction * range ), new V_AllPlayers(), new V_Null(), new V_False() ); Element distance = Element.Part <V_DistanceBetween>(position.GetVariable(), raycast); Element compare = distance <= radius; return(new MethodResult(actions, compare)); }
private Element[] CurrentIndex(Element targetPlayer, params Element[] setAtIndex) { return(ArrayBuilder <Element> .Build( StackLength() - 1, setAtIndex )); }
private Element[] CurrentIndex(Element targetPlayer, params Element[] setAtIndex) { return(ArrayBuilder <Element> .Build( Element.Part <V_CountOf>(base.Get(targetPlayer)) - 1, setAtIndex )); }
public override IWorkshopTree Get(ActionSet actionSet, IWorkshopTree[] parameterValues) { if (parameterValues[0] is V_Number n) { V_Number[] indexes = new V_Number[n.Value < 0 ? 0 : (int)n.Value]; for (int i = 0; i < indexes.Length; i++) { indexes[i] = new V_Number(i); } return(Element.CreateArray(indexes)); } else { IndexReference array = actionSet.VarCollection.Assign("_foreachArrayBuilder", actionSet.IsGlobal, false); IndexReference length = actionSet.VarCollection.Assign("_foreachArrayBuilderLength", actionSet.IsGlobal, true); IndexReference i = actionSet.VarCollection.Assign("_foreachArrayBuilderIndex", actionSet.IsGlobal, true); actionSet.AddAction(ArrayBuilder <Element> .Build( length.SetVariable((Element)parameterValues[0]), array.SetVariable(new V_EmptyArray()), i.SetVariable(0), Element.Part <A_While>((Element)i.GetVariable() < (Element)length.GetVariable()), array.SetVariable((Element)i.GetVariable(), null, (Element)i.GetVariable()), i.ModifyVariable(Operation.Add, 1), new A_End() )); return(array.GetVariable()); } }
public static void FromPathmapFile(string file) { PathMap map = PathMap.ImportFromXML(file); string baseEditorFile = Extras.CombinePathWithDotNotation(null, "!PathfindEditor.del"); string baseEditorContent = File.ReadAllText(baseEditorFile); Diagnostics diagnostics = new Diagnostics(); DeltinScript deltinScript = new DeltinScript( new FileGetter(null), diagnostics, new ScriptFile(diagnostics, new Uri(baseEditorFile), baseEditorContent), (varCollection) => { // Set the initial nodes. Rule initialNodes = new Rule("Initial Nodes"); initialNodes.Actions = ArrayBuilder <Element> .Build( WorkshopArrayBuilder.SetVariable(null, map.NodesAsWorkshopData(), null, LoadNodes, false), WorkshopArrayBuilder.SetVariable(null, map.SegmentsAsWorkshopData(), null, LoadSegments, false) ); return(new Rule[] { initialNodes }); } ); string code = deltinScript.WorkshopCode; if (code != null) { Program.WorkshopCodeResult(code); } else { Log.Write(LogLevel.Normal, new ColorMod("Build Failed.", ConsoleColor.Red)); diagnostics.PrintDiagnostics(Log); } }
public static DeltinScript Generate(string fileName, Pathmap map, OutputLanguage language) { string baseEditorFile = Extras.CombinePathWithDotNotation(null, "!PathfindEditor.del"); return(new DeltinScript(new TranslateSettings(baseEditorFile) { AdditionalRules = (varCollection) => { // Set the initial nodes. Rule initialNodes = new Rule("Initial Nodes"); initialNodes.Actions = ArrayBuilder <Element> .Build( // File name HUD. Element.Hud(text: new V_CustomString(fileName), sortOrder: 1, textColor: Color.Orange, location: HudLocation.Right), // Set nodes, segments, and attributes. WorkshopArrayBuilder.SetVariable(null, map.NodesAsWorkshopData(), null, LoadNodes, false), WorkshopArrayBuilder.SetVariable(null, map.SegmentsAsWorkshopData(), null, LoadSegments, false), WorkshopArrayBuilder.SetVariable(null, map.AttributesAsWorkshopData(), null, LoadAttributes, false) ); return new Rule[] { initialNodes }; }, OptimizeOutput = false, OutputLanguage = language })); }
private Rule GetUpdateRule() { // Once a node is reached during pathfinding, start traveling to the next node. // If the distance between the player and the current node is less than 0.4 meters away (1) // OR // the number of nodes is 2 or greater (2), the player is between the current node and the next node (3), and the player is in line of sight of the next node (4), // start traveling to the next node. (5) Element position = Element.Part <V_PositionOf>(new V_EventPlayer()); Rule rule = new Rule(Constants.INTERNAL_ELEMENT + "Pathfinder: Update", RuleEvent.OngoingPlayer); rule.Conditions = new Condition[] { new Condition( Element.Part <V_CountOf>(Path.GetVariable()), Operators.GreaterThan, 0 ), new Condition( Element.Part <V_Or>( // (1) new V_Compare( Element.Part <V_DistanceBetween>( NextPosition(new V_EventPlayer()), position ), Operators.LessThan, new V_Number(MoveToNext) ), Element.Part <V_And>( // (2) new V_Compare( Element.Part <V_CountOf>(Path.GetVariable()), Operators.Equal, new V_Number(2) ), Element.Part <V_And>( // (3) IsBetween(position, PositionAt(new V_EventPlayer(), 0), PositionAt(new V_EventPlayer(), 1)), // (4) Element.Part <V_IsInLineOfSight>(position + new V_Vector(0, 1.5, 0), PositionAt(new V_EventPlayer(), 1) + new V_Vector(0, 1.5, 0)) ) ) ) ) }; rule.Actions = ArrayBuilder <Element> .Build( LastUpdate.SetVariable(new V_TotalTimeElapsed()), Next(), // (5) DistanceToNext.SetVariable(Element.Part <V_DistanceBetween>(Element.Part <V_PositionOf>(new V_EventPlayer()), NextPosition(new V_EventPlayer()))), A_Wait.MinimumWait, new A_LoopIfConditionIsTrue() ); return(rule); }
public override IWorkshopTree Get(ActionSet actionSet, IWorkshopTree[] parameterValues) { // Setup the continue skip. ContinueSkip continueSkip = actionSet.ContinueSkip; continueSkip.Setup(actionSet); IndexReference result = actionSet.VarCollection.Assign($"_conditionTestResult", actionSet.IsGlobal, true); continueSkip.SetSkipCount(actionSet, continueSkip.GetSkipCount(actionSet) + 3); actionSet.AddAction(ArrayBuilder <Element> .Build( // This will continue at (0) if the rule loops. new A_LoopIfConditionIsFalse(), // Set the result to true. result.SetVariable(new V_True()), Element.Part <A_Skip>(new V_Number(1)), // The rule will loop back here (0) if false. result.SetVariable(new V_False()) )); continueSkip.ResetSkipCount(actionSet); if (TestingIfTrue) { return(result.GetVariable()); } else { return(Element.Part <V_Not>(result.GetVariable())); } }
protected override MethodResult Get() { IndexedVar player = TranslateContext.VarCollection.AssignVar(Scope, "OptimisedSphereHitbox: player", TranslateContext.IsGlobal, null); IndexedVar position = TranslateContext.VarCollection.AssignVar(Scope, "OptimisedSphereHitbox: position", TranslateContext.IsGlobal, null); Element[] actions = ArrayBuilder<Element>.Build ( player.SetVariable((Element)Parameters[0]), position.SetVariable((Element)Parameters[1]) ); Element radius = (Element)Parameters[2]; Element eyePos = Element.Part<V_EyePosition>(player.GetVariable()); Element range = Element.Part<V_DistanceBetween>(eyePos, position.GetVariable()); Element direction = Element.Part<V_FacingDirectionOf>(player.GetVariable()); Element raycast = Element.Part<V_RayCastHitPosition>(eyePos, Element.Part<V_Add>( eyePos, Element.Part<V_Multiply>(direction, range) ), new V_AllPlayers(), new V_Null(), new V_False() ); Element distance = Element.Part<V_DistanceBetween>(position.GetVariable(), raycast); Element compare = Element.Part<V_Compare>(distance, EnumData.GetEnumValue(Operators.LessThanOrEqual), radius); return new MethodResult(actions, compare); }
protected override MethodResult Get(PathfinderInfo info) { Element player = (Element)Parameters[0]; return(new MethodResult(ArrayBuilder <Element> .Build( info.Path.SetVariable(new V_EmptyArray(), player) ), null)); }
public override byte[] ToByteArray() { return(ArrayBuilder.Build <byte>( Varint.Encode(TypeId), Varint.Encode(X), Varint.Encode(Z) )); }
private Rule GetStartRule(DeltinScript deltinScript) { var condition = new Condition( Element.Part <V_CountOf>(Path.GetVariable()), Operators.GreaterThan, 0 ); Element eventPlayer = new V_EventPlayer(); Element eventPlayerPos = Element.Part <V_PositionOf>(eventPlayer); TranslateRule rule = new TranslateRule(deltinScript, Constants.INTERNAL_ELEMENT + "Pathfinder: Move", RuleEvent.OngoingPlayer); IfBuilder isBetween = new IfBuilder(rule.ActionSet, Element.Part <V_And>( Element.Part <V_CountOf>(Path.GetVariable()) >= 2, IsBetween(eventPlayerPos, NextPosition(eventPlayer), PositionAt(eventPlayer, 1)) ) ); isBetween.Setup(); rule.ActionSet.AddAction(Next()); isBetween.Finish(); rule.ActionSet.AddAction(ArrayBuilder <Element> .Build ( LastUpdate.SetVariable(new V_TotalTimeElapsed()), DistanceToNext.SetVariable(Element.Part <V_DistanceBetween>(Element.Part <V_PositionOf>(new V_EventPlayer()), NextPosition(new V_EventPlayer()))), // Element.Part<A_StartFacing>( // new V_EventPlayer(), // Element.Part<V_DirectionTowards>( // new V_EyePosition(), // NextPosition() // ), // new V_Number(700), // EnumData.GetEnumValue(Relative.ToWorld), // EnumData.GetEnumValue(FacingRev.DirectionAndTurnRate) // ), // Move to the next node. Element.Part <A_StartThrottleInDirection>( new V_EventPlayer(), Element.Part <V_DirectionTowards>( new V_EyePosition(), NextPosition(new V_EventPlayer()) // Because of ThrottleRev this will be reevaluated so 'Start Throttle In Direction' only needs to run once. ), new V_Number(1), EnumData.GetEnumValue(Relative.ToWorld), EnumData.GetEnumValue(ThrottleBehavior.ReplaceExistingThrottle), EnumData.GetEnumValue(ThrottleRev.DirectionAndMagnitude) ) )); var result = rule.GetRule(); result.Conditions = new Condition[] { condition }; return(result); }
override protected MethodResult Get(PathfinderInfo info) { Element player = (Element)Parameters[0]; TranslateContext.Actions.AddRange(ArrayBuilder <Element> .Build( Element.Part <A_Teleport>(player, info.NextPosition(player)) )); return(new MethodResult(null, null)); }
public override byte[] ToByteArray() { List <byte> buf = new List <byte>(); foreach (var e in EntityIds) { buf.AddRange(Varint.Encode(e)); } return(ArrayBuilder.Build <byte>( buf.ToArray(), Varint.Encode(X), Varint.Encode(Y))); }
public void SingleItem() { var builder = new ArrayBuilder <int>(5); builder.Add(3); var array = builder.Build(); Assert.Equal(1, array.Length); var enumerator = array.GetEnumerator(); Assert.True(enumerator.MoveNext()); Assert.Equal(3, enumerator.Current); }
protected override MethodResult Get() { IndexedVar player = TranslateContext.VarCollection.AssignVar(Scope, "IsAIUnintrusive: player", TranslateContext.IsGlobal, null); Element[] actions = ArrayBuilder <Element> .Build ( player.SetVariable((Element)Parameters[0]), Element.Part <A_Communicate>(player.GetVariable(), EnumData.GetEnumValue(Communication.VoiceLineUp)) ); Element result = Element.Part <V_Not>(Element.Part <V_IsCommunicating>(player.GetVariable(), EnumData.GetEnumValue(Communication.VoiceLineUp))); return(new MethodResult(actions, result)); }
private static ReadonlyArray <IConstraint <TState> > GetConstraints(Type type, BuilderContext <TState> context) { var array = new ArrayBuilder <IConstraint <TState> >(context.Builder.Constraints.Count); foreach (var builder in context.Builder.Constraints) { if (builder.Create(type, context) is IConstraint <TState> constraint) { array.Add(constraint); } } return(array.Build()); }
protected override MethodResult Get() { IndexedVar array = IndexedVar.AssignInternalVar(TranslateContext.VarCollection, Scope, "OptimisedRangeOfArray: array", TranslateContext.IsGlobal); Element[] actions = ArrayBuilder <Element> .Build ( array.SetVariable((Element)Parameters[0]) ); Element min = Element.Part <V_FirstOf>(Element.Part <V_SortedArray>(array.GetVariable(), new V_ArrayElement())); Element max = Element.Part <V_LastOf>(Element.Part <V_SortedArray>(array.GetVariable(), new V_ArrayElement())); return(new MethodResult(actions, max - min)); }
public void BuildTest() { byte[] first = new byte[] { 0, 1 }; byte[] second = new byte[] { 2, 3, 4 }; byte[] third = new byte[] { 5, 6, 7 }; var array = ArrayBuilder.Build <byte>(first, second, third); Assert.Equal(8, array.Length); for (int i = 0; i < 8; i++) { Assert.Equal(i, array[i]); } }
private static ReadonlyArray <PropertyValidator <TState> > GetPropertyValidators(Type type, BuilderContext <TState> context) { var properties = type.GetInstanceProperties(); var array = new ArrayBuilder <PropertyValidator <TState> >(properties.Length); foreach (var property in properties) { if (PropertyValidator <TState> .Create(property, context) is PropertyValidator <TState> validator) { array.Add(validator); } } return(array.Build()); }
protected override MethodResult Get() { IndexedVar fraction = TranslateContext.VarCollection.AssignVar(Scope, "OptimisedLinearInterpolate: fraction", TranslateContext.IsGlobal, null); Element[] actions = ArrayBuilder <Element> .Build ( fraction.SetVariable((Element)Parameters[2]) ); Element point1 = (Element)Parameters[0]; Element point2 = (Element)Parameters[1]; Element p1 = Element.Part <V_Multiply>(point1, Element.Part <V_Subtract>(new V_Number(1), fraction.GetVariable())); Element p2 = Element.Part <V_Multiply>(point2, fraction.GetVariable()); return(new MethodResult(actions, Element.Part <V_Add>(p1, p2))); }
protected override MethodResult Get() { Element effectArray = (Element)Parameters[0]; int destroyPerLoop = 1; if (Parameters[1] != null) { destroyPerLoop = (int)(double)((ConstantObject)Parameters[1]).Value; } List <Element> actions = new List <Element>(); IndexedVar index = TranslateContext.VarCollection.AssignVar(Scope, "DestroyEffectArray index", TranslateContext.IsGlobal, null); actions.AddRange(index.SetVariable(new V_Number(0))); Element[] destroyActions = new Element[destroyPerLoop]; for (int i = 0; i < destroyPerLoop; i++) { if (i == 0) { destroyActions[i] = Element.Part <A_DestroyEffect>(Element.Part <V_ValueInArray>(effectArray, index.GetVariable())); } else { destroyActions[i] = Element.Part <A_DestroyEffect>(Element.Part <V_ValueInArray>(effectArray, Element.Part <V_Add>(index.GetVariable(), new V_Number(i)))); } } actions.AddRange( Element.While( TranslateContext.ContinueSkip, new V_Compare( index.GetVariable(), Operators.LessThan, Element.Part <V_CountOf>(effectArray) ), ArrayBuilder <Element> .Build ( destroyActions, index.SetVariable(Element.Part <V_Add>(index.GetVariable(), new V_Number(destroyPerLoop))) ) ) ); return(new MethodResult(actions.ToArray(), null)); }
protected override MethodResult Get() { IndexedVar player = IndexedVar.AssignInternalVar(TranslateContext.VarCollection, Scope, "OptimisedIsOnScreen: player", TranslateContext.IsGlobal); IndexedVar point = IndexedVar.AssignInternalVar(TranslateContext.VarCollection, Scope, "OptimisedIsOnScreen: point", TranslateContext.IsGlobal); Element[] actions = ArrayBuilder <Element> .Build ( player.SetVariable((Element)Parameters[0]), point.SetVariable((Element)Parameters[1]) ); Element fov = (Element)Parameters[2]; Element los = Element.Part <V_IsInLineOfSight>(Element.Part <V_EyePosition>(player.GetVariable()), point.GetVariable()); Element angle = Element.Part <V_IsInViewAngle>(player.GetVariable(), point.GetVariable(), fov / 2); return(new MethodResult(actions, Element.Part <V_And>(los, angle))); }
protected override MethodResult Get() { IndexedVar point1 = TranslateContext.VarCollection.AssignVar(Scope, "OptimisedHorizontalDistance: point1", TranslateContext.IsGlobal, null); IndexedVar point2 = TranslateContext.VarCollection.AssignVar(Scope, "OptimisedHorizontalDistance: point2", TranslateContext.IsGlobal, null); Element[] actions = ArrayBuilder <Element> .Build ( point1.SetVariable((Element)Parameters[0]), point2.SetVariable((Element)Parameters[1]) ); Element x = Element.Part <V_Subtract>(Element.Part <V_XOf>(point1.GetVariable()), Element.Part <V_XOf>(point2.GetVariable())); Element z = Element.Part <V_Subtract>(Element.Part <V_ZOf>(point1.GetVariable()), Element.Part <V_ZOf>(point2.GetVariable())); Element sum = Element.Part <V_Add>(Element.Part <V_RaiseToPower>(x, new V_Number(2)), Element.Part <V_RaiseToPower>(z, new V_Number(2))); return(new MethodResult(actions, Element.Part <V_SquareRoot>(sum))); }
protected override MethodResult Get() { IndexedVar point1 = IndexedVar.AssignInternalVar(TranslateContext.VarCollection, Scope, "OptimisedHorizontalDistance: point1", TranslateContext.IsGlobal); IndexedVar point2 = IndexedVar.AssignInternalVar(TranslateContext.VarCollection, Scope, "OptimisedHorizontalDistance: point2", TranslateContext.IsGlobal); Element[] actions = ArrayBuilder <Element> .Build ( point1.SetVariable((Element)Parameters[0]), point2.SetVariable((Element)Parameters[1]) ); Element x = Element.XOf(point1.GetVariable()) - Element.XOf(point2.GetVariable()); Element z = Element.ZOf(point1.GetVariable()) - Element.ZOf(point2.GetVariable()); Element sum = Element.Part <V_RaiseToPower>(x, Element.Num(2)) + Element.Part <V_RaiseToPower>(z, Element.Num(2)); return(new MethodResult(actions, Element.Part <V_SquareRoot>(sum))); }
protected void Backtrack(Element destination, IndexReference finalPath) { actionSet.AddAction(current.SetVariable(destination)); actionSet.AddAction(finalPath.SetVariable(new V_EmptyArray())); // Get the path. actionSet.AddAction(Element.Part <A_While>(new V_Compare( current.GetVariable(), Operators.GreaterThanOrEqual, new V_Number(0) ))); // !WAIT actionSet.AddAction(A_Wait.MinimumWait); Element next = Nodes[(Element)current.GetVariable()]; Element array = (Element)finalPath.GetVariable(); Element first; Element second; if (!reversed) { first = next; second = array; } else { first = array; second = next; } // For debugging generated path. // actionSet.AddAction(Element.Part<A_CreateEffect>( // Element.Part<V_AllPlayers>(), // EnumData.GetEnumValue(Effect.Orb), // EnumData.GetEnumValue(Color.SkyBlue), // next, // new V_Number(0.5), // EnumData.GetEnumValue(EffectRev.VisibleTo) // )); actionSet.AddAction(ArrayBuilder <Element> .Build( finalPath.SetVariable(Element.Part <V_Append>(first, second)), current.SetVariable(Element.Part <V_ValueInArray>(parentArray.GetVariable(), current.GetVariable()) - 1) )); actionSet.AddAction(new A_End()); }
protected override MethodResult Get() { IndexedVar player = TranslateContext.VarCollection.AssignVar(Scope, "SetHealth: player", TranslateContext.IsGlobal, null); IndexedVar health = TranslateContext.VarCollection.AssignVar(Scope, "SetHealth: health", TranslateContext.IsGlobal, null); Element[] actions = ArrayBuilder <Element> .Build ( player.SetVariable((Element)Parameters[0]), health.SetVariable((Element)Parameters[1]), Element.Part <A_SkipIf>(Element.Part <V_Not>(Element.Part <V_Compare>(Element.Part <V_Health>(player.GetVariable()), EnumData.GetEnumValue(Operators.LessThan), health.GetVariable())), new V_Number(2)), Element.Part <A_Heal>(player.GetVariable(), new V_Null(), Element.Part <V_Subtract>(health.GetVariable(), Element.Part <V_Health>(player.GetVariable()))), Element.Part <A_Skip>(new V_Number(2)), Element.Part <A_SkipIf>(Element.Part <V_Not>(Element.Part <V_Compare>(Element.Part <V_Health>(player.GetVariable()), EnumData.GetEnumValue(Operators.GreaterThan), health.GetVariable())), new V_Number(1)), Element.Part <A_Damage>(player.GetVariable(), new V_Null(), Element.Part <V_Subtract>(Element.Part <V_Health>(player.GetVariable()), health.GetVariable())) ); return(new MethodResult(actions, null)); }
protected override MethodResult Get() { IndexedVar player = IndexedVar.AssignInternalVar(TranslateContext.VarCollection, Scope, "SetHealth: player", TranslateContext.IsGlobal); IndexedVar health = IndexedVar.AssignInternalVar(TranslateContext.VarCollection, Scope, "SetHealth: health", TranslateContext.IsGlobal); Element[] actions = ArrayBuilder <Element> .Build ( player.SetVariable((Element)Parameters[0]), health.SetVariable((Element)Parameters[1]), Element.Part <A_SkipIf>(!(Element.Part <V_Health>(player.GetVariable()) < health.GetVariable()), Element.Num(2)), Element.Part <A_Heal>(player.GetVariable(), new V_Null(), health.GetVariable() - Element.Part <V_Health>(player.GetVariable())), Element.Part <A_Skip>(Element.Num(2)), Element.Part <A_SkipIf>(!(Element.Part <V_Health>(player.GetVariable()) > health.GetVariable()), Element.Num(1)), Element.Part <A_Damage>(player.GetVariable(), new V_Null(), Element.Part <V_Health>(player.GetVariable()) - health.GetVariable()) ); return(new MethodResult(actions, null)); }
protected override MethodResult Get() { IndexedVar array = IndexedVar.AssignInternalVar(TranslateContext.VarCollection, Scope, "OptimisedSortedMedian: array", TranslateContext.IsGlobal); IndexedVar medianIndex = IndexedVar.AssignInternalVar(TranslateContext.VarCollection, Scope, "OptimisedSortedMedian: medianIndex", TranslateContext.IsGlobal); Element length = Element.Part <V_CountOf>(array.GetVariable()); Element condition = Element.Part <V_Compare>(length % 2, EnumData.GetEnumValue(Operators.Equal), new V_Number(0)); Element consequent = (Element.Part <V_ValueInArray>(array.GetVariable(), medianIndex.GetVariable() - 0.5) + Element.Part <V_ValueInArray>(array.GetVariable(), medianIndex.GetVariable() + 0.5)) / 2; Element alternative = Element.Part <V_ValueInArray>(array.GetVariable(), medianIndex.GetVariable()); Element[] actions = ArrayBuilder <Element> .Build ( array.SetVariable(Element.Part <V_SortedArray>((Element)Parameters[0], new V_ArrayElement())), medianIndex.SetVariable((length + 1) / 2) ); return(new MethodResult(actions, Element.TernaryConditional(condition, consequent, alternative, false))); }