protected static int ComparePriorities(TaskNode a, TaskNode b) { //return what a is compared to b (-1 is less than, 0 equal, 1 greater) double priorityA = a.CalcPriority(); double priorityB = b.CalcPriority(); if (priorityA > priorityB) { return(1); } if (priorityA < priorityB) { return(-1); } return(0); }
public override void Execute() { //sort children by priority children.Sort(ComparePriorities); bool onConcurrent = true; //assume the highest priority event is concurrent, now execute each child in descending priority order //until we've executed a non-concurrent one for (int i = children.Count - 1; onConcurrent == true && i >= 0; i--) { TaskNode current = children[i]; onConcurrent = current.Concurrent; current.Execute(); } }
//parses a Subtask's block of children and adds the children under Subtask private bool ParseLevel(Subtask parent, StreamReader sr) { if (DebugPrinting) { form1.WriteAI("DEBUG: Searching for child tasks in \"" + parent.ToString() + "\""); } char[] buffer = new char[1]; String temp; bool concurrent = false; //pass through every char until we reach something of interest (namely a child or >) while (sr.Peek() != -1 && (char)sr.Peek() != '}') { sr.Read(buffer, 0, 1); temp = new String(buffer); //decide what to do about the char read in switch (buffer[0]) { case '#': String comment = sr.ReadLine(); if (DebugPrinting) { form1.WriteAI("DEBUG: Skipping comment \"#" + comment + "\""); } if (comment.StartsWith("!")) { int t; if (Int32.TryParse(comment.Substring(1), out t)) { form1.setAIInterval(t); } form1.WriteAI("DEBUG: Setting AI Interval to " + t); } break; case '[': //makenode will move sr ahead to the next header in this block if (DebugPrinting) { form1.WriteAI("DEBUG: Encountered a child task in \"" + parent.ToString() + "\""); } TaskNode child = MakeNode(sr, concurrent); if (child == null) { form1.WriteAI("ERROR: Failed to create child task in \"" + parent.ToString() + "\""); return(false); } parent.children.Add(child); concurrent = false; break; case '*': concurrent = true; break; case '>': String path = form1.launchPath + "\\ai\\" + sr.ReadLine(); if (DebugPrinting) { form1.WriteAI("DEBUG: Including external file \"" + path + "\""); } FileStream fs; try { fs = File.Open(path, FileMode.Open); } catch (IOException) { form1.WriteAI("ERROR: Could not find included file \"" + path + "\""); return(false); } StreamReader tempStream = new StreamReader(fs); if (!ParseLevel(parent, tempStream)) { return(false); } break; default: if (DebugPrinting) { form1.WriteAI("DEBUG: Skipping character \"" + temp + "\""); form1.WriteAI("(" + ((int)(temp.ToCharArray()[0])).ToString() + ")"); } break; } } //for each char between things of interest //at the block ending char '}' return(true); }