Example #1
0
    // throttle forward at 50.
    public override float run(Core_Bot_Basic bot)
    {
        bot.SetThrottle(50);

        return(1f);
    }
    //-----------------------------------------------------------------------------------------------------
    // run the next instruction in my main_function (which maybe a nested one).
    //		returns the time until Step() should be called again.
    float Step()
    {
        if (callStack.Count == 0)
        {
            // last step we finished our root function.
            // start it up again!
            //	(later there could be more maintainance here)
            callStack.Push(new Frame(main_function));
        }

        // get the current frame:
        Frame curFrame = callStack.Peek();

        // get the current instruction in this frame's function:
        Instruction curInstruction = curFrame.function.subInstructions[curFrame.instructionPointer];

        // increment the frame's instructionPointer to the next code block in its function
        //	(we increment the counter BEFORE running the instruction)
        curFrame.instructionPointer++;

        BotVariable param = curInstruction.param;
        float       fParam;
        Vector3     v3Param;

        // run this instruction!:
        switch (curInstruction.type)
        {
        case InstructionType.Return:
            callStack.Pop();
            return(1F);

            break;

        case InstructionType.If:
            //TODO, requires conditionals.
            return(0F);

            break;

        case InstructionType.Throttle:
            fParam = FloatFromBotVariable(curInstruction.param);
            if (fParam != Mathf.NegativeInfinity)
            {
                bot.SetThrottle(fParam);
                return(1F);
            }
            break;

        case InstructionType.Break:
            fParam = FloatFromBotVariable(curInstruction.param);
            if (fParam != Mathf.NegativeInfinity)
            {
                bot.SetBreaks(fParam);
                return(1F);
            }
            break;

        case InstructionType.TurnTo:
            v3Param = LocationFromBotVariable(curInstruction.param);
            if (v3Param != null)
            {
                bot.TurnToward(v3Param, 2);
                return(1F);
            }

            break;

        case InstructionType.While:
            //////TODO, requires conditionals.

            /*
             * // if the condition fails, abort this function:
             * if( ! operation.operation(left, right))
             * {
             *      bot.processor.callStack.Pop ();
             *      return 1f;
             * }
             * // if the condition is true, make a new frame with this function,
             * // add it to the top of the callStack with the instruction pointer
             * // pointing AFTER the while.  Then move this frame's instruction pointer
             * // to point at this while again.  Result should be that it runs a dummy
             * // copy of this function, then returns to check this while again.
             * else {
             *      Frame thisFrame = bot.processor.callStack.Peek();
             *      bot.processor.callStack.Push(new Frame(thisFrame.function));
             *      bot.processor.callStack.Peek().instructionPointer++;
             *      thisFrame.instructionPointer--;
             *      return .5f;
             * }
             * //*/
            break;

        default:
            Debug.LogError("Unexpected instruction type!");
            break;
        }
        return(0f);
    }