Author Sticky: Post Working Scripts Here
Disgrace Posted: Sunday, 30 June 07:10AM
Script Name: Zero Based Action Queue

Description: Helps manages the actions assigned to an object

Notes: The only limitation of this method that I know of, is that if the actions are processed in an OnHeartBeat() event, the actions will overflow if any action takes longer than 6 seconds. By overflow, I mean the action may occur more than a single time.

NWScript:
////////////////////////////////////////////////////////////////////////////////

// ZERO BASED ACTION QUEUE

//

// Description: An action queue to manage actions assigned to objects.

//

// Usage:

//

// 1. When you want an object to perform a set of actions, add them to the queue

//     PushAction(ACTION_WALK_TO_STORE);

//     PushAction(ACTION_BUY_SOME_STUFF);

//

// 2. In a heartbeat or a UDE add your action specific code. NOTE: pay special

//    attention to the PopAction() call.

//

//    int nActionToPerform = GetActiveAction();

//    if (nActionToPerform == 0) return;

//    switch (nActionToPerform) {

//        case ACTION_WALK_TO_STORE: {

//            object oStore = GetObjectByTag("TAG_STORE");

//            ActionMoveToObject(oStore);

//            ActionDoCommand(PopAction());

//            break;

//        }

//        case ACTION_BUY_SOME_STUFF: {

//            yada yada...

//        }

//    }

//

//  3. The only limitation of this method that I know of, is that if the actions

//     are processed in an OnHeartBeat() event, the actions will overflow if

//     any action takes longer than 6 seconds. By overflow, I mean the action

//     may occur more than a single time.

//

// Author: Scott Woolcox.

//

// email me scott_woolcox@hotmail.com if you have a better solution, or find any

// problems.

////////////////////////////////////////////////////////////////////////////////

// define the event number you wish to use for execution in an OnUserDefined() event.

int ACTION_QUEUE_UDEVENT_NUMBER = 3000;

/**

 * Adds a new action to the bottom of the qeueu

 */

void PushAction(int nAction, object oTarget=OBJECT_SELF) {

    string sActions = GetLocalString(oTarget, "action_list");

    if (GetStringLength(sActions) == 0) {

        sActions = IntToString(nAction)+";";

    } else {

        sActions = sActions+IntToString(nAction)+";";

    }

    SetLocalString(oTarget, "action_list", sActions);

}

/**

 * Gets the current action that requires processing.

 */

int GetActiveAction(object oTarget=OBJECT_SELF) {

    string sAction = GetLocalString(oTarget, "activeaction");

    if (sAction == "") {

        string sActions = GetLocalString(oTarget, "action_list");

        if (sActions == "") return 0;

        int nIndex = FindSubString(sActions, ";");

        sAction = GetStringLeft(sActions, nIndex);

        if (sAction == "") {

            return 0;

        } else {

            SetLocalString(oTarget, "activeaction", sAction);

        }

    }

    return StringToInt(sAction);

}

/**

 * Removes a processed message from the queue

 */

void PopAction(object oTarget=OBJECT_SELF) {

    string sActions = GetLocalString(oTarget, "action_list");

    int nLen = GetStringLength(sActions);

    int nIndex = FindSubString(sActions, ";")+1;

    sActions = GetSubString(sActions, nIndex, nLen-nIndex);

    SetLocalString(oTarget, "action_list", sActions);

    DeleteLocalString(oTarget, "activeaction");

    SignalEvent(oTarget, EventUserDefined(ACTION_QUEUE_UDEVENT_NUMBER));

}