Remote NPC Attack

Got any custom JavaScript additions/tweaks you think other people would like to see? Post 'em here!
Post Reply
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Remote NPC Attack

Post by Xuri »

By using the 'XATTACK command in this script, GMs can order NPC characters to attack a specific target. If they attack an innocent character, they will turn criminal & grey, so if a quest character attacks a player character, other player characters can fight the NPC without becoming criminals themselves.

To use this script, save it as xattack.js in UOX3\JS\COMMANDS\CUSTOM\, then open JSE_FILEASSOCIATIONS.SCP
and find the [COMMAND_SCRIPTS] sections. Scroll to the bottom of it, then add a line saying (for instance):

1051=commands/custom/xattack.js
(NOTE: Use a different (unique!) script-id if 1050 is already taken.)
// Remote NPC Attack
// v0.6
// by Xuri (xuri@uox3.org)
// Last Updated: 21/03/2021

// xattack lets GM characters command NPCs to attack other characters
// If the target being attacked is innocent (blue), the attacking NPC
// will become a criminal.
//
// endfight will take a targeted character and their immediate target out of combat,
// essentially ending the battle. This does not stop them from re-engaging after if they
// are hostile to each other, though!

function CommandRegistration()
{
    RegisterCommand( "xattack", 2, true );
    RegisterCommand( "endfight", 2, true );
}

function command_XATTACK( socket, cmdString )
{
    socket.CustomTarget( 0, "Which NPC should attack someone?" );
}

function onCallback0( socket, myTarget )
{
    if( !socket.GetWord( 1 ) && myTarget.isChar )
    {
        if( myTarget.npc )
        {
            socket.tempObj = myTarget;
            socket.CustomTarget( 1, "Which character should the NPC attack?", 1 );
        }
        else
            socket.SysMessage( "You can only order NPCs to attack." );
    }
    else
        socket.SysMessage( "You must target a character." );
}

function onCallback1( socket, myTarget )
{
    var myNPC = socket.tempObj;
    if( !ValidateObject( myNPC ))
    {
        socket.SysMessage( "Invalid NPC attacker!" );
        return;
    }

    if( !socket.GetWord( 1 ) && ValidateObject( myTarget ) && myTarget.isChar )
    {
        if( myTarget == myNPC )
        {
            socket.SysMessage( "You cannot order an NPC to attack themselves." );
            return;
        }

        if( !myTarget.vulnerable )
        {
            socket.SysMessage( "You cannot order an NPC to attack an invulnerable target." );
            return;
        }

        if( myNPC.InitiateCombat( myTarget ))
        {
            socket.SysMessage( "Combat successfully initiated." );
        }
        else
        {
            socket.SysMessage( "Failed to initiate combat between the two characters!" );
        }
    }
    else
        socket.SysMessage( "You must target a valid character." );
}

function command_ENDFIGHT( pSock, execString )
{
    pSock.CustomTarget( 14, "Subdue which fight?" );
}

function onCallback14( pSock, myTarget )
{
    var pUser = pSock.currentChar;
    if( !pSock.GetWord( 1 ) && myTarget.isChar )
    {
        if( myTarget.atWar == true )
        {
            var opponent = myTarget.target;
            opponent.target = null;
            opponent.atWar = 0;
            opponent.attacker = null;
            myTarget.target = null;
            myTarget.atWar = 0;
            myTarget.attacker = null;
            pUser.SysMessage( "Fight between the target character and their opponent has been subdued." );
        }
        else
            pUser.SysMessage( "That character is not in a fight." );
    }
    else
    {
        pUser.SysMessage( "You must select a valid character!" );
    }
}
-= Ho Eyo He Hum =-
Blue Dragon
UOX3 Apprentice
Posts: 159
Joined: Mon Jan 09, 2012 1:43 am
Has thanked: 0
Been thanked: 0

Post by Blue Dragon »

I found a little problem here too. The command allows you to make the npc kill himself, LOL. The second target has to be another character other than the first target.

Only this. In the remainder, the script is 100%. Very good!
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

Updated with 'endfight command and some minor fixes :)
-= Ho Eyo He Hum =-
Post Reply