public void updateInstructionMap(ProcessContext context, ref ProcessedAs[] processMap) { if (context.instruction() == null) { return; } ProcessedAs currentPA = processMap[context.Index]; if (currentPA == ProcessedAs.CONSTANT) { return; } if (currentPA == ProcessedAs.DO_NOTHING) { if (returnTypes.Count > 0) { currentPA = processMap[context.Index] = ProcessedAs.QUERY; } if (command) { currentPA = processMap[context.Index] = ProcessedAs.COMMAND; } } int lastIndex = context.next(context.Index, false); for (int i = 0; i < parameters.Count; i++) { if (lastIndex >= context.Count) { //don't re-paint-process the instructions at the beginning break; } if (validParameter(parameters[i], context.instruction(lastIndex))) { if (parameters[i] == ReturnType.NONE && context.instruction(lastIndex).command) { processMap[lastIndex] = ProcessedAs.COMMAND; } else { processMap[lastIndex] = ProcessedAs.QUERY; } lastIndex = context.getLastParameterIndex(lastIndex); } else { processMap[lastIndex] = ProcessedAs.CONSTANT; } lastIndex = context.next(lastIndex, false); } }
public int getLastParameterIndex(ProcessContext context) { int lastIndex = context.next(); for (int i = 0; i < parameters.Count; i++) { if (validParameter(parameters[i], context.instruction(lastIndex))) { lastIndex = context.getLastParameterIndex(lastIndex); } lastIndex = context.next(lastIndex); } lastIndex = context.prev(lastIndex); return(lastIndex); }
public int[] getParameterIndices(ProcessContext context) { int[] paramIndices = new int[parameters.Count]; int paramIndex = context.next(); for (int i = 0; i < parameters.Count; i++) { paramIndices[i] = paramIndex; if (validParameter(parameters[i], context.instruction(paramIndex))) { paramIndex = context.getLastParameterIndex(paramIndex); } paramIndex = context.next(paramIndex); } return(paramIndices); }
// Update is called once per frame public void takeTurn() { //Process instructions moveDirection = Vector3.zero; int i = 0; while (i < instructions.Count) { Instruction inst = instructions[i]; if (inst != null) { ProcessContext context = new ProcessContext(this, i); inst.doAction(context); i = inst.getLastParameterIndex(context); i = context.next(i, false); } } moveDirection.x = (moveDirection.x == 0) ? 0 : Mathf.Sign(moveDirection.x); moveDirection.y = (moveDirection.y == 0) ? 0 : Mathf.Sign(moveDirection.y); moveDirection = transform.TransformDirection(moveDirection); transform.position = GridManager.moveObject(gameObject, transform.position + moveDirection); //If there are instructions to destroy, if (indicesToDestroy.Count > 0) { //Destroy them //doing it at this spot in the code //allows for simultaneous destruction foreach (int index in indicesToDestroy.Keys) { instructions[index] = indicesToDestroy[index]; } //If this bot has no more instructions, bool instFound = false; foreach (Instruction inst in instructions) { if (inst != null && inst.name != "BROKEN") { instFound = true; break; } } if (!instFound) { //Destroy this bot Destroy(gameObject); } indicesToDestroy.Clear(); } }